diff options
author | Eugene Leviant <eleviant@accesssoftek.com> | 2019-07-23 08:03:30 +0000 |
---|---|---|
committer | Eugene Leviant <eleviant@accesssoftek.com> | 2019-07-23 08:03:30 +0000 |
commit | c7e6d14c6c30c6798b14faf2a28e5be642b78ad8 (patch) | |
tree | fe9fee907b939db50c6a98a0ff9fc879da701a69 /llvm/tools/llvm-objcopy/ELF/Object.cpp | |
parent | 78b5e9bc25973f59b4f046b7ec8c26b38e0835cb (diff) | |
download | llvm-c7e6d14c6c30c6798b14faf2a28e5be642b78ad8.zip llvm-c7e6d14c6c30c6798b14faf2a28e5be642b78ad8.tar.gz llvm-c7e6d14c6c30c6798b14faf2a28e5be642b78ad8.tar.bz2 |
[llvm-objcopy] Allow strip symtab in executables and DSOs
Re-commit of the patch after addressing -Wl,--emit-relocs case.
Differential revision: https://reviews.llvm.org/D61672
llvm-svn: 366787
Diffstat (limited to 'llvm/tools/llvm-objcopy/ELF/Object.cpp')
-rw-r--r-- | llvm/tools/llvm-objcopy/ELF/Object.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp index fa69638..2d85b3a 100644 --- a/llvm/tools/llvm-objcopy/ELF/Object.cpp +++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp @@ -1995,6 +1995,25 @@ template <class ELFT> Error ELFWriter<ELFT>::write() { return Buf.commit(); } +static Error removeUnneededSections(Object &Obj) { + // We can remove an empty symbol table from non-relocatable objects. + // Relocatable objects typically have relocation sections whose + // sh_link field points to .symtab, so we can't remove .symtab + // even if it is empty. + if (Obj.isRelocatable() || Obj.SymbolTable == nullptr || + !Obj.SymbolTable->empty()) + return Error::success(); + + // .strtab can be used for section names. In such a case we shouldn't + // remove it. + auto *StrTab = Obj.SymbolTable->getStrTab() == Obj.SectionNames + ? nullptr + : Obj.SymbolTable->getStrTab(); + return Obj.removeSections(false, [&](const SectionBase &Sec) { + return &Sec == Obj.SymbolTable || &Sec == StrTab; + }); +} + template <class ELFT> Error ELFWriter<ELFT>::finalize() { // It could happen that SectionNames has been removed and yet the user wants // a section header table output. We need to throw an error if a user tries @@ -2004,6 +2023,8 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() { "cannot write section header table because " "section header string table was removed"); + if (Error E = removeUnneededSections(Obj)) + return E; Obj.sortSections(); // We need to assign indexes before we perform layout because we need to know |