llvm.org GIT mirror llvm / d280420
[SPARC] Repair GOT references to internal symbols. They had been getting emitted as a section + offset reference, which is bogus since the value needs to be the offset within the GOT, not the actual address of the symbol's object. Differential Revision: http://reviews.llvm.org/D10441 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240020 91177308-0d34-0410-b5e6-96231b3b80d8 James Y Knight 5 years ago
5 changed file(s) with 69 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
8282 ELF_RELOC(R_SPARC_TLS_TPOFF32, 78)
8383 ELF_RELOC(R_SPARC_TLS_TPOFF64, 79)
8484 ELF_RELOC(R_SPARC_GOTDATA_HIX22, 80)
85 ELF_RELOC(R_SPARC_GOTDATA_LOX22, 81)
85 ELF_RELOC(R_SPARC_GOTDATA_LOX10, 81)
8686 ELF_RELOC(R_SPARC_GOTDATA_OP_HIX22, 82)
87 ELF_RELOC(R_SPARC_GOTDATA_OP_LOX22, 83)
87 ELF_RELOC(R_SPARC_GOTDATA_OP_LOX10, 83)
8888 ELF_RELOC(R_SPARC_GOTDATA_OP, 84)
946946 return false;
947947 }
948948
949 // Determine if an expression contains a reference to the symbol
950 // "_GLOBAL_OFFSET_TABLE_".
949951 static bool hasGOTReference(const MCExpr *Expr) {
950952 switch (Expr->getKind()) {
951953 case MCExpr::Target:
996998 return false;
997999
9981000 bool isPIC = getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_;
1001
1002 // Ugly: if a sparc assembly expression says "%hi(...)" but the
1003 // expression within contains _GLOBAL_OFFSET_TABLE_, it REALLY means
1004 // %pc22. Same with %lo -> %pc10. Worse, if it doesn't contain that,
1005 // the meaning depends on whether the assembler was invoked with
1006 // -KPIC or not: if so, it really means %got22/%got10; if not, it
1007 // actually means what it said! Sigh, historical mistakes...
9991008
10001009 switch(VK) {
10011010 default: break;
3030 protected:
3131 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
3232 bool IsPCRel) const override;
33
34 bool needsRelocateWithSymbol(const MCSymbol &Sym,
35 unsigned Type) const override;
36
3337 };
3438 }
3539
104108 return ELF::R_SPARC_NONE;
105109 }
106110
111 bool SparcELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
112 unsigned Type) const {
113 switch (Type) {
114 default:
115 return false;
116
117 // All relocations that use a GOT need a symbol, not an offset, as
118 // the offset of the symbol within the section is irrelevant to
119 // where the GOT entry is. Don't need to list all the TLS entries,
120 // as they're all marked as requiring a symbol anyways.
121 case ELF::R_SPARC_GOT10:
122 case ELF::R_SPARC_GOT13:
123 case ELF::R_SPARC_GOT22:
124 case ELF::R_SPARC_GOTDATA_HIX22:
125 case ELF::R_SPARC_GOTDATA_LOX10:
126 case ELF::R_SPARC_GOTDATA_OP_HIX22:
127 case ELF::R_SPARC_GOTDATA_OP_LOX10:
128 return true;
129 }
130 }
131
107132 MCObjectWriter *llvm::createSparcELFObjectWriter(raw_pwrite_stream &OS,
108133 bool Is64Bit,
109134 bool IsLittleEndian,
0 ; RUN: llc < %s -march=sparcv9 -filetype=obj --relocation-model=static | llvm-readobj -r | FileCheck %s --check-prefix=CHECK-ABS
11 ; RUN: llc < %s -march=sparcv9 -filetype=obj --relocation-model=pic | llvm-readobj -r | FileCheck %s --check-prefix=CHECK-PIC
22
3 ;CHECK-ABS: Relocations [
4 ;CHECK-ABS: 0x{{[0-9,A-F]+}} R_SPARC_H44 AGlobalVar 0x0
5 ;CHECK-ABS: 0x{{[0-9,A-F]+}} R_SPARC_M44 AGlobalVar 0x0
6 ;CHECK-ABS: 0x{{[0-9,A-F]+}} R_SPARC_L44 AGlobalVar 0x0
7 ;CHECK-ABS: 0x{{[0-9,A-F]+}} R_SPARC_WDISP30 bar 0x0
8 ;CHECK-ABS:]
3 ;CHECK-ABS: Relocations [
4 ;CHECK-ABS: 0x{{[0-9,A-F]+}} R_SPARC_H44 AGlobalVar 0x0
5 ;CHECK-ABS-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_M44 AGlobalVar 0x0
6 ;CHECK-ABS-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_L44 AGlobalVar 0x0
7 ;CHECK-ABS-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_H44 .rodata.str1.1 0x0
8 ;CHECK-ABS-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_M44 .rodata.str1.1 0x0
9 ;CHECK-ABS-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_WDISP30 bar 0x0
10 ;CHECK-ABS-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_L44 .rodata.str1.1 0x0
11 ;CHECK-ABS: ]
912
10 ; CHECK-PIC: Relocations [
11 ; CHECK-PIC: 0x{{[0-9,A-F]+}} R_SPARC_PC22 _GLOBAL_OFFSET_TABLE_ 0x4
12 ; CHECK-PIC: 0x{{[0-9,A-F]+}} R_SPARC_PC10 _GLOBAL_OFFSET_TABLE_ 0x8
13 ; CHECK-PIC: 0x{{[0-9,A-F]+}} R_SPARC_GOT22 AGlobalVar 0x0
14 ; CHECK-PIC: 0x{{[0-9,A-F]+}} R_SPARC_GOT10 AGlobalVar 0x0
15 ; CHECK-PIC: 0x{{[0-9,A-F]+}} R_SPARC_WPLT30 bar 0x0
16 ; CHECK-PIC: ]
13 ; CHECK-PIC: Relocations [
14 ; CHECK-PIC: 0x{{[0-9,A-F]+}} R_SPARC_PC22 _GLOBAL_OFFSET_TABLE_ 0x4
15 ; CHECK-PIC-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_PC10 _GLOBAL_OFFSET_TABLE_ 0x8
16 ; CHECK-PIC-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_GOT22 AGlobalVar 0x0
17 ; CHECK-PIC-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_GOT10 AGlobalVar 0x0
18 ; CHECK-PIC-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_GOT22 .L.mystr 0x0
19 ; CHECK-PIC-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_GOT10 .L.mystr 0x0
20 ; CHECK-PIC-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_WPLT30 bar 0x0
21 ; CHECK-PIC: ]
1722
1823
1924 @AGlobalVar = global i64 0, align 8
25 @.mystr = private unnamed_addr constant [6 x i8] c"hello\00", align 1
2026
2127 define i64 @foo(i64 %a) {
2228 entry:
2329 %0 = load i64, i64* @AGlobalVar, align 4
2430 %1 = add i64 %a, %0
25 %2 = call i64 @bar(i64 %1)
31 %2 = call i64 @bar(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.mystr, i32 0, i32 0), i64 %1)
2632 ret i64 %2
2733 }
2834
2935
30 declare i64 @bar(i64)
36 declare i64 @bar(i8*, i64)
66 ! CHECK-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_PC10 _GLOBAL_OFFSET_TABLE_ 0x8
77 ! CHECK-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_GOT22 AGlobalVar 0x0
88 ! CHECK-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_GOT10 AGlobalVar 0x0
9 ! CHECK-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_GOT22 .LC0 0x0
10 ! CHECK-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_GOT10 .LC0 0x0
911 ! CHECK-NEXT: 0x{{[0-9,A-F]+}} R_SPARC_WPLT30 bar 0x0
1012 ! CHECK: ]
1113
14 .section ".rodata"
15 .align 8
16 .LC0:
17 .asciz "string"
18 .section ".text"
1219 .text
1320 .globl foo
1421 .align 4
2835 add %i1, %o7, %i1
2936 sethi %hi(AGlobalVar), %i2
3037 add %i2, %lo(AGlobalVar), %i2
31 ldx [%i1+%i2], %i1
32 ldx [%i1], %i1
38 ldx [%i1+%i2], %i3
39 ldx [%i3], %i3
40 sethi %hi(.LC0), %i2
41 add %i2, %lo(.LC0), %i2
42 ldx [%i1+%i2], %i4
3343 call bar
3444 add %i0, %i1, %o0
3545 ret
4555 AGlobalVar:
4656 .xword 0 ! 0x0
4757 .size AGlobalVar, 8
48