Support 8 and 16-bit relocs for i386coff. Enable i386 gas testsuite tests
[deliverable/binutils-gdb.git] / bfd / ihex.c
index a8c6e2ebe1443d4226cb85f1c2a1e77e3bbca4ba..17740d8239ec2d06f92669b23f65e134bc740a7b 100644 (file)
@@ -1,5 +1,5 @@
 /* BFD back-end for Intel Hex objects.
-   Copyright 1995 Free Software Foundation, Inc.
+   Copyright 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
    Written by Ian Lance Taylor of Cygnus Support <ian@cygnus.com>.
 
 This file is part of BFD, the Binary File Descriptor library.
@@ -104,6 +104,18 @@ Another document reports these additional types:
    10..13      Upper 16 bits of start address
    14..15      Checksum in hex notation
    16..17      Carriage return, line feed
+
+The MRI compiler uses this, which is a repeat of type 5:
+
+  EXTENDED START RECORD
+   Byte 1      Header = colon (:)
+   2..3                The byte count, must be "04"
+   4..7                Load address, must be "0000"
+   8..9                Record type, must be "05"
+   10..13      Upper 16 bits of start address
+   14..17      Lower 16 bits of start address
+   18..19      Checksum in hex notation
+   20..21      Carriage return, line feed
 */
 
 #include "bfd.h"
@@ -130,6 +142,7 @@ static boolean ihex_write_object_contents PARAMS ((bfd *));
 static asymbol *ihex_make_empty_symbol PARAMS ((bfd *));
 static boolean ihex_set_arch_mach
   PARAMS ((bfd *, enum bfd_architecture, unsigned long));
+static int ihex_sizeof_headers PARAMS ((bfd *, boolean));
 
 /* The number of bytes we put on one line during output.  */
 
@@ -243,7 +256,7 @@ ihex_bad_byte (abfd, lineno, c, error)
          buf[1] = '\0';
        }
       (*_bfd_error_handler)
-       ("%s:%d: unexpected character `%s' in Intel Hex file\n",
+       (_("%s:%d: unexpected character `%s' in Intel Hex file\n"),
         bfd_get_filename (abfd), lineno, buf);
       bfd_set_error (bfd_error_bad_value);
     }
@@ -261,7 +274,7 @@ ihex_scan (abfd)
   asection *sec;
   int lineno;
   boolean error;
-  bfd_byte *buf;
+  bfd_byte *buf = NULL;
   size_t bufsize;
   int c;
 
@@ -270,12 +283,11 @@ ihex_scan (abfd)
 
   abfd->start_address = 0;
 
-  extbase = 0;
   segbase = 0;
+  extbase = 0;
   sec = NULL;
   lineno = 1;
   error = false;
