Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ELF] Better resemble GNU ld when placing orphan sections into memory…
… regions An orphan section should be placed in the same memory region as its anchor section if the latter specifies the memory region explicitly. If there is no explicit assignment for the anchor section in the linker script, its memory region is selected by matching attributes, and the same should be done for the orphan section. Before the patch, some scripts that were handled smoothly in GNU ld caused an "error: no memory region specified for section" in lld. Differential Revision: https://reviews.llvm.org/D112925
- Loading branch information
1 parent
74d9c4a
commit d2dd36b
Showing
4 changed files
with
143 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
REQUIRES: x86 | ||
|
||
RUN: split-file %s %ts | ||
RUN: llvm-mc -filetype=obj -triple=x86_64 %ts/s -o %t.o | ||
|
||
## Check that despite having a lower sort rank, an orphan section '.init_array' | ||
## is placed after '.data' and '.data2' and in the same memory region. | ||
|
||
## Also check that a non-SHF_ALLOC orphan section '.nonalloc' is not placed in | ||
## a memory region. Both defined memory regions are exhausted after all expected | ||
## sections are added, thus, trying to put any unexpected section would lead to | ||
## an error. | ||
|
||
RUN: ld.lld -o %t -T %ts/t %t.o | ||
RUN: llvm-readelf -S %t | FileCheck %s | ||
|
||
CHECK: Name Type Address Off Size | ||
CHECK: .text PROGBITS 0000000000008000 {{[0-9a-f]+}} 000004 | ||
CHECK: .data PROGBITS 0000000000009000 {{[0-9a-f]+}} 000008 | ||
CHECK: .data2 PROGBITS 0000000000009008 {{[0-9a-f]+}} 00000c | ||
CHECK: .init_array INIT_ARRAY 0000000000009014 {{[0-9a-f]+}} 000010 | ||
CHECK: .nonalloc PROGBITS 0000000000000000 {{[0-9a-f]+}} 000010 | ||
|
||
## Check that attributes of memory regions are ignored for orphan sections when | ||
## the anchor section specifies the memory region explicitly, This seems to | ||
## contradict https://sourceware.org/binutils/docs/ld/MEMORY.html, but better | ||
## resembles the way GNU ld actually works. | ||
|
||
RUN: ld.lld -o %t2 -T %ts/t2 %t.o | ||
RUN: llvm-readelf -S %t2 | FileCheck %s | ||
|
||
## Same as the previous case, but now properties of sections conflict with | ||
## memory region attributes. Still, orphan sections are placed in the same | ||
## regions as their anchors. | ||
|
||
RUN: ld.lld -o %t3 -T %ts/t3 %t.o | ||
RUN: llvm-readelf -S %t3 | FileCheck %s | ||
|
||
## Check that when memory regions for anchor sections are not specified | ||
## explicitly and are selected by attributes, orphan sections are also assigned | ||
## to memory regions by matching properties. | ||
|
||
RUN: ld.lld -o %t4 -T %ts/t4 %t.o | ||
RUN: llvm-readelf -S %t4 | FileCheck %s --check-prefix=CHECK4 | ||
|
||
CHECK4: Name Type Address Off Size | ||
CHECK4: .text PROGBITS 0000000000008000 {{[0-9a-f]+}} 000004 | ||
CHECK4: .init_array INIT_ARRAY 0000000000009000 {{[0-9a-f]+}} 000010 | ||
CHECK4: .data PROGBITS 0000000000009010 {{[0-9a-f]+}} 000008 | ||
CHECK4: .data2 PROGBITS 0000000000009018 {{[0-9a-f]+}} 00000c | ||
CHECK4: .nonalloc PROGBITS 0000000000000000 {{[0-9a-f]+}} 000010 | ||
|
||
#--- s | ||
.text | ||
.zero 4 | ||
|
||
.data | ||
.zero 8 | ||
|
||
.section .data2,"aw",@progbits | ||
.zero 0xc | ||
|
||
.section .init_array,"aw",@init_array | ||
.zero 0x10 | ||
|
||
.section .nonalloc,"" | ||
.zero 0x10 | ||
|
||
#--- t | ||
MEMORY | ||
{ | ||
TEXT : ORIGIN = 0x8000, LENGTH = 0x4 | ||
DATA : ORIGIN = 0x9000, LENGTH = 0x24 | ||
} | ||
|
||
SECTIONS | ||
{ | ||
.text : { *(.text) } > TEXT | ||
.data : { *(.data) } > DATA | ||
} | ||
|
||
#--- t2 | ||
MEMORY | ||
{ | ||
TEXT (rwx) : ORIGIN = 0x8000, LENGTH = 0x4 | ||
DATA (rwx) : ORIGIN = 0x9000, LENGTH = 0x24 | ||
} | ||
|
||
SECTIONS | ||
{ | ||
.text : { *(.text) } > TEXT | ||
.data : { *(.data) } > DATA | ||
} | ||
|
||
#--- t3 | ||
MEMORY | ||
{ | ||
TEXT (!w) : ORIGIN = 0x8000, LENGTH = 0x4 | ||
DATA (!w) : ORIGIN = 0x9000, LENGTH = 0x24 | ||
} | ||
|
||
SECTIONS | ||
{ | ||
.text : { *(.text) } > TEXT | ||
.data : { *(.data) } > DATA | ||
} | ||
|
||
#--- t4 | ||
MEMORY | ||
{ | ||
TEXT (rx) : ORIGIN = 0x8000, LENGTH = 0x4 | ||
DATA (w!x) : ORIGIN = 0x9000, LENGTH = 0x24 | ||
} | ||
|
||
SECTIONS | ||
{ | ||
.text : { *(.text) } | ||
} |