bfd/
[deliverable/binutils-gdb.git] / bfd / oasys.c
index 995d962aee1f6647809313f38278cbd8c96fc473..c83d3ea257bb38830ca3002337489318ee6683a4 100644 (file)
-/*
-   
- bfd backend for oasys objects.
+/* BFD back-end for oasys objects.
+   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2001,
+   2002, 2003, 2004, 2005, 2006, 2007 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.
 
- Object files contain records in order:
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
 
- optional header
- symbol records
- section records
- data records
- debugging records
- end record
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
+   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., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
 
-
-   Written by Steve Chamberlain
-   steve@cygnus.com
-
-
-
- */
-
-
-#include <ansidecl.h>
+#define UNDERSCORE_HACK 1
 #include "sysdep.h"
 #include "bfd.h"
+#include "safe-ctype.h"
 #include "libbfd.h"
-#include "obstack.h"
 #include "oasys.h"
 #include "liboasys.h"
 
+/* Read in all the section data and relocation stuff too.  */
 
-
-#define obstack_chunk_alloc malloc
-#define obstack_chunk_free free
-
-typedef void generic_symbol_type;
-
-
-void DEFUN(oasys_read_record,(abfd, record),
-      bfd *abfd AND 
-      oasys_record_union_type *record)
+static bfd_boolean
+oasys_read_record (bfd *abfd, oasys_record_union_type *record)
 {
-
-  bfd_read(record, 1, sizeof(record->header), abfd);
-
-  bfd_read(((char *)record )+ sizeof(record->header),
-          1, record->header.length - sizeof(record->header),
-          abfd);
+  bfd_size_type amt = sizeof (record->header);
+
+  if (bfd_bread ((void *) record, amt, abfd) != amt)
+    return FALSE;
+
+  amt = record->header.length - sizeof (record->header);
+  if ((long) amt <= 0)
+    return TRUE;
+  if (bfd_bread ((void *) ((char *) record + sizeof (record->header)), amt, abfd)
+      != amt)
+    return FALSE;
+  return TRUE;
 }
+
 static size_t
-oasys_string_length(record)
-oasys_record_union_type *record;
+oasys_string_length (oasys_record_union_type *record)
 {
-return  record->header.length
-       - ((char *)record->symbol.name - (char *)record);
+  return record->header.length
+    - ((char *) record->symbol.name - (char *) record);
 }
 
-/*****************************************************************************/
-
-/*
+/* Slurp the symbol table by reading in all the records at the start file
+   till we get to the first section record.
 
-Slurp the symbol table by reading in all the records at the start file
-till we get to the first section record.
+   We'll sort the symbolss into  two lists, defined and undefined. The
+   undefined symbols will be placed into the table according to their
+   refno.
 
-We'll sort the symbols into  two lists, defined and undefined. The
-undefined symbols will also be sorted by refno. We do this by placing
-all undefined symbols at the front of the table moving in, and the
-defined symbols at the end of the table moving back.
+   We do this by placing all undefined symbols at the front of the table
+   moving in, and the defined symbols at the end of the table moving back.  */
 
