]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - binutils/objcopy.c
PR25629, objcopy : SIGSEGV in filter_symbols
[thirdparty/binutils-gdb.git] / binutils / objcopy.c
index ef3b693be4916106d4afd132f50ec90b10b125dc..3b9f1b73ca90b52d46091a39856ecf8e9a65bb57 100644 (file)
@@ -780,6 +780,7 @@ parse_flags (const char *s)
       PARSE_FLAG ("code", SEC_CODE);
       PARSE_FLAG ("data", SEC_DATA);
       PARSE_FLAG ("rom", SEC_ROM);
+      PARSE_FLAG ("exclude", SEC_EXCLUDE);
       PARSE_FLAG ("share", SEC_COFF_SHARED);
       PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
       PARSE_FLAG ("merge", SEC_MERGE);
@@ -794,7 +795,7 @@ parse_flags (const char *s)
          copy[len] = '\0';
          non_fatal (_("unrecognized section flag `%s'"), copy);
          fatal (_("supported flags: %s"),
-                "alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings");
+                "alloc, load, noload, readonly, debug, code, data, rom, exclude, share, contents, merge, strings");
        }
 
       s = snext;
@@ -1579,9 +1580,14 @@ filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
       if (add_leading_char || prefix_symbols_string)
        {
          char *n, *ptr;
+         size_t len = strlen (name) + 1;
 
-         ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string)
-                                     + strlen (name) + 1);
+         if (add_leading_char)
+           len++;
+         if (prefix_symbols_string)
+           len += strlen (prefix_symbols_string);
+
+         ptr = n = (char *) xmalloc (len);
          if (add_leading_char)
            *ptr++ = bfd_get_symbol_leading_char (obfd);
 
@@ -1945,7 +1951,7 @@ typedef struct objcopy_internal_note
   bfd_vma            start;
   bfd_vma            end;
 } objcopy_internal_note;
-  
+
 #define DEBUG_MERGE 0
 
 #if DEBUG_MERGE
@@ -2041,7 +2047,7 @@ compare_gnu_build_notes (const void * data1, const void * data2)
                    pnote1->note.namesz - 3 : pnote2->note.namesz - 3);
   if (cmp)
     return cmp;
-  
+
   if (pnote1->end < pnote2->start)
     return -1;
   if (pnote1->start > pnote2->end)
@@ -2052,13 +2058,15 @@ compare_gnu_build_notes (const void * data1, const void * data2)
     return -1;
   if (pnote1->end > pnote2->end)
     return 1;
-  
+  if (pnote1->end < pnote2->end)
+    return -1;
+
   /* Put OPEN notes before function notes.  */
   if (is_open_note (pnote1) && ! is_open_note (pnote2))
     return -1;
   if (! is_open_note (pnote1) && is_open_note (pnote2))
     return 1;
-  
+
   return 0;
 }
 
@@ -2086,7 +2094,7 @@ sort_gnu_build_notes (const void * data1, const void * data2)
 
       return 1;                                /* 1: F   2: O   */
     }
-  
+
   /* Sort by starting address.  */
   if (pnote1->start < pnote2->start)
     return -1;
@@ -2104,7 +2112,7 @@ sort_gnu_build_notes (const void * data1, const void * data2)
       && pnote2->note.namesz > 4
       && pnote1->note.namedata[3] != pnote2->note.namedata[3])
     return pnote1->note.namedata[3] - pnote2->note.namedata[3];
-  
+
   return 0;
 }
 
@@ -2151,7 +2159,7 @@ merge_gnu_build_notes (bfd *          abfd,
          goto done;
        }
     }
-  
+
   /* Make a copy of the notes and convert to our internal format.
      Minimum size of a note is 12 bytes.  Also locate the version
      notes and check them.  */
