xref: /llvm-project/lld/test/MachO/headerpad.s (revision 0634a8440d63359e8f0e6674c192524bb404909f)
1# REQUIRES: x86
2
3## NOTE: Here we check that the first non-header section -- __text -- appears
4## *exactly* `-headerpad` bytes from the end of the header. ld64 actually
5## starts laying out the non-header sections in the __TEXT segment from the end
6## of the (page-aligned) segment rather than the front, so its binaries
7## typically have more than `-headerpad` bytes of actual padding. `-headerpad`
8## just enforces a lower bound. We should consider implementing the same
9## alignment behavior.
10
11# RUN: rm -rf %t; mkdir -p %t
12
13################ Check default behavior
14# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
15# RUN: %lld -o %t/test %t/test.o
16# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s --check-prefix=PADx
17#
18# PADx:      magic        {{.+}}  ncmds  sizeofcmds         flags
19# PADx-NEXT: MH_MAGIC_64  {{.+}}  [[#]]  [[#%u, CMDSIZE:]]  {{.*}}
20# PADx:      sectname __text
21# PADx-NEXT: segname __TEXT
22# PADx-NEXT: addr
23# PADx-NEXT: size
24# PADx-NEXT: offset [[#%u, CMDSIZE + 0x20 + 0x20]]
25
26################ Zero pad, no LCDylibs
27# RUN: %no-lsystem-lld -o %t/test %t/test.o -headerpad 0
28# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s --check-prefix=PAD0
29# RUN: %no-lsystem-lld -o %t/test %t/test.o -headerpad 0 -headerpad_max_install_names
30# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s --check-prefix=PAD0
31#
32# PAD0:      magic        {{.+}}  ncmds  sizeofcmds         flags
33# PAD0-NEXT: MH_MAGIC_64  {{.+}}  [[#]]  [[#%u, CMDSIZE:]]  {{.*}}
34# PAD0:      sectname __text
35# PAD0-NEXT: segname __TEXT
36# PAD0-NEXT: addr
37# PAD0-NEXT: size
38# PAD0-NEXT: offset [[#%u, CMDSIZE + 0x20 + 0]]
39
40################ Each lexical form of a hex number, no LCDylibs
41# RUN: %no-lsystem-lld -o %t/test %t/test.o -headerpad 11
42# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s --check-prefix=PAD11
43# RUN: %no-lsystem-lld -o %t/test %t/test.o -headerpad 0x11
44# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s --check-prefix=PAD11
45# RUN: %no-lsystem-lld -o %t/test %t/test.o -headerpad 0X11 -headerpad_max_install_names
46# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s --check-prefix=PAD11
47#
48# PAD11:      magic        {{.+}}  ncmds  sizeofcmds         flags
49# PAD11-NEXT: MH_MAGIC_64  {{.+}}  [[#]]  [[#%u, CMDSIZE:]]  {{.*}}
50# PAD11:      sectname __text
51# PAD11-NEXT: segname __TEXT
52# PAD11-NEXT: addr
53# PAD11-NEXT: size
54# PAD11-NEXT: offset [[#%u, CMDSIZE + 0x20 + 0x11]]
55
56################ Each & all 3 kinds of LCDylib
57# RUN: echo "" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/null.o
58# RUN: %lld -o %t/libnull.dylib %t/null.o -dylib \
59# RUN:     -headerpad_max_install_names
60# RUN: llvm-objdump --macho --all-headers %t/libnull.dylib | FileCheck %s --check-prefix=PADMAX
61# RUN: %lld -o %t/libnull.dylib %t/null.o -dylib \
62# RUN:     -headerpad_max_install_names -lSystem
63# RUN: llvm-objdump --macho --all-headers %t/libnull.dylib | FileCheck %s --check-prefix=PADMAX
64# RUN: %lld -o %t/libnull.dylib %t/null.o -dylib \
65# RUN:     -headerpad_max_install_names \
66# RUN:     -lSystem -sub_library libSystem
67# RUN: llvm-objdump --macho --all-headers %t/libnull.dylib | FileCheck %s --check-prefix=PADMAX
68#
69# PADMAX:      magic        {{.+}}  ncmds        sizeofcmds         flags
70# PADMAX-NEXT: MH_MAGIC_64  {{.+}}  [[#%u, N:]]  [[#%u, CMDSIZE:]]  {{.*}}
71# PADMAX:      sectname __text
72# PADMAX-NEXT: segname __TEXT
73# PADMAX-NEXT: addr
74# PADMAX-NEXT: size
75# PADMAX-NEXT: offset [[#%u, CMDSIZE + 0x20 + mul(0x400, N - 9)]]
76
77################ All 3 kinds of LCDylib swamped by a larger override
78# RUN: %lld -o %t/libnull.dylib %t/null.o -dylib \
79# RUN:     -headerpad_max_install_names -headerpad 0x1001 \
80# RUN:     -lSystem -sub_library libSystem
81# RUN: llvm-objdump --macho --all-headers %t/libnull.dylib | FileCheck %s --check-prefix=PADOVR
82#
83# PADOVR:      magic        {{.+}}  ncmds        sizeofcmds         flags
84# PADOVR-NEXT: MH_MAGIC_64  {{.+}}  [[#%u, N:]]  [[#%u, CMDSIZE:]]  {{.*}}
85# PADOVR:      sectname __text
86# PADOVR-NEXT: segname __TEXT
87# PADOVR-NEXT: addr
88# PADOVR-NEXT: size
89# PADOVR-NEXT: offset [[#%u, CMDSIZE + 0x20 + 0x1001]]
90
91.globl _main
92_main:
93  ret
94