[llvm-objcopy][llvm-strip] Add switch to allow removing referenced sections
llvm-objcopy currently emits an error if a section to be removed is
referenced by another section. This is a reasonable thing to do, but is
different to GNU objcopy. We should allow users who know what they are
doing to have a way to produce the invalid ELF. This change adds a new
switch --allow-broken-links to both llvm-strip and llvm-objcopy to do
precisely that. The corresponding sh_link field is then set to 0 instead
of an error being emitted.
I cannot use llvm-readelf/readobj to test the link fields because they
emit an error if any sections, like the .dynsym, cannot be properly
loaded.
Reviewed by: rupprecht, grimar
Differential Revision: https://reviews.llvm.org/D60324
llvm-svn: 358649
diff --git a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
index ca0507d..7e99116 100644
--- a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
+++ b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
@@ -250,7 +250,8 @@
auto OnlyKeepDWOPred = [&DWOFile](const SectionBase &Sec) {
return onlyKeepDWOPred(*DWOFile, Sec);
};
- if (Error E = DWOFile->removeSections(OnlyKeepDWOPred))
+ if (Error E = DWOFile->removeSections(Config.AllowBrokenLinks,
+ OnlyKeepDWOPred))
return E;
if (Config.OutputArch) {
DWOFile->Machine = Config.OutputArch.getValue().EMachine;
@@ -547,7 +548,7 @@
return &Obj.addSection<DecompressedSection>(*CS);
});
- return Obj.removeSections(RemovePred);
+ return Obj.removeSections(Config.AllowBrokenLinks, RemovePred);
}
// This function handles the high level operations of GNU objcopy including