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