1 //===-- ObjectFileMachO.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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/StringRef.h" 10 11 #include "Plugins/Process/Utility/RegisterContextDarwin_arm.h" 12 #include "Plugins/Process/Utility/RegisterContextDarwin_arm64.h" 13 #include "Plugins/Process/Utility/RegisterContextDarwin_i386.h" 14 #include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h" 15 #include "lldb/Core/Debugger.h" 16 #include "lldb/Core/FileSpecList.h" 17 #include "lldb/Core/Module.h" 18 #include "lldb/Core/ModuleSpec.h" 19 #include "lldb/Core/PluginManager.h" 20 #include "lldb/Core/Progress.h" 21 #include "lldb/Core/Section.h" 22 #include "lldb/Core/StreamFile.h" 23 #include "lldb/Host/Host.h" 24 #include "lldb/Host/SafeMachO.h" 25 #include "lldb/Symbol/DWARFCallFrameInfo.h" 26 #include "lldb/Symbol/ObjectFile.h" 27 #include "lldb/Target/DynamicLoader.h" 28 #include "lldb/Target/MemoryRegionInfo.h" 29 #include "lldb/Target/Platform.h" 30 #include "lldb/Target/Process.h" 31 #include "lldb/Target/SectionLoadList.h" 32 #include "lldb/Target/Target.h" 33 #include "lldb/Target/Thread.h" 34 #include "lldb/Target/ThreadList.h" 35 #include "lldb/Utility/ArchSpec.h" 36 #include "lldb/Utility/DataBuffer.h" 37 #include "lldb/Utility/FileSpec.h" 38 #include "lldb/Utility/Log.h" 39 #include "lldb/Utility/RangeMap.h" 40 #include "lldb/Utility/RegisterValue.h" 41 #include "lldb/Utility/Status.h" 42 #include "lldb/Utility/StreamString.h" 43 #include "lldb/Utility/Timer.h" 44 #include "lldb/Utility/UUID.h" 45 46 #include "llvm/ADT/DenseSet.h" 47 #include "llvm/Support/FormatVariadic.h" 48 #include "llvm/Support/MemoryBuffer.h" 49 50 #include "ObjectFileMachO.h" 51 52 #if defined(__APPLE__) 53 #include <TargetConditionals.h> 54 // GetLLDBSharedCacheUUID() needs to call dlsym() 55 #include <dlfcn.h> 56 #endif 57 58 #ifndef __APPLE__ 59 #include "Utility/UuidCompatibility.h" 60 #else 61 #include <uuid/uuid.h> 62 #endif 63 64 #include <memory> 65 66 #if LLVM_SUPPORT_XCODE_SIGNPOSTS 67 // Unfortunately the signpost header pulls in the system MachO header, too. 68 #undef CPU_TYPE_ARM 69 #undef CPU_TYPE_ARM64 70 #undef CPU_TYPE_ARM64_32 71 #undef CPU_TYPE_I386 72 #undef CPU_TYPE_X86_64 73 #undef MH_BINDATLOAD 74 #undef MH_BUNDLE 75 #undef MH_CIGAM 76 #undef MH_CIGAM_64 77 #undef MH_CORE 78 #undef MH_DSYM 79 #undef MH_DYLDLINK 80 #undef MH_DYLIB 81 #undef MH_DYLIB_STUB 82 #undef MH_DYLINKER 83 #undef MH_DYLINKER 84 #undef MH_EXECUTE 85 #undef MH_FVMLIB 86 #undef MH_INCRLINK 87 #undef MH_KEXT_BUNDLE 88 #undef MH_MAGIC 89 #undef MH_MAGIC_64 90 #undef MH_NOUNDEFS 91 #undef MH_OBJECT 92 #undef MH_OBJECT 93 #undef MH_PRELOAD 94 95 #undef LC_BUILD_VERSION 96 #undef LC_VERSION_MIN_MACOSX 97 #undef LC_VERSION_MIN_IPHONEOS 98 #undef LC_VERSION_MIN_TVOS 99 #undef LC_VERSION_MIN_WATCHOS 100 101 #undef PLATFORM_MACOS 102 #undef PLATFORM_MACCATALYST 103 #undef PLATFORM_IOS 104 #undef PLATFORM_IOSSIMULATOR 105 #undef PLATFORM_TVOS 106 #undef PLATFORM_TVOSSIMULATOR 107 #undef PLATFORM_WATCHOS 108 #undef PLATFORM_WATCHOSSIMULATOR 109 #endif 110 111 #define THUMB_ADDRESS_BIT_MASK 0xfffffffffffffffeull 112 using namespace lldb; 113 using namespace lldb_private; 114 using namespace llvm::MachO; 115 116 LLDB_PLUGIN_DEFINE(ObjectFileMachO) 117 118 // Some structure definitions needed for parsing the dyld shared cache files 119 // found on iOS devices. 120 121 struct lldb_copy_dyld_cache_header_v1 { 122 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc. 123 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info 124 uint32_t mappingCount; // number of dyld_cache_mapping_info entries 125 uint32_t imagesOffset; 126 uint32_t imagesCount; 127 uint64_t dyldBaseAddress; 128 uint64_t codeSignatureOffset; 129 uint64_t codeSignatureSize; 130 uint64_t slideInfoOffset; 131 uint64_t slideInfoSize; 132 uint64_t localSymbolsOffset; 133 uint64_t localSymbolsSize; 134 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 135 // and later 136 }; 137 138 struct lldb_copy_dyld_cache_mapping_info { 139 uint64_t address; 140 uint64_t size; 141 uint64_t fileOffset; 142 uint32_t maxProt; 143 uint32_t initProt; 144 }; 145 146 struct lldb_copy_dyld_cache_local_symbols_info { 147 uint32_t nlistOffset; 148 uint32_t nlistCount; 149 uint32_t stringsOffset; 150 uint32_t stringsSize; 151 uint32_t entriesOffset; 152 uint32_t entriesCount; 153 }; 154 struct lldb_copy_dyld_cache_local_symbols_entry { 155 uint32_t dylibOffset; 156 uint32_t nlistStartIndex; 157 uint32_t nlistCount; 158 }; 159 160 static void PrintRegisterValue(RegisterContext *reg_ctx, const char *name, 161 const char *alt_name, size_t reg_byte_size, 162 Stream &data) { 163 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name); 164 if (reg_info == nullptr) 165 reg_info = reg_ctx->GetRegisterInfoByName(alt_name); 166 if (reg_info) { 167 lldb_private::RegisterValue reg_value; 168 if (reg_ctx->ReadRegister(reg_info, reg_value)) { 169 if (reg_info->byte_size >= reg_byte_size) 170 data.Write(reg_value.GetBytes(), reg_byte_size); 171 else { 172 data.Write(reg_value.GetBytes(), reg_info->byte_size); 173 for (size_t i = 0, n = reg_byte_size - reg_info->byte_size; i < n; ++i) 174 data.PutChar(0); 175 } 176 return; 177 } 178 } 179 // Just write zeros if all else fails 180 for (size_t i = 0; i < reg_byte_size; ++i) 181 data.PutChar(0); 182 } 183 184 class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64 { 185 public: 186 RegisterContextDarwin_x86_64_Mach(lldb_private::Thread &thread, 187 const DataExtractor &data) 188 : RegisterContextDarwin_x86_64(thread, 0) { 189 SetRegisterDataFrom_LC_THREAD(data); 190 } 191 192 void InvalidateAllRegisters() override { 193 // Do nothing... registers are always valid... 194 } 195 196 void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) { 197 lldb::offset_t offset = 0; 198 SetError(GPRRegSet, Read, -1); 199 SetError(FPURegSet, Read, -1); 200 SetError(EXCRegSet, Read, -1); 201 bool done = false; 202 203 while (!done) { 204 int flavor = data.GetU32(&offset); 205 if (flavor == 0) 206 done = true; 207 else { 208 uint32_t i; 209 uint32_t count = data.GetU32(&offset); 210 switch (flavor) { 211 case GPRRegSet: 212 for (i = 0; i < count; ++i) 213 (&gpr.rax)[i] = data.GetU64(&offset); 214 SetError(GPRRegSet, Read, 0); 215 done = true; 216 217 break; 218 case FPURegSet: 219 // TODO: fill in FPU regs.... 220 // SetError (FPURegSet, Read, -1); 221 done = true; 222 223 break; 224 case EXCRegSet: 225 exc.trapno = data.GetU32(&offset); 226 exc.err = data.GetU32(&offset); 227 exc.faultvaddr = data.GetU64(&offset); 228 SetError(EXCRegSet, Read, 0); 229 done = true; 230 break; 231 case 7: 232 case 8: 233 case 9: 234 // fancy flavors that encapsulate of the above flavors... 235 break; 236 237 default: 238 done = true; 239 break; 240 } 241 } 242 } 243 } 244 245 static bool Create_LC_THREAD(Thread *thread, Stream &data) { 246 RegisterContextSP reg_ctx_sp(thread->GetRegisterContext()); 247 if (reg_ctx_sp) { 248 RegisterContext *reg_ctx = reg_ctx_sp.get(); 249 250 data.PutHex32(GPRRegSet); // Flavor 251 data.PutHex32(GPRWordCount); 252 PrintRegisterValue(reg_ctx, "rax", nullptr, 8, data); 253 PrintRegisterValue(reg_ctx, "rbx", nullptr, 8, data); 254 PrintRegisterValue(reg_ctx, "rcx", nullptr, 8, data); 255 PrintRegisterValue(reg_ctx, "rdx", nullptr, 8, data); 256 PrintRegisterValue(reg_ctx, "rdi", nullptr, 8, data); 257 PrintRegisterValue(reg_ctx, "rsi", nullptr, 8, data); 258 PrintRegisterValue(reg_ctx, "rbp", nullptr, 8, data); 259 PrintRegisterValue(reg_ctx, "rsp", nullptr, 8, data); 260 PrintRegisterValue(reg_ctx, "r8", nullptr, 8, data); 261 PrintRegisterValue(reg_ctx, "r9", nullptr, 8, data); 262 PrintRegisterValue(reg_ctx, "r10", nullptr, 8, data); 263 PrintRegisterValue(reg_ctx, "r11", nullptr, 8, data); 264 PrintRegisterValue(reg_ctx, "r12", nullptr, 8, data); 265 PrintRegisterValue(reg_ctx, "r13", nullptr, 8, data); 266 PrintRegisterValue(reg_ctx, "r14", nullptr, 8, data); 267 PrintRegisterValue(reg_ctx, "r15", nullptr, 8, data); 268 PrintRegisterValue(reg_ctx, "rip", nullptr, 8, data); 269 PrintRegisterValue(reg_ctx, "rflags", nullptr, 8, data); 270 PrintRegisterValue(reg_ctx, "cs", nullptr, 8, data); 271 PrintRegisterValue(reg_ctx, "fs", nullptr, 8, data); 272 PrintRegisterValue(reg_ctx, "gs", nullptr, 8, data); 273 274 // // Write out the FPU registers 275 // const size_t fpu_byte_size = sizeof(FPU); 276 // size_t bytes_written = 0; 277 // data.PutHex32 (FPURegSet); 278 // data.PutHex32 (fpu_byte_size/sizeof(uint64_t)); 279 // bytes_written += data.PutHex32(0); // uint32_t pad[0] 280 // bytes_written += data.PutHex32(0); // uint32_t pad[1] 281 // bytes_written += WriteRegister (reg_ctx, "fcw", "fctrl", 2, 282 // data); // uint16_t fcw; // "fctrl" 283 // bytes_written += WriteRegister (reg_ctx, "fsw" , "fstat", 2, 284 // data); // uint16_t fsw; // "fstat" 285 // bytes_written += WriteRegister (reg_ctx, "ftw" , "ftag", 1, 286 // data); // uint8_t ftw; // "ftag" 287 // bytes_written += data.PutHex8 (0); // uint8_t pad1; 288 // bytes_written += WriteRegister (reg_ctx, "fop" , NULL, 2, 289 // data); // uint16_t fop; // "fop" 290 // bytes_written += WriteRegister (reg_ctx, "fioff", "ip", 4, 291 // data); // uint32_t ip; // "fioff" 292 // bytes_written += WriteRegister (reg_ctx, "fiseg", NULL, 2, 293 // data); // uint16_t cs; // "fiseg" 294 // bytes_written += data.PutHex16 (0); // uint16_t pad2; 295 // bytes_written += WriteRegister (reg_ctx, "dp", "fooff" , 4, 296 // data); // uint32_t dp; // "fooff" 297 // bytes_written += WriteRegister (reg_ctx, "foseg", NULL, 2, 298 // data); // uint16_t ds; // "foseg" 299 // bytes_written += data.PutHex16 (0); // uint16_t pad3; 300 // bytes_written += WriteRegister (reg_ctx, "mxcsr", NULL, 4, 301 // data); // uint32_t mxcsr; 302 // bytes_written += WriteRegister (reg_ctx, "mxcsrmask", NULL, 303 // 4, data);// uint32_t mxcsrmask; 304 // bytes_written += WriteRegister (reg_ctx, "stmm0", NULL, 305 // sizeof(MMSReg), data); 306 // bytes_written += WriteRegister (reg_ctx, "stmm1", NULL, 307 // sizeof(MMSReg), data); 308 // bytes_written += WriteRegister (reg_ctx, "stmm2", NULL, 309 // sizeof(MMSReg), data); 310 // bytes_written += WriteRegister (reg_ctx, "stmm3", NULL, 311 // sizeof(MMSReg), data); 312 // bytes_written += WriteRegister (reg_ctx, "stmm4", NULL, 313 // sizeof(MMSReg), data); 314 // bytes_written += WriteRegister (reg_ctx, "stmm5", NULL, 315 // sizeof(MMSReg), data); 316 // bytes_written += WriteRegister (reg_ctx, "stmm6", NULL, 317 // sizeof(MMSReg), data); 318 // bytes_written += WriteRegister (reg_ctx, "stmm7", NULL, 319 // sizeof(MMSReg), data); 320 // bytes_written += WriteRegister (reg_ctx, "xmm0" , NULL, 321 // sizeof(XMMReg), data); 322 // bytes_written += WriteRegister (reg_ctx, "xmm1" , NULL, 323 // sizeof(XMMReg), data); 324 // bytes_written += WriteRegister (reg_ctx, "xmm2" , NULL, 325 // sizeof(XMMReg), data); 326 // bytes_written += WriteRegister (reg_ctx, "xmm3" , NULL, 327 // sizeof(XMMReg), data); 328 // bytes_written += WriteRegister (reg_ctx, "xmm4" , NULL, 329 // sizeof(XMMReg), data); 330 // bytes_written += WriteRegister (reg_ctx, "xmm5" , NULL, 331 // sizeof(XMMReg), data); 332 // bytes_written += WriteRegister (reg_ctx, "xmm6" , NULL, 333 // sizeof(XMMReg), data); 334 // bytes_written += WriteRegister (reg_ctx, "xmm7" , NULL, 335 // sizeof(XMMReg), data); 336 // bytes_written += WriteRegister (reg_ctx, "xmm8" , NULL, 337 // sizeof(XMMReg), data); 338 // bytes_written += WriteRegister (reg_ctx, "xmm9" , NULL, 339 // sizeof(XMMReg), data); 340 // bytes_written += WriteRegister (reg_ctx, "xmm10", NULL, 341 // sizeof(XMMReg), data); 342 // bytes_written += WriteRegister (reg_ctx, "xmm11", NULL, 343 // sizeof(XMMReg), data); 344 // bytes_written += WriteRegister (reg_ctx, "xmm12", NULL, 345 // sizeof(XMMReg), data); 346 // bytes_written += WriteRegister (reg_ctx, "xmm13", NULL, 347 // sizeof(XMMReg), data); 348 // bytes_written += WriteRegister (reg_ctx, "xmm14", NULL, 349 // sizeof(XMMReg), data); 350 // bytes_written += WriteRegister (reg_ctx, "xmm15", NULL, 351 // sizeof(XMMReg), data); 352 // 353 // // Fill rest with zeros 354 // for (size_t i=0, n = fpu_byte_size - bytes_written; i<n; ++ 355 // i) 356 // data.PutChar(0); 357 358 // Write out the EXC registers 359 data.PutHex32(EXCRegSet); 360 data.PutHex32(EXCWordCount); 361 PrintRegisterValue(reg_ctx, "trapno", nullptr, 4, data); 362 PrintRegisterValue(reg_ctx, "err", nullptr, 4, data); 363 PrintRegisterValue(reg_ctx, "faultvaddr", nullptr, 8, data); 364 return true; 365 } 366 return false; 367 } 368 369 protected: 370 int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return 0; } 371 372 int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return 0; } 373 374 int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return 0; } 375 376 int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override { 377 return 0; 378 } 379 380 int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override { 381 return 0; 382 } 383 384 int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override { 385 return 0; 386 } 387 }; 388 389 class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386 { 390 public: 391 RegisterContextDarwin_i386_Mach(lldb_private::Thread &thread, 392 const DataExtractor &data) 393 : RegisterContextDarwin_i386(thread, 0) { 394 SetRegisterDataFrom_LC_THREAD(data); 395 } 396 397 void InvalidateAllRegisters() override { 398 // Do nothing... registers are always valid... 399 } 400 401 void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) { 402 lldb::offset_t offset = 0; 403 SetError(GPRRegSet, Read, -1); 404 SetError(FPURegSet, Read, -1); 405 SetError(EXCRegSet, Read, -1); 406 bool done = false; 407 408 while (!done) { 409 int flavor = data.GetU32(&offset); 410 if (flavor == 0) 411 done = true; 412 else { 413 uint32_t i; 414 uint32_t count = data.GetU32(&offset); 415 switch (flavor) { 416 case GPRRegSet: 417 for (i = 0; i < count; ++i) 418 (&gpr.eax)[i] = data.GetU32(&offset); 419 SetError(GPRRegSet, Read, 0); 420 done = true; 421 422 break; 423 case FPURegSet: 424 // TODO: fill in FPU regs.... 425 // SetError (FPURegSet, Read, -1); 426 done = true; 427 428 break; 429 case EXCRegSet: 430 exc.trapno = data.GetU32(&offset); 431 exc.err = data.GetU32(&offset); 432 exc.faultvaddr = data.GetU32(&offset); 433 SetError(EXCRegSet, Read, 0); 434 done = true; 435 break; 436 case 7: 437 case 8: 438 case 9: 439 // fancy flavors that encapsulate of the above flavors... 440 break; 441 442 default: 443 done = true; 444 break; 445 } 446 } 447 } 448 } 449 450 static bool Create_LC_THREAD(Thread *thread, Stream &data) { 451 RegisterContextSP reg_ctx_sp(thread->GetRegisterContext()); 452 if (reg_ctx_sp) { 453 RegisterContext *reg_ctx = reg_ctx_sp.get(); 454 455 data.PutHex32(GPRRegSet); // Flavor 456 data.PutHex32(GPRWordCount); 457 PrintRegisterValue(reg_ctx, "eax", nullptr, 4, data); 458 PrintRegisterValue(reg_ctx, "ebx", nullptr, 4, data); 459 PrintRegisterValue(reg_ctx, "ecx", nullptr, 4, data); 460 PrintRegisterValue(reg_ctx, "edx", nullptr, 4, data); 461 PrintRegisterValue(reg_ctx, "edi", nullptr, 4, data); 462 PrintRegisterValue(reg_ctx, "esi", nullptr, 4, data); 463 PrintRegisterValue(reg_ctx, "ebp", nullptr, 4, data); 464 PrintRegisterValue(reg_ctx, "esp", nullptr, 4, data); 465 PrintRegisterValue(reg_ctx, "ss", nullptr, 4, data); 466 PrintRegisterValue(reg_ctx, "eflags", nullptr, 4, data); 467 PrintRegisterValue(reg_ctx, "eip", nullptr, 4, data); 468 PrintRegisterValue(reg_ctx, "cs", nullptr, 4, data); 469 PrintRegisterValue(reg_ctx, "ds", nullptr, 4, data); 470 PrintRegisterValue(reg_ctx, "es", nullptr, 4, data); 471 PrintRegisterValue(reg_ctx, "fs", nullptr, 4, data); 472 PrintRegisterValue(reg_ctx, "gs", nullptr, 4, data); 473 474 // Write out the EXC registers 475 data.PutHex32(EXCRegSet); 476 data.PutHex32(EXCWordCount); 477 PrintRegisterValue(reg_ctx, "trapno", nullptr, 4, data); 478 PrintRegisterValue(reg_ctx, "err", nullptr, 4, data); 479 PrintRegisterValue(reg_ctx, "faultvaddr", nullptr, 4, data); 480 return true; 481 } 482 return false; 483 } 484 485 protected: 486 int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return 0; } 487 488 int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return 0; } 489 490 int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return 0; } 491 492 int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override { 493 return 0; 494 } 495 496 int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override { 497 return 0; 498 } 499 500 int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override { 501 return 0; 502 } 503 }; 504 505 class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm { 506 public: 507 RegisterContextDarwin_arm_Mach(lldb_private::Thread &thread, 508 const DataExtractor &data) 509 : RegisterContextDarwin_arm(thread, 0) { 510 SetRegisterDataFrom_LC_THREAD(data); 511 } 512 513 void InvalidateAllRegisters() override { 514 // Do nothing... registers are always valid... 515 } 516 517 void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) { 518 lldb::offset_t offset = 0; 519 SetError(GPRRegSet, Read, -1); 520 SetError(FPURegSet, Read, -1); 521 SetError(EXCRegSet, Read, -1); 522 bool done = false; 523 524 while (!done) { 525 int flavor = data.GetU32(&offset); 526 uint32_t count = data.GetU32(&offset); 527 lldb::offset_t next_thread_state = offset + (count * 4); 528 switch (flavor) { 529 case GPRAltRegSet: 530 case GPRRegSet: 531 // On ARM, the CPSR register is also included in the count but it is 532 // not included in gpr.r so loop until (count-1). 533 for (uint32_t i = 0; i < (count - 1); ++i) { 534 gpr.r[i] = data.GetU32(&offset); 535 } 536 // Save cpsr explicitly. 537 gpr.cpsr = data.GetU32(&offset); 538 539 SetError(GPRRegSet, Read, 0); 540 offset = next_thread_state; 541 break; 542 543 case FPURegSet: { 544 uint8_t *fpu_reg_buf = (uint8_t *)&fpu.floats.s[0]; 545 const int fpu_reg_buf_size = sizeof(fpu.floats); 546 if (data.ExtractBytes(offset, fpu_reg_buf_size, eByteOrderLittle, 547 fpu_reg_buf) == fpu_reg_buf_size) { 548 offset += fpu_reg_buf_size; 549 fpu.fpscr = data.GetU32(&offset); 550 SetError(FPURegSet, Read, 0); 551 } else { 552 done = true; 553 } 554 } 555 offset = next_thread_state; 556 break; 557 558 case EXCRegSet: 559 if (count == 3) { 560 exc.exception = data.GetU32(&offset); 561 exc.fsr = data.GetU32(&offset); 562 exc.far = data.GetU32(&offset); 563 SetError(EXCRegSet, Read, 0); 564 } 565 done = true; 566 offset = next_thread_state; 567 break; 568 569 // Unknown register set flavor, stop trying to parse. 570 default: 571 done = true; 572 } 573 } 574 } 575 576 static bool Create_LC_THREAD(Thread *thread, Stream &data) { 577 RegisterContextSP reg_ctx_sp(thread->GetRegisterContext()); 578 if (reg_ctx_sp) { 579 RegisterContext *reg_ctx = reg_ctx_sp.get(); 580 581 data.PutHex32(GPRRegSet); // Flavor 582 data.PutHex32(GPRWordCount); 583 PrintRegisterValue(reg_ctx, "r0", nullptr, 4, data); 584 PrintRegisterValue(reg_ctx, "r1", nullptr, 4, data); 585 PrintRegisterValue(reg_ctx, "r2", nullptr, 4, data); 586 PrintRegisterValue(reg_ctx, "r3", nullptr, 4, data); 587 PrintRegisterValue(reg_ctx, "r4", nullptr, 4, data); 588 PrintRegisterValue(reg_ctx, "r5", nullptr, 4, data); 589 PrintRegisterValue(reg_ctx, "r6", nullptr, 4, data); 590 PrintRegisterValue(reg_ctx, "r7", nullptr, 4, data); 591 PrintRegisterValue(reg_ctx, "r8", nullptr, 4, data); 592 PrintRegisterValue(reg_ctx, "r9", nullptr, 4, data); 593 PrintRegisterValue(reg_ctx, "r10", nullptr, 4, data); 594 PrintRegisterValue(reg_ctx, "r11", nullptr, 4, data); 595 PrintRegisterValue(reg_ctx, "r12", nullptr, 4, data); 596 PrintRegisterValue(reg_ctx, "sp", nullptr, 4, data); 597 PrintRegisterValue(reg_ctx, "lr", nullptr, 4, data); 598 PrintRegisterValue(reg_ctx, "pc", nullptr, 4, data); 599 PrintRegisterValue(reg_ctx, "cpsr", nullptr, 4, data); 600 601 // Write out the EXC registers 602 // data.PutHex32 (EXCRegSet); 603 // data.PutHex32 (EXCWordCount); 604 // WriteRegister (reg_ctx, "exception", NULL, 4, data); 605 // WriteRegister (reg_ctx, "fsr", NULL, 4, data); 606 // WriteRegister (reg_ctx, "far", NULL, 4, data); 607 return true; 608 } 609 return false; 610 } 611 612 protected: 613 int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return -1; } 614 615 int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return -1; } 616 617 int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return -1; } 618 619 int DoReadDBG(lldb::tid_t tid, int flavor, DBG &dbg) override { return -1; } 620 621 int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override { 622 return 0; 623 } 624 625 int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override { 626 return 0; 627 } 628 629 int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override { 630 return 0; 631 } 632 633 int DoWriteDBG(lldb::tid_t tid, int flavor, const DBG &dbg) override { 634 return -1; 635 } 636 }; 637 638 class RegisterContextDarwin_arm64_Mach : public RegisterContextDarwin_arm64 { 639 public: 640 RegisterContextDarwin_arm64_Mach(lldb_private::Thread &thread, 641 const DataExtractor &data) 642 : RegisterContextDarwin_arm64(thread, 0) { 643 SetRegisterDataFrom_LC_THREAD(data); 644 } 645 646 void InvalidateAllRegisters() override { 647 // Do nothing... registers are always valid... 648 } 649 650 void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) { 651 lldb::offset_t offset = 0; 652 SetError(GPRRegSet, Read, -1); 653 SetError(FPURegSet, Read, -1); 654 SetError(EXCRegSet, Read, -1); 655 bool done = false; 656 while (!done) { 657 int flavor = data.GetU32(&offset); 658 uint32_t count = data.GetU32(&offset); 659 lldb::offset_t next_thread_state = offset + (count * 4); 660 switch (flavor) { 661 case GPRRegSet: 662 // x0-x29 + fp + lr + sp + pc (== 33 64-bit registers) plus cpsr (1 663 // 32-bit register) 664 if (count >= (33 * 2) + 1) { 665 for (uint32_t i = 0; i < 29; ++i) 666 gpr.x[i] = data.GetU64(&offset); 667 gpr.fp = data.GetU64(&offset); 668 gpr.lr = data.GetU64(&offset); 669 gpr.sp = data.GetU64(&offset); 670 gpr.pc = data.GetU64(&offset); 671 gpr.cpsr = data.GetU32(&offset); 672 SetError(GPRRegSet, Read, 0); 673 } 674 offset = next_thread_state; 675 break; 676 case FPURegSet: { 677 uint8_t *fpu_reg_buf = (uint8_t *)&fpu.v[0]; 678 const int fpu_reg_buf_size = sizeof(fpu); 679 if (fpu_reg_buf_size == count * sizeof(uint32_t) && 680 data.ExtractBytes(offset, fpu_reg_buf_size, eByteOrderLittle, 681 fpu_reg_buf) == fpu_reg_buf_size) { 682 SetError(FPURegSet, Read, 0); 683 } else { 684 done = true; 685 } 686 } 687 offset = next_thread_state; 688 break; 689 case EXCRegSet: 690 if (count == 4) { 691 exc.far = data.GetU64(&offset); 692 exc.esr = data.GetU32(&offset); 693 exc.exception = data.GetU32(&offset); 694 SetError(EXCRegSet, Read, 0); 695 } 696 offset = next_thread_state; 697 break; 698 default: 699 done = true; 700 break; 701 } 702 } 703 } 704 705 static bool Create_LC_THREAD(Thread *thread, Stream &data) { 706 RegisterContextSP reg_ctx_sp(thread->GetRegisterContext()); 707 if (reg_ctx_sp) { 708 RegisterContext *reg_ctx = reg_ctx_sp.get(); 709 710 data.PutHex32(GPRRegSet); // Flavor 711 data.PutHex32(GPRWordCount); 712 PrintRegisterValue(reg_ctx, "x0", nullptr, 8, data); 713 PrintRegisterValue(reg_ctx, "x1", nullptr, 8, data); 714 PrintRegisterValue(reg_ctx, "x2", nullptr, 8, data); 715 PrintRegisterValue(reg_ctx, "x3", nullptr, 8, data); 716 PrintRegisterValue(reg_ctx, "x4", nullptr, 8, data); 717 PrintRegisterValue(reg_ctx, "x5", nullptr, 8, data); 718 PrintRegisterValue(reg_ctx, "x6", nullptr, 8, data); 719 PrintRegisterValue(reg_ctx, "x7", nullptr, 8, data); 720 PrintRegisterValue(reg_ctx, "x8", nullptr, 8, data); 721 PrintRegisterValue(reg_ctx, "x9", nullptr, 8, data); 722 PrintRegisterValue(reg_ctx, "x10", nullptr, 8, data); 723 PrintRegisterValue(reg_ctx, "x11", nullptr, 8, data); 724 PrintRegisterValue(reg_ctx, "x12", nullptr, 8, data); 725 PrintRegisterValue(reg_ctx, "x13", nullptr, 8, data); 726 PrintRegisterValue(reg_ctx, "x14", nullptr, 8, data); 727 PrintRegisterValue(reg_ctx, "x15", nullptr, 8, data); 728 PrintRegisterValue(reg_ctx, "x16", nullptr, 8, data); 729 PrintRegisterValue(reg_ctx, "x17", nullptr, 8, data); 730 PrintRegisterValue(reg_ctx, "x18", nullptr, 8, data); 731 PrintRegisterValue(reg_ctx, "x19", nullptr, 8, data); 732 PrintRegisterValue(reg_ctx, "x20", nullptr, 8, data); 733 PrintRegisterValue(reg_ctx, "x21", nullptr, 8, data); 734 PrintRegisterValue(reg_ctx, "x22", nullptr, 8, data); 735 PrintRegisterValue(reg_ctx, "x23", nullptr, 8, data); 736 PrintRegisterValue(reg_ctx, "x24", nullptr, 8, data); 737 PrintRegisterValue(reg_ctx, "x25", nullptr, 8, data); 738 PrintRegisterValue(reg_ctx, "x26", nullptr, 8, data); 739 PrintRegisterValue(reg_ctx, "x27", nullptr, 8, data); 740 PrintRegisterValue(reg_ctx, "x28", nullptr, 8, data); 741 PrintRegisterValue(reg_ctx, "fp", nullptr, 8, data); 742 PrintRegisterValue(reg_ctx, "lr", nullptr, 8, data); 743 PrintRegisterValue(reg_ctx, "sp", nullptr, 8, data); 744 PrintRegisterValue(reg_ctx, "pc", nullptr, 8, data); 745 PrintRegisterValue(reg_ctx, "cpsr", nullptr, 4, data); 746 747 // Write out the EXC registers 748 // data.PutHex32 (EXCRegSet); 749 // data.PutHex32 (EXCWordCount); 750 // WriteRegister (reg_ctx, "far", NULL, 8, data); 751 // WriteRegister (reg_ctx, "esr", NULL, 4, data); 752 // WriteRegister (reg_ctx, "exception", NULL, 4, data); 753 return true; 754 } 755 return false; 756 } 757 758 protected: 759 int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return -1; } 760 761 int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return -1; } 762 763 int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return -1; } 764 765 int DoReadDBG(lldb::tid_t tid, int flavor, DBG &dbg) override { return -1; } 766 767 int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override { 768 return 0; 769 } 770 771 int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override { 772 return 0; 773 } 774 775 int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override { 776 return 0; 777 } 778 779 int DoWriteDBG(lldb::tid_t tid, int flavor, const DBG &dbg) override { 780 return -1; 781 } 782 }; 783 784 static uint32_t MachHeaderSizeFromMagic(uint32_t magic) { 785 switch (magic) { 786 case MH_MAGIC: 787 case MH_CIGAM: 788 return sizeof(struct llvm::MachO::mach_header); 789 790 case MH_MAGIC_64: 791 case MH_CIGAM_64: 792 return sizeof(struct llvm::MachO::mach_header_64); 793 break; 794 795 default: 796 break; 797 } 798 return 0; 799 } 800 801 #define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008 802 803 char ObjectFileMachO::ID; 804 805 void ObjectFileMachO::Initialize() { 806 PluginManager::RegisterPlugin( 807 GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance, 808 CreateMemoryInstance, GetModuleSpecifications, SaveCore); 809 } 810 811 void ObjectFileMachO::Terminate() { 812 PluginManager::UnregisterPlugin(CreateInstance); 813 } 814 815 lldb_private::ConstString ObjectFileMachO::GetPluginNameStatic() { 816 static ConstString g_name("mach-o"); 817 return g_name; 818 } 819 820 const char *ObjectFileMachO::GetPluginDescriptionStatic() { 821 return "Mach-o object file reader (32 and 64 bit)"; 822 } 823 824 ObjectFile *ObjectFileMachO::CreateInstance(const lldb::ModuleSP &module_sp, 825 DataBufferSP &data_sp, 826 lldb::offset_t data_offset, 827 const FileSpec *file, 828 lldb::offset_t file_offset, 829 lldb::offset_t length) { 830 if (!data_sp) { 831 data_sp = MapFileData(*file, length, file_offset); 832 if (!data_sp) 833 return nullptr; 834 data_offset = 0; 835 } 836 837 if (!ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length)) 838 return nullptr; 839 840 // Update the data to contain the entire file if it doesn't already 841 if (data_sp->GetByteSize() < length) { 842 data_sp = MapFileData(*file, length, file_offset); 843 if (!data_sp) 844 return nullptr; 845 data_offset = 0; 846 } 847 auto objfile_up = std::make_unique<ObjectFileMachO>( 848 module_sp, data_sp, data_offset, file, file_offset, length); 849 if (!objfile_up || !objfile_up->ParseHeader()) 850 return nullptr; 851 852 return objfile_up.release(); 853 } 854 855 ObjectFile *ObjectFileMachO::CreateMemoryInstance( 856 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp, 857 const ProcessSP &process_sp, lldb::addr_t header_addr) { 858 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) { 859 std::unique_ptr<ObjectFile> objfile_up( 860 new ObjectFileMachO(module_sp, data_sp, process_sp, header_addr)); 861 if (objfile_up.get() && objfile_up->ParseHeader()) 862 return objfile_up.release(); 863 } 864 return nullptr; 865 } 866 867 size_t ObjectFileMachO::GetModuleSpecifications( 868 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 869 lldb::offset_t data_offset, lldb::offset_t file_offset, 870 lldb::offset_t length, lldb_private::ModuleSpecList &specs) { 871 const size_t initial_count = specs.GetSize(); 872 873 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) { 874 DataExtractor data; 875 data.SetData(data_sp); 876 llvm::MachO::mach_header header; 877 if (ParseHeader(data, &data_offset, header)) { 878 size_t header_and_load_cmds = 879 header.sizeofcmds + MachHeaderSizeFromMagic(header.magic); 880 if (header_and_load_cmds >= data_sp->GetByteSize()) { 881 data_sp = MapFileData(file, header_and_load_cmds, file_offset); 882 data.SetData(data_sp); 883 data_offset = MachHeaderSizeFromMagic(header.magic); 884 } 885 if (data_sp) { 886 ModuleSpec base_spec; 887 base_spec.GetFileSpec() = file; 888 base_spec.SetObjectOffset(file_offset); 889 base_spec.SetObjectSize(length); 890 GetAllArchSpecs(header, data, data_offset, base_spec, specs); 891 } 892 } 893 } 894 return specs.GetSize() - initial_count; 895 } 896 897 ConstString ObjectFileMachO::GetSegmentNameTEXT() { 898 static ConstString g_segment_name_TEXT("__TEXT"); 899 return g_segment_name_TEXT; 900 } 901 902 ConstString ObjectFileMachO::GetSegmentNameDATA() { 903 static ConstString g_segment_name_DATA("__DATA"); 904 return g_segment_name_DATA; 905 } 906 907 ConstString ObjectFileMachO::GetSegmentNameDATA_DIRTY() { 908 static ConstString g_segment_name("__DATA_DIRTY"); 909 return g_segment_name; 910 } 911 912 ConstString ObjectFileMachO::GetSegmentNameDATA_CONST() { 913 static ConstString g_segment_name("__DATA_CONST"); 914 return g_segment_name; 915 } 916 917 ConstString ObjectFileMachO::GetSegmentNameOBJC() { 918 static ConstString g_segment_name_OBJC("__OBJC"); 919 return g_segment_name_OBJC; 920 } 921 922 ConstString ObjectFileMachO::GetSegmentNameLINKEDIT() { 923 static ConstString g_section_name_LINKEDIT("__LINKEDIT"); 924 return g_section_name_LINKEDIT; 925 } 926 927 ConstString ObjectFileMachO::GetSegmentNameDWARF() { 928 static ConstString g_section_name("__DWARF"); 929 return g_section_name; 930 } 931 932 ConstString ObjectFileMachO::GetSectionNameEHFrame() { 933 static ConstString g_section_name_eh_frame("__eh_frame"); 934 return g_section_name_eh_frame; 935 } 936 937 bool ObjectFileMachO::MagicBytesMatch(DataBufferSP &data_sp, 938 lldb::addr_t data_offset, 939 lldb::addr_t data_length) { 940 DataExtractor data; 941 data.SetData(data_sp, data_offset, data_length); 942 lldb::offset_t offset = 0; 943 uint32_t magic = data.GetU32(&offset); 944 return MachHeaderSizeFromMagic(magic) != 0; 945 } 946 947 ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp, 948 DataBufferSP &data_sp, 949 lldb::offset_t data_offset, 950 const FileSpec *file, 951 lldb::offset_t file_offset, 952 lldb::offset_t length) 953 : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 954 m_mach_segments(), m_mach_sections(), m_entry_point_address(), 955 m_thread_context_offsets(), m_thread_context_offsets_valid(false), 956 m_reexported_dylibs(), m_allow_assembly_emulation_unwind_plans(true) { 957 ::memset(&m_header, 0, sizeof(m_header)); 958 ::memset(&m_dysymtab, 0, sizeof(m_dysymtab)); 959 } 960 961 ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp, 962 lldb::DataBufferSP &header_data_sp, 963 const lldb::ProcessSP &process_sp, 964 lldb::addr_t header_addr) 965 : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), 966 m_mach_segments(), m_mach_sections(), m_entry_point_address(), 967 m_thread_context_offsets(), m_thread_context_offsets_valid(false), 968 m_reexported_dylibs(), m_allow_assembly_emulation_unwind_plans(true) { 969 ::memset(&m_header, 0, sizeof(m_header)); 970 ::memset(&m_dysymtab, 0, sizeof(m_dysymtab)); 971 } 972 973 bool ObjectFileMachO::ParseHeader(DataExtractor &data, 974 lldb::offset_t *data_offset_ptr, 975 llvm::MachO::mach_header &header) { 976 data.SetByteOrder(endian::InlHostByteOrder()); 977 // Leave magic in the original byte order 978 header.magic = data.GetU32(data_offset_ptr); 979 bool can_parse = false; 980 bool is_64_bit = false; 981 switch (header.magic) { 982 case MH_MAGIC: 983 data.SetByteOrder(endian::InlHostByteOrder()); 984 data.SetAddressByteSize(4); 985 can_parse = true; 986 break; 987 988 case MH_MAGIC_64: 989 data.SetByteOrder(endian::InlHostByteOrder()); 990 data.SetAddressByteSize(8); 991 can_parse = true; 992 is_64_bit = true; 993 break; 994 995 case MH_CIGAM: 996 data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig 997 ? eByteOrderLittle 998 : eByteOrderBig); 999 data.SetAddressByteSize(4); 1000 can_parse = true; 1001 break; 1002 1003 case MH_CIGAM_64: 1004 data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig 1005 ? eByteOrderLittle 1006 : eByteOrderBig); 1007 data.SetAddressByteSize(8); 1008 is_64_bit = true; 1009 can_parse = true; 1010 break; 1011 1012 default: 1013 break; 1014 } 1015 1016 if (can_parse) { 1017 data.GetU32(data_offset_ptr, &header.cputype, 6); 1018 if (is_64_bit) 1019 *data_offset_ptr += 4; 1020 return true; 1021 } else { 1022 memset(&header, 0, sizeof(header)); 1023 } 1024 return false; 1025 } 1026 1027 bool ObjectFileMachO::ParseHeader() { 1028 ModuleSP module_sp(GetModule()); 1029 if (!module_sp) 1030 return false; 1031 1032 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 1033 bool can_parse = false; 1034 lldb::offset_t offset = 0; 1035 m_data.SetByteOrder(endian::InlHostByteOrder()); 1036 // Leave magic in the original byte order 1037 m_header.magic = m_data.GetU32(&offset); 1038 switch (m_header.magic) { 1039 case MH_MAGIC: 1040 m_data.SetByteOrder(endian::InlHostByteOrder()); 1041 m_data.SetAddressByteSize(4); 1042 can_parse = true; 1043 break; 1044 1045 case MH_MAGIC_64: 1046 m_data.SetByteOrder(endian::InlHostByteOrder()); 1047 m_data.SetAddressByteSize(8); 1048 can_parse = true; 1049 break; 1050 1051 case MH_CIGAM: 1052 m_data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig 1053 ? eByteOrderLittle 1054 : eByteOrderBig); 1055 m_data.SetAddressByteSize(4); 1056 can_parse = true; 1057 break; 1058 1059 case MH_CIGAM_64: 1060 m_data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig 1061 ? eByteOrderLittle 1062 : eByteOrderBig); 1063 m_data.SetAddressByteSize(8); 1064 can_parse = true; 1065 break; 1066 1067 default: 1068 break; 1069 } 1070 1071 if (can_parse) { 1072 m_data.GetU32(&offset, &m_header.cputype, 6); 1073 1074 ModuleSpecList all_specs; 1075 ModuleSpec base_spec; 1076 GetAllArchSpecs(m_header, m_data, MachHeaderSizeFromMagic(m_header.magic), 1077 base_spec, all_specs); 1078 1079 for (unsigned i = 0, e = all_specs.GetSize(); i != e; ++i) { 1080 ArchSpec mach_arch = 1081 all_specs.GetModuleSpecRefAtIndex(i).GetArchitecture(); 1082 1083 // Check if the module has a required architecture 1084 const ArchSpec &module_arch = module_sp->GetArchitecture(); 1085 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch)) 1086 continue; 1087 1088 if (SetModulesArchitecture(mach_arch)) { 1089 const size_t header_and_lc_size = 1090 m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic); 1091 if (m_data.GetByteSize() < header_and_lc_size) { 1092 DataBufferSP data_sp; 1093 ProcessSP process_sp(m_process_wp.lock()); 1094 if (process_sp) { 1095 data_sp = ReadMemory(process_sp, m_memory_addr, header_and_lc_size); 1096 } else { 1097 // Read in all only the load command data from the file on disk 1098 data_sp = MapFileData(m_file, header_and_lc_size, m_file_offset); 1099 if (data_sp->GetByteSize() != header_and_lc_size) 1100 continue; 1101 } 1102 if (data_sp) 1103 m_data.SetData(data_sp); 1104 } 1105 } 1106 return true; 1107 } 1108 // None found. 1109 return false; 1110 } else { 1111 memset(&m_header, 0, sizeof(struct llvm::MachO::mach_header)); 1112 } 1113 return false; 1114 } 1115 1116 ByteOrder ObjectFileMachO::GetByteOrder() const { 1117 return m_data.GetByteOrder(); 1118 } 1119 1120 bool ObjectFileMachO::IsExecutable() const { 1121 return m_header.filetype == MH_EXECUTE; 1122 } 1123 1124 bool ObjectFileMachO::IsDynamicLoader() const { 1125 return m_header.filetype == MH_DYLINKER; 1126 } 1127 1128 uint32_t ObjectFileMachO::GetAddressByteSize() const { 1129 return m_data.GetAddressByteSize(); 1130 } 1131 1132 AddressClass ObjectFileMachO::GetAddressClass(lldb::addr_t file_addr) { 1133 Symtab *symtab = GetSymtab(); 1134 if (!symtab) 1135 return AddressClass::eUnknown; 1136 1137 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr); 1138 if (symbol) { 1139 if (symbol->ValueIsAddress()) { 1140 SectionSP section_sp(symbol->GetAddressRef().GetSection()); 1141 if (section_sp) { 1142 const lldb::SectionType section_type = section_sp->GetType(); 1143 switch (section_type) { 1144 case eSectionTypeInvalid: 1145 return AddressClass::eUnknown; 1146 1147 case eSectionTypeCode: 1148 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM) { 1149 // For ARM we have a bit in the n_desc field of the symbol that 1150 // tells us ARM/Thumb which is bit 0x0008. 1151 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB) 1152 return AddressClass::eCodeAlternateISA; 1153 } 1154 return AddressClass::eCode; 1155 1156 case eSectionTypeContainer: 1157 return AddressClass::eUnknown; 1158 1159 case eSectionTypeData: 1160 case eSectionTypeDataCString: 1161 case eSectionTypeDataCStringPointers: 1162 case eSectionTypeDataSymbolAddress: 1163 case eSectionTypeData4: 1164 case eSectionTypeData8: 1165 case eSectionTypeData16: 1166 case eSectionTypeDataPointers: 1167 case eSectionTypeZeroFill: 1168 case eSectionTypeDataObjCMessageRefs: 1169 case eSectionTypeDataObjCCFStrings: 1170 case eSectionTypeGoSymtab: 1171 return AddressClass::eData; 1172 1173 case eSectionTypeDebug: 1174 case eSectionTypeDWARFDebugAbbrev: 1175 case eSectionTypeDWARFDebugAbbrevDwo: 1176 case eSectionTypeDWARFDebugAddr: 1177 case eSectionTypeDWARFDebugAranges: 1178 case eSectionTypeDWARFDebugCuIndex: 1179 case eSectionTypeDWARFDebugFrame: 1180 case eSectionTypeDWARFDebugInfo: 1181 case eSectionTypeDWARFDebugInfoDwo: 1182 case eSectionTypeDWARFDebugLine: 1183 case eSectionTypeDWARFDebugLineStr: 1184 case eSectionTypeDWARFDebugLoc: 1185 case eSectionTypeDWARFDebugLocDwo: 1186 case eSectionTypeDWARFDebugLocLists: 1187 case eSectionTypeDWARFDebugLocListsDwo: 1188 case eSectionTypeDWARFDebugMacInfo: 1189 case eSectionTypeDWARFDebugMacro: 1190 case eSectionTypeDWARFDebugNames: 1191 case eSectionTypeDWARFDebugPubNames: 1192 case eSectionTypeDWARFDebugPubTypes: 1193 case eSectionTypeDWARFDebugRanges: 1194 case eSectionTypeDWARFDebugRngLists: 1195 case eSectionTypeDWARFDebugRngListsDwo: 1196 case eSectionTypeDWARFDebugStr: 1197 case eSectionTypeDWARFDebugStrDwo: 1198 case eSectionTypeDWARFDebugStrOffsets: 1199 case eSectionTypeDWARFDebugStrOffsetsDwo: 1200 case eSectionTypeDWARFDebugTuIndex: 1201 case eSectionTypeDWARFDebugTypes: 1202 case eSectionTypeDWARFDebugTypesDwo: 1203 case eSectionTypeDWARFAppleNames: 1204 case eSectionTypeDWARFAppleTypes: 1205 case eSectionTypeDWARFAppleNamespaces: 1206 case eSectionTypeDWARFAppleObjC: 1207 case eSectionTypeDWARFGNUDebugAltLink: 1208 return AddressClass::eDebug; 1209 1210 case eSectionTypeEHFrame: 1211 case eSectionTypeARMexidx: 1212 case eSectionTypeARMextab: 1213 case eSectionTypeCompactUnwind: 1214 return AddressClass::eRuntime; 1215 1216 case eSectionTypeAbsoluteAddress: 1217 case eSectionTypeELFSymbolTable: 1218 case eSectionTypeELFDynamicSymbols: 1219 case eSectionTypeELFRelocationEntries: 1220 case eSectionTypeELFDynamicLinkInfo: 1221 case eSectionTypeOther: 1222 return AddressClass::eUnknown; 1223 } 1224 } 1225 } 1226 1227 const SymbolType symbol_type = symbol->GetType(); 1228 switch (symbol_type) { 1229 case eSymbolTypeAny: 1230 return AddressClass::eUnknown; 1231 case eSymbolTypeAbsolute: 1232 return AddressClass::eUnknown; 1233 1234 case eSymbolTypeCode: 1235 case eSymbolTypeTrampoline: 1236 case eSymbolTypeResolver: 1237 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM) { 1238 // For ARM we have a bit in the n_desc field of the symbol that tells 1239 // us ARM/Thumb which is bit 0x0008. 1240 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB) 1241 return AddressClass::eCodeAlternateISA; 1242 } 1243 return AddressClass::eCode; 1244 1245 case eSymbolTypeData: 1246 return AddressClass::eData; 1247 case eSymbolTypeRuntime: 1248 return AddressClass::eRuntime; 1249 case eSymbolTypeException: 1250 return AddressClass::eRuntime; 1251 case eSymbolTypeSourceFile: 1252 return AddressClass::eDebug; 1253 case eSymbolTypeHeaderFile: 1254 return AddressClass::eDebug; 1255 case eSymbolTypeObjectFile: 1256 return AddressClass::eDebug; 1257 case eSymbolTypeCommonBlock: 1258 return AddressClass::eDebug; 1259 case eSymbolTypeBlock: 1260 return AddressClass::eDebug; 1261 case eSymbolTypeLocal: 1262 return AddressClass::eData; 1263 case eSymbolTypeParam: 1264 return AddressClass::eData; 1265 case eSymbolTypeVariable: 1266 return AddressClass::eData; 1267 case eSymbolTypeVariableType: 1268 return AddressClass::eDebug; 1269 case eSymbolTypeLineEntry: 1270 return AddressClass::eDebug; 1271 case eSymbolTypeLineHeader: 1272 return AddressClass::eDebug; 1273 case eSymbolTypeScopeBegin: 1274 return AddressClass::eDebug; 1275 case eSymbolTypeScopeEnd: 1276 return AddressClass::eDebug; 1277 case eSymbolTypeAdditional: 1278 return AddressClass::eUnknown; 1279 case eSymbolTypeCompiler: 1280 return AddressClass::eDebug; 1281 case eSymbolTypeInstrumentation: 1282 return AddressClass::eDebug; 1283 case eSymbolTypeUndefined: 1284 return AddressClass::eUnknown; 1285 case eSymbolTypeObjCClass: 1286 return AddressClass::eRuntime; 1287 case eSymbolTypeObjCMetaClass: 1288 return AddressClass::eRuntime; 1289 case eSymbolTypeObjCIVar: 1290 return AddressClass::eRuntime; 1291 case eSymbolTypeReExported: 1292 return AddressClass::eRuntime; 1293 } 1294 } 1295 return AddressClass::eUnknown; 1296 } 1297 1298 Symtab *ObjectFileMachO::GetSymtab() { 1299 ModuleSP module_sp(GetModule()); 1300 if (module_sp) { 1301 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 1302 if (m_symtab_up == nullptr) { 1303 m_symtab_up = std::make_unique<Symtab>(this); 1304 std::lock_guard<std::recursive_mutex> symtab_guard( 1305 m_symtab_up->GetMutex()); 1306 ParseSymtab(); 1307 m_symtab_up->Finalize(); 1308 } 1309 } 1310 return m_symtab_up.get(); 1311 } 1312 1313 bool ObjectFileMachO::IsStripped() { 1314 if (m_dysymtab.cmd == 0) { 1315 ModuleSP module_sp(GetModule()); 1316 if (module_sp) { 1317 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 1318 for (uint32_t i = 0; i < m_header.ncmds; ++i) { 1319 const lldb::offset_t load_cmd_offset = offset; 1320 1321 llvm::MachO::load_command lc; 1322 if (m_data.GetU32(&offset, &lc.cmd, 2) == nullptr) 1323 break; 1324 if (lc.cmd == LC_DYSYMTAB) { 1325 m_dysymtab.cmd = lc.cmd; 1326 m_dysymtab.cmdsize = lc.cmdsize; 1327 if (m_data.GetU32(&offset, &m_dysymtab.ilocalsym, 1328 (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == 1329 nullptr) { 1330 // Clear m_dysymtab if we were unable to read all items from the 1331 // load command 1332 ::memset(&m_dysymtab, 0, sizeof(m_dysymtab)); 1333 } 1334 } 1335 offset = load_cmd_offset + lc.cmdsize; 1336 } 1337 } 1338 } 1339 if (m_dysymtab.cmd) 1340 return m_dysymtab.nlocalsym <= 1; 1341 return false; 1342 } 1343 1344 ObjectFileMachO::EncryptedFileRanges ObjectFileMachO::GetEncryptedFileRanges() { 1345 EncryptedFileRanges result; 1346 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 1347 1348 llvm::MachO::encryption_info_command encryption_cmd; 1349 for (uint32_t i = 0; i < m_header.ncmds; ++i) { 1350 const lldb::offset_t load_cmd_offset = offset; 1351 if (m_data.GetU32(&offset, &encryption_cmd, 2) == nullptr) 1352 break; 1353 1354 // LC_ENCRYPTION_INFO and LC_ENCRYPTION_INFO_64 have the same sizes for the 1355 // 3 fields we care about, so treat them the same. 1356 if (encryption_cmd.cmd == LC_ENCRYPTION_INFO || 1357 encryption_cmd.cmd == LC_ENCRYPTION_INFO_64) { 1358 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3)) { 1359 if (encryption_cmd.cryptid != 0) { 1360 EncryptedFileRanges::Entry entry; 1361 entry.SetRangeBase(encryption_cmd.cryptoff); 1362 entry.SetByteSize(encryption_cmd.cryptsize); 1363 result.Append(entry); 1364 } 1365 } 1366 } 1367 offset = load_cmd_offset + encryption_cmd.cmdsize; 1368 } 1369 1370 return result; 1371 } 1372 1373 void ObjectFileMachO::SanitizeSegmentCommand( 1374 llvm::MachO::segment_command_64 &seg_cmd, uint32_t cmd_idx) { 1375 if (m_length == 0 || seg_cmd.filesize == 0) 1376 return; 1377 1378 if ((m_header.flags & MH_DYLIB_IN_CACHE) && !IsInMemory()) { 1379 // In shared cache images, the load commands are relative to the 1380 // shared cache file, and not the specific image we are 1381 // examining. Let's fix this up so that it looks like a normal 1382 // image. 1383 if (strncmp(seg_cmd.segname, "__TEXT", sizeof(seg_cmd.segname)) == 0) 1384 m_text_address = seg_cmd.vmaddr; 1385 if (strncmp(seg_cmd.segname, "__LINKEDIT", sizeof(seg_cmd.segname)) == 0) 1386 m_linkedit_original_offset = seg_cmd.fileoff; 1387 1388 seg_cmd.fileoff = seg_cmd.vmaddr - m_text_address; 1389 } 1390 1391 if (seg_cmd.fileoff > m_length) { 1392 // We have a load command that says it extends past the end of the file. 1393 // This is likely a corrupt file. We don't have any way to return an error 1394 // condition here (this method was likely invoked from something like 1395 // ObjectFile::GetSectionList()), so we just null out the section contents, 1396 // and dump a message to stdout. The most common case here is core file 1397 // debugging with a truncated file. 1398 const char *lc_segment_name = 1399 seg_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT"; 1400 GetModule()->ReportWarning( 1401 "load command %u %s has a fileoff (0x%" PRIx64 1402 ") that extends beyond the end of the file (0x%" PRIx64 1403 "), ignoring this section", 1404 cmd_idx, lc_segment_name, seg_cmd.fileoff, m_length); 1405 1406 seg_cmd.fileoff = 0; 1407 seg_cmd.filesize = 0; 1408 } 1409 1410 if (seg_cmd.fileoff + seg_cmd.filesize > m_length) { 1411 // We have a load command that says it extends past the end of the file. 1412 // This is likely a corrupt file. We don't have any way to return an error 1413 // condition here (this method was likely invoked from something like 1414 // ObjectFile::GetSectionList()), so we just null out the section contents, 1415 // and dump a message to stdout. The most common case here is core file 1416 // debugging with a truncated file. 1417 const char *lc_segment_name = 1418 seg_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT"; 1419 GetModule()->ReportWarning( 1420 "load command %u %s has a fileoff + filesize (0x%" PRIx64 1421 ") that extends beyond the end of the file (0x%" PRIx64 1422 "), the segment will be truncated to match", 1423 cmd_idx, lc_segment_name, seg_cmd.fileoff + seg_cmd.filesize, m_length); 1424 1425 // Truncate the length 1426 seg_cmd.filesize = m_length - seg_cmd.fileoff; 1427 } 1428 } 1429 1430 static uint32_t 1431 GetSegmentPermissions(const llvm::MachO::segment_command_64 &seg_cmd) { 1432 uint32_t result = 0; 1433 if (seg_cmd.initprot & VM_PROT_READ) 1434 result |= ePermissionsReadable; 1435 if (seg_cmd.initprot & VM_PROT_WRITE) 1436 result |= ePermissionsWritable; 1437 if (seg_cmd.initprot & VM_PROT_EXECUTE) 1438 result |= ePermissionsExecutable; 1439 return result; 1440 } 1441 1442 static lldb::SectionType GetSectionType(uint32_t flags, 1443 ConstString section_name) { 1444 1445 if (flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS)) 1446 return eSectionTypeCode; 1447 1448 uint32_t mach_sect_type = flags & SECTION_TYPE; 1449 static ConstString g_sect_name_objc_data("__objc_data"); 1450 static ConstString g_sect_name_objc_msgrefs("__objc_msgrefs"); 1451 static ConstString g_sect_name_objc_selrefs("__objc_selrefs"); 1452 static ConstString g_sect_name_objc_classrefs("__objc_classrefs"); 1453 static ConstString g_sect_name_objc_superrefs("__objc_superrefs"); 1454 static ConstString g_sect_name_objc_const("__objc_const"); 1455 static ConstString g_sect_name_objc_classlist("__objc_classlist"); 1456 static ConstString g_sect_name_cfstring("__cfstring"); 1457 1458 static ConstString g_sect_name_dwarf_debug_abbrev("__debug_abbrev"); 1459 static ConstString g_sect_name_dwarf_debug_aranges("__debug_aranges"); 1460 static ConstString g_sect_name_dwarf_debug_frame("__debug_frame"); 1461 static ConstString g_sect_name_dwarf_debug_info("__debug_info"); 1462 static ConstString g_sect_name_dwarf_debug_line("__debug_line"); 1463 static ConstString g_sect_name_dwarf_debug_loc("__debug_loc"); 1464 static ConstString g_sect_name_dwarf_debug_loclists("__debug_loclists"); 1465 static ConstString g_sect_name_dwarf_debug_macinfo("__debug_macinfo"); 1466 static ConstString g_sect_name_dwarf_debug_names("__debug_names"); 1467 static ConstString g_sect_name_dwarf_debug_pubnames("__debug_pubnames"); 1468 static ConstString g_sect_name_dwarf_debug_pubtypes("__debug_pubtypes"); 1469 static ConstString g_sect_name_dwarf_debug_ranges("__debug_ranges"); 1470 static ConstString g_sect_name_dwarf_debug_str("__debug_str"); 1471 static ConstString g_sect_name_dwarf_debug_types("__debug_types"); 1472 static ConstString g_sect_name_dwarf_apple_names("__apple_names"); 1473 static ConstString g_sect_name_dwarf_apple_types("__apple_types"); 1474 static ConstString g_sect_name_dwarf_apple_namespaces("__apple_namespac"); 1475 static ConstString g_sect_name_dwarf_apple_objc("__apple_objc"); 1476 static ConstString g_sect_name_eh_frame("__eh_frame"); 1477 static ConstString g_sect_name_compact_unwind("__unwind_info"); 1478 static ConstString g_sect_name_text("__text"); 1479 static ConstString g_sect_name_data("__data"); 1480 static ConstString g_sect_name_go_symtab("__gosymtab"); 1481 1482 if (section_name == g_sect_name_dwarf_debug_abbrev) 1483 return eSectionTypeDWARFDebugAbbrev; 1484 if (section_name == g_sect_name_dwarf_debug_aranges) 1485 return eSectionTypeDWARFDebugAranges; 1486 if (section_name == g_sect_name_dwarf_debug_frame) 1487 return eSectionTypeDWARFDebugFrame; 1488 if (section_name == g_sect_name_dwarf_debug_info) 1489 return eSectionTypeDWARFDebugInfo; 1490 if (section_name == g_sect_name_dwarf_debug_line) 1491 return eSectionTypeDWARFDebugLine; 1492 if (section_name == g_sect_name_dwarf_debug_loc) 1493 return eSectionTypeDWARFDebugLoc; 1494 if (section_name == g_sect_name_dwarf_debug_loclists) 1495 return eSectionTypeDWARFDebugLocLists; 1496 if (section_name == g_sect_name_dwarf_debug_macinfo) 1497 return eSectionTypeDWARFDebugMacInfo; 1498 if (section_name == g_sect_name_dwarf_debug_names) 1499 return eSectionTypeDWARFDebugNames; 1500 if (section_name == g_sect_name_dwarf_debug_pubnames) 1501 return eSectionTypeDWARFDebugPubNames; 1502 if (section_name == g_sect_name_dwarf_debug_pubtypes) 1503 return eSectionTypeDWARFDebugPubTypes; 1504 if (section_name == g_sect_name_dwarf_debug_ranges) 1505 return eSectionTypeDWARFDebugRanges; 1506 if (section_name == g_sect_name_dwarf_debug_str) 1507 return eSectionTypeDWARFDebugStr; 1508 if (section_name == g_sect_name_dwarf_debug_types) 1509 return eSectionTypeDWARFDebugTypes; 1510 if (section_name == g_sect_name_dwarf_apple_names) 1511 return eSectionTypeDWARFAppleNames; 1512 if (section_name == g_sect_name_dwarf_apple_types) 1513 return eSectionTypeDWARFAppleTypes; 1514 if (section_name == g_sect_name_dwarf_apple_namespaces) 1515 return eSectionTypeDWARFAppleNamespaces; 1516 if (section_name == g_sect_name_dwarf_apple_objc) 1517 return eSectionTypeDWARFAppleObjC; 1518 if (section_name == g_sect_name_objc_selrefs) 1519 return eSectionTypeDataCStringPointers; 1520 if (section_name == g_sect_name_objc_msgrefs) 1521 return eSectionTypeDataObjCMessageRefs; 1522 if (section_name == g_sect_name_eh_frame) 1523 return eSectionTypeEHFrame; 1524 if (section_name == g_sect_name_compact_unwind) 1525 return eSectionTypeCompactUnwind; 1526 if (section_name == g_sect_name_cfstring) 1527 return eSectionTypeDataObjCCFStrings; 1528 if (section_name == g_sect_name_go_symtab) 1529 return eSectionTypeGoSymtab; 1530 if (section_name == g_sect_name_objc_data || 1531 section_name == g_sect_name_objc_classrefs || 1532 section_name == g_sect_name_objc_superrefs || 1533 section_name == g_sect_name_objc_const || 1534 section_name == g_sect_name_objc_classlist) { 1535 return eSectionTypeDataPointers; 1536 } 1537 1538 switch (mach_sect_type) { 1539 // TODO: categorize sections by other flags for regular sections 1540 case S_REGULAR: 1541 if (section_name == g_sect_name_text) 1542 return eSectionTypeCode; 1543 if (section_name == g_sect_name_data) 1544 return eSectionTypeData; 1545 return eSectionTypeOther; 1546 case S_ZEROFILL: 1547 return eSectionTypeZeroFill; 1548 case S_CSTRING_LITERALS: // section with only literal C strings 1549 return eSectionTypeDataCString; 1550 case S_4BYTE_LITERALS: // section with only 4 byte literals 1551 return eSectionTypeData4; 1552 case S_8BYTE_LITERALS: // section with only 8 byte literals 1553 return eSectionTypeData8; 1554 case S_LITERAL_POINTERS: // section with only pointers to literals 1555 return eSectionTypeDataPointers; 1556 case S_NON_LAZY_SYMBOL_POINTERS: // section with only non-lazy symbol pointers 1557 return eSectionTypeDataPointers; 1558 case S_LAZY_SYMBOL_POINTERS: // section with only lazy symbol pointers 1559 return eSectionTypeDataPointers; 1560 case S_SYMBOL_STUBS: // section with only symbol stubs, byte size of stub in 1561 // the reserved2 field 1562 return eSectionTypeCode; 1563 case S_MOD_INIT_FUNC_POINTERS: // section with only function pointers for 1564 // initialization 1565 return eSectionTypeDataPointers; 1566 case S_MOD_TERM_FUNC_POINTERS: // section with only function pointers for 1567 // termination 1568 return eSectionTypeDataPointers; 1569 case S_COALESCED: 1570 return eSectionTypeOther; 1571 case S_GB_ZEROFILL: 1572 return eSectionTypeZeroFill; 1573 case S_INTERPOSING: // section with only pairs of function pointers for 1574 // interposing 1575 return eSectionTypeCode; 1576 case S_16BYTE_LITERALS: // section with only 16 byte literals 1577 return eSectionTypeData16; 1578 case S_DTRACE_DOF: 1579 return eSectionTypeDebug; 1580 case S_LAZY_DYLIB_SYMBOL_POINTERS: 1581 return eSectionTypeDataPointers; 1582 default: 1583 return eSectionTypeOther; 1584 } 1585 } 1586 1587 struct ObjectFileMachO::SegmentParsingContext { 1588 const EncryptedFileRanges EncryptedRanges; 1589 lldb_private::SectionList &UnifiedList; 1590 uint32_t NextSegmentIdx = 0; 1591 uint32_t NextSectionIdx = 0; 1592 bool FileAddressesChanged = false; 1593 1594 SegmentParsingContext(EncryptedFileRanges EncryptedRanges, 1595 lldb_private::SectionList &UnifiedList) 1596 : EncryptedRanges(std::move(EncryptedRanges)), UnifiedList(UnifiedList) {} 1597 }; 1598 1599 void ObjectFileMachO::ProcessSegmentCommand( 1600 const llvm::MachO::load_command &load_cmd_, lldb::offset_t offset, 1601 uint32_t cmd_idx, SegmentParsingContext &context) { 1602 llvm::MachO::segment_command_64 load_cmd; 1603 memcpy(&load_cmd, &load_cmd_, sizeof(load_cmd_)); 1604 1605 if (!m_data.GetU8(&offset, (uint8_t *)load_cmd.segname, 16)) 1606 return; 1607 1608 ModuleSP module_sp = GetModule(); 1609 const bool is_core = GetType() == eTypeCoreFile; 1610 const bool is_dsym = (m_header.filetype == MH_DSYM); 1611 bool add_section = true; 1612 bool add_to_unified = true; 1613 ConstString const_segname( 1614 load_cmd.segname, strnlen(load_cmd.segname, sizeof(load_cmd.segname))); 1615 1616 SectionSP unified_section_sp( 1617 context.UnifiedList.FindSectionByName(const_segname)); 1618 if (is_dsym && unified_section_sp) { 1619 if (const_segname == GetSegmentNameLINKEDIT()) { 1620 // We need to keep the __LINKEDIT segment private to this object file 1621 // only 1622 add_to_unified = false; 1623 } else { 1624 // This is the dSYM file and this section has already been created by the 1625 // object file, no need to create it. 1626 add_section = false; 1627 } 1628 } 1629 load_cmd.vmaddr = m_data.GetAddress(&offset); 1630 load_cmd.vmsize = m_data.GetAddress(&offset); 1631 load_cmd.fileoff = m_data.GetAddress(&offset); 1632 load_cmd.filesize = m_data.GetAddress(&offset); 1633 if (!m_data.GetU32(&offset, &load_cmd.maxprot, 4)) 1634 return; 1635 1636 SanitizeSegmentCommand(load_cmd, cmd_idx); 1637 1638 const uint32_t segment_permissions = GetSegmentPermissions(load_cmd); 1639 const bool segment_is_encrypted = 1640 (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0; 1641 1642 // Keep a list of mach segments around in case we need to get at data that 1643 // isn't stored in the abstracted Sections. 1644 m_mach_segments.push_back(load_cmd); 1645 1646 // Use a segment ID of the segment index shifted left by 8 so they never 1647 // conflict with any of the sections. 1648 SectionSP segment_sp; 1649 if (add_section && (const_segname || is_core)) { 1650 segment_sp = std::make_shared<Section>( 1651 module_sp, // Module to which this section belongs 1652 this, // Object file to which this sections belongs 1653 ++context.NextSegmentIdx 1654 << 8, // Section ID is the 1 based segment index 1655 // shifted right by 8 bits as not to collide with any of the 256 1656 // section IDs that are possible 1657 const_segname, // Name of this section 1658 eSectionTypeContainer, // This section is a container of other 1659 // sections. 1660 load_cmd.vmaddr, // File VM address == addresses as they are 1661 // found in the object file 1662 load_cmd.vmsize, // VM size in bytes of this section 1663 load_cmd.fileoff, // Offset to the data for this section in 1664 // the file 1665 load_cmd.filesize, // Size in bytes of this section as found 1666 // in the file 1667 0, // Segments have no alignment information 1668 load_cmd.flags); // Flags for this section 1669 1670 segment_sp->SetIsEncrypted(segment_is_encrypted); 1671 m_sections_up->AddSection(segment_sp); 1672 segment_sp->SetPermissions(segment_permissions); 1673 if (add_to_unified) 1674 context.UnifiedList.AddSection(segment_sp); 1675 } else if (unified_section_sp) { 1676 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr) { 1677 // Check to see if the module was read from memory? 1678 if (module_sp->GetObjectFile()->IsInMemory()) { 1679 // We have a module that is in memory and needs to have its file 1680 // address adjusted. We need to do this because when we load a file 1681 // from memory, its addresses will be slid already, yet the addresses 1682 // in the new symbol file will still be unslid. Since everything is 1683 // stored as section offset, this shouldn't cause any problems. 1684 1685 // Make sure we've parsed the symbol table from the ObjectFile before 1686 // we go around changing its Sections. 1687 module_sp->GetObjectFile()->GetSymtab(); 1688 // eh_frame would present the same problems but we parse that on a per- 1689 // function basis as-needed so it's more difficult to remove its use of 1690 // the Sections. Realistically, the environments where this code path 1691 // will be taken will not have eh_frame sections. 1692 1693 unified_section_sp->SetFileAddress(load_cmd.vmaddr); 1694 1695 // Notify the module that the section addresses have been changed once 1696 // we're done so any file-address caches can be updated. 1697 context.FileAddressesChanged = true; 1698 } 1699 } 1700 m_sections_up->AddSection(unified_section_sp); 1701 } 1702 1703 llvm::MachO::section_64 sect64; 1704 ::memset(§64, 0, sizeof(sect64)); 1705 // Push a section into our mach sections for the section at index zero 1706 // (NO_SECT) if we don't have any mach sections yet... 1707 if (m_mach_sections.empty()) 1708 m_mach_sections.push_back(sect64); 1709 uint32_t segment_sect_idx; 1710 const lldb::user_id_t first_segment_sectID = context.NextSectionIdx + 1; 1711 1712 const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8; 1713 for (segment_sect_idx = 0; segment_sect_idx < load_cmd.nsects; 1714 ++segment_sect_idx) { 1715 if (m_data.GetU8(&offset, (uint8_t *)sect64.sectname, 1716 sizeof(sect64.sectname)) == nullptr) 1717 break; 1718 if (m_data.GetU8(&offset, (uint8_t *)sect64.segname, 1719 sizeof(sect64.segname)) == nullptr) 1720 break; 1721 sect64.addr = m_data.GetAddress(&offset); 1722 sect64.size = m_data.GetAddress(&offset); 1723 1724 if (m_data.GetU32(&offset, §64.offset, num_u32s) == nullptr) 1725 break; 1726 1727 if ((m_header.flags & MH_DYLIB_IN_CACHE) && !IsInMemory()) { 1728 sect64.offset = sect64.addr - m_text_address; 1729 } 1730 1731 // Keep a list of mach sections around in case we need to get at data that 1732 // isn't stored in the abstracted Sections. 1733 m_mach_sections.push_back(sect64); 1734 1735 if (add_section) { 1736 ConstString section_name( 1737 sect64.sectname, strnlen(sect64.sectname, sizeof(sect64.sectname))); 1738 if (!const_segname) { 1739 // We have a segment with no name so we need to conjure up segments 1740 // that correspond to the section's segname if there isn't already such 1741 // a section. If there is such a section, we resize the section so that 1742 // it spans all sections. We also mark these sections as fake so 1743 // address matches don't hit if they land in the gaps between the child 1744 // sections. 1745 const_segname.SetTrimmedCStringWithLength(sect64.segname, 1746 sizeof(sect64.segname)); 1747 segment_sp = context.UnifiedList.FindSectionByName(const_segname); 1748 if (segment_sp.get()) { 1749 Section *segment = segment_sp.get(); 1750 // Grow the section size as needed. 1751 const lldb::addr_t sect64_min_addr = sect64.addr; 1752 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size; 1753 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize(); 1754 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress(); 1755 const lldb::addr_t curr_seg_max_addr = 1756 curr_seg_min_addr + curr_seg_byte_size; 1757 if (sect64_min_addr >= curr_seg_min_addr) { 1758 const lldb::addr_t new_seg_byte_size = 1759 sect64_max_addr - curr_seg_min_addr; 1760 // Only grow the section size if needed 1761 if (new_seg_byte_size > curr_seg_byte_size) 1762 segment->SetByteSize(new_seg_byte_size); 1763 } else { 1764 // We need to change the base address of the segment and adjust the 1765 // child section offsets for all existing children. 1766 const lldb::addr_t slide_amount = 1767 sect64_min_addr - curr_seg_min_addr; 1768 segment->Slide(slide_amount, false); 1769 segment->GetChildren().Slide(-slide_amount, false); 1770 segment->SetByteSize(curr_seg_max_addr - sect64_min_addr); 1771 } 1772 1773 // Grow the section size as needed. 1774 if (sect64.offset) { 1775 const lldb::addr_t segment_min_file_offset = 1776 segment->GetFileOffset(); 1777 const lldb::addr_t segment_max_file_offset = 1778 segment_min_file_offset + segment->GetFileSize(); 1779 1780 const lldb::addr_t section_min_file_offset = sect64.offset; 1781 const lldb::addr_t section_max_file_offset = 1782 section_min_file_offset + sect64.size; 1783 const lldb::addr_t new_file_offset = 1784 std::min(section_min_file_offset, segment_min_file_offset); 1785 const lldb::addr_t new_file_size = 1786 std::max(section_max_file_offset, segment_max_file_offset) - 1787 new_file_offset; 1788 segment->SetFileOffset(new_file_offset); 1789 segment->SetFileSize(new_file_size); 1790 } 1791 } else { 1792 // Create a fake section for the section's named segment 1793 segment_sp = std::make_shared<Section>( 1794 segment_sp, // Parent section 1795 module_sp, // Module to which this section belongs 1796 this, // Object file to which this section belongs 1797 ++context.NextSegmentIdx 1798 << 8, // Section ID is the 1 based segment index 1799 // shifted right by 8 bits as not to 1800 // collide with any of the 256 section IDs 1801 // that are possible 1802 const_segname, // Name of this section 1803 eSectionTypeContainer, // This section is a container of 1804 // other sections. 1805 sect64.addr, // File VM address == addresses as they are 1806 // found in the object file 1807 sect64.size, // VM size in bytes of this section 1808 sect64.offset, // Offset to the data for this section in 1809 // the file 1810 sect64.offset ? sect64.size : 0, // Size in bytes of 1811 // this section as 1812 // found in the file 1813 sect64.align, 1814 load_cmd.flags); // Flags for this section 1815 segment_sp->SetIsFake(true); 1816 segment_sp->SetPermissions(segment_permissions); 1817 m_sections_up->AddSection(segment_sp); 1818 if (add_to_unified) 1819 context.UnifiedList.AddSection(segment_sp); 1820 segment_sp->SetIsEncrypted(segment_is_encrypted); 1821 } 1822 } 1823 assert(segment_sp.get()); 1824 1825 lldb::SectionType sect_type = GetSectionType(sect64.flags, section_name); 1826 1827 SectionSP section_sp(new Section( 1828 segment_sp, module_sp, this, ++context.NextSectionIdx, section_name, 1829 sect_type, sect64.addr - segment_sp->GetFileAddress(), sect64.size, 1830 sect64.offset, sect64.offset == 0 ? 0 : sect64.size, sect64.align, 1831 sect64.flags)); 1832 // Set the section to be encrypted to match the segment 1833 1834 bool section_is_encrypted = false; 1835 if (!segment_is_encrypted && load_cmd.filesize != 0) 1836 section_is_encrypted = context.EncryptedRanges.FindEntryThatContains( 1837 sect64.offset) != nullptr; 1838 1839 section_sp->SetIsEncrypted(segment_is_encrypted || section_is_encrypted); 1840 section_sp->SetPermissions(segment_permissions); 1841 segment_sp->GetChildren().AddSection(section_sp); 1842 1843 if (segment_sp->IsFake()) { 1844 segment_sp.reset(); 1845 const_segname.Clear(); 1846 } 1847 } 1848 } 1849 if (segment_sp && is_dsym) { 1850 if (first_segment_sectID <= context.NextSectionIdx) { 1851 lldb::user_id_t sect_uid; 1852 for (sect_uid = first_segment_sectID; sect_uid <= context.NextSectionIdx; 1853 ++sect_uid) { 1854 SectionSP curr_section_sp( 1855 segment_sp->GetChildren().FindSectionByID(sect_uid)); 1856 SectionSP next_section_sp; 1857 if (sect_uid + 1 <= context.NextSectionIdx) 1858 next_section_sp = 1859 segment_sp->GetChildren().FindSectionByID(sect_uid + 1); 1860 1861 if (curr_section_sp.get()) { 1862 if (curr_section_sp->GetByteSize() == 0) { 1863 if (next_section_sp.get() != nullptr) 1864 curr_section_sp->SetByteSize(next_section_sp->GetFileAddress() - 1865 curr_section_sp->GetFileAddress()); 1866 else 1867 curr_section_sp->SetByteSize(load_cmd.vmsize); 1868 } 1869 } 1870 } 1871 } 1872 } 1873 } 1874 1875 void ObjectFileMachO::ProcessDysymtabCommand( 1876 const llvm::MachO::load_command &load_cmd, lldb::offset_t offset) { 1877 m_dysymtab.cmd = load_cmd.cmd; 1878 m_dysymtab.cmdsize = load_cmd.cmdsize; 1879 m_data.GetU32(&offset, &m_dysymtab.ilocalsym, 1880 (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2); 1881 } 1882 1883 void ObjectFileMachO::CreateSections(SectionList &unified_section_list) { 1884 if (m_sections_up) 1885 return; 1886 1887 m_sections_up = std::make_unique<SectionList>(); 1888 1889 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 1890 // bool dump_sections = false; 1891 ModuleSP module_sp(GetModule()); 1892 1893 offset = MachHeaderSizeFromMagic(m_header.magic); 1894 1895 SegmentParsingContext context(GetEncryptedFileRanges(), unified_section_list); 1896 llvm::MachO::load_command load_cmd; 1897 for (uint32_t i = 0; i < m_header.ncmds; ++i) { 1898 const lldb::offset_t load_cmd_offset = offset; 1899 if (m_data.GetU32(&offset, &load_cmd, 2) == nullptr) 1900 break; 1901 1902 if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64) 1903 ProcessSegmentCommand(load_cmd, offset, i, context); 1904 else if (load_cmd.cmd == LC_DYSYMTAB) 1905 ProcessDysymtabCommand(load_cmd, offset); 1906 1907 offset = load_cmd_offset + load_cmd.cmdsize; 1908 } 1909 1910 if (context.FileAddressesChanged && module_sp) 1911 module_sp->SectionFileAddressesChanged(); 1912 } 1913 1914 class MachSymtabSectionInfo { 1915 public: 1916 MachSymtabSectionInfo(SectionList *section_list) 1917 : m_section_list(section_list), m_section_infos() { 1918 // Get the number of sections down to a depth of 1 to include all segments 1919 // and their sections, but no other sections that may be added for debug 1920 // map or 1921 m_section_infos.resize(section_list->GetNumSections(1)); 1922 } 1923 1924 SectionSP GetSection(uint8_t n_sect, addr_t file_addr) { 1925 if (n_sect == 0) 1926 return SectionSP(); 1927 if (n_sect < m_section_infos.size()) { 1928 if (!m_section_infos[n_sect].section_sp) { 1929 SectionSP section_sp(m_section_list->FindSectionByID(n_sect)); 1930 m_section_infos[n_sect].section_sp = section_sp; 1931 if (section_sp) { 1932 m_section_infos[n_sect].vm_range.SetBaseAddress( 1933 section_sp->GetFileAddress()); 1934 m_section_infos[n_sect].vm_range.SetByteSize( 1935 section_sp->GetByteSize()); 1936 } else { 1937 std::string filename = "<unknown>"; 1938 SectionSP first_section_sp(m_section_list->GetSectionAtIndex(0)); 1939 if (first_section_sp) 1940 filename = first_section_sp->GetObjectFile()->GetFileSpec().GetPath(); 1941 1942 Host::SystemLog(Host::eSystemLogError, 1943 "error: unable to find section %d for a symbol in " 1944 "%s, corrupt file?\n", 1945 n_sect, filename.c_str()); 1946 } 1947 } 1948 if (m_section_infos[n_sect].vm_range.Contains(file_addr)) { 1949 // Symbol is in section. 1950 return m_section_infos[n_sect].section_sp; 1951 } else if (m_section_infos[n_sect].vm_range.GetByteSize() == 0 && 1952 m_section_infos[n_sect].vm_range.GetBaseAddress() == 1953 file_addr) { 1954 // Symbol is in section with zero size, but has the same start address 1955 // as the section. This can happen with linker symbols (symbols that 1956 // start with the letter 'l' or 'L'. 1957 return m_section_infos[n_sect].section_sp; 1958 } 1959 } 1960 return m_section_list->FindSectionContainingFileAddress(file_addr); 1961 } 1962 1963 protected: 1964 struct SectionInfo { 1965 SectionInfo() : vm_range(), section_sp() {} 1966 1967 VMRange vm_range; 1968 SectionSP section_sp; 1969 }; 1970 SectionList *m_section_list; 1971 std::vector<SectionInfo> m_section_infos; 1972 }; 1973 1974 #define TRIE_SYMBOL_IS_THUMB (1ULL << 63) 1975 struct TrieEntry { 1976 void Dump() const { 1977 printf("0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"", 1978 static_cast<unsigned long long>(address), 1979 static_cast<unsigned long long>(flags), 1980 static_cast<unsigned long long>(other), name.GetCString()); 1981 if (import_name) 1982 printf(" -> \"%s\"\n", import_name.GetCString()); 1983 else 1984 printf("\n"); 1985 } 1986 ConstString name; 1987 uint64_t address = LLDB_INVALID_ADDRESS; 1988 uint64_t flags = 1989 0; // EXPORT_SYMBOL_FLAGS_REEXPORT, EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER, 1990 // TRIE_SYMBOL_IS_THUMB 1991 uint64_t other = 0; 1992 ConstString import_name; 1993 }; 1994 1995 struct TrieEntryWithOffset { 1996 lldb::offset_t nodeOffset; 1997 TrieEntry entry; 1998 1999 TrieEntryWithOffset(lldb::offset_t offset) : nodeOffset(offset), entry() {} 2000 2001 void Dump(uint32_t idx) const { 2002 printf("[%3u] 0x%16.16llx: ", idx, 2003 static_cast<unsigned long long>(nodeOffset)); 2004 entry.Dump(); 2005 } 2006 2007 bool operator<(const TrieEntryWithOffset &other) const { 2008 return (nodeOffset < other.nodeOffset); 2009 } 2010 }; 2011 2012 static bool ParseTrieEntries(DataExtractor &data, lldb::offset_t offset, 2013 const bool is_arm, addr_t text_seg_base_addr, 2014 std::vector<llvm::StringRef> &nameSlices, 2015 std::set<lldb::addr_t> &resolver_addresses, 2016 std::vector<TrieEntryWithOffset> &reexports, 2017 std::vector<TrieEntryWithOffset> &ext_symbols) { 2018 if (!data.ValidOffset(offset)) 2019 return true; 2020 2021 // Terminal node -- end of a branch, possibly add this to 2022 // the symbol table or resolver table. 2023 const uint64_t terminalSize = data.GetULEB128(&offset); 2024 lldb::offset_t children_offset = offset + terminalSize; 2025 if (terminalSize != 0) { 2026 TrieEntryWithOffset e(offset); 2027 e.entry.flags = data.GetULEB128(&offset); 2028 const char *import_name = nullptr; 2029 if (e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT) { 2030 e.entry.address = 0; 2031 e.entry.other = data.GetULEB128(&offset); // dylib ordinal 2032 import_name = data.GetCStr(&offset); 2033 } else { 2034 e.entry.address = data.GetULEB128(&offset); 2035 if (text_seg_base_addr != LLDB_INVALID_ADDRESS) 2036 e.entry.address += text_seg_base_addr; 2037 if (e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) { 2038 e.entry.other = data.GetULEB128(&offset); 2039 uint64_t resolver_addr = e.entry.other; 2040 if (text_seg_base_addr != LLDB_INVALID_ADDRESS) 2041 resolver_addr += text_seg_base_addr; 2042 if (is_arm) 2043 resolver_addr &= THUMB_ADDRESS_BIT_MASK; 2044 resolver_addresses.insert(resolver_addr); 2045 } else 2046 e.entry.other = 0; 2047 } 2048 bool add_this_entry = false; 2049 if (Flags(e.entry.flags).Test(EXPORT_SYMBOL_FLAGS_REEXPORT) && 2050 import_name && import_name[0]) { 2051 // add symbols that are reexport symbols with a valid import name. 2052 add_this_entry = true; 2053 } else if (e.entry.flags == 0 && 2054 (import_name == nullptr || import_name[0] == '\0')) { 2055 // add externally visible symbols, in case the nlist record has 2056 // been stripped/omitted. 2057 add_this_entry = true; 2058 } 2059 if (add_this_entry) { 2060 std::string name; 2061 if (!nameSlices.empty()) { 2062 for (auto name_slice : nameSlices) 2063 name.append(name_slice.data(), name_slice.size()); 2064 } 2065 if (name.size() > 1) { 2066 // Skip the leading '_' 2067 e.entry.name.SetCStringWithLength(name.c_str() + 1, name.size() - 1); 2068 } 2069 if (import_name) { 2070 // Skip the leading '_' 2071 e.entry.import_name.SetCString(import_name + 1); 2072 } 2073 if (Flags(e.entry.flags).Test(EXPORT_SYMBOL_FLAGS_REEXPORT)) { 2074 reexports.push_back(e); 2075 } else { 2076 if (is_arm && (e.entry.address & 1)) { 2077 e.entry.flags |= TRIE_SYMBOL_IS_THUMB; 2078 e.entry.address &= THUMB_ADDRESS_BIT_MASK; 2079 } 2080 ext_symbols.push_back(e); 2081 } 2082 } 2083 } 2084 2085 const uint8_t childrenCount = data.GetU8(&children_offset); 2086 for (uint8_t i = 0; i < childrenCount; ++i) { 2087 const char *cstr = data.GetCStr(&children_offset); 2088 if (cstr) 2089 nameSlices.push_back(llvm::StringRef(cstr)); 2090 else 2091 return false; // Corrupt data 2092 lldb::offset_t childNodeOffset = data.GetULEB128(&children_offset); 2093 if (childNodeOffset) { 2094 if (!ParseTrieEntries(data, childNodeOffset, is_arm, text_seg_base_addr, 2095 nameSlices, resolver_addresses, reexports, 2096 ext_symbols)) { 2097 return false; 2098 } 2099 } 2100 nameSlices.pop_back(); 2101 } 2102 return true; 2103 } 2104 2105 static SymbolType GetSymbolType(const char *&symbol_name, 2106 bool &demangled_is_synthesized, 2107 const SectionSP &text_section_sp, 2108 const SectionSP &data_section_sp, 2109 const SectionSP &data_dirty_section_sp, 2110 const SectionSP &data_const_section_sp, 2111 const SectionSP &symbol_section) { 2112 SymbolType type = eSymbolTypeInvalid; 2113 2114 const char *symbol_sect_name = symbol_section->GetName().AsCString(); 2115 if (symbol_section->IsDescendant(text_section_sp.get())) { 2116 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS | 2117 S_ATTR_SELF_MODIFYING_CODE | 2118 S_ATTR_SOME_INSTRUCTIONS)) 2119 type = eSymbolTypeData; 2120 else 2121 type = eSymbolTypeCode; 2122 } else if (symbol_section->IsDescendant(data_section_sp.get()) || 2123 symbol_section->IsDescendant(data_dirty_section_sp.get()) || 2124 symbol_section->IsDescendant(data_const_section_sp.get())) { 2125 if (symbol_sect_name && 2126 ::strstr(symbol_sect_name, "__objc") == symbol_sect_name) { 2127 type = eSymbolTypeRuntime; 2128 2129 if (symbol_name) { 2130 llvm::StringRef symbol_name_ref(symbol_name); 2131 if (symbol_name_ref.startswith("OBJC_")) { 2132 static const llvm::StringRef g_objc_v2_prefix_class("OBJC_CLASS_$_"); 2133 static const llvm::StringRef g_objc_v2_prefix_metaclass( 2134 "OBJC_METACLASS_$_"); 2135 static const llvm::StringRef g_objc_v2_prefix_ivar("OBJC_IVAR_$_"); 2136 if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) { 2137 symbol_name = symbol_name + g_objc_v2_prefix_class.size(); 2138 type = eSymbolTypeObjCClass; 2139 demangled_is_synthesized = true; 2140 } else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass)) { 2141 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size(); 2142 type = eSymbolTypeObjCMetaClass; 2143 demangled_is_synthesized = true; 2144 } else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar)) { 2145 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size(); 2146 type = eSymbolTypeObjCIVar; 2147 demangled_is_synthesized = true; 2148 } 2149 } 2150 } 2151 } else if (symbol_sect_name && 2152 ::strstr(symbol_sect_name, "__gcc_except_tab") == 2153 symbol_sect_name) { 2154 type = eSymbolTypeException; 2155 } else { 2156 type = eSymbolTypeData; 2157 } 2158 } else if (symbol_sect_name && 2159 ::strstr(symbol_sect_name, "__IMPORT") == symbol_sect_name) { 2160 type = eSymbolTypeTrampoline; 2161 } 2162 return type; 2163 } 2164 2165 // Read the UUID out of a dyld_shared_cache file on-disk. 2166 UUID ObjectFileMachO::GetSharedCacheUUID(FileSpec dyld_shared_cache, 2167 const ByteOrder byte_order, 2168 const uint32_t addr_byte_size) { 2169 UUID dsc_uuid; 2170 DataBufferSP DscData = MapFileData( 2171 dyld_shared_cache, sizeof(struct lldb_copy_dyld_cache_header_v1), 0); 2172 if (!DscData) 2173 return dsc_uuid; 2174 DataExtractor dsc_header_data(DscData, byte_order, addr_byte_size); 2175 2176 char version_str[7]; 2177 lldb::offset_t offset = 0; 2178 memcpy(version_str, dsc_header_data.GetData(&offset, 6), 6); 2179 version_str[6] = '\0'; 2180 if (strcmp(version_str, "dyld_v") == 0) { 2181 offset = offsetof(struct lldb_copy_dyld_cache_header_v1, uuid); 2182 dsc_uuid = UUID::fromOptionalData( 2183 dsc_header_data.GetData(&offset, sizeof(uuid_t)), sizeof(uuid_t)); 2184 } 2185 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS)); 2186 if (log && dsc_uuid.IsValid()) { 2187 LLDB_LOGF(log, "Shared cache %s has UUID %s", 2188 dyld_shared_cache.GetPath().c_str(), 2189 dsc_uuid.GetAsString().c_str()); 2190 } 2191 return dsc_uuid; 2192 } 2193 2194 static llvm::Optional<struct nlist_64> 2195 ParseNList(DataExtractor &nlist_data, lldb::offset_t &nlist_data_offset, 2196 size_t nlist_byte_size) { 2197 struct nlist_64 nlist; 2198 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size)) 2199 return {}; 2200 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset); 2201 nlist.n_type = nlist_data.GetU8_unchecked(&nlist_data_offset); 2202 nlist.n_sect = nlist_data.GetU8_unchecked(&nlist_data_offset); 2203 nlist.n_desc = nlist_data.GetU16_unchecked(&nlist_data_offset); 2204 nlist.n_value = nlist_data.GetAddress_unchecked(&nlist_data_offset); 2205 return nlist; 2206 } 2207 2208 enum { DebugSymbols = true, NonDebugSymbols = false }; 2209 2210 size_t ObjectFileMachO::ParseSymtab() { 2211 LLDB_SCOPED_TIMERF("ObjectFileMachO::ParseSymtab () module = %s", 2212 m_file.GetFilename().AsCString("")); 2213 ModuleSP module_sp(GetModule()); 2214 if (!module_sp) 2215 return 0; 2216 2217 Progress progress(llvm::formatv("Parsing symbol table for {0}", 2218 m_file.GetFilename().AsCString("<Unknown>"))); 2219 2220 llvm::MachO::symtab_command symtab_load_command = {0, 0, 0, 0, 0, 0}; 2221 llvm::MachO::linkedit_data_command function_starts_load_command = {0, 0, 0, 0}; 2222 llvm::MachO::dyld_info_command dyld_info = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 2223 // The data element of type bool indicates that this entry is thumb 2224 // code. 2225 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts; 2226 2227 // Record the address of every function/data that we add to the symtab. 2228 // We add symbols to the table in the order of most information (nlist 2229 // records) to least (function starts), and avoid duplicating symbols 2230 // via this set. 2231 llvm::DenseSet<addr_t> symbols_added; 2232 2233 // We are using a llvm::DenseSet for "symbols_added" so we must be sure we 2234 // do not add the tombstone or empty keys to the set. 2235 auto add_symbol_addr = [&symbols_added](lldb::addr_t file_addr) { 2236 // Don't add the tombstone or empty keys. 2237 if (file_addr == UINT64_MAX || file_addr == UINT64_MAX - 1) 2238 return; 2239 symbols_added.insert(file_addr); 2240 }; 2241 FunctionStarts function_starts; 2242 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 2243 uint32_t i; 2244 FileSpecList dylib_files; 2245 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS)); 2246 llvm::StringRef g_objc_v2_prefix_class("_OBJC_CLASS_$_"); 2247 llvm::StringRef g_objc_v2_prefix_metaclass("_OBJC_METACLASS_$_"); 2248 llvm::StringRef g_objc_v2_prefix_ivar("_OBJC_IVAR_$_"); 2249 2250 for (i = 0; i < m_header.ncmds; ++i) { 2251 const lldb::offset_t cmd_offset = offset; 2252 // Read in the load command and load command size 2253 llvm::MachO::load_command lc; 2254 if (m_data.GetU32(&offset, &lc, 2) == nullptr) 2255 break; 2256 // Watch for the symbol table load command 2257 switch (lc.cmd) { 2258 case LC_SYMTAB: 2259 symtab_load_command.cmd = lc.cmd; 2260 symtab_load_command.cmdsize = lc.cmdsize; 2261 // Read in the rest of the symtab load command 2262 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 2263 nullptr) // fill in symoff, nsyms, stroff, strsize fields 2264 return 0; 2265 break; 2266 2267 case LC_DYLD_INFO: 2268 case LC_DYLD_INFO_ONLY: 2269 if (m_data.GetU32(&offset, &dyld_info.rebase_off, 10)) { 2270 dyld_info.cmd = lc.cmd; 2271 dyld_info.cmdsize = lc.cmdsize; 2272 } else { 2273 memset(&dyld_info, 0, sizeof(dyld_info)); 2274 } 2275 break; 2276 2277 case LC_LOAD_DYLIB: 2278 case LC_LOAD_WEAK_DYLIB: 2279 case LC_REEXPORT_DYLIB: 2280 case LC_LOADFVMLIB: 2281 case LC_LOAD_UPWARD_DYLIB: { 2282 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset); 2283 const char *path = m_data.PeekCStr(name_offset); 2284 if (path) { 2285 FileSpec file_spec(path); 2286 // Strip the path if there is @rpath, @executable, etc so we just use 2287 // the basename 2288 if (path[0] == '@') 2289 file_spec.GetDirectory().Clear(); 2290 2291 if (lc.cmd == LC_REEXPORT_DYLIB) { 2292 m_reexported_dylibs.AppendIfUnique(file_spec); 2293 } 2294 2295 dylib_files.Append(file_spec); 2296 } 2297 } break; 2298 2299 case LC_FUNCTION_STARTS: 2300 function_starts_load_command.cmd = lc.cmd; 2301 function_starts_load_command.cmdsize = lc.cmdsize; 2302 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == 2303 nullptr) // fill in symoff, nsyms, stroff, strsize fields 2304 memset(&function_starts_load_command, 0, 2305 sizeof(function_starts_load_command)); 2306 break; 2307 2308 default: 2309 break; 2310 } 2311 offset = cmd_offset + lc.cmdsize; 2312 } 2313 2314 if (!symtab_load_command.cmd) 2315 return 0; 2316 2317 Symtab *symtab = m_symtab_up.get(); 2318 SectionList *section_list = GetSectionList(); 2319 if (section_list == nullptr) 2320 return 0; 2321 2322 const uint32_t addr_byte_size = m_data.GetAddressByteSize(); 2323 const ByteOrder byte_order = m_data.GetByteOrder(); 2324 bool bit_width_32 = addr_byte_size == 4; 2325 const size_t nlist_byte_size = 2326 bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64); 2327 2328 DataExtractor nlist_data(nullptr, 0, byte_order, addr_byte_size); 2329 DataExtractor strtab_data(nullptr, 0, byte_order, addr_byte_size); 2330 DataExtractor function_starts_data(nullptr, 0, byte_order, addr_byte_size); 2331 DataExtractor indirect_symbol_index_data(nullptr, 0, byte_order, 2332 addr_byte_size); 2333 DataExtractor dyld_trie_data(nullptr, 0, byte_order, addr_byte_size); 2334 2335 const addr_t nlist_data_byte_size = 2336 symtab_load_command.nsyms * nlist_byte_size; 2337 const addr_t strtab_data_byte_size = symtab_load_command.strsize; 2338 addr_t strtab_addr = LLDB_INVALID_ADDRESS; 2339 2340 ProcessSP process_sp(m_process_wp.lock()); 2341 Process *process = process_sp.get(); 2342 2343 uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete; 2344 bool is_shared_cache_image = m_header.flags & MH_DYLIB_IN_CACHE; 2345 bool is_local_shared_cache_image = is_shared_cache_image && !IsInMemory(); 2346 SectionSP linkedit_section_sp( 2347 section_list->FindSectionByName(GetSegmentNameLINKEDIT())); 2348 2349 if (process && m_header.filetype != llvm::MachO::MH_OBJECT && 2350 !is_local_shared_cache_image) { 2351 Target &target = process->GetTarget(); 2352 2353 memory_module_load_level = target.GetMemoryModuleLoadLevel(); 2354 2355 // Reading mach file from memory in a process or core file... 2356 2357 if (linkedit_section_sp) { 2358 addr_t linkedit_load_addr = 2359 linkedit_section_sp->GetLoadBaseAddress(&target); 2360 if (linkedit_load_addr == LLDB_INVALID_ADDRESS) { 2361 // We might be trying to access the symbol table before the 2362 // __LINKEDIT's load address has been set in the target. We can't 2363 // fail to read the symbol table, so calculate the right address 2364 // manually 2365 linkedit_load_addr = CalculateSectionLoadAddressForMemoryImage( 2366 m_memory_addr, GetMachHeaderSection(), linkedit_section_sp.get()); 2367 } 2368 2369 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset(); 2370 const addr_t symoff_addr = linkedit_load_addr + 2371 symtab_load_command.symoff - 2372 linkedit_file_offset; 2373 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - 2374 linkedit_file_offset; 2375 2376 // Always load dyld - the dynamic linker - from memory if we didn't 2377 // find a binary anywhere else. lldb will not register 2378 // dylib/framework/bundle loads/unloads if we don't have the dyld 2379 // symbols, we force dyld to load from memory despite the user's 2380 // target.memory-module-load-level setting. 2381 if (memory_module_load_level == eMemoryModuleLoadLevelComplete || 2382 m_header.filetype == llvm::MachO::MH_DYLINKER) { 2383 DataBufferSP nlist_data_sp( 2384 ReadMemory(process_sp, symoff_addr, nlist_data_byte_size)); 2385 if (nlist_data_sp) 2386 nlist_data.SetData(nlist_data_sp, 0, nlist_data_sp->GetByteSize()); 2387 if (m_dysymtab.nindirectsyms != 0) { 2388 const addr_t indirect_syms_addr = linkedit_load_addr + 2389 m_dysymtab.indirectsymoff - 2390 linkedit_file_offset; 2391 DataBufferSP indirect_syms_data_sp(ReadMemory( 2392 process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4)); 2393 if (indirect_syms_data_sp) 2394 indirect_symbol_index_data.SetData( 2395 indirect_syms_data_sp, 0, 2396 indirect_syms_data_sp->GetByteSize()); 2397 // If this binary is outside the shared cache, 2398 // cache the string table. 2399 // Binaries in the shared cache all share a giant string table, 2400 // and we can't share the string tables across multiple 2401 // ObjectFileMachO's, so we'd end up re-reading this mega-strtab 2402 // for every binary in the shared cache - it would be a big perf 2403 // problem. For binaries outside the shared cache, it's faster to 2404 // read the entire strtab at once instead of piece-by-piece as we 2405 // process the nlist records. 2406 if (!is_shared_cache_image) { 2407 DataBufferSP strtab_data_sp( 2408 ReadMemory(process_sp, strtab_addr, strtab_data_byte_size)); 2409 if (strtab_data_sp) { 2410 strtab_data.SetData(strtab_data_sp, 0, 2411 strtab_data_sp->GetByteSize()); 2412 } 2413 } 2414 } 2415 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial) { 2416 if (function_starts_load_command.cmd) { 2417 const addr_t func_start_addr = 2418 linkedit_load_addr + function_starts_load_command.dataoff - 2419 linkedit_file_offset; 2420 DataBufferSP func_start_data_sp( 2421 ReadMemory(process_sp, func_start_addr, 2422 function_starts_load_command.datasize)); 2423 if (func_start_data_sp) 2424 function_starts_data.SetData(func_start_data_sp, 0, 2425 func_start_data_sp->GetByteSize()); 2426 } 2427 } 2428 } 2429 } 2430 } else { 2431 if (is_local_shared_cache_image) { 2432 // The load commands in shared cache images are relative to the 2433 // beginning of the shared cache, not the library image. The 2434 // data we get handed when creating the ObjectFileMachO starts 2435 // at the beginning of a specific library and spans to the end 2436 // of the cache to be able to reach the shared LINKEDIT 2437 // segments. We need to convert the load command offsets to be 2438 // relative to the beginning of our specific image. 2439 lldb::addr_t linkedit_offset = linkedit_section_sp->GetFileOffset(); 2440 lldb::offset_t linkedit_slide = 2441 linkedit_offset - m_linkedit_original_offset; 2442 symtab_load_command.symoff += linkedit_slide; 2443 symtab_load_command.stroff += linkedit_slide; 2444 dyld_info.export_off += linkedit_slide; 2445 m_dysymtab.indirectsymoff += linkedit_slide; 2446 function_starts_load_command.dataoff += linkedit_slide; 2447 } 2448 2449 nlist_data.SetData(m_data, symtab_load_command.symoff, 2450 nlist_data_byte_size); 2451 strtab_data.SetData(m_data, symtab_load_command.stroff, 2452 strtab_data_byte_size); 2453 2454 if (dyld_info.export_size > 0) { 2455 dyld_trie_data.SetData(m_data, dyld_info.export_off, 2456 dyld_info.export_size); 2457 } 2458 2459 if (m_dysymtab.nindirectsyms != 0) { 2460 indirect_symbol_index_data.SetData(m_data, m_dysymtab.indirectsymoff, 2461 m_dysymtab.nindirectsyms * 4); 2462 } 2463 if (function_starts_load_command.cmd) { 2464 function_starts_data.SetData(m_data, function_starts_load_command.dataoff, 2465 function_starts_load_command.datasize); 2466 } 2467 } 2468 2469 const bool have_strtab_data = strtab_data.GetByteSize() > 0; 2470 2471 ConstString g_segment_name_TEXT = GetSegmentNameTEXT(); 2472 ConstString g_segment_name_DATA = GetSegmentNameDATA(); 2473 ConstString g_segment_name_DATA_DIRTY = GetSegmentNameDATA_DIRTY(); 2474 ConstString g_segment_name_DATA_CONST = GetSegmentNameDATA_CONST(); 2475 ConstString g_segment_name_OBJC = GetSegmentNameOBJC(); 2476 ConstString g_section_name_eh_frame = GetSectionNameEHFrame(); 2477 SectionSP text_section_sp( 2478 section_list->FindSectionByName(g_segment_name_TEXT)); 2479 SectionSP data_section_sp( 2480 section_list->FindSectionByName(g_segment_name_DATA)); 2481 SectionSP data_dirty_section_sp( 2482 section_list->FindSectionByName(g_segment_name_DATA_DIRTY)); 2483 SectionSP data_const_section_sp( 2484 section_list->FindSectionByName(g_segment_name_DATA_CONST)); 2485 SectionSP objc_section_sp( 2486 section_list->FindSectionByName(g_segment_name_OBJC)); 2487 SectionSP eh_frame_section_sp; 2488 if (text_section_sp.get()) 2489 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName( 2490 g_section_name_eh_frame); 2491 else 2492 eh_frame_section_sp = 2493 section_list->FindSectionByName(g_section_name_eh_frame); 2494 2495 const bool is_arm = (m_header.cputype == llvm::MachO::CPU_TYPE_ARM); 2496 const bool always_thumb = GetArchitecture().IsAlwaysThumbInstructions(); 2497 2498 // lldb works best if it knows the start address of all functions in a 2499 // module. Linker symbols or debug info are normally the best source of 2500 // information for start addr / size but they may be stripped in a released 2501 // binary. Two additional sources of information exist in Mach-O binaries: 2502 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each 2503 // function's start address in the 2504 // binary, relative to the text section. 2505 // eh_frame - the eh_frame FDEs have the start addr & size of 2506 // each function 2507 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on 2508 // all modern binaries. 2509 // Binaries built to run on older releases may need to use eh_frame 2510 // information. 2511 2512 if (text_section_sp && function_starts_data.GetByteSize()) { 2513 FunctionStarts::Entry function_start_entry; 2514 function_start_entry.data = false; 2515 lldb::offset_t function_start_offset = 0; 2516 function_start_entry.addr = text_section_sp->GetFileAddress(); 2517 uint64_t delta; 2518 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 2519 0) { 2520 // Now append the current entry 2521 function_start_entry.addr += delta; 2522 if (is_arm) { 2523 if (function_start_entry.addr & 1) { 2524 function_start_entry.addr &= THUMB_ADDRESS_BIT_MASK; 2525 function_start_entry.data = true; 2526 } else if (always_thumb) { 2527 function_start_entry.data = true; 2528 } 2529 } 2530 function_starts.Append(function_start_entry); 2531 } 2532 } else { 2533 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the 2534 // load command claiming an eh_frame but it doesn't actually have the 2535 // eh_frame content. And if we have a dSYM, we don't need to do any of 2536 // this fill-in-the-missing-symbols works anyway - the debug info should 2537 // give us all the functions in the module. 2538 if (text_section_sp.get() && eh_frame_section_sp.get() && 2539 m_type != eTypeDebugInfo) { 2540 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, 2541 DWARFCallFrameInfo::EH); 2542 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions; 2543 eh_frame.GetFunctionAddressAndSizeVector(functions); 2544 addr_t text_base_addr = text_section_sp->GetFileAddress(); 2545 size_t count = functions.GetSize(); 2546 for (size_t i = 0; i < count; ++i) { 2547 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = 2548 functions.GetEntryAtIndex(i); 2549 if (func) { 2550 FunctionStarts::Entry function_start_entry; 2551 function_start_entry.addr = func->base - text_base_addr; 2552 if (is_arm) { 2553 if (function_start_entry.addr & 1) { 2554 function_start_entry.addr &= THUMB_ADDRESS_BIT_MASK; 2555 function_start_entry.data = true; 2556 } else if (always_thumb) { 2557 function_start_entry.data = true; 2558 } 2559 } 2560 function_starts.Append(function_start_entry); 2561 } 2562 } 2563 } 2564 } 2565 2566 const size_t function_starts_count = function_starts.GetSize(); 2567 2568 // For user process binaries (executables, dylibs, frameworks, bundles), if 2569 // we don't have LC_FUNCTION_STARTS/eh_frame section in this binary, we're 2570 // going to assume the binary has been stripped. Don't allow assembly 2571 // language instruction emulation because we don't know proper function 2572 // start boundaries. 2573 // 2574 // For all other types of binaries (kernels, stand-alone bare board 2575 // binaries, kexts), they may not have LC_FUNCTION_STARTS / eh_frame 2576 // sections - we should not make any assumptions about them based on that. 2577 if (function_starts_count == 0 && CalculateStrata() == eStrataUser) { 2578 m_allow_assembly_emulation_unwind_plans = false; 2579 Log *unwind_or_symbol_log(lldb_private::GetLogIfAnyCategoriesSet( 2580 LIBLLDB_LOG_SYMBOLS | LIBLLDB_LOG_UNWIND)); 2581 2582 if (unwind_or_symbol_log) 2583 module_sp->LogMessage( 2584 unwind_or_symbol_log, 2585 "no LC_FUNCTION_STARTS, will not allow assembly profiled unwinds"); 2586 } 2587 2588 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() 2589 ? eh_frame_section_sp->GetID() 2590 : static_cast<user_id_t>(NO_SECT); 2591 2592 lldb::offset_t nlist_data_offset = 0; 2593 2594 uint32_t N_SO_index = UINT32_MAX; 2595 2596 MachSymtabSectionInfo section_info(section_list); 2597 std::vector<uint32_t> N_FUN_indexes; 2598 std::vector<uint32_t> N_NSYM_indexes; 2599 std::vector<uint32_t> N_INCL_indexes; 2600 std::vector<uint32_t> N_BRAC_indexes; 2601 std::vector<uint32_t> N_COMM_indexes; 2602 typedef std::multimap<uint64_t, uint32_t> ValueToSymbolIndexMap; 2603 typedef llvm::DenseMap<uint32_t, uint32_t> NListIndexToSymbolIndexMap; 2604 typedef llvm::DenseMap<const char *, uint32_t> ConstNameToSymbolIndexMap; 2605 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx; 2606 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx; 2607 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx; 2608 // Any symbols that get merged into another will get an entry in this map 2609 // so we know 2610 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx; 2611 uint32_t nlist_idx = 0; 2612 Symbol *symbol_ptr = nullptr; 2613 2614 uint32_t sym_idx = 0; 2615 Symbol *sym = nullptr; 2616 size_t num_syms = 0; 2617 std::string memory_symbol_name; 2618 uint32_t unmapped_local_symbols_found = 0; 2619 2620 std::vector<TrieEntryWithOffset> reexport_trie_entries; 2621 std::vector<TrieEntryWithOffset> external_sym_trie_entries; 2622 std::set<lldb::addr_t> resolver_addresses; 2623 2624 if (dyld_trie_data.GetByteSize() > 0) { 2625 ConstString text_segment_name("__TEXT"); 2626 SectionSP text_segment_sp = 2627 GetSectionList()->FindSectionByName(text_segment_name); 2628 lldb::addr_t text_segment_file_addr = LLDB_INVALID_ADDRESS; 2629 if (text_segment_sp) 2630 text_segment_file_addr = text_segment_sp->GetFileAddress(); 2631 std::vector<llvm::StringRef> nameSlices; 2632 ParseTrieEntries(dyld_trie_data, 0, is_arm, text_segment_file_addr, 2633 nameSlices, resolver_addresses, reexport_trie_entries, 2634 external_sym_trie_entries); 2635 } 2636 2637 typedef std::set<ConstString> IndirectSymbols; 2638 IndirectSymbols indirect_symbol_names; 2639 2640 #if TARGET_OS_IPHONE 2641 2642 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been 2643 // optimized by moving LOCAL symbols out of the memory mapped portion of 2644 // the DSC. The symbol information has all been retained, but it isn't 2645 // available in the normal nlist data. However, there *are* duplicate 2646 // entries of *some* 2647 // LOCAL symbols in the normal nlist data. To handle this situation 2648 // correctly, we must first attempt 2649 // to parse any DSC unmapped symbol information. If we find any, we set a 2650 // flag that tells the normal nlist parser to ignore all LOCAL symbols. 2651 2652 if (m_header.flags & MH_DYLIB_IN_CACHE) { 2653 // Before we can start mapping the DSC, we need to make certain the 2654 // target process is actually using the cache we can find. 2655 2656 // Next we need to determine the correct path for the dyld shared cache. 2657 2658 ArchSpec header_arch = GetArchitecture(); 2659 char dsc_path[PATH_MAX]; 2660 char dsc_path_development[PATH_MAX]; 2661 2662 snprintf( 2663 dsc_path, sizeof(dsc_path), "%s%s%s", 2664 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR 2665 */ 2666 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */ 2667 header_arch.GetArchitectureName()); 2668 2669 snprintf( 2670 dsc_path_development, sizeof(dsc_path), "%s%s%s%s", 2671 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR 2672 */ 2673 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */ 2674 header_arch.GetArchitectureName(), ".development"); 2675 2676 FileSpec dsc_nondevelopment_filespec(dsc_path); 2677 FileSpec dsc_development_filespec(dsc_path_development); 2678 FileSpec dsc_filespec; 2679 2680 UUID dsc_uuid; 2681 UUID process_shared_cache_uuid; 2682 addr_t process_shared_cache_base_addr; 2683 2684 if (process) { 2685 GetProcessSharedCacheUUID(process, process_shared_cache_base_addr, 2686 process_shared_cache_uuid); 2687 } 2688 2689 // First see if we can find an exact match for the inferior process 2690 // shared cache UUID in the development or non-development shared caches 2691 // on disk. 2692 if (process_shared_cache_uuid.IsValid()) { 2693 if (FileSystem::Instance().Exists(dsc_development_filespec)) { 2694 UUID dsc_development_uuid = GetSharedCacheUUID( 2695 dsc_development_filespec, byte_order, addr_byte_size); 2696 if (dsc_development_uuid.IsValid() && 2697 dsc_development_uuid == process_shared_cache_uuid) { 2698 dsc_filespec = dsc_development_filespec; 2699 dsc_uuid = dsc_development_uuid; 2700 } 2701 } 2702 if (!dsc_uuid.IsValid() && 2703 FileSystem::Instance().Exists(dsc_nondevelopment_filespec)) { 2704 UUID dsc_nondevelopment_uuid = GetSharedCacheUUID( 2705 dsc_nondevelopment_filespec, byte_order, addr_byte_size); 2706 if (dsc_nondevelopment_uuid.IsValid() && 2707 dsc_nondevelopment_uuid == process_shared_cache_uuid) { 2708 dsc_filespec = dsc_nondevelopment_filespec; 2709 dsc_uuid = dsc_nondevelopment_uuid; 2710 } 2711 } 2712 } 2713 2714 // Failing a UUID match, prefer the development dyld_shared cache if both 2715 // are present. 2716 if (!FileSystem::Instance().Exists(dsc_filespec)) { 2717 if (FileSystem::Instance().Exists(dsc_development_filespec)) { 2718 dsc_filespec = dsc_development_filespec; 2719 } else { 2720 dsc_filespec = dsc_nondevelopment_filespec; 2721 } 2722 } 2723 2724 /* The dyld_cache_header has a pointer to the 2725 dyld_cache_local_symbols_info structure (localSymbolsOffset). 2726 The dyld_cache_local_symbols_info structure gives us three things: 2727 1. The start and count of the nlist records in the dyld_shared_cache 2728 file 2729 2. The start and size of the strings for these nlist records 2730 3. The start and count of dyld_cache_local_symbols_entry entries 2731 2732 There is one dyld_cache_local_symbols_entry per dylib/framework in the 2733 dyld shared cache. 2734 The "dylibOffset" field is the Mach-O header of this dylib/framework in 2735 the dyld shared cache. 2736 The dyld_cache_local_symbols_entry also lists the start of this 2737 dylib/framework's nlist records 2738 and the count of how many nlist records there are for this 2739 dylib/framework. 2740 */ 2741 2742 // Process the dyld shared cache header to find the unmapped symbols 2743 2744 DataBufferSP dsc_data_sp = MapFileData( 2745 dsc_filespec, sizeof(struct lldb_copy_dyld_cache_header_v1), 0); 2746 if (!dsc_uuid.IsValid()) { 2747 dsc_uuid = GetSharedCacheUUID(dsc_filespec, byte_order, addr_byte_size); 2748 } 2749 if (dsc_data_sp) { 2750 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size); 2751 2752 bool uuid_match = true; 2753 if (dsc_uuid.IsValid() && process) { 2754 if (process_shared_cache_uuid.IsValid() && 2755 dsc_uuid != process_shared_cache_uuid) { 2756 // The on-disk dyld_shared_cache file is not the same as the one in 2757 // this process' memory, don't use it. 2758 uuid_match = false; 2759 ModuleSP module_sp(GetModule()); 2760 if (module_sp) 2761 module_sp->ReportWarning("process shared cache does not match " 2762 "on-disk dyld_shared_cache file, some " 2763 "symbol names will be missing."); 2764 } 2765 } 2766 2767 offset = offsetof(struct lldb_copy_dyld_cache_header_v1, mappingOffset); 2768 2769 uint32_t mappingOffset = dsc_header_data.GetU32(&offset); 2770 2771 // If the mappingOffset points to a location inside the header, we've 2772 // opened an old dyld shared cache, and should not proceed further. 2773 if (uuid_match && 2774 mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v1)) { 2775 2776 DataBufferSP dsc_mapping_info_data_sp = MapFileData( 2777 dsc_filespec, sizeof(struct lldb_copy_dyld_cache_mapping_info), 2778 mappingOffset); 2779 2780 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, 2781 byte_order, addr_byte_size); 2782 offset = 0; 2783 2784 // The File addresses (from the in-memory Mach-O load commands) for 2785 // the shared libraries in the shared library cache need to be 2786 // adjusted by an offset to match up with the dylibOffset identifying 2787 // field in the dyld_cache_local_symbol_entry's. This offset is 2788 // recorded in mapping_offset_value. 2789 const uint64_t mapping_offset_value = 2790 dsc_mapping_info_data.GetU64(&offset); 2791 2792 offset = 2793 offsetof(struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset); 2794 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset); 2795 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset); 2796 2797 if (localSymbolsOffset && localSymbolsSize) { 2798 // Map the local symbols 2799 DataBufferSP dsc_local_symbols_data_sp = 2800 MapFileData(dsc_filespec, localSymbolsSize, localSymbolsOffset); 2801 2802 if (dsc_local_symbols_data_sp) { 2803 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, 2804 byte_order, addr_byte_size); 2805 2806 offset = 0; 2807 2808 typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap; 2809 typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName; 2810 UndefinedNameToDescMap undefined_name_to_desc; 2811 SymbolIndexToName reexport_shlib_needs_fixup; 2812 2813 // Read the local_symbols_infos struct in one shot 2814 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info; 2815 dsc_local_symbols_data.GetU32(&offset, 2816 &local_symbols_info.nlistOffset, 6); 2817 2818 SectionSP text_section_sp( 2819 section_list->FindSectionByName(GetSegmentNameTEXT())); 2820 2821 uint32_t header_file_offset = 2822 (text_section_sp->GetFileAddress() - mapping_offset_value); 2823 2824 offset = local_symbols_info.entriesOffset; 2825 for (uint32_t entry_index = 0; 2826 entry_index < local_symbols_info.entriesCount; entry_index++) { 2827 struct lldb_copy_dyld_cache_local_symbols_entry 2828 local_symbols_entry; 2829 local_symbols_entry.dylibOffset = 2830 dsc_local_symbols_data.GetU32(&offset); 2831 local_symbols_entry.nlistStartIndex = 2832 dsc_local_symbols_data.GetU32(&offset); 2833 local_symbols_entry.nlistCount = 2834 dsc_local_symbols_data.GetU32(&offset); 2835 2836 if (header_file_offset == local_symbols_entry.dylibOffset) { 2837 unmapped_local_symbols_found = local_symbols_entry.nlistCount; 2838 2839 // The normal nlist code cannot correctly size the Symbols 2840 // array, we need to allocate it here. 2841 sym = symtab->Resize( 2842 symtab_load_command.nsyms + m_dysymtab.nindirectsyms + 2843 unmapped_local_symbols_found - m_dysymtab.nlocalsym); 2844 num_syms = symtab->GetNumSymbols(); 2845 2846 nlist_data_offset = 2847 local_symbols_info.nlistOffset + 2848 (nlist_byte_size * local_symbols_entry.nlistStartIndex); 2849 uint32_t string_table_offset = local_symbols_info.stringsOffset; 2850 2851 for (uint32_t nlist_index = 0; 2852 nlist_index < local_symbols_entry.nlistCount; 2853 nlist_index++) { 2854 ///////////////////////////// 2855 { 2856 llvm::Optional<struct nlist_64> nlist_maybe = 2857 ParseNList(dsc_local_symbols_data, nlist_data_offset, 2858 nlist_byte_size); 2859 if (!nlist_maybe) 2860 break; 2861 struct nlist_64 nlist = *nlist_maybe; 2862 2863 SymbolType type = eSymbolTypeInvalid; 2864 const char *symbol_name = dsc_local_symbols_data.PeekCStr( 2865 string_table_offset + nlist.n_strx); 2866 2867 if (symbol_name == NULL) { 2868 // No symbol should be NULL, even the symbols with no 2869 // string values should have an offset zero which 2870 // points to an empty C-string 2871 Host::SystemLog( 2872 Host::eSystemLogError, 2873 "error: DSC unmapped local symbol[%u] has invalid " 2874 "string table offset 0x%x in %s, ignoring symbol\n", 2875 entry_index, nlist.n_strx, 2876 module_sp->GetFileSpec().GetPath().c_str()); 2877 continue; 2878 } 2879 if (symbol_name[0] == '\0') 2880 symbol_name = NULL; 2881 2882 const char *symbol_name_non_abi_mangled = NULL; 2883 2884 SectionSP symbol_section; 2885 uint32_t symbol_byte_size = 0; 2886 bool add_nlist = true; 2887 bool is_debug = ((nlist.n_type & N_STAB) != 0); 2888 bool demangled_is_synthesized = false; 2889 bool is_gsym = false; 2890 bool set_value = true; 2891 2892 assert(sym_idx < num_syms); 2893 2894 sym[sym_idx].SetDebug(is_debug); 2895 2896 if (is_debug) { 2897 switch (nlist.n_type) { 2898 case N_GSYM: 2899 // global symbol: name,,NO_SECT,type,0 2900 // Sometimes the N_GSYM value contains the address. 2901 2902 // FIXME: In the .o files, we have a GSYM and a debug 2903 // symbol for all the ObjC data. They 2904 // have the same address, but we want to ensure that 2905 // we always find only the real symbol, 'cause we 2906 // don't currently correctly attribute the 2907 // GSYM one to the ObjCClass/Ivar/MetaClass 2908 // symbol type. This is a temporary hack to make 2909 // sure the ObjectiveC symbols get treated correctly. 2910 // To do this right, we should coalesce all the GSYM 2911 // & global symbols that have the same address. 2912 2913 is_gsym = true; 2914 sym[sym_idx].SetExternal(true); 2915 2916 if (symbol_name && symbol_name[0] == '_' && 2917 symbol_name[1] == 'O') { 2918 llvm::StringRef symbol_name_ref(symbol_name); 2919 if (symbol_name_ref.startswith( 2920 g_objc_v2_prefix_class)) { 2921 symbol_name_non_abi_mangled = symbol_name + 1; 2922 symbol_name = 2923 symbol_name + g_objc_v2_prefix_class.size(); 2924 type = eSymbolTypeObjCClass; 2925 demangled_is_synthesized = true; 2926 2927 } else if (symbol_name_ref.startswith( 2928 g_objc_v2_prefix_metaclass)) { 2929 symbol_name_non_abi_mangled = symbol_name + 1; 2930 symbol_name = 2931 symbol_name + g_objc_v2_prefix_metaclass.size(); 2932 type = eSymbolTypeObjCMetaClass; 2933 demangled_is_synthesized = true; 2934 } else if (symbol_name_ref.startswith( 2935 g_objc_v2_prefix_ivar)) { 2936 symbol_name_non_abi_mangled = symbol_name + 1; 2937 symbol_name = 2938 symbol_name + g_objc_v2_prefix_ivar.size(); 2939 type = eSymbolTypeObjCIVar; 2940 demangled_is_synthesized = true; 2941 } 2942 } else { 2943 if (nlist.n_value != 0) 2944 symbol_section = section_info.GetSection( 2945 nlist.n_sect, nlist.n_value); 2946 type = eSymbolTypeData; 2947 } 2948 break; 2949 2950 case N_FNAME: 2951 // procedure name (f77 kludge): name,,NO_SECT,0,0 2952 type = eSymbolTypeCompiler; 2953 break; 2954 2955 case N_FUN: 2956 // procedure: name,,n_sect,linenumber,address 2957 if (symbol_name) { 2958 type = eSymbolTypeCode; 2959 symbol_section = section_info.GetSection( 2960 nlist.n_sect, nlist.n_value); 2961 2962 N_FUN_addr_to_sym_idx.insert( 2963 std::make_pair(nlist.n_value, sym_idx)); 2964 // We use the current number of symbols in the 2965 // symbol table in lieu of using nlist_idx in case 2966 // we ever start trimming entries out 2967 N_FUN_indexes.push_back(sym_idx); 2968 } else { 2969 type = eSymbolTypeCompiler; 2970 2971 if (!N_FUN_indexes.empty()) { 2972 // Copy the size of the function into the 2973 // original 2974 // STAB entry so we don't have 2975 // to hunt for it later 2976 symtab->SymbolAtIndex(N_FUN_indexes.back()) 2977 ->SetByteSize(nlist.n_value); 2978 N_FUN_indexes.pop_back(); 2979 // We don't really need the end function STAB as 2980 // it contains the size which we already placed 2981 // with the original symbol, so don't add it if 2982 // we want a minimal symbol table 2983 add_nlist = false; 2984 } 2985 } 2986 break; 2987 2988 case N_STSYM: 2989 // static symbol: name,,n_sect,type,address 2990 N_STSYM_addr_to_sym_idx.insert( 2991 std::make_pair(nlist.n_value, sym_idx)); 2992 symbol_section = section_info.GetSection(nlist.n_sect, 2993 nlist.n_value); 2994 if (symbol_name && symbol_name[0]) { 2995 type = ObjectFile::GetSymbolTypeFromName( 2996 symbol_name + 1, eSymbolTypeData); 2997 } 2998 break; 2999 3000 case N_LCSYM: 3001 // .lcomm symbol: name,,n_sect,type,address 3002 symbol_section = section_info.GetSection(nlist.n_sect, 3003 nlist.n_value); 3004 type = eSymbolTypeCommonBlock; 3005 break; 3006 3007 case N_BNSYM: 3008 // We use the current number of symbols in the symbol 3009 // table in lieu of using nlist_idx in case we ever 3010 // start trimming entries out Skip these if we want 3011 // minimal symbol tables 3012 add_nlist = false; 3013 break; 3014 3015 case N_ENSYM: 3016 // Set the size of the N_BNSYM to the terminating 3017 // index of this N_ENSYM so that we can always skip 3018 // the entire symbol if we need to navigate more 3019 // quickly at the source level when parsing STABS 3020 // Skip these if we want minimal symbol tables 3021 add_nlist = false; 3022 break; 3023 3024 case N_OPT: 3025 // emitted with gcc2_compiled and in gcc source 3026 type = eSymbolTypeCompiler; 3027 break; 3028 3029 case N_RSYM: 3030 // register sym: name,,NO_SECT,type,register 3031 type = eSymbolTypeVariable; 3032 break; 3033 3034 case N_SLINE: 3035 // src line: 0,,n_sect,linenumber,address 3036 symbol_section = section_info.GetSection(nlist.n_sect, 3037 nlist.n_value); 3038 type = eSymbolTypeLineEntry; 3039 break; 3040 3041 case N_SSYM: 3042 // structure elt: name,,NO_SECT,type,struct_offset 3043 type = eSymbolTypeVariableType; 3044 break; 3045 3046 case N_SO: 3047 // source file name 3048 type = eSymbolTypeSourceFile; 3049 if (symbol_name == NULL) { 3050 add_nlist = false; 3051 if (N_SO_index != UINT32_MAX) { 3052 // Set the size of the N_SO to the terminating 3053 // index of this N_SO so that we can always skip 3054 // the entire N_SO if we need to navigate more 3055 // quickly at the source level when parsing STABS 3056 symbol_ptr = symtab->SymbolAtIndex(N_SO_index); 3057 symbol_ptr->SetByteSize(sym_idx); 3058 symbol_ptr->SetSizeIsSibling(true); 3059 } 3060 N_NSYM_indexes.clear(); 3061 N_INCL_indexes.clear(); 3062 N_BRAC_indexes.clear(); 3063 N_COMM_indexes.clear(); 3064 N_FUN_indexes.clear(); 3065 N_SO_index = UINT32_MAX; 3066 } else { 3067 // We use the current number of symbols in the 3068 // symbol table in lieu of using nlist_idx in case 3069 // we ever start trimming entries out 3070 const bool N_SO_has_full_path = symbol_name[0] == '/'; 3071 if (N_SO_has_full_path) { 3072 if ((N_SO_index == sym_idx - 1) && 3073 ((sym_idx - 1) < num_syms)) { 3074 // We have two consecutive N_SO entries where 3075 // the first contains a directory and the 3076 // second contains a full path. 3077 sym[sym_idx - 1].GetMangled().SetValue( 3078 ConstString(symbol_name), false); 3079 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; 3080 add_nlist = false; 3081 } else { 3082 // This is the first entry in a N_SO that 3083 // contains a directory or 3084 // a full path to the source file 3085 N_SO_index = sym_idx; 3086 } 3087 } else if ((N_SO_index == sym_idx - 1) && 3088 ((sym_idx - 1) < num_syms)) { 3089 // This is usually the second N_SO entry that 3090 // contains just the filename, so here we combine 3091 // it with the first one if we are minimizing the 3092 // symbol table 3093 const char *so_path = sym[sym_idx - 1] 3094 .GetMangled() 3095 .GetDemangledName() 3096 .AsCString(); 3097 if (so_path && so_path[0]) { 3098 std::string full_so_path(so_path); 3099 const size_t double_slash_pos = 3100 full_so_path.find("//"); 3101 if (double_slash_pos != std::string::npos) { 3102 // The linker has been generating bad N_SO 3103 // entries with doubled up paths 3104 // in the format "%s%s" where the first 3105 // string in the DW_AT_comp_dir, and the 3106 // second is the directory for the source 3107 // file so you end up with a path that looks 3108 // like "/tmp/src//tmp/src/" 3109 FileSpec so_dir(so_path); 3110 if (!FileSystem::Instance().Exists(so_dir)) { 3111 so_dir.SetFile( 3112 &full_so_path[double_slash_pos + 1], 3113 FileSpec::Style::native); 3114 if (FileSystem::Instance().Exists(so_dir)) { 3115 // Trim off the incorrect path 3116 full_so_path.erase(0, double_slash_pos + 1); 3117 } 3118 } 3119 } 3120 if (*full_so_path.rbegin() != '/') 3121 full_so_path += '/'; 3122 full_so_path += symbol_name; 3123 sym[sym_idx - 1].GetMangled().SetValue( 3124 ConstString(full_so_path.c_str()), false); 3125 add_nlist = false; 3126 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; 3127 } 3128 } else { 3129 // This could be a relative path to a N_SO 3130 N_SO_index = sym_idx; 3131 } 3132 } 3133 break; 3134 3135 case N_OSO: 3136 // object file name: name,,0,0,st_mtime 3137 type = eSymbolTypeObjectFile; 3138 break; 3139 3140 case N_LSYM: 3141 // local sym: name,,NO_SECT,type,offset 3142 type = eSymbolTypeLocal; 3143 break; 3144 3145 // INCL scopes 3146 case N_BINCL: 3147 // include file beginning: name,,NO_SECT,0,sum We use 3148 // the current number of symbols in the symbol table 3149 // in lieu of using nlist_idx in case we ever start 3150 // trimming entries out 3151 N_INCL_indexes.push_back(sym_idx); 3152 type = eSymbolTypeScopeBegin; 3153 break; 3154 3155 case N_EINCL: 3156 // include file end: name,,NO_SECT,0,0 3157 // Set the size of the N_BINCL to the terminating 3158 // index of this N_EINCL so that we can always skip 3159 // the entire symbol if we need to navigate more 3160 // quickly at the source level when parsing STABS 3161 if (!N_INCL_indexes.empty()) { 3162 symbol_ptr = 3163 symtab->SymbolAtIndex(N_INCL_indexes.back()); 3164 symbol_ptr->SetByteSize(sym_idx + 1); 3165 symbol_ptr->SetSizeIsSibling(true); 3166 N_INCL_indexes.pop_back(); 3167 } 3168 type = eSymbolTypeScopeEnd; 3169 break; 3170 3171 case N_SOL: 3172 // #included file name: name,,n_sect,0,address 3173 type = eSymbolTypeHeaderFile; 3174 3175 // We currently don't use the header files on darwin 3176 add_nlist = false; 3177 break; 3178 3179 case N_PARAMS: 3180 // compiler parameters: name,,NO_SECT,0,0 3181 type = eSymbolTypeCompiler; 3182 break; 3183 3184 case N_VERSION: 3185 // compiler version: name,,NO_SECT,0,0 3186 type = eSymbolTypeCompiler; 3187 break; 3188 3189 case N_OLEVEL: 3190 // compiler -O level: name,,NO_SECT,0,0 3191 type = eSymbolTypeCompiler; 3192 break; 3193 3194 case N_PSYM: 3195 // parameter: name,,NO_SECT,type,offset 3196 type = eSymbolTypeVariable; 3197 break; 3198 3199 case N_ENTRY: 3200 // alternate entry: name,,n_sect,linenumber,address 3201 symbol_section = section_info.GetSection(nlist.n_sect, 3202 nlist.n_value); 3203 type = eSymbolTypeLineEntry; 3204 break; 3205 3206 // Left and Right Braces 3207 case N_LBRAC: 3208 // left bracket: 0,,NO_SECT,nesting level,address We 3209 // use the current number of symbols in the symbol 3210 // table in lieu of using nlist_idx in case we ever 3211 // start trimming entries out 3212 symbol_section = section_info.GetSection(nlist.n_sect, 3213 nlist.n_value); 3214 N_BRAC_indexes.push_back(sym_idx); 3215 type = eSymbolTypeScopeBegin; 3216 break; 3217 3218 case N_RBRAC: 3219 // right bracket: 0,,NO_SECT,nesting level,address 3220 // Set the size of the N_LBRAC to the terminating 3221 // index of this N_RBRAC so that we can always skip 3222 // the entire symbol if we need to navigate more 3223 // quickly at the source level when parsing STABS 3224 symbol_section = section_info.GetSection(nlist.n_sect, 3225 nlist.n_value); 3226 if (!N_BRAC_indexes.empty()) { 3227 symbol_ptr = 3228 symtab->SymbolAtIndex(N_BRAC_indexes.back()); 3229 symbol_ptr->SetByteSize(sym_idx + 1); 3230 symbol_ptr->SetSizeIsSibling(true); 3231 N_BRAC_indexes.pop_back(); 3232 } 3233 type = eSymbolTypeScopeEnd; 3234 break; 3235 3236 case N_EXCL: 3237 // deleted include file: name,,NO_SECT,0,sum 3238 type = eSymbolTypeHeaderFile; 3239 break; 3240 3241 // COMM scopes 3242 case N_BCOMM: 3243 // begin common: name,,NO_SECT,0,0 3244 // We use the current number of symbols in the symbol 3245 // table in lieu of using nlist_idx in case we ever 3246 // start trimming entries out 3247 type = eSymbolTypeScopeBegin; 3248 N_COMM_indexes.push_back(sym_idx); 3249 break; 3250 3251 case N_ECOML: 3252 // end common (local name): 0,,n_sect,0,address 3253 symbol_section = section_info.GetSection(nlist.n_sect, 3254 nlist.n_value); 3255 // Fall through 3256 3257 case N_ECOMM: 3258 // end common: name,,n_sect,0,0 3259 // Set the size of the N_BCOMM to the terminating 3260 // index of this N_ECOMM/N_ECOML so that we can 3261 // always skip the entire symbol if we need to 3262 // navigate more quickly at the source level when 3263 // parsing STABS 3264 if (!N_COMM_indexes.empty()) { 3265 symbol_ptr = 3266 symtab->SymbolAtIndex(N_COMM_indexes.back()); 3267 symbol_ptr->SetByteSize(sym_idx + 1); 3268 symbol_ptr->SetSizeIsSibling(true); 3269 N_COMM_indexes.pop_back(); 3270 } 3271 type = eSymbolTypeScopeEnd; 3272 break; 3273 3274 case N_LENG: 3275 // second stab entry with length information 3276 type = eSymbolTypeAdditional; 3277 break; 3278 3279 default: 3280 break; 3281 } 3282 } else { 3283 // uint8_t n_pext = N_PEXT & nlist.n_type; 3284 uint8_t n_type = N_TYPE & nlist.n_type; 3285 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0); 3286 3287 switch (n_type) { 3288 case N_INDR: { 3289 const char *reexport_name_cstr = 3290 strtab_data.PeekCStr(nlist.n_value); 3291 if (reexport_name_cstr && reexport_name_cstr[0]) { 3292 type = eSymbolTypeReExported; 3293 ConstString reexport_name( 3294 reexport_name_cstr + 3295 ((reexport_name_cstr[0] == '_') ? 1 : 0)); 3296 sym[sym_idx].SetReExportedSymbolName(reexport_name); 3297 set_value = false; 3298 reexport_shlib_needs_fixup[sym_idx] = reexport_name; 3299 indirect_symbol_names.insert(ConstString( 3300 symbol_name + ((symbol_name[0] == '_') ? 1 : 0))); 3301 } else 3302 type = eSymbolTypeUndefined; 3303 } break; 3304 3305 case N_UNDF: 3306 if (symbol_name && symbol_name[0]) { 3307 ConstString undefined_name( 3308 symbol_name + ((symbol_name[0] == '_') ? 1 : 0)); 3309 undefined_name_to_desc[undefined_name] = nlist.n_desc; 3310 } 3311 // Fall through 3312 case N_PBUD: 3313 type = eSymbolTypeUndefined; 3314 break; 3315 3316 case N_ABS: 3317 type = eSymbolTypeAbsolute; 3318 break; 3319 3320 case N_SECT: { 3321 symbol_section = section_info.GetSection(nlist.n_sect, 3322 nlist.n_value); 3323 3324 if (symbol_section == NULL) { 3325 // TODO: warn about this? 3326 add_nlist = false; 3327 break; 3328 } 3329 3330 if (TEXT_eh_frame_sectID == nlist.n_sect) { 3331 type = eSymbolTypeException; 3332 } else { 3333 uint32_t section_type = 3334 symbol_section->Get() & SECTION_TYPE; 3335 3336 switch (section_type) { 3337 case S_CSTRING_LITERALS: 3338 type = eSymbolTypeData; 3339 break; // section with only literal C strings 3340 case S_4BYTE_LITERALS: 3341 type = eSymbolTypeData; 3342 break; // section with only 4 byte literals 3343 case S_8BYTE_LITERALS: 3344 type = eSymbolTypeData; 3345 break; // section with only 8 byte literals 3346 case S_LITERAL_POINTERS: 3347 type = eSymbolTypeTrampoline; 3348 break; // section with only pointers to literals 3349 case S_NON_LAZY_SYMBOL_POINTERS: 3350 type = eSymbolTypeTrampoline; 3351 break; // section with only non-lazy symbol 3352 // pointers 3353 case S_LAZY_SYMBOL_POINTERS: 3354 type = eSymbolTypeTrampoline; 3355 break; // section with only lazy symbol pointers 3356 case S_SYMBOL_STUBS: 3357 type = eSymbolTypeTrampoline; 3358 break; // section with only symbol stubs, byte 3359 // size of stub in the reserved2 field 3360 case S_MOD_INIT_FUNC_POINTERS: 3361 type = eSymbolTypeCode; 3362 break; // section with only function pointers for 3363 // initialization 3364 case S_MOD_TERM_FUNC_POINTERS: 3365 type = eSymbolTypeCode; 3366 break; // section with only function pointers for 3367 // termination 3368 case S_INTERPOSING: 3369 type = eSymbolTypeTrampoline; 3370 break; // section with only pairs of function 3371 // pointers for interposing 3372 case S_16BYTE_LITERALS: 3373 type = eSymbolTypeData; 3374 break; // section with only 16 byte literals 3375 case S_DTRACE_DOF: 3376 type = eSymbolTypeInstrumentation; 3377 break; 3378 case S_LAZY_DYLIB_SYMBOL_POINTERS: 3379 type = eSymbolTypeTrampoline; 3380 break; 3381 default: 3382 switch (symbol_section->GetType()) { 3383 case lldb::eSectionTypeCode: 3384 type = eSymbolTypeCode; 3385 break; 3386 case eSectionTypeData: 3387 case eSectionTypeDataCString: // Inlined C string 3388 // data 3389 case eSectionTypeDataCStringPointers: // Pointers 3390 // to C 3391 // string 3392 // data 3393 case eSectionTypeDataSymbolAddress: // Address of 3394 // a symbol in 3395 // the symbol 3396 // table 3397 case eSectionTypeData4: 3398 case eSectionTypeData8: 3399 case eSectionTypeData16: 3400 type = eSymbolTypeData; 3401 break; 3402 default: 3403 break; 3404 } 3405 break; 3406 } 3407 3408 if (type == eSymbolTypeInvalid) { 3409 const char *symbol_sect_name = 3410 symbol_section->GetName().AsCString(); 3411 if (symbol_section->IsDescendant( 3412 text_section_sp.get())) { 3413 if (symbol_section->IsClear( 3414 S_ATTR_PURE_INSTRUCTIONS | 3415 S_ATTR_SELF_MODIFYING_CODE | 3416 S_ATTR_SOME_INSTRUCTIONS)) 3417 type = eSymbolTypeData; 3418 else 3419 type = eSymbolTypeCode; 3420 } else if (symbol_section->IsDescendant( 3421 data_section_sp.get()) || 3422 symbol_section->IsDescendant( 3423 data_dirty_section_sp.get()) || 3424 symbol_section->IsDescendant( 3425 data_const_section_sp.get())) { 3426 if (symbol_sect_name && 3427 ::strstr(symbol_sect_name, "__objc") == 3428 symbol_sect_name) { 3429 type = eSymbolTypeRuntime; 3430 3431 if (symbol_name) { 3432 llvm::StringRef symbol_name_ref(symbol_name); 3433 if (symbol_name_ref.startswith("_OBJC_")) { 3434 llvm::StringRef 3435 g_objc_v2_prefix_class( 3436 "_OBJC_CLASS_$_"); 3437 llvm::StringRef 3438 g_objc_v2_prefix_metaclass( 3439 "_OBJC_METACLASS_$_"); 3440 llvm::StringRef 3441 g_objc_v2_prefix_ivar("_OBJC_IVAR_$_"); 3442 if (symbol_name_ref.startswith( 3443 g_objc_v2_prefix_class)) { 3444 symbol_name_non_abi_mangled = 3445 symbol_name + 1; 3446 symbol_name = 3447 symbol_name + 3448 g_objc_v2_prefix_class.size(); 3449 type = eSymbolTypeObjCClass; 3450 demangled_is_synthesized = true; 3451 } else if ( 3452 symbol_name_ref.startswith( 3453 g_objc_v2_prefix_metaclass)) { 3454 symbol_name_non_abi_mangled = 3455 symbol_name + 1; 3456 symbol_name = 3457 symbol_name + 3458 g_objc_v2_prefix_metaclass.size(); 3459 type = eSymbolTypeObjCMetaClass; 3460 demangled_is_synthesized = true; 3461 } else if (symbol_name_ref.startswith( 3462 g_objc_v2_prefix_ivar)) { 3463 symbol_name_non_abi_mangled = 3464 symbol_name + 1; 3465 symbol_name = 3466 symbol_name + 3467 g_objc_v2_prefix_ivar.size(); 3468 type = eSymbolTypeObjCIVar; 3469 demangled_is_synthesized = true; 3470 } 3471 } 3472 } 3473 } else if (symbol_sect_name && 3474 ::strstr(symbol_sect_name, 3475 "__gcc_except_tab") == 3476 symbol_sect_name) { 3477 type = eSymbolTypeException; 3478 } else { 3479 type = eSymbolTypeData; 3480 } 3481 } else if (symbol_sect_name && 3482 ::strstr(symbol_sect_name, "__IMPORT") == 3483 symbol_sect_name) { 3484 type = eSymbolTypeTrampoline; 3485 } else if (symbol_section->IsDescendant( 3486 objc_section_sp.get())) { 3487 type = eSymbolTypeRuntime; 3488 if (symbol_name && symbol_name[0] == '.') { 3489 llvm::StringRef symbol_name_ref(symbol_name); 3490 llvm::StringRef 3491 g_objc_v1_prefix_class(".objc_class_name_"); 3492 if (symbol_name_ref.startswith( 3493 g_objc_v1_prefix_class)) { 3494 symbol_name_non_abi_mangled = symbol_name; 3495 symbol_name = symbol_name + 3496 g_objc_v1_prefix_class.size(); 3497 type = eSymbolTypeObjCClass; 3498 demangled_is_synthesized = true; 3499 } 3500 } 3501 } 3502 } 3503 } 3504 } break; 3505 } 3506 } 3507 3508 if (add_nlist) { 3509 uint64_t symbol_value = nlist.n_value; 3510 if (symbol_name_non_abi_mangled) { 3511 sym[sym_idx].GetMangled().SetMangledName( 3512 ConstString(symbol_name_non_abi_mangled)); 3513 sym[sym_idx].GetMangled().SetDemangledName( 3514 ConstString(symbol_name)); 3515 } else { 3516 bool symbol_name_is_mangled = false; 3517 3518 if (symbol_name && symbol_name[0] == '_') { 3519 symbol_name_is_mangled = symbol_name[1] == '_'; 3520 symbol_name++; // Skip the leading underscore 3521 } 3522 3523 if (symbol_name) { 3524 ConstString const_symbol_name(symbol_name); 3525 sym[sym_idx].GetMangled().SetValue( 3526 const_symbol_name, symbol_name_is_mangled); 3527 if (is_gsym && is_debug) { 3528 const char *gsym_name = 3529 sym[sym_idx] 3530 .GetMangled() 3531 .GetName(Mangled::ePreferMangled) 3532 .GetCString(); 3533 if (gsym_name) 3534 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx; 3535 } 3536 } 3537 } 3538 if (symbol_section) { 3539 const addr_t section_file_addr = 3540 symbol_section->GetFileAddress(); 3541 if (symbol_byte_size == 0 && 3542 function_starts_count > 0) { 3543 addr_t symbol_lookup_file_addr = nlist.n_value; 3544 // Do an exact address match for non-ARM addresses, 3545 // else get the closest since the symbol might be a 3546 // thumb symbol which has an address with bit zero 3547 // set 3548 FunctionStarts::Entry *func_start_entry = 3549 function_starts.FindEntry(symbol_lookup_file_addr, 3550 !is_arm); 3551 if (is_arm && func_start_entry) { 3552 // Verify that the function start address is the 3553 // symbol address (ARM) or the symbol address + 1 3554 // (thumb) 3555 if (func_start_entry->addr != 3556 symbol_lookup_file_addr && 3557 func_start_entry->addr != 3558 (symbol_lookup_file_addr + 1)) { 3559 // Not the right entry, NULL it out... 3560 func_start_entry = NULL; 3561 } 3562 } 3563 if (func_start_entry) { 3564 func_start_entry->data = true; 3565 3566 addr_t symbol_file_addr = func_start_entry->addr; 3567 uint32_t symbol_flags = 0; 3568 if (is_arm) { 3569 if (symbol_file_addr & 1) 3570 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB; 3571 symbol_file_addr &= THUMB_ADDRESS_BIT_MASK; 3572 } 3573 3574 const FunctionStarts::Entry *next_func_start_entry = 3575 function_starts.FindNextEntry(func_start_entry); 3576 const addr_t section_end_file_addr = 3577 section_file_addr + 3578 symbol_section->GetByteSize(); 3579 if (next_func_start_entry) { 3580 addr_t next_symbol_file_addr = 3581 next_func_start_entry->addr; 3582 // Be sure the clear the Thumb address bit when 3583 // we calculate the size from the current and 3584 // next address 3585 if (is_arm) 3586 next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK; 3587 symbol_byte_size = std::min<lldb::addr_t>( 3588 next_symbol_file_addr - symbol_file_addr, 3589 section_end_file_addr - symbol_file_addr); 3590 } else { 3591 symbol_byte_size = 3592 section_end_file_addr - symbol_file_addr; 3593 } 3594 } 3595 } 3596 symbol_value -= section_file_addr; 3597 } 3598 3599 if (is_debug == false) { 3600 if (type == eSymbolTypeCode) { 3601 // See if we can find a N_FUN entry for any code 3602 // symbols. If we do find a match, and the name 3603 // matches, then we can merge the two into just the 3604 // function symbol to avoid duplicate entries in 3605 // the symbol table 3606 auto range = 3607 N_FUN_addr_to_sym_idx.equal_range(nlist.n_value); 3608 if (range.first != range.second) { 3609 bool found_it = false; 3610 for (auto pos = range.first; pos != range.second; 3611 ++pos) { 3612 if (sym[sym_idx].GetMangled().GetName( 3613 Mangled::ePreferMangled) == 3614 sym[pos->second].GetMangled().GetName( 3615 Mangled::ePreferMangled)) { 3616 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; 3617 // We just need the flags from the linker 3618 // symbol, so put these flags 3619 // into the N_FUN flags to avoid duplicate 3620 // symbols in the symbol table 3621 sym[pos->second].SetExternal( 3622 sym[sym_idx].IsExternal()); 3623 sym[pos->second].SetFlags(nlist.n_type << 16 | 3624 nlist.n_desc); 3625 if (resolver_addresses.find(nlist.n_value) != 3626 resolver_addresses.end()) 3627 sym[pos->second].SetType(eSymbolTypeResolver); 3628 sym[sym_idx].Clear(); 3629 found_it = true; 3630 break; 3631 } 3632 } 3633 if (found_it) 3634 continue; 3635 } else { 3636 if (resolver_addresses.find(nlist.n_value) != 3637 resolver_addresses.end()) 3638 type = eSymbolTypeResolver; 3639 } 3640 } else if (type == eSymbolTypeData || 3641 type == eSymbolTypeObjCClass || 3642 type == eSymbolTypeObjCMetaClass || 3643 type == eSymbolTypeObjCIVar) { 3644 // See if we can find a N_STSYM entry for any data 3645 // symbols. If we do find a match, and the name 3646 // matches, then we can merge the two into just the 3647 // Static symbol to avoid duplicate entries in the 3648 // symbol table 3649 auto range = N_STSYM_addr_to_sym_idx.equal_range( 3650 nlist.n_value); 3651 if (range.first != range.second) { 3652 bool found_it = false; 3653 for (auto pos = range.first; pos != range.second; 3654 ++pos) { 3655 if (sym[sym_idx].GetMangled().GetName( 3656 Mangled::ePreferMangled) == 3657 sym[pos->second].GetMangled().GetName( 3658 Mangled::ePreferMangled)) { 3659 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; 3660 // We just need the flags from the linker 3661 // symbol, so put these flags 3662 // into the N_STSYM flags to avoid duplicate 3663 // symbols in the symbol table 3664 sym[pos->second].SetExternal( 3665 sym[sym_idx].IsExternal()); 3666 sym[pos->second].SetFlags(nlist.n_type << 16 | 3667 nlist.n_desc); 3668 sym[sym_idx].Clear(); 3669 found_it = true; 3670 break; 3671 } 3672 } 3673 if (found_it) 3674 continue; 3675 } else { 3676 const char *gsym_name = 3677 sym[sym_idx] 3678 .GetMangled() 3679 .GetName(Mangled::ePreferMangled) 3680 .GetCString(); 3681 if (gsym_name) { 3682 // Combine N_GSYM stab entries with the non 3683 // stab symbol 3684 ConstNameToSymbolIndexMap::const_iterator pos = 3685 N_GSYM_name_to_sym_idx.find(gsym_name); 3686 if (pos != N_GSYM_name_to_sym_idx.end()) { 3687 const uint32_t GSYM_sym_idx = pos->second; 3688 m_nlist_idx_to_sym_idx[nlist_idx] = 3689 GSYM_sym_idx; 3690 // Copy the address, because often the N_GSYM 3691 // address has an invalid address of zero 3692 // when the global is a common symbol 3693 sym[GSYM_sym_idx].GetAddressRef().SetSection( 3694 symbol_section); 3695 sym[GSYM_sym_idx].GetAddressRef().SetOffset( 3696 symbol_value); 3697 add_symbol_addr(sym[GSYM_sym_idx] 3698 .GetAddress() 3699 .GetFileAddress()); 3700 // We just need the flags from the linker 3701 // symbol, so put these flags 3702 // into the N_GSYM flags to avoid duplicate 3703 // symbols in the symbol table 3704 sym[GSYM_sym_idx].SetFlags(nlist.n_type << 16 | 3705 nlist.n_desc); 3706 sym[sym_idx].Clear(); 3707 continue; 3708 } 3709 } 3710 } 3711 } 3712 } 3713 3714 sym[sym_idx].SetID(nlist_idx); 3715 sym[sym_idx].SetType(type); 3716 if (set_value) { 3717 sym[sym_idx].GetAddressRef().SetSection(symbol_section); 3718 sym[sym_idx].GetAddressRef().SetOffset(symbol_value); 3719 add_symbol_addr( 3720 sym[sym_idx].GetAddress().GetFileAddress()); 3721 } 3722 sym[sym_idx].SetFlags(nlist.n_type << 16 | nlist.n_desc); 3723 3724 if (symbol_byte_size > 0) 3725 sym[sym_idx].SetByteSize(symbol_byte_size); 3726 3727 if (demangled_is_synthesized) 3728 sym[sym_idx].SetDemangledNameIsSynthesized(true); 3729 ++sym_idx; 3730 } else { 3731 sym[sym_idx].Clear(); 3732 } 3733 } 3734 ///////////////////////////// 3735 } 3736 break; // No more entries to consider 3737 } 3738 } 3739 3740 for (const auto &pos : reexport_shlib_needs_fixup) { 3741 const auto undef_pos = undefined_name_to_desc.find(pos.second); 3742 if (undef_pos != undefined_name_to_desc.end()) { 3743 const uint8_t dylib_ordinal = 3744 llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second); 3745 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize()) 3746 sym[pos.first].SetReExportedSymbolSharedLibrary( 3747 dylib_files.GetFileSpecAtIndex(dylib_ordinal - 1)); 3748 } 3749 } 3750 } 3751 } 3752 } 3753 } 3754 } 3755 3756 // Must reset this in case it was mutated above! 3757 nlist_data_offset = 0; 3758 #endif 3759 3760 if (nlist_data.GetByteSize() > 0) { 3761 3762 // If the sym array was not created while parsing the DSC unmapped 3763 // symbols, create it now. 3764 if (sym == nullptr) { 3765 sym = 3766 symtab->Resize(symtab_load_command.nsyms + m_dysymtab.nindirectsyms); 3767 num_syms = symtab->GetNumSymbols(); 3768 } 3769 3770 if (unmapped_local_symbols_found) { 3771 assert(m_dysymtab.ilocalsym == 0); 3772 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size); 3773 nlist_idx = m_dysymtab.nlocalsym; 3774 } else { 3775 nlist_idx = 0; 3776 } 3777 3778 typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap; 3779 typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName; 3780 UndefinedNameToDescMap undefined_name_to_desc; 3781 SymbolIndexToName reexport_shlib_needs_fixup; 3782 3783 // Symtab parsing is a huge mess. Everything is entangled and the code 3784 // requires access to a ridiculous amount of variables. LLDB depends 3785 // heavily on the proper merging of symbols and to get that right we need 3786 // to make sure we have parsed all the debug symbols first. Therefore we 3787 // invoke the lambda twice, once to parse only the debug symbols and then 3788 // once more to parse the remaining symbols. 3789 auto ParseSymbolLambda = [&](struct nlist_64 &nlist, uint32_t nlist_idx, 3790 bool debug_only) { 3791 const bool is_debug = ((nlist.n_type & N_STAB) != 0); 3792 if (is_debug != debug_only) 3793 return true; 3794 3795 const char *symbol_name_non_abi_mangled = nullptr; 3796 const char *symbol_name = nullptr; 3797 3798 if (have_strtab_data) { 3799 symbol_name = strtab_data.PeekCStr(nlist.n_strx); 3800 3801 if (symbol_name == nullptr) { 3802 // No symbol should be NULL, even the symbols with no string values 3803 // should have an offset zero which points to an empty C-string 3804 Host::SystemLog(Host::eSystemLogError, 3805 "error: symbol[%u] has invalid string table offset " 3806 "0x%x in %s, ignoring symbol\n", 3807 nlist_idx, nlist.n_strx, 3808 module_sp->GetFileSpec().GetPath().c_str()); 3809 return true; 3810 } 3811 if (symbol_name[0] == '\0') 3812 symbol_name = nullptr; 3813 } else { 3814 const addr_t str_addr = strtab_addr + nlist.n_strx; 3815 Status str_error; 3816 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, 3817 str_error)) 3818 symbol_name = memory_symbol_name.c_str(); 3819 } 3820 3821 SymbolType type = eSymbolTypeInvalid; 3822 SectionSP symbol_section; 3823 lldb::addr_t symbol_byte_size = 0; 3824 bool add_nlist = true; 3825 bool is_gsym = false; 3826 bool demangled_is_synthesized = false; 3827 bool set_value = true; 3828 3829 assert(sym_idx < num_syms); 3830 sym[sym_idx].SetDebug(is_debug); 3831 3832 if (is_debug) { 3833 switch (nlist.n_type) { 3834 case N_GSYM: 3835 // global symbol: name,,NO_SECT,type,0 3836 // Sometimes the N_GSYM value contains the address. 3837 3838 // FIXME: In the .o files, we have a GSYM and a debug symbol for all 3839 // the ObjC data. They 3840 // have the same address, but we want to ensure that we always find 3841 // only the real symbol, 'cause we don't currently correctly 3842 // attribute the GSYM one to the ObjCClass/Ivar/MetaClass symbol 3843 // type. This is a temporary hack to make sure the ObjectiveC 3844 // symbols get treated correctly. To do this right, we should 3845 // coalesce all the GSYM & global symbols that have the same 3846 // address. 3847 is_gsym = true; 3848 sym[sym_idx].SetExternal(true); 3849 3850 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O') { 3851 llvm::StringRef symbol_name_ref(symbol_name); 3852 if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) { 3853 symbol_name_non_abi_mangled = symbol_name + 1; 3854 symbol_name = symbol_name + g_objc_v2_prefix_class.size(); 3855 type = eSymbolTypeObjCClass; 3856 demangled_is_synthesized = true; 3857 3858 } else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass)) { 3859 symbol_name_non_abi_mangled = symbol_name + 1; 3860 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size(); 3861 type = eSymbolTypeObjCMetaClass; 3862 demangled_is_synthesized = true; 3863 } else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar)) { 3864 symbol_name_non_abi_mangled = symbol_name + 1; 3865 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size(); 3866 type = eSymbolTypeObjCIVar; 3867 demangled_is_synthesized = true; 3868 } 3869 } else { 3870 if (nlist.n_value != 0) 3871 symbol_section = 3872 section_info.GetSection(nlist.n_sect, nlist.n_value); 3873 type = eSymbolTypeData; 3874 } 3875 break; 3876 3877 case N_FNAME: 3878 // procedure name (f77 kludge): name,,NO_SECT,0,0 3879 type = eSymbolTypeCompiler; 3880 break; 3881 3882 case N_FUN: 3883 // procedure: name,,n_sect,linenumber,address 3884 if (symbol_name) { 3885 type = eSymbolTypeCode; 3886 symbol_section = 3887 section_info.GetSection(nlist.n_sect, nlist.n_value); 3888 3889 N_FUN_addr_to_sym_idx.insert( 3890 std::make_pair(nlist.n_value, sym_idx)); 3891 // We use the current number of symbols in the symbol table in 3892 // lieu of using nlist_idx in case we ever start trimming entries 3893 // out 3894 N_FUN_indexes.push_back(sym_idx); 3895 } else { 3896 type = eSymbolTypeCompiler; 3897 3898 if (!N_FUN_indexes.empty()) { 3899 // Copy the size of the function into the original STAB entry 3900 // so we don't have to hunt for it later 3901 symtab->SymbolAtIndex(N_FUN_indexes.back()) 3902 ->SetByteSize(nlist.n_value); 3903 N_FUN_indexes.pop_back(); 3904 // We don't really need the end function STAB as it contains 3905 // the size which we already placed with the original symbol, 3906 // so don't add it if we want a minimal symbol table 3907 add_nlist = false; 3908 } 3909 } 3910 break; 3911 3912 case N_STSYM: 3913 // static symbol: name,,n_sect,type,address 3914 N_STSYM_addr_to_sym_idx.insert( 3915 std::make_pair(nlist.n_value, sym_idx)); 3916 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value); 3917 if (symbol_name && symbol_name[0]) { 3918 type = ObjectFile::GetSymbolTypeFromName(symbol_name + 1, 3919 eSymbolTypeData); 3920 } 3921 break; 3922 3923 case N_LCSYM: 3924 // .lcomm symbol: name,,n_sect,type,address 3925 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value); 3926 type = eSymbolTypeCommonBlock; 3927 break; 3928 3929 case N_BNSYM: 3930 // We use the current number of symbols in the symbol table in lieu 3931 // of using nlist_idx in case we ever start trimming entries out 3932 // Skip these if we want minimal symbol tables 3933 add_nlist = false; 3934 break; 3935 3936 case N_ENSYM: 3937 // Set the size of the N_BNSYM to the terminating index of this 3938 // N_ENSYM so that we can always skip the entire symbol if we need 3939 // to navigate more quickly at the source level when parsing STABS 3940 // Skip these if we want minimal symbol tables 3941 add_nlist = false; 3942 break; 3943 3944 case N_OPT: 3945 // emitted with gcc2_compiled and in gcc source 3946 type = eSymbolTypeCompiler; 3947 break; 3948 3949 case N_RSYM: 3950 // register sym: name,,NO_SECT,type,register 3951 type = eSymbolTypeVariable; 3952 break; 3953 3954 case N_SLINE: 3955 // src line: 0,,n_sect,linenumber,address 3956 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value); 3957 type = eSymbolTypeLineEntry; 3958 break; 3959 3960 case N_SSYM: 3961 // structure elt: name,,NO_SECT,type,struct_offset 3962 type = eSymbolTypeVariableType; 3963 break; 3964 3965 case N_SO: 3966 // source file name 3967 type = eSymbolTypeSourceFile; 3968 if (symbol_name == nullptr) { 3969 add_nlist = false; 3970 if (N_SO_index != UINT32_MAX) { 3971 // Set the size of the N_SO to the terminating index of this 3972 // N_SO so that we can always skip the entire N_SO if we need 3973 // to navigate more quickly at the source level when parsing 3974 // STABS 3975 symbol_ptr = symtab->SymbolAtIndex(N_SO_index); 3976 symbol_ptr->SetByteSize(sym_idx); 3977 symbol_ptr->SetSizeIsSibling(true); 3978 } 3979 N_NSYM_indexes.clear(); 3980 N_INCL_indexes.clear(); 3981 N_BRAC_indexes.clear(); 3982 N_COMM_indexes.clear(); 3983 N_FUN_indexes.clear(); 3984 N_SO_index = UINT32_MAX; 3985 } else { 3986 // We use the current number of symbols in the symbol table in 3987 // lieu of using nlist_idx in case we ever start trimming entries 3988 // out 3989 const bool N_SO_has_full_path = symbol_name[0] == '/'; 3990 if (N_SO_has_full_path) { 3991 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) { 3992 // We have two consecutive N_SO entries where the first 3993 // contains a directory and the second contains a full path. 3994 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), 3995 false); 3996 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; 3997 add_nlist = false; 3998 } else { 3999 // This is the first entry in a N_SO that contains a 4000 // directory or a full path to the source file 4001 N_SO_index = sym_idx; 4002 } 4003 } else if ((N_SO_index == sym_idx - 1) && 4004 ((sym_idx - 1) < num_syms)) { 4005 // This is usually the second N_SO entry that contains just the 4006 // filename, so here we combine it with the first one if we are 4007 // minimizing the symbol table 4008 const char *so_path = 4009 sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString(); 4010 if (so_path && so_path[0]) { 4011 std::string full_so_path(so_path); 4012 const size_t double_slash_pos = full_so_path.find("//"); 4013 if (double_slash_pos != std::string::npos) { 4014 // The linker has been generating bad N_SO entries with 4015 // doubled up paths in the format "%s%s" where the first 4016 // string in the DW_AT_comp_dir, and the second is the 4017 // directory for the source file so you end up with a path 4018 // that looks like "/tmp/src//tmp/src/" 4019 FileSpec so_dir(so_path); 4020 if (!FileSystem::Instance().Exists(so_dir)) { 4021 so_dir.SetFile(&full_so_path[double_slash_pos + 1], 4022 FileSpec::Style::native); 4023 if (FileSystem::Instance().Exists(so_dir)) { 4024 // Trim off the incorrect path 4025 full_so_path.erase(0, double_slash_pos + 1); 4026 } 4027 } 4028 } 4029 if (*full_so_path.rbegin() != '/') 4030 full_so_path += '/'; 4031 full_so_path += symbol_name; 4032 sym[sym_idx - 1].GetMangled().SetValue( 4033 ConstString(full_so_path.c_str()), false); 4034 add_nlist = false; 4035 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; 4036 } 4037 } else { 4038 // This could be a relative path to a N_SO 4039 N_SO_index = sym_idx; 4040 } 4041 } 4042 break; 4043 4044 case N_OSO: 4045 // object file name: name,,0,0,st_mtime 4046 type = eSymbolTypeObjectFile; 4047 break; 4048 4049 case N_LSYM: 4050 // local sym: name,,NO_SECT,type,offset 4051 type = eSymbolTypeLocal; 4052 break; 4053 4054 // INCL scopes 4055 case N_BINCL: 4056 // include file beginning: name,,NO_SECT,0,sum We use the current 4057 // number of symbols in the symbol table in lieu of using nlist_idx 4058 // in case we ever start trimming entries out 4059 N_INCL_indexes.push_back(sym_idx); 4060 type = eSymbolTypeScopeBegin; 4061 break; 4062 4063 case N_EINCL: 4064 // include file end: name,,NO_SECT,0,0 4065 // Set the size of the N_BINCL to the terminating index of this 4066 // N_EINCL so that we can always skip the entire symbol if we need 4067 // to navigate more quickly at the source level when parsing STABS 4068 if (!N_INCL_indexes.empty()) { 4069 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back()); 4070 symbol_ptr->SetByteSize(sym_idx + 1); 4071 symbol_ptr->SetSizeIsSibling(true); 4072 N_INCL_indexes.pop_back(); 4073 } 4074 type = eSymbolTypeScopeEnd; 4075 break; 4076 4077 case N_SOL: 4078 // #included file name: name,,n_sect,0,address 4079 type = eSymbolTypeHeaderFile; 4080 4081 // We currently don't use the header files on darwin 4082 add_nlist = false; 4083 break; 4084 4085 case N_PARAMS: 4086 // compiler parameters: name,,NO_SECT,0,0 4087 type = eSymbolTypeCompiler; 4088 break; 4089 4090 case N_VERSION: 4091 // compiler version: name,,NO_SECT,0,0 4092 type = eSymbolTypeCompiler; 4093 break; 4094 4095 case N_OLEVEL: 4096 // compiler -O level: name,,NO_SECT,0,0 4097 type = eSymbolTypeCompiler; 4098 break; 4099 4100 case N_PSYM: 4101 // parameter: name,,NO_SECT,type,offset 4102 type = eSymbolTypeVariable; 4103 break; 4104 4105 case N_ENTRY: 4106 // alternate entry: name,,n_sect,linenumber,address 4107 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value); 4108 type = eSymbolTypeLineEntry; 4109 break; 4110 4111 // Left and Right Braces 4112 case N_LBRAC: 4113 // left bracket: 0,,NO_SECT,nesting level,address We use the 4114 // current number of symbols in the symbol table in lieu of using 4115 // nlist_idx in case we ever start trimming entries out 4116 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value); 4117 N_BRAC_indexes.push_back(sym_idx); 4118 type = eSymbolTypeScopeBegin; 4119 break; 4120 4121 case N_RBRAC: 4122 // right bracket: 0,,NO_SECT,nesting level,address Set the size of 4123 // the N_LBRAC to the terminating index of this N_RBRAC so that we 4124 // can always skip the entire symbol if we need to navigate more 4125 // quickly at the source level when parsing STABS 4126 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value); 4127 if (!N_BRAC_indexes.empty()) { 4128 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back()); 4129 symbol_ptr->SetByteSize(sym_idx + 1); 4130 symbol_ptr->SetSizeIsSibling(true); 4131 N_BRAC_indexes.pop_back(); 4132 } 4133 type = eSymbolTypeScopeEnd; 4134 break; 4135 4136 case N_EXCL: 4137 // deleted include file: name,,NO_SECT,0,sum 4138 type = eSymbolTypeHeaderFile; 4139 break; 4140 4141 // COMM scopes 4142 case N_BCOMM: 4143 // begin common: name,,NO_SECT,0,0 4144 // We use the current number of symbols in the symbol table in lieu 4145 // of using nlist_idx in case we ever start trimming entries out 4146 type = eSymbolTypeScopeBegin; 4147 N_COMM_indexes.push_back(sym_idx); 4148 break; 4149 4150 case N_ECOML: 4151 // end common (local name): 0,,n_sect,0,address 4152 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value); 4153 LLVM_FALLTHROUGH; 4154 4155 case N_ECOMM: 4156 // end common: name,,n_sect,0,0 4157 // Set the size of the N_BCOMM to the terminating index of this 4158 // N_ECOMM/N_ECOML so that we can always skip the entire symbol if 4159 // we need to navigate more quickly at the source level when 4160 // parsing STABS 4161 if (!N_COMM_indexes.empty()) { 4162 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back()); 4163 symbol_ptr->SetByteSize(sym_idx + 1); 4164 symbol_ptr->SetSizeIsSibling(true); 4165 N_COMM_indexes.pop_back(); 4166 } 4167 type = eSymbolTypeScopeEnd; 4168 break; 4169 4170 case N_LENG: 4171 // second stab entry with length information 4172 type = eSymbolTypeAdditional; 4173 break; 4174 4175 default: 4176 break; 4177 } 4178 } else { 4179 uint8_t n_type = N_TYPE & nlist.n_type; 4180 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0); 4181 4182 switch (n_type) { 4183 case N_INDR: { 4184 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value); 4185 if (reexport_name_cstr && reexport_name_cstr[0]) { 4186 type = eSymbolTypeReExported; 4187 ConstString reexport_name(reexport_name_cstr + 4188 ((reexport_name_cstr[0] == '_') ? 1 : 0)); 4189 sym[sym_idx].SetReExportedSymbolName(reexport_name); 4190 set_value = false; 4191 reexport_shlib_needs_fixup[sym_idx] = reexport_name; 4192 indirect_symbol_names.insert( 4193 ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0))); 4194 } else 4195 type = eSymbolTypeUndefined; 4196 } break; 4197 4198 case N_UNDF: 4199 if (symbol_name && symbol_name[0]) { 4200 ConstString undefined_name(symbol_name + 4201 ((symbol_name[0] == '_') ? 1 : 0)); 4202 undefined_name_to_desc[undefined_name] = nlist.n_desc; 4203 } 4204 LLVM_FALLTHROUGH; 4205 4206 case N_PBUD: 4207 type = eSymbolTypeUndefined; 4208 break; 4209 4210 case N_ABS: 4211 type = eSymbolTypeAbsolute; 4212 break; 4213 4214 case N_SECT: { 4215 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value); 4216 4217 if (!symbol_section) { 4218 // TODO: warn about this? 4219 add_nlist = false; 4220 break; 4221 } 4222 4223 if (TEXT_eh_frame_sectID == nlist.n_sect) { 4224 type = eSymbolTypeException; 4225 } else { 4226 uint32_t section_type = symbol_section->Get() & SECTION_TYPE; 4227 4228 switch (section_type) { 4229 case S_CSTRING_LITERALS: 4230 type = eSymbolTypeData; 4231 break; // section with only literal C strings 4232 case S_4BYTE_LITERALS: 4233 type = eSymbolTypeData; 4234 break; // section with only 4 byte literals 4235 case S_8BYTE_LITERALS: 4236 type = eSymbolTypeData; 4237 break; // section with only 8 byte literals 4238 case S_LITERAL_POINTERS: 4239 type = eSymbolTypeTrampoline; 4240 break; // section with only pointers to literals 4241 case S_NON_LAZY_SYMBOL_POINTERS: 4242 type = eSymbolTypeTrampoline; 4243 break; // section with only non-lazy symbol pointers 4244 case S_LAZY_SYMBOL_POINTERS: 4245 type = eSymbolTypeTrampoline; 4246 break; // section with only lazy symbol pointers 4247 case S_SYMBOL_STUBS: 4248 type = eSymbolTypeTrampoline; 4249 break; // section with only symbol stubs, byte size of stub in 4250 // the reserved2 field 4251 case S_MOD_INIT_FUNC_POINTERS: 4252 type = eSymbolTypeCode; 4253 break; // section with only function pointers for initialization 4254 case S_MOD_TERM_FUNC_POINTERS: 4255 type = eSymbolTypeCode; 4256 break; // section with only function pointers for termination 4257 case S_INTERPOSING: 4258 type = eSymbolTypeTrampoline; 4259 break; // section with only pairs of function pointers for 4260 // interposing 4261 case S_16BYTE_LITERALS: 4262 type = eSymbolTypeData; 4263 break; // section with only 16 byte literals 4264 case S_DTRACE_DOF: 4265 type = eSymbolTypeInstrumentation; 4266 break; 4267 case S_LAZY_DYLIB_SYMBOL_POINTERS: 4268 type = eSymbolTypeTrampoline; 4269 break; 4270 default: 4271 switch (symbol_section->GetType()) { 4272 case lldb::eSectionTypeCode: 4273 type = eSymbolTypeCode; 4274 break; 4275 case eSectionTypeData: 4276 case eSectionTypeDataCString: // Inlined C string data 4277 case eSectionTypeDataCStringPointers: // Pointers to C string 4278 // data 4279 case eSectionTypeDataSymbolAddress: // Address of a symbol in 4280 // the symbol table 4281 case eSectionTypeData4: 4282 case eSectionTypeData8: 4283 case eSectionTypeData16: 4284 type = eSymbolTypeData; 4285 break; 4286 default: 4287 break; 4288 } 4289 break; 4290 } 4291 4292 if (type == eSymbolTypeInvalid) { 4293 const char *symbol_sect_name = 4294 symbol_section->GetName().AsCString(); 4295 if (symbol_section->IsDescendant(text_section_sp.get())) { 4296 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS | 4297 S_ATTR_SELF_MODIFYING_CODE | 4298 S_ATTR_SOME_INSTRUCTIONS)) 4299 type = eSymbolTypeData; 4300 else 4301 type = eSymbolTypeCode; 4302 } else if (symbol_section->IsDescendant(data_section_sp.get()) || 4303 symbol_section->IsDescendant( 4304 data_dirty_section_sp.get()) || 4305 symbol_section->IsDescendant( 4306 data_const_section_sp.get())) { 4307 if (symbol_sect_name && 4308 ::strstr(symbol_sect_name, "__objc") == symbol_sect_name) { 4309 type = eSymbolTypeRuntime; 4310 4311 if (symbol_name) { 4312 llvm::StringRef symbol_name_ref(symbol_name); 4313 if (symbol_name_ref.startswith("_OBJC_")) { 4314 llvm::StringRef g_objc_v2_prefix_class( 4315 "_OBJC_CLASS_$_"); 4316 llvm::StringRef g_objc_v2_prefix_metaclass( 4317 "_OBJC_METACLASS_$_"); 4318 llvm::StringRef g_objc_v2_prefix_ivar( 4319 "_OBJC_IVAR_$_"); 4320 if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) { 4321 symbol_name_non_abi_mangled = symbol_name + 1; 4322 symbol_name = 4323 symbol_name + g_objc_v2_prefix_class.size(); 4324 type = eSymbolTypeObjCClass; 4325 demangled_is_synthesized = true; 4326 } else if (symbol_name_ref.startswith( 4327 g_objc_v2_prefix_metaclass)) { 4328 symbol_name_non_abi_mangled = symbol_name + 1; 4329 symbol_name = 4330 symbol_name + g_objc_v2_prefix_metaclass.size(); 4331 type = eSymbolTypeObjCMetaClass; 4332 demangled_is_synthesized = true; 4333 } else if (symbol_name_ref.startswith( 4334 g_objc_v2_prefix_ivar)) { 4335 symbol_name_non_abi_mangled = symbol_name + 1; 4336 symbol_name = 4337 symbol_name + g_objc_v2_prefix_ivar.size(); 4338 type = eSymbolTypeObjCIVar; 4339 demangled_is_synthesized = true; 4340 } 4341 } 4342 } 4343 } else if (symbol_sect_name && 4344 ::strstr(symbol_sect_name, "__gcc_except_tab") == 4345 symbol_sect_name) { 4346 type = eSymbolTypeException; 4347 } else { 4348 type = eSymbolTypeData; 4349 } 4350 } else if (symbol_sect_name && 4351 ::strstr(symbol_sect_name, "__IMPORT") == 4352 symbol_sect_name) { 4353 type = eSymbolTypeTrampoline; 4354 } else if (symbol_section->IsDescendant(objc_section_sp.get())) { 4355 type = eSymbolTypeRuntime; 4356 if (symbol_name && symbol_name[0] == '.') { 4357 llvm::StringRef symbol_name_ref(symbol_name); 4358 llvm::StringRef g_objc_v1_prefix_class( 4359 ".objc_class_name_"); 4360 if (symbol_name_ref.startswith(g_objc_v1_prefix_class)) { 4361 symbol_name_non_abi_mangled = symbol_name; 4362 symbol_name = symbol_name + g_objc_v1_prefix_class.size(); 4363 type = eSymbolTypeObjCClass; 4364 demangled_is_synthesized = true; 4365 } 4366 } 4367 } 4368 } 4369 } 4370 } break; 4371 } 4372 } 4373 4374 if (!add_nlist) { 4375 sym[sym_idx].Clear(); 4376 return true; 4377 } 4378 4379 uint64_t symbol_value = nlist.n_value; 4380 4381 if (symbol_name_non_abi_mangled) { 4382 sym[sym_idx].GetMangled().SetMangledName( 4383 ConstString(symbol_name_non_abi_mangled)); 4384 sym[sym_idx].GetMangled().SetDemangledName(ConstString(symbol_name)); 4385 } else { 4386 bool symbol_name_is_mangled = false; 4387 4388 if (symbol_name && symbol_name[0] == '_') { 4389 symbol_name_is_mangled = symbol_name[1] == '_'; 4390 symbol_name++; // Skip the leading underscore 4391 } 4392 4393 if (symbol_name) { 4394 ConstString const_symbol_name(symbol_name); 4395 sym[sym_idx].GetMangled().SetValue(const_symbol_name, 4396 symbol_name_is_mangled); 4397 } 4398 } 4399 4400 if (is_gsym) { 4401 const char *gsym_name = sym[sym_idx] 4402 .GetMangled() 4403 .GetName(Mangled::ePreferMangled) 4404 .GetCString(); 4405 if (gsym_name) 4406 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx; 4407 } 4408 4409 if (symbol_section) { 4410 const addr_t section_file_addr = symbol_section->GetFileAddress(); 4411 if (symbol_byte_size == 0 && function_starts_count > 0) { 4412 addr_t symbol_lookup_file_addr = nlist.n_value; 4413 // Do an exact address match for non-ARM addresses, else get the 4414 // closest since the symbol might be a thumb symbol which has an 4415 // address with bit zero set. 4416 FunctionStarts::Entry *func_start_entry = 4417 function_starts.FindEntry(symbol_lookup_file_addr, !is_arm); 4418 if (is_arm && func_start_entry) { 4419 // Verify that the function start address is the symbol address 4420 // (ARM) or the symbol address + 1 (thumb). 4421 if (func_start_entry->addr != symbol_lookup_file_addr && 4422 func_start_entry->addr != (symbol_lookup_file_addr + 1)) { 4423 // Not the right entry, NULL it out... 4424 func_start_entry = nullptr; 4425 } 4426 } 4427 if (func_start_entry) { 4428 func_start_entry->data = true; 4429 4430 addr_t symbol_file_addr = func_start_entry->addr; 4431 if (is_arm) 4432 symbol_file_addr &= THUMB_ADDRESS_BIT_MASK; 4433 4434 const FunctionStarts::Entry *next_func_start_entry = 4435 function_starts.FindNextEntry(func_start_entry); 4436 const addr_t section_end_file_addr = 4437 section_file_addr + symbol_section->GetByteSize(); 4438 if (next_func_start_entry) { 4439 addr_t next_symbol_file_addr = next_func_start_entry->addr; 4440 // Be sure the clear the Thumb address bit when we calculate the 4441 // size from the current and next address 4442 if (is_arm) 4443 next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK; 4444 symbol_byte_size = std::min<lldb::addr_t>( 4445 next_symbol_file_addr - symbol_file_addr, 4446 section_end_file_addr - symbol_file_addr); 4447 } else { 4448 symbol_byte_size = section_end_file_addr - symbol_file_addr; 4449 } 4450 } 4451 } 4452 symbol_value -= section_file_addr; 4453 } 4454 4455 if (!is_debug) { 4456 if (type == eSymbolTypeCode) { 4457 // See if we can find a N_FUN entry for any code symbols. If we do 4458 // find a match, and the name matches, then we can merge the two into 4459 // just the function symbol to avoid duplicate entries in the symbol 4460 // table. 4461 std::pair<ValueToSymbolIndexMap::const_iterator, 4462 ValueToSymbolIndexMap::const_iterator> 4463 range; 4464 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value); 4465 if (range.first != range.second) { 4466 for (ValueToSymbolIndexMap::const_iterator pos = range.first; 4467 pos != range.second; ++pos) { 4468 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == 4469 sym[pos->second].GetMangled().GetName( 4470 Mangled::ePreferMangled)) { 4471 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; 4472 // We just need the flags from the linker symbol, so put these 4473 // flags into the N_FUN flags to avoid duplicate symbols in the 4474 // symbol table. 4475 sym[pos->second].SetExternal(sym[sym_idx].IsExternal()); 4476 sym[pos->second].SetFlags(nlist.n_type << 16 | nlist.n_desc); 4477 if (resolver_addresses.find(nlist.n_value) != 4478 resolver_addresses.end()) 4479 sym[pos->second].SetType(eSymbolTypeResolver); 4480 sym[sym_idx].Clear(); 4481 return true; 4482 } 4483 } 4484 } else { 4485 if (resolver_addresses.find(nlist.n_value) != 4486 resolver_addresses.end()) 4487 type = eSymbolTypeResolver; 4488 } 4489 } else if (type == eSymbolTypeData || type == eSymbolTypeObjCClass || 4490 type == eSymbolTypeObjCMetaClass || 4491 type == eSymbolTypeObjCIVar) { 4492 // See if we can find a N_STSYM entry for any data symbols. If we do 4493 // find a match, and the name matches, then we can merge the two into 4494 // just the Static symbol to avoid duplicate entries in the symbol 4495 // table. 4496 std::pair<ValueToSymbolIndexMap::const_iterator, 4497 ValueToSymbolIndexMap::const_iterator> 4498 range; 4499 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value); 4500 if (range.first != range.second) { 4501 for (ValueToSymbolIndexMap::const_iterator pos = range.first; 4502 pos != range.second; ++pos) { 4503 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == 4504 sym[pos->second].GetMangled().GetName( 4505 Mangled::ePreferMangled)) { 4506 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; 4507 // We just need the flags from the linker symbol, so put these 4508 // flags into the N_STSYM flags to avoid duplicate symbols in 4509 // the symbol table. 4510 sym[pos->second].SetExternal(sym[sym_idx].IsExternal()); 4511 sym[pos->second].SetFlags(nlist.n_type << 16 | nlist.n_desc); 4512 sym[sym_idx].Clear(); 4513 return true; 4514 } 4515 } 4516 } else { 4517 // Combine N_GSYM stab entries with the non stab symbol. 4518 const char *gsym_name = sym[sym_idx] 4519 .GetMangled() 4520 .GetName(Mangled::ePreferMangled) 4521 .GetCString(); 4522 if (gsym_name) { 4523 ConstNameToSymbolIndexMap::const_iterator pos = 4524 N_GSYM_name_to_sym_idx.find(gsym_name); 4525 if (pos != N_GSYM_name_to_sym_idx.end()) { 4526 const uint32_t GSYM_sym_idx = pos->second; 4527 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx; 4528 // Copy the address, because often the N_GSYM address has an 4529 // invalid address of zero when the global is a common symbol. 4530 sym[GSYM_sym_idx].GetAddressRef().SetSection(symbol_section); 4531 sym[GSYM_sym_idx].GetAddressRef().SetOffset(symbol_value); 4532 add_symbol_addr( 4533 sym[GSYM_sym_idx].GetAddress().GetFileAddress()); 4534 // We just need the flags from the linker symbol, so put these 4535 // flags into the N_GSYM flags to avoid duplicate symbols in 4536 // the symbol table. 4537 sym[GSYM_sym_idx].SetFlags(nlist.n_type << 16 | nlist.n_desc); 4538 sym[sym_idx].Clear(); 4539 return true; 4540 } 4541 } 4542 } 4543 } 4544 } 4545 4546 sym[sym_idx].SetID(nlist_idx); 4547 sym[sym_idx].SetType(type); 4548 if (set_value) { 4549 sym[sym_idx].GetAddressRef().SetSection(symbol_section); 4550 sym[sym_idx].GetAddressRef().SetOffset(symbol_value); 4551 if (symbol_section) 4552 add_symbol_addr(sym[sym_idx].GetAddress().GetFileAddress()); 4553 } 4554 sym[sym_idx].SetFlags(nlist.n_type << 16 | nlist.n_desc); 4555 if (nlist.n_desc & N_WEAK_REF) 4556 sym[sym_idx].SetIsWeak(true); 4557 4558 if (symbol_byte_size > 0) 4559 sym[sym_idx].SetByteSize(symbol_byte_size); 4560 4561 if (demangled_is_synthesized) 4562 sym[sym_idx].SetDemangledNameIsSynthesized(true); 4563 4564 ++sym_idx; 4565 return true; 4566 }; 4567 4568 // First parse all the nlists but don't process them yet. See the next 4569 // comment for an explanation why. 4570 std::vector<struct nlist_64> nlists; 4571 nlists.reserve(symtab_load_command.nsyms); 4572 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx) { 4573 if (auto nlist = 4574 ParseNList(nlist_data, nlist_data_offset, nlist_byte_size)) 4575 nlists.push_back(*nlist); 4576 else 4577 break; 4578 } 4579 4580 // Now parse all the debug symbols. This is needed to merge non-debug 4581 // symbols in the next step. Non-debug symbols are always coalesced into 4582 // the debug symbol. Doing this in one step would mean that some symbols 4583 // won't be merged. 4584 nlist_idx = 0; 4585 for (auto &nlist : nlists) { 4586 if (!ParseSymbolLambda(nlist, nlist_idx++, DebugSymbols)) 4587 break; 4588 } 4589 4590 // Finally parse all the non debug symbols. 4591 nlist_idx = 0; 4592 for (auto &nlist : nlists) { 4593 if (!ParseSymbolLambda(nlist, nlist_idx++, NonDebugSymbols)) 4594 break; 4595 } 4596 4597 for (const auto &pos : reexport_shlib_needs_fixup) { 4598 const auto undef_pos = undefined_name_to_desc.find(pos.second); 4599 if (undef_pos != undefined_name_to_desc.end()) { 4600 const uint8_t dylib_ordinal = 4601 llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second); 4602 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize()) 4603 sym[pos.first].SetReExportedSymbolSharedLibrary( 4604 dylib_files.GetFileSpecAtIndex(dylib_ordinal - 1)); 4605 } 4606 } 4607 } 4608 4609 // Count how many trie symbols we'll add to the symbol table 4610 int trie_symbol_table_augment_count = 0; 4611 for (auto &e : external_sym_trie_entries) { 4612 if (symbols_added.find(e.entry.address) == symbols_added.end()) 4613 trie_symbol_table_augment_count++; 4614 } 4615 4616 if (num_syms < sym_idx + trie_symbol_table_augment_count) { 4617 num_syms = sym_idx + trie_symbol_table_augment_count; 4618 sym = symtab->Resize(num_syms); 4619 } 4620 uint32_t synthetic_sym_id = symtab_load_command.nsyms; 4621 4622 // Add symbols from the trie to the symbol table. 4623 for (auto &e : external_sym_trie_entries) { 4624 if (symbols_added.find(e.entry.address) != symbols_added.end()) 4625 continue; 4626 4627 // Find the section that this trie address is in, use that to annotate 4628 // symbol type as we add the trie address and name to the symbol table. 4629 Address symbol_addr; 4630 if (module_sp->ResolveFileAddress(e.entry.address, symbol_addr)) { 4631 SectionSP symbol_section(symbol_addr.GetSection()); 4632 const char *symbol_name = e.entry.name.GetCString(); 4633 bool demangled_is_synthesized = false; 4634 SymbolType type = 4635 GetSymbolType(symbol_name, demangled_is_synthesized, text_section_sp, 4636 data_section_sp, data_dirty_section_sp, 4637 data_const_section_sp, symbol_section); 4638 4639 sym[sym_idx].SetType(type); 4640 if (symbol_section) { 4641 sym[sym_idx].SetID(synthetic_sym_id++); 4642 sym[sym_idx].GetMangled().SetMangledName(ConstString(symbol_name)); 4643 if (demangled_is_synthesized) 4644 sym[sym_idx].SetDemangledNameIsSynthesized(true); 4645 sym[sym_idx].SetIsSynthetic(true); 4646 sym[sym_idx].SetExternal(true); 4647 sym[sym_idx].GetAddressRef() = symbol_addr; 4648 add_symbol_addr(symbol_addr.GetFileAddress()); 4649 if (e.entry.flags & TRIE_SYMBOL_IS_THUMB) 4650 sym[sym_idx].SetFlags(MACHO_NLIST_ARM_SYMBOL_IS_THUMB); 4651 ++sym_idx; 4652 } 4653 } 4654 } 4655 4656 if (function_starts_count > 0) { 4657 uint32_t num_synthetic_function_symbols = 0; 4658 for (i = 0; i < function_starts_count; ++i) { 4659 if (symbols_added.find(function_starts.GetEntryRef(i).addr) == 4660 symbols_added.end()) 4661 ++num_synthetic_function_symbols; 4662 } 4663 4664 if (num_synthetic_function_symbols > 0) { 4665 if (num_syms < sym_idx + num_synthetic_function_symbols) { 4666 num_syms = sym_idx + num_synthetic_function_symbols; 4667 sym = symtab->Resize(num_syms); 4668 } 4669 for (i = 0; i < function_starts_count; ++i) { 4670 const FunctionStarts::Entry *func_start_entry = 4671 function_starts.GetEntryAtIndex(i); 4672 if (symbols_added.find(func_start_entry->addr) == symbols_added.end()) { 4673 addr_t symbol_file_addr = func_start_entry->addr; 4674 uint32_t symbol_flags = 0; 4675 if (func_start_entry->data) 4676 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB; 4677 Address symbol_addr; 4678 if (module_sp->ResolveFileAddress(symbol_file_addr, symbol_addr)) { 4679 SectionSP symbol_section(symbol_addr.GetSection()); 4680 uint32_t symbol_byte_size = 0; 4681 if (symbol_section) { 4682 const addr_t section_file_addr = symbol_section->GetFileAddress(); 4683 const FunctionStarts::Entry *next_func_start_entry = 4684 function_starts.FindNextEntry(func_start_entry); 4685 const addr_t section_end_file_addr = 4686 section_file_addr + symbol_section->GetByteSize(); 4687 if (next_func_start_entry) { 4688 addr_t next_symbol_file_addr = next_func_start_entry->addr; 4689 if (is_arm) 4690 next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK; 4691 symbol_byte_size = std::min<lldb::addr_t>( 4692 next_symbol_file_addr - symbol_file_addr, 4693 section_end_file_addr - symbol_file_addr); 4694 } else { 4695 symbol_byte_size = section_end_file_addr - symbol_file_addr; 4696 } 4697 sym[sym_idx].SetID(synthetic_sym_id++); 4698 sym[sym_idx].GetMangled().SetDemangledName( 4699 GetNextSyntheticSymbolName()); 4700 sym[sym_idx].SetType(eSymbolTypeCode); 4701 sym[sym_idx].SetIsSynthetic(true); 4702 sym[sym_idx].GetAddressRef() = symbol_addr; 4703 add_symbol_addr(symbol_addr.GetFileAddress()); 4704 if (symbol_flags) 4705 sym[sym_idx].SetFlags(symbol_flags); 4706 if (symbol_byte_size) 4707 sym[sym_idx].SetByteSize(symbol_byte_size); 4708 ++sym_idx; 4709 } 4710 } 4711 } 4712 } 4713 } 4714 } 4715 4716 // Trim our symbols down to just what we ended up with after removing any 4717 // symbols. 4718 if (sym_idx < num_syms) { 4719 num_syms = sym_idx; 4720 sym = symtab->Resize(num_syms); 4721 } 4722 4723 // Now synthesize indirect symbols 4724 if (m_dysymtab.nindirectsyms != 0) { 4725 if (indirect_symbol_index_data.GetByteSize()) { 4726 NListIndexToSymbolIndexMap::const_iterator end_index_pos = 4727 m_nlist_idx_to_sym_idx.end(); 4728 4729 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); 4730 ++sect_idx) { 4731 if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) == 4732 S_SYMBOL_STUBS) { 4733 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2; 4734 if (symbol_stub_byte_size == 0) 4735 continue; 4736 4737 const uint32_t num_symbol_stubs = 4738 m_mach_sections[sect_idx].size / symbol_stub_byte_size; 4739 4740 if (num_symbol_stubs == 0) 4741 continue; 4742 4743 const uint32_t symbol_stub_index_offset = 4744 m_mach_sections[sect_idx].reserved1; 4745 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) { 4746 const uint32_t symbol_stub_index = 4747 symbol_stub_index_offset + stub_idx; 4748 const lldb::addr_t symbol_stub_addr = 4749 m_mach_sections[sect_idx].addr + 4750 (stub_idx * symbol_stub_byte_size); 4751 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4; 4752 if (indirect_symbol_index_data.ValidOffsetForDataOfSize( 4753 symbol_stub_offset, 4)) { 4754 const uint32_t stub_sym_id = 4755 indirect_symbol_index_data.GetU32(&symbol_stub_offset); 4756 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL)) 4757 continue; 4758 4759 NListIndexToSymbolIndexMap::const_iterator index_pos = 4760 m_nlist_idx_to_sym_idx.find(stub_sym_id); 4761 Symbol *stub_symbol = nullptr; 4762 if (index_pos != end_index_pos) { 4763 // We have a remapping from the original nlist index to a 4764 // current symbol index, so just look this up by index 4765 stub_symbol = symtab->SymbolAtIndex(index_pos->second); 4766 } else { 4767 // We need to lookup a symbol using the original nlist symbol 4768 // index since this index is coming from the S_SYMBOL_STUBS 4769 stub_symbol = symtab->FindSymbolByID(stub_sym_id); 4770 } 4771 4772 if (stub_symbol) { 4773 Address so_addr(symbol_stub_addr, section_list); 4774 4775 if (stub_symbol->GetType() == eSymbolTypeUndefined) { 4776 // Change the external symbol into a trampoline that makes 4777 // sense These symbols were N_UNDF N_EXT, and are useless 4778 // to us, so we can re-use them so we don't have to make up 4779 // a synthetic symbol for no good reason. 4780 if (resolver_addresses.find(symbol_stub_addr) == 4781 resolver_addresses.end()) 4782 stub_symbol->SetType(eSymbolTypeTrampoline); 4783 else 4784 stub_symbol->SetType(eSymbolTypeResolver); 4785 stub_symbol->SetExternal(false); 4786 stub_symbol->GetAddressRef() = so_addr; 4787 stub_symbol->SetByteSize(symbol_stub_byte_size); 4788 } else { 4789 // Make a synthetic symbol to describe the trampoline stub 4790 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled()); 4791 if (sym_idx >= num_syms) { 4792 sym = symtab->Resize(++num_syms); 4793 stub_symbol = nullptr; // this pointer no longer valid 4794 } 4795 sym[sym_idx].SetID(synthetic_sym_id++); 4796 sym[sym_idx].GetMangled() = stub_symbol_mangled_name; 4797 if (resolver_addresses.find(symbol_stub_addr) == 4798 resolver_addresses.end()) 4799 sym[sym_idx].SetType(eSymbolTypeTrampoline); 4800 else 4801 sym[sym_idx].SetType(eSymbolTypeResolver); 4802 sym[sym_idx].SetIsSynthetic(true); 4803 sym[sym_idx].GetAddressRef() = so_addr; 4804 add_symbol_addr(so_addr.GetFileAddress()); 4805 sym[sym_idx].SetByteSize(symbol_stub_byte_size); 4806 ++sym_idx; 4807 } 4808 } else { 4809 if (log) 4810 log->Warning("symbol stub referencing symbol table symbol " 4811 "%u that isn't in our minimal symbol table, " 4812 "fix this!!!", 4813 stub_sym_id); 4814 } 4815 } 4816 } 4817 } 4818 } 4819 } 4820 } 4821 4822 if (!reexport_trie_entries.empty()) { 4823 for (const auto &e : reexport_trie_entries) { 4824 if (e.entry.import_name) { 4825 // Only add indirect symbols from the Trie entries if we didn't have 4826 // a N_INDR nlist entry for this already 4827 if (indirect_symbol_names.find(e.entry.name) == 4828 indirect_symbol_names.end()) { 4829 // Make a synthetic symbol to describe re-exported symbol. 4830 if (sym_idx >= num_syms) 4831 sym = symtab->Resize(++num_syms); 4832 sym[sym_idx].SetID(synthetic_sym_id++); 4833 sym[sym_idx].GetMangled() = Mangled(e.entry.name); 4834 sym[sym_idx].SetType(eSymbolTypeReExported); 4835 sym[sym_idx].SetIsSynthetic(true); 4836 sym[sym_idx].SetReExportedSymbolName(e.entry.import_name); 4837 if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize()) { 4838 sym[sym_idx].SetReExportedSymbolSharedLibrary( 4839 dylib_files.GetFileSpecAtIndex(e.entry.other - 1)); 4840 } 4841 ++sym_idx; 4842 } 4843 } 4844 } 4845 } 4846 4847 // StreamFile s(stdout, false); 4848 // s.Printf ("Symbol table before CalculateSymbolSizes():\n"); 4849 // symtab->Dump(&s, NULL, eSortOrderNone); 4850 // Set symbol byte sizes correctly since mach-o nlist entries don't have 4851 // sizes 4852 symtab->CalculateSymbolSizes(); 4853 4854 // s.Printf ("Symbol table after CalculateSymbolSizes():\n"); 4855 // symtab->Dump(&s, NULL, eSortOrderNone); 4856 4857 return symtab->GetNumSymbols(); 4858 } 4859 4860 void ObjectFileMachO::Dump(Stream *s) { 4861 ModuleSP module_sp(GetModule()); 4862 if (module_sp) { 4863 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 4864 s->Printf("%p: ", static_cast<void *>(this)); 4865 s->Indent(); 4866 if (m_header.magic == MH_MAGIC_64 || m_header.magic == MH_CIGAM_64) 4867 s->PutCString("ObjectFileMachO64"); 4868 else 4869 s->PutCString("ObjectFileMachO32"); 4870 4871 *s << ", file = '" << m_file; 4872 ModuleSpecList all_specs; 4873 ModuleSpec base_spec; 4874 GetAllArchSpecs(m_header, m_data, MachHeaderSizeFromMagic(m_header.magic), 4875 base_spec, all_specs); 4876 for (unsigned i = 0, e = all_specs.GetSize(); i != e; ++i) { 4877 *s << "', triple"; 4878 if (e) 4879 s->Printf("[%d]", i); 4880 *s << " = "; 4881 *s << all_specs.GetModuleSpecRefAtIndex(i) 4882 .GetArchitecture() 4883 .GetTriple() 4884 .getTriple(); 4885 } 4886 *s << "\n"; 4887 SectionList *sections = GetSectionList(); 4888 if (sections) 4889 sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true, 4890 UINT32_MAX); 4891 4892 if (m_symtab_up) 4893 m_symtab_up->Dump(s, nullptr, eSortOrderNone); 4894 } 4895 } 4896 4897 UUID ObjectFileMachO::GetUUID(const llvm::MachO::mach_header &header, 4898 const lldb_private::DataExtractor &data, 4899 lldb::offset_t lc_offset) { 4900 uint32_t i; 4901 llvm::MachO::uuid_command load_cmd; 4902 4903 lldb::offset_t offset = lc_offset; 4904 for (i = 0; i < header.ncmds; ++i) { 4905 const lldb::offset_t cmd_offset = offset; 4906 if (data.GetU32(&offset, &load_cmd, 2) == nullptr) 4907 break; 4908 4909 if (load_cmd.cmd == LC_UUID) { 4910 const uint8_t *uuid_bytes = data.PeekData(offset, 16); 4911 4912 if (uuid_bytes) { 4913 // OpenCL on Mac OS X uses the same UUID for each of its object files. 4914 // We pretend these object files have no UUID to prevent crashing. 4915 4916 const uint8_t opencl_uuid[] = {0x8c, 0x8e, 0xb3, 0x9b, 0x3b, 0xa8, 4917 0x4b, 0x16, 0xb6, 0xa4, 0x27, 0x63, 4918 0xbb, 0x14, 0xf0, 0x0d}; 4919 4920 if (!memcmp(uuid_bytes, opencl_uuid, 16)) 4921 return UUID(); 4922 4923 return UUID::fromOptionalData(uuid_bytes, 16); 4924 } 4925 return UUID(); 4926 } 4927 offset = cmd_offset + load_cmd.cmdsize; 4928 } 4929 return UUID(); 4930 } 4931 4932 static llvm::StringRef GetOSName(uint32_t cmd) { 4933 switch (cmd) { 4934 case llvm::MachO::LC_VERSION_MIN_IPHONEOS: 4935 return llvm::Triple::getOSTypeName(llvm::Triple::IOS); 4936 case llvm::MachO::LC_VERSION_MIN_MACOSX: 4937 return llvm::Triple::getOSTypeName(llvm::Triple::MacOSX); 4938 case llvm::MachO::LC_VERSION_MIN_TVOS: 4939 return llvm::Triple::getOSTypeName(llvm::Triple::TvOS); 4940 case llvm::MachO::LC_VERSION_MIN_WATCHOS: 4941 return llvm::Triple::getOSTypeName(llvm::Triple::WatchOS); 4942 default: 4943 llvm_unreachable("unexpected LC_VERSION load command"); 4944 } 4945 } 4946 4947 namespace { 4948 struct OSEnv { 4949 llvm::StringRef os_type; 4950 llvm::StringRef environment; 4951 OSEnv(uint32_t cmd) { 4952 switch (cmd) { 4953 case llvm::MachO::PLATFORM_MACOS: 4954 os_type = llvm::Triple::getOSTypeName(llvm::Triple::MacOSX); 4955 return; 4956 case llvm::MachO::PLATFORM_IOS: 4957 os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS); 4958 return; 4959 case llvm::MachO::PLATFORM_TVOS: 4960 os_type = llvm::Triple::getOSTypeName(llvm::Triple::TvOS); 4961 return; 4962 case llvm::MachO::PLATFORM_WATCHOS: 4963 os_type = llvm::Triple::getOSTypeName(llvm::Triple::WatchOS); 4964 return; 4965 // NEED_BRIDGEOS_TRIPLE case llvm::MachO::PLATFORM_BRIDGEOS: 4966 // NEED_BRIDGEOS_TRIPLE os_type = 4967 // llvm::Triple::getOSTypeName(llvm::Triple::BridgeOS); 4968 // NEED_BRIDGEOS_TRIPLE return; 4969 case llvm::MachO::PLATFORM_MACCATALYST: 4970 os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS); 4971 environment = llvm::Triple::getEnvironmentTypeName(llvm::Triple::MacABI); 4972 return; 4973 case llvm::MachO::PLATFORM_IOSSIMULATOR: 4974 os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS); 4975 environment = 4976 llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator); 4977 return; 4978 case llvm::MachO::PLATFORM_TVOSSIMULATOR: 4979 os_type = llvm::Triple::getOSTypeName(llvm::Triple::TvOS); 4980 environment = 4981 llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator); 4982 return; 4983 case llvm::MachO::PLATFORM_WATCHOSSIMULATOR: 4984 os_type = llvm::Triple::getOSTypeName(llvm::Triple::WatchOS); 4985 environment = 4986 llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator); 4987 return; 4988 default: { 4989 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS | 4990 LIBLLDB_LOG_PROCESS)); 4991 LLDB_LOGF(log, "unsupported platform in LC_BUILD_VERSION"); 4992 } 4993 } 4994 } 4995 }; 4996 4997 struct MinOS { 4998 uint32_t major_version, minor_version, patch_version; 4999 MinOS(uint32_t version) 5000 : major_version(version >> 16), minor_version((version >> 8) & 0xffu), 5001 patch_version(version & 0xffu) {} 5002 }; 5003 } // namespace 5004 5005 void ObjectFileMachO::GetAllArchSpecs(const llvm::MachO::mach_header &header, 5006 const lldb_private::DataExtractor &data, 5007 lldb::offset_t lc_offset, 5008 ModuleSpec &base_spec, 5009 lldb_private::ModuleSpecList &all_specs) { 5010 auto &base_arch = base_spec.GetArchitecture(); 5011 base_arch.SetArchitecture(eArchTypeMachO, header.cputype, header.cpusubtype); 5012 if (!base_arch.IsValid()) 5013 return; 5014 5015 bool found_any = false; 5016 auto add_triple = [&](const llvm::Triple &triple) { 5017 auto spec = base_spec; 5018 spec.GetArchitecture().GetTriple() = triple; 5019 if (spec.GetArchitecture().IsValid()) { 5020 spec.GetUUID() = ObjectFileMachO::GetUUID(header, data, lc_offset); 5021 all_specs.Append(spec); 5022 found_any = true; 5023 } 5024 }; 5025 5026 // Set OS to an unspecified unknown or a "*" so it can match any OS 5027 llvm::Triple base_triple = base_arch.GetTriple(); 5028 base_triple.setOS(llvm::Triple::UnknownOS); 5029 base_triple.setOSName(llvm::StringRef()); 5030 5031 if (header.filetype == MH_PRELOAD) { 5032 if (header.cputype == CPU_TYPE_ARM) { 5033 // If this is a 32-bit arm binary, and it's a standalone binary, force 5034 // the Vendor to Apple so we don't accidentally pick up the generic 5035 // armv7 ABI at runtime. Apple's armv7 ABI always uses r7 for the 5036 // frame pointer register; most other armv7 ABIs use a combination of 5037 // r7 and r11. 5038 base_triple.setVendor(llvm::Triple::Apple); 5039 } else { 5040 // Set vendor to an unspecified unknown or a "*" so it can match any 5041 // vendor This is required for correct behavior of EFI debugging on 5042 // x86_64 5043 base_triple.setVendor(llvm::Triple::UnknownVendor); 5044 base_triple.setVendorName(llvm::StringRef()); 5045 } 5046 return add_triple(base_triple); 5047 } 5048 5049 llvm::MachO::load_command load_cmd; 5050 5051 // See if there is an LC_VERSION_MIN_* load command that can give 5052 // us the OS type. 5053 lldb::offset_t offset = lc_offset; 5054 for (uint32_t i = 0; i < header.ncmds; ++i) { 5055 const lldb::offset_t cmd_offset = offset; 5056 if (data.GetU32(&offset, &load_cmd, 2) == NULL) 5057 break; 5058 5059 llvm::MachO::version_min_command version_min; 5060 switch (load_cmd.cmd) { 5061 case llvm::MachO::LC_VERSION_MIN_MACOSX: 5062 case llvm::MachO::LC_VERSION_MIN_IPHONEOS: 5063 case llvm::MachO::LC_VERSION_MIN_TVOS: 5064 case llvm::MachO::LC_VERSION_MIN_WATCHOS: { 5065 if (load_cmd.cmdsize != sizeof(version_min)) 5066 break; 5067 if (data.ExtractBytes(cmd_offset, sizeof(version_min), 5068 data.GetByteOrder(), &version_min) == 0) 5069 break; 5070 MinOS min_os(version_min.version); 5071 llvm::SmallString<32> os_name; 5072 llvm::raw_svector_ostream os(os_name); 5073 os << GetOSName(load_cmd.cmd) << min_os.major_version << '.' 5074 << min_os.minor_version << '.' << min_os.patch_version; 5075 5076 auto triple = base_triple; 5077 triple.setOSName(os.str()); 5078 5079 // Disambiguate legacy simulator platforms. 5080 if (load_cmd.cmd != llvm::MachO::LC_VERSION_MIN_MACOSX && 5081 (base_triple.getArch() == llvm::Triple::x86_64 || 5082 base_triple.getArch() == llvm::Triple::x86)) { 5083 // The combination of legacy LC_VERSION_MIN load command and 5084 // x86 architecture always indicates a simulator environment. 5085 // The combination of LC_VERSION_MIN and arm architecture only 5086 // appears for native binaries. Back-deploying simulator 5087 // binaries on Apple Silicon Macs use the modern unambigous 5088 // LC_BUILD_VERSION load commands; no special handling required. 5089 triple.setEnvironment(llvm::Triple::Simulator); 5090 } 5091 add_triple(triple); 5092 break; 5093 } 5094 default: 5095 break; 5096 } 5097 5098 offset = cmd_offset + load_cmd.cmdsize; 5099 } 5100 5101 // See if there are LC_BUILD_VERSION load commands that can give 5102 // us the OS type. 5103 offset = lc_offset; 5104 for (uint32_t i = 0; i < header.ncmds; ++i) { 5105 const lldb::offset_t cmd_offset = offset; 5106 if (data.GetU32(&offset, &load_cmd, 2) == NULL) 5107 break; 5108 5109 do { 5110 if (load_cmd.cmd == llvm::MachO::LC_BUILD_VERSION) { 5111 llvm::MachO::build_version_command build_version; 5112 if (load_cmd.cmdsize < sizeof(build_version)) { 5113 // Malformed load command. 5114 break; 5115 } 5116 if (data.ExtractBytes(cmd_offset, sizeof(build_version), 5117 data.GetByteOrder(), &build_version) == 0) 5118 break; 5119 MinOS min_os(build_version.minos); 5120 OSEnv os_env(build_version.platform); 5121 llvm::SmallString<16> os_name; 5122 llvm::raw_svector_ostream os(os_name); 5123 os << os_env.os_type << min_os.major_version << '.' 5124 << min_os.minor_version << '.' << min_os.patch_version; 5125 auto triple = base_triple; 5126 triple.setOSName(os.str()); 5127 os_name.clear(); 5128 if (!os_env.environment.empty()) 5129 triple.setEnvironmentName(os_env.environment); 5130 add_triple(triple); 5131 } 5132 } while (0); 5133 offset = cmd_offset + load_cmd.cmdsize; 5134 } 5135 5136 if (!found_any) { 5137 if (header.filetype == MH_KEXT_BUNDLE) { 5138 base_triple.setVendor(llvm::Triple::Apple); 5139 add_triple(base_triple); 5140 } else { 5141 // We didn't find a LC_VERSION_MIN load command and this isn't a KEXT 5142 // so lets not say our Vendor is Apple, leave it as an unspecified 5143 // unknown. 5144 base_triple.setVendor(llvm::Triple::UnknownVendor); 5145 base_triple.setVendorName(llvm::StringRef()); 5146 add_triple(base_triple); 5147 } 5148 } 5149 } 5150 5151 ArchSpec ObjectFileMachO::GetArchitecture( 5152 ModuleSP module_sp, const llvm::MachO::mach_header &header, 5153 const lldb_private::DataExtractor &data, lldb::offset_t lc_offset) { 5154 ModuleSpecList all_specs; 5155 ModuleSpec base_spec; 5156 GetAllArchSpecs(header, data, MachHeaderSizeFromMagic(header.magic), 5157 base_spec, all_specs); 5158 5159 // If the object file offers multiple alternative load commands, 5160 // pick the one that matches the module. 5161 if (module_sp) { 5162 const ArchSpec &module_arch = module_sp->GetArchitecture(); 5163 for (unsigned i = 0, e = all_specs.GetSize(); i != e; ++i) { 5164 ArchSpec mach_arch = 5165 all_specs.GetModuleSpecRefAtIndex(i).GetArchitecture(); 5166 if (module_arch.IsCompatibleMatch(mach_arch)) 5167 return mach_arch; 5168 } 5169 } 5170 5171 // Return the first arch we found. 5172 if (all_specs.GetSize() == 0) 5173 return {}; 5174 return all_specs.GetModuleSpecRefAtIndex(0).GetArchitecture(); 5175 } 5176 5177 UUID ObjectFileMachO::GetUUID() { 5178 ModuleSP module_sp(GetModule()); 5179 if (module_sp) { 5180 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 5181 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 5182 return GetUUID(m_header, m_data, offset); 5183 } 5184 return UUID(); 5185 } 5186 5187 uint32_t ObjectFileMachO::GetDependentModules(FileSpecList &files) { 5188 uint32_t count = 0; 5189 ModuleSP module_sp(GetModule()); 5190 if (module_sp) { 5191 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 5192 llvm::MachO::load_command load_cmd; 5193 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 5194 std::vector<std::string> rpath_paths; 5195 std::vector<std::string> rpath_relative_paths; 5196 std::vector<std::string> at_exec_relative_paths; 5197 uint32_t i; 5198 for (i = 0; i < m_header.ncmds; ++i) { 5199 const uint32_t cmd_offset = offset; 5200 if (m_data.GetU32(&offset, &load_cmd, 2) == nullptr) 5201 break; 5202 5203 switch (load_cmd.cmd) { 5204 case LC_RPATH: 5205 case LC_LOAD_DYLIB: 5206 case LC_LOAD_WEAK_DYLIB: 5207 case LC_REEXPORT_DYLIB: 5208 case LC_LOAD_DYLINKER: 5209 case LC_LOADFVMLIB: 5210 case LC_LOAD_UPWARD_DYLIB: { 5211 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset); 5212 const char *path = m_data.PeekCStr(name_offset); 5213 if (path) { 5214 if (load_cmd.cmd == LC_RPATH) 5215 rpath_paths.push_back(path); 5216 else { 5217 if (path[0] == '@') { 5218 if (strncmp(path, "@rpath", strlen("@rpath")) == 0) 5219 rpath_relative_paths.push_back(path + strlen("@rpath")); 5220 else if (strncmp(path, "@executable_path", 5221 strlen("@executable_path")) == 0) 5222 at_exec_relative_paths.push_back(path + 5223 strlen("@executable_path")); 5224 } else { 5225 FileSpec file_spec(path); 5226 if (files.AppendIfUnique(file_spec)) 5227 count++; 5228 } 5229 } 5230 } 5231 } break; 5232 5233 default: 5234 break; 5235 } 5236 offset = cmd_offset + load_cmd.cmdsize; 5237 } 5238 5239 FileSpec this_file_spec(m_file); 5240 FileSystem::Instance().Resolve(this_file_spec); 5241 5242 if (!rpath_paths.empty()) { 5243 // Fixup all LC_RPATH values to be absolute paths 5244 std::string loader_path("@loader_path"); 5245 std::string executable_path("@executable_path"); 5246 for (auto &rpath : rpath_paths) { 5247 if (llvm::StringRef(rpath).startswith(loader_path)) { 5248 rpath.erase(0, loader_path.size()); 5249 rpath.insert(0, this_file_spec.GetDirectory().GetCString()); 5250 } else if (llvm::StringRef(rpath).startswith(executable_path)) { 5251 rpath.erase(0, executable_path.size()); 5252 rpath.insert(0, this_file_spec.GetDirectory().GetCString()); 5253 } 5254 } 5255 5256 for (const auto &rpath_relative_path : rpath_relative_paths) { 5257 for (const auto &rpath : rpath_paths) { 5258 std::string path = rpath; 5259 path += rpath_relative_path; 5260 // It is OK to resolve this path because we must find a file on disk 5261 // for us to accept it anyway if it is rpath relative. 5262 FileSpec file_spec(path); 5263 FileSystem::Instance().Resolve(file_spec); 5264 if (FileSystem::Instance().Exists(file_spec) && 5265 files.AppendIfUnique(file_spec)) { 5266 count++; 5267 break; 5268 } 5269 } 5270 } 5271 } 5272 5273 // We may have @executable_paths but no RPATHS. Figure those out here. 5274 // Only do this if this object file is the executable. We have no way to 5275 // get back to the actual executable otherwise, so we won't get the right 5276 // path. 5277 if (!at_exec_relative_paths.empty() && CalculateType() == eTypeExecutable) { 5278 FileSpec exec_dir = this_file_spec.CopyByRemovingLastPathComponent(); 5279 for (const auto &at_exec_relative_path : at_exec_relative_paths) { 5280 FileSpec file_spec = 5281 exec_dir.CopyByAppendingPathComponent(at_exec_relative_path); 5282 if (FileSystem::Instance().Exists(file_spec) && 5283 files.AppendIfUnique(file_spec)) 5284 count++; 5285 } 5286 } 5287 } 5288 return count; 5289 } 5290 5291 lldb_private::Address ObjectFileMachO::GetEntryPointAddress() { 5292 // If the object file is not an executable it can't hold the entry point. 5293 // m_entry_point_address is initialized to an invalid address, so we can just 5294 // return that. If m_entry_point_address is valid it means we've found it 5295 // already, so return the cached value. 5296 5297 if ((!IsExecutable() && !IsDynamicLoader()) || 5298 m_entry_point_address.IsValid()) { 5299 return m_entry_point_address; 5300 } 5301 5302 // Otherwise, look for the UnixThread or Thread command. The data for the 5303 // Thread command is given in /usr/include/mach-o.h, but it is basically: 5304 // 5305 // uint32_t flavor - this is the flavor argument you would pass to 5306 // thread_get_state 5307 // uint32_t count - this is the count of longs in the thread state data 5308 // struct XXX_thread_state state - this is the structure from 5309 // <machine/thread_status.h> corresponding to the flavor. 5310 // <repeat this trio> 5311 // 5312 // So we just keep reading the various register flavors till we find the GPR 5313 // one, then read the PC out of there. 5314 // FIXME: We will need to have a "RegisterContext data provider" class at some 5315 // point that can get all the registers 5316 // out of data in this form & attach them to a given thread. That should 5317 // underlie the MacOS X User process plugin, and we'll also need it for the 5318 // MacOS X Core File process plugin. When we have that we can also use it 5319 // here. 5320 // 5321 // For now we hard-code the offsets and flavors we need: 5322 // 5323 // 5324 5325 ModuleSP module_sp(GetModule()); 5326 if (module_sp) { 5327 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 5328 llvm::MachO::load_command load_cmd; 5329 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 5330 uint32_t i; 5331 lldb::addr_t start_address = LLDB_INVALID_ADDRESS; 5332 bool done = false; 5333 5334 for (i = 0; i < m_header.ncmds; ++i) { 5335 const lldb::offset_t cmd_offset = offset; 5336 if (m_data.GetU32(&offset, &load_cmd, 2) == nullptr) 5337 break; 5338 5339 switch (load_cmd.cmd) { 5340 case LC_UNIXTHREAD: 5341 case LC_THREAD: { 5342 while (offset < cmd_offset + load_cmd.cmdsize) { 5343 uint32_t flavor = m_data.GetU32(&offset); 5344 uint32_t count = m_data.GetU32(&offset); 5345 if (count == 0) { 5346 // We've gotten off somehow, log and exit; 5347 return m_entry_point_address; 5348 } 5349 5350 switch (m_header.cputype) { 5351 case llvm::MachO::CPU_TYPE_ARM: 5352 if (flavor == 1 || 5353 flavor == 9) // ARM_THREAD_STATE/ARM_THREAD_STATE32 5354 // from mach/arm/thread_status.h 5355 { 5356 offset += 60; // This is the offset of pc in the GPR thread state 5357 // data structure. 5358 start_address = m_data.GetU32(&offset); 5359 done = true; 5360 } 5361 break; 5362 case llvm::MachO::CPU_TYPE_ARM64: 5363 case llvm::MachO::CPU_TYPE_ARM64_32: 5364 if (flavor == 6) // ARM_THREAD_STATE64 from mach/arm/thread_status.h 5365 { 5366 offset += 256; // This is the offset of pc in the GPR thread state 5367 // data structure. 5368 start_address = m_data.GetU64(&offset); 5369 done = true; 5370 } 5371 break; 5372 case llvm::MachO::CPU_TYPE_I386: 5373 if (flavor == 5374 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h 5375 { 5376 offset += 40; // This is the offset of eip in the GPR thread state 5377 // data structure. 5378 start_address = m_data.GetU32(&offset); 5379 done = true; 5380 } 5381 break; 5382 case llvm::MachO::CPU_TYPE_X86_64: 5383 if (flavor == 5384 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h 5385 { 5386 offset += 16 * 8; // This is the offset of rip in the GPR thread 5387 // state data structure. 5388 start_address = m_data.GetU64(&offset); 5389 done = true; 5390 } 5391 break; 5392 default: 5393 return m_entry_point_address; 5394 } 5395 // Haven't found the GPR flavor yet, skip over the data for this 5396 // flavor: 5397 if (done) 5398 break; 5399 offset += count * 4; 5400 } 5401 } break; 5402 case LC_MAIN: { 5403 ConstString text_segment_name("__TEXT"); 5404 uint64_t entryoffset = m_data.GetU64(&offset); 5405 SectionSP text_segment_sp = 5406 GetSectionList()->FindSectionByName(text_segment_name); 5407 if (text_segment_sp) { 5408 done = true; 5409 start_address = text_segment_sp->GetFileAddress() + entryoffset; 5410 } 5411 } break; 5412 5413 default: 5414 break; 5415 } 5416 if (done) 5417 break; 5418 5419 // Go to the next load command: 5420 offset = cmd_offset + load_cmd.cmdsize; 5421 } 5422 5423 if (start_address == LLDB_INVALID_ADDRESS && IsDynamicLoader()) { 5424 if (GetSymtab()) { 5425 Symbol *dyld_start_sym = GetSymtab()->FindFirstSymbolWithNameAndType( 5426 ConstString("_dyld_start"), SymbolType::eSymbolTypeCode, 5427 Symtab::eDebugAny, Symtab::eVisibilityAny); 5428 if (dyld_start_sym && dyld_start_sym->GetAddress().IsValid()) { 5429 start_address = dyld_start_sym->GetAddress().GetFileAddress(); 5430 } 5431 } 5432 } 5433 5434 if (start_address != LLDB_INVALID_ADDRESS) { 5435 // We got the start address from the load commands, so now resolve that 5436 // address in the sections of this ObjectFile: 5437 if (!m_entry_point_address.ResolveAddressUsingFileSections( 5438 start_address, GetSectionList())) { 5439 m_entry_point_address.Clear(); 5440 } 5441 } else { 5442 // We couldn't read the UnixThread load command - maybe it wasn't there. 5443 // As a fallback look for the "start" symbol in the main executable. 5444 5445 ModuleSP module_sp(GetModule()); 5446 5447 if (module_sp) { 5448 SymbolContextList contexts; 5449 SymbolContext context; 5450 module_sp->FindSymbolsWithNameAndType(ConstString("start"), 5451 eSymbolTypeCode, contexts); 5452 if (contexts.GetSize()) { 5453 if (contexts.GetContextAtIndex(0, context)) 5454 m_entry_point_address = context.symbol->GetAddress(); 5455 } 5456 } 5457 } 5458 } 5459 5460 return m_entry_point_address; 5461 } 5462 5463 lldb_private::Address ObjectFileMachO::GetBaseAddress() { 5464 lldb_private::Address header_addr; 5465 SectionList *section_list = GetSectionList(); 5466 if (section_list) { 5467 SectionSP text_segment_sp( 5468 section_list->FindSectionByName(GetSegmentNameTEXT())); 5469 if (text_segment_sp) { 5470 header_addr.SetSection(text_segment_sp); 5471 header_addr.SetOffset(0); 5472 } 5473 } 5474 return header_addr; 5475 } 5476 5477 uint32_t ObjectFileMachO::GetNumThreadContexts() { 5478 ModuleSP module_sp(GetModule()); 5479 if (module_sp) { 5480 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 5481 if (!m_thread_context_offsets_valid) { 5482 m_thread_context_offsets_valid = true; 5483 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 5484 FileRangeArray::Entry file_range; 5485 llvm::MachO::thread_command thread_cmd; 5486 for (uint32_t i = 0; i < m_header.ncmds; ++i) { 5487 const uint32_t cmd_offset = offset; 5488 if (m_data.GetU32(&offset, &thread_cmd, 2) == nullptr) 5489 break; 5490 5491 if (thread_cmd.cmd == LC_THREAD) { 5492 file_range.SetRangeBase(offset); 5493 file_range.SetByteSize(thread_cmd.cmdsize - 8); 5494 m_thread_context_offsets.Append(file_range); 5495 } 5496 offset = cmd_offset + thread_cmd.cmdsize; 5497 } 5498 } 5499 } 5500 return m_thread_context_offsets.GetSize(); 5501 } 5502 5503 std::string ObjectFileMachO::GetIdentifierString() { 5504 std::string result; 5505 ModuleSP module_sp(GetModule()); 5506 if (module_sp) { 5507 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 5508 5509 // First, look over the load commands for an LC_NOTE load command with 5510 // data_owner string "kern ver str" & use that if found. 5511 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 5512 for (uint32_t i = 0; i < m_header.ncmds; ++i) { 5513 const uint32_t cmd_offset = offset; 5514 llvm::MachO::load_command lc; 5515 if (m_data.GetU32(&offset, &lc.cmd, 2) == nullptr) 5516 break; 5517 if (lc.cmd == LC_NOTE) { 5518 char data_owner[17]; 5519 m_data.CopyData(offset, 16, data_owner); 5520 data_owner[16] = '\0'; 5521 offset += 16; 5522 uint64_t fileoff = m_data.GetU64_unchecked(&offset); 5523 uint64_t size = m_data.GetU64_unchecked(&offset); 5524 5525 // "kern ver str" has a uint32_t version and then a nul terminated 5526 // c-string. 5527 if (strcmp("kern ver str", data_owner) == 0) { 5528 offset = fileoff; 5529 uint32_t version; 5530 if (m_data.GetU32(&offset, &version, 1) != nullptr) { 5531 if (version == 1) { 5532 uint32_t strsize = size - sizeof(uint32_t); 5533 char *buf = (char *)malloc(strsize); 5534 if (buf) { 5535 m_data.CopyData(offset, strsize, buf); 5536 buf[strsize - 1] = '\0'; 5537 result = buf; 5538 if (buf) 5539 free(buf); 5540 return result; 5541 } 5542 } 5543 } 5544 } 5545 } 5546 offset = cmd_offset + lc.cmdsize; 5547 } 5548 5549 // Second, make a pass over the load commands looking for an obsolete 5550 // LC_IDENT load command. 5551 offset = MachHeaderSizeFromMagic(m_header.magic); 5552 for (uint32_t i = 0; i < m_header.ncmds; ++i) { 5553 const uint32_t cmd_offset = offset; 5554 llvm::MachO::ident_command ident_command; 5555 if (m_data.GetU32(&offset, &ident_command, 2) == nullptr) 5556 break; 5557 if (ident_command.cmd == LC_IDENT && ident_command.cmdsize != 0) { 5558 char *buf = (char *)malloc(ident_command.cmdsize); 5559 if (buf != nullptr && m_data.CopyData(offset, ident_command.cmdsize, 5560 buf) == ident_command.cmdsize) { 5561 buf[ident_command.cmdsize - 1] = '\0'; 5562 result = buf; 5563 } 5564 if (buf) 5565 free(buf); 5566 } 5567 offset = cmd_offset + ident_command.cmdsize; 5568 } 5569 } 5570 return result; 5571 } 5572 5573 bool ObjectFileMachO::GetCorefileMainBinaryInfo(addr_t &address, UUID &uuid, 5574 ObjectFile::BinaryType &type) { 5575 address = LLDB_INVALID_ADDRESS; 5576 uuid.Clear(); 5577 ModuleSP module_sp(GetModule()); 5578 if (module_sp) { 5579 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 5580 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 5581 for (uint32_t i = 0; i < m_header.ncmds; ++i) { 5582 const uint32_t cmd_offset = offset; 5583 llvm::MachO::load_command lc; 5584 if (m_data.GetU32(&offset, &lc.cmd, 2) == nullptr) 5585 break; 5586 if (lc.cmd == LC_NOTE) { 5587 char data_owner[17]; 5588 memset(data_owner, 0, sizeof(data_owner)); 5589 m_data.CopyData(offset, 16, data_owner); 5590 offset += 16; 5591 uint64_t fileoff = m_data.GetU64_unchecked(&offset); 5592 uint64_t size = m_data.GetU64_unchecked(&offset); 5593 5594 // "main bin spec" (main binary specification) data payload is 5595 // formatted: 5596 // uint32_t version [currently 1] 5597 // uint32_t type [0 == unspecified, 1 == kernel, 5598 // 2 == user process, 3 == firmware ] 5599 // uint64_t address [ UINT64_MAX if address not specified ] 5600 // uuid_t uuid [ all zero's if uuid not specified ] 5601 // uint32_t log2_pagesize [ process page size in log base 5602 // 2, e.g. 4k pages are 12. 5603 // 0 for unspecified ] 5604 // uint32_t unused [ for alignment ] 5605 5606 if (strcmp("main bin spec", data_owner) == 0 && size >= 32) { 5607 offset = fileoff; 5608 uint32_t version; 5609 if (m_data.GetU32(&offset, &version, 1) != nullptr && version == 1) { 5610 uint32_t binspec_type = 0; 5611 uuid_t raw_uuid; 5612 memset(raw_uuid, 0, sizeof(uuid_t)); 5613 5614 if (m_data.GetU32(&offset, &binspec_type, 1) && 5615 m_data.GetU64(&offset, &address, 1) && 5616 m_data.CopyData(offset, sizeof(uuid_t), raw_uuid) != 0) { 5617 uuid = UUID::fromOptionalData(raw_uuid, sizeof(uuid_t)); 5618 // convert the "main bin spec" type into our 5619 // ObjectFile::BinaryType enum 5620 switch (binspec_type) { 5621 case 0: 5622 type = eBinaryTypeUnknown; 5623 break; 5624 case 1: 5625 type = eBinaryTypeKernel; 5626 break; 5627 case 2: 5628 type = eBinaryTypeUser; 5629 break; 5630 case 3: 5631 type = eBinaryTypeStandalone; 5632 break; 5633 } 5634 return true; 5635 } 5636 } 5637 } 5638 } 5639 offset = cmd_offset + lc.cmdsize; 5640 } 5641 } 5642 return false; 5643 } 5644 5645 lldb::RegisterContextSP 5646 ObjectFileMachO::GetThreadContextAtIndex(uint32_t idx, 5647 lldb_private::Thread &thread) { 5648 lldb::RegisterContextSP reg_ctx_sp; 5649 5650 ModuleSP module_sp(GetModule()); 5651 if (module_sp) { 5652 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 5653 if (!m_thread_context_offsets_valid) 5654 GetNumThreadContexts(); 5655 5656 const FileRangeArray::Entry *thread_context_file_range = 5657 m_thread_context_offsets.GetEntryAtIndex(idx); 5658 if (thread_context_file_range) { 5659 5660 DataExtractor data(m_data, thread_context_file_range->GetRangeBase(), 5661 thread_context_file_range->GetByteSize()); 5662 5663 switch (m_header.cputype) { 5664 case llvm::MachO::CPU_TYPE_ARM64: 5665 case llvm::MachO::CPU_TYPE_ARM64_32: 5666 reg_ctx_sp = 5667 std::make_shared<RegisterContextDarwin_arm64_Mach>(thread, data); 5668 break; 5669 5670 case llvm::MachO::CPU_TYPE_ARM: 5671 reg_ctx_sp = 5672 std::make_shared<RegisterContextDarwin_arm_Mach>(thread, data); 5673 break; 5674 5675 case llvm::MachO::CPU_TYPE_I386: 5676 reg_ctx_sp = 5677 std::make_shared<RegisterContextDarwin_i386_Mach>(thread, data); 5678 break; 5679 5680 case llvm::MachO::CPU_TYPE_X86_64: 5681 reg_ctx_sp = 5682 std::make_shared<RegisterContextDarwin_x86_64_Mach>(thread, data); 5683 break; 5684 } 5685 } 5686 } 5687 return reg_ctx_sp; 5688 } 5689 5690 ObjectFile::Type ObjectFileMachO::CalculateType() { 5691 switch (m_header.filetype) { 5692 case MH_OBJECT: // 0x1u 5693 if (GetAddressByteSize() == 4) { 5694 // 32 bit kexts are just object files, but they do have a valid 5695 // UUID load command. 5696 if (GetUUID()) { 5697 // this checking for the UUID load command is not enough we could 5698 // eventually look for the symbol named "OSKextGetCurrentIdentifier" as 5699 // this is required of kexts 5700 if (m_strata == eStrataInvalid) 5701 m_strata = eStrataKernel; 5702 return eTypeSharedLibrary; 5703 } 5704 } 5705 return eTypeObjectFile; 5706 5707 case MH_EXECUTE: 5708 return eTypeExecutable; // 0x2u 5709 case MH_FVMLIB: 5710 return eTypeSharedLibrary; // 0x3u 5711 case MH_CORE: 5712 return eTypeCoreFile; // 0x4u 5713 case MH_PRELOAD: 5714 return eTypeSharedLibrary; // 0x5u 5715 case MH_DYLIB: 5716 return eTypeSharedLibrary; // 0x6u 5717 case MH_DYLINKER: 5718 return eTypeDynamicLinker; // 0x7u 5719 case MH_BUNDLE: 5720 return eTypeSharedLibrary; // 0x8u 5721 case MH_DYLIB_STUB: 5722 return eTypeStubLibrary; // 0x9u 5723 case MH_DSYM: 5724 return eTypeDebugInfo; // 0xAu 5725 case MH_KEXT_BUNDLE: 5726 return eTypeSharedLibrary; // 0xBu 5727 default: 5728 break; 5729 } 5730 return eTypeUnknown; 5731 } 5732 5733 ObjectFile::Strata ObjectFileMachO::CalculateStrata() { 5734 switch (m_header.filetype) { 5735 case MH_OBJECT: // 0x1u 5736 { 5737 // 32 bit kexts are just object files, but they do have a valid 5738 // UUID load command. 5739 if (GetUUID()) { 5740 // this checking for the UUID load command is not enough we could 5741 // eventually look for the symbol named "OSKextGetCurrentIdentifier" as 5742 // this is required of kexts 5743 if (m_type == eTypeInvalid) 5744 m_type = eTypeSharedLibrary; 5745 5746 return eStrataKernel; 5747 } 5748 } 5749 return eStrataUnknown; 5750 5751 case MH_EXECUTE: // 0x2u 5752 // Check for the MH_DYLDLINK bit in the flags 5753 if (m_header.flags & MH_DYLDLINK) { 5754 return eStrataUser; 5755 } else { 5756 SectionList *section_list = GetSectionList(); 5757 if (section_list) { 5758 static ConstString g_kld_section_name("__KLD"); 5759 if (section_list->FindSectionByName(g_kld_section_name)) 5760 return eStrataKernel; 5761 } 5762 } 5763 return eStrataRawImage; 5764 5765 case MH_FVMLIB: 5766 return eStrataUser; // 0x3u 5767 case MH_CORE: 5768 return eStrataUnknown; // 0x4u 5769 case MH_PRELOAD: 5770 return eStrataRawImage; // 0x5u 5771 case MH_DYLIB: 5772 return eStrataUser; // 0x6u 5773 case MH_DYLINKER: 5774 return eStrataUser; // 0x7u 5775 case MH_BUNDLE: 5776 return eStrataUser; // 0x8u 5777 case MH_DYLIB_STUB: 5778 return eStrataUser; // 0x9u 5779 case MH_DSYM: 5780 return eStrataUnknown; // 0xAu 5781 case MH_KEXT_BUNDLE: 5782 return eStrataKernel; // 0xBu 5783 default: 5784 break; 5785 } 5786 return eStrataUnknown; 5787 } 5788 5789 llvm::VersionTuple ObjectFileMachO::GetVersion() { 5790 ModuleSP module_sp(GetModule()); 5791 if (module_sp) { 5792 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 5793 llvm::MachO::dylib_command load_cmd; 5794 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 5795 uint32_t version_cmd = 0; 5796 uint64_t version = 0; 5797 uint32_t i; 5798 for (i = 0; i < m_header.ncmds; ++i) { 5799 const lldb::offset_t cmd_offset = offset; 5800 if (m_data.GetU32(&offset, &load_cmd, 2) == nullptr) 5801 break; 5802 5803 if (load_cmd.cmd == LC_ID_DYLIB) { 5804 if (version_cmd == 0) { 5805 version_cmd = load_cmd.cmd; 5806 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == nullptr) 5807 break; 5808 version = load_cmd.dylib.current_version; 5809 } 5810 break; // Break for now unless there is another more complete version 5811 // number load command in the future. 5812 } 5813 offset = cmd_offset + load_cmd.cmdsize; 5814 } 5815 5816 if (version_cmd == LC_ID_DYLIB) { 5817 unsigned major = (version & 0xFFFF0000ull) >> 16; 5818 unsigned minor = (version & 0x0000FF00ull) >> 8; 5819 unsigned subminor = (version & 0x000000FFull); 5820 return llvm::VersionTuple(major, minor, subminor); 5821 } 5822 } 5823 return llvm::VersionTuple(); 5824 } 5825 5826 ArchSpec ObjectFileMachO::GetArchitecture() { 5827 ModuleSP module_sp(GetModule()); 5828 ArchSpec arch; 5829 if (module_sp) { 5830 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 5831 5832 return GetArchitecture(module_sp, m_header, m_data, 5833 MachHeaderSizeFromMagic(m_header.magic)); 5834 } 5835 return arch; 5836 } 5837 5838 void ObjectFileMachO::GetProcessSharedCacheUUID(Process *process, 5839 addr_t &base_addr, UUID &uuid) { 5840 uuid.Clear(); 5841 base_addr = LLDB_INVALID_ADDRESS; 5842 if (process && process->GetDynamicLoader()) { 5843 DynamicLoader *dl = process->GetDynamicLoader(); 5844 LazyBool using_shared_cache; 5845 LazyBool private_shared_cache; 5846 dl->GetSharedCacheInformation(base_addr, uuid, using_shared_cache, 5847 private_shared_cache); 5848 } 5849 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS | 5850 LIBLLDB_LOG_PROCESS)); 5851 LLDB_LOGF( 5852 log, 5853 "inferior process shared cache has a UUID of %s, base address 0x%" PRIx64, 5854 uuid.GetAsString().c_str(), base_addr); 5855 } 5856 5857 // From dyld SPI header dyld_process_info.h 5858 typedef void *dyld_process_info; 5859 struct lldb_copy__dyld_process_cache_info { 5860 uuid_t cacheUUID; // UUID of cache used by process 5861 uint64_t cacheBaseAddress; // load address of dyld shared cache 5862 bool noCache; // process is running without a dyld cache 5863 bool privateCache; // process is using a private copy of its dyld cache 5864 }; 5865 5866 // #including mach/mach.h pulls in machine.h & CPU_TYPE_ARM etc conflicts with 5867 // llvm enum definitions llvm::MachO::CPU_TYPE_ARM turning them into compile 5868 // errors. So we need to use the actual underlying types of task_t and 5869 // kern_return_t below. 5870 extern "C" unsigned int /*task_t*/ mach_task_self(); 5871 5872 void ObjectFileMachO::GetLLDBSharedCacheUUID(addr_t &base_addr, UUID &uuid) { 5873 uuid.Clear(); 5874 base_addr = LLDB_INVALID_ADDRESS; 5875 5876 #if defined(__APPLE__) 5877 uint8_t *(*dyld_get_all_image_infos)(void); 5878 dyld_get_all_image_infos = 5879 (uint8_t * (*)()) dlsym(RTLD_DEFAULT, "_dyld_get_all_image_infos"); 5880 if (dyld_get_all_image_infos) { 5881 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos(); 5882 if (dyld_all_image_infos_address) { 5883 uint32_t *version = (uint32_t *) 5884 dyld_all_image_infos_address; // version <mach-o/dyld_images.h> 5885 if (*version >= 13) { 5886 uuid_t *sharedCacheUUID_address = 0; 5887 int wordsize = sizeof(uint8_t *); 5888 if (wordsize == 8) { 5889 sharedCacheUUID_address = 5890 (uuid_t *)((uint8_t *)dyld_all_image_infos_address + 5891 160); // sharedCacheUUID <mach-o/dyld_images.h> 5892 if (*version >= 15) 5893 base_addr = 5894 *(uint64_t 5895 *)((uint8_t *)dyld_all_image_infos_address + 5896 176); // sharedCacheBaseAddress <mach-o/dyld_images.h> 5897 } else { 5898 sharedCacheUUID_address = 5899 (uuid_t *)((uint8_t *)dyld_all_image_infos_address + 5900 84); // sharedCacheUUID <mach-o/dyld_images.h> 5901 if (*version >= 15) { 5902 base_addr = 0; 5903 base_addr = 5904 *(uint32_t 5905 *)((uint8_t *)dyld_all_image_infos_address + 5906 100); // sharedCacheBaseAddress <mach-o/dyld_images.h> 5907 } 5908 } 5909 uuid = UUID::fromOptionalData(sharedCacheUUID_address, sizeof(uuid_t)); 5910 } 5911 } 5912 } else { 5913 // Exists in macOS 10.12 and later, iOS 10.0 and later - dyld SPI 5914 dyld_process_info (*dyld_process_info_create)( 5915 unsigned int /* task_t */ task, uint64_t timestamp, 5916 unsigned int /*kern_return_t*/ *kernelError); 5917 void (*dyld_process_info_get_cache)(void *info, void *cacheInfo); 5918 void (*dyld_process_info_release)(dyld_process_info info); 5919 5920 dyld_process_info_create = (void *(*)(unsigned int /* task_t */, uint64_t, 5921 unsigned int /*kern_return_t*/ *)) 5922 dlsym(RTLD_DEFAULT, "_dyld_process_info_create"); 5923 dyld_process_info_get_cache = (void (*)(void *, void *))dlsym( 5924 RTLD_DEFAULT, "_dyld_process_info_get_cache"); 5925 dyld_process_info_release = 5926 (void (*)(void *))dlsym(RTLD_DEFAULT, "_dyld_process_info_release"); 5927 5928 if (dyld_process_info_create && dyld_process_info_get_cache) { 5929 unsigned int /*kern_return_t */ kern_ret; 5930 dyld_process_info process_info = 5931 dyld_process_info_create(::mach_task_self(), 0, &kern_ret); 5932 if (process_info) { 5933 struct lldb_copy__dyld_process_cache_info sc_info; 5934 memset(&sc_info, 0, sizeof(struct lldb_copy__dyld_process_cache_info)); 5935 dyld_process_info_get_cache(process_info, &sc_info); 5936 if (sc_info.cacheBaseAddress != 0) { 5937 base_addr = sc_info.cacheBaseAddress; 5938 uuid = UUID::fromOptionalData(sc_info.cacheUUID, sizeof(uuid_t)); 5939 } 5940 dyld_process_info_release(process_info); 5941 } 5942 } 5943 } 5944 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS | 5945 LIBLLDB_LOG_PROCESS)); 5946 if (log && uuid.IsValid()) 5947 LLDB_LOGF(log, 5948 "lldb's in-memory shared cache has a UUID of %s base address of " 5949 "0x%" PRIx64, 5950 uuid.GetAsString().c_str(), base_addr); 5951 #endif 5952 } 5953 5954 llvm::VersionTuple ObjectFileMachO::GetMinimumOSVersion() { 5955 if (!m_min_os_version) { 5956 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 5957 for (uint32_t i = 0; i < m_header.ncmds; ++i) { 5958 const lldb::offset_t load_cmd_offset = offset; 5959 5960 llvm::MachO::version_min_command lc; 5961 if (m_data.GetU32(&offset, &lc.cmd, 2) == nullptr) 5962 break; 5963 if (lc.cmd == llvm::MachO::LC_VERSION_MIN_MACOSX || 5964 lc.cmd == llvm::MachO::LC_VERSION_MIN_IPHONEOS || 5965 lc.cmd == llvm::MachO::LC_VERSION_MIN_TVOS || 5966 lc.cmd == llvm::MachO::LC_VERSION_MIN_WATCHOS) { 5967 if (m_data.GetU32(&offset, &lc.version, 5968 (sizeof(lc) / sizeof(uint32_t)) - 2)) { 5969 const uint32_t xxxx = lc.version >> 16; 5970 const uint32_t yy = (lc.version >> 8) & 0xffu; 5971 const uint32_t zz = lc.version & 0xffu; 5972 if (xxxx) { 5973 m_min_os_version = llvm::VersionTuple(xxxx, yy, zz); 5974 break; 5975 } 5976 } 5977 } else if (lc.cmd == llvm::MachO::LC_BUILD_VERSION) { 5978 // struct build_version_command { 5979 // uint32_t cmd; /* LC_BUILD_VERSION */ 5980 // uint32_t cmdsize; /* sizeof(struct 5981 // build_version_command) plus */ 5982 // /* ntools * sizeof(struct 5983 // build_tool_version) */ 5984 // uint32_t platform; /* platform */ 5985 // uint32_t minos; /* X.Y.Z is encoded in nibbles 5986 // xxxx.yy.zz */ uint32_t sdk; /* X.Y.Z is encoded in 5987 // nibbles xxxx.yy.zz */ uint32_t ntools; /* number of 5988 // tool entries following this */ 5989 // }; 5990 5991 offset += 4; // skip platform 5992 uint32_t minos = m_data.GetU32(&offset); 5993 5994 const uint32_t xxxx = minos >> 16; 5995 const uint32_t yy = (minos >> 8) & 0xffu; 5996 const uint32_t zz = minos & 0xffu; 5997 if (xxxx) { 5998 m_min_os_version = llvm::VersionTuple(xxxx, yy, zz); 5999 break; 6000 } 6001 } 6002 6003 offset = load_cmd_offset + lc.cmdsize; 6004 } 6005 6006 if (!m_min_os_version) { 6007 // Set version to an empty value so we don't keep trying to 6008 m_min_os_version = llvm::VersionTuple(); 6009 } 6010 } 6011 6012 return *m_min_os_version; 6013 } 6014 6015 llvm::VersionTuple ObjectFileMachO::GetSDKVersion() { 6016 if (!m_sdk_versions.hasValue()) { 6017 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); 6018 for (uint32_t i = 0; i < m_header.ncmds; ++i) { 6019 const lldb::offset_t load_cmd_offset = offset; 6020 6021 llvm::MachO::version_min_command lc; 6022 if (m_data.GetU32(&offset, &lc.cmd, 2) == nullptr) 6023 break; 6024 if (lc.cmd == llvm::MachO::LC_VERSION_MIN_MACOSX || 6025 lc.cmd == llvm::MachO::LC_VERSION_MIN_IPHONEOS || 6026 lc.cmd == llvm::MachO::LC_VERSION_MIN_TVOS || 6027 lc.cmd == llvm::MachO::LC_VERSION_MIN_WATCHOS) { 6028 if (m_data.GetU32(&offset, &lc.version, 6029 (sizeof(lc) / sizeof(uint32_t)) - 2)) { 6030 const uint32_t xxxx = lc.sdk >> 16; 6031 const uint32_t yy = (lc.sdk >> 8) & 0xffu; 6032 const uint32_t zz = lc.sdk & 0xffu; 6033 if (xxxx) { 6034 m_sdk_versions = llvm::VersionTuple(xxxx, yy, zz); 6035 break; 6036 } else { 6037 GetModule()->ReportWarning("minimum OS version load command with " 6038 "invalid (0) version found."); 6039 } 6040 } 6041 } 6042 offset = load_cmd_offset + lc.cmdsize; 6043 } 6044 6045 if (!m_sdk_versions.hasValue()) { 6046 offset = MachHeaderSizeFromMagic(m_header.magic); 6047 for (uint32_t i = 0; i < m_header.ncmds; ++i) { 6048 const lldb::offset_t load_cmd_offset = offset; 6049 6050 llvm::MachO::version_min_command lc; 6051 if (m_data.GetU32(&offset, &lc.cmd, 2) == nullptr) 6052 break; 6053 if (lc.cmd == llvm::MachO::LC_BUILD_VERSION) { 6054 // struct build_version_command { 6055 // uint32_t cmd; /* LC_BUILD_VERSION */ 6056 // uint32_t cmdsize; /* sizeof(struct 6057 // build_version_command) plus */ 6058 // /* ntools * sizeof(struct 6059 // build_tool_version) */ 6060 // uint32_t platform; /* platform */ 6061 // uint32_t minos; /* X.Y.Z is encoded in nibbles 6062 // xxxx.yy.zz */ uint32_t sdk; /* X.Y.Z is encoded 6063 // in nibbles xxxx.yy.zz */ uint32_t ntools; /* number 6064 // of tool entries following this */ 6065 // }; 6066 6067 offset += 4; // skip platform 6068 uint32_t minos = m_data.GetU32(&offset); 6069 6070 const uint32_t xxxx = minos >> 16; 6071 const uint32_t yy = (minos >> 8) & 0xffu; 6072 const uint32_t zz = minos & 0xffu; 6073 if (xxxx) { 6074 m_sdk_versions = llvm::VersionTuple(xxxx, yy, zz); 6075 break; 6076 } 6077 } 6078 offset = load_cmd_offset + lc.cmdsize; 6079 } 6080 } 6081 6082 if (!m_sdk_versions.hasValue()) 6083 m_sdk_versions = llvm::VersionTuple(); 6084 } 6085 6086 return m_sdk_versions.getValue(); 6087 } 6088 6089 bool ObjectFileMachO::GetIsDynamicLinkEditor() { 6090 return m_header.filetype == llvm::MachO::MH_DYLINKER; 6091 } 6092 6093 bool ObjectFileMachO::AllowAssemblyEmulationUnwindPlans() { 6094 return m_allow_assembly_emulation_unwind_plans; 6095 } 6096 6097 // PluginInterface protocol 6098 lldb_private::ConstString ObjectFileMachO::GetPluginName() { 6099 return GetPluginNameStatic(); 6100 } 6101 6102 uint32_t ObjectFileMachO::GetPluginVersion() { return 1; } 6103 6104 Section *ObjectFileMachO::GetMachHeaderSection() { 6105 // Find the first address of the mach header which is the first non-zero file 6106 // sized section whose file offset is zero. This is the base file address of 6107 // the mach-o file which can be subtracted from the vmaddr of the other 6108 // segments found in memory and added to the load address 6109 ModuleSP module_sp = GetModule(); 6110 if (!module_sp) 6111 return nullptr; 6112 SectionList *section_list = GetSectionList(); 6113 if (!section_list) 6114 return nullptr; 6115 const size_t num_sections = section_list->GetSize(); 6116 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 6117 Section *section = section_list->GetSectionAtIndex(sect_idx).get(); 6118 if (section->GetFileOffset() == 0 && SectionIsLoadable(section)) 6119 return section; 6120 } 6121 return nullptr; 6122 } 6123 6124 bool ObjectFileMachO::SectionIsLoadable(const Section *section) { 6125 if (!section) 6126 return false; 6127 const bool is_dsym = (m_header.filetype == MH_DSYM); 6128 if (section->GetFileSize() == 0 && !is_dsym) 6129 return false; 6130 if (section->IsThreadSpecific()) 6131 return false; 6132 if (GetModule().get() != section->GetModule().get()) 6133 return false; 6134 // Be careful with __LINKEDIT and __DWARF segments 6135 if (section->GetName() == GetSegmentNameLINKEDIT() || 6136 section->GetName() == GetSegmentNameDWARF()) { 6137 // Only map __LINKEDIT and __DWARF if we have an in memory image and 6138 // this isn't a kernel binary like a kext or mach_kernel. 6139 const bool is_memory_image = (bool)m_process_wp.lock(); 6140 const Strata strata = GetStrata(); 6141 if (is_memory_image == false || strata == eStrataKernel) 6142 return false; 6143 } 6144 return true; 6145 } 6146 6147 lldb::addr_t ObjectFileMachO::CalculateSectionLoadAddressForMemoryImage( 6148 lldb::addr_t header_load_address, const Section *header_section, 6149 const Section *section) { 6150 ModuleSP module_sp = GetModule(); 6151 if (module_sp && header_section && section && 6152 header_load_address != LLDB_INVALID_ADDRESS) { 6153 lldb::addr_t file_addr = header_section->GetFileAddress(); 6154 if (file_addr != LLDB_INVALID_ADDRESS && SectionIsLoadable(section)) 6155 return section->GetFileAddress() - file_addr + header_load_address; 6156 } 6157 return LLDB_INVALID_ADDRESS; 6158 } 6159 6160 bool ObjectFileMachO::SetLoadAddress(Target &target, lldb::addr_t value, 6161 bool value_is_offset) { 6162 ModuleSP module_sp = GetModule(); 6163 if (!module_sp) 6164 return false; 6165 6166 SectionList *section_list = GetSectionList(); 6167 if (!section_list) 6168 return false; 6169 6170 size_t num_loaded_sections = 0; 6171 const size_t num_sections = section_list->GetSize(); 6172 6173 if (value_is_offset) { 6174 // "value" is an offset to apply to each top level segment 6175 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 6176 // Iterate through the object file sections to find all of the 6177 // sections that size on disk (to avoid __PAGEZERO) and load them 6178 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 6179 if (SectionIsLoadable(section_sp.get())) 6180 if (target.GetSectionLoadList().SetSectionLoadAddress( 6181 section_sp, section_sp->GetFileAddress() + value)) 6182 ++num_loaded_sections; 6183 } 6184 } else { 6185 // "value" is the new base address of the mach_header, adjust each 6186 // section accordingly 6187 6188 Section *mach_header_section = GetMachHeaderSection(); 6189 if (mach_header_section) { 6190 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 6191 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 6192 6193 lldb::addr_t section_load_addr = 6194 CalculateSectionLoadAddressForMemoryImage( 6195 value, mach_header_section, section_sp.get()); 6196 if (section_load_addr != LLDB_INVALID_ADDRESS) { 6197 if (target.GetSectionLoadList().SetSectionLoadAddress( 6198 section_sp, section_load_addr)) 6199 ++num_loaded_sections; 6200 } 6201 } 6202 } 6203 } 6204 return num_loaded_sections > 0; 6205 } 6206 6207 bool ObjectFileMachO::SaveCore(const lldb::ProcessSP &process_sp, 6208 const FileSpec &outfile, Status &error) { 6209 if (!process_sp) 6210 return false; 6211 6212 Target &target = process_sp->GetTarget(); 6213 const ArchSpec target_arch = target.GetArchitecture(); 6214 const llvm::Triple &target_triple = target_arch.GetTriple(); 6215 if (target_triple.getVendor() == llvm::Triple::Apple && 6216 (target_triple.getOS() == llvm::Triple::MacOSX || 6217 target_triple.getOS() == llvm::Triple::IOS || 6218 target_triple.getOS() == llvm::Triple::WatchOS || 6219 target_triple.getOS() == llvm::Triple::TvOS)) { 6220 // NEED_BRIDGEOS_TRIPLE target_triple.getOS() == llvm::Triple::BridgeOS)) 6221 // { 6222 bool make_core = false; 6223 switch (target_arch.GetMachine()) { 6224 case llvm::Triple::aarch64: 6225 case llvm::Triple::aarch64_32: 6226 case llvm::Triple::arm: 6227 case llvm::Triple::thumb: 6228 case llvm::Triple::x86: 6229 case llvm::Triple::x86_64: 6230 make_core = true; 6231 break; 6232 default: 6233 error.SetErrorStringWithFormat("unsupported core architecture: %s", 6234 target_triple.str().c_str()); 6235 break; 6236 } 6237 6238 if (make_core) { 6239 std::vector<llvm::MachO::segment_command_64> segment_load_commands; 6240 // uint32_t range_info_idx = 0; 6241 MemoryRegionInfo range_info; 6242 Status range_error = process_sp->GetMemoryRegionInfo(0, range_info); 6243 const uint32_t addr_byte_size = target_arch.GetAddressByteSize(); 6244 const ByteOrder byte_order = target_arch.GetByteOrder(); 6245 if (range_error.Success()) { 6246 while (range_info.GetRange().GetRangeBase() != LLDB_INVALID_ADDRESS) { 6247 const addr_t addr = range_info.GetRange().GetRangeBase(); 6248 const addr_t size = range_info.GetRange().GetByteSize(); 6249 6250 if (size == 0) 6251 break; 6252 6253 // Calculate correct protections 6254 uint32_t prot = 0; 6255 if (range_info.GetReadable() == MemoryRegionInfo::eYes) 6256 prot |= VM_PROT_READ; 6257 if (range_info.GetWritable() == MemoryRegionInfo::eYes) 6258 prot |= VM_PROT_WRITE; 6259 if (range_info.GetExecutable() == MemoryRegionInfo::eYes) 6260 prot |= VM_PROT_EXECUTE; 6261 6262 if (prot != 0) { 6263 uint32_t cmd_type = LC_SEGMENT_64; 6264 uint32_t segment_size = sizeof(llvm::MachO::segment_command_64); 6265 if (addr_byte_size == 4) { 6266 cmd_type = LC_SEGMENT; 6267 segment_size = sizeof(llvm::MachO::segment_command); 6268 } 6269 llvm::MachO::segment_command_64 segment = { 6270 cmd_type, // uint32_t cmd; 6271 segment_size, // uint32_t cmdsize; 6272 {0}, // char segname[16]; 6273 addr, // uint64_t vmaddr; // uint32_t for 32-bit Mach-O 6274 size, // uint64_t vmsize; // uint32_t for 32-bit Mach-O 6275 0, // uint64_t fileoff; // uint32_t for 32-bit Mach-O 6276 size, // uint64_t filesize; // uint32_t for 32-bit Mach-O 6277 prot, // uint32_t maxprot; 6278 prot, // uint32_t initprot; 6279 0, // uint32_t nsects; 6280 0}; // uint32_t flags; 6281 segment_load_commands.push_back(segment); 6282 } else { 6283 // No protections and a size of 1 used to be returned from old 6284 // debugservers when we asked about a region that was past the 6285 // last memory region and it indicates the end... 6286 if (size == 1) 6287 break; 6288 } 6289 6290 range_error = process_sp->GetMemoryRegionInfo( 6291 range_info.GetRange().GetRangeEnd(), range_info); 6292 if (range_error.Fail()) 6293 break; 6294 } 6295 6296 StreamString buffer(Stream::eBinary, addr_byte_size, byte_order); 6297 6298 llvm::MachO::mach_header_64 mach_header; 6299 if (addr_byte_size == 8) { 6300 mach_header.magic = MH_MAGIC_64; 6301 } else { 6302 mach_header.magic = MH_MAGIC; 6303 } 6304 mach_header.cputype = target_arch.GetMachOCPUType(); 6305 mach_header.cpusubtype = target_arch.GetMachOCPUSubType(); 6306 mach_header.filetype = MH_CORE; 6307 mach_header.ncmds = segment_load_commands.size(); 6308 mach_header.flags = 0; 6309 mach_header.reserved = 0; 6310 ThreadList &thread_list = process_sp->GetThreadList(); 6311 const uint32_t num_threads = thread_list.GetSize(); 6312 6313 // Make an array of LC_THREAD data items. Each one contains the 6314 // contents of the LC_THREAD load command. The data doesn't contain 6315 // the load command + load command size, we will add the load command 6316 // and load command size as we emit the data. 6317 std::vector<StreamString> LC_THREAD_datas(num_threads); 6318 for (auto &LC_THREAD_data : LC_THREAD_datas) { 6319 LC_THREAD_data.GetFlags().Set(Stream::eBinary); 6320 LC_THREAD_data.SetAddressByteSize(addr_byte_size); 6321 LC_THREAD_data.SetByteOrder(byte_order); 6322 } 6323 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) { 6324 ThreadSP thread_sp(thread_list.GetThreadAtIndex(thread_idx)); 6325 if (thread_sp) { 6326 switch (mach_header.cputype) { 6327 case llvm::MachO::CPU_TYPE_ARM64: 6328 case llvm::MachO::CPU_TYPE_ARM64_32: 6329 RegisterContextDarwin_arm64_Mach::Create_LC_THREAD( 6330 thread_sp.get(), LC_THREAD_datas[thread_idx]); 6331 break; 6332 6333 case llvm::MachO::CPU_TYPE_ARM: 6334 RegisterContextDarwin_arm_Mach::Create_LC_THREAD( 6335 thread_sp.get(), LC_THREAD_datas[thread_idx]); 6336 break; 6337 6338 case llvm::MachO::CPU_TYPE_I386: 6339 RegisterContextDarwin_i386_Mach::Create_LC_THREAD( 6340 thread_sp.get(), LC_THREAD_datas[thread_idx]); 6341 break; 6342 6343 case llvm::MachO::CPU_TYPE_X86_64: 6344 RegisterContextDarwin_x86_64_Mach::Create_LC_THREAD( 6345 thread_sp.get(), LC_THREAD_datas[thread_idx]); 6346 break; 6347 } 6348 } 6349 } 6350 6351 // The size of the load command is the size of the segments... 6352 if (addr_byte_size == 8) { 6353 mach_header.sizeofcmds = segment_load_commands.size() * 6354 sizeof(llvm::MachO::segment_command_64); 6355 } else { 6356 mach_header.sizeofcmds = segment_load_commands.size() * 6357 sizeof(llvm::MachO::segment_command); 6358 } 6359 6360 // and the size of all LC_THREAD load command 6361 for (const auto &LC_THREAD_data : LC_THREAD_datas) { 6362 ++mach_header.ncmds; 6363 mach_header.sizeofcmds += 8 + LC_THREAD_data.GetSize(); 6364 } 6365 6366 // Write the mach header 6367 buffer.PutHex32(mach_header.magic); 6368 buffer.PutHex32(mach_header.cputype); 6369 buffer.PutHex32(mach_header.cpusubtype); 6370 buffer.PutHex32(mach_header.filetype); 6371 buffer.PutHex32(mach_header.ncmds); 6372 buffer.PutHex32(mach_header.sizeofcmds); 6373 buffer.PutHex32(mach_header.flags); 6374 if (addr_byte_size == 8) { 6375 buffer.PutHex32(mach_header.reserved); 6376 } 6377 6378 // Skip the mach header and all load commands and align to the next 6379 // 0x1000 byte boundary 6380 addr_t file_offset = buffer.GetSize() + mach_header.sizeofcmds; 6381 if (file_offset & 0x00000fff) { 6382 file_offset += 0x00001000ull; 6383 file_offset &= (~0x00001000ull + 1); 6384 } 6385 6386 for (auto &segment : segment_load_commands) { 6387 segment.fileoff = file_offset; 6388 file_offset += segment.filesize; 6389 } 6390 6391 // Write out all of the LC_THREAD load commands 6392 for (const auto &LC_THREAD_data : LC_THREAD_datas) { 6393 const size_t LC_THREAD_data_size = LC_THREAD_data.GetSize(); 6394 buffer.PutHex32(LC_THREAD); 6395 buffer.PutHex32(8 + LC_THREAD_data_size); // cmd + cmdsize + data 6396 buffer.Write(LC_THREAD_data.GetString().data(), LC_THREAD_data_size); 6397 } 6398 6399 // Write out all of the segment load commands 6400 for (const auto &segment : segment_load_commands) { 6401 printf("0x%8.8x 0x%8.8x [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 6402 ") [0x%16.16" PRIx64 " 0x%16.16" PRIx64 6403 ") 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x]\n", 6404 segment.cmd, segment.cmdsize, segment.vmaddr, 6405 segment.vmaddr + segment.vmsize, segment.fileoff, 6406 segment.filesize, segment.maxprot, segment.initprot, 6407 segment.nsects, segment.flags); 6408 6409 buffer.PutHex32(segment.cmd); 6410 buffer.PutHex32(segment.cmdsize); 6411 buffer.PutRawBytes(segment.segname, sizeof(segment.segname)); 6412 if (addr_byte_size == 8) { 6413 buffer.PutHex64(segment.vmaddr); 6414 buffer.PutHex64(segment.vmsize); 6415 buffer.PutHex64(segment.fileoff); 6416 buffer.PutHex64(segment.filesize); 6417 } else { 6418 buffer.PutHex32(static_cast<uint32_t>(segment.vmaddr)); 6419 buffer.PutHex32(static_cast<uint32_t>(segment.vmsize)); 6420 buffer.PutHex32(static_cast<uint32_t>(segment.fileoff)); 6421 buffer.PutHex32(static_cast<uint32_t>(segment.filesize)); 6422 } 6423 buffer.PutHex32(segment.maxprot); 6424 buffer.PutHex32(segment.initprot); 6425 buffer.PutHex32(segment.nsects); 6426 buffer.PutHex32(segment.flags); 6427 } 6428 6429 std::string core_file_path(outfile.GetPath()); 6430 auto core_file = FileSystem::Instance().Open( 6431 outfile, File::eOpenOptionWrite | File::eOpenOptionTruncate | 6432 File::eOpenOptionCanCreate); 6433 if (!core_file) { 6434 error = core_file.takeError(); 6435 } else { 6436 // Read 1 page at a time 6437 uint8_t bytes[0x1000]; 6438 // Write the mach header and load commands out to the core file 6439 size_t bytes_written = buffer.GetString().size(); 6440 error = 6441 core_file.get()->Write(buffer.GetString().data(), bytes_written); 6442 if (error.Success()) { 6443 // Now write the file data for all memory segments in the process 6444 for (const auto &segment : segment_load_commands) { 6445 if (core_file.get()->SeekFromStart(segment.fileoff) == -1) { 6446 error.SetErrorStringWithFormat( 6447 "unable to seek to offset 0x%" PRIx64 " in '%s'", 6448 segment.fileoff, core_file_path.c_str()); 6449 break; 6450 } 6451 6452 printf("Saving %" PRId64 6453 " bytes of data for memory region at 0x%" PRIx64 "\n", 6454 segment.vmsize, segment.vmaddr); 6455 addr_t bytes_left = segment.vmsize; 6456 addr_t addr = segment.vmaddr; 6457 Status memory_read_error; 6458 while (bytes_left > 0 && error.Success()) { 6459 const size_t bytes_to_read = 6460 bytes_left > sizeof(bytes) ? sizeof(bytes) : bytes_left; 6461 6462 // In a savecore setting, we don't really care about caching, 6463 // as the data is dumped and very likely never read again, 6464 // so we call ReadMemoryFromInferior to bypass it. 6465 const size_t bytes_read = process_sp->ReadMemoryFromInferior( 6466 addr, bytes, bytes_to_read, memory_read_error); 6467 6468 if (bytes_read == bytes_to_read) { 6469 size_t bytes_written = bytes_read; 6470 error = core_file.get()->Write(bytes, bytes_written); 6471 bytes_left -= bytes_read; 6472 addr += bytes_read; 6473 } else { 6474 // Some pages within regions are not readable, those should 6475 // be zero filled 6476 memset(bytes, 0, bytes_to_read); 6477 size_t bytes_written = bytes_to_read; 6478 error = core_file.get()->Write(bytes, bytes_written); 6479 bytes_left -= bytes_to_read; 6480 addr += bytes_to_read; 6481 } 6482 } 6483 } 6484 } 6485 } 6486 } else { 6487 error.SetErrorString( 6488 "process doesn't support getting memory region info"); 6489 } 6490 } 6491 return true; // This is the right plug to handle saving core files for 6492 // this process 6493 } 6494 return false; 6495 } 6496