Skip to content

Commit 490d02a

Browse files
committed
Gas is very inconsistent about when a relaxation/relocation is needed. Do
the right thing and stop trying to copy it. Fixes PR8944. llvm-svn: 125648
1 parent 2ec773f commit 490d02a

File tree

6 files changed

+20
-65
lines changed

6 files changed

+20
-65
lines changed

llvm/include/llvm/MC/MCObjectWriter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class MCObjectWriter {
9696
const MCSymbolData &DataA,
9797
const MCFragment &FB,
9898
bool InSet,
99-
bool IsPCRel) const = 0;
99+
bool IsPCRel) const;
100100

101101

102102
/// Write the object file.

llvm/lib/MC/ELFObjectWriter.cpp

-25
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,6 @@ namespace {
359359
MCDataFragment *F,
360360
const MCSectionData *SD);
361361

362-
virtual bool
363-
IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
364-
const MCSymbolData &DataA,
365-
const MCFragment &FB,
366-
bool InSet,
367-
bool IsPCRel) const;
368-
369362
virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
370363
virtual void WriteSection(MCAssembler &Asm,
371364
const SectionIndexMapTy &SectionIndexMap,
@@ -1181,24 +1174,6 @@ void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
11811174
}
11821175
}
11831176

1184-
bool
1185-
ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1186-
const MCSymbolData &DataA,
1187-
const MCFragment &FB,
1188-
bool InSet,
1189-
bool IsPCRel) const {
1190-
// FIXME: This is in here just to match gnu as output. If the two ends
1191-
// are in the same section, there is nothing that the linker can do to
1192-
// break it.
1193-
if (DataA.isExternal())
1194-
return false;
1195-
1196-
const MCSection &SecA = DataA.getSymbol().AliasedSymbol().getSection();
1197-
const MCSection &SecB = FB.getParent()->getSection();
1198-
// On ELF A - B is absolute if A and B are in the same section.
1199-
return &SecA == &SecB;
1200-
}
1201-
12021177
void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
12031178
MCAsmLayout &Layout,
12041179
GroupMapTy &GroupMap,

llvm/lib/MC/MCAssembler.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,10 @@ bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
229229
} else if (!Target.getSymA()) {
230230
IsResolved = false;
231231
} else {
232-
const MCSymbol &SA = Target.getSymA()->getSymbol();
233-
if (SA.AliasedSymbol().isUndefined()) {
232+
const MCSymbolRefExpr *A = Target.getSymA();
233+
const MCSymbol &SA = A->getSymbol();
234+
if (A->getKind() != MCSymbolRefExpr::VK_None ||
235+
SA.AliasedSymbol().isUndefined()) {
234236
IsResolved = false;
235237
} else {
236238
const MCSymbolData &DataA = getSymbolData(SA);

llvm/lib/MC/MCObjectWriter.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,15 @@ MCObjectWriter::IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
6666
InSet,
6767
false);
6868
}
69+
70+
bool
71+
MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
72+
const MCSymbolData &DataA,
73+
const MCFragment &FB,
74+
bool InSet,
75+
bool IsPCRel) const {
76+
const MCSection &SecA = DataA.getSymbol().AliasedSymbol().getSection();
77+
const MCSection &SecB = FB.getParent()->getSection();
78+
// On ELF and COFF A - B is absolute if A and B are in the same section.
79+
return &SecA == &SecB;
80+
}

llvm/lib/MC/WinCOFFObjectWriter.cpp

-20
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,6 @@ class WinCOFFObjectWriter : public MCObjectWriter {
179179
MCValue Target,
180180
uint64_t &FixedValue);
181181

182-
virtual bool
183-
IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
184-
const MCSymbolData &DataA,
185-
const MCFragment &FB,
186-
bool InSet,
187-
bool IsPCRel) const;
188-
189182
void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
190183
};
191184
}
@@ -719,19 +712,6 @@ void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm,
719712
coff_section->Relocations.push_back(Reloc);
720713
}
721714

722-
bool
723-
WinCOFFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
724-
const MCAssembler &Asm,
725-
const MCSymbolData &DataA,
726-
const MCFragment &FB,
727-
bool InSet,
728-
bool IsPCRel) const {
729-
const MCSection &SecA = DataA.getSymbol().AliasedSymbol().getSection();
730-
const MCSection &SecB = FB.getParent()->getSection();
731-
// On COFF A - B is absolute if A and B are in the same section.
732-
return &SecA == &SecB;
733-
}
734-
735715
void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
736716
const MCAsmLayout &Layout) {
737717
// Assign symbol and section indexes and offsets.

llvm/test/MC/ELF/relax.s

+3-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | elf-dump --dump-section-data | FileCheck %s
22

3-
// Test that we do a relaxation for foo but not for bar or zed. Relaxing foo is
4-
// probably not necessary, but matches what gnu as does.
5-
6-
// Also test that the relaxation done for foo uses the symbol, not section and
7-
// offset.
3+
// Test that we do not relax these.
84

95
bar:
106
.globl foo
@@ -20,22 +16,12 @@ foo:
2016
// CHECK-NEXT: ('sh_flags', 0x00000006)
2117
// CHECK-NEXT: ('sh_addr', 0x00000000)
2218
// CHECK-NEXT: ('sh_offset', 0x00000040)
23-
// CHECK-NEXT: ('sh_size', 0x00000009)
19+
// CHECK-NEXT: ('sh_size', 0x00000006)
2420
// CHECK-NEXT: ('sh_link', 0x00000000)
2521
// CHECK-NEXT: ('sh_info', 0x00000000)
2622
// CHECK-NEXT: ('sh_addralign', 0x00000004)
2723
// CHECK-NEXT: ('sh_entsize', 0x00000000)
28-
// CHECK-NEXT: ('_section_data', 'ebfee900 000000eb f7')
24+
// CHECK-NEXT: ('_section_data', 'ebfeebfc ebfa')
2925

3026
// CHECK: # Symbol 0x00000006
3127
// CHECK-NEXT: (('st_name', 0x00000005) # 'foo'
32-
33-
// CHECK: .rela.text
34-
// CHECK: ('_relocations', [
35-
// CHECK-NEXT: Relocation 0x00000000
36-
// CHECK-NEXT: (('r_offset', 0x00000003)
37-
// CHECK-NEXT: ('r_sym', 0x00000006)
38-
// CHECK-NEXT: ('r_type', 0x00000002)
39-
// CHECK-NEXT: ('r_addend', 0xfffffffc)
40-
// CHECK-NEXT: ),
41-
// CHECK-NEXT: ])

0 commit comments

Comments
 (0)