* libbfd.c: bfd_put*: Remove casts to bfd_vma.
[binutils-gdb.git] / bfd / oasys.c
index 2c0102eccf10ae76cf6bc576880a9648eb3d59a5..201fedf3107fb4603572b6ec0524a93a68a3b69d 100644 (file)
@@ -1,6 +1,6 @@
-/* bfd backend for oasys objects.
-   Copyright (C) 1990-1991 Free Software Foundation, Inc.
-   Written by Steve Chamberlain of Cygnus Support <steve@cygnus.com>.
+/* BFD back-end for oasys objects.
+   Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
+   Written by Steve Chamberlain of Cygnus Support, <sac@cygnus.com>.
 
 This file is part of BFD, the Binary File Descriptor library.
 
@@ -18,17 +18,20 @@ You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
-/* $Id$ */
-
 #define UNDERSCORE_HACK 1
-#include <ansidecl.h>
-#include <sysdep.h>
-
 #include "bfd.h"
+#include "sysdep.h"
 #include "libbfd.h"
 #include "oasys.h"
 #include "liboasys.h"
 
+/* XXX - FIXME.  offsetof belongs in the system-specific files in
+   ../include/sys. */
+/* Define offsetof for those systems which lack it */
+
+#ifndef offsetof
+#define offsetof(type, identifier) (size_t) &(((type *) 0)->identifier) 
+#endif
 
 /* Read in all the section data and relocation stuff too */
 PROTO(static boolean,oasys_slurp_section_data,(bfd *CONST abfd));
