444 lines · plain
1# REQUIRES: x862 3# RUN: rm -rf %t; split-file %s %t4 5# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/main.s -o %t/main.o6# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o7# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/seg.s -o %t/seg.o8 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.txt17# RUN: llvm-objdump --no-print-imm-hex --macho -d --no-symbolic-operands --no-show-raw-insn %t.out >> %t-dump.txt18# RUN: llvm-objdump --macho --function-starts %t.out >> %t-dump.txt19# RUN: FileCheck %s < %t-dump.txt20 21## Setting the entry point to the start of the __text section should22## 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 %s25# MAINENTRY: [[#%x, MAINADDR:]] g F __TEXT,__text _main26# MAINENTRY: LC_MAIN27# MAINENTRY-NEXT: cmdsize28# MAINENTRY-NEXT: entryoff [[#%d, MAINADDR - 0x100000000]]29 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.txt41# RUN: llvm-objdump --no-print-imm-hex --macho -d --no-symbolic-operands --no-show-raw-insn %t.ordered.out >> %t-ordered-dump.txt42# RUN: llvm-objdump --macho --function-starts %t.out >> %t-ordered-dump.txt43# RUN: FileCheck %s < %t-ordered-dump.txt44 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.txt54# RUN: llvm-objdump --no-print-imm-hex --macho -d --no-symbolic-operands --no-show-raw-insn %t.dl.out >> %t-dump.dl.txt55# RUN: llvm-objdump --macho --function-starts %t.out >> %t-dump.dl.txt56# RUN: FileCheck %s < %t-dump.dl.txt57 58## ...except that the entry point is now _otherfun instead of _main since59## _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 %s62# OTHERENTRY: [[#%x, OTHERADDR:]] g F __TEXT,__text _otherfun63# OTHERENTRY: LC_MAIN64# OTHERENTRY-NEXT: cmdsize65# OTHERENTRY-NEXT: entryoff [[#%d, OTHERADDR - 0x100000000]]66 67 68## Test that the link succeeds with dead-stripping enabled too.69# RUN: %lld -dead_strip -lSystem %t/main.o -o %t/stripped.out70# RUN: llvm-objdump --macho --syms --section-headers %t/stripped.out > %t-stripped-dump.txt71# RUN: llvm-objdump --no-print-imm-hex --macho -d --no-symbolic-operands --no-show-raw-insn %t/stripped.out >> %t-stripped-dump.txt72# RUN: FileCheck --check-prefix=STRIP %s < %t-stripped-dump.txt73 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.out76# RUN: llvm-objdump --section-headers %t/stripped1.out | FileCheck --check-prefix=STRIP2 %s77 78## (Fun fact: `-e 'section$start$__TEXT$__text -dead_strip` strips79## everything in the text section because markLive runs well before80## section$start symbols are replaced, so the entry point is just81## an undefined symbol that keeps nothing alive, and then later it82## sets the entry point to the start of the now-empty text section83## and the output program crashes when running. This matches ld64's84## behavior.)85 86# STRIP-LABEL: Sections:87# STRIP-NEXT: Idx Name Size VMA Type88# STRIP-NEXT: 0 __text {{[0-9a-f]*}} [[#%x, TEXTSTART:]] TEXT89# STRIP-NEXT: 1 __cstring 00000000 [[#%x, CSTRINGSTART:]] DATA90# STRIP-NEXT: 2 __data 0000000091# STRIP-NEXT: 3 __mybss 0000000092# STRIP-NEXT: 4 __bar 0000000093# STRIP-NEXT: 5 __ever 0000000094# STRIP-NEXT: 6 __lookup 0000000095# STRIP-NEXT: 7 symbol 0000000096# STRIP-NEXT: 8 __quux 0000000097 98# STRIP-LABEL: SYMBOL TABLE:99# STRIP-NOT: section$start$__FOO$__bar100 101# STRIP-LABEL: _main:102# STRIP: [[#%x, PC1:]]:103# STRIP-SAME: leaq [[#%d, TEXTSTART - PC1 - 7]](%rip), %rax104# STRIP-NEXT: [[#%x, PC2:]]:105# STRIP-SAME: leaq [[#%d, CSTRINGSTART - PC2 - 7]](%rip), %rbx106 107# STRIP2-LABEL: Sections:108# STRIP2-NEXT: Idx Name Size VMA Type109# STRIP2-NEXT: 0 __text {{[0-9a-f]*}} [[#%x, TEXTSTART:]] TEXT110# STRIP2-NEXT: 1 __cstring 00000000 [[#%x, CSTRINGSTART:]] DATA111# STRIP2-NEXT: 2 __data 00000000112# STRIP2-NEXT: 3 __mybss 00000000113# STRIP2-NEXT: 4 __bar 00000000114# STRIP2-NEXT: 5 __notexist 00000000115# STRIP2-NEXT: 6 __ever 00000000116# STRIP2-NEXT: 7 __lookup 00000000117# STRIP2-NEXT: 8 symbol 00000000118# STRIP2-NEXT: 9 __quux 00000000119 120# CHECK-LABEL: Sections:121# CHECK-NEXT: Idx Name Size VMA Type122# CHECK: 0 __text {{[0-9a-f]*}} [[#%x, TEXTSTART:]] TEXT123# CHECK: 1 __aftertext {{[0-9a-f]*}} [[#%x, TEXTEND:]]124# CHECK: 2 __cstring {{[0-9a-f]*}} [[#%x, CSTRINGSTART:]] DATA125# CHECK: 3 __aftercstring {{[0-9a-f]*}} [[#%x, CSTRINGEND:]]126# CHECK: 4 __data 00000008 [[#%x, DATASTART:]] DATA127# CHECK: 5 __mybss 00008000 [[#%x, MYBSSSTART:]] BSS128# CHECK: 6 __quux 0000002a [[#%x, QUUXSTART:]]129# CHECK: 7 __bar 00000059 [[#%x, BARSTART:]]130# CHECK: 8 __uflag_sect 00000000131# CHECK: 9 __lookup 00000000132# CHECK-NOT: symbol133# CHECK-NOT: __unref134 135# CHECK-LABEL: SYMBOL TABLE:136# CHECK-NOT: section$start$__TEXT$__text137# CHECK-NOT: section$end$__TEXT$__text138# CHECK-NOT: section$start$__TEXT$__cstring139# CHECK-NOT: section$end$__TEXT$__cstring140# CHECK-NOT: section$start$__DATA$__data141# CHECK-NOT: section$end$__DATA$__data142# CHECK-NOT: section$start$__DYNAMIC$__lookup143# CHECK-NOT: section$start$__DYNAMIC$__unref144# CHECK: section$end$ACTUAL$symbol145# CHECK: section$start$ACTUAL$symbol146 147# CHECK-LABEL: _main:148 149## The CHECK-SAMEs work around FileCheck's150## "error: numeric variable 'PC2' defined earlier in the same CHECK directive"151## limitation.152## The 7s are the length of a leaq instruction.153## section$start$__TEXT$__text / section$end$__TEXT$__text154 155# CHECK: [[#%x, PC1:]]:156# CHECK-SAME: leaq [[#%d, TEXTSTART - PC1 - 7]](%rip), %rax157# CHECK-NEXT: [[#%x, PC2:]]:158# CHECK-SAME: leaq [[#%d, TEXTEND - PC2 - 7]](%rip), %rbx159 160## section$start$__TEXT$__cstring / section$end$__TEXT$__cstring161# CHECK: [[#%x, PC3:]]:162# CHECK-SAME: leaq [[#%d, CSTRINGSTART - PC3 - 7]](%rip), %rax163# CHECK-NEXT: [[#%x, PC4:]]:164# CHECK-SAME: leaq [[#%d, CSTRINGEND - PC4 - 7]](%rip), %rbx165 166## section$start$__DATA$__data / section$end$__DATA$__data167# CHECK: [[#%x, PC5:]]:168# CHECK-SAME: leaq [[#%d, DATASTART - PC5 - 7]](%rip), %rax169# CHECK-NEXT: [[#%x, PC6:]]:170# CHECK-SAME: leaq [[#%d, DATASTART + 8 - PC6 - 7]](%rip), %rbx171 172## section$start$__MYBSS$__mybss / section$end$__MYBSS$__mybss173# CHECK: [[#%x, PC7:]]:174# CHECK-SAME: leaq [[#%d, MYBSSSTART - PC7 - 7]](%rip), %rax175# CHECK-NEXT: [[#%x, PC8:]]:176# CHECK-SAME: leaq [[#%d, MYBSSSTART + 0x8000 - PC8 - 7]](%rip), %rbx177 178## Section-rename tests.179## Input section __FOO/__bar is renamed to output section180## __BAZ/__quux by a -rename_section flag.181## section$start$__FOO$__bar ends up referring to the __BAZ/__quux section.182# CHECK: [[#%x, PC11:]]:183# CHECK-SAME: leaq [[#%d, QUUXSTART - PC11 - 7]](%rip), %rax184# CHECK-NEXT: [[#%x, PC12:]]:185# CHECK-SAME: leaq [[#%d, QUUXSTART + 42 - PC12 - 7]](%rip), %rbx186## section$start$__BAZ$__quux also refers to the __BAZ/__quux section.187# CHECK: [[#%x, PC13:]]:188# CHECK-SAME: leaq [[#%d, QUUXSTART - PC13 - 7]](%rip), %rax189# CHECK-NEXT: [[#%x, PC14:]]:190# CHECK-SAME: leaq [[#%d, QUUXSTART + 42 - PC14 - 7]](%rip), %rbx191## Input section __WHAT/__ever is renamed to output section192## __FOO/__bar by a -rename_section flag.193## section$start$__WHAT$__ever ends up referring to the __FOO/__bar section.194# CHECK: [[#%x, PC15:]]:195# CHECK-SAME: leaq [[#%d, BARSTART - PC15 - 7]](%rip), %rax196# CHECK-NEXT: [[#%x, PC16:]]:197# CHECK-SAME: leaq [[#%d, BARSTART + 89 - PC16 - 7]](%rip), %rbx198 199## The function_starts section should not have an entry for the200## section$end$__TEXT$__text symbol.201# CHECK: [[#%.16x, TEXTSTART]]202# CHECK-NOT: [[#%.16x, TEXTEND]]203 204###############################################################################205## Test segment$start and segment$end.206 207# RUN: %lld -lSystem %t/seg.o -o %t.seg.out \208# RUN: -rename_segment __FOO __BAZ \209# RUN: -rename_segment __WHAT __FOO \210# RUN: -u 'segment$start$__UFLAG_SEG' \211# RUN: -e 'segment$start$__TEXT'212# RUN: llvm-objdump --macho --syms %t.seg.out > %t-seg-dump.txt213## llvm-objdump can't dump segment names; use lld-otool for this.214# RUN: llvm-otool -l %t.seg.out | grep -A6 LC_SEGMENT >> %t-seg-dump.txt215# RUN: llvm-objdump --no-print-imm-hex --macho -d --no-symbolic-operands --no-show-raw-insn %t.seg.out >> %t-seg-dump.txt216# RUN: llvm-objdump --macho --function-starts %t.out >> %t-seg-dump.txt217# RUN: FileCheck %s --check-prefix=SEG < %t-seg-dump.txt218 219# SEG-LABEL: SYMBOL TABLE:220# SEG-NOT: segment$start$__TEXT221# SEG-NOT: segment$end$__TEXT222# SEG-NOT: segment$start$__FOO223# SEG-NOT: segment$end$__FOO224# SEG-NOT: segment$start$__BAZ225# SEG-NOT: segment$end$__BAZ226# SEG-NOT: segment$start$__WHAT227# SEG-NOT: segment$end$__WHAT228# SEG-NOT: segment$start$__UFLAG_SEG229# SEG-NOT: segment$start$__UFLAG_SEG230# SEG-DAG: segment$end$REGULAR231# SEG-DAG: segment$start$REGULAR232 233# SEG: cmd LC_SEGMENT_64234# SEG-NEXT: cmdsize235# SEG-NEXT: segname __PAGEZERO236 237# SEG: cmd LC_SEGMENT_64238# SEG-NEXT: cmdsize239# SEG-NEXT: segname __TEXT240# SEG-NEXT: vmaddr 0x[[#%x, TEXTSTART:]]241# SEG-NEXT: vmsize 0x[[#%x, TEXTSIZE:]]242 243# SEG: cmd LC_SEGMENT_64244# SEG-NEXT: cmdsize245# SEG-NEXT: segname __BAZ246# SEG-NEXT: vmaddr 0x[[#%x, BAZSTART:]]247# SEG-NEXT: vmsize 0x[[#%x, BAZSIZE:]]248 249# SEG: cmd LC_SEGMENT_64250# SEG-NEXT: cmdsize251# SEG-NEXT: segname __FOO252# SEG-NEXT: vmaddr 0x[[#%x, FOOSTART:]]253# SEG-NEXT: vmsize 0x[[#%x, FOOSIZE:]]254 255# SEG: cmd LC_SEGMENT_64256# SEG-NEXT: cmdsize257# SEG-NEXT: segname __UFLAG_SEG258# SEG-NEXT: vmaddr 0x[[#%x, UFLAGSTART:]]259# SEG-NEXT: vmsize 0x0000000000000000260 261# SEG: cmd LC_SEGMENT_64262# SEG-NEXT: cmdsize263# SEG-NEXT: segname ASDF264# SEG-NEXT: vmaddr 0x[[#%x, ASDFSTART:]]265# SEG-NEXT: vmsize 0x0000000000000000266 267# SEG: _main:268 269## segment$start$__TEXT / segment$end$__TEXT270# SEG: [[#%x, PC1:]]:271# SEG-SAME: leaq [[#%d, TEXTSTART - PC1 - 7]](%rip), %rax272# SEG-NEXT: [[#%x, PC2:]]:273# SEG-SAME: leaq [[#%d, TEXTSTART + TEXTSIZE - PC2 - 7]](%rip), %rbx274 275## segment$start$__FOO / segment$end$__FOO, which is renamed to __BAZ276# SEG: [[#%x, PC3:]]:277# SEG-SAME: leaq [[#%d, BAZSTART - PC3 - 7]](%rip), %rax278# SEG-NEXT: [[#%x, PC4:]]:279# SEG-SAME: leaq [[#%d, BAZSTART + BAZSIZE - PC4 - 7]](%rip), %rbx280 281## segment$start$__BAZ / segment$end$__BAZ282# SEG: [[#%x, PC5:]]:283# SEG-SAME: leaq [[#%d, BAZSTART - PC5 - 7]](%rip), %rax284# SEG-NEXT: [[#%x, PC6:]]:285# SEG-SAME: leaq [[#%d, BAZSTART + BAZSIZE - PC6 - 7]](%rip), %rbx286 287## segment$start$__WHAT / segment$end$__WHAT, which is renamed to __FOO288# SEG: [[#%x, PC7:]]:289# SEG-SAME: leaq [[#%d, FOOSTART - PC7 - 7]](%rip), %rax290# SEG-NEXT: [[#%x, PC8:]]:291# SEG-SAME: leaq [[#%d, FOOSTART + FOOSIZE - PC8 - 7]](%rip), %rbx292 293## segment$start$ASDF / segment$end$ASDF294# SEG: [[#%x, PC9:]]:295# SEG-SAME: leaq [[#%d, ASDFSTART - PC9 - 7]](%rip), %rax296# SEG-NEXT: [[#%x, PC10:]]:297# SEG-SAME: leaq [[#%d, ASDFSTART - PC10 - 7]](%rip), %rbx298 299#--- order.txt300_otherfun301_main302section$end$__TEXT$__text303section$start$__TEXT$__text304 305#--- main.s306.zerofill __MYBSS,__mybss,_zero_foo,0x8000307 308.globl section$start$ACTUAL$symbol309.globl section$end$ACTUAL$symbol310 311## Renamed to __BAZ,__quux by -rename_section312.section __FOO,__bar313.space 42314 315## Renamed to __FOO,__bar by -rename_section316.section __WHAT,__ever317.space 89318 319.text320.globl _main321_main:322 # Basics: start/end of existing, normal sections.323 324 # For __TEXT/__text, these magic symbols shouldn't be325 # included in __function_starts326 movq section$start$__TEXT$__text@GOTPCREL(%rip), %rax327 movq section$end$__TEXT$__text@GOTPCREL(%rip), %rbx328 329 # __TEXT/__cstring are interesting because they're not ConcatInputSections.330 movq section$start$__TEXT$__cstring@GOTPCREL(%rip), %rax331 movq section$end$__TEXT$__cstring@GOTPCREL(%rip), %rbx332 333 # Vanilla __DATA/__data334 movq section$start$__DATA$__data@GOTPCREL(%rip), %rax335 movq section$end$__DATA$__data@GOTPCREL(%rip), %rbx336 337 # Vanilla zerofill.338 movq section$start$__MYBSS$__mybss@GOTPCREL(%rip), %rax339 movq section$end$__MYBSS$__mybss@GOTPCREL(%rip), %rbx340 341 # Section-rename tests.342 movq section$start$__FOO$__bar@GOTPCREL(%rip), %rax343 movq section$end$__FOO$__bar@GOTPCREL(%rip), %rbx344 345 movq section$start$__BAZ$__quux@GOTPCREL(%rip), %rax346 movq section$end$__BAZ$__quux@GOTPCREL(%rip), %rbx347 348 movq section$start$__WHAT$__ever@GOTPCREL(%rip), %rax349 movq section$end$__WHAT$__ever@GOTPCREL(%rip), %rbx350 351 # If there are actual symbols with the magic names, the magic352 # names lose their magic and just refer to those symbols (and353 # no section is implicitly created for them).354 movq section$start$ACTUAL$symbol@GOTPCREL(%rip), %rax355 movq section$end$ACTUAL$symbol@GOTPCREL(%rip), %rbx356 357 # -U section$start is not exported as dynamic_lookup, it just358 # creates a section like -u.359 movq section$start$__DYNAMIC$__lookup@GOTPCREL(%rip), %rax360 movq section$end$__DYNAMIC$__lookup@GOTPCREL(%rip), %rbx361 362 ret363 364.globl _otherfun365_otherfun:366 ret367 368.section __TEXT,__aftertext369.fill 1370 371.cstring372.asciz "foo"373.asciz "barbaz"374 375.section __TEXT,__aftercstring376.fill 1377 378.data379.quad 0x1234380 381.subsections_via_symbols382 383#--- foo.s384.text385.globl section$start$ACTUAL$symbol386section$start$ACTUAL$symbol:387.fill 1388 389.globl section$end$ACTUAL$symbol390section$end$ACTUAL$symbol:391.fill 1392 393.subsections_via_symbols394 395#--- seg.s396## Renamed to __BAZ,__bar by -rename_segment397.section __FOO,__bar398.space 1399 400## Renamed to __FOO,__ever by -rename_segment401.section __WHAT,__ever402.space 1403 404.text405.globl segment$start$REGULAR406segment$start$REGULAR:407 retq408 409.globl segment$end$REGULAR410segment$end$REGULAR:411 retq412 413.globl _main414_main:415 movq segment$start$__TEXT@GOTPCREL(%rip), %rax416 movq segment$end$__TEXT@GOTPCREL(%rip), %rbx417 418 movq segment$start$__FOO@GOTPCREL(%rip), %rax419 movq segment$end$__FOO@GOTPCREL(%rip), %rbx420 421 movq segment$start$__BAZ@GOTPCREL(%rip), %rax422 movq segment$end$__BAZ@GOTPCREL(%rip), %rbx423 424 # (These two lines make ld64 crash linking the .o file.425 # The rest of the test works with ld64 too.)426 movq segment$start$__WHAT@GOTPCREL(%rip), %rax427 movq segment$end$__WHAT@GOTPCREL(%rip), %rbx428 429 # References to non-existing segments create that segment,430 # without any sections in it.431 movq segment$start$ASDF@GOTPCREL(%rip), %rax432 movq segment$end$ASDF@GOTPCREL(%rip), %rbx433 434 # Non-undefined symbols don't create segments.435 callq segment$start$REGULAR436 callq segment$end$REGULAR437 438 ret439 440.section __TEXT,__fill441.space 578442 443.subsections_via_symbols444