From 03f1b74aed795110f040b7588c8921c5de4bf1ea Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 11 Nov 2010 16:48:11 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@118793 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/MC/ELFObjectWriter.cpp | 34 +++++++++---------------- test/MC/ELF/weakref-reloc.s | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 test/MC/ELF/weakref-reloc.s diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp index 4adc188b616..f84164d253b 100644 --- a/lib/MC/ELFObjectWriter.cpp +++ b/lib/MC/ELFObjectWriter.cpp @@ -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(); diff --git a/test/MC/ELF/weakref-reloc.s b/test/MC/ELF/weakref-reloc.s new file mode 100644 index 00000000000..580d754bc6a --- /dev/null +++ b/test/MC/ELF/weakref-reloc.s @@ -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: ),