Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ObjectFileMachO.cpp -----------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "llvm/ADT/StringRef.h"
10
11
#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
12
#include "Plugins/Process/Utility/RegisterContextDarwin_arm64.h"
13
#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
14
#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
15
#include "lldb/Core/Debugger.h"
16
#include "lldb/Core/FileSpecList.h"
17
#include "lldb/Core/Module.h"
18
#include "lldb/Core/ModuleSpec.h"
19
#include "lldb/Core/PluginManager.h"
20
#include "lldb/Core/Progress.h"
21
#include "lldb/Core/Section.h"
22
#include "lldb/Core/StreamFile.h"
23
#include "lldb/Host/Host.h"
24
#include "lldb/Symbol/DWARFCallFrameInfo.h"
25
#include "lldb/Symbol/LocateSymbolFile.h"
26
#include "lldb/Symbol/ObjectFile.h"
27
#include "lldb/Target/DynamicLoader.h"
28
#include "lldb/Target/MemoryRegionInfo.h"
29
#include "lldb/Target/Platform.h"
30
#include "lldb/Target/Process.h"
31
#include "lldb/Target/SectionLoadList.h"
32
#include "lldb/Target/Target.h"
33
#include "lldb/Target/Thread.h"
34
#include "lldb/Target/ThreadList.h"
35
#include "lldb/Utility/ArchSpec.h"
36
#include "lldb/Utility/DataBuffer.h"
37
#include "lldb/Utility/FileSpec.h"
38
#include "lldb/Utility/Log.h"
39
#include "lldb/Utility/RangeMap.h"
40
#include "lldb/Utility/RegisterValue.h"
41
#include "lldb/Utility/Status.h"
42
#include "lldb/Utility/StreamString.h"
43
#include "lldb/Utility/Timer.h"
44
#include "lldb/Utility/UUID.h"
45
46
#include "lldb/Host/SafeMachO.h"
47
48
#include "llvm/ADT/DenseSet.h"
49
#include "llvm/Support/FormatVariadic.h"
50
#include "llvm/Support/MemoryBuffer.h"
51
52
#include "ObjectFileMachO.h"
53
54
#if defined(__APPLE__)
55
#include <TargetConditionals.h>
56
// GetLLDBSharedCacheUUID() needs to call dlsym()
57
#include <dlfcn.h>
58
#endif
59
60
#ifndef __APPLE__
61
#include "Utility/UuidCompatibility.h"
62
#else
63
#include <uuid/uuid.h>
64
#endif
65
66
#include <bitset>
67
#include <memory>
68
69
// Unfortunately the signpost header pulls in the system MachO header, too.
70
#ifdef CPU_TYPE_ARM
71
#undef CPU_TYPE_ARM
72
#endif
73
#ifdef CPU_TYPE_ARM64
74
#undef CPU_TYPE_ARM64
75
#endif
76
#ifdef CPU_TYPE_ARM64_32
77
#undef CPU_TYPE_ARM64_32
78
#endif
79
#ifdef CPU_TYPE_I386
80
#undef CPU_TYPE_I386
81
#endif
82
#ifdef CPU_TYPE_X86_64
83
#undef CPU_TYPE_X86_64
84
#endif
85
#ifdef MH_DYLINKER
86
#undef MH_DYLINKER
87
#endif
88
#ifdef MH_OBJECT
89
#undef MH_OBJECT
90
#endif
91
#ifdef LC_VERSION_MIN_MACOSX
92
#undef LC_VERSION_MIN_MACOSX
93
#endif
94
#ifdef LC_VERSION_MIN_IPHONEOS
95
#undef LC_VERSION_MIN_IPHONEOS
96
#endif
97
#ifdef LC_VERSION_MIN_TVOS
98
#undef LC_VERSION_MIN_TVOS
99
#endif
100
#ifdef LC_VERSION_MIN_WATCHOS
101
#undef LC_VERSION_MIN_WATCHOS
102
#endif
103
#ifdef LC_BUILD_VERSION
104
#undef LC_BUILD_VERSION
105
#endif
106
#ifdef PLATFORM_MACOS
107
#undef PLATFORM_MACOS
108
#endif
109
#ifdef PLATFORM_MACCATALYST
110
#undef PLATFORM_MACCATALYST
111
#endif
112
#ifdef PLATFORM_IOS
113
#undef PLATFORM_IOS
114
#endif
115
#ifdef PLATFORM_IOSSIMULATOR
116
#undef PLATFORM_IOSSIMULATOR
117
#endif
118
#ifdef PLATFORM_TVOS
119
#undef PLATFORM_TVOS
120
#endif
121
#ifdef PLATFORM_TVOSSIMULATOR
122
#undef PLATFORM_TVOSSIMULATOR
123
#endif
124
#ifdef PLATFORM_WATCHOS
125
#undef PLATFORM_WATCHOS
126
#endif
127
#ifdef PLATFORM_WATCHOSSIMULATOR
128
#undef PLATFORM_WATCHOSSIMULATOR
129
#endif
130
131
0
#define THUMB_ADDRESS_BIT_MASK 0xfffffffffffffffeull
132
using namespace lldb;
133
using namespace lldb_private;
134
using namespace llvm::MachO;
135
136
LLDB_PLUGIN_DEFINE(ObjectFileMachO)
137
138
// Some structure definitions needed for parsing the dyld shared cache files
139
// found on iOS devices.
140
141
struct lldb_copy_dyld_cache_header_v1 {
142
  char magic[16];         // e.g. "dyld_v0    i386", "dyld_v1   armv7", etc.
143
  uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
144
  uint32_t mappingCount;  // number of dyld_cache_mapping_info entries
145
  uint32_t imagesOffset;
146
  uint32_t imagesCount;
147
  uint64_t dyldBaseAddress;
148
  uint64_t codeSignatureOffset;
149
  uint64_t codeSignatureSize;
150
  uint64_t slideInfoOffset;
151
  uint64_t slideInfoSize;
152
  uint64_t localSymbolsOffset;
153
  uint64_t localSymbolsSize;
154
  uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13
155
                    // and later
156
};
157
158
struct lldb_copy_dyld_cache_mapping_info {
159
  uint64_t address;
160
  uint64_t size;
161
  uint64_t fileOffset;
162
  uint32_t maxProt;
163
  uint32_t initProt;
164
};
165
166
struct lldb_copy_dyld_cache_local_symbols_info {
167
  uint32_t nlistOffset;
168
  uint32_t nlistCount;
169
  uint32_t stringsOffset;
170
  uint32_t stringsSize;
171
  uint32_t entriesOffset;
172
  uint32_t entriesCount;
173
};
174
struct lldb_copy_dyld_cache_local_symbols_entry {
175
  uint32_t dylibOffset;
176
  uint32_t nlistStartIndex;
177
  uint32_t nlistCount;
178
};
179
180
static void PrintRegisterValue(RegisterContext *reg_ctx, const char *name,
181
                               const char *alt_name, size_t reg_byte_size,
182
0
                               Stream &data) {
183
0
  const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
184
0
  if (reg_info == nullptr)
185
0
    reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
186
0
  if (reg_info) {
187
0
    lldb_private::RegisterValue reg_value;
188
0
    if (reg_ctx->ReadRegister(reg_info, reg_value)) {
189
0
      if (reg_info->byte_size >= reg_byte_size)
190
0
        data.Write(reg_value.GetBytes(), reg_byte_size);
191
0
      else {
192
0
        data.Write(reg_value.GetBytes(), reg_info->byte_size);
193
0
        for (size_t i = 0, n = reg_byte_size - reg_info->byte_size; i < n; ++i)
194
0
          data.PutChar(0);
195
0
      }
196
0
      return;
197
0
    }
198
0
  }
199
  // Just write zeros if all else fails
200
0
  for (size_t i = 0; i < reg_byte_size; ++i)
201
0
    data.PutChar(0);
202
0
}
203
204
class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64 {
205
public:
206
  RegisterContextDarwin_x86_64_Mach(lldb_private::Thread &thread,
207
                                    const DataExtractor &data)
208
9
      : RegisterContextDarwin_x86_64(thread, 0) {
209
9
    SetRegisterDataFrom_LC_THREAD(data);
210
9
  }
211
212
0
  void InvalidateAllRegisters() override {
213
    // Do nothing... registers are always valid...
214
0
  }
215
216
9
  void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) {
217
9
    lldb::offset_t offset = 0;
218
9
    SetError(GPRRegSet, Read, -1);
219
9
    SetError(FPURegSet, Read, -1);
220
9
    SetError(EXCRegSet, Read, -1);
221
9
    bool done = false;
222
223
18
    while (!done) {
224
9
      int flavor = data.GetU32(&offset);
225
9
      if (flavor == 0)
226
0
        done = true;
227
9
      else {
228
9
        uint32_t i;
229
9
        uint32_t count = data.GetU32(&offset);
230
9
        switch (flavor) {
231
9
        case GPRRegSet:
232
387
          for (i = 0; i < count; 
++i378
)
233
378
            (&gpr.rax)[i] = data.GetU64(&offset);
234
9
          SetError(GPRRegSet, Read, 0);
235
9
          done = true;
236
237
9
          break;
238
0
        case FPURegSet:
239
          // TODO: fill in FPU regs....
240
          // SetError (FPURegSet, Read, -1);
241
0
          done = true;
242
243
0
          break;
244
0
        case EXCRegSet:
245
0
          exc.trapno = data.GetU32(&offset);
246
0
          exc.err = data.GetU32(&offset);
247
0
          exc.faultvaddr = data.GetU64(&offset);
248
0
          SetError(EXCRegSet, Read, 0);
249
0
          done = true;
250
0
          break;
251
0
        case 7:
252
0
        case 8:
253
0
        case 9:
254
          // fancy flavors that encapsulate of the above flavors...
255
0
          break;
256
257
0
        default:
258
0
          done = true;
259
0
          break;
260
9
        }
261
9
      }
262
9
    }
263
9
  }
264
265
0
  static bool Create_LC_THREAD(Thread *thread, Stream &data) {
266
0
    RegisterContextSP reg_ctx_sp(thread->GetRegisterContext());
267
0
    if (reg_ctx_sp) {
268
0
      RegisterContext *reg_ctx = reg_ctx_sp.get();
269
270
0
      data.PutHex32(GPRRegSet); // Flavor
271
0
      data.PutHex32(GPRWordCount);
272
0
      PrintRegisterValue(reg_ctx, "rax", nullptr, 8, data);
273
0
      PrintRegisterValue(reg_ctx, "rbx", nullptr, 8, data);
274
0
      PrintRegisterValue(reg_ctx, "rcx", nullptr, 8, data);
275
0
      PrintRegisterValue(reg_ctx, "rdx", nullptr, 8, data);
276
0
      PrintRegisterValue(reg_ctx, "rdi", nullptr, 8, data);
277
0
      PrintRegisterValue(reg_ctx, "rsi", nullptr, 8, data);
278
0
      PrintRegisterValue(reg_ctx, "rbp", nullptr, 8, data);
279
0
      PrintRegisterValue(reg_ctx, "rsp", nullptr, 8, data);
280
0
      PrintRegisterValue(reg_ctx, "r8", nullptr, 8, data);
281
0
      PrintRegisterValue(reg_ctx, "r9", nullptr, 8, data);
282
0
      PrintRegisterValue(reg_ctx, "r10", nullptr, 8, data);
283
0
      PrintRegisterValue(reg_ctx, "r11", nullptr, 8, data);
284
0
      PrintRegisterValue(reg_ctx, "r12", nullptr, 8, data);
285
0
      PrintRegisterValue(reg_ctx, "r13", nullptr, 8, data);
286
0
      PrintRegisterValue(reg_ctx, "r14", nullptr, 8, data);
287
0
      PrintRegisterValue(reg_ctx, "r15", nullptr, 8, data);
288
0
      PrintRegisterValue(reg_ctx, "rip", nullptr, 8, data);
289
0
      PrintRegisterValue(reg_ctx, "rflags", nullptr, 8, data);
290
0
      PrintRegisterValue(reg_ctx, "cs", nullptr, 8, data);
291
0
      PrintRegisterValue(reg_ctx, "fs", nullptr, 8, data);
292
0
      PrintRegisterValue(reg_ctx, "gs", nullptr, 8, data);
293
294
      //            // Write out the FPU registers
295
      //            const size_t fpu_byte_size = sizeof(FPU);
296
      //            size_t bytes_written = 0;
297
      //            data.PutHex32 (FPURegSet);
298
      //            data.PutHex32 (fpu_byte_size/sizeof(uint64_t));
299
      //            bytes_written += data.PutHex32(0); // uint32_t pad[0]
300
      //            bytes_written += data.PutHex32(0); // uint32_t pad[1]
301
      //            bytes_written += WriteRegister (reg_ctx, "fcw", "fctrl", 2,
302
      //            data);   // uint16_t    fcw;    // "fctrl"
303
      //            bytes_written += WriteRegister (reg_ctx, "fsw" , "fstat", 2,
304
      //            data);  // uint16_t    fsw;    // "fstat"
305
      //            bytes_written += WriteRegister (reg_ctx, "ftw" , "ftag", 1,
306
      //            data);   // uint8_t     ftw;    // "ftag"
307
      //            bytes_written += data.PutHex8  (0); // uint8_t pad1;
308
      //            bytes_written += WriteRegister (reg_ctx, "fop" , NULL, 2,
309
      //            data);     // uint16_t    fop;    // "fop"
310
      //            bytes_written += WriteRegister (reg_ctx, "fioff", "ip", 4,
311
      //            data);    // uint32_t    ip;     // "fioff"
312
      //            bytes_written += WriteRegister (reg_ctx, "fiseg", NULL, 2,
313
      //            data);    // uint16_t    cs;     // "fiseg"
314
      //            bytes_written += data.PutHex16 (0); // uint16_t    pad2;
315
      //            bytes_written += WriteRegister (reg_ctx, "dp", "fooff" , 4,
316
      //            data);   // uint32_t    dp;     // "fooff"
317
      //            bytes_written += WriteRegister (reg_ctx, "foseg", NULL, 2,
318
      //            data);    // uint16_t    ds;     // "foseg"
319
      //            bytes_written += data.PutHex16 (0); // uint16_t    pad3;
320
      //            bytes_written += WriteRegister (reg_ctx, "mxcsr", NULL, 4,
321
      //            data);    // uint32_t    mxcsr;
322
      //            bytes_written += WriteRegister (reg_ctx, "mxcsrmask", NULL,
323
      //            4, data);// uint32_t    mxcsrmask;
324
      //            bytes_written += WriteRegister (reg_ctx, "stmm0", NULL,
325
      //            sizeof(MMSReg), data);
326
      //            bytes_written += WriteRegister (reg_ctx, "stmm1", NULL,
327
      //            sizeof(MMSReg), data);
328
      //            bytes_written += WriteRegister (reg_ctx, "stmm2", NULL,
329
      //            sizeof(MMSReg), data);
330
      //            bytes_written += WriteRegister (reg_ctx, "stmm3", NULL,
331
      //            sizeof(MMSReg), data);
332
      //            bytes_written += WriteRegister (reg_ctx, "stmm4", NULL,
333
      //            sizeof(MMSReg), data);
334
      //            bytes_written += WriteRegister (reg_ctx, "stmm5", NULL,
335
      //            sizeof(MMSReg), data);
336
      //            bytes_written += WriteRegister (reg_ctx, "stmm6", NULL,
337
      //            sizeof(MMSReg), data);
338
      //            bytes_written += WriteRegister (reg_ctx, "stmm7", NULL,
339
      //            sizeof(MMSReg), data);
340
      //            bytes_written += WriteRegister (reg_ctx, "xmm0" , NULL,
341
      //            sizeof(XMMReg), data);
342
      //            bytes_written += WriteRegister (reg_ctx, "xmm1" , NULL,
343
      //            sizeof(XMMReg), data);
344
      //            bytes_written += WriteRegister (reg_ctx, "xmm2" , NULL,
345
      //            sizeof(XMMReg), data);
346
      //            bytes_written += WriteRegister (reg_ctx, "xmm3" , NULL,
347
      //            sizeof(XMMReg), data);
348
      //            bytes_written += WriteRegister (reg_ctx, "xmm4" , NULL,
349
      //            sizeof(XMMReg), data);
350
      //            bytes_written += WriteRegister (reg_ctx, "xmm5" , NULL,
351
      //            sizeof(XMMReg), data);
352
      //            bytes_written += WriteRegister (reg_ctx, "xmm6" , NULL,
353
      //            sizeof(XMMReg), data);
354
      //            bytes_written += WriteRegister (reg_ctx, "xmm7" , NULL,
355
      //            sizeof(XMMReg), data);
356
      //            bytes_written += WriteRegister (reg_ctx, "xmm8" , NULL,
357
      //            sizeof(XMMReg), data);
358
      //            bytes_written += WriteRegister (reg_ctx, "xmm9" , NULL,
359
      //            sizeof(XMMReg), data);
360
      //            bytes_written += WriteRegister (reg_ctx, "xmm10", NULL,
361
      //            sizeof(XMMReg), data);
362
      //            bytes_written += WriteRegister (reg_ctx, "xmm11", NULL,
363
      //            sizeof(XMMReg), data);
364
      //            bytes_written += WriteRegister (reg_ctx, "xmm12", NULL,
365
      //            sizeof(XMMReg), data);
366
      //            bytes_written += WriteRegister (reg_ctx, "xmm13", NULL,
367
      //            sizeof(XMMReg), data);
368
      //            bytes_written += WriteRegister (reg_ctx, "xmm14", NULL,
369
      //            sizeof(XMMReg), data);
370
      //            bytes_written += WriteRegister (reg_ctx, "xmm15", NULL,
371
      //            sizeof(XMMReg), data);
372
      //
373
      //            // Fill rest with zeros
374
      //            for (size_t i=0, n = fpu_byte_size - bytes_written; i<n; ++
375
      //            i)
376
      //                data.PutChar(0);
377
378
      // Write out the EXC registers
379
0
      data.PutHex32(EXCRegSet);
380
0
      data.PutHex32(EXCWordCount);
381
0
      PrintRegisterValue(reg_ctx, "trapno", nullptr, 4, data);
382
0
      PrintRegisterValue(reg_ctx, "err", nullptr, 4, data);
383
0
      PrintRegisterValue(reg_ctx, "faultvaddr", nullptr, 8, data);
384
0
      return true;
385
0
    }
386
0
    return false;
387
0
  }
388
389
protected:
390
0
  int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return 0; }
391
392
0
  int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return 0; }
393
394
0
  int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return 0; }
395
396
0
  int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override {
397
0
    return 0;
398
0
  }
399
400
0
  int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override {
401
0
    return 0;
402
0
  }
403
404
0
  int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override {
405
0
    return 0;
406
0
  }
407
};
408
409
class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386 {
410
public:
411
  RegisterContextDarwin_i386_Mach(lldb_private::Thread &thread,
412
                                  const DataExtractor &data)
413
0
      : RegisterContextDarwin_i386(thread, 0) {
414
0
    SetRegisterDataFrom_LC_THREAD(data);
415
0
  }
416
417
0
  void InvalidateAllRegisters() override {
418
    // Do nothing... registers are always valid...
419
0
  }
420
421
0
  void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) {
422
0
    lldb::offset_t offset = 0;
423
0
    SetError(GPRRegSet, Read, -1);
424
0
    SetError(FPURegSet, Read, -1);
425
0
    SetError(EXCRegSet, Read, -1);
426
0
    bool done = false;
427
428
0
    while (!done) {
429
0
      int flavor = data.GetU32(&offset);
430
0
      if (flavor == 0)
431
0
        done = true;
432
0
      else {
433
0
        uint32_t i;
434
0
        uint32_t count = data.GetU32(&offset);
435
0
        switch (flavor) {
436
0
        case GPRRegSet:
437
0
          for (i = 0; i < count; ++i)
438
0
            (&gpr.eax)[i] = data.GetU32(&offset);
439
0
          SetError(GPRRegSet, Read, 0);
440
0
          done = true;
441
442
0
          break;
443
0
        case FPURegSet:
444
          // TODO: fill in FPU regs....
445
          // SetError (FPURegSet, Read, -1);
446
0
          done = true;
447
448
0
          break;
449
0
        case EXCRegSet:
450
0
          exc.trapno = data.GetU32(&offset);
451
0
          exc.err = data.GetU32(&offset);
452
0
          exc.faultvaddr = data.GetU32(&offset);
453
0
          SetError(EXCRegSet, Read, 0);
454
0
          done = true;
455
0
          break;
456
0
        case 7:
457
0
        case 8:
458
0
        case 9:
459
          // fancy flavors that encapsulate of the above flavors...
460
0
          break;
461
462
0
        default:
463
0
          done = true;
464
0
          break;
465
0
        }
466
0
      }
467
0
    }
468
0
  }
469
470
0
  static bool Create_LC_THREAD(Thread *thread, Stream &data) {
471
0
    RegisterContextSP reg_ctx_sp(thread->GetRegisterContext());
472
0
    if (reg_ctx_sp) {
473
0
      RegisterContext *reg_ctx = reg_ctx_sp.get();
474
475
0
      data.PutHex32(GPRRegSet); // Flavor
476
0
      data.PutHex32(GPRWordCount);
477
0
      PrintRegisterValue(reg_ctx, "eax", nullptr, 4, data);
478
0
      PrintRegisterValue(reg_ctx, "ebx", nullptr, 4, data);
479
0
      PrintRegisterValue(reg_ctx, "ecx", nullptr, 4, data);
480
0
      PrintRegisterValue(reg_ctx, "edx", nullptr, 4, data);
481
0
      PrintRegisterValue(reg_ctx, "edi", nullptr, 4, data);
482
0
      PrintRegisterValue(reg_ctx, "esi", nullptr, 4, data);
483
0
      PrintRegisterValue(reg_ctx, "ebp", nullptr, 4, data);
484
0
      PrintRegisterValue(reg_ctx, "esp", nullptr, 4, data);
485
0
      PrintRegisterValue(reg_ctx, "ss", nullptr, 4, data);
486
0
      PrintRegisterValue(reg_ctx, "eflags", nullptr, 4, data);
487
0
      PrintRegisterValue(reg_ctx, "eip", nullptr, 4, data);
488
0
      PrintRegisterValue(reg_ctx, "cs", nullptr, 4, data);
489
0
      PrintRegisterValue(reg_ctx, "ds", nullptr, 4, data);
490
0
      PrintRegisterValue(reg_ctx, "es", nullptr, 4, data);
491
0
      PrintRegisterValue(reg_ctx, "fs", nullptr, 4, data);
492
0
      PrintRegisterValue(reg_ctx, "gs", nullptr, 4, data);
493
494
      // Write out the EXC registers
495
0
      data.PutHex32(EXCRegSet);
496
0
      data.PutHex32(EXCWordCount);
497
0
      PrintRegisterValue(reg_ctx, "trapno", nullptr, 4, data);
498
0
      PrintRegisterValue(reg_ctx, "err", nullptr, 4, data);
499
0
      PrintRegisterValue(reg_ctx, "faultvaddr", nullptr, 4, data);
500
0
      return true;
501
0
    }
502
0
    return false;
503
0
  }
504
505
protected:
506
0
  int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return 0; }
507
508
0
  int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return 0; }
509
510
0
  int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return 0; }
511
512
0
  int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override {
513
0
    return 0;
514
0
  }
515
516
0
  int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override {
517
0
    return 0;
518
0
  }
519
520
0
  int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override {
521
0
    return 0;
522
0
  }
523
};
524
525
class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm {
526
public:
527
  RegisterContextDarwin_arm_Mach(lldb_private::Thread &thread,
528
                                 const DataExtractor &data)
529
0
      : RegisterContextDarwin_arm(thread, 0) {
530
0
    SetRegisterDataFrom_LC_THREAD(data);
531
0
  }
532
533
0
  void InvalidateAllRegisters() override {
534
    // Do nothing... registers are always valid...
535
0
  }
536
537
0
  void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) {
538
0
    lldb::offset_t offset = 0;
539
0
    SetError(GPRRegSet, Read, -1);
540
0
    SetError(FPURegSet, Read, -1);
541
0
    SetError(EXCRegSet, Read, -1);
542
0
    bool done = false;
543
544
0
    while (!done) {
545
0
      int flavor = data.GetU32(&offset);
546
0
      uint32_t count = data.GetU32(&offset);
547
0
      lldb::offset_t next_thread_state = offset + (count * 4);
548
0
      switch (flavor) {
549
0
      case GPRAltRegSet:
550
0
      case GPRRegSet:
551
        // On ARM, the CPSR register is also included in the count but it is
552
        // not included in gpr.r so loop until (count-1).
553
0
        for (uint32_t i = 0; i < (count - 1); ++i) {
554
0
          gpr.r[i] = data.GetU32(&offset);
555
0
        }
556
        // Save cpsr explicitly.
557
0
        gpr.cpsr = data.GetU32(&offset);
558
559
0
        SetError(GPRRegSet, Read, 0);
560
0
        offset = next_thread_state;
561
0
        break;
562
563
0
      case FPURegSet: {
564
0
        uint8_t *fpu_reg_buf = (uint8_t *)&fpu.floats.s[0];
565
0
        const int fpu_reg_buf_size = sizeof(fpu.floats);
566
0
        if (data.ExtractBytes(offset, fpu_reg_buf_size, eByteOrderLittle,
567
0
                              fpu_reg_buf) == fpu_reg_buf_size) {
568
0
          offset += fpu_reg_buf_size;
569
0
          fpu.fpscr = data.GetU32(&offset);
570
0
          SetError(FPURegSet, Read, 0);
571
0
        } else {
572
0
          done = true;
573
0
        }
574
0
      }
575
0
        offset = next_thread_state;
576
0
        break;
577
578
0
      case EXCRegSet:
579
0
        if (count == 3) {
580
0
          exc.exception = data.GetU32(&offset);
581
0
          exc.fsr = data.GetU32(&offset);
582
0
          exc.far = data.GetU32(&offset);
583
0
          SetError(EXCRegSet, Read, 0);
584
0
        }
585
0
        done = true;
586
0
        offset = next_thread_state;
587
0
        break;
588
589
      // Unknown register set flavor, stop trying to parse.
590
0
      default:
591
0
        done = true;
592
0
      }
593
0
    }
594
0
  }
595
596
0
  static bool Create_LC_THREAD(Thread *thread, Stream &data) {
597
0
    RegisterContextSP reg_ctx_sp(thread->GetRegisterContext());
598
0
    if (reg_ctx_sp) {
599
0
      RegisterContext *reg_ctx = reg_ctx_sp.get();
600
601
0
      data.PutHex32(GPRRegSet); // Flavor
602
0
      data.PutHex32(GPRWordCount);
603
0
      PrintRegisterValue(reg_ctx, "r0", nullptr, 4, data);
604
0
      PrintRegisterValue(reg_ctx, "r1", nullptr, 4, data);
605
0
      PrintRegisterValue(reg_ctx, "r2", nullptr, 4, data);
606
0
      PrintRegisterValue(reg_ctx, "r3", nullptr, 4, data);
607
0
      PrintRegisterValue(reg_ctx, "r4", nullptr, 4, data);
608
0
      PrintRegisterValue(reg_ctx, "r5", nullptr, 4, data);
609
0
      PrintRegisterValue(reg_ctx, "r6", nullptr, 4, data);
610
0
      PrintRegisterValue(reg_ctx, "r7", nullptr, 4, data);
611
0
      PrintRegisterValue(reg_ctx, "r8", nullptr, 4, data);
612
0
      PrintRegisterValue(reg_ctx, "r9", nullptr, 4, data);
613
0
      PrintRegisterValue(reg_ctx, "r10", nullptr, 4, data);
614
0
      PrintRegisterValue(reg_ctx, "r11", nullptr, 4, data);
615
0
      PrintRegisterValue(reg_ctx, "r12", nullptr, 4, data);
616
0
      PrintRegisterValue(reg_ctx, "sp", nullptr, 4, data);
617
0
      PrintRegisterValue(reg_ctx, "lr", nullptr, 4, data);
618
0
      PrintRegisterValue(reg_ctx, "pc", nullptr, 4, data);
619
0
      PrintRegisterValue(reg_ctx, "cpsr", nullptr, 4, data);
620
621
      // Write out the EXC registers
622
      //            data.PutHex32 (EXCRegSet);
623
      //            data.PutHex32 (EXCWordCount);
624
      //            WriteRegister (reg_ctx, "exception", NULL, 4, data);
625
      //            WriteRegister (reg_ctx, "fsr", NULL, 4, data);
626
      //            WriteRegister (reg_ctx, "far", NULL, 4, data);
627
0
      return true;
628
0
    }
629
0
    return false;
630
0
  }
631
632
protected:
633
0
  int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return -1; }
634
635
0
  int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return -1; }
636
637
0
  int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return -1; }
638
639
0
  int DoReadDBG(lldb::tid_t tid, int flavor, DBG &dbg) override { return -1; }
640
641
0
  int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override {
642
0
    return 0;
643
0
  }
644
645
0
  int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override {
646
0
    return 0;
647
0
  }
648
649
0
  int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override {
650
0
    return 0;
651
0
  }
652
653
0
  int DoWriteDBG(lldb::tid_t tid, int flavor, const DBG &dbg) override {
654
0
    return -1;
655
0
  }
