From fc6e0f6f8721a59d788ba07fef8b4f3a5c07ae9a Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 15 Oct 2014 19:30:18 +0000 Subject: [PATCH] Allow forward references to section symbols. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219835 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/MC/MCContext.cpp | 9 ++++++++- test/MC/ELF/section-sym2.s | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/MC/ELF/section-sym2.s diff --git a/lib/MC/MCContext.cpp b/lib/MC/MCContext.cpp index 3ac8d658873..ea6db142fd7 100644 --- a/lib/MC/MCContext.cpp +++ b/lib/MC/MCContext.cpp @@ -119,11 +119,18 @@ MCSymbol *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) { return Sym; StringRef Name = Section.getSectionName(); + + StringMapEntry &Entry = Symbols.GetOrCreateValue(Name); + MCSymbol *OldSym = Entry.getValue(); + if (OldSym && OldSym->isUndefined()) { + Sym = OldSym; + return OldSym; + } + StringMapEntry *NameEntry = &UsedNames.GetOrCreateValue(Name); NameEntry->setValue(true); Sym = new (*this) MCSymbol(NameEntry->getKey(), /*isTemporary*/ false); - StringMapEntry &Entry = Symbols.GetOrCreateValue(Name); if (!Entry.getValue()) Entry.setValue(Sym); diff --git a/test/MC/ELF/section-sym2.s b/test/MC/ELF/section-sym2.s new file mode 100644 index 00000000000..acdb7d9547d --- /dev/null +++ b/test/MC/ELF/section-sym2.s @@ -0,0 +1,28 @@ +// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | llvm-readobj -t -r --expand-relocs | FileCheck %s + +// Test that we can forward reference a section. + +mov .rodata, %rsi +.section .rodata + +// CHECK:Relocations [ +// CHECK: Section (2) .rela.text { +// CHECK: Relocation { +// CHECK: Offset: 0x4 +// CHECK: Type: R_X86_64_32S (11) +// CHECK: Symbol: .rodata +// CHECK: Addend: 0x0 +// CHECK: } +// CHECK: } +// CHECK:] + +// There is only one .rodata symbol + +// CHECK:Symbols [ +// CHECK-NOT: Name: .rodata +// CHECK: Name: .rodata +// CHECK-NEXT: Value: 0x0 +// CHECK-NEXT: Size: 0 +// CHECK-NEXT: Binding: Local (0x0) +// CHECK-NEXT: Type: Section (0x3) +// CHECK-NOT: Name: .rodata