-  buf = NULL;
   bufsize = 0;
   while ((c = ihex_get_byte (abfd, &error)) != EOF)
     {
@@ -354,7 +366,7 @@ ihex_scan (abfd)
          if (((- chksum) & 0xff) != (unsigned int) HEX2 (buf + 2 * i))
            {
              (*_bfd_error_handler)
-               ("%s:%d: bad checksum in Intel Hex file (expected %u, found %u)",
+               (_("%s:%d: bad checksum in Intel Hex file (expected %u, found %u)"),
                 bfd_get_filename (abfd), lineno,
                 (- chksum) & 0xff, (unsigned int) HEX2 (buf + 2 * i));
              bfd_set_error (bfd_error_bad_value);
@@ -406,7 +418,7 @@ ihex_scan (abfd)
              if (len != 2)
                {
                  (*_bfd_error_handler)
-                   ("%s:%d: bad extended address record length in Intel Hex file",
+                   (_("%s:%d: bad extended address record length in Intel Hex file"),
                     bfd_get_filename (abfd), lineno);
                  bfd_set_error (bfd_error_bad_value);
                  goto error_return;
@@ -423,7 +435,7 @@ ihex_scan (abfd)
              if (len != 4)
                {
                  (*_bfd_error_handler)
-                   ("%s:%d: bad extended start address length in Intel Hex file",
+                   (_("%s:%d: bad extended start address length in Intel Hex file"),
                     bfd_get_filename (abfd), lineno);
                  bfd_set_error (bfd_error_bad_value);
                  goto error_return;
@@ -440,13 +452,13 @@ ihex_scan (abfd)
              if (len != 2)
                {
                  (*_bfd_error_handler)
-                   ("%s:%d: bad extended linear address record length in Intel Hex file",
+                   (_("%s:%d: bad extended linear address record length in Intel Hex file"),
                     bfd_get_filename (abfd), lineno);
                  bfd_set_error (bfd_error_bad_value);
                  goto error_return;
                }
 
-             segbase = HEX4 (buf) << 16;
+             extbase = HEX4 (buf) << 16;
 
              sec = NULL;
 
@@ -454,16 +466,19 @@ ihex_scan (abfd)
 
            case 5:
              /* An extended linear start address record.  */
-             if (len != 2)
+             if (len != 2 && len != 4)
                {
                  (*_bfd_error_handler)
-                   ("%s:%d: bad extended linear start address length in Intel Hex file",
+                   (_("%s:%d: bad extended linear start address length in Intel Hex file"),
                     bfd_get_filename (abfd), lineno);
                  bfd_set_error (bfd_error_bad_value);
                  goto error_return;
                }
 
-             abfd->start_address += HEX4 (buf) << 16;
+             if (len == 2)
+               abfd->start_address += HEX4 (buf) << 16;
+             else
+               abfd->start_address = (HEX4 (buf) << 16) + HEX4 (buf + 4);
 
              sec = NULL;
 
@@ -471,7 +486,7 @@ ihex_scan (abfd)
 
            default:
              (*_bfd_error_handler)
-               ("%s:%d: unrecognized ihex type %u in Intel Hex file\n",
+               (_("%s:%d: unrecognized ihex type %u in Intel Hex file\n"),
                 bfd_get_filename (abfd), lineno, type);
              bfd_set_error (bfd_error_bad_value);
              goto error_return;
@@ -555,7 +570,7 @@ ihex_read_section (abfd, section, contents)
 {
   int c;
   bfd_byte *p;
-  bfd_byte *buf;
+  bfd_byte *buf = NULL;
   size_t bufsize;
   boolean error;
 
@@ -563,7 +578,6 @@ ihex_read_section (abfd, section, contents)
     goto error_return;
 
   p = contents;
-  buf = NULL;
   bufsize = 0;
   error = false;
   while ((c = ihex_get_byte (abfd, &error)) != EOF)
@@ -592,7 +606,7 @@ ihex_read_section (abfd, section, contents)
       if (type != 0)
        {
          (*_bfd_error_handler)
-           ("%s: internal error in ihex_read_section",
+           (_("%s: internal error in ihex_read_section"),
             bfd_get_filename (abfd));
          bfd_set_error (bfd_error_bad_value);
          goto error_return;
@@ -627,7 +641,7 @@ ihex_read_section (abfd, section, contents)
   if ((bfd_size_type) (p - contents) < section->_raw_size)
     {
       (*_bfd_error_handler)
-       ("%s: bad section length in ihex_read_section",
+       (_("%s: bad section length in ihex_read_section"),
         bfd_get_filename (abfd));
       bfd_set_error (bfd_error_bad_value);
       goto error_return;
@@ -778,12 +792,12 @@ static boolean
 ihex_write_object_contents (abfd)
      bfd *abfd;
 {
-  bfd_vma extbase;
   bfd_vma segbase;
+  bfd_vma extbase;
   struct ihex_data_list *l;
 
-  extbase = 0;
   segbase = 0;
+  extbase = 0;
   for (l = abfd->tdata.ihex_data->head; l != NULL; l = l->next)
     {
       bfd_vma where;
@@ -801,21 +815,39 @@ ihex_write_object_contents (abfd)
          if (now > CHUNK)
            now = CHUNK;
 
-         if (where > extbase + segbase + 0xffff)
+         if (where > segbase + extbase + 0xffff)
            {
              bfd_byte addr[2];
 
              /* We need a new base address.  */
              if (where <= 0xfffff)
                {
+                 /* The addresses should be sorted.  */
+                 BFD_ASSERT (extbase == 0);
+
                  segbase = where & 0xf0000;
-                 addr[0] = (segbase >> 12) & 0xff;
-                 addr[1] = (segbase >> 4) & 0xff;
+                 addr[0] = (bfd_byte)(segbase >> 12) & 0xff;
+                 addr[1] = (bfd_byte)(segbase >> 4) & 0xff;
                  if (! ihex_write_record (abfd, 2, 0, 2, addr))
                    return false;
                }
              else
                {
+                 /* The extended address record and the extended
+                     linear address record are combined, at least by
+                     some readers.  We need an extended linear address
+                     record here, so if we've already written out an
+                     extended address record, zero it out to avoid
+                     confusion.  */
+                 if (segbase != 0)
+                   {
+                     addr[0] = 0;
+                     addr[1] = 0;
+                     if (! ihex_write_record (abfd, 2, 0, 2, addr))
+                       return false;
+                     segbase = 0;
+                   }
+
                  extbase = where & 0xffff0000;
                  if (where > extbase + 0xffff)
                    {
@@ -823,13 +855,13 @@ ihex_write_object_contents (abfd)
 
                      sprintf_vma (buf, where);
                      (*_bfd_error_handler)
-                       ("%s: address 0x%s out of range for Intex Hex file",
+                       (_("%s: address 0x%s out of range for Intex Hex file"),
                         bfd_get_filename (abfd), buf);
                      bfd_set_error (bfd_error_bad_value);
                      return false;
                    }
-                 addr[0] = (extbase >> 24) & 0xff;
-                 addr[1] = (extbase >> 16) & 0xff;
+                 addr[0] = (bfd_byte)(extbase >> 24) & 0xff;
+                 addr[1] = (bfd_byte)(extbase >> 16) & 0xff;
                  if (! ihex_write_record (abfd, 2, 0, 4, addr))
                    return false;
                }
@@ -852,21 +884,24 @@ ihex_write_object_contents (abfd)
 
       start = abfd->start_address;
 
-      if (start > 0xfffff)
+      if (start <= 0xfffff)
        {
-         startbuf[0] = (start >> 24) & 0xff;
-         startbuf[1] = (start >> 16) & 0xff;
-         if (! ihex_write_record (abfd, 2, 0, 5, startbuf))
+         startbuf[0] = (bfd_byte)((start & 0xf0000) >> 12) & 0xff;
+         startbuf[1] = 0;
+         startbuf[2] = (bfd_byte)(start >> 8) & 0xff;
+         startbuf[3] = (bfd_byte)start & 0xff;
+         if (! ihex_write_record (abfd, 4, 0, 3, startbuf))
+           return false;
+       }
+      else
+       {
+         startbuf[0] = (bfd_byte)(start >> 24) & 0xff;
+         startbuf[1] = (bfd_byte)(start >> 16) & 0xff;
+         startbuf[2] = (bfd_byte)(start >> 8) & 0xff;
+         startbuf[3] = (bfd_byte)start & 0xff;
+         if (! ihex_write_record (abfd, 4, 0, 5, startbuf))
            return false;
-         start &= 0xffff;
        }
-
-      startbuf[0] = ((start & 0xf0000) >> 12) & 0xff;
-      startbuf[1] = 0;
-      startbuf[2] = (start >> 8) & 0xff;
-      startbuf[3] = start & 0xff;
-      if (! ihex_write_record (abfd, 4, 0, 3, startbuf))
-       return false;
     }
 
   if (! ihex_write_record (abfd, 0, 0, 1, NULL))
@@ -912,8 +947,8 @@ ihex_set_arch_mach (abfd, arch, mach)
 /*ARGSUSED*/
 static int
 ihex_sizeof_headers (abfd, exec)
-     bfd *abfd;
-     boolean exec;
+     bfd *abfd ATTRIBUTE_UNUSED;
+     boolean exec ATTRIBUTE_UNUSED;
 {
   return 0;
 }
@@ -931,7 +966,7 @@ ihex_sizeof_headers (abfd, exec)
   ((long (*) PARAMS ((bfd *, asymbol **))) bfd_0l)
 #define ihex_print_symbol _bfd_nosymbols_print_symbol
 #define ihex_get_symbol_info _bfd_nosymbols_get_symbol_info
-#define ihex_bfd_is_local_label _bfd_nosymbols_bfd_is_local_label
+#define ihex_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
 #define ihex_get_lineno _bfd_nosymbols_get_lineno
 #define ihex_find_nearest_line _bfd_nosymbols_find_nearest_line
 #define ihex_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
@@ -947,6 +982,7 @@ ihex_sizeof_headers (abfd, exec)
 #define ihex_bfd_get_relocated_section_contents \
   bfd_generic_get_relocated_section_contents
 #define ihex_bfd_relax_section bfd_generic_relax_section
+#define ihex_bfd_gc_sections bfd_generic_gc_sections
 #define ihex_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
 #define ihex_bfd_link_add_symbols _bfd_generic_link_add_symbols
 #define ihex_bfd_final_link _bfd_generic_final_link
@@ -958,8 +994,8 @@ const bfd_target ihex_vec =
 {
   "ihex",                      /* name */
   bfd_target_ihex_flavour,
-  true,                                /* target byte order */
-  true,                                /* target headers byte order */
+  BFD_ENDIAN_UNKNOWN,          /* target byte order */
+  BFD_ENDIAN_UNKNOWN,          /* target headers byte order */
   0,                           /* object flags */
   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD),   /* section flags */
   0,                           /* leading underscore */
@@ -1001,5 +1037,7 @@ const bfd_target ihex_vec =
   BFD_JUMP_TABLE_LINK (ihex),
   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
 
+  NULL,
+  
   (PTR) 0
 };
This page took 0.027976 seconds and 4 git commands to generate.