656
};
657
658
class RegisterContextDarwin_arm64_Mach : public RegisterContextDarwin_arm64 {
659
public:
660
  RegisterContextDarwin_arm64_Mach(lldb_private::Thread &thread,
661
                                   const DataExtractor &data)
662
0
      : RegisterContextDarwin_arm64(thread, 0) {
663
0
    SetRegisterDataFrom_LC_THREAD(data);
664
0
  }
665
666
0
  void InvalidateAllRegisters() override {
667
    // Do nothing... registers are always valid...
668
0
  }
669
670
0
  void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) {
671
0
    lldb::offset_t offset = 0;
672
0
    SetError(GPRRegSet, Read, -1);
673
0
    SetError(FPURegSet, Read, -1);
674
0
    SetError(EXCRegSet, Read, -1);
675
0
    bool done = false;
676
0
    while (!done) {
677
0
      int flavor = data.GetU32(&offset);
678
0
      uint32_t count = data.GetU32(&offset);
679
0
      lldb::offset_t next_thread_state = offset + (count * 4);
680
0
      switch (flavor) {
681
0
      case GPRRegSet:
682
        // x0-x29 + fp + lr + sp + pc (== 33 64-bit registers) plus cpsr (1
683
        // 32-bit register)
684
0
        if (count >= (33 * 2) + 1) {
685
0
          for (uint32_t i = 0; i < 29; ++i)
686
0
            gpr.x[i] = data.GetU64(&offset);
687
0
          gpr.fp = data.GetU64(&offset);
688
0
          gpr.lr = data.GetU64(&offset);
689
0
          gpr.sp = data.GetU64(&offset);
690
0
          gpr.pc = data.GetU64(&offset);
691
0
          gpr.cpsr = data.GetU32(&offset);
692
0
          SetError(GPRRegSet, Read, 0);
693
0
        }
694
0
        offset = next_thread_state;
695
0
        break;
696
0
      case FPURegSet: {
697
0
        uint8_t *fpu_reg_buf = (uint8_t *)&fpu.v[0];
698
0
        const int fpu_reg_buf_size = sizeof(fpu);
699
0
        if (fpu_reg_buf_size == count * sizeof(uint32_t) &&
700
0
            data.ExtractBytes(offset, fpu_reg_buf_size, eByteOrderLittle,
701
0
                              fpu_reg_buf) == fpu_reg_buf_size) {
702
0
          SetError(FPURegSet, Read, 0);
703
0
        } else {
704
0
          done = true;
705
0
        }
706
0
      }
707
0
        offset = next_thread_state;
708
0
        break;
709
0
      case EXCRegSet:
710
0
        if (count == 4) {
711
0
          exc.far = data.GetU64(&offset);
712
0
          exc.esr = data.GetU32(&offset);
713
0
          exc.exception = data.GetU32(&offset);
714
0
          SetError(EXCRegSet, Read, 0);
715
0
        }
716
0
        offset = next_thread_state;
717
0
        break;
718
0
      default:
719
0
        done = true;
720
0
        break;
721
0
      }
722
0
    }
723
0
  }
724
725
0
  static bool Create_LC_THREAD(Thread *thread, Stream &data) {
726
0
    RegisterContextSP reg_ctx_sp(thread->GetRegisterContext());
727
0
    if (reg_ctx_sp) {
728
0
      RegisterContext *reg_ctx = reg_ctx_sp.get();
729
730
0
      data.PutHex32(GPRRegSet); // Flavor
731
0
      data.PutHex32(GPRWordCount);
732
0
      PrintRegisterValue(reg_ctx, "x0", nullptr, 8, data);
733
0
      PrintRegisterValue(reg_ctx, "x1", nullptr, 8, data);
734
0
      PrintRegisterValue(reg_ctx, "x2", nullptr, 8, data);
735
0
      PrintRegisterValue(reg_ctx, "x3", nullptr, 8, data);
736
0
      PrintRegisterValue(reg_ctx, "x4", nullptr, 8, data);
737
0
      PrintRegisterValue(reg_ctx, "x5", nullptr, 8, data);
738
0
      PrintRegisterValue(reg_ctx, "x6", nullptr, 8, data);
739
0
      PrintRegisterValue(reg_ctx, "x7", nullptr, 8, data);
740
0
      PrintRegisterValue(reg_ctx, "x8", nullptr, 8, data);
741
0
      PrintRegisterValue(reg_ctx, "x9", nullptr, 8, data);
742
0
      PrintRegisterValue(reg_ctx, "x10", nullptr, 8, data);
743
0
      PrintRegisterValue(reg_ctx, "x11", nullptr, 8, data);
744
0
      PrintRegisterValue(reg_ctx, "x12", nullptr, 8, data);
745
0
      PrintRegisterValue(reg_ctx, "x13", nullptr, 8, data);
746
0
      PrintRegisterValue(reg_ctx, "x14", nullptr, 8, data);
747
0
      PrintRegisterValue(reg_ctx, "x15", nullptr, 8, data);
748
0
      PrintRegisterValue(reg_ctx, "x16", nullptr, 8, data);
749
0
      PrintRegisterValue(reg_ctx, "x17", nullptr, 8, data);
750
0
      PrintRegisterValue(reg_ctx, "x18", nullptr, 8, data);
751
0
      PrintRegisterValue(reg_ctx, "x19", nullptr, 8, data);
752
0
      PrintRegisterValue(reg_ctx, "x20", nullptr, 8, data);
753
0
      PrintRegisterValue(reg_ctx, "x21", nullptr, 8, data);
754
0
      PrintRegisterValue(reg_ctx, "x22", nullptr, 8, data);
755
0
      PrintRegisterValue(reg_ctx, "x23", nullptr, 8, data);
756
0
      PrintRegisterValue(reg_ctx, "x24", nullptr, 8, data);
757
0
      PrintRegisterValue(reg_ctx, "x25", nullptr, 8, data);
758
0
      PrintRegisterValue(reg_ctx, "x26", nullptr, 8, data);
759
0
      PrintRegisterValue(reg_ctx, "x27", nullptr, 8, data);
760
0
      PrintRegisterValue(reg_ctx, "x28", nullptr, 8, data);
761
0
      PrintRegisterValue(reg_ctx, "fp", nullptr, 8, data);
762
0
      PrintRegisterValue(reg_ctx, "lr", nullptr, 8, data);
763
0
      PrintRegisterValue(reg_ctx, "sp", nullptr, 8, data);
764
0
      PrintRegisterValue(reg_ctx, "pc", nullptr, 8, data);
765
0
      PrintRegisterValue(reg_ctx, "cpsr", nullptr, 4, data);
766
0
      data.PutHex32(0); // uint32_t pad at the end
767
768
      // Write out the EXC registers
769
0
      data.PutHex32(EXCRegSet);
770
0
      data.PutHex32(EXCWordCount);
771
0
      PrintRegisterValue(reg_ctx, "far", nullptr, 8, data);
772
0
      PrintRegisterValue(reg_ctx, "esr", nullptr, 4, data);
773
0
      PrintRegisterValue(reg_ctx, "exception", nullptr, 4, data);
774
0
      return true;
775
0
    }
776
0
    return false;
777
0
  }
778
779
protected:
780
0
  int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return -1; }
781
782
0
  int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return -1; }
783
784
0
  int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return -1; }
785
786
0
  int DoReadDBG(lldb::tid_t tid, int flavor, DBG &dbg) override { return -1; }
787
788
0
  int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override {
789
0
    return 0;
790
0
  }
791
792
0
  int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override {
793
0
    return 0;
794
0
  }
795
796
0
  int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override {
797
0
    return 0;
798
0
  }
799
800
0
  int DoWriteDBG(lldb::tid_t tid, int flavor, const DBG &dbg) override {
801
0
    return -1;
802
0
  }
803
};
804
805
2.14M
static uint32_t MachHeaderSizeFromMagic(uint32_t magic) {
806
2.14M
  switch (magic) {
807
7.97k
  case MH_MAGIC:
808
7.97k
  case MH_CIGAM:
809
7.97k
    return sizeof(struct llvm::MachO::mach_header);
810
811
2.13M
  case MH_MAGIC_64:
812
2.13M
  case MH_CIGAM_64:
813
2.13M
    return sizeof(struct llvm::MachO::mach_header_64);
814
0
    break;
815
816
2.93k
  default:
817
2.93k
    break;
818
2.14M
  }
819
2.93k
  return 0;
820
2.14M
}
821
822
0
#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
823
824
char ObjectFileMachO::ID;
825
826
3.45k
void ObjectFileMachO::Initialize() {
827
3.45k
  PluginManager::RegisterPlugin(
828
3.45k
      GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
829
3.45k
      CreateMemoryInstance, GetModuleSpecifications, SaveCore);
830
3.45k
}
831
832
3.44k
void ObjectFileMachO::Terminate() {
833
3.44k
  PluginManager::UnregisterPlugin(CreateInstance);
834
3.44k
}
835
836
ObjectFile *ObjectFileMachO::CreateInstance(const lldb::ModuleSP &module_sp,
837
                                            DataBufferSP &data_sp,
838
                                            lldb::offset_t data_offset,
839
                                            const FileSpec *file,
840
                                            lldb::offset_t file_offset,
841
139k
                                            lldb::offset_t length) {
842
139k
  if (!data_sp) {
843
0
    data_sp = MapFileData(*file, length, file_offset);
844
0
    if (!data_sp)
845
0
      return nullptr;
846
0
    data_offset = 0;
847
0
  }
848
849
139k
  if (!ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
850
225
    return nullptr;
851
852
  // Update the data to contain the entire file if it doesn't already
853
139k
  if (data_sp->GetByteSize() < length) {
854
11.6k
    data_sp = MapFileData(*file, length, file_offset);
855
11.6k
    if (!data_sp)
856
0
      return nullptr;
857
11.6k
    data_offset = 0;
858
11.6k
  }
859
139k
  auto objfile_up = std::make_unique<ObjectFileMachO>(
860
139k
      module_sp, data_sp, data_offset, file, file_offset, length);
861
139k
  if (!objfile_up || !objfile_up->ParseHeader())
862
0
    return nullptr;
863
864
139k
  return objfile_up.release();
865
139k
}
866
867
ObjectFile *ObjectFileMachO::CreateMemoryInstance(
868
    const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
869
4
    const ProcessSP &process_sp, lldb::addr_t header_addr) {
870
4
  if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) {
871
2
    std::unique_ptr<ObjectFile> objfile_up(
872
2
        new ObjectFileMachO(module_sp, data_sp, process_sp, header_addr));
873
2
    if (objfile_up.get() && objfile_up->ParseHeader())
874
2
      return objfile_up.release();
875
2
  }
876
2
  return nullptr;
877
4
}
878
879
size_t ObjectFileMachO::GetModuleSpecifications(
880
    const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
881
    lldb::offset_t data_offset, lldb::offset_t file_offset,
882
145k
    lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
883
145k
  const size_t initial_count = specs.GetSize();
884
885
145k
  if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) {
886
142k
    DataExtractor data;
887
142k
    data.SetData(data_sp);
888
142k
    llvm::MachO::mach_header header;
889
142k
    if (ParseHeader(data, &data_offset, header)) {
890
142k
      size_t header_and_load_cmds =
891
142k
          header.sizeofcmds + MachHeaderSizeFromMagic(header.magic);
892
142k
      if (header_and_load_cmds >= data_sp->GetByteSize()) {
893
15.2k
        data_sp = MapFileData(file, header_and_load_cmds, file_offset);
894
15.2k
        data.SetData(data_sp);
895
15.2k
        data_offset = MachHeaderSizeFromMagic(header.magic);
896
15.2k
      }
897
142k
      if (data_sp) {
898
142k
        ModuleSpec base_spec;
899
142k
        base_spec.GetFileSpec() = file;
900
142k
        base_spec.SetObjectOffset(file_offset);
901
142k
        base_spec.SetObjectSize(length);
902
142k
        GetAllArchSpecs(header, data, data_offset, base_spec, specs);
903
142k
      }
904
142k
    }
905
142k
  }
906
145k
  return specs.GetSize() - initial_count;
907
145k
}
908
909
153k
ConstString ObjectFileMachO::GetSegmentNameTEXT() {
910
153k
  static ConstString g_segment_name_TEXT("__TEXT");
911
153k
  return g_segment_name_TEXT;
912
153k
}
913
914
135k
ConstString ObjectFileMachO::GetSegmentNameDATA() {
915
135k
  static ConstString g_segment_name_DATA("__DATA");
916
135k
  return g_segment_name_DATA;
917
135k
}
918
919
135k
ConstString ObjectFileMachO::GetSegmentNameDATA_DIRTY() {
920
135k
  static ConstString g_segment_name("__DATA_DIRTY");
921
135k
  return g_segment_name;
922
135k
}
923
924
135k
ConstString ObjectFileMachO::GetSegmentNameDATA_CONST() {
925
135k
  static ConstString g_segment_name("__DATA_CONST");
926
135k
  return g_segment_name;
927
135k
}
928
929
135k
ConstString ObjectFileMachO::GetSegmentNameOBJC() {
930
135k
  static ConstString g_segment_name_OBJC("__OBJC");
931
135k
  return g_segment_name_OBJC;
932
135k
}
933
934
140k
ConstString ObjectFileMachO::GetSegmentNameLINKEDIT() {
935
140k
  static ConstString g_section_name_LINKEDIT("__LINKEDIT");
936
140k
  return g_section_name_LINKEDIT;
937
140k
}
938
939
26
ConstString ObjectFileMachO::GetSegmentNameDWARF() {
940
26
  static ConstString g_section_name("__DWARF");
941
26
  return g_section_name;
942
26
}
943
944
135k
ConstString ObjectFileMachO::GetSectionNameEHFrame() {
945
135k
  static ConstString g_section_name_eh_frame("__eh_frame");
946
135k
  return g_section_name_eh_frame;
947
135k
}
948
949
bool ObjectFileMachO::MagicBytesMatch(DataBufferSP &data_sp,
950
                                      lldb::addr_t data_offset,
951
285k
                                      lldb::addr_t data_length) {
952
285k
  DataExtractor data;
953
285k
  data.SetData(data_sp, data_offset, data_length);
954
285k
  lldb::offset_t offset = 0;
955
285k
  uint32_t magic = data.GetU32(&offset);
956
285k
  return MachHeaderSizeFromMagic(magic) != 0;
957
285k
}
958
959
ObjectFileMachO::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
139k
      m_reexported_dylibs(), m_allow_assembly_emulation_unwind_plans(true) {
969
139k
  ::memset(&m_header, 0, sizeof(m_header));
970
139k
  ::memset(&m_dysymtab, 0, sizeof(m_dysymtab));
971
139k
}
972
973
ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
974
                                 lldb::DataBufferSP &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
2
      m_reexported_dylibs(), m_allow_assembly_emulation_unwind_plans(true) {
981
2
  ::memset(&m_header, 0, sizeof(m_header));
982
2
  ::memset(&m_dysymtab, 0, sizeof(m_dysymtab));
983
2
}
984
985
bool ObjectFileMachO::ParseHeader(DataExtractor &data,
986
                                  lldb::offset_t *data_offset_ptr,
987
143k
                                  llvm::MachO::mach_header &header) {
988
143k
  data.SetByteOrder(endian::InlHostByteOrder());
989
  // Leave magic in the original byte order
990
143k
  header.magic = data.GetU32(data_offset_ptr);
991
143k
  bool can_parse = false;
992
143k
  bool is_64_bit = false;
993
143k
  switch (header.magic) {
994
2.65k
  case MH_MAGIC:
995
2.65k
    data.SetByteOrder(endian::InlHostByteOrder());
996
2.65k
    data.SetAddressByteSize(4);
997
2.65k
    can_parse = true;
998
2.65k
    break;
999
1000
140k
  case MH_MAGIC_64:
1001
140k
    data.SetByteOrder(endian::InlHostByteOrder());
1002
140k
    data.SetAddressByteSize(8);
1003
140k
    can_parse = true;
1004
140k
    is_64_bit = true;
1005
140k
    break;
1006
1007
0
  case MH_CIGAM:
1008
0
    data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig
1009
0
                          ? eByteOrderLittle
1010
0
                          : eByteOrderBig);
1011
0
    data.SetAddressByteSize(4);
1012
0
    can_parse = true;
1013
0
    break;
1014
1015
0
  case MH_CIGAM_64:
1016
0
    data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig
1017
0
                          ? eByteOrderLittle
1018
0
                          : eByteOrderBig);
1019
0
    data.SetAddressByteSize(8);
1020
0
    is_64_bit = true;
1021
0
    can_parse = true;
1022
0
    break;
1023
1024
105
  default:
1025
105
    break;
1026
143k
  }
1027
1028
143k
  if (can_parse) {
1029
142k
    data.GetU32(data_offset_ptr, &header.cputype, 6);
1030
142k
    if (is_64_bit)
1031
140k
      *data_offset_ptr += 4;
1032
142k
    return true;
1033
142k
  } else {
1034
105
    memset(&header, 0, sizeof(header));
1035
105
  }
1036
105
  return false;
1037
143k
}
1038
1039
139k
bool ObjectFileMachO::ParseHeader() {
1040
139k
  ModuleSP module_sp(GetModule());
1041
139k
  if (!module_sp)
1042
0
    return false;
1043
1044
139k
  std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
1045
139k
  bool can_parse = false;
1046
139k
  lldb::offset_t offset = 0;
1047
139k
  m_data.SetByteOrder(endian::InlHostByteOrder());
1048
  // Leave magic in the original byte order
1049
139k
  m_header.magic = m_data.GetU32(&offset);
1050
139k
  switch (m_header.magic) {
1051
1
  case MH_MAGIC:
1052
1
    m_data.SetByteOrder(endian::InlHostByteOrder());
1053
1
    m_data.SetAddressByteSize(4);
1054
1
    can_parse = true;
1055
1
    break;
1056
1057
139k
  case MH_MAGIC_64:
1058
139k
    m_data.SetByteOrder(endian::InlHostByteOrder());
1059
139k
    m_data.SetAddressByteSize(8);
1060
139k
    can_parse = true;
1061
139k
    break;
1062
1063
0
  case MH_CIGAM:
1064
0
    m_data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig
1065
0
                            ? eByteOrderLittle
1066
0
                            : eByteOrderBig);
1067
0
    m_data.SetAddressByteSize(4);
1068
0
    can_parse = true;
1069
0
    break;
1070
1071
0
  case MH_CIGAM_64:
1072
0
    m_data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig
1073
0
                            ? eByteOrderLittle
1074
0
                            : eByteOrderBig);
1075
0
    m_data.SetAddressByteSize(8);
1076
0
    can_parse = true;
1077
0
    break;
1078
1079
0
  default:
1080
0
    break;
1081
139k
  }
1082
1083
139k
  if (can_parse) {
1084
139k
    m_data.GetU32(&offset, &m_header.cputype, 6);
1085
1086
139k
    ModuleSpecList all_specs;
1087
139k
    ModuleSpec base_spec;
1088
139k
    GetAllArchSpecs(m_header, m_data, MachHeaderSizeFromMagic(m_header.magic),
1089
139k
                    base_spec, all_specs);
1090
1091
139k
    for (unsigned i = 0, e = all_specs.GetSize(); i != e; 
++i0
) {
1092
139k
      ArchSpec mach_arch =
1093
139k
          all_specs.GetModuleSpecRefAtIndex(i).GetArchitecture();
1094
1095
      // Check if the module has a required architecture
1096
139k
      const ArchSpec &module_arch = module_sp->GetArchitecture();
1097
139k
      if (module_arch.IsValid() && 
!module_arch.IsCompatibleMatch(mach_arch)139k
)
1098
0
        continue;
1099
1100
139k
      if (SetModulesArchitecture(mach_arch)) {
1101
139k
        const size_t header_and_lc_size =
1102
139k
            m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
1103
139k
        if (m_data.GetByteSize() < header_and_lc_size) {
1104
2
          DataBufferSP data_sp;
1105
2
          ProcessSP process_sp(m_process_wp.lock());
1106
2
          if (process_sp) {
1107
2
            data_sp = ReadMemory(process_sp, m_memory_addr, header_and_lc_size);
1108
2
          } else {
1109
            // Read in all only the load command data from the file on disk
1110
0
            data_sp = MapFileData(m_file, header_and_lc_size, m_file_offset);
1111
0
            if (data_sp->GetByteSize() != header_and_lc_size)
1112
0
              continue;
1113
0
          }
1114
2
          if (data_sp)
1115
2
            m_data.SetData(data_sp);
1116
2
        }
1117
139k
      }
1118
139k
      return true;
1119
139k
    }
1120
    // None found.
1121
0
    return false;
1122
139k
  } else {
1123
0
    memset(&m_header, 0, sizeof(struct llvm::MachO::mach_header));
1124
0
  }
1125
0
  return false;
1126
139k
}
1127
1128
501
ByteOrder ObjectFileMachO::GetByteOrder() const {
1129
501
  return m_data.GetByteOrder();
1130
501
}
1131
1132
541k
bool ObjectFileMachO::IsExecutable() const {
1133
541k
  return m_header.filetype == MH_EXECUTE;
1134
541k
}
1135
1136
0
bool ObjectFileMachO::IsDynamicLoader() const {
1137
0
  return m_header.filetype == MH_DYLINKER;
1138
0
}
1139
1140
2.58M
bool ObjectFileMachO::IsSharedCacheBinary() const {
1141
2.58M
  return m_header.flags & MH_DYLIB_IN_CACHE;
1142
2.58M
}
1143
1144
5.57k
uint32_t ObjectFileMachO::GetAddressByteSize() const {
1145
5.57k
  return m_data.GetAddressByteSize();
1146
5.57k
}
1147
1148
139k
AddressClass ObjectFileMachO::GetAddressClass(lldb::addr_t file_addr) {
1149
139k
  Symtab *symtab = GetSymtab();
1150
139k
  if (!symtab)
1151
0
    return AddressClass::eUnknown;
1152
1153
139k
  Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
1154
139k
  if (symbol) {
1155
139k
    if (symbol->ValueIsAddress()) {
1156
139k
      SectionSP section_sp(symbol->GetAddressRef().GetSection());
1157
139k
      if (section_sp) {
1158
139k
        const lldb::SectionType section_type = section_sp->GetType();
1159
139k
        switch (section_type) {
1160
0
        case eSectionTypeInvalid:
1161
0
          return AddressClass::eUnknown;
1162
1163
139k
        case eSectionTypeCode:
1164
139k
          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
0
            if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1168
0
              return AddressClass::eCodeAlternateISA;
1169
0
          }
1170
139k
          return AddressClass::eCode;
1171
1172
0
        case eSectionTypeContainer:
1173
0
          return AddressClass::eUnknown;
1174
1175
27
        case eSectionTypeData:
1176
27
        case eSectionTypeDataCString:
1177
27
        case eSectionTypeDataCStringPointers:
1178
27
        case eSectionTypeDataSymbolAddress:
1179
27
        case eSectionTypeData4:
1180
27
        case eSectionTypeData8:
1181
27
        case eSectionTypeData16:
1182
163
        case eSectionTypeDataPointers:
1183
174
        case eSectionTypeZeroFill:
1184
174
        case eSectionTypeDataObjCMessageRefs:
1185
174
        case eSectionTypeDataObjCCFStrings:
1186
174
        case eSectionTypeGoSymtab:
1187
174
          return AddressClass::eData;
1188
1189
0
        case eSectionTypeDebug:
1190
0
        case eSectionTypeDWARFDebugAbbrev:
1191
0
        case eSectionTypeDWARFDebugAbbrevDwo:
1192
0
        case eSectionTypeDWARFDebugAddr:
1193
0
        case eSectionTypeDWARFDebugAranges:
1194
0
        case eSectionTypeDWARFDebugCuIndex:
1195
0
        case eSectionTypeDWARFDebugFrame:
1196
0
        case eSectionTypeDWARFDebugInfo:
1197
0
        case eSectionTypeDWARFDebugInfoDwo:
1198
0
        case eSectionTypeDWARFDebugLine:
1199
0
        case eSectionTypeDWARFDebugLineStr:
1200
0
        case eSectionTypeDWARFDebugLoc:
1201
0
        case eSectionTypeDWARFDebugLocDwo:
1202
0
        case eSectionTypeDWARFDebugLocLists:
1203
0
        case eSectionTypeDWARFDebugLocListsDwo:
1204
0
        case eSectionTypeDWARFDebugMacInfo:
1205
0
        case eSectionTypeDWARFDebugMacro:
1206
0
        case eSectionTypeDWARFDebugNames:
1207
0
        case eSectionTypeDWARFDebugPubNames:
1208
0
        case eSectionTypeDWARFDebugPubTypes:
1209
0
        case eSectionTypeDWARFDebugRanges:
1210
0
        case eSectionTypeDWARFDebugRngLists:
1211
0
        case eSectionTypeDWARFDebugRngListsDwo:
1212
0
        case eSectionTypeDWARFDebugStr:
1213
0
        case eSectionTypeDWARFDebugStrDwo:
1214
0
        case eSectionTypeDWARFDebugStrOffsets:
1215
0
        case eSectionTypeDWARFDebugStrOffsetsDwo:
1216
0
        case eSectionTypeDWARFDebugTuIndex:
1217
0
        case eSectionTypeDWARFDebugTypes:
1218
0
        case eSectionTypeDWARFDebugTypesDwo:
1219
0
        case eSectionTypeDWARFAppleNames:
1220
0
        case eSectionTypeDWARFAppleTypes:
1221
0
        case eSectionTypeDWARFAppleNamespaces:
1222
0
        case eSectionTypeDWARFAppleObjC:
1223
0
        case eSectionTypeDWARFGNUDebugAltLink:
1224
0
          return AddressClass::eDebug;
1225
1226
0
        case eSectionTypeEHFrame:
1227
0
        case eSectionTypeARMexidx:
1228
0
        case eSectionTypeARMextab:
1229
0
        case eSectionTypeCompactUnwind:
1230
0
          return AddressClass::eRuntime;
1231
1232
0
        case eSectionTypeAbsoluteAddress:
1233
0
        case eSectionTypeELFSymbolTable:
1234
0
        case eSectionTypeELFDynamicSymbols:
1235
0
        case eSectionTypeELFRelocationEntries:
1236
0
        case eSectionTypeELFDynamicLinkInfo:
1237
12
        case eSectionTypeOther:
1238
12
          return AddressClass::eUnknown;
1239
139k
        }
1240
139k
      }
1241
139k
    }
1242
1243
0
    const SymbolType symbol_type = symbol->GetType();
1244
0
    switch (symbol_type) {
1245
0
    case eSymbolTypeAny:
1246
0
      return AddressClass::eUnknown;
1247
0
    case eSymbolTypeAbsolute:
1248
0
      return AddressClass::eUnknown;
1249
1250
0
    case eSymbolTypeCode:
1251
0
    case eSymbolTypeTrampoline:
1252
0
    case eSymbolTypeResolver:
1253
0
      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
0
        if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1257
0
          return AddressClass::eCodeAlternateISA;
1258
0
      }
1259
0
      return AddressClass::eCode;
1260
1261
0
    case eSymbolTypeData:
1262
0
      return AddressClass::eData;
1263
0
    case eSymbolTypeRuntime:
1264
0
      return AddressClass::eRuntime;
1265
0
    case eSymbolTypeException:
1266
0
      return AddressClass::eRuntime;
1267
0
    case eSymbolTypeSourceFile:
1268
0
      return AddressClass::eDebug;
1269
0
    case eSymbolTypeHeaderFile:
1270
0
      return AddressClass::eDebug;
1271
0
    case eSymbolTypeObjectFile:
1272
0
      return AddressClass::eDebug;
1273
0
    case eSymbolTypeCommonBlock:
1274
0
      return AddressClass::eDebug;
1275
0
    case eSymbolTypeBlock:
1276
0
      return AddressClass::eDebug;
1277
0
    case eSymbolTypeLocal:
1278
0
      return AddressClass::eData;
1279
0
    case eSymbolTypeParam:
1280
0
      return AddressClass::eData;
1281
0
    case eSymbolTypeVariable:
1282
0
      return AddressClass::eData;
1283
0
    case eSymbolTypeVariableType:
1284
0
      return AddressClass::eDebug;
1285
0
    case eSymbolTypeLineEntry:
1286
0
      return AddressClass::eDebug;
1287
0
    case eSymbolTypeLineHeader:
1288
0
      return AddressClass::eDebug;
1289
0
    case eSymbolTypeScopeBegin:
1290
0
      return AddressClass::eDebug;
1291
0
    case eSymbolTypeScopeEnd:
1292
0
      return AddressClass::eDebug;
1293
0
    case eSymbolTypeAdditional:
1294
0
      return AddressClass::eUnknown;
1295
0
    case eSymbolTypeCompiler:
1296
0
      return AddressClass::eDebug;
1297
0
    case eSymbolTypeInstrumentation:
1298
0
      return AddressClass::eDebug;
1299
0
    case eSymbolTypeUndefined:
1300
0
      return AddressClass::eUnknown;
1301
0
    case eSymbolTypeObjCClass:
1302
0
      return AddressClass::eRuntime;
1303
0
    case eSymbolTypeObjCMetaClass:
1304
0
      return AddressClass::eRuntime;
1305
0
    case eSymbolTypeObjCIVar:
1306
0
      return AddressClass::eRuntime;
1307
0
    case eSymbolTypeReExported:
1308
0
      return AddressClass::eRuntime;
1309
0
    }
1310
0
  }
1311
8
  return AddressClass::eUnknown;
1312
139k
}
1313
1314
131k
bool ObjectFileMachO::IsStripped() {
1315
131k
  if (m_dysymtab.cmd == 0) {
1316
8
    ModuleSP module_sp(GetModule());
1317
8
    if (module_sp) {
1318
8
      lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1319
85
      for (uint32_t i = 0; i < m_header.ncmds; 
++i77
) {
1320
77
        const lldb::offset_t load_cmd_offset = offset;
1321
1322
77
        llvm::MachO::load_command lc;
1323
77
        if (m_data.GetU32(&offset, &lc.cmd, 2) == nullptr)
1324
0
          break;
1325
77
        if (lc.cmd == LC_DYSYMTAB) {
1326
0
          m_dysymtab.cmd = lc.cmd;
1327
0
          m_dysymtab.cmdsize = lc.cmdsize;
1328
0
          if (m_data.GetU32(&offset, &m_dysymtab.ilocalsym,
1329
0
                            (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) ==
1330
0
              nullptr) {
1331
            // Clear m_dysymtab if we were unable to read all items from the
1332
            // load command
1333
0
            ::memset(&m_dysymtab, 0, sizeof(m_dysymtab));
1334
0
          }
1335
0
        }
1336
77
        offset = load_cmd_offset + lc.cmdsize;
1337
77
      }
1338
8
    }
1339
8
  }
1340
131k
  if (m_dysymtab.cmd)
1341
131k
    return m_dysymtab.nlocalsym <= 1;
1342
8
  return false;
1343
131k
}
1344
1345
139k
ObjectFileMachO::EncryptedFileRanges ObjectFileMachO::GetEncryptedFileRanges() {
1346
139k
  EncryptedFileRanges result;
1347
139k
  lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1348
1349
139k
  llvm::MachO::encryption_info_command encryption_cmd;
1350
3.14M
  for (uint32_t i = 0; i < m_header.ncmds; 
++i3.00M
) {
1351
3.00M
    const lldb::offset_t load_cmd_offset = offset;
1352
3.00M
    if (m_data.GetU32(&offset, &encryption_cmd, 2) == nullptr)
1353
0
      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
3.00M
    if (encryption_cmd.cmd == LC_ENCRYPTION_INFO ||
1358
3.00M
        
encryption_cmd.cmd == LC_ENCRYPTION_INFO_643.00M
) {
1359
1
      if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3)) {
1360
1
        if (encryption_cmd.cryptid != 0) {
1361
0
          EncryptedFileRanges::Entry entry;
1362
0
          entry.SetRangeBase(encryption_cmd.cryptoff);
1363
0
          entry.SetByteSize(encryption_cmd.cryptsize);
1364
0
          result.Append(entry);
1365
0
        }
1366
1
      }
1367
1
    }
1368
3.00M
    offset = load_cmd_offset + encryption_cmd.cmdsize;
1369
3.00M
  }
