Skip to content

Commit 0373bed

Browse files
author
George Rimar
committed
[llvm-objcopy] - Use replaceSectionReferences to update the sections for symbols in symbol table.
If the compression was used and we had a symbol not involved in relocation, we never updated its section and it was silently removed from the output. Differential revision: https://reviews.llvm.org/D59542 llvm-svn: 356554
1 parent 2b70dcd commit 0373bed

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

llvm/test/tools/llvm-objcopy/ELF/Inputs/compress-debug-sections.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Sections:
2121
- Offset: 0x2
2222
Symbol: .notdebug_foo
2323
Type: R_X86_64_32
24+
## This section should not be involved in relocations.
25+
- Name: .debug_bar
26+
Type: SHT_PROGBITS
27+
Content: 0000000000000000
2428
Symbols:
2529
Global:
2630
- Name: .debug_foo
@@ -29,4 +33,6 @@ Symbols:
2933
- Name: .notdebug_foo
3034
Type: STT_SECTION
3135
Section: .notdebug_foo
36+
- Name: .Linfo_string0
37+
Section: .debug_bar
3238
...
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# REQUIRES: zlib
2+
3+
# RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t.o
4+
5+
## Test that after the compression is done we do not remove the symbol
6+
## and it is placed into the right section.
7+
8+
# RUN: llvm-objcopy --compress-debug-sections %t.o %t-compressed1.o
9+
# RUN: llvm-readobj -symbols %t-compressed1.o | FileCheck %s --check-prefixes=CHECK,ZLIB
10+
11+
# RUN: llvm-objcopy --compress-debug-sections=zlib-gnu %t.o %t-compressed2.o
12+
# RUN: llvm-readobj -symbols %t-compressed2.o | FileCheck %s --check-prefixes=CHECK,ZLIBGNU
13+
14+
# CHECK: Name: .Linfo_string0
15+
# CHECK-NEXT: Value: 0x0
16+
# CHECK-NEXT: Size: 0
17+
# CHECK-NEXT: Binding: Global
18+
# CHECK-NEXT: Type: None
19+
# CHECK-NEXT: Other: 0
20+
# ZLIB-NEXT: Section: .debug_bar
21+
# ZLIBGNU-NEXT: Section: .zdebug_bar

llvm/tools/llvm-objcopy/ELF/Object.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,13 @@ Error SymbolTableSection::removeSymbols(
457457
return Error::success();
458458
}
459459

460+
void SymbolTableSection::replaceSectionReferences(
461+
const DenseMap<SectionBase *, SectionBase *> &FromTo) {
462+
for (std::unique_ptr<Symbol> &Sym : Symbols)
463+
if (SectionBase *To = FromTo.lookup(Sym->DefinedIn))
464+
Sym->DefinedIn = To;
465+
}
466+
460467
void SymbolTableSection::initialize(SectionTableRef SecTable) {
461468
Size = 0;
462469
setStrTab(SecTable.getSectionOfType<StringTableSection>(
@@ -638,12 +645,6 @@ void RelocationSection::replaceSectionReferences(
638645
// Update the target section if it was replaced.
639646
if (SectionBase *To = FromTo.lookup(SecToApplyRel))
640647
SecToApplyRel = To;
641-
642-
// Change the sections where symbols are defined in if their
643-
// original sections were replaced.
644-
for (const Relocation &R : Relocations)
645-
if (SectionBase *To = FromTo.lookup(R.RelocSymbol->DefinedIn))
646-
R.RelocSymbol->DefinedIn = To;
647648
}
648649

649650
void SectionWriter::visit(const DynamicRelocationSection &Sec) {

llvm/tools/llvm-objcopy/ELF/Object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,8 @@ class SymbolTableSection : public SectionBase {
529529
void accept(SectionVisitor &Visitor) const override;
530530
void accept(MutableSectionVisitor &Visitor) override;
531531
Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
532+
void replaceSectionReferences(
533+
const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
532534

533535
static bool classof(const SectionBase *S) {
534536
return S->Type == ELF::SHT_SYMTAB;

0 commit comments

Comments
 (0)