xref: /llvm-project/lld/test/ELF/linkerscript/orphan-memory.test (revision d2dd36bbbe508ba97ab0adc5a834306f6fdc3a78)
1REQUIRES: x86
2
3RUN: split-file %s %ts
4RUN: llvm-mc -filetype=obj -triple=x86_64 %ts/s -o %t.o
5
6## Check that despite having a lower sort rank, an orphan section '.init_array'
7## is placed after '.data' and '.data2' and in the same memory region.
8
9## Also check that a non-SHF_ALLOC orphan section '.nonalloc' is not placed in
10## a memory region. Both defined memory regions are exhausted after all expected
11## sections are added, thus, trying to put any unexpected section would lead to
12## an error.
13
14RUN: ld.lld -o %t -T %ts/t %t.o
15RUN: llvm-readelf -S %t | FileCheck %s
16
17CHECK: Name        Type       Address          Off           Size
18CHECK: .text       PROGBITS   0000000000008000 {{[0-9a-f]+}} 000004
19CHECK: .data       PROGBITS   0000000000009000 {{[0-9a-f]+}} 000008
20CHECK: .data2      PROGBITS   0000000000009008 {{[0-9a-f]+}} 00000c
21CHECK: .init_array INIT_ARRAY 0000000000009014 {{[0-9a-f]+}} 000010
22CHECK: .nonalloc   PROGBITS   0000000000000000 {{[0-9a-f]+}} 000010
23
24## Check that attributes of memory regions are ignored for orphan sections when
25## the anchor section specifies the memory region explicitly, This seems to
26## contradict https://sourceware.org/binutils/docs/ld/MEMORY.html, but better
27## resembles the way GNU ld actually works.
28
29RUN: ld.lld -o %t2 -T %ts/t2 %t.o
30RUN: llvm-readelf -S %t2 | FileCheck %s
31
32## Same as the previous case, but now properties of sections conflict with
33## memory region attributes. Still, orphan sections are placed in the same
34## regions as their anchors.
35
36RUN: ld.lld -o %t3 -T %ts/t3 %t.o
37RUN: llvm-readelf -S %t3 | FileCheck %s
38
39## Check that when memory regions for anchor sections are not specified
40## explicitly and are selected by attributes, orphan sections are also assigned
41## to memory regions by matching properties.
42
43RUN: ld.lld -o %t4 -T %ts/t4 %t.o
44RUN: llvm-readelf -S %t4 | FileCheck %s --check-prefix=CHECK4
45
46CHECK4: Name        Type       Address          Off           Size
47CHECK4: .text       PROGBITS   0000000000008000 {{[0-9a-f]+}} 000004
48CHECK4: .init_array INIT_ARRAY 0000000000009000 {{[0-9a-f]+}} 000010
49CHECK4: .data       PROGBITS   0000000000009010 {{[0-9a-f]+}} 000008
50CHECK4: .data2      PROGBITS   0000000000009018 {{[0-9a-f]+}} 00000c
51CHECK4: .nonalloc   PROGBITS   0000000000000000 {{[0-9a-f]+}} 000010
52
53#--- s
54  .text
55  .zero 4
56
57  .data
58  .zero 8
59
60  .section .data2,"aw",@progbits
61  .zero 0xc
62
63  .section .init_array,"aw",@init_array
64  .zero 0x10
65
66  .section .nonalloc,""
67  .zero 0x10
68
69#--- t
70MEMORY
71{
72  TEXT : ORIGIN = 0x8000, LENGTH = 0x4
73  DATA : ORIGIN = 0x9000, LENGTH = 0x24
74}
75
76SECTIONS
77{
78  .text : { *(.text) } > TEXT
79  .data : { *(.data) } > DATA
80}
81
82#--- t2
83MEMORY
84{
85  TEXT (rwx) : ORIGIN = 0x8000, LENGTH = 0x4
86  DATA (rwx) : ORIGIN = 0x9000, LENGTH = 0x24
87}
88
89SECTIONS
90{
91  .text : { *(.text) } > TEXT
92  .data : { *(.data) } > DATA
93}
94
95#--- t3
96MEMORY
97{
98  TEXT (!w) : ORIGIN = 0x8000, LENGTH = 0x4
99  DATA (!w) : ORIGIN = 0x9000, LENGTH = 0x24
100}
101
102SECTIONS
103{
104  .text : { *(.text) } > TEXT
105  .data : { *(.data) } > DATA
106}
107
108#--- t4
109MEMORY
110{
111  TEXT (rx)  : ORIGIN = 0x8000, LENGTH = 0x4
112  DATA (w!x) : ORIGIN = 0x9000, LENGTH = 0x24
113}
114
115SECTIONS
116{
117  .text : { *(.text) }
118}
119