-*/
-
-static boolean
-oasys_slurp_symbol_table(abfd)
-bfd *abfd;
+static bfd_boolean
+oasys_slurp_symbol_table (bfd *const abfd)
 {
   oasys_record_union_type record;
-  oasys_data_type *data = oasys_data(abfd);
-  boolean loop = true;
-  asymbol *dest_undefined;
+  oasys_data_type *data = OASYS_DATA (abfd);
+  bfd_boolean loop = TRUE;
   asymbol *dest_defined;
   asymbol *dest;
   char *string_ptr;
+  bfd_size_type amt;
 
+  if (data->symbols != NULL)
+    return TRUE;
 
-  if (data->symbols != (asymbol *)NULL) {
-    return true;
-  }
-  /* Buy enough memory for all the symbols and all the names */
-  data->symbols = 
-    (asymbol *)malloc(sizeof(asymbol) * abfd->symcount);
-  data->strings = malloc(data->symbol_string_length);
+  /* Buy enough memory for all the symbols and all the names.  */
+  amt = abfd->symcount;
+  amt *= sizeof (asymbol);
+  data->symbols = bfd_alloc (abfd, amt);
+
+  amt = data->symbol_string_length;
+#ifdef UNDERSCORE_HACK
+  /* Buy 1 more char for each symbol to keep the underscore in.  */
+  amt += abfd->symcount;
+#endif
+  data->strings = bfd_alloc (abfd, amt);
 
-  dest_undefined = data->symbols;
-  dest_defined = data->symbols + abfd->symcount -1;
+  if (!data->symbols || !data->strings)
+    return FALSE;
+
+  dest_defined = data->symbols + abfd->symcount - 1;
 
   string_ptr = data->strings;
-  bfd_seek(abfd, (file_ptr)0, SEEK_SET);
-  while (loop) {
-    oasys_read_record(abfd, &record);
-    switch (record.header.type) {
-    case oasys_record_is_header_enum:
-      break;
-    case oasys_record_is_local_enum:
-    case oasys_record_is_symbol_enum:
-       {
-         size_t length = oasys_string_length(&record);
-         switch (record.symbol.relb[0] & RELOCATION_TYPE_BITS) {
-         case RELOCATION_TYPE_ABS:
-           dest = dest_defined--;
-           dest->section = 0;
-           dest->flags = BSF_ABSOLUTE | BSF_EXPORT | BSF_GLOBAL;
-           dest_defined--;
-           break;
-         case RELOCATION_TYPE_REL:
-           dest = dest_defined--;
-           dest->section =
-             oasys_data(abfd)->sections[record.symbol.relb[0] &
-                                        RELOCATION_SECT_BITS];
-           if (record.header.type == oasys_record_is_local_enum) 
-               {
-                 dest->flags =  BSF_LOCAL;
-               }
-           else {
+  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
+    return FALSE;
+  while (loop)
+    {
+      if (! oasys_read_record (abfd, &record))
+       return FALSE;
 
-             dest->flags = BSF_EXPORT | BSF_GLOBAL;
-           }
-           break;
-         case RELOCATION_TYPE_UND:
-           dest = dest_undefined++;
-           dest->section = (asection *)NULL;
-           dest->flags = BSF_UNDEFINED;
-           break;
-         case RELOCATION_TYPE_COM:
-           dest = dest_defined--;
+      switch (record.header.type)
+       {
+       case oasys_record_is_header_enum:
+         break;
+       case oasys_record_is_local_enum:
+       case oasys_record_is_symbol_enum:
+         {
+           int flag = record.header.type == (int) oasys_record_is_local_enum ?
+           (BSF_LOCAL) : (BSF_GLOBAL | BSF_EXPORT);
+
+           size_t length = oasys_string_length (&record);
+           switch (record.symbol.relb & RELOCATION_TYPE_BITS)
+             {
+             case RELOCATION_TYPE_ABS:
+               dest = dest_defined--;
+               dest->section = bfd_abs_section_ptr;
+               dest->flags = 0;
+
+               break;
+             case RELOCATION_TYPE_REL:
+               dest = dest_defined--;
+               dest->section =
+                 OASYS_DATA (abfd)->sections[record.symbol.relb &
+                                             RELOCATION_SECT_BITS];
+               if (record.header.type == (int) oasys_record_is_local_enum)
+                 {
+                   dest->flags = BSF_LOCAL;
+                   if (dest->section == (asection *) (~0))
+                     {
+                       /* It seems that sometimes internal symbols are tied up, but
+                      still get output, even though there is no
+                      section */
+                       dest->section = 0;
+                     }
+                 }
+               else
+                 dest->flags = flag;
+               break;
+             case RELOCATION_TYPE_UND:
+               dest = data->symbols + H_GET_16 (abfd, record.symbol.refno);
+               dest->section = bfd_und_section_ptr;
+               break;
+             case RELOCATION_TYPE_COM:
+               dest = dest_defined--;
+               dest->name = string_ptr;
+               dest->the_bfd = abfd;
+               dest->section = bfd_com_section_ptr;
+               break;
+             default:
+               dest = dest_defined--;
+               BFD_ASSERT (FALSE);
+               break;
+             }
            dest->name = string_ptr;
            dest->the_bfd = abfd;
+           dest->udata.p = NULL;
+           dest->value = H_GET_32 (abfd, record.symbol.value);
+
+#ifdef UNDERSCORE_HACK
+           if (record.symbol.name[0] != '_')
+             {
+               string_ptr[0] = '_';
+               string_ptr++;
+             }
+#endif
+           memcpy (string_ptr, record.symbol.name, length);
 
-           dest->section = (asection *)NULL;
-           dest->flags = BSF_FORT_COMM;
-           break;
+           string_ptr[length] = 0;
+           string_ptr += length + 1;
          }
-         dest->name = string_ptr;
-         dest->the_bfd = abfd;
-
-         dest->value = bfd_h_getlong(abfd, &record.symbol.value);
-         memcpy(string_ptr, record.symbol.name, length);
-         string_ptr[length] =0;
-         string_ptr += length +1;
+         break;
+       default:
+         loop = FALSE;
        }
-      break;
-    default:
-      loop = false;
     }
-  }
-  return true;
-
+  return TRUE;
 }
 
-size_t
-oasys_get_symtab_upper_bound (abfd)
-bfd *abfd;
+static long
+oasys_get_symtab_upper_bound (bfd *const abfd)
 {
-  oasys_slurp_symbol_table (abfd);
+  if (! oasys_slurp_symbol_table (abfd))
+    return -1;
 
-  return (abfd->symcount != 0) ? 
-    (abfd->symcount+1) * (sizeof (oasys_symbol_type *)) : 0;
+  return (abfd->symcount + 1) * (sizeof (oasys_symbol_type *));
 }
 
-/* 
-*/
-
-extern bfd_target oasys_vec;
+extern const bfd_target oasys_vec;
 
-unsigned int
-oasys_get_symtab (abfd, location)
-bfd *abfd;
-asymbol **location;
+static long
+oasys_canonicalize_symtab (bfd *abfd, asymbol **location)
 {
-  asymbol *symbase ;
-  unsigned int counter ;
-  if (oasys_slurp_symbol_table(abfd) == false) {
-    return 0;
-  }
-  symbase = oasys_data(abfd)->symbols;
-  for (counter = 0; counter < abfd->symcount; counter++) {
+  asymbol *symbase;
+  unsigned int counter;
+
+  if (! oasys_slurp_symbol_table (abfd))
+    return -1;
+
+  symbase = OASYS_DATA (abfd)->symbols;
+  for (counter = 0; counter < abfd->symcount; counter++)
     *(location++) = symbase++;
-  }
+
   *location = 0;
   return abfd->symcount;
 }
 
-/***********************************************************************
-*  archive stuff 
-*/
-#define swap(x) x = bfd_h_get_x(abfd, &x);
-bfd_target *
-oasys_archive_p(abfd)
-bfd *abfd;
+/* Archive stuff.  */
+
+static const bfd_target *
+oasys_archive_p (bfd *abfd)
 {
   oasys_archive_header_type header;
+  oasys_extarchive_header_type header_ext;
   unsigned int i;
-  
-  bfd_seek(abfd, (file_ptr) 0, false);
-
-  
-  bfd_read(&header, 1, sizeof(header), abfd);
-
-  
-  swap(header.version);
-  swap(header.mod_count);
-  swap(header.mod_tbl_offset);
-  swap(header.sym_tbl_size);
-  swap(header.sym_count);
-  swap(header.sym_tbl_offset);
-  swap(header.xref_count);
-  swap(header.xref_lst_offset);
-
-  /*
-     There isn't a magic number in an Oasys archive, so the best we
-     can do to verify reasnableness is to make sure that the values in
-     the header are too weird
-     */
-
-  if (header.version>10000 ||
-      header.mod_count>10000 ||
-      header.sym_count>100000 ||
-      header.xref_count > 100000) return (bfd_target *)NULL;
-
-  /*
-     That all worked, lets buy the space for the header and read in
-     the headers.
-     */
+  file_ptr filepos;
+  bfd_size_type amt;
+
+  amt = sizeof (header_ext);
+  if (bfd_seek (abfd, (file_ptr) 0, 0) != 0
+      || bfd_bread ((void *) &header_ext, amt, abfd) != amt)
+    {
+      if (bfd_get_error () != bfd_error_system_call)
+       bfd_set_error (bfd_error_wrong_format);
+      return NULL;
+    }
+
+  header.version = H_GET_32 (abfd, header_ext.version);
+  header.mod_count = H_GET_32 (abfd, header_ext.mod_count);
+  header.mod_tbl_offset = H_GET_32 (abfd, header_ext.mod_tbl_offset);
+  header.sym_tbl_size = H_GET_32 (abfd, header_ext.sym_tbl_size);
+  header.sym_count = H_GET_32 (abfd, header_ext.sym_count);
+  header.sym_tbl_offset = H_GET_32 (abfd, header_ext.sym_tbl_offset);
+  header.xref_count = H_GET_32 (abfd, header_ext.xref_count);
+  header.xref_lst_offset = H_GET_32 (abfd, header_ext.xref_lst_offset);
+
+  /* There isn't a magic number in an Oasys archive, so the best we
+     can do to verify reasonableness is to make sure that the values in
+     the header are too weird.  */
+
+  if (header.version > 10000
+      || header.mod_count > 10000
+      || header.sym_count > 100000
+      || header.xref_count > 100000)
+    return NULL;
+
+  /* That all worked, let's buy the space for the header and read in
+     the headers.  */
   {
-    oasys_ar_data_type *ar =
-      (oasys_ar_data_type*) malloc(sizeof(oasys_ar_data_type));
+    oasys_ar_data_type *ar;
+    oasys_module_info_type *module;
+    oasys_module_table_type record;
 
+    amt = sizeof (oasys_ar_data_type);
+    ar = bfd_alloc (abfd, amt);
 
-    oasys_module_info_type *module = 
-      (oasys_module_info_type*)
-       malloc(sizeof(oasys_module_info_type) * header.mod_count);
+    amt = header.mod_count;
+    amt *= sizeof (oasys_module_info_type);
+    module = bfd_alloc (abfd, amt);
 
-    oasys_module_table_type record;
+    if (!ar || !module)
+      return NULL;
 
-    oasys_ar_data(abfd) =ar;
+    abfd->tdata.oasys_ar_data = ar;
     ar->module = module;
     ar->module_count = header.mod_count;
 
-    bfd_seek(abfd , header.mod_tbl_offset, SEEK_SET);
-    for (i = 0; i < header.mod_count; i++) {
-
-      bfd_read(&record, 1, sizeof(record), abfd);
-      swap(record.mod_size);
-      swap(record.file_offset);
-      swap(record.mod_name_length);
-      module[i].name = malloc(record.mod_name_length+1);
+    filepos = header.mod_tbl_offset;
+    for (i = 0; i < header.mod_count; i++)
+      {
+       if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
+         return NULL;
 
-      bfd_read(module[i].name, 1, record.mod_name_length +1, abfd);
-      /* SKip some stuff */
-      bfd_seek(abfd, record.dep_count * sizeof(int32_type),
-           SEEK_CUR);
+       /* There are two ways of specifying the archive header.  */
+         {
+           oasys_extmodule_table_type_b_type record_ext;
+
+           amt = sizeof (record_ext);
+           if (bfd_bread ((void *) &record_ext, amt, abfd) != amt)
+             return NULL;
+
+           record.mod_size = H_GET_32 (abfd, record_ext.mod_size);
+           record.file_offset = H_GET_32 (abfd, record_ext.file_offset);
+
+           record.dep_count = H_GET_32 (abfd, record_ext.dep_count);
+           record.depee_count = H_GET_32 (abfd, record_ext.depee_count);
+           record.sect_count = H_GET_32 (abfd, record_ext.sect_count);
+           record.module_name_size = H_GET_32 (abfd,
+                                               record_ext.mod_name_length);
+
+           amt = record.module_name_size;
+           module[i].name = bfd_alloc (abfd, amt + 1);
+           if (!module[i].name)
+             return NULL;
+           if (bfd_bread ((void *) module[i].name, amt, abfd) != amt)
+             return NULL;
+           module[i].name[record.module_name_size] = 0;
+           filepos += (sizeof (record_ext)
+                       + record.dep_count * 4
+                       + record.module_name_size + 1);
+         }
 
-      module[i].size = record.mod_size;
-      module[i].pos = record.file_offset;
-    }
-      
+       module[i].size = record.mod_size;
+       module[i].pos = record.file_offset;
+       module[i].abfd = 0;
+      }
   }
   return abfd->xvec;
 }
 
-#define MAX_SECS 16
-bfd_target *
-oasys_object_p (abfd)
-bfd *abfd;
+static bfd_boolean
+oasys_mkobject (bfd *abfd)
 {
-  oasys_data_type *oasys;
-  oasys_data_type static_data;
+  bfd_size_type amt = sizeof (oasys_data_type);
 
-  boolean loop = true;
+  abfd->tdata.oasys_obj_data = bfd_alloc (abfd, amt);
 
+  return abfd->tdata.oasys_obj_data != NULL;
+}
 
-  boolean had_usefull = false;
+/* The howto table is build using the top two bits of a reloc byte to
+   index into it. The bits are PCREL,WORD/LONG.  */
 
-  memset((PTR)static_data.sections, 0xff, sizeof(static_data.sections));
-    
-  /* Point to the start of the file */
-  bfd_seek(abfd, (file_ptr)0, SEEK_SET);
-  static_data.symbol_string_length = 0;
-  /* Inspect the records, but only keep the section info -
-     remember the size of the symbols
-     */
-  while (loop) {
-    oasys_record_union_type record;
-    oasys_read_record(abfd, &record);
-    if (record.header.length < sizeof(record.header))
-      return (bfd_target *)NULL;
-
-    switch ((oasys_record_enum_type)(record.header.type)) {
-    case oasys_record_is_header_enum:
-      had_usefull = true;
-      break;
-    case oasys_record_is_symbol_enum:
-    case oasys_record_is_local_enum:
-      /* Count symbols and remember their size for a future malloc   */
-      abfd->symcount++;
-      static_data.symbol_string_length += 1 + oasys_string_length(&record);
-      had_usefull = true;
-      break;
-    case oasys_record_is_section_enum:
-      {
-       asection *s;
-       char *buffer;
-       unsigned int section_number;
-       if (record.section.header.length != sizeof(record.section))
+static reloc_howto_type howto_table[] =
+{
+
+  HOWTO (0, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, 0, "abs16",   TRUE, 0x0000ffff, 0x0000ffff, FALSE),
+  HOWTO (0, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "abs32",   TRUE, 0xffffffff, 0xffffffff, FALSE),
+  HOWTO (0, 0, 1, 16, TRUE,  0, complain_overflow_signed,   0, "pcrel16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
+  HOWTO (0, 0, 2, 32, TRUE,  0, complain_overflow_signed,   0, "pcrel32", TRUE, 0xffffffff, 0xffffffff, FALSE)
+};
+
+/* Read in all the section data and relocation stuff too.  */
+
+static bfd_boolean
+oasys_slurp_section_data (bfd *const abfd)
+{
+  oasys_record_union_type record;
+  oasys_data_type *data = OASYS_DATA (abfd);
+  bfd_boolean loop = TRUE;
+  oasys_per_section_type *per;
+  asection *s;
+  bfd_size_type amt;
+
+  /* See if the data has been slurped already.  */
+  for (s = abfd->sections; s != NULL; s = s->next)
+    {
+      per = oasys_per_section (s);
+      if (per->initialized)
+       return TRUE;
+    }
+
+  if (data->first_data_record == 0)
+    return TRUE;
+
+  if (bfd_seek (abfd, data->first_data_record, SEEK_SET) != 0)
+    return FALSE;
+
+  while (loop)
+    {
+      if (! oasys_read_record (abfd, &record))
+       return FALSE;
+
+      switch (record.header.type)
+       {
+       case oasys_record_is_header_enum:
+         break;
+       case oasys_record_is_data_enum:
          {
-           return (bfd_target *)NULL;
+           bfd_byte *src = record.data.data;
+           bfd_byte *end_src = ((bfd_byte *) & record) + record.header.length;
+           bfd_byte *dst_ptr;
+           bfd_byte *dst_base_ptr;
+           unsigned int relbit;
+           unsigned int count;
+           asection *section =
+           data->sections[record.data.relb & RELOCATION_SECT_BITS];
+           bfd_vma dst_offset;
+
+           per = oasys_per_section (section);
+
+           if (! per->initialized)
+             {
+               arelent **relpp;
+
+               per->data = bfd_zalloc (abfd, section->size);
+               if (!per->data)
+                 return FALSE;
+               relpp = &section->relocation;
+               per->reloc_tail_ptr = (oasys_reloc_type **) relpp;
+               per->had_vma = FALSE;
+               per->initialized = TRUE;
+               section->reloc_count = 0;
+               section->flags = SEC_ALLOC;
+             }
+
+           dst_offset = H_GET_32 (abfd, record.data.addr);
+           if (! per->had_vma)
+             {
+               /* Take the first vma we see as the base.  */
+               section->vma = dst_offset;
+               per->had_vma = TRUE;
+             }
+
+           dst_offset -= section->vma;
+
+           dst_base_ptr = oasys_per_section (section)->data;
+           dst_ptr = oasys_per_section (section)->data +
+             dst_offset;
+
+           if (src < end_src)
+             section->flags |= SEC_LOAD | SEC_HAS_CONTENTS;
+
+           while (src < end_src)
+             {
+               unsigned char mod_byte = *src++;
+               size_t gap = end_src - src;
+
+               count = 8;
+               if (mod_byte == 0 && gap >= 8)
+                 {
+                   dst_ptr[0] = src[0];
+                   dst_ptr[1] = src[1];
+                   dst_ptr[2] = src[2];
+                   dst_ptr[3] = src[3];
+                   dst_ptr[4] = src[4];
+                   dst_ptr[5] = src[5];
+                   dst_ptr[6] = src[6];
+                   dst_ptr[7] = src[7];
+                   dst_ptr += 8;
+                   src += 8;
+                 }
+               else
+                 {
+                   for (relbit = 1; count-- != 0 && src < end_src; relbit <<= 1)
+                     {
+                       if (relbit & mod_byte)
+                         {
+                           unsigned char reloc = *src;
+                           /* This item needs to be relocated.  */
+                           switch (reloc & RELOCATION_TYPE_BITS)
+                             {
+                             case RELOCATION_TYPE_ABS:
+                               break;
+
+                             case RELOCATION_TYPE_REL:
+                               {
+                                 /* Relocate the item relative to the section.  */
+                                 oasys_reloc_type *r;
+
+                                 amt = sizeof (oasys_reloc_type);
+                                 r = bfd_alloc (abfd, amt);
+                                 if (!r)
+                                   return FALSE;
+                                 *(per->reloc_tail_ptr) = r;
+                                 per->reloc_tail_ptr = &r->next;
+                                 r->next = NULL;
+                                 /* Reference to undefined symbol.  */
+                                 src++;
+                                 /* There is no symbol.  */
+                                 r->symbol = 0;
+                                 /* Work out the howto.  */
+                                 abort ();
+                                 r->relent.address = dst_ptr - dst_base_ptr;
+                                 r->relent.howto = &howto_table[reloc >> 6];
+                                 r->relent.sym_ptr_ptr = NULL;
+                                 section->reloc_count++;
+
+                                 /* Fake up the data to look like
+                                    it's got the -ve pc in it, this
+                                    makes it much easier to convert
+                                    into other formats.  This is done
+                                    by hitting the addend.  */
+                                 if (r->relent.howto->pc_relative)
+                                   r->relent.addend -= dst_ptr - dst_base_ptr;
+                               }
+                               break;
+
+                             case RELOCATION_TYPE_UND:
+                               {
+                                 oasys_reloc_type *r;
+
+                                 amt = sizeof (oasys_reloc_type);
+                                 r = bfd_alloc (abfd, amt);
+                                 if (!r)
+                                   return FALSE;
+                                 *(per->reloc_tail_ptr) = r;
+                                 per->reloc_tail_ptr = &r->next;
+                                 r->next = NULL;
+                                 /* Reference to undefined symbol.  */
+                                 src++;
+                                 /* Get symbol number.  */
+                                 r->symbol = (src[0] << 8) | src[1];
+                                 /* Work out the howto.  */
+                                 abort ();
+
+                                 r->relent.addend = 0;
+                                 r->relent.address = dst_ptr - dst_base_ptr;
+                                 r->relent.howto = &howto_table[reloc >> 6];
+                                 r->relent.sym_ptr_ptr = NULL;
+                                 section->reloc_count++;
+
+                                 src += 2;
+                                 /* Fake up the data to look like
+                                    it's got the -ve pc in it, this
+                                    makes it much easier to convert
+                                    into other formats.  This is done
+                                    by hitting the addend.  */
+                                 if (r->relent.howto->pc_relative)
+                                   r->relent.addend -= dst_ptr - dst_base_ptr;
+                               }
+                               break;
+                             case RELOCATION_TYPE_COM:
+                               BFD_FAIL ();
+                             }
+                         }
+                       *dst_ptr++ = *src++;
+                     }
+                 }
+             }
          }
-       buffer = malloc(3);
-       section_number= record.section.relb & RELOCATION_SECT_BITS;
-       sprintf(buffer,"%u", section_number);
-       s = bfd_make_section(abfd,buffer);
-       static_data.sections[section_number] = s;
-       switch (record.section.relb & RELOCATION_TYPE_BITS) {
-       case RELOCATION_TYPE_ABS:
-       case RELOCATION_TYPE_REL:
          break;
-       case RELOCATION_TYPE_UND:
-       case RELOCATION_TYPE_COM:
-         BFD_FAIL();
+       case oasys_record_is_local_enum:
+       case oasys_record_is_symbol_enum:
+       case oasys_record_is_section_enum:
+         break;
+       default:
+         loop = FALSE;
        }
+    }
 
+  return TRUE;
 
-       s->size  = bfd_h_getlong(abfd, & record.section.value) ;
-       s->vma = bfd_h_getlong(abfd, &record.section.vma);
-       s->flags |= SEC_LOAD | SEC_HAS_CONTENTS;
-       had_usefull = true;
-      }
-      break;
-    case oasys_record_is_data_enum:
-      static_data.first_data_record = bfd_tell(abfd) - record.header.length;
-    case oasys_record_is_debug_enum:
-    case oasys_record_is_module_enum:
-    case oasys_record_is_named_section_enum:
-    case oasys_record_is_end_enum:
-      if (had_usefull == false) return (bfd_target *)NULL;
-      loop = false;
-      break;
-    default:
-      return (bfd_target *)NULL;
-    }
-  }
-  oasys_data(abfd) = (oasys_data_type
-                     *)malloc(sizeof(oasys_data_type));
-  oasys = oasys_data(abfd);
-  * oasys = static_data;
-
-  oasys->symbols = (asymbol *)NULL;
-  /* 
-     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;
-  if (abfd->symcount != 0) {
-    abfd->flags |= HAS_SYMS;
-  }
-  return abfd->xvec;
 }
 
+#define MAX_SECS 16
 
-void 
-oasys_print_symbol(ignore_abfd, file,  symbol, how)
-bfd *ignore_abfd;
-FILE *file;
-asymbol *symbol;
-bfd_print_symbol_enum_type how;
+static const bfd_target *
+oasys_object_p (bfd *abfd)
 {
-  switch (how) {
-  case bfd_print_symbol_name_enum:
-  case bfd_print_symbol_type_enum:
-    fprintf(file,"%s", symbol->name);
-    break;
-  case bfd_print_symbol_all_enum:
-    {
-      char *section_name = symbol->section == (asection *)NULL ?
-       "*abs" : symbol->section->name;
+  oasys_data_type *oasys;
+  oasys_data_type *save = OASYS_DATA (abfd);
+  bfd_boolean loop = TRUE;
+  bfd_boolean had_usefull = FALSE;
 
-      bfd_print_symbol_vandf((void *)file,symbol);
+  abfd->tdata.oasys_obj_data = 0;
+  oasys_mkobject (abfd);
+  oasys = OASYS_DATA (abfd);
+  memset ((void *) oasys->sections, 0xff, sizeof (oasys->sections));
 
-      fprintf(file," %-5s %s",
-             section_name,
-             symbol->name);
-    }
-    break;
-  }
-}
-/*
- The howto table is build using the top two bits of a reloc byte to
- index into it. The bits are PCREL,WORD/LONG
-*/
-static reloc_howto_type howto_table[]= 
-{
-/* T rs size bsz pcrel bitpos abs ovr sf name partial inplace mask */
-
-{  0, 0,  1,   16, false,0,   true,true,0,"abs16",true,0x0000ffff},
-{  0, 0,  2,   32, false,0,   true,true,0,"abs32",true,0xffffffff},
-{  0, 0,  1,   16, true,0,   true,true,0,"pcrel16",true,0x0000ffff},
-{  0, 0,  2,   32, true,0,   true,true,0,"pcrel32",true,0xffffffff}
-};
+  /* Point to the start of the file.  */
+  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
+    goto fail;
+  oasys->symbol_string_length = 0;
 
-/* Read in all the section data and relocation stuff too */
-static boolean oasys_slurp_section_data(abfd)
-bfd *abfd;
-{
-  oasys_record_union_type record;
-  oasys_data_type *data = oasys_data(abfd);
-  boolean loop = true;
+  /* Inspect the records, but only keep the section info -
+     remember the size of the symbols.  */
+  oasys->first_data_record = 0;
+  while (loop)
+    {
+      oasys_record_union_type record;
+      if (! oasys_read_record (abfd, &record))
+       goto fail;
+      if ((size_t) record.header.length < (size_t) sizeof (record.header))
+       goto fail;
+
+      switch ((oasys_record_enum_type) (record.header.type))
+       {
+       case oasys_record_is_header_enum:
+         had_usefull = TRUE;
+         break;
+       case oasys_record_is_symbol_enum:
+       case oasys_record_is_local_enum:
+         /* Count symbols and remember their size for a future malloc.  */
+         abfd->symcount++;
+         oasys->symbol_string_length += 1 + oasys_string_length (&record);
+         had_usefull = TRUE;
+         break;
+       case oasys_record_is_section_enum:
+         {
+           asection *s;
+           char *buffer;
+           unsigned int section_number;
+
+           if (record.section.header.length != sizeof (record.section))
+             goto fail;
+
+           buffer = bfd_alloc (abfd, (bfd_size_type) 3);
+           if (!buffer)
+             goto fail;
+           section_number = record.section.relb & RELOCATION_SECT_BITS;
+           sprintf (buffer, "%u", section_number);
+           s = bfd_make_section (abfd, buffer);
+           oasys->sections[section_number] = s;
+           switch (record.section.relb & RELOCATION_TYPE_BITS)
+             {
+             case RELOCATION_TYPE_ABS:
+             case RELOCATION_TYPE_REL:
+               break;
+             case RELOCATION_TYPE_UND:
+             case RELOCATION_TYPE_COM:
+               BFD_FAIL ();
+             }
+
+           s->size = H_GET_32 (abfd, record.section.value);
+           s->vma = H_GET_32 (abfd, record.section.vma);
+           s->flags = 0;
+           had_usefull = TRUE;
+         }
+         break;
+       case oasys_record_is_data_enum:
+         oasys->first_data_record = bfd_tell (abfd) - record.header.length;
+       case oasys_record_is_debug_enum:
+       case oasys_record_is_module_enum:
+       case oasys_record_is_named_section_enum:
+       case oasys_record_is_end_enum:
+         if (! had_usefull)
+           goto fail;
+         loop = FALSE;
+         break;
+       default:
+         goto fail;
+       }
+    }
+  oasys->symbols = NULL;
 
-  oasys_per_section_type *per ;
+  /* Oasys support several architectures, but I can't see a simple way
+     to discover which one is in a particular file - we'll guess.  */
+  bfd_default_set_arch_mach (abfd, bfd_arch_m68k, 0);
+  if (abfd->symcount != 0)
+    abfd->flags |= HAS_SYMS;
 
-  asection *s;
+  /* We don't know if a section has data until we've read it.  */
+  oasys_slurp_section_data (abfd);
 
-  /* Buy enough memory for all the section data and relocations */
-  for (s = abfd->sections; s != (asection *)NULL; s= s->next) {
-    per =  oasys_per_section(s);
-    if (per->data != (bfd_byte*)NULL) return true;
-    per->data = (bfd_byte *) malloc(s->size);
-    obstack_init(&per->reloc_obstack);
-    per->reloc_tail_ptr = (oasys_reloc_type **)&(s->relocation);
-  }
+  return abfd->xvec;
 
-  bfd_seek(abfd, data->first_data_record, SEEK_SET);
-  while (loop) {
-    oasys_read_record(abfd, &record);
-    switch (record.header.type) {
-    case oasys_record_is_header_enum:
-      break;
-    case oasys_record_is_data_enum:
-      {
+fail:
+  (void) bfd_release (abfd, oasys);
+  abfd->tdata.oasys_obj_data = save;
+  return NULL;
+}
 
-       uint8e_type *src = record.data.data;
-       uint8e_type *end_src = ((uint8e_type *)&record) + record.header.length;
-       unsigned int relbit;
-       bfd_byte *dst_ptr ;
-       bfd_byte *dst_base_ptr ;
-       asection *section;
-       unsigned int count;
 
-       bfd_vma dst_offset = bfd_h_getlong(abfd, record.data.addr);
-       section = data->sections[record.data.relb & RELOCATION_SECT_BITS];
-       per =  oasys_per_section(section);
-dst_base_ptr =         dst_ptr = oasys_per_section(section)->data + dst_offset;
+static void
+oasys_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
+                      asymbol *symbol,
+                      symbol_info *ret)
+{
+  bfd_symbol_info (symbol, ret);
 
-       while (src < end_src) {
-         uint8e_type mod_byte = *src++;
-         count = 8;
+  if (!symbol->section)
+    ret->type = (symbol->flags & BSF_LOCAL) ? 'a' : 'A';
+}
 
-         for (relbit = 1; count-- != 0; relbit <<=1) 
-           {
-             if (relbit & mod_byte) 
-               {
-                 uint8e_type reloc = *src;
-                 /* This item needs to be relocated */
-                 switch (reloc & RELOCATION_TYPE_BITS) {
-                 case RELOCATION_TYPE_ABS:
+static void
+oasys_print_symbol (bfd *abfd, void * afile, asymbol *symbol, bfd_print_symbol_type how)
+{
+  FILE *file = (FILE *) afile;
 
-                   break;
+  switch (how)
+    {
+    case bfd_print_symbol_name:
+    case bfd_print_symbol_more:
+      fprintf (file, "%s", symbol->name);
+      break;
+    case bfd_print_symbol_all:
+      {
+       const char *section_name = symbol->section == NULL ?
+       (const char *) "*abs" : symbol->section->name;
 
-                 case RELOCATION_TYPE_REL: 
-                   {
-                     /* Relocate the item relative to the section */
-                     oasys_reloc_type *r =
-                       (oasys_reloc_type *)
-                         obstack_alloc(&per->reloc_obstack,
-                                       sizeof(oasys_reloc_type));
-                     *(per->reloc_tail_ptr) = r;
-                     per->reloc_tail_ptr = &r->next;
-                     r->next= (oasys_reloc_type *)NULL;
-                     /* Reference to undefined symbol */
-                     src++;
-                     /* There is no symbol */
-                     r->symbol = 0;
-                     /* Work out the howto */
-                     r->relent.section =
-                       data->sections[reloc & RELOCATION_SECT_BITS];
-                     r->relent.addend = 0;
-                     r->relent.address = dst_ptr - dst_base_ptr;
-                     r->relent.howto = &howto_table[reloc>>6];
-                     section->reloc_count++;
+       bfd_print_symbol_vandf (abfd, (void *) file, symbol);
 
-                   }
-                   break;
-
-
-                 case RELOCATION_TYPE_UND:
-                   { 
-                     oasys_reloc_type *r =
-                       (oasys_reloc_type *)
-                         obstack_alloc(&per->reloc_obstack,
-                                       sizeof(oasys_reloc_type));
-                     *(per->reloc_tail_ptr) = r;
-                     per->reloc_tail_ptr = &r->next;
-                     r->next= (oasys_reloc_type *)NULL;
-                     /* Reference to undefined symbol */
-                     src++;
-                     /* Get symbol number */
-                     r->symbol = (src[0]<<8) | src[1];
-                     /* Work out the howto */
-                     r->relent.section = (asection *)NULL;
-                     r->relent.addend = 0;
-                     r->relent.address = dst_ptr - dst_base_ptr;
-                     r->relent.howto = &howto_table[reloc>>6];
-
-                     section->reloc_count++;
-                     src+=2;
-                   }
-                   break;
-                 case RELOCATION_TYPE_COM:
-                   BFD_FAIL();
-                 }
-               }
-             *dst_ptr++ = *src++;
-           }
-       }         
+       fprintf (file, " %-5s %s",
+                section_name,
+                symbol->name);
       }
       break;
-    case oasys_record_is_local_enum:
-    case oasys_record_is_symbol_enum:
-    case oasys_record_is_section_enum:
-      break;
-    default:
-      loop = false;
     }
-  }
-  return true;
-
 }
 
+static bfd_boolean
+oasys_new_section_hook (bfd *abfd, asection *newsect)
+{
+  if (!newsect->used_by_bfd)
+    {
+      newsect->used_by_bfd
+       = bfd_alloc (abfd, (bfd_size_type) sizeof (oasys_per_section_type));
+      if (!newsect->used_by_bfd)
+       return FALSE;
+    }
+  oasys_per_section (newsect)->data = NULL;
+  oasys_per_section (newsect)->section = newsect;
+  oasys_per_section (newsect)->offset = 0;
+  oasys_per_section (newsect)->initialized = FALSE;
+  newsect->alignment_power = 1;
 
+  /* Turn the section string into an index.  */
+  sscanf (newsect->name, "%u", &newsect->target_index);
 
+  return _bfd_generic_new_section_hook (abfd, newsect);
+}
 
 
-boolean
-oasys_new_section_hook (abfd, newsect)
-bfd *abfd;
-asection *newsect;
+static long
+oasys_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
 {
-  newsect->used_by_bfd = (oasys_per_section_type *)
-    malloc(sizeof(oasys_per_section_type));
-  oasys_per_section( newsect)->data = (bfd_byte *)NULL;
-  oasys_per_section(newsect)->section = newsect;
-  oasys_per_section(newsect)->offset  = 0;
-  return true;
+  if (! oasys_slurp_section_data (abfd))
+    return -1;
+  return (asect->reloc_count + 1) * sizeof (arelent *);
 }
 
-
-unsigned int
-oasys_get_reloc_upper_bound (abfd, asect)
-bfd *abfd;
-sec_ptr asect;
+static bfd_boolean
+oasys_get_section_contents (bfd *abfd,
+                           sec_ptr section,
+                           void * location,
+                           file_ptr offset,
+                           bfd_size_type count)
 {
-  oasys_slurp_section_data(abfd);
-  return (asect->reloc_count+1) * sizeof(arelent *);
-}
+  oasys_per_section_type *p = oasys_per_section (section);
 
-static boolean
-oasys_get_section_contents (abfd, section, location, offset, count)
-bfd *abfd;
-sec_ptr section;
-void  *location;
-file_ptr offset;
-unsigned      int count;
-{
-  oasys_per_section_type *p = section->used_by_bfd;
-  oasys_slurp_section_data(abfd);
-  (void)  memcpy(location, p->data + offset, count);
-  return true;
-}
+  oasys_slurp_section_data (abfd);
 
+  if (! p->initialized)
+    (void) memset (location, 0, (size_t) count);
+  else
+    (void) memcpy (location, (void *) (p->data + offset), (size_t) count);
 
-unsigned int
-oasys_canonicalize_reloc (abfd, section, relptr, symbols)
-bfd *abfd;
-sec_ptr section;
-arelent **relptr;
-asymbol **symbols;
-{
-  oasys_reloc_type *src = (oasys_reloc_type *)(section->relocation);
-  while (src != (oasys_reloc_type *)NULL) {
-    if (src->relent.section == (asection *)NULL) {
-    src->relent.sym_ptr_ptr = symbols + src->symbol;
-  }
-    *relptr ++ = &src->relent;
-    src = src->next;
-  }
-  *relptr = (arelent *)NULL;
-  return section->reloc_count;
+  return TRUE;
 }
 
-boolean
-oasys_set_arch_mach (abfd, arch, machine)
-bfd *abfd;
-enum bfd_architecture arch;
-unsigned long machine;
+static long
+oasys_canonicalize_reloc (bfd *ignore_abfd ATTRIBUTE_UNUSED,
+                         sec_ptr section,
+                         arelent **relptr,
+                         asymbol **symbols ATTRIBUTE_UNUSED)
 {
-  abfd->obj_arch = arch;
-  abfd->obj_machine = machine;
-  return true;
-}
+  unsigned int reloc_count = 0;
+  oasys_reloc_type *src = (oasys_reloc_type *) (section->relocation);
 
-boolean
-oasys_mkobject(abfd)
-bfd *abfd;
-{
-  oasys_data_type *oasys =
-    (oasys_data_type *) malloc(sizeof(oasys_data_type));
-  oasys_data(abfd) = oasys;
-  if (oasys == (oasys_data_type *)NULL) {
-    bfd_error = no_memory;
-    return false;
-  }
+  if (src != NULL)
+    abort ();
 
-  return true;
+  *relptr = NULL;
+  return section->reloc_count = reloc_count;
 }
 
 
+/* Writing.  */
 
+/* Calculate the checksum and write one record.  */
 
-
-
-
-static void
-init_for_output(abfd)
-bfd *abfd;
+static bfd_boolean
+oasys_write_record (bfd *abfd,
+                   oasys_record_enum_type type,
+                   oasys_record_union_type *record,
+                   size_t size)
 {
-  asection *s; 
-  for (s = abfd->sections; s != (asection *)NULL; s = s->next) {
-    if (s->size != 0) {
-      oasys_per_section(s)->data = (bfd_byte *)(malloc(s->size));
-    }
-  }
+  int checksum;
+  size_t i;
+  unsigned char *ptr;
+
+  record->header.length = size;
+  record->header.type = (int) type;
+  record->header.check_sum = 0;
+  record->header.fill = 0;
+  ptr = (unsigned char *) &record->pad[0];
+  checksum = 0;
+  for (i = 0; i < size; i++)
+    checksum += *ptr++;
+  record->header.check_sum = 0xff & (-checksum);
+  if (bfd_bwrite ((void *) record, (bfd_size_type) size, abfd) != size)
+    return FALSE;
+  return TRUE;
 }
 
-/** exec and core file sections */
-
-/* set section contents is complicated with OASYS since the format is 
-* not a byte image, but a record stream.
-*/
-boolean
-oasys_set_section_contents (abfd, section, location, offset, count)
-bfd *abfd;
-sec_ptr section;
-unsigned char *location;
-file_ptr offset;
-int count;
-{
-  if (oasys_per_section(section)->data == (bfd_byte *)NULL) {
-    init_for_output(abfd);
-  }
-  (void) memcpy(oasys_per_section(section)->data + offset, location, count);
-  return true;
-}
 
+/* Write out all the symbols.  */
 
+static bfd_boolean
+oasys_write_syms (bfd *abfd)
+{
+  unsigned int count;
+  asymbol **generic = bfd_get_outsymbols (abfd);
+  unsigned int index = 0;
 
-\f
-/* Native-level interface to symbols. */
+  for (count = 0; count < bfd_get_symcount (abfd); count++)
+    {
+      oasys_symbol_record_type symbol;
+      asymbol *const g = generic[count];
+      const char *src = g->name;
+      char *dst = symbol.name;
+      unsigned int l = 0;
 
-/* We read the symbols into a buffer, which is discarded when this
-function exits.  We read the strings into a buffer large enough to
-hold them all plus all the cached symbol entries. */
+      if (bfd_is_com_section (g->section))
+       {
+         symbol.relb = RELOCATION_TYPE_COM;
+         H_PUT_16 (abfd, index, symbol.refno);
+         index++;
+       }
+      else if (bfd_is_abs_section (g->section))
+       {
+         symbol.relb = RELOCATION_TYPE_ABS;
+         H_PUT_16 (abfd, 0, symbol.refno);
+       }
+      else if (bfd_is_und_section (g->section))
+       {
+         symbol.relb = RELOCATION_TYPE_UND;
+         H_PUT_16 (abfd, index, symbol.refno);
+         /* Overload the value field with the output index number */
+         index++;
+       }
+      else if (g->flags & BSF_DEBUGGING)
+       /* Throw it away.  */
+       continue;
+      else
+       {
+         if (g->section == NULL)
+           /* Sometime, the oasys tools give out a symbol with illegal
+              bits in it, we'll output it in the same broken way.  */
+           symbol.relb = RELOCATION_TYPE_REL | 0;
+         else
+           symbol.relb = RELOCATION_TYPE_REL | g->section->output_section->target_index;
+
+         H_PUT_16 (abfd, 0, symbol.refno);
+       }
 
-asymbol *
-oasys_make_empty_symbol (abfd)
-bfd *abfd;
-{
+#ifdef UNDERSCORE_HACK
+      if (src[l] == '_')
+       dst[l++] = '.';
+#endif
+      while (src[l])
+       {
+         dst[l] = src[l];
+         l++;
+       }
 
-  oasys_symbol_type  *new =
-    (oasys_symbol_type *)zalloc (sizeof (oasys_symbol_type));
-  new->symbol.the_bfd = abfd;
-  return &new->symbol;
+      H_PUT_32 (abfd, g->value, symbol.value);
+
+      if (g->flags & BSF_LOCAL)
+       {
+         if (! oasys_write_record (abfd,
+                                   oasys_record_is_local_enum,
+                                   (oasys_record_union_type *) & symbol,
+                                   offsetof (oasys_symbol_record_type,
+                                             name[0]) + l))
+           return FALSE;
+       }
+      else
+       {
+         if (! oasys_write_record (abfd,
+                                   oasys_record_is_symbol_enum,
+                                   (oasys_record_union_type *) & symbol,
+                                   offsetof (oasys_symbol_record_type,
+                                             name[0]) + l))
+           return FALSE;
+       }
+      g->value = index - 1;
+    }
 
+  return TRUE;
 }
 
-void
-oasys_reclaim_symbol_table (abfd)
-bfd *abfd;
-{
-#if 0
-  asection *section;
+/* Write a section header for each section.  */
 
-  if (!bfd_get_symcount (abfd)) return;
+static bfd_boolean
+oasys_write_sections (bfd *abfd)
+{
+  asection *s;
+  static oasys_section_record_type out;
 
-  for (section = abfd->sections; section != NULL; section = section->next)
-    if (section->relocation) {
-      free ((void *)section->relocation);
-      section->relocation = NULL;
-      section->reloc_count = 0;
+  for (s = abfd->sections; s != NULL; s = s->next)
+    {
+      if (!ISDIGIT (s->name[0]))
+       {
+         (*_bfd_error_handler)
+           (_("%s: can not represent section `%s' in oasys"),
+            bfd_get_filename (abfd), s->name);
+         bfd_set_error (bfd_error_nonrepresentable_section);
+         return FALSE;
+       }
+      out.relb = RELOCATION_TYPE_REL | s->target_index;
+      H_PUT_32 (abfd, s->size, out.value);
+      H_PUT_32 (abfd, s->vma, out.vma);
+
+      if (! oasys_write_record (abfd,
+                               oasys_record_is_section_enum,
+                               (oasys_record_union_type *) & out,
+                               sizeof (out)))
+       return FALSE;
     }
-
-  bfd_get_symcount (abfd) = 0;
-  free ((void *)obj_aout_symbols (abfd));
-  obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
-#endif
+  return TRUE;
 }
-\f
 
+static bfd_boolean
+oasys_write_header (bfd *abfd)
+{
+  /* Create and write the header.  */
+  oasys_header_record_type r;
+  size_t length = strlen (abfd->filename);
 
-\f
-/* Obsbolete procedural interface; better to look at the cache directly */
+  if (length > (size_t) sizeof (r.module_name))
+    length = sizeof (r.module_name);
 
-/* User should have checked the file flags; perhaps we should return
-BFD_NO_MORE_SYMBOLS if there are none? */
+  (void) memcpy (r.module_name, abfd->filename, length);
+  (void) memset (r.module_name + length, ' ', sizeof (r.module_name) - length);
 
-int
-oasys_get_symcount_upper_bound (abfd)
-bfd *abfd;
-{
-#if 0
-  /* In case we're doing an output file or something...?  */
-  if (bfd_get_symcount (abfd)) return bfd_get_symcount (abfd);
+  r.version_number = OASYS_VERSION_NUMBER;
+  r.rev_number = OASYS_REV_NUMBER;
 
-  return (exec_hdr (abfd)->a_syms) / (sizeof (struct nlist));
-#endif
+  return oasys_write_record (abfd, oasys_record_is_header_enum,
+                            (oasys_record_union_type *) & r,
+                            offsetof (oasys_header_record_type,
+                                      description[0]));
 }
 
-symindex
-oasys_get_first_symbol (ignore_abfd)
-bfd * ignore_abfd;
+static bfd_boolean
+oasys_write_end (bfd *abfd)
 {
-  return 0;
+  oasys_end_record_type end;
+  unsigned char null = 0;
+
+  end.relb = RELOCATION_TYPE_ABS;
+  H_PUT_32 (abfd, abfd->start_address, end.entry);
+  H_PUT_16 (abfd, 0, end.fill);
+  end.zero = 0;
+  if (! oasys_write_record (abfd,
+                           oasys_record_is_end_enum,
+                           (oasys_record_union_type *) & end,
+                           sizeof (end)))
+    return FALSE;
+
+  return bfd_bwrite ((void *) &null, (bfd_size_type) 1, abfd) == 1;
 }
 
-symindex
-oasys_get_next_symbol (abfd, oidx)
-bfd *abfd;
-symindex oidx;
+static int
+comp (const void * ap, const void * bp)
 {
-#if 0
-  if (oidx == BFD_NO_MORE_SYMBOLS) return BFD_NO_MORE_SYMBOLS;
-  return ++oidx >= bfd_get_symcount (abfd) ? BFD_NO_MORE_SYMBOLS :
-  oidx;
-#endif
-}
+  arelent *a = *((arelent **) ap);
+  arelent *b = *((arelent **) bp);
 
-char *
-oasys_symbol_name (abfd, idx)
-bfd *abfd;
-symindex idx;
-{
-#if 0
-  return (obj_aout_symbols (abfd) + idx)->symbol.name;
-#endif
+  return a->address - b->address;
 }
 
-long
-oasys_symbol_value (abfd, idx)
-bfd *abfd;
-symindex idx;
+static bfd_boolean
+oasys_write_data (bfd *abfd)
 {
-#if 0
-  return (obj_aout_symbols (abfd) + idx)->symbol.value;
-#endif
-}
+  asection *s;
 
-symclass
-oasys_classify_symbol (abfd, idx)
-bfd *abfd;
-symindex idx;
-{
-#if 0
-  aout_symbol_type *sym = obj_aout_symbols (abfd) + idx;
+  for (s = abfd->sections; s != NULL; s = s->next)
+    {
+      if (s->flags & SEC_LOAD)
+       {
+         bfd_byte *raw_data = oasys_per_section (s)->data;
+         oasys_data_record_type processed_data;
+         bfd_size_type current_byte_index = 0;
+         unsigned int relocs_to_go = s->reloc_count;
+         arelent **p = s->orelocation;
 
-  if ((sym->symbol.flags & BSF_FORT_COMM) != 0)   return bfd_symclass_fcommon;
-  if ((sym->symbol.flags & BSF_GLOBAL) != 0)    return bfd_symclass_global;
-  if ((sym->symbol.flags & BSF_DEBUGGING) != 0)  return bfd_symclass_debugger;
-  if ((sym->symbol.flags & BSF_UNDEFINED) != 0) return bfd_symclass_undefined;
-#endif
-  return bfd_symclass_unknown;
-}
+         if (s->reloc_count != 0)
+           /* Sort the reloc records so it's easy to insert the relocs into the
+              data.  */
+           qsort (s->orelocation, s->reloc_count, sizeof (arelent **), comp);
 
-boolean
-oasys_symbol_hasclass (abfd, idx, class)
-bfd *abfd;
-symindex idx;
-symclass class;
-{
-#if 0
-  aout_symbol_type *sym = obj_aout_symbols (abfd) + idx;
-  switch (class) {
-  case bfd_symclass_fcommon:
-    return (sym->symbol.flags & BSF_FORT_COMM) ? true :false;
-  case bfd_symclass_global:
-    return (sym->symbol.flags & BSF_GLOBAL) ? true:false;
-  case bfd_symclass_debugger:
-    return (sym->symbol.flags & BSF_DEBUGGING) ? true:false;;
-  case bfd_symclass_undefined:
-    return (sym->symbol.flags & BSF_UNDEFINED) ? true:false;;
-  default: return false;
-  }
-#endif
-}
+         current_byte_index = 0;
+         processed_data.relb = s->target_index | RELOCATION_TYPE_REL;
+
+         while (current_byte_index < s->size)
+           {
+             /* Scan forwards by eight bytes or however much is left and see if
+              there are any relocations going on.  */
+             bfd_byte *mod = &processed_data.data[0];
+             bfd_byte *dst = &processed_data.data[1];
+
+             unsigned int i = 0;
+             *mod = 0;
+
+             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->size && dst <=
+                    & processed_data.data[sizeof (processed_data.data) - 8])
+               {
+                 if (relocs_to_go != 0)
+                   {
+                     arelent *r = *p;
+
+                     /* There is a relocation, is it for this byte ?  */
+                     if (r->address == current_byte_index)
+                       abort ();
+                   }
+
+                 /* If this is coming from an unloadable section then copy
+                    zeros.  */
+                 if (raw_data == NULL)
+                   *dst++ = 0;
+                 else
+                   *dst++ = *raw_data++;
 
+                 if (++i >= 8)
+                   {
+                     i = 0;
+                     mod = dst++;
+                     *mod = 0;
+                   }
+                 current_byte_index++;
+               }
 
-void
-oasys_reclaim_reloc (ignore_abfd, section)
-bfd *ignore_abfd;
-sec_ptr section;
+             /* Don't write a useless null modification byte.  */
+             if (dst == mod + 1)
+               --dst;
+
+             if (! (oasys_write_record
+                    (abfd, oasys_record_is_data_enum,
+                     ((oasys_record_union_type *) &processed_data),
+                     (size_t) (dst - (bfd_byte *) &processed_data))))
+               return FALSE;
+           }
+       }
+    }
+
+  return TRUE;
+}
+
+static bfd_boolean
+oasys_write_object_contents (bfd *abfd)
 {
-#if 0
-  if (section->relocation) {
-    free (section->relocation);
-    section->relocation = NULL;
-    section->reloc_count = 0;
-  }
-#endif
+  if (! oasys_write_header (abfd))
+    return FALSE;
+  if (! oasys_write_syms (abfd))
+    return FALSE;
+  if (! oasys_write_sections (abfd))
+    return FALSE;
+  if (! oasys_write_data (abfd))
+    return FALSE;
+  if (! oasys_write_end (abfd))
+    return FALSE;
+  return TRUE;
 }
 
-boolean
-oasys_close_and_cleanup (abfd)
-bfd *abfd;
+/* Set section contents is complicated with OASYS since the format is
+   not a byte image, but a record stream.  */
+
+static bfd_boolean
+oasys_set_section_contents (bfd *abfd,
+                           sec_ptr section,
+                           const void * location,
+                           file_ptr offset,
+                           bfd_size_type count)
 {
-  if (bfd_read_p (abfd) == false)
-    switch (abfd->format) {
-    case bfd_archive:
-      if (!_bfd_write_archive_contents (abfd)) {
-       return false;
-      }
-      break;
-    case bfd_object:
-/*      if (!oasys_write_object_contents (abfd)) */{
-       return false;
-      }
-      break;
-    default:
-      bfd_error = invalid_operation;
-      return false;
+  if (count != 0)
+    {
+      if (oasys_per_section (section)->data == NULL)
+       {
+         oasys_per_section (section)->data = bfd_alloc (abfd, section->size);
+         if (!oasys_per_section (section)->data)
+           return FALSE;
+       }
+      (void) memcpy ((void *) (oasys_per_section (section)->data + offset),
+                    location, (size_t) count);
     }
+  return TRUE;
+}
 
 
-  if (oasys_data(abfd) != (oasys_data_type *)NULL) {
-    /* FIXME MORE LEAKS */
 
-  }
+/* Native-level interface to symbols.  */
+
+/* We read the symbols into a buffer, which is discarded when this
+   function exits.  We read the strings into a buffer large enough to
+   hold them all plus all the cached symbol entries.  */
 
-  return true;
+static asymbol *
+oasys_make_empty_symbol (bfd *abfd)
+{
+  bfd_size_type amt = sizeof (oasys_symbol_type);
+  oasys_symbol_type *new = bfd_zalloc (abfd, amt);
+
+  if (!new)
+    return NULL;
+  new->symbol.the_bfd = abfd;
+  return &new->symbol;
 }
 
+/* User should have checked the file flags; perhaps we should return
+   BFD_NO_MORE_SYMBOLS if there are none?  */
+
 static bfd *
-oasys_openr_next_archived_file(arch, prev)
-bfd *arch;
-bfd *prev;
+oasys_openr_next_archived_file (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) {
-    /* Reset the index - the first two entries are bogus*/
+
+  /* Take the next one from the arch state, or reset.  */
+  if (prev == NULL)
+    /* Reset the index - the first two entries are bogus.  */
     ar->module_index = 0;
-  }
 
   p = ar->module + ar->module_index;
   ar->module_index++;
 
-  if (ar->module_index <= ar->module_count) {
-    if (p->abfd == (bfd *)NULL) {
-      p->abfd = _bfd_create_empty_archive_element_shell(arch);
-      p->abfd->origin = p->pos;
-      p->abfd->filename = p->name;
+  if (ar->module_index <= ar->module_count)
+    {
+      if (p->abfd == NULL)
+       {
+         p->abfd = _bfd_create_empty_archive_element_shell (arch);
+         p->abfd->origin = p->pos;
+         p->abfd->filename = p->name;
 
-      /* Fixup a pointer to this element for the member */
-      p->abfd->arelt_data = (void *)p;
+         /* Fixup a pointer to this element for the member.  */
+         p->abfd->arelt_data = (void *) p;
+       }
+      return p->abfd;
     }
-    return p->abfd;
-  }
-  else {
-    bfd_error = no_more_archived_files;
-    return (bfd *)NULL;
-  }
+
+  bfd_set_error (bfd_error_no_more_archived_files);
+  return NULL;
 }
 
-static boolean
-oasys_find_nearest_line(abfd,
-                        section,
-                        symbols,
-                        offset,
-                        filename_ptr,
-                        functionname_ptr,
-                        line_ptr)
-bfd *abfd;
-asection *section;
-asymbol **symbols;
-bfd_vma offset;
-char **filename_ptr;
-char **functionname_ptr;
-unsigned int *line_ptr;
-{
-  return false;
+static bfd_boolean
+oasys_find_nearest_line (bfd *abfd ATTRIBUTE_UNUSED,
+                        asection *section ATTRIBUTE_UNUSED,
+                        asymbol **symbols ATTRIBUTE_UNUSED,
+                        bfd_vma offset ATTRIBUTE_UNUSED,
+                        const char **filename_ptr ATTRIBUTE_UNUSED,
+                        const char **functionname_ptr ATTRIBUTE_UNUSED,
+                        unsigned int *line_ptr ATTRIBUTE_UNUSED)
+{
+  return FALSE;
+}
 
+static bfd_boolean
+oasys_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
+                        const char **filename_ptr ATTRIBUTE_UNUSED,
+                        const char **functionname_ptr ATTRIBUTE_UNUSED,
+                        unsigned int *line_ptr ATTRIBUTE_UNUSED)
+{
+  return FALSE;
 }
 
 static int
-oasys_stat_arch_elt(abfd, buf)
-bfd *abfd;
-struct stat *buf;
+oasys_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
 {
-  oasys_module_info_type *mod = abfd->arelt_data;
-  if (mod == (oasys_module_info_type *)NULL) {
-    bfd_error = invalid_operation;
-    return -1;
-  }
-  else {
-    buf->st_size = mod->size;
-    buf->st_mode = 0666;
-  return 0;
-  }
+  oasys_module_info_type *mod = (oasys_module_info_type *) abfd->arelt_data;
 
+  if (mod == NULL)
+    {
+      bfd_set_error (bfd_error_invalid_operation);
+      return -1;
+    }
 
+  buf->st_size = mod->size;
+  buf->st_mode = 0666;
+  return 0;
 }
 
+static int
+oasys_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
+                     struct bfd_link_info *info ATTRIBUTE_UNUSED)
+{
+  return 0;
+}
 
-/*SUPPRESS 460 */
-bfd_target oasys_vec =
+#define        oasys_close_and_cleanup                    _bfd_generic_close_and_cleanup
+#define oasys_bfd_free_cached_info                 _bfd_generic_bfd_free_cached_info
+#define oasys_slurp_armap                          bfd_true
+#define oasys_slurp_extended_name_table            bfd_true
+#define oasys_construct_extended_name_table        ((bfd_boolean (*) (bfd *, char **, bfd_size_type *, const char **)) bfd_true)
+#define oasys_truncate_arname                      bfd_dont_truncate_arname
+#define oasys_write_armap                          ((bfd_boolean (*) (bfd *, unsigned int, struct orl *, unsigned int, int)) bfd_true)
+#define oasys_read_ar_hdr                          bfd_nullvoidptr
+#define oasys_get_elt_at_index                     _bfd_generic_get_elt_at_index
+#define oasys_update_armap_timestamp               bfd_true
+#define oasys_bfd_is_local_label_name              bfd_generic_is_local_label_name
+#define oasys_bfd_is_target_special_symbol         ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
+#define oasys_get_lineno                           _bfd_nosymbols_get_lineno
+#define oasys_bfd_make_debug_symbol                _bfd_nosymbols_bfd_make_debug_symbol
+#define oasys_read_minisymbols                     _bfd_generic_read_minisymbols
+#define oasys_minisymbol_to_symbol                 _bfd_generic_minisymbol_to_symbol
+#define oasys_bfd_reloc_type_lookup                _bfd_norelocs_bfd_reloc_type_lookup
+#define oasys_bfd_reloc_name_lookup          _bfd_norelocs_bfd_reloc_name_lookup
+#define oasys_set_arch_mach                        bfd_default_set_arch_mach
+#define oasys_get_section_contents_in_window       _bfd_generic_get_section_contents_in_window
+#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_gc_sections                      bfd_generic_gc_sections
+#define oasys_bfd_merge_sections                   bfd_generic_merge_sections
+#define oasys_bfd_is_group_section                 bfd_generic_is_group_section
+#define oasys_bfd_discard_group                    bfd_generic_discard_group
+#define oasys_section_already_linked               _bfd_generic_section_already_linked
+#define oasys_bfd_define_common_symbol             bfd_generic_define_common_symbol
+#define oasys_bfd_link_hash_table_create           _bfd_generic_link_hash_table_create
+#define oasys_bfd_link_hash_table_free             _bfd_generic_link_hash_table_free
+#define oasys_bfd_link_add_symbols                 _bfd_generic_link_add_symbols
+#define oasys_bfd_link_just_syms                   _bfd_generic_link_just_syms
+#define oasys_bfd_final_link                       _bfd_generic_final_link
+#define oasys_bfd_link_split_section               _bfd_generic_link_split_section
+
+const bfd_target oasys_vec =
 {
-  "oasys",                     /* name */
-  bfd_target_oasys_flavour_enum,
-  true,                                /* target byte order */
-  true,                                /* target headers byte order */
-  (HAS_RELOC | EXEC_P |                /* object flags */
+  "oasys",                     /* Name.  */
+  bfd_target_oasys_flavour,
+  BFD_ENDIAN_BIG,              /* Target byte order.  */
+  BFD_ENDIAN_BIG,              /* Target headers byte order.  */
+  (HAS_RELOC | EXEC_P |                /* Object flags.  */
    HAS_LINENO | HAS_DEBUG |
-   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,                           /* valid reloc types */
-  ' ',                         /* ar_pad_char */
-  16,                          /* ar_max_namelen */
-  oasys_close_and_cleanup,     /* _close_and_cleanup */
-  oasys_set_section_contents,  /* bfd_set_section_contents */
-  oasys_get_section_contents,
-  oasys_new_section_hook,      /*   new_section_hook */
-  0,                           /* _core_file_failing_command */
-  0,                           /* _core_file_failing_signal */
-  0,                           /* _core_file_matches_ex...p */
-
-  0,                           /* bfd_slurp_bsd_armap,               bfd_slurp_armap */
-  bfd_true,                    /* bfd_slurp_extended_name_table */
-  bfd_bsd_truncate_arname,     /* bfd_truncate_arname */
-
-  oasys_get_symtab_upper_bound,        /* get_symtab_upper_bound */
-  oasys_get_symtab,            /* canonicalize_symtab */
-  0,                           /* oasys_reclaim_symbol_table,            bfd_reclaim_symbol_table */
-  oasys_get_reloc_upper_bound, /* get_reloc_upper_bound */
-  oasys_canonicalize_reloc,    /* bfd_canonicalize_reloc */
-  0,                           /*  oasys_reclaim_reloc,                   bfd_reclaim_reloc */
-  0,                           /* oasys_get_symcount_upper_bound,        bfd_get_symcount_upper_bound */
-  0,                           /* oasys_get_first_symbol,                bfd_get_first_symbol */
-  0,                           /* oasys_get_next_symbol,                 bfd_get_next_symbol */
-  0,                           /* oasys_classify_symbol,                 bfd_classify_symbol */
-  0,                           /* oasys_symbol_hasclass,                 bfd_symbol_hasclass */
-  0,                           /* oasys_symbol_name,                     bfd_symbol_name */
-  0,                           /* oasys_symbol_value,                    bfd_symbol_value */
-
-  _do_getblong, _do_putblong, _do_getbshort, _do_putbshort, /* data */
-  _do_getblong, _do_putblong, _do_getbshort, _do_putbshort, /* hdrs */
+   HAS_SYMS | HAS_LOCALS | 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.  */
+  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
+  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
+  bfd_getb16, bfd_getb_signed_16, bfd_putb16,  /* Data.  */
+  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
+  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
+  bfd_getb16, bfd_getb_signed_16, bfd_putb16,  /* Headers.  */
 
   {_bfd_dummy_target,
-     oasys_object_p,           /* bfd_check_format */
-     oasys_archive_p,
-     bfd_false
-     },
-  {
+   oasys_object_p,             /* bfd_check_format.  */
+   oasys_archive_p,
+   _bfd_dummy_target,
+  },
+  {                            /* bfd_set_format.  */
     bfd_false,
-    oasys_mkobject, 
+    oasys_mkobject,
     _bfd_generic_mkarchive,
     bfd_false
-    },
-  oasys_make_empty_symbol,
-  oasys_print_symbol,
-  bfd_false,                   /*      oasys_get_lineno,*/
-  oasys_set_arch_mach,         /* bfd_set_arch_mach,*/
-  bfd_false,
-  oasys_openr_next_archived_file,
-  oasys_find_nearest_line,     /* bfd_find_nearest_line */
-  oasys_stat_arch_elt,         /* bfd_stat_arch_elt */
+  },
+  {                            /* bfd_write_contents.  */
+    bfd_false,
+    oasys_write_object_contents,
+    _bfd_write_archive_contents,
+    bfd_false,
+  },
+
+  BFD_JUMP_TABLE_GENERIC (oasys),
+  BFD_JUMP_TABLE_COPY (_bfd_generic),
+  BFD_JUMP_TABLE_CORE (_bfd_nocore),
+  BFD_JUMP_TABLE_ARCHIVE (oasys),
+  BFD_JUMP_TABLE_SYMBOLS (oasys),
+  BFD_JUMP_TABLE_RELOCS (oasys),
+  BFD_JUMP_TABLE_WRITE (oasys),
+  BFD_JUMP_TABLE_LINK (oasys),
+  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
+
+  NULL,
+
+  NULL
 };
This page took 0.042458 seconds and 4 git commands to generate.