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