41 changes: 35 additions & 6 deletions lld/test/MachO/section-merge.s
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# REQUIRES: x86
# RUN: mkdir -p %t
# RUN: rm -rf %t; split-file %s %t
## Verify that we preserve alignment when merging sections.
# RUN: echo ".globl _foo; .data; .p2align 0; _foo: .byte 0xca" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/foo.o
# RUN: echo ".globl _bar; .data; .p2align 2; _bar: .byte 0xfe" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/bar.o
# RUN: echo ".globl _baz; .data; .p2align 3; _baz: .byte 0xba" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/baz.o
# RUN: echo ".globl _qux; .data; .p2align 0; _qux: .quad 0xdeadbeef" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/qux.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/foo.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/bar.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/baz.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/qux.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/main.o
# RUN: %lld -o %t/output %t/foo.o %t/bar.o %t/baz.o %t/qux.o %t/main.o

Expand All @@ -18,8 +18,37 @@
# CHECK: Contents of section __DATA,__data:
# CHECK-NEXT: {{0*}}[[#ADDR]] ca000000 fe000000 baefbead de000000

#--- foo.s
.globl _foo
.data
.p2align 0
_foo:
.byte 0xca

#--- bar.s
.globl _bar
.data
.p2align 2
_bar:
.byte 0xfe

#--- baz.s
.globl _baz
.data
.p2align 3
_baz:
.byte 0xba

#--- qux.s
.globl _qux
.data
.p2align 0
_qux:
.quad 0xdeadbeef

#--- main.s
.section __TEXT,__text
.global _main
.globl _main

_main:
mov $0, %rax
Expand Down
3 changes: 1 addition & 2 deletions lld/test/MachO/stabs.s
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# REQUIRES: x86, shell
# UNSUPPORTED: system-windows
# RUN: split-file %s %t
# RUN: rm -rf %t; split-file %s %t
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/no-debug.s -o %t/no-debug.o
## Set modtimes of the files for deterministic test output.
# RUN: env TZ=UTC touch -t "197001010000.16" %t/test.o
# RUN: env TZ=UTC touch -t "197001010000.32" %t/foo.o
# RUN: rm -f %t/foo.a
# RUN: llvm-ar rcsU %t/foo.a %t/foo.o

# RUN: %lld -lSystem %t/test.o %t/foo.o %t/no-debug.o -o %t/test
Expand Down
12 changes: 7 additions & 5 deletions lld/test/MachO/subsections-section-relocs.s
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# 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: rm -rf %t; split-file %s %t
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o

# RUN: %lld -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
Expand All @@ -20,6 +17,11 @@
# STRINGS: Private symbol
# STRINGS: foo

#--- order-file
_bar_str
_foo_str

#--- test.s
.text
.globl _main, _foo_str, _bar_str

Expand Down
37 changes: 20 additions & 17 deletions lld/test/MachO/subsections-symbol-relocs.s
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
# 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: rm -rf %t; split-file %s %t
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o

# RUN: %lld -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
Expand All @@ -34,6 +19,24 @@
# CHECK-NEXT: movq $0, %rax
# CHECK-NEXT: retq

#--- order-file-1
_bar
_foo
_main
## _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.
_qux

#--- order-file-2
## _bar and _baz point to the same address, so both order files should achieve
## the same result.
_baz
_foo
_main
_qux

#--- test.s
.text
.globl _main, _foo, _bar, _qux
.alt_entry _qux
Expand Down
42 changes: 33 additions & 9 deletions lld/test/MachO/symbol-order.s
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# REQUIRES: x86
# RUN: mkdir -p %t
# RUN: echo ".global f, g; .section __TEXT,test_g; g: callq f" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/g.o
# RUN: echo ".global f; .section __TEXT,test_f1; f: ret" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/f1.o
# RUN: echo ".global f; .section __TEXT,test_f2; f: ret" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/f2.o
# RUN: echo ".global f, g; .section __TEXT,test_fg; f: ret; g: callq f" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/fg.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
# RUN: rm -rf %t; split-file %s %t
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/g.s -o %t/g.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/f1.s -o %t/f1.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/f2.s -o %t/f2.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/fg.s -o %t/fg.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
# RUN: %lld -dylib -o %t/libf1.dylib %t/f1.o -lSystem

