bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new feature: objcopy --rename-section


From: Nick Clifton
Subject: new feature: objcopy --rename-section
Date: 31 Jul 2001 14:48:00 +0100

Hi Guys,

  I am applying the patch below to implement a new command line switch
  in objcopy.  This switch, --rename-section can be used to change a
  section's name.  Although a similar effect can be achieved using
  linker scripts, this method is simpler.  The switch also has the
  added bonus of allowing arbitrary sections to be created for binary
  input files, instead of always forcing them to be called .data.  eg:

   objcopy -I binary -O <output_format> -B <arch> \
    --rename-section .data=.rodata,alloc,load,readonly,data,contents \
    <binary_input_file> <object_output_file>

  I also took this opportunity to fix up some of the formatting in the
  objcopy.c source file.

Cheers
        Nick

2001-07-31  Nick Clifton  <address@hidden>

        * objcopy.c: Fix formatting.
        (section_rename): New structure.
        (OPTION_RENAME_SECTION): New switch.
        (copy_usage): Document new switch.
        (add_section_rename): New function: Add a node to the section
        rename list.
        (find_section_rename): New function: Find the new name and
        flags for a section.
        (setup_section): Call find_section_name to determine the name
        of the output section.
        (copy_main): Handle OPTION_RENAME_SECTION.
        * binutils.texi: Document new switch.
        * NEWS: Mention new switch.

Index: binutils/objcopy.c
===================================================================
RCS file: /cvs/src/src/binutils/objcopy.c,v
retrieving revision 1.24
diff -p -w -r1.24 objcopy.c
*** objcopy.c   2001/07/06 08:05:27     1.24
--- objcopy.c   2001/07/31 13:42:11
*************** struct redefine_node
*** 48,53 ****
--- 48,65 ----
    struct redefine_node *next;
  };
  
+ typedef struct section_rename
+ {
+   const char *            old_name;
+   const char *            new_name;
+   flagword                flags;
+   struct section_rename * next;
+ }
+ section_rename;
+ 
+ /* List of sections to be renamed.  */
+ static section_rename * section_rename_list;
+ 
  static void copy_usage PARAMS ((FILE *, int));
  static void strip_usage PARAMS ((FILE *, int));
  static flagword parse_flags PARAMS ((const char *));