@@ -76,7 +79,7 @@ DEFUN(oasys_slurp_symbol_table,(abfd),
     bfd * CONST abfd)
 {
   oasys_record_union_type record;
-  oasys_data_type *data = oasys_data(abfd);
+  oasys_data_type *data = OASYS_DATA(abfd);
   boolean loop = true;
   asymbol *dest_defined;
   asymbol *dest;
@@ -111,7 +114,7 @@ DEFUN(oasys_slurp_symbol_table,(abfd),
     case oasys_record_is_local_enum:
     case oasys_record_is_symbol_enum:
        {
-         int     flag = record.header.type == oasys_record_is_local_enum ?
+         int     flag = record.header.type == (int)oasys_record_is_local_enum ?
            (BSF_LOCAL) : (BSF_GLOBAL | BSF_EXPORT);
 
 
@@ -119,15 +122,16 @@ DEFUN(oasys_slurp_symbol_table,(abfd),
          switch (record.symbol.relb & RELOCATION_TYPE_BITS) {
          case RELOCATION_TYPE_ABS:
            dest = dest_defined--;
-           dest->section = 0;
-           dest->flags = BSF_ABSOLUTE | flag;
+           dest->section = &bfd_abs_section;
+           dest->flags =  0;
+           
            break;
          case RELOCATION_TYPE_REL:
            dest = dest_defined--;
            dest->section =
-             oasys_data(abfd)->sections[record.symbol.relb &
+             OASYS_DATA(abfd)->sections[record.symbol.relb &
                                         RELOCATION_SECT_BITS];
-           if (record.header.type == oasys_record_is_local_enum) 
+           if (record.header.type == (int)oasys_record_is_local_enum) 
                {
                  dest->flags = BSF_LOCAL;
                  if (dest->section ==(asection *)(~0)) {
@@ -144,16 +148,15 @@ DEFUN(oasys_slurp_symbol_table,(abfd),
            break;
          case RELOCATION_TYPE_UND:
            dest = data->symbols + bfd_h_get_16(abfd, (bfd_byte *)&record.symbol.refno[0]);
-           dest->section = (asection *)NULL;
-           dest->flags = BSF_UNDEFINED;
+           dest->section = &bfd_und_section;
            break;
          case RELOCATION_TYPE_COM:
            dest = dest_defined--;
            dest->name = string_ptr;
            dest->the_bfd = abfd;
 
-           dest->section = (asection *)NULL;
-           dest->flags = BSF_FORT_COMM;
+           dest->section = &bfd_com_section;
+
            break;
          default:
            dest = dest_defined--;
@@ -209,7 +212,7 @@ DEFUN(oasys_get_symtab,(abfd, location),
   if (oasys_slurp_symbol_table(abfd) == false) {
     return 0;
   }
-  symbase = oasys_data(abfd)->symbols;
+  symbase = OASYS_DATA(abfd)->symbols;
   for (counter = 0; counter < abfd->symcount; counter++) {
     *(location++) = symbase++;
   }
@@ -272,14 +275,14 @@ DEFUN(oasys_archive_p,(abfd),
       oasys_module_table_type record;
 
 
-      set_tdata(abfd, ar);
+      abfd->tdata.oasys_ar_data = ar;
       ar->module = module;
       ar->module_count = header.mod_count;
 
 
       filepos = header.mod_tbl_offset;
       for (i = 0; i < header.mod_count; i++) {
-        bfd_seek(abfd , filepos, SEEK_SET);
+        bfd_seek(abfd, filepos, SEEK_SET);
 
        /* There are two ways of specifying the archive header */
 
@@ -342,11 +345,8 @@ static boolean
 DEFUN(oasys_mkobject,(abfd),
       bfd *abfd)
 {
-  oasys_data_type *oasys;
 
-  set_tdata (abfd,
-    (oasys_data_type*)bfd_alloc(abfd, sizeof(oasys_data_type)));
-  oasys = oasys_data(abfd);
+  abfd->tdata.oasys_obj_data =    (oasys_data_type*)bfd_alloc(abfd, sizeof(oasys_data_type));
   return true;
 }
 
@@ -356,13 +356,13 @@ DEFUN(oasys_object_p,(abfd),
       bfd *abfd)
 {
   oasys_data_type *oasys;
-  oasys_data_type *save = oasys_data(abfd);
+  oasys_data_type *save = OASYS_DATA(abfd);
   boolean loop = true;
   boolean had_usefull = false;
 
-  set_tdata (abfd, 0);
+  abfd->tdata.oasys_obj_data = 0;
   oasys_mkobject(abfd);
-  oasys = oasys_data(abfd);
+  oasys = OASYS_DATA(abfd);
   memset((PTR)oasys->sections, 0xff, sizeof(oasys->sections));
     
   /* Point to the start of the file */
@@ -413,7 +413,7 @@ DEFUN(oasys_object_p,(abfd),
            BFD_FAIL();
          }
 
-         s->size  = bfd_h_get_32(abfd, (bfd_byte *) & record.section.value[0]) ;
+         s->_raw_size  = bfd_h_get_32(abfd, (bfd_byte *) & record.section.value[0]) ;
          s->vma = bfd_h_get_32(abfd, (bfd_byte *)&record.section.vma[0]);
          s->flags= 0;
          had_usefull = true;
@@ -437,8 +437,7 @@ DEFUN(oasys_object_p,(abfd),
     Oasys support several architectures, but I can't see a simple way
     to discover which one is in a particular file - we'll guess 
     */
-  abfd->obj_arch = bfd_arch_m68k;
-  abfd->obj_machine =0;
+  bfd_default_set_arch_mach(abfd, bfd_arch_m68k, 0);
   if (abfd->symcount != 0) {
     abfd->flags |= HAS_SYMS;
   }
@@ -454,7 +453,7 @@ DEFUN(oasys_object_p,(abfd),
 
  fail:
   (void)  bfd_release(abfd, oasys);
-  set_tdata (abfd, save);
+  abfd->tdata.oasys_obj_data = save;
   return (bfd_target *)NULL;
 }
 
@@ -464,19 +463,20 @@ DEFUN(oasys_print_symbol,(ignore_abfd, afile, symbol, how),
       bfd *ignore_abfd AND
       PTR afile AND
       asymbol *symbol AND
-      bfd_print_symbol_enum_type how)
+      bfd_print_symbol_type how)
 {
   FILE *file = (FILE *)afile;
 
   switch (how) {
-  case bfd_print_symbol_name_enum:
-  case bfd_print_symbol_type_enum:
+  case bfd_print_symbol_name:
+  case bfd_print_symbol_more:
     fprintf(file,"%s", symbol->name);
     break;
-  case bfd_print_symbol_all_enum:
+  case bfd_print_symbol_all:
+  case bfd_print_symbol_nm:
     {
-CONST      char *section_name = symbol->section == (asection *)NULL ?
-       "*abs" : symbol->section->name;
+      CONST char *section_name = symbol->section == (asection *)NULL ?
+       (CONST char *) "*abs" : symbol->section->name;
 
       bfd_print_symbol_vandf((PTR)file,symbol);
 
@@ -506,7 +506,7 @@ DEFUN(oasys_slurp_section_data,(abfd),
   bfd *CONST abfd)
 {
   oasys_record_union_type record;
-  oasys_data_type *data = oasys_data(abfd);
+  oasys_data_type *data = OASYS_DATA(abfd);
   boolean loop = true;
 
   oasys_per_section_type *per ;
@@ -547,7 +547,7 @@ DEFUN(oasys_slurp_section_data,(abfd),
 
              if (per->initialized == false) 
                  {
-                   per->data = (bfd_byte *) bfd_zalloc(abfd, section->size);
+                   per->data = (bfd_byte *) bfd_zalloc(abfd, section->_raw_size);
                    per->reloc_tail_ptr = (oasys_reloc_type **)&(section->relocation);
                    per->had_vma = false;
                    per->initialized = true;
@@ -618,9 +618,15 @@ DEFUN(oasys_slurp_section_data,(abfd),
                                    /* There is no symbol */
                                    r->symbol = 0;
                                    /* Work out the howto */
+                                   abort();
+#if 0
                                    r->relent.section =
-                                     data->sections[reloc & RELOCATION_SECT_BITS];
-                                   r->relent.addend = - r->relent.section->vma;
+                                     data->sections[reloc &
+                                                    RELOCATION_SECT_BITS];
+
+                                   r->relent.addend = -
+                                    r->relent.section->vma;
+#endif
                                    r->relent.address = dst_ptr - dst_base_ptr;
                                    r->relent.howto = &howto_table[reloc>>6];
                                    r->relent.sym_ptr_ptr = (asymbol **)NULL;
@@ -653,7 +659,12 @@ DEFUN(oasys_slurp_section_data,(abfd),
                                    /* Get symbol number */
                                    r->symbol = (src[0]<<8) | src[1];
                                    /* Work out the howto */
-                                   r->relent.section = (asection *)NULL;
+                                   abort();
+                                   
+#if 0
+                                   r->relent.section = (asection
+                                                        *)NULL;
+#endif
                                    r->relent.addend = 0;
                                    r->relent.address = dst_ptr - dst_base_ptr;
                                    r->relent.howto = &howto_table[reloc>>6];
@@ -752,8 +763,8 @@ DEFUN(oasys_get_section_contents,(abfd, section, location, offset, count),
 
 
 unsigned int
-DEFUN(oasys_canonicalize_reloc,(abfd, section, relptr, symbols),
-      bfd *abfd AND
+DEFUN(oasys_canonicalize_reloc,(ignore_abfd, section, relptr, symbols),
+      bfd *ignore_abfd AND
       sec_ptr section AND
       arelent **relptr AND
       asymbol **symbols)
@@ -761,10 +772,15 @@ DEFUN(oasys_canonicalize_reloc,(abfd, section, relptr, symbols),
   unsigned int reloc_count = 0;
   oasys_reloc_type *src = (oasys_reloc_type *)(section->relocation);
   while (src != (oasys_reloc_type *)NULL) {
+      abort();
+      
+#if 0
     if (src->relent.section == (asection *)NULL) 
        {
          src->relent.sym_ptr_ptr = symbols + src->symbol;
        }
+#endif
+
     *relptr ++ = &src->relent;
     src = src->next;
     reloc_count++;
@@ -774,17 +790,6 @@ DEFUN(oasys_canonicalize_reloc,(abfd, section, relptr, symbols),
 }
 
 
-boolean
-DEFUN(oasys_set_arch_mach, (abfd, arch, machine),
-      bfd *abfd AND
-      enum bfd_architecture arch AND
-      unsigned long machine)
-{
-  abfd->obj_arch = arch;
-  abfd->obj_machine = machine;
-  return true;
-}
-
 
 
 /* Writing */
@@ -802,7 +807,7 @@ DEFUN(oasys_write_record,(abfd, type, record, size),
   size_t i;
   uint8e_type *ptr;
   record->header.length = size;
-  record->header.type = type;
+  record->header.type = (int)type;
   record->header.check_sum = 0;
   record->header.fill = 0;
   ptr = &record->pad[0];
@@ -832,17 +837,17 @@ DEFUN(oasys_write_syms, (abfd),
     char *dst = symbol.name;
     unsigned int l = 0;
 
-    if (g->flags & BSF_FORT_COMM) {
+    if (bfd_is_com_section (g->section)) {
       symbol.relb = RELOCATION_TYPE_COM;
       bfd_h_put_16(abfd, index, (uint8e_type *)(&symbol.refno[0]));
       index++;
     }
-    else if (g->flags & BSF_ABSOLUTE) {
+    else if (g->section == & bfd_abs_section) {
       symbol.relb = RELOCATION_TYPE_ABS;
       bfd_h_put_16(abfd, 0, (uint8e_type *)(&symbol.refno[0]));
 
     }
-    else if (g->flags & BSF_UNDEFINED) {
+    else if (g->section == &bfd_und_section) {
       symbol.relb = RELOCATION_TYPE_UND ;
       bfd_h_put_16(abfd, index, (uint8e_type *)(&symbol.refno[0]));
       /* Overload the value field with the output index number */
@@ -864,6 +869,10 @@ DEFUN(oasys_write_syms, (abfd),
       }
       bfd_h_put_16(abfd, 0, (uint8e_type *)(&symbol.refno[0]));
     }
+#ifdef UNDERSCORE_HACK
+    if (src[l] == '_')
+      dst[l++] = '.';
+#endif
     while (src[l]) {
       dst[l] = src[l];
       l++;
@@ -904,7 +913,7 @@ DEFUN(oasys_write_sections, (abfd),
                                                    s->name);
        }
     out.relb = RELOCATION_TYPE_REL | s->target_index;
-    bfd_h_put_32(abfd, s->size, (bfd_byte *) out.value);
+    bfd_h_put_32(abfd, s->_cooked_size, (bfd_byte *) out.value);
     bfd_h_put_32(abfd, s->vma, (bfd_byte *) out.vma);
 
     oasys_write_record(abfd,
@@ -987,7 +996,7 @@ DEFUN(oasys_write_data, (abfd),
       unsigned int relocs_to_go = s->reloc_count;
       arelent **p = s->orelocation;
       if (s->reloc_count != 0) {
-       /* Sort the reloc records so it's easy to insert the relocs into the
+/* Sort the reloc records so it's easy to insert the relocs into the
           data */
     
        qsort(s->orelocation,
@@ -998,7 +1007,7 @@ DEFUN(oasys_write_data, (abfd),
       current_byte_index = 0;
       processed_data.relb = s->target_index | RELOCATION_TYPE_REL;
 
-      while (current_byte_index < s->size) 
+      while (current_byte_index < s->_cooked_size) 
          {
            /* Scan forwards by eight bytes or however much is left and see if
               there are any relocations going on */
@@ -1006,21 +1015,23 @@ DEFUN(oasys_write_data, (abfd),
            uint8e_type *dst = &processed_data.data[1];
 
            unsigned int i;
-           unsigned int long_length = 128;
+           *mod = 0;
 
 
-           bfd_h_put_32(abfd, s->vma + current_byte_index, processed_data.addr);
-           if ((size_t)(long_length + current_byte_index) > (size_t)(s->size)) {
-             long_length = s->size - current_byte_index;
-           }
-           while (long_length  > 0 &&  (dst - (uint8e_type*)&processed_data < 128)) {
-           
-             unsigned int length = long_length;
-             *mod =0;
-             if (length > 8)
-               length = 8;
+           bfd_h_put_32(abfd, s->vma + current_byte_index,
+                        processed_data.addr);
+
+           /* Don't start a relocation unless you're sure you can finish it
+              within the same data record.  The worst case relocation is a
+              4-byte relocatable value which is split across two modification
+              bytes (1 relocation byte + 2 symbol reference bytes + 2 data +
+              1 modification byte + 2 data = 8 bytes total).  That's where
+              the magic number 8 comes from.
+           */
+           while (current_byte_index < s->_raw_size && dst <=
+               (uint8e_type*)&processed_data.data[sizeof(processed_data.data)-8]) {
+           
 
-             for (i = 0; i < length; i++) {
                if (relocs_to_go != 0) {        
                  arelent *r = *p;
                  reloc_howto_type *CONST how=r->howto;
@@ -1057,6 +1068,9 @@ DEFUN(oasys_write_data, (abfd),
                  
                    /* Is this a section relative relocation, or a symbol
                       relative relocation ? */
+                   abort();
+       
+#if 0
                    if (r->section != (asection*)NULL) 
                        {
                          /* The relent has a section attached, so it must be section
@@ -1066,6 +1080,7 @@ DEFUN(oasys_write_data, (abfd),
                          *dst++ = rel_byte;
                        }
                    else 
+#endif
                        {
                          asymbol *p = *(r->sym_ptr_ptr);
 
@@ -1093,6 +1108,21 @@ DEFUN(oasys_write_data, (abfd),
                          }
 
                        }
+#define ADVANCE { if (++i >= 8) { i = 0; mod = dst++; *mod = 0; } current_byte_index++; }
+                   /* relocations never occur from an unloadable section,
+                      so we can assume that raw_data is not NULL
+                    */
+                   *dst++ = *raw_data++;
+                   ADVANCE
+                   *dst++ = *raw_data++;
+                   ADVANCE
+                   if (how->size == 2) {
+                     *dst++ = *raw_data++;
+                     ADVANCE
+                     *dst++ = *raw_data++;
+                     ADVANCE
+                   }
+                   continue;
                  }
                }
                /* If this is coming from an unloadable section then copy
@@ -1103,10 +1133,12 @@ DEFUN(oasys_write_data, (abfd),
                else {
                  *dst++ = *raw_data++;
                }
-               current_byte_index++;
-             }
-             mod = dst++;
-             long_length -= length;
+               ADVANCE
+           }
+
+           /* Don't write a useless null modification byte */
+           if (dst == mod+1) {
+             --dst;
            }
 
            oasys_write_record(abfd,
@@ -1120,7 +1152,7 @@ DEFUN(oasys_write_data, (abfd),
 }
 static boolean
 DEFUN(oasys_write_object_contents, (abfd),
-      bfd * CONST abfd)
+      bfd *abfd)
 {
   oasys_write_header(abfd);
   oasys_write_syms(abfd);
@@ -1150,7 +1182,7 @@ DEFUN(oasys_set_section_contents,(abfd, section, location, offset, count),
     if (oasys_per_section(section)->data == (bfd_byte *)NULL ) 
        {
          oasys_per_section(section)->data =
-           (bfd_byte *)(bfd_alloc(abfd,section->size));    
+           (bfd_byte *)(bfd_alloc(abfd,section->_cooked_size));    
        }
     (void) memcpy((PTR)(oasys_per_section(section)->data + offset),
                  location,
@@ -1190,7 +1222,7 @@ oasys_openr_next_archived_file(arch, prev)
 bfd *arch;
 bfd *prev;
 {
-  oasys_ar_data_type *ar = oasys_ar_data(arch);
+  oasys_ar_data_type *ar = OASYS_AR_DATA(arch);
   oasys_module_info_type *p;
   /* take the next one from the arch state, or reset */
   if (prev == (bfd *)NULL) {
@@ -1272,17 +1304,23 @@ return 0;
 #define oasys_write_armap 0
 #define oasys_get_lineno (struct lineno_cache_entry *(*)())bfd_nullvoidptr
 #define        oasys_close_and_cleanup         bfd_generic_close_and_cleanup
-
+#define oasys_set_arch_mach bfd_default_set_arch_mach
 #define oasys_bfd_debug_info_start bfd_void
 #define oasys_bfd_debug_info_end bfd_void
 #define oasys_bfd_debug_info_accumulate  (FOO(void, (*), (bfd *, asection *)))bfd_void
-
+#define oasys_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
+#define oasys_bfd_relax_section bfd_generic_relax_section
+#define oasys_bfd_seclet_link bfd_generic_seclet_link
+#define oasys_bfd_reloc_type_lookup \
+  ((CONST struct reloc_howto_struct *(*) PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr)
+#define oasys_bfd_make_debug_symbol \
+  ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
 
 /*SUPPRESS 460 */
 bfd_target oasys_vec =
 {
   "oasys",                     /* name */
-  bfd_target_oasys_flavour_enum,
+  bfd_target_oasys_flavour,
   true,                                /* target byte order */
   true,                                /* target headers byte order */
   (HAS_RELOC | EXEC_P |                /* object flags */
@@ -1290,6 +1328,7 @@ bfd_target oasys_vec =
    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
   (SEC_CODE|SEC_DATA|SEC_ROM|SEC_HAS_CONTENTS
    |SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
+   0,                          /* leading underscore */
   ' ',                         /* ar_pad_char */
   16,                          /* ar_max_namelen */
   1,                           /* minimum alignment */
@@ -1313,5 +1352,6 @@ bfd_target oasys_vec =
       _bfd_write_archive_contents,
       bfd_false,
     },
-  JUMP_TABLE(oasys)
+  JUMP_TABLE(oasys),
+  (PTR) 0
 };