# RUN: rm -f %t/libf2_g.a
# RUN: llvm-ar rcs %t/libf2_g.a %t/f2.o %t/g.o

# RUN: rm -f %t/libfg.a
# RUN: llvm-ar rcs %t/libfg.a %t/fg.o

# RUN: %lld %t/libf1.dylib %t/libf2_g.a %t/test.o -o %t/test.out -lSystem
Expand Down Expand Up @@ -39,6 +36,33 @@
# ARCHIVE-PRIORITY-NEXT: segment section address dylib symbol
# ARCHIVE-PRIORITY-EMPTY:

#--- g.s
.global f, g
.section __TEXT,test_g
g:
callq f

#--- f1.s
.global f
.section __TEXT,test_f1
f:
ret

#--- f2.s
.global f
.section __TEXT,test_f2
f:
ret

#--- fg.s
.global f, g
.section __TEXT,test_fg
f:
ret
g:
callq f

#--- test.s
.global g
.global _main
_main:
Expand Down
22 changes: 16 additions & 6 deletions lld/test/MachO/weak-definition-direct-fetch.s
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# REQUIRES: x86
# RUN: mkdir -p %t
# RUN: rm -rf %t; split-file %s %t

## This test exercises the various possible combinations of weak and non-weak
## symbols that get referenced directly by a relocation in an object file.

# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
# RUN: echo ".globl _foo; .section __TEXT,nonweak; _foo:" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/foo.o
# RUN: echo ".globl _foo; .weak_definition _foo; .section __TEXT,weak; _foo:" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/weakfoo.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/weakfoo.s -o %t/weakfoo.o

# RUN: %lld -dylib -install_name \
# RUN: @executable_path/libfoo.dylib %t/foo.o -o %t/libfoo.dylib
Expand All @@ -24,9 +24,7 @@
# RUN: llvm-nm %t/libweakfoo.dylib 2>&1 | FileCheck %s --check-prefix=NOSYM
# NOSYM: no symbols

# RUN: rm -f %t/foo.a
# RUN: llvm-ar --format=darwin rcs %t/foo.a %t/foo.o
# RUN: rm -f %t/weakfoo.a
# RUN: llvm-ar --format=darwin rcs %t/weakfoo.a %t/weakfoo.o

## End of input file setup. The following lines check which symbol "wins" when
Expand Down Expand Up @@ -84,6 +82,18 @@
# RUN: %lld -lSystem -o %t/nonweak-ar-weak-obj -L%t %t/foo.a %t/weakfoo.o %t/test.o
# RUN: llvm-objdump --macho --lazy-bind --syms %t/nonweak-ar-weak-obj | FileCheck %s --check-prefix=PREFER-WEAK-OBJECT

#--- foo.s
.globl _foo
.section __TEXT,nonweak
_foo:

#--- weakfoo.s
.globl _foo
.weak_definition _foo
.section __TEXT,weak
_foo:

#--- test.s
.globl _main
_main:
callq _foo
Expand Down
24 changes: 18 additions & 6 deletions lld/test/MachO/weak-definition-indirect-fetch.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# REQUIRES: x86
# RUN: mkdir -p %t
# RUN: rm -rf %t; split-file %s %t

## This tests examines the effect of .weak_definition on symbols in an archive
## that are not referenced directly, but which are still loaded due to some
Expand All @@ -9,13 +9,11 @@
## will be fetched when linking against the main test file due to its references
## to _bar and _baz.

# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
# RUN: echo ".globl _foo, _bar; .section __TEXT,nonweak; _bar: _foo:" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/foo.o
# RUN: echo ".globl _foo, _baz; .weak_definition _foo; .section __TEXT,weak; _baz: _foo:" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/weakfoo.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/weakfoo.s -o %t/weakfoo.o

# RUN: rm -f %t/foo.a
# RUN: llvm-ar --format=darwin rcs %t/foo.a %t/foo.o
# RUN: rm -f %t/weakfoo.a
# RUN: llvm-ar --format=darwin rcs %t/weakfoo.a %t/weakfoo.o

