Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 037da24

Browse files
committed
Update the fragments of symbols in compressed sections.
While unnamed relocations are already cached in side tables in ELFObjectWriter::RecordRelocation, symbols still need their fragments updated to refer to the newly compressed fragment (even if that fragment isn't big enough to fit the offset). Even though we only create temporary symbols in debug info sections this comes up in 32 bit builds where even temporary symbols in mergeable sections (such as debug_str) have to be emitted as named symbols. I tried a few other ways to do this but they all didn't work for various reasons: 1) Canonicalize the MCSymbolData in RecordRelocation, nulling out the Fragment (so it didn't have to be updated by CompressDebugSection). This doesn't work because some code relies on symbols having fragments to indicate that they're defined, I think. 2) Canonicalize the MCSymbolData in RecordRelocation to be "first fragment + absolute offset" so it would be cheaper to just test and update the fragment in CompressDebugSections. This doesn't work because the offset computed in RecordRelocation isn't that of the symbol's fragment, it's the passed in fragment (I haven't figured out what that fragment is - perhaps it's the location where the relocation is to be written). And if the fragment offset has to be computed only for this use we might as well just do it when we need to, in CompressDebugSection. I also added an assert to help catch this a bit more clearly, even though it is UB. The test case improvements would either assert fail and/or valgrind vail without the fix, even if they wouldn't necessarily fail the FileCheck output. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206653 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 6c4ec69 commit 037da24

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/MC/ELFObjectWriter.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,10 @@ static const MCSymbol *getBaseSymbol(const MCAsmLayout &Layout,
616616
void ELFObjectWriter::WriteSymbol(SymbolTableWriter &Writer, ELFSymbolData &MSD,
617617
const MCAsmLayout &Layout) {
618618
MCSymbolData &OrigData = *MSD.SymbolData;
619+
assert(!OrigData.getFragment() ||
620+
(&OrigData.getFragment()->getParent()->getSection() ==
621+
&OrigData.getSymbol().getSection()) &&
622+
"The symbol's section doesn't match the fragment's symbol");
619623
const MCSymbol *Base = getBaseSymbol(Layout, OrigData.getSymbol());
620624

621625
// This has to be in sync with when computeSymbolTable uses SHN_ABS or
@@ -1244,6 +1248,19 @@ getCompressedFragment(MCAsmLayout &Layout,
12441248
return CompressedFragment;
12451249
}
12461250

1251+
static void UpdateSymbols(const MCAsmLayout &Layout, const MCSectionData &SD,
1252+
MCAssembler::symbol_range Symbols,
1253+
MCFragment *NewFragment) {
1254+
for (MCSymbolData &Data : Symbols) {
1255+
MCFragment *F = Data.getFragment();
1256+
if (F && F->getParent() == &SD) {
1257+
Data.setOffset(Data.getOffset() +
1258+
Layout.getFragmentOffset(Data.Fragment));
1259+
Data.setFragment(NewFragment);
1260+
}
1261+
}
1262+
}
1263+
12471264
static void CompressDebugSection(MCAssembler &Asm, MCAsmLayout &Layout,
12481265
const MCSectionELF &Section,
12491266
MCSectionData &SD) {
@@ -1257,6 +1274,10 @@ static void CompressDebugSection(MCAssembler &Asm, MCAsmLayout &Layout,
12571274
if (!CompressedFragment)
12581275
return;
12591276

1277+
// Update the fragment+offsets of any symbols referring to fragments in this
1278+
// section to refer to the new fragment.
1279+
UpdateSymbols(Layout, SD, Asm.symbols(), CompressedFragment.get());
1280+
12601281
// Invalidate the layout for the whole section since it will have new and
12611282
// different fragments now.
12621283
Layout.invalidateFragmentsFrom(&Fragments.front());

test/MC/ELF/compression.s

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// RUN: llvm-mc -filetype=obj -compress-debug-sections -triple x86_64-pc-linux-gnu %s -o %t
1+
// RUN: llvm-mc -filetype=obj -compress-debug-sections -triple x86_64-pc-linux-gnu < %s -o %t
22
// RUN: llvm-objdump -s %t | FileCheck %s
33
// RUN: llvm-dwarfdump -debug-dump=abbrev %t | FileCheck --check-prefix=ABBREV %s
4+
// RUN: llvm-mc -filetype=obj -compress-debug-sections -triple i386-pc-linux-gnu < %s \
5+
// RUN: | llvm-readobj -symbols - | FileCheck --check-prefix=386-SYMBOLS %s
46

57
// REQUIRES: zlib
68

@@ -22,12 +24,21 @@
2224
// ABBREV: Abbrev table for offset: 0x00000000
2325
// ABBREV: [1] DW_TAG_compile_unit DW_CHILDREN_no
2426

27+
// In x86 32 bit named symbols are used for temporary symbols in merge
28+
// sections, so make sure we handle symbols inside compressed sections
29+
// 386-SYMBOLS: Name: .Linfo_string0
30+
// 386-SYMBOLS-NOT: }
31+
// 386-SYMBOLS: Section: .zdebug_str
32+
2533
.section .debug_line,"",@progbits
2634

2735
.section .debug_abbrev,"",@progbits
36+
.Lsection_abbrev:
2837
.byte 1 # Abbreviation Code
2938
.byte 17 # DW_TAG_compile_unit
3039
.byte 0 # DW_CHILDREN_no
40+
.byte 27 # DW_AT_comp_dir
41+
.byte 14 # DW_FORM_strp
3142
.byte 0 # EOM(1)
3243
.byte 0 # EOM(2)
3344
.text
@@ -38,3 +49,15 @@ foo:
3849
nop
3950
.cfi_endproc
4051
.cfi_sections .debug_frame
52+
53+
.section .debug_str,"MS",@progbits,1
54+
.Linfo_string0:
55+
.asciz "foo"
56+
57+
.section .debug_info,"",@progbits
58+
.long 40 # Length of Unit
59+
.short 4 # DWARF version number
60+
.long .Lsection_abbrev # Offset Into Abbrev. Section
61+
.byte 4 # Address Size (in bytes)
62+
.byte 1 # Abbrev [1] DW_TAG_compile_unit
63+
.long .Linfo_string0 # DW_AT_comp_dir

0 commit comments

Comments
 (0)