1370
1371
139k
  return result;
1372
139k
}
1373
1374
void ObjectFileMachO::SanitizeSegmentCommand(
1375
420k
    llvm::MachO::segment_command_64 &seg_cmd, uint32_t cmd_idx) {
1376
420k
  if (m_length == 0 || 
seg_cmd.filesize == 0420k
)
1377
7.30k
    return;
1378
1379
412k
  if (IsSharedCacheBinary() && 
!IsInMemory()384k
) {
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
384k
    if (strncmp(seg_cmd.segname, "__TEXT", sizeof(seg_cmd.segname)) == 0)
1385
127k
      m_text_address = seg_cmd.vmaddr;
1386
384k
    if (strncmp(seg_cmd.segname, "__LINKEDIT", sizeof(seg_cmd.segname)) == 0)
1387
127k
      m_linkedit_original_offset = seg_cmd.fileoff;
1388
1389
384k
    seg_cmd.fileoff = seg_cmd.vmaddr - m_text_address;
1390
384k
  }
1391
1392
412k
  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
0
    const char *lc_segment_name =
1400
0
        seg_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
1401
0
    GetModule()->ReportWarning(
1402
0
        "load command %u %s has a fileoff (0x%" PRIx64
1403
0
        ") that extends beyond the end of the file (0x%" PRIx64
1404
0
        "), ignoring this section",
1405
0
        cmd_idx, lc_segment_name, seg_cmd.fileoff, m_length);
1406
1407
0
    seg_cmd.fileoff = 0;
1408
0
    seg_cmd.filesize = 0;
1409
0
  }
1410
1411
412k
  if (seg_cmd.fileoff + seg_cmd.filesize > m_length) {
1412
    // We have a load command that says it extends past the end of the file.
1413
    // This is likely a corrupt file.  We don't have any way to return an error
1414
    // condition here (this method was likely invoked from something like
1415
    // ObjectFile::GetSectionList()), so we just null out the section contents,
1416
    // and dump a message to stdout.  The most common case here is core file
1417
    // debugging with a truncated file.
1418
0
    const char *lc_segment_name =
1419
0
        seg_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
1420
0
    GetModule()->ReportWarning(
1421
0
        "load command %u %s has a fileoff + filesize (0x%" PRIx64
1422
0
        ") that extends beyond the end of the file (0x%" PRIx64
1423
0
        "), the segment will be truncated to match",
1424
0
        cmd_idx, lc_segment_name, seg_cmd.fileoff + seg_cmd.filesize, m_length);
1425
1426
    // Truncate the length
1427
0
    seg_cmd.filesize = m_length - seg_cmd.fileoff;
1428
0
  }
1429
412k
}
1430
1431
static uint32_t
1432
420k
GetSegmentPermissions(const llvm::MachO::segment_command_64 &seg_cmd) {
1433
420k
  uint32_t result = 0;
1434
420k
  if (seg_cmd.initprot & VM_PROT_READ)
1435
416k
    result |= ePermissionsReadable;
1436
420k
  if (seg_cmd.initprot & VM_PROT_WRITE)
1437
144k
    result |= ePermissionsWritable;
1438
420k
  if (seg_cmd.initprot & VM_PROT_EXECUTE)
1439
139k
    result |= ePermissionsExecutable;
1440
420k
  return result;
1441
420k
}
1442
1443
static lldb::SectionType GetSectionType(uint32_t flags,
1444
2.03M
                                        ConstString section_name) {
1445
1446
2.03M
  if (flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS))
1447
382k
    return eSectionTypeCode;
1448
1449
1.64M
  uint32_t mach_sect_type = flags & SECTION_TYPE;
1450
1.64M
  static ConstString g_sect_name_objc_data("__objc_data");
1451
1.64M
  static ConstString g_sect_name_objc_msgrefs("__objc_msgrefs");
1452
1.64M
  static ConstString g_sect_name_objc_selrefs("__objc_selrefs");
1453
1.64M
  static ConstString g_sect_name_objc_classrefs("__objc_classrefs");
1454
1.64M
  static ConstString g_sect_name_objc_superrefs("__objc_superrefs");
1455
1.64M
  static ConstString g_sect_name_objc_const("__objc_const");
1456
1.64M
  static ConstString g_sect_name_objc_classlist("__objc_classlist");
1457
1.64M
  static ConstString g_sect_name_cfstring("__cfstring");
1458
1459
1.64M
  static ConstString g_sect_name_dwarf_debug_abbrev("__debug_abbrev");
1460
1.64M
  static ConstString g_sect_name_dwarf_debug_aranges("__debug_aranges");
1461
1.64M
  static ConstString g_sect_name_dwarf_debug_frame("__debug_frame");
1462
1.64M
  static ConstString g_sect_name_dwarf_debug_info("__debug_info");
1463
1.64M
  static ConstString g_sect_name_dwarf_debug_line("__debug_line");
1464
1.64M
  static ConstString g_sect_name_dwarf_debug_loc("__debug_loc");
1465
1.64M
  static ConstString g_sect_name_dwarf_debug_loclists("__debug_loclists");
1466
1.64M
  static ConstString g_sect_name_dwarf_debug_macinfo("__debug_macinfo");
1467
1.64M
  static ConstString g_sect_name_dwarf_debug_names("__debug_names");
1468
1.64M
  static ConstString g_sect_name_dwarf_debug_pubnames("__debug_pubnames");
1469
1.64M
  static ConstString g_sect_name_dwarf_debug_pubtypes("__debug_pubtypes");
1470
1.64M
  static ConstString g_sect_name_dwarf_debug_ranges("__debug_ranges");
1471
1.64M
  static ConstString g_sect_name_dwarf_debug_str("__debug_str");
1472
1.64M
  static ConstString g_sect_name_dwarf_debug_types("__debug_types");
1473
1.64M
  static ConstString g_sect_name_dwarf_apple_names("__apple_names");
1474
1.64M
  static ConstString g_sect_name_dwarf_apple_types("__apple_types");
1475
1.64M
  static ConstString g_sect_name_dwarf_apple_namespaces("__apple_namespac");
1476
1.64M
  static ConstString g_sect_name_dwarf_apple_objc("__apple_objc");
1477
1.64M
  static ConstString g_sect_name_eh_frame("__eh_frame");
1478
1.64M
  static ConstString g_sect_name_compact_unwind("__unwind_info");
1479
1.64M
  static ConstString g_sect_name_text("__text");
1480
1.64M
  static ConstString g_sect_name_data("__data");
1481
1.64M
  static ConstString g_sect_name_go_symtab("__gosymtab");
1482
1483
1.64M
  if (section_name == g_sect_name_dwarf_debug_abbrev)
1484
6.33k
    return eSectionTypeDWARFDebugAbbrev;
1485
1.64M
  if (section_name == g_sect_name_dwarf_debug_aranges)
1486
1.27k
    return eSectionTypeDWARFDebugAranges;
1487
1.64M
  if (section_name == g_sect_name_dwarf_debug_frame)
1488
1
    return eSectionTypeDWARFDebugFrame;
1489
1.64M
  if (section_name == g_sect_name_dwarf_debug_info)
1490
6.33k
    return eSectionTypeDWARFDebugInfo;
1491
1.63M
  if (section_name == g_sect_name_dwarf_debug_line)
1492
6.33k
    return eSectionTypeDWARFDebugLine;
1493
1.62M
  if (section_name == g_sect_name_dwarf_debug_loc)
1494
220
    return eSectionTypeDWARFDebugLoc;
1495
1.62M
  if (section_name == g_sect_name_dwarf_debug_loclists)
1496
0
    return eSectionTypeDWARFDebugLocLists;
1497
1.62M
  if (section_name == g_sect_name_dwarf_debug_macinfo)
1498
325
    return eSectionTypeDWARFDebugMacInfo;
1499
1.62M
  if (section_name == g_sect_name_dwarf_debug_names)
1500
0
    return eSectionTypeDWARFDebugNames;
1501
1.62M
  if (section_name == g_sect_name_dwarf_debug_pubnames)
1502
3
    return eSectionTypeDWARFDebugPubNames;
1503
1.62M
  if (section_name == g_sect_name_dwarf_debug_pubtypes)
1504
3
    return eSectionTypeDWARFDebugPubTypes;
1505
1.62M
  if (section_name == g_sect_name_dwarf_debug_ranges)
1506
581
    return eSectionTypeDWARFDebugRanges;
1507
1.62M
  if (section_name == g_sect_name_dwarf_debug_str)
1508
6.33k
    return eSectionTypeDWARFDebugStr;
1509
1.62M
  if (section_name == g_sect_name_dwarf_debug_types)
1510
0
    return eSectionTypeDWARFDebugTypes;
1511
1.62M
  if (section_name == g_sect_name_dwarf_apple_names)
1512
6.31k
    return eSectionTypeDWARFAppleNames;
1513
1.61M
  if (section_name == g_sect_name_dwarf_apple_types)
1514
6.31k
    return eSectionTypeDWARFAppleTypes;
1515
1.60M
  if (section_name == g_sect_name_dwarf_apple_namespaces)
1516
6.31k
    return eSectionTypeDWARFAppleNamespaces;
1517
1.60M
  if (section_name == g_sect_name_dwarf_apple_objc)
1518
6.31k
    return eSectionTypeDWARFAppleObjC;
1519
1.59M
  if (section_name == g_sect_name_objc_selrefs)
1520
19.5k
    return eSectionTypeDataCStringPointers;
1521
1.57M
  if (section_name == g_sect_name_objc_msgrefs)
1522
0
    return eSectionTypeDataObjCMessageRefs;
1523
1.57M
  if (section_name == g_sect_name_eh_frame)
1524
76.9k
    return eSectionTypeEHFrame;
1525
1.49M
  if (section_name == g_sect_name_compact_unwind)
1526
129k
    return eSectionTypeCompactUnwind;
1527
1.36M
  if (section_name == g_sect_name_cfstring)
1528
14.3k
    return eSectionTypeDataObjCCFStrings;
1529
1.35M
  if (section_name == g_sect_name_go_symtab)
1530
0
    return eSectionTypeGoSymtab;
1531
1.35M
  if (section_name == g_sect_name_objc_data ||
1532
1.35M
      
section_name == g_sect_name_objc_classrefs1.33M
||
1533
1.35M
      
section_name == g_sect_name_objc_superrefs1.32M
||
1534
1.35M
      
section_name == g_sect_name_objc_const1.30M
||
1535
1.35M
      
section_name == g_sect_name_objc_classlist1.28M
) {
1536
82.4k
    return eSectionTypeDataPointers;
1537
82.4k
  }
1538
1539
1.27M
  switch (mach_sect_type) {
1540
  // TODO: categorize sections by other flags for regular sections
1541
499k
  case S_REGULAR:
1542
499k
    if (section_name == g_sect_name_text)
1543
0
      return eSectionTypeCode;
1544
499k
    if (section_name == g_sect_name_data)
1545
90.0k
      return eSectionTypeData;
1546
409k
    return eSectionTypeOther;
1547
152k
  case S_ZEROFILL:
1548
152k
    return eSectionTypeZeroFill;
1549
217k
  case S_CSTRING_LITERALS: // section with only literal C strings
1550
217k
    return eSectionTypeDataCString;
1551
149
  case S_4BYTE_LITERALS: // section with only 4 byte literals
1552
149
    return eSectionTypeData4;
1553
160
  case S_8BYTE_LITERALS: // section with only 8 byte literals
1554
160
    return eSectionTypeData8;
1555
0
  case S_LITERAL_POINTERS: // section with only pointers to literals
1556
0
    return eSectionTypeDataPointers;
1557
244k
  case S_NON_LAZY_SYMBOL_POINTERS: // section with only non-lazy symbol pointers
1558
244k
    return eSectionTypeDataPointers;
1559
120k
  case S_LAZY_SYMBOL_POINTERS: // section with only lazy symbol pointers
1560
120k
    return eSectionTypeDataPointers;
1561
0
  case S_SYMBOL_STUBS: // section with only symbol stubs, byte size of stub in
1562
                       // the reserved2 field
1563
0
    return eSectionTypeCode;
1564
6.40k
  case S_MOD_INIT_FUNC_POINTERS: // section with only function pointers for
1565
                                 // initialization
1566
6.40k
    return eSectionTypeDataPointers;
1567
0
  case S_MOD_TERM_FUNC_POINTERS: // section with only function pointers for
1568
                                 // termination
1569
0
    return eSectionTypeDataPointers;
1570
0
  case S_COALESCED:
1571
0
    return eSectionTypeOther;
1572
0
  case S_GB_ZEROFILL:
1573
0
    return eSectionTypeZeroFill;
1574
0
  case S_INTERPOSING: // section with only pairs of function pointers for
1575
                      // interposing
1576
0
    return eSectionTypeCode;
1577
221
  case S_16BYTE_LITERALS: // section with only 16 byte literals
1578
221
    return eSectionTypeData16;
1579
22.5k
  case S_DTRACE_DOF:
1580
22.5k
    return eSectionTypeDebug;
1581
0
  case S_LAZY_DYLIB_SYMBOL_POINTERS:
1582
0
    return eSectionTypeDataPointers;
1583
8.27k
  default:
1584
8.27k
    return eSectionTypeOther;
1585
1.27M
  }
1586
1.27M
}
1587
1588
struct ObjectFileMachO::SegmentParsingContext {
1589
  const EncryptedFileRanges EncryptedRanges;
1590
  lldb_private::SectionList &UnifiedList;
1591
  uint32_t NextSegmentIdx = 0;
1592
  uint32_t NextSectionIdx = 0;
1593
  bool FileAddressesChanged = false;
1594
1595
  SegmentParsingContext(EncryptedFileRanges EncryptedRanges,
1596
                        lldb_private::SectionList &UnifiedList)
1597
139k
      : EncryptedRanges(std::move(EncryptedRanges)), UnifiedList(UnifiedList) {}
1598
};
1599
1600
void ObjectFileMachO::ProcessSegmentCommand(
1601
    const llvm::MachO::load_command &load_cmd_, lldb::offset_t offset,
1602
420k
    uint32_t cmd_idx, SegmentParsingContext &context) {
1603
420k
  llvm::MachO::segment_command_64 load_cmd;
1604
420k
  memcpy(&load_cmd, &load_cmd_, sizeof(load_cmd_));
1605
1606
420k
  if (!m_data.GetU8(&offset, (uint8_t *)load_cmd.segname, 16))
1607
0
    return;
1608
1609
420k
  ModuleSP module_sp = GetModule();
1610
420k
  const bool is_core = GetType() == eTypeCoreFile;
1611
420k
  const bool is_dsym = (m_header.filetype == MH_DSYM);
1612
420k
  bool add_section = true;
1613
420k
  bool add_to_unified = true;
1614
420k
  ConstString const_segname(
1615
420k
      load_cmd.segname, strnlen(load_cmd.segname, sizeof(load_cmd.segname)));
1616
1617
420k
  SectionSP unified_section_sp(
1618
420k
      context.UnifiedList.FindSectionByName(const_segname));
1619
420k
  if (is_dsym && 
unified_section_sp7.01k
) {
1620
5.71k
    if (const_segname == GetSegmentNameLINKEDIT()) {
1621
      // We need to keep the __LINKEDIT segment private to this object file
1622
      // only
1623
1.28k
      add_to_unified = false;
1624
4.43k
    } else {
1625
      // This is the dSYM file and this section has already been created by the
1626
      // object file, no need to create it.
1627
4.43k
      add_section = false;
1628
4.43k
    }
1629
5.71k
  }
1630
420k
  load_cmd.vmaddr = m_data.GetAddress(&offset);
1631
420k
  load_cmd.vmsize = m_data.GetAddress(&offset);
1632
420k
  load_cmd.fileoff = m_data.GetAddress(&offset);
1633
420k
  load_cmd.filesize = m_data.GetAddress(&offset);
1634
420k
  if (!m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1635
0
    return;
1636
1637
420k
  SanitizeSegmentCommand(load_cmd, cmd_idx);
1638
1639
420k
  const uint32_t segment_permissions = GetSegmentPermissions(load_cmd);
1640
420k
  const bool segment_is_encrypted =
1641
420k
      (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0;
1642
1643
  // Keep a list of mach segments around in case we need to get at data that
1644
  // isn't stored in the abstracted Sections.
1645
420k
  m_mach_segments.push_back(load_cmd);
1646
1647
  // Use a segment ID of the segment index shifted left by 8 so they never
1648
  // conflict with any of the sections.
1649
420k
  SectionSP segment_sp;
1650
420k
  if (add_section && 
(415k
const_segname415k
||
is_core5.12k
)) {
1651
410k
    segment_sp = std::make_shared<Section>(
1652
410k
        module_sp, // Module to which this section belongs
1653
410k
        this,      // Object file to which this sections belongs
1654
410k
        ++context.NextSegmentIdx
1655
410k
            << 8, // Section ID is the 1 based segment index
1656
        // shifted right by 8 bits as not to collide with any of the 256
1657
        // section IDs that are possible
1658
410k
        const_segname,         // Name of this section
1659
410k
        eSectionTypeContainer, // This section is a container of other
1660
        // sections.
1661
410k
        load_cmd.vmaddr, // File VM address == addresses as they are
1662
        // found in the object file
1663
410k
        load_cmd.vmsize,  // VM size in bytes of this section
1664
410k
        load_cmd.fileoff, // Offset to the data for this section in
1665
        // the file
1666
410k
        load_cmd.filesize, // Size in bytes of this section as found
1667
        // in the file
1668
410k
        0,               // Segments have no alignment information
1669
410k
        load_cmd.flags); // Flags for this section
1670
1671
410k
    segment_sp->SetIsEncrypted(segment_is_encrypted);
1672
410k
    m_sections_up->AddSection(segment_sp);
1673
410k
    segment_sp->SetPermissions(segment_permissions);
1674
410k
    if (add_to_unified)
1675
409k
      context.UnifiedList.AddSection(segment_sp);
1676
410k
  } else 
if (9.49k
unified_section_sp9.49k
) {
1677
    // If this is a dSYM and the file addresses in the dSYM differ from the
1678
    // file addresses in the ObjectFile, we must use the file base address for
1679
    // the Section from the dSYM for the DWARF to resolve correctly.
1680
    // This only happens with binaries in the shared cache in practice;
1681
    // normally a mismatch like this would give a binary & dSYM that do not
1682
    // match UUIDs. When a binary is included in the shared cache, its
1683
    // segments are rearranged to optimize the shared cache, so its file
1684
    // addresses will differ from what the ObjectFile had originally,
1685
    // and what the dSYM has.
1686
4.43k
    if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr) {
1687
0
      Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS));
1688
0
      if (log) {
1689
0
        log->Printf(
1690
0
            "Installing dSYM's %s segment file address over ObjectFile's "
1691
0
            "so symbol table/debug info resolves correctly for %s",
1692
0
            const_segname.AsCString(),
1693
0
            module_sp->GetFileSpec().GetFilename().AsCString());
1694
0
      }
1695
1696
      // Make sure we've parsed the symbol table from the ObjectFile before
1697
      // we go around changing its Sections.
1698
0
      module_sp->GetObjectFile()->GetSymtab();
1699
      // eh_frame would present the same problems but we parse that on a per-
1700
      // function basis as-needed so it's more difficult to remove its use of
1701
      // the Sections.  Realistically, the environments where this code path
1702
      // will be taken will not have eh_frame sections.
1703
1704
0
      unified_section_sp->SetFileAddress(load_cmd.vmaddr);
1705
1706
      // Notify the module that the section addresses have been changed once
1707
      // we're done so any file-address caches can be updated.
1708
0
      context.FileAddressesChanged = true;
1709
0
    }
1710
4.43k
    m_sections_up->AddSection(unified_section_sp);
1711
4.43k
  }
1712
1713
420k
  llvm::MachO::section_64 sect64;
1714
420k
  ::memset(&sect64, 0, sizeof(sect64));
1715
  // Push a section into our mach sections for the section at index zero
1716
  // (NO_SECT) if we don't have any mach sections yet...
1717
420k
  if (m_mach_sections.empty())
1718
139k
    m_mach_sections.push_back(sect64);
1719
420k
  uint32_t segment_sect_idx;
1720
420k
  const lldb::user_id_t first_segment_sectID = context.NextSectionIdx + 1;
1721
1722
420k
  const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 
75
:
8420k
;
1723
2.46M
  for (segment_sect_idx = 0; segment_sect_idx < load_cmd.nsects;
1724
2.04M
       ++segment_sect_idx) {
1725
2.04M
    if (m_data.GetU8(&offset, (uint8_t *)sect64.sectname,
1726
2.04M
                     sizeof(sect64.sectname)) == nullptr)
1727
0
      break;
1728
2.04M
    if (m_data.GetU8(&offset, (uint8_t *)sect64.segname,
1729
2.04M
                     sizeof(sect64.segname)) == nullptr)
1730
0
      break;
1731
2.04M
    sect64.addr = m_data.GetAddress(&offset);
1732
2.04M
    sect64.size = m_data.GetAddress(&offset);
1733
1734
2.04M
    if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == nullptr)
1735
0
      break;
1736
1737
2.04M
    if (IsSharedCacheBinary() && 
!IsInMemory()1.89M
) {
1738
1.89M
      sect64.offset = sect64.addr - m_text_address;
1739
1.89M
    }
1740
1741
    // Keep a list of mach sections around in case we need to get at data that
1742
    // isn't stored in the abstracted Sections.
1743
2.04M
    m_mach_sections.push_back(sect64);
1744
1745
2.04M
    if (add_section) {
1746
2.03M
      ConstString section_name(
1747
2.03M
          sect64.sectname, strnlen(sect64.sectname, sizeof(sect64.sectname)));
1748
2.03M
      if (!const_segname) {
1749
        // We have a segment with no name so we need to conjure up segments
1750
        // that correspond to the section's segname if there isn't already such
1751
        // a section. If there is such a section, we resize the section so that
1752
        // it spans all sections.  We also mark these sections as fake so
1753
        // address matches don't hit if they land in the gaps between the child
1754
        // sections.
1755
60.9k
        const_segname.SetTrimmedCStringWithLength(sect64.segname,
1756
60.9k
                                                  sizeof(sect64.segname));
1757
60.9k
        segment_sp = context.UnifiedList.FindSectionByName(const_segname);
1758
60.9k
        if (segment_sp.get()) {
1759
42.4k
          Section *segment = segment_sp.get();
1760
          // Grow the section size as needed.
1761
42.4k
          const lldb::addr_t sect64_min_addr = sect64.addr;
1762
42.4k
          const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1763
42.4k
          const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1764
42.4k
          const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1765
42.4k
          const lldb::addr_t curr_seg_max_addr =
1766
42.4k
              curr_seg_min_addr + curr_seg_byte_size;
1767
42.4k
          if (sect64_min_addr >= curr_seg_min_addr) {
1768
42.3k
            const lldb::addr_t new_seg_byte_size =
1769
42.3k
                sect64_max_addr - curr_seg_min_addr;
1770
            // Only grow the section size if needed
1771
42.3k
            if (new_seg_byte_size > curr_seg_byte_size)
1772
42.2k
              segment->SetByteSize(new_seg_byte_size);
1773
42.3k
          } else {
1774
            // We need to change the base address of the segment and adjust the
1775
            // child section offsets for all existing children.
1776
101
            const lldb::addr_t slide_amount =
1777
101
                sect64_min_addr - curr_seg_min_addr;
1778
101
            segment->Slide(slide_amount, false);
1779
101
            segment->GetChildren().Slide(-slide_amount, false);
1780
101
            segment->SetByteSize(curr_seg_max_addr - sect64_min_addr);
1781
101
          }
1782
1783
          // Grow the section size as needed.
1784
42.4k
          if (sect64.offset) {
1785
42.3k
            const lldb::addr_t segment_min_file_offset =
1786
42.3k
                segment->GetFileOffset();
1787
42.3k
            const lldb::addr_t segment_max_file_offset =
1788
42.3k
                segment_min_file_offset + segment->GetFileSize();
1789
1790
42.3k
            const lldb::addr_t section_min_file_offset = sect64.offset;
1791
42.3k
            const lldb::addr_t section_max_file_offset =
1792
42.3k
                section_min_file_offset + sect64.size;
1793
42.3k
            const lldb::addr_t new_file_offset =
1794
42.3k
                std::min(section_min_file_offset, segment_min_file_offset);
1795
42.3k
            const lldb::addr_t new_file_size =
1796
42.3k
                std::max(section_max_file_offset, segment_max_file_offset) -
1797
42.3k
                new_file_offset;
1798
42.3k
            segment->SetFileOffset(new_file_offset);
1799
42.3k
            segment->SetFileSize(new_file_size);
1800
42.3k
          }
1801
42.4k
        } else {
1802
          // Create a fake section for the section's named segment
1803
18.5k
          segment_sp = std::make_shared<Section>(
1804
18.5k
              segment_sp, // Parent section
1805
18.5k
              module_sp,  // Module to which this section belongs
1806
18.5k
              this,       // Object file to which this section belongs
1807
18.5k
              ++context.NextSegmentIdx
1808
18.5k
                  << 8, // Section ID is the 1 based segment index
1809
              // shifted right by 8 bits as not to
1810
              // collide with any of the 256 section IDs
1811
              // that are possible
1812
18.5k
              const_segname,         // Name of this section
1813
18.5k
              eSectionTypeContainer, // This section is a container of
1814
              // other sections.
1815
18.5k
              sect64.addr, // File VM address == addresses as they are
1816
              // found in the object file
1817
18.5k
              sect64.size,   // VM size in bytes of this section
1818
18.5k
              sect64.offset, // Offset to the data for this section in
1819
              // the file
1820
18.5k
              sect64.offset ? 
sect64.size18.1k
:
0335
, // Size in bytes of
1821
              // this section as
1822
              // found in the file
1823
18.5k
              sect64.align,
1824
18.5k
              load_cmd.flags); // Flags for this section
1825
18.5k
          segment_sp->SetIsFake(true);
1826
18.5k
          segment_sp->SetPermissions(segment_permissions);
1827
18.5k
          m_sections_up->AddSection(segment_sp);
1828
18.5k
          if (add_to_unified)
1829
18.5k
            context.UnifiedList.AddSection(segment_sp);
1830
18.5k
          segment_sp->SetIsEncrypted(segment_is_encrypted);
1831
18.5k
        }
1832
60.9k
      }
1833
2.03M
      assert(segment_sp.get());
1834
1835
0
      lldb::SectionType sect_type = GetSectionType(sect64.flags, section_name);
1836
1837
2.03M
      SectionSP section_sp(new Section(
1838
2.03M
          segment_sp, module_sp, this, ++context.NextSectionIdx, section_name,
1839
2.03M
          sect_type, sect64.addr - segment_sp->GetFileAddress(), sect64.size,
1840
2.03M
          sect64.offset, sect64.offset == 0 ? 
06.42k
:
sect64.size2.02M
, sect64.align,
1841
2.03M
          sect64.flags));
1842
      // Set the section to be encrypted to match the segment
1843
1844
2.03M
      bool section_is_encrypted = false;
1845
2.03M
      if (!segment_is_encrypted && load_cmd.filesize != 0)
1846
2.03M
        section_is_encrypted = context.EncryptedRanges.FindEntryThatContains(
1847
2.03M
                                   sect64.offset) != nullptr;
1848
1849
2.03M
      section_sp->SetIsEncrypted(segment_is_encrypted || section_is_encrypted);
1850
2.03M
      section_sp->SetPermissions(segment_permissions);
1851
2.03M
      segment_sp->GetChildren().AddSection(section_sp);
1852
1853
2.03M
      if (segment_sp->IsFake()) {
1854
60.9k
        segment_sp.reset();
1855
60.9k
        const_segname.Clear();
1856
60.9k
      }
1857
2.03M
    }
1858
2.04M
  }
1859
420k
  if (segment_sp && 
is_dsym410k
) {
1860
2.57k
    if (first_segment_sectID <= context.NextSectionIdx) {
1861
1.28k
      lldb::user_id_t sect_uid;
1862
13.1k
      for (sect_uid = first_segment_sectID; sect_uid <= context.NextSectionIdx;
1863
11.8k
           ++sect_uid) {
1864
11.8k
        SectionSP curr_section_sp(
1865
11.8k
            segment_sp->GetChildren().FindSectionByID(sect_uid));
1866
11.8k
        SectionSP next_section_sp;
1867
11.8k
        if (sect_uid + 1 <= context.NextSectionIdx)
1868
10.5k
          next_section_sp =
1869
10.5k
              segment_sp->GetChildren().FindSectionByID(sect_uid + 1);
1870
1871
11.8k
        if (curr_section_sp.get()) {
1872
11.8k
          if (curr_section_sp->GetByteSize() == 0) {
1873
0
            if (next_section_sp.get() != nullptr)
1874
0
              curr_section_sp->SetByteSize(next_section_sp->GetFileAddress() -
1875
0
                                           curr_section_sp->GetFileAddress());
1876
0
            else
1877
0
              curr_section_sp->SetByteSize(load_cmd.vmsize);
1878
0
          }
1879
11.8k
        }
1880
11.8k
      }
1881
1.28k
    }
1882
2.57k
  }
