[lld] r289471 - [ELF][MIPS] Calculate default _gp value relative to the GPREL section with the lowest address
Simon Atanasyan via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 12 13:34:12 PST 2016
Author: atanasyan
Date: Mon Dec 12 15:34:11 2016
New Revision: 289471
URL: http://llvm.org/viewvc/llvm-project?rev=289471&view=rev
Log:
[ELF][MIPS] Calculate default _gp value relative to the GPREL section with the lowest address
Added:
lld/trunk/test/ELF/mips-gp-lowest.s
Modified:
lld/trunk/ELF/Writer.cpp
Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=289471&r1=289470&r2=289471&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Dec 12 15:34:11 2016
@@ -1490,8 +1490,16 @@ template <class ELFT> void Writer<ELFT>:
// Setup MIPS _gp_disp/__gnu_local_gp symbols which should
// be equal to the _gp symbol's value.
if (Config->EMachine == EM_MIPS) {
- if (!ElfSym<ELFT>::MipsGp->Value)
- ElfSym<ELFT>::MipsGp->Value = In<ELFT>::MipsGot->getVA() + 0x7ff0;
+ if (!ElfSym<ELFT>::MipsGp->Value) {
+ // Find GP-relative section with the lowest address
+ // and use this address to calculate default _gp value.
+ uintX_t Gp = -1;
+ for (const OutputSectionBase * OS : OutputSections)
+ if ((OS->Flags & SHF_MIPS_GPREL) && OS->Addr < Gp)
+ Gp = OS->Addr;
+ if (Gp != (uintX_t)-1)
+ ElfSym<ELFT>::MipsGp->Value = Gp + 0x7ff0;
+ }
if (ElfSym<ELFT>::MipsGpDisp)
ElfSym<ELFT>::MipsGpDisp->Value = ElfSym<ELFT>::MipsGp->Value;
if (ElfSym<ELFT>::MipsLocalGp)
Added: lld/trunk/test/ELF/mips-gp-lowest.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/mips-gp-lowest.s?rev=289471&view=auto
==============================================================================
--- lld/trunk/test/ELF/mips-gp-lowest.s (added)
+++ lld/trunk/test/ELF/mips-gp-lowest.s Mon Dec 12 15:34:11 2016
@@ -0,0 +1,44 @@
+# Check that default _gp value is calculated relative
+# to the GP-relative section with the lowest address.
+
+# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
+# RUN: echo "SECTIONS { \
+# RUN: .sdata : { *(.sdata) } \
+# RUN: .got : { *(.got) } }" > %t.rel.script
+# RUN: ld.lld %t.o --script %t.rel.script -shared -o %t.so
+# RUN: llvm-readobj -s -t %t.so | FileCheck %s
+
+# REQUIRES: mips
+
+ .text
+ .global foo
+foo:
+ lui $gp, %call16(foo)
+
+ .sdata
+ .word 0
+
+# CHECK: Section {
+# CHECK: Name: .sdata
+# CHECK-NEXT: Type: SHT_PROGBITS
+# CHECK-NEXT: Flags [
+# CHECK-NEXT: SHF_ALLOC
+# CHECK-NEXT: SHF_MIPS_GPREL
+# CHECK-NEXT: SHF_WRITE
+# CHECK-NEXT: ]
+# CHECK-NEXT: Address: 0xDD
+# CHECK: }
+# CHECK: Section {
+# CHECK: Name: .got
+# CHECK-NEXT: Type: SHT_PROGBITS
+# CHECK-NEXT: Flags [
+# CHECK-NEXT: SHF_ALLOC
+# CHECK-NEXT: SHF_MIPS_GPREL
+# CHECK-NEXT: SHF_WRITE
+# CHECK-NEXT: ]
+# CHECK-NEXT: Address: 0xE4
+# CHECK: }
+
+# CHECK: Name: _gp (5)
+# CHECK-NEXT: Value: 0x80CD
+# ^-- 0xDD + 0x7ff0
More information about the llvm-commits
mailing list