This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Add --only-keep-debug-and-symbols option to objcopy


Hi!

Following patch on top of Nick's patch from yesterday adds
--only-keep-debug-and-symbols option which does something similar
to eu-strip -f foo.debug foo with the foo.debug file.
Particularly, it keeps debugging sections (like --only-keep-debug),
but also .symtab/.strtab.  Allocated sections are changed into
SHT_NOBITS sections, so that the symbols don't have to be converted
into SHN_ABS symbols.
Ok to commit?

2004-01-09  Jakub Jelinek  <jakub@redhat.com>

	* objcopy.c: Include elf-bfd.h.
	(enum strip_action): Add STRIP_NONDEBUGSYM.
	(enum command_line_switch): Add OPTION_ONLY_KEEP_DEBUG_SYMS.
	(strip_options, copy_options): Add --only-keep-debug-and-symbols
	option.
	(copy_usage, strip_usage): Likewise.
	(is_strip_section): Don't strip debugging sections if
	STRIP_NONDEBUGSYM.
	(copy_object): Handle STRIP_NONDEBUGSYM like STRIP_NONDEBUG.
	(setup_section): If STRIP_NONDEBUGSYM make SEC_ALLOC sections
	~(SEC_LOAD | SEC_HAS_CONTENTS) and on ELF targets also SHT_NOBITS.
	Don't copy private data if STRIP_NONDEBUGSYM.
	(strip_main, copy_main): Handle OPTION_ONLY_KEEP_DEBUG_SYMS.

	* objcopy.c (copy_section): Free relpp if relcount == 0.

--- binutils/objcopy.c.jj	2004-01-09 12:20:46.000000000 +0100
+++ binutils/objcopy.c	2004-01-09 16:08:00.000000000 +0100
@@ -28,6 +28,7 @@
 #include "budbg.h"
 #include "filenames.h"
 #include "fnmatch.h"
+#include "elf-bfd.h"
 #include <sys/stat.h>
 
 /* A list of symbols to explicitly strip out, or to keep.  A linked
@@ -81,6 +82,8 @@ enum strip_action
     STRIP_DEBUG,		/* Strip all debugger symbols.  */
     STRIP_UNNEEDED,		/* Strip unnecessary symbols.  */
     STRIP_NONDEBUG,		/* Strip everything but debug info.  */
+    STRIP_NONDEBUGSYM,		/* Strip everything but debug info
+				   and symbols.  */
     STRIP_ALL			/* Strip all symbols.  */
   };
 
@@ -242,6 +245,7 @@ enum command_line_switch
     OPTION_FORMATS_INFO,
     OPTION_ADD_GNU_DEBUGLINK,
     OPTION_ONLY_KEEP_DEBUG,
+    OPTION_ONLY_KEEP_DEBUG_SYMS,
     OPTION_READONLY_TEXT,
     OPTION_WRITABLE_TEXT,
     OPTION_PURE,