1883
420k
}
1884
1885
void ObjectFileMachO::ProcessDysymtabCommand(
1886
138k
    const llvm::MachO::load_command &load_cmd, lldb::offset_t offset) {
1887
138k
  m_dysymtab.cmd = load_cmd.cmd;
1888
138k
  m_dysymtab.cmdsize = load_cmd.cmdsize;
1889
138k
  m_data.GetU32(&offset, &m_dysymtab.ilocalsym,
1890
138k
                (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1891
138k
}
1892
1893
139k
void ObjectFileMachO::CreateSections(SectionList &unified_section_list) {
1894
139k
  if (m_sections_up)
1895
0
    return;
1896
1897
139k
  m_sections_up = std::make_unique<SectionList>();
1898
1899
139k
  lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1900
  // bool dump_sections = false;
1901
139k
  ModuleSP module_sp(GetModule());
1902
1903
139k
  offset = MachHeaderSizeFromMagic(m_header.magic);
1904
1905
139k
  SegmentParsingContext context(GetEncryptedFileRanges(), unified_section_list);
1906
139k
  llvm::MachO::load_command load_cmd;
1907
3.14M
  for (uint32_t i = 0; i < m_header.ncmds; 
++i3.00M
) {
1908
3.00M
    const lldb::offset_t load_cmd_offset = offset;
1909
3.00M
    if (m_data.GetU32(&offset, &load_cmd, 2) == nullptr)
1910
0
      break;
1911
1912
3.00M
    if (load_cmd.cmd == LC_SEGMENT || 
load_cmd.cmd == LC_SEGMENT_643.00M
)
1913
420k
      ProcessSegmentCommand(load_cmd, offset, i, context);
1914
2.58M
    else if (load_cmd.cmd == LC_DYSYMTAB)
1915
138k
      ProcessDysymtabCommand(load_cmd, offset);
1916
1917
3.00M
    offset = load_cmd_offset + load_cmd.cmdsize;
1918
3.00M
  }
1919
1920
139k
  if (context.FileAddressesChanged && 
module_sp0
)
1921
0
    module_sp->SectionFileAddressesChanged();
1922
139k
}
1923
1924
class MachSymtabSectionInfo {
1925
public:
1926
  MachSymtabSectionInfo(SectionList *section_list)
1927
135k
      : m_section_list(section_list), m_section_infos() {
1928
    // Get the number of sections down to a depth of 1 to include all segments
1929
    // and their sections, but no other sections that may be added for debug
1930
    // map or
1931
135k
    m_section_infos.resize(section_list->GetNumSections(1));
1932
135k
  }
1933
1934
141M
  SectionSP GetSection(uint8_t n_sect, addr_t file_addr) {
1935
141M
    if (n_sect == 0)
1936
0
      return SectionSP();
1937
141M
    if (n_sect < m_section_infos.size()) {
1938
141M
      if (!m_section_infos[n_sect].section_sp) {
1939
775k
        SectionSP section_sp(m_section_list->FindSectionByID(n_sect));
1940
775k
        m_section_infos[n_sect].section_sp = section_sp;
1941
775k
        if (section_sp) {
1942
775k
          m_section_infos[n_sect].vm_range.SetBaseAddress(
1943
775k
              section_sp->GetFileAddress());
1944
775k
          m_section_infos[n_sect].vm_range.SetByteSize(
1945
775k
              section_sp->GetByteSize());
1946
775k
        } else {
1947
0
          std::string filename = "<unknown>";
1948
0
          SectionSP first_section_sp(m_section_list->GetSectionAtIndex(0));
1949
0
          if (first_section_sp)
1950
0
            filename = first_section_sp->GetObjectFile()->GetFileSpec().GetPath();
1951
1952
0
          Host::SystemLog(Host::eSystemLogError,
1953
0
                          "error: unable to find section %d for a symbol in "
1954
0
                          "%s, corrupt file?\n",
1955
0
                          n_sect, filename.c_str());
1956
0
        }
1957
775k
      }
1958
141M
      if (m_section_infos[n_sect].vm_range.Contains(file_addr)) {
1959
        // Symbol is in section.
1960
141M
        return m_section_infos[n_sect].section_sp;
1961
141M
      } else 
if (2.78k
m_section_infos[n_sect].vm_range.GetByteSize() == 02.78k
&&
1962
2.78k
                 m_section_infos[n_sect].vm_range.GetBaseAddress() ==
1963
1
                     file_addr) {
1964
        // Symbol is in section with zero size, but has the same start address
1965
        // as the section. This can happen with linker symbols (symbols that
1966
        // start with the letter 'l' or 'L'.
1967
1
        return m_section_infos[n_sect].section_sp;
1968
1
      }
1969
141M
    }
1970
2.78k
    return m_section_list->FindSectionContainingFileAddress(file_addr);
1971
141M
  }
1972
1973
protected:
1974
  struct SectionInfo {
1975
2.40M
    SectionInfo() : vm_range(), section_sp() {}
1976
1977
    VMRange vm_range;
1978
    SectionSP section_sp;
1979
  };
1980
  SectionList *m_section_list;
1981
  std::vector<SectionInfo> m_section_infos;
1982
};
1983
1984
18
#define TRIE_SYMBOL_IS_THUMB (1ULL << 63)
1985
struct TrieEntry {
1986
0
  void Dump() const {
1987
0
    printf("0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"",
1988
0
           static_cast<unsigned long long>(address),
1989
0
           static_cast<unsigned long long>(flags),
1990
0
           static_cast<unsigned long long>(other), name.GetCString());
1991
0
    if (import_name)
1992
0
      printf(" -> \"%s\"\n", import_name.GetCString());
1993
0
    else
1994
0
      printf("\n");
1995
0
  }
1996
  ConstString name;
1997
  uint64_t address = LLDB_INVALID_ADDRESS;
1998
  uint64_t flags =
1999
      0; // EXPORT_SYMBOL_FLAGS_REEXPORT, EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER,
2000
         // TRIE_SYMBOL_IS_THUMB
2001
  uint64_t other = 0;
2002
  ConstString import_name;
2003
};
2004
2005
struct TrieEntryWithOffset {
2006
  lldb::offset_t nodeOffset;
2007
  TrieEntry entry;
2008
2009
42.9M
  TrieEntryWithOffset(lldb::offset_t offset) : nodeOffset(offset), entry() {}
2010
2011
0
  void Dump(uint32_t idx) const {
2012
0
    printf("[%3u] 0x%16.16llx: ", idx,
2013
0
           static_cast<unsigned long long>(nodeOffset));
2014
0
    entry.Dump();
2015
0
  }
2016
2017
0
  bool operator<(const TrieEntryWithOffset &other) const {
2018
0
    return (nodeOffset < other.nodeOffset);
2019
0
  }
2020
};
2021
2022
static bool ParseTrieEntries(DataExtractor &data, lldb::offset_t offset,
2023
                             const bool is_arm, addr_t text_seg_base_addr,
2024
                             std::vector<llvm::StringRef> &nameSlices,
2025
                             std::set<lldb::addr_t> &resolver_addresses,
2026
                             std::vector<TrieEntryWithOffset> &reexports,
2027
60.9M
                             std::vector<TrieEntryWithOffset> &ext_symbols) {
2028
60.9M
  if (!data.ValidOffset(offset))
2029
0
    return true;
2030
2031
  // Terminal node -- end of a branch, possibly add this to
2032
  // the symbol table or resolver table.
2033
60.9M
  const uint64_t terminalSize = data.GetULEB128(&offset);
2034
60.9M
  lldb::offset_t children_offset = offset + terminalSize;
2035
60.9M
  if (terminalSize != 0) {
2036
42.9M
    TrieEntryWithOffset e(offset);
2037
42.9M
    e.entry.flags = data.GetULEB128(&offset);
2038
42.9M
    const char *import_name = nullptr;
2039
42.9M
    if (e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT) {
2040
1.32M
      e.entry.address = 0;
2041
1.32M
      e.entry.other = data.GetULEB128(&offset); // dylib ordinal
2042
1.32M
      import_name = data.GetCStr(&offset);
2043
41.6M
    } else {
2044
41.6M
      e.entry.address = data.GetULEB128(&offset);
2045
41.6M
      if (text_seg_base_addr != LLDB_INVALID_ADDRESS)
2046
41.6M
        e.entry.address += text_seg_base_addr;
2047
41.6M
      if (e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) {
2048
311k
        e.entry.other = data.GetULEB128(&offset);
2049
311k
        uint64_t resolver_addr = e.entry.other;
2050
311k
        if (text_seg_base_addr != LLDB_INVALID_ADDRESS)
2051
311k
          resolver_addr += text_seg_base_addr;
2052
311k
        if (is_arm)
2053
0
          resolver_addr &= THUMB_ADDRESS_BIT_MASK;
2054
311k
        resolver_addresses.insert(resolver_addr);
2055
311k
      } else
2056
41.3M
        e.entry.other = 0;
2057
41.6M
    }
2058
42.9M
    bool add_this_entry = false;
2059
42.9M
    if (Flags(e.entry.flags).Test(EXPORT_SYMBOL_FLAGS_REEXPORT) &&
2060
42.9M
        
import_name1.32M
&&
import_name[0]1.32M
) {
2061
      // add symbols that are reexport symbols with a valid import name.
2062
118k
      add_this_entry = true;
2063
42.8M
    } else if (e.entry.flags == 0 &&
2064
42.8M
               
(41.2M
import_name == nullptr41.2M
||
import_name[0] == '\0'0
)) {
2065
      // add externally visible symbols, in case the nlist record has
2066
      // been stripped/omitted.
2067
41.2M
      add_this_entry = true;
2068
41.2M
    }
2069
42.9M
    if (add_this_entry) {
2070
41.3M
      std::string name;
2071
41.3M
      if (!nameSlices.empty()) {
2072
41.3M
        for (auto name_slice : nameSlices)
2073
297M
          name.append(name_slice.data(), name_slice.size());
2074
41.3M
      }
2075
41.3M
      if (name.size() > 1) {
2076
        // Skip the leading '_'
2077
41.3M
        e.entry.name.SetCStringWithLength(name.c_str() + 1, name.size() - 1);
2078
41.3M
      }
2079
41.3M
      if (import_name) {
2080
        // Skip the leading '_'
2081
118k
        e.entry.import_name.SetCString(import_name + 1);
2082
118k
      }
2083
41.3M
      if (Flags(e.entry.flags).Test(EXPORT_SYMBOL_FLAGS_REEXPORT)) {
2084
118k
        reexports.push_back(e);
2085
41.2M
      } else {
2086
41.2M
        if (is_arm && 
(e.entry.address & 1)0
) {
2087
0
          e.entry.flags |= TRIE_SYMBOL_IS_THUMB;
2088
0
          e.entry.address &= THUMB_ADDRESS_BIT_MASK;
2089
0
        }
2090
41.2M
        ext_symbols.push_back(e);
2091
41.2M
      }
2092
41.3M
    }
2093
42.9M
  }
2094
2095
60.9M
  const uint8_t childrenCount = data.GetU8(&children_offset);
2096
121M
  for (uint8_t i = 0; i < childrenCount; 
++i60.8M
) {
2097
60.8M
    const char *cstr = data.GetCStr(&children_offset);
2098
60.8M
    if (cstr)
2099
60.8M
      nameSlices.push_back(llvm::StringRef(cstr));
2100
0
    else
2101
0
      return false; // Corrupt data
2102
60.8M
    lldb::offset_t childNodeOffset = data.GetULEB128(&children_offset);
2103
60.8M
    if (childNodeOffset) {
2104
60.8M
      if (!ParseTrieEntries(data, childNodeOffset, is_arm, text_seg_base_addr,
2105
60.8M
                            nameSlices, resolver_addresses, reexports,
2106
60.8M
                            ext_symbols)) {
2107
0
        return false;
2108
0
      }
2109
60.8M
    }
2110
60.8M
    nameSlices.pop_back();
2111
60.8M
  }
2112
60.9M
  return true;
2113
60.9M
}
2114
2115
static SymbolType GetSymbolType(const char *&symbol_name,
2116
                                bool &demangled_is_synthesized,
2117
                                const SectionSP &text_section_sp,
2118
                                const SectionSP &data_section_sp,
2119
                                const SectionSP &data_dirty_section_sp,
2120
                                const SectionSP &data_const_section_sp,
2121
18
                                const SectionSP &symbol_section) {
2122
18
  SymbolType type = eSymbolTypeInvalid;
2123
2124
18
  const char *symbol_sect_name = symbol_section->GetName().AsCString();
2125
18
  if (symbol_section->IsDescendant(text_section_sp.get())) {
2126
4
    if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
2127
4
                                S_ATTR_SELF_MODIFYING_CODE |
2128
4
                                S_ATTR_SOME_INSTRUCTIONS))
2129
0
      type = eSymbolTypeData;
2130
4
    else
2131
4
      type = eSymbolTypeCode;
2132
14
  } else if (symbol_section->IsDescendant(data_section_sp.get()) ||
2133
14
             
symbol_section->IsDescendant(data_dirty_section_sp.get())0
||
2134
14
             
symbol_section->IsDescendant(data_const_section_sp.get())0
) {
2135
14
    if (symbol_sect_name &&
2136
14
        ::strstr(symbol_sect_name, "__objc") == symbol_sect_name) {
2137
12
      type = eSymbolTypeRuntime;
2138
2139
12
      if (symbol_name) {
2140
12
        llvm::StringRef symbol_name_ref(symbol_name);
2141
12
        if (symbol_name_ref.startswith("OBJC_")) {
2142
12
          static const llvm::StringRef g_objc_v2_prefix_class("OBJC_CLASS_$_");
2143
12
          static const llvm::StringRef g_objc_v2_prefix_metaclass(
2144
12
              "OBJC_METACLASS_$_");
2145
12
          static const llvm::StringRef g_objc_v2_prefix_ivar("OBJC_IVAR_$_");
2146
12
          if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) {
2147
4
            symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2148
4
            type = eSymbolTypeObjCClass;
2149
4
            demangled_is_synthesized = true;
2150
8
          } else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass)) {
2151
4
            symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2152
4
            type = eSymbolTypeObjCMetaClass;
2153
4
            demangled_is_synthesized = true;
2154
4
          } else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar)) {
2155
4
            symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2156
4
            type = eSymbolTypeObjCIVar;
2157
4
            demangled_is_synthesized = true;
2158
4
          }
2159
12
        }
2160
12
      }
2161
12
    } else 
if (2
symbol_sect_name2
&&
2162
2
               ::strstr(symbol_sect_name, "__gcc_except_tab") ==
2163
2
                   symbol_sect_name) {
2164
0
      type = eSymbolTypeException;
2165
2
    } else {
2166
2
      type = eSymbolTypeData;
2167
2
    }
2168
14
  } else 
if (0
symbol_sect_name0
&&
2169
0
             ::strstr(symbol_sect_name, "__IMPORT") == symbol_sect_name) {
2170
0
    type = eSymbolTypeTrampoline;
2171
0
  }
2172
18
  return type;
2173
18
}
2174
2175
// Read the UUID out of a dyld_shared_cache file on-disk.
2176
UUID ObjectFileMachO::GetSharedCacheUUID(FileSpec dyld_shared_cache,
2177
                                         const ByteOrder byte_order,
2178
0
                                         const uint32_t addr_byte_size) {
2179
0
  UUID dsc_uuid;
2180
0
  DataBufferSP DscData = MapFileData(
2181
0
      dyld_shared_cache, sizeof(struct lldb_copy_dyld_cache_header_v1), 0);
2182
0
  if (!DscData)
2183
0
    return dsc_uuid;
2184
0
  DataExtractor dsc_header_data(DscData, byte_order, addr_byte_size);
2185
2186
0
  char version_str[7];
2187
0
  lldb::offset_t offset = 0;
2188
0
  memcpy(version_str, dsc_header_data.GetData(&offset, 6), 6);
2189
0
  version_str[6] = '\0';
2190
0
  if (strcmp(version_str, "dyld_v") == 0) {
2191
0
    offset = offsetof(struct lldb_copy_dyld_cache_header_v1, uuid);
2192
0
    dsc_uuid = UUID::fromOptionalData(
2193
0
        dsc_header_data.GetData(&offset, sizeof(uuid_t)), sizeof(uuid_t));
2194
0
  }
2195
0
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS));
2196
0
  if (log && dsc_uuid.IsValid()) {
2197
0
    LLDB_LOGF(log, "Shared cache %s has UUID %s",
2198
0
              dyld_shared_cache.GetPath().c_str(),
2199
0
              dsc_uuid.GetAsString().c_str());
2200
0
  }
2201
0
  return dsc_uuid;
2202
0
}
2203
2204
static llvm::Optional<struct nlist_64>
2205
ParseNList(DataExtractor &nlist_data, lldb::offset_t &nlist_data_offset,
2206
159M
           size_t nlist_byte_size) {
2207
159M
  struct nlist_64 nlist;
2208
159M
  if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2209
0
    return {};
2210
159M
  nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2211
159M
  nlist.n_type = nlist_data.GetU8_unchecked(&nlist_data_offset);
2212
159M
  nlist.n_sect = nlist_data.GetU8_unchecked(&nlist_data_offset);
2213
159M
  nlist.n_desc = nlist_data.GetU16_unchecked(&nlist_data_offset);
2214
159M
  nlist.n_value = nlist_data.GetAddress_unchecked(&nlist_data_offset);
2215
159M
  return nlist;
2216
159M
}
2217
2218
enum { DebugSymbols = true, NonDebugSymbols = false };
2219
2220
135k
void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
2221
135k
  LLDB_SCOPED_TIMERF("ObjectFileMachO::ParseSymtab () module = %s",
2222
135k
                     m_file.GetFilename().AsCString(""));
2223
135k
  ModuleSP module_sp(GetModule());
2224
135k
  if (!module_sp)
2225
0
    return;
2226
2227
135k
  Progress progress(llvm::formatv("Parsing symbol table for {0}",
2228
135k
                                  m_file.GetFilename().AsCString("<Unknown>")));
2229
2230
135k
  llvm::MachO::symtab_command symtab_load_command = {0, 0, 0, 0, 0, 0};
2231
135k
  llvm::MachO::linkedit_data_command function_starts_load_command = {0, 0, 0, 0};
2232
135k
  llvm::MachO::linkedit_data_command exports_trie_load_command = {0, 0, 0, 0};
2233
135k
  llvm::MachO::dyld_info_command dyld_info = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
2234
  // The data element of type bool indicates that this entry is thumb
2235
  // code.
2236
135k
  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
135k
  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
155M
  auto add_symbol_addr = [&symbols_added](lldb::addr_t file_addr) {
2247
    // Don't add the tombstone or empty keys.
2248
155M
    if (file_addr == UINT64_MAX || file_addr == UINT64_MAX - 1)
2249
0
      return;
2250
155M
    symbols_added.insert(file_addr);
2251
155M
  };
2252
135k
  FunctionStarts function_starts;
2253
135k
  lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
2254
135k
  uint32_t i;
2255
135k
  FileSpecList dylib_files;
2256
135k
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS));
2257
135k
  llvm::StringRef g_objc_v2_prefix_class("_OBJC_CLASS_$_");
2258
135k
  llvm::StringRef g_objc_v2_prefix_metaclass("_OBJC_METACLASS_$_");
2259
135k
  llvm::StringRef g_objc_v2_prefix_ivar("_OBJC_IVAR_$_");
2260
2261
3.11M
  for (i = 0; i < m_header.ncmds; 
++i2.97M
) {
2262
2.97M
    const lldb::offset_t cmd_offset = offset;
2263
    // Read in the load command and load command size
2264
2.97M
    llvm::MachO::load_command lc;
2265
2.97M
    if (m_data.GetU32(&offset, &lc, 2) == nullptr)
2266
0
      break;
2267
    // Watch for the symbol table load command
2268
2.97M
    switch (lc.cmd) {
2269
135k
    case LC_SYMTAB:
2270
135k
      symtab_load_command.cmd = lc.cmd;
2271
135k
      symtab_load_command.cmdsize = lc.cmdsize;
2272
      // Read in the rest of the symtab load command
2273
135k
      if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) ==
2274
135k
          nullptr) // fill in symoff, nsyms, stroff, strsize fields
2275
0
        return;
2276
135k
      break;
2277
2278
135k
    case LC_DYLD_INFO:
2279
130k
    case LC_DYLD_INFO_ONLY:
2280
130k
      if (m_data.GetU32(&offset, &dyld_info.rebase_off, 10)) {
2281
130k
        dyld_info.cmd = lc.cmd;
2282
130k
        dyld_info.cmdsize = lc.cmdsize;
2283
130k
      } else {
2284
0
        memset(&dyld_info, 0, sizeof(dyld_info));
2285
0
      }
2286
130k
      break;
2287
2288
898k
    case LC_LOAD_DYLIB:
2289
914k
    case LC_LOAD_WEAK_DYLIB:
2290
1.01M
    case LC_REEXPORT_DYLIB:
2291
1.01M
    case LC_LOADFVMLIB:
2292
1.11M
    case LC_LOAD_UPWARD_DYLIB: {
2293
1.11M
      uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
2294
1.11M
      const char *path = m_data.PeekCStr(name_offset);
2295
1.11M
      if (path) {
2296
1.11M
        FileSpec file_spec(path);
2297
        // Strip the path if there is @rpath, @executable, etc so we just use
2298
        // the basename
2299
1.11M
        if (path[0] == '@')
2300
139
          file_spec.GetDirectory().Clear();
2301
2302
1.11M
        if (lc.cmd == LC_REEXPORT_DYLIB) {
2303
98.4k
          m_reexported_dylibs.AppendIfUnique(file_spec);
2304
98.4k
        }
2305
2306
1.11M
        dylib_files.Append(file_spec);
2307
1.11M
      }
2308
1.11M
    } break;
2309
2310
2.60k
    case LC_DYLD_EXPORTS_TRIE:
2311
2.60k
      exports_trie_load_command.cmd = lc.cmd;
2312
2.60k
      exports_trie_load_command.cmdsize = lc.cmdsize;
2313
2.60k
      if (m_data.GetU32(&offset, &exports_trie_load_command.dataoff, 2) ==
2314
2.60k
          nullptr) // fill in offset and size fields
2315
0
        memset(&exports_trie_load_command, 0,
2316
0
               sizeof(exports_trie_load_command));
2317
2.60k
      break;
2318
133k
    case LC_FUNCTION_STARTS:
2319
133k
      function_starts_load_command.cmd = lc.cmd;
2320
133k
      function_starts_load_command.cmdsize = lc.cmdsize;
2321
133k
      if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) ==
2322
133k
          nullptr) // fill in data offset and size fields
2323
0
        memset(&function_starts_load_command, 0,
2324
0
               sizeof(function_starts_load_command));
2325
133k
      break;
2326
2327
1.45M
    default:
2328
1.45M
      break;
2329
2.97M
    }
2330
2.97M
    offset = cmd_offset + lc.cmdsize;
2331
2.97M
  }
2332
2333
135k
  if (!symtab_load_command.cmd)
2334
3
    return;
2335
2336
135k
  SectionList *section_list = GetSectionList();
2337
135k
  if (section_list == nullptr)
2338
0
    return;
2339
2340
135k
  const uint32_t addr_byte_size = m_data.GetAddressByteSize();
2341
135k
  const ByteOrder byte_order = m_data.GetByteOrder();
2342
135k
  bool bit_width_32 = addr_byte_size == 4;
2343
135k
  const size_t nlist_byte_size =
2344
135k
      bit_width_32 ? 
sizeof(struct nlist)1
:
sizeof(struct nlist_64)135k
;
2345
2346
135k
  DataExtractor nlist_data(nullptr, 0, byte_order, addr_byte_size);
2347
135k
  DataExtractor strtab_data(nullptr, 0, byte_order, addr_byte_size);
2348
135k
  DataExtractor function_starts_data(nullptr, 0, byte_order, addr_byte_size);
2349
135k
  DataExtractor indirect_symbol_index_data(nullptr, 0, byte_order,
2350
135k
                                           addr_byte_size);
2351
135k
  DataExtractor dyld_trie_data(nullptr, 0, byte_order, addr_byte_size);
2352
2353
135k
  const addr_t nlist_data_byte_size =
2354
135k
      symtab_load_command.nsyms * nlist_byte_size;
2355
135k
  const addr_t strtab_data_byte_size = symtab_load_command.strsize;
2356
135k
  addr_t strtab_addr = LLDB_INVALID_ADDRESS;
2357
2358
135k
  ProcessSP process_sp(m_process_wp.lock());
2359
135k
  Process *process = process_sp.get();
2360
2361
135k
  uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete;
2362
135k
  bool is_shared_cache_image = IsSharedCacheBinary();
2363
135k
  bool is_local_shared_cache_image = is_shared_cache_image && 
