blob: b8587111df5db51e3b711da359f9d5d0ce71976a [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
Jim Ingham46d005d2014-04-02 22:53:21 +000010#include "llvm/ADT/StringRef.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
Greg Clayton3f839a32012-09-05 01:38:55 +000012#include "lldb/lldb-private-log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Core/ArchSpec.h"
14#include "lldb/Core/DataBuffer.h"
Jason Molendaf6ce26f2013-04-10 05:58:57 +000015#include "lldb/Core/Debugger.h"
Greg Claytona2715cf2014-06-13 00:54:12 +000016#include "lldb/Core/Error.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/FileSpecList.h"
Greg Clayton3f839a32012-09-05 01:38:55 +000018#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000020#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/PluginManager.h"
Greg Clayton1eac0c72012-04-24 03:06:13 +000022#include "lldb/Core/RangeMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Section.h"
24#include "lldb/Core/StreamFile.h"
25#include "lldb/Core/StreamString.h"
26#include "lldb/Core/Timer.h"
27#include "lldb/Core/UUID.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000028#include "lldb/Host/Host.h"
29#include "lldb/Host/FileSpec.h"
Sean Callananb6d70eb2011-10-12 02:08:07 +000030#include "lldb/Symbol/ClangNamespaceDecl.h"
Jason Molenda5635f772013-03-21 03:36:01 +000031#include "lldb/Symbol/DWARFCallFrameInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Symbol/ObjectFile.h"
Greg Clayton26b47e22012-04-18 05:19:20 +000033#include "lldb/Target/Platform.h"
Greg Claytonc9660542012-02-05 02:38:54 +000034#include "lldb/Target/Process.h"
Greg Clayton7524e092014-02-06 20:10:16 +000035#include "lldb/Target/SectionLoadList.h"
Greg Clayton26b47e22012-04-18 05:19:20 +000036#include "lldb/Target/Target.h"
Greg Claytona2715cf2014-06-13 00:54:12 +000037#include "lldb/Target/Thread.h"
38#include "lldb/Target/ThreadList.h"
Greg Claytonc859e2d2012-02-13 23:10:39 +000039#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
Jason Molendaa3329782014-03-29 18:54:20 +000040#include "Plugins/Process/Utility/RegisterContextDarwin_arm64.h"
Greg Claytonc859e2d2012-02-13 23:10:39 +000041#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Claytonc3776bf2012-02-09 06:16:32 +000042#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
Jim Ingham46d005d2014-04-02 22:53:21 +000044#include "lldb/Utility/SafeMachO.h"
45
46#include "ObjectFileMachO.h"
47
Jason Molendaa3329782014-03-29 18:54:20 +000048#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__))
Jason Molenda0e0954c2013-04-16 06:24:42 +000049// GetLLDBSharedCacheUUID() needs to call dlsym()
50#include <dlfcn.h>
51#endif
52
Daniel Maleaffeb4b62013-04-17 19:24:22 +000053#ifndef __APPLE__
54#include "Utility/UuidCompatibility.h"
55#endif
56
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057using namespace lldb;
58using namespace lldb_private;
Greg Claytone1a916a2010-07-21 22:12:05 +000059using namespace llvm::MachO;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060
Jason Molenda4e7511e2013-03-06 23:19:17 +000061class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
Greg Claytonc3776bf2012-02-09 06:16:32 +000062{
63public:
64 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
65 RegisterContextDarwin_x86_64 (thread, 0)
66 {
67 SetRegisterDataFrom_LC_THREAD (data);
68 }
69
70 virtual void
71 InvalidateAllRegisters ()
72 {
73 // Do nothing... registers are always valid...
74 }
75
76 void
77 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
78 {
Greg Claytonc7bece562013-01-25 18:06:21 +000079 lldb::offset_t offset = 0;
Greg Claytonc3776bf2012-02-09 06:16:32 +000080 SetError (GPRRegSet, Read, -1);
81 SetError (FPURegSet, Read, -1);
82 SetError (EXCRegSet, Read, -1);
Greg Claytonc859e2d2012-02-13 23:10:39 +000083 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +000084
Greg Claytonc859e2d2012-02-13 23:10:39 +000085 while (!done)
Greg Claytonc3776bf2012-02-09 06:16:32 +000086 {
Greg Claytonc859e2d2012-02-13 23:10:39 +000087 int flavor = data.GetU32 (&offset);
88 if (flavor == 0)
89 done = true;
90 else
Greg Claytonc3776bf2012-02-09 06:16:32 +000091 {
Greg Claytonc859e2d2012-02-13 23:10:39 +000092 uint32_t i;
93 uint32_t count = data.GetU32 (&offset);
94 switch (flavor)
95 {
96 case GPRRegSet:
97 for (i=0; i<count; ++i)
98 (&gpr.rax)[i] = data.GetU64(&offset);
99 SetError (GPRRegSet, Read, 0);
100 done = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000101
Greg Claytonc859e2d2012-02-13 23:10:39 +0000102 break;
103 case FPURegSet:
104 // TODO: fill in FPU regs....
105 //SetError (FPURegSet, Read, -1);
106 done = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000107
Greg Claytonc859e2d2012-02-13 23:10:39 +0000108 break;
109 case EXCRegSet:
110 exc.trapno = data.GetU32(&offset);
111 exc.err = data.GetU32(&offset);
112 exc.faultvaddr = data.GetU64(&offset);
113 SetError (EXCRegSet, Read, 0);
114 done = true;
115 break;
116 case 7:
117 case 8:
118 case 9:
119 // fancy flavors that encapsulate of the the above
120 // falvors...
121 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000122
Greg Claytonc859e2d2012-02-13 23:10:39 +0000123 default:
124 done = true;
125 break;
126 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000127 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000128 }
129 }
Greg Claytona2715cf2014-06-13 00:54:12 +0000130
131
132 static size_t
133 WriteRegister (RegisterContext *reg_ctx, const char *name, const char *alt_name, size_t reg_byte_size, Stream &data)
134 {
135 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
136 if (reg_info == NULL)
137 reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
138 if (reg_info)
139 {
140 lldb_private::RegisterValue reg_value;
141 if (reg_ctx->ReadRegister(reg_info, reg_value))
142 {
143 if (reg_info->byte_size >= reg_byte_size)
144 data.Write(reg_value.GetBytes(), reg_byte_size);
145 else
146 {
147 data.Write(reg_value.GetBytes(), reg_info->byte_size);
148 for (size_t i=0, n = reg_byte_size - reg_info->byte_size; i<n; ++ i)
149 data.PutChar(0);
150 }
151 return reg_byte_size;
152 }
153 }
154 // Just write zeros if all else fails
155 for (size_t i=0; i<reg_byte_size; ++ i)
156 data.PutChar(0);
157 return reg_byte_size;
158 }
159
160 static bool
161 Create_LC_THREAD (Thread *thread, Stream &data)
162 {
163 RegisterContextSP reg_ctx_sp (thread->GetRegisterContext());
164 if (reg_ctx_sp)
165 {
166 RegisterContext *reg_ctx = reg_ctx_sp.get();
167
168 data.PutHex32 (GPRRegSet); // Flavor
169 data.PutHex32 (sizeof(GPR)/sizeof(uint64_t)); // Number of uint64_t values that follow
170 WriteRegister (reg_ctx, "rax", NULL, 8, data);
171 WriteRegister (reg_ctx, "rbx", NULL, 8, data);
172 WriteRegister (reg_ctx, "rcx", NULL, 8, data);
173 WriteRegister (reg_ctx, "rdx", NULL, 8, data);
174 WriteRegister (reg_ctx, "rdi", NULL, 8, data);
175 WriteRegister (reg_ctx, "rsi", NULL, 8, data);
176 WriteRegister (reg_ctx, "rbp", NULL, 8, data);
177 WriteRegister (reg_ctx, "rsp", NULL, 8, data);
178 WriteRegister (reg_ctx, "r8", NULL, 8, data);
179 WriteRegister (reg_ctx, "r9", NULL, 8, data);
180 WriteRegister (reg_ctx, "r10", NULL, 8, data);
181 WriteRegister (reg_ctx, "r11", NULL, 8, data);
182 WriteRegister (reg_ctx, "r12", NULL, 8, data);
183 WriteRegister (reg_ctx, "r13", NULL, 8, data);
184 WriteRegister (reg_ctx, "r14", NULL, 8, data);
185 WriteRegister (reg_ctx, "r15", NULL, 8, data);
186 WriteRegister (reg_ctx, "rip", NULL, 8, data);
187 WriteRegister (reg_ctx, "rflags", NULL, 8, data);
188 WriteRegister (reg_ctx, "cs", NULL, 8, data);
189 WriteRegister (reg_ctx, "fs", NULL, 8, data);
190 WriteRegister (reg_ctx, "gs", NULL, 8, data);
191
192// // Write out the FPU registers
193// const size_t fpu_byte_size = sizeof(FPU);
194// size_t bytes_written = 0;
195// data.PutHex32 (FPURegSet);
196// data.PutHex32 (fpu_byte_size/sizeof(uint64_t));
197// bytes_written += data.PutHex32(0); // uint32_t pad[0]
198// bytes_written += data.PutHex32(0); // uint32_t pad[1]
199// bytes_written += WriteRegister (reg_ctx, "fcw", "fctrl", 2, data); // uint16_t fcw; // "fctrl"
200// bytes_written += WriteRegister (reg_ctx, "fsw" , "fstat", 2, data); // uint16_t fsw; // "fstat"
201// bytes_written += WriteRegister (reg_ctx, "ftw" , "ftag", 1, data); // uint8_t ftw; // "ftag"
202// bytes_written += data.PutHex8 (0); // uint8_t pad1;
203// bytes_written += WriteRegister (reg_ctx, "fop" , NULL, 2, data); // uint16_t fop; // "fop"
204// bytes_written += WriteRegister (reg_ctx, "fioff", "ip", 4, data); // uint32_t ip; // "fioff"
205// bytes_written += WriteRegister (reg_ctx, "fiseg", NULL, 2, data); // uint16_t cs; // "fiseg"
206// bytes_written += data.PutHex16 (0); // uint16_t pad2;
207// bytes_written += WriteRegister (reg_ctx, "dp", "fooff" , 4, data); // uint32_t dp; // "fooff"
208// bytes_written += WriteRegister (reg_ctx, "foseg", NULL, 2, data); // uint16_t ds; // "foseg"
209// bytes_written += data.PutHex16 (0); // uint16_t pad3;
210// bytes_written += WriteRegister (reg_ctx, "mxcsr", NULL, 4, data); // uint32_t mxcsr;
211// bytes_written += WriteRegister (reg_ctx, "mxcsrmask", NULL, 4, data);// uint32_t mxcsrmask;
212// bytes_written += WriteRegister (reg_ctx, "stmm0", NULL, sizeof(MMSReg), data);
213// bytes_written += WriteRegister (reg_ctx, "stmm1", NULL, sizeof(MMSReg), data);
214// bytes_written += WriteRegister (reg_ctx, "stmm2", NULL, sizeof(MMSReg), data);
215// bytes_written += WriteRegister (reg_ctx, "stmm3", NULL, sizeof(MMSReg), data);
216// bytes_written += WriteRegister (reg_ctx, "stmm4", NULL, sizeof(MMSReg), data);
217// bytes_written += WriteRegister (reg_ctx, "stmm5", NULL, sizeof(MMSReg), data);
218// bytes_written += WriteRegister (reg_ctx, "stmm6", NULL, sizeof(MMSReg), data);
219// bytes_written += WriteRegister (reg_ctx, "stmm7", NULL, sizeof(MMSReg), data);
220// bytes_written += WriteRegister (reg_ctx, "xmm0" , NULL, sizeof(XMMReg), data);
221// bytes_written += WriteRegister (reg_ctx, "xmm1" , NULL, sizeof(XMMReg), data);
222// bytes_written += WriteRegister (reg_ctx, "xmm2" , NULL, sizeof(XMMReg), data);
223// bytes_written += WriteRegister (reg_ctx, "xmm3" , NULL, sizeof(XMMReg), data);
224// bytes_written += WriteRegister (reg_ctx, "xmm4" , NULL, sizeof(XMMReg), data);
225// bytes_written += WriteRegister (reg_ctx, "xmm5" , NULL, sizeof(XMMReg), data);
226// bytes_written += WriteRegister (reg_ctx, "xmm6" , NULL, sizeof(XMMReg), data);
227// bytes_written += WriteRegister (reg_ctx, "xmm7" , NULL, sizeof(XMMReg), data);
228// bytes_written += WriteRegister (reg_ctx, "xmm8" , NULL, sizeof(XMMReg), data);
229// bytes_written += WriteRegister (reg_ctx, "xmm9" , NULL, sizeof(XMMReg), data);
230// bytes_written += WriteRegister (reg_ctx, "xmm10", NULL, sizeof(XMMReg), data);
231// bytes_written += WriteRegister (reg_ctx, "xmm11", NULL, sizeof(XMMReg), data);
232// bytes_written += WriteRegister (reg_ctx, "xmm12", NULL, sizeof(XMMReg), data);
233// bytes_written += WriteRegister (reg_ctx, "xmm13", NULL, sizeof(XMMReg), data);
234// bytes_written += WriteRegister (reg_ctx, "xmm14", NULL, sizeof(XMMReg), data);
235// bytes_written += WriteRegister (reg_ctx, "xmm15", NULL, sizeof(XMMReg), data);
236//
237// // Fill rest with zeros
238// for (size_t i=0, n = fpu_byte_size - bytes_written; i<n; ++ i)
239// data.PutChar(0);
240
241 // Write out the EXC registers
242 data.PutHex32 (EXCRegSet);
243 data.PutHex32 (sizeof(EXC)/sizeof(uint64_t));
244 WriteRegister (reg_ctx, "trapno", NULL, 4, data);
245 WriteRegister (reg_ctx, "err", NULL, 4, data);
246 WriteRegister (reg_ctx, "faultvaddr", NULL, 8, data);
247 return true;
248 }
249 return false;
250 }
251
Greg Claytonc859e2d2012-02-13 23:10:39 +0000252protected:
253 virtual int
254 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
255 {
256 return 0;
257 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000258
Greg Claytonc859e2d2012-02-13 23:10:39 +0000259 virtual int
260 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
261 {
262 return 0;
263 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000264
Greg Claytonc859e2d2012-02-13 23:10:39 +0000265 virtual int
266 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
267 {
268 return 0;
269 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000270
Greg Claytonc859e2d2012-02-13 23:10:39 +0000271 virtual int
272 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
273 {
274 return 0;
275 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000276
Greg Claytonc859e2d2012-02-13 23:10:39 +0000277 virtual int
278 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
279 {
280 return 0;
281 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000282
Greg Claytonc859e2d2012-02-13 23:10:39 +0000283 virtual int
284 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
285 {
286 return 0;
287 }
288};
Greg Claytonc3776bf2012-02-09 06:16:32 +0000289
Greg Claytonc859e2d2012-02-13 23:10:39 +0000290
Jason Molenda4e7511e2013-03-06 23:19:17 +0000291class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
Greg Claytonc859e2d2012-02-13 23:10:39 +0000292{
293public:
294 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
295 RegisterContextDarwin_i386 (thread, 0)
296 {
297 SetRegisterDataFrom_LC_THREAD (data);
298 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000299
Greg Claytonc859e2d2012-02-13 23:10:39 +0000300 virtual void
301 InvalidateAllRegisters ()
302 {
303 // Do nothing... registers are always valid...
304 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000305
Greg Claytonc859e2d2012-02-13 23:10:39 +0000306 void
307 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
308 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000309 lldb::offset_t offset = 0;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000310 SetError (GPRRegSet, Read, -1);
311 SetError (FPURegSet, Read, -1);
312 SetError (EXCRegSet, Read, -1);
313 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000314
Greg Claytonc859e2d2012-02-13 23:10:39 +0000315 while (!done)
316 {
317 int flavor = data.GetU32 (&offset);
318 if (flavor == 0)
319 done = true;
320 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000321 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000322 uint32_t i;
323 uint32_t count = data.GetU32 (&offset);
324 switch (flavor)
325 {
326 case GPRRegSet:
327 for (i=0; i<count; ++i)
328 (&gpr.eax)[i] = data.GetU32(&offset);
329 SetError (GPRRegSet, Read, 0);
330 done = true;
331
332 break;
333 case FPURegSet:
334 // TODO: fill in FPU regs....
335 //SetError (FPURegSet, Read, -1);
336 done = true;
337
338 break;
339 case EXCRegSet:
340 exc.trapno = data.GetU32(&offset);
341 exc.err = data.GetU32(&offset);
342 exc.faultvaddr = data.GetU32(&offset);
343 SetError (EXCRegSet, Read, 0);
344 done = true;
345 break;
346 case 7:
347 case 8:
348 case 9:
349 // fancy flavors that encapsulate of the the above
350 // falvors...
351 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000352
Greg Claytonc859e2d2012-02-13 23:10:39 +0000353 default:
354 done = true;
355 break;
356 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000357 }
358 }
359 }
360protected:
361 virtual int
362 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
363 {
364 return 0;
365 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000366
Greg Claytonc3776bf2012-02-09 06:16:32 +0000367 virtual int
368 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
369 {
370 return 0;
371 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000372
Greg Claytonc3776bf2012-02-09 06:16:32 +0000373 virtual int
374 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
375 {
376 return 0;
377 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000378
Greg Claytonc3776bf2012-02-09 06:16:32 +0000379 virtual int
380 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
381 {
382 return 0;
383 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000384
Greg Claytonc3776bf2012-02-09 06:16:32 +0000385 virtual int
386 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
387 {
388 return 0;
389 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000390
Greg Claytonc3776bf2012-02-09 06:16:32 +0000391 virtual int
392 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
393 {
394 return 0;
395 }
396};
397
Jason Molenda4e7511e2013-03-06 23:19:17 +0000398class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Claytonc859e2d2012-02-13 23:10:39 +0000399{
400public:
401 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonc2807462012-10-30 23:57:32 +0000402 RegisterContextDarwin_arm (thread, 0)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000403 {
404 SetRegisterDataFrom_LC_THREAD (data);
405 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000406
Greg Claytonc859e2d2012-02-13 23:10:39 +0000407 virtual void
408 InvalidateAllRegisters ()
409 {
410 // Do nothing... registers are always valid...
411 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000412
Greg Claytonc859e2d2012-02-13 23:10:39 +0000413 void
414 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
415 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000416 lldb::offset_t offset = 0;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000417 SetError (GPRRegSet, Read, -1);
418 SetError (FPURegSet, Read, -1);
419 SetError (EXCRegSet, Read, -1);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000420 bool done = false;
421
422 while (!done)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000423 {
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000424 int flavor = data.GetU32 (&offset);
425 uint32_t count = data.GetU32 (&offset);
Jason Molendaddf91772013-05-14 04:50:47 +0000426 lldb::offset_t next_thread_state = offset + (count * 4);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000427 switch (flavor)
428 {
429 case GPRRegSet:
430 for (uint32_t i=0; i<count; ++i)
Jason Molendaddf91772013-05-14 04:50:47 +0000431 {
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000432 gpr.r[i] = data.GetU32(&offset);
Jason Molendaddf91772013-05-14 04:50:47 +0000433 }
434
435 // Note that gpr.cpsr is also copied by the above loop; this loop technically extends
436 // one element past the end of the gpr.r[] array.
437
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000438 SetError (GPRRegSet, Read, 0);
Jason Molendaddf91772013-05-14 04:50:47 +0000439 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000440 break;
441
442 case FPURegSet:
443 {
Jason Molenda663d2e12013-05-14 03:52:22 +0000444 uint8_t *fpu_reg_buf = (uint8_t*) &fpu.floats.s[0];
445 const int fpu_reg_buf_size = sizeof (fpu.floats);
446 if (data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000447 {
Jason Molenda663d2e12013-05-14 03:52:22 +0000448 offset += fpu_reg_buf_size;
449 fpu.fpscr = data.GetU32(&offset);
450 SetError (FPURegSet, Read, 0);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000451 }
Jason Molenda663d2e12013-05-14 03:52:22 +0000452 else
453 {
454 done = true;
455 }
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000456 }
Jason Molendaddf91772013-05-14 04:50:47 +0000457 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000458 break;
459
460 case EXCRegSet:
Jason Molendaddf91772013-05-14 04:50:47 +0000461 if (count == 3)
462 {
463 exc.exception = data.GetU32(&offset);
464 exc.fsr = data.GetU32(&offset);
465 exc.far = data.GetU32(&offset);
466 SetError (EXCRegSet, Read, 0);
467 }
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000468 done = true;
Jason Molendaddf91772013-05-14 04:50:47 +0000469 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000470 break;
471
472 // Unknown register set flavor, stop trying to parse.
473 default:
474 done = true;
475 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000476 }
477 }
478protected:
479 virtual int
480 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
481 {
Jason Molendaddf91772013-05-14 04:50:47 +0000482 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000483 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000484
Greg Claytonc859e2d2012-02-13 23:10:39 +0000485 virtual int
486 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
487 {
Jason Molendaddf91772013-05-14 04:50:47 +0000488 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000489 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000490
Greg Claytonc859e2d2012-02-13 23:10:39 +0000491 virtual int
492 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
493 {
Jason Molendaddf91772013-05-14 04:50:47 +0000494 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000495 }
Greg Claytonc2807462012-10-30 23:57:32 +0000496
497 virtual int
498 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
499 {
500 return -1;
501 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000502
Greg Claytonc859e2d2012-02-13 23:10:39 +0000503 virtual int
504 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
505 {
506 return 0;
507 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000508
Greg Claytonc859e2d2012-02-13 23:10:39 +0000509 virtual int
510 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
511 {
512 return 0;
513 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000514
Greg Claytonc859e2d2012-02-13 23:10:39 +0000515 virtual int
516 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
517 {
518 return 0;
519 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000520
Greg Claytonc2807462012-10-30 23:57:32 +0000521 virtual int
522 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
523 {
524 return -1;
525 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000526};
527
Jason Molendaa3329782014-03-29 18:54:20 +0000528class RegisterContextDarwin_arm64_Mach : public RegisterContextDarwin_arm64
529{
530public:
531 RegisterContextDarwin_arm64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
532 RegisterContextDarwin_arm64 (thread, 0)
533 {
534 SetRegisterDataFrom_LC_THREAD (data);
535 }
536
537 virtual void
538 InvalidateAllRegisters ()
539 {
540 // Do nothing... registers are always valid...
541 }
542
543 void
544 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
545 {
546 lldb::offset_t offset = 0;
547 SetError (GPRRegSet, Read, -1);
548 SetError (FPURegSet, Read, -1);
549 SetError (EXCRegSet, Read, -1);
550 bool done = false;
551 while (!done)
552 {
553 int flavor = data.GetU32 (&offset);
554 uint32_t count = data.GetU32 (&offset);
555 lldb::offset_t next_thread_state = offset + (count * 4);
556 switch (flavor)
557 {
558 case GPRRegSet:
559 // x0-x29 + fp + lr + sp + pc (== 33 64-bit registers) plus cpsr (1 32-bit register)
560 if (count >= (33 * 2) + 1)
561 {
562 for (uint32_t i=0; i<33; ++i)
563 gpr.x[i] = data.GetU64(&offset);
564 gpr.cpsr = data.GetU32(&offset);
565 SetError (GPRRegSet, Read, 0);
566 }
567 offset = next_thread_state;
568 break;
569 case FPURegSet:
570 {
571 uint8_t *fpu_reg_buf = (uint8_t*) &fpu.v[0];
572 const int fpu_reg_buf_size = sizeof (fpu);
573 if (fpu_reg_buf_size == count
574 && data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
575 {
576 SetError (FPURegSet, Read, 0);
577 }
578 else
579 {
580 done = true;
581 }
582 }
583 offset = next_thread_state;
584 break;
585 case EXCRegSet:
586 if (count == 4)
587 {
588 exc.far = data.GetU64(&offset);
589 exc.esr = data.GetU32(&offset);
590 exc.exception = data.GetU32(&offset);
591 SetError (EXCRegSet, Read, 0);
592 }
593 offset = next_thread_state;
594 break;
595 default:
596 done = true;
597 break;
598 }
599 }
600 }
601protected:
602 virtual int
603 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
604 {
605 return -1;
606 }
607
608 virtual int
609 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
610 {
611 return -1;
612 }
613
614 virtual int
615 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
616 {
617 return -1;
618 }
619
620 virtual int
621 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
622 {
623 return -1;
624 }
625
626 virtual int
627 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
628 {
629 return 0;
630 }
631
632 virtual int
633 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
634 {
635 return 0;
636 }
637
638 virtual int
639 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
640 {
641 return 0;
642 }
643
644 virtual int
645 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
646 {
647 return -1;
648 }
649};
650
Greg Clayton9aae0a12013-05-15 19:52:08 +0000651static uint32_t
652MachHeaderSizeFromMagic(uint32_t magic)
653{
654 switch (magic)
655 {
Charles Davis510938e2013-08-27 05:04:57 +0000656 case MH_MAGIC:
657 case MH_CIGAM:
Greg Clayton9aae0a12013-05-15 19:52:08 +0000658 return sizeof(struct mach_header);
659
Charles Davis510938e2013-08-27 05:04:57 +0000660 case MH_MAGIC_64:
661 case MH_CIGAM_64:
Greg Clayton9aae0a12013-05-15 19:52:08 +0000662 return sizeof(struct mach_header_64);
663 break;
664
665 default:
666 break;
667 }
668 return 0;
669}
670
Greg Claytonded470d2011-03-19 01:12:21 +0000671#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672
673void
674ObjectFileMachO::Initialize()
675{
676 PluginManager::RegisterPlugin (GetPluginNameStatic(),
677 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +0000678 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000679 CreateMemoryInstance,
Greg Claytona2715cf2014-06-13 00:54:12 +0000680 GetModuleSpecifications,
681 SaveCore);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682}
683
684void
685ObjectFileMachO::Terminate()
686{
687 PluginManager::UnregisterPlugin (CreateInstance);
688}
689
690
Greg Clayton57abc5d2013-05-10 21:47:16 +0000691lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692ObjectFileMachO::GetPluginNameStatic()
693{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000694 static ConstString g_name("mach-o");
695 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696}
697
698const char *
699ObjectFileMachO::GetPluginDescriptionStatic()
700{
701 return "Mach-o object file reader (32 and 64 bit)";
702}
703
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704ObjectFile *
Greg Clayton5ce9c562013-02-06 17:22:03 +0000705ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
706 DataBufferSP& data_sp,
707 lldb::offset_t data_offset,
708 const FileSpec* file,
709 lldb::offset_t file_offset,
710 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000712 if (!data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000714 data_sp = file->MemoryMapFileContents(file_offset, length);
715 data_offset = 0;
716 }
717
718 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
719 {
720 // Update the data to contain the entire file if it doesn't already
721 if (data_sp->GetByteSize() < length)
722 {
723 data_sp = file->MemoryMapFileContents(file_offset, length);
724 data_offset = 0;
725 }
Greg Clayton7b0992d2013-04-18 22:45:39 +0000726 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, data_offset, file, file_offset, length));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 if (objfile_ap.get() && objfile_ap->ParseHeader())
728 return objfile_ap.release();
729 }
730 return NULL;
731}
732
Greg Claytonc9660542012-02-05 02:38:54 +0000733ObjectFile *
Jason Molenda4e7511e2013-03-06 23:19:17 +0000734ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
735 DataBufferSP& data_sp,
736 const ProcessSP &process_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000737 lldb::addr_t header_addr)
738{
739 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
740 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000741 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonc9660542012-02-05 02:38:54 +0000742 if (objfile_ap.get() && objfile_ap->ParseHeader())
743 return objfile_ap.release();
744 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000745 return NULL;
Greg Claytonc9660542012-02-05 02:38:54 +0000746}
747
Greg Claytonf4d6de62013-04-24 22:29:28 +0000748size_t
749ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
750 lldb::DataBufferSP& data_sp,
751 lldb::offset_t data_offset,
752 lldb::offset_t file_offset,
753 lldb::offset_t length,
754 lldb_private::ModuleSpecList &specs)
755{
756 const size_t initial_count = specs.GetSize();
757
758 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
759 {
760 DataExtractor data;
761 data.SetData(data_sp);
762 llvm::MachO::mach_header header;
763 if (ParseHeader (data, &data_offset, header))
764 {
Jason Molenda48cd3332014-04-22 04:52:30 +0000765 size_t header_and_load_cmds = header.sizeofcmds + MachHeaderSizeFromMagic(header.magic);
766 if (header_and_load_cmds >= data_sp->GetByteSize())
Greg Claytonf4d6de62013-04-24 22:29:28 +0000767 {
Jason Molenda48cd3332014-04-22 04:52:30 +0000768 data_sp = file.ReadFileContents(file_offset, header_and_load_cmds);
Greg Clayton2540a8a2013-07-12 22:07:46 +0000769 data.SetData(data_sp);
770 data_offset = MachHeaderSizeFromMagic(header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +0000771 }
772 if (data_sp)
773 {
774 ModuleSpec spec;
775 spec.GetFileSpec() = file;
Greg Clayton7ab7f892014-05-29 21:33:45 +0000776 spec.SetObjectOffset(file_offset);
777
778 if (GetArchitecture (header, data, data_offset, spec.GetArchitecture()))
Jason Molendab000e4d2013-08-27 02:22:06 +0000779 {
Greg Clayton7ab7f892014-05-29 21:33:45 +0000780 if (spec.GetArchitecture().IsValid())
781 {
782 GetUUID (header, data, data_offset, spec.GetUUID());
783 specs.Append(spec);
784 }
Greg Claytonf4d6de62013-04-24 22:29:28 +0000785 }
786 }
787 }
788 }
789 return specs.GetSize() - initial_count;
790}
791
792
Greg Claytonc9660542012-02-05 02:38:54 +0000793
794const ConstString &
795ObjectFileMachO::GetSegmentNameTEXT()
796{
797 static ConstString g_segment_name_TEXT ("__TEXT");
798 return g_segment_name_TEXT;
799}
800
801const ConstString &
802ObjectFileMachO::GetSegmentNameDATA()
803{
804 static ConstString g_segment_name_DATA ("__DATA");
805 return g_segment_name_DATA;
806}
807
808const ConstString &
809ObjectFileMachO::GetSegmentNameOBJC()
810{
811 static ConstString g_segment_name_OBJC ("__OBJC");
812 return g_segment_name_OBJC;
813}
814
815const ConstString &
816ObjectFileMachO::GetSegmentNameLINKEDIT()
817{
818 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
819 return g_section_name_LINKEDIT;
820}
821
822const ConstString &
823ObjectFileMachO::GetSectionNameEHFrame()
824{
825 static ConstString g_section_name_eh_frame ("__eh_frame");
826 return g_section_name_eh_frame;
827}
828
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829bool
Jason Molenda4e7511e2013-03-06 23:19:17 +0000830ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
831 lldb::addr_t data_offset,
Greg Clayton44435ed2012-01-12 05:25:17 +0000832 lldb::addr_t data_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833{
Greg Clayton44435ed2012-01-12 05:25:17 +0000834 DataExtractor data;
835 data.SetData (data_sp, data_offset, data_length);
Greg Claytonc7bece562013-01-25 18:06:21 +0000836 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000837 uint32_t magic = data.GetU32(&offset);
838 return MachHeaderSizeFromMagic(magic) != 0;
839}
840
841
Greg Clayton5ce9c562013-02-06 17:22:03 +0000842ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
843 DataBufferSP& data_sp,
844 lldb::offset_t data_offset,
845 const FileSpec* file,
846 lldb::offset_t file_offset,
847 lldb::offset_t length) :
848 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000849 m_mach_segments(),
850 m_mach_sections(),
851 m_entry_point_address(),
852 m_thread_context_offsets(),
853 m_thread_context_offsets_valid(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854{
Greg Clayton72b77eb2011-02-04 21:13:05 +0000855 ::memset (&m_header, 0, sizeof(m_header));
856 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857}
858
Greg Claytone72dfb32012-02-24 01:59:29 +0000859ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000860 lldb::DataBufferSP& header_data_sp,
861 const lldb::ProcessSP &process_sp,
862 lldb::addr_t header_addr) :
Greg Claytone72dfb32012-02-24 01:59:29 +0000863 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000864 m_mach_segments(),
865 m_mach_sections(),
866 m_entry_point_address(),
867 m_thread_context_offsets(),
868 m_thread_context_offsets_valid(false)
Greg Claytonc9660542012-02-05 02:38:54 +0000869{
870 ::memset (&m_header, 0, sizeof(m_header));
871 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
872}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873
874ObjectFileMachO::~ObjectFileMachO()
875{
876}
877
Greg Claytonf4d6de62013-04-24 22:29:28 +0000878bool
879ObjectFileMachO::ParseHeader (DataExtractor &data,
880 lldb::offset_t *data_offset_ptr,
881 llvm::MachO::mach_header &header)
882{
883 data.SetByteOrder (lldb::endian::InlHostByteOrder());
884 // Leave magic in the original byte order
885 header.magic = data.GetU32(data_offset_ptr);
886 bool can_parse = false;
887 bool is_64_bit = false;
888 switch (header.magic)
889 {
Charles Davis510938e2013-08-27 05:04:57 +0000890 case MH_MAGIC:
Greg Claytonf4d6de62013-04-24 22:29:28 +0000891 data.SetByteOrder (lldb::endian::InlHostByteOrder());
892 data.SetAddressByteSize(4);
893 can_parse = true;
894 break;
895
Charles Davis510938e2013-08-27 05:04:57 +0000896 case MH_MAGIC_64:
Greg Claytonf4d6de62013-04-24 22:29:28 +0000897 data.SetByteOrder (lldb::endian::InlHostByteOrder());
898 data.SetAddressByteSize(8);
899 can_parse = true;
900 is_64_bit = true;
901 break;
902
Charles Davis510938e2013-08-27 05:04:57 +0000903 case MH_CIGAM:
Greg Claytonf4d6de62013-04-24 22:29:28 +0000904 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
905 data.SetAddressByteSize(4);
906 can_parse = true;
907 break;
908
Charles Davis510938e2013-08-27 05:04:57 +0000909 case MH_CIGAM_64:
Greg Claytonf4d6de62013-04-24 22:29:28 +0000910 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
911 data.SetAddressByteSize(8);
912 is_64_bit = true;
913 can_parse = true;
914 break;
915
916 default:
917 break;
918 }
919
920 if (can_parse)
921 {
922 data.GetU32(data_offset_ptr, &header.cputype, 6);
923 if (is_64_bit)
924 *data_offset_ptr += 4;
925 return true;
926 }
927 else
928 {
929 memset(&header, 0, sizeof(header));
930 }
931 return false;
932}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933
934bool
935ObjectFileMachO::ParseHeader ()
936{
Greg Claytona1743492012-03-13 23:14:29 +0000937 ModuleSP module_sp(GetModule());
938 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939 {
Greg Claytona1743492012-03-13 23:14:29 +0000940 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
941 bool can_parse = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000942 lldb::offset_t offset = 0;
Greg Clayton7fb56d02011-02-01 01:31:41 +0000943 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +0000944 // Leave magic in the original byte order
945 m_header.magic = m_data.GetU32(&offset);
946 switch (m_header.magic)
Greg Claytonc9660542012-02-05 02:38:54 +0000947 {
Charles Davis510938e2013-08-27 05:04:57 +0000948 case MH_MAGIC:
Greg Claytona1743492012-03-13 23:14:29 +0000949 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
950 m_data.SetAddressByteSize(4);
951 can_parse = true;
952 break;
953
Charles Davis510938e2013-08-27 05:04:57 +0000954 case MH_MAGIC_64:
Greg Claytona1743492012-03-13 23:14:29 +0000955 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
956 m_data.SetAddressByteSize(8);
957 can_parse = true;
958 break;
959
Charles Davis510938e2013-08-27 05:04:57 +0000960 case MH_CIGAM:
Greg Claytona1743492012-03-13 23:14:29 +0000961 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
962 m_data.SetAddressByteSize(4);
963 can_parse = true;
964 break;
965
Charles Davis510938e2013-08-27 05:04:57 +0000966 case MH_CIGAM_64:
Greg Claytona1743492012-03-13 23:14:29 +0000967 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
968 m_data.SetAddressByteSize(8);
969 can_parse = true;
970 break;
971
972 default:
973 break;
Greg Claytonc9660542012-02-05 02:38:54 +0000974 }
Greg Claytona1743492012-03-13 23:14:29 +0000975
976 if (can_parse)
977 {
978 m_data.GetU32(&offset, &m_header.cputype, 6);
979
Greg Clayton7ab7f892014-05-29 21:33:45 +0000980
981 ArchSpec mach_arch;
982
983 if (GetArchitecture (mach_arch))
Greg Claytona1743492012-03-13 23:14:29 +0000984 {
Greg Clayton7ab7f892014-05-29 21:33:45 +0000985 // Check if the module has a required architecture
986 const ArchSpec &module_arch = module_sp->GetArchitecture();
987 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
988 return false;
989
990 if (SetModulesArchitecture (mach_arch))
Greg Claytona1743492012-03-13 23:14:29 +0000991 {
Greg Clayton7ab7f892014-05-29 21:33:45 +0000992 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
993 if (m_data.GetByteSize() < header_and_lc_size)
Greg Claytona1743492012-03-13 23:14:29 +0000994 {
Greg Clayton7ab7f892014-05-29 21:33:45 +0000995 DataBufferSP data_sp;
996 ProcessSP process_sp (m_process_wp.lock());
997 if (process_sp)
998 {
999 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
1000 }
1001 else
1002 {
1003 // Read in all only the load command data from the file on disk
1004 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
1005 if (data_sp->GetByteSize() != header_and_lc_size)
1006 return false;
1007 }
1008 if (data_sp)
1009 m_data.SetData (data_sp);
Greg Claytona1743492012-03-13 23:14:29 +00001010 }
Greg Claytona1743492012-03-13 23:14:29 +00001011 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00001012 return true;
Greg Claytona1743492012-03-13 23:14:29 +00001013 }
Greg Claytona1743492012-03-13 23:14:29 +00001014 }
1015 else
1016 {
1017 memset(&m_header, 0, sizeof(struct mach_header));
1018 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019 }
1020 return false;
1021}
1022
1023
1024ByteOrder
1025ObjectFileMachO::GetByteOrder () const
1026{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027 return m_data.GetByteOrder ();
1028}
1029
Jim Ingham5aee1622010-08-09 23:31:02 +00001030bool
1031ObjectFileMachO::IsExecutable() const
1032{
Charles Davis510938e2013-08-27 05:04:57 +00001033 return m_header.filetype == MH_EXECUTE;
Jim Ingham5aee1622010-08-09 23:31:02 +00001034}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001035
Greg Claytonc7bece562013-01-25 18:06:21 +00001036uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001037ObjectFileMachO::GetAddressByteSize () const
1038{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039 return m_data.GetAddressByteSize ();
1040}
1041
Greg Claytone0d378b2011-03-24 21:19:54 +00001042AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +00001043ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
1044{
1045 Symtab *symtab = GetSymtab();
1046 if (symtab)
1047 {
1048 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
1049 if (symbol)
1050 {
Greg Claytone7612132012-03-07 21:03:09 +00001051 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +00001052 {
Greg Claytone7612132012-03-07 21:03:09 +00001053 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +00001054 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +00001055 {
Charles Davis510938e2013-08-27 05:04:57 +00001056 const lldb::SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +00001057 switch (section_type)
1058 {
1059 case eSectionTypeInvalid: return eAddressClassUnknown;
1060 case eSectionTypeCode:
Charles Davis510938e2013-08-27 05:04:57 +00001061 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +00001062 {
1063 // For ARM we have a bit in the n_desc field of the symbol
1064 // that tells us ARM/Thumb which is bit 0x0008.
1065 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1066 return eAddressClassCodeAlternateISA;
1067 }
1068 return eAddressClassCode;
1069
1070 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton5009f9d2011-10-27 17:55:14 +00001071 case eSectionTypeData:
1072 case eSectionTypeDataCString:
1073 case eSectionTypeDataCStringPointers:
1074 case eSectionTypeDataSymbolAddress:
1075 case eSectionTypeData4:
1076 case eSectionTypeData8:
1077 case eSectionTypeData16:
1078 case eSectionTypeDataPointers:
1079 case eSectionTypeZeroFill:
1080 case eSectionTypeDataObjCMessageRefs:
1081 case eSectionTypeDataObjCCFStrings:
1082 return eAddressClassData;
1083 case eSectionTypeDebug:
1084 case eSectionTypeDWARFDebugAbbrev:
1085 case eSectionTypeDWARFDebugAranges:
1086 case eSectionTypeDWARFDebugFrame:
1087 case eSectionTypeDWARFDebugInfo:
1088 case eSectionTypeDWARFDebugLine:
1089 case eSectionTypeDWARFDebugLoc:
1090 case eSectionTypeDWARFDebugMacInfo:
1091 case eSectionTypeDWARFDebugPubNames:
1092 case eSectionTypeDWARFDebugPubTypes:
1093 case eSectionTypeDWARFDebugRanges:
1094 case eSectionTypeDWARFDebugStr:
1095 case eSectionTypeDWARFAppleNames:
1096 case eSectionTypeDWARFAppleTypes:
1097 case eSectionTypeDWARFAppleNamespaces:
1098 case eSectionTypeDWARFAppleObjC:
1099 return eAddressClassDebug;
Greg Claytonded470d2011-03-19 01:12:21 +00001100 case eSectionTypeEHFrame: return eAddressClassRuntime;
Michael Sartaina7499c92013-07-01 19:45:50 +00001101 case eSectionTypeELFSymbolTable:
1102 case eSectionTypeELFDynamicSymbols:
1103 case eSectionTypeELFRelocationEntries:
1104 case eSectionTypeELFDynamicLinkInfo:
Greg Claytonded470d2011-03-19 01:12:21 +00001105 case eSectionTypeOther: return eAddressClassUnknown;
1106 }
1107 }
1108 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001109
Greg Claytone0d378b2011-03-24 21:19:54 +00001110 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +00001111 switch (symbol_type)
1112 {
1113 case eSymbolTypeAny: return eAddressClassUnknown;
1114 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda4e7511e2013-03-06 23:19:17 +00001115
Greg Claytonded470d2011-03-19 01:12:21 +00001116 case eSymbolTypeCode:
1117 case eSymbolTypeTrampoline:
Greg Clayton059f7242013-02-27 21:16:04 +00001118 case eSymbolTypeResolver:
Charles Davis510938e2013-08-27 05:04:57 +00001119 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +00001120 {
1121 // For ARM we have a bit in the n_desc field of the symbol
1122 // that tells us ARM/Thumb which is bit 0x0008.
1123 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1124 return eAddressClassCodeAlternateISA;
1125 }
1126 return eAddressClassCode;
1127
1128 case eSymbolTypeData: return eAddressClassData;
1129 case eSymbolTypeRuntime: return eAddressClassRuntime;
1130 case eSymbolTypeException: return eAddressClassRuntime;
1131 case eSymbolTypeSourceFile: return eAddressClassDebug;
1132 case eSymbolTypeHeaderFile: return eAddressClassDebug;
1133 case eSymbolTypeObjectFile: return eAddressClassDebug;
1134 case eSymbolTypeCommonBlock: return eAddressClassDebug;
1135 case eSymbolTypeBlock: return eAddressClassDebug;
1136 case eSymbolTypeLocal: return eAddressClassData;
1137 case eSymbolTypeParam: return eAddressClassData;
1138 case eSymbolTypeVariable: return eAddressClassData;
1139 case eSymbolTypeVariableType: return eAddressClassDebug;
1140 case eSymbolTypeLineEntry: return eAddressClassDebug;
1141 case eSymbolTypeLineHeader: return eAddressClassDebug;
1142 case eSymbolTypeScopeBegin: return eAddressClassDebug;
1143 case eSymbolTypeScopeEnd: return eAddressClassDebug;
1144 case eSymbolTypeAdditional: return eAddressClassUnknown;
1145 case eSymbolTypeCompiler: return eAddressClassDebug;
1146 case eSymbolTypeInstrumentation:return eAddressClassDebug;
1147 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +00001148 case eSymbolTypeObjCClass: return eAddressClassRuntime;
1149 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
1150 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Clayton9191db42013-10-21 18:40:51 +00001151 case eSymbolTypeReExported: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +00001152 }
1153 }
1154 }
1155 return eAddressClassUnknown;
1156}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157
1158Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +00001159ObjectFileMachO::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001160{
Greg Claytona1743492012-03-13 23:14:29 +00001161 ModuleSP module_sp(GetModule());
1162 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163 {
Greg Claytona1743492012-03-13 23:14:29 +00001164 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
1165 if (m_symtab_ap.get() == NULL)
1166 {
1167 m_symtab_ap.reset(new Symtab(this));
1168 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton3046e662013-07-10 01:23:25 +00001169 ParseSymtab ();
Greg Claytona1743492012-03-13 23:14:29 +00001170 m_symtab_ap->Finalize ();
1171 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001172 }
1173 return m_symtab_ap.get();
1174}
1175
Greg Clayton3046e662013-07-10 01:23:25 +00001176bool
1177ObjectFileMachO::IsStripped ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178{
Greg Clayton3046e662013-07-10 01:23:25 +00001179 if (m_dysymtab.cmd == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180 {
Greg Clayton3046e662013-07-10 01:23:25 +00001181 ModuleSP module_sp(GetModule());
1182 if (module_sp)
Greg Claytona1743492012-03-13 23:14:29 +00001183 {
Greg Clayton3046e662013-07-10 01:23:25 +00001184 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1185 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton4d78c402012-05-25 18:09:55 +00001186 {
Greg Clayton3046e662013-07-10 01:23:25 +00001187 const lldb::offset_t load_cmd_offset = offset;
1188
1189 load_command lc;
1190 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
1191 break;
Charles Davis510938e2013-08-27 05:04:57 +00001192 if (lc.cmd == LC_DYSYMTAB)
Greg Clayton4d78c402012-05-25 18:09:55 +00001193 {
Greg Clayton3046e662013-07-10 01:23:25 +00001194 m_dysymtab.cmd = lc.cmd;
1195 m_dysymtab.cmdsize = lc.cmdsize;
1196 if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
1197 {
1198 // Clear m_dysymtab if we were unable to read all items from the load command
1199 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
1200 }
Greg Clayton4d78c402012-05-25 18:09:55 +00001201 }
Greg Clayton3046e662013-07-10 01:23:25 +00001202 offset = load_cmd_offset + lc.cmdsize;
Greg Clayton4d78c402012-05-25 18:09:55 +00001203 }
Greg Clayton1eac0c72012-04-24 03:06:13 +00001204 }
Greg Clayton1eac0c72012-04-24 03:06:13 +00001205 }
Greg Clayton3046e662013-07-10 01:23:25 +00001206 if (m_dysymtab.cmd)
Greg Clayton93e28612013-10-11 22:03:48 +00001207 return m_dysymtab.nlocalsym <= 1;
Greg Clayton3046e662013-07-10 01:23:25 +00001208 return false;
1209}
Greg Clayton1eac0c72012-04-24 03:06:13 +00001210
Greg Clayton3046e662013-07-10 01:23:25 +00001211void
1212ObjectFileMachO::CreateSections (SectionList &unified_section_list)
1213{
1214 if (!m_sections_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215 {
Greg Clayton3046e662013-07-10 01:23:25 +00001216 m_sections_ap.reset(new SectionList());
1217
Charles Davis510938e2013-08-27 05:04:57 +00001218 const bool is_dsym = (m_header.filetype == MH_DSYM);
Greg Clayton3046e662013-07-10 01:23:25 +00001219 lldb::user_id_t segID = 0;
1220 lldb::user_id_t sectID = 0;
1221 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1222 uint32_t i;
1223 const bool is_core = GetType() == eTypeCoreFile;
1224 //bool dump_sections = false;
1225 ModuleSP module_sp (GetModule());
1226 // First look up any LC_ENCRYPTION_INFO load commands
1227 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
1228 EncryptedFileRanges encrypted_file_ranges;
1229 encryption_info_command encryption_cmd;
1230 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231 {
Greg Clayton3046e662013-07-10 01:23:25 +00001232 const lldb::offset_t load_cmd_offset = offset;
1233 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
1234 break;
1235
Charles Davis510938e2013-08-27 05:04:57 +00001236 if (encryption_cmd.cmd == LC_ENCRYPTION_INFO)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237 {
Greg Clayton3046e662013-07-10 01:23:25 +00001238 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
Jason Molendaf6ce26f2013-04-10 05:58:57 +00001239 {
Greg Clayton3046e662013-07-10 01:23:25 +00001240 if (encryption_cmd.cryptid != 0)
Greg Claytond37d6922013-04-16 16:51:19 +00001241 {
Greg Clayton3046e662013-07-10 01:23:25 +00001242 EncryptedFileRanges::Entry entry;
1243 entry.SetRangeBase(encryption_cmd.cryptoff);
1244 entry.SetByteSize(encryption_cmd.cryptsize);
1245 encrypted_file_ranges.Append(entry);
Jason Molendaf6ce26f2013-04-10 05:58:57 +00001246 }
1247 }
Greg Clayton3046e662013-07-10 01:23:25 +00001248 }
1249 offset = load_cmd_offset + encryption_cmd.cmdsize;
1250 }
1251
1252 offset = MachHeaderSizeFromMagic(m_header.magic);
1253
1254 struct segment_command_64 load_cmd;
1255 for (i=0; i<m_header.ncmds; ++i)
1256 {
1257 const lldb::offset_t load_cmd_offset = offset;
1258 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1259 break;
1260
Charles Davis510938e2013-08-27 05:04:57 +00001261 if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64)
Greg Clayton3046e662013-07-10 01:23:25 +00001262 {
1263 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001264 {
Greg Clayton3046e662013-07-10 01:23:25 +00001265 bool add_section = true;
1266 bool add_to_unified = true;
1267 ConstString const_segname (load_cmd.segname, std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
Jason Molenda4e7511e2013-03-06 23:19:17 +00001268
Greg Clayton3046e662013-07-10 01:23:25 +00001269 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1270 if (is_dsym && unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001271 {
Greg Clayton3046e662013-07-10 01:23:25 +00001272 if (const_segname == GetSegmentNameLINKEDIT())
1273 {
1274 // We need to keep the __LINKEDIT segment private to this object file only
1275 add_to_unified = false;
1276 }
1277 else
1278 {
1279 // This is the dSYM file and this section has already been created by
1280 // the object file, no need to create it.
1281 add_section = false;
1282 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283 }
Greg Clayton3046e662013-07-10 01:23:25 +00001284 load_cmd.vmaddr = m_data.GetAddress(&offset);
1285 load_cmd.vmsize = m_data.GetAddress(&offset);
1286 load_cmd.fileoff = m_data.GetAddress(&offset);
1287 load_cmd.filesize = m_data.GetAddress(&offset);
1288 if (m_length != 0 && load_cmd.filesize != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001289 {
Greg Clayton3046e662013-07-10 01:23:25 +00001290 if (load_cmd.fileoff > m_length)
1291 {
1292 // We have a load command that says it extends past the end of hte file. This is likely
1293 // a corrupt file. We don't have any way to return an error condition here (this method
1294 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1295 // is null out the SectionList vector and if a process has been set up, dump a message
1296 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001297 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001298 module_sp->ReportWarning("load command %u %s has a fileoff (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 "), ignoring this section",
Jason Molenda20eb31b2013-08-16 03:20:42 +00001299 i,
1300 lc_segment_name,
1301 load_cmd.fileoff,
1302 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001303
1304 load_cmd.fileoff = 0;
1305 load_cmd.filesize = 0;
1306 }
1307
1308 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1309 {
1310 // We have a load command that says it extends past the end of hte file. This is likely
1311 // a corrupt file. We don't have any way to return an error condition here (this method
1312 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1313 // is null out the SectionList vector and if a process has been set up, dump a message
1314 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001315 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001316 GetModule()->ReportWarning("load command %u %s has a fileoff + filesize (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 "), the segment will be truncated to match",
Charles Davis510938e2013-08-27 05:04:57 +00001317 i,
1318 lc_segment_name,
1319 load_cmd.fileoff + load_cmd.filesize,
1320 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001321
1322 // Tuncase the length
1323 load_cmd.filesize = m_length - load_cmd.fileoff;
1324 }
1325 }
1326 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1327 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328
Charles Davis510938e2013-08-27 05:04:57 +00001329 const bool segment_is_encrypted = (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001330
Greg Clayton3046e662013-07-10 01:23:25 +00001331 // Keep a list of mach segments around in case we need to
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001332 // get at data that isn't stored in the abstracted Sections.
Greg Clayton3046e662013-07-10 01:23:25 +00001333 m_mach_segments.push_back (load_cmd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334
Greg Clayton3046e662013-07-10 01:23:25 +00001335 // Use a segment ID of the segment index shifted left by 8 so they
1336 // never conflict with any of the sections.
1337 SectionSP segment_sp;
1338 if (add_section && (const_segname || is_core))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339 {
Greg Clayton3046e662013-07-10 01:23:25 +00001340 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
1341 this, // Object file to which this sections belongs
1342 ++segID << 8, // Section ID is the 1 based segment index shifted right by 8 bits as not to collide with any of the 256 section IDs that are possible
1343 const_segname, // Name of this section
1344 eSectionTypeContainer, // This section is a container of other sections.
1345 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1346 load_cmd.vmsize, // VM size in bytes of this section
1347 load_cmd.fileoff, // Offset to the data for this section in the file
1348 load_cmd.filesize, // Size in bytes of this section as found in the the file
1349 load_cmd.flags)); // Flags for this section
Greg Clayton8d38ac42010-06-28 23:51:11 +00001350
Greg Clayton3046e662013-07-10 01:23:25 +00001351 segment_sp->SetIsEncrypted (segment_is_encrypted);
1352 m_sections_ap->AddSection(segment_sp);
1353 if (add_to_unified)
1354 unified_section_list.AddSection(segment_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001355 }
Greg Clayton3046e662013-07-10 01:23:25 +00001356 else if (unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357 {
Jason Molenda20eb31b2013-08-16 03:20:42 +00001358 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr)
1359 {
1360 // Check to see if the module was read from memory?
1361 if (module_sp->GetObjectFile()->GetHeaderAddress().IsValid())
1362 {
1363 // We have a module that is in memory and needs to have its
1364 // file address adjusted. We need to do this because when we
1365 // load a file from memory, its addresses will be slid already,
1366 // yet the addresses in the new symbol file will still be unslid.
1367 // Since everything is stored as section offset, this shouldn't
1368 // cause any problems.
Jason Molenda5894a732013-08-17 03:39:52 +00001369
1370 // Make sure we've parsed the symbol table from the
1371 // ObjectFile before we go around changing its Sections.
1372 module_sp->GetObjectFile()->GetSymtab();
1373 // eh_frame would present the same problems but we parse that on
1374 // a per-function basis as-needed so it's more difficult to
1375 // remove its use of the Sections. Realistically, the environments
1376 // where this code path will be taken will not have eh_frame sections.
1377
Jason Molenda20eb31b2013-08-16 03:20:42 +00001378 unified_section_sp->SetFileAddress(load_cmd.vmaddr);
1379 }
1380 }
Greg Clayton3046e662013-07-10 01:23:25 +00001381 m_sections_ap->AddSection(unified_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001382 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001383
Greg Clayton3046e662013-07-10 01:23:25 +00001384 struct section_64 sect64;
1385 ::memset (&sect64, 0, sizeof(sect64));
1386 // Push a section into our mach sections for the section at
Charles Davis510938e2013-08-27 05:04:57 +00001387 // index zero (NO_SECT) if we don't have any mach sections yet...
Greg Clayton3046e662013-07-10 01:23:25 +00001388 if (m_mach_sections.empty())
1389 m_mach_sections.push_back(sect64);
1390 uint32_t segment_sect_idx;
1391 const lldb::user_id_t first_segment_sectID = sectID + 1;
1392
1393
Charles Davis510938e2013-08-27 05:04:57 +00001394 const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8;
Greg Clayton3046e662013-07-10 01:23:25 +00001395 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001396 {
Greg Clayton3046e662013-07-10 01:23:25 +00001397 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
Greg Clayton89411422010-10-08 00:21:05 +00001398 break;
Greg Clayton3046e662013-07-10 01:23:25 +00001399 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1400 break;
1401 sect64.addr = m_data.GetAddress(&offset);
1402 sect64.size = m_data.GetAddress(&offset);
1403
1404 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1405 break;
1406
1407 // Keep a list of mach sections around in case we need to
1408 // get at data that isn't stored in the abstracted Sections.
1409 m_mach_sections.push_back (sect64);
1410
1411 if (add_section)
1412 {
1413 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1414 if (!const_segname)
1415 {
1416 // We have a segment with no name so we need to conjure up
1417 // segments that correspond to the section's segname if there
1418 // isn't already such a section. If there is such a section,
1419 // we resize the section so that it spans all sections.
1420 // We also mark these sections as fake so address matches don't
1421 // hit if they land in the gaps between the child sections.
1422 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1423 segment_sp = unified_section_list.FindSectionByName (const_segname);
1424 if (segment_sp.get())
1425 {
1426 Section *segment = segment_sp.get();
1427 // Grow the section size as needed.
1428 const lldb::addr_t sect64_min_addr = sect64.addr;
1429 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1430 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1431 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1432 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1433 if (sect64_min_addr >= curr_seg_min_addr)
1434 {
1435 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1436 // Only grow the section size if needed
1437 if (new_seg_byte_size > curr_seg_byte_size)
1438 segment->SetByteSize (new_seg_byte_size);
1439 }
1440 else
1441 {
1442 // We need to change the base address of the segment and
1443 // adjust the child section offsets for all existing children.
1444 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1445 segment->Slide(slide_amount, false);
1446 segment->GetChildren().Slide(-slide_amount, false);
1447 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1448 }
1449
1450 // Grow the section size as needed.
1451 if (sect64.offset)
1452 {
1453 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1454 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1455
1456 const lldb::addr_t section_min_file_offset = sect64.offset;
1457 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1458 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1459 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1460 segment->SetFileOffset (new_file_offset);
1461 segment->SetFileSize (new_file_size);
1462 }
1463 }
1464 else
1465 {
1466 // Create a fake section for the section's named segment
1467 segment_sp.reset(new Section (segment_sp, // Parent section
1468 module_sp, // Module to which this section belongs
1469 this, // Object file to which this section belongs
1470 ++segID << 8, // Section ID is the 1 based segment index shifted right by 8 bits as not to collide with any of the 256 section IDs that are possible
1471 const_segname, // Name of this section
1472 eSectionTypeContainer, // This section is a container of other sections.
1473 sect64.addr, // File VM address == addresses as they are found in the object file
1474 sect64.size, // VM size in bytes of this section
1475 sect64.offset, // Offset to the data for this section in the file
1476 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1477 load_cmd.flags)); // Flags for this section
1478 segment_sp->SetIsFake(true);
1479
1480 m_sections_ap->AddSection(segment_sp);
1481 if (add_to_unified)
1482 unified_section_list.AddSection(segment_sp);
1483 segment_sp->SetIsEncrypted (segment_is_encrypted);
1484 }
1485 }
1486 assert (segment_sp.get());
1487
Charles Davis510938e2013-08-27 05:04:57 +00001488 uint32_t mach_sect_type = sect64.flags & SECTION_TYPE;
Greg Clayton3046e662013-07-10 01:23:25 +00001489 static ConstString g_sect_name_objc_data ("__objc_data");
1490 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1491 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1492 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1493 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1494 static ConstString g_sect_name_objc_const ("__objc_const");
1495 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1496 static ConstString g_sect_name_cfstring ("__cfstring");
1497
1498 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1499 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1500 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1501 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1502 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1503 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1504 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1505 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1506 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1507 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1508 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1509 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1510 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1511 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1512 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1513 static ConstString g_sect_name_eh_frame ("__eh_frame");
1514 static ConstString g_sect_name_DATA ("__DATA");
1515 static ConstString g_sect_name_TEXT ("__TEXT");
1516
Charles Davis510938e2013-08-27 05:04:57 +00001517 lldb::SectionType sect_type = eSectionTypeOther;
Greg Clayton3046e662013-07-10 01:23:25 +00001518
1519 if (section_name == g_sect_name_dwarf_debug_abbrev)
1520 sect_type = eSectionTypeDWARFDebugAbbrev;
1521 else if (section_name == g_sect_name_dwarf_debug_aranges)
1522 sect_type = eSectionTypeDWARFDebugAranges;
1523 else if (section_name == g_sect_name_dwarf_debug_frame)
1524 sect_type = eSectionTypeDWARFDebugFrame;
1525 else if (section_name == g_sect_name_dwarf_debug_info)
1526 sect_type = eSectionTypeDWARFDebugInfo;
1527 else if (section_name == g_sect_name_dwarf_debug_line)
1528 sect_type = eSectionTypeDWARFDebugLine;
1529 else if (section_name == g_sect_name_dwarf_debug_loc)
1530 sect_type = eSectionTypeDWARFDebugLoc;
1531 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1532 sect_type = eSectionTypeDWARFDebugMacInfo;
1533 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1534 sect_type = eSectionTypeDWARFDebugPubNames;
1535 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1536 sect_type = eSectionTypeDWARFDebugPubTypes;
1537 else if (section_name == g_sect_name_dwarf_debug_ranges)
1538 sect_type = eSectionTypeDWARFDebugRanges;
1539 else if (section_name == g_sect_name_dwarf_debug_str)
1540 sect_type = eSectionTypeDWARFDebugStr;
1541 else if (section_name == g_sect_name_dwarf_apple_names)
1542 sect_type = eSectionTypeDWARFAppleNames;
1543 else if (section_name == g_sect_name_dwarf_apple_types)
1544 sect_type = eSectionTypeDWARFAppleTypes;
1545 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1546 sect_type = eSectionTypeDWARFAppleNamespaces;
1547 else if (section_name == g_sect_name_dwarf_apple_objc)
1548 sect_type = eSectionTypeDWARFAppleObjC;
1549 else if (section_name == g_sect_name_objc_selrefs)
1550 sect_type = eSectionTypeDataCStringPointers;
1551 else if (section_name == g_sect_name_objc_msgrefs)
1552 sect_type = eSectionTypeDataObjCMessageRefs;
1553 else if (section_name == g_sect_name_eh_frame)
1554 sect_type = eSectionTypeEHFrame;
1555 else if (section_name == g_sect_name_cfstring)
1556 sect_type = eSectionTypeDataObjCCFStrings;
1557 else if (section_name == g_sect_name_objc_data ||
1558 section_name == g_sect_name_objc_classrefs ||
1559 section_name == g_sect_name_objc_superrefs ||
1560 section_name == g_sect_name_objc_const ||
1561 section_name == g_sect_name_objc_classlist)
1562 {
1563 sect_type = eSectionTypeDataPointers;
1564 }
1565
1566 if (sect_type == eSectionTypeOther)
1567 {
1568 switch (mach_sect_type)
1569 {
1570 // TODO: categorize sections by other flags for regular sections
Charles Davis510938e2013-08-27 05:04:57 +00001571 case S_REGULAR:
Greg Clayton3046e662013-07-10 01:23:25 +00001572 if (segment_sp->GetName() == g_sect_name_TEXT)
1573 sect_type = eSectionTypeCode;
1574 else if (segment_sp->GetName() == g_sect_name_DATA)
1575 sect_type = eSectionTypeData;
1576 else
1577 sect_type = eSectionTypeOther;
1578 break;
Charles Davis510938e2013-08-27 05:04:57 +00001579 case S_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1580 case S_CSTRING_LITERALS: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1581 case S_4BYTE_LITERALS: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1582 case S_8BYTE_LITERALS: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1583 case S_LITERAL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1584 case S_NON_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1585 case S_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1586 case S_SYMBOL_STUBS: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1587 case S_MOD_INIT_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1588 case S_MOD_TERM_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1589 case S_COALESCED: sect_type = eSectionTypeOther; break;
1590 case S_GB_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1591 case S_INTERPOSING: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1592 case S_16BYTE_LITERALS: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1593 case S_DTRACE_DOF: sect_type = eSectionTypeDebug; break;
1594 case S_LAZY_DYLIB_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break;
Greg Clayton3046e662013-07-10 01:23:25 +00001595 default: break;
1596 }
1597 }
1598
1599 SectionSP section_sp(new Section (segment_sp,
1600 module_sp,
1601 this,
1602 ++sectID,
1603 section_name,
1604 sect_type,
1605 sect64.addr - segment_sp->GetFileAddress(),
1606 sect64.size,
1607 sect64.offset,
1608 sect64.offset == 0 ? 0 : sect64.size,
1609 sect64.flags));
1610 // Set the section to be encrypted to match the segment
1611
1612 bool section_is_encrypted = false;
1613 if (!segment_is_encrypted && load_cmd.filesize != 0)
1614 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1615
1616 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1617 segment_sp->GetChildren().AddSection(section_sp);
1618
1619 if (segment_sp->IsFake())
1620 {
1621 segment_sp.reset();
1622 const_segname.Clear();
1623 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001624 }
1625 }
Greg Clayton3046e662013-07-10 01:23:25 +00001626 if (segment_sp && is_dsym)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001627 {
Greg Clayton3046e662013-07-10 01:23:25 +00001628 if (first_segment_sectID <= sectID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001629 {
Greg Clayton3046e662013-07-10 01:23:25 +00001630 lldb::user_id_t sect_uid;
1631 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001632 {
Greg Clayton3046e662013-07-10 01:23:25 +00001633 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1634 SectionSP next_section_sp;
1635 if (sect_uid + 1 <= sectID)
1636 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1637
1638 if (curr_section_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639 {
Greg Clayton3046e662013-07-10 01:23:25 +00001640 if (curr_section_sp->GetByteSize() == 0)
1641 {
1642 if (next_section_sp.get() != NULL)
1643 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1644 else
1645 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1646 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001647 }
1648 }
1649 }
1650 }
1651 }
1652 }
1653 }
Charles Davis510938e2013-08-27 05:04:57 +00001654 else if (load_cmd.cmd == LC_DYSYMTAB)
Greg Clayton3046e662013-07-10 01:23:25 +00001655 {
1656 m_dysymtab.cmd = load_cmd.cmd;
1657 m_dysymtab.cmdsize = load_cmd.cmdsize;
1658 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1659 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001660
Greg Clayton3046e662013-07-10 01:23:25 +00001661 offset = load_cmd_offset + load_cmd.cmdsize;
1662 }
1663
1664// StreamFile s(stdout, false); // REMOVE THIS LINE
1665// s.Printf ("Sections for %s:\n", m_file.GetPath().c_str());// REMOVE THIS LINE
1666// m_sections_ap->Dump(&s, NULL, true, UINT32_MAX);// REMOVE THIS LINE
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001667 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001668}
1669
1670class MachSymtabSectionInfo
1671{
1672public:
1673
1674 MachSymtabSectionInfo (SectionList *section_list) :
1675 m_section_list (section_list),
1676 m_section_infos()
1677 {
1678 // Get the number of sections down to a depth of 1 to include
1679 // all segments and their sections, but no other sections that
1680 // may be added for debug map or
1681 m_section_infos.resize(section_list->GetNumSections(1));
1682 }
1683
1684
Greg Claytone72dfb32012-02-24 01:59:29 +00001685 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001686 GetSection (uint8_t n_sect, addr_t file_addr)
1687 {
1688 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00001689 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001690 if (n_sect < m_section_infos.size())
1691 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001692 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001693 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001694 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1695 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001696 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00001697 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001698 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1699 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00001700 }
1701 else
1702 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001703 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00001704 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001705 }
1706 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00001707 {
1708 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00001709 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001710 }
1711 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1712 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1713 {
1714 // Symbol is in section with zero size, but has the same start
1715 // address as the section. This can happen with linker symbols
1716 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00001717 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001718 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001719 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001720 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001721 }
1722
1723protected:
1724 struct SectionInfo
1725 {
1726 SectionInfo () :
1727 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001728 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001729 {
1730 }
1731
1732 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00001733 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001734 };
1735 SectionList *m_section_list;
1736 std::vector<SectionInfo> m_section_infos;
1737};
1738
Greg Clayton9191db42013-10-21 18:40:51 +00001739struct TrieEntry
1740{
1741 TrieEntry () :
1742 name(),
1743 address(LLDB_INVALID_ADDRESS),
1744 flags (0),
1745 other(0),
1746 import_name()
1747 {
1748 }
1749
1750 void
1751 Clear ()
1752 {
1753 name.Clear();
1754 address = LLDB_INVALID_ADDRESS;
1755 flags = 0;
1756 other = 0;
1757 import_name.Clear();
1758 }
1759
1760 void
1761 Dump () const
1762 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001763 printf ("0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"",
1764 static_cast<unsigned long long>(address),
1765 static_cast<unsigned long long>(flags),
1766 static_cast<unsigned long long>(other), name.GetCString());
Greg Clayton9191db42013-10-21 18:40:51 +00001767 if (import_name)
1768 printf (" -> \"%s\"\n", import_name.GetCString());
1769 else
1770 printf ("\n");
1771 }
1772 ConstString name;
1773 uint64_t address;
1774 uint64_t flags;
1775 uint64_t other;
1776 ConstString import_name;
1777};
1778
1779struct TrieEntryWithOffset
1780{
1781 lldb::offset_t nodeOffset;
1782 TrieEntry entry;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001783
Greg Clayton9191db42013-10-21 18:40:51 +00001784 TrieEntryWithOffset (lldb::offset_t offset) :
1785 nodeOffset (offset),
1786 entry()
1787 {
1788 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001789
Greg Clayton9191db42013-10-21 18:40:51 +00001790 void
1791 Dump (uint32_t idx) const
1792 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001793 printf ("[%3u] 0x%16.16llx: ", idx,
1794 static_cast<unsigned long long>(nodeOffset));
Greg Clayton9191db42013-10-21 18:40:51 +00001795 entry.Dump();
1796 }
1797
1798 bool
1799 operator<(const TrieEntryWithOffset& other) const
1800 {
1801 return ( nodeOffset < other.nodeOffset );
1802 }
1803};
1804
1805static void
1806ParseTrieEntries (DataExtractor &data,
1807 lldb::offset_t offset,
1808 std::vector<llvm::StringRef> &nameSlices,
1809 std::set<lldb::addr_t> &resolver_addresses,
1810 std::vector<TrieEntryWithOffset>& output)
1811{
1812 if (!data.ValidOffset(offset))
1813 return;
1814
1815 const uint64_t terminalSize = data.GetULEB128(&offset);
1816 lldb::offset_t children_offset = offset + terminalSize;
1817 if ( terminalSize != 0 ) {
1818 TrieEntryWithOffset e (offset);
1819 e.entry.flags = data.GetULEB128(&offset);
1820 const char *import_name = NULL;
1821 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
1822 e.entry.address = 0;
1823 e.entry.other = data.GetULEB128(&offset); // dylib ordinal
1824 import_name = data.GetCStr(&offset);
1825 }
1826 else {
1827 e.entry.address = data.GetULEB128(&offset);
1828 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
1829 {
Jim Inghamea3ac272014-01-10 22:55:37 +00001830 //resolver_addresses.insert(e.entry.address);
Greg Clayton9191db42013-10-21 18:40:51 +00001831 e.entry.other = data.GetULEB128(&offset);
Jim Inghamea3ac272014-01-10 22:55:37 +00001832 resolver_addresses.insert(e.entry.other);
Greg Clayton9191db42013-10-21 18:40:51 +00001833 }
1834 else
1835 e.entry.other = 0;
1836 }
1837 // Only add symbols that are reexport symbols with a valid import name
1838 if (EXPORT_SYMBOL_FLAGS_REEXPORT & e.entry.flags && import_name && import_name[0])
1839 {
1840 std::string name;
1841 if (!nameSlices.empty())
1842 {
1843 for (auto name_slice: nameSlices)
1844 name.append(name_slice.data(), name_slice.size());
1845 }
1846 if (name.size() > 1)
1847 {
1848 // Skip the leading '_'
1849 e.entry.name.SetCStringWithLength(name.c_str() + 1,name.size() - 1);
1850 }
1851 if (import_name)
1852 {
1853 // Skip the leading '_'
1854 e.entry.import_name.SetCString(import_name+1);
1855 }
1856 output.push_back(e);
1857 }
1858 }
1859
1860 const uint8_t childrenCount = data.GetU8(&children_offset);
1861 for (uint8_t i=0; i < childrenCount; ++i) {
1862 nameSlices.push_back(data.GetCStr(&children_offset));
1863 lldb::offset_t childNodeOffset = data.GetULEB128(&children_offset);
1864 if (childNodeOffset)
1865 {
1866 ParseTrieEntries(data,
1867 childNodeOffset,
1868 nameSlices,
1869 resolver_addresses,
1870 output);
1871 }
1872 nameSlices.pop_back();
1873 }
1874}
1875
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001876size_t
Greg Clayton3046e662013-07-10 01:23:25 +00001877ObjectFileMachO::ParseSymtab ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001878{
1879 Timer scoped_timer(__PRETTY_FUNCTION__,
1880 "ObjectFileMachO::ParseSymtab () module = %s",
1881 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001882 ModuleSP module_sp (GetModule());
1883 if (!module_sp)
1884 return 0;
1885
1886 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1887 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
Greg Clayton9191db42013-10-21 18:40:51 +00001888 struct dyld_info_command dyld_info = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001889 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1890 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00001891 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001892 uint32_t i;
Greg Clayton9191db42013-10-21 18:40:51 +00001893 FileSpecList dylib_files;
Greg Clayton5160ce52013-03-27 23:08:40 +00001894 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton77ccca72011-12-30 00:32:24 +00001895
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001896 for (i=0; i<m_header.ncmds; ++i)
1897 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001898 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001899 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001900 struct load_command lc;
1901 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001902 break;
1903 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001904 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001905 {
Charles Davis510938e2013-08-27 05:04:57 +00001906 case LC_SYMTAB:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001907 symtab_load_command.cmd = lc.cmd;
1908 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001909 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001910 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1911 return 0;
1912 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001913 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001914 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001915 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001916 return 0;
1917 }
1918
1919 if (symtab_load_command.stroff == 0)
1920 {
1921 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001922 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001923 return 0;
1924 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001925
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001926 if (symtab_load_command.nsyms == 0)
1927 {
1928 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001929 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001930 return 0;
1931 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001932
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001933 if (symtab_load_command.strsize == 0)
1934 {
1935 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001936 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001937 return 0;
1938 }
1939 break;
1940
Greg Clayton9191db42013-10-21 18:40:51 +00001941 case LC_DYLD_INFO:
1942 case LC_DYLD_INFO_ONLY:
1943 if (m_data.GetU32(&offset, &dyld_info.rebase_off, 10))
1944 {
1945 dyld_info.cmd = lc.cmd;
1946 dyld_info.cmdsize = lc.cmdsize;
1947 }
1948 else
1949 {
1950 memset (&dyld_info, 0, sizeof(dyld_info));
1951 }
1952 break;
1953
1954 case LC_LOAD_DYLIB:
1955 case LC_LOAD_WEAK_DYLIB:
1956 case LC_REEXPORT_DYLIB:
1957 case LC_LOADFVMLIB:
1958 case LC_LOAD_UPWARD_DYLIB:
1959 {
1960 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1961 const char *path = m_data.PeekCStr(name_offset);
1962 if (path)
1963 {
1964 FileSpec file_spec(path, false);
1965 // Strip the path if there is @rpath, @executanble, etc so we just use the basename
1966 if (path[0] == '@')
1967 file_spec.GetDirectory().Clear();
Jim Inghamfbe0b9a2014-05-21 03:58:03 +00001968
1969 if (lc.cmd == LC_REEXPORT_DYLIB)
1970 {
1971 m_reexported_dylibs.AppendIfUnique(file_spec);
1972 }
Greg Clayton9191db42013-10-21 18:40:51 +00001973
1974 dylib_files.Append(file_spec);
1975 }
1976 }
1977 break;
1978
Charles Davis510938e2013-08-27 05:04:57 +00001979 case LC_FUNCTION_STARTS:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001980 function_starts_load_command.cmd = lc.cmd;
1981 function_starts_load_command.cmdsize = lc.cmdsize;
1982 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001983 memset (&function_starts_load_command, 0, sizeof(function_starts_load_command));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001984 break;
1985
1986 default:
1987 break;
1988 }
1989 offset = cmd_offset + lc.cmdsize;
1990 }
1991
1992 if (symtab_load_command.cmd)
1993 {
1994 Symtab *symtab = m_symtab_ap.get();
1995 SectionList *section_list = GetSectionList();
1996 if (section_list == NULL)
1997 return 0;
1998
Greg Claytonc7bece562013-01-25 18:06:21 +00001999 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
2000 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002001 bool bit_width_32 = addr_byte_size == 4;
2002 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
2003
Greg Claytonc7bece562013-01-25 18:06:21 +00002004 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
2005 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
2006 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00002007 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00002008 DataExtractor dyld_trie_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002009
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002010 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
2011 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00002012 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
Greg Claytonfd814c52013-08-13 01:42:25 +00002013
2014 ProcessSP process_sp (m_process_wp.lock());
2015 Process *process = process_sp.get();
2016
Greg Clayton86eac942013-08-13 21:32:34 +00002017 uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete;
2018
Greg Clayton4c82d422012-05-18 23:20:01 +00002019 if (process)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002020 {
Greg Clayton4c82d422012-05-18 23:20:01 +00002021 Target &target = process->GetTarget();
Greg Claytonfd814c52013-08-13 01:42:25 +00002022
Greg Clayton86eac942013-08-13 21:32:34 +00002023 memory_module_load_level = target.GetMemoryModuleLoadLevel();
Greg Claytonfd814c52013-08-13 01:42:25 +00002024
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002025 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2026 // Reading mach file from memory in a process or core file...
2027
2028 if (linkedit_section_sp)
2029 {
2030 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
2031 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2032 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00002033 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00002034
2035 bool data_was_read = false;
2036
Jason Molendaa3329782014-03-29 18:54:20 +00002037#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__))
2038 if (m_header.flags & 0x80000000u && process->GetAddressByteSize() == sizeof (void*))
Greg Clayton77ccca72011-12-30 00:32:24 +00002039 {
Greg Clayton26b47e22012-04-18 05:19:20 +00002040 // This mach-o memory file is in the dyld shared cache. If this
2041 // program is not remote and this is iOS, then this process will
2042 // share the same shared cache as the process we are debugging and
2043 // we can read the entire __LINKEDIT from the address space in this
2044 // process. This is a needed optimization that is used for local iOS
2045 // debugging only since all shared libraries in the shared cache do
2046 // not have corresponding files that exist in the file system of the
2047 // device. They have been combined into a single file. This means we
2048 // always have to load these files from memory. All of the symbol and
2049 // string tables from all of the __LINKEDIT sections from the shared
2050 // libraries in the shared cache have been merged into a single large
2051 // symbol and string table. Reading all of this symbol and string table
2052 // data across can slow down debug launch times, so we optimize this by
2053 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00002054
2055 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
2056 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
2057 bool use_lldb_cache = true;
2058 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
2059 {
2060 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002061 ModuleSP module_sp (GetModule());
2062 if (module_sp)
2063 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
2064
Jason Molenda0e0954c2013-04-16 06:24:42 +00002065 }
2066
Greg Clayton26b47e22012-04-18 05:19:20 +00002067 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00002068 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00002069 {
2070 data_was_read = true;
2071 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00002072 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00002073 if (function_starts_load_command.cmd)
2074 {
2075 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2076 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
2077 }
2078 }
2079 }
2080#endif
2081
2082 if (!data_was_read)
2083 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002084 if (memory_module_load_level == eMemoryModuleLoadLevelComplete)
Jason Molendad34e6522013-02-05 22:31:24 +00002085 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002086 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
2087 if (nlist_data_sp)
2088 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2089 // Load strings individually from memory when loading from memory since shared cache
2090 // string tables contain strings for all symbols from all shared cached libraries
2091 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
2092 //if (strtab_data_sp)
2093 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
2094 if (m_dysymtab.nindirectsyms != 0)
2095 {
2096 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
2097 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
2098 if (indirect_syms_data_sp)
2099 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
2100 }
Jason Molendad34e6522013-02-05 22:31:24 +00002101 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002102
2103 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial)
Greg Clayton26b47e22012-04-18 05:19:20 +00002104 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002105 if (function_starts_load_command.cmd)
2106 {
2107 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2108 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
2109 if (func_start_data_sp)
2110 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
2111 }
Greg Clayton26b47e22012-04-18 05:19:20 +00002112 }
Greg Clayton77ccca72011-12-30 00:32:24 +00002113 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002114 }
2115 }
2116 else
2117 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002118 nlist_data.SetData (m_data,
2119 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002120 nlist_data_byte_size);
2121 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00002122 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002123 strtab_data_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00002124
2125 if (dyld_info.export_size > 0)
2126 {
2127 dyld_trie_data.SetData (m_data,
2128 dyld_info.export_off,
2129 dyld_info.export_size);
2130 }
2131
Jason Molendad34e6522013-02-05 22:31:24 +00002132 if (m_dysymtab.nindirectsyms != 0)
2133 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002134 indirect_symbol_index_data.SetData (m_data,
2135 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00002136 m_dysymtab.nindirectsyms * 4);
2137 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002138 if (function_starts_load_command.cmd)
2139 {
2140 function_starts_data.SetData (m_data,
2141 function_starts_load_command.dataoff,
2142 function_starts_load_command.datasize);
2143 }
2144 }
Greg Clayton77ccca72011-12-30 00:32:24 +00002145
Greg Clayton86eac942013-08-13 21:32:34 +00002146 if (nlist_data.GetByteSize() == 0 && memory_module_load_level == eMemoryModuleLoadLevelComplete)
2147 {
2148 if (log)
2149 module_sp->LogMessage(log, "failed to read nlist data");
2150 return 0;
2151 }
2152
2153
Greg Claytondebb8812012-05-25 17:04:00 +00002154 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2155 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00002156 {
Greg Claytondebb8812012-05-25 17:04:00 +00002157 if (process)
2158 {
2159 if (strtab_addr == LLDB_INVALID_ADDRESS)
2160 {
2161 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002162 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00002163 return 0;
2164 }
2165 }
2166 else
Greg Clayton4c82d422012-05-18 23:20:01 +00002167 {
2168 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002169 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00002170 return 0;
2171 }
2172 }
Greg Clayton4c82d422012-05-18 23:20:01 +00002173
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002174 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
2175 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
2176 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
2177 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
2178 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
2179 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
2180 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
2181 SectionSP eh_frame_section_sp;
2182 if (text_section_sp.get())
2183 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
2184 else
2185 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
2186
Charles Davis510938e2013-08-27 05:04:57 +00002187 const bool is_arm = (m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
Jason Molenda5635f772013-03-21 03:36:01 +00002188
2189 // lldb works best if it knows the start addresss of all functions in a module.
2190 // Linker symbols or debug info are normally the best source of information for start addr / size but
2191 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00002192 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00002193 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
2194 // binary, relative to the text section.
2195 // eh_frame - the eh_frame FDEs have the start addr & size of each function
2196 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
2197 // Binaries built to run on older releases may need to use eh_frame information.
2198
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002199 if (text_section_sp && function_starts_data.GetByteSize())
2200 {
2201 FunctionStarts::Entry function_start_entry;
2202 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00002203 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002204 function_start_entry.addr = text_section_sp->GetFileAddress();
2205 uint64_t delta;
2206 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
2207 {
2208 // Now append the current entry
2209 function_start_entry.addr += delta;
2210 function_starts.Append(function_start_entry);
2211 }
Jason Molendad63d3c72013-04-16 00:18:44 +00002212 }
Jason Molenda5635f772013-03-21 03:36:01 +00002213 else
2214 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00002215 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
2216 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
2217 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
2218 // the module.
2219 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00002220 {
2221 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
2222 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
2223 eh_frame.GetFunctionAddressAndSizeVector (functions);
2224 addr_t text_base_addr = text_section_sp->GetFileAddress();
2225 size_t count = functions.GetSize();
2226 for (size_t i = 0; i < count; ++i)
2227 {
2228 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
2229 if (func)
2230 {
2231 FunctionStarts::Entry function_start_entry;
2232 function_start_entry.addr = func->base - text_base_addr;
2233 function_starts.Append(function_start_entry);
2234 }
2235 }
2236 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002237 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002238
Greg Claytonc7bece562013-01-25 18:06:21 +00002239 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002240
Charles Davis510938e2013-08-27 05:04:57 +00002241 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NO_SECT;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002242
Greg Claytonc7bece562013-01-25 18:06:21 +00002243 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002244
2245 uint32_t N_SO_index = UINT32_MAX;
2246
2247 MachSymtabSectionInfo section_info (section_list);
2248 std::vector<uint32_t> N_FUN_indexes;
2249 std::vector<uint32_t> N_NSYM_indexes;
2250 std::vector<uint32_t> N_INCL_indexes;
2251 std::vector<uint32_t> N_BRAC_indexes;
2252 std::vector<uint32_t> N_COMM_indexes;
Greg Claytond81088c2014-01-16 01:38:29 +00002253 typedef std::multimap <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002254 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00002255 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002256 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2257 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00002258 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002259 // Any symbols that get merged into another will get an entry
2260 // in this map so we know
2261 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2262 uint32_t nlist_idx = 0;
2263 Symbol *symbol_ptr = NULL;
2264
2265 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00002266 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00002267 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00002268 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00002269 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002270
Jim Inghamea3ac272014-01-10 22:55:37 +00002271 std::vector<TrieEntryWithOffset> trie_entries;
2272 std::set<lldb::addr_t> resolver_addresses;
2273
2274 if (dyld_trie_data.GetByteSize() > 0)
2275 {
2276 std::vector<llvm::StringRef> nameSlices;
2277 ParseTrieEntries (dyld_trie_data,
2278 0,
2279 nameSlices,
2280 resolver_addresses,
2281 trie_entries);
2282
2283 ConstString text_segment_name ("__TEXT");
2284 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
2285 if (text_segment_sp)
2286 {
2287 const lldb::addr_t text_segment_file_addr = text_segment_sp->GetFileAddress();
2288 if (text_segment_file_addr != LLDB_INVALID_ADDRESS)
2289 {
2290 for (auto &e : trie_entries)
2291 e.entry.address += text_segment_file_addr;
2292 }
2293 }
2294 }
2295
Jason Molendaa3329782014-03-29 18:54:20 +00002296#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__))
Jason Molendaa5609c82012-06-21 01:51:02 +00002297
2298 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
2299 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
2300 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
2301 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
2302 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
2303 // nlist parser to ignore all LOCAL symbols.
2304
2305 if (m_header.flags & 0x80000000u)
2306 {
2307 // Before we can start mapping the DSC, we need to make certain the target process is actually
2308 // using the cache we can find.
2309
Jason Molendaa5609c82012-06-21 01:51:02 +00002310 // Next we need to determine the correct path for the dyld shared cache.
2311
Greg Clayton7ab7f892014-05-29 21:33:45 +00002312 ArchSpec header_arch;
2313 GetArchitecture(header_arch);
Jason Molendaa5609c82012-06-21 01:51:02 +00002314 char dsc_path[PATH_MAX];
2315
2316 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00002317 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
2318 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00002319 header_arch.GetArchitectureName());
2320
2321 FileSpec dsc_filespec(dsc_path, false);
2322
2323 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molendad63d3c72013-04-16 00:18:44 +00002324 struct lldb_copy_dyld_cache_header_v0
Greg Clayton946f8902012-09-05 22:30:51 +00002325 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002326 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
2327 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
2328 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda4e7511e2013-03-06 23:19:17 +00002329 uint32_t imagesOffset;
2330 uint32_t imagesCount;
2331 uint64_t dyldBaseAddress;
2332 uint64_t codeSignatureOffset;
2333 uint64_t codeSignatureSize;
2334 uint64_t slideInfoOffset;
2335 uint64_t slideInfoSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00002336 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
2337 uint64_t localSymbolsSize; // size of local symbols information
2338 };
2339 struct lldb_copy_dyld_cache_header_v1
2340 {
2341 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
2342 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
2343 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
2344 uint32_t imagesOffset;
2345 uint32_t imagesCount;
2346 uint64_t dyldBaseAddress;
2347 uint64_t codeSignatureOffset;
2348 uint64_t codeSignatureSize;
2349 uint64_t slideInfoOffset;
2350 uint64_t slideInfoSize;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002351 uint64_t localSymbolsOffset;
2352 uint64_t localSymbolsSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00002353 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Clayton946f8902012-09-05 22:30:51 +00002354 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00002355
Jason Molendad63d3c72013-04-16 00:18:44 +00002356 struct lldb_copy_dyld_cache_mapping_info
2357 {
2358 uint64_t address;
2359 uint64_t size;
2360 uint64_t fileOffset;
2361 uint32_t maxProt;
2362 uint32_t initProt;
2363 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00002364
Greg Clayton946f8902012-09-05 22:30:51 +00002365 struct lldb_copy_dyld_cache_local_symbols_info
2366 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002367 uint32_t nlistOffset;
2368 uint32_t nlistCount;
2369 uint32_t stringsOffset;
2370 uint32_t stringsSize;
2371 uint32_t entriesOffset;
2372 uint32_t entriesCount;
Greg Clayton946f8902012-09-05 22:30:51 +00002373 };
2374 struct lldb_copy_dyld_cache_local_symbols_entry
2375 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002376 uint32_t dylibOffset;
2377 uint32_t nlistStartIndex;
2378 uint32_t nlistCount;
Greg Clayton946f8902012-09-05 22:30:51 +00002379 };
Jason Molendaa5609c82012-06-21 01:51:02 +00002380
Jason Molendaf8130862012-06-22 03:28:35 +00002381 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
2382 The dyld_cache_local_symbols_info structure gives us three things:
2383 1. The start and count of the nlist records in the dyld_shared_cache file
2384 2. The start and size of the strings for these nlist records
2385 3. The start and count of dyld_cache_local_symbols_entry entries
2386
2387 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
2388 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002389 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00002390 and the count of how many nlist records there are for this dylib/framework.
2391 */
2392
Jason Molendaa5609c82012-06-21 01:51:02 +00002393 // Process the dsc header to find the unmapped symbols
2394 //
2395 // Save some VM space, do not map the entire cache in one shot.
2396
Jason Molenda255f9bb2013-03-06 23:17:36 +00002397 DataBufferSP dsc_data_sp;
2398 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
2399
2400 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00002401 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002402 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002403
Jason Molenda255f9bb2013-03-06 23:17:36 +00002404 char version_str[17];
2405 int version = -1;
2406 lldb::offset_t offset = 0;
2407 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
2408 version_str[16] = '\0';
2409 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
2410 {
2411 int v;
2412 if (::sscanf (version_str + 6, "%d", &v) == 1)
2413 {
2414 version = v;
2415 }
2416 }
2417
Jason Molenda0e0954c2013-04-16 06:24:42 +00002418 UUID dsc_uuid;
2419 if (version >= 1)
2420 {
2421 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
2422 uint8_t uuid_bytes[sizeof (uuid_t)];
2423 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
2424 dsc_uuid.SetBytes (uuid_bytes);
2425 }
2426
2427 bool uuid_match = true;
2428 if (dsc_uuid.IsValid() && process)
2429 {
2430 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
2431
2432 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
2433 {
2434 // The on-disk dyld_shared_cache file is not the same as the one in this
2435 // process' memory, don't use it.
2436 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002437 ModuleSP module_sp (GetModule());
2438 if (module_sp)
2439 module_sp->ReportWarning ("process shared cache does not match on-disk dyld_shared_cache file, some symbol names will be missing.");
Jason Molenda0e0954c2013-04-16 06:24:42 +00002440 }
2441 }
2442
Jason Molenda4e7511e2013-03-06 23:19:17 +00002443 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00002444
Jason Molendaa5609c82012-06-21 01:51:02 +00002445 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
2446
2447 // If the mappingOffset points to a location inside the header, we've
2448 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda0e0954c2013-04-16 06:24:42 +00002449 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendaa5609c82012-06-21 01:51:02 +00002450 {
2451
Jason Molenda255f9bb2013-03-06 23:17:36 +00002452 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
2453 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
2454 offset = 0;
2455
2456 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
2457 // in the shared library cache need to be adjusted by an offset to match up with the
2458 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
2459 // recorded in mapping_offset_value.
2460 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
2461
2462 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00002463 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
2464 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
2465
Jason Molenda4e7511e2013-03-06 23:19:17 +00002466 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00002467 {
2468 // Map the local symbols
Jason Molenda4e7511e2013-03-06 23:19:17 +00002469 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00002470 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002471 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002472
2473 offset = 0;
2474
2475 // Read the local_symbols_infos struct in one shot
2476 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
2477 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
2478
Jason Molendaa5609c82012-06-21 01:51:02 +00002479 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
2480
Jason Molenda255f9bb2013-03-06 23:17:36 +00002481 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002482
2483 offset = local_symbols_info.entriesOffset;
2484 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
2485 {
2486 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
2487 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
2488 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
2489 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
2490
Jason Molenda4e7511e2013-03-06 23:19:17 +00002491 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00002492 {
2493 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
2494
2495 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
2496 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
2497 num_syms = symtab->GetNumSymbols();
2498
2499 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
2500 uint32_t string_table_offset = local_symbols_info.stringsOffset;
2501
Jason Molenda4e7511e2013-03-06 23:19:17 +00002502 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00002503 {
2504 /////////////////////////////
2505 {
2506 struct nlist_64 nlist;
2507 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2508 break;
2509
2510 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2511 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2512 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2513 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2514 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2515
2516 SymbolType type = eSymbolTypeInvalid;
2517 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2518
2519 if (symbol_name == NULL)
2520 {
2521 // No symbol should be NULL, even the symbols with no
2522 // string values should have an offset zero which points
2523 // to an empty C-string
2524 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002525 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Jason Molendaa5609c82012-06-21 01:51:02 +00002526 entry_index,
2527 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002528 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00002529 continue;
2530 }
2531 if (symbol_name[0] == '\0')
2532 symbol_name = NULL;
2533
2534 const char *symbol_name_non_abi_mangled = NULL;
2535
2536 SectionSP symbol_section;
2537 uint32_t symbol_byte_size = 0;
2538 bool add_nlist = true;
Charles Davis510938e2013-08-27 05:04:57 +00002539 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002540 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002541 bool is_gsym = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002542
2543 assert (sym_idx < num_syms);
2544
2545 sym[sym_idx].SetDebug (is_debug);
2546
2547 if (is_debug)
2548 {
2549 switch (nlist.n_type)
2550 {
Charles Davis510938e2013-08-27 05:04:57 +00002551 case N_GSYM:
2552 // global symbol: name,,NO_SECT,type,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002553 // Sometimes the N_GSYM value contains the address.
2554
2555 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2556 // have the same address, but we want to ensure that we always find only the real symbol,
2557 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2558 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2559 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2560 // same address.
2561
2562 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2563 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2564 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2565 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2566 add_nlist = false;
2567 else
2568 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002569 is_gsym = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002570 sym[sym_idx].SetExternal(true);
2571 if (nlist.n_value != 0)
2572 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2573 type = eSymbolTypeData;
2574 }
2575 break;
2576
Charles Davis510938e2013-08-27 05:04:57 +00002577 case N_FNAME:
2578 // procedure name (f77 kludge): name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002579 type = eSymbolTypeCompiler;
2580 break;
2581
Charles Davis510938e2013-08-27 05:04:57 +00002582 case N_FUN:
2583 // procedure: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002584 if (symbol_name)
2585 {
2586 type = eSymbolTypeCode;
2587 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2588
Greg Claytond81088c2014-01-16 01:38:29 +00002589 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002590 // We use the current number of symbols in the symbol table in lieu of
2591 // using nlist_idx in case we ever start trimming entries out
2592 N_FUN_indexes.push_back(sym_idx);
2593 }
2594 else
2595 {
2596 type = eSymbolTypeCompiler;
2597
2598 if ( !N_FUN_indexes.empty() )
2599 {
2600 // Copy the size of the function into the original STAB entry so we don't have
2601 // to hunt for it later
2602 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2603 N_FUN_indexes.pop_back();
2604 // We don't really need the end function STAB as it contains the size which
2605 // we already placed with the original symbol, so don't add it if we want a
2606 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002607 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002608 }
2609 }
2610 break;
2611
Charles Davis510938e2013-08-27 05:04:57 +00002612 case N_STSYM:
2613 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00002614 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002615 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2616 type = eSymbolTypeData;
2617 break;
2618
Charles Davis510938e2013-08-27 05:04:57 +00002619 case N_LCSYM:
2620 // .lcomm symbol: name,,n_sect,type,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002621 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2622 type = eSymbolTypeCommonBlock;
2623 break;
2624
Charles Davis510938e2013-08-27 05:04:57 +00002625 case N_BNSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002626 // We use the current number of symbols in the symbol table in lieu of
2627 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00002628 // Skip these if we want minimal symbol tables
2629 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002630 break;
2631
Charles Davis510938e2013-08-27 05:04:57 +00002632 case N_ENSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002633 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2634 // so that we can always skip the entire symbol if we need to navigate
2635 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00002636 // Skip these if we want minimal symbol tables
2637 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002638 break;
2639
2640
Charles Davis510938e2013-08-27 05:04:57 +00002641 case N_OPT:
2642 // emitted with gcc2_compiled and in gcc source
Jason Molendaa5609c82012-06-21 01:51:02 +00002643 type = eSymbolTypeCompiler;
2644 break;
2645
Charles Davis510938e2013-08-27 05:04:57 +00002646 case N_RSYM:
2647 // register sym: name,,NO_SECT,type,register
Jason Molendaa5609c82012-06-21 01:51:02 +00002648 type = eSymbolTypeVariable;
2649 break;
2650
Charles Davis510938e2013-08-27 05:04:57 +00002651 case N_SLINE:
2652 // src line: 0,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002653 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2654 type = eSymbolTypeLineEntry;
2655 break;
2656
Charles Davis510938e2013-08-27 05:04:57 +00002657 case N_SSYM:
2658 // structure elt: name,,NO_SECT,type,struct_offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002659 type = eSymbolTypeVariableType;
2660 break;
2661
Charles Davis510938e2013-08-27 05:04:57 +00002662 case N_SO:
2663 // source file name
Jason Molendaa5609c82012-06-21 01:51:02 +00002664 type = eSymbolTypeSourceFile;
2665 if (symbol_name == NULL)
2666 {
Greg Clayton3046e662013-07-10 01:23:25 +00002667 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002668 if (N_SO_index != UINT32_MAX)
2669 {
2670 // Set the size of the N_SO to the terminating index of this N_SO
2671 // so that we can always skip the entire N_SO if we need to navigate
2672 // more quickly at the source level when parsing STABS
2673 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00002674 symbol_ptr->SetByteSize(sym_idx);
Jason Molendaa5609c82012-06-21 01:51:02 +00002675 symbol_ptr->SetSizeIsSibling(true);
2676 }
2677 N_NSYM_indexes.clear();
2678 N_INCL_indexes.clear();
2679 N_BRAC_indexes.clear();
2680 N_COMM_indexes.clear();
2681 N_FUN_indexes.clear();
2682 N_SO_index = UINT32_MAX;
2683 }
2684 else
2685 {
2686 // We use the current number of symbols in the symbol table in lieu of
2687 // using nlist_idx in case we ever start trimming entries out
2688 const bool N_SO_has_full_path = symbol_name[0] == '/';
2689 if (N_SO_has_full_path)
2690 {
Greg Clayton3046e662013-07-10 01:23:25 +00002691 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002692 {
2693 // We have two consecutive N_SO entries where the first contains a directory
2694 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00002695 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002696 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2697 add_nlist = false;
2698 }
2699 else
2700 {
2701 // This is the first entry in a N_SO that contains a directory or
2702 // a full path to the source file
2703 N_SO_index = sym_idx;
2704 }
2705 }
Greg Clayton3046e662013-07-10 01:23:25 +00002706 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002707 {
2708 // This is usually the second N_SO entry that contains just the filename,
2709 // so here we combine it with the first one if we are minimizing the symbol table
2710 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2711 if (so_path && so_path[0])
2712 {
2713 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00002714 const size_t double_slash_pos = full_so_path.find("//");
2715 if (double_slash_pos != std::string::npos)
2716 {
2717 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00002718 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Clayton0662d962012-09-07 20:29:13 +00002719 // and the second is the directory for the source file so you end up with
2720 // a path that looks like "/tmp/src//tmp/src/"
2721 FileSpec so_dir(so_path, false);
2722 if (!so_dir.Exists())
2723 {
2724 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2725 if (so_dir.Exists())
2726 {
2727 // Trim off the incorrect path
2728 full_so_path.erase(0, double_slash_pos + 1);
2729 }
2730 }
2731 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002732 if (*full_so_path.rbegin() != '/')
2733 full_so_path += '/';
2734 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00002735 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002736 add_nlist = false;
2737 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2738 }
2739 }
Greg Clayton946f8902012-09-05 22:30:51 +00002740 else
2741 {
2742 // This could be a relative path to a N_SO
2743 N_SO_index = sym_idx;
2744 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002745 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002746 break;
2747
Charles Davis510938e2013-08-27 05:04:57 +00002748 case N_OSO:
2749 // object file name: name,,0,0,st_mtime
Jason Molendaa5609c82012-06-21 01:51:02 +00002750 type = eSymbolTypeObjectFile;
2751 break;
2752
Charles Davis510938e2013-08-27 05:04:57 +00002753 case N_LSYM:
2754 // local sym: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002755 type = eSymbolTypeLocal;
2756 break;
2757
2758 //----------------------------------------------------------------------
2759 // INCL scopes
2760 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00002761 case N_BINCL:
2762 // include file beginning: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00002763 // We use the current number of symbols in the symbol table in lieu of
2764 // using nlist_idx in case we ever start trimming entries out
2765 N_INCL_indexes.push_back(sym_idx);
2766 type = eSymbolTypeScopeBegin;
2767 break;
2768
Charles Davis510938e2013-08-27 05:04:57 +00002769 case N_EINCL:
2770 // include file end: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002771 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2772 // so that we can always skip the entire symbol if we need to navigate
2773 // more quickly at the source level when parsing STABS
2774 if ( !N_INCL_indexes.empty() )
2775 {
2776 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2777 symbol_ptr->SetByteSize(sym_idx + 1);
2778 symbol_ptr->SetSizeIsSibling(true);
2779 N_INCL_indexes.pop_back();
2780 }
2781 type = eSymbolTypeScopeEnd;
2782 break;
2783
Charles Davis510938e2013-08-27 05:04:57 +00002784 case N_SOL:
2785 // #included file name: name,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002786 type = eSymbolTypeHeaderFile;
2787
2788 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00002789 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002790 break;
2791
Charles Davis510938e2013-08-27 05:04:57 +00002792 case N_PARAMS:
2793 // compiler parameters: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002794 type = eSymbolTypeCompiler;
2795 break;
2796
Charles Davis510938e2013-08-27 05:04:57 +00002797 case N_VERSION:
2798 // compiler version: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002799 type = eSymbolTypeCompiler;
2800 break;
2801
Charles Davis510938e2013-08-27 05:04:57 +00002802 case N_OLEVEL:
2803 // compiler -O level: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002804 type = eSymbolTypeCompiler;
2805 break;
2806
Charles Davis510938e2013-08-27 05:04:57 +00002807 case N_PSYM:
2808 // parameter: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002809 type = eSymbolTypeVariable;
2810 break;
2811
Charles Davis510938e2013-08-27 05:04:57 +00002812 case N_ENTRY:
2813 // alternate entry: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002814 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2815 type = eSymbolTypeLineEntry;
2816 break;
2817
2818 //----------------------------------------------------------------------
2819 // Left and Right Braces
2820 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00002821 case N_LBRAC:
2822 // left bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002823 // We use the current number of symbols in the symbol table in lieu of
2824 // using nlist_idx in case we ever start trimming entries out
2825 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2826 N_BRAC_indexes.push_back(sym_idx);
2827 type = eSymbolTypeScopeBegin;
2828 break;
2829
Charles Davis510938e2013-08-27 05:04:57 +00002830 case N_RBRAC:
2831 // right bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002832 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2833 // so that we can always skip the entire symbol if we need to navigate
2834 // more quickly at the source level when parsing STABS
2835 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2836 if ( !N_BRAC_indexes.empty() )
2837 {
2838 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2839 symbol_ptr->SetByteSize(sym_idx + 1);
2840 symbol_ptr->SetSizeIsSibling(true);
2841 N_BRAC_indexes.pop_back();
2842 }
2843 type = eSymbolTypeScopeEnd;
2844 break;
2845
Charles Davis510938e2013-08-27 05:04:57 +00002846 case N_EXCL:
2847 // deleted include file: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00002848 type = eSymbolTypeHeaderFile;
2849 break;
2850
2851 //----------------------------------------------------------------------
2852 // COMM scopes
2853 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00002854 case N_BCOMM:
2855 // begin common: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002856 // We use the current number of symbols in the symbol table in lieu of
2857 // using nlist_idx in case we ever start trimming entries out
2858 type = eSymbolTypeScopeBegin;
2859 N_COMM_indexes.push_back(sym_idx);
2860 break;
2861
Charles Davis510938e2013-08-27 05:04:57 +00002862 case N_ECOML:
2863 // end common (local name): 0,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002864 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2865 // Fall through
2866
Charles Davis510938e2013-08-27 05:04:57 +00002867 case N_ECOMM:
2868 // end common: name,,n_sect,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002869 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2870 // so that we can always skip the entire symbol if we need to navigate
2871 // more quickly at the source level when parsing STABS
2872 if ( !N_COMM_indexes.empty() )
2873 {
2874 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2875 symbol_ptr->SetByteSize(sym_idx + 1);
2876 symbol_ptr->SetSizeIsSibling(true);
2877 N_COMM_indexes.pop_back();
2878 }
2879 type = eSymbolTypeScopeEnd;
2880 break;
2881
Charles Davis510938e2013-08-27 05:04:57 +00002882 case N_LENG:
2883 // second stab entry with length information
Jason Molendaa5609c82012-06-21 01:51:02 +00002884 type = eSymbolTypeAdditional;
2885 break;
2886
2887 default: break;
2888 }
2889 }
2890 else
2891 {
Charles Davis510938e2013-08-27 05:04:57 +00002892 //uint8_t n_pext = N_PEXT & nlist.n_type;
2893 uint8_t n_type = N_TYPE & nlist.n_type;
2894 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Jason Molendaa5609c82012-06-21 01:51:02 +00002895
2896 switch (n_type)
2897 {
Charles Davis510938e2013-08-27 05:04:57 +00002898 case N_INDR: // Fall through
2899 case N_PBUD: // Fall through
2900 case N_UNDF:
Jason Molendaa5609c82012-06-21 01:51:02 +00002901 type = eSymbolTypeUndefined;
2902 break;
2903
Charles Davis510938e2013-08-27 05:04:57 +00002904 case N_ABS:
Jason Molendaa5609c82012-06-21 01:51:02 +00002905 type = eSymbolTypeAbsolute;
2906 break;
2907
Charles Davis510938e2013-08-27 05:04:57 +00002908 case N_SECT:
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002909 {
2910 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002911
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002912 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00002913 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002914 // TODO: warn about this?
2915 add_nlist = false;
2916 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002917 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002918
2919 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00002920 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002921 type = eSymbolTypeException;
2922 }
2923 else
2924 {
Charles Davis510938e2013-08-27 05:04:57 +00002925 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002926
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002927 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00002928 {
Charles Davis510938e2013-08-27 05:04:57 +00002929 case S_REGULAR: break; // regular section
2930 //case S_ZEROFILL: type = eSymbolTypeData; break; // zero fill on demand section
2931 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
2932 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
2933 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
2934 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2935 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2936 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2937 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2938 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2939 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
2940 //case S_COALESCED: type = eSymbolType; break; // section contains symbols that are to be coalesced
2941 //case S_GB_ZEROFILL: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2942 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2943 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
2944 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
2945 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002946 default: break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002947 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002948
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002949 if (type == eSymbolTypeInvalid)
2950 {
2951 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2952 if (symbol_section->IsDescendant (text_section_sp.get()))
2953 {
Charles Davis510938e2013-08-27 05:04:57 +00002954 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
2955 S_ATTR_SELF_MODIFYING_CODE |
2956 S_ATTR_SOME_INSTRUCTIONS))
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002957 type = eSymbolTypeData;
2958 else
2959 type = eSymbolTypeCode;
2960 }
2961 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00002962 {
2963 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2964 {
2965 type = eSymbolTypeRuntime;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002966
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002967 if (symbol_name &&
2968 symbol_name[0] == '_' &&
2969 symbol_name[1] == 'O' &&
Jason Molendaa5609c82012-06-21 01:51:02 +00002970 symbol_name[2] == 'B')
2971 {
2972 llvm::StringRef symbol_name_ref(symbol_name);
2973 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2974 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2975 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2976 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2977 {
2978 symbol_name_non_abi_mangled = symbol_name + 1;
2979 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2980 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002981 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002982 }
2983 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2984 {
2985 symbol_name_non_abi_mangled = symbol_name + 1;
2986 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2987 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002988 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002989 }
2990 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2991 {
2992 symbol_name_non_abi_mangled = symbol_name + 1;
2993 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2994 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002995 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002996 }
2997 }
2998 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002999 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00003000 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003001 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00003002 }
3003 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003004 {
3005 type = eSymbolTypeData;
3006 }
3007 }
3008 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3009 {
3010 type = eSymbolTypeTrampoline;
3011 }
3012 else if (symbol_section->IsDescendant(objc_section_sp.get()))
3013 {
3014 type = eSymbolTypeRuntime;
3015 if (symbol_name && symbol_name[0] == '.')
3016 {
3017 llvm::StringRef symbol_name_ref(symbol_name);
3018 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3019 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00003020 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003021 symbol_name_non_abi_mangled = symbol_name;
3022 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3023 type = eSymbolTypeObjCClass;
3024 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003025 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003026 }
3027 }
3028 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003029 }
3030 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003031 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003032 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003033 }
3034
3035 if (add_nlist)
3036 {
3037 uint64_t symbol_value = nlist.n_value;
Jason Molendaa5609c82012-06-21 01:51:02 +00003038 if (symbol_name_non_abi_mangled)
3039 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00003040 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3041 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00003042 }
3043 else
3044 {
Greg Clayton3046e662013-07-10 01:23:25 +00003045 bool symbol_name_is_mangled = false;
3046
Jason Molendaa5609c82012-06-21 01:51:02 +00003047 if (symbol_name && symbol_name[0] == '_')
3048 {
3049 symbol_name_is_mangled = symbol_name[1] == '_';
3050 symbol_name++; // Skip the leading underscore
3051 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003052
Jason Molendaa5609c82012-06-21 01:51:02 +00003053 if (symbol_name)
3054 {
Greg Claytondacc4a92013-05-14 22:19:37 +00003055 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00003056 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00003057 if (is_gsym && is_debug)
3058 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
Jason Molendaa5609c82012-06-21 01:51:02 +00003059 }
3060 }
3061 if (symbol_section)
3062 {
3063 const addr_t section_file_addr = symbol_section->GetFileAddress();
3064 if (symbol_byte_size == 0 && function_starts_count > 0)
3065 {
3066 addr_t symbol_lookup_file_addr = nlist.n_value;
3067 // Do an exact address match for non-ARM addresses, else get the closest since
3068 // the symbol might be a thumb symbol which has an address with bit zero set
3069 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3070 if (is_arm && func_start_entry)
3071 {
3072 // Verify that the function start address is the symbol address (ARM)
3073 // or the symbol address + 1 (thumb)
3074 if (func_start_entry->addr != symbol_lookup_file_addr &&
3075 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3076 {
3077 // Not the right entry, NULL it out...
3078 func_start_entry = NULL;
3079 }
3080 }
3081 if (func_start_entry)
3082 {
3083 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003084
Jason Molendaa5609c82012-06-21 01:51:02 +00003085 addr_t symbol_file_addr = func_start_entry->addr;
3086 uint32_t symbol_flags = 0;
3087 if (is_arm)
3088 {
3089 if (symbol_file_addr & 1)
3090 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3091 symbol_file_addr &= 0xfffffffffffffffeull;
3092 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003093
Jason Molendaa5609c82012-06-21 01:51:02 +00003094 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3095 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3096 if (next_func_start_entry)
3097 {
3098 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3099 // Be sure the clear the Thumb address bit when we calculate the size
3100 // from the current and next address
3101 if (is_arm)
3102 next_symbol_file_addr &= 0xfffffffffffffffeull;
3103 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3104 }
3105 else
3106 {
3107 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3108 }
3109 }
3110 }
3111 symbol_value -= section_file_addr;
3112 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003113
Greg Claytondacc4a92013-05-14 22:19:37 +00003114 if (is_debug == false)
3115 {
3116 if (type == eSymbolTypeCode)
3117 {
3118 // See if we can find a N_FUN entry for any code symbols.
3119 // If we do find a match, and the name matches, then we
3120 // can merge the two into just the function symbol to avoid
3121 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003122 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3123 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3124 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00003125 {
Greg Claytond81088c2014-01-16 01:38:29 +00003126 bool found_it = false;
3127 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00003128 {
Greg Claytond81088c2014-01-16 01:38:29 +00003129 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3130 {
3131 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3132 // We just need the flags from the linker symbol, so put these flags
3133 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3134 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3135 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3136 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3137 sym[pos->second].SetType (eSymbolTypeResolver);
3138 sym[sym_idx].Clear();
3139 found_it = true;
3140 break;
3141 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003142 }
Greg Claytond81088c2014-01-16 01:38:29 +00003143 if (found_it)
3144 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00003145 }
Jim Inghamea3ac272014-01-10 22:55:37 +00003146 else
3147 {
Greg Claytond81088c2014-01-16 01:38:29 +00003148 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00003149 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00003150 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003151 }
3152 else if (type == eSymbolTypeData)
3153 {
3154 // See if we can find a N_STSYM entry for any data symbols.
3155 // If we do find a match, and the name matches, then we
3156 // can merge the two into just the Static symbol to avoid
3157 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003158 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3159 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
3160 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00003161 {
Greg Claytond81088c2014-01-16 01:38:29 +00003162 bool found_it = false;
3163 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00003164 {
Greg Claytond81088c2014-01-16 01:38:29 +00003165 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3166 {
3167 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3168 // We just need the flags from the linker symbol, so put these flags
3169 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3170 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3171 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3172 sym[sym_idx].Clear();
3173 found_it = true;
3174 break;
3175 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003176 }
Greg Claytond81088c2014-01-16 01:38:29 +00003177 if (found_it)
3178 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00003179 }
3180 else
3181 {
3182 // Combine N_GSYM stab entries with the non stab symbol
Greg Clayton3046e662013-07-10 01:23:25 +00003183 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
Greg Claytondacc4a92013-05-14 22:19:37 +00003184 if (pos != N_GSYM_name_to_sym_idx.end())
3185 {
3186 const uint32_t GSYM_sym_idx = pos->second;
3187 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3188 // Copy the address, because often the N_GSYM address has an invalid address of zero
3189 // when the global is a common symbol
3190 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3191 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3192 // We just need the flags from the linker symbol, so put these flags
3193 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3194 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3195 sym[sym_idx].Clear();
3196 continue;
3197 }
3198 }
3199 }
3200 }
3201
Jason Molendaa5609c82012-06-21 01:51:02 +00003202 sym[sym_idx].SetID (nlist_idx);
3203 sym[sym_idx].SetType (type);
3204 sym[sym_idx].GetAddress().SetSection (symbol_section);
3205 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3206 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003207
Jason Molendaa5609c82012-06-21 01:51:02 +00003208 if (symbol_byte_size > 0)
3209 sym[sym_idx].SetByteSize(symbol_byte_size);
3210
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003211 if (demangled_is_synthesized)
3212 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00003213 ++sym_idx;
3214 }
3215 else
3216 {
3217 sym[sym_idx].Clear();
3218 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003219
Jason Molendaa5609c82012-06-21 01:51:02 +00003220 }
3221 /////////////////////////////
3222 }
3223 break; // No more entries to consider
3224 }
3225 }
3226 }
3227 }
3228 }
3229 }
3230 }
3231
3232 // Must reset this in case it was mutated above!
3233 nlist_data_offset = 0;
3234#endif
Jim Inghamea3ac272014-01-10 22:55:37 +00003235
Greg Claytonfd814c52013-08-13 01:42:25 +00003236 if (nlist_data.GetByteSize() > 0)
Jason Molendaa5609c82012-06-21 01:51:02 +00003237 {
Jason Molendaa5609c82012-06-21 01:51:02 +00003238
Greg Claytonfd814c52013-08-13 01:42:25 +00003239 // If the sym array was not created while parsing the DSC unmapped
3240 // symbols, create it now.
3241 if (sym == NULL)
Greg Clayton4c82d422012-05-18 23:20:01 +00003242 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003243 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
3244 num_syms = symtab->GetNumSymbols();
3245 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003246
Greg Claytonfd814c52013-08-13 01:42:25 +00003247 if (unmapped_local_symbols_found)
3248 {
3249 assert(m_dysymtab.ilocalsym == 0);
3250 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
3251 nlist_idx = m_dysymtab.nlocalsym;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003252 }
Greg Claytondebb8812012-05-25 17:04:00 +00003253 else
3254 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003255 nlist_idx = 0;
Greg Claytondebb8812012-05-25 17:04:00 +00003256 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003257
Greg Claytonfd814c52013-08-13 01:42:25 +00003258 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003259 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003260 struct nlist_64 nlist;
3261 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
3262 break;
3263
3264 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
3265 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
3266 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
3267 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
3268 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
3269
3270 SymbolType type = eSymbolTypeInvalid;
3271 const char *symbol_name = NULL;
3272
3273 if (have_strtab_data)
Greg Clayton77ccca72011-12-30 00:32:24 +00003274 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003275 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003276
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003277 if (symbol_name == NULL)
3278 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003279 // No symbol should be NULL, even the symbols with no
3280 // string values should have an offset zero which points
3281 // to an empty C-string
3282 Host::SystemLog (Host::eSystemLogError,
3283 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
3284 nlist_idx,
3285 nlist.n_strx,
3286 module_sp->GetFileSpec().GetPath().c_str());
3287 continue;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003288 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003289 if (symbol_name[0] == '\0')
3290 symbol_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003291 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003292 else
3293 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003294 const addr_t str_addr = strtab_addr + nlist.n_strx;
3295 Error str_error;
3296 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
3297 symbol_name = memory_symbol_name.c_str();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003298 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003299 const char *symbol_name_non_abi_mangled = NULL;
3300
3301 SectionSP symbol_section;
3302 lldb::addr_t symbol_byte_size = 0;
3303 bool add_nlist = true;
3304 bool is_gsym = false;
Charles Davis510938e2013-08-27 05:04:57 +00003305 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00003306 bool demangled_is_synthesized = false;
3307
3308 assert (sym_idx < num_syms);
3309
3310 sym[sym_idx].SetDebug (is_debug);
3311
3312 if (is_debug)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003313 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003314 switch (nlist.n_type)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003315 {
Charles Davis510938e2013-08-27 05:04:57 +00003316 case N_GSYM:
3317 // global symbol: name,,NO_SECT,type,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003318 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003319
Greg Claytonfd814c52013-08-13 01:42:25 +00003320 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
3321 // have the same address, but we want to ensure that we always find only the real symbol,
3322 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
3323 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
3324 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
3325 // same address.
Greg Clayton29e08cb2012-03-14 01:53:24 +00003326
Greg Claytonfd814c52013-08-13 01:42:25 +00003327 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
3328 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
3329 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
3330 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
3331 add_nlist = false;
3332 else
3333 {
3334 is_gsym = true;
3335 sym[sym_idx].SetExternal(true);
3336 if (nlist.n_value != 0)
3337 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3338 type = eSymbolTypeData;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003339 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003340 break;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003341
Charles Davis510938e2013-08-27 05:04:57 +00003342 case N_FNAME:
3343 // procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003344 type = eSymbolTypeCompiler;
3345 break;
3346
Charles Davis510938e2013-08-27 05:04:57 +00003347 case N_FUN:
3348 // procedure: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003349 if (symbol_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00003350 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003351 type = eSymbolTypeCode;
3352 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3353
Greg Claytond81088c2014-01-16 01:38:29 +00003354 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003355 // We use the current number of symbols in the symbol table in lieu of
3356 // using nlist_idx in case we ever start trimming entries out
3357 N_FUN_indexes.push_back(sym_idx);
Greg Claytondacc4a92013-05-14 22:19:37 +00003358 }
3359 else
3360 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003361 type = eSymbolTypeCompiler;
3362
3363 if ( !N_FUN_indexes.empty() )
Greg Claytondacc4a92013-05-14 22:19:37 +00003364 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003365 // Copy the size of the function into the original STAB entry so we don't have
3366 // to hunt for it later
3367 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
3368 N_FUN_indexes.pop_back();
3369 // We don't really need the end function STAB as it contains the size which
3370 // we already placed with the original symbol, so don't add it if we want a
3371 // minimal symbol table
3372 add_nlist = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00003373 }
3374 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003375 break;
3376
Charles Davis510938e2013-08-27 05:04:57 +00003377 case N_STSYM:
3378 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00003379 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003380 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3381 type = eSymbolTypeData;
3382 break;
3383
Charles Davis510938e2013-08-27 05:04:57 +00003384 case N_LCSYM:
3385 // .lcomm symbol: name,,n_sect,type,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003386 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3387 type = eSymbolTypeCommonBlock;
3388 break;
3389
Charles Davis510938e2013-08-27 05:04:57 +00003390 case N_BNSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003391 // We use the current number of symbols in the symbol table in lieu of
3392 // using nlist_idx in case we ever start trimming entries out
3393 // Skip these if we want minimal symbol tables
3394 add_nlist = false;
3395 break;
3396
Charles Davis510938e2013-08-27 05:04:57 +00003397 case N_ENSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003398 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
3399 // so that we can always skip the entire symbol if we need to navigate
3400 // more quickly at the source level when parsing STABS
3401 // Skip these if we want minimal symbol tables
3402 add_nlist = false;
3403 break;
3404
3405
Charles Davis510938e2013-08-27 05:04:57 +00003406 case N_OPT:
3407 // emitted with gcc2_compiled and in gcc source
Greg Claytonfd814c52013-08-13 01:42:25 +00003408 type = eSymbolTypeCompiler;
3409 break;
3410
Charles Davis510938e2013-08-27 05:04:57 +00003411 case N_RSYM:
3412 // register sym: name,,NO_SECT,type,register
Greg Claytonfd814c52013-08-13 01:42:25 +00003413 type = eSymbolTypeVariable;
3414 break;
3415
Charles Davis510938e2013-08-27 05:04:57 +00003416 case N_SLINE:
3417 // src line: 0,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003418 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3419 type = eSymbolTypeLineEntry;
3420 break;
3421
Charles Davis510938e2013-08-27 05:04:57 +00003422 case N_SSYM:
3423 // structure elt: name,,NO_SECT,type,struct_offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003424 type = eSymbolTypeVariableType;
3425 break;
3426
Charles Davis510938e2013-08-27 05:04:57 +00003427 case N_SO:
3428 // source file name
Greg Claytonfd814c52013-08-13 01:42:25 +00003429 type = eSymbolTypeSourceFile;
3430 if (symbol_name == NULL)
3431 {
3432 add_nlist = false;
3433 if (N_SO_index != UINT32_MAX)
3434 {
3435 // Set the size of the N_SO to the terminating index of this N_SO
3436 // so that we can always skip the entire N_SO if we need to navigate
3437 // more quickly at the source level when parsing STABS
3438 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
3439 symbol_ptr->SetByteSize(sym_idx);
3440 symbol_ptr->SetSizeIsSibling(true);
3441 }
3442 N_NSYM_indexes.clear();
3443 N_INCL_indexes.clear();
3444 N_BRAC_indexes.clear();
3445 N_COMM_indexes.clear();
3446 N_FUN_indexes.clear();
3447 N_SO_index = UINT32_MAX;
3448 }
3449 else
3450 {
3451 // We use the current number of symbols in the symbol table in lieu of
3452 // using nlist_idx in case we ever start trimming entries out
3453 const bool N_SO_has_full_path = symbol_name[0] == '/';
3454 if (N_SO_has_full_path)
3455 {
3456 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3457 {
3458 // We have two consecutive N_SO entries where the first contains a directory
3459 // and the second contains a full path.
3460 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
3461 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3462 add_nlist = false;
3463 }
3464 else
3465 {
3466 // This is the first entry in a N_SO that contains a directory or
3467 // a full path to the source file
3468 N_SO_index = sym_idx;
3469 }
3470 }
3471 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3472 {
3473 // This is usually the second N_SO entry that contains just the filename,
3474 // so here we combine it with the first one if we are minimizing the symbol table
3475 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
3476 if (so_path && so_path[0])
3477 {
3478 std::string full_so_path (so_path);
3479 const size_t double_slash_pos = full_so_path.find("//");
3480 if (double_slash_pos != std::string::npos)
3481 {
3482 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00003483 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Claytonfd814c52013-08-13 01:42:25 +00003484 // and the second is the directory for the source file so you end up with
3485 // a path that looks like "/tmp/src//tmp/src/"
3486 FileSpec so_dir(so_path, false);
3487 if (!so_dir.Exists())
3488 {
3489 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3490 if (so_dir.Exists())
3491 {
3492 // Trim off the incorrect path
3493 full_so_path.erase(0, double_slash_pos + 1);
3494 }
3495 }
3496 }
3497 if (*full_so_path.rbegin() != '/')
3498 full_so_path += '/';
3499 full_so_path += symbol_name;
3500 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
3501 add_nlist = false;
3502 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3503 }
3504 }
3505 else
3506 {
3507 // This could be a relative path to a N_SO
3508 N_SO_index = sym_idx;
3509 }
3510 }
3511
3512 break;
3513
Charles Davis510938e2013-08-27 05:04:57 +00003514 case N_OSO:
3515 // object file name: name,,0,0,st_mtime
Greg Claytonfd814c52013-08-13 01:42:25 +00003516 type = eSymbolTypeObjectFile;
3517 break;
3518
Charles Davis510938e2013-08-27 05:04:57 +00003519 case N_LSYM:
3520 // local sym: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003521 type = eSymbolTypeLocal;
3522 break;
3523
3524 //----------------------------------------------------------------------
3525 // INCL scopes
3526 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003527 case N_BINCL:
3528 // include file beginning: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00003529 // We use the current number of symbols in the symbol table in lieu of
3530 // using nlist_idx in case we ever start trimming entries out
3531 N_INCL_indexes.push_back(sym_idx);
3532 type = eSymbolTypeScopeBegin;
3533 break;
3534
Charles Davis510938e2013-08-27 05:04:57 +00003535 case N_EINCL:
3536 // include file end: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003537 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3538 // so that we can always skip the entire symbol if we need to navigate
3539 // more quickly at the source level when parsing STABS
3540 if ( !N_INCL_indexes.empty() )
3541 {
3542 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3543 symbol_ptr->SetByteSize(sym_idx + 1);
3544 symbol_ptr->SetSizeIsSibling(true);
3545 N_INCL_indexes.pop_back();
3546 }
3547 type = eSymbolTypeScopeEnd;
3548 break;
3549
Charles Davis510938e2013-08-27 05:04:57 +00003550 case N_SOL:
3551 // #included file name: name,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003552 type = eSymbolTypeHeaderFile;
3553
3554 // We currently don't use the header files on darwin
3555 add_nlist = false;
3556 break;
3557
Charles Davis510938e2013-08-27 05:04:57 +00003558 case N_PARAMS:
3559 // compiler parameters: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003560 type = eSymbolTypeCompiler;
3561 break;
3562
Charles Davis510938e2013-08-27 05:04:57 +00003563 case N_VERSION:
3564 // compiler version: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003565 type = eSymbolTypeCompiler;
3566 break;
3567
Charles Davis510938e2013-08-27 05:04:57 +00003568 case N_OLEVEL:
3569 // compiler -O level: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003570 type = eSymbolTypeCompiler;
3571 break;
3572
Charles Davis510938e2013-08-27 05:04:57 +00003573 case N_PSYM:
3574 // parameter: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003575 type = eSymbolTypeVariable;
3576 break;
3577
Charles Davis510938e2013-08-27 05:04:57 +00003578 case N_ENTRY:
3579 // alternate entry: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003580 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3581 type = eSymbolTypeLineEntry;
3582 break;
3583
3584 //----------------------------------------------------------------------
3585 // Left and Right Braces
3586 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003587 case N_LBRAC:
3588 // left bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003589 // We use the current number of symbols in the symbol table in lieu of
3590 // using nlist_idx in case we ever start trimming entries out
3591 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3592 N_BRAC_indexes.push_back(sym_idx);
3593 type = eSymbolTypeScopeBegin;
3594 break;
3595
Charles Davis510938e2013-08-27 05:04:57 +00003596 case N_RBRAC:
3597 // right bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003598 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3599 // so that we can always skip the entire symbol if we need to navigate
3600 // more quickly at the source level when parsing STABS
3601 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3602 if ( !N_BRAC_indexes.empty() )
3603 {
3604 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3605 symbol_ptr->SetByteSize(sym_idx + 1);
3606 symbol_ptr->SetSizeIsSibling(true);
3607 N_BRAC_indexes.pop_back();
3608 }
3609 type = eSymbolTypeScopeEnd;
3610 break;
3611
Charles Davis510938e2013-08-27 05:04:57 +00003612 case N_EXCL:
3613 // deleted include file: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00003614 type = eSymbolTypeHeaderFile;
3615 break;
3616
3617 //----------------------------------------------------------------------
3618 // COMM scopes
3619 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003620 case N_BCOMM:
3621 // begin common: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003622 // We use the current number of symbols in the symbol table in lieu of
3623 // using nlist_idx in case we ever start trimming entries out
3624 type = eSymbolTypeScopeBegin;
3625 N_COMM_indexes.push_back(sym_idx);
3626 break;
3627
Charles Davis510938e2013-08-27 05:04:57 +00003628 case N_ECOML:
3629 // end common (local name): 0,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003630 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3631 // Fall through
3632
Charles Davis510938e2013-08-27 05:04:57 +00003633 case N_ECOMM:
3634 // end common: name,,n_sect,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003635 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3636 // so that we can always skip the entire symbol if we need to navigate
3637 // more quickly at the source level when parsing STABS
3638 if ( !N_COMM_indexes.empty() )
3639 {
3640 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3641 symbol_ptr->SetByteSize(sym_idx + 1);
3642 symbol_ptr->SetSizeIsSibling(true);
3643 N_COMM_indexes.pop_back();
3644 }
3645 type = eSymbolTypeScopeEnd;
3646 break;
3647
Charles Davis510938e2013-08-27 05:04:57 +00003648 case N_LENG:
3649 // second stab entry with length information
Greg Claytonfd814c52013-08-13 01:42:25 +00003650 type = eSymbolTypeAdditional;
3651 break;
3652
3653 default: break;
3654 }
3655 }
3656 else
3657 {
Charles Davis510938e2013-08-27 05:04:57 +00003658 //uint8_t n_pext = N_PEXT & nlist.n_type;
3659 uint8_t n_type = N_TYPE & nlist.n_type;
3660 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00003661
3662 switch (n_type)
3663 {
Charles Davis510938e2013-08-27 05:04:57 +00003664 case N_INDR:// Fall through
3665 case N_PBUD:// Fall through
3666 case N_UNDF:
Greg Claytonfd814c52013-08-13 01:42:25 +00003667 type = eSymbolTypeUndefined;
3668 break;
3669
Charles Davis510938e2013-08-27 05:04:57 +00003670 case N_ABS:
Greg Claytonfd814c52013-08-13 01:42:25 +00003671 type = eSymbolTypeAbsolute;
3672 break;
3673
Charles Davis510938e2013-08-27 05:04:57 +00003674 case N_SECT:
Greg Claytonfd814c52013-08-13 01:42:25 +00003675 {
3676 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3677
3678 if (!symbol_section)
3679 {
3680 // TODO: warn about this?
3681 add_nlist = false;
3682 break;
3683 }
3684
3685 if (TEXT_eh_frame_sectID == nlist.n_sect)
3686 {
3687 type = eSymbolTypeException;
3688 }
3689 else
3690 {
Charles Davis510938e2013-08-27 05:04:57 +00003691 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Greg Claytonfd814c52013-08-13 01:42:25 +00003692
3693 switch (section_type)
3694 {
Charles Davis510938e2013-08-27 05:04:57 +00003695 case S_REGULAR: break; // regular section
3696 //case S_ZEROFILL: type = eSymbolTypeData; break; // zero fill on demand section
3697 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
3698 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
3699 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
3700 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3701 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3702 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3703 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3704 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3705 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
3706 //case S_COALESCED: type = eSymbolType; break; // section contains symbols that are to be coalesced
3707 //case S_GB_ZEROFILL: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3708 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3709 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
3710 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
3711 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Claytonfd814c52013-08-13 01:42:25 +00003712 default: break;
3713 }
3714
3715 if (type == eSymbolTypeInvalid)
3716 {
3717 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3718 if (symbol_section->IsDescendant (text_section_sp.get()))
3719 {
Charles Davis510938e2013-08-27 05:04:57 +00003720 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
3721 S_ATTR_SELF_MODIFYING_CODE |
3722 S_ATTR_SOME_INSTRUCTIONS))
Greg Claytonfd814c52013-08-13 01:42:25 +00003723 type = eSymbolTypeData;
3724 else
3725 type = eSymbolTypeCode;
3726 }
3727 else
3728 if (symbol_section->IsDescendant(data_section_sp.get()))
3729 {
3730 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
3731 {
3732 type = eSymbolTypeRuntime;
3733
3734 if (symbol_name &&
3735 symbol_name[0] == '_' &&
3736 symbol_name[1] == 'O' &&
3737 symbol_name[2] == 'B')
3738 {
3739 llvm::StringRef symbol_name_ref(symbol_name);
3740 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3741 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3742 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3743 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3744 {
3745 symbol_name_non_abi_mangled = symbol_name + 1;
3746 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3747 type = eSymbolTypeObjCClass;
3748 demangled_is_synthesized = true;
3749 }
3750 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3751 {
3752 symbol_name_non_abi_mangled = symbol_name + 1;
3753 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3754 type = eSymbolTypeObjCMetaClass;
3755 demangled_is_synthesized = true;
3756 }
3757 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3758 {
3759 symbol_name_non_abi_mangled = symbol_name + 1;
3760 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3761 type = eSymbolTypeObjCIVar;
3762 demangled_is_synthesized = true;
3763 }
3764 }
3765 }
3766 else
3767 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3768 {
3769 type = eSymbolTypeException;
3770 }
3771 else
3772 {
3773 type = eSymbolTypeData;
3774 }
3775 }
3776 else
3777 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3778 {
3779 type = eSymbolTypeTrampoline;
3780 }
3781 else
3782 if (symbol_section->IsDescendant(objc_section_sp.get()))
3783 {
3784 type = eSymbolTypeRuntime;
3785 if (symbol_name && symbol_name[0] == '.')
3786 {
3787 llvm::StringRef symbol_name_ref(symbol_name);
3788 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3789 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3790 {
3791 symbol_name_non_abi_mangled = symbol_name;
3792 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3793 type = eSymbolTypeObjCClass;
3794 demangled_is_synthesized = true;
3795 }
3796 }
3797 }
3798 }
3799 }
3800 }
3801 break;
Greg Claytondacc4a92013-05-14 22:19:37 +00003802 }
3803 }
3804
Greg Claytonfd814c52013-08-13 01:42:25 +00003805 if (add_nlist)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003806 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003807 uint64_t symbol_value = nlist.n_value;
3808
3809 if (symbol_name_non_abi_mangled)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003810 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003811 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3812 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
3813 }
3814 else
3815 {
3816 bool symbol_name_is_mangled = false;
3817
3818 if (symbol_name && symbol_name[0] == '_')
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003819 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003820 symbol_name_is_mangled = symbol_name[1] == '_';
3821 symbol_name++; // Skip the leading underscore
3822 }
3823
3824 if (symbol_name)
3825 {
3826 ConstString const_symbol_name(symbol_name);
3827 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
3828 if (is_gsym && is_debug)
3829 {
3830 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
3831 }
3832 }
3833 }
3834 if (symbol_section)
3835 {
3836 const addr_t section_file_addr = symbol_section->GetFileAddress();
3837 if (symbol_byte_size == 0 && function_starts_count > 0)
3838 {
3839 addr_t symbol_lookup_file_addr = nlist.n_value;
3840 // Do an exact address match for non-ARM addresses, else get the closest since
3841 // the symbol might be a thumb symbol which has an address with bit zero set
3842 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3843 if (is_arm && func_start_entry)
3844 {
3845 // Verify that the function start address is the symbol address (ARM)
3846 // or the symbol address + 1 (thumb)
3847 if (func_start_entry->addr != symbol_lookup_file_addr &&
3848 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3849 {
3850 // Not the right entry, NULL it out...
3851 func_start_entry = NULL;
3852 }
3853 }
3854 if (func_start_entry)
3855 {
3856 func_start_entry->data = true;
3857
3858 addr_t symbol_file_addr = func_start_entry->addr;
3859 if (is_arm)
3860 symbol_file_addr &= 0xfffffffffffffffeull;
3861
3862 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3863 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3864 if (next_func_start_entry)
3865 {
3866 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3867 // Be sure the clear the Thumb address bit when we calculate the size
3868 // from the current and next address
3869 if (is_arm)
3870 next_symbol_file_addr &= 0xfffffffffffffffeull;
3871 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3872 }
3873 else
3874 {
3875 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3876 }
3877 }
3878 }
3879 symbol_value -= section_file_addr;
3880 }
3881
3882 if (is_debug == false)
3883 {
3884 if (type == eSymbolTypeCode)
3885 {
3886 // See if we can find a N_FUN entry for any code symbols.
3887 // If we do find a match, and the name matches, then we
3888 // can merge the two into just the function symbol to avoid
3889 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003890 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3891 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3892 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00003893 {
Greg Claytond81088c2014-01-16 01:38:29 +00003894 bool found_it = false;
3895 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00003896 {
Greg Claytond81088c2014-01-16 01:38:29 +00003897 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3898 {
3899 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3900 // We just need the flags from the linker symbol, so put these flags
3901 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3902 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3903 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3904 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3905 sym[pos->second].SetType (eSymbolTypeResolver);
3906 sym[sym_idx].Clear();
3907 found_it = true;
3908 break;
3909 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003910 }
Greg Claytond81088c2014-01-16 01:38:29 +00003911 if (found_it)
3912 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00003913 }
Jim Inghamea3ac272014-01-10 22:55:37 +00003914 else
3915 {
3916 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00003917 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00003918 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003919 }
3920 else if (type == eSymbolTypeData)
3921 {
3922 // See if we can find a N_STSYM entry for any data symbols.
3923 // If we do find a match, and the name matches, then we
3924 // can merge the two into just the Static symbol to avoid
3925 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003926 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3927 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
3928 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00003929 {
Greg Claytond81088c2014-01-16 01:38:29 +00003930 bool found_it = false;
3931 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00003932 {
Greg Claytond81088c2014-01-16 01:38:29 +00003933 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3934 {
3935 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3936 // We just need the flags from the linker symbol, so put these flags
3937 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3938 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3939 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3940 sym[sym_idx].Clear();
3941 found_it = true;
3942 break;
3943 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003944 }
Greg Claytond81088c2014-01-16 01:38:29 +00003945 if (found_it)
3946 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00003947 }
3948 else
3949 {
3950 // Combine N_GSYM stab entries with the non stab symbol
3951 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
3952 if (pos != N_GSYM_name_to_sym_idx.end())
3953 {
3954 const uint32_t GSYM_sym_idx = pos->second;
3955 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3956 // Copy the address, because often the N_GSYM address has an invalid address of zero
3957 // when the global is a common symbol
3958 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3959 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3960 // We just need the flags from the linker symbol, so put these flags
3961 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3962 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3963 sym[sym_idx].Clear();
3964 continue;
3965 }
3966 }
3967 }
3968 }
3969
3970 sym[sym_idx].SetID (nlist_idx);
3971 sym[sym_idx].SetType (type);
3972 sym[sym_idx].GetAddress().SetSection (symbol_section);
3973 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3974 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3975
3976 if (symbol_byte_size > 0)
3977 sym[sym_idx].SetByteSize(symbol_byte_size);
3978
3979 if (demangled_is_synthesized)
3980 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3981
3982 ++sym_idx;
3983 }
3984 else
3985 {
3986 sym[sym_idx].Clear();
3987 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003988 }
3989 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003990
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003991 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3992
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003993 if (function_starts_count > 0)
3994 {
3995 char synthetic_function_symbol[PATH_MAX];
3996 uint32_t num_synthetic_function_symbols = 0;
3997 for (i=0; i<function_starts_count; ++i)
3998 {
3999 if (function_starts.GetEntryRef (i).data == false)
4000 ++num_synthetic_function_symbols;
4001 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004002
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004003 if (num_synthetic_function_symbols > 0)
4004 {
4005 if (num_syms < sym_idx + num_synthetic_function_symbols)
4006 {
4007 num_syms = sym_idx + num_synthetic_function_symbols;
4008 sym = symtab->Resize (num_syms);
4009 }
4010 uint32_t synthetic_function_symbol_idx = 0;
4011 for (i=0; i<function_starts_count; ++i)
4012 {
4013 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
4014 if (func_start_entry->data == false)
4015 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004016 addr_t symbol_file_addr = func_start_entry->addr;
4017 uint32_t symbol_flags = 0;
4018 if (is_arm)
4019 {
4020 if (symbol_file_addr & 1)
4021 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
4022 symbol_file_addr &= 0xfffffffffffffffeull;
4023 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004024 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004025 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004026 {
4027 SectionSP symbol_section (symbol_addr.GetSection());
4028 uint32_t symbol_byte_size = 0;
4029 if (symbol_section)
4030 {
4031 const addr_t section_file_addr = symbol_section->GetFileAddress();
4032 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
4033 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
4034 if (next_func_start_entry)
4035 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004036 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4037 if (is_arm)
4038 next_symbol_file_addr &= 0xfffffffffffffffeull;
4039 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004040 }
4041 else
4042 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004043 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004044 }
4045 snprintf (synthetic_function_symbol,
4046 sizeof(synthetic_function_symbol),
4047 "___lldb_unnamed_function%u$$%s",
4048 ++synthetic_function_symbol_idx,
4049 module_sp->GetFileSpec().GetFilename().GetCString());
4050 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Clayton037520e2012-07-18 23:18:10 +00004051 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004052 sym[sym_idx].SetType (eSymbolTypeCode);
4053 sym[sym_idx].SetIsSynthetic (true);
4054 sym[sym_idx].GetAddress() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004055 if (symbol_flags)
4056 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004057 if (symbol_byte_size)
4058 sym[sym_idx].SetByteSize (symbol_byte_size);
4059 ++sym_idx;
4060 }
4061 }
4062 }
4063 }
4064 }
4065 }
4066
4067 // Trim our symbols down to just what we ended up with after
4068 // removing any symbols.
4069 if (sym_idx < num_syms)
4070 {
4071 num_syms = sym_idx;
4072 sym = symtab->Resize (num_syms);
4073 }
4074
4075 // Now synthesize indirect symbols
4076 if (m_dysymtab.nindirectsyms != 0)
4077 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004078 if (indirect_symbol_index_data.GetByteSize())
4079 {
4080 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
4081
4082 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
4083 {
Charles Davis510938e2013-08-27 05:04:57 +00004084 if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) == S_SYMBOL_STUBS)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004085 {
4086 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
4087 if (symbol_stub_byte_size == 0)
4088 continue;
4089
4090 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
4091
4092 if (num_symbol_stubs == 0)
4093 continue;
4094
4095 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
4096 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
4097 {
4098 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
4099 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
Greg Claytonc7bece562013-01-25 18:06:21 +00004100 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004101 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
4102 {
4103 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Charles Davis510938e2013-08-27 05:04:57 +00004104 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004105 continue;
4106
4107 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
4108 Symbol *stub_symbol = NULL;
4109 if (index_pos != end_index_pos)
4110 {
4111 // We have a remapping from the original nlist index to
4112 // a current symbol index, so just look this up by index
4113 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
4114 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004115 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004116 {
4117 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00004118 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004119 // S_SYMBOL_STUBS
4120 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
4121 }
4122
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004123 if (stub_symbol)
4124 {
4125 Address so_addr(symbol_stub_addr, section_list);
4126
4127 if (stub_symbol->GetType() == eSymbolTypeUndefined)
4128 {
4129 // Change the external symbol into a trampoline that makes sense
4130 // These symbols were N_UNDF N_EXT, and are useless to us, so we
4131 // can re-use them so we don't have to make up a synthetic symbol
4132 // for no good reason.
Greg Clayton9191db42013-10-21 18:40:51 +00004133 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4134 stub_symbol->SetType (eSymbolTypeTrampoline);
4135 else
4136 stub_symbol->SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004137 stub_symbol->SetExternal (false);
4138 stub_symbol->GetAddress() = so_addr;
4139 stub_symbol->SetByteSize (symbol_stub_byte_size);
4140 }
4141 else
4142 {
4143 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00004144 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004145 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00004146 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004147 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00004148 stub_symbol = NULL; // this pointer no longer valid
4149 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004150 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00004151 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton9191db42013-10-21 18:40:51 +00004152 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4153 sym[sym_idx].SetType (eSymbolTypeTrampoline);
4154 else
4155 sym[sym_idx].SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004156 sym[sym_idx].SetIsSynthetic (true);
4157 sym[sym_idx].GetAddress() = so_addr;
4158 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
4159 ++sym_idx;
4160 }
4161 }
Greg Clayton3f839a32012-09-05 01:38:55 +00004162 else
4163 {
4164 if (log)
4165 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
4166 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004167 }
4168 }
4169 }
4170 }
4171 }
4172 }
Greg Clayton9191db42013-10-21 18:40:51 +00004173
4174
4175 if (!trie_entries.empty())
4176 {
4177 for (const auto &e : trie_entries)
4178 {
4179 if (e.entry.import_name)
4180 {
4181 // Make a synthetic symbol to describe re-exported symbol.
4182 if (sym_idx >= num_syms)
4183 sym = symtab->Resize (++num_syms);
4184 sym[sym_idx].SetID (synthetic_sym_id++);
4185 sym[sym_idx].GetMangled() = Mangled(e.entry.name);
4186 sym[sym_idx].SetType (eSymbolTypeReExported);
4187 sym[sym_idx].SetIsSynthetic (true);
4188 sym[sym_idx].SetReExportedSymbolName(e.entry.import_name);
4189 if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize())
4190 {
4191 sym[sym_idx].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(e.entry.other-1));
4192 }
4193 ++sym_idx;
4194 }
4195 }
4196 }
4197
4198
Greg Clayton3046e662013-07-10 01:23:25 +00004199
4200// StreamFile s(stdout, false);
4201// s.Printf ("Symbol table before CalculateSymbolSizes():\n");
4202// symtab->Dump(&s, NULL, eSortOrderNone);
4203 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
4204 symtab->CalculateSymbolSizes();
4205
4206// s.Printf ("Symbol table after CalculateSymbolSizes():\n");
4207// symtab->Dump(&s, NULL, eSortOrderNone);
4208
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004209 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004210 }
4211 return 0;
4212}
4213
4214
4215void
4216ObjectFileMachO::Dump (Stream *s)
4217{
Greg Claytona1743492012-03-13 23:14:29 +00004218 ModuleSP module_sp(GetModule());
4219 if (module_sp)
4220 {
4221 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004222 s->Printf("%p: ", static_cast<void*>(this));
Greg Claytona1743492012-03-13 23:14:29 +00004223 s->Indent();
Charles Davis510938e2013-08-27 05:04:57 +00004224 if (m_header.magic == MH_MAGIC_64 || m_header.magic == MH_CIGAM_64)
Greg Claytona1743492012-03-13 23:14:29 +00004225 s->PutCString("ObjectFileMachO64");
4226 else
4227 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004228
Greg Clayton7ab7f892014-05-29 21:33:45 +00004229 ArchSpec header_arch;
4230 GetArchitecture(header_arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004231
Greg Claytona1743492012-03-13 23:14:29 +00004232 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004233
Greg Clayton3046e662013-07-10 01:23:25 +00004234 SectionList *sections = GetSectionList();
4235 if (sections)
4236 sections->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004237
Greg Claytona1743492012-03-13 23:14:29 +00004238 if (m_symtab_ap.get())
4239 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
4240 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004241}
4242
Greg Claytonf4d6de62013-04-24 22:29:28 +00004243bool
4244ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
4245 const lldb_private::DataExtractor &data,
4246 lldb::offset_t lc_offset,
4247 lldb_private::UUID& uuid)
4248{
4249 uint32_t i;
4250 struct uuid_command load_cmd;
4251
4252 lldb::offset_t offset = lc_offset;
4253 for (i=0; i<header.ncmds; ++i)
4254 {
4255 const lldb::offset_t cmd_offset = offset;
4256 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4257 break;
4258
Charles Davis510938e2013-08-27 05:04:57 +00004259 if (load_cmd.cmd == LC_UUID)
Greg Claytonf4d6de62013-04-24 22:29:28 +00004260 {
4261 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
4262
4263 if (uuid_bytes)
4264 {
4265 // OpenCL on Mac OS X uses the same UUID for each of its object files.
4266 // We pretend these object files have no UUID to prevent crashing.
4267
4268 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
4269 0x3b, 0xa8,
4270 0x4b, 0x16,
4271 0xb6, 0xa4,
4272 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
4273
4274 if (!memcmp(uuid_bytes, opencl_uuid, 16))
4275 return false;
4276
4277 uuid.SetBytes (uuid_bytes);
4278 return true;
4279 }
4280 return false;
4281 }
4282 offset = cmd_offset + load_cmd.cmdsize;
4283 }
4284 return false;
4285}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004286
Greg Clayton7ab7f892014-05-29 21:33:45 +00004287
4288bool
4289ObjectFileMachO::GetArchitecture (const llvm::MachO::mach_header &header,
4290 const lldb_private::DataExtractor &data,
4291 lldb::offset_t lc_offset,
4292 ArchSpec &arch)
4293{
4294 arch.SetArchitecture (eArchTypeMachO, header.cputype, header.cpusubtype);
4295
4296 if (arch.IsValid())
4297 {
4298 llvm::Triple &triple = arch.GetTriple();
4299 if (header.filetype == MH_PRELOAD)
4300 {
4301 // Set OS to "unknown" - this is a standalone binary with no dyld et al
4302 triple.setOS(llvm::Triple::UnknownOS);
4303 return true;
4304 }
4305 else
4306 {
4307 struct load_command load_cmd;
4308
4309 lldb::offset_t offset = lc_offset;
4310 for (uint32_t i=0; i<header.ncmds; ++i)
4311 {
4312 const lldb::offset_t cmd_offset = offset;
4313 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4314 break;
4315
4316 switch (load_cmd.cmd)
4317 {
4318 case LC_VERSION_MIN_IPHONEOS:
4319 triple.setOS (llvm::Triple::IOS);
4320 return true;
4321
4322 case LC_VERSION_MIN_MACOSX:
4323 triple.setOS (llvm::Triple::MacOSX);
4324 return true;
4325
4326 default:
4327 break;
4328 }
4329
4330 offset = cmd_offset + load_cmd.cmdsize;
4331 }
4332
4333 if (header.cputype == CPU_TYPE_ARM || header.cputype == CPU_TYPE_ARM64)
4334 triple.setOS (llvm::Triple::IOS);
4335 else
4336 triple.setOS (llvm::Triple::MacOSX);
4337 }
4338 }
4339 return arch.IsValid();
4340}
4341
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004342bool
Greg Clayton60830262011-02-04 18:53:10 +00004343ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004344{
Greg Claytona1743492012-03-13 23:14:29 +00004345 ModuleSP module_sp(GetModule());
4346 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004347 {
Greg Claytona1743492012-03-13 23:14:29 +00004348 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004349 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00004350 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004351 }
4352 return false;
4353}
4354
4355
4356uint32_t
4357ObjectFileMachO::GetDependentModules (FileSpecList& files)
4358{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004359 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00004360 ModuleSP module_sp(GetModule());
4361 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004362 {
Greg Claytona1743492012-03-13 23:14:29 +00004363 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4364 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004365 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004366 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
4367 uint32_t i;
4368 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004369 {
Greg Claytona1743492012-03-13 23:14:29 +00004370 const uint32_t cmd_offset = offset;
4371 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4372 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004373
Greg Claytona1743492012-03-13 23:14:29 +00004374 switch (load_cmd.cmd)
4375 {
Charles Davis510938e2013-08-27 05:04:57 +00004376 case LC_LOAD_DYLIB:
4377 case LC_LOAD_WEAK_DYLIB:
4378 case LC_REEXPORT_DYLIB:
4379 case LC_LOAD_DYLINKER:
4380 case LC_LOADFVMLIB:
4381 case LC_LOAD_UPWARD_DYLIB:
Greg Claytona1743492012-03-13 23:14:29 +00004382 {
4383 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
4384 const char *path = m_data.PeekCStr(name_offset);
4385 // Skip any path that starts with '@' since these are usually:
4386 // @executable_path/.../file
4387 // @rpath/.../file
4388 if (path && path[0] != '@')
4389 {
4390 FileSpec file_spec(path, resolve_path);
4391 if (files.AppendIfUnique(file_spec))
4392 count++;
4393 }
4394 }
4395 break;
4396
4397 default:
4398 break;
4399 }
4400 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004401 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004402 }
4403 return count;
4404}
4405
Jim Ingham672e6f52011-03-07 23:44:08 +00004406lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00004407ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00004408{
4409 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
4410 // is initialized to an invalid address, so we can just return that.
4411 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
Jason Molenda4e7511e2013-03-06 23:19:17 +00004412
Jim Ingham672e6f52011-03-07 23:44:08 +00004413 if (!IsExecutable() || m_entry_point_address.IsValid())
4414 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004415
4416 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00004417 // /usr/include/mach-o.h, but it is basically:
4418 //
4419 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
4420 // uint32_t count - this is the count of longs in the thread state data
4421 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
4422 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00004423 //
Jim Ingham672e6f52011-03-07 23:44:08 +00004424 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
4425 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
4426 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
4427 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
4428 //
4429 // For now we hard-code the offsets and flavors we need:
4430 //
4431 //
4432
Greg Claytona1743492012-03-13 23:14:29 +00004433 ModuleSP module_sp(GetModule());
4434 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00004435 {
Greg Claytona1743492012-03-13 23:14:29 +00004436 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4437 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004438 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004439 uint32_t i;
4440 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
4441 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004442
Greg Claytona1743492012-03-13 23:14:29 +00004443 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00004444 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004445 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004446 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4447 break;
4448
4449 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00004450 {
Charles Davis510938e2013-08-27 05:04:57 +00004451 case LC_UNIXTHREAD:
4452 case LC_THREAD:
Jim Ingham672e6f52011-03-07 23:44:08 +00004453 {
Greg Claytona1743492012-03-13 23:14:29 +00004454 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00004455 {
Greg Claytona1743492012-03-13 23:14:29 +00004456 uint32_t flavor = m_data.GetU32(&offset);
4457 uint32_t count = m_data.GetU32(&offset);
4458 if (count == 0)
4459 {
4460 // We've gotten off somehow, log and exit;
4461 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00004462 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004463
Greg Claytona1743492012-03-13 23:14:29 +00004464 switch (m_header.cputype)
4465 {
Charles Davis510938e2013-08-27 05:04:57 +00004466 case llvm::MachO::CPU_TYPE_ARM:
Greg Claytona1743492012-03-13 23:14:29 +00004467 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
4468 {
4469 offset += 60; // This is the offset of pc in the GPR thread state data structure.
4470 start_address = m_data.GetU32(&offset);
4471 done = true;
4472 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004473 break;
Jason Molendaa3329782014-03-29 18:54:20 +00004474 case llvm::MachO::CPU_TYPE_ARM64:
4475 if (flavor == 6) // ARM_THREAD_STATE64 from mach/arm/thread_status.h
4476 {
4477 offset += 256; // This is the offset of pc in the GPR thread state data structure.
4478 start_address = m_data.GetU64(&offset);
4479 done = true;
4480 }
4481 break;
Charles Davis510938e2013-08-27 05:04:57 +00004482 case llvm::MachO::CPU_TYPE_I386:
Greg Claytona1743492012-03-13 23:14:29 +00004483 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
4484 {
4485 offset += 40; // This is the offset of eip in the GPR thread state data structure.
4486 start_address = m_data.GetU32(&offset);
4487 done = true;
4488 }
4489 break;
Charles Davis510938e2013-08-27 05:04:57 +00004490 case llvm::MachO::CPU_TYPE_X86_64:
Greg Claytona1743492012-03-13 23:14:29 +00004491 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
4492 {
4493 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
4494 start_address = m_data.GetU64(&offset);
4495 done = true;
4496 }
4497 break;
4498 default:
4499 return m_entry_point_address;
4500 }
4501 // Haven't found the GPR flavor yet, skip over the data for this flavor:
4502 if (done)
4503 break;
4504 offset += count * 4;
4505 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004506 }
Greg Claytona1743492012-03-13 23:14:29 +00004507 break;
Charles Davis510938e2013-08-27 05:04:57 +00004508 case LC_MAIN:
Sean Callanan226b70c2012-03-08 02:39:03 +00004509 {
Greg Claytona1743492012-03-13 23:14:29 +00004510 ConstString text_segment_name ("__TEXT");
4511 uint64_t entryoffset = m_data.GetU64(&offset);
4512 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
4513 if (text_segment_sp)
4514 {
4515 done = true;
4516 start_address = text_segment_sp->GetFileAddress() + entryoffset;
4517 }
Sean Callanan226b70c2012-03-08 02:39:03 +00004518 }
Greg Claytona1743492012-03-13 23:14:29 +00004519
4520 default:
4521 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00004522 }
Greg Claytona1743492012-03-13 23:14:29 +00004523 if (done)
4524 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00004525
Greg Claytona1743492012-03-13 23:14:29 +00004526 // Go to the next load command:
4527 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00004528 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004529
Greg Claytona1743492012-03-13 23:14:29 +00004530 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00004531 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004532 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00004533 // of this ObjectFile:
4534 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00004535 {
Greg Claytona1743492012-03-13 23:14:29 +00004536 m_entry_point_address.Clear();
4537 }
4538 }
4539 else
4540 {
4541 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
4542 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00004543
Greg Claytona1743492012-03-13 23:14:29 +00004544 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00004545
Greg Claytona1743492012-03-13 23:14:29 +00004546 if (module_sp)
4547 {
4548 SymbolContextList contexts;
4549 SymbolContext context;
4550 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
4551 {
4552 if (contexts.GetContextAtIndex(0, context))
4553 m_entry_point_address = context.symbol->GetAddress();
4554 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004555 }
4556 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004557 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004558
Jim Ingham672e6f52011-03-07 23:44:08 +00004559 return m_entry_point_address;
4560
4561}
4562
Greg Claytonc9660542012-02-05 02:38:54 +00004563lldb_private::Address
4564ObjectFileMachO::GetHeaderAddress ()
4565{
4566 lldb_private::Address header_addr;
4567 SectionList *section_list = GetSectionList();
4568 if (section_list)
4569 {
4570 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
4571 if (text_segment_sp)
4572 {
Greg Claytone72dfb32012-02-24 01:59:29 +00004573 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00004574 header_addr.SetOffset (0);
4575 }
4576 }
4577 return header_addr;
4578}
4579
Greg Claytonc3776bf2012-02-09 06:16:32 +00004580uint32_t
4581ObjectFileMachO::GetNumThreadContexts ()
4582{
Greg Claytona1743492012-03-13 23:14:29 +00004583 ModuleSP module_sp(GetModule());
4584 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004585 {
Greg Claytona1743492012-03-13 23:14:29 +00004586 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4587 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004588 {
Greg Claytona1743492012-03-13 23:14:29 +00004589 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00004590 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004591 FileRangeArray::Entry file_range;
4592 thread_command thread_cmd;
4593 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004594 {
Greg Claytona1743492012-03-13 23:14:29 +00004595 const uint32_t cmd_offset = offset;
4596 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
4597 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004598
Charles Davis510938e2013-08-27 05:04:57 +00004599 if (thread_cmd.cmd == LC_THREAD)
Greg Claytona1743492012-03-13 23:14:29 +00004600 {
4601 file_range.SetRangeBase (offset);
4602 file_range.SetByteSize (thread_cmd.cmdsize - 8);
4603 m_thread_context_offsets.Append (file_range);
4604 }
4605 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00004606 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004607 }
4608 }
4609 return m_thread_context_offsets.GetSize();
4610}
4611
4612lldb::RegisterContextSP
4613ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
4614{
Greg Claytonc3776bf2012-02-09 06:16:32 +00004615 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00004616
Greg Claytona1743492012-03-13 23:14:29 +00004617 ModuleSP module_sp(GetModule());
4618 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004619 {
Greg Claytona1743492012-03-13 23:14:29 +00004620 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4621 if (!m_thread_context_offsets_valid)
4622 GetNumThreadContexts ();
4623
4624 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00004625 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00004626 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004627
4628 DataExtractor data (m_data,
4629 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00004630 thread_context_file_range->GetByteSize());
4631
4632 switch (m_header.cputype)
4633 {
Jason Molendaa3329782014-03-29 18:54:20 +00004634 case llvm::MachO::CPU_TYPE_ARM64:
4635 reg_ctx_sp.reset (new RegisterContextDarwin_arm64_Mach (thread, data));
4636 break;
4637
Charles Davis510938e2013-08-27 05:04:57 +00004638 case llvm::MachO::CPU_TYPE_ARM:
Jim Ingham28eb5712012-10-12 17:34:26 +00004639 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
4640 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004641
Charles Davis510938e2013-08-27 05:04:57 +00004642 case llvm::MachO::CPU_TYPE_I386:
Jim Ingham28eb5712012-10-12 17:34:26 +00004643 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
4644 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004645
Charles Davis510938e2013-08-27 05:04:57 +00004646 case llvm::MachO::CPU_TYPE_X86_64:
Jim Ingham28eb5712012-10-12 17:34:26 +00004647 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
4648 break;
4649 }
Greg Claytona1743492012-03-13 23:14:29 +00004650 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004651 }
4652 return reg_ctx_sp;
4653}
4654
Greg Claytonc9660542012-02-05 02:38:54 +00004655
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004656ObjectFile::Type
4657ObjectFileMachO::CalculateType()
4658{
4659 switch (m_header.filetype)
4660 {
Charles Davis510938e2013-08-27 05:04:57 +00004661 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004662 if (GetAddressByteSize () == 4)
4663 {
4664 // 32 bit kexts are just object files, but they do have a valid
4665 // UUID load command.
4666 UUID uuid;
4667 if (GetUUID(&uuid))
4668 {
4669 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004670 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004671 // "OSKextGetCurrentIdentifier" as this is required of kexts
4672 if (m_strata == eStrataInvalid)
4673 m_strata = eStrataKernel;
4674 return eTypeSharedLibrary;
4675 }
4676 }
4677 return eTypeObjectFile;
4678
Charles Davis510938e2013-08-27 05:04:57 +00004679 case MH_EXECUTE: return eTypeExecutable; // 0x2u
4680 case MH_FVMLIB: return eTypeSharedLibrary; // 0x3u
4681 case MH_CORE: return eTypeCoreFile; // 0x4u
4682 case MH_PRELOAD: return eTypeSharedLibrary; // 0x5u
4683 case MH_DYLIB: return eTypeSharedLibrary; // 0x6u
4684 case MH_DYLINKER: return eTypeDynamicLinker; // 0x7u
4685 case MH_BUNDLE: return eTypeSharedLibrary; // 0x8u
4686 case MH_DYLIB_STUB: return eTypeStubLibrary; // 0x9u
4687 case MH_DSYM: return eTypeDebugInfo; // 0xAu
4688 case MH_KEXT_BUNDLE: return eTypeSharedLibrary; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004689 default:
4690 break;
4691 }
4692 return eTypeUnknown;
4693}
4694
4695ObjectFile::Strata
4696ObjectFileMachO::CalculateStrata()
4697{
4698 switch (m_header.filetype)
4699 {
Charles Davis510938e2013-08-27 05:04:57 +00004700 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004701 {
4702 // 32 bit kexts are just object files, but they do have a valid
4703 // UUID load command.
4704 UUID uuid;
4705 if (GetUUID(&uuid))
4706 {
4707 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004708 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004709 // "OSKextGetCurrentIdentifier" as this is required of kexts
4710 if (m_type == eTypeInvalid)
4711 m_type = eTypeSharedLibrary;
4712
4713 return eStrataKernel;
4714 }
4715 }
4716 return eStrataUnknown;
4717
Charles Davis510938e2013-08-27 05:04:57 +00004718 case MH_EXECUTE: // 0x2u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004719 // Check for the MH_DYLDLINK bit in the flags
Charles Davis510938e2013-08-27 05:04:57 +00004720 if (m_header.flags & MH_DYLDLINK)
Sean Callanan49bce8e2012-02-10 20:22:35 +00004721 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004722 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00004723 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004724 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00004725 {
4726 SectionList *section_list = GetSectionList();
4727 if (section_list)
4728 {
4729 static ConstString g_kld_section_name ("__KLD");
4730 if (section_list->FindSectionByName(g_kld_section_name))
4731 return eStrataKernel;
4732 }
4733 }
4734 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004735
Charles Davis510938e2013-08-27 05:04:57 +00004736 case MH_FVMLIB: return eStrataUser; // 0x3u
4737 case MH_CORE: return eStrataUnknown; // 0x4u
4738 case MH_PRELOAD: return eStrataRawImage; // 0x5u
4739 case MH_DYLIB: return eStrataUser; // 0x6u
4740 case MH_DYLINKER: return eStrataUser; // 0x7u
4741 case MH_BUNDLE: return eStrataUser; // 0x8u
4742 case MH_DYLIB_STUB: return eStrataUser; // 0x9u
4743 case MH_DSYM: return eStrataUnknown; // 0xAu
4744 case MH_KEXT_BUNDLE: return eStrataKernel; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004745 default:
4746 break;
4747 }
4748 return eStrataUnknown;
4749}
4750
4751
Greg Claytonc2ff9312012-02-22 19:41:02 +00004752uint32_t
4753ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4754{
Greg Claytona1743492012-03-13 23:14:29 +00004755 ModuleSP module_sp(GetModule());
4756 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004757 {
Greg Claytona1743492012-03-13 23:14:29 +00004758 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4759 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004760 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004761 uint32_t version_cmd = 0;
4762 uint64_t version = 0;
4763 uint32_t i;
4764 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004765 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004766 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004767 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4768 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004769
Charles Davis510938e2013-08-27 05:04:57 +00004770 if (load_cmd.cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004771 {
Greg Claytona1743492012-03-13 23:14:29 +00004772 if (version_cmd == 0)
4773 {
4774 version_cmd = load_cmd.cmd;
4775 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4776 break;
4777 version = load_cmd.dylib.current_version;
4778 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004779 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00004780 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00004781 }
Greg Claytona1743492012-03-13 23:14:29 +00004782 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004783 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004784
Charles Davis510938e2013-08-27 05:04:57 +00004785 if (version_cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004786 {
Greg Claytona1743492012-03-13 23:14:29 +00004787 if (versions != NULL && num_versions > 0)
4788 {
4789 if (num_versions > 0)
4790 versions[0] = (version & 0xFFFF0000ull) >> 16;
4791 if (num_versions > 1)
4792 versions[1] = (version & 0x0000FF00ull) >> 8;
4793 if (num_versions > 2)
4794 versions[2] = (version & 0x000000FFull);
4795 // Fill in an remaining version numbers with invalid values
4796 for (i=3; i<num_versions; ++i)
4797 versions[i] = UINT32_MAX;
4798 }
4799 // The LC_ID_DYLIB load command has a version with 3 version numbers
4800 // in it, so always return 3
4801 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004802 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00004803 }
4804 return false;
4805}
4806
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004807bool
Greg Clayton514487e2011-02-15 21:59:32 +00004808ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004809{
Greg Claytona1743492012-03-13 23:14:29 +00004810 ModuleSP module_sp(GetModule());
4811 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00004812 {
Greg Claytona1743492012-03-13 23:14:29 +00004813 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton7ab7f892014-05-29 21:33:45 +00004814 return GetArchitecture (m_header, m_data, MachHeaderSizeFromMagic(m_header.magic), arch);
Greg Clayton593577a2011-09-21 03:57:31 +00004815 }
Greg Claytona1743492012-03-13 23:14:29 +00004816 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004817}
4818
4819
Jason Molenda0e0954c2013-04-16 06:24:42 +00004820UUID
4821ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4822{
4823 UUID uuid;
4824 if (process)
4825 {
4826 addr_t all_image_infos = process->GetImageInfoAddress();
4827
4828 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4829 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4830 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4831 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4832
4833 Error err;
4834 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00004835 if (version_or_magic != static_cast<uint32_t>(-1)
Charles Davis510938e2013-08-27 05:04:57 +00004836 && version_or_magic != MH_MAGIC
4837 && version_or_magic != MH_CIGAM
4838 && version_or_magic != MH_MAGIC_64
4839 && version_or_magic != MH_CIGAM_64
Jason Molenda0e0954c2013-04-16 06:24:42 +00004840 && version_or_magic >= 13)
4841 {
4842 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4843 int wordsize = process->GetAddressByteSize();
4844 if (wordsize == 8)
4845 {
4846 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4847 }
4848 if (wordsize == 4)
4849 {
4850 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4851 }
4852 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4853 {
4854 uuid_t shared_cache_uuid;
4855 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4856 {
4857 uuid.SetBytes (shared_cache_uuid);
4858 }
4859 }
4860 }
4861 }
4862 return uuid;
4863}
4864
4865UUID
4866ObjectFileMachO::GetLLDBSharedCacheUUID ()
4867{
4868 UUID uuid;
Jason Molendaa3329782014-03-29 18:54:20 +00004869#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__))
Jason Molenda0e0954c2013-04-16 06:24:42 +00004870 uint8_t *(*dyld_get_all_image_infos)(void);
4871 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4872 if (dyld_get_all_image_infos)
4873 {
4874 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4875 if (dyld_all_image_infos_address)
4876 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00004877 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4878 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00004879 {
Jason Molendaa3329782014-03-29 18:54:20 +00004880 uuid_t *sharedCacheUUID_address = 0;
4881 int wordsize = sizeof (uint8_t *);
4882 if (wordsize == 8)
4883 {
4884 sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 160); // sharedCacheUUID <mach-o/dyld_images.h>
4885 }
4886 else
4887 {
4888 sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84); // sharedCacheUUID <mach-o/dyld_images.h>
4889 }
Jason Molenda0e0954c2013-04-16 06:24:42 +00004890 uuid.SetBytes (sharedCacheUUID_address);
4891 }
4892 }
4893 }
4894#endif
4895 return uuid;
4896}
4897
Greg Clayton9b234982013-10-24 22:54:08 +00004898uint32_t
4899ObjectFileMachO::GetMinimumOSVersion (uint32_t *versions, uint32_t num_versions)
4900{
4901 if (m_min_os_versions.empty())
4902 {
4903 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
4904 bool success = false;
4905 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
4906 {
4907 const lldb::offset_t load_cmd_offset = offset;
4908
4909 version_min_command lc;
4910 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
4911 break;
4912 if (lc.cmd == LC_VERSION_MIN_MACOSX || lc.cmd == LC_VERSION_MIN_IPHONEOS)
4913 {
4914 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
4915 {
4916 const uint32_t xxxx = lc.version >> 16;
4917 const uint32_t yy = (lc.version >> 8) & 0xffu;
4918 const uint32_t zz = lc.version & 0xffu;
4919 if (xxxx)
4920 {
4921 m_min_os_versions.push_back(xxxx);
4922 if (yy)
4923 {
4924 m_min_os_versions.push_back(yy);
4925 if (zz)
4926 m_min_os_versions.push_back(zz);
4927 }
4928 }
4929 success = true;
4930 }
4931 }
4932 offset = load_cmd_offset + lc.cmdsize;
4933 }
4934
4935 if (success == false)
4936 {
4937 // Push an invalid value so we don't keep trying to
4938 m_min_os_versions.push_back(UINT32_MAX);
4939 }
4940 }
4941
4942 if (m_min_os_versions.size() > 1 || m_min_os_versions[0] != UINT32_MAX)
4943 {
4944 if (versions != NULL && num_versions > 0)
4945 {
4946 for (size_t i=0; i<num_versions; ++i)
4947 {
4948 if (i < m_min_os_versions.size())
4949 versions[i] = m_min_os_versions[i];
4950 else
4951 versions[i] = 0;
4952 }
4953 }
4954 return m_min_os_versions.size();
4955 }
4956 // Call the superclasses version that will empty out the data
4957 return ObjectFile::GetMinimumOSVersion (versions, num_versions);
4958}
4959
4960uint32_t
4961ObjectFileMachO::GetSDKVersion(uint32_t *versions, uint32_t num_versions)
4962{
4963 if (m_sdk_versions.empty())
4964 {
4965 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
4966 bool success = false;
4967 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
4968 {
4969 const lldb::offset_t load_cmd_offset = offset;
4970
4971 version_min_command lc;
4972 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
4973 break;
4974 if (lc.cmd == LC_VERSION_MIN_MACOSX || lc.cmd == LC_VERSION_MIN_IPHONEOS)
4975 {
4976 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
4977 {
4978 const uint32_t xxxx = lc.reserved >> 16;
4979 const uint32_t yy = (lc.reserved >> 8) & 0xffu;
4980 const uint32_t zz = lc.reserved & 0xffu;
4981 if (xxxx)
4982 {
4983 m_sdk_versions.push_back(xxxx);
4984 if (yy)
4985 {
4986 m_sdk_versions.push_back(yy);
4987 if (zz)
4988 m_sdk_versions.push_back(zz);
4989 }
4990 }
4991 success = true;
4992 }
4993 }
4994 offset = load_cmd_offset + lc.cmdsize;
4995 }
4996
4997 if (success == false)
4998 {
4999 // Push an invalid value so we don't keep trying to
5000 m_sdk_versions.push_back(UINT32_MAX);
5001 }
5002 }
5003
5004 if (m_sdk_versions.size() > 1 || m_sdk_versions[0] != UINT32_MAX)
5005 {
5006 if (versions != NULL && num_versions > 0)
5007 {
5008 for (size_t i=0; i<num_versions; ++i)
5009 {
5010 if (i < m_sdk_versions.size())
5011 versions[i] = m_sdk_versions[i];
5012 else
5013 versions[i] = 0;
5014 }
5015 }
5016 return m_sdk_versions.size();
5017 }
5018 // Call the superclasses version that will empty out the data
5019 return ObjectFile::GetSDKVersion (versions, num_versions);
5020}
5021
Jason Molenda0e0954c2013-04-16 06:24:42 +00005022
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005023//------------------------------------------------------------------
5024// PluginInterface protocol
5025//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00005026lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005027ObjectFileMachO::GetPluginName()
5028{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005029 return GetPluginNameStatic();
5030}
5031
5032uint32_t
5033ObjectFileMachO::GetPluginVersion()
5034{
5035 return 1;
5036}
5037
Greg Clayton7524e092014-02-06 20:10:16 +00005038
5039bool
Greg Clayton751caf62014-02-07 22:54:47 +00005040ObjectFileMachO::SetLoadAddress (Target &target,
5041 lldb::addr_t value,
5042 bool value_is_offset)
Greg Clayton7524e092014-02-06 20:10:16 +00005043{
5044 bool changed = false;
5045 ModuleSP module_sp = GetModule();
5046 if (module_sp)
5047 {
5048 size_t num_loaded_sections = 0;
5049 SectionList *section_list = GetSectionList ();
5050 if (section_list)
5051 {
5052 lldb::addr_t mach_base_file_addr = LLDB_INVALID_ADDRESS;
5053 const size_t num_sections = section_list->GetSize();
5054
Greg Clayton751caf62014-02-07 22:54:47 +00005055 const bool is_memory_image = (bool)m_process_wp.lock();
5056 const Strata strata = GetStrata();
5057 static ConstString g_linkedit_segname ("__LINKEDIT");
5058 if (value_is_offset)
Greg Clayton7524e092014-02-06 20:10:16 +00005059 {
Greg Clayton751caf62014-02-07 22:54:47 +00005060 // "value" is an offset to apply to each top level segment
Greg Clayton7524e092014-02-06 20:10:16 +00005061 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5062 {
5063 // Iterate through the object file sections to find all
5064 // of the sections that size on disk (to avoid __PAGEZERO)
5065 // and load them
5066 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Greg Clayton751caf62014-02-07 22:54:47 +00005067 if (section_sp &&
5068 section_sp->GetFileSize() > 0 &&
5069 section_sp->IsThreadSpecific() == false &&
5070 module_sp.get() == section_sp->GetModule().get())
Greg Clayton7524e092014-02-06 20:10:16 +00005071 {
Greg Clayton751caf62014-02-07 22:54:47 +00005072 // Ignore __LINKEDIT and __DWARF segments
5073 if (section_sp->GetName() == g_linkedit_segname)
5074 {
5075 // Only map __LINKEDIT if we have an in memory image and this isn't
5076 // a kernel binary like a kext or mach_kernel.
5077 if (is_memory_image == false || strata == eStrataKernel)
5078 continue;
5079 }
5080 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
Greg Clayton7524e092014-02-06 20:10:16 +00005081 ++num_loaded_sections;
5082 }
5083 }
5084 }
Greg Clayton751caf62014-02-07 22:54:47 +00005085 else
5086 {
5087 // "value" is the new base address of the mach_header, adjust each
5088 // section accordingly
5089
5090 // First find the address of the mach header which is the first non-zero
5091 // file sized section whose file offset is zero as this will be subtracted
5092 // from each other valid section's vmaddr and then get "base_addr" added to
5093 // it when loading the module in the target
5094 for (size_t sect_idx = 0;
5095 sect_idx < num_sections && mach_base_file_addr == LLDB_INVALID_ADDRESS;
5096 ++sect_idx)
5097 {
5098 // Iterate through the object file sections to find all
5099 // of the sections that size on disk (to avoid __PAGEZERO)
5100 // and load them
5101 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
5102 if (section &&
5103 section->GetFileSize() > 0 &&
5104 section->GetFileOffset() == 0 &&
5105 section->IsThreadSpecific() == false &&
5106 module_sp.get() == section->GetModule().get())
5107 {
5108 // Ignore __LINKEDIT and __DWARF segments
5109 if (section->GetName() == g_linkedit_segname)
5110 {
5111 // Only map __LINKEDIT if we have an in memory image and this isn't
5112 // a kernel binary like a kext or mach_kernel.
5113 if (is_memory_image == false || strata == eStrataKernel)
5114 continue;
5115 }
5116 mach_base_file_addr = section->GetFileAddress();
5117 }
5118 }
5119
5120 if (mach_base_file_addr != LLDB_INVALID_ADDRESS)
5121 {
5122 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5123 {
5124 // Iterate through the object file sections to find all
5125 // of the sections that size on disk (to avoid __PAGEZERO)
5126 // and load them
5127 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
5128 if (section_sp &&
5129 section_sp->GetFileSize() > 0 &&
5130 section_sp->IsThreadSpecific() == false &&
5131 module_sp.get() == section_sp->GetModule().get())
5132 {
5133 // Ignore __LINKEDIT and __DWARF segments
5134 if (section_sp->GetName() == g_linkedit_segname)
5135 {
5136 // Only map __LINKEDIT if we have an in memory image and this isn't
5137 // a kernel binary like a kext or mach_kernel.
5138 if (is_memory_image == false || strata == eStrataKernel)
5139 continue;
5140 }
5141 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() - mach_base_file_addr + value))
5142 ++num_loaded_sections;
5143 }
5144 }
5145 }
5146 }
Greg Clayton7524e092014-02-06 20:10:16 +00005147 }
5148 changed = num_loaded_sections > 0;
5149 return num_loaded_sections > 0;
5150 }
5151 return changed;
5152}
5153
Greg Claytona2715cf2014-06-13 00:54:12 +00005154bool
5155ObjectFileMachO::SaveCore (const lldb::ProcessSP &process_sp,
5156 const FileSpec &outfile,
5157 Error &error)
5158{
5159 if (process_sp)
5160 {
5161 Target &target = process_sp->GetTarget();
5162 const ArchSpec target_arch = target.GetArchitecture();
5163 const llvm::Triple &target_triple = target_arch.GetTriple();
5164 if (target_triple.getVendor() == llvm::Triple::Apple &&
5165 (target_triple.getOS() == llvm::Triple::MacOSX ||
5166 target_triple.getOS() == llvm::Triple::IOS))
5167 {
5168 bool make_core = false;
5169 switch (target_arch.GetMachine())
5170 {
5171 case llvm::Triple::arm:
5172 case llvm::Triple::x86:
5173 case llvm::Triple::x86_64:
5174 make_core = true;
5175 break;
5176 default:
5177 error.SetErrorStringWithFormat ("unsupported core architecture: %s", target_triple.str().c_str());
5178 break;
5179 }
5180
5181 if (make_core)
5182 {
5183 std::vector<segment_command_64> segment_load_commands;
5184 uint32_t range_info_idx = 0;
5185 MemoryRegionInfo range_info;
5186 Error range_error = process_sp->GetMemoryRegionInfo(0, range_info);
5187 if (range_error.Success())
5188 {
5189 while (range_info.GetRange().GetRangeBase() != LLDB_INVALID_ADDRESS)
5190 {
5191 const addr_t addr = range_info.GetRange().GetRangeBase();
5192 const addr_t size = range_info.GetRange().GetByteSize();
5193
5194 if (size == 0)
5195 break;
5196
5197 // Calculate correct protections
5198 uint32_t prot = 0;
5199 if (range_info.GetReadable() == MemoryRegionInfo::eYes)
5200 prot |= VM_PROT_READ;
5201 if (range_info.GetWritable() == MemoryRegionInfo::eYes)
5202 prot |= VM_PROT_WRITE;
5203 if (range_info.GetExecutable() == MemoryRegionInfo::eYes)
5204 prot |= VM_PROT_EXECUTE;
5205
5206// printf ("[%3u] [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") %c%c%c\n",
5207// range_info_idx,
5208// addr,
5209// size,
5210// (prot & VM_PROT_READ ) ? 'r' : '-',
5211// (prot & VM_PROT_WRITE ) ? 'w' : '-',
5212// (prot & VM_PROT_EXECUTE) ? 'x' : '-');
5213
5214 if (prot != 0)
5215 {
5216 segment_command_64 segment = {
5217 LC_SEGMENT_64, // uint32_t cmd;
5218 sizeof(segment), // uint32_t cmdsize;
5219 {0}, // char segname[16];
5220 addr, // uint64_t vmaddr;
5221 size, // uint64_t vmsize;
5222 0, // uint64_t fileoff;
5223 size, // uint64_t filesize;
5224 prot, // uint32_t maxprot;
5225 prot, // uint32_t initprot;
5226 0, // uint32_t nsects;
5227 0 }; // uint32_t flags;
5228 segment_load_commands.push_back(segment);
5229 }
5230 else
5231 {
5232 // No protections and a size of 1 used to be returned from old
5233 // debugservers when we asked about a region that was past the
5234 // last memory region and it indicates the end...
5235 if (size == 1)
5236 break;
5237 }
5238
5239 range_error = process_sp->GetMemoryRegionInfo(range_info.GetRange().GetRangeEnd(), range_info);
5240 if (range_error.Fail())
5241 break;
5242 }
5243
5244 const uint32_t addr_byte_size = target_arch.GetAddressByteSize();
5245 const ByteOrder byte_order = target_arch.GetByteOrder();
5246 StreamString buffer (Stream::eBinary,
5247 addr_byte_size,
5248 byte_order);
5249
5250 mach_header_64 mach_header;
5251 mach_header.magic = MH_MAGIC_64;
5252 mach_header.cputype = target_arch.GetMachOCPUType();
5253 mach_header.cpusubtype = target_arch.GetMachOCPUSubType();
5254 mach_header.filetype = MH_CORE;
5255 mach_header.ncmds = segment_load_commands.size();
5256 mach_header.flags = 0;
5257 mach_header.reserved = 0;
5258 ThreadList &thread_list = process_sp->GetThreadList();
5259 const uint32_t num_threads = thread_list.GetSize();
5260
5261 // Make an array of LC_THREAD data items. Each one contains
5262 // the contents of the LC_THREAD load command. The data doesn't
5263 // contain the load command + load command size, we will
5264 // add the load command and load command size as we emit the data.
5265 std::vector<StreamString> LC_THREAD_datas(num_threads);
5266 for (auto &LC_THREAD_data : LC_THREAD_datas)
5267 {
5268 LC_THREAD_data.GetFlags().Set(Stream::eBinary);
5269 LC_THREAD_data.SetAddressByteSize(addr_byte_size);
5270 LC_THREAD_data.SetByteOrder(byte_order);
5271 }
5272 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx)
5273 {
5274 ThreadSP thread_sp (thread_list.GetThreadAtIndex(thread_idx));
5275 if (thread_sp)
5276 {
5277 switch (mach_header.cputype)
5278 {
5279 case llvm::MachO::CPU_TYPE_ARM:
5280 //RegisterContextDarwin_arm_Mach::Create_LC_THREAD (thread_sp.get(), thread_load_commands);
5281 break;
5282
5283 case llvm::MachO::CPU_TYPE_I386:
5284 //RegisterContextDarwin_i386_Mach::Create_LC_THREAD (thread_sp.get(), thread_load_commands);
5285 break;
5286
5287 case llvm::MachO::CPU_TYPE_X86_64:
5288 RegisterContextDarwin_x86_64_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5289 break;
5290 }
5291
5292 }
5293 }
5294
5295 // The size of the load command is the size of the segments...
5296 mach_header.sizeofcmds = segment_load_commands.size() * segment_load_commands[0].cmdsize;
5297
5298 // and the size of all LC_THREAD load command
5299 for (const auto &LC_THREAD_data : LC_THREAD_datas)
5300 {
5301 mach_header.sizeofcmds += 8 + LC_THREAD_data.GetSize();
5302 }
5303
5304 printf ("mach_header: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x\n",
5305 mach_header.magic,
5306 mach_header.cputype,
5307 mach_header.cpusubtype,
5308 mach_header.filetype,
5309 mach_header.ncmds,
5310 mach_header.sizeofcmds,
5311 mach_header.flags,
5312 mach_header.reserved);
5313
5314 // Write the mach header
5315 buffer.PutHex32(mach_header.magic);
5316 buffer.PutHex32(mach_header.cputype);
5317 buffer.PutHex32(mach_header.cpusubtype);
5318 buffer.PutHex32(mach_header.filetype);
5319 buffer.PutHex32(mach_header.ncmds);
5320 buffer.PutHex32(mach_header.sizeofcmds);
5321 buffer.PutHex32(mach_header.flags);
5322 buffer.PutHex32(mach_header.reserved);
5323
5324 // Skip the mach header and all load commands and align to the next
5325 // 0x1000 byte boundary
5326 addr_t file_offset = buffer.GetSize() + mach_header.sizeofcmds;
5327 if (file_offset & 0x00000fff)
5328 {
5329 file_offset += 0x00001000ull;
5330 file_offset &= (~0x00001000ull + 1);
5331 }
5332
5333 for (auto &segment : segment_load_commands)
5334 {
5335 segment.fileoff = file_offset;
5336 file_offset += segment.filesize;
5337 }
5338
5339 // Write out all of the LC_THREAD load commands
5340 for (const auto &LC_THREAD_data : LC_THREAD_datas)
5341 {
5342 const size_t LC_THREAD_data_size = LC_THREAD_data.GetSize();
5343 buffer.PutHex32(LC_THREAD);
5344 buffer.PutHex32(8 + LC_THREAD_data_size); // cmd + cmdsize + data
5345 buffer.Write(LC_THREAD_data.GetData(), LC_THREAD_data_size);
5346 }
5347
5348 // Write out all of the segment load commands
5349 for (const auto &segment : segment_load_commands)
5350 {
5351 printf ("0x%8.8x 0x%8.8x [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") [0x%16.16" PRIx64 " 0x%16.16" PRIx64 ") 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x]\n",
5352 segment.cmd,
5353 segment.cmdsize,
5354 segment.vmaddr,
5355 segment.vmaddr + segment.vmsize,
5356 segment.fileoff,
5357 segment.filesize,
5358 segment.maxprot,
5359 segment.initprot,
5360 segment.nsects,
5361 segment.flags);
5362
5363 buffer.PutHex32(segment.cmd);
5364 buffer.PutHex32(segment.cmdsize);
5365 buffer.PutRawBytes(segment.segname, sizeof(segment.segname));
5366 buffer.PutHex64(segment.vmaddr);
5367 buffer.PutHex64(segment.vmsize);
5368 buffer.PutHex64(segment.fileoff);
5369 buffer.PutHex64(segment.filesize);
5370 buffer.PutHex32(segment.maxprot);
5371 buffer.PutHex32(segment.initprot);
5372 buffer.PutHex32(segment.nsects);
5373 buffer.PutHex32(segment.flags);
5374 }
5375
5376 File core_file;
5377 std::string core_file_path(outfile.GetPath());
5378 error = core_file.Open(core_file_path.c_str(),
5379 File::eOpenOptionWrite |
5380 File::eOpenOptionTruncate |
5381 File::eOpenOptionCanCreate);
5382 if (error.Success())
5383 {
5384 // Read 1 page at a time
5385 uint8_t bytes[0x1000];
5386 // Write the mach header and load commands out to the core file
5387 size_t bytes_written = buffer.GetString().size();
5388 error = core_file.Write(buffer.GetString().data(), bytes_written);
5389 if (error.Success())
5390 {
5391 // Now write the file data for all memory segments in the process
5392 for (const auto &segment : segment_load_commands)
5393 {
5394 if (segment.fileoff != core_file.SeekFromStart(segment.fileoff))
5395 {
5396 error.SetErrorStringWithFormat("unable to seek to offset 0x%" PRIx64 " in '%s'", segment.fileoff, core_file_path.c_str());
5397 break;
5398 }
5399
5400 printf ("Saving data for segment at 0x%llx\n", segment.vmaddr);
5401 addr_t bytes_left = segment.vmsize;
5402 addr_t addr = segment.vmaddr;
5403 Error memory_read_error;
5404 while (bytes_left > 0 && error.Success())
5405 {
5406 const size_t bytes_to_read = bytes_left > sizeof(bytes) ? sizeof(bytes) : bytes_left;
5407 const size_t bytes_read = process_sp->ReadMemory(addr, bytes, bytes_to_read, memory_read_error);
5408 if (bytes_read == bytes_to_read)
5409 {
5410 size_t bytes_written = bytes_read;
5411 error = core_file.Write(bytes, bytes_written);
5412 bytes_left -= bytes_read;
5413 addr += bytes_read;
5414 }
5415 else
5416 {
5417 // Some pages within regions are not readable, those
5418 // should be zero filled
5419 memset (bytes, 0, bytes_to_read);
5420 size_t bytes_written = bytes_to_read;
5421 error = core_file.Write(bytes, bytes_written);
5422 bytes_left -= bytes_to_read;
5423 addr += bytes_to_read;
5424 }
5425 }
5426 }
5427 }
5428 }
5429 }
5430 else
5431 {
5432 error.SetErrorString("process doesn't support getting memory region info");
5433 }
5434 }
5435 return true; // This is the right plug to handle saving core files for this process
5436 }
5437 }
5438 return false;
5439}
5440