xref: /llvm-project/llvm/test/tools/llvm-objcopy/MachO/symbol-table.test (revision c00fc180ecdac4b13caddd1ba506ffa3f6e7e206)
1## This test is based on a trimmed down version of the binary built as follows:
2## a.c:
3## __attribute__((used)) static int PrivateSymbol;
4## __attribute__((visibility("hidden"))) int PrivateExternalSymbol;
5## __attribute__((used)) int CommonSymbol;
6## extern int UndefinedExternalSymbol;
7## // Defined external symbol
8## int main() {
9##   return PrivateSymbol + PrivateExternalSymbol + CommonSymbol +
10##          UndefinedExternalSymbol;
11## }
12## build command:
13##   clang -g -fno-exceptions -fno-unwind-tables -undefined dynamic_lookup \
14##   a.c -o a.exe
15## All the load commands except the symbol table and its transitive dependencies
16## have been removed.
17
18# RUN: yaml2obj %s -o %t
19# RUN: llvm-objcopy %t %t.copy
20# RUN: cmp %t %t.copy
21
22## Verify that the binary is valid and check its symbol table.
23# RUN: llvm-readobj --symbols %t.copy | FileCheck %s
24
25# CHECK: Symbols [
26# CHECK-NEXT:   Symbol {
27# CHECK-NEXT:     Name: _PrivateSymbol (170)
28# CHECK-NEXT:     Type: Section (0xE)
29# CHECK-NEXT:     Section: __bss (0x4)
30# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
31# CHECK-NEXT:     Flags [ (0x0)
32# CHECK-NEXT:     ]
33# CHECK-NEXT:     Value: 0x100001008
34# CHECK-NEXT:   }
35# CHECK-NEXT:   Symbol {
36# CHECK-NEXT:     Name: _PrivateExternalSymbol (122)
37# CHECK-NEXT:     PrivateExtern
38# CHECK-NEXT:     Type: Section (0xE)
39# CHECK-NEXT:     Section: __common (0x5)
40# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
41# CHECK-NEXT:     Flags [ (0x0)
42# CHECK-NEXT:     ]
43# CHECK-NEXT:     Value: 0x100001010
44# CHECK-NEXT:   }
45# CHECK-NEXT:   Symbol {
46# CHECK-NEXT:     Name: /Users/aaaaaaaa/ (192)
47# CHECK-NEXT:     Type: SymDebugTable (0x64)
48# CHECK-NEXT:     Section:  (0x0)
49# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
50# CHECK-NEXT:     Flags [ (0x0)
51# CHECK-NEXT:     ]
52# CHECK-NEXT:     Value: 0x0
53# CHECK-NEXT:   }
54# CHECK-NEXT:   Symbol {
55# CHECK-NEXT:     Name: main.c (185)
56# CHECK-NEXT:     Type: SymDebugTable (0x64)
57# CHECK-NEXT:     Section:  (0x0)
58# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
59# CHECK-NEXT:     Flags [ (0x0)
60# CHECK-NEXT:     ]
61# CHECK-NEXT:     Value: 0x0
62# CHECK-NEXT:   }
63# CHECK-NEXT:   Symbol {
64# CHECK-NEXT:     Name: /var/folders/1d/zyfdpp7j2995h5hqspjy28bc0000gn/T/main-c5ac21.o (39)
65# CHECK-NEXT:     Type: SymDebugTable (0x66)
66# CHECK-NEXT:     Section:  (0x3)
67# CHECK-NEXT:     RefType: ReferenceFlagUndefinedLazy (0x1)
68# CHECK-NEXT:     Flags [ (0x0)
69# CHECK-NEXT:     ]
70# CHECK-NEXT:     Value: 0x5EA74C81
71# CHECK-NEXT:   }
72# CHECK-NEXT:   Symbol {
73# CHECK-NEXT:     Name:  (208)
74# CHECK-NEXT:     Type: SymDebugTable (0x2E)
75# CHECK-NEXT:     Section:  (0x1)
76# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
77# CHECK-NEXT:     Flags [ (0x0)
78# CHECK-NEXT:     ]
79# CHECK-NEXT:     Value: 0x100000F80
80# CHECK-NEXT:   }
81# CHECK-NEXT:   Symbol {
82# CHECK-NEXT:     Name: _main (102)
83# CHECK-NEXT:     Type: SymDebugTable (0x24)
84# CHECK-NEXT:     Section:  (0x1)
85# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
86# CHECK-NEXT:     Flags [ (0x0)
87# CHECK-NEXT:     ]
88# CHECK-NEXT:     Value: 0x100000F80
89# CHECK-NEXT:   }
90# CHECK-NEXT:   Symbol {
91# CHECK-NEXT:     Name:  (208)
92# CHECK-NEXT:     Type: SymDebugTable (0x24)
93# CHECK-NEXT:     Section:  (0x0)
94# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
95# CHECK-NEXT:     Flags [ (0x0)
96# CHECK-NEXT:     ]
97# CHECK-NEXT:     Value: 0x2D
98# CHECK-NEXT:   }
99# CHECK-NEXT:   Symbol {
100# CHECK-NEXT:     Name:  (208)
101# CHECK-NEXT:     Type: SymDebugTable (0x4E)
102# CHECK-NEXT:     Section:  (0x1)
103# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
104# CHECK-NEXT:     Flags [ (0x0)
105# CHECK-NEXT:     ]
106# CHECK-NEXT:     Value: 0x2D
107# CHECK-NEXT:   }
108# CHECK-NEXT:   Symbol {
109# CHECK-NEXT:     Name: _PrivateSymbol (170)
110# CHECK-NEXT:     Type: SymDebugTable (0x26)
111# CHECK-NEXT:     Section:  (0x4)
112# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
113# CHECK-NEXT:     Flags [ (0x0)
114# CHECK-NEXT:     ]
115# CHECK-NEXT:     Value: 0x100001008
116# CHECK-NEXT:   }
117# CHECK-NEXT:   Symbol {
118# CHECK-NEXT:     Name: _CommonSymbol (108)
119# CHECK-NEXT:     Type: SymDebugTable (0x20)
120# CHECK-NEXT:     Section:  (0x0)
121# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
122# CHECK-NEXT:     Flags [ (0x0)
123# CHECK-NEXT:     ]
124# CHECK-NEXT:     Value: 0x0
125# CHECK-NEXT:   }
126# CHECK-NEXT:   Symbol {
127# CHECK-NEXT:     Name: _PrivateExternalSymbol (122)
128# CHECK-NEXT:     Type: SymDebugTable (0x20)
129# CHECK-NEXT:     Section:  (0x0)
130# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
131# CHECK-NEXT:     Flags [ (0x0)
132# CHECK-NEXT:     ]
133# CHECK-NEXT:     Value: 0x0
134# CHECK-NEXT:   }
135# CHECK-NEXT:   Symbol {
136# CHECK-NEXT:     Name:  (208)
137# CHECK-NEXT:     Type: SymDebugTable (0x64)
138# CHECK-NEXT:     Section:  (0x1)
139# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
140# CHECK-NEXT:     Flags [ (0x0)
141# CHECK-NEXT:     ]
142# CHECK-NEXT:     Value: 0x0
143# CHECK-NEXT:   }
144# CHECK-NEXT:   Symbol {
145# CHECK-NEXT:     Name: _CommonSymbol (108)
146# CHECK-NEXT:     Extern
147# CHECK-NEXT:     Type: Section (0xE)
148# CHECK-NEXT:     Section: __common (0x5)
149# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
150# CHECK-NEXT:     Flags [ (0x0)
151# CHECK-NEXT:     ]
152# CHECK-NEXT:     Value: 0x10000100C
153# CHECK-NEXT:   }
154# CHECK-NEXT:   Symbol {
155# CHECK-NEXT:     Name: __mh_execute_header (19)
156# CHECK-NEXT:     Extern
157# CHECK-NEXT:     Type: Section (0xE)
158# CHECK-NEXT:     Section: __text (0x1)
159# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
160# CHECK-NEXT:     Flags [ (0x10)
161# CHECK-NEXT:       ReferencedDynamically (0x10)
162# CHECK-NEXT:     ]
163# CHECK-NEXT:     Value: 0x100000000
164# CHECK-NEXT:   }
165# CHECK-NEXT:   Symbol {
166# CHECK-NEXT:     Name: _main (102)
167# CHECK-NEXT:     Extern
168# CHECK-NEXT:     Type: Section (0xE)
169# CHECK-NEXT:     Section: __text (0x1)
170# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
171# CHECK-NEXT:     Flags [ (0x0)
172# CHECK-NEXT:     ]
173# CHECK-NEXT:     Value: 0x100000F80
174# CHECK-NEXT:   }
175# CHECK-NEXT:   Symbol {
176# CHECK-NEXT:     Name: _UndefinedExternalSymbol (145)
177# CHECK-NEXT:     Extern
178# CHECK-NEXT:     Type: Undef (0x0)
179# CHECK-NEXT:     Section:  (0x0)
180# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
181# CHECK-NEXT:     Flags [ (0xFE00)
182# CHECK-NEXT:       AltEntry (0x200)
183# CHECK-NEXT:       ColdFunc (0x400)
184# CHECK-NEXT:     ]
185# CHECK-NEXT:     Value: 0x0
186# CHECK-NEXT:   }
187# CHECK-NEXT:   Symbol {
188# CHECK-NEXT:     Name: dyld_stub_binder (2)
189# CHECK-NEXT:     Extern
190# CHECK-NEXT:     Type: Undef (0x0)
191# CHECK-NEXT:     Section:  (0x0)
192# CHECK-NEXT:     RefType: UndefinedNonLazy (0x0)
193# CHECK-NEXT:     Flags [ (0x100)
194# CHECK-NEXT:       SymbolResolver (0x100)
195# CHECK-NEXT:     ]
196# CHECK-NEXT:     Value: 0x0
197# CHECK-NEXT:   }
198# CHECK-NEXT: ]
199
200--- !mach-o
201FileHeader:
202  magic:           0xFEEDFACF
203  cputype:         0x01000007
204  cpusubtype:      0x80000003
205  filetype:        0x00000002
206  ncmds:           5
207  sizeofcmds:      720
208  flags:           0x00200085
209  reserved:        0x00000000
210LoadCommands:
211  - cmd:             LC_SEGMENT_64
212    cmdsize:         232
213    segname:         __TEXT
214    vmaddr:          4294967296
215    vmsize:          4096
216    fileoff:         0
217    filesize:        4096
218    maxprot:         5
219    initprot:        5
220    nsects:          2
221    flags:           0
222    Sections:
223      - sectname:        __text
224        segname:         __TEXT
225        addr:            0x0000000100000F80
226        size:            45
227        offset:          0x00000F80
228        align:           4
229        reloff:          0x00000000
230        nreloc:          0
231        flags:           0x80000400
232        reserved1:       0x00000000
233        reserved2:       0x00000000
234        reserved3:       0x00000000
235        content:         554889E5488B0575000000488D0D7A000000C745FC000000008B156900000003156B0000000311031089D05DC3
236      - sectname:        __unwind_info
237        segname:         __TEXT
238        addr:            0x0000000100000FB0
239        size:            72
240        offset:          0x00000FB0
241        align:           2
242        reloff:          0x00000000
243        nreloc:          0
244        flags:           0x00000000
245        reserved1:       0x00000000
246        reserved2:       0x00000000
247        reserved3:       0x00000000
248        content:         010000001C000000000000001C000000000000001C00000002000000800F00003400000034000000AE0F00000000000034000000030000000C000100100001000000000000000000
249  - cmd:             LC_SEGMENT_64
250    cmdsize:         312
251    segname:         __DATA
252    vmaddr:          4294971392
253    vmsize:          4096
254    fileoff:         4096
255    filesize:        4096
256    maxprot:         3
257    initprot:        3
258    nsects:          3
259    flags:           0
260    Sections:
261      - sectname:        __got
262        segname:         __DATA
263        addr:            0x0000000100001000
264        size:            8
265        offset:          0x00001000
266        align:           3
267        reloff:          0x00000000
268        nreloc:          0
269        flags:           0x00000006
270        reserved1:       0x00000000
271        reserved2:       0x00000000
272        reserved3:       0x00000000
273        content:         '0000000000000000'
274      - sectname:        __bss
275        segname:         __DATA
276        addr:            0x0000000100001008
277        size:            4
278        offset:          0x00000000
279        align:           2
280        reloff:          0x00000000
281        nreloc:          0
282        flags:           0x00000001
283        reserved1:       0x00000000
284        reserved2:       0x00000000
285        reserved3:       0x00000000
286      - sectname:        __common
287        segname:         __DATA
288        addr:            0x000000010000100C
289        size:            8
290        offset:          0x00000000
291        align:           2
292        reloff:          0x00000000
293        nreloc:          0
294        flags:           0x00000001
295        reserved1:       0x00000000
296        reserved2:       0x00000000
297        reserved3:       0x00000000
298  - cmd:             LC_SEGMENT_64
299    cmdsize:         72
300    segname:         __LINKEDIT
301    vmaddr:          4294975488
302    vmsize:          4096
303    fileoff:         8192
304    filesize:        508
305    maxprot:         1
306    initprot:        1
307    nsects:          0
308    flags:           0
309  - cmd:             LC_SYMTAB
310    cmdsize:         24
311    symoff:          8192
312    nsyms:           18
313    stroff:          8484
314    strsize:         216
315  - cmd:             LC_DYSYMTAB
316    cmdsize:         80
317    ilocalsym:       0
318    nlocalsym:       13
319    iextdefsym:      13
320    nextdefsym:      3
321    iundefsym:       16
322    nundefsym:       2
323    tocoff:          0
324    ntoc:            0
325    modtaboff:       0
326    nmodtab:         0
327    extrefsymoff:    0
328    nextrefsyms:     0
329    indirectsymoff:  8480
330    nindirectsyms:   1
331    extreloff:       0
332    nextrel:         0
333    locreloff:       0
334    nlocrel:         0
335LinkEditData:
336  NameList:
337    - n_strx:          170
338      n_type:          0x0E
339      n_sect:          4
340      n_desc:          0
341      n_value:         4294971400
342    - n_strx:          122
343      n_type:          0x1E
344      n_sect:          5
345      n_desc:          0
346      n_value:         4294971408
347    - n_strx:          192
348      n_type:          0x64
349      n_sect:          0
350      n_desc:          0
351      n_value:         0
352    - n_strx:          185
353      n_type:          0x64
354      n_sect:          0
355      n_desc:          0
356      n_value:         0
357    - n_strx:          39
358      n_type:          0x66
359      n_sect:          3
360      n_desc:          1
361      n_value:         1588022401
362    - n_strx:          208
363      n_type:          0x2E
364      n_sect:          1
365      n_desc:          0
366      n_value:         4294971264
367    - n_strx:          102
368      n_type:          0x24
369      n_sect:          1
370      n_desc:          0
371      n_value:         4294971264
372    - n_strx:          208
373      n_type:          0x24
374      n_sect:          0
375      n_desc:          0
376      n_value:         45
377    - n_strx:          208
378      n_type:          0x4E
379      n_sect:          1
380      n_desc:          0
381      n_value:         45
382    - n_strx:          170
383      n_type:          0x26
384      n_sect:          4
385      n_desc:          0
386      n_value:         4294971400
387    - n_strx:          108
388      n_type:          0x20
389      n_sect:          0
390      n_desc:          0
391      n_value:         0
392    - n_strx:          122
393      n_type:          0x20
394      n_sect:          0
395      n_desc:          0
396      n_value:         0
397    - n_strx:          208
398      n_type:          0x64
399      n_sect:          1
400      n_desc:          0
401      n_value:         0
402    - n_strx:          108
403      n_type:          0x0F
404      n_sect:          5
405      n_desc:          0
406      n_value:         4294971404
407    - n_strx:          19
408      n_type:          0x0F
409      n_sect:          1
410      n_desc:          16
411      n_value:         4294967296
412    - n_strx:          102
413      n_type:          0x0F
414      n_sect:          1
415      n_desc:          0
416      n_value:         4294971264
417    - n_strx:          145
418      n_type:          0x01
419      n_sect:          0
420      n_desc:          65024
421      n_value:         0
422    - n_strx:          2
423      n_type:          0x01
424      n_sect:          0
425      n_desc:          256
426      n_value:         0
427  StringTable:
428    - ' '
429    - dyld_stub_binder
430    - __mh_execute_header
431    - '/var/folders/1d/zyfdpp7j2995h5hqspjy28bc0000gn/T/main-c5ac21.o'
432    - _main
433    - _CommonSymbol
434    - _PrivateExternalSymbol
435    - _UndefinedExternalSymbol
436    - _PrivateSymbol
437    - main.c
438    - '/Users/aaaaaaaa/'
439    - ''
440    - ''
441    - ''
442    - ''
443    - ''
444    - ''
445    - ''
446...
447