[LinkerScript] Adding test cases for SECTIONS semantics

llvm-svn: 232404
This commit is contained in:
Rafael Auler 2015-03-16 19:56:42 +00:00
parent 4da7a29a11
commit 8dd680dcb1
5 changed files with 414 additions and 0 deletions

View File

@ -0,0 +1,88 @@
---
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
OSABI: ELFOSABI_GNU
Type: ET_REL
Machine: EM_X86_64
Sections:
- Name: .text
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x0000000000000010
Content: 554889E5B000E800000000BF01000000BA0E0000004889C6E80000000031C05DC3
- Name: .rela.text
Type: SHT_RELA
Link: .symtab
AddressAlign: 0x0000000000000008
Info: .text
Relocations:
- Offset: 0x0000000000000007
Symbol: prog2
Type: R_X86_64_PC32
Addend: -4
- Offset: 0x0000000000000019
Symbol: write
Type: R_X86_64_PC32
Addend: -4
- Name: .data
Type: SHT_PROGBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
AddressAlign: 0x0000000000000004
Content: ''
- Name: .bss
Type: SHT_NOBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
AddressAlign: 0x0000000000000004
Content: ''
- Name: .comment
Type: SHT_PROGBITS
Flags: [ SHF_MERGE, SHF_STRINGS ]
AddressAlign: 0x0000000000000001
Content: 00636C616E672076657273696F6E20332E372E302028687474703A2F2F6C6C766D2E6F72672F6769742F636C616E672E6769742036336134646334616430343938646139623934386330383263623735336430353735323938346638292028687474703A2F2F6C6C766D2E6F72672F6769742F6C6C766D2E67697420623838363135326664656538376564653738613565643965616638663664313839343033616266312900
- Name: .note.GNU-stack
Type: SHT_PROGBITS
AddressAlign: 0x0000000000000001
Content: ''
- Name: .eh_frame
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC ]
AddressAlign: 0x0000000000000008
Content: 1400000000000000037A5200017810011B0C0708900100001C0000001C000000000000002100000000410E108602430D0600000000000000
- Name: .rela.eh_frame
Type: SHT_RELA
Link: .symtab
AddressAlign: 0x0000000000000008
Info: .eh_frame
Relocations:
- Offset: 0x0000000000000020
Symbol: .text
Type: R_X86_64_PC32
Symbols:
Local:
- Name: .text
Type: STT_SECTION
Section: .text
- Name: .data
Type: STT_SECTION
Section: .data
- Name: .bss
Type: STT_SECTION
Section: .bss
- Name: .comment
Type: STT_SECTION
Section: .comment
- Name: .note.GNU-stack
Type: STT_SECTION
Section: .note.GNU-stack
- Name: .eh_frame
Type: STT_SECTION
Section: .eh_frame
Global:
- Name: main
Type: STT_FUNC
Section: .text
Size: 0x0000000000000021
- Name: prog2
- Name: write
...

View File

