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