[llvm-objcopy]Don't implicitly strip sections in segments
This patch changes llvm-objcopy's behaviour to not strip sections that
are in segments, if they otherwise would be due to a stripping operation
(--strip-all, --strip-sections, --strip-non-alloc). This preserves the
segment contents. It does not change the behaviour of --strip-all-gnu
(although we could choose to do so), because GNU objcopy's behaviour in
this case seems to be to strip the section, nor does it prevent removing
of sections in segments with --remove-section (if a user REALLY wants to
remove a section, we should probably let them, although I could be
persuaded that warning might be appropriate). Tests have been added to
show this latter behaviour.
This fixes https://bugs.llvm.org/show_bug.cgi?id=41006.
Reviewed by: grimar, rupprecht, jakehehrlich
Differential Revision: https://reviews.llvm.org/D59293
This is a reland of r356129, attempting to fix greendragon failures
due to a suspected compatibility issue with od on the greendragon bots
versus other versions.
llvm-svn: 356136
diff --git a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
index cbf6a3d..250eeea 100644
--- a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
+++ b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
@@ -403,7 +403,7 @@
if (Config.StripSections) {
RemovePred = [RemovePred](const SectionBase &Sec) {
- return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
+ return RemovePred(Sec) || Sec.ParentSegment == nullptr;
};
}
@@ -419,7 +419,7 @@
return true;
if (&Sec == Obj.SectionNames)
return false;
- return (Sec.Flags & SHF_ALLOC) == 0;
+ return (Sec.Flags & SHF_ALLOC) == 0 && Sec.ParentSegment == nullptr;
};
if (Config.StripAll)
@@ -430,6 +430,8 @@
return false;
if (StringRef(Sec.Name).startswith(".gnu.warning"))
return false;
+ if (Sec.ParentSegment != nullptr)
+ return false;
return (Sec.Flags & SHF_ALLOC) == 0;
};
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOpts.td b/llvm/tools/llvm-objcopy/ObjcopyOpts.td
index 44fd01c..a5f7119 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOpts.td
+++ b/llvm/tools/llvm-objcopy/ObjcopyOpts.td
@@ -103,10 +103,9 @@
"rom, share, contents, merge, strings.">,
MetaVarName<"section=flag1[,flag2,...]">;
-def strip_all
- : Flag<["-", "--"], "strip-all">,
- HelpText<
- "Remove non-allocated sections other than .gnu.warning* sections">;
+def strip_all : Flag<["-", "--"], "strip-all">,
+ HelpText<"Remove non-allocated sections outside segments. "
+ ".gnu.warning* sections are not removed">;
def S : Flag<["-"], "S">, Alias<strip_all>;
def strip_all_gnu : Flag<["-", "--"], "strip-all-gnu">,
HelpText<"Compatible with GNU objcopy's --strip-all">;
@@ -116,13 +115,15 @@
HelpText<"Alias for --strip-debug">;
def strip_dwo : Flag<["-", "--"], "strip-dwo">,
HelpText<"Remove all DWARF .dwo sections from file">;
-def strip_sections : Flag<["-", "--"], "strip-sections">,
- HelpText<"Remove all section headers">;
-def strip_non_alloc : Flag<["-", "--"], "strip-non-alloc">,
- HelpText<"Remove all non-allocated sections">;
+def strip_sections
+ : Flag<["-", "--"], "strip-sections">,
+ HelpText<"Remove all section headers and all sections not in segments">;
+def strip_non_alloc
+ : Flag<["-", "--"], "strip-non-alloc">,
+ HelpText<"Remove all non-allocated sections outside segments">;
def strip_unneeded : Flag<["-", "--"], "strip-unneeded">,
HelpText<"Remove all symbols not needed by relocations">;
-defm strip_unneeded_symbol
+defm strip_unneeded_symbol
: Eq<"strip-unneeded-symbol",
"Remove symbol <symbol> if it is not needed by relocations">,
MetaVarName<"symbol">;
diff --git a/llvm/tools/llvm-objcopy/StripOpts.td b/llvm/tools/llvm-objcopy/StripOpts.td
index c3f3519..40b043a 100644
--- a/llvm/tools/llvm-objcopy/StripOpts.td
+++ b/llvm/tools/llvm-objcopy/StripOpts.td
@@ -31,10 +31,9 @@
HelpText<"Preserve access and modification timestamps">;
def p : Flag<["-"], "p">, Alias<preserve_dates>;
-def strip_all
- : Flag<["-", "--"], "strip-all">,
- HelpText<
- "Remove non-allocated sections other than .gnu.warning* sections">;
+def strip_all : Flag<["-", "--"], "strip-all">,
+ HelpText<"Remove non-allocated sections outside segments. "
+ ".gnu.warning* sections are not removed">;
def s : Flag<["-"], "s">, Alias<strip_all>;
def strip_all_gnu : Flag<["-", "--"], "strip-all-gnu">,