@ -0,0 +1,89 @@
---
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
OSABI: ELFOSABI_GNU
Type: ET_REL
Machine: EM_X86_64
Sections:
- Name: .text
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x0000000000000010
Content: 554889E548B800000000000000005DC3
- Name: .rela.text
Type: SHT_RELA
Link: .symtab
AddressAlign: 0x0000000000000008
Info: .text
Relocations:
- Offset: 0x0000000000000006
Symbol: .rodata.str1.1
Type: R_X86_64_64
- Name: .data
Type: SHT_PROGBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
AddressAlign: 0x0000000000000004
Content: ''
- Name: .bss
Type: SHT_NOBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
AddressAlign: 0x0000000000000004
Content: ''
- Name: .rodata.str1.1
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_MERGE, SHF_STRINGS ]
AddressAlign: 0x0000000000000001
Content: 48656C6C6F2C20776F726C64210A00
- Name: .comment
Type: SHT_PROGBITS
Flags: [ SHF_MERGE, SHF_STRINGS ]
AddressAlign: 0x0000000000000001
Content: 00636C616E672076657273696F6E20332E372E302028687474703A2F2F6C6C766D2E6F72672F6769742F636C616E672E6769742036336134646334616430343938646139623934386330383263623735336430353735323938346638292028687474703A2F2F6C6C766D2E6F72672F6769742F6C6C766D2E67697420623838363135326664656538376564653738613565643965616638663664313839343033616266312900
- Name: .note.GNU-stack
Type: SHT_PROGBITS
AddressAlign: 0x0000000000000001
Content: ''
- Name: .eh_frame
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC ]
AddressAlign: 0x0000000000000008
Content: 1400000000000000037A5200017810011B0C0708900100001C0000001C000000000000001000000000410E108602430D0600000000000000
- Name: .rela.eh_frame
Type: SHT_RELA
Link: .symtab
AddressAlign: 0x0000000000000008
Info: .eh_frame
Relocations:
- Offset: 0x0000000000000020
Symbol: .text
Type: R_X86_64_PC32
Symbols:
Local:
- Name: .text
Type: STT_SECTION
Section: .text
- Name: .data
Type: STT_SECTION
Section: .data
- Name: .bss
Type: STT_SECTION
Section: .bss
- Name: .rodata.str1.1
Type: STT_SECTION
Section: .rodata.str1.1
- Name: .comment
Type: STT_SECTION
Section: .comment
- Name: .note.GNU-stack
Type: STT_SECTION
Section: .note.GNU-stack
- Name: .eh_frame
Type: STT_SECTION
Section: .eh_frame
Global:
- Name: prog2
Type: STT_FUNC
Section: .text
Size: 0x0000000000000010
...

View File

@ -0,0 +1,52 @@
---
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
OSABI: ELFOSABI_GNU
Type: ET_REL
Machine: EM_X86_64
Sections:
- Name: .text
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x0000000000000004
Content: B8010000000F05C3E800000000B83C0000000F05C3
- Name: .rela.text
Type: SHT_RELA
Link: .symtab
AddressAlign: 0x0000000000000008
Info: .text
Relocations:
- Offset: 0x0000000000000009
Symbol: main
Type: R_X86_64_PC32
Addend: -4
- Name: .data
Type: SHT_PROGBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
AddressAlign: 0x0000000000000004
Content: ''
- Name: .bss
Type: SHT_NOBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
AddressAlign: 0x0000000000000004
Content: ''
Symbols:
Local:
- Name: .text
Type: STT_SECTION
Section: .text
- Name: .data
Type: STT_SECTION
Section: .data
- Name: .bss
Type: STT_SECTION
Section: .bss
Global:
- Name: _start
Section: .text
Value: 0x0000000000000008
- Name: write
Section: .text
- Name: main
...

View File