@@ -2232,7 +2240,7 @@ merge_gnu_build_notes (bfd *          abfd,
             address.  */
          end = (bfd_vma) -1;
          break;
-         
+
        case 8:
          if (! is_64bit (abfd))
            {
@@ -2257,7 +2265,7 @@ merge_gnu_build_notes (bfd *          abfd,
          start = bfd_get_64 (abfd, pnote->note.descdata);
          end = bfd_get_64 (abfd, pnote->note.descdata + 8);
          break;
-         
+
        default:
          err = _("corrupt GNU build attribute note: bad description size");
          goto done;
@@ -2396,7 +2404,7 @@ merge_gnu_build_notes (bfd *          abfd,
              || memcmp (back->note.namedata,
                         pnote->note.namedata, pnote->note.namesz) != 0)
            break;
-         
+
          if (back->start == pnote->start
              && back->end == pnote->end)
            {
@@ -2450,7 +2458,7 @@ merge_gnu_build_notes (bfd *          abfd,
       if (! is_deleted_note (pnote))
        merge_debug ("Unable to do anything with note at %#08lx\n",
                     (pnote->note.namedata - (char *) contents) - 12);
-#endif              
+#endif
     }
 
   /* Resort the notes.  */
@@ -2532,7 +2540,7 @@ merge_gnu_build_notes (bfd *          abfd,
                   pnote->note.namesz
                   );
 #endif
-  
+
   new_size = new - new_contents;
   if (new_size < size)
     {
@@ -3098,8 +3106,10 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
          for (i = 0; i < c - 1; i++)
            {
              flagword flags;
-             bfd_size_type size;
-             bfd_vma gap_start, gap_stop;
+             bfd_size_type size;           /* Octets.  */
+             bfd_vma gap_start, gap_stop;  /* Octets.  */
+             unsigned int opb1 = bfd_octets_per_byte (obfd, osections[i]);
+             unsigned int opb2 = bfd_octets_per_byte (obfd, osections[i+1]);
 
              flags = bfd_section_flags (osections[i]);
              if ((flags & SEC_HAS_CONTENTS) == 0
@@ -3107,8 +3117,8 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
                continue;
 
              size = bfd_section_size (osections[i]);
-             gap_start = bfd_section_lma (osections[i]) + size;
-             gap_stop = bfd_section_lma (osections[i + 1]);
+             gap_start = bfd_section_lma (osections[i]) * opb1 + size;
+             gap_stop = bfd_section_lma (osections[i + 1]) * opb2;
              if (gap_start < gap_stop)
                {
                  if (!bfd_set_section_size (osections[i],
@@ -3128,14 +3138,16 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
 
       if (pad_to_set)
        {
-         bfd_vma lma;
-         bfd_size_type size;
+         bfd_vma lma;         /* Octets.  */
+         bfd_size_type size;  /* Octets.  */
+         unsigned int opb = bfd_octets_per_byte (obfd, osections[c - 1]);
+         bfd_vma _pad_to = pad_to * opb;
 
-         lma = bfd_section_lma (osections[c - 1]);
+         lma = bfd_section_lma (osections[c - 1]) * opb;
          size = bfd_section_size (osections[c - 1]);
-         if (lma + size < pad_to)
+         if (lma + size < _pad_to)
            {
-             if (!bfd_set_section_size (osections[c - 1], pad_to - lma))
+             if (!bfd_set_section_size (osections[c - 1], _pad_to - lma))
                {
                  bfd_nonfatal_message (NULL, obfd, osections[c - 1],
                                        _("can't add padding"));
@@ -3143,9 +3155,9 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
                }
              else
                {
-                 gaps[c - 1] = pad_to - (lma + size);
-                 if (max_gap < pad_to - (lma + size))
-                   max_gap = pad_to - (lma + size);
+                 gaps[c - 1] = _pad_to - (lma + size);
+                 if (max_gap < _pad_to - (lma + size))
+                   max_gap = _pad_to - (lma + size);
                }
            }
        }
@@ -4057,7 +4069,7 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
     alignment = p->alignment;
   else
     alignment = bfd_section_alignment (isection);
-  
+
   /* FIXME: This is probably not enough.  If we change the LMA we
      may have to recompute the header for the file as well.  */
   if (!bfd_set_section_alignment (osection, alignment))
@@ -5468,7 +5480,7 @@ copy_main (int argc, char *argv[])
            s = strchr (optarg, '=');
            if (s == NULL)
              fatal (_("bad format for --set-section-alignment: argument needed"));
-           
+
            align = atoi (s + 1);
            if (align <= 0)
              fatal (_("bad format for --set-section-alignment: numeric argument needed"));
@@ -5480,7 +5492,7 @@ copy_main (int argc, char *argv[])
                align >>= 1;
                ++palign;
              }
-           
+
            if (align != 1)
              /* Number has more than on 1, i.e. wasn't a power of 2.  */
              fatal (_("bad format for --set-section-alignment: alignment is not a power of two"));
@@ -5496,7 +5508,7 @@ copy_main (int argc, char *argv[])
              p->alignment = palign;
          }
          break;
-         
+
        case OPTION_RENAME_SECTION:
          {
            flagword flags;