Fix the symbol index of weak references. Also make RecordRelocation a bit

easier to read by having const references to the symbol, aliased symbol and
renamed symbol.

llvm-svn: 118793
This commit is contained in:
Rafael Espindola 2010-11-11 16:48:11 +00:00
parent c669dfa113
commit fa7b55754c
2 changed files with 61 additions and 22 deletions

View File

@ -669,16 +669,16 @@ void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
int64_t Addend = 0;
int Index = 0;
int64_t Value = Target.getConstant();
const MCSymbol *Symbol = 0;
const MCSymbol *Renamed = 0;
const MCSymbol &Symbol = Target.getSymA()->getSymbol();
const MCSymbol &ASymbol = AliasedSymbol(Symbol);
const MCSymbol *RenamedP = Renames.lookup(&Symbol);
if (!RenamedP)
RenamedP = &ASymbol;
const MCSymbol &Renamed = *RenamedP;
bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
if (!Target.isAbsolute()) {
Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
Renamed = Renames.lookup(Symbol);
if (!Renamed)
Renamed = &Target.getSymA()->getSymbol();
MCSymbolData &SD = Asm.getSymbolData(*Symbol);
MCSymbolData &SD = Asm.getSymbolData(Symbol);
MCFragment *F = SD.getFragment();
if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
@ -695,15 +695,6 @@ void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
Value += b - a;
}
// Check that this case has already been fully resolved before we get
// here.
if (Symbol->isDefined() && !SD.isExternal() &&
IsPCRel &&
&Fragment->getParent()->getSection() == &Symbol->getSection()) {
llvm_unreachable("We don't need a relocation in this case.");
return;
}
bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
if (!RelocOnSymbol) {
Index = F->getParent()->getOrdinal();
@ -712,11 +703,10 @@ void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
// Offset of the symbol in the section
Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
} else {
UsedInReloc.insert(Renamed);
MCSymbolData &RenamedSD = Asm.getSymbolData(*Renamed);
if (RenamedSD.getFlags() & ELF_Other_Weakref) {
WeakrefUsedInReloc.insert(Symbol);
}
if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
WeakrefUsedInReloc.insert(&Renamed);
else
UsedInReloc.insert(&Renamed);
Index = -1;
}
Addend = Value;
@ -864,7 +854,7 @@ void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
ERE.Index = Index;
ERE.Type = Type;
ERE.Symbol = Renamed;
ERE.Symbol = &Renamed;
ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();

View File

@ -0,0 +1,49 @@
// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | elf-dump | FileCheck %s
// Test that the relocations point to the correct symbols. We used to get the
// symbol index wrong for weakrefs when creating _GLOBAL_OFFSET_TABLE_.
.weakref bar,foo
call zed@PLT
call bar
// CHECK: # Symbol 0x00000004
// CHECK-NEXT: (('st_name', 0x00000009) # '_GLOBAL_OFFSET_TABLE_'
// CHECK-NEXT: ('st_bind', 0x00000001)
// CHECK-NEXT: ('st_type', 0x00000000)
// CHECK-NEXT: ('st_other', 0x00000000)
// CHECK-NEXT: ('st_shndx', 0x00000000)
// CHECK-NEXT: ('st_value', 0x00000000)
// CHECK-NEXT: ('st_size', 0x00000000)
// CHECK-NEXT: ),
// CHECK-NEXT: # Symbol 0x00000005
// CHECK-NEXT: (('st_name', 0x00000001) # 'foo'
// CHECK-NEXT: ('st_bind', 0x00000002)
// CHECK-NEXT: ('st_type', 0x00000000)
// CHECK-NEXT: ('st_other', 0x00000000)
// CHECK-NEXT: ('st_shndx', 0x00000000)
// CHECK-NEXT: ('st_value', 0x00000000)
// CHECK-NEXT: ('st_size', 0x00000000)
// CHECK-NEXT: ),
// CHECK-NEXT: # Symbol 0x00000006
// CHECK-NEXT: (('st_name', 0x00000005) # 'zed'
// CHECK-NEXT: ('st_bind', 0x00000001)
// CHECK-NEXT: ('st_type', 0x00000000)
// CHECK-NEXT: ('st_other', 0x00000000)
// CHECK-NEXT: ('st_shndx', 0x00000000)
// CHECK-NEXT: ('st_value', 0x00000000)
// CHECK-NEXT: ('st_size', 0x00000000)
// CHECK-NEXT: ),
// CHECK: # Relocation 0x00000000
// CHECK-NEXT: (('r_offset', 0x00000001)
// CHECK-NEXT: ('r_sym', 0x00000006)
// CHECK-NEXT: ('r_type', 0x00000004)
// CHECK-NEXT: ('r_addend', 0xfffffffc)
// CHECK-NEXT: ),
// CHECK-NEXT: # Relocation 0x00000001
// CHECK-NEXT: (('r_offset', 0x00000006)
// CHECK-NEXT: ('r_sym', 0x00000005)
// CHECK-NEXT: ('r_type', 0x00000002)
// CHECK-NEXT: ('r_addend', 0xfffffffc)
// CHECK-NEXT: ),