*************** static int strip_main PARAMS ((int, char
*** 73,78 ****
--- 85,92 ----
  static int copy_main PARAMS ((int, char **));
  static const char *lookup_sym_redefinition PARAMS((const char *));
  static void redefine_list_append PARAMS ((const char *, const char *));
+ static const char * find_section_rename PARAMS ((bfd *, sec_ptr, flagword *));
+ static void add_section_rename PARAMS ((const char *, const char *, 
flagword));
  
  #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
  
*************** static struct symlist *weaken_specific_l
*** 196,202 ****
  static struct redefine_node *redefine_sym_list = NULL;
  
  /* If this is true, we weaken global symbols (set BSF_WEAK).  */
- 
  static boolean weaken = false;
  
  /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
--- 210,215 ----
*************** static boolean weaken = false;
*** 226,231 ****
--- 239,245 ----
  #define OPTION_LOCALIZE_SYMBOLS (OPTION_KEEP_SYMBOLS + 1)
  #define OPTION_KEEPGLOBAL_SYMBOLS (OPTION_LOCALIZE_SYMBOLS + 1)
  #define OPTION_WEAKEN_SYMBOLS (OPTION_KEEPGLOBAL_SYMBOLS + 1)
+ #define OPTION_RENAME_SECTION (OPTION_WEAKEN_SYMBOLS + 1)
  
  /* Options to handle if running as "strip".  */
  
*************** static struct option copy_options[] =
*** 292,297 ****
--- 306,312 ----
    {"keep-global-symbol", required_argument, 0, 'G'},
    {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
    {"remove-section", required_argument, 0, 'R'},
+   {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
    {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
    {"set-start", required_argument, 0, OPTION_SET_START},
    {"strip-all", no_argument, 0, 'S'},
*************** extern boolean S3Forced;
*** 334,339 ****
--- 349,355 ----
  /* Defined in bfd/binary.c.  Used to set architecture of input binary files.  
*/
  extern enum bfd_architecture bfd_external_binary_architecture;
  
+ 
  static void
  copy_usage (stream, exit_status)
       FILE *stream;
*************** copy_usage (stream, exit_status)
*** 381,386 ****
--- 397,403 ----
       --set-section-flags <name>=<flags>\n\
                                     Set section <name>'s properties to 
<flags>\n\
       --add-section <name>=<file>   Add section <name> found in <file> to 
output\n\
+      --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
       --change-leading-char         Force output format's leading character 
style\n\
       --remove-leading-char         Remove leading character from global 
symbols\n\
       --redefine-sym <old>=<new>    Redefine symbol name <old> to <new>\n\
*************** copy_file (input_filename, output_filena
*** 1415,1424 ****
      }
  }
  
! /* Create a section in OBFD with the same name and attributes
!    as ISECTION in IBFD.  */
  
  static void
  setup_section (ibfd, isection, obfdarg)
       bfd *ibfd;
       sec_ptr isection;
--- 1423,1492 ----
      }
  }
  
! /* Add a name to the section renaming list.  */
  
  static void
+ add_section_rename (old_name, new_name, flags)
+      const char * old_name;
+      const char * new_name;
+      flagword flags;
+ {
+   section_rename * rename;
+ 
+   /* Check for conflicts first.  */
+   for (rename = section_rename_list; rename != NULL; rename = rename->next)
+     if (strcmp (rename->old_name, old_name) == 0)
+       {
+       /* Silently ignore duplicate definitions.  */
+       if (strcmp (rename->new_name, new_name) == 0
+           && rename->flags == flags)
+         return;
+       
+       fatal (_("Multiple renames of section %s"), old_name);
+       }
+ 
+   rename = (section_rename *) xmalloc (sizeof (* rename));
+ 
+   rename->old_name = old_name;
+   rename->new_name = new_name;
+   rename->flags    = flags;
+   rename->next     = section_rename_list;
+   
+   section_rename_list = rename;
+ }
+ 
+ /* Check the section rename list for a new name of the input section
+    ISECTION.  Return the new name if one is found.
+    Also set RETURNED_FLAGS to the flags to be used for this section.  */
+ 
+ static const char *
+ find_section_rename (ibfd, isection, returned_flags)
+      bfd * ibfd ATTRIBUTE_UNUSED;
+      sec_ptr isection;
+      flagword * returned_flags;
+ {
+   const char * old_name = bfd_section_name (ibfd, isection);
+   section_rename * rename;
+ 
+   /* Default to using the flags of the input section.  */
+   * returned_flags = bfd_get_section_flags (ibfd, isection);
+ 
+   for (rename = section_rename_list; rename != NULL; rename = rename->next)
+     if (strcmp (rename->old_name, old_name) == 0)
+       {
+       if (rename->flags != (flagword) -1)
+         * returned_flags = rename->flags;
+ 
+       return rename->new_name;
+       }
+ 
+   return old_name;
+ }
+ 
+ /* Create a section in OBFD with the same
+    name and attributes as ISECTION in IBFD.  */
+ 
+ static void
  setup_section (ibfd, isection, obfdarg)
       bfd *ibfd;
       sec_ptr isection;
*************** setup_section (ibfd, isection, obfdarg)
*** 1432,1437 ****
--- 1500,1506 ----
    bfd_vma lma;
    flagword flags;
    const char *err;
+   const char * name;
    
    if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
        && (strip_symbols == STRIP_DEBUG
*************** setup_section (ibfd, isection, obfdarg)
*** 1450,1457 ****
    if (sections_copied && (p == NULL || ! p->copy))
      return;
  
!   osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, 
isection));
  
    if (osection == NULL)
      {
        err = _("making");
--- 1519,1529 ----
    if (sections_copied && (p == NULL || ! p->copy))
      return;
  
!   /* Get the, possibly new, name of the output section.  */
!   name = find_section_rename (ibfd, isection, & flags);
    
+   osection = bfd_make_section_anyway (obfd, name);
+ 
    if (osection == NULL)
      {
        err = _("making");
*************** setup_section (ibfd, isection, obfdarg)
*** 1507,1513 ****
        goto loser;
      }
  
-   flags = bfd_get_section_flags (ibfd, isection);
    if (p != NULL && p->set_flags)
      flags = p->flags | (flags & SEC_HAS_CONTENTS);
    if (!bfd_set_section_flags (obfd, osection, flags))
--- 1579,1584 ----
*************** copy_main (argc, argv)
*** 2311,2316 ****
--- 2381,2434 ----
          }
          break;
  
+       case OPTION_RENAME_SECTION:
+         {
+           flagword flags;
+           const char * s;
+           char * old_name;
+           char * new_name;
+           unsigned int len;
+ 
+           s = strchr (optarg, '=');
+           if (s == NULL)
+             fatal (_("bad format for %s"), "--rename-section");
+ 
+           len = s - optarg;
+           if (len == 0)
+             fatal (_("no old name is %s"), "--rename-section");
+ 
+           old_name = (char *) xmalloc (len + 1);
+           strncpy (old_name, optarg, len);
+           old_name[len] = 0;
+ 
+           s = strchr (optarg + len, ',');
+           if (s)
+             {
+               unsigned int new_len;
+ 
+               flags = parse_flags (s + 1);
+               new_len = s - (optarg + len);
+               if (new_len == 0)
+                 fatal (_("no new name in %s"), "--rename-section");
+               new_name = (char *) xmalloc (new_len + 1);
+               strncpy (new_name, optarg + len, new_len);
+               new_name [new_len] = 0;
+             }
+           else
+             {
+               s = optarg + len;
+               len = strlen (s);
+               if (len == 0)
+                 fatal (_("no new name in %s"), "--rename-section");
+               new_name = (char *) xmalloc (len + 1);
+               strcpy (new_name, s);
+               flags = -1;
+             }
+ 
+           add_section_rename (old_name, new_name, flags);
+         }
+         break;
+ 
        case OPTION_SET_START:
          set_start = parse_vma (optarg, "--set-start");
          set_start_set = true;

Index: binutils/NEWS
===================================================================
RCS file: /cvs/src/src/binutils/NEWS,v
retrieving revision 1.17
diff -p -w -r1.17 NEWS
*** NEWS        2001/07/05 07:49:05     1.17
--- NEWS        2001/07/31 13:42:12
***************
*** 1,5 ****
--- 1,7 ----
  -*- text -*-
  
+ * objcopy: Add --rename-section to change section names.
+ 
  * readelf: Support added for DWARF 2.1 extensions.  Support added for
    displaying the contents of .debug.macinfo sections.
  
Index: binutils/doc/binutils.texi
===================================================================
RCS file: /cvs/src/src/binutils/doc/binutils.texi,v
retrieving revision 1.2
diff -p -w -r1.2 binutils.texi
*** binutils.texi       2001/07/05 07:49:05     1.2
--- binutils.texi       2001/07/31 13:42:20
*************** objcopy [ -F @var{bfdname} | address@hidden
*** 941,946 ****
--- 941,947 ----
          [ --change-warnings ] [ --no-change-warnings ]
          [ --set-section-flags @address@hidden ]
          [ --add-section @address@hidden ]
+         [ --rename-section @address@hidden,@var{flags}] ]
          [ --change-leading-char ] [ --remove-leading-char ]
          [ address@hidden ] [ --srec-forceS3 ]
          [ --redefine-sym @address@hidden ] [ --weaken ]
*************** Add a new section named @var{sectionname
*** 1209,1214 ****
--- 1210,1233 ----
  contents of the new section are taken from the file @var{filename}.  The
  size of the section will be the size of the file.  This option only
  works on file formats which can support sections with arbitrary names.
+ 
+ @item --rename-section @address@hidden,@var{flags}]
+ Rename a section from @var{oldname} to @var{newname}, optionally
+ changing the section's flags to @var{flags} in the process.  This has
+ the advantage over usng a linker script to perform the rename in that
+ the output stays as an object file and does not become a linked
+ executable.
+ 
+ This option is particularly helpful when the input format is binary,
+ since this will always create a section called .data.  If for example,
+ you wanted instead to create a section called .rodata containing binary
+ data you could use the following command line to achieve it:
+ 
+ @smallexample
+   objcopy -I binary -O <output_format> -B <architecture> \
+    --rename-section .data=.rodata,alloc,load,readonly,data,contents \
+    <input_binary_file> <output_object_file>
+ @end smallexample
  
  @item --change-leading-char
  Some object file formats use special characters at the start of




reply via email to

[Prev in Thread] Current Thread [Next in Thread]