[llvm] r333339 - [llvm-objcopy] Add --keep-file-symbols option
Paul Semel via llvm-commits
llvm-commits at lists.llvm.org
Sat May 26 01:10:37 PDT 2018
Author: paulsemel
Date: Sat May 26 01:10:37 2018
New Revision: 333339
URL: http://llvm.org/viewvc/llvm-project?rev=333339&view=rev
Log:
[llvm-objcopy] Add --keep-file-symbols option
This option prevent from removing file symbols while removing symbols.
Differential Revision: https://reviews.llvm.org/D46830
Added:
llvm/trunk/test/tools/llvm-objcopy/keep-file-symbols.test
Modified:
llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td
llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp
Added: llvm/trunk/test/tools/llvm-objcopy/keep-file-symbols.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/keep-file-symbols.test?rev=333339&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/keep-file-symbols.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/keep-file-symbols.test Sat May 26 01:10:37 2018
@@ -0,0 +1,70 @@
+# RUN: yaml2obj %s > %t
+# RUN: llvm-objcopy --strip-all --keep-file-symbols %t %t2
+# RUN: llvm-readobj -symbols %t2 | FileCheck %s --check-prefix=STRIPALL
+# RUN: llvm-objcopy --keep-file-symbols --strip-symbol foo %t %t2
+# RUN: llvm-readobj -symbols %t2 | FileCheck %s --check-prefix=STRIP
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x1000
+ AddressAlign: 0x0000000000000010
+ Size: 64
+Symbols:
+ Local:
+ - Name: foo
+ Type: STT_FILE
+ Section: .text
+ Global:
+ - Name: bar
+ Type: STT_FUNC
+ Section: .text
+
+#STRIPALL: Symbols [
+#STRIPALL-NEXT: Symbol {
+#STRIPALL-NEXT: Name: foo
+#STRIPALL-NEXT: Value: 0x0
+#STRIPALL-NEXT: Size: 0
+#STRIPALL-NEXT: Binding: Local
+#STRIPALL-NEXT: Type: File
+#STRIPALL-NEXT: Other: 0
+#STRIPALL-NEXT: Section: .text
+#STRIPALL-NEXT: }
+#STRIPALL-NEXT:]
+
+#STRIP: Symbols [
+#STRIP-NEXT: Symbol {
+#STRIP-NEXT: Name:
+#STRIP-NEXT: Value: 0x0
+#STRIP-NEXT: Size: 0
+#STRIP-NEXT: Binding: Local
+#STRIP-NEXT: Type: None
+#STRIP-NEXT: Other: 0
+#STRIP-NEXT: Section: Undefined
+#STRIP-NEXT: }
+#STRIP-NEXT: Symbol {
+#STRIP-NEXT: Name: foo
+#STRIP-NEXT: Value: 0x0
+#STRIP-NEXT: Size: 0
+#STRIP-NEXT: Binding: Local
+#STRIP-NEXT: Type: File
+#STRIP-NEXT: Other: 0
+#STRIP-NEXT: Section: .text
+#STRIP-NEXT: }
+#STRIP-NEXT: Symbol {
+#STRIP-NEXT: Name: bar
+#STRIP-NEXT: Value: 0x0
+#STRIP-NEXT: Size: 0
+#STRIP-NEXT: Binding: Global
+#STRIP-NEXT: Type: Function
+#STRIP-NEXT: Other: 0
+#STRIP-NEXT: Section: .text
+#STRIP-NEXT: }
+#STRIP-NEXT:]
Modified: llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td?rev=333339&r1=333338&r2=333339&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td (original)
+++ llvm/trunk/tools/llvm-objcopy/ObjcopyOpts.td Sat May 26 01:10:37 2018
@@ -92,3 +92,5 @@ def only_keep_debug : Flag<["-", "--"],
HelpText<"Currently ignored. Only for compaitability with GNU objcopy.">;
def strip_unneeded : Flag<["-", "--"], "strip-unneeded">,
HelpText<"Remove all symbols not needed by relocations">;
+def keep_file_symbols : Flag<["-", "--"], "keep-file-symbols">,
+ HelpText<"Do not remove file symbols">;
Modified: llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp?rev=333339&r1=333338&r2=333339&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp Sat May 26 01:10:37 2018
@@ -174,6 +174,7 @@ struct CopyConfig {
bool Weaken = false;
bool DiscardAll = false;
bool OnlyKeepDebug = false;
+ bool KeepFileSymbols = false;
};
using SectionPred = std::function<bool(const SectionBase &Sec)>;
@@ -273,8 +274,9 @@ void HandleArgs(const CopyConfig &Config
}
Obj.removeSymbols([&](const Symbol &Sym) {
- if (!Config.SymbolsToKeep.empty() &&
- is_contained(Config.SymbolsToKeep, Sym.Name))
+ if ((!Config.SymbolsToKeep.empty() &&
+ is_contained(Config.SymbolsToKeep, Sym.Name)) ||
+ (Config.KeepFileSymbols && Sym.Type == STT_FILE))
return false;
if (Config.DiscardAll && Sym.Binding == STB_LOCAL &&
@@ -410,7 +412,8 @@ void HandleArgs(const CopyConfig &Config
// and at least one of those symbols is present
// (equivalently, the updated symbol table is not empty)
// the symbol table and the string table should not be removed.
- if (!Config.SymbolsToKeep.empty() && !Obj.SymbolTable->empty()) {
+ if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) &&
+ !Obj.SymbolTable->empty()) {
RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
return false;
@@ -531,6 +534,7 @@ CopyConfig ParseObjcopyOptions(ArrayRef<
Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all);
Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug);
+ Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
Config.SymbolsToLocalize.push_back(Arg->getValue());
for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
More information about the llvm-commits
mailing list