!IsInMemory()127k
;
2364
135k
  SectionSP linkedit_section_sp(
2365
135k
      section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2366
2367
135k
  if (process && 
m_header.filetype != llvm::MachO::MH_OBJECT2
&&
2368
135k
      
!is_local_shared_cache_image2
) {
2369
2
    Target &target = process->GetTarget();
2370
2371
2
    memory_module_load_level = target.GetMemoryModuleLoadLevel();
2372
2373
    // Reading mach file from memory in a process or core file...
2374
2375
2
    if (linkedit_section_sp) {
2376
2
      addr_t linkedit_load_addr =
2377
2
          linkedit_section_sp->GetLoadBaseAddress(&target);
2378
2
      if (linkedit_load_addr == LLDB_INVALID_ADDRESS) {
2379
        // We might be trying to access the symbol table before the
2380
        // __LINKEDIT's load address has been set in the target. We can't
2381
        // fail to read the symbol table, so calculate the right address
2382
        // manually
2383
2
        linkedit_load_addr = CalculateSectionLoadAddressForMemoryImage(
2384
2
            m_memory_addr, GetMachHeaderSection(), linkedit_section_sp.get());
2385
2
      }
2386
2387
2
      const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2388
2
      const addr_t symoff_addr = linkedit_load_addr +
2389
2
                                 symtab_load_command.symoff -
2390
2
                                 linkedit_file_offset;
2391
2
      strtab_addr = linkedit_load_addr + symtab_load_command.stroff -
2392
2
                    linkedit_file_offset;
2393
2394
        // Always load dyld - the dynamic linker - from memory if we didn't
2395
        // find a binary anywhere else. lldb will not register
2396
        // dylib/framework/bundle loads/unloads if we don't have the dyld
2397
        // symbols, we force dyld to load from memory despite the user's
2398
        // target.memory-module-load-level setting.
2399
2
        if (memory_module_load_level == eMemoryModuleLoadLevelComplete ||
2400
2
            
m_header.filetype == llvm::MachO::MH_DYLINKER0
) {
2401
2
          DataBufferSP nlist_data_sp(
2402
2
              ReadMemory(process_sp, symoff_addr, nlist_data_byte_size));
2403
2
          if (nlist_data_sp)
2404
2
            nlist_data.SetData(nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2405
2
          if (m_dysymtab.nindirectsyms != 0) {
2406
2
            const addr_t indirect_syms_addr = linkedit_load_addr +
2407
2
                                              m_dysymtab.indirectsymoff -
2408
2
                                              linkedit_file_offset;
2409
2
            DataBufferSP indirect_syms_data_sp(ReadMemory(
2410
2
                process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
2411
2
            if (indirect_syms_data_sp)
2412
2
              indirect_symbol_index_data.SetData(
2413
2
                  indirect_syms_data_sp, 0,
2414
2
                  indirect_syms_data_sp->GetByteSize());
2415
            // If this binary is outside the shared cache,
2416
            // cache the string table.
2417
            // Binaries in the shared cache all share a giant string table,
2418
            // and we can't share the string tables across multiple
2419
            // ObjectFileMachO's, so we'd end up re-reading this mega-strtab
2420
            // for every binary in the shared cache - it would be a big perf
2421
            // problem. For binaries outside the shared cache, it's faster to
2422
            // read the entire strtab at once instead of piece-by-piece as we
2423
            // process the nlist records.
2424
2
            if (!is_shared_cache_image) {
2425
2
              DataBufferSP strtab_data_sp(
2426
2
                  ReadMemory(process_sp, strtab_addr, strtab_data_byte_size));
2427
2
              if (strtab_data_sp) {
2428
2
                strtab_data.SetData(strtab_data_sp, 0,
2429
2
                                    strtab_data_sp->GetByteSize());
2430
2
              }
2431
2
            }
2432
2
          }
2433
2
        if (memory_module_load_level >= eMemoryModuleLoadLevelPartial) {
2434
2
          if (function_starts_load_command.cmd) {
2435
2
            const addr_t func_start_addr =
2436
2
                linkedit_load_addr + function_starts_load_command.dataoff -
2437
2
                linkedit_file_offset;
2438
2
            DataBufferSP func_start_data_sp(
2439
2
                ReadMemory(process_sp, func_start_addr,
2440
2
                           function_starts_load_command.datasize));
2441
2
            if (func_start_data_sp)
2442
2
              function_starts_data.SetData(func_start_data_sp, 0,
2443
2
                                           func_start_data_sp->GetByteSize());
2444
2
          }
2445
2
        }
2446
2
      }
2447
2
    }
2448
135k
  } else {
2449
135k
    if (is_local_shared_cache_image) {
2450
      // The load commands in shared cache images are relative to the
2451
      // beginning of the shared cache, not the library image. The
2452
      // data we get handed when creating the ObjectFileMachO starts
2453
      // at the beginning of a specific library and spans to the end
2454
      // of the cache to be able to reach the shared LINKEDIT
2455
      // segments. We need to convert the load command offsets to be
2456
      // relative to the beginning of our specific image.
2457
127k
      lldb::addr_t linkedit_offset = linkedit_section_sp->GetFileOffset();
2458
127k
      lldb::offset_t linkedit_slide =
2459
127k
          linkedit_offset - m_linkedit_original_offset;
2460
127k
      symtab_load_command.symoff += linkedit_slide;
2461
127k
      symtab_load_command.stroff += linkedit_slide;
2462
127k
      dyld_info.export_off += linkedit_slide;
2463
127k
      m_dysymtab.indirectsymoff += linkedit_slide;
2464
127k
      function_starts_load_command.dataoff += linkedit_slide;
2465
127k
      exports_trie_load_command.dataoff += linkedit_slide;
2466
127k
    }
2467
2468
135k
    nlist_data.SetData(m_data, symtab_load_command.symoff,
2469
135k
                       nlist_data_byte_size);
2470
135k
    strtab_data.SetData(m_data, symtab_load_command.stroff,
2471
135k
                        strtab_data_byte_size);
2472
2473
    // We shouldn't have exports data from both the LC_DYLD_INFO command
2474
    // AND the LC_DYLD_EXPORTS_TRIE command in the same binary:
2475
135k
    lldbassert(!((dyld_info.export_size > 0)
2476
135k
                 && (exports_trie_load_command.datasize > 0)));
2477
135k
    if (dyld_info.export_size > 0) {
2478
129k
      dyld_trie_data.SetData(m_data, dyld_info.export_off,
2479
129k
                             dyld_info.export_size);
2480
129k
    } else 
if (5.24k
exports_trie_load_command.datasize > 05.24k
) {
2481
2.60k
      dyld_trie_data.SetData(m_data, exports_trie_load_command.dataoff,
2482
2.60k
                             exports_trie_load_command.datasize);
2483
2.60k
    }
2484
2485
135k
    if (m_dysymtab.nindirectsyms != 0) {
2486
128k
      indirect_symbol_index_data.SetData(m_data, m_dysymtab.indirectsymoff,
2487
128k
                                         m_dysymtab.nindirectsyms * 4);
2488
128k
    }
2489
135k
    if (function_starts_load_command.cmd) {
2490
133k
      function_starts_data.SetData(m_data, function_starts_load_command.dataoff,
2491
133k
                                   function_starts_load_command.datasize);
2492
133k
    }
2493
135k
  }
2494
2495
135k
  const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2496
2497
135k
  ConstString g_segment_name_TEXT = GetSegmentNameTEXT();
2498
135k
  ConstString g_segment_name_DATA = GetSegmentNameDATA();
2499
135k
  ConstString g_segment_name_DATA_DIRTY = GetSegmentNameDATA_DIRTY();
2500
135k
  ConstString g_segment_name_DATA_CONST = GetSegmentNameDATA_CONST();
2501
135k
  ConstString g_segment_name_OBJC = GetSegmentNameOBJC();
2502
135k
  ConstString g_section_name_eh_frame = GetSectionNameEHFrame();
2503
135k
  SectionSP text_section_sp(
2504
135k
      section_list->FindSectionByName(g_segment_name_TEXT));
2505
135k
  SectionSP data_section_sp(
2506
135k
      section_list->FindSectionByName(g_segment_name_DATA));
2507
135k
  SectionSP data_dirty_section_sp(
2508
135k
      section_list->FindSectionByName(g_segment_name_DATA_DIRTY));
2509
135k
  SectionSP data_const_section_sp(
2510
135k
      section_list->FindSectionByName(g_segment_name_DATA_CONST));
2511
135k
  SectionSP objc_section_sp(
2512
135k
      section_list->FindSectionByName(g_segment_name_OBJC));
2513
135k
  SectionSP eh_frame_section_sp;
2514
135k
  if (text_section_sp.get())
2515
135k
    eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName(
2516
135k
        g_section_name_eh_frame);
2517
0
  else
2518
0
    eh_frame_section_sp =
2519
0
        section_list->FindSectionByName(g_section_name_eh_frame);
2520
2521
135k
  const bool is_arm = (m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
2522
135k
  const bool always_thumb = GetArchitecture().IsAlwaysThumbInstructions();
2523
2524
  // lldb works best if it knows the start address of all functions in a
2525
  // module. Linker symbols or debug info are normally the best source of
2526
  // information for start addr / size but they may be stripped in a released
2527
  // binary. Two additional sources of information exist in Mach-O binaries:
2528
  //    LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each
2529
  //    function's start address in the
2530
  //                         binary, relative to the text section.
2531
  //    eh_frame           - the eh_frame FDEs have the start addr & size of
2532
  //    each function
2533
  //  LC_FUNCTION_STARTS is the fastest source to read in, and is present on
2534
  //  all modern binaries.
2535
  //  Binaries built to run on older releases may need to use eh_frame
2536
  //  information.
2537
2538
135k
  if (text_section_sp && function_starts_data.GetByteSize()) {
2539
133k
    FunctionStarts::Entry function_start_entry;
2540
133k
    function_start_entry.data = false;
2541
133k
    lldb::offset_t function_start_offset = 0;
2542
133k
    function_start_entry.addr = text_section_sp->GetFileAddress();
2543
133k
    uint64_t delta;
2544
99.7M
    while ((delta = function_starts_data.GetULEB128(&function_start_offset)) >
2545
99.7M
           0) {
2546
      // Now append the current entry
2547
99.6M
      function_start_entry.addr += delta;
2548
99.6M
      if (is_arm) {
2549
0
        if (function_start_entry.addr & 1) {
2550
0
          function_start_entry.addr &= THUMB_ADDRESS_BIT_MASK;
2551
0
          function_start_entry.data = true;
2552
0
        } else if (always_thumb) {
2553
0
          function_start_entry.data = true;
2554
0
        }
2555
0
      }
2556
99.6M
      function_starts.Append(function_start_entry);
2557
99.6M
    }
2558
133k
  } else {
2559
    // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the
2560
    // load command claiming an eh_frame but it doesn't actually have the
2561
    // eh_frame content.  And if we have a dSYM, we don't need to do any of
2562
    // this fill-in-the-missing-symbols works anyway - the debug info should
2563
    // give us all the functions in the module.
2564
1.94k
    if (text_section_sp.get() && eh_frame_section_sp.get() &&
2565
1.94k
        
m_type != eTypeDebugInfo1.63k
) {
2566
1.63k
      DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp,
2567
1.63k
                                  DWARFCallFrameInfo::EH);
2568
1.63k
      DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
2569
1.63k
      eh_frame.GetFunctionAddressAndSizeVector(functions);
2570
1.63k
      addr_t text_base_addr = text_section_sp->GetFileAddress();
2571
1.63k
      size_t count = functions.GetSize();
2572
38.2k
      for (size_t i = 0; i < count; 
++i36.6k
) {
2573
36.6k
        const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func =
2574
36.6k
            functions.GetEntryAtIndex(i);
2575
36.6k
        if (func) {
2576
36.6k
          FunctionStarts::Entry function_start_entry;
2577
36.6k
          function_start_entry.addr = func->base - text_base_addr;
2578
36.6k
          if (is_arm) {
2579
0
            if (function_start_entry.addr & 1) {
2580
0
              function_start_entry.addr &= THUMB_ADDRESS_BIT_MASK;
2581
0
              function_start_entry.data = true;
2582
0
            } else if (always_thumb) {
2583
0
              function_start_entry.data = true;
2584
0
            }
2585
0
          }
2586
36.6k
          function_starts.Append(function_start_entry);
2587
36.6k
        }
2588
36.6k
      }
2589
1.63k
    }
2590
1.94k
  }
2591
2592
135k
  const size_t function_starts_count = function_starts.GetSize();
2593
2594
  // For user process binaries (executables, dylibs, frameworks, bundles), if
2595
  // we don't have LC_FUNCTION_STARTS/eh_frame section in this binary, we're
2596
  // going to assume the binary has been stripped.  Don't allow assembly
2597
  // language instruction emulation because we don't know proper function
2598
  // start boundaries.
2599
  //
2600
  // For all other types of binaries (kernels, stand-alone bare board
2601
  // binaries, kexts), they may not have LC_FUNCTION_STARTS / eh_frame
2602
  // sections - we should not make any assumptions about them based on that.
2603
135k
  if (function_starts_count == 0 && 
CalculateStrata() == eStrataUser3.99k
) {
2604
3.68k
    m_allow_assembly_emulation_unwind_plans = false;
2605
3.68k
    Log *unwind_or_symbol_log(lldb_private::GetLogIfAnyCategoriesSet(
2606
3.68k
        LIBLLDB_LOG_SYMBOLS | LIBLLDB_LOG_UNWIND));
2607
2608
3.68k
    if (unwind_or_symbol_log)
2609
0
      module_sp->LogMessage(
2610
0
          unwind_or_symbol_log,
2611
0
          "no LC_FUNCTION_STARTS, will not allow assembly profiled unwinds");
2612
3.68k
  }
2613
2614
135k
  const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get()
2615
135k
                                             ? 
eh_frame_section_sp->GetID()76.8k
2616
135k
                                             : 
static_cast<user_id_t>(NO_SECT)58.1k
;
2617
2618
135k
  lldb::offset_t nlist_data_offset = 0;
2619
2620
135k
  uint32_t N_SO_index = UINT32_MAX;
2621
2622
135k
  MachSymtabSectionInfo section_info(section_list);
2623
135k
  std::vector<uint32_t> N_FUN_indexes;
2624
135k
  std::vector<uint32_t> N_NSYM_indexes;
2625
135k
  std::vector<uint32_t> N_INCL_indexes;
2626
135k
  std::vector<uint32_t> N_BRAC_indexes;
2627
135k
  std::vector<uint32_t> N_COMM_indexes;
2628
135k
  typedef std::multimap<uint64_t, uint32_t> ValueToSymbolIndexMap;
2629
135k
  typedef llvm::DenseMap<uint32_t, uint32_t> NListIndexToSymbolIndexMap;
2630
135k
  typedef llvm::DenseMap<const char *, uint32_t> ConstNameToSymbolIndexMap;
2631
135k
  ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2632
135k
  ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
2633
135k
  ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
2634
  // Any symbols that get merged into another will get an entry in this map
2635
  // so we know
2636
135k
  NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2637
135k
  uint32_t nlist_idx = 0;
2638
135k
  Symbol *symbol_ptr = nullptr;
2639
2640
135k
  uint32_t sym_idx = 0;
2641
135k
  Symbol *sym = nullptr;
2642
135k
  size_t num_syms = 0;
2643
135k
  std::string memory_symbol_name;
2644
135k
  uint32_t unmapped_local_symbols_found = 0;
2645
2646
135k
  std::vector<TrieEntryWithOffset> reexport_trie_entries;
2647
135k
  std::vector<TrieEntryWithOffset> external_sym_trie_entries;
2648
135k
  std::set<lldb::addr_t> resolver_addresses;
2649
2650
135k
  if (dyld_trie_data.GetByteSize() > 0) {
2651
132k
    ConstString text_segment_name("__TEXT");
2652
132k
    SectionSP text_segment_sp =
2653
132k
        GetSectionList()->FindSectionByName(text_segment_name);
2654
132k
    lldb::addr_t text_segment_file_addr = LLDB_INVALID_ADDRESS;
2655
132k
    if (text_segment_sp)
2656
132k
      text_segment_file_addr = text_segment_sp->GetFileAddress();
2657
132k
    std::vector<llvm::StringRef> nameSlices;
2658
132k
    ParseTrieEntries(dyld_trie_data, 0, is_arm, text_segment_file_addr,
2659
132k
                     nameSlices, resolver_addresses, reexport_trie_entries,
2660
132k
                     external_sym_trie_entries);
2661
132k
  }
2662
2663
135k
  typedef std::set<ConstString> IndirectSymbols;
2664
135k
  IndirectSymbols indirect_symbol_names;
2665
2666
#if TARGET_OS_IPHONE
2667
2668
  // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been
2669
  // optimized by moving LOCAL symbols out of the memory mapped portion of
2670
  // the DSC. The symbol information has all been retained, but it isn't
2671
  // available in the normal nlist data. However, there *are* duplicate
2672
  // entries of *some*
2673
  // LOCAL symbols in the normal nlist data. To handle this situation
2674
  // correctly, we must first attempt
2675
  // to parse any DSC unmapped symbol information. If we find any, we set a
2676
  // flag that tells the normal nlist parser to ignore all LOCAL symbols.
2677
2678
  if (IsSharedCacheBinary()) {
2679
    // Before we can start mapping the DSC, we need to make certain the
2680
    // target process is actually using the cache we can find.
2681
2682
    // Next we need to determine the correct path for the dyld shared cache.
2683
2684
    ArchSpec header_arch = GetArchitecture();
2685
    char dsc_path[PATH_MAX];
2686
    char dsc_path_development[PATH_MAX];
2687
2688
    snprintf(
2689
        dsc_path, sizeof(dsc_path), "%s%s%s",
2690
        "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR
2691
                                                   */
2692
        "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
2693
        header_arch.GetArchitectureName());
2694
2695
    snprintf(
2696
        dsc_path_development, sizeof(dsc_path), "%s%s%s%s",
2697
        "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR
2698
                                                   */
2699
        "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
2700
        header_arch.GetArchitectureName(), ".development");
2701
2702
    FileSpec dsc_nondevelopment_filespec(dsc_path);
2703
    FileSpec dsc_development_filespec(dsc_path_development);
2704
    FileSpec dsc_filespec;
2705
2706
    UUID dsc_uuid;
2707
    UUID process_shared_cache_uuid;
2708
    addr_t process_shared_cache_base_addr;
2709
2710
    if (process) {
2711
      GetProcessSharedCacheUUID(process, process_shared_cache_base_addr,
2712
                                process_shared_cache_uuid);
2713
    }
2714
2715
    // First see if we can find an exact match for the inferior process
2716
    // shared cache UUID in the development or non-development shared caches
2717
    // on disk.
2718
    if (process_shared_cache_uuid.IsValid()) {
2719
      if (FileSystem::Instance().Exists(dsc_development_filespec)) {
2720
        UUID dsc_development_uuid = GetSharedCacheUUID(
2721
            dsc_development_filespec, byte_order, addr_byte_size);
2722
        if (dsc_development_uuid.IsValid() &&
2723
            dsc_development_uuid == process_shared_cache_uuid) {
2724
          dsc_filespec = dsc_development_filespec;
2725
          dsc_uuid = dsc_development_uuid;
2726
        }
2727
      }
2728
      if (!dsc_uuid.IsValid() &&
2729
          FileSystem::Instance().Exists(dsc_nondevelopment_filespec)) {
2730
        UUID dsc_nondevelopment_uuid = GetSharedCacheUUID(
2731
            dsc_nondevelopment_filespec, byte_order, addr_byte_size);
2732
        if (dsc_nondevelopment_uuid.IsValid() &&
2733
            dsc_nondevelopment_uuid == process_shared_cache_uuid) {
2734
          dsc_filespec = dsc_nondevelopment_filespec;
2735
          dsc_uuid = dsc_nondevelopment_uuid;
2736
        }
2737
      }
2738
    }
2739
2740
    // Failing a UUID match, prefer the development dyld_shared cache if both
2741
    // are present.
2742
    if (!FileSystem::Instance().Exists(dsc_filespec)) {
2743
      if (FileSystem::Instance().Exists(dsc_development_filespec)) {
2744
        dsc_filespec = dsc_development_filespec;
2745
      } else {
2746
        dsc_filespec = dsc_nondevelopment_filespec;
2747
      }
2748
    }
2749
2750
    /* The dyld_cache_header has a pointer to the
2751
       dyld_cache_local_symbols_info structure (localSymbolsOffset).
2752
       The dyld_cache_local_symbols_info structure gives us three things:
2753
         1. The start and count of the nlist records in the dyld_shared_cache
2754
       file
2755
         2. The start and size of the strings for these nlist records
2756
         3. The start and count of dyld_cache_local_symbols_entry entries
2757
2758
       There is one dyld_cache_local_symbols_entry per dylib/framework in the
2759
       dyld shared cache.
2760
       The "dylibOffset" field is the Mach-O header of this dylib/framework in
2761
       the dyld shared cache.
2762
       The dyld_cache_local_symbols_entry also lists the start of this
2763
       dylib/framework's nlist records
2764
       and the count of how many nlist records there are for this
2765
       dylib/framework.
2766
    */
2767
2768
    // Process the dyld shared cache header to find the unmapped symbols
2769
2770
    DataBufferSP dsc_data_sp = MapFileData(
2771
        dsc_filespec, sizeof(struct lldb_copy_dyld_cache_header_v1), 0);
2772
    if (!dsc_uuid.IsValid()) {
2773
      dsc_uuid = GetSharedCacheUUID(dsc_filespec, byte_order, addr_byte_size);
2774
    }
2775
    if (dsc_data_sp) {
2776
      DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
2777
2778
      bool uuid_match = true;
2779
      if (dsc_uuid.IsValid() && process) {
2780
        if (process_shared_cache_uuid.IsValid() &&
2781
            dsc_uuid != process_shared_cache_uuid) {
2782
          // The on-disk dyld_shared_cache file is not the same as the one in
2783
          // this process' memory, don't use it.
2784
          uuid_match = false;
2785
          ModuleSP module_sp(GetModule());
2786
          if (module_sp)
2787
            module_sp->ReportWarning("process shared cache does not match "
2788
                                     "on-disk dyld_shared_cache file, some "
2789
                                     "symbol names will be missing.");
2790
        }
2791
      }
2792
2793
      offset = offsetof(struct lldb_copy_dyld_cache_header_v1, mappingOffset);
2794
2795
      uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
2796
2797
      // If the mappingOffset points to a location inside the header, we've
2798
      // opened an old dyld shared cache, and should not proceed further.
2799
      if (uuid_match &&
2800
          mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v1)) {
2801
2802
        DataBufferSP dsc_mapping_info_data_sp = MapFileData(
2803
            dsc_filespec, sizeof(struct lldb_copy_dyld_cache_mapping_info),
2804
            mappingOffset);
2805
2806
        DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp,
2807
                                            byte_order, addr_byte_size);
2808
        offset = 0;
2809
2810
        // The File addresses (from the in-memory Mach-O load commands) for
2811
        // the shared libraries in the shared library cache need to be
2812
        // adjusted by an offset to match up with the dylibOffset identifying
2813
        // field in the dyld_cache_local_symbol_entry's.  This offset is
2814
        // recorded in mapping_offset_value.
2815
        const uint64_t mapping_offset_value =
2816
            dsc_mapping_info_data.GetU64(&offset);
2817
2818
        offset =
2819
            offsetof(struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
2820
        uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
2821
        uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
2822
2823
        if (localSymbolsOffset && localSymbolsSize) {
2824
          // Map the local symbols
2825
          DataBufferSP dsc_local_symbols_data_sp =
2826
              MapFileData(dsc_filespec, localSymbolsSize, localSymbolsOffset);
2827
2828
          if (dsc_local_symbols_data_sp) {
2829
            DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp,
2830
                                                 byte_order, addr_byte_size);
2831
2832
            offset = 0;
2833
2834
            typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap;
2835
            typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName;
2836
            UndefinedNameToDescMap undefined_name_to_desc;
2837
            SymbolIndexToName reexport_shlib_needs_fixup;
2838
2839
            // Read the local_symbols_infos struct in one shot
2840
            struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
2841
            dsc_local_symbols_data.GetU32(&offset,
2842
                                          &local_symbols_info.nlistOffset, 6);
2843
2844
            SectionSP text_section_sp(
2845
                section_list->FindSectionByName(GetSegmentNameTEXT()));
2846
2847
            uint32_t header_file_offset =
2848
                (text_section_sp->GetFileAddress() - mapping_offset_value);
2849
2850
            offset = local_symbols_info.entriesOffset;
2851
            for (uint32_t entry_index = 0;
2852
                 entry_index < local_symbols_info.entriesCount; entry_index++) {
2853
              struct lldb_copy_dyld_cache_local_symbols_entry
2854
                  local_symbols_entry;
2855
              local_symbols_entry.dylibOffset =
2856
                  dsc_local_symbols_data.GetU32(&offset);
2857
              local_symbols_entry.nlistStartIndex =
2858
                  dsc_local_symbols_data.GetU32(&offset);
2859
              local_symbols_entry.nlistCount =
2860
                  dsc_local_symbols_data.GetU32(&offset);
2861
2862
              if (header_file_offset == local_symbols_entry.dylibOffset) {
2863
                unmapped_local_symbols_found = local_symbols_entry.nlistCount;
2864
2865
                // The normal nlist code cannot correctly size the Symbols
2866
                // array, we need to allocate it here.
2867
                sym = symtab.Resize(
2868
                    symtab_load_command.nsyms + m_dysymtab.nindirectsyms +
2869
                    unmapped_local_symbols_found - m_dysymtab.nlocalsym);
2870
                num_syms = symtab.GetNumSymbols();
2871
2872
                nlist_data_offset =
2873
                    local_symbols_info.nlistOffset +
2874
                    (nlist_byte_size * local_symbols_entry.nlistStartIndex);
2875
                uint32_t string_table_offset = local_symbols_info.stringsOffset;
2876
2877
                for (uint32_t nlist_index = 0;
2878
                     nlist_index < local_symbols_entry.nlistCount;
2879
                     nlist_index++) {
2880
                  /////////////////////////////
2881
                  {
2882
                    llvm::Optional<struct nlist_64> nlist_maybe =
2883
                        ParseNList(dsc_local_symbols_data, nlist_data_offset,
2884
                                   nlist_byte_size);
2885
                    if (!nlist_maybe)
2886
                      break;
2887
                    struct nlist_64 nlist = *nlist_maybe;
2888
2889
                    SymbolType type = eSymbolTypeInvalid;
2890
                    const char *symbol_name = dsc_local_symbols_data.PeekCStr(
2891
                        string_table_offset + nlist.n_strx);
2892
2893
                    if (symbol_name == NULL) {
2894
                      // No symbol should be NULL, even the symbols with no
2895
                      // string values should have an offset zero which
2896
                      // points to an empty C-string
2897
                      Host::SystemLog(
2898
                          Host::eSystemLogError,
2899
                          "error: DSC unmapped local symbol[%u] has invalid "
2900
                          "string table offset 0x%x in %s, ignoring symbol\n",
2901
                          entry_index, nlist.n_strx,
2902
                          module_sp->GetFileSpec().GetPath().c_str());
2903
                      continue;
2904
                    }
2905
                    if (symbol_name[0] == '\0')
2906
                      symbol_name = NULL;
2907
2908
                    const char *symbol_name_non_abi_mangled = NULL;
2909
2910
                    SectionSP symbol_section;
2911
                    uint32_t symbol_byte_size = 0;
2912
                    bool add_nlist = true;
2913
                    bool is_debug = ((nlist.n_type & N_STAB) != 0);
2914
                    bool demangled_is_synthesized = false;
2915
                    bool is_gsym = false;
2916
                    bool set_value = true;
2917
2918
                    assert(sym_idx < num_syms);
2919
2920
                    sym[sym_idx].SetDebug(is_debug);
2921
2922
                    if (is_debug) {
2923
                      switch (nlist.n_type) {
2924
                      case N_GSYM:
2925
                        // global symbol: name,,NO_SECT,type,0
2926
                        // Sometimes the N_GSYM value contains the address.
2927
2928
                        // FIXME: In the .o files, we have a GSYM and a debug
2929
                        // symbol for all the ObjC data.  They
2930
                        // have the same address, but we want to ensure that
2931
                        // we always find only the real symbol, 'cause we
2932
                        // don't currently correctly attribute the
2933
                        // GSYM one to the ObjCClass/Ivar/MetaClass
2934
                        // symbol type.  This is a temporary hack to make
2935
                        // sure the ObjectiveC symbols get treated correctly.
2936
                        // To do this right, we should coalesce all the GSYM
2937
                        // & global symbols that have the same address.
2938
2939
                        is_gsym = true;
2940
                        sym[sym_idx].SetExternal(true);
2941
2942
                        if (symbol_name && symbol_name[0] == '_' &&
2943
                            symbol_name[1] == 'O') {
2944
                          llvm::StringRef symbol_name_ref(symbol_name);
2945
                          if (symbol_name_ref.startswith(
2946
                                  g_objc_v2_prefix_class)) {
2947
                            symbol_name_non_abi_mangled = symbol_name + 1;
2948
                            symbol_name =
2949
                                symbol_name + g_objc_v2_prefix_class.size();
2950
                            type = eSymbolTypeObjCClass;
2951
                            demangled_is_synthesized = true;
2952
2953
                          } else if (symbol_name_ref.startswith(
2954
                                         g_objc_v2_prefix_metaclass)) {
2955
                            symbol_name_non_abi_mangled = symbol_name + 1;
2956
                            symbol_name =
2957
                                symbol_name + g_objc_v2_prefix_metaclass.size();
2958
                            type = eSymbolTypeObjCMetaClass;
2959
                            demangled_is_synthesized = true;
2960
                          } else if (symbol_name_ref.startswith(
2961
                                         g_objc_v2_prefix_ivar)) {
2962
                            symbol_name_non_abi_mangled = symbol_name + 1;
2963
                            symbol_name =
2964
                                symbol_name + g_objc_v2_prefix_ivar.size();
2965
                            type = eSymbolTypeObjCIVar;
2966
                            demangled_is_synthesized = true;
2967
                          }
2968
                        } else {
2969
                          if (nlist.n_value != 0)
2970
                            symbol_section = section_info.GetSection(
2971
                                nlist.n_sect, nlist.n_value);
2972
                          type = eSymbolTypeData;
2973
                        }
2974
                        break;
2975
2976
                      case N_FNAME:
2977
                        // procedure name (f77 kludge): name,,NO_SECT,0,0
2978
                        type = eSymbolTypeCompiler;
2979
                        break;
2980
2981
                      case N_FUN:
2982
                        // procedure: name,,n_sect,linenumber,address
2983
                        if (symbol_name) {
2984
                          type = eSymbolTypeCode;
2985
                          symbol_section = section_info.GetSection(
2986
                              nlist.n_sect, nlist.n_value);
2987
2988
                          N_FUN_addr_to_sym_idx.insert(
2989
                              std::make_pair(nlist.n_value, sym_idx));
2990
                          // We use the current number of symbols in the
2991
                          // symbol table in lieu of using nlist_idx in case
2992
                          // we ever start trimming entries out
2993
                          N_FUN_indexes.push_back(sym_idx);
2994
                        } else {
2995
                          type = eSymbolTypeCompiler;
2996
2997
                          if (!N_FUN_indexes.empty()) {
2998
                            // Copy the size of the function into the
2999
                            // original
3000
                            // STAB entry so we don't have
3001
                            // to hunt for it later
3002
                            symtab.SymbolAtIndex(N_FUN_indexes.back())
3003
                                ->SetByteSize(nlist.n_value);
3004
                            N_FUN_indexes.pop_back();
3005
                            // We don't really need the end function STAB as
3006
                            // it contains the size which we already placed
3007
                            // with the original symbol, so don't add it if
3008
                            // we want a minimal symbol table
3009
                            add_nlist = false;
3010
                          }
3011
                        }
3012
                        break;
3013
3014
                      case N_STSYM:
3015
                        // static symbol: name,,n_sect,type,address
3016
                        N_STSYM_addr_to_sym_idx.insert(
3017
                            std::make_pair(nlist.n_value, sym_idx));
3018
                        symbol_section = section_info.GetSection(nlist.n_sect,
3019
                                                                 nlist.n_value);
3020
                        if (symbol_name && symbol_name[0]) {
3021
                          type = ObjectFile::GetSymbolTypeFromName(
3022
                              symbol_name + 1, eSymbolTypeData);
3023
                        }
3024
                        break;
3025
3026
                      case N_LCSYM:
3027
                        // .lcomm symbol: name,,n_sect,type,address
3028
                        symbol_section = section_info.GetSection(nlist.n_sect,
3029
                                                                 nlist.n_value);
3030
                        type = eSymbolTypeCommonBlock;
3031
                        break;
3032
3033
                      case N_BNSYM:
3034
                        // We use the current number of symbols in the symbol
3035
                        // table in lieu of using nlist_idx in case we ever
3036
                        // start trimming entries out Skip these if we want
3037
                        // minimal symbol tables
3038
                        add_nlist = false;
3039
                        break;
3040
3041
                      case N_ENSYM:
3042
                        // Set the size of the N_BNSYM to the terminating
3043
                        // index of this N_ENSYM so that we can always skip
3044
                        // the entire symbol if we need to navigate more
3045
                        // quickly at the source level when parsing STABS
3046
                        // Skip these if we want minimal symbol tables
3047
                        add_nlist = false;
3048
                        break;
3049
3050
                      case N_OPT:
3051
                        // emitted with gcc2_compiled and in gcc source
3052
                        type = eSymbolTypeCompiler;
3053
                        break;
3054
3055
                      case N_RSYM:
3056
                        // register sym: name,,NO_SECT,type,register
3057
                        type = eSymbolTypeVariable;
3058
                        break;
3059
3060
                      case N_SLINE:
3061
                        // src line: 0,,n_sect,linenumber,address
3062
                        symbol_section = section_info.GetSection(nlist.n_sect,
3063
                                                                 nlist.n_value);
3064
                        type = eSymbolTypeLineEntry;
3065
                        break;
3066
3067
                      case N_SSYM:
3068
                        // structure elt: name,,NO_SECT,type,struct_offset
3069
                        type = eSymbolTypeVariableType;
3070
                        break;
3071
3072
                      case N_SO:
3073
                        // source file name
3074
                        type = eSymbolTypeSourceFile;
3075
                        if (symbol_name == NULL) {
3076
                          add_nlist = false;
3077
                          if (N_SO_index != UINT32_MAX) {
3078
                            // Set the size of the N_SO to the terminating
3079
                            // index of this N_SO so that we can always skip
3080
                            // the entire N_SO if we need to navigate more
3081
                            // quickly at the source level when parsing STABS
3082
                            symbol_ptr = symtab.SymbolAtIndex(N_SO_index);
3083
                            symbol_ptr->SetByteSize(sym_idx);
3084
                            symbol_ptr->SetSizeIsSibling(true);
3085
                          }
3086
                          N_NSYM_indexes.clear();
3087
                          N_INCL_indexes.clear();
3088
                          N_BRAC_indexes.clear();
3089
                          N_COMM_indexes.clear();
3090
                          N_FUN_indexes.clear();
3091
                          N_SO_index = UINT32_MAX;
3092
                        } else {
3093
                          // We use the current number of symbols in the
3094
                          // symbol table in lieu of using nlist_idx in case
3095
                          // we ever start trimming entries out
3096
                          const bool N_SO_has_full_path = symbol_name[0] == '/';
3097
                          if (N_SO_has_full_path) {
3098
                            if ((N_SO_index == sym_idx - 1) &&
3099
                                ((sym_idx - 1) < num_syms)) {
3100
                              // We have two consecutive N_SO entries where
3101
                              // the first contains a directory and the
3102
                              // second contains a full path.
3103
                              sym[sym_idx - 1].GetMangled().SetValue(
3104
                                  ConstString(symbol_name), false);
3105
                              m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3106
                              add_nlist = false;
3107
                            } else {
3108
                              // This is the first entry in a N_SO that
3109
                              // contains a directory or
3110
                              // a full path to the source file
3111
                              N_SO_index = sym_idx;
3112
                            }
3113
                          } else if ((N_SO_index == sym_idx - 1) &&
3114
                                     ((sym_idx - 1) < num_syms)) {
3115
                            // This is usually the second N_SO entry that
3116
                            // contains just the filename, so here we combine
3117
                            // it with the first one if we are minimizing the
3118
                            // symbol table
3119
                            const char *so_path = sym[sym_idx - 1]
3120
                                                      .GetMangled()
3121
                                                      .GetDemangledName()
3122
                                                      .AsCString();
3123
                            if (so_path && so_path[0]) {
3124
                              std::string full_so_path(so_path);
3125
                              const size_t double_slash_pos =
3126
                                  full_so_path.find("//");
3127
                              if (double_slash_pos != std::string::npos) {
3128
                                // The linker has been generating bad N_SO
3129
                                // entries with doubled up paths
3130
                                // in the format "%s%s" where the first
3131
                                // string in the DW_AT_comp_dir, and the
3132
                                // second is the directory for the source
3133
                                // file so you end up with a path that looks
3134
                                // like "/tmp/src//tmp/src/"
3135
                                FileSpec so_dir(so_path);
3136
                                if (!FileSystem::Instance().Exists(so_dir)) {
3137
                                  so_dir.SetFile(
3138
                                      &full_so_path[double_slash_pos + 1],
3139
                                      FileSpec::Style::native);
3140
                                  if (FileSystem::Instance().Exists(so_dir)) {
3141
                                    // Trim off the incorrect path
3142
                                    full_so_path.erase(0, double_slash_pos + 1);
3143
                                  }
3144
                                }
3145
                              }
3146
                              if (*full_so_path.rbegin() != '/')
3147
                                full_so_path += '/';
3148
                              full_so_path += symbol_name;
3149
                              sym[sym_idx - 1].GetMangled().SetValue(
3150
                                  ConstString(full_so_path.c_str()), false);
3151
                              add_nlist = false;
3152
                              m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3153
                            }
3154
                          } else {
3155
                            // This could be a relative path to a N_SO
3156
                            N_SO_index = sym_idx;
3157
                          }
3158
                        }
3159
                        break;
3160
3161
                      case N_OSO:
3162
                        // object file name: name,,0,0,st_mtime
3163
                        type = eSymbolTypeObjectFile;
3164
                        break;
3165
3166
                      case N_LSYM:
3167
                        // local sym: name,,NO_SECT,type,offset
3168
                        type = eSymbolTypeLocal;
3169
                        break;
3170
3171
                      // INCL scopes
3172
                      case N_BINCL:
3173
                        // include file beginning: name,,NO_SECT,0,sum We use
3174
                        // the current number of symbols in the symbol table
3175
                        // in lieu of using nlist_idx in case we ever start
3176
                        // trimming entries out
3177
                        N_INCL_indexes.push_back(sym_idx);
3178
                        type = eSymbolTypeScopeBegin;
3179
                        break;
3180
3181
                      case N_EINCL:
3182
                        // include file end: name,,NO_SECT,0,0
3183
                        // Set the size of the N_BINCL to the terminating
3184
                        // index of this N_EINCL so that we can always skip
3185
                        // the entire symbol if we need to navigate more
3186
                        // quickly at the source level when parsing STABS
3187
                        if (!N_INCL_indexes.empty()) {
3188
                          symbol_ptr =
3189
                              symtab.SymbolAtIndex(N_INCL_indexes.back());
3190
                          symbol_ptr->SetByteSize(sym_idx + 1);
3191
                          symbol_ptr->SetSizeIsSibling(true);
3192
                          N_INCL_indexes.pop_back();
3193
                        }
3194
                        type = eSymbolTypeScopeEnd;
3195
                        break;
3196
3197
                      case N_SOL:
3198
                        // #included file name: name,,n_sect,0,address
3199
                        type = eSymbolTypeHeaderFile;
3200
3201
                        // We currently don't use the header files on darwin
3202
                        add_nlist = false;
3203
                        break;
3204
3205
                      case N_PARAMS:
3206
                        // compiler parameters: name,,NO_SECT,0,0
3207
                        type = eSymbolTypeCompiler;
3208
                        break;
3209
3210
                      case N_VERSION:
3211
                        // compiler version: name,,NO_SECT,0,0
3212
                        type = eSymbolTypeCompiler;
3213
                        break;
3214
3215
                      case N_OLEVEL:
3216
                        // compiler -O level: name,,NO_SECT,0,0
3217
                        type = eSymbolTypeCompiler;
3218
                        break;
3219
3220
                      case N_PSYM:
3221
                        // parameter: name,,NO_SECT,type,offset
3222
                        type = eSymbolTypeVariable;
3223
                        break;
3224
3225
                      case N_ENTRY:
3226
                        // alternate entry: name,,n_sect,linenumber,address
3227
                        symbol_section = section_info.GetSection(nlist.n_sect,
3228
                                                                 nlist.n_value);
3229
                        type = eSymbolTypeLineEntry;
3230
                        break;
3231
3232
                      // Left and Right Braces
3233
                      case N_LBRAC:
3234
                        // left bracket: 0,,NO_SECT,nesting level,address We
3235
                        // use the current number of symbols in the symbol
3236
                        // table in lieu of using nlist_idx in case we ever
3237
                        // start trimming entries out
3238
                        symbol_section = section_info.GetSection(nlist.n_sect,
3239
                                                                 nlist.n_value);
3240
                        N_BRAC_indexes.push_back(sym_idx);
3241
                        type = eSymbolTypeScopeBegin;
3242
                        break;
3243
3244
                      case N_RBRAC:
3245
                        // right bracket: 0,,NO_SECT,nesting level,address
3246
                        // Set the size of the N_LBRAC to the terminating
3247
                        // index of this N_RBRAC so that we can always skip
3248
                        // the entire symbol if we need to navigate more
3249
                        // quickly at the source level when parsing STABS
3250
                        symbol_section = section_info.GetSection(nlist.n_sect,
3251
                                                                 nlist.n_value);
3252
                        if (!N_BRAC_indexes.empty()) {
3253
                          symbol_ptr =
3254
                              symtab.SymbolAtIndex(N_BRAC_indexes.back());
3255
                          symbol_ptr->SetByteSize(sym_idx + 1);
3256
                          symbol_ptr->SetSizeIsSibling(true);
3257
                          N_BRAC_indexes.pop_back();
3258
                        }
3259
                        type = eSymbolTypeScopeEnd;
3260
                        break;
3261
3262
                      case N_EXCL:
3263
                        // deleted include file: name,,NO_SECT,0,sum
3264
                        type = eSymbolTypeHeaderFile;
3265
                        break;
3266
3267
                      // COMM scopes
3268
                      case N_BCOMM:
3269
                        // begin common: name,,NO_SECT,0,0
3270
                        // We use the current number of symbols in the symbol
3271
                        // table in lieu of using nlist_idx in case we ever
3272
                        // start trimming entries out
3273
                        type = eSymbolTypeScopeBegin;
3274
                        N_COMM_indexes.push_back(sym_idx);
3275
                        break;
3276
3277
                      case N_ECOML:
3278
                        // end common (local name): 0,,n_sect,0,address
3279
                        symbol_section = section_info.GetSection(nlist.n_sect,
3280
                                                                 nlist.n_value);
3281
                        // Fall through
3282
3283
                      case N_ECOMM:
3284
                        // end common: name,,n_sect,0,0
3285
                        // Set the size of the N_BCOMM to the terminating
3286
                        // index of this N_ECOMM/N_ECOML so that we can
3287
                        // always skip the entire symbol if we need to
3288
                        // navigate more quickly at the source level when
3289
                        // parsing STABS
3290
                        if (!N_COMM_indexes.empty()) {
3291
                          symbol_ptr =
3292
                              symtab.SymbolAtIndex(N_COMM_indexes.back());
3293
                          symbol_ptr->SetByteSize(sym_idx + 1);
3294
                          symbol_ptr->SetSizeIsSibling(true);
3295
                          N_COMM_indexes.pop_back();
3296
                        }
3297
                        type = eSymbolTypeScopeEnd;
3298
                        break;
3299
3300
                      case N_LENG:
3301
                        // second stab entry with length information
3302
                        type = eSymbolTypeAdditional;
3303
                        break;
3304
3305
                      default:
3306
                        break;
3307
                      }
3308
                    } else {
3309
                      // uint8_t n_pext    = N_PEXT & nlist.n_type;
3310
                      uint8_t n_type = N_TYPE & nlist.n_type;
3311
                      sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
3312
3313
                      switch (n_type) {
3314
                      case N_INDR: {
3315
                        const char *reexport_name_cstr =
3316
                            strtab_data.PeekCStr(nlist.n_value);
3317
                        if (reexport_name_cstr && reexport_name_cstr[0]) {
3318
                          type = eSymbolTypeReExported;
3319
                          ConstString reexport_name(
3320
                              reexport_name_cstr +
3321
                              ((reexport_name_cstr[0] == '_') ? 1 : 0));
3322
                          sym[sym_idx].SetReExportedSymbolName(reexport_name);
3323
                          set_value = false;
3324
                          reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3325
                          indirect_symbol_names.insert(ConstString(
3326
                              symbol_name + ((symbol_name[0] == '_') ? 1 : 0)));
3327
                        } else
3328
                          type = eSymbolTypeUndefined;
3329
                      } break;
3330
3331
                      case N_UNDF:
3332
                        if (symbol_name && symbol_name[0]) {
3333
                          ConstString undefined_name(
3334
                              symbol_name + ((symbol_name[0] == '_') ? 1 : 0));
3335
                          undefined_name_to_desc[undefined_name] = nlist.n_desc;
3336
                        }
3337
                      // Fall through
3338
                      case N_PBUD:
3339
                        type = eSymbolTypeUndefined;
3340
                        break;
3341
3342
                      case N_ABS:
3343
                        type = eSymbolTypeAbsolute;
3344
                        break;
3345
3346
                      case N_SECT: {
3347
                        symbol_section = section_info.GetSection(nlist.n_sect,
3348
                                                                 nlist.n_value);
3349
3350
                        if (symbol_section == NULL) {
3351
                          // TODO: warn about this?
3352
                          add_nlist = false;
3353
                          break;
3354
                        }
3355
3356
                        if (TEXT_eh_frame_sectID == nlist.n_sect) {
3357
                          type = eSymbolTypeException;
3358
                        } else {
3359
                          uint32_t section_type =
3360
                              symbol_section->Get() & SECTION_TYPE;
3361
3362
                          switch (section_type) {
3363
                          case S_CSTRING_LITERALS:
3364
                            type = eSymbolTypeData;
3365
                            break; // section with only literal C strings
3366
                          case S_4BYTE_LITERALS:
3367
                            type = eSymbolTypeData;
3368
                            break; // section with only 4 byte literals
3369
                          case S_8BYTE_LITERALS:
3370
                            type = eSymbolTypeData;
3371
                            break; // section with only 8 byte literals
3372
                          case S_LITERAL_POINTERS:
3373
                            type = eSymbolTypeTrampoline;
3374
                            break; // section with only pointers to literals
3375
                          case S_NON_LAZY_SYMBOL_POINTERS:
3376
                            type = eSymbolTypeTrampoline;
3377
                            break; // section with only non-lazy symbol
3378
                                   // pointers
3379
                          case S_LAZY_SYMBOL_POINTERS:
3380
                            type = eSymbolTypeTrampoline;
3381
                            break; // section with only lazy symbol pointers
3382
                          case S_SYMBOL_STUBS:
3383
                            type = eSymbolTypeTrampoline;
3384
                            break; // section with only symbol stubs, byte
3385
                                   // size of stub in the reserved2 field
3386
                          case S_MOD_INIT_FUNC_POINTERS:
3387
                            type = eSymbolTypeCode;
3388
                            break; // section with only function pointers for
3389
                                   // initialization
3390
                          case S_MOD_TERM_FUNC_POINTERS:
3391
                            type = eSymbolTypeCode;
3392
                            break; // section with only function pointers for
3393
                                   // termination
3394
                          case S_INTERPOSING:
3395
                            type = eSymbolTypeTrampoline;
3396
                            break; // section with only pairs of function
3397
                                   // pointers for interposing
3398
                          case S_16BYTE_LITERALS:
3399
                            type = eSymbolTypeData;
3400
                            break; // section with only 16 byte literals
3401
                          case S_DTRACE_DOF:
3402
                            type = eSymbolTypeInstrumentation;
3403
                            break;
3404
                          case S_LAZY_DYLIB_SYMBOL_POINTERS:
3405
                            type = eSymbolTypeTrampoline;
3406
                            break;
3407
                          default:
3408
                            switch (symbol_section->GetType()) {
3409
                            case lldb::eSectionTypeCode:
3410
                              type = eSymbolTypeCode;
3411
                              break;
3412
                            case eSectionTypeData:
3413
                            case eSectionTypeDataCString: // Inlined C string
3414
                                                          // data
3415
                            case eSectionTypeDataCStringPointers: // Pointers
3416
                                                                  // to C
3417
                                                                  // string
3418
                                                                  // data
3419
                            case eSectionTypeDataSymbolAddress:   // Address of
3420
                                                                  // a symbol in
3421
                                                                  // the symbol
3422
                                                                  // table
3423
                            case eSectionTypeData4:
3424
                            case eSectionTypeData8:
3425
                            case eSectionTypeData16:
3426
                              type = eSymbolTypeData;
3427
                              break;
3428
                            default:
3429
                              break;
3430
                            }
3431
                            break;
3432
                          }
3433
3434
                          if (type == eSymbolTypeInvalid) {
3435
                            const char *symbol_sect_name =
3436
                                symbol_section->GetName().AsCString();
3437
                            if (symbol_section->IsDescendant(
3438
                                    text_section_sp.get())) {
3439
                              if (symbol_section->IsClear(
3440
                                      S_ATTR_PURE_INSTRUCTIONS |
3441
                                      S_ATTR_SELF_MODIFYING_CODE |
3442
                                      S_ATTR_SOME_INSTRUCTIONS))
3443
                                type = eSymbolTypeData;
3444
                              else
3445
                                type = eSymbolTypeCode;
3446
                            } else if (symbol_section->IsDescendant(
3447
                                           data_section_sp.get()) ||
3448
                                       symbol_section->IsDescendant(
3449
                                           data_dirty_section_sp.get()) ||
3450
                                       symbol_section->IsDescendant(
3451
                                           data_const_section_sp.get())) {
3452
                              if (symbol_sect_name &&
3453
                                  ::strstr(symbol_sect_name, "__objc") ==
3454
                                      symbol_sect_name) {
3455
                                type = eSymbolTypeRuntime;
3456
3457
                                if (symbol_name) {
3458
                                  llvm::StringRef symbol_name_ref(symbol_name);
3459
                                  if (symbol_name_ref.startswith("_OBJC_")) {
3460
                                    llvm::StringRef
3461
                                        g_objc_v2_prefix_class(
3462
                                            "_OBJC_CLASS_$_");
3463
                                    llvm::StringRef
3464
                                        g_objc_v2_prefix_metaclass(
3465
                                            "_OBJC_METACLASS_$_");
3466
                                    llvm::StringRef
3467
                                        g_objc_v2_prefix_ivar("_OBJC_IVAR_$_");
3468
                                    if (symbol_name_ref.startswith(
3469
                                            g_objc_v2_prefix_class)) {
3470
                                      symbol_name_non_abi_mangled =
3471
                                          symbol_name + 1;
3472
                                      symbol_name =
3473
                                          symbol_name +
3474
                                          g_objc_v2_prefix_class.size();
3475
                                      type = eSymbolTypeObjCClass;
3476
                                      demangled_is_synthesized = true;
3477
                                    } else if (
3478
                                        symbol_name_ref.startswith(
3479
                                            g_objc_v2_prefix_metaclass)) {
3480
                                      symbol_name_non_abi_mangled =
3481
                                          symbol_name + 1;
3482
                                      symbol_name =
3483
                                          symbol_name +
3484
                                          g_objc_v2_prefix_metaclass.size();
3485
                                      type = eSymbolTypeObjCMetaClass;
3486
                                      demangled_is_synthesized = true;
3487
                                    } else if (symbol_name_ref.startswith(
3488
                                                   g_objc_v2_prefix_ivar)) {
3489
                                      symbol_name_non_abi_mangled =
3490
                                          symbol_name + 1;
3491
                                      symbol_name =
3492
                                          symbol_name +
3493
                                          g_objc_v2_prefix_ivar.size();
3494
                                      type = eSymbolTypeObjCIVar;
3495
                                      demangled_is_synthesized = true;
3496
                                    }
3497
                                  }
3498
                                }
3499
                              } else if (symbol_sect_name &&
3500
                                         ::strstr(symbol_sect_name,
3501
                                                  "__gcc_except_tab") ==
3502
                                             symbol_sect_name) {
3503
                                type = eSymbolTypeException;
3504
                              } else {
3505
                                type = eSymbolTypeData;
3506
                              }
3507
                            } else if (symbol_sect_name &&
3508
                                       ::strstr(symbol_sect_name, "__IMPORT") ==
3509
                                           symbol_sect_name) {
3510
                              type = eSymbolTypeTrampoline;
3511
                            } else if (symbol_section->IsDescendant(
3512
                                           objc_section_sp.get())) {
3513
                              type = eSymbolTypeRuntime;
3514
                              if (symbol_name && symbol_name[0] == '.') {
3515
                                llvm::StringRef symbol_name_ref(symbol_name);
3516
                                llvm::StringRef
3517
                                    g_objc_v1_prefix_class(".objc_class_name_");
3518
                                if (symbol_name_ref.startswith(
3519
                                        g_objc_v1_prefix_class)) {
3520
                                  symbol_name_non_abi_mangled = symbol_name;
3521
                                  symbol_name = symbol_name +
3522
                                                g_objc_v1_prefix_class.size();
3523
                                  type = eSymbolTypeObjCClass;
3524
                                  demangled_is_synthesized = true;
3525
                                }
3526
                              }
3527
                            }
3528
                          }
3529
                        }
3530
                      } break;
3531
                      }
3532
                    }
3533
3534
                    if (add_nlist) {
3535
                      uint64_t symbol_value = nlist.n_value;
3536
                      if (symbol_name_non_abi_mangled) {
3537
                        sym[sym_idx].GetMangled().SetMangledName(
3538
                            ConstString(symbol_name_non_abi_mangled));
3539
                        sym[sym_idx].GetMangled().SetDemangledName(
3540
                            ConstString(symbol_name));
3541
                      } else {
3542
                        bool symbol_name_is_mangled = false;
3543
3544
                        if (symbol_name && symbol_name[0] == '_') {
3545
                          symbol_name_is_mangled = symbol_name[1] == '_';
3546
                          symbol_name++; // Skip the leading underscore
3547
                        }
3548
3549
                        if (symbol_name) {
3550
                          ConstString const_symbol_name(symbol_name);
3551
                          sym[sym_idx].GetMangled().SetValue(
3552
                              const_symbol_name, symbol_name_is_mangled);
3553
                          if (is_gsym && is_debug) {
3554
                            const char *gsym_name =
3555
                                sym[sym_idx]
3556
                                    .GetMangled()
3557
                                    .GetName(Mangled::ePreferMangled)
3558
                                    .GetCString();
3559
                            if (gsym_name)
3560
                              N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
3561
                          }
3562
                        }
3563
                      }
3564
                      if (symbol_section) {
3565
                        const addr_t section_file_addr =
3566
                            symbol_section->GetFileAddress();
3567
                        if (symbol_byte_size == 0 &&
3568
                            function_starts_count > 0) {
3569
                          addr_t symbol_lookup_file_addr = nlist.n_value;
3570
                          // Do an exact address match for non-ARM addresses,
3571
                          // else get the closest since the symbol might be a
3572
                          // thumb symbol which has an address with bit zero
3573
                          // set
3574
                          FunctionStarts::Entry *func_start_entry =
3575
                              function_starts.FindEntry(symbol_lookup_file_addr,
3576
                                                        !is_arm);
3577
                          if (is_arm && func_start_entry) {
3578
                            // Verify that the function start address is the
3579
                            // symbol address (ARM) or the symbol address + 1
3580
                            // (thumb)
3581
                            if (func_start_entry->addr !=
3582
                                    symbol_lookup_file_addr &&
3583
                                func_start_entry->addr !=
3584
                                    (symbol_lookup_file_addr + 1)) {
3585
                              // Not the right entry, NULL it out...
3586
                              func_start_entry = NULL;
3587
                            }
3588
                          }
3589
                          if (func_start_entry) {
3590
                            func_start_entry->data = true;
3591
3592
                            addr_t symbol_file_addr = func_start_entry->addr;
3593
                            uint32_t symbol_flags = 0;
3594
                            if (is_arm) {
3595
                              if (symbol_file_addr & 1)
3596
                                symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3597
                              symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
3598
                            }
3599
3600
                            const FunctionStarts::Entry *next_func_start_entry =
3601
                                function_starts.FindNextEntry(func_start_entry);
3602
                            const addr_t section_end_file_addr =
3603
                                section_file_addr +
3604
                                symbol_section->GetByteSize();
3605
                            if (next_func_start_entry) {
3606
                              addr_t next_symbol_file_addr =
3607
                                  next_func_start_entry->addr;
3608
                              // Be sure the clear the Thumb address bit when
3609
                              // we calculate the size from the current and
3610
                              // next address
3611
                              if (is_arm)
3612
                                next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
3613
                              symbol_byte_size = std::min<lldb::addr_t>(
3614
                                  next_symbol_file_addr - symbol_file_addr,
3615
                                  section_end_file_addr - symbol_file_addr);
3616
                            } else {
3617
                              symbol_byte_size =
3618
                                  section_end_file_addr - symbol_file_addr;
3619
                            }
3620
                          }
3621
                        }
3622
                        symbol_value -= section_file_addr;
3623
                      }
3624
3625
                      if (is_debug == false) {
3626
                        if (type == eSymbolTypeCode) {
3627
                          // See if we can find a N_FUN entry for any code
3628
                          // symbols. If we do find a match, and the name
3629
                          // matches, then we can merge the two into just the
3630
                          // function symbol to avoid duplicate entries in
3631
                          // the symbol table
3632
                          auto range =
3633
                              N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3634
                          if (range.first != range.second) {
3635
                            bool found_it = false;
3636
                            for (auto pos = range.first; pos != range.second;
3637
                                 ++pos) {
3638
                              if (sym[sym_idx].GetMangled().GetName(
3639
                                      Mangled::ePreferMangled) ==
3640
                                  sym[pos->second].GetMangled().GetName(
3641
                                      Mangled::ePreferMangled)) {
3642
                                m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3643
                                // We just need the flags from the linker
3644
                                // symbol, so put these flags
3645
                                // into the N_FUN flags to avoid duplicate
3646
                                // symbols in the symbol table
3647
                                sym[pos->second].SetExternal(
3648
                                    sym[sym_idx].IsExternal());
3649
                                sym[pos->second].SetFlags(nlist.n_type << 16 |
3650
                                                          nlist.n_desc);
3651
                                if (resolver_addresses.find(nlist.n_value) !=
3652
                                    resolver_addresses.end())
3653
                                  sym[pos->second].SetType(eSymbolTypeResolver);
3654
                                sym[sym_idx].Clear();
3655
                                found_it = true;
3656
                                break;
3657
                              }
3658
                            }
3659
                            if (found_it)
3660
                              continue;
3661
                          } else {
3662
                            if (resolver_addresses.find(nlist.n_value) !=
3663
                                resolver_addresses.end())
3664
                              type = eSymbolTypeResolver;
3665
                          }
3666
                        } else if (type == eSymbolTypeData ||
3667
                                   type == eSymbolTypeObjCClass ||
3668
                                   type == eSymbolTypeObjCMetaClass ||
3669
                                   type == eSymbolTypeObjCIVar) {
3670
                          // See if we can find a N_STSYM entry for any data
3671
                          // symbols. If we do find a match, and the name
3672
                          // matches, then we can merge the two into just the
3673
                          // Static symbol to avoid duplicate entries in the
3674
                          // symbol table
3675
                          auto range = N_STSYM_addr_to_sym_idx.equal_range(
3676
                              nlist.n_value);
3677
                          if (range.first != range.second) {
3678
                            bool found_it = false;
3679
                            for (auto pos = range.first; pos != range.second;
3680
                                 ++pos) {
3681
                              if (sym[sym_idx].GetMangled().GetName(
3682
                                      Mangled::ePreferMangled) ==
3683
                                  sym[pos->second].GetMangled().GetName(
3684
                                      Mangled::ePreferMangled)) {
3685
                                m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3686
                                // We just need the flags from the linker
3687
                                // symbol, so put these flags
3688
                                // into the N_STSYM flags to avoid duplicate
3689
                                // symbols in the symbol table
3690
                                sym[pos->second].SetExternal(
3691
                                    sym[sym_idx].IsExternal());
3692
                                sym[pos->second].SetFlags(nlist.n_type << 16 |
3693
                                                          nlist.n_desc);
3694
                                sym[sym_idx].Clear();
3695
                                found_it = true;
3696
                                break;
3697
                              }
3698
                            }
3699
                            if (found_it)
3700
                              continue;
3701
                          } else {
3702
                            const char *gsym_name =
3703
                                sym[sym_idx]
3704
                                    .GetMangled()
3705
                                    .GetName(Mangled::ePreferMangled)
3706
                                    .GetCString();
3707
                            if (gsym_name) {
3708
                              // Combine N_GSYM stab entries with the non
3709
                              // stab symbol
3710
                              ConstNameToSymbolIndexMap::const_iterator pos =
3711
                                  N_GSYM_name_to_sym_idx.find(gsym_name);
3712
                              if (pos != N_GSYM_name_to_sym_idx.end()) {
3713
                                const uint32_t GSYM_sym_idx = pos->second;
3714
                                m_nlist_idx_to_sym_idx[nlist_idx] =
3715
                                    GSYM_sym_idx;
3716
                                // Copy the address, because often the N_GSYM
3717
                                // address has an invalid address of zero
3718
                                // when the global is a common symbol
3719
                                sym[GSYM_sym_idx].GetAddressRef().SetSection(
3720
                                    symbol_section);
3721
                                sym[GSYM_sym_idx].GetAddressRef().SetOffset(
3722
                                    symbol_value);
3723
                                add_symbol_addr(sym[GSYM_sym_idx]
3724
                                                    .GetAddress()
3725
                                                    .GetFileAddress());
3726
                                // We just need the flags from the linker
3727
                                // symbol, so put these flags
3728
                                // into the N_GSYM flags to avoid duplicate
3729
                                // symbols in the symbol table
3730
                                sym[GSYM_sym_idx].SetFlags(nlist.n_type << 16 |
3731
                                                           nlist.n_desc);
3732
                                sym[sym_idx].Clear();
3733
                                continue;
3734
                              }
3735
                            }
3736
                          }
3737
                        }
3738
                      }
3739
3740
                      sym[sym_idx].SetID(nlist_idx);
3741
                      sym[sym_idx].SetType(type);
3742
                      if (set_value) {
3743
                        sym[sym_idx].GetAddressRef().SetSection(symbol_section);
3744
                        sym[sym_idx].GetAddressRef().SetOffset(symbol_value);
3745
                        add_symbol_addr(
3746
                            sym[sym_idx].GetAddress().GetFileAddress());
3747
                      }
3748
                      sym[sym_idx].SetFlags(nlist.n_type << 16 | nlist.n_desc);
3749
3750
                      if (symbol_byte_size > 0)
3751
                        sym[sym_idx].SetByteSize(symbol_byte_size);
3752
3753
                      if (demangled_is_synthesized)
3754
                        sym[sym_idx].SetDemangledNameIsSynthesized(true);
3755
                      ++sym_idx;
3756
                    } else {
3757
                      sym[sym_idx].Clear();
3758
                    }
3759
                  }
3760
                  /////////////////////////////
3761
                }
3762
                break; // No more entries to consider
3763
              }
3764
            }
3765
3766
            for (const auto &pos : reexport_shlib_needs_fixup) {
3767
              const auto undef_pos = undefined_name_to_desc.find(pos.second);
3768
              if (undef_pos != undefined_name_to_desc.end()) {
3769
                const uint8_t dylib_ordinal =
3770
                    llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
3771
                if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
3772
                  sym[pos.first].SetReExportedSymbolSharedLibrary(
3773
                      dylib_files.GetFileSpecAtIndex(dylib_ordinal - 1));
3774
              }
3775
            }
3776
          }
3777
        }
3778
      }
3779
    }
3780
  }
3781
3782
  // Must reset this in case it was mutated above!
3783
  nlist_data_offset = 0;
3784
#endif
3785
3786
135k
  if (nlist_data.GetByteSize() > 0) {
3787
3788
    // If the sym array was not created while parsing the DSC unmapped
3789
    // symbols, create it now.
3790
135k
    if (sym == nullptr) {
3791
135k
      sym =
3792
135k
          symtab.Resize(symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
3793
135k
      num_syms = symtab.GetNumSymbols();
3794
135k
    }
3795
3796
135k
    if (unmapped_local_symbols_found) {
3797
0
      assert(m_dysymtab.ilocalsym == 0);
3798
0
      nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
3799
0
      nlist_idx = m_dysymtab.nlocalsym;
3800
135k
    } else {
3801
135k
      nlist_idx = 0;
3802
135k
    }
3803
3804
0
    typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap;
3805
135k
    typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName;
3806
135k
    UndefinedNameToDescMap undefined_name_to_desc;
3807
135k
    SymbolIndexToName reexport_shlib_needs_fixup;
3808
3809
    // Symtab parsing is a huge mess. Everything is entangled and the code
3810
    // requires access to a ridiculous amount of variables. LLDB depends
3811
    // heavily on the proper merging of symbols and to get that right we need
3812
    // to make sure we have parsed all the debug symbols first. Therefore we
3813
    // invoke the lambda twice, once to parse only the debug symbols and then
3814
    // once more to parse the remaining symbols.
3815
135k
    auto ParseSymbolLambda = [&](struct nlist_64 &nlist, uint32_t nlist_idx,
3816
318M
                                 bool debug_only) {
3817
318M
      const bool is_debug = ((nlist.n_type & N_STAB) != 0);
3818
318M
      if (is_debug != debug_only)
3819
159M
        return true;
3820
3821
159M
      const char *symbol_name_non_abi_mangled = nullptr;
3822
159M
      const char *symbol_name = nullptr;
3823
3824
159M
      if (have_strtab_data) {
3825
159M
        symbol_name = strtab_data.PeekCStr(nlist.n_strx);
3826
3827
159M
        if (symbol_name == nullptr) {
3828
          // No symbol should be NULL, even the symbols with no string values
3829
          // should have an offset zero which points to an empty C-string
3830
0
          Host::SystemLog(Host::eSystemLogError,
3831
0
                          "error: symbol[%u] has invalid string table offset "
3832
0
                          "0x%x in %s, ignoring symbol\n",
3833
0
                          nlist_idx, nlist.n_strx,
3834
0
                          module_sp->GetFileSpec().GetPath().c_str());
3835
0
          return true;
3836
0
        }
3837
159M
        if (symbol_name[0] == '\0')
3838
217k
          symbol_name = nullptr;
3839
159M
      } else {
3840
0
        const addr_t str_addr = strtab_addr + nlist.n_strx;
3841
0
        Status str_error;
3842
0
        if (process->ReadCStringFromMemory(str_addr, memory_symbol_name,
3843
0
                                           str_error))
3844
0
          symbol_name = memory_symbol_name.c_str();
3845
0
      }
3846
3847
159M
      SymbolType type = eSymbolTypeInvalid;
3848
159M
      SectionSP symbol_section;
3849
159M
      lldb::addr_t symbol_byte_size = 0;
3850
159M
      bool add_nlist = true;
3851
159M
      bool is_gsym = false;
3852
159M
      bool demangled_is_synthesized = false;
3853
159M
      bool set_value = true;
3854
3855
159M
      assert(sym_idx < num_syms);
3856
0
      sym[sym_idx].SetDebug(is_debug);
3857
3858
159M
      if (is_debug) {
3859
313k
        switch (nlist.n_type) {
3860
5.53k
        case N_GSYM:
3861
          // global symbol: name,,NO_SECT,type,0
3862
          // Sometimes the N_GSYM value contains the address.
3863
3864
          // FIXME: In the .o files, we have a GSYM and a debug symbol for all
3865
          // the ObjC data.  They
3866
          // have the same address, but we want to ensure that we always find
3867
          // only the real symbol, 'cause we don't currently correctly
3868
          // attribute the GSYM one to the ObjCClass/Ivar/MetaClass symbol
3869
          // type.  This is a temporary hack to make sure the ObjectiveC
3870
          // symbols get treated correctly.  To do this right, we should
3871
          // coalesce all the GSYM & global symbols that have the same
3872
          // address.
3873
5.53k
          is_gsym = true;
3874
5.53k
          sym[sym_idx].SetExternal(true);
3875
3876
5.53k
          if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O') {
3877
1.93k
            llvm::StringRef symbol_name_ref(symbol_name);
3878
1.93k
            if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) {
3879
568
              symbol_name_non_abi_mangled = symbol_name + 1;
3880
568
              symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3881
568
              type = eSymbolTypeObjCClass;
3882
568
              demangled_is_synthesized = true;
3883
3884
1.36k
            } else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass)) {
3885
568
              symbol_name_non_abi_mangled = symbol_name + 1;
3886
568
              symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3887
568
              type = eSymbolTypeObjCMetaClass;
3888
568
              demangled_is_synthesized = true;
3889
797
            } else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar)) {
3890
788
              symbol_name_non_abi_mangled = symbol_name + 1;
3891
788
              symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3892
788
              type = eSymbolTypeObjCIVar;
3893
788
              demangled_is_synthesized = true;
3894
788
            }
3895
3.60k
          } else {
3896
3.60k
            if (nlist.n_value != 0)
3897
0
              symbol_section =
3898
0
                  section_info.GetSection(nlist.n_sect, nlist.n_value);
3899
3.60k
            type = eSymbolTypeData;
3900
3.60k
          }
3901
5.53k
          break;
3902
3903
0
        case N_FNAME:
3904
          // procedure name (f77 kludge): name,,NO_SECT,0,0
3905
0
          type = eSymbolTypeCompiler;
3906
0
          break;
3907
3908
142k
        case N_FUN:
3909
          // procedure: name,,n_sect,linenumber,address
3910
142k
          if (symbol_name) {
3911
71.4k
            type = eSymbolTypeCode;
3912
71.4k
            symbol_section =
3913
71.4k
                section_info.GetSection(nlist.n_sect, nlist.n_value);
3914
3915
71.4k
            N_FUN_addr_to_sym_idx.insert(
3916
71.4k
                std::make_pair(nlist.n_value, sym_idx));
3917
            // We use the current number of symbols in the symbol table in
3918
            // lieu of using nlist_idx in case we ever start trimming entries
3919
            // out
3920
71.4k
            N_FUN_indexes.push_back(sym_idx);
3921
71.4k
          } else {
3922
71.4k
            type = eSymbolTypeCompiler;
3923
3924
71.4k
            if (!N_FUN_indexes.empty()) {
3925
              // Copy the size of the function into the original STAB entry
3926
              // so we don't have to hunt for it later
3927
71.4k
              symtab.SymbolAtIndex(N_FUN_indexes.back())
3928
71.4k
                  ->SetByteSize(nlist.n_value);
3929
71.4k
              N_FUN_indexes.pop_back();
3930
              // We don't really need the end function STAB as it contains
3931
              // the size which we already placed with the original symbol,
3932
              // so don't add it if we want a minimal symbol table
3933
71.4k
              add_nlist = false;
3934
71.4k
            }
3935
71.4k
          }
3936
142k
          break;
3937
3938
8.23k
        case N_STSYM:
3939
          // static symbol: name,,n_sect,type,address
3940
8.23k
          N_STSYM_addr_to_sym_idx.insert(
3941
8.23k
              std::make_pair(nlist.n_value, sym_idx));
3942
8.23k
          symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
3943
8.23k
          if (symbol_name && symbol_name[0]) {
3944
8.23k
            type = ObjectFile::GetSymbolTypeFromName(symbol_name + 1,
3945
8.23k
                                                     eSymbolTypeData);
3946
8.23k
          }
3947
8.23k
          break;
3948
3949
0
        case N_LCSYM:
3950
          // .lcomm symbol: name,,n_sect,type,address
3951
0
          symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
3952
0
          type = eSymbolTypeCommonBlock;
3953
0
          break;
3954
3955
71.4k
        case N_BNSYM:
3956
          // We use the current number of symbols in the symbol table in lieu
3957
          // of using nlist_idx in case we ever start trimming entries out
3958
          // Skip these if we want minimal symbol tables
3959
71.4k
          add_nlist = false;
3960
71.4k
          break;
3961
3962
71.4k
        case N_ENSYM:
3963
          // Set the size of the N_BNSYM to the terminating index of this
3964
          // N_ENSYM so that we can always skip the entire symbol if we need
3965
          // to navigate more quickly at the source level when parsing STABS
3966
          // Skip these if we want minimal symbol tables
3967
71.4k
          add_nlist = false;
3968
71.4k
          break;
3969
3970
18
        case N_OPT:
3971
          // emitted with gcc2_compiled and in gcc source
3972
18
          type = eSymbolTypeCompiler;
3973
18
          break;
3974
3975
0
        case N_RSYM:
3976
          // register sym: name,,NO_SECT,type,register
3977
0
          type = eSymbolTypeVariable;
3978
0
          break;
3979
3980
0
        case N_SLINE:
3981
          // src line: 0,,n_sect,linenumber,address
3982
0
          symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
3983
0
          type = eSymbolTypeLineEntry;
3984
0
          break;
3985
3986
0
        case N_SSYM:
3987
          // structure elt: name,,NO_SECT,type,struct_offset
3988
0
          type = eSymbolTypeVariableType;
3989
0
          break;
3990
3991
10.4k
        case N_SO:
3992
          // source file name
3993
10.4k
          type = eSymbolTypeSourceFile;
3994
10.4k
          if (symbol_name == nullptr) {
3995
3.48k
            add_nlist = false;
3996
3.48k
            if (N_SO_index != UINT32_MAX) {
3997
              // Set the size of the N_SO to the terminating index of this
3998
              // N_SO so that we can always skip the entire N_SO if we need
3999
              // to navigate more quickly at the source level when parsing
4000
              // STABS
4001
3.48k
              symbol_ptr = symtab.SymbolAtIndex(N_SO_index);
4002
3.48k
              symbol_ptr->SetByteSize(sym_idx);
4003
3.48k
              symbol_ptr->SetSizeIsSibling(true);
4004
3.48k
            }
4005
3.48k
            N_NSYM_indexes.clear();
4006
3.48k
            N_INCL_indexes.clear();
4007
3.48k
            N_BRAC_indexes.clear();
4008
3.48k
            N_COMM_indexes.clear();
4009
3.48k
            N_FUN_indexes.clear();
4010
3.48k
            N_SO_index = UINT32_MAX;
4011
6.97k
          } else {
4012
            // We use the current number of symbols in the symbol table in
4013
            // lieu of using nlist_idx in case we ever start trimming entries
4014
            // out
4015
6.97k
            const bool N_SO_has_full_path = symbol_name[0] == '/';
4016
6.97k
            if (N_SO_has_full_path) {
4017
3.48k
              if ((N_SO_index == sym_idx - 1) && 
((sym_idx - 1) < num_syms)2.76k
) {
4018
                // We have two consecutive N_SO entries where the first
4019
                // contains a directory and the second contains a full path.
4020
0
                sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name),
4021
0
                                                       false);
4022
0
                m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
4023
0
                add_nlist = false;
4024
3.48k
              } else {
4025
                // This is the first entry in a N_SO that contains a
4026
                // directory or a full path to the source file
4027
3.48k
                N_SO_index = sym_idx;
4028
3.48k
              }
4029
3.48k
            } else if ((N_SO_index == sym_idx - 1) &&
4030
3.48k
                       ((sym_idx - 1) < num_syms)) {
4031
              // This is usually the second N_SO entry that contains just the
4032
              // filename, so here we combine it with the first one if we are
4033
              // minimizing the symbol table
4034
3.48k
              const char *so_path =
4035
3.48k
                  sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
4036
3.48k
              if (so_path && so_path[0]) {
4037
3.48k
                std::string full_so_path(so_path);
4038
3.48k
                const size_t double_slash_pos = full_so_path.find("//");
4039
3.48k
                if (double_slash_pos != std::string::npos) {
4040
                  // The linker has been generating bad N_SO entries with
4041
                  // doubled up paths in the format "%s%s" where the first
4042
                  // string in the DW_AT_comp_dir, and the second is the
4043
                  // directory for the source file so you end up with a path
4044
                  // that looks like "/tmp/src//tmp/src/"
4045
0
                  FileSpec so_dir(so_path);
4046
0
                  if (!FileSystem::Instance().Exists(so_dir)) {
4047
0
                    so_dir.SetFile(&full_so_path[double_slash_pos + 1],
4048
0
                                   FileSpec::Style::native);
4049
0
                    if (FileSystem::Instance().Exists(so_dir)) {
4050
                      // Trim off the incorrect path
4051
0
                      full_so_path.erase(0, double_slash_pos + 1);
4052
0
                    }
4053
0
                  }
4054
0
                }
4055
3.48k
                if (*full_so_path.rbegin() != '/')
4056
0
                  full_so_path += '/';
4057
3.48k
                full_so_path += symbol_name;
4058
3.48k
                sym[sym_idx - 1].GetMangled().SetValue(
4059
3.48k
                    ConstString(full_so_path.c_str()), false);
4060
3.48k
                add_nlist = false;
4061
3.48k
                m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
4062
3.48k
              }
4063
3.48k
            } else {
4064
              // This could be a relative path to a N_SO
4065
0
              N_SO_index = sym_idx;
4066
0
            }
