]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - bfd/srec.c
Use bool in bfd
[thirdparty/binutils-gdb.git] / bfd / srec.c
index 10c6ceb176dabea890d0676ba564ef0b80ea6d65..3ddb3c56e33c2fae3fb6dcc144ca1bdeb565c42d 100644 (file)
@@ -1,5 +1,5 @@
 /* BFD back-end for s-record objects.
-   Copyright (C) 1990-2018 Free Software Foundation, Inc.
+   Copyright (C) 1990-2021 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.
@@ -134,7 +134,7 @@ unsigned int _bfd_srec_len = DEFAULT_CHUNK;
 /* The type of srec output (free or forced to S3).
    This variable can be modified by objcopy's --srec-forceS3
    parameter.  */
-bfd_boolean _bfd_srec_forceS3 = FALSE;
+bool _bfd_srec_forceS3 = false;
 
 /* When writing an S-record file, the S-records can not be output as
    they are seen.  This structure is used to hold them in memory.  */
@@ -178,18 +178,18 @@ tdata_type;
 static void
 srec_init (void)
 {
-  static bfd_boolean inited = FALSE;
+  static bool inited = false;
 
   if (! inited)
     {
-      inited = TRUE;
+      inited = true;
       hex_init ();
     }
 }
 
 /* Set up the S-record tdata information.  */
 
-static bfd_boolean
+static bool
 srec_mkobject (bfd *abfd)
 {
   tdata_type *tdata;
@@ -198,7 +198,7 @@ srec_mkobject (bfd *abfd)
 
   tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
   if (tdata == NULL)
-    return FALSE;
+    return false;
 
   abfd->tdata.srec_data = tdata;
   tdata->type = 1;
@@ -208,21 +208,21 @@ srec_mkobject (bfd *abfd)
   tdata->symtail = NULL;
   tdata->csymbols = NULL;
 
-  return TRUE;
+  return true;
 }
 
 /* Read a byte from an S record file.  Set *ERRORPTR if an error
    occurred.  Return EOF on error or end of file.  */
 
 static int
