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