4067
6.97k
          }
4068
10.4k
          break;
4069
4070
3.48k
        case N_OSO:
4071
          // object file name: name,,0,0,st_mtime
4072
3.48k
          type = eSymbolTypeObjectFile;
4073
3.48k
          break;
4074
4075
0
        case N_LSYM:
4076
          // local sym: name,,NO_SECT,type,offset
4077
0
          type = eSymbolTypeLocal;
4078
0
          break;
4079
4080
        // INCL scopes
4081
0
        case N_BINCL:
4082
          // include file beginning: name,,NO_SECT,0,sum We use the current
4083
          // number of symbols in the symbol table in lieu of using nlist_idx
4084
          // in case we ever start trimming entries out
4085
0
          N_INCL_indexes.push_back(sym_idx);
4086
0
          type = eSymbolTypeScopeBegin;
4087
0
          break;
4088
4089
0
        case N_EINCL:
4090
          // include file end: name,,NO_SECT,0,0
4091
          // Set the size of the N_BINCL to the terminating index of this
4092
          // N_EINCL so that we can always skip the entire symbol if we need
4093
          // to navigate more quickly at the source level when parsing STABS
4094
0
          if (!N_INCL_indexes.empty()) {
4095
0
            symbol_ptr = symtab.SymbolAtIndex(N_INCL_indexes.back());
4096
0
            symbol_ptr->SetByteSize(sym_idx + 1);
4097
0
            symbol_ptr->SetSizeIsSibling(true);
4098
0
            N_INCL_indexes.pop_back();
4099
0
          }
