1da816ca0SGreg Clayton //===-- SymbolTest.cpp ----------------------------------------------------===//
2da816ca0SGreg Clayton //
3da816ca0SGreg Clayton // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4da816ca0SGreg Clayton // See https://llvm.org/LICENSE.txt for license information.
5da816ca0SGreg Clayton // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6da816ca0SGreg Clayton //
7da816ca0SGreg Clayton //===----------------------------------------------------------------------===//
8da816ca0SGreg Clayton
9da816ca0SGreg Clayton #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
10da816ca0SGreg Clayton #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
11da816ca0SGreg Clayton #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
12da816ca0SGreg Clayton #include "TestingSupport/SubsystemRAII.h"
13da816ca0SGreg Clayton #include "TestingSupport/TestUtilities.h"
14da816ca0SGreg Clayton
15da816ca0SGreg Clayton #include "lldb/Core/DataFileCache.h"
16da816ca0SGreg Clayton #include "lldb/Core/Module.h"
17da816ca0SGreg Clayton #include "lldb/Host/FileSystem.h"
18da816ca0SGreg Clayton #include "lldb/Host/HostInfo.h"
19da816ca0SGreg Clayton #include "lldb/Symbol/Symbol.h"
20da816ca0SGreg Clayton #include "lldb/Symbol/Symtab.h"
21da816ca0SGreg Clayton #include "lldb/Utility/DataEncoder.h"
22da816ca0SGreg Clayton #include "lldb/Utility/DataExtractor.h"
23da816ca0SGreg Clayton
24da816ca0SGreg Clayton #include <memory>
25da816ca0SGreg Clayton
26da816ca0SGreg Clayton #include "gtest/gtest.h"
27da816ca0SGreg Clayton
28da816ca0SGreg Clayton using namespace lldb;
29da816ca0SGreg Clayton using namespace lldb_private;
30*a669a237Swalter erquinigo using namespace lldb_private::plugin::dwarf;
31da816ca0SGreg Clayton
32da816ca0SGreg Clayton class SymtabTest : public testing::Test {
33da816ca0SGreg Clayton SubsystemRAII<FileSystem, HostInfo, ObjectFileMachO, SymbolFileDWARF,
34da816ca0SGreg Clayton TypeSystemClang>
35da816ca0SGreg Clayton subsystem;
36da816ca0SGreg Clayton };
37da816ca0SGreg Clayton
EncodeDecode(const Symtab & object,ByteOrder byte_order)38da816ca0SGreg Clayton static void EncodeDecode(const Symtab &object, ByteOrder byte_order) {
39da816ca0SGreg Clayton const uint8_t addr_size = 8;
40da816ca0SGreg Clayton DataEncoder file(byte_order, addr_size);
41da816ca0SGreg Clayton
42da816ca0SGreg Clayton object.Encode(file);
43da816ca0SGreg Clayton llvm::ArrayRef<uint8_t> bytes = file.GetData();
44da816ca0SGreg Clayton DataExtractor data(bytes.data(), bytes.size(), byte_order, addr_size);
45da816ca0SGreg Clayton Symtab decoded_object(object.GetObjectFile());
46da816ca0SGreg Clayton offset_t data_offset = 0;
47da816ca0SGreg Clayton bool uuid_mismatch = false;
48da816ca0SGreg Clayton decoded_object.Decode(data, &data_offset, uuid_mismatch);
49da816ca0SGreg Clayton ASSERT_EQ(object.GetNumSymbols(), decoded_object.GetNumSymbols());
50da816ca0SGreg Clayton for (size_t i = 0; i < object.GetNumSymbols(); ++i)
51da816ca0SGreg Clayton EXPECT_EQ(*object.SymbolAtIndex(i), *decoded_object.SymbolAtIndex(i));
52da816ca0SGreg Clayton }
53da816ca0SGreg Clayton
EncodeDecode(const Symtab & object)54da816ca0SGreg Clayton static void EncodeDecode(const Symtab &object) {
55da816ca0SGreg Clayton EncodeDecode(object, eByteOrderLittle);
56da816ca0SGreg Clayton EncodeDecode(object, eByteOrderBig);
57da816ca0SGreg Clayton }
58da816ca0SGreg Clayton
TEST_F(SymtabTest,EncodeDecodeSymtab)59da816ca0SGreg Clayton TEST_F(SymtabTest, EncodeDecodeSymtab) {
60da816ca0SGreg Clayton
61da816ca0SGreg Clayton auto ExpectedFile = TestFile::fromYaml(R"(
62da816ca0SGreg Clayton --- !mach-o
63da816ca0SGreg Clayton FileHeader:
64da816ca0SGreg Clayton magic: 0xFEEDFACF
65da816ca0SGreg Clayton cputype: 0x100000C
66da816ca0SGreg Clayton cpusubtype: 0x0
67da816ca0SGreg Clayton filetype: 0x2
68da816ca0SGreg Clayton ncmds: 17
69da816ca0SGreg Clayton sizeofcmds: 792
70da816ca0SGreg Clayton flags: 0x200085
71da816ca0SGreg Clayton reserved: 0x0
72da816ca0SGreg Clayton LoadCommands:
73da816ca0SGreg Clayton - cmd: LC_SEGMENT_64
74da816ca0SGreg Clayton cmdsize: 72
75da816ca0SGreg Clayton segname: __PAGEZERO
76da816ca0SGreg Clayton vmaddr: 0
77da816ca0SGreg Clayton vmsize: 4294967296
78da816ca0SGreg Clayton fileoff: 0
79da816ca0SGreg Clayton filesize: 0
80da816ca0SGreg Clayton maxprot: 0
81da816ca0SGreg Clayton initprot: 0
82da816ca0SGreg Clayton nsects: 0
83da816ca0SGreg Clayton flags: 0
84da816ca0SGreg Clayton - cmd: LC_SEGMENT_64
85da816ca0SGreg Clayton cmdsize: 232
86da816ca0SGreg Clayton segname: __TEXT
87da816ca0SGreg Clayton vmaddr: 4294967296
88da816ca0SGreg Clayton vmsize: 16384
89da816ca0SGreg Clayton fileoff: 0
90da816ca0SGreg Clayton filesize: 16384
91da816ca0SGreg Clayton maxprot: 5
92da816ca0SGreg Clayton initprot: 5
93da816ca0SGreg Clayton nsects: 2
94da816ca0SGreg Clayton flags: 0
95da816ca0SGreg Clayton Sections:
96da816ca0SGreg Clayton - sectname: __text
97da816ca0SGreg Clayton segname: __TEXT
98da816ca0SGreg Clayton addr: 0x100003F94
99da816ca0SGreg Clayton size: 36
100da816ca0SGreg Clayton offset: 0x3F94
101da816ca0SGreg Clayton align: 2
102da816ca0SGreg Clayton reloff: 0x0
103da816ca0SGreg Clayton nreloc: 0
104da816ca0SGreg Clayton flags: 0x80000400
105da816ca0SGreg Clayton reserved1: 0x0
106da816ca0SGreg Clayton reserved2: 0x0
107da816ca0SGreg Clayton reserved3: 0x0
108da816ca0SGreg Clayton content: FF8300D1E80300AA00008052FF1F00B9E81B00B9E10B00F9E20700F9FF830091C0035FD6
109da816ca0SGreg Clayton - sectname: __unwind_info
110da816ca0SGreg Clayton segname: __TEXT
111da816ca0SGreg Clayton addr: 0x100003FB8
112da816ca0SGreg Clayton size: 72
113da816ca0SGreg Clayton offset: 0x3FB8
114da816ca0SGreg Clayton align: 2
115da816ca0SGreg Clayton reloff: 0x0
116da816ca0SGreg Clayton nreloc: 0
117da816ca0SGreg Clayton flags: 0x0
118da816ca0SGreg Clayton reserved1: 0x0
119da816ca0SGreg Clayton reserved2: 0x0
120da816ca0SGreg Clayton reserved3: 0x0
121da816ca0SGreg Clayton content: 010000001C000000000000001C000000000000001C00000002000000943F00003400000034000000B93F00000000000034000000030000000C000100100001000000000000200002
122da816ca0SGreg Clayton - cmd: LC_SEGMENT_64
123da816ca0SGreg Clayton cmdsize: 72
124da816ca0SGreg Clayton segname: __LINKEDIT
125da816ca0SGreg Clayton vmaddr: 4294983680
126da816ca0SGreg Clayton vmsize: 16384
127da816ca0SGreg Clayton fileoff: 16384
128da816ca0SGreg Clayton filesize: 674
129da816ca0SGreg Clayton maxprot: 1
130da816ca0SGreg Clayton initprot: 1
131da816ca0SGreg Clayton nsects: 0
132da816ca0SGreg Clayton flags: 0
133da816ca0SGreg Clayton - cmd: LC_DYLD_CHAINED_FIXUPS
134da816ca0SGreg Clayton cmdsize: 16
135da816ca0SGreg Clayton dataoff: 16384
136da816ca0SGreg Clayton datasize: 56
137da816ca0SGreg Clayton - cmd: LC_DYLD_EXPORTS_TRIE
138da816ca0SGreg Clayton cmdsize: 16
139da816ca0SGreg Clayton dataoff: 16440
140da816ca0SGreg Clayton datasize: 48
141da816ca0SGreg Clayton - cmd: LC_SYMTAB
142da816ca0SGreg Clayton cmdsize: 24
143da816ca0SGreg Clayton symoff: 16496
144da816ca0SGreg Clayton nsyms: 10
145da816ca0SGreg Clayton stroff: 16656
146da816ca0SGreg Clayton strsize: 128
147da816ca0SGreg Clayton - cmd: LC_DYSYMTAB
148da816ca0SGreg Clayton cmdsize: 80
149da816ca0SGreg Clayton ilocalsym: 0
150da816ca0SGreg Clayton nlocalsym: 8
151da816ca0SGreg Clayton iextdefsym: 8
152da816ca0SGreg Clayton nextdefsym: 2
153da816ca0SGreg Clayton iundefsym: 10
154da816ca0SGreg Clayton nundefsym: 0
155da816ca0SGreg Clayton tocoff: 0
156da816ca0SGreg Clayton ntoc: 0
157da816ca0SGreg Clayton modtaboff: 0
158da816ca0SGreg Clayton nmodtab: 0
159da816ca0SGreg Clayton extrefsymoff: 0
160da816ca0SGreg Clayton nextrefsyms: 0
161da816ca0SGreg Clayton indirectsymoff: 0
162da816ca0SGreg Clayton nindirectsyms: 0
163da816ca0SGreg Clayton extreloff: 0
164da816ca0SGreg Clayton nextrel: 0
165da816ca0SGreg Clayton locreloff: 0
166da816ca0SGreg Clayton nlocrel: 0
167da816ca0SGreg Clayton - cmd: LC_LOAD_DYLINKER
168da816ca0SGreg Clayton cmdsize: 32
169da816ca0SGreg Clayton name: 12
170da816ca0SGreg Clayton Content: '/usr/lib/dyld'
171da816ca0SGreg Clayton ZeroPadBytes: 7
172da816ca0SGreg Clayton - cmd: LC_UUID
173da816ca0SGreg Clayton cmdsize: 24
174da816ca0SGreg Clayton uuid: 1EECD2B8-16EA-3FEC-AB3C-F46139DBD0E2
175da816ca0SGreg Clayton - cmd: LC_BUILD_VERSION
176da816ca0SGreg Clayton cmdsize: 32
177da816ca0SGreg Clayton platform: 1
178da816ca0SGreg Clayton minos: 786432
179da816ca0SGreg Clayton sdk: 786432
180da816ca0SGreg Clayton ntools: 1
181da816ca0SGreg Clayton Tools:
182da816ca0SGreg Clayton - tool: 3
183da816ca0SGreg Clayton version: 46596096
184da816ca0SGreg Clayton - cmd: LC_SOURCE_VERSION
185da816ca0SGreg Clayton cmdsize: 16
186da816ca0SGreg Clayton version: 0
187da816ca0SGreg Clayton - cmd: LC_MAIN
188da816ca0SGreg Clayton cmdsize: 24
189da816ca0SGreg Clayton entryoff: 16276
190da816ca0SGreg Clayton stacksize: 0
191da816ca0SGreg Clayton - cmd: LC_LOAD_DYLIB
192da816ca0SGreg Clayton cmdsize: 48
193da816ca0SGreg Clayton dylib:
194da816ca0SGreg Clayton name: 24
195da816ca0SGreg Clayton timestamp: 2
196da816ca0SGreg Clayton current_version: 78643968
197da816ca0SGreg Clayton compatibility_version: 65536
198da816ca0SGreg Clayton Content: '/usr/lib/libc++.1.dylib'
199da816ca0SGreg Clayton ZeroPadBytes: 1
200da816ca0SGreg Clayton - cmd: LC_LOAD_DYLIB
201da816ca0SGreg Clayton cmdsize: 56
202da816ca0SGreg Clayton dylib:
203da816ca0SGreg Clayton name: 24
204da816ca0SGreg Clayton timestamp: 2
205da816ca0SGreg Clayton current_version: 85917696
206da816ca0SGreg Clayton compatibility_version: 65536
207da816ca0SGreg Clayton Content: '/usr/lib/libSystem.B.dylib'
208da816ca0SGreg Clayton ZeroPadBytes: 6
209da816ca0SGreg Clayton - cmd: LC_FUNCTION_STARTS
210da816ca0SGreg Clayton cmdsize: 16
211da816ca0SGreg Clayton dataoff: 16488
212da816ca0SGreg Clayton datasize: 8
213da816ca0SGreg Clayton - cmd: LC_DATA_IN_CODE
214da816ca0SGreg Clayton cmdsize: 16
215da816ca0SGreg Clayton dataoff: 16496
216da816ca0SGreg Clayton datasize: 0
217da816ca0SGreg Clayton - cmd: LC_CODE_SIGNATURE
218da816ca0SGreg Clayton cmdsize: 16
219da816ca0SGreg Clayton dataoff: 16784
220da816ca0SGreg Clayton datasize: 274
221da816ca0SGreg Clayton LinkEditData:
222da816ca0SGreg Clayton NameList:
223da816ca0SGreg Clayton - n_strx: 28
224da816ca0SGreg Clayton n_type: 0x64
225da816ca0SGreg Clayton n_sect: 0
226da816ca0SGreg Clayton n_desc: 0
227da816ca0SGreg Clayton n_value: 0
228da816ca0SGreg Clayton - n_strx: 64
229da816ca0SGreg Clayton n_type: 0x64
230da816ca0SGreg Clayton n_sect: 0
231da816ca0SGreg Clayton n_desc: 0
232da816ca0SGreg Clayton n_value: 0
233da816ca0SGreg Clayton - n_strx: 73
234da816ca0SGreg Clayton n_type: 0x66
235da816ca0SGreg Clayton n_sect: 0
236da816ca0SGreg Clayton n_desc: 1
237da816ca0SGreg Clayton n_value: 1639532873
238da816ca0SGreg Clayton - n_strx: 1
239da816ca0SGreg Clayton n_type: 0x2E
240da816ca0SGreg Clayton n_sect: 1
241da816ca0SGreg Clayton n_desc: 0
242da816ca0SGreg Clayton n_value: 4294983572
243da816ca0SGreg Clayton - n_strx: 115
244da816ca0SGreg Clayton n_type: 0x24
245da816ca0SGreg Clayton n_sect: 1
246da816ca0SGreg Clayton n_desc: 0
247da816ca0SGreg Clayton n_value: 4294983572
248da816ca0SGreg Clayton - n_strx: 1
249da816ca0SGreg Clayton n_type: 0x24
250da816ca0SGreg Clayton n_sect: 0
251da816ca0SGreg Clayton n_desc: 0
252da816ca0SGreg Clayton n_value: 36
253da816ca0SGreg Clayton - n_strx: 1
254da816ca0SGreg Clayton n_type: 0x4E
255da816ca0SGreg Clayton n_sect: 1
256da816ca0SGreg Clayton n_desc: 0
257da816ca0SGreg Clayton n_value: 36
258da816ca0SGreg Clayton - n_strx: 1
259da816ca0SGreg Clayton n_type: 0x64
260da816ca0SGreg Clayton n_sect: 1
261da816ca0SGreg Clayton n_desc: 0
262da816ca0SGreg Clayton n_value: 0
263da816ca0SGreg Clayton - n_strx: 2
264da816ca0SGreg Clayton n_type: 0xF
265da816ca0SGreg Clayton n_sect: 1
266da816ca0SGreg Clayton n_desc: 16
267da816ca0SGreg Clayton n_value: 4294967296
268da816ca0SGreg Clayton - n_strx: 22
269da816ca0SGreg Clayton n_type: 0xF
270da816ca0SGreg Clayton n_sect: 1
271da816ca0SGreg Clayton n_desc: 0
272da816ca0SGreg Clayton n_value: 4294983572
273da816ca0SGreg Clayton StringTable:
274da816ca0SGreg Clayton - ' '
275da816ca0SGreg Clayton - __mh_execute_header
276da816ca0SGreg Clayton - _main
277da816ca0SGreg Clayton - '/Users/gclayton/Documents/src/args/'
278da816ca0SGreg Clayton - main.cpp
279da816ca0SGreg Clayton - '/Users/gclayton/Documents/src/args/main.o'
280da816ca0SGreg Clayton - _main
281da816ca0SGreg Clayton - ''
282da816ca0SGreg Clayton - ''
283da816ca0SGreg Clayton - ''
284da816ca0SGreg Clayton - ''
285da816ca0SGreg Clayton - ''
286da816ca0SGreg Clayton - ''
287da816ca0SGreg Clayton - ''
288da816ca0SGreg Clayton ...
289da816ca0SGreg Clayton )");
290da816ca0SGreg Clayton
291da816ca0SGreg Clayton ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());
292da816ca0SGreg Clayton auto module_sp = std::make_shared<Module>(ExpectedFile->moduleSpec());
293da816ca0SGreg Clayton ObjectFile *objfile = module_sp->GetObjectFile();
294da816ca0SGreg Clayton ASSERT_NE(objfile, nullptr);
295da816ca0SGreg Clayton
296da816ca0SGreg Clayton // Test encoding and decoding an empty symbol table.
297da816ca0SGreg Clayton Symtab symtab(objfile);
298da816ca0SGreg Clayton symtab.PreloadSymbols();
299da816ca0SGreg Clayton EncodeDecode(symtab);
300da816ca0SGreg Clayton
301da816ca0SGreg Clayton // Now encode and decode an actual symbol table from our yaml.
302da816ca0SGreg Clayton Symtab *module_symtab = module_sp->GetSymtab();
303da816ca0SGreg Clayton ASSERT_NE(module_symtab, nullptr);
304da816ca0SGreg Clayton module_symtab->PreloadSymbols();
305da816ca0SGreg Clayton EncodeDecode(*module_symtab);
306da816ca0SGreg Clayton }
307268089b6SGreg Clayton
TEST_F(SymtabTest,TestDecodeCStringMaps)308268089b6SGreg Clayton TEST_F(SymtabTest, TestDecodeCStringMaps) {
309268089b6SGreg Clayton // Symbol tables save out the symbols, but they also save out the symbol table
310268089b6SGreg Clayton // name indexes. These name indexes are a map of sorted ConstString + T pairs
311268089b6SGreg Clayton // and when they are decoded from a file, they are no longer sorted since
312268089b6SGreg Clayton // ConstString objects can be sorted by "const char *" and the order in which
313268089b6SGreg Clayton // these strings are created won't be the same in a new process. We need to
314268089b6SGreg Clayton // ensure these name lookups happen correctly when we load the name indexes,
315268089b6SGreg Clayton // so this test loads a symbol table from a cache file from
316268089b6SGreg Clayton // "lldb/unittests/Symbol/Inputs/indexnames-symtab-cache" and make sure we
317268089b6SGreg Clayton // can correctly lookup each of the names in the symbol table.
318268089b6SGreg Clayton auto ExpectedFile = TestFile::fromYaml(R"(
319268089b6SGreg Clayton --- !mach-o
320268089b6SGreg Clayton FileHeader:
321268089b6SGreg Clayton magic: 0xFEEDFACF
322268089b6SGreg Clayton cputype: 0x100000C
323268089b6SGreg Clayton cpusubtype: 0x0
324268089b6SGreg Clayton filetype: 0x2
325268089b6SGreg Clayton ncmds: 16
326268089b6SGreg Clayton sizeofcmds: 744
327268089b6SGreg Clayton flags: 0x200085
328268089b6SGreg Clayton reserved: 0x0
329268089b6SGreg Clayton LoadCommands:
330268089b6SGreg Clayton - cmd: LC_SEGMENT_64
331268089b6SGreg Clayton cmdsize: 72
332268089b6SGreg Clayton segname: __PAGEZERO
333268089b6SGreg Clayton vmaddr: 0
334268089b6SGreg Clayton vmsize: 4294967296
335268089b6SGreg Clayton fileoff: 0
336268089b6SGreg Clayton filesize: 0
337268089b6SGreg Clayton maxprot: 0
338268089b6SGreg Clayton initprot: 0
339268089b6SGreg Clayton nsects: 0
340268089b6SGreg Clayton flags: 0
341268089b6SGreg Clayton - cmd: LC_SEGMENT_64
342268089b6SGreg Clayton cmdsize: 232
343268089b6SGreg Clayton segname: __TEXT
344268089b6SGreg Clayton vmaddr: 4294967296
345268089b6SGreg Clayton vmsize: 16384
346268089b6SGreg Clayton fileoff: 0
347268089b6SGreg Clayton filesize: 16384
348268089b6SGreg Clayton maxprot: 5
349268089b6SGreg Clayton initprot: 5
350268089b6SGreg Clayton nsects: 2
351268089b6SGreg Clayton flags: 0
352268089b6SGreg Clayton Sections:
353268089b6SGreg Clayton - sectname: __text
354268089b6SGreg Clayton segname: __TEXT
355268089b6SGreg Clayton addr: 0x100003F64
356268089b6SGreg Clayton size: 76
357268089b6SGreg Clayton offset: 0x3F64
358268089b6SGreg Clayton align: 2
359268089b6SGreg Clayton reloff: 0x0
360268089b6SGreg Clayton nreloc: 0
361268089b6SGreg Clayton flags: 0x80000400
362268089b6SGreg Clayton reserved1: 0x0
363268089b6SGreg Clayton reserved2: 0x0
364268089b6SGreg Clayton reserved3: 0x0
365268089b6SGreg Clayton content: 80018052C0035FD6E0028052C0035FD640048052C0035FD6FF8300D1FD7B01A9FD43009108008052E80B00B9BFC31FB8F4FFFF97F5FFFF97F6FFFF97E00B40B9FD7B41A9FF830091C0035FD6
366268089b6SGreg Clayton - sectname: __unwind_info
367268089b6SGreg Clayton segname: __TEXT
368268089b6SGreg Clayton addr: 0x100003FB0
369268089b6SGreg Clayton size: 80
370268089b6SGreg Clayton offset: 0x3FB0
371268089b6SGreg Clayton align: 2
372268089b6SGreg Clayton reloff: 0x0
373268089b6SGreg Clayton nreloc: 0
374268089b6SGreg Clayton flags: 0x0
375268089b6SGreg Clayton reserved1: 0x0
376268089b6SGreg Clayton reserved2: 0x0
377268089b6SGreg Clayton reserved3: 0x0
378268089b6SGreg Clayton content: 010000001C000000000000001C000000000000001C00000002000000643F00003400000034000000B13F00000000000034000000030000000C0002001400020000000001180000000000000400000002
379268089b6SGreg Clayton - cmd: LC_SEGMENT_64
380268089b6SGreg Clayton cmdsize: 72
381268089b6SGreg Clayton segname: __LINKEDIT
382268089b6SGreg Clayton vmaddr: 4294983680
383268089b6SGreg Clayton vmsize: 16384
384268089b6SGreg Clayton fileoff: 16384
385268089b6SGreg Clayton filesize: 994
386268089b6SGreg Clayton maxprot: 1
387268089b6SGreg Clayton initprot: 1
388268089b6SGreg Clayton nsects: 0
389268089b6SGreg Clayton flags: 0
390268089b6SGreg Clayton - cmd: LC_DYLD_CHAINED_FIXUPS
391268089b6SGreg Clayton cmdsize: 16
392268089b6SGreg Clayton dataoff: 16384
393268089b6SGreg Clayton datasize: 56
394268089b6SGreg Clayton - cmd: LC_DYLD_EXPORTS_TRIE
395268089b6SGreg Clayton cmdsize: 16
396268089b6SGreg Clayton dataoff: 16440
397268089b6SGreg Clayton datasize: 80
398268089b6SGreg Clayton - cmd: LC_SYMTAB
399268089b6SGreg Clayton cmdsize: 24
400268089b6SGreg Clayton symoff: 16528
401268089b6SGreg Clayton nsyms: 25
402268089b6SGreg Clayton stroff: 16928
403268089b6SGreg Clayton strsize: 176
404268089b6SGreg Clayton - cmd: LC_DYSYMTAB
405268089b6SGreg Clayton cmdsize: 80
406268089b6SGreg Clayton ilocalsym: 0
407268089b6SGreg Clayton nlocalsym: 20
408268089b6SGreg Clayton iextdefsym: 20
409268089b6SGreg Clayton nextdefsym: 5
410268089b6SGreg Clayton iundefsym: 25
411268089b6SGreg Clayton nundefsym: 0
412268089b6SGreg Clayton tocoff: 0
413268089b6SGreg Clayton ntoc: 0
414268089b6SGreg Clayton modtaboff: 0
415268089b6SGreg Clayton nmodtab: 0
416268089b6SGreg Clayton extrefsymoff: 0
417268089b6SGreg Clayton nextrefsyms: 0
418268089b6SGreg Clayton indirectsymoff: 0
419268089b6SGreg Clayton nindirectsyms: 0
420268089b6SGreg Clayton extreloff: 0
421268089b6SGreg Clayton nextrel: 0
422268089b6SGreg Clayton locreloff: 0
423268089b6SGreg Clayton nlocrel: 0
424268089b6SGreg Clayton - cmd: LC_LOAD_DYLINKER
425268089b6SGreg Clayton cmdsize: 32
426268089b6SGreg Clayton name: 12
427268089b6SGreg Clayton Content: '/usr/lib/dyld'
428268089b6SGreg Clayton ZeroPadBytes: 7
429268089b6SGreg Clayton - cmd: LC_UUID
430268089b6SGreg Clayton cmdsize: 24
431268089b6SGreg Clayton uuid: 3E94866E-0D1A-39BD-975B-64E8F1FDBAAE
432268089b6SGreg Clayton - cmd: LC_BUILD_VERSION
433268089b6SGreg Clayton cmdsize: 32
434268089b6SGreg Clayton platform: 1
435268089b6SGreg Clayton minos: 786432
436268089b6SGreg Clayton sdk: 787200
437268089b6SGreg Clayton ntools: 1
438268089b6SGreg Clayton Tools:
439268089b6SGreg Clayton - tool: 3
440268089b6SGreg Clayton version: 49938432
441268089b6SGreg Clayton - cmd: LC_SOURCE_VERSION
442268089b6SGreg Clayton cmdsize: 16
443268089b6SGreg Clayton version: 0
444268089b6SGreg Clayton - cmd: LC_MAIN
445268089b6SGreg Clayton cmdsize: 24
446268089b6SGreg Clayton entryoff: 16252
447268089b6SGreg Clayton stacksize: 0
448268089b6SGreg Clayton - cmd: LC_LOAD_DYLIB
449268089b6SGreg Clayton cmdsize: 56
450268089b6SGreg Clayton dylib:
451268089b6SGreg Clayton name: 24
452268089b6SGreg Clayton timestamp: 2
453268089b6SGreg Clayton current_version: 85943299
454268089b6SGreg Clayton compatibility_version: 65536
455268089b6SGreg Clayton Content: '/usr/lib/libSystem.B.dylib'
456268089b6SGreg Clayton ZeroPadBytes: 6
457268089b6SGreg Clayton - cmd: LC_FUNCTION_STARTS
458268089b6SGreg Clayton cmdsize: 16
459268089b6SGreg Clayton dataoff: 16520
460268089b6SGreg Clayton datasize: 8
461268089b6SGreg Clayton - cmd: LC_DATA_IN_CODE
462268089b6SGreg Clayton cmdsize: 16
463268089b6SGreg Clayton dataoff: 16528
464268089b6SGreg Clayton datasize: 0
465268089b6SGreg Clayton - cmd: LC_CODE_SIGNATURE
466268089b6SGreg Clayton cmdsize: 16
467268089b6SGreg Clayton dataoff: 17104
468268089b6SGreg Clayton datasize: 274
469268089b6SGreg Clayton LinkEditData:
470268089b6SGreg Clayton NameList:
471268089b6SGreg Clayton - n_strx: 43
472268089b6SGreg Clayton n_type: 0x64
473268089b6SGreg Clayton n_sect: 0
474268089b6SGreg Clayton n_desc: 0
475268089b6SGreg Clayton n_value: 0
476268089b6SGreg Clayton - n_strx: 91
477268089b6SGreg Clayton n_type: 0x64
478268089b6SGreg Clayton n_sect: 0
479268089b6SGreg Clayton n_desc: 0
480268089b6SGreg Clayton n_value: 0
481268089b6SGreg Clayton - n_strx: 98
482268089b6SGreg Clayton n_type: 0x66
483268089b6SGreg Clayton n_sect: 0
484268089b6SGreg Clayton n_desc: 1
485268089b6SGreg Clayton n_value: 1651098491
486268089b6SGreg Clayton - n_strx: 1
487268089b6SGreg Clayton n_type: 0x2E
488268089b6SGreg Clayton n_sect: 1
489268089b6SGreg Clayton n_desc: 0
490268089b6SGreg Clayton n_value: 4294983524
491268089b6SGreg Clayton - n_strx: 152
492268089b6SGreg Clayton n_type: 0x24
493268089b6SGreg Clayton n_sect: 1
494268089b6SGreg Clayton n_desc: 0
495268089b6SGreg Clayton n_value: 4294983524
496268089b6SGreg Clayton - n_strx: 1
497268089b6SGreg Clayton n_type: 0x24
498268089b6SGreg Clayton n_sect: 0
499268089b6SGreg Clayton n_desc: 0
500268089b6SGreg Clayton n_value: 8
501268089b6SGreg Clayton - n_strx: 1
502268089b6SGreg Clayton n_type: 0x4E
503268089b6SGreg Clayton n_sect: 1
504268089b6SGreg Clayton n_desc: 0
505268089b6SGreg Clayton n_value: 8
506268089b6SGreg Clayton - n_strx: 1
507268089b6SGreg Clayton n_type: 0x2E
508268089b6SGreg Clayton n_sect: 1
509268089b6SGreg Clayton n_desc: 0
510268089b6SGreg Clayton n_value: 4294983532
511268089b6SGreg Clayton - n_strx: 157
512268089b6SGreg Clayton n_type: 0x24
513268089b6SGreg Clayton n_sect: 1
514268089b6SGreg Clayton n_desc: 0
515268089b6SGreg Clayton n_value: 4294983532
516268089b6SGreg Clayton - n_strx: 1
517268089b6SGreg Clayton n_type: 0x24
518268089b6SGreg Clayton n_sect: 0
519268089b6SGreg Clayton n_desc: 0
520268089b6SGreg Clayton n_value: 8
521268089b6SGreg Clayton - n_strx: 1
522268089b6SGreg Clayton n_type: 0x4E
523268089b6SGreg Clayton n_sect: 1
524268089b6SGreg Clayton n_desc: 0
525268089b6SGreg Clayton n_value: 8
526268089b6SGreg Clayton - n_strx: 1
527268089b6SGreg Clayton n_type: 0x2E
528268089b6SGreg Clayton n_sect: 1
529268089b6SGreg Clayton n_desc: 0
530268089b6SGreg Clayton n_value: 4294983540
531268089b6SGreg Clayton - n_strx: 162
532268089b6SGreg Clayton n_type: 0x24
533268089b6SGreg Clayton n_sect: 1
534268089b6SGreg Clayton n_desc: 0
535268089b6SGreg Clayton n_value: 4294983540
536268089b6SGreg Clayton - n_strx: 1
537268089b6SGreg Clayton n_type: 0x24
538268089b6SGreg Clayton n_sect: 0
539268089b6SGreg Clayton n_desc: 0
540268089b6SGreg Clayton n_value: 8
541268089b6SGreg Clayton - n_strx: 1
542268089b6SGreg Clayton n_type: 0x4E
543268089b6SGreg Clayton n_sect: 1
544268089b6SGreg Clayton n_desc: 0
545268089b6SGreg Clayton n_value: 8
546268089b6SGreg Clayton - n_strx: 1
547268089b6SGreg Clayton n_type: 0x2E
548268089b6SGreg Clayton n_sect: 1
549268089b6SGreg Clayton n_desc: 0
550268089b6SGreg Clayton n_value: 4294983548
551268089b6SGreg Clayton - n_strx: 167
552268089b6SGreg Clayton n_type: 0x24
553268089b6SGreg Clayton n_sect: 1
554268089b6SGreg Clayton n_desc: 0
555268089b6SGreg Clayton n_value: 4294983548
556268089b6SGreg Clayton - n_strx: 1
557268089b6SGreg Clayton n_type: 0x24
558268089b6SGreg Clayton n_sect: 0
559268089b6SGreg Clayton n_desc: 0
560268089b6SGreg Clayton n_value: 52
561268089b6SGreg Clayton - n_strx: 1
562268089b6SGreg Clayton n_type: 0x4E
563268089b6SGreg Clayton n_sect: 1
564268089b6SGreg Clayton n_desc: 0
565268089b6SGreg Clayton n_value: 52
566268089b6SGreg Clayton - n_strx: 1
567268089b6SGreg Clayton n_type: 0x64
568268089b6SGreg Clayton n_sect: 1
569268089b6SGreg Clayton n_desc: 0
570268089b6SGreg Clayton n_value: 0
571268089b6SGreg Clayton - n_strx: 2
572268089b6SGreg Clayton n_type: 0xF
573268089b6SGreg Clayton n_sect: 1
574268089b6SGreg Clayton n_desc: 16
575268089b6SGreg Clayton n_value: 4294967296
576268089b6SGreg Clayton - n_strx: 22
577268089b6SGreg Clayton n_type: 0xF
578268089b6SGreg Clayton n_sect: 1
579268089b6SGreg Clayton n_desc: 0
580268089b6SGreg Clayton n_value: 4294983532
581268089b6SGreg Clayton - n_strx: 27
582268089b6SGreg Clayton n_type: 0xF
583268089b6SGreg Clayton n_sect: 1
584268089b6SGreg Clayton n_desc: 0
585268089b6SGreg Clayton n_value: 4294983540
586268089b6SGreg Clayton - n_strx: 32
587268089b6SGreg Clayton n_type: 0xF
588268089b6SGreg Clayton n_sect: 1
589268089b6SGreg Clayton n_desc: 0
590268089b6SGreg Clayton n_value: 4294983524
591268089b6SGreg Clayton - n_strx: 37
592268089b6SGreg Clayton n_type: 0xF
593268089b6SGreg Clayton n_sect: 1
594268089b6SGreg Clayton n_desc: 0
595268089b6SGreg Clayton n_value: 4294983548
596268089b6SGreg Clayton StringTable:
597268089b6SGreg Clayton - ' '
598268089b6SGreg Clayton - __mh_execute_header
599268089b6SGreg Clayton - _bar
600268089b6SGreg Clayton - _baz
601268089b6SGreg Clayton - _foo
602268089b6SGreg Clayton - _main
603268089b6SGreg Clayton - '/Users/gclayton/Documents/objfiles/index-names/'
604268089b6SGreg Clayton - main.c
605268089b6SGreg Clayton - '/Users/gclayton/Documents/objfiles/index-names/main.o'
606268089b6SGreg Clayton - _foo
607268089b6SGreg Clayton - _bar
608268089b6SGreg Clayton - _baz
609268089b6SGreg Clayton - _main
610268089b6SGreg Clayton - ''
611268089b6SGreg Clayton - ''
612268089b6SGreg Clayton - ''
613268089b6SGreg Clayton FunctionStarts: [ 0x3F64, 0x3F6C, 0x3F74, 0x3F7C ]
614268089b6SGreg Clayton ...
615268089b6SGreg Clayton )");
616268089b6SGreg Clayton // This data was taken from a hex dump of the object file from the above yaml
617268089b6SGreg Clayton // and hexdumped so we can load the cache data in this test.
618268089b6SGreg Clayton const uint8_t symtab_cache_bytes[] = {
619268089b6SGreg Clayton 0x01, 0x10, 0x3e, 0x94, 0x86, 0x6e, 0x0d, 0x1a,
620268089b6SGreg Clayton 0x39, 0xbd, 0x97, 0x5b, 0x64, 0xe8, 0xf1, 0xfd,
621268089b6SGreg Clayton 0xba, 0xae, 0xff, 0x53, 0x54, 0x41, 0x42, 0x91,
622268089b6SGreg Clayton 0x00, 0x00, 0x00, 0x00, 0x2f, 0x55, 0x73, 0x65,
623268089b6SGreg Clayton 0x72, 0x73, 0x2f, 0x67, 0x63, 0x6c, 0x61, 0x79,
624268089b6SGreg Clayton 0x74, 0x6f, 0x6e, 0x2f, 0x44, 0x6f, 0x63, 0x75,
625268089b6SGreg Clayton 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x2f, 0x6f, 0x62,
626268089b6SGreg Clayton 0x6a, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2f, 0x69,
627268089b6SGreg Clayton 0x6e, 0x64, 0x65, 0x78, 0x2d, 0x6e, 0x61, 0x6d,
628268089b6SGreg Clayton 0x65, 0x73, 0x2f, 0x6d, 0x61, 0x69, 0x6e, 0x2e,
629268089b6SGreg Clayton 0x63, 0x00, 0x2f, 0x55, 0x73, 0x65, 0x72, 0x73,
630268089b6SGreg Clayton 0x2f, 0x67, 0x63, 0x6c, 0x61, 0x79, 0x74, 0x6f,
631268089b6SGreg Clayton 0x6e, 0x2f, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65,
632268089b6SGreg Clayton 0x6e, 0x74, 0x73, 0x2f, 0x6f, 0x62, 0x6a, 0x66,
633268089b6SGreg Clayton 0x69, 0x6c, 0x65, 0x73, 0x2f, 0x69, 0x6e, 0x64,
634268089b6SGreg Clayton 0x65, 0x78, 0x2d, 0x6e, 0x61, 0x6d, 0x65, 0x73,
635268089b6SGreg Clayton 0x2f, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x6f, 0x00,
636268089b6SGreg Clayton 0x66, 0x6f, 0x6f, 0x00, 0x62, 0x61, 0x72, 0x00,
637268089b6SGreg Clayton 0x62, 0x61, 0x7a, 0x00, 0x6d, 0x61, 0x69, 0x6e,
638268089b6SGreg Clayton 0x00, 0x5f, 0x6d, 0x68, 0x5f, 0x65, 0x78, 0x65,
639268089b6SGreg Clayton 0x63, 0x75, 0x74, 0x65, 0x5f, 0x68, 0x65, 0x61,
640268089b6SGreg Clayton 0x64, 0x65, 0x72, 0x00, 0x53, 0x59, 0x4d, 0x42,
641268089b6SGreg Clayton 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
642268089b6SGreg Clayton 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x2a,
643268089b6SGreg Clayton 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
644268089b6SGreg Clayton 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
645268089b6SGreg Clayton 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
646268089b6SGreg Clayton 0x64, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
647268089b6SGreg Clayton 0x0a, 0x20, 0x01, 0x37, 0x00, 0x00, 0x00, 0x00,
648268089b6SGreg Clayton 0x7b, 0xc3, 0x69, 0x62, 0x00, 0x00, 0x00, 0x00,
649268089b6SGreg Clayton 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
650268089b6SGreg Clayton 0x01, 0x00, 0x66, 0x00, 0x04, 0x00, 0x00, 0x00,
651268089b6SGreg Clayton 0x00, 0x00, 0x02, 0x32, 0x01, 0x6d, 0x00, 0x00,
652268089b6SGreg Clayton 0x00, 0x01, 0x64, 0x3f, 0x00, 0x00, 0x01, 0x00,
653268089b6SGreg Clayton 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
654268089b6SGreg Clayton 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x08, 0x00,
655268089b6SGreg Clayton 0x00, 0x00, 0x00, 0x00, 0x02, 0x32, 0x01, 0x71,
656268089b6SGreg Clayton 0x00, 0x00, 0x00, 0x01, 0x6c, 0x3f, 0x00, 0x00,
657268089b6SGreg Clayton 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
658268089b6SGreg Clayton 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00,
659268089b6SGreg Clayton 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x32,
660268089b6SGreg Clayton 0x01, 0x75, 0x00, 0x00, 0x00, 0x01, 0x74, 0x3f,
661268089b6SGreg Clayton 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00,
662268089b6SGreg Clayton 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
663268089b6SGreg Clayton 0x0f, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
664268089b6SGreg Clayton 0x02, 0x32, 0x01, 0x79, 0x00, 0x00, 0x00, 0x01,
665268089b6SGreg Clayton 0x7c, 0x3f, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
666268089b6SGreg Clayton 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
667268089b6SGreg Clayton 0x00, 0x00, 0x0f, 0x00, 0x14, 0x00, 0x00, 0x00,
668268089b6SGreg Clayton 0x00, 0x00, 0x04, 0x12, 0x02, 0x7e, 0x00, 0x00,
669268089b6SGreg Clayton 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
670268089b6SGreg Clayton 0x00, 0x00, 0x64, 0x3f, 0x00, 0x00, 0x00, 0x00,
671268089b6SGreg Clayton 0x00, 0x00, 0x10, 0x00, 0x0f, 0x00, 0x01, 0x00,
672268089b6SGreg Clayton 0x43, 0x4d, 0x41, 0x50, 0x07, 0x00, 0x00, 0x00,
673268089b6SGreg Clayton 0x6d, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
674268089b6SGreg Clayton 0x75, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
675268089b6SGreg Clayton 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
676268089b6SGreg Clayton 0x79, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
677268089b6SGreg Clayton 0x37, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
678268089b6SGreg Clayton 0x71, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
679268089b6SGreg Clayton 0x7e, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00
680268089b6SGreg Clayton };
681268089b6SGreg Clayton
682268089b6SGreg Clayton ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());
683268089b6SGreg Clayton auto module_sp = std::make_shared<Module>(ExpectedFile->moduleSpec());
684268089b6SGreg Clayton ObjectFile *objfile = module_sp->GetObjectFile();
685268089b6SGreg Clayton ASSERT_NE(objfile, nullptr);
686268089b6SGreg Clayton
687268089b6SGreg Clayton // Test encoding and decoding an empty symbol table.
688268089b6SGreg Clayton DataExtractor data(symtab_cache_bytes, sizeof(symtab_cache_bytes),
689268089b6SGreg Clayton eByteOrderLittle, 8);
690268089b6SGreg Clayton Symtab symtab(objfile);
691268089b6SGreg Clayton offset_t data_offset = 0;
692268089b6SGreg Clayton bool uuid_mismatch = false; // Gets set to true if signature doesn't match.
693268089b6SGreg Clayton const bool success = symtab.Decode(data, &data_offset, uuid_mismatch);
694268089b6SGreg Clayton ASSERT_EQ(success, true);
695268089b6SGreg Clayton ASSERT_EQ(uuid_mismatch, false);
696268089b6SGreg Clayton
697268089b6SGreg Clayton // Now make sure that name lookup works for all symbols. This indicates that
698268089b6SGreg Clayton // the Symtab::NameToIndexMap was decoded correctly and works as expected.
699268089b6SGreg Clayton Symbol *symbol = nullptr;
700268089b6SGreg Clayton symbol = symtab.FindFirstSymbolWithNameAndType(ConstString("main"),
701268089b6SGreg Clayton eSymbolTypeCode,
702268089b6SGreg Clayton Symtab::eDebugAny,
703268089b6SGreg Clayton Symtab::eVisibilityAny);
704268089b6SGreg Clayton ASSERT_NE(symbol, nullptr);
705268089b6SGreg Clayton symbol = symtab.FindFirstSymbolWithNameAndType(ConstString("foo"),
706268089b6SGreg Clayton eSymbolTypeCode,
707268089b6SGreg Clayton Symtab::eDebugAny,
708268089b6SGreg Clayton Symtab::eVisibilityAny);
709268089b6SGreg Clayton ASSERT_NE(symbol, nullptr);
710268089b6SGreg Clayton symbol = symtab.FindFirstSymbolWithNameAndType(ConstString("bar"),
711268089b6SGreg Clayton eSymbolTypeCode,
712268089b6SGreg Clayton Symtab::eDebugAny,
713268089b6SGreg Clayton Symtab::eVisibilityAny);
714268089b6SGreg Clayton ASSERT_NE(symbol, nullptr);
715268089b6SGreg Clayton symbol = symtab.FindFirstSymbolWithNameAndType(ConstString("baz"),
716268089b6SGreg Clayton eSymbolTypeCode,
717268089b6SGreg Clayton Symtab::eDebugAny,
718268089b6SGreg Clayton Symtab::eVisibilityAny);
719268089b6SGreg Clayton ASSERT_NE(symbol, nullptr);
720268089b6SGreg Clayton }
721