Adds two new output options:
--keep-section=SECTION Keep the named section. SECTION is an extended
wildcard pattern. May be given more than once.
--remove-section=SECTION Remove the named section. SECTION is an
extended wildcard pattern. May be given more than
once. Only non-allocated sections can be removed.
The --remove-section was already partially implemented, but only for the
.comment section. The short option -R is to be compatible with binutils.
The new testcase makes sure that various combinations of kept/removed
sections pull the correct dependencies into the output and/or debug files.
Signed-off-by: Mark Wielaard
---
ChangeLog | 4 +
NEWS | 4 +
src/ChangeLog | 13 +
src/strip.c| 111 ++-
tests/ChangeLog| 6 +
tests/Makefile.am | 4 +-
tests/run-strip-remove-keep.sh | 688 +
7 files changed, 819 insertions(+), 11 deletions(-)
create mode 100755 tests/run-strip-remove-keep.sh
diff --git a/ChangeLog b/ChangeLog
index 0d6cd2b..c9db732 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2017-07-14 Mark Wielaard
+
+ * NEWS: Add 0.170 section and new strip options.
+
2017-05-05 Mark Wielaard
* configure.ac: Set version to 0.169. Update copyright year.
diff --git a/NEWS b/NEWS
index eb7dd97..b69aef4 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+Version 0.170
+
+strip: Add -R, --remove-section=SECTION and --keep-section=SECTION.
+
Version 0.169
backends: Add support for EM_PPC64 GNU_ATTRIBUTES.
diff --git a/src/ChangeLog b/src/ChangeLog
index e19122e..0b60fc7 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,16 @@
+2017-07-14 Mark Wielaard
+
+ * strip (OPT_KEEP_SECTION): New define.
+ (options): Add documentation for remove-section. Add keep-section.
+ (struct section_pattern): New data type.
+ (keep_secs, remove_secs): New globals.
+ (add_pattern, free_sec_patterns, free_patterns, section_name_matches):
+ New functions.
+ (main): Call free_patterns.
+ (parse_opt): Handle 'R' and OPT_KEEP_SECTION. Check remove_comment
+ on ARGP_KEY_SUCCESS.
+ (handle_elf): Handle and sanity check keep_secs and remove_secs.
+
2017-06-07 Mark Wielaard
* strip.c (handle_elf): Introduce new handle_elf boolean. Use it to
diff --git a/src/strip.c b/src/strip.c
index 2bf95f9..3aad92e 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -26,6 +26,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -60,6 +61,7 @@ ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
#define OPT_PERMISSIVE 0x101
#define OPT_STRIP_SECTIONS 0x102
#define OPT_RELOC_DEBUG0x103
+#define OPT_KEEP_SECTION 0x104
/* Definitions of arguments for argp functions. */
@@ -83,7 +85,8 @@ static const struct argp_option options[] =
N_("Resolve all trivial relocations between debug sections if the removed
sections are placed in a debug file (only relevant for ET_REL files, operation
is not reversable, needs -f)"), 0 },
{ "remove-comment", OPT_REMOVE_COMMENT, NULL, 0,
N_("Remove .comment section"), 0 },
- { "remove-section", 'R', "SECTION", OPTION_HIDDEN, NULL, 0 },
+ { "remove-section", 'R', "SECTION", 0, N_("Remove the named section.
SECTION is an extended wildcard pattern. May be given more than once. Only
non-allocated sections can be removed."), 0 },
+ { "keep-section", OPT_KEEP_SECTION, "SECTION", 0, N_("Keep the named
section. SECTION is an extended wildcard pattern. May be given more than
once."), 0 },
{ "permissive", OPT_PERMISSIVE, NULL, 0,
N_("Relax a few rules to handle slightly broken ELF files"), 0 },
{ NULL, 0, NULL, 0, NULL, 0 }
@@ -157,6 +160,58 @@ static bool permissive;
/* If true perform relocations between debug sections. */
static bool reloc_debug;
+/* Sections the user explicitly wants to keep or remove. */
+struct section_pattern
+{
+ char *pattern;
+ struct section_pattern *next;
+};
+
+static struct section_pattern *keep_secs = NULL;
+static struct section_pattern *remove_secs = NULL;
+
+static void
+add_pattern (struct section_pattern **patterns, const char *pattern)
+{
+ struct section_pattern *p = xmalloc (sizeof *p);
+ p->pattern = xstrdup (pattern);
+ p->next = *patterns;
+ *patterns = p;
+}
+
+static void
+free_sec_patterns (struct section_pattern *patterns)
+{
+ struct section_pattern *pattern = patterns;
+ while (pattern != NULL)
+{
+ struct section_pattern *p = pattern;
+ pattern = p->next;
+ free (p->pattern);
+ free (p);
+}
+}
+
+static void
+free_patterns (void)
+{
+ free_sec_patterns (keep_secs);
+ free_sec_patterns (remove_secs);
+}
+
+static bool
+section_name_matches (struct section_pattern *patterns, const char *name)