4100
0
          type = eSymbolTypeScopeEnd;
4101
0
          break;
4102
4103
47
        case N_SOL:
4104
          // #included file name: name,,n_sect,0,address
4105
47
          type = eSymbolTypeHeaderFile;
4106
4107
          // We currently don't use the header files on darwin
4108
47
          add_nlist = false;
4109
47
          break;
4110
4111
0
        case N_PARAMS:
4112
          // compiler parameters: name,,NO_SECT,0,0
4113
0
          type = eSymbolTypeCompiler;
4114
0
          break;
4115
4116
0
        case N_VERSION:
4117
          // compiler version: name,,NO_SECT,0,0
4118
0
          type = eSymbolTypeCompiler;
4119
0
          break;
4120
4121
0
        case N_OLEVEL:
4122
          // compiler -O level: name,,NO_SECT,0,0
4123
0
          type = eSymbolTypeCompiler;
4124
0
          break;
4125
4126
0
        case N_PSYM:
4127
          // parameter: name,,NO_SECT,type,offset
4128
0
          type = eSymbolTypeVariable;
4129
0
          break;
4130
4131
0
        case N_ENTRY:
4132
          // alternate entry: name,,n_sect,linenumber,address
4133
0
          symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
4134
0
          type = eSymbolTypeLineEntry;
4135
0
          break;
4136
4137
        // Left and Right Braces
4138
0
        case N_LBRAC:
4139
          // left bracket: 0,,NO_SECT,nesting level,address We use the
4140
          // current number of symbols in the symbol table in lieu of using
4141
          // nlist_idx in case we ever start trimming entries out
4142
0
          symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
4143
0
          N_BRAC_indexes.push_back(sym_idx);
4144
0
          type = eSymbolTypeScopeBegin;
4145
0
          break;
4146
4147
0
        case N_RBRAC:
4148
          // right bracket: 0,,NO_SECT,nesting level,address Set the size of
4149
          // the N_LBRAC to the terminating index of this N_RBRAC so that we
4150
          // can always skip the entire symbol if we need to navigate more
4151
          // quickly at the source level when parsing STABS
4152
0
          symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
4153
0
          if (!N_BRAC_indexes.empty()) {
4154
0
            symbol_ptr = symtab.SymbolAtIndex(N_BRAC_indexes.back());
4155
0
            symbol_ptr->SetByteSize(sym_idx + 1);
4156
0
            symbol_ptr->SetSizeIsSibling(true);
4157
0
            N_BRAC_indexes.pop_back();
4158
0
          }
4159
0
          type = eSymbolTypeScopeEnd;
4160
0
          break;
4161
4162
0
        case N_EXCL:
4163
          // deleted include file: name,,NO_SECT,0,sum
4164
0
          type = eSymbolTypeHeaderFile;
4165
0
          break;
4166
4167
        // COMM scopes
4168
0
        case N_BCOMM:
4169
          // begin common: name,,NO_SECT,0,0
4170
          // We use the current number of symbols in the symbol table in lieu
4171
          // of using nlist_idx in case we ever start trimming entries out
4172
0
          type = eSymbolTypeScopeBegin;
4173
0
          N_COMM_indexes.push_back(sym_idx);
4174
0
          break;
4175
4176
0
        case N_ECOML:
4177
          // end common (local name): 0,,n_sect,0,address
4178
0
          symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
4179
0
          LLVM_FALLTHROUGH;
4180
4181
0
        case N_ECOMM:
4182
          // end common: name,,n_sect,0,0
4183
          // Set the size of the N_BCOMM to the terminating index of this
4184
          // N_ECOMM/N_ECOML so that we can always skip the entire symbol if
4185
          // we need to navigate more quickly at the source level when
4186
          // parsing STABS
4187
0
          if (!N_COMM_indexes.empty()) {
4188
0
            symbol_ptr = symtab.SymbolAtIndex(N_COMM_indexes.back());
4189
0
            symbol_ptr->SetByteSize(sym_idx + 1);
4190
0
            symbol_ptr->SetSizeIsSibling(true);
4191
0
            N_COMM_indexes.pop_back();
4192
0
          }
4193
0
          type = eSymbolTypeScopeEnd;
4194
0
          break;
4195
4196
0
        case N_LENG:
4197
          // second stab entry with length information
4198
0
          type = eSymbolTypeAdditional;
4199
0
          break;
4200
4201
1
        default:
4202
1
          break;
4203
313k
        }
4204
158M
      } else {
4205
158M
        uint8_t n_type = N_TYPE & nlist.n_type;
4206
158M
        sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
4207
4208
158M
        switch (n_type) {
4209
2.65k
        case N_INDR: {
4210
2.65k
          const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
4211
2.65k
          if (reexport_name_cstr && reexport_name_cstr[0]) {
4212
2.65k
            type = eSymbolTypeReExported;
4213
2.65k
            ConstString reexport_name(reexport_name_cstr +
4214
2.65k
                                      ((reexport_name_cstr[0] == '_') ? 1 : 
00
));
4215
2.65k
            sym[sym_idx].SetReExportedSymbolName(reexport_name);
4216
2.65k
            set_value = false;
4217
2.65k
            reexport_shlib_needs_fixup[sym_idx] = reexport_name;
4218
2.65k
            indirect_symbol_names.insert(
4219
2.65k
                ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 
00
)));
4220
2.65k
          } else
4221
0
            type = eSymbolTypeUndefined;
4222
2.65k
        } break;
4223
4224
17.5M
        case N_UNDF:
4225
17.5M
          if (symbol_name && symbol_name[0]) {
4226
17.5M
            ConstString undefined_name(symbol_name +
4227
17.5M
                                       ((symbol_name[0] == '_') ? 
117.4M
:
0117k
));
4228
17.5M
            undefined_name_to_desc[undefined_name] = nlist.n_desc;
4229
17.5M
          }
4230
17.5M
          LLVM_FALLTHROUGH;
4231
4232
17.5M
        case N_PBUD:
4233
17.5M
          type = eSymbolTypeUndefined;
4234
17.5M
          break;
4235
4236
5
        case N_ABS:
4237
5
          type = eSymbolTypeAbsolute;
4238
5
          break;
4239
4240
141M
        case N_SECT: {
4241
141M
          symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
4242
4243
141M
          if (!symbol_section) {
4244
            // TODO: warn about this?
4245
0
            add_nlist = false;
4246
0
            break;
4247
0
          }
4248
4249
141M
          if (TEXT_eh_frame_sectID == nlist.n_sect) {
4250
0
            type = eSymbolTypeException;
4251
141M
          } else {
4252
141M
            uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
4253
4254
141M
            switch (section_type) {
4255
66.2k
            case S_CSTRING_LITERALS:
4256
66.2k
              type = eSymbolTypeData;
4257
66.2k
              break; // section with only literal C strings
4258
0
            case S_4BYTE_LITERALS:
4259
0
              type = eSymbolTypeData;
4260
0
              break; // section with only 4 byte literals
4261
0
            case S_8BYTE_LITERALS:
4262
0
              type = eSymbolTypeData;
4263
0
              break; // section with only 8 byte literals
4264
5.46k
            case S_LITERAL_POINTERS:
4265
5.46k
              type = eSymbolTypeTrampoline;
4266
5.46k
              break; // section with only pointers to literals
4267
0
            case S_NON_LAZY_SYMBOL_POINTERS:
4268
0
              type = eSymbolTypeTrampoline;
4269
0
              break; // section with only non-lazy symbol pointers
4270
0
            case S_LAZY_SYMBOL_POINTERS:
4271
0
              type = eSymbolTypeTrampoline;
4272
0
              break; // section with only lazy symbol pointers
4273
0
            case S_SYMBOL_STUBS:
4274
0
              type = eSymbolTypeTrampoline;
4275
0
              break; // section with only symbol stubs, byte size of stub in
4276
                     // the reserved2 field
4277
0
            case S_MOD_INIT_FUNC_POINTERS:
4278
0
              type = eSymbolTypeCode;
4279
0
              break; // section with only function pointers for initialization
4280
0
            case S_MOD_TERM_FUNC_POINTERS:
4281
0
              type = eSymbolTypeCode;
4282
0
              break; // section with only function pointers for termination
4283
0
            case S_INTERPOSING:
4284
0
              type = eSymbolTypeTrampoline;
4285
0
              break; // section with only pairs of function pointers for
4286
                     // interposing
4287
464
            case S_16BYTE_LITERALS:
4288
464
              type = eSymbolTypeData;
4289
464
              break; // section with only 16 byte literals
4290
0
            case S_DTRACE_DOF:
4291
0
              type = eSymbolTypeInstrumentation;
4292
0
              break;
4293
0
            case S_LAZY_DYLIB_SYMBOL_POINTERS:
4294
0
              type = eSymbolTypeTrampoline;
4295
0
              break;
4296
141M
            default:
4297
141M
              switch (symbol_section->GetType()) {
4298
89.3M
              case lldb::eSectionTypeCode:
4299
89.3M
                type = eSymbolTypeCode;
4300
89.3M
                break;
4301
3.17M
              case eSectionTypeData:
4302
3.17M
              case eSectionTypeDataCString:         // Inlined C string data
4303
3.17M
              case eSectionTypeDataCStringPointers: // Pointers to C string
4304
                                                    // data
4305
3.17M
              case eSectionTypeDataSymbolAddress:   // Address of a symbol in
4306
                                                    // the symbol table
4307
3.17M
              case eSectionTypeData4:
4308
3.17M
              case eSectionTypeData8:
4309
3.17M
              case eSectionTypeData16:
4310
3.17M
                type = eSymbolTypeData;
4311
3.17M
                break;
4312
48.7M
              default:
4313
48.7M
                break;
4314
141M
              }
4315
141M
              break;
4316
141M
            }
4317
4318
141M
            if (type == eSymbolTypeInvalid) {
4319
48.7M
              const char *symbol_sect_name =
4320
48.7M
                  symbol_section->GetName().AsCString();
4321
48.7M
              if (symbol_section->IsDescendant(text_section_sp.get())) {
4322
8.45M
                if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
4323
8.45M
                                            S_ATTR_SELF_MODIFYING_CODE |
4324
8.45M
                                            S_ATTR_SOME_INSTRUCTIONS))
4325
8.45M
                  type = eSymbolTypeData;
4326
0
                else
4327
0
                  type = eSymbolTypeCode;
4328
40.2M
              } else if (symbol_section->IsDescendant(data_section_sp.get()) ||
4329
40.2M
                         symbol_section->IsDescendant(
4330
6.55M
                             data_dirty_section_sp.get()) ||
4331
40.2M
                         symbol_section->IsDescendant(
4332
34.5M
                             data_const_section_sp.get())) {
4333
34.5M
                if (symbol_sect_name &&
4334
34.5M
                    ::strstr(symbol_sect_name, "__objc") == symbol_sect_name) {
4335
9.05M
                  type = eSymbolTypeRuntime;
4336
4337
9.05M
                  if (symbol_name) {
4338
9.05M
                    llvm::StringRef symbol_name_ref(symbol_name);
4339
9.05M
                    if (symbol_name_ref.startswith("_OBJC_")) {
4340
4.28M
                      llvm::StringRef g_objc_v2_prefix_class(
4341
4.28M
                          "_OBJC_CLASS_$_");
4342
4.28M
                      llvm::StringRef g_objc_v2_prefix_metaclass(
4343
4.28M
                          "_OBJC_METACLASS_$_");
4344
4.28M
                      llvm::StringRef g_objc_v2_prefix_ivar(
4345
4.28M
                          "_OBJC_IVAR_$_");
4346
4.28M
                      if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) {
4347
733k
                        symbol_name_non_abi_mangled = symbol_name + 1;
4348
733k
                        symbol_name =
4349
733k
                            symbol_name + g_objc_v2_prefix_class.size();
4350
733k
                        type = eSymbolTypeObjCClass;
4351
733k
                        demangled_is_synthesized = true;
4352
3.55M
                      } else if (symbol_name_ref.startswith(
4353
3.55M
                                     g_objc_v2_prefix_metaclass)) {
4354
733k
                        symbol_name_non_abi_mangled = symbol_name + 1;
4355
733k
                        symbol_name =
4356
733k
                            symbol_name + g_objc_v2_prefix_metaclass.size();
4357
733k
                        type = eSymbolTypeObjCMetaClass;
4358
733k
                        demangled_is_synthesized = true;
4359
2.82M
                      } else if (symbol_name_ref.startswith(
4360
2.82M
                                     g_objc_v2_prefix_ivar)) {
4361
2.04M
                        symbol_name_non_abi_mangled = symbol_name + 1;
4362
2.04M
                        symbol_name =
4363
2.04M
                            symbol_name + g_objc_v2_prefix_ivar.size();
4364
2.04M
                        type = eSymbolTypeObjCIVar;
4365
2.04M
                        demangled_is_synthesized = true;
4366
2.04M
                      }
4367
4.28M
                    }
4368
9.05M
                  }
4369
25.5M
                } else if (symbol_sect_name &&
4370
25.5M
                           ::strstr(symbol_sect_name, "__gcc_except_tab") ==
4371
25.5M
                               symbol_sect_name) {
4372
0
                  type = eSymbolTypeException;
4373
25.5M
                } else {
4374
25.5M
                  type = eSymbolTypeData;
4375
25.5M
                }
4376
34.5M
              } else 
if (5.68M
symbol_sect_name5.68M
&&
4377
5.68M
                         ::strstr(symbol_sect_name, "__IMPORT") ==
4378
5.68M
                             symbol_sect_name) {
4379
0
                type = eSymbolTypeTrampoline;
4380
5.68M
              } else if (symbol_section->IsDescendant(objc_section_sp.get())) {
4381
0
                type = eSymbolTypeRuntime;
4382
0
                if (symbol_name && symbol_name[0] == '.') {
4383
0
                  llvm::StringRef symbol_name_ref(symbol_name);
4384
0
                  llvm::StringRef g_objc_v1_prefix_class(
4385
0
                      ".objc_class_name_");
4386
0
                  if (symbol_name_ref.startswith(g_objc_v1_prefix_class)) {
4387
0
                    symbol_name_non_abi_mangled = symbol_name;
4388
0
                    symbol_name = symbol_name + g_objc_v1_prefix_class.size();
4389
0
                    type = eSymbolTypeObjCClass;
4390
0
                    demangled_is_synthesized = true;
4391
0
                  }
4392
0
                }
4393
0
              }
4394
48.7M
            }
4395
141M
          }
4396
141M
        } break;
4397
158M
        }
4398
158M
      }
4399
4400
159M
      if (!add_nlist) {
4401
221k
        sym[sym_idx].Clear();
4402
221k
        return true;
4403
221k
      }
4404
4405
158M
      uint64_t symbol_value = nlist.n_value;
4406
4407
158M
      if (symbol_name_non_abi_mangled) {
4408
3.50M
        sym[sym_idx].GetMangled().SetMangledName(
4409
3.50M
            ConstString(symbol_name_non_abi_mangled));
4410
3.50M
        sym[sym_idx].GetMangled().SetDemangledName(ConstString(symbol_name));
4411
155M
      } else {
4412
155M
        bool symbol_name_is_mangled = false;
4413
4414
155M
        if (symbol_name && symbol_name[0] == '_') {
4415
141M
          symbol_name_is_mangled = symbol_name[1] == '_';
4416
141M
          symbol_name++; // Skip the leading underscore
4417
141M
        }
4418
4419
155M
        if (symbol_name) {
4420
155M
          ConstString const_symbol_name(symbol_name);
4421
155M
          sym[sym_idx].GetMangled().SetValue(const_symbol_name,
4422
155M
                                             symbol_name_is_mangled);
4423
155M
        }
4424
155M
      }
4425
4426
158M
      if (is_gsym) {
4427
5.53k
        const char *gsym_name = sym[sym_idx]
4428
5.53k
                                    .GetMangled()
4429
5.53k
                                    .GetName(Mangled::ePreferMangled)
4430
5.53k
                                    .GetCString();
4431
5.53k
        if (gsym_name)
4432
5.53k
          N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
4433
5.53k
      }
