47 changes: 47 additions & 0 deletions lld/test/MachO/subsections-section-relocs.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# REQUIRES: x86
# RUN: mkdir -p %t
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o

# RUN: echo "_bar_str" > %t/order-file
# RUN: echo "_foo_str" >> %t/order-file

# RUN: lld -flavor darwinnew -o %t/test %t/test.o -order_file %t/order-file
# RUN: llvm-objdump --section-headers -d --no-show-raw-insn %t/test | FileCheck %s
# CHECK-LABEL: Sections:
# CHECK: __cstring {{[^ ]*}} {{0*}}[[#%x, CSTRING_ADDR:]]
# CHECK-LABEL: Disassembly of section __TEXT,__text:
## L._str should end up at CSTRING_ADDR + 4, and leaq is 7 bytes long so we
## have RIP = ADDR + 7
# CHECK: [[#%x, ADDR:]]: leaq
# CHECK-SAME: [[#%u, CSTRING_ADDR + 4 - ADDR - 7]](%rip), %rsi {{.*}} <_bar_str+0x4>

# RUN: llvm-readobj --string-dump=__cstring %t/test | FileCheck %s --check-prefix=STRINGS
# STRINGS: bar
# STRINGS: Private symbol
# STRINGS: foo

.text
.globl _main, _foo_str, _bar_str

_main:
leaq L_.str(%rip), %rsi
mov $0, %rax
ret

.section __TEXT,__cstring
_foo_str:
.asciz "foo"

_bar_str:
.asciz "bar"

## References to this generate a section relocation
## N.B.: ld64 doesn't actually reorder symbols in __cstring based on the order
## file. Only our implementation does. However, I'm not sure how else to
## test section relocations that target an address inside a relocated
## symbol: using a non-__cstring section would cause llvm-mc to emit a
## symbol relocation instead using the nearest symbol.
L_.str:
.asciz "Private symbol"

.subsections_via_symbols
55 changes: 55 additions & 0 deletions lld/test/MachO/subsections-symbol-relocs.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# REQUIRES: x86
# RUN: mkdir -p %t
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o

# RUN: echo "_bar" > %t/order-file-1
# RUN: echo "_foo" >> %t/order-file-1
# RUN: echo "_main" >> %t/order-file-1
## _qux is marked as .alt_entry, so it should not create a new subsection and
## its contents should move with _bar to the start of the output despite the
## order file listing it at the end.
# RUN: echo "_qux" >> %t/order-file-1

## _bar and _baz point to the same address, so both order files should achieve
## the same result.
# RUN: echo "_baz" > %t/order-file-2
# RUN: echo "_foo" >> %t/order-file-2
# RUN: echo "_main" >> %t/order-file-2
# RUN: echo "_qux" >> %t/order-file-2

# RUN: lld -flavor darwinnew -o %t/test-1 %t/test.o -order_file %t/order-file-1
# RUN: llvm-objdump -d --no-show-raw-insn %t/test-1 | FileCheck %s
# RUN: lld -flavor darwinnew -o %t/test-2 %t/test.o -order_file %t/order-file-2
# RUN: llvm-objdump -d --no-show-raw-insn %t/test-2 | FileCheck %s
# CHECK-LABEL: Disassembly of section __TEXT,__text:
# CHECK: <_bar>:
# CHECK-NEXT: callq {{.*}} <_foo>
# CHECK-EMPTY:
# CHECK-NEXT: <_qux>:
# CHECK-NEXT: retq
# CHECK: <_foo>:
# CHECK-NEXT: retq
# CHECK: <_main>:
# CHECK-NEXT: callq {{.*}} <_bar>
# CHECK-NEXT: movq $0, %rax
# CHECK-NEXT: retq

.text
.globl _main, _foo, _bar, _qux
.alt_entry _qux

_foo:
retq

_main:
callq _bar
movq $0, %rax
retq

_bar:
_baz:
callq _foo
_qux:
retq

.subsections_via_symbols