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