[llvm] r341419 - [llvm-strip] Allow copying relocation sections without symbol tables.
Jordan Rupprecht via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 4 15:28:49 PDT 2018
Author: rupprecht
Date: Tue Sep 4 15:28:49 2018
New Revision: 341419
URL: http://llvm.org/viewvc/llvm-project?rev=341419&view=rev
Log:
[llvm-strip] Allow copying relocation sections without symbol tables.
Summary:
Fixes the error "Link field value 0 in section .rela.plt is invalid" when copying/stripping certain binaries. Minimal repro:
```
$ cat /tmp/a.c
int main() { return 0; }
$ clang -static /tmp/a.c -o /tmp/a
$ llvm-strip /tmp/a -o /tmp/b
llvm-strip: error: Link field value 0 in section .rela.plt is invalid.
```
Reviewers: jakehehrlich, alexshap
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D51493
Added:
llvm/trunk/test/tools/llvm-objcopy/reloc-no-symtab.test
Modified:
llvm/trunk/tools/llvm-objcopy/Object.cpp
Added: llvm/trunk/test/tools/llvm-objcopy/reloc-no-symtab.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/reloc-no-symtab.test?rev=341419&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/reloc-no-symtab.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/reloc-no-symtab.test Tue Sep 4 15:28:49 2018
@@ -0,0 +1,45 @@
+# Regression test for the following case:
+# $ cat /tmp/a.c
+# int main() { return 0; }
+# $ clang -static /tmp/a.c -o /tmp/a
+# $ llvm-strip /tmp/a -o /tmp/b
+# llvm-strip: error: Link field value 0 in section .rela.plt is invalid.
+
+# RUN: yaml2obj %s > %t.original
+# RUN: llvm-strip %t.original -o %t.stripped
+# RUN: llvm-readobj -sections %t.original | FileCheck %s
+# RUN: llvm-readobj -sections %t.stripped | FileCheck %s
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ - Name: .rela.plt
+ Type: SHT_RELA
+ Flags: [ SHF_ALLOC, SHF_INFO_LINK ]
+ Info: .got.plt
+ Link: 0
+ - Name: .plt
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ - Name: .got.plt
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_WRITE ]
+
+# CHECK: Name: .rela.plt
+# CHECK-NEXT: Type: SHT_RELA
+# CHECK-NEXT: Flags [
+# CHECK-NEXT: SHF_ALLOC
+# CHECK-NEXT: SHF_INFO_LINK
+# CHECK-NEXT: ]
+# CHECK-NEXT: Address:
+# CHECK-NEXT: Offset:
+# CHECK-NEXT: Size: 0
+# CHECK-NEXT: Link: 0
+# CHECK-NEXT: Info: 4
Modified: llvm/trunk/tools/llvm-objcopy/Object.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/Object.cpp?rev=341419&r1=341418&r2=341419&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/Object.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/Object.cpp Tue Sep 4 15:28:49 2018
@@ -375,11 +375,13 @@ void RelocSectionWithSymtabBase<SymTabTy
template <class SymTabType>
void RelocSectionWithSymtabBase<SymTabType>::initialize(
SectionTableRef SecTable) {
- setSymTab(SecTable.getSectionOfType<SymTabType>(
- Link,
- "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
- "Link field value " + Twine(Link) + " in section " + Name +
- " is not a symbol table"));
+ if (Link != SHN_UNDEF)
+ setSymTab(SecTable.getSectionOfType<SymTabType>(
+ Link,
+ "Link field value " + Twine(Link) + " in section " + Name +
+ " is invalid",
+ "Link field value " + Twine(Link) + " in section " + Name +
+ " is not a symbol table"));
if (Info != SHN_UNDEF)
setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) +
@@ -391,7 +393,8 @@ void RelocSectionWithSymtabBase<SymTabTy
template <class SymTabType>
void RelocSectionWithSymtabBase<SymTabType>::finalize() {
- this->Link = Symbols->Index;
+ this->Link = Symbols ? Symbols->Index : 0;
+
if (SecToApplyRel != nullptr)
this->Info = SecToApplyRel->Index;
}
More information about the llvm-commits
mailing list