4434
4435
158M
      if (symbol_section) {
4436
141M
        const addr_t section_file_addr = symbol_section->GetFileAddress();
4437
141M
        if (symbol_byte_size == 0 && function_starts_count > 0) {
4438
141M
          addr_t symbol_lookup_file_addr = nlist.n_value;
4439
          // Do an exact address match for non-ARM addresses, else get the
4440
          // closest since the symbol might be a thumb symbol which has an
4441
          // address with bit zero set.
4442
141M
          FunctionStarts::Entry *func_start_entry =
4443
141M
              function_starts.FindEntry(symbol_lookup_file_addr, !is_arm);
4444
141M
          if (is_arm && 
func_start_entry0
) {
4445
            // Verify that the function start address is the symbol address
4446
            // (ARM) or the symbol address + 1 (thumb).
4447
0
            if (func_start_entry->addr != symbol_lookup_file_addr &&
4448
0
                func_start_entry->addr != (symbol_lookup_file_addr + 1)) {
4449
              // Not the right entry, NULL it out...
4450
0
              func_start_entry = nullptr;
4451
0
            }
4452
0
          }
4453
141M
          if (func_start_entry) {
4454
89.4M
            func_start_entry->data = true;
4455
4456
89.4M
            addr_t symbol_file_addr = func_start_entry->addr;
4457
89.4M
            if (is_arm)
4458
0
              symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
4459
4460
89.4M
            const FunctionStarts::Entry *next_func_start_entry =
4461
89.4M
                function_starts.FindNextEntry(func_start_entry);
4462
89.4M
            const addr_t section_end_file_addr =
4463
89.4M
                section_file_addr + symbol_section->GetByteSize();
4464
89.4M
            if (next_func_start_entry) {
4465
89.3M
              addr_t next_symbol_file_addr = next_func_start_entry->addr;
4466
              // Be sure the clear the Thumb address bit when we calculate the
4467
              // size from the current and next address
4468
89.3M
              if (is_arm)
4469
0
                next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
4470
89.3M
              symbol_byte_size = std::min<lldb::addr_t>(
4471
89.3M
                  next_symbol_file_addr - symbol_file_addr,
4472
89.3M
                  section_end_file_addr - symbol_file_addr);
4473
89.3M
            } else {
4474
124k
              symbol_byte_size = section_end_file_addr - symbol_file_addr;
4475
124k
            }
4476
89.4M
          }
4477
141M
        }
4478
141M
        symbol_value -= section_file_addr;
4479
141M
      }
4480
4481
158M
      if (!is_debug) {
4482
158M
        if (type == eSymbolTypeCode) {
4483
          // See if we can find a N_FUN entry for any code symbols. If we do
4484
          // find a match, and the name matches, then we can merge the two into
4485
          // just the function symbol to avoid duplicate entries in the symbol
4486
          // table.
4487
89.3M
          std::pair<ValueToSymbolIndexMap::const_iterator,
4488
89.3M
                    ValueToSymbolIndexMap::const_iterator>
4489
89.3M
              range;
4490
89.3M
          range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
4491
89.3M
          if (range.first != range.second) {
4492
71.4k
            for (ValueToSymbolIndexMap::const_iterator pos = range.first;
4493
71.4k
                 pos != range.second; 
++pos0
) {
4494
71.4k
              if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) ==
4495
71.4k
                  sym[pos->second].GetMangled().GetName(
4496
71.4k
                      Mangled::ePreferMangled)) {
4497
71.4k
                m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4498
                // We just need the flags from the linker symbol, so put these
4499
                // flags into the N_FUN flags to avoid duplicate symbols in the
4500
                // symbol table.
4501
71.4k
                sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4502
71.4k
                sym[pos->second].SetFlags(nlist.n_type << 16 | nlist.n_desc);
4503
71.4k
                if (resolver_addresses.find(nlist.n_value) !=
4504
71.4k
                    resolver_addresses.end())
4505
3
                  sym[pos->second].SetType(eSymbolTypeResolver);
4506
71.4k
                sym[sym_idx].Clear();
4507
71.4k
                return true;
4508
71.4k
              }
4509
71.4k
            }
4510
89.3M
          } else {
4511
89.3M
            if (resolver_addresses.find(nlist.n_value) !=
4512
89.3M
                resolver_addresses.end())
4513
311k
              type = eSymbolTypeResolver;
4514
89.3M
          }
4515
89.3M
        } else 
if (69.5M
type == eSymbolTypeData69.5M
||
type == eSymbolTypeObjCClass32.2M
||
4516
69.5M
                   
type == eSymbolTypeObjCMetaClass31.5M
||
4517
69.5M
                   
type == eSymbolTypeObjCIVar30.8M
) {
4518
          // See if we can find a N_STSYM entry for any data symbols. If we do
4519
          // find a match, and the name matches, then we can merge the two into
4520
          // just the Static symbol to avoid duplicate entries in the symbol
4521
          // table.
4522
40.7M
          std::pair<ValueToSymbolIndexMap::const_iterator,
4523
40.7M
                    ValueToSymbolIndexMap::const_iterator>
4524
40.7M
              range;
4525
40.7M
          range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
4526
40.7M
          if (range.first != range.second) {
4527
5.88k
            for (ValueToSymbolIndexMap::const_iterator pos = range.first;
4528
5.88k
                 pos != range.second; 
++pos0
) {
4529
5.88k
              if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) ==
4530
5.88k
                  sym[pos->second].GetMangled().GetName(
4531
5.88k
                      Mangled::ePreferMangled)) {
4532
5.88k
                m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4533
                // We just need the flags from the linker symbol, so put these
4534
                // flags into the N_STSYM flags to avoid duplicate symbols in
4535
                // the symbol table.
4536
5.88k
                sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4537
5.88k
                sym[pos->second].SetFlags(nlist.n_type << 16 | nlist.n_desc);
4538
5.88k
                sym[sym_idx].Clear();
4539
5.88k
                return true;
4540
5.88k
              }
4541
5.88k
            }
4542
40.7M
          } else {
4543
            // Combine N_GSYM stab entries with the non stab symbol.
4544
40.7M
            const char *gsym_name = sym[sym_idx]
4545
40.7M
                                        .GetMangled()
4546
40.7M
                                        .GetName(Mangled::ePreferMangled)
4547
40.7M
                                        .GetCString();
4548
40.7M
            if (gsym_name) {
4549
40.7M
              ConstNameToSymbolIndexMap::const_iterator pos =
4550
40.7M
                  N_GSYM_name_to_sym_idx.find(gsym_name);
4551
40.7M
              if (pos != N_GSYM_name_to_sym_idx.end()) {
4552
5.52k
                const uint32_t GSYM_sym_idx = pos->second;
4553
5.52k
                m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
4554
                // Copy the address, because often the N_GSYM address has an
4555
                // invalid address of zero when the global is a common symbol.
4556
5.52k
                sym[GSYM_sym_idx].GetAddressRef().SetSection(symbol_section);
4557
5.52k
                sym[GSYM_sym_idx].GetAddressRef().SetOffset(symbol_value);
4558
5.52k
                add_symbol_addr(
4559
5.52k
                    sym[GSYM_sym_idx].GetAddress().GetFileAddress());
4560
                // We just need the flags from the linker symbol, so put these
4561
                // flags into the N_GSYM flags to avoid duplicate symbols in
4562
                // the symbol table.
4563
5.52k
                sym[GSYM_sym_idx].SetFlags(nlist.n_type << 16 | nlist.n_desc);
4564
5.52k
                sym[sym_idx].Clear();
4565
5.52k
                return true;
4566
5.52k
              }
4567
40.7M
            }
4568
40.7M
          }
4569
40.7M
        }
4570
158M
      }
4571
4572
158M
      sym[sym_idx].SetID(nlist_idx);
4573
158M
      sym[sym_idx].SetType(type);
4574
158M
      if (set_value) {
4575
158M
        sym[sym_idx].GetAddressRef().SetSection(symbol_section);
4576
158M
        sym[sym_idx].GetAddressRef().SetOffset(symbol_value);
4577
158M
        if (symbol_section)
4578
141M
          add_symbol_addr(sym[sym_idx].GetAddress().GetFileAddress());
4579
158M
      }
4580
158M
      sym[sym_idx].SetFlags(nlist.n_type << 16 | nlist.n_desc);
4581
158M
      if (nlist.n_desc & N_WEAK_REF)
4582
106k
        sym[sym_idx].SetIsWeak(true);
4583
4584
158M
      if (symbol_byte_size > 0)
4585
89.3M
        sym[sym_idx].SetByteSize(symbol_byte_size);
4586
4587
158M
      if (demangled_is_synthesized)
4588
3.50M
        sym[sym_idx].SetDemangledNameIsSynthesized(true);
4589
4590
158M
      ++sym_idx;
4591
158M
      return true;
4592
158M
    };
4593
4594
    // First parse all the nlists but don't process them yet. See the next
4595
    // comment for an explanation why.
4596
135k
    std::vector<struct nlist_64> nlists;
4597
135k
    nlists.reserve(symtab_load_command.nsyms);
4598
159M
    for (; nlist_idx < symtab_load_command.nsyms; 
++nlist_idx159M
) {
4599
159M
      if (auto nlist =
4600
159M
              ParseNList(nlist_data, nlist_data_offset, nlist_byte_size))
4601
159M
        nlists.push_back(*nlist);
4602
0
      else
4603
0
        break;
4604
159M
    }
4605
4606
    // Now parse all the debug symbols. This is needed to merge non-debug
4607
    // symbols in the next step. Non-debug symbols are always coalesced into
4608
    // the debug symbol. Doing this in one step would mean that some symbols
4609
    // won't be merged.
4610
135k
    nlist_idx = 0;
4611
159M
    for (auto &nlist : nlists) {
4612
159M
      if (!ParseSymbolLambda(nlist, nlist_idx++, DebugSymbols))
4613
0
        break;
4614
159M
    }
4615
4616
    // Finally parse all the non debug symbols.
4617
135k
    nlist_idx = 0;
4618
159M
    for (auto &nlist : nlists) {
4619
159M
      if (!ParseSymbolLambda(nlist, nlist_idx++, NonDebugSymbols))
4620
0
        break;
4621
159M
    }
4622
4623
135k
    for (const auto &pos : reexport_shlib_needs_fixup) {
4624
2.65k
      const auto undef_pos = undefined_name_to_desc.find(pos.second);
4625
2.65k
      if (undef_pos != undefined_name_to_desc.end()) {
4626
2.65k
        const uint8_t dylib_ordinal =
4627
2.65k
            llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
4628
2.65k
        if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
4629
138
          sym[pos.first].SetReExportedSymbolSharedLibrary(
4630
138
              dylib_files.GetFileSpecAtIndex(dylib_ordinal - 1));
4631
2.65k
      }
4632
2.65k
    }
4633
135k
  }
4634
4635
  // Count how many trie symbols we'll add to the symbol table
4636
0
  int trie_symbol_table_augment_count = 0;
4637
41.2M
  for (auto &e : external_sym_trie_entries) {
4638
41.2M
    if (symbols_added.find(e.entry.address) == symbols_added.end())
4639
18
      trie_symbol_table_augment_count++;
4640
41.2M
  }
4641
4642
135k
  if (num_syms < sym_idx + trie_symbol_table_augment_count) {
4643
1
    num_syms = sym_idx + trie_symbol_table_augment_count;
4644
1
    sym = symtab.Resize(num_syms);
4645
1
  }
4646
135k
  uint32_t synthetic_sym_id = symtab_load_command.nsyms;
4647
4648
  // Add symbols from the trie to the symbol table.
4649
41.2M
  for (auto &e : external_sym_trie_entries) {
4650
41.2M
    if (symbols_added.contains(e.entry.address))
4651
41.2M
      continue;
4652
4653
    // Find the section that this trie address is in, use that to annotate
4654
    // symbol type as we add the trie address and name to the symbol table.
4655
18
    Address symbol_addr;
4656
18
    if (module_sp->ResolveFileAddress(e.entry.address, symbol_addr)) {
4657
18
      SectionSP symbol_section(symbol_addr.GetSection());
4658
18
      const char *symbol_name = e.entry.name.GetCString();
4659
18
      bool demangled_is_synthesized = false;
4660
18
      SymbolType type =
4661
18
          GetSymbolType(symbol_name, demangled_is_synthesized, text_section_sp,
4662
18
                        data_section_sp, data_dirty_section_sp,
4663
18
                        data_const_section_sp, symbol_section);
4664
4665
18
      sym[sym_idx].SetType(type);
4666
18
      if (symbol_section) {
4667
18
        sym[sym_idx].SetID(synthetic_sym_id++);
4668
18
        sym[sym_idx].GetMangled().SetMangledName(ConstString(symbol_name));
4669
18
        if (demangled_is_synthesized)
4670
12
          sym[sym_idx].SetDemangledNameIsSynthesized(true);
4671
18
        sym[sym_idx].SetIsSynthetic(true);
4672
18
        sym[sym_idx].SetExternal(true);
4673
18
        sym[sym_idx].GetAddressRef() = symbol_addr;
4674
18
        add_symbol_addr(symbol_addr.GetFileAddress());
4675
18
        if (e.entry.flags & TRIE_SYMBOL_IS_THUMB)
4676
0
          sym[sym_idx].SetFlags(MACHO_NLIST_ARM_SYMBOL_IS_THUMB);
4677
18
        ++sym_idx;
4678
18
      }
4679
18
    }
4680
18
  }
4681
4682
135k
  if (function_starts_count > 0) {
4683
131k
    uint32_t num_synthetic_function_symbols = 0;
4684
99.7M
    for (i = 0; i < function_starts_count; 
++i99.6M
) {
4685
99.6M
      if (symbols_added.find(function_starts.GetEntryRef(i).addr) ==
4686
99.6M
          symbols_added.end())
4687
13.4M
        ++num_synthetic_function_symbols;
4688
99.6M
    }
4689
4690
131k
    if (num_synthetic_function_symbols > 0) {
4691
13.7k
      if (num_syms < sym_idx + num_synthetic_function_symbols) {
4692
7.61k
        num_syms = sym_idx + num_synthetic_function_symbols;
4693
7.61k
        sym = symtab.Resize(num_syms);
4694
7.61k
      }
4695
19.9M
      for (i = 0; i < function_starts_count; 
++i19.9M
) {
4696
19.9M
        const FunctionStarts::Entry *func_start_entry =
4697
19.9M
            function_starts.GetEntryAtIndex(i);
4698
19.9M
        if (symbols_added.find(func_start_entry->addr) == symbols_added.end()) {
4699
13.4M
          addr_t symbol_file_addr = func_start_entry->addr;
4700
13.4M
          uint32_t symbol_flags = 0;
4701
13.4M
          if (func_start_entry->data)
4702
0
            symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
4703
13.4M
          Address symbol_addr;
4704
13.4M
          if (module_sp->ResolveFileAddress(symbol_file_addr, symbol_addr)) {
4705
13.4M
            SectionSP symbol_section(symbol_addr.GetSection());
4706
13.4M
            uint32_t symbol_byte_size = 0;
4707
13.4M
            if (symbol_section) {
4708
13.4M
              const addr_t section_file_addr = symbol_section->GetFileAddress();
4709
13.4M
              const FunctionStarts::Entry *next_func_start_entry =
4710
13.4M
                  function_starts.FindNextEntry(func_start_entry);
4711
13.4M
              const addr_t section_end_file_addr =
4712
13.4M
                  section_file_addr + symbol_section->GetByteSize();
4713
13.4M
              if (next_func_start_entry) {
4714
13.4M
                addr_t next_symbol_file_addr = next_func_start_entry->addr;
4715
13.4M
                if (is_arm)
4716
0
                  next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
4717
13.4M
                symbol_byte_size = std::min<lldb::addr_t>(
4718
13.4M
                    next_symbol_file_addr - symbol_file_addr,
4719
13.4M
                    section_end_file_addr - symbol_file_addr);
4720
13.4M
              } else {
4721
9.40k
                symbol_byte_size = section_end_file_addr - symbol_file_addr;
4722
9.40k
              }
4723
13.4M
              sym[sym_idx].SetID(synthetic_sym_id++);
4724
              // Don't set the name for any synthetic symbols, the Symbol
4725
              // object will generate one if needed when the name is accessed
4726
              // via accessors.
4727
13.4M
              sym[sym_idx].GetMangled().SetDemangledName(ConstString());
4728
13.4M
              sym[sym_idx].SetType(eSymbolTypeCode);
4729
13.4M
              sym[sym_idx].SetIsSynthetic(true);
4730
13.4M
              sym[sym_idx].GetAddressRef() = symbol_addr;
4731
13.4M
              add_symbol_addr(symbol_addr.GetFileAddress());
4732
13.4M
              if (symbol_flags)
4733
0
                sym[sym_idx].SetFlags(symbol_flags);
4734
13.4M
              if (symbol_byte_size)
4735
13.4M
                sym[sym_idx].SetByteSize(symbol_byte_size);
4736
13.4M
              ++sym_idx;
4737
13.4M
            }
4738
13.4M
          }
4739
13.4M
        }
4740
19.9M
      }
4741
13.7k
    }
4742
131k
  }
4743
4744
  // Trim our symbols down to just what we ended up with after removing any
4745
  // symbols.
4746
135k
  if (sym_idx < num_syms) {
4747
121k
    num_syms = sym_idx;
4748
121k
    sym = symtab.Resize(num_syms);
4749
121k
  }
4750
4751
  // Now synthesize indirect symbols
4752
135k
  if (m_dysymtab.nindirectsyms != 0) {
4753
128k
    if (indirect_symbol_index_data.GetByteSize()) {
4754
128k
      NListIndexToSymbolIndexMap::const_iterator end_index_pos =
4755
128k
          m_nlist_idx_to_sym_idx.end();
4756
4757
2.07M
      for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size();
4758
1.94M
           ++sect_idx) {
4759
1.94M
        if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) ==
4760
1.94M
            S_SYMBOL_STUBS) {
4761
123k
          uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
4762
123k
          if (symbol_stub_byte_size == 0)
4763
0
            continue;
4764
4765
123k
          const uint32_t num_symbol_stubs =
4766
123k
              m_mach_sections[sect_idx].size / symbol_stub_byte_size;
4767
4768
123k
          if (num_symbol_stubs == 0)
4769
0
            continue;
4770
4771
123k
          const uint32_t symbol_stub_index_offset =
4772
123k
              m_mach_sections[sect_idx].reserved1;
4773
14.5M
          for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; 
++stub_idx14.4M
) {
4774
14.4M
            const uint32_t symbol_stub_index =
4775
14.4M
                symbol_stub_index_offset + stub_idx;
4776
14.4M
            const lldb::addr_t symbol_stub_addr =
4777
14.4M
                m_mach_sections[sect_idx].addr +
4778
14.4M
                (stub_idx * symbol_stub_byte_size);
4779
14.4M
            lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
4780
14.4M
            if (indirect_symbol_index_data.ValidOffsetForDataOfSize(
4781
14.4M
                    symbol_stub_offset, 4)) {
4782
14.4M
              const uint32_t stub_sym_id =
4783
14.4M
                  indirect_symbol_index_data.GetU32(&symbol_stub_offset);
4784
14.4M
              if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
4785
3
                continue;
4786
4787
14.4M
              NListIndexToSymbolIndexMap::const_iterator index_pos =
4788
14.4M
                  m_nlist_idx_to_sym_idx.find(stub_sym_id);
4789
14.4M
              Symbol *stub_symbol = nullptr;
4790
14.4M
              if (index_pos != end_index_pos) {
4791
                // We have a remapping from the original nlist index to a
4792
                // current symbol index, so just look this up by index
4793
5.42k
                stub_symbol = symtab.SymbolAtIndex(index_pos->second);
4794
14.4M
              } else {
4795
                // We need to lookup a symbol using the original nlist symbol
4796
                // index since this index is coming from the S_SYMBOL_STUBS
4797
14.4M
                stub_symbol = symtab.FindSymbolByID(stub_sym_id);
4798
14.4M
              }
4799
4800
14.4M
              if (stub_symbol) {
4801
14.4M
                Address so_addr(symbol_stub_addr, section_list);
4802
4803
14.4M
                if (stub_symbol->GetType() == eSymbolTypeUndefined) {
4804
                  // Change the external symbol into a trampoline that makes
4805
                  // sense These symbols were N_UNDF N_EXT, and are useless
4806
                  // to us, so we can re-use them so we don't have to make up
4807
                  // a synthetic symbol for no good reason.
4808
14.0M
                  if (resolver_addresses.find(symbol_stub_addr) ==
4809
14.0M
                      resolver_addresses.end())
4810
14.0M
                    stub_symbol->SetType(eSymbolTypeTrampoline);
4811
0
                  else
4812
0
                    stub_symbol->SetType(eSymbolTypeResolver);
4813
14.0M
                  stub_symbol->SetExternal(false);
4814
14.0M
                  stub_symbol->GetAddressRef() = so_addr;
4815
14.0M
                  stub_symbol->SetByteSize(symbol_stub_byte_size);
4816
14.0M
                } else {
4817
                  // Make a synthetic symbol to describe the trampoline stub
4818
399k
                  Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
4819
399k
                  if (sym_idx >= num_syms) {
4820
399k
                    sym = symtab.Resize(++num_syms);
4821
399k
                    stub_symbol = nullptr; // this pointer no longer valid
4822
399k
                  }
4823
399k
                  sym[sym_idx].SetID(synthetic_sym_id++);
4824
399k
                  sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
4825
399k
                  if (resolver_addresses.find(symbol_stub_addr) ==
4826
399k
                      resolver_addresses.end())
4827
399k
                    sym[sym_idx].SetType(eSymbolTypeTrampoline);
4828
0
                  else
4829
0
                    sym[sym_idx].SetType(eSymbolTypeResolver);
4830
399k
                  sym[sym_idx].SetIsSynthetic(true);
4831
399k
                  sym[sym_idx].GetAddressRef() = so_addr;
4832
399k
                  add_symbol_addr(so_addr.GetFileAddress());
4833
399k
                  sym[sym_idx].SetByteSize(symbol_stub_byte_size);
4834
399k
                  ++sym_idx;
4835
399k
                }
4836
14.4M
              } else {
4837
0
                if (log)
4838
0
                  log->Warning("symbol stub referencing symbol table symbol "
4839
0
                               "%u that isn't in our minimal symbol table, "
4840
0
                               "fix this!!!",
4841
0
                               stub_sym_id);
4842
0
              }
4843
14.4M
            }
4844
14.4M
          }
4845
123k
        }
4846
1.94M
      }
4847
128k
    }
4848
128k
  }
4849
4850
135k
  if (!reexport_trie_entries.empty()) {
4851
118k
    for (const auto &e : reexport_trie_entries) {
4852
118k
      if (e.entry.import_name) {
4853
        // Only add indirect symbols from the Trie entries if we didn't have
4854
        // a N_INDR nlist entry for this already
4855
118k
        if (indirect_symbol_names.find(e.entry.name) ==
4856
118k
            indirect_symbol_names.end()) {
4857
          // Make a synthetic symbol to describe re-exported symbol.
4858
118k
          if (sym_idx >= num_syms)
4859
118k
            sym = symtab.Resize(++num_syms);
4860
118k
          sym[sym_idx].SetID(synthetic_sym_id++);
4861
118k
          sym[sym_idx].GetMangled() = Mangled(e.entry.name);
4862
118k
          sym[sym_idx].SetType(eSymbolTypeReExported);
4863
118k
          sym[sym_idx].SetIsSynthetic(true);
4864
118k
          sym[sym_idx].SetReExportedSymbolName(e.entry.import_name);
4865
118k
          if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize()) {
4866
118k
            sym[sym_idx].SetReExportedSymbolSharedLibrary(
4867
118k
                dylib_files.GetFileSpecAtIndex(e.entry.other - 1));
4868
118k
          }
4869
118k
          ++sym_idx;
4870
118k
        }
4871
118k
      }
4872
118k
    }
4873
5.58k
  }
4874
135k
}
4875
4876
124
void ObjectFileMachO::Dump(Stream *s) {
4877
124
  ModuleSP module_sp(GetModule());
4878
124
  if (module_sp) {
4879
124
    std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
4880
124
    s->Printf("%p: ", static_cast<void *>(this));
4881
124
    s->Indent();
4882
124
    if (m_header.magic == MH_MAGIC_64 || 
m_header.magic == MH_CIGAM_640
)
4883
124
      s->PutCString("ObjectFileMachO64");
4884
0
    else
4885
0
      s->PutCString("ObjectFileMachO32");
4886
4887
124
    *s << ", file = '" << m_file;
4888
124
    ModuleSpecList all_specs;
4889
124
    ModuleSpec base_spec;
4890
124
    GetAllArchSpecs(m_header, m_data, MachHeaderSizeFromMagic(m_header.magic),
4891
124
                    base_spec, all_specs);
4892
360
    for (unsigned i = 0, e = all_specs.GetSize(); i != e; 
++i236
) {
4893
236
      *s << "', triple";
4894
236
      if (e)
4895
236
        s->Printf("[%d]", i);
4896
236
      *s << " = ";
4897
236
      *s << all_specs.GetModuleSpecRefAtIndex(i)
4898
236
                .GetArchitecture()
4899
236
                .GetTriple()
4900
236
                .getTriple();
4901
236
    }
4902
124
    *s << "\n";
4903
124
    SectionList *sections = GetSectionList();
4904
124
    if (sections)
4905
124
      sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
4906
124
                     UINT32_MAX);
4907
4908
124
    if (m_symtab_up)
4909
120
      m_symtab_up->Dump(s, nullptr, eSortOrderNone);
4910
124
  }
4911
124
}
4912
4913
UUID ObjectFileMachO::GetUUID(const llvm::MachO::mach_header &header,
4914
                              const lldb_private::DataExtractor &data,
4915
1.21M
                              lldb::offset_t lc_offset) {
4916
1.21M
  uint32_t i;
4917
1.21M
  llvm::MachO::uuid_command load_cmd;
4918
4919
1.21M
  lldb::offset_t offset = lc_offset;
4920
9.71M
  for (i = 0; i < header.ncmds; 
++i8.49M
) {
4921
9.68M
    const lldb::offset_t cmd_offset = offset;
4922
9.68M
    if (data.GetU32(&offset, &load_cmd, 2) == nullptr)
4923
2
      break;
4924
4925
9.68M
    if (load_cmd.cmd == LC_UUID) {
4926
1.19M
      const uint8_t *uuid_bytes = data.PeekData(offset, 16);
4927
4928
1.19M
      if (uuid_bytes) {
4929
        // OpenCL on Mac OS X uses the same UUID for each of its object files.
4930
        // We pretend these object files have no UUID to prevent crashing.
4931
4932
1.19M
        const uint8_t opencl_uuid[] = {0x8c, 0x8e, 0xb3, 0x9b, 0x3b, 0xa8,
4933
1.19M
                                       0x4b, 0x16, 0xb6, 0xa4, 0x27, 0x63,
4934
1.19M
                                       0xbb, 0x14, 0xf0, 0x0d};
4935
4936
1.19M
        if (!memcmp(uuid_bytes, opencl_uuid, 16))
4937
0
          return UUID();
4938
4939
1.19M
        return UUID::fromOptionalData(uuid_bytes, 16);
4940
1.19M
      }
4941
0
      return UUID();
4942
1.19M
    }
4943
8.49M
    offset = cmd_offset + load_cmd.cmdsize;
4944
8.49M
  }
4945
22.5k
  return UUID();
4946
1.21M
}
4947
4948
32.6k
static llvm::StringRef GetOSName(uint32_t cmd) {
4949
32.6k
  switch (cmd) {
4950
3
  case llvm::MachO::LC_VERSION_MIN_IPHONEOS:
4951
3
    return llvm::Triple::getOSTypeName(llvm::Triple::IOS);
4952
32.6k
  case llvm::MachO::LC_VERSION_MIN_MACOSX:
4953
32.6k
    return llvm::Triple::getOSTypeName(llvm::Triple::MacOSX);
4954
0
  case llvm::MachO::LC_VERSION_MIN_TVOS:
4955
0
    return llvm::Triple::getOSTypeName(llvm::Triple::TvOS);
4956
0
  case llvm::MachO::LC_VERSION_MIN_WATCHOS:
4957
0
    return llvm::Triple::getOSTypeName(llvm::Triple::WatchOS);
4958
0
  default:
4959
0
    llvm_unreachable("unexpected LC_VERSION load command");
4960
32.6k
  }
4961
32.6k
}
4962
4963
namespace {
4964
struct OSEnv {
4965
  llvm::StringRef os_type;
4966
  llvm::StringRef environment;
4967
1.04M
  OSEnv(uint32_t cmd) {
4968
1.04M
    switch (cmd) {
4969
543k
    case llvm::MachO::PLATFORM_MACOS:
4970
543k
      os_type = llvm::Triple::getOSTypeName(llvm::Triple::MacOSX);
4971
543k
      return;
4972
0
    case llvm::MachO::PLATFORM_IOS:
4973
0
      os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS);
4974
0
      return;
4975
0
    case llvm::MachO::PLATFORM_TVOS:
4976
0
      os_type = llvm::Triple::getOSTypeName(llvm::Triple::TvOS);
4977
0
      return;
4978
5
    case llvm::MachO::PLATFORM_WATCHOS:
4979
5
      os_type = llvm::Triple::getOSTypeName(llvm::Triple::WatchOS);
4980
5
      return;
4981
    // TODO: add BridgeOS & DriverKit once in llvm/lib/Support/Triple.cpp
4982
    // NEED_BRIDGEOS_TRIPLE
4983
    // case llvm::MachO::PLATFORM_BRIDGEOS:
4984
    //   os_type = llvm::Triple::getOSTypeName(llvm::Triple::BridgeOS);
4985
    //   return;
4986
    // case llvm::MachO::PLATFORM_DRIVERKIT:
4987
    //   os_type = llvm::Triple::getOSTypeName(llvm::Triple::DriverKit);
4988
    //   return;
4989
496k
    case llvm::MachO::PLATFORM_MACCATALYST:
4990
496k
      os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS);
4991
496k
      environment = llvm::Triple::getEnvironmentTypeName(llvm::Triple::MacABI);
4992
496k
      return;
4993
0
    case llvm::MachO::PLATFORM_IOSSIMULATOR:
4994
0
      os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS);
4995
0
      environment =
4996
0
          llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator);
4997
0
      return;
4998
0
    case llvm::MachO::PLATFORM_TVOSSIMULATOR:
4999
0
      os_type = llvm::Triple::getOSTypeName(llvm::Triple::TvOS);
5000
0
      environment =
5001
0
          llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator);
5002