# PREFER-NONWEAK-OBJECT: O __TEXT,nonweak _foo
Expand All @@ -35,6 +33,20 @@
# RUN: %lld -lSystem -o %t/nonweak-ar-weak-obj -L%t %t/foo.a %t/weakfoo.o %t/test.o
# RUN: llvm-objdump --syms %t/nonweak-ar-weak-obj | FileCheck %s --check-prefix=PREFER-NONWEAK-OBJECT

#--- foo.s
.globl _foo, _bar
.section __TEXT,nonweak
_bar:
_foo:

#--- weakfoo.s
.globl _foo, _baz
.weak_definition _foo
.section __TEXT,weak
_baz:
_foo:

#--- test.s
.globl _main
_main:
callq _bar
Expand Down
21 changes: 17 additions & 4 deletions lld/test/MachO/weak-definition-order.s
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# REQUIRES: x86
# RUN: mkdir -p %t
# RUN: rm -rf %t; split-file %s %t

## This test demonstrates that when we have two weak symbols of the same type,
## we pick the one whose containing file appears earlier in the command-line
## invocation.

# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
# RUN: echo ".globl _foo; .weak_definition _foo; .section __TEXT,weak1; _foo:" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/weak1.o
# RUN: echo ".globl _foo; .weak_definition _foo; .section __TEXT,weak2; _foo:" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/weak2.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/weak1.s -o %t/weak1.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/weak2.s -o %t/weak2.o

# RUN: %lld -lSystem -o %t/obj12 -L%t %t/weak1.o %t/weak2.o %t/test.o
# RUN: llvm-objdump --syms %t/obj12 | FileCheck %s --check-prefix=WEAK1
Expand All @@ -29,6 +29,19 @@
# DYLIB1: __DATA __la_symbol_ptr 0x{{[0-9a-f]*}} pointer 0 libweak1 _foo
# DYLIB2: __DATA __la_symbol_ptr 0x{{[0-9a-f]*}} pointer 0 libweak2 _foo

#--- weak1.s
.globl _foo
.weak_definition _foo
.section __TEXT,weak1;
_foo:

#--- weak2.s
.globl _foo
.weak_definition _foo
.section __TEXT,weak2
_foo:

#--- test.s
.globl _main
_main:
callq _foo
Expand Down
35 changes: 25 additions & 10 deletions lld/test/MachO/weak-definition-over-dysym.s
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
# REQUIRES: x86
# RUN: mkdir -p %t
# RUN: rm -rf %t; split-file %s %t

## This test demonstrates that when an archive file is fetched, its symbols
## always override any conflicting dylib symbols, regardless of any weak
## definition flags.

# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
# RUN: echo ".globl _foo; _foo:" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/libfoo.o
# RUN: %lld -dylib -install_name \
# RUN: @executable_path/libfoo.dylib %t/libfoo.o -o %t/libfoo.dylib
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/libfoo.s -o %t/libfoo.o
# RUN: %lld -dylib -install_name @executable_path/libfoo.dylib %t/libfoo.o -o %t/libfoo.dylib

# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
# RUN: echo ".globl _foo, _bar; .section __TEXT,nonweak; _bar: _foo:" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/foo.o
# RUN: echo ".globl _foo, _bar; .weak_definition _foo; .section __TEXT,weak; _bar: _foo:" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/weakfoo.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/weakfoo.s -o %t/weakfoo.o

# RUN: rm -f %t/foo.a
# RUN: llvm-ar --format=darwin rcs %t/foo.a %t/foo.o
# RUN: rm -f %t/weakfoo.a
# RUN: llvm-ar --format=darwin rcs %t/weakfoo.a %t/weakfoo.o

# PREFER-WEAK-OBJECT: O __TEXT,weak _foo
Expand All @@ -32,6 +29,24 @@
# RUN: %lld -lSystem -o %t/nonweak-dylib-weak-obj -L%t -lfoo %t/weakfoo.o %t/test.o
# RUN: llvm-objdump --macho --lazy-bind --syms %t/nonweak-dylib-weak-obj | FileCheck %s --check-prefix=PREFER-WEAK-OBJECT

#--- libfoo.s
.globl _foo
_foo:

#--- foo.s
.globl _foo, _bar
.section __TEXT,nonweak
_bar:
_foo:

#--- weakfoo.s
.globl _foo, _bar
.weak_definition _foo
.section __TEXT,weak
_bar:
_foo:

#--- test.s
.globl _main
_main:
callq _foo
Expand Down