3 # RUN: rm -rf %t; split-file %s %t
5 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/main.s -o %t/main.o
6 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o
7 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/seg.s -o %t/seg.o
9 # RUN: %lld -lSystem %t/main.o %t/foo.o -o %t.out \
10 # RUN: -rename_section __FOO __bar __BAZ __quux \
11 # RUN: -rename_section __WHAT __ever __FOO __bar \
12 # RUN: -u 'section$start$__UFLAG_SEG$__uflag_sect' \
13 # RUN: -U 'section$start$__DYNAMIC$__lookup' \
14 # RUN: -U 'section$start$__DYNAMIC$__unref' \
15 # RUN: -e 'section$start$__TEXT$__text'
16 # RUN: llvm-objdump --macho --syms --section-headers %t.out > %t-dump.txt
17 # RUN: llvm-objdump --no-print-imm-hex --macho -d --no-symbolic-operands --no-show-raw-insn %t.out >> %t-dump.txt
18 # RUN: llvm-objdump --macho --function-starts %t.out >> %t-dump.txt
19 # RUN: FileCheck %s < %t-dump.txt
21 ## Setting the entry point to the start of the __text section should
22 ## set it to _main, since that's the first function in that section.
23 # RUN: llvm-objdump --macho --syms --all-headers %t.out \
24 # RUN: | FileCheck --check-prefix=MAINENTRY %s
25 # MAINENTRY: [[#%x, MAINADDR:]] g F __TEXT,__text _main
27 # MAINENTRY-NEXT: cmdsize
28 # MAINENTRY-NEXT: entryoff [[#%d, MAINADDR - 0x100000000]]
30 ## Nothing should change if we reorder two functions in the text segment.
31 ## (Reorder some section$start/end symbols too for good measure.)
32 # RUN: %lld -lSystem %t/main.o %t/foo.o -o %t.ordered.out \
33 # RUN: -order_file %t/order.txt \
34 # RUN: -rename_section __FOO __bar __BAZ __quux \
35 # RUN: -rename_section __WHAT __ever __FOO __bar \
36 # RUN: -u 'section$start$__UFLAG_SEG$__uflag_sect' \
37 # RUN: -U 'section$start$__DYNAMIC$__lookup' \
38 # RUN: -U 'section$start$__DYNAMIC$__unref' \
39 # RUN: -e 'section$start$__TEXT$__text'
40 # RUN: llvm-objdump --macho --syms --section-headers %t.ordered.out > %t-ordered-dump.txt
41 # RUN: llvm-objdump --no-print-imm-hex --macho -d --no-symbolic-operands --no-show-raw-insn %t.ordered.out >> %t-ordered-dump.txt
42 # RUN: llvm-objdump --macho --function-starts %t.out >> %t-ordered-dump.txt
43 # RUN: FileCheck %s < %t-ordered-dump.txt
45 ## `-undefined dynamic_lookup` also shouldn't change anything.
46 # RUN: %lld -lSystem %t/main.o %t/foo.o -o %t.dl.out -undefined dynamic_lookup \
47 # RUN: -rename_section __FOO __bar __BAZ __quux \
48 # RUN: -rename_section __WHAT __ever __FOO __bar \
49 # RUN: -u 'section$start$__UFLAG_SEG$__uflag_sect' \
50 # RUN: -U 'section$start$__DYNAMIC$__lookup' \
51 # RUN: -U 'section$start$__DYNAMIC$__unref' \
52 # RUN: -e 'section$start$__TEXT$__text'
53 # RUN: llvm-objdump --macho --syms --section-headers %t.dl.out > %t-dump.dl.txt
54 # RUN: llvm-objdump --no-print-imm-hex --macho -d --no-symbolic-operands --no-show-raw-insn %t.dl.out >> %t-dump.dl.txt
55 # RUN: llvm-objdump --macho --function-starts %t.out >> %t-dump.dl.txt
56 # RUN: FileCheck %s < %t-dump.dl.txt
58 ## ...except that the entry point is now _otherfun instead of _main since
59 ## _otherfun is now at the start of the __text section.
60 # RUN: llvm-objdump --macho --syms --all-headers %t.ordered.out \
61 # RUN: | FileCheck --check-prefix=OTHERENTRY %s
62 # OTHERENTRY: [[#%x, OTHERADDR:]] g F __TEXT,__text _otherfun
64 # OTHERENTRY-NEXT: cmdsize
65 # OTHERENTRY-NEXT: entryoff [[#%d, OTHERADDR - 0x100000000]]
68 ## Test that the link succeeds with dead-stripping enabled too.
69 # RUN: %lld -dead_strip -lSystem %t/main.o -o %t/stripped.out
70 # RUN: llvm-objdump --macho --syms --section-headers %t/stripped.out > %t-stripped-dump.txt
71 # RUN: llvm-objdump --no-print-imm-hex --macho -d --no-symbolic-operands --no-show-raw-insn %t/stripped.out >> %t-stripped-dump.txt
72 # RUN: FileCheck --check-prefix=STRIP %s < %t-stripped-dump.txt
74 ## -u 'section$start$*' does not cause an undefined symbol error. This matches ld64.
75 # RUN: %lld -dead_strip -lSystem %t/main.o -u 'section$start$__FOO$__notexist' -o %t/stripped1.out
76 # RUN: llvm-objdump --section-headers %t/stripped1.out | FileCheck --check-prefix=STRIP2 %s
78 ## (Fun fact: `-e 'section$start$__TEXT$__text -dead_strip` strips
79 ## everything in the text section because markLive runs well before
80 ## section$start symbols are replaced, so the entry point is just
81 ## an undefined symbol that keeps nothing alive, and then later it
82 ## sets the entry point to the start of the now-empty text section
83 ## and the output program crashes when running. This matches ld64's
86 # STRIP-LABEL: Sections:
87 # STRIP-NEXT: Idx Name Size VMA Type
88 # STRIP-NEXT: 0 __text {{[0-9a-f]*}} [[#%x, TEXTSTART:]] TEXT
89 # STRIP-NEXT: 1 __cstring 00000000 [[#%x, CSTRINGSTART:]] DATA
90 # STRIP-NEXT: 2 __data 00000000
91 # STRIP-NEXT: 3 __llvm_orderfile 00000000
92 # STRIP-NEXT: 4 __mybss 00000000
93 # STRIP-NEXT: 5 __bar 00000000
94 # STRIP-NEXT: 6 __ever 00000000
95 # STRIP-NEXT: 7 __lookup 00000000
96 # STRIP-NEXT: 8 symbol 00000000
97 # STRIP-NEXT: 9 __quux 00000000
99 # STRIP-LABEL: SYMBOL TABLE:
100 # STRIP-NOT: section$start$__FOO$__bar
102 # STRIP-LABEL: _main:
103 # STRIP: [[#%x, PC1:]]:
104 # STRIP-SAME: leaq [[#%d, TEXTSTART - PC1 - 7]](%rip), %rax
105 # STRIP-NEXT: [[#%x, PC2:]]:
106 # STRIP-SAME: leaq [[#%d, CSTRINGSTART - PC2 - 7]](%rip), %rbx
108 # STRIP2-LABEL: Sections:
109 # STRIP2-NEXT: Idx Name Size VMA Type
110 # STRIP2-NEXT: 0 __text {{[0-9a-f]*}} [[#%x, TEXTSTART:]] TEXT
111 # STRIP2-NEXT: 1 __cstring 00000000 [[#%x, CSTRINGSTART:]] DATA
112 # STRIP2-NEXT: 2 __data 00000000
113 # STRIP2-NEXT: 3 __llvm_orderfile 00000000
114 # STRIP2-NEXT: 4 __mybss 00000000
115 # STRIP2-NEXT: 5 __bar 00000000
116 # STRIP2-NEXT: 6 __notexist 00000000
117 # STRIP2-NEXT: 7 __ever 00000000
118 # STRIP2-NEXT: 8 __lookup 00000000
119 # STRIP2-NEXT: 9 symbol 00000000
120 # STRIP2-NEXT: 10 __quux 00000000
122 # CHECK-LABEL: Sections:
123 # CHECK-NEXT: Idx Name Size VMA Type
124 # CHECK: 0 __text {{[0-9a-f]*}} [[#%x, TEXTSTART:]] TEXT
125 # CHECK: 1 __aftertext {{[0-9a-f]*}} [[#%x, TEXTEND:]]
126 # CHECK: 2 __cstring {{[0-9a-f]*}} [[#%x, CSTRINGSTART:]] DATA
127 # CHECK: 3 __aftercstring {{[0-9a-f]*}} [[#%x, CSTRINGEND:]]
128 # CHECK: 4 __data 00000008 [[#%x, DATASTART:]] DATA
129 # CHECK: 5 __llvm_orderfile 00000000 [[#%x, LLVMORDERFILESTART:]] DATA
130 # CHECK: 6 __mybss 00008000 [[#%x, MYBSSSTART:]] BSS
131 # CHECK: 7 __quux 0000002a [[#%x, QUUXSTART:]]
132 # CHECK: 8 __bar 00000059 [[#%x, BARSTART:]]
133 # CHECK: 9 __uflag_sect 00000000
134 # CHECK: 10 __lookup 00000000
138 # CHECK-LABEL: SYMBOL TABLE:
139 # CHECK-NOT: section$start$__TEXT$__text
140 # CHECK-NOT: section$end$__TEXT$__text
141 # CHECK-NOT: section$start$__TEXT$__cstring
142 # CHECK-NOT: section$end$__TEXT$__cstring
143 # CHECK-NOT: section$start$__DATA$__data
144 # CHECK-NOT: section$end$__DATA$__data
145 # CHECK-NOT: section$start$__DATA$__llvm_orderfile
146 # CHECK-NOT: section$end$__DATA$__llvm_orderfile
147 # CHECK-NOT: section$start$__DYNAMIC$__lookup
148 # CHECK-NOT: section$start$__DYNAMIC$__unref
149 # CHECK: section$end$ACTUAL$symbol
150 # CHECK: section$start$ACTUAL$symbol
152 # CHECK-LABEL: _main:
154 ## The CHECK-SAMEs work around FileCheck's
155 ## "error: numeric variable 'PC2' defined earlier in the same CHECK directive"
157 ## The 7s are the length of a leaq instruction.
158 ## section$start$__TEXT$__text / section$end$__TEXT$__text
160 # CHECK: [[#%x, PC1:]]:
161 # CHECK-SAME: leaq [[#%d, TEXTSTART - PC1 - 7]](%rip), %rax
162 # CHECK-NEXT: [[#%x, PC2:]]:
163 # CHECK-SAME: leaq [[#%d, TEXTEND - PC2 - 7]](%rip), %rbx
165 ## section$start$__TEXT$__cstring / section$end$__TEXT$__cstring
166 # CHECK: [[#%x, PC3:]]:
167 # CHECK-SAME: leaq [[#%d, CSTRINGSTART - PC3 - 7]](%rip), %rax
168 # CHECK-NEXT: [[#%x, PC4:]]:
169 # CHECK-SAME: leaq [[#%d, CSTRINGEND - PC4 - 7]](%rip), %rbx
171 ## section$start$__DATA$__data / section$end$__DATA$__data
172 # CHECK: [[#%x, PC5:]]:
173 # CHECK-SAME: leaq [[#%d, DATASTART - PC5 - 7]](%rip), %rax
174 # CHECK-NEXT: [[#%x, PC6:]]:
175 # CHECK-SAME: leaq [[#%d, DATASTART + 8 - PC6 - 7]](%rip), %rbx
177 ## section$start$__MYBSS$__mybss / section$end$__MYBSS$__mybss
178 # CHECK: [[#%x, PC7:]]:
179 # CHECK-SAME: leaq [[#%d, MYBSSSTART - PC7 - 7]](%rip), %rax
180 # CHECK-NEXT: [[#%x, PC8:]]:
181 # CHECK-SAME: leaq [[#%d, MYBSSSTART + 0x8000 - PC8 - 7]](%rip), %rbx
183 ## section$start$__DATA$__llvm_orderfile / section$end$__DATA$__llvm_orderfile
184 ## This section has size 0.
185 # CHECK: [[#%x, PC9:]]:
186 # CHECK-SAME: leaq [[#%d, LLVMORDERFILESTART - PC9 - 7]](%rip), %rax
187 # CHECK-NEXT: [[#%x, PC10:]]:
188 # CHECK-SAME: leaq [[#%d, LLVMORDERFILESTART - PC10 - 7]](%rip), %rbx
190 ## Section-rename tests.
191 ## Input section __FOO/__bar is renamed to output section
192 ## __BAZ/__quux by a -rename_section flag.
193 ## section$start$__FOO$__bar ends up referring to the __BAZ/__quux section.
194 # CHECK: [[#%x, PC11:]]:
195 # CHECK-SAME: leaq [[#%d, QUUXSTART - PC11 - 7]](%rip), %rax
196 # CHECK-NEXT: [[#%x, PC12:]]:
197 # CHECK-SAME: leaq [[#%d, QUUXSTART + 42 - PC12 - 7]](%rip), %rbx
198 ## section$start$__BAZ$__quux also refers to the __BAZ/__quux section.
199 # CHECK: [[#%x, PC13:]]:
200 # CHECK-SAME: leaq [[#%d, QUUXSTART - PC13 - 7]](%rip), %rax
201 # CHECK-NEXT: [[#%x, PC14:]]:
202 # CHECK-SAME: leaq [[#%d, QUUXSTART + 42 - PC14 - 7]](%rip), %rbx
203 ## Input section __WHAT/__ever is renamed to output section
204 ## __FOO/__bar by a -rename_section flag.
205 ## section$start$__WHAT$__ever ends up referring to the __FOO/__bar section.
206 # CHECK: [[#%x, PC15:]]:
207 # CHECK-SAME: leaq [[#%d, BARSTART - PC15 - 7]](%rip), %rax
208 # CHECK-NEXT: [[#%x, PC16:]]:
209 # CHECK-SAME: leaq [[#%d, BARSTART + 89 - PC16 - 7]](%rip), %rbx
211 ## The function_starts section should not have an entry for the
212 ## section$end$__TEXT$__text symbol.
213 # CHECK: [[#%.16x, TEXTSTART]]
214 # CHECK-NOT: [[#%.16x, TEXTEND]]
216 ###############################################################################
217 ## Test segment$start and segment$end.
219 # RUN: %lld -lSystem %t/seg.o -o %t.seg.out \
220 # RUN: -rename_segment __FOO __BAZ \
221 # RUN: -rename_segment __WHAT __FOO \
222 # RUN: -u 'segment$start$__UFLAG_SEG' \
223 # RUN: -e 'segment$start$__TEXT'
224 # RUN: llvm-objdump --macho --syms %t.seg.out > %t-seg-dump.txt
225 ## llvm-objdump can't dump segment names; use lld-otool for this.
226 # RUN: llvm-otool -l %t.seg.out | grep -A6 LC_SEGMENT >> %t-seg-dump.txt
227 # RUN: llvm-objdump --no-print-imm-hex --macho -d --no-symbolic-operands --no-show-raw-insn %t.seg.out >> %t-seg-dump.txt
228 # RUN: llvm-objdump --macho --function-starts %t.out >> %t-seg-dump.txt
229 # RUN: FileCheck %s --check-prefix=SEG < %t-seg-dump.txt
231 # SEG-LABEL: SYMBOL TABLE:
232 # SEG-NOT: segment$start$__TEXT
233 # SEG-NOT: segment$end$__TEXT
234 # SEG-NOT: segment$start$__FOO
235 # SEG-NOT: segment$end$__FOO
236 # SEG-NOT: segment$start$__BAZ
237 # SEG-NOT: segment$end$__BAZ
238 # SEG-NOT: segment$start$__WHAT
239 # SEG-NOT: segment$end$__WHAT
240 # SEG-NOT: segment$start$__UFLAG_SEG
241 # SEG-NOT: segment$start$__UFLAG_SEG
242 # SEG-DAG: segment$end$REGULAR
243 # SEG-DAG: segment$start$REGULAR
245 # SEG: cmd LC_SEGMENT_64
247 # SEG-NEXT: segname __PAGEZERO
249 # SEG: cmd LC_SEGMENT_64
251 # SEG-NEXT: segname __TEXT
252 # SEG-NEXT: vmaddr 0x[[#%x, TEXTSTART:]]
253 # SEG-NEXT: vmsize 0x[[#%x, TEXTSIZE:]]
255 # SEG: cmd LC_SEGMENT_64
257 # SEG-NEXT: segname __BAZ
258 # SEG-NEXT: vmaddr 0x[[#%x, BAZSTART:]]
259 # SEG-NEXT: vmsize 0x[[#%x, BAZSIZE:]]
261 # SEG: cmd LC_SEGMENT_64
263 # SEG-NEXT: segname __FOO
264 # SEG-NEXT: vmaddr 0x[[#%x, FOOSTART:]]
265 # SEG-NEXT: vmsize 0x[[#%x, FOOSIZE:]]
267 # SEG: cmd LC_SEGMENT_64
269 # SEG-NEXT: segname __UFLAG_SEG
270 # SEG-NEXT: vmaddr 0x[[#%x, UFLAGSTART:]]
271 # SEG-NEXT: vmsize 0x0000000000000000
273 # SEG: cmd LC_SEGMENT_64
275 # SEG-NEXT: segname ASDF
276 # SEG-NEXT: vmaddr 0x[[#%x, ASDFSTART:]]
277 # SEG-NEXT: vmsize 0x0000000000000000
281 ## segment$start$__TEXT / segment$end$__TEXT
282 # SEG: [[#%x, PC1:]]:
283 # SEG-SAME: leaq [[#%d, TEXTSTART - PC1 - 7]](%rip), %rax
284 # SEG-NEXT: [[#%x, PC2:]]:
285 # SEG-SAME: leaq [[#%d, TEXTSTART + TEXTSIZE - PC2 - 7]](%rip), %rbx
287 ## segment$start$__FOO / segment$end$__FOO, which is renamed to __BAZ
288 # SEG: [[#%x, PC3:]]:
289 # SEG-SAME: leaq [[#%d, BAZSTART - PC3 - 7]](%rip), %rax
290 # SEG-NEXT: [[#%x, PC4:]]:
291 # SEG-SAME: leaq [[#%d, BAZSTART + BAZSIZE - PC4 - 7]](%rip), %rbx
293 ## segment$start$__BAZ / segment$end$__BAZ
294 # SEG: [[#%x, PC5:]]:
295 # SEG-SAME: leaq [[#%d, BAZSTART - PC5 - 7]](%rip), %rax
296 # SEG-NEXT: [[#%x, PC6:]]:
297 # SEG-SAME: leaq [[#%d, BAZSTART + BAZSIZE - PC6 - 7]](%rip), %rbx
299 ## segment$start$__WHAT / segment$end$__WHAT, which is renamed to __FOO
300 # SEG: [[#%x, PC7:]]:
301 # SEG-SAME: leaq [[#%d, FOOSTART - PC7 - 7]](%rip), %rax
302 # SEG-NEXT: [[#%x, PC8:]]:
303 # SEG-SAME: leaq [[#%d, FOOSTART + FOOSIZE - PC8 - 7]](%rip), %rbx
305 ## segment$start$ASDF / segment$end$ASDF
306 # SEG: [[#%x, PC9:]]:
307 # SEG-SAME: leaq [[#%d, ASDFSTART - PC9 - 7]](%rip), %rax
308 # SEG-NEXT: [[#%x, PC10:]]:
309 # SEG-SAME: leaq [[#%d, ASDFSTART - PC10 - 7]](%rip), %rbx
314 section$end$__TEXT$__text
315 section$start$__TEXT$__text
318 .zerofill __MYBSS,__mybss,_zero_foo,0x8000
320 .globl section$start$ACTUAL$symbol
321 .globl section$end$ACTUAL$symbol
323 ## Renamed to __BAZ,__quux by -rename_section
327 ## Renamed to __FOO,__bar by -rename_section
328 .section __WHAT,__ever
334 # Basics: start/end of existing, normal sections.
336 # For __TEXT/__text, these magic symbols shouldn't be
337 # included in __function_starts
338 movq section$start$__TEXT$__text@GOTPCREL
(%rip
), %rax
339 movq section$end$__TEXT$__text@GOTPCREL
(%rip
), %rbx
341 # __TEXT/__cstring are interesting because they're not ConcatInputSections.
342 movq section$start$__TEXT$__cstring@GOTPCREL
(%rip
), %rax
343 movq section$end$__TEXT$__cstring@GOTPCREL
(%rip
), %rbx
345 # Vanilla __DATA/__data
346 movq section$start$__DATA$__data@GOTPCREL
(%rip
), %rax
347 movq section$end$__DATA$__data@GOTPCREL
(%rip
), %rbx
350 movq section$start$__MYBSS$__mybss@GOTPCREL
(%rip
), %rax
351 movq section$end$__MYBSS$__mybss@GOTPCREL
(%rip
), %rbx
353 # Referring to a non-existent section wills it into existence.
354 # This is needed for e.g. __DATA/__llvm_orderfile in libclang_rt.profile.
355 # This means `-u` can be used as a janky `-sectcreate`.
356 movq section$start$__DATA$__llvm_orderfile@GOTPCREL
(%rip
), %rax
357 movq section$end$__DATA$__llvm_orderfile@GOTPCREL
(%rip
), %rbx
359 # Section-rename tests.
360 movq section$start$__FOO$__bar@GOTPCREL
(%rip
), %rax
361 movq section$end$__FOO$__bar@GOTPCREL
(%rip
), %rbx
363 movq section$start$__BAZ$__quux@GOTPCREL
(%rip
), %rax
364 movq section$end$__BAZ$__quux@GOTPCREL
(%rip
), %rbx
366 movq section$start$__WHAT$__ever@GOTPCREL
(%rip
), %rax
367 movq section$end$__WHAT$__ever@GOTPCREL
(%rip
), %rbx
369 # If there are actual symbols with the magic names, the magic
370 # names lose their magic and just refer to those symbols (and
371 # no section is implicitly created for them).
372 movq section$start$ACTUAL$symbol@GOTPCREL
(%rip
), %rax
373 movq section$end$ACTUAL$symbol@GOTPCREL
(%rip
), %rbx
375 # -U section$start is not exported as dynamic_lookup, it just
376 # creates a section like -u.
377 movq section$start$__DYNAMIC$__lookup@GOTPCREL
(%rip
), %rax
378 movq section$end$__DYNAMIC$__lookup@GOTPCREL
(%rip
), %rbx
386 .section __TEXT,__aftertext
393 .section __TEXT,__aftercstring
399 .subsections_via_symbols
403 .globl section$start$ACTUAL$symbol
404 section$start$ACTUAL$symbol
:
407 .globl section$end$ACTUAL$symbol
408 section$end$ACTUAL$symbol
:
411 .subsections_via_symbols
414 ## Renamed to __BAZ,__bar by -rename_segment
418 ## Renamed to __FOO,__ever by -rename_segment
419 .section __WHAT,__ever
423 .globl segment$start$REGULAR
424 segment$start$REGULAR
:
427 .globl segment$end$REGULAR
433 movq segment$start$__TEXT@GOTPCREL
(%rip
), %rax
434 movq segment$end$__TEXT@GOTPCREL
(%rip
), %rbx
436 movq segment$start$__FOO@GOTPCREL
(%rip
), %rax
437 movq segment$end$__FOO@GOTPCREL
(%rip
), %rbx
439 movq segment$start$__BAZ@GOTPCREL
(%rip
), %rax
440 movq segment$end$__BAZ@GOTPCREL
(%rip
), %rbx
442 # (These two lines make ld64 crash linking the .o file.
443 # The rest of the test works with ld64 too.)
444 movq segment$start$__WHAT@GOTPCREL
(%rip
), %rax
445 movq segment$end$__WHAT@GOTPCREL
(%rip
), %rbx
447 # References to non-existing segments create that segment,
448 # without any sections in it.
449 movq segment$start$ASDF@GOTPCREL
(%rip
), %rax
450 movq segment$end$ASDF@GOTPCREL
(%rip
), %rbx
452 # Non-undefined symbols don't create segments.
453 callq segment$start$REGULAR
454 callq segment$end$REGULAR
458 .section __TEXT,__fill
461 .subsections_via_symbols