readelf: increase size of static buffers
[deliverable/binutils-gdb.git] / binutils / objcopy.c
index fd94d63773e9b8ee4b73358c38987e7ddee7bd7d..738ef4c2c9135742d6c0518a3bac631574e8fa58 100644 (file)
@@ -1549,12 +1549,13 @@ filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
 
       /* Check if we will remove the current leading character.  */
       rem_leading_char =
-       (name[0] == bfd_get_symbol_leading_char (abfd))
-       && (change_leading_char
-           || (remove_leading_char
-               && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
-                   || undefined
-                   || bfd_is_com_section (bfd_asymbol_section (sym)))));
+       (name[0] != '\0'
+        && name[0] == bfd_get_symbol_leading_char (abfd)
+        && (change_leading_char
+            || (remove_leading_char
+                && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
+                    || undefined
+                    || bfd_is_com_section (bfd_asymbol_section (sym))))));
 
       /* Check if we will add a new leading character.  */
       add_leading_char =
@@ -1580,9 +1581,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);
 
@@ -2556,6 +2562,23 @@ merge_gnu_build_notes (bfd *          abfd,
   return size;
 }
 
+static flagword
+check_new_section_flags (flagword flags, bfd * abfd, const char * secname)
+{
+  /* Only set the SEC_COFF_SHARED flag on COFF files.
+     The same bit value is used by ELF targets to indicate
+     compressed sections, and setting that flag here breaks
+     things.  */
+  if ((flags & SEC_COFF_SHARED)
+      && bfd_get_flavour (abfd) != bfd_target_coff_flavour)
+    {
+      non_fatal (_("%s[%s]: Note - dropping 'share' flag as output format is not COFF"),
+                bfd_get_filename (abfd), secname);
+      flags &= ~ SEC_COFF_SHARED;
+    }
+  return flags;
+}
+
 /* Copy object file IBFD onto OBFD.
    Returns TRUE upon success, FALSE otherwise.  */
 
@@ -2573,7 +2596,7 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
   void *dhandle;
   enum bfd_architecture iarch;
   unsigned int imach;
-  unsigned int c, i;
+  unsigned int num_sec, i;
 
   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
@@ -2751,6 +2774,11 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
 
                     file_alignment, section_alignment);
        }
+
+      if (preserve_dates
+         && bfd_get_flavour (ibfd) == bfd_target_coff_flavour
+         && bfd_pei_p (ibfd))
+       pe->timestamp = pe_data (ibfd)->coff.timestamp;
     }
 
   if (isympp)
@@ -2804,7 +2832,10 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
          pset = find_section_list (padd->name, FALSE,
                                    SECTION_CONTEXT_SET_FLAGS);
          if (pset != NULL)
-           flags = pset->flags | SEC_HAS_CONTENTS;
+           {         
+             flags = pset->flags | SEC_HAS_CONTENTS;
+             flags = check_new_section_flags (flags, obfd, padd->name);
+           }
          else
            flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
 
@@ -3075,8 +3106,8 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
        }
     }
 
-  c = bfd_count_sections (obfd);
-  if (c != 0
+  num_sec = bfd_count_sections (obfd);
+  if (num_sec != 0
       && (gap_fill_set || pad_to_set))
     {
       asection **set;
@@ -3087,22 +3118,24 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
         increasing the section sizes as required to fill the gaps.
         We write out the gap contents below.  */
 
-      osections = (asection **) xmalloc (c * sizeof (asection *));
+      osections = xmalloc (num_sec * sizeof (*osections));
       set = osections;
       bfd_map_over_sections (obfd, get_sections, &set);
 
-      qsort (osections, c, sizeof (asection *), compare_section_lma);
+      qsort (osections, num_sec, sizeof (*osections), compare_section_lma);
 
-      gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
-      memset (gaps, 0, c * sizeof (bfd_size_type));
+      gaps = xmalloc (num_sec * sizeof (*gaps));
+      memset (gaps, 0, num_sec * sizeof (*gaps));
 
       if (gap_fill_set)
        {
-         for (i = 0; i < c - 1; i++)
+         for (i = 0; i < num_sec - 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
@@ -3110,8 +3143,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],
@@ -3131,24 +3164,26 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
 
       if (pad_to_set)
        {
-         bfd_vma lma;
-         bfd_size_type size;
-
-         lma = bfd_section_lma (osections[c - 1]);
-         size = bfd_section_size (osections[c - 1]);
-         if (lma + size < pad_to)
+         bfd_vma lma;         /* Octets.  */
+         bfd_size_type size;  /* Octets.  */
+         unsigned int opb = bfd_octets_per_byte (obfd, osections[num_sec - 1]);
+         bfd_vma _pad_to = pad_to * opb;
+
+         lma = bfd_section_lma (osections[num_sec - 1]) * opb;
+         size = bfd_section_size (osections[num_sec - 1]);
+         if (lma + size < _pad_to)
            {
-             if (!bfd_set_section_size (osections[c - 1], pad_to - lma))
+             if (!bfd_set_section_size (osections[num_sec - 1], _pad_to - lma))
                {
-                 bfd_nonfatal_message (NULL, obfd, osections[c - 1],
+                 bfd_nonfatal_message (NULL, obfd, osections[num_sec - 1],
                                        _("can't add padding"));
                  status = 1;
                }
              else
                {
-                 gaps[c - 1] = pad_to - (lma + size);
-                 if (max_gap < pad_to - (lma + size))
-                   max_gap = pad_to - (lma + size);
+                 gaps[num_sec - 1] = _pad_to - (lma + size);
+                 if (max_gap < _pad_to - (lma + size))
+                   max_gap = _pad_to - (lma + size);
                }
            }
        }
@@ -3346,7 +3381,7 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
        }
     }
 
-  if (gap_fill_set || pad_to_set)
+  if (gaps != NULL)
     {
       bfd_byte *buf;
 
@@ -3356,8 +3391,7 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
       buf = (bfd_byte *) xmalloc (max_gap);
       memset (buf, gap_fill, max_gap);
 
-      c = bfd_count_sections (obfd);
-      for (i = 0; i < c; i++)
+      for (i = 0; i < num_sec; i++)
        {
          if (gaps[i] != 0)
            {
@@ -3940,6 +3974,7 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
   flagword flags;
   const char *err;
   const char * name;
+  const char * new_name;
   char *prefix = NULL;
   bfd_boolean make_nobits;
   unsigned int alignment;
@@ -3955,7 +3990,12 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
       flags &= bfd_applicable_section_flags (ibfd);
       flags &= bfd_applicable_section_flags (obfd);
     }
-  name = find_section_rename (name, &flags);
+  new_name = find_section_rename (name, &flags);
+  if (new_name != name)
+    {
+      name = new_name;
+      flags = check_new_section_flags (flags, obfd, name);
+    }
 
   /* Prefix sections.  */
   if (prefix_alloc_sections_string
@@ -3979,7 +4019,10 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
   p = find_section_list (bfd_section_name (isection), FALSE,
                         SECTION_CONTEXT_SET_FLAGS);
   if (p != NULL)
-    flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
+    {
+      flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
+      flags = check_new_section_flags (flags, obfd, bfd_section_name (isection));
+    }
   else if (strip_symbols == STRIP_NONDEBUG
           && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
           && !is_nondebug_keep_contents_section (ibfd, isection))
This page took 0.03392 seconds and 4 git commands to generate.