@@ -261,6 +265,7 @@ static struct option strip_options[] =
   {"input-target", required_argument, 0, 'I'},
   {"keep-symbol", required_argument, 0, 'K'},
   {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
+  {"only-keep-debug-and-symbols", no_argument, 0, OPTION_ONLY_KEEP_DEBUG_SYMS},
   {"output-format", required_argument, 0, 'O'},	/* Obsolete */
   {"output-target", required_argument, 0, 'O'},
   {"output-file", required_argument, 0, 'o'},
@@ -317,6 +322,7 @@ static struct option copy_options[] =
   {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
   {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
   {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
+  {"only-keep-debug-and-symbols", no_argument, 0, OPTION_ONLY_KEEP_DEBUG_SYMS},
   {"only-section", required_argument, 0, 'j'},
   {"output-format", required_argument, 0, 'O'},	/* Obsolete */
   {"output-target", required_argument, 0, 'O'},
@@ -404,6 +410,8 @@ copy_usage (FILE *stream, int exit_statu
      --strip-unneeded              Remove all symbols not needed by relocations\n\
   -N --strip-symbol <name>         Do not copy symbol <name>\n\
      --only-keep-debug             Strip everything but the debug information\n\
+     --only-keep-debug-and-symbols Strip everything but the debug information\n\
+				   and symbols\n\
   -K --keep-symbol <name>          Only copy symbol <name>\n\
   -L --localize-symbol <name>      Force symbol <name> to be marked as a local\n\
   -G --keep-global-symbol <name>   Localize all symbols except <name>\n\
@@ -482,6 +490,8 @@ strip_usage (FILE *stream, int exit_stat
   -g -S -d --strip-debug           Remove all debugging symbols & sections\n\
      --strip-unneeded              Remove all symbols not needed by relocations\n\
      --only-keep-debug             Strip everything but the debug information\n\
+     --only-keep-debug-and-symbols Strip everything but the debug information\n\
+				   and symbols\n\
   -N --strip-symbol=<name>         Do not copy symbol <name>\n\
   -K --keep-symbol=<name>          Only copy symbol <name>\n\
   -w --wildcard                    Permit wildcard in symbol comparasion\n\
@@ -772,7 +782,8 @@ is_strip_section (bfd *abfd ATTRIBUTE_UN
 	  || convert_debugging)
 	return TRUE;
 
-      if (strip_symbols == STRIP_NONDEBUG)
+      if (strip_symbols == STRIP_NONDEBUG
+	  || strip_symbols == STRIP_NONDEBUGSYM)
 	return FALSE;
     }
 
@@ -1366,6 +1377,7 @@ copy_object (bfd *ibfd, bfd *obfd)
       || strip_symbols == STRIP_ALL
       || strip_symbols == STRIP_UNNEEDED
       || strip_symbols == STRIP_NONDEBUG
+      || strip_symbols == STRIP_NONDEBUGSYM
       || discard_locals != LOCALS_UNDEF
       || strip_specific_list != NULL
       || keep_specific_list != NULL
@@ -1486,7 +1498,8 @@ copy_object (bfd *ibfd, bfd *obfd)
      permit the routine to look at the filtered symbol table, which is
      important for the ECOFF code at least.  */
   if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
-      && strip_symbols == STRIP_NONDEBUG)
+      && (strip_symbols == STRIP_NONDEBUG
+	  || strip_symbols == STRIP_NONDEBUGSYM))
     /* Do not copy the private data when creating an ELF format
        debug info file.  We do not want the program headers.  */
     ;
@@ -1909,6 +1922,13 @@ setup_section (bfd *ibfd, sec_ptr isecti
 
   if (p != NULL && p->set_flags)
     flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
+  else if (strip_symbols == STRIP_NONDEBUGSYM && (flags & SEC_ALLOC) != 0)
+    {
+      flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
+      if (obfd->xvec->flavour == bfd_target_elf_flavour)
+	elf_section_type (osection) = SHT_NOBITS;
+    }
+
   if (!bfd_set_section_flags (obfd, osection, flags))
     {
       err = _("flags");
@@ -1927,7 +1947,8 @@ setup_section (bfd *ibfd, sec_ptr isecti
   /* Allow the BFD backend to copy any private data it understands
      from the input section to the output section.  */
   if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
-      && strip_symbols == STRIP_NONDEBUG)
+      && (strip_symbols == STRIP_NONDEBUG
+	  || strip_symbols == STRIP_NONDEBUGSYM))
     /* Do not copy the private data when creating an ELF format
        debug info file.  We do not want the program headers.  */
     ;
@@ -2029,6 +2050,8 @@ copy_section (bfd *ibfd, sec_ptr isectio
 	}
 
       bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
+      if (relcount == 0)
+	free (relpp);
     }
 
   isection->_cooked_size = isection->_raw_size;
@@ -2320,6 +2343,9 @@ strip_main (int argc, char *argv[])
 	case OPTION_ONLY_KEEP_DEBUG:
 	  strip_symbols = STRIP_NONDEBUG;
 	  break;
+	case OPTION_ONLY_KEEP_DEBUG_SYMS:
+	  strip_symbols = STRIP_NONDEBUGSYM;
+	  break;
 	case 0:
 	  /* We've been given a long option.  */
 	  break;
@@ -2477,6 +2503,10 @@ copy_main (int argc, char *argv[])
 	  strip_symbols = STRIP_NONDEBUG;
 	  break;
 
+	case OPTION_ONLY_KEEP_DEBUG_SYMS:
+	  strip_symbols = STRIP_NONDEBUGSYM;
+	  break;
+
 	case OPTION_ADD_GNU_DEBUGLINK:
 	  gnu_debuglink_filename = optarg;
 	  break;

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]