-srec_get_byte (bfd *abfd, bfd_boolean *errorptr)
+srec_get_byte (bfd *abfd, bool *errorptr)
 {
   bfd_byte c;
 
   if (bfd_bread (&c, (bfd_size_type) 1, abfd) != 1)
     {
       if (bfd_get_error () != bfd_error_file_truncated)
-       *errorptr = TRUE;
+       *errorptr = true;
       return EOF;
     }
 
@@ -237,7 +237,7 @@ static void
 srec_bad_byte (bfd *abfd,
               unsigned int lineno,
               int c,
-              bfd_boolean error)
+              bool error)
 {
   if (c == EOF)
     {
@@ -257,7 +257,7 @@ srec_bad_byte (bfd *abfd,
        }
       _bfd_error_handler
        /* xgettext:c-format */
-       (_("%B:%d: Unexpected character `%s' in S-record file\n"),
+       (_("%pB:%d: unexpected character `%s' in S-record file"),
         abfd, lineno, buf);
       bfd_set_error (bfd_error_bad_value);
     }
@@ -265,14 +265,14 @@ srec_bad_byte (bfd *abfd,
 
 /* Add a new symbol found in an S-record file.  */
 
-static bfd_boolean
+static bool
 srec_new_symbol (bfd *abfd, const char *name, bfd_vma val)
 {
   struct srec_symbol *n;
 
   n = (struct srec_symbol *) bfd_alloc (abfd, sizeof (* n));
   if (n == NULL)
-    return FALSE;
+    return false;
 
   n->name = name;
   n->val = val;
@@ -286,18 +286,18 @@ srec_new_symbol (bfd *abfd, const char *name, bfd_vma val)
 
   ++abfd->symcount;
 
-  return TRUE;
+  return true;
 }
 
 /* Read the S record file and turn it into sections.  We create a new
    section for each contiguous set of bytes.  */
 
-static bfd_boolean
+static bool
 srec_scan (bfd *abfd)
 {
   int c;
   unsigned int lineno = 1;
-  bfd_boolean error = FALSE;
+  bool error = false;
   bfd_byte *buf = NULL;
   size_t bufsize = 0;
   asection *sec = NULL;
@@ -485,7 +485,7 @@ srec_scan (bfd *abfd)
            if (bytes < min_bytes)
              {
                /* xgettext:c-format */
-               _bfd_error_handler (_("%B:%d: byte count %d too small\n"),
+               _bfd_error_handler (_("%pB:%d: byte count %d too small"),
                                    abfd, lineno, bytes);
                bfd_set_error (bfd_error_bad_value);
                goto error_return;
@@ -493,8 +493,7 @@ srec_scan (bfd *abfd)
 
            if (bytes * 2 > bufsize)
              {
-               if (buf != NULL)
-                 free (buf);
+               free (buf);
                buf = (bfd_byte *) bfd_malloc ((bfd_size_type) bytes * 2);
                if (buf == NULL)
                  goto error_return;
@@ -550,7 +549,7 @@ srec_scan (bfd *abfd)
                  {
                    char secbuf[20];
                    char *secname;
-                   bfd_size_type amt;
+                   size_t amt;
                    flagword flags;
 
                    sprintf (secbuf, ".sec%d", bfd_count_sections (abfd) + 1);
@@ -578,7 +577,7 @@ srec_scan (bfd *abfd)
                  {
                    _bfd_error_handler
                      /* xgettext:c-format */
-                     (_("%B:%d: Bad checksum in S-record file\n"),
+                     (_("%pB:%d: bad checksum in S-record file"),
                       abfd, lineno);
                    bfd_set_error (bfd_error_bad_value);
                    goto error_return;
@@ -612,16 +611,14 @@ srec_scan (bfd *abfd)
                  {
                    _bfd_error_handler
                      /* xgettext:c-format */
-                     (_("%B:%d: Bad checksum in S-record file\n"),
+                     (_("%pB:%d: bad checksum in S-record file"),
                       abfd, lineno);
                    bfd_set_error (bfd_error_bad_value);
                    goto error_return;
                  }
 
-               if (buf != NULL)
-                 free (buf);
-
-               return TRUE;
+               free (buf);
+               return true;
              }
          }
          break;
@@ -631,22 +628,18 @@ srec_scan (bfd *abfd)
   if (error)
     goto error_return;
 
-  if (buf != NULL)
-    free (buf);
-
-  return TRUE;
+  free (buf);
+  return true;
 
  error_return:
-  if (symbuf != NULL)
-    free (symbuf);
-  if (buf != NULL)
-    free (buf);
-  return FALSE;
+  free (symbuf);
+  free (buf);
+  return false;
 }
 
 /* Check whether an existing file is an S-record file.  */
 
-static const bfd_target *
+static bfd_cleanup
 srec_object_p (bfd *abfd)
 {
   void * tdata_save;
@@ -676,12 +669,12 @@ srec_object_p (bfd *abfd)
   if (abfd->symcount > 0)
     abfd->flags |= HAS_SYMS;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 /* Check whether an existing file is an S-record file with symbols.  */
 
-static const bfd_target *
+static bfd_cleanup
 symbolsrec_object_p (bfd *abfd)
 {
   void * tdata_save;
@@ -711,17 +704,17 @@ symbolsrec_object_p (bfd *abfd)
   if (abfd->symcount > 0)
     abfd->flags |= HAS_SYMS;
 
-  return abfd->xvec;
+  return _bfd_no_cleanup;
 }
 
 /* Read in the contents of a section in an S-record file.  */
 
-static bfd_boolean
+static bool
 srec_read_section (bfd *abfd, asection *section, bfd_byte *contents)
 {
   int c;
   bfd_size_type sofar = 0;
-  bfd_boolean error = FALSE;
+  bool error = false;
   bfd_byte *buf = NULL;
   size_t bufsize = 0;
 
@@ -751,8 +744,7 @@ srec_read_section (bfd *abfd, asection *section, bfd_byte *contents)
 
       if (bytes * 2 > bufsize)
        {
-         if (buf != NULL)
-           free (buf);
+         free (buf);
          buf = (bfd_byte *) bfd_malloc ((bfd_size_type) bytes * 2);
          if (buf == NULL)
            goto error_return;
@@ -768,9 +760,8 @@ srec_read_section (bfd *abfd, asection *section, bfd_byte *contents)
        {
        default:
          BFD_ASSERT (sofar == section->size);
-         if (buf != NULL)
-           free (buf);
-         return TRUE;
+         free (buf);
+         return true;
 
        case '3':
          address = HEX (data);
@@ -793,9 +784,8 @@ srec_read_section (bfd *abfd, asection *section, bfd_byte *contents)
            {
              /* We've come to the end of this section.  */
              BFD_ASSERT (sofar == section->size);
-             if (buf != NULL)
-               free (buf);
-             return TRUE;
+             free (buf);
+             return true;
            }
 
          /* Don't consider checksum.  */
@@ -817,20 +807,17 @@ srec_read_section (bfd *abfd, asection *section, bfd_byte *contents)
 
   BFD_ASSERT (sofar == section->size);
 
-  if (buf != NULL)
-    free (buf);
-
-  return TRUE;
+  free (buf);
+  return true;
 
  error_return:
-  if (buf != NULL)
-    free (buf);
-  return FALSE;
+  free (buf);
+  return false;
 }
 
 /* Get the contents of a section in an S-record file.  */
 
-static bfd_boolean
+static bool
 srec_get_section_contents (bfd *abfd,
                           asection *section,
                           void * location,
@@ -838,60 +825,60 @@ srec_get_section_contents (bfd *abfd,
                           bfd_size_type count)
 {
   if (count == 0)
-    return TRUE;
+    return true;
 
   if (offset + count < count
       || offset + count > section->size)
     {
       bfd_set_error (bfd_error_invalid_operation);
-      return FALSE;
+      return false;
     }
 
   if (section->used_by_bfd == NULL)
     {
       section->used_by_bfd = bfd_alloc (abfd, section->size);
       if (section->used_by_bfd == NULL)
-       return FALSE;
+       return false;
 
       if (! srec_read_section (abfd, section,
                               (bfd_byte *) section->used_by_bfd))
-       return FALSE;
+       return false;
     }
 
   memcpy (location, (bfd_byte *) section->used_by_bfd + offset,
          (size_t) count);
 
-  return TRUE;
+  return true;
 }
 
 /* Set the architecture.  We accept an unknown architecture here.  */
 
-static bfd_boolean
+static bool
 srec_set_arch_mach (bfd *abfd, enum bfd_architecture arch, unsigned long mach)
 {
   if (arch != bfd_arch_unknown)
     return bfd_default_set_arch_mach (abfd, arch, mach);
 
   abfd->arch_info = & bfd_default_arch_struct;
-  return TRUE;
+  return true;
 }
 
 /* We have to save up all the Srecords for a splurge before output.  */
 
-static bfd_boolean
+static bool
 srec_set_section_contents (bfd *abfd,
                           sec_ptr section,
                           const void * location,
                           file_ptr offset,
                           bfd_size_type bytes_to_do)
 {
-  int opb = bfd_octets_per_byte (abfd);
+  int opb = bfd_octets_per_byte (abfd, NULL);
   tdata_type *tdata = abfd->tdata.srec_data;
   srec_data_list_type *entry;
 
   entry = (srec_data_list_type *) bfd_alloc (abfd, sizeof (* entry));
   if (entry == NULL)
-    return FALSE;
+    return false;
 
   if (bytes_to_do
       && (section->flags & SEC_ALLOC)
@@ -901,7 +888,7 @@ srec_set_section_contents (bfd *abfd,
 
       data = (bfd_byte *) bfd_alloc (abfd, bytes_to_do);
       if (data == NULL)
-       return FALSE;
+       return false;
       memcpy ((void *) data, location, (size_t) bytes_to_do);
 
       /* If _bfd_srec_forceS3 is TRUE then always select S3 records,
@@ -943,14 +930,14 @@ srec_set_section_contents (bfd *abfd,
            tdata->tail = entry;
        }
     }
-  return TRUE;
+  return true;
 }
 
 /* Write a record of type, of the supplied number of bytes. The
    supplied bytes and length don't have a checksum. That's worked out
    here.  */
 
-static bfd_boolean
+static bool
 srec_write_record (bfd *abfd,
                   unsigned int type,
                   bfd_vma address,
@@ -1012,21 +999,21 @@ srec_write_record (bfd *abfd,
   return bfd_bwrite ((void *) buffer, wrlen, abfd) == wrlen;
 }
 
-static bfd_boolean
+static bool
 srec_write_header (bfd *abfd)
 {
-  unsigned int len = strlen (abfd->filename);
+  unsigned int len = strlen (bfd_get_filename (abfd));
 
   /* I'll put an arbitrary 40 char limit on header size.  */
   if (len > 40)
     len = 40;
 
   return srec_write_record (abfd, 0, (bfd_vma) 0,
-                           (bfd_byte *) abfd->filename,
-                           (bfd_byte *) abfd->filename + len);
+                           (bfd_byte *) bfd_get_filename (abfd),
+                           (bfd_byte *) bfd_get_filename (abfd) + len);
 }
 
-static bfd_boolean
+static bool
 srec_write_section (bfd *abfd,
                    tdata_type *tdata,
                    srec_data_list_type *list)
@@ -1053,30 +1040,31 @@ srec_write_section (bfd *abfd,
       if (octets_this_chunk > _bfd_srec_len)
        octets_this_chunk = _bfd_srec_len;
 
-      address = list->where + octets_written / bfd_octets_per_byte (abfd);
+      address = list->where + (octets_written
+                              / bfd_octets_per_byte (abfd, NULL));
 
       if (! srec_write_record (abfd,
                               tdata->type,
                               address,
                               location,
                               location + octets_this_chunk))
-       return FALSE;
+       return false;
 
       octets_written += octets_this_chunk;
       location += octets_this_chunk;
     }
 
-  return TRUE;
+  return true;
 }
 
-static bfd_boolean
+static bool
 srec_write_terminator (bfd *abfd, tdata_type *tdata)
 {
   return srec_write_record (abfd, 10 - tdata->type,
                            abfd->start_address, NULL, NULL);
 }
 
-static bfd_boolean
+static bool
 srec_write_symbols (bfd *abfd)
 {
   /* Dump out the symbols of a bfd.  */
@@ -1088,17 +1076,20 @@ srec_write_symbols (bfd *abfd)
       bfd_size_type len;
       asymbol **table = bfd_get_outsymbols (abfd);
 
-      len = strlen (abfd->filename);
+      len = strlen (bfd_get_filename (abfd));
       if (bfd_bwrite ("$$ ", (bfd_size_type) 3, abfd) != 3
-         || bfd_bwrite (abfd->filename, len, abfd) != len
+         || bfd_bwrite (bfd_get_filename (abfd), len, abfd) != len
          || bfd_bwrite ("\r\n", (bfd_size_type) 2, abfd) != 2)
-       return FALSE;
+       return false;
 
       for (i = 0; i < count; i++)
        {
          asymbol *s = table[i];
+
          if (! bfd_is_local_label (abfd, s)
-             && (s->flags & BSF_DEBUGGING) == 0)
+             && (s->flags & BSF_DEBUGGING) == 0
+             && s->section != NULL
+             && s->section->output_section != NULL)
            {
              /* Just dump out non debug symbols.  */
              char buf[43], *p;
@@ -1106,7 +1097,7 @@ srec_write_symbols (bfd *abfd)
              len = strlen (s->name);
              if (bfd_bwrite ("  ", (bfd_size_type) 2, abfd) != 2
                  || bfd_bwrite (s->name, len, abfd) != len)
-               return FALSE;
+               return false;
 
              sprintf_vma (buf + 2, (s->value
                                     + s->section->output_section->lma
@@ -1121,17 +1112,17 @@ srec_write_symbols (bfd *abfd)
              *--p = ' ';
              len += 4;
              if (bfd_bwrite (p, len, abfd) != len)
-               return FALSE;
+               return false;
            }
        }
       if (bfd_bwrite ("$$ \r\n", (bfd_size_type) 5, abfd) != 5)
-       return FALSE;
+       return false;
     }
 
-  return TRUE;
+  return true;
 }
 
-static bfd_boolean
+static bool
 internal_srec_write_object_contents (bfd *abfd, int symbols)
 {
   tdata_type *tdata = abfd->tdata.srec_data;
@@ -1140,11 +1131,11 @@ internal_srec_write_object_contents (bfd *abfd, int symbols)
   if (symbols)
     {
       if (! srec_write_symbols (abfd))
-       return FALSE;
+       return false;
     }
 
   if (! srec_write_header (abfd))
-    return FALSE;
+    return false;
 
   /* Now wander though all the sections provided and output them.  */
   list = tdata->head;
@@ -1152,19 +1143,19 @@ internal_srec_write_object_contents (bfd *abfd, int symbols)
   while (list != (srec_data_list_type *) NULL)
     {
       if (! srec_write_section (abfd, tdata, list))
-       return FALSE;
+       return false;
       list = list->next;
     }
   return srec_write_terminator (abfd, tdata);
 }
 
-static bfd_boolean
+static bool
 srec_write_object_contents (bfd *abfd)
 {
   return internal_srec_write_object_contents (abfd, 0);
 }
 
-static bfd_boolean
+static bool
 symbolsrec_write_object_contents (bfd *abfd)
 {
   return internal_srec_write_object_contents (abfd, 1);
@@ -1257,7 +1248,7 @@ srec_print_symbol (bfd *abfd,
 #define        srec_close_and_cleanup                    _bfd_generic_close_and_cleanup
 #define srec_bfd_free_cached_info                _bfd_generic_bfd_free_cached_info
 #define srec_new_section_hook                    _bfd_generic_new_section_hook
-#define srec_bfd_is_target_special_symbol        ((bfd_boolean (*) (bfd *, asymbol *)) (bfd_boolean (*)) bfd_false)
+#define srec_bfd_is_target_special_symbol        _bfd_bool_bfd_asymbol_false
 #define srec_bfd_is_local_label_name             bfd_generic_is_local_label_name
 #define srec_get_lineno                                  _bfd_nosymbols_get_lineno
 #define srec_find_nearest_line                   _bfd_nosymbols_find_nearest_line
@@ -1275,9 +1266,11 @@ srec_print_symbol (bfd *abfd,
 #define srec_bfd_lookup_section_flags            bfd_generic_lookup_section_flags
 #define srec_bfd_merge_sections                          bfd_generic_merge_sections
 #define srec_bfd_is_group_section                bfd_generic_is_group_section
+#define srec_bfd_group_name                      bfd_generic_group_name
 #define srec_bfd_discard_group                   bfd_generic_discard_group
 #define srec_section_already_linked              _bfd_generic_section_already_linked
 #define srec_bfd_define_common_symbol            bfd_generic_define_common_symbol
+#define srec_bfd_link_hide_symbol                _bfd_generic_link_hide_symbol
 #define srec_bfd_define_start_stop               bfd_generic_define_start_stop
 #define srec_bfd_link_hash_table_create                  _bfd_generic_link_hash_table_create
 #define srec_bfd_link_add_symbols                _bfd_generic_link_add_symbols
@@ -1302,6 +1295,7 @@ const bfd_target srec_vec =
   ' ',                         /* AR_pad_char.  */
   16,                          /* AR_max_namelen.  */
   0,                           /* match priority.  */
+  TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
   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.  */
@@ -1316,16 +1310,16 @@ const bfd_target srec_vec =
     _bfd_dummy_target,
   },
   {
-    bfd_false,
+    _bfd_bool_bfd_false_error,
     srec_mkobject,
     _bfd_generic_mkarchive,
-    bfd_false,
+    _bfd_bool_bfd_false_error,
   },
   {                            /* bfd_write_contents.  */
-    bfd_false,
+    _bfd_bool_bfd_false_error,
     srec_write_object_contents,
     _bfd_write_archive_contents,
-    bfd_false,
+    _bfd_bool_bfd_false_error,
   },
 
   BFD_JUMP_TABLE_GENERIC (srec),
@@ -1358,6 +1352,7 @@ const bfd_target symbolsrec_vec =
   ' ',                         /* AR_pad_char.  */
   16,                          /* AR_max_namelen.  */
   0,                           /* match priority.  */
+  TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
   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.  */
@@ -1372,16 +1367,16 @@ const bfd_target symbolsrec_vec =
     _bfd_dummy_target,
   },
   {
-    bfd_false,
+    _bfd_bool_bfd_false_error,
     srec_mkobject,
     _bfd_generic_mkarchive,
-    bfd_false,
+    _bfd_bool_bfd_false_error,
   },
   {                            /* bfd_write_contents.  */
-    bfd_false,
+    _bfd_bool_bfd_false_error,
     symbolsrec_write_object_contents,
     _bfd_write_archive_contents,
-    bfd_false,
+    _bfd_bool_bfd_false_error,
   },
 
   BFD_JUMP_TABLE_GENERIC (srec),