[Mips] Emit PLT entries for more static relocations.

llvm-svn: 206358
This commit is contained in:
Simon Atanasyan 2014-04-16 06:16:32 +00:00
parent abb4ac7f87
commit 60536ee625
4 changed files with 255 additions and 6 deletions

View File

@ -182,7 +182,7 @@ private:
void handleReference(Reference &ref);
void handlePlain(Reference &ref);
void handlePLT(Reference &ref);
void handle26(Reference &ref);
void handleGOT(Reference &ref);
const GOTAtom *getLocalGOTEntry(const Reference &ref);
@ -266,7 +266,7 @@ void RelocationPass<ELFT>::handleReference(Reference &ref) {
handlePlain(ref);
break;
case R_MIPS_26:
handlePLT(ref);
handle26(ref);
break;
case R_MIPS_GOT16:
case R_MIPS_CALL16:
@ -287,11 +287,22 @@ void RelocationPass<ELFT>::handlePlain(Reference &ref) {
if (!ref.target())
return;
auto sla = dyn_cast<SharedLibraryAtom>(ref.target());
if (sla && sla->type() == SharedLibraryAtom::Type::Data)
if (!sla)
return;
switch (sla->type()) {
case SharedLibraryAtom::Type::Data:
ref.setTarget(getObjectEntry(sla));
break;
case SharedLibraryAtom::Type::Code:
ref.setTarget(getPLTEntry(sla));
break;
default:
// Nothing to do.
break;
}
}
template <typename ELFT> void RelocationPass<ELFT>::handlePLT(Reference &ref) {
template <typename ELFT> void RelocationPass<ELFT>::handle26(Reference &ref) {
if (ref.kindValue() == R_MIPS_26 && !isLocal(ref.target())) {
ref.setKindValue(LLD_R_MIPS_GLOBAL_26);
@ -299,8 +310,9 @@ template <typename ELFT> void RelocationPass<ELFT>::handlePLT(Reference &ref) {
const_cast<Reference &>(ref).setTarget(getLA25Entry(ref.target()));
}
if (isa<SharedLibraryAtom>(ref.target()))
ref.setTarget(getPLTEntry(ref.target()));
const auto *sla = dyn_cast<SharedLibraryAtom>(ref.target());
if (sla && sla->type() == SharedLibraryAtom::Type::Code)
ref.setTarget(getPLTEntry(sla));
}
template <typename ELFT> void RelocationPass<ELFT>::handleGOT(Reference &ref) {

View File

@ -0,0 +1,55 @@
# Define symbols in the .text (T1, T2, T3) and .data (D1, D2, D3) sections.
# The object file has PIC, CPIC flags.
!ELF
FileHeader: !FileHeader
Class: ELFCLASS32
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_MIPS
Flags: [EF_MIPS_PIC, EF_MIPS_CPIC]
Sections:
- Name: .text
Type: SHT_PROGBITS
Content: "000000000000000000000000"
AddressAlign: 16
Flags: [SHF_EXECINSTR, SHF_ALLOC]
- Name: .data
Type: SHT_PROGBITS
Content: "0000000000000000"
AddressAlign: 16
Flags: [SHF_WRITE, SHF_ALLOC]
Symbols:
Global:
- Name: T1
Section: .text
Type: STT_FUNC
Value: 0x0
Size: 4
- Name: T2
Section: .text
Type: STT_FUNC
Value: 0x4
Size: 4
- Name: T3
Section: .text
Type: STT_FUNC
Value: 0x8
Size: 4
- Name: D1
Section: .data
Type: STT_OBJECT
Value: 0x0
Size: 4
- Name: D2
Section: .data
Type: STT_OBJECT
Value: 0x4
Size: 4
- Name: D3
Section: .data
Type: STT_OBJECT
Value: 0x8
Size: 4

View File

@ -0,0 +1,134 @@
# Test1. Check PLT entries creation when:
# a) Linking non-shared executable file
# b) Relocations' targets are symbols defined in the shared object.
#
# RUN: yaml2obj -format=elf %S/Inputs/pic-obj.yaml > %t-so-obj
# RUN: lld -flavor gnu -target mipsel -shared -o %t-so %t-so-obj
# RUN: yaml2obj -format=elf %s > %t-obj
# RUN: lld -flavor gnu -target mipsel -e T0 -o %t1-exe %t-obj %t-so
# RUN: llvm-objdump -disassemble %t1-exe | FileCheck -check-prefix=PLT1 %s
# PLT1: Disassembly of section .plt:
# PLT1-NEXT: .plt:
# PLT1-NEXT: 400210: 40 00 1c 3c lui $gp, 64
# PLT1-NEXT: 400214: 00 20 99 8f lw $25, 8192($gp)
# PLT1-NEXT: 400218: 00 20 9c 27 addiu $gp, $gp, 8192
# PLT1-NEXT: 40021c: 23 c0 1c 03 subu $24, $24, $gp
# PLT1-NEXT: 400220: 21 78 e0 03 move $15, $ra
# PLT1-NEXT: 400224: 82 c0 18 00 srl $24, $24, 2
# PLT1-NEXT: 400228: 09 f8 20 03 jalr $25
# PLT1-NEXT: 40022c: fe ff 18 27 addiu $24, $24, -2
#
# PLT1-NEXT: 400230: 40 00 0f 3c lui $15, 64
# PLT1-NEXT: 400234: 08 20 f9 8d lw $25, 8200($15)
# PLT1-NEXT: 400238: 08 00 20 03 jr $25
# PLT1-NEXT: 40023c: 08 20 f8 25 addiu $24, $15, 8200
#
# PLT1-NEXT: 400240: 40 00 0f 3c lui $15, 64
# PLT1-NEXT: 400244: 0c 20 f9 8d lw $25, 8204($15)
# PLT1-NEXT: 400248: 08 00 20 03 jr $25
# PLT1-NEXT: 40024c: 0c 20 f8 25 addiu $24, $15, 8204
#
# PLT1-NEXT: 400250: 40 00 0f 3c lui $15, 64
# PLT1-NEXT: 400254: 10 20 f9 8d lw $25, 8208($15)
# PLT1-NEXT: 400258: 08 00 20 03 jr $25
# PLT1-NEXT: 40025c: 10 20 f8 25 addiu $24, $15, 8208
# Test2. Check PLT entries creation when:
# a) Linking non-shared executable file
# b) Relocations' targets are undefined symbols
#
# R1UN: yaml2obj -format=elf %s > %t-obj
# R1UN: lld -flavor gnu -target mipsel --noinhibit-exec -e T0 -o %t2-exe %t-obj
# R1UN: llvm-objdump -disassemble %t2-exe | FileCheck -check-prefix=PLT2 %s
# PLT2-NOT: Disassembly of section .plt:
# Test3. Check PLT entries creation when:
# a) Linking non-shared executable file
# b) Relocations' targets are symbols defined in the other object.
#
# R1UN: yaml2obj -format=elf %s > %t-obj1
# R1UN: yaml2obj -format=elf %S/Inputs/pic-obj.yaml > %t-obj2
# R1UN: lld -flavor gnu -target mipsel -e T0 -o %t3-exe %t-obj1 %t-obj2
# R1UN: llvm-objdump -disassemble %t3-exe | FileCheck -check-prefix=PLT3 %s
# PLT3-NOT: Disassembly of section .plt:
!ELF
FileHeader: !FileHeader
Class: ELFCLASS32
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_MIPS
Flags: [EF_MIPS_CPIC]
Sections:
- Name: .text
Type: SHT_PROGBITS
Content: "0000000000000000"
AddressAlign: 16
Flags: [SHF_EXECINSTR, SHF_ALLOC]
- Name: .data
Type: SHT_PROGBITS
Content: "0000000000000000"
AddressAlign: 16
Flags: [SHF_WRITE, SHF_ALLOC]
- Name: .rel.text
Type: SHT_REL
Info: .text
AddressAlign: 4
Relocations:
- Offset: 0x04
Symbol: T3
Type: R_MIPS_26
- Name: .rel.data
Type: SHT_REL
Info: .data
AddressAlign: 4
Relocations:
- Offset: 0x00
Symbol: T1
Type: R_MIPS_HI16
- Offset: 0x00
Symbol: T1
Type: R_MIPS_LO16
- Offset: 0x04
Symbol: T2
Type: R_MIPS_32
- Offset: 0x04
Symbol: D1
Type: R_MIPS_HI16
- Offset: 0x04
Symbol: D1
Type: R_MIPS_LO16
- Offset: 0x04
Symbol: D2
Type: R_MIPS_32
Symbols:
Global:
- Name: T0
Section: .text
Type: STT_FUNC
Value: 0x0
Size: 8
- Name: T1
Type: STT_FUNC
- Name: T2
Type: STT_FUNC
- Name: T3
Type: STT_FUNC
- Name: D0
Section: .data
Type: STT_OBJECT
Value: 0x0
Size: 8
- Name: D1
Type: STT_OBJECT
- Name: D2
Type: STT_OBJECT

View File

@ -0,0 +1,48 @@
# Test1. Check PLT entries creation when:
# a) Linking shared object file
# b) Relocations' targets are undefined symbols
#
# RUN: yaml2obj -format=elf %s > %t-obj
# RUN: lld -flavor gnu -target mipsel -shared --noinhibit-exec -o %t1-so %t-obj
# RUN: llvm-objdump -disassemble %t1-so | FileCheck -check-prefix=PLT1 %s
# PLT1-NOT: Disassembly of section .plt:
!ELF
FileHeader: !FileHeader
Class: ELFCLASS32
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_MIPS
Flags: [EF_MIPS_PIC, EF_MIPS_CPIC]
Sections:
- Name: .text
Type: SHT_PROGBITS
Content: "0000000000000000"
AddressAlign: 16
Flags: [SHF_EXECINSTR, SHF_ALLOC]
- Name: .data
Type: SHT_PROGBITS
Content: "0000000000000000"
AddressAlign: 16
Flags: [SHF_WRITE, SHF_ALLOC]
- Name: .rel.text
Type: SHT_REL
Info: .text
AddressAlign: 4
Relocations:
- Offset: 0x00
Symbol: T1
Type: R_MIPS_32
Symbols:
Global:
- Name: T0
Section: .text
Type: STT_FUNC
Value: 0x0
Size: 4
- Name: T1