xref: /llvm-project/llvm/test/Object/archive-symtab.test (revision a6316d6da5938328840d4161e4dc7c671808c20f)
1# RUN: yaml2obj --docnum=1 %s -o %t.elf-x86-64
2# RUN: yaml2obj --docnum=2 %s -o %t2.elf-x86-64
3
4--- !ELF
5FileHeader:
6  Class:   ELFCLASS64
7  Data:    ELFDATA2LSB
8  Type:    ET_REL
9  Machine: EM_X86_64
10Sections:
11  - Name:  .text
12    Type:  SHT_PROGBITS
13    Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
14Symbols:
15  - Name:    main
16    Type:    STT_FUNC
17    Section: .text
18    Binding: STB_GLOBAL
19  - Name:    SomeOtherFunction
20    Binding: STB_GLOBAL
21  - Name:    puts
22    Binding: STB_GLOBAL
23
24--- !ELF
25FileHeader:
26  Class:   ELFCLASS64
27  Data:    ELFDATA2LSB
28  Type:    ET_REL
29  Machine: EM_X86_64
30Sections:
31  - Name:  .text
32    Type:  SHT_PROGBITS
33    Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
34Symbols:
35  - Name:    bar
36    Type:    STT_FUNC
37    Section: .text
38  - Name:    foo
39    Type:    STT_FUNC
40    Section: .text
41    Binding: STB_GLOBAL
42    Value:   0x0000000000000006
43  - Name:    main
44    Type:    STT_FUNC
45    Section: .text
46    Binding: STB_GLOBAL
47    Value:   0x0000000000000016
48
49# RUN: rm -f %t.a
50# RUN: llvm-ar rcsU %t.a %t.elf-x86-64 %t2.elf-x86-64
51# RUN: llvm-nm --print-armap %t.a | FileCheck %s
52
53# RUN: rm -f %t.a
54# RUN: env SYM64_THRESHOLD=0 llvm-ar crTs %t.a %t.elf-x86-64 %t2.elf-x86-64
55# RUN: llvm-nm --print-armap  %t.a | FileCheck %s
56# RUN: grep '/SYM64/' %t.a
57
58# RUN: rm -f %t.a
59# RUN: env SYM64_THRESHOLD=836 llvm-ar rcsU %t.a %t.elf-x86-64 %t2.elf-x86-64
60# RUN: llvm-nm --print-armap %t.a | FileCheck %s
61# RUN: grep '/SYM64/' %t.a
62
63# RUN: rm -f %t.a
64# RUN: env SYM64_THRESHOLD=837 llvm-ar rcsU %t.a %t.elf-x86-64 %t2.elf-x86-64
65# RUN: llvm-nm --print-armap %t.a | FileCheck %s
66# RUN: not grep '/SYM64/' %t.a
67
68# RUN: rm -f %t.a
69# RUN: llvm-ar rcsU --format=bigarchive %t.a %t.elf-x86-64 %t2.elf-x86-64
70# RUN: llvm-nm --print-armap %t.a | FileCheck %s
71
72# CHECK: Archive map
73# CHECK-NEXT: main in {{.*}}.elf-x86-64
74# CHECK-NEXT: foo in {{.*}}2.elf-x86-64
75# CHECK-NEXT: main in {{.*}}2.elf-x86-64
76# CHECK-NOT: bar
77
78
79# RUN: rm -f %t.a
80# RUN: llvm-ar --format=gnu rcT %t.a %t.elf-x86-64 %t2.elf-x86-64
81# RUN: llvm-nm --print-armap %t.a | FileCheck -DFILE=%/t.elf-x86-64 -DFILE2=%/t2.elf-x86-64 --check-prefix=THIN %s
82
83# THIN: Archive map
84# THIN-NEXT: main in [[FILE]]
85# THIN-NEXT: foo in [[FILE2]]
86# THIN-NEXT: main in [[FILE2]]
87
88
89# CHECK: {{.*}}.elf-x86-64:
90# CHECK-NEXT:                  U SomeOtherFunction
91# CHECK-NEXT: 0000000000000000 T main
92# CHECK-NEXT:                  U puts
93
94# CHECK: {{.*}}2.elf-x86-64:
95# CHECK-NEXT: 0000000000000000 t bar
96# CHECK-NEXT: 0000000000000006 T foo
97# CHECK-NEXT: 0000000000000016 T main
98
99# RUN: rm -f %t.a
100# RUN: llvm-ar rcSU %t.a %t.elf-x86-64 %t2.elf-x86-64
101# RUN: llvm-nm --print-armap %t.a | FileCheck %s --check-prefix=NOMAP
102
103# NOMAP-NOT: Archive map
104
105# RUN: llvm-ar s %t.a
106# RUN: llvm-nm --print-armap %t.a | FileCheck %s
107
108## Check that the archive does have a corrupt symbol table.
109# RUN: rm -f %t.a
110# RUN: cp %p/Inputs/archive-test.a-corrupt-symbol-table %t.a
111# RUN: llvm-nm --print-armap %t.a | FileCheck %s --check-prefix=CORRUPT
112
113# CORRUPT: Archive map
114# CORRUPT-NEXT: mbin in trivial-object-test.elf-x86-64
115# CORRUPT-NEXT: foo in trivial-object-test2.elf-x86-64
116# CORRUPT-NEXT: main in trivial-object-test2.elf-x86-64
117
118# CORRUPT: {{.*}}.elf-x86-64:
119# CORRUPT-NEXT:                  U SomeOtherFunction
120# CORRUPT-NEXT: 0000000000000000 T main
121# CORRUPT-NEXT:                  U puts
122
123# CORRUPT: {{.*}}2.elf-x86-64:
124# CORRUPT-NEXT: 0000000000000000 t bar
125# CORRUPT-NEXT: 0000000000000006 T foo
126# CORRUPT-NEXT: 0000000000000016 T main
127
128## Check that the we *don't* update the symbol table.
129# RUN: llvm-ar s %t.a
130# RUN: llvm-nm --print-armap %t.a | FileCheck %s --check-prefix=CORRUPT
131
132## Repeat the test with llvm-ranlib.
133
134# RUN: rm -f %t.a
135# RUN: llvm-ar rcSU %t.a %t.elf-x86-64 %t2.elf-x86-64
136# RUN: llvm-nm --print-armap %t.a | FileCheck %s --check-prefix=NOMAP
137
138# RUN: llvm-ranlib %t.a
139# RUN: llvm-nm --print-armap %t.a | FileCheck %s
140
141# RUN: llvm-nm --print-armap %p/Inputs/macho-archive-x86_64.a | FileCheck %s --check-prefix=BSD-MachO
142# RUN: llvm-nm --print-armap %p/Inputs/macho-archive-unsorted-x86_64.a | FileCheck %s --check-prefix=BSD-MachO
143
144# BSD-MachO: Archive map
145# BSD-MachO: _bar in bar.o
146# BSD-MachO: _foo in foo.o
147
148# RUN: rm -f %t.a
149# RUN: llvm-ar --format=bsd rcsU %t.a %p/Inputs/trivial-object-test.macho-x86-64 %p/Inputs/trivial-object-test2.macho-x86-64
150# RUN: llvm-nm --print-armap %t.a | FileCheck --check-prefix=MACHO %s
151
152# RUN: rm -f %t.a
153# RUN: env SYM64_THRESHOLD=784 llvm-ar --format=darwin rcsU %t.a %p/Inputs/trivial-object-test.macho-x86-64 %p/Inputs/trivial-object-test2.macho-x86-64
154# RUN: llvm-nm --print-armap %t.a | FileCheck --check-prefix=MACHO %s
155# RUN: grep '__\.SYMDEF_64' %t.a
156
157# RUN: rm -f %t.a
158# RUN: env SYM64_THRESHOLD=785 llvm-ar --format=darwin rcsU %t.a %p/Inputs/trivial-object-test.macho-x86-64 %p/Inputs/trivial-object-test2.macho-x86-64
159# RUN: llvm-nm --print-armap %t.a | FileCheck --check-prefix=MACHO %s
160# RUN: not grep '__\.SYMDEF_64' %t.a
161
162# MACHO: Archive map
163# MACHO-NEXT: _main in trivial-object-test.macho-x86-64
164# MACHO-NEXT: _foo in trivial-object-test2.macho-x86-64
165# MACHO-NEXT: _main in trivial-object-test2.macho-x86-64
166# MACHO-NOT: {{^}}bar
167
168# MACHO: trivial-object-test.macho-x86-64
169# MACHO-NEXT: 0000000000000028 s L_.str
170# MACHO-NEXT:                  U _SomeOtherFunction
171# MACHO-NEXT: 0000000000000000 T _main
172# MACHO-NEXT:                  U _puts
173
174# MACHO: trivial-object-test2.macho-x86-64
175# MACHO-NEXT: 0000000000000000 t _bar
176# MACHO-NEXT: 0000000000000001 T _foo
177# MACHO-NEXT: 0000000000000002 T _main
178
179# RUN: llvm-nm --print-armap %p/Inputs/macho-toc64-archive-x86_64.a | FileCheck --check-prefix=MACHO-TOC64 %s
180
181# MACHO-TOC64: Archive map
182# MACHO-TOC64-NEXT: _test in test.o
183# MACHO-TOC64-NEXT: _test in xtest.o
184
185# RUN: rm -f %t.a
186# RUN: llvm-ar --format=gnu rcsU %t.a %p/Inputs/coff-short-import-code %p/Inputs/coff-short-import-data
187# RUN: llvm-nm --print-armap %t.a | FileCheck --check-prefix=COFF-SHORT-IMPORT %s
188
189# COFF-SHORT-IMPORT:      Archive map
190# COFF-SHORT-IMPORT-NEXT: __imp__foo in coff-short-import-code
191# COFF-SHORT-IMPORT-NEXT: _foo in coff-short-import-code
192# COFF-SHORT-IMPORT-NEXT: __imp__bar in coff-short-import-data
193# COFF-SHORT-IMPORT-NOT:  _bar in coff-short-import-data
194
195## Test that we pad the symbol table so that it ends in a multiple of 8 bytes:
196## 8 + 60 + 36 == 104
197# RUN: rm -f %t.a
198# RUN: llvm-ar --format=bsd rcsU %t.a %p/Inputs/trivial-object-test.macho-x86-64
199# RUN: FileCheck --check-prefix=MACHO-SYMTAB-ALIGN %s < %t.a
200# MACHO-SYMTAB-ALIGN: !<arch>
201# MACHO-SYMTAB-ALIGN-NEXT: #1/12           {{..........}}  0     0     0       36        `
202
203## Test that we pad the symbol table so that it ends in a multiple of 8 bytes:
204## 8 + 60 + 52 == 120
205# RUN: rm -f %t.a
206# RUN: llvm-ar --format=bsd rcsD %t.a %p/Inputs/trivial-object-test.macho-x86-64 %p/Inputs/trivial-object-test.macho-x86-64
207# RUN: FileCheck --check-prefix=MACHO-SYMTAB-ALIGN2 %s < %t.a
208# MACHO-SYMTAB-ALIGN2: !<arch>
209# MACHO-SYMTAB-ALIGN2-NEXT: #1/12           0           0     0     0       52        `
210
211## With a gnu symbol table we only align to two
212# 8 + 60 + 14 = 82
213# RUN: rm -f %t.a
214# RUN: llvm-ar --format=gnu rcsD %t.a %p/Inputs/trivial-object-test.macho-x86-64
215# RUN: FileCheck --check-prefix=GNU-SYMTAB-ALIGN %s < %t.a
216# GNU-SYMTAB-ALIGN: !<arch>
217# GNU-SYMTAB-ALIGN-NEXT: /               0           0     0     0       14        `
218
219
220## ** Test the behavior of an empty archive:
221
222## No symbol table emitted for GNU archives
223# RUN: rm -f %t.a
224# RUN: llvm-ar rcs --format=gnu %t.a
225# RUN: not grep -q '/               ' %t.a
226
227## No symbol table for BSD archives
228# RUN: rm -f %t.a
229# RUN: llvm-ar rcs --format=bsd %t.a
230# RUN: not grep -q '__\.SYMDEF' %t.a
231
232## And we do emit a symbol table for DARWIN archives
233# RUN: rm -f %t.a
234# RUN: llvm-ar rcs --format=darwin %t.a
235# RUN: grep -q '__\.SYMDEF' %t.a
236