@ -0,0 +1,97 @@
/*
Tests a simple linker script that changes the order of output sections and
also changes the address of output sections by using simple expressions.
This test uses three X86-64 input objects, prog1.o, prog2.o and prog3.o,
which were created with the following C or assembly code:
*** prog1.o:
(command line clang -c prog1.c -o prog1.o)
const char *prog2();
void write(int, const char *, int);
int main() {
write(1, prog2(), 14);
}
*** prog2.o:
(command line clang -c prog2.c -o prog2.o)
const char *prog2() {
return "Hello, world!\n";
}
*** prog3.o:
(command line clang -c prog3.S -o prog3.o)
.globl write
write:
mov $1, %eax
syscall
ret
.globl _start
_start:
call main
mov $60, %eax
syscall
ret
We use the following linker script for this test:
*/
ENTRY(_start)
SECTIONS
{
. = 0x500000;
.text : { prog1.o(.text) }
.mystring : { prog2.o(.rodata.str1.1) }
. = . + 0x6000;
.text.2 : {prog3.o(.text) prog2.o(.text) }
}
/*
RUN: mkdir -p %T
RUN: yaml2obj -format=elf %p/Inputs/prog1.o.yaml -o=%T/prog1.o
RUN: yaml2obj -format=elf %p/Inputs/prog2.o.yaml -o=%T/prog2.o
RUN: yaml2obj -format=elf %p/Inputs/prog3.o.yaml -o=%T/prog3.o
RUN: cd %T
RUN: lld -flavor gnu -target x86_64 -T %s prog1.o prog2.o prog3.o \
RUN: -static -o %t1
RUN: llvm-readobj -s %t1 | FileCheck -check-prefix CHECKSECTIONS %s
CHECKSECTIONS: Index: 1
CHECKSECTIONS: Name: .text
CHECKSECTIONS: Address: 0x500000
CHECKSECTIONS: Size: 33
CHECKSECTIONS: Index: 2
CHECKSECTIONS: Name: .mystring
CHECKSECTIONS: Address: 0x500021
CHECKSECTIONS: Size: 15
CHECKSECTIONS: Index: 3
CHECKSECTIONS: Name: .text.2
CHECKSECTIONS: Address: 0x506030
CHECKSECTIONS: Size: 48
RUN: llvm-readobj -symbols %t1 | FileCheck -check-prefix CHECKSYMS %s
CHECKSYMS: Name: main
CHECKSYMS-NEXT: Value: 0x500000
CHECKSYMS: Name: write
CHECKSYMS-NEXT: Value: 0x506030
CHECKSYMS: Name: _start
CHECKSYMS-NEXT: Value: 0x506038
CHECKSYMS: Name: prog2
CHECKSYMS-NEXT: Value: 0x506050
*/

View File

@ -0,0 +1,88 @@
/*
Tests a linker script that uses the SECTIONS command with rules containing
wildcards and simple SORT directives. It also tests that the linker script
evaluates the expressions in the same order as the one written in the script
file.
This test uses three X86-64 input objects, prog1.o, prog2.o and prog3.o,
which were created with the following C or assembly code:
*** prog1.o:
(command line clang -c prog1.c -o prog1.o)
const char *prog2();
void write(int, const char *, int);
int main() {
write(1, prog2(), 14);
}
*** prog2.o:
(command line clang -c prog2.c -o prog2.o)
const char *prog2() {
return "Hello, world!\n";
}
*** prog3.o:
(command line clang -c prog3.S -o prog3.o)
.globl write
write:
mov $1, %eax
syscall
ret
.globl _start
_start:
call main
mov $60, %eax
syscall
ret
We use the following linker script for this test:
*/
ENTRY(_start)
SECTIONS
{
my_start_addr = 0x500000;
my_symbol = my_start_addr;
. = my_symbol;
.foo : { SORT(*)(.text .rodata*) }
}
/*
RUN: mkdir -p %T
RUN: yaml2obj -format=elf %p/Inputs/prog1.o.yaml -o=%T/p1.o
RUN: yaml2obj -format=elf %p/Inputs/prog2.o.yaml -o=%T/p2.o
RUN: yaml2obj -format=elf %p/Inputs/prog3.o.yaml -o=%T/p3.o
RUN: cd %T
RUN: lld -flavor gnu -target x86_64 -T %s p1.o p2.o p3.o \
RUN: -static -o %t1
RUN: llvm-readobj -s %t1 | FileCheck -check-prefix CHECKSECTIONS %s
CHECKSECTIONS: Index: 1
CHECKSECTIONS: Name: .foo
CHECKSECTIONS: Address: 0x500000
CHECKSECTIONS: Size: 101
RUN: llvm-readobj -symbols %t1 | FileCheck -check-prefix CHECKSYMS %s
CHECKSYMS: Name: main
CHECKSYMS-NEXT: Value: 0x500000
CHECKSYMS: Name: prog2
CHECKSYMS-NEXT: Value: 0x500030
CHECKSYMS: Name: write
CHECKSYMS-NEXT: Value: 0x500050
CHECKSYMS: Name: _start
CHECKSYMS-NEXT: Value: 0x500058
*/