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