838 lines · cpp
1//===-- SymbolTest.cpp ----------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"10#include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"11#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"12#include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"13#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"14#include "TestingSupport/SubsystemRAII.h"15#include "TestingSupport/TestUtilities.h"16 17#include "lldb/Core/DataFileCache.h"18#include "lldb/Core/Module.h"19#include "lldb/Host/FileSystem.h"20#include "lldb/Host/HostInfo.h"21#include "lldb/Symbol/Symbol.h"22#include "lldb/Symbol/Symtab.h"23#include "lldb/Utility/DataEncoder.h"24#include "lldb/Utility/DataExtractor.h"25 26#include <memory>27 28#include "gtest/gtest.h"29 30using namespace lldb;31using namespace lldb_private;32using namespace lldb_private::plugin::dwarf;33 34class SymtabTest : public testing::Test {35 SubsystemRAII<FileSystem, HostInfo, ObjectFileMachO, SymbolFileDWARF,36 ObjectFileELF, SymbolFileSymtab, TypeSystemClang>37 subsystem;38};39 40static void EncodeDecode(const Symtab &object, ByteOrder byte_order) {41 const uint8_t addr_size = 8;42 DataEncoder file(byte_order, addr_size);43 44 object.Encode(file);45 llvm::ArrayRef<uint8_t> bytes = file.GetData();46 DataExtractor data(bytes.data(), bytes.size(), byte_order, addr_size);47 Symtab decoded_object(object.GetObjectFile());48 offset_t data_offset = 0;49 bool uuid_mismatch = false;50 decoded_object.Decode(data, &data_offset, uuid_mismatch);51 ASSERT_EQ(object.GetNumSymbols(), decoded_object.GetNumSymbols());52 for (size_t i = 0; i < object.GetNumSymbols(); ++i)53 EXPECT_EQ(*object.SymbolAtIndex(i), *decoded_object.SymbolAtIndex(i));54}55 56static void EncodeDecode(const Symtab &object) {57 EncodeDecode(object, eByteOrderLittle);58 EncodeDecode(object, eByteOrderBig);59}60 61TEST_F(SymtabTest, EncodeDecodeSymtab) {62 63 auto ExpectedFile = TestFile::fromYaml(R"(64--- !mach-o65FileHeader:66 magic: 0xFEEDFACF67 cputype: 0x100000C68 cpusubtype: 0x069 filetype: 0x270 ncmds: 1771 sizeofcmds: 79272 flags: 0x20008573 reserved: 0x074LoadCommands:75 - cmd: LC_SEGMENT_6476 cmdsize: 7277 segname: __PAGEZERO78 vmaddr: 079 vmsize: 429496729680 fileoff: 081 filesize: 082 maxprot: 083 initprot: 084 nsects: 085 flags: 086 - cmd: LC_SEGMENT_6487 cmdsize: 23288 segname: __TEXT89 vmaddr: 429496729690 vmsize: 1638491 fileoff: 092 filesize: 1638493 maxprot: 594 initprot: 595 nsects: 296 flags: 097 Sections:98 - sectname: __text99 segname: __TEXT100 addr: 0x100003F94101 size: 36102 offset: 0x3F94103 align: 2104 reloff: 0x0105 nreloc: 0106 flags: 0x80000400107 reserved1: 0x0108 reserved2: 0x0109 reserved3: 0x0110 content: FF8300D1E80300AA00008052FF1F00B9E81B00B9E10B00F9E20700F9FF830091C0035FD6111 - sectname: __unwind_info112 segname: __TEXT113 addr: 0x100003FB8114 size: 72115 offset: 0x3FB8116 align: 2117 reloff: 0x0118 nreloc: 0119 flags: 0x0120 reserved1: 0x0121 reserved2: 0x0122 reserved3: 0x0123 content: 010000001C000000000000001C000000000000001C00000002000000943F00003400000034000000B93F00000000000034000000030000000C000100100001000000000000200002124 - cmd: LC_SEGMENT_64125 cmdsize: 72126 segname: __LINKEDIT127 vmaddr: 4294983680128 vmsize: 16384129 fileoff: 16384130 filesize: 674131 maxprot: 1132 initprot: 1133 nsects: 0134 flags: 0135 - cmd: LC_DYLD_CHAINED_FIXUPS136 cmdsize: 16137 dataoff: 16384138 datasize: 56139 - cmd: LC_DYLD_EXPORTS_TRIE140 cmdsize: 16141 dataoff: 16440142 datasize: 48143 - cmd: LC_SYMTAB144 cmdsize: 24145 symoff: 16496146 nsyms: 10147 stroff: 16656148 strsize: 128149 - cmd: LC_DYSYMTAB150 cmdsize: 80151 ilocalsym: 0152 nlocalsym: 8153 iextdefsym: 8154 nextdefsym: 2155 iundefsym: 10156 nundefsym: 0157 tocoff: 0158 ntoc: 0159 modtaboff: 0160 nmodtab: 0161 extrefsymoff: 0162 nextrefsyms: 0163 indirectsymoff: 0164 nindirectsyms: 0165 extreloff: 0166 nextrel: 0167 locreloff: 0168 nlocrel: 0169 - cmd: LC_LOAD_DYLINKER170 cmdsize: 32171 name: 12172 Content: '/usr/lib/dyld'173 ZeroPadBytes: 7174 - cmd: LC_UUID175 cmdsize: 24176 uuid: 1EECD2B8-16EA-3FEC-AB3C-F46139DBD0E2177 - cmd: LC_BUILD_VERSION178 cmdsize: 32179 platform: 1180 minos: 786432181 sdk: 786432182 ntools: 1183 Tools:184 - tool: 3185 version: 46596096186 - cmd: LC_SOURCE_VERSION187 cmdsize: 16188 version: 0189 - cmd: LC_MAIN190 cmdsize: 24191 entryoff: 16276192 stacksize: 0193 - cmd: LC_LOAD_DYLIB194 cmdsize: 48195 dylib:196 name: 24197 timestamp: 2198 current_version: 78643968199 compatibility_version: 65536200 Content: '/usr/lib/libc++.1.dylib'201 ZeroPadBytes: 1202 - cmd: LC_LOAD_DYLIB203 cmdsize: 56204 dylib:205 name: 24206 timestamp: 2207 current_version: 85917696208 compatibility_version: 65536209 Content: '/usr/lib/libSystem.B.dylib'210 ZeroPadBytes: 6211 - cmd: LC_FUNCTION_STARTS212 cmdsize: 16213 dataoff: 16488214 datasize: 8215 - cmd: LC_DATA_IN_CODE216 cmdsize: 16217 dataoff: 16496218 datasize: 0219 - cmd: LC_CODE_SIGNATURE220 cmdsize: 16221 dataoff: 16784222 datasize: 274223LinkEditData:224 NameList:225 - n_strx: 28226 n_type: 0x64227 n_sect: 0228 n_desc: 0229 n_value: 0230 - n_strx: 64231 n_type: 0x64232 n_sect: 0233 n_desc: 0234 n_value: 0235 - n_strx: 73236 n_type: 0x66237 n_sect: 0238 n_desc: 1239 n_value: 1639532873240 - n_strx: 1241 n_type: 0x2E242 n_sect: 1243 n_desc: 0244 n_value: 4294983572245 - n_strx: 115246 n_type: 0x24247 n_sect: 1248 n_desc: 0249 n_value: 4294983572250 - n_strx: 1251 n_type: 0x24252 n_sect: 0253 n_desc: 0254 n_value: 36255 - n_strx: 1256 n_type: 0x4E257 n_sect: 1258 n_desc: 0259 n_value: 36260 - n_strx: 1261 n_type: 0x64262 n_sect: 1263 n_desc: 0264 n_value: 0265 - n_strx: 2266 n_type: 0xF267 n_sect: 1268 n_desc: 16269 n_value: 4294967296270 - n_strx: 22271 n_type: 0xF272 n_sect: 1273 n_desc: 0274 n_value: 4294983572275 StringTable:276 - ' '277 - __mh_execute_header278 - _main279 - '/Users/gclayton/Documents/src/args/'280 - main.cpp281 - '/Users/gclayton/Documents/src/args/main.o'282 - _main283 - ''284 - ''285 - ''286 - ''287 - ''288 - ''289 - ''290...291)");292 293 ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());294 auto module_sp = std::make_shared<Module>(ExpectedFile->moduleSpec());295 ObjectFile *objfile = module_sp->GetObjectFile();296 ASSERT_NE(objfile, nullptr);297 298 // Test encoding and decoding an empty symbol table.299 Symtab symtab(objfile);300 symtab.PreloadSymbols();301 EncodeDecode(symtab);302 303 // Now encode and decode an actual symbol table from our yaml.304 Symtab *module_symtab = module_sp->GetSymtab();305 ASSERT_NE(module_symtab, nullptr);306 module_symtab->PreloadSymbols();307 EncodeDecode(*module_symtab);308}309 310TEST_F(SymtabTest, TestDecodeCStringMaps) {311 // Symbol tables save out the symbols, but they also save out the symbol table312 // name indexes. These name indexes are a map of sorted ConstString + T pairs313 // and when they are decoded from a file, they are no longer sorted since314 // ConstString objects can be sorted by "const char *" and the order in which315 // these strings are created won't be the same in a new process. We need to316 // ensure these name lookups happen correctly when we load the name indexes,317 // so this test loads a symbol table from a cache file from318 // "lldb/unittests/Symbol/Inputs/indexnames-symtab-cache" and make sure we319 // can correctly lookup each of the names in the symbol table.320 auto ExpectedFile = TestFile::fromYaml(R"(321--- !mach-o322FileHeader:323 magic: 0xFEEDFACF324 cputype: 0x100000C325 cpusubtype: 0x0326 filetype: 0x2327 ncmds: 16328 sizeofcmds: 744329 flags: 0x200085330 reserved: 0x0331LoadCommands:332 - cmd: LC_SEGMENT_64333 cmdsize: 72334 segname: __PAGEZERO335 vmaddr: 0336 vmsize: 4294967296337 fileoff: 0338 filesize: 0339 maxprot: 0340 initprot: 0341 nsects: 0342 flags: 0343 - cmd: LC_SEGMENT_64344 cmdsize: 232345 segname: __TEXT346 vmaddr: 4294967296347 vmsize: 16384348 fileoff: 0349 filesize: 16384350 maxprot: 5351 initprot: 5352 nsects: 2353 flags: 0354 Sections:355 - sectname: __text356 segname: __TEXT357 addr: 0x100003F64358 size: 76359 offset: 0x3F64360 align: 2361 reloff: 0x0362 nreloc: 0363 flags: 0x80000400364 reserved1: 0x0365 reserved2: 0x0366 reserved3: 0x0367 content: 80018052C0035FD6E0028052C0035FD640048052C0035FD6FF8300D1FD7B01A9FD43009108008052E80B00B9BFC31FB8F4FFFF97F5FFFF97F6FFFF97E00B40B9FD7B41A9FF830091C0035FD6368 - sectname: __unwind_info369 segname: __TEXT370 addr: 0x100003FB0371 size: 80372 offset: 0x3FB0373 align: 2374 reloff: 0x0375 nreloc: 0376 flags: 0x0377 reserved1: 0x0378 reserved2: 0x0379 reserved3: 0x0380 content: 010000001C000000000000001C000000000000001C00000002000000643F00003400000034000000B13F00000000000034000000030000000C0002001400020000000001180000000000000400000002381 - cmd: LC_SEGMENT_64382 cmdsize: 72383 segname: __LINKEDIT384 vmaddr: 4294983680385 vmsize: 16384386 fileoff: 16384387 filesize: 994388 maxprot: 1389 initprot: 1390 nsects: 0391 flags: 0392 - cmd: LC_DYLD_CHAINED_FIXUPS393 cmdsize: 16394 dataoff: 16384395 datasize: 56396 - cmd: LC_DYLD_EXPORTS_TRIE397 cmdsize: 16398 dataoff: 16440399 datasize: 80400 - cmd: LC_SYMTAB401 cmdsize: 24402 symoff: 16528403 nsyms: 25404 stroff: 16928405 strsize: 176406 - cmd: LC_DYSYMTAB407 cmdsize: 80408 ilocalsym: 0409 nlocalsym: 20410 iextdefsym: 20411 nextdefsym: 5412 iundefsym: 25413 nundefsym: 0414 tocoff: 0415 ntoc: 0416 modtaboff: 0417 nmodtab: 0418 extrefsymoff: 0419 nextrefsyms: 0420 indirectsymoff: 0421 nindirectsyms: 0422 extreloff: 0423 nextrel: 0424 locreloff: 0425 nlocrel: 0426 - cmd: LC_LOAD_DYLINKER427 cmdsize: 32428 name: 12429 Content: '/usr/lib/dyld'430 ZeroPadBytes: 7431 - cmd: LC_UUID432 cmdsize: 24433 uuid: 3E94866E-0D1A-39BD-975B-64E8F1FDBAAE434 - cmd: LC_BUILD_VERSION435 cmdsize: 32436 platform: 1437 minos: 786432438 sdk: 787200439 ntools: 1440 Tools:441 - tool: 3442 version: 49938432443 - cmd: LC_SOURCE_VERSION444 cmdsize: 16445 version: 0446 - cmd: LC_MAIN447 cmdsize: 24448 entryoff: 16252449 stacksize: 0450 - cmd: LC_LOAD_DYLIB451 cmdsize: 56452 dylib:453 name: 24454 timestamp: 2455 current_version: 85943299456 compatibility_version: 65536457 Content: '/usr/lib/libSystem.B.dylib'458 ZeroPadBytes: 6459 - cmd: LC_FUNCTION_STARTS460 cmdsize: 16461 dataoff: 16520462 datasize: 8463 - cmd: LC_DATA_IN_CODE464 cmdsize: 16465 dataoff: 16528466 datasize: 0467 - cmd: LC_CODE_SIGNATURE468 cmdsize: 16469 dataoff: 17104470 datasize: 274471LinkEditData:472 NameList:473 - n_strx: 43474 n_type: 0x64475 n_sect: 0476 n_desc: 0477 n_value: 0478 - n_strx: 91479 n_type: 0x64480 n_sect: 0481 n_desc: 0482 n_value: 0483 - n_strx: 98484 n_type: 0x66485 n_sect: 0486 n_desc: 1487 n_value: 1651098491488 - n_strx: 1489 n_type: 0x2E490 n_sect: 1491 n_desc: 0492 n_value: 4294983524493 - n_strx: 152494 n_type: 0x24495 n_sect: 1496 n_desc: 0497 n_value: 4294983524498 - n_strx: 1499 n_type: 0x24500 n_sect: 0501 n_desc: 0502 n_value: 8503 - n_strx: 1504 n_type: 0x4E505 n_sect: 1506 n_desc: 0507 n_value: 8508 - n_strx: 1509 n_type: 0x2E510 n_sect: 1511 n_desc: 0512 n_value: 4294983532513 - n_strx: 157514 n_type: 0x24515 n_sect: 1516 n_desc: 0517 n_value: 4294983532518 - n_strx: 1519 n_type: 0x24520 n_sect: 0521 n_desc: 0522 n_value: 8523 - n_strx: 1524 n_type: 0x4E525 n_sect: 1526 n_desc: 0527 n_value: 8528 - n_strx: 1529 n_type: 0x2E530 n_sect: 1531 n_desc: 0532 n_value: 4294983540533 - n_strx: 162534 n_type: 0x24535 n_sect: 1536 n_desc: 0537 n_value: 4294983540538 - n_strx: 1539 n_type: 0x24540 n_sect: 0541 n_desc: 0542 n_value: 8543 - n_strx: 1544 n_type: 0x4E545 n_sect: 1546 n_desc: 0547 n_value: 8548 - n_strx: 1549 n_type: 0x2E550 n_sect: 1551 n_desc: 0552 n_value: 4294983548553 - n_strx: 167554 n_type: 0x24555 n_sect: 1556 n_desc: 0557 n_value: 4294983548558 - n_strx: 1559 n_type: 0x24560 n_sect: 0561 n_desc: 0562 n_value: 52563 - n_strx: 1564 n_type: 0x4E565 n_sect: 1566 n_desc: 0567 n_value: 52568 - n_strx: 1569 n_type: 0x64570 n_sect: 1571 n_desc: 0572 n_value: 0573 - n_strx: 2574 n_type: 0xF575 n_sect: 1576 n_desc: 16577 n_value: 4294967296578 - n_strx: 22579 n_type: 0xF580 n_sect: 1581 n_desc: 0582 n_value: 4294983532583 - n_strx: 27584 n_type: 0xF585 n_sect: 1586 n_desc: 0587 n_value: 4294983540588 - n_strx: 32589 n_type: 0xF590 n_sect: 1591 n_desc: 0592 n_value: 4294983524593 - n_strx: 37594 n_type: 0xF595 n_sect: 1596 n_desc: 0597 n_value: 4294983548598 StringTable:599 - ' '600 - __mh_execute_header601 - _bar602 - _baz603 - _foo604 - _main605 - '/Users/gclayton/Documents/objfiles/index-names/'606 - main.c607 - '/Users/gclayton/Documents/objfiles/index-names/main.o'608 - _foo609 - _bar610 - _baz611 - _main612 - ''613 - ''614 - ''615 FunctionStarts: [ 0x3F64, 0x3F6C, 0x3F74, 0x3F7C ]616...617)");618 // This data was taken from a hex dump of the object file from the above yaml619 // and hexdumped so we can load the cache data in this test.620 const uint8_t symtab_cache_bytes[] = {621 0x01, 0x10, 0x3e, 0x94, 0x86, 0x6e, 0x0d, 0x1a,622 0x39, 0xbd, 0x97, 0x5b, 0x64, 0xe8, 0xf1, 0xfd,623 0xba, 0xae, 0xff, 0x53, 0x54, 0x41, 0x42, 0x91,624 0x00, 0x00, 0x00, 0x00, 0x2f, 0x55, 0x73, 0x65,625 0x72, 0x73, 0x2f, 0x67, 0x63, 0x6c, 0x61, 0x79,626 0x74, 0x6f, 0x6e, 0x2f, 0x44, 0x6f, 0x63, 0x75,627 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x2f, 0x6f, 0x62,628 0x6a, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2f, 0x69,629 0x6e, 0x64, 0x65, 0x78, 0x2d, 0x6e, 0x61, 0x6d,630 0x65, 0x73, 0x2f, 0x6d, 0x61, 0x69, 0x6e, 0x2e,631 0x63, 0x00, 0x2f, 0x55, 0x73, 0x65, 0x72, 0x73,632 0x2f, 0x67, 0x63, 0x6c, 0x61, 0x79, 0x74, 0x6f,633 0x6e, 0x2f, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65,634 0x6e, 0x74, 0x73, 0x2f, 0x6f, 0x62, 0x6a, 0x66,635 0x69, 0x6c, 0x65, 0x73, 0x2f, 0x69, 0x6e, 0x64,636 0x65, 0x78, 0x2d, 0x6e, 0x61, 0x6d, 0x65, 0x73,637 0x2f, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x6f, 0x00,638 0x66, 0x6f, 0x6f, 0x00, 0x62, 0x61, 0x72, 0x00,639 0x62, 0x61, 0x7a, 0x00, 0x6d, 0x61, 0x69, 0x6e,640 0x00, 0x5f, 0x6d, 0x68, 0x5f, 0x65, 0x78, 0x65,641 0x63, 0x75, 0x74, 0x65, 0x5f, 0x68, 0x65, 0x61,642 0x64, 0x65, 0x72, 0x00, 0x53, 0x59, 0x4d, 0x42,643 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,644 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x2a,645 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,646 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,647 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,648 0x64, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,649 0x0a, 0x20, 0x01, 0x37, 0x00, 0x00, 0x00, 0x00,650 0x7b, 0xc3, 0x69, 0x62, 0x00, 0x00, 0x00, 0x00,651 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,652 0x01, 0x00, 0x66, 0x00, 0x04, 0x00, 0x00, 0x00,653 0x00, 0x00, 0x02, 0x32, 0x01, 0x6d, 0x00, 0x00,654 0x00, 0x01, 0x64, 0x3f, 0x00, 0x00, 0x01, 0x00,655 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,656 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x08, 0x00,657 0x00, 0x00, 0x00, 0x00, 0x02, 0x32, 0x01, 0x71,658 0x00, 0x00, 0x00, 0x01, 0x6c, 0x3f, 0x00, 0x00,659 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,660 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00,661 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x32,662 0x01, 0x75, 0x00, 0x00, 0x00, 0x01, 0x74, 0x3f,663 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00,664 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,665 0x0f, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,666 0x02, 0x32, 0x01, 0x79, 0x00, 0x00, 0x00, 0x01,667 0x7c, 0x3f, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,668 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,669 0x00, 0x00, 0x0f, 0x00, 0x14, 0x00, 0x00, 0x00,670 0x00, 0x00, 0x04, 0x12, 0x02, 0x7e, 0x00, 0x00,671 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,672 0x00, 0x00, 0x64, 0x3f, 0x00, 0x00, 0x00, 0x00,673 0x00, 0x00, 0x10, 0x00, 0x0f, 0x00, 0x01, 0x00,674 0x43, 0x4d, 0x41, 0x50, 0x07, 0x00, 0x00, 0x00,675 0x6d, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,676 0x75, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,677 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,678 0x79, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,679 0x37, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,680 0x71, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,681 0x7e, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00682 };683 684 ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());685 auto module_sp = std::make_shared<Module>(ExpectedFile->moduleSpec());686 ObjectFile *objfile = module_sp->GetObjectFile();687 ASSERT_NE(objfile, nullptr);688 689 // Test encoding and decoding an empty symbol table.690 DataExtractor data(symtab_cache_bytes, sizeof(symtab_cache_bytes),691 eByteOrderLittle, 8);692 Symtab symtab(objfile);693 offset_t data_offset = 0;694 bool uuid_mismatch = false; // Gets set to true if signature doesn't match.695 const bool success = symtab.Decode(data, &data_offset, uuid_mismatch);696 ASSERT_EQ(success, true);697 ASSERT_EQ(uuid_mismatch, false);698 699 // Now make sure that name lookup works for all symbols. This indicates that700 // the Symtab::NameToIndexMap was decoded correctly and works as expected.701 Symbol *symbol = nullptr;702 symbol = symtab.FindFirstSymbolWithNameAndType(ConstString("main"),703 eSymbolTypeCode,704 Symtab::eDebugAny,705 Symtab::eVisibilityAny);706 ASSERT_NE(symbol, nullptr);707 symbol = symtab.FindFirstSymbolWithNameAndType(ConstString("foo"),708 eSymbolTypeCode,709 Symtab::eDebugAny,710 Symtab::eVisibilityAny);711 ASSERT_NE(symbol, nullptr);712 symbol = symtab.FindFirstSymbolWithNameAndType(ConstString("bar"),713 eSymbolTypeCode,714 Symtab::eDebugAny,715 Symtab::eVisibilityAny);716 ASSERT_NE(symbol, nullptr);717 symbol = symtab.FindFirstSymbolWithNameAndType(ConstString("baz"),718 eSymbolTypeCode,719 Symtab::eDebugAny,720 Symtab::eVisibilityAny);721 ASSERT_NE(symbol, nullptr);722}723 724TEST_F(SymtabTest, TestSymbolFileCreatedOnDemand) {725 auto ExpectedFile = TestFile::fromYaml(R"(726--- !ELF727FileHeader:728 Class: ELFCLASS64729 Data: ELFDATA2LSB730 Type: ET_EXEC731 Machine: EM_X86_64732 Entry: 0x0000000000400180733Sections:734 - Name: .text735 Type: SHT_PROGBITS736 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]737 Address: 0x0000000000400180738 AddressAlign: 0x0000000000000010739 Content: 554889E58B042500106000890425041060005DC3740Symbols:741 - Name: _start742 Type: STT_FUNC743 Section: .text744 Value: 0x0000000000400180745 Size: 0x0000000000000014746 Binding: STB_GLOBAL747...748)");749 ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());750 auto module_sp = std::make_shared<Module>(ExpectedFile->moduleSpec());751 752 // The symbol file should not be created by default.753 Symtab *module_symtab = module_sp->GetSymtab(/*can_create=*/false);754 ASSERT_EQ(module_symtab, nullptr);755 756 // But it should be created on demand.757 module_symtab = module_sp->GetSymtab(/*can_create=*/true);758 ASSERT_NE(module_symtab, nullptr);759 760 // And we should be able to get it again once it has been created.761 Symtab *cached_module_symtab = module_sp->GetSymtab(/*can_create=*/false);762 ASSERT_EQ(module_symtab, cached_module_symtab);763}764 765TEST_F(SymtabTest, TestSymbolTableCreatedOnDemand) {766 const char *yamldata = R"(767--- !ELF768FileHeader:769 Class: ELFCLASS64770 Data: ELFDATA2LSB771 Type: ET_EXEC772 Machine: EM_386773DWARF:774 debug_abbrev:775 - Table:776 - Code: 0x00000001777 Tag: DW_TAG_compile_unit778 Children: DW_CHILDREN_no779 Attributes:780 - Attribute: DW_AT_addr_base781 Form: DW_FORM_sec_offset782 debug_info:783 - Version: 5784 AddrSize: 4785 UnitType: DW_UT_compile786 Entries:787 - AbbrCode: 0x00000001788 Values:789 - Value: 0x8 # Offset of the first Address past the header790 - AbbrCode: 0x0791 debug_addr:792 - Version: 5793 AddressSize: 4794 Entries:795 - Address: 0x1234796 - Address: 0x5678797 debug_line:798 - Length: 42799 Version: 2800 PrologueLength: 36801 MinInstLength: 1802 DefaultIsStmt: 1803 LineBase: 251804 LineRange: 14805 OpcodeBase: 13806 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]807 IncludeDirs:808 - '/tmp'809 Files:810 - Name: main.cpp811 DirIdx: 1812 ModTime: 0813 Length: 0814)";815 llvm::Expected<TestFile> file = TestFile::fromYaml(yamldata);816 EXPECT_THAT_EXPECTED(file, llvm::Succeeded());817 auto module_sp = std::make_shared<Module>(file->moduleSpec());818 819 SymbolFile *symbol_file = module_sp->GetSymbolFile();820 // At this point, the symbol table is not created. This is because the above821 // yaml data contains the necessary sections in order for822 // SymbolFileDWARF::CalculateAbilities() to identify all abilities, saving it823 // from calling SymbolFileDWARFDebugMap::CalculateAbilities(), which824 // eventually loads the symbol table, which we don't want.825 826 // The symbol table should not be created if asked not to.827 Symtab *symtab = symbol_file->GetSymtab(/*can_create=*/false);828 ASSERT_EQ(symtab, nullptr);829 830 // But it should be created on demand.831 symtab = symbol_file->GetSymtab(/*can_create=*/true);832 ASSERT_NE(symtab, nullptr);833 834 // And we should be able to get it again once it has been created.835 Symtab *cached_symtab = symbol_file->GetSymtab(/*can_create=*/false);836 ASSERT_EQ(symtab, cached_symtab);837}838