blob: 67be5a5980eeb8611c026879dd7fe551a6883386 [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
Todd Fiala013434e2014-07-09 01:29:05 +000048#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
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:
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000119 // fancy flavors that encapsulate of the above
120 // flavors...
Greg Claytonc859e2d2012-02-13 23:10:39 +0000121 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:
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000349 // fancy flavors that encapsulate of the above
350 // flavors...
Greg Claytonc859e2d2012-02-13 23:10:39 +0000351 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
Jason Molenda05a09c62014-08-22 02:46:46 +00001252 bool section_file_addresses_changed = false;
1253
Greg Clayton3046e662013-07-10 01:23:25 +00001254 offset = MachHeaderSizeFromMagic(m_header.magic);
1255
1256 struct segment_command_64 load_cmd;
1257 for (i=0; i<m_header.ncmds; ++i)
1258 {
1259 const lldb::offset_t load_cmd_offset = offset;
1260 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1261 break;
1262
Charles Davis510938e2013-08-27 05:04:57 +00001263 if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64)
Greg Clayton3046e662013-07-10 01:23:25 +00001264 {
1265 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001266 {
Greg Clayton3046e662013-07-10 01:23:25 +00001267 bool add_section = true;
1268 bool add_to_unified = true;
1269 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 +00001270
Greg Clayton3046e662013-07-10 01:23:25 +00001271 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1272 if (is_dsym && unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273 {
Greg Clayton3046e662013-07-10 01:23:25 +00001274 if (const_segname == GetSegmentNameLINKEDIT())
1275 {
1276 // We need to keep the __LINKEDIT segment private to this object file only
1277 add_to_unified = false;
1278 }
1279 else
1280 {
1281 // This is the dSYM file and this section has already been created by
1282 // the object file, no need to create it.
1283 add_section = false;
1284 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001285 }
Greg Clayton3046e662013-07-10 01:23:25 +00001286 load_cmd.vmaddr = m_data.GetAddress(&offset);
1287 load_cmd.vmsize = m_data.GetAddress(&offset);
1288 load_cmd.fileoff = m_data.GetAddress(&offset);
1289 load_cmd.filesize = m_data.GetAddress(&offset);
1290 if (m_length != 0 && load_cmd.filesize != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291 {
Greg Clayton3046e662013-07-10 01:23:25 +00001292 if (load_cmd.fileoff > m_length)
1293 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001294 // We have a load command that says it extends past the end of the file. This is likely
Greg Clayton3046e662013-07-10 01:23:25 +00001295 // a corrupt file. We don't have any way to return an error condition here (this method
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001296 // was likely invoked from something like ObjectFile::GetSectionList()) -- all we can do
Greg Clayton3046e662013-07-10 01:23:25 +00001297 // is null out the SectionList vector and if a process has been set up, dump a message
1298 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001299 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001300 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 +00001301 i,
1302 lc_segment_name,
1303 load_cmd.fileoff,
1304 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001305
1306 load_cmd.fileoff = 0;
1307 load_cmd.filesize = 0;
1308 }
1309
1310 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1311 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001312 // We have a load command that says it extends past the end of the file. This is likely
Greg Clayton3046e662013-07-10 01:23:25 +00001313 // a corrupt file. We don't have any way to return an error condition here (this method
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001314 // was likely invoked from something like ObjectFile::GetSectionList()) -- all we can do
Greg Clayton3046e662013-07-10 01:23:25 +00001315 // is null out the SectionList vector and if a process has been set up, dump a message
1316 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001317 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001318 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 +00001319 i,
1320 lc_segment_name,
1321 load_cmd.fileoff + load_cmd.filesize,
1322 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001323
1324 // Tuncase the length
1325 load_cmd.filesize = m_length - load_cmd.fileoff;
1326 }
1327 }
1328 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1329 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001330
Charles Davis510938e2013-08-27 05:04:57 +00001331 const bool segment_is_encrypted = (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001332
Greg Clayton3046e662013-07-10 01:23:25 +00001333 // Keep a list of mach segments around in case we need to
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334 // get at data that isn't stored in the abstracted Sections.
Greg Clayton3046e662013-07-10 01:23:25 +00001335 m_mach_segments.push_back (load_cmd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001336
Greg Clayton3046e662013-07-10 01:23:25 +00001337 // Use a segment ID of the segment index shifted left by 8 so they
1338 // never conflict with any of the sections.
1339 SectionSP segment_sp;
1340 if (add_section && (const_segname || is_core))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341 {
Greg Clayton3046e662013-07-10 01:23:25 +00001342 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
1343 this, // Object file to which this sections belongs
1344 ++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
1345 const_segname, // Name of this section
1346 eSectionTypeContainer, // This section is a container of other sections.
1347 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1348 load_cmd.vmsize, // VM size in bytes of this section
1349 load_cmd.fileoff, // Offset to the data for this section in the file
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001350 load_cmd.filesize, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +00001351 0, // Segments have no alignment information
Greg Clayton3046e662013-07-10 01:23:25 +00001352 load_cmd.flags)); // Flags for this section
Greg Clayton8d38ac42010-06-28 23:51:11 +00001353
Greg Clayton3046e662013-07-10 01:23:25 +00001354 segment_sp->SetIsEncrypted (segment_is_encrypted);
1355 m_sections_ap->AddSection(segment_sp);
1356 if (add_to_unified)
1357 unified_section_list.AddSection(segment_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358 }
Greg Clayton3046e662013-07-10 01:23:25 +00001359 else if (unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001360 {
Jason Molenda20eb31b2013-08-16 03:20:42 +00001361 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr)
1362 {
1363 // Check to see if the module was read from memory?
1364 if (module_sp->GetObjectFile()->GetHeaderAddress().IsValid())
1365 {
1366 // We have a module that is in memory and needs to have its
1367 // file address adjusted. We need to do this because when we
1368 // load a file from memory, its addresses will be slid already,
1369 // yet the addresses in the new symbol file will still be unslid.
1370 // Since everything is stored as section offset, this shouldn't
1371 // cause any problems.
Jason Molenda5894a732013-08-17 03:39:52 +00001372
1373 // Make sure we've parsed the symbol table from the
1374 // ObjectFile before we go around changing its Sections.
1375 module_sp->GetObjectFile()->GetSymtab();
1376 // eh_frame would present the same problems but we parse that on
1377 // a per-function basis as-needed so it's more difficult to
1378 // remove its use of the Sections. Realistically, the environments
1379 // where this code path will be taken will not have eh_frame sections.
1380
Jason Molenda20eb31b2013-08-16 03:20:42 +00001381 unified_section_sp->SetFileAddress(load_cmd.vmaddr);
Jason Molenda05a09c62014-08-22 02:46:46 +00001382
1383 // Notify the module that the section addresses have been changed once
1384 // we're done so any file-address caches can be updated.
1385 section_file_addresses_changed = true;
Jason Molenda20eb31b2013-08-16 03:20:42 +00001386 }
1387 }
Greg Clayton3046e662013-07-10 01:23:25 +00001388 m_sections_ap->AddSection(unified_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390
Greg Clayton3046e662013-07-10 01:23:25 +00001391 struct section_64 sect64;
1392 ::memset (&sect64, 0, sizeof(sect64));
1393 // Push a section into our mach sections for the section at
Charles Davis510938e2013-08-27 05:04:57 +00001394 // index zero (NO_SECT) if we don't have any mach sections yet...
Greg Clayton3046e662013-07-10 01:23:25 +00001395 if (m_mach_sections.empty())
1396 m_mach_sections.push_back(sect64);
1397 uint32_t segment_sect_idx;
1398 const lldb::user_id_t first_segment_sectID = sectID + 1;
1399
1400
Charles Davis510938e2013-08-27 05:04:57 +00001401 const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8;
Greg Clayton3046e662013-07-10 01:23:25 +00001402 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001403 {
Greg Clayton3046e662013-07-10 01:23:25 +00001404 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
Greg Clayton89411422010-10-08 00:21:05 +00001405 break;
Greg Clayton3046e662013-07-10 01:23:25 +00001406 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1407 break;
1408 sect64.addr = m_data.GetAddress(&offset);
1409 sect64.size = m_data.GetAddress(&offset);
1410
1411 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1412 break;
1413
1414 // Keep a list of mach sections around in case we need to
1415 // get at data that isn't stored in the abstracted Sections.
1416 m_mach_sections.push_back (sect64);
1417
1418 if (add_section)
1419 {
1420 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1421 if (!const_segname)
1422 {
1423 // We have a segment with no name so we need to conjure up
1424 // segments that correspond to the section's segname if there
1425 // isn't already such a section. If there is such a section,
1426 // we resize the section so that it spans all sections.
1427 // We also mark these sections as fake so address matches don't
1428 // hit if they land in the gaps between the child sections.
1429 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1430 segment_sp = unified_section_list.FindSectionByName (const_segname);
1431 if (segment_sp.get())
1432 {
1433 Section *segment = segment_sp.get();
1434 // Grow the section size as needed.
1435 const lldb::addr_t sect64_min_addr = sect64.addr;
1436 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1437 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1438 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1439 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1440 if (sect64_min_addr >= curr_seg_min_addr)
1441 {
1442 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1443 // Only grow the section size if needed
1444 if (new_seg_byte_size > curr_seg_byte_size)
1445 segment->SetByteSize (new_seg_byte_size);
1446 }
1447 else
1448 {
1449 // We need to change the base address of the segment and
1450 // adjust the child section offsets for all existing children.
1451 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1452 segment->Slide(slide_amount, false);
1453 segment->GetChildren().Slide(-slide_amount, false);
1454 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1455 }
1456
1457 // Grow the section size as needed.
1458 if (sect64.offset)
1459 {
1460 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1461 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1462
1463 const lldb::addr_t section_min_file_offset = sect64.offset;
1464 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1465 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1466 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1467 segment->SetFileOffset (new_file_offset);
1468 segment->SetFileSize (new_file_size);
1469 }
1470 }
1471 else
1472 {
1473 // Create a fake section for the section's named segment
1474 segment_sp.reset(new Section (segment_sp, // Parent section
1475 module_sp, // Module to which this section belongs
1476 this, // Object file to which this section belongs
1477 ++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
1478 const_segname, // Name of this section
1479 eSectionTypeContainer, // This section is a container of other sections.
1480 sect64.addr, // File VM address == addresses as they are found in the object file
1481 sect64.size, // VM size in bytes of this section
1482 sect64.offset, // Offset to the data for this section in the file
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001483 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +00001484 sect64.align,
Greg Clayton3046e662013-07-10 01:23:25 +00001485 load_cmd.flags)); // Flags for this section
1486 segment_sp->SetIsFake(true);
1487
1488 m_sections_ap->AddSection(segment_sp);
1489 if (add_to_unified)
1490 unified_section_list.AddSection(segment_sp);
1491 segment_sp->SetIsEncrypted (segment_is_encrypted);
1492 }
1493 }
1494 assert (segment_sp.get());
1495
Charles Davis510938e2013-08-27 05:04:57 +00001496 lldb::SectionType sect_type = eSectionTypeOther;
Greg Clayton3046e662013-07-10 01:23:25 +00001497
Greg Clayton38f9cc42014-06-16 22:53:16 +00001498 if (sect64.flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS))
1499 sect_type = eSectionTypeCode;
1500 else
Greg Clayton3046e662013-07-10 01:23:25 +00001501 {
Greg Clayton38f9cc42014-06-16 22:53:16 +00001502 uint32_t mach_sect_type = sect64.flags & SECTION_TYPE;
1503 static ConstString g_sect_name_objc_data ("__objc_data");
1504 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1505 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1506 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1507 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1508 static ConstString g_sect_name_objc_const ("__objc_const");
1509 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1510 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton3046e662013-07-10 01:23:25 +00001511
Greg Clayton38f9cc42014-06-16 22:53:16 +00001512 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1513 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1514 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1515 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1516 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1517 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1518 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1519 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1520 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1521 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1522 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1523 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1524 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1525 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1526 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1527 static ConstString g_sect_name_eh_frame ("__eh_frame");
1528 static ConstString g_sect_name_text ("__text");
1529 static ConstString g_sect_name_data ("__data");
1530
1531
1532 if (section_name == g_sect_name_dwarf_debug_abbrev)
1533 sect_type = eSectionTypeDWARFDebugAbbrev;
1534 else if (section_name == g_sect_name_dwarf_debug_aranges)
1535 sect_type = eSectionTypeDWARFDebugAranges;
1536 else if (section_name == g_sect_name_dwarf_debug_frame)
1537 sect_type = eSectionTypeDWARFDebugFrame;
1538 else if (section_name == g_sect_name_dwarf_debug_info)
1539 sect_type = eSectionTypeDWARFDebugInfo;
1540 else if (section_name == g_sect_name_dwarf_debug_line)
1541 sect_type = eSectionTypeDWARFDebugLine;
1542 else if (section_name == g_sect_name_dwarf_debug_loc)
1543 sect_type = eSectionTypeDWARFDebugLoc;
1544 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1545 sect_type = eSectionTypeDWARFDebugMacInfo;
1546 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1547 sect_type = eSectionTypeDWARFDebugPubNames;
1548 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1549 sect_type = eSectionTypeDWARFDebugPubTypes;
1550 else if (section_name == g_sect_name_dwarf_debug_ranges)
1551 sect_type = eSectionTypeDWARFDebugRanges;
1552 else if (section_name == g_sect_name_dwarf_debug_str)
1553 sect_type = eSectionTypeDWARFDebugStr;
1554 else if (section_name == g_sect_name_dwarf_apple_names)
1555 sect_type = eSectionTypeDWARFAppleNames;
1556 else if (section_name == g_sect_name_dwarf_apple_types)
1557 sect_type = eSectionTypeDWARFAppleTypes;
1558 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1559 sect_type = eSectionTypeDWARFAppleNamespaces;
1560 else if (section_name == g_sect_name_dwarf_apple_objc)
1561 sect_type = eSectionTypeDWARFAppleObjC;
1562 else if (section_name == g_sect_name_objc_selrefs)
1563 sect_type = eSectionTypeDataCStringPointers;
1564 else if (section_name == g_sect_name_objc_msgrefs)
1565 sect_type = eSectionTypeDataObjCMessageRefs;
1566 else if (section_name == g_sect_name_eh_frame)
1567 sect_type = eSectionTypeEHFrame;
1568 else if (section_name == g_sect_name_cfstring)
1569 sect_type = eSectionTypeDataObjCCFStrings;
1570 else if (section_name == g_sect_name_objc_data ||
1571 section_name == g_sect_name_objc_classrefs ||
1572 section_name == g_sect_name_objc_superrefs ||
1573 section_name == g_sect_name_objc_const ||
1574 section_name == g_sect_name_objc_classlist)
Greg Clayton3046e662013-07-10 01:23:25 +00001575 {
Greg Clayton38f9cc42014-06-16 22:53:16 +00001576 sect_type = eSectionTypeDataPointers;
1577 }
1578
1579 if (sect_type == eSectionTypeOther)
1580 {
1581 switch (mach_sect_type)
1582 {
1583 // TODO: categorize sections by other flags for regular sections
1584 case S_REGULAR:
1585 if (section_name == g_sect_name_text)
1586 sect_type = eSectionTypeCode;
1587 else if (section_name == g_sect_name_data)
1588 sect_type = eSectionTypeData;
1589 else
1590 sect_type = eSectionTypeOther;
1591 break;
1592 case S_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1593 case S_CSTRING_LITERALS: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1594 case S_4BYTE_LITERALS: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1595 case S_8BYTE_LITERALS: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1596 case S_LITERAL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1597 case S_NON_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1598 case S_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1599 case S_SYMBOL_STUBS: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1600 case S_MOD_INIT_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1601 case S_MOD_TERM_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1602 case S_COALESCED: sect_type = eSectionTypeOther; break;
1603 case S_GB_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1604 case S_INTERPOSING: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1605 case S_16BYTE_LITERALS: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1606 case S_DTRACE_DOF: sect_type = eSectionTypeDebug; break;
1607 case S_LAZY_DYLIB_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break;
1608 default: break;
1609 }
Greg Clayton3046e662013-07-10 01:23:25 +00001610 }
1611 }
1612
1613 SectionSP section_sp(new Section (segment_sp,
1614 module_sp,
1615 this,
1616 ++sectID,
1617 section_name,
1618 sect_type,
1619 sect64.addr - segment_sp->GetFileAddress(),
1620 sect64.size,
1621 sect64.offset,
1622 sect64.offset == 0 ? 0 : sect64.size,
Greg Clayton48672af2014-06-24 22:22:43 +00001623 sect64.align,
Greg Clayton3046e662013-07-10 01:23:25 +00001624 sect64.flags));
1625 // Set the section to be encrypted to match the segment
1626
1627 bool section_is_encrypted = false;
1628 if (!segment_is_encrypted && load_cmd.filesize != 0)
1629 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1630
1631 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1632 segment_sp->GetChildren().AddSection(section_sp);
1633
1634 if (segment_sp->IsFake())
1635 {
1636 segment_sp.reset();
1637 const_segname.Clear();
1638 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639 }
1640 }
Greg Clayton3046e662013-07-10 01:23:25 +00001641 if (segment_sp && is_dsym)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001642 {
Greg Clayton3046e662013-07-10 01:23:25 +00001643 if (first_segment_sectID <= sectID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001644 {
Greg Clayton3046e662013-07-10 01:23:25 +00001645 lldb::user_id_t sect_uid;
1646 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001647 {
Greg Clayton3046e662013-07-10 01:23:25 +00001648 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1649 SectionSP next_section_sp;
1650 if (sect_uid + 1 <= sectID)
1651 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1652
1653 if (curr_section_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001654 {
Greg Clayton3046e662013-07-10 01:23:25 +00001655 if (curr_section_sp->GetByteSize() == 0)
1656 {
1657 if (next_section_sp.get() != NULL)
1658 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1659 else
1660 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1661 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001662 }
1663 }
1664 }
1665 }
1666 }
1667 }
1668 }
Charles Davis510938e2013-08-27 05:04:57 +00001669 else if (load_cmd.cmd == LC_DYSYMTAB)
Greg Clayton3046e662013-07-10 01:23:25 +00001670 {
1671 m_dysymtab.cmd = load_cmd.cmd;
1672 m_dysymtab.cmdsize = load_cmd.cmdsize;
1673 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1674 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001675
Greg Clayton3046e662013-07-10 01:23:25 +00001676 offset = load_cmd_offset + load_cmd.cmdsize;
1677 }
Jason Molenda05a09c62014-08-22 02:46:46 +00001678
1679
1680 if (section_file_addresses_changed && module_sp.get())
1681 {
1682 module_sp->SectionFileAddressesChanged();
1683 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001684 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685}
1686
1687class MachSymtabSectionInfo
1688{
1689public:
1690
1691 MachSymtabSectionInfo (SectionList *section_list) :
1692 m_section_list (section_list),
1693 m_section_infos()
1694 {
1695 // Get the number of sections down to a depth of 1 to include
1696 // all segments and their sections, but no other sections that
1697 // may be added for debug map or
1698 m_section_infos.resize(section_list->GetNumSections(1));
1699 }
1700
1701
Greg Claytone72dfb32012-02-24 01:59:29 +00001702 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001703 GetSection (uint8_t n_sect, addr_t file_addr)
1704 {
1705 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00001706 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001707 if (n_sect < m_section_infos.size())
1708 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001709 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001710 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001711 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1712 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001713 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00001714 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001715 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1716 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00001717 }
1718 else
1719 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001720 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00001721 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001722 }
1723 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00001724 {
1725 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00001726 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001727 }
1728 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1729 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1730 {
1731 // Symbol is in section with zero size, but has the same start
1732 // address as the section. This can happen with linker symbols
1733 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00001734 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001735 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001737 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001738 }
1739
1740protected:
1741 struct SectionInfo
1742 {
1743 SectionInfo () :
1744 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001745 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001746 {
1747 }
1748
1749 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00001750 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001751 };
1752 SectionList *m_section_list;
1753 std::vector<SectionInfo> m_section_infos;
1754};
1755
Greg Clayton9191db42013-10-21 18:40:51 +00001756struct TrieEntry
1757{
1758 TrieEntry () :
1759 name(),
1760 address(LLDB_INVALID_ADDRESS),
1761 flags (0),
1762 other(0),
1763 import_name()
1764 {
1765 }
1766
1767 void
1768 Clear ()
1769 {
1770 name.Clear();
1771 address = LLDB_INVALID_ADDRESS;
1772 flags = 0;
1773 other = 0;
1774 import_name.Clear();
1775 }
1776
1777 void
1778 Dump () const
1779 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001780 printf ("0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"",
1781 static_cast<unsigned long long>(address),
1782 static_cast<unsigned long long>(flags),
1783 static_cast<unsigned long long>(other), name.GetCString());
Greg Clayton9191db42013-10-21 18:40:51 +00001784 if (import_name)
1785 printf (" -> \"%s\"\n", import_name.GetCString());
1786 else
1787 printf ("\n");
1788 }
1789 ConstString name;
1790 uint64_t address;
1791 uint64_t flags;
1792 uint64_t other;
1793 ConstString import_name;
1794};
1795
1796struct TrieEntryWithOffset
1797{
1798 lldb::offset_t nodeOffset;
1799 TrieEntry entry;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001800
Greg Clayton9191db42013-10-21 18:40:51 +00001801 TrieEntryWithOffset (lldb::offset_t offset) :
1802 nodeOffset (offset),
1803 entry()
1804 {
1805 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001806
Greg Clayton9191db42013-10-21 18:40:51 +00001807 void
1808 Dump (uint32_t idx) const
1809 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001810 printf ("[%3u] 0x%16.16llx: ", idx,
1811 static_cast<unsigned long long>(nodeOffset));
Greg Clayton9191db42013-10-21 18:40:51 +00001812 entry.Dump();
1813 }
1814
1815 bool
1816 operator<(const TrieEntryWithOffset& other) const
1817 {
1818 return ( nodeOffset < other.nodeOffset );
1819 }
1820};
1821
1822static void
1823ParseTrieEntries (DataExtractor &data,
1824 lldb::offset_t offset,
1825 std::vector<llvm::StringRef> &nameSlices,
1826 std::set<lldb::addr_t> &resolver_addresses,
1827 std::vector<TrieEntryWithOffset>& output)
1828{
1829 if (!data.ValidOffset(offset))
1830 return;
1831
1832 const uint64_t terminalSize = data.GetULEB128(&offset);
1833 lldb::offset_t children_offset = offset + terminalSize;
1834 if ( terminalSize != 0 ) {
1835 TrieEntryWithOffset e (offset);
1836 e.entry.flags = data.GetULEB128(&offset);
1837 const char *import_name = NULL;
1838 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
1839 e.entry.address = 0;
1840 e.entry.other = data.GetULEB128(&offset); // dylib ordinal
1841 import_name = data.GetCStr(&offset);
1842 }
1843 else {
1844 e.entry.address = data.GetULEB128(&offset);
1845 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
1846 {
Jim Inghamea3ac272014-01-10 22:55:37 +00001847 //resolver_addresses.insert(e.entry.address);
Greg Clayton9191db42013-10-21 18:40:51 +00001848 e.entry.other = data.GetULEB128(&offset);
Jim Inghamea3ac272014-01-10 22:55:37 +00001849 resolver_addresses.insert(e.entry.other);
Greg Clayton9191db42013-10-21 18:40:51 +00001850 }
1851 else
1852 e.entry.other = 0;
1853 }
1854 // Only add symbols that are reexport symbols with a valid import name
1855 if (EXPORT_SYMBOL_FLAGS_REEXPORT & e.entry.flags && import_name && import_name[0])
1856 {
1857 std::string name;
1858 if (!nameSlices.empty())
1859 {
1860 for (auto name_slice: nameSlices)
1861 name.append(name_slice.data(), name_slice.size());
1862 }
1863 if (name.size() > 1)
1864 {
1865 // Skip the leading '_'
1866 e.entry.name.SetCStringWithLength(name.c_str() + 1,name.size() - 1);
1867 }
1868 if (import_name)
1869 {
1870 // Skip the leading '_'
1871 e.entry.import_name.SetCString(import_name+1);
1872 }
1873 output.push_back(e);
1874 }
1875 }
1876
1877 const uint8_t childrenCount = data.GetU8(&children_offset);
1878 for (uint8_t i=0; i < childrenCount; ++i) {
1879 nameSlices.push_back(data.GetCStr(&children_offset));
1880 lldb::offset_t childNodeOffset = data.GetULEB128(&children_offset);
1881 if (childNodeOffset)
1882 {
1883 ParseTrieEntries(data,
1884 childNodeOffset,
1885 nameSlices,
1886 resolver_addresses,
1887 output);
1888 }
1889 nameSlices.pop_back();
1890 }
1891}
1892
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001893size_t
Greg Clayton3046e662013-07-10 01:23:25 +00001894ObjectFileMachO::ParseSymtab ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001895{
1896 Timer scoped_timer(__PRETTY_FUNCTION__,
1897 "ObjectFileMachO::ParseSymtab () module = %s",
1898 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001899 ModuleSP module_sp (GetModule());
1900 if (!module_sp)
1901 return 0;
1902
1903 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1904 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
Greg Clayton9191db42013-10-21 18:40:51 +00001905 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 +00001906 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1907 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00001908 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001909 uint32_t i;
Greg Clayton9191db42013-10-21 18:40:51 +00001910 FileSpecList dylib_files;
Greg Clayton5160ce52013-03-27 23:08:40 +00001911 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton77ccca72011-12-30 00:32:24 +00001912
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001913 for (i=0; i<m_header.ncmds; ++i)
1914 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001915 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001916 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001917 struct load_command lc;
1918 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001919 break;
1920 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001921 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001922 {
Charles Davis510938e2013-08-27 05:04:57 +00001923 case LC_SYMTAB:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001924 symtab_load_command.cmd = lc.cmd;
1925 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001926 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001927 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1928 return 0;
1929 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001930 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001931 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001932 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001933 return 0;
1934 }
1935
1936 if (symtab_load_command.stroff == 0)
1937 {
1938 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001939 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001940 return 0;
1941 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001942
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001943 if (symtab_load_command.nsyms == 0)
1944 {
1945 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001946 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001947 return 0;
1948 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001949
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001950 if (symtab_load_command.strsize == 0)
1951 {
1952 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001953 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001954 return 0;
1955 }
1956 break;
1957
Greg Clayton9191db42013-10-21 18:40:51 +00001958 case LC_DYLD_INFO:
1959 case LC_DYLD_INFO_ONLY:
1960 if (m_data.GetU32(&offset, &dyld_info.rebase_off, 10))
1961 {
1962 dyld_info.cmd = lc.cmd;
1963 dyld_info.cmdsize = lc.cmdsize;
1964 }
1965 else
1966 {
1967 memset (&dyld_info, 0, sizeof(dyld_info));
1968 }
1969 break;
1970
1971 case LC_LOAD_DYLIB:
1972 case LC_LOAD_WEAK_DYLIB:
1973 case LC_REEXPORT_DYLIB:
1974 case LC_LOADFVMLIB:
1975 case LC_LOAD_UPWARD_DYLIB:
1976 {
1977 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1978 const char *path = m_data.PeekCStr(name_offset);
1979 if (path)
1980 {
1981 FileSpec file_spec(path, false);
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001982 // Strip the path if there is @rpath, @executable, etc so we just use the basename
Greg Clayton9191db42013-10-21 18:40:51 +00001983 if (path[0] == '@')
1984 file_spec.GetDirectory().Clear();
Jim Inghamfbe0b9a2014-05-21 03:58:03 +00001985
1986 if (lc.cmd == LC_REEXPORT_DYLIB)
1987 {
1988 m_reexported_dylibs.AppendIfUnique(file_spec);
1989 }
Greg Clayton9191db42013-10-21 18:40:51 +00001990
1991 dylib_files.Append(file_spec);
1992 }
1993 }
1994 break;
1995
Charles Davis510938e2013-08-27 05:04:57 +00001996 case LC_FUNCTION_STARTS:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001997 function_starts_load_command.cmd = lc.cmd;
1998 function_starts_load_command.cmdsize = lc.cmdsize;
1999 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 +00002000 memset (&function_starts_load_command, 0, sizeof(function_starts_load_command));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002001 break;
2002
2003 default:
2004 break;
2005 }
2006 offset = cmd_offset + lc.cmdsize;
2007 }
2008
2009 if (symtab_load_command.cmd)
2010 {
2011 Symtab *symtab = m_symtab_ap.get();
2012 SectionList *section_list = GetSectionList();
2013 if (section_list == NULL)
2014 return 0;
2015
Greg Claytonc7bece562013-01-25 18:06:21 +00002016 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
2017 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002018 bool bit_width_32 = addr_byte_size == 4;
2019 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
2020
Greg Claytonc7bece562013-01-25 18:06:21 +00002021 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
2022 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
2023 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00002024 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00002025 DataExtractor dyld_trie_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002026
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002027 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
2028 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00002029 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
Greg Claytonfd814c52013-08-13 01:42:25 +00002030
2031 ProcessSP process_sp (m_process_wp.lock());
2032 Process *process = process_sp.get();
2033
Greg Clayton86eac942013-08-13 21:32:34 +00002034 uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete;
2035
Greg Clayton48672af2014-06-24 22:22:43 +00002036 if (process && m_header.filetype != llvm::MachO::MH_OBJECT)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002037 {
Greg Clayton4c82d422012-05-18 23:20:01 +00002038 Target &target = process->GetTarget();
Greg Claytonfd814c52013-08-13 01:42:25 +00002039
Greg Clayton86eac942013-08-13 21:32:34 +00002040 memory_module_load_level = target.GetMemoryModuleLoadLevel();
Greg Claytonfd814c52013-08-13 01:42:25 +00002041
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002042 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2043 // Reading mach file from memory in a process or core file...
2044
2045 if (linkedit_section_sp)
2046 {
2047 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
2048 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2049 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00002050 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00002051
2052 bool data_was_read = false;
2053
Todd Fiala013434e2014-07-09 01:29:05 +00002054#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molendaa3329782014-03-29 18:54:20 +00002055 if (m_header.flags & 0x80000000u && process->GetAddressByteSize() == sizeof (void*))
Greg Clayton77ccca72011-12-30 00:32:24 +00002056 {
Greg Clayton26b47e22012-04-18 05:19:20 +00002057 // This mach-o memory file is in the dyld shared cache. If this
2058 // program is not remote and this is iOS, then this process will
2059 // share the same shared cache as the process we are debugging and
2060 // we can read the entire __LINKEDIT from the address space in this
2061 // process. This is a needed optimization that is used for local iOS
2062 // debugging only since all shared libraries in the shared cache do
2063 // not have corresponding files that exist in the file system of the
2064 // device. They have been combined into a single file. This means we
2065 // always have to load these files from memory. All of the symbol and
2066 // string tables from all of the __LINKEDIT sections from the shared
2067 // libraries in the shared cache have been merged into a single large
2068 // symbol and string table. Reading all of this symbol and string table
2069 // data across can slow down debug launch times, so we optimize this by
2070 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00002071
2072 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
2073 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
2074 bool use_lldb_cache = true;
2075 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
2076 {
2077 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002078 ModuleSP module_sp (GetModule());
2079 if (module_sp)
2080 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
2081
Jason Molenda0e0954c2013-04-16 06:24:42 +00002082 }
2083
Greg Clayton26b47e22012-04-18 05:19:20 +00002084 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00002085 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00002086 {
2087 data_was_read = true;
2088 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00002089 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00002090 if (function_starts_load_command.cmd)
2091 {
2092 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2093 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
2094 }
2095 }
2096 }
2097#endif
2098
2099 if (!data_was_read)
2100 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002101 if (memory_module_load_level == eMemoryModuleLoadLevelComplete)
Jason Molendad34e6522013-02-05 22:31:24 +00002102 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002103 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
2104 if (nlist_data_sp)
2105 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2106 // Load strings individually from memory when loading from memory since shared cache
2107 // string tables contain strings for all symbols from all shared cached libraries
2108 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
2109 //if (strtab_data_sp)
2110 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
2111 if (m_dysymtab.nindirectsyms != 0)
2112 {
2113 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
2114 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
2115 if (indirect_syms_data_sp)
2116 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
2117 }
Jason Molendad34e6522013-02-05 22:31:24 +00002118 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002119
2120 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial)
Greg Clayton26b47e22012-04-18 05:19:20 +00002121 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002122 if (function_starts_load_command.cmd)
2123 {
2124 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2125 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
2126 if (func_start_data_sp)
2127 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
2128 }
Greg Clayton26b47e22012-04-18 05:19:20 +00002129 }
Greg Clayton77ccca72011-12-30 00:32:24 +00002130 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002131 }
2132 }
2133 else
2134 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002135 nlist_data.SetData (m_data,
2136 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002137 nlist_data_byte_size);
2138 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00002139 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002140 strtab_data_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00002141
2142 if (dyld_info.export_size > 0)
2143 {
2144 dyld_trie_data.SetData (m_data,
2145 dyld_info.export_off,
2146 dyld_info.export_size);
2147 }
2148
Jason Molendad34e6522013-02-05 22:31:24 +00002149 if (m_dysymtab.nindirectsyms != 0)
2150 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002151 indirect_symbol_index_data.SetData (m_data,
2152 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00002153 m_dysymtab.nindirectsyms * 4);
2154 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002155 if (function_starts_load_command.cmd)
2156 {
2157 function_starts_data.SetData (m_data,
2158 function_starts_load_command.dataoff,
2159 function_starts_load_command.datasize);
2160 }
2161 }
Greg Clayton77ccca72011-12-30 00:32:24 +00002162
Greg Clayton86eac942013-08-13 21:32:34 +00002163 if (nlist_data.GetByteSize() == 0 && memory_module_load_level == eMemoryModuleLoadLevelComplete)
2164 {
2165 if (log)
2166 module_sp->LogMessage(log, "failed to read nlist data");
2167 return 0;
2168 }
2169
2170
Greg Claytondebb8812012-05-25 17:04:00 +00002171 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2172 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00002173 {
Greg Claytondebb8812012-05-25 17:04:00 +00002174 if (process)
2175 {
2176 if (strtab_addr == LLDB_INVALID_ADDRESS)
2177 {
2178 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002179 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00002180 return 0;
2181 }
2182 }
2183 else
Greg Clayton4c82d422012-05-18 23:20:01 +00002184 {
2185 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002186 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00002187 return 0;
2188 }
2189 }
Greg Clayton4c82d422012-05-18 23:20:01 +00002190
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002191 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
2192 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
2193 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
2194 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
2195 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
2196 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
2197 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
2198 SectionSP eh_frame_section_sp;
2199 if (text_section_sp.get())
2200 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
2201 else
2202 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
2203
Charles Davis510938e2013-08-27 05:04:57 +00002204 const bool is_arm = (m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
Jason Molenda5635f772013-03-21 03:36:01 +00002205
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002206 // lldb works best if it knows the start address of all functions in a module.
Jason Molenda5635f772013-03-21 03:36:01 +00002207 // Linker symbols or debug info are normally the best source of information for start addr / size but
2208 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00002209 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00002210 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
2211 // binary, relative to the text section.
2212 // eh_frame - the eh_frame FDEs have the start addr & size of each function
2213 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
2214 // Binaries built to run on older releases may need to use eh_frame information.
2215
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002216 if (text_section_sp && function_starts_data.GetByteSize())
2217 {
2218 FunctionStarts::Entry function_start_entry;
2219 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00002220 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002221 function_start_entry.addr = text_section_sp->GetFileAddress();
2222 uint64_t delta;
2223 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
2224 {
2225 // Now append the current entry
2226 function_start_entry.addr += delta;
2227 function_starts.Append(function_start_entry);
2228 }
Jason Molendad63d3c72013-04-16 00:18:44 +00002229 }
Jason Molenda5635f772013-03-21 03:36:01 +00002230 else
2231 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00002232 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
2233 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
2234 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
2235 // the module.
2236 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00002237 {
2238 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
2239 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
2240 eh_frame.GetFunctionAddressAndSizeVector (functions);
2241 addr_t text_base_addr = text_section_sp->GetFileAddress();
2242 size_t count = functions.GetSize();
2243 for (size_t i = 0; i < count; ++i)
2244 {
2245 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
2246 if (func)
2247 {
2248 FunctionStarts::Entry function_start_entry;
2249 function_start_entry.addr = func->base - text_base_addr;
2250 function_starts.Append(function_start_entry);
2251 }
2252 }
2253 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002254 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002255
Greg Claytonc7bece562013-01-25 18:06:21 +00002256 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002257
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +00002258 const user_id_t TEXT_eh_frame_sectID =
2259 eh_frame_section_sp.get() ? eh_frame_section_sp->GetID()
2260 : static_cast<user_id_t>(NO_SECT);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002261
Greg Claytonc7bece562013-01-25 18:06:21 +00002262 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002263
2264 uint32_t N_SO_index = UINT32_MAX;
2265
2266 MachSymtabSectionInfo section_info (section_list);
2267 std::vector<uint32_t> N_FUN_indexes;
2268 std::vector<uint32_t> N_NSYM_indexes;
2269 std::vector<uint32_t> N_INCL_indexes;
2270 std::vector<uint32_t> N_BRAC_indexes;
2271 std::vector<uint32_t> N_COMM_indexes;
Greg Claytond81088c2014-01-16 01:38:29 +00002272 typedef std::multimap <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002273 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00002274 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002275 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2276 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00002277 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002278 // Any symbols that get merged into another will get an entry
2279 // in this map so we know
2280 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2281 uint32_t nlist_idx = 0;
2282 Symbol *symbol_ptr = NULL;
2283
2284 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00002285 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00002286 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00002287 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00002288 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002289
Jim Inghamea3ac272014-01-10 22:55:37 +00002290 std::vector<TrieEntryWithOffset> trie_entries;
2291 std::set<lldb::addr_t> resolver_addresses;
2292
2293 if (dyld_trie_data.GetByteSize() > 0)
2294 {
2295 std::vector<llvm::StringRef> nameSlices;
2296 ParseTrieEntries (dyld_trie_data,
2297 0,
2298 nameSlices,
2299 resolver_addresses,
2300 trie_entries);
2301
2302 ConstString text_segment_name ("__TEXT");
2303 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
2304 if (text_segment_sp)
2305 {
2306 const lldb::addr_t text_segment_file_addr = text_segment_sp->GetFileAddress();
2307 if (text_segment_file_addr != LLDB_INVALID_ADDRESS)
2308 {
2309 for (auto &e : trie_entries)
2310 e.entry.address += text_segment_file_addr;
2311 }
2312 }
2313 }
2314
Todd Fiala013434e2014-07-09 01:29:05 +00002315#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molendaa5609c82012-06-21 01:51:02 +00002316
2317 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
2318 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
2319 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
2320 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
2321 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
2322 // nlist parser to ignore all LOCAL symbols.
2323
2324 if (m_header.flags & 0x80000000u)
2325 {
2326 // Before we can start mapping the DSC, we need to make certain the target process is actually
2327 // using the cache we can find.
2328
Jason Molendaa5609c82012-06-21 01:51:02 +00002329 // Next we need to determine the correct path for the dyld shared cache.
2330
Greg Clayton7ab7f892014-05-29 21:33:45 +00002331 ArchSpec header_arch;
2332 GetArchitecture(header_arch);
Jason Molendaa5609c82012-06-21 01:51:02 +00002333 char dsc_path[PATH_MAX];
2334
2335 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00002336 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
2337 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00002338 header_arch.GetArchitectureName());
2339
2340 FileSpec dsc_filespec(dsc_path, false);
2341
2342 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molendad63d3c72013-04-16 00:18:44 +00002343 struct lldb_copy_dyld_cache_header_v0
Greg Clayton946f8902012-09-05 22:30:51 +00002344 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002345 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
2346 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
2347 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda4e7511e2013-03-06 23:19:17 +00002348 uint32_t imagesOffset;
2349 uint32_t imagesCount;
2350 uint64_t dyldBaseAddress;
2351 uint64_t codeSignatureOffset;
2352 uint64_t codeSignatureSize;
2353 uint64_t slideInfoOffset;
2354 uint64_t slideInfoSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00002355 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
2356 uint64_t localSymbolsSize; // size of local symbols information
2357 };
2358 struct lldb_copy_dyld_cache_header_v1
2359 {
2360 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
2361 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
2362 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
2363 uint32_t imagesOffset;
2364 uint32_t imagesCount;
2365 uint64_t dyldBaseAddress;
2366 uint64_t codeSignatureOffset;
2367 uint64_t codeSignatureSize;
2368 uint64_t slideInfoOffset;
2369 uint64_t slideInfoSize;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002370 uint64_t localSymbolsOffset;
2371 uint64_t localSymbolsSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00002372 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Clayton946f8902012-09-05 22:30:51 +00002373 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00002374
Jason Molendad63d3c72013-04-16 00:18:44 +00002375 struct lldb_copy_dyld_cache_mapping_info
2376 {
2377 uint64_t address;
2378 uint64_t size;
2379 uint64_t fileOffset;
2380 uint32_t maxProt;
2381 uint32_t initProt;
2382 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00002383
Greg Clayton946f8902012-09-05 22:30:51 +00002384 struct lldb_copy_dyld_cache_local_symbols_info
2385 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002386 uint32_t nlistOffset;
2387 uint32_t nlistCount;
2388 uint32_t stringsOffset;
2389 uint32_t stringsSize;
2390 uint32_t entriesOffset;
2391 uint32_t entriesCount;
Greg Clayton946f8902012-09-05 22:30:51 +00002392 };
2393 struct lldb_copy_dyld_cache_local_symbols_entry
2394 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002395 uint32_t dylibOffset;
2396 uint32_t nlistStartIndex;
2397 uint32_t nlistCount;
Greg Clayton946f8902012-09-05 22:30:51 +00002398 };
Jason Molendaa5609c82012-06-21 01:51:02 +00002399
Jason Molendaf8130862012-06-22 03:28:35 +00002400 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
2401 The dyld_cache_local_symbols_info structure gives us three things:
2402 1. The start and count of the nlist records in the dyld_shared_cache file
2403 2. The start and size of the strings for these nlist records
2404 3. The start and count of dyld_cache_local_symbols_entry entries
2405
2406 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
2407 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002408 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00002409 and the count of how many nlist records there are for this dylib/framework.
2410 */
2411
Jason Molendaa5609c82012-06-21 01:51:02 +00002412 // Process the dsc header to find the unmapped symbols
2413 //
2414 // Save some VM space, do not map the entire cache in one shot.
2415
Jason Molenda255f9bb2013-03-06 23:17:36 +00002416 DataBufferSP dsc_data_sp;
2417 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
2418
2419 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00002420 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002421 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002422
Jason Molenda255f9bb2013-03-06 23:17:36 +00002423 char version_str[17];
2424 int version = -1;
2425 lldb::offset_t offset = 0;
2426 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
2427 version_str[16] = '\0';
2428 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
2429 {
2430 int v;
2431 if (::sscanf (version_str + 6, "%d", &v) == 1)
2432 {
2433 version = v;
2434 }
2435 }
2436
Jason Molenda0e0954c2013-04-16 06:24:42 +00002437 UUID dsc_uuid;
2438 if (version >= 1)
2439 {
2440 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
2441 uint8_t uuid_bytes[sizeof (uuid_t)];
2442 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
2443 dsc_uuid.SetBytes (uuid_bytes);
2444 }
2445
2446 bool uuid_match = true;
2447 if (dsc_uuid.IsValid() && process)
2448 {
2449 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
2450
2451 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
2452 {
2453 // The on-disk dyld_shared_cache file is not the same as the one in this
2454 // process' memory, don't use it.
2455 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002456 ModuleSP module_sp (GetModule());
2457 if (module_sp)
2458 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 +00002459 }
2460 }
2461
Jason Molenda4e7511e2013-03-06 23:19:17 +00002462 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00002463
Jason Molendaa5609c82012-06-21 01:51:02 +00002464 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
2465
2466 // If the mappingOffset points to a location inside the header, we've
2467 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda0e0954c2013-04-16 06:24:42 +00002468 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendaa5609c82012-06-21 01:51:02 +00002469 {
2470
Jason Molenda255f9bb2013-03-06 23:17:36 +00002471 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
2472 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
2473 offset = 0;
2474
2475 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
2476 // in the shared library cache need to be adjusted by an offset to match up with the
2477 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
2478 // recorded in mapping_offset_value.
2479 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
2480
2481 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00002482 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
2483 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
2484
Jason Molenda4e7511e2013-03-06 23:19:17 +00002485 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00002486 {
2487 // Map the local symbols
Jason Molenda4e7511e2013-03-06 23:19:17 +00002488 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00002489 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002490 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002491
2492 offset = 0;
2493
2494 // Read the local_symbols_infos struct in one shot
2495 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
2496 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
2497
Jason Molendaa5609c82012-06-21 01:51:02 +00002498 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
2499
Jason Molenda255f9bb2013-03-06 23:17:36 +00002500 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002501
2502 offset = local_symbols_info.entriesOffset;
2503 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
2504 {
2505 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
2506 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
2507 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
2508 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
2509
Jason Molenda4e7511e2013-03-06 23:19:17 +00002510 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00002511 {
2512 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
2513
2514 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
2515 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
2516 num_syms = symtab->GetNumSymbols();
2517
2518 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
2519 uint32_t string_table_offset = local_symbols_info.stringsOffset;
2520
Jason Molenda4e7511e2013-03-06 23:19:17 +00002521 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00002522 {
2523 /////////////////////////////
2524 {
2525 struct nlist_64 nlist;
2526 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2527 break;
2528
2529 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2530 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2531 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2532 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2533 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2534
2535 SymbolType type = eSymbolTypeInvalid;
2536 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2537
2538 if (symbol_name == NULL)
2539 {
2540 // No symbol should be NULL, even the symbols with no
2541 // string values should have an offset zero which points
2542 // to an empty C-string
2543 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002544 "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 +00002545 entry_index,
2546 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002547 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00002548 continue;
2549 }
2550 if (symbol_name[0] == '\0')
2551 symbol_name = NULL;
2552
2553 const char *symbol_name_non_abi_mangled = NULL;
2554
2555 SectionSP symbol_section;
2556 uint32_t symbol_byte_size = 0;
2557 bool add_nlist = true;
Charles Davis510938e2013-08-27 05:04:57 +00002558 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002559 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002560 bool is_gsym = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002561
2562 assert (sym_idx < num_syms);
2563
2564 sym[sym_idx].SetDebug (is_debug);
2565
2566 if (is_debug)
2567 {
2568 switch (nlist.n_type)
2569 {
Charles Davis510938e2013-08-27 05:04:57 +00002570 case N_GSYM:
2571 // global symbol: name,,NO_SECT,type,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002572 // Sometimes the N_GSYM value contains the address.
2573
2574 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2575 // have the same address, but we want to ensure that we always find only the real symbol,
2576 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2577 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2578 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2579 // same address.
2580
2581 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2582 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2583 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2584 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2585 add_nlist = false;
2586 else
2587 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002588 is_gsym = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002589 sym[sym_idx].SetExternal(true);
2590 if (nlist.n_value != 0)
2591 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2592 type = eSymbolTypeData;
2593 }
2594 break;
2595
Charles Davis510938e2013-08-27 05:04:57 +00002596 case N_FNAME:
2597 // procedure name (f77 kludge): name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002598 type = eSymbolTypeCompiler;
2599 break;
2600
Charles Davis510938e2013-08-27 05:04:57 +00002601 case N_FUN:
2602 // procedure: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002603 if (symbol_name)
2604 {
2605 type = eSymbolTypeCode;
2606 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2607
Greg Claytond81088c2014-01-16 01:38:29 +00002608 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002609 // We use the current number of symbols in the symbol table in lieu of
2610 // using nlist_idx in case we ever start trimming entries out
2611 N_FUN_indexes.push_back(sym_idx);
2612 }
2613 else
2614 {
2615 type = eSymbolTypeCompiler;
2616
2617 if ( !N_FUN_indexes.empty() )
2618 {
2619 // Copy the size of the function into the original STAB entry so we don't have
2620 // to hunt for it later
2621 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2622 N_FUN_indexes.pop_back();
2623 // We don't really need the end function STAB as it contains the size which
2624 // we already placed with the original symbol, so don't add it if we want a
2625 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002626 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002627 }
2628 }
2629 break;
2630
Charles Davis510938e2013-08-27 05:04:57 +00002631 case N_STSYM:
2632 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00002633 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002634 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2635 type = eSymbolTypeData;
2636 break;
2637
Charles Davis510938e2013-08-27 05:04:57 +00002638 case N_LCSYM:
2639 // .lcomm symbol: name,,n_sect,type,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002640 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2641 type = eSymbolTypeCommonBlock;
2642 break;
2643
Charles Davis510938e2013-08-27 05:04:57 +00002644 case N_BNSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002645 // We use the current number of symbols in the symbol table in lieu of
2646 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00002647 // Skip these if we want minimal symbol tables
2648 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002649 break;
2650
Charles Davis510938e2013-08-27 05:04:57 +00002651 case N_ENSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002652 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2653 // so that we can always skip the entire symbol if we need to navigate
2654 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00002655 // Skip these if we want minimal symbol tables
2656 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002657 break;
2658
2659
Charles Davis510938e2013-08-27 05:04:57 +00002660 case N_OPT:
2661 // emitted with gcc2_compiled and in gcc source
Jason Molendaa5609c82012-06-21 01:51:02 +00002662 type = eSymbolTypeCompiler;
2663 break;
2664
Charles Davis510938e2013-08-27 05:04:57 +00002665 case N_RSYM:
2666 // register sym: name,,NO_SECT,type,register
Jason Molendaa5609c82012-06-21 01:51:02 +00002667 type = eSymbolTypeVariable;
2668 break;
2669
Charles Davis510938e2013-08-27 05:04:57 +00002670 case N_SLINE:
2671 // src line: 0,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002672 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2673 type = eSymbolTypeLineEntry;
2674 break;
2675
Charles Davis510938e2013-08-27 05:04:57 +00002676 case N_SSYM:
2677 // structure elt: name,,NO_SECT,type,struct_offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002678 type = eSymbolTypeVariableType;
2679 break;
2680
Charles Davis510938e2013-08-27 05:04:57 +00002681 case N_SO:
2682 // source file name
Jason Molendaa5609c82012-06-21 01:51:02 +00002683 type = eSymbolTypeSourceFile;
2684 if (symbol_name == NULL)
2685 {
Greg Clayton3046e662013-07-10 01:23:25 +00002686 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002687 if (N_SO_index != UINT32_MAX)
2688 {
2689 // Set the size of the N_SO to the terminating index of this N_SO
2690 // so that we can always skip the entire N_SO if we need to navigate
2691 // more quickly at the source level when parsing STABS
2692 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00002693 symbol_ptr->SetByteSize(sym_idx);
Jason Molendaa5609c82012-06-21 01:51:02 +00002694 symbol_ptr->SetSizeIsSibling(true);
2695 }
2696 N_NSYM_indexes.clear();
2697 N_INCL_indexes.clear();
2698 N_BRAC_indexes.clear();
2699 N_COMM_indexes.clear();
2700 N_FUN_indexes.clear();
2701 N_SO_index = UINT32_MAX;
2702 }
2703 else
2704 {
2705 // We use the current number of symbols in the symbol table in lieu of
2706 // using nlist_idx in case we ever start trimming entries out
2707 const bool N_SO_has_full_path = symbol_name[0] == '/';
2708 if (N_SO_has_full_path)
2709 {
Greg Clayton3046e662013-07-10 01:23:25 +00002710 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002711 {
2712 // We have two consecutive N_SO entries where the first contains a directory
2713 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00002714 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002715 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2716 add_nlist = false;
2717 }
2718 else
2719 {
2720 // This is the first entry in a N_SO that contains a directory or
2721 // a full path to the source file
2722 N_SO_index = sym_idx;
2723 }
2724 }
Greg Clayton3046e662013-07-10 01:23:25 +00002725 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002726 {
2727 // This is usually the second N_SO entry that contains just the filename,
2728 // so here we combine it with the first one if we are minimizing the symbol table
2729 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2730 if (so_path && so_path[0])
2731 {
2732 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00002733 const size_t double_slash_pos = full_so_path.find("//");
2734 if (double_slash_pos != std::string::npos)
2735 {
2736 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00002737 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Clayton0662d962012-09-07 20:29:13 +00002738 // and the second is the directory for the source file so you end up with
2739 // a path that looks like "/tmp/src//tmp/src/"
2740 FileSpec so_dir(so_path, false);
2741 if (!so_dir.Exists())
2742 {
2743 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2744 if (so_dir.Exists())
2745 {
2746 // Trim off the incorrect path
2747 full_so_path.erase(0, double_slash_pos + 1);
2748 }
2749 }
2750 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002751 if (*full_so_path.rbegin() != '/')
2752 full_so_path += '/';
2753 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00002754 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002755 add_nlist = false;
2756 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2757 }
2758 }
Greg Clayton946f8902012-09-05 22:30:51 +00002759 else
2760 {
2761 // This could be a relative path to a N_SO
2762 N_SO_index = sym_idx;
2763 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002764 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002765 break;
2766
Charles Davis510938e2013-08-27 05:04:57 +00002767 case N_OSO:
2768 // object file name: name,,0,0,st_mtime
Jason Molendaa5609c82012-06-21 01:51:02 +00002769 type = eSymbolTypeObjectFile;
2770 break;
2771
Charles Davis510938e2013-08-27 05:04:57 +00002772 case N_LSYM:
2773 // local sym: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002774 type = eSymbolTypeLocal;
2775 break;
2776
2777 //----------------------------------------------------------------------
2778 // INCL scopes
2779 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00002780 case N_BINCL:
2781 // include file beginning: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00002782 // We use the current number of symbols in the symbol table in lieu of
2783 // using nlist_idx in case we ever start trimming entries out
2784 N_INCL_indexes.push_back(sym_idx);
2785 type = eSymbolTypeScopeBegin;
2786 break;
2787
Charles Davis510938e2013-08-27 05:04:57 +00002788 case N_EINCL:
2789 // include file end: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002790 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2791 // so that we can always skip the entire symbol if we need to navigate
2792 // more quickly at the source level when parsing STABS
2793 if ( !N_INCL_indexes.empty() )
2794 {
2795 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2796 symbol_ptr->SetByteSize(sym_idx + 1);
2797 symbol_ptr->SetSizeIsSibling(true);
2798 N_INCL_indexes.pop_back();
2799 }
2800 type = eSymbolTypeScopeEnd;
2801 break;
2802
Charles Davis510938e2013-08-27 05:04:57 +00002803 case N_SOL:
2804 // #included file name: name,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002805 type = eSymbolTypeHeaderFile;
2806
2807 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00002808 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002809 break;
2810
Charles Davis510938e2013-08-27 05:04:57 +00002811 case N_PARAMS:
2812 // compiler parameters: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002813 type = eSymbolTypeCompiler;
2814 break;
2815
Charles Davis510938e2013-08-27 05:04:57 +00002816 case N_VERSION:
2817 // compiler version: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002818 type = eSymbolTypeCompiler;
2819 break;
2820
Charles Davis510938e2013-08-27 05:04:57 +00002821 case N_OLEVEL:
2822 // compiler -O level: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002823 type = eSymbolTypeCompiler;
2824 break;
2825
Charles Davis510938e2013-08-27 05:04:57 +00002826 case N_PSYM:
2827 // parameter: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002828 type = eSymbolTypeVariable;
2829 break;
2830
Charles Davis510938e2013-08-27 05:04:57 +00002831 case N_ENTRY:
2832 // alternate entry: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002833 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2834 type = eSymbolTypeLineEntry;
2835 break;
2836
2837 //----------------------------------------------------------------------
2838 // Left and Right Braces
2839 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00002840 case N_LBRAC:
2841 // left bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002842 // We use the current number of symbols in the symbol table in lieu of
2843 // using nlist_idx in case we ever start trimming entries out
2844 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2845 N_BRAC_indexes.push_back(sym_idx);
2846 type = eSymbolTypeScopeBegin;
2847 break;
2848
Charles Davis510938e2013-08-27 05:04:57 +00002849 case N_RBRAC:
2850 // right bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002851 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2852 // so that we can always skip the entire symbol if we need to navigate
2853 // more quickly at the source level when parsing STABS
2854 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2855 if ( !N_BRAC_indexes.empty() )
2856 {
2857 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2858 symbol_ptr->SetByteSize(sym_idx + 1);
2859 symbol_ptr->SetSizeIsSibling(true);
2860 N_BRAC_indexes.pop_back();
2861 }
2862 type = eSymbolTypeScopeEnd;
2863 break;
2864
Charles Davis510938e2013-08-27 05:04:57 +00002865 case N_EXCL:
2866 // deleted include file: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00002867 type = eSymbolTypeHeaderFile;
2868 break;
2869
2870 //----------------------------------------------------------------------
2871 // COMM scopes
2872 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00002873 case N_BCOMM:
2874 // begin common: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002875 // We use the current number of symbols in the symbol table in lieu of
2876 // using nlist_idx in case we ever start trimming entries out
2877 type = eSymbolTypeScopeBegin;
2878 N_COMM_indexes.push_back(sym_idx);
2879 break;
2880
Charles Davis510938e2013-08-27 05:04:57 +00002881 case N_ECOML:
2882 // end common (local name): 0,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002883 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2884 // Fall through
2885
Charles Davis510938e2013-08-27 05:04:57 +00002886 case N_ECOMM:
2887 // end common: name,,n_sect,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002888 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2889 // so that we can always skip the entire symbol if we need to navigate
2890 // more quickly at the source level when parsing STABS
2891 if ( !N_COMM_indexes.empty() )
2892 {
2893 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2894 symbol_ptr->SetByteSize(sym_idx + 1);
2895 symbol_ptr->SetSizeIsSibling(true);
2896 N_COMM_indexes.pop_back();
2897 }
2898 type = eSymbolTypeScopeEnd;
2899 break;
2900
Charles Davis510938e2013-08-27 05:04:57 +00002901 case N_LENG:
2902 // second stab entry with length information
Jason Molendaa5609c82012-06-21 01:51:02 +00002903 type = eSymbolTypeAdditional;
2904 break;
2905
2906 default: break;
2907 }
2908 }
2909 else
2910 {
Charles Davis510938e2013-08-27 05:04:57 +00002911 //uint8_t n_pext = N_PEXT & nlist.n_type;
2912 uint8_t n_type = N_TYPE & nlist.n_type;
2913 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Jason Molendaa5609c82012-06-21 01:51:02 +00002914
2915 switch (n_type)
2916 {
Charles Davis510938e2013-08-27 05:04:57 +00002917 case N_INDR: // Fall through
2918 case N_PBUD: // Fall through
2919 case N_UNDF:
Jason Molendaa5609c82012-06-21 01:51:02 +00002920 type = eSymbolTypeUndefined;
2921 break;
2922
Charles Davis510938e2013-08-27 05:04:57 +00002923 case N_ABS:
Jason Molendaa5609c82012-06-21 01:51:02 +00002924 type = eSymbolTypeAbsolute;
2925 break;
2926
Charles Davis510938e2013-08-27 05:04:57 +00002927 case N_SECT:
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002928 {
2929 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002930
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002931 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00002932 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002933 // TODO: warn about this?
2934 add_nlist = false;
2935 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002936 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002937
2938 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00002939 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002940 type = eSymbolTypeException;
2941 }
2942 else
2943 {
Charles Davis510938e2013-08-27 05:04:57 +00002944 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002945
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002946 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00002947 {
Charles Davis510938e2013-08-27 05:04:57 +00002948 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
2949 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
2950 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
2951 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2952 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2953 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2954 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2955 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2956 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
Charles Davis510938e2013-08-27 05:04:57 +00002957 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2958 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
2959 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
2960 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton38f9cc42014-06-16 22:53:16 +00002961 default:
2962 switch (symbol_section->GetType())
2963 {
2964 case lldb::eSectionTypeCode:
2965 type = eSymbolTypeCode;
2966 break;
2967 case eSectionTypeData:
2968 case eSectionTypeDataCString: // Inlined C string data
2969 case eSectionTypeDataCStringPointers: // Pointers to C string data
2970 case eSectionTypeDataSymbolAddress: // Address of a symbol in the symbol table
2971 case eSectionTypeData4:
2972 case eSectionTypeData8:
2973 case eSectionTypeData16:
Greg Clayton38f9cc42014-06-16 22:53:16 +00002974 type = eSymbolTypeData;
2975 break;
2976 default:
2977 break;
2978 }
2979 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002980 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002981
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002982 if (type == eSymbolTypeInvalid)
2983 {
2984 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2985 if (symbol_section->IsDescendant (text_section_sp.get()))
2986 {
Charles Davis510938e2013-08-27 05:04:57 +00002987 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
2988 S_ATTR_SELF_MODIFYING_CODE |
2989 S_ATTR_SOME_INSTRUCTIONS))
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002990 type = eSymbolTypeData;
2991 else
2992 type = eSymbolTypeCode;
2993 }
2994 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00002995 {
2996 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2997 {
2998 type = eSymbolTypeRuntime;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002999
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003000 if (symbol_name &&
3001 symbol_name[0] == '_' &&
3002 symbol_name[1] == 'O' &&
Jason Molendaa5609c82012-06-21 01:51:02 +00003003 symbol_name[2] == 'B')
3004 {
3005 llvm::StringRef symbol_name_ref(symbol_name);
3006 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3007 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3008 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3009 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3010 {
3011 symbol_name_non_abi_mangled = symbol_name + 1;
3012 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3013 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003014 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003015 }
3016 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3017 {
3018 symbol_name_non_abi_mangled = symbol_name + 1;
3019 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3020 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003021 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003022 }
3023 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3024 {
3025 symbol_name_non_abi_mangled = symbol_name + 1;
3026 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3027 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003028 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003029 }
3030 }
3031 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003032 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00003033 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003034 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00003035 }
3036 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003037 {
3038 type = eSymbolTypeData;
3039 }
3040 }
3041 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3042 {
3043 type = eSymbolTypeTrampoline;
3044 }
3045 else if (symbol_section->IsDescendant(objc_section_sp.get()))
3046 {
3047 type = eSymbolTypeRuntime;
3048 if (symbol_name && symbol_name[0] == '.')
3049 {
3050 llvm::StringRef symbol_name_ref(symbol_name);
3051 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3052 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00003053 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003054 symbol_name_non_abi_mangled = symbol_name;
3055 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3056 type = eSymbolTypeObjCClass;
3057 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003058 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003059 }
3060 }
3061 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003062 }
3063 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003064 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003065 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003066 }
3067
3068 if (add_nlist)
3069 {
3070 uint64_t symbol_value = nlist.n_value;
Jason Molendaa5609c82012-06-21 01:51:02 +00003071 if (symbol_name_non_abi_mangled)
3072 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00003073 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3074 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00003075 }
3076 else
3077 {
Greg Clayton3046e662013-07-10 01:23:25 +00003078 bool symbol_name_is_mangled = false;
3079
Jason Molendaa5609c82012-06-21 01:51:02 +00003080 if (symbol_name && symbol_name[0] == '_')
3081 {
3082 symbol_name_is_mangled = symbol_name[1] == '_';
3083 symbol_name++; // Skip the leading underscore
3084 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003085
Jason Molendaa5609c82012-06-21 01:51:02 +00003086 if (symbol_name)
3087 {
Greg Claytondacc4a92013-05-14 22:19:37 +00003088 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00003089 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00003090 if (is_gsym && is_debug)
3091 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
Jason Molendaa5609c82012-06-21 01:51:02 +00003092 }
3093 }
3094 if (symbol_section)
3095 {
3096 const addr_t section_file_addr = symbol_section->GetFileAddress();
3097 if (symbol_byte_size == 0 && function_starts_count > 0)
3098 {
3099 addr_t symbol_lookup_file_addr = nlist.n_value;
3100 // Do an exact address match for non-ARM addresses, else get the closest since
3101 // the symbol might be a thumb symbol which has an address with bit zero set
3102 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3103 if (is_arm && func_start_entry)
3104 {
3105 // Verify that the function start address is the symbol address (ARM)
3106 // or the symbol address + 1 (thumb)
3107 if (func_start_entry->addr != symbol_lookup_file_addr &&
3108 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3109 {
3110 // Not the right entry, NULL it out...
3111 func_start_entry = NULL;
3112 }
3113 }
3114 if (func_start_entry)
3115 {
3116 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003117
Jason Molendaa5609c82012-06-21 01:51:02 +00003118 addr_t symbol_file_addr = func_start_entry->addr;
3119 uint32_t symbol_flags = 0;
3120 if (is_arm)
3121 {
3122 if (symbol_file_addr & 1)
3123 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3124 symbol_file_addr &= 0xfffffffffffffffeull;
3125 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003126
Jason Molendaa5609c82012-06-21 01:51:02 +00003127 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3128 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3129 if (next_func_start_entry)
3130 {
3131 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3132 // Be sure the clear the Thumb address bit when we calculate the size
3133 // from the current and next address
3134 if (is_arm)
3135 next_symbol_file_addr &= 0xfffffffffffffffeull;
3136 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3137 }
3138 else
3139 {
3140 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3141 }
3142 }
3143 }
3144 symbol_value -= section_file_addr;
3145 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003146
Greg Claytondacc4a92013-05-14 22:19:37 +00003147 if (is_debug == false)
3148 {
3149 if (type == eSymbolTypeCode)
3150 {
3151 // See if we can find a N_FUN entry for any code symbols.
3152 // If we do find a match, and the name matches, then we
3153 // can merge the two into just the function symbol to avoid
3154 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003155 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3156 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3157 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00003158 {
Greg Claytond81088c2014-01-16 01:38:29 +00003159 bool found_it = false;
3160 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00003161 {
Greg Claytond81088c2014-01-16 01:38:29 +00003162 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3163 {
3164 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3165 // We just need the flags from the linker symbol, so put these flags
3166 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3167 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3168 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3169 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3170 sym[pos->second].SetType (eSymbolTypeResolver);
3171 sym[sym_idx].Clear();
3172 found_it = true;
3173 break;
3174 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003175 }
Greg Claytond81088c2014-01-16 01:38:29 +00003176 if (found_it)
3177 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00003178 }
Jim Inghamea3ac272014-01-10 22:55:37 +00003179 else
3180 {
Greg Claytond81088c2014-01-16 01:38:29 +00003181 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00003182 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00003183 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003184 }
3185 else if (type == eSymbolTypeData)
3186 {
3187 // See if we can find a N_STSYM entry for any data symbols.
3188 // If we do find a match, and the name matches, then we
3189 // can merge the two into just the Static symbol to avoid
3190 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003191 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3192 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
3193 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00003194 {
Greg Claytond81088c2014-01-16 01:38:29 +00003195 bool found_it = false;
3196 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00003197 {
Greg Claytond81088c2014-01-16 01:38:29 +00003198 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3199 {
3200 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3201 // We just need the flags from the linker symbol, so put these flags
3202 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3203 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3204 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3205 sym[sym_idx].Clear();
3206 found_it = true;
3207 break;
3208 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003209 }
Greg Claytond81088c2014-01-16 01:38:29 +00003210 if (found_it)
3211 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00003212 }
3213 else
3214 {
3215 // Combine N_GSYM stab entries with the non stab symbol
Greg Clayton3046e662013-07-10 01:23:25 +00003216 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 +00003217 if (pos != N_GSYM_name_to_sym_idx.end())
3218 {
3219 const uint32_t GSYM_sym_idx = pos->second;
3220 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3221 // Copy the address, because often the N_GSYM address has an invalid address of zero
3222 // when the global is a common symbol
3223 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3224 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3225 // We just need the flags from the linker symbol, so put these flags
3226 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3227 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3228 sym[sym_idx].Clear();
3229 continue;
3230 }
3231 }
3232 }
3233 }
3234
Jason Molendaa5609c82012-06-21 01:51:02 +00003235 sym[sym_idx].SetID (nlist_idx);
3236 sym[sym_idx].SetType (type);
3237 sym[sym_idx].GetAddress().SetSection (symbol_section);
3238 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3239 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003240
Jason Molendaa5609c82012-06-21 01:51:02 +00003241 if (symbol_byte_size > 0)
3242 sym[sym_idx].SetByteSize(symbol_byte_size);
3243
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003244 if (demangled_is_synthesized)
3245 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00003246 ++sym_idx;
3247 }
3248 else
3249 {
3250 sym[sym_idx].Clear();
3251 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003252
Jason Molendaa5609c82012-06-21 01:51:02 +00003253 }
3254 /////////////////////////////
3255 }
3256 break; // No more entries to consider
3257 }
3258 }
3259 }
3260 }
3261 }
3262 }
3263 }
3264
3265 // Must reset this in case it was mutated above!
3266 nlist_data_offset = 0;
3267#endif
Jim Inghamea3ac272014-01-10 22:55:37 +00003268
Greg Claytonfd814c52013-08-13 01:42:25 +00003269 if (nlist_data.GetByteSize() > 0)
Jason Molendaa5609c82012-06-21 01:51:02 +00003270 {
Jason Molendaa5609c82012-06-21 01:51:02 +00003271
Greg Claytonfd814c52013-08-13 01:42:25 +00003272 // If the sym array was not created while parsing the DSC unmapped
3273 // symbols, create it now.
3274 if (sym == NULL)
Greg Clayton4c82d422012-05-18 23:20:01 +00003275 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003276 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
3277 num_syms = symtab->GetNumSymbols();
3278 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003279
Greg Claytonfd814c52013-08-13 01:42:25 +00003280 if (unmapped_local_symbols_found)
3281 {
3282 assert(m_dysymtab.ilocalsym == 0);
3283 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
3284 nlist_idx = m_dysymtab.nlocalsym;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003285 }
Greg Claytondebb8812012-05-25 17:04:00 +00003286 else
3287 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003288 nlist_idx = 0;
Greg Claytondebb8812012-05-25 17:04:00 +00003289 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003290
Greg Claytonfd814c52013-08-13 01:42:25 +00003291 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003292 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003293 struct nlist_64 nlist;
3294 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
3295 break;
3296
3297 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
3298 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
3299 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
3300 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
3301 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
3302
3303 SymbolType type = eSymbolTypeInvalid;
3304 const char *symbol_name = NULL;
3305
3306 if (have_strtab_data)
Greg Clayton77ccca72011-12-30 00:32:24 +00003307 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003308 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003309
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003310 if (symbol_name == NULL)
3311 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003312 // No symbol should be NULL, even the symbols with no
3313 // string values should have an offset zero which points
3314 // to an empty C-string
3315 Host::SystemLog (Host::eSystemLogError,
3316 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
3317 nlist_idx,
3318 nlist.n_strx,
3319 module_sp->GetFileSpec().GetPath().c_str());
3320 continue;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003321 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003322 if (symbol_name[0] == '\0')
3323 symbol_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003324 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003325 else
3326 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003327 const addr_t str_addr = strtab_addr + nlist.n_strx;
3328 Error str_error;
3329 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
3330 symbol_name = memory_symbol_name.c_str();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003331 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003332 const char *symbol_name_non_abi_mangled = NULL;
3333
3334 SectionSP symbol_section;
3335 lldb::addr_t symbol_byte_size = 0;
3336 bool add_nlist = true;
3337 bool is_gsym = false;
Charles Davis510938e2013-08-27 05:04:57 +00003338 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00003339 bool demangled_is_synthesized = false;
3340
3341 assert (sym_idx < num_syms);
3342
3343 sym[sym_idx].SetDebug (is_debug);
3344
3345 if (is_debug)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003346 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003347 switch (nlist.n_type)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003348 {
Charles Davis510938e2013-08-27 05:04:57 +00003349 case N_GSYM:
3350 // global symbol: name,,NO_SECT,type,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003351 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003352
Greg Claytonfd814c52013-08-13 01:42:25 +00003353 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
3354 // have the same address, but we want to ensure that we always find only the real symbol,
3355 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
3356 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
3357 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
3358 // same address.
Greg Clayton29e08cb2012-03-14 01:53:24 +00003359
Greg Claytonfd814c52013-08-13 01:42:25 +00003360 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
3361 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
3362 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
3363 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
3364 add_nlist = false;
3365 else
3366 {
3367 is_gsym = true;
3368 sym[sym_idx].SetExternal(true);
3369 if (nlist.n_value != 0)
3370 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3371 type = eSymbolTypeData;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003372 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003373 break;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003374
Charles Davis510938e2013-08-27 05:04:57 +00003375 case N_FNAME:
3376 // procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003377 type = eSymbolTypeCompiler;
3378 break;
3379
Charles Davis510938e2013-08-27 05:04:57 +00003380 case N_FUN:
3381 // procedure: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003382 if (symbol_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00003383 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003384 type = eSymbolTypeCode;
3385 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3386
Greg Claytond81088c2014-01-16 01:38:29 +00003387 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003388 // We use the current number of symbols in the symbol table in lieu of
3389 // using nlist_idx in case we ever start trimming entries out
3390 N_FUN_indexes.push_back(sym_idx);
Greg Claytondacc4a92013-05-14 22:19:37 +00003391 }
3392 else
3393 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003394 type = eSymbolTypeCompiler;
3395
3396 if ( !N_FUN_indexes.empty() )
Greg Claytondacc4a92013-05-14 22:19:37 +00003397 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003398 // Copy the size of the function into the original STAB entry so we don't have
3399 // to hunt for it later
3400 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
3401 N_FUN_indexes.pop_back();
3402 // We don't really need the end function STAB as it contains the size which
3403 // we already placed with the original symbol, so don't add it if we want a
3404 // minimal symbol table
3405 add_nlist = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00003406 }
3407 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003408 break;
3409
Charles Davis510938e2013-08-27 05:04:57 +00003410 case N_STSYM:
3411 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00003412 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003413 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3414 type = eSymbolTypeData;
3415 break;
3416
Charles Davis510938e2013-08-27 05:04:57 +00003417 case N_LCSYM:
3418 // .lcomm symbol: name,,n_sect,type,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003419 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3420 type = eSymbolTypeCommonBlock;
3421 break;
3422
Charles Davis510938e2013-08-27 05:04:57 +00003423 case N_BNSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003424 // We use the current number of symbols in the symbol table in lieu of
3425 // using nlist_idx in case we ever start trimming entries out
3426 // Skip these if we want minimal symbol tables
3427 add_nlist = false;
3428 break;
3429
Charles Davis510938e2013-08-27 05:04:57 +00003430 case N_ENSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003431 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
3432 // so that we can always skip the entire symbol if we need to navigate
3433 // more quickly at the source level when parsing STABS
3434 // Skip these if we want minimal symbol tables
3435 add_nlist = false;
3436 break;
3437
3438
Charles Davis510938e2013-08-27 05:04:57 +00003439 case N_OPT:
3440 // emitted with gcc2_compiled and in gcc source
Greg Claytonfd814c52013-08-13 01:42:25 +00003441 type = eSymbolTypeCompiler;
3442 break;
3443
Charles Davis510938e2013-08-27 05:04:57 +00003444 case N_RSYM:
3445 // register sym: name,,NO_SECT,type,register
Greg Claytonfd814c52013-08-13 01:42:25 +00003446 type = eSymbolTypeVariable;
3447 break;
3448
Charles Davis510938e2013-08-27 05:04:57 +00003449 case N_SLINE:
3450 // src line: 0,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003451 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3452 type = eSymbolTypeLineEntry;
3453 break;
3454
Charles Davis510938e2013-08-27 05:04:57 +00003455 case N_SSYM:
3456 // structure elt: name,,NO_SECT,type,struct_offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003457 type = eSymbolTypeVariableType;
3458 break;
3459
Charles Davis510938e2013-08-27 05:04:57 +00003460 case N_SO:
3461 // source file name
Greg Claytonfd814c52013-08-13 01:42:25 +00003462 type = eSymbolTypeSourceFile;
3463 if (symbol_name == NULL)
3464 {
3465 add_nlist = false;
3466 if (N_SO_index != UINT32_MAX)
3467 {
3468 // Set the size of the N_SO to the terminating index of this N_SO
3469 // so that we can always skip the entire N_SO if we need to navigate
3470 // more quickly at the source level when parsing STABS
3471 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
3472 symbol_ptr->SetByteSize(sym_idx);
3473 symbol_ptr->SetSizeIsSibling(true);
3474 }
3475 N_NSYM_indexes.clear();
3476 N_INCL_indexes.clear();
3477 N_BRAC_indexes.clear();
3478 N_COMM_indexes.clear();
3479 N_FUN_indexes.clear();
3480 N_SO_index = UINT32_MAX;
3481 }
3482 else
3483 {
3484 // We use the current number of symbols in the symbol table in lieu of
3485 // using nlist_idx in case we ever start trimming entries out
3486 const bool N_SO_has_full_path = symbol_name[0] == '/';
3487 if (N_SO_has_full_path)
3488 {
3489 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3490 {
3491 // We have two consecutive N_SO entries where the first contains a directory
3492 // and the second contains a full path.
3493 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
3494 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3495 add_nlist = false;
3496 }
3497 else
3498 {
3499 // This is the first entry in a N_SO that contains a directory or
3500 // a full path to the source file
3501 N_SO_index = sym_idx;
3502 }
3503 }
3504 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3505 {
3506 // This is usually the second N_SO entry that contains just the filename,
3507 // so here we combine it with the first one if we are minimizing the symbol table
3508 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
3509 if (so_path && so_path[0])
3510 {
3511 std::string full_so_path (so_path);
3512 const size_t double_slash_pos = full_so_path.find("//");
3513 if (double_slash_pos != std::string::npos)
3514 {
3515 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00003516 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Claytonfd814c52013-08-13 01:42:25 +00003517 // and the second is the directory for the source file so you end up with
3518 // a path that looks like "/tmp/src//tmp/src/"
3519 FileSpec so_dir(so_path, false);
3520 if (!so_dir.Exists())
3521 {
3522 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3523 if (so_dir.Exists())
3524 {
3525 // Trim off the incorrect path
3526 full_so_path.erase(0, double_slash_pos + 1);
3527 }
3528 }
3529 }
3530 if (*full_so_path.rbegin() != '/')
3531 full_so_path += '/';
3532 full_so_path += symbol_name;
3533 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
3534 add_nlist = false;
3535 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3536 }
3537 }
3538 else
3539 {
3540 // This could be a relative path to a N_SO
3541 N_SO_index = sym_idx;
3542 }
3543 }
3544
3545 break;
3546
Charles Davis510938e2013-08-27 05:04:57 +00003547 case N_OSO:
3548 // object file name: name,,0,0,st_mtime
Greg Claytonfd814c52013-08-13 01:42:25 +00003549 type = eSymbolTypeObjectFile;
3550 break;
3551
Charles Davis510938e2013-08-27 05:04:57 +00003552 case N_LSYM:
3553 // local sym: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003554 type = eSymbolTypeLocal;
3555 break;
3556
3557 //----------------------------------------------------------------------
3558 // INCL scopes
3559 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003560 case N_BINCL:
3561 // include file beginning: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00003562 // We use the current number of symbols in the symbol table in lieu of
3563 // using nlist_idx in case we ever start trimming entries out
3564 N_INCL_indexes.push_back(sym_idx);
3565 type = eSymbolTypeScopeBegin;
3566 break;
3567
Charles Davis510938e2013-08-27 05:04:57 +00003568 case N_EINCL:
3569 // include file end: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003570 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3571 // so that we can always skip the entire symbol if we need to navigate
3572 // more quickly at the source level when parsing STABS
3573 if ( !N_INCL_indexes.empty() )
3574 {
3575 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3576 symbol_ptr->SetByteSize(sym_idx + 1);
3577 symbol_ptr->SetSizeIsSibling(true);
3578 N_INCL_indexes.pop_back();
3579 }
3580 type = eSymbolTypeScopeEnd;
3581 break;
3582
Charles Davis510938e2013-08-27 05:04:57 +00003583 case N_SOL:
3584 // #included file name: name,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003585 type = eSymbolTypeHeaderFile;
3586
3587 // We currently don't use the header files on darwin
3588 add_nlist = false;
3589 break;
3590
Charles Davis510938e2013-08-27 05:04:57 +00003591 case N_PARAMS:
3592 // compiler parameters: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003593 type = eSymbolTypeCompiler;
3594 break;
3595
Charles Davis510938e2013-08-27 05:04:57 +00003596 case N_VERSION:
3597 // compiler version: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003598 type = eSymbolTypeCompiler;
3599 break;
3600
Charles Davis510938e2013-08-27 05:04:57 +00003601 case N_OLEVEL:
3602 // compiler -O level: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003603 type = eSymbolTypeCompiler;
3604 break;
3605
Charles Davis510938e2013-08-27 05:04:57 +00003606 case N_PSYM:
3607 // parameter: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003608 type = eSymbolTypeVariable;
3609 break;
3610
Charles Davis510938e2013-08-27 05:04:57 +00003611 case N_ENTRY:
3612 // alternate entry: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003613 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3614 type = eSymbolTypeLineEntry;
3615 break;
3616
3617 //----------------------------------------------------------------------
3618 // Left and Right Braces
3619 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003620 case N_LBRAC:
3621 // left bracket: 0,,NO_SECT,nesting level,address
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 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3625 N_BRAC_indexes.push_back(sym_idx);
3626 type = eSymbolTypeScopeBegin;
3627 break;
3628
Charles Davis510938e2013-08-27 05:04:57 +00003629 case N_RBRAC:
3630 // right bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003631 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3632 // so that we can always skip the entire symbol if we need to navigate
3633 // more quickly at the source level when parsing STABS
3634 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3635 if ( !N_BRAC_indexes.empty() )
3636 {
3637 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3638 symbol_ptr->SetByteSize(sym_idx + 1);
3639 symbol_ptr->SetSizeIsSibling(true);
3640 N_BRAC_indexes.pop_back();
3641 }
3642 type = eSymbolTypeScopeEnd;
3643 break;
3644
Charles Davis510938e2013-08-27 05:04:57 +00003645 case N_EXCL:
3646 // deleted include file: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00003647 type = eSymbolTypeHeaderFile;
3648 break;
3649
3650 //----------------------------------------------------------------------
3651 // COMM scopes
3652 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003653 case N_BCOMM:
3654 // begin common: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003655 // We use the current number of symbols in the symbol table in lieu of
3656 // using nlist_idx in case we ever start trimming entries out
3657 type = eSymbolTypeScopeBegin;
3658 N_COMM_indexes.push_back(sym_idx);
3659 break;
3660
Charles Davis510938e2013-08-27 05:04:57 +00003661 case N_ECOML:
3662 // end common (local name): 0,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003663 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3664 // Fall through
3665
Charles Davis510938e2013-08-27 05:04:57 +00003666 case N_ECOMM:
3667 // end common: name,,n_sect,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003668 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3669 // so that we can always skip the entire symbol if we need to navigate
3670 // more quickly at the source level when parsing STABS
3671 if ( !N_COMM_indexes.empty() )
3672 {
3673 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3674 symbol_ptr->SetByteSize(sym_idx + 1);
3675 symbol_ptr->SetSizeIsSibling(true);
3676 N_COMM_indexes.pop_back();
3677 }
3678 type = eSymbolTypeScopeEnd;
3679 break;
3680
Charles Davis510938e2013-08-27 05:04:57 +00003681 case N_LENG:
3682 // second stab entry with length information
Greg Claytonfd814c52013-08-13 01:42:25 +00003683 type = eSymbolTypeAdditional;
3684 break;
3685
3686 default: break;
3687 }
3688 }
3689 else
3690 {
Charles Davis510938e2013-08-27 05:04:57 +00003691 //uint8_t n_pext = N_PEXT & nlist.n_type;
3692 uint8_t n_type = N_TYPE & nlist.n_type;
3693 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00003694
3695 switch (n_type)
3696 {
Charles Davis510938e2013-08-27 05:04:57 +00003697 case N_INDR:// Fall through
3698 case N_PBUD:// Fall through
3699 case N_UNDF:
Greg Claytonfd814c52013-08-13 01:42:25 +00003700 type = eSymbolTypeUndefined;
3701 break;
3702
Charles Davis510938e2013-08-27 05:04:57 +00003703 case N_ABS:
Greg Claytonfd814c52013-08-13 01:42:25 +00003704 type = eSymbolTypeAbsolute;
3705 break;
3706
Charles Davis510938e2013-08-27 05:04:57 +00003707 case N_SECT:
Greg Claytonfd814c52013-08-13 01:42:25 +00003708 {
3709 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3710
3711 if (!symbol_section)
3712 {
3713 // TODO: warn about this?
3714 add_nlist = false;
3715 break;
3716 }
3717
3718 if (TEXT_eh_frame_sectID == nlist.n_sect)
3719 {
3720 type = eSymbolTypeException;
3721 }
3722 else
3723 {
Charles Davis510938e2013-08-27 05:04:57 +00003724 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Greg Claytonfd814c52013-08-13 01:42:25 +00003725
3726 switch (section_type)
3727 {
Charles Davis510938e2013-08-27 05:04:57 +00003728 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
3729 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
3730 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
3731 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3732 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3733 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3734 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3735 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3736 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
Charles Davis510938e2013-08-27 05:04:57 +00003737 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3738 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
3739 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
3740 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton38f9cc42014-06-16 22:53:16 +00003741 default:
3742 switch (symbol_section->GetType())
3743 {
3744 case lldb::eSectionTypeCode:
3745 type = eSymbolTypeCode;
3746 break;
3747 case eSectionTypeData:
3748 case eSectionTypeDataCString: // Inlined C string data
3749 case eSectionTypeDataCStringPointers: // Pointers to C string data
3750 case eSectionTypeDataSymbolAddress: // Address of a symbol in the symbol table
3751 case eSectionTypeData4:
3752 case eSectionTypeData8:
3753 case eSectionTypeData16:
Greg Clayton38f9cc42014-06-16 22:53:16 +00003754 type = eSymbolTypeData;
3755 break;
3756 default:
3757 break;
3758 }
3759 break;
Greg Claytonfd814c52013-08-13 01:42:25 +00003760 }
3761
3762 if (type == eSymbolTypeInvalid)
3763 {
3764 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3765 if (symbol_section->IsDescendant (text_section_sp.get()))
3766 {
Charles Davis510938e2013-08-27 05:04:57 +00003767 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
3768 S_ATTR_SELF_MODIFYING_CODE |
3769 S_ATTR_SOME_INSTRUCTIONS))
Greg Claytonfd814c52013-08-13 01:42:25 +00003770 type = eSymbolTypeData;
3771 else
3772 type = eSymbolTypeCode;
3773 }
3774 else
3775 if (symbol_section->IsDescendant(data_section_sp.get()))
3776 {
3777 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
3778 {
3779 type = eSymbolTypeRuntime;
3780
3781 if (symbol_name &&
3782 symbol_name[0] == '_' &&
3783 symbol_name[1] == 'O' &&
3784 symbol_name[2] == 'B')
3785 {
3786 llvm::StringRef symbol_name_ref(symbol_name);
3787 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3788 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3789 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3790 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3791 {
3792 symbol_name_non_abi_mangled = symbol_name + 1;
3793 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3794 type = eSymbolTypeObjCClass;
3795 demangled_is_synthesized = true;
3796 }
3797 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3798 {
3799 symbol_name_non_abi_mangled = symbol_name + 1;
3800 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3801 type = eSymbolTypeObjCMetaClass;
3802 demangled_is_synthesized = true;
3803 }
3804 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3805 {
3806 symbol_name_non_abi_mangled = symbol_name + 1;
3807 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3808 type = eSymbolTypeObjCIVar;
3809 demangled_is_synthesized = true;
3810 }
3811 }
3812 }
3813 else
3814 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3815 {
3816 type = eSymbolTypeException;
3817 }
3818 else
3819 {
3820 type = eSymbolTypeData;
3821 }
3822 }
3823 else
3824 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3825 {
3826 type = eSymbolTypeTrampoline;
3827 }
3828 else
3829 if (symbol_section->IsDescendant(objc_section_sp.get()))
3830 {
3831 type = eSymbolTypeRuntime;
3832 if (symbol_name && symbol_name[0] == '.')
3833 {
3834 llvm::StringRef symbol_name_ref(symbol_name);
3835 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3836 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3837 {
3838 symbol_name_non_abi_mangled = symbol_name;
3839 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3840 type = eSymbolTypeObjCClass;
3841 demangled_is_synthesized = true;
3842 }
3843 }
3844 }
3845 }
3846 }
3847 }
3848 break;
Greg Claytondacc4a92013-05-14 22:19:37 +00003849 }
3850 }
3851
Greg Claytonfd814c52013-08-13 01:42:25 +00003852 if (add_nlist)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003853 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003854 uint64_t symbol_value = nlist.n_value;
3855
3856 if (symbol_name_non_abi_mangled)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003857 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003858 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3859 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
3860 }
3861 else
3862 {
3863 bool symbol_name_is_mangled = false;
3864
3865 if (symbol_name && symbol_name[0] == '_')
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003866 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003867 symbol_name_is_mangled = symbol_name[1] == '_';
3868 symbol_name++; // Skip the leading underscore
3869 }
3870
3871 if (symbol_name)
3872 {
3873 ConstString const_symbol_name(symbol_name);
3874 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
3875 if (is_gsym && is_debug)
3876 {
3877 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
3878 }
3879 }
3880 }
3881 if (symbol_section)
3882 {
3883 const addr_t section_file_addr = symbol_section->GetFileAddress();
3884 if (symbol_byte_size == 0 && function_starts_count > 0)
3885 {
3886 addr_t symbol_lookup_file_addr = nlist.n_value;
3887 // Do an exact address match for non-ARM addresses, else get the closest since
3888 // the symbol might be a thumb symbol which has an address with bit zero set
3889 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3890 if (is_arm && func_start_entry)
3891 {
3892 // Verify that the function start address is the symbol address (ARM)
3893 // or the symbol address + 1 (thumb)
3894 if (func_start_entry->addr != symbol_lookup_file_addr &&
3895 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3896 {
3897 // Not the right entry, NULL it out...
3898 func_start_entry = NULL;
3899 }
3900 }
3901 if (func_start_entry)
3902 {
3903 func_start_entry->data = true;
3904
3905 addr_t symbol_file_addr = func_start_entry->addr;
3906 if (is_arm)
3907 symbol_file_addr &= 0xfffffffffffffffeull;
3908
3909 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3910 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3911 if (next_func_start_entry)
3912 {
3913 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3914 // Be sure the clear the Thumb address bit when we calculate the size
3915 // from the current and next address
3916 if (is_arm)
3917 next_symbol_file_addr &= 0xfffffffffffffffeull;
3918 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3919 }
3920 else
3921 {
3922 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3923 }
3924 }
3925 }
3926 symbol_value -= section_file_addr;
3927 }
3928
3929 if (is_debug == false)
3930 {
3931 if (type == eSymbolTypeCode)
3932 {
3933 // See if we can find a N_FUN entry for any code symbols.
3934 // If we do find a match, and the name matches, then we
3935 // can merge the two into just the function symbol to avoid
3936 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003937 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3938 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3939 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00003940 {
Greg Claytond81088c2014-01-16 01:38:29 +00003941 bool found_it = false;
3942 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00003943 {
Greg Claytond81088c2014-01-16 01:38:29 +00003944 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3945 {
3946 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3947 // We just need the flags from the linker symbol, so put these flags
3948 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3949 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3950 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3951 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3952 sym[pos->second].SetType (eSymbolTypeResolver);
3953 sym[sym_idx].Clear();
3954 found_it = true;
3955 break;
3956 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003957 }
Greg Claytond81088c2014-01-16 01:38:29 +00003958 if (found_it)
3959 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00003960 }
Jim Inghamea3ac272014-01-10 22:55:37 +00003961 else
3962 {
3963 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00003964 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00003965 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003966 }
3967 else if (type == eSymbolTypeData)
3968 {
3969 // See if we can find a N_STSYM entry for any data symbols.
3970 // If we do find a match, and the name matches, then we
3971 // can merge the two into just the Static symbol to avoid
3972 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003973 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3974 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
3975 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00003976 {
Greg Claytond81088c2014-01-16 01:38:29 +00003977 bool found_it = false;
3978 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00003979 {
Greg Claytond81088c2014-01-16 01:38:29 +00003980 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3981 {
3982 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3983 // We just need the flags from the linker symbol, so put these flags
3984 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3985 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3986 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3987 sym[sym_idx].Clear();
3988 found_it = true;
3989 break;
3990 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003991 }
Greg Claytond81088c2014-01-16 01:38:29 +00003992 if (found_it)
3993 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00003994 }
3995 else
3996 {
3997 // Combine N_GSYM stab entries with the non stab symbol
3998 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
3999 if (pos != N_GSYM_name_to_sym_idx.end())
4000 {
4001 const uint32_t GSYM_sym_idx = pos->second;
4002 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
4003 // Copy the address, because often the N_GSYM address has an invalid address of zero
4004 // when the global is a common symbol
4005 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
4006 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
4007 // We just need the flags from the linker symbol, so put these flags
4008 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
4009 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4010 sym[sym_idx].Clear();
4011 continue;
4012 }
4013 }
4014 }
4015 }
4016
4017 sym[sym_idx].SetID (nlist_idx);
4018 sym[sym_idx].SetType (type);
4019 sym[sym_idx].GetAddress().SetSection (symbol_section);
4020 sym[sym_idx].GetAddress().SetOffset (symbol_value);
4021 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4022
4023 if (symbol_byte_size > 0)
4024 sym[sym_idx].SetByteSize(symbol_byte_size);
4025
4026 if (demangled_is_synthesized)
4027 sym[sym_idx].SetDemangledNameIsSynthesized(true);
4028
4029 ++sym_idx;
4030 }
4031 else
4032 {
4033 sym[sym_idx].Clear();
4034 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004035 }
4036 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004037
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004038 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
4039
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004040 if (function_starts_count > 0)
4041 {
4042 char synthetic_function_symbol[PATH_MAX];
4043 uint32_t num_synthetic_function_symbols = 0;
4044 for (i=0; i<function_starts_count; ++i)
4045 {
4046 if (function_starts.GetEntryRef (i).data == false)
4047 ++num_synthetic_function_symbols;
4048 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004049
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004050 if (num_synthetic_function_symbols > 0)
4051 {
4052 if (num_syms < sym_idx + num_synthetic_function_symbols)
4053 {
4054 num_syms = sym_idx + num_synthetic_function_symbols;
4055 sym = symtab->Resize (num_syms);
4056 }
4057 uint32_t synthetic_function_symbol_idx = 0;
4058 for (i=0; i<function_starts_count; ++i)
4059 {
4060 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
4061 if (func_start_entry->data == false)
4062 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004063 addr_t symbol_file_addr = func_start_entry->addr;
4064 uint32_t symbol_flags = 0;
4065 if (is_arm)
4066 {
4067 if (symbol_file_addr & 1)
4068 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
4069 symbol_file_addr &= 0xfffffffffffffffeull;
4070 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004071 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004072 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004073 {
4074 SectionSP symbol_section (symbol_addr.GetSection());
4075 uint32_t symbol_byte_size = 0;
4076 if (symbol_section)
4077 {
4078 const addr_t section_file_addr = symbol_section->GetFileAddress();
4079 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
4080 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
4081 if (next_func_start_entry)
4082 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004083 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4084 if (is_arm)
4085 next_symbol_file_addr &= 0xfffffffffffffffeull;
4086 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 +00004087 }
4088 else
4089 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004090 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004091 }
4092 snprintf (synthetic_function_symbol,
4093 sizeof(synthetic_function_symbol),
4094 "___lldb_unnamed_function%u$$%s",
4095 ++synthetic_function_symbol_idx,
4096 module_sp->GetFileSpec().GetFilename().GetCString());
4097 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Clayton037520e2012-07-18 23:18:10 +00004098 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004099 sym[sym_idx].SetType (eSymbolTypeCode);
4100 sym[sym_idx].SetIsSynthetic (true);
4101 sym[sym_idx].GetAddress() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004102 if (symbol_flags)
4103 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004104 if (symbol_byte_size)
4105 sym[sym_idx].SetByteSize (symbol_byte_size);
4106 ++sym_idx;
4107 }
4108 }
4109 }
4110 }
4111 }
4112 }
4113
4114 // Trim our symbols down to just what we ended up with after
4115 // removing any symbols.
4116 if (sym_idx < num_syms)
4117 {
4118 num_syms = sym_idx;
4119 sym = symtab->Resize (num_syms);
4120 }
4121
4122 // Now synthesize indirect symbols
4123 if (m_dysymtab.nindirectsyms != 0)
4124 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004125 if (indirect_symbol_index_data.GetByteSize())
4126 {
4127 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
4128
4129 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
4130 {
Charles Davis510938e2013-08-27 05:04:57 +00004131 if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) == S_SYMBOL_STUBS)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004132 {
4133 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
4134 if (symbol_stub_byte_size == 0)
4135 continue;
4136
4137 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
4138
4139 if (num_symbol_stubs == 0)
4140 continue;
4141
4142 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
4143 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
4144 {
4145 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
4146 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 +00004147 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004148 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
4149 {
4150 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Charles Davis510938e2013-08-27 05:04:57 +00004151 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004152 continue;
4153
4154 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
4155 Symbol *stub_symbol = NULL;
4156 if (index_pos != end_index_pos)
4157 {
4158 // We have a remapping from the original nlist index to
4159 // a current symbol index, so just look this up by index
4160 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
4161 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004162 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004163 {
4164 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00004165 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004166 // S_SYMBOL_STUBS
4167 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
4168 }
4169
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004170 if (stub_symbol)
4171 {
4172 Address so_addr(symbol_stub_addr, section_list);
4173
4174 if (stub_symbol->GetType() == eSymbolTypeUndefined)
4175 {
4176 // Change the external symbol into a trampoline that makes sense
4177 // These symbols were N_UNDF N_EXT, and are useless to us, so we
4178 // can re-use them so we don't have to make up a synthetic symbol
4179 // for no good reason.
Greg Clayton9191db42013-10-21 18:40:51 +00004180 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4181 stub_symbol->SetType (eSymbolTypeTrampoline);
4182 else
4183 stub_symbol->SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004184 stub_symbol->SetExternal (false);
4185 stub_symbol->GetAddress() = so_addr;
4186 stub_symbol->SetByteSize (symbol_stub_byte_size);
4187 }
4188 else
4189 {
4190 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00004191 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004192 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00004193 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004194 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00004195 stub_symbol = NULL; // this pointer no longer valid
4196 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004197 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00004198 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton9191db42013-10-21 18:40:51 +00004199 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4200 sym[sym_idx].SetType (eSymbolTypeTrampoline);
4201 else
4202 sym[sym_idx].SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004203 sym[sym_idx].SetIsSynthetic (true);
4204 sym[sym_idx].GetAddress() = so_addr;
4205 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
4206 ++sym_idx;
4207 }
4208 }
Greg Clayton3f839a32012-09-05 01:38:55 +00004209 else
4210 {
4211 if (log)
4212 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
4213 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004214 }
4215 }
4216 }
4217 }
4218 }
4219 }
Greg Clayton9191db42013-10-21 18:40:51 +00004220
4221
4222 if (!trie_entries.empty())
4223 {
4224 for (const auto &e : trie_entries)
4225 {
4226 if (e.entry.import_name)
4227 {
4228 // Make a synthetic symbol to describe re-exported symbol.
4229 if (sym_idx >= num_syms)
4230 sym = symtab->Resize (++num_syms);
4231 sym[sym_idx].SetID (synthetic_sym_id++);
4232 sym[sym_idx].GetMangled() = Mangled(e.entry.name);
4233 sym[sym_idx].SetType (eSymbolTypeReExported);
4234 sym[sym_idx].SetIsSynthetic (true);
4235 sym[sym_idx].SetReExportedSymbolName(e.entry.import_name);
4236 if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize())
4237 {
4238 sym[sym_idx].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(e.entry.other-1));
4239 }
4240 ++sym_idx;
4241 }
4242 }
4243 }
4244
4245
Greg Clayton3046e662013-07-10 01:23:25 +00004246
4247// StreamFile s(stdout, false);
4248// s.Printf ("Symbol table before CalculateSymbolSizes():\n");
4249// symtab->Dump(&s, NULL, eSortOrderNone);
4250 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
4251 symtab->CalculateSymbolSizes();
4252
4253// s.Printf ("Symbol table after CalculateSymbolSizes():\n");
4254// symtab->Dump(&s, NULL, eSortOrderNone);
4255
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004256 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004257 }
4258 return 0;
4259}
4260
4261
4262void
4263ObjectFileMachO::Dump (Stream *s)
4264{
Greg Claytona1743492012-03-13 23:14:29 +00004265 ModuleSP module_sp(GetModule());
4266 if (module_sp)
4267 {
4268 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004269 s->Printf("%p: ", static_cast<void*>(this));
Greg Claytona1743492012-03-13 23:14:29 +00004270 s->Indent();
Charles Davis510938e2013-08-27 05:04:57 +00004271 if (m_header.magic == MH_MAGIC_64 || m_header.magic == MH_CIGAM_64)
Greg Claytona1743492012-03-13 23:14:29 +00004272 s->PutCString("ObjectFileMachO64");
4273 else
4274 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004275
Greg Clayton7ab7f892014-05-29 21:33:45 +00004276 ArchSpec header_arch;
4277 GetArchitecture(header_arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004278
Greg Claytona1743492012-03-13 23:14:29 +00004279 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004280
Greg Clayton3046e662013-07-10 01:23:25 +00004281 SectionList *sections = GetSectionList();
4282 if (sections)
4283 sections->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004284
Greg Claytona1743492012-03-13 23:14:29 +00004285 if (m_symtab_ap.get())
4286 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
4287 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004288}
4289
Greg Claytonf4d6de62013-04-24 22:29:28 +00004290bool
4291ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
4292 const lldb_private::DataExtractor &data,
4293 lldb::offset_t lc_offset,
4294 lldb_private::UUID& uuid)
4295{
4296 uint32_t i;
4297 struct uuid_command load_cmd;
4298
4299 lldb::offset_t offset = lc_offset;
4300 for (i=0; i<header.ncmds; ++i)
4301 {
4302 const lldb::offset_t cmd_offset = offset;
4303 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4304 break;
4305
Charles Davis510938e2013-08-27 05:04:57 +00004306 if (load_cmd.cmd == LC_UUID)
Greg Claytonf4d6de62013-04-24 22:29:28 +00004307 {
4308 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
4309
4310 if (uuid_bytes)
4311 {
4312 // OpenCL on Mac OS X uses the same UUID for each of its object files.
4313 // We pretend these object files have no UUID to prevent crashing.
4314
4315 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
4316 0x3b, 0xa8,
4317 0x4b, 0x16,
4318 0xb6, 0xa4,
4319 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
4320
4321 if (!memcmp(uuid_bytes, opencl_uuid, 16))
4322 return false;
4323
4324 uuid.SetBytes (uuid_bytes);
4325 return true;
4326 }
4327 return false;
4328 }
4329 offset = cmd_offset + load_cmd.cmdsize;
4330 }
4331 return false;
4332}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004333
Greg Clayton7ab7f892014-05-29 21:33:45 +00004334
4335bool
4336ObjectFileMachO::GetArchitecture (const llvm::MachO::mach_header &header,
4337 const lldb_private::DataExtractor &data,
4338 lldb::offset_t lc_offset,
4339 ArchSpec &arch)
4340{
4341 arch.SetArchitecture (eArchTypeMachO, header.cputype, header.cpusubtype);
4342
4343 if (arch.IsValid())
4344 {
4345 llvm::Triple &triple = arch.GetTriple();
4346 if (header.filetype == MH_PRELOAD)
4347 {
4348 // Set OS to "unknown" - this is a standalone binary with no dyld et al
4349 triple.setOS(llvm::Triple::UnknownOS);
4350 return true;
4351 }
4352 else
4353 {
4354 struct load_command load_cmd;
4355
4356 lldb::offset_t offset = lc_offset;
4357 for (uint32_t i=0; i<header.ncmds; ++i)
4358 {
4359 const lldb::offset_t cmd_offset = offset;
4360 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4361 break;
4362
4363 switch (load_cmd.cmd)
4364 {
4365 case LC_VERSION_MIN_IPHONEOS:
4366 triple.setOS (llvm::Triple::IOS);
4367 return true;
4368
4369 case LC_VERSION_MIN_MACOSX:
4370 triple.setOS (llvm::Triple::MacOSX);
4371 return true;
4372
4373 default:
4374 break;
4375 }
4376
4377 offset = cmd_offset + load_cmd.cmdsize;
4378 }
4379
Greg Claytona3a6c122014-07-29 18:04:57 +00004380 // Only set the OS to iOS for ARM, we don't want to set it for x86 and x86_64.
4381 // We do this because we now have MacOSX or iOS as the OS value for x86 and
4382 // x86_64 for normal desktop (MacOSX) and simulator (iOS) binaries. And if
4383 // we compare a "x86_64-apple-ios" to a "x86_64-apple-" triple, it will say
4384 // it is compatible (because the OS is unspecified in the second one and will
4385 // match anything in the first
Greg Clayton7ab7f892014-05-29 21:33:45 +00004386 if (header.cputype == CPU_TYPE_ARM || header.cputype == CPU_TYPE_ARM64)
4387 triple.setOS (llvm::Triple::IOS);
Greg Clayton7ab7f892014-05-29 21:33:45 +00004388 }
4389 }
4390 return arch.IsValid();
4391}
4392
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004393bool
Greg Clayton60830262011-02-04 18:53:10 +00004394ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004395{
Greg Claytona1743492012-03-13 23:14:29 +00004396 ModuleSP module_sp(GetModule());
4397 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004398 {
Greg Claytona1743492012-03-13 23:14:29 +00004399 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004400 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00004401 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004402 }
4403 return false;
4404}
4405
4406
4407uint32_t
4408ObjectFileMachO::GetDependentModules (FileSpecList& files)
4409{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004410 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00004411 ModuleSP module_sp(GetModule());
4412 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004413 {
Greg Claytona1743492012-03-13 23:14:29 +00004414 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4415 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004416 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004417 const bool resolve_path = false; // Don't resolve the dependent file paths since they may not reside on this system
Greg Claytona1743492012-03-13 23:14:29 +00004418 uint32_t i;
4419 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004420 {
Greg Claytona1743492012-03-13 23:14:29 +00004421 const uint32_t cmd_offset = offset;
4422 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4423 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004424
Greg Claytona1743492012-03-13 23:14:29 +00004425 switch (load_cmd.cmd)
4426 {
Charles Davis510938e2013-08-27 05:04:57 +00004427 case LC_LOAD_DYLIB:
4428 case LC_LOAD_WEAK_DYLIB:
4429 case LC_REEXPORT_DYLIB:
4430 case LC_LOAD_DYLINKER:
4431 case LC_LOADFVMLIB:
4432 case LC_LOAD_UPWARD_DYLIB:
Greg Claytona1743492012-03-13 23:14:29 +00004433 {
4434 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
4435 const char *path = m_data.PeekCStr(name_offset);
4436 // Skip any path that starts with '@' since these are usually:
4437 // @executable_path/.../file
4438 // @rpath/.../file
4439 if (path && path[0] != '@')
4440 {
4441 FileSpec file_spec(path, resolve_path);
4442 if (files.AppendIfUnique(file_spec))
4443 count++;
4444 }
4445 }
4446 break;
4447
4448 default:
4449 break;
4450 }
4451 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004452 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004453 }
4454 return count;
4455}
4456
Jim Ingham672e6f52011-03-07 23:44:08 +00004457lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00004458ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00004459{
4460 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
4461 // is initialized to an invalid address, so we can just return that.
4462 // 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 +00004463
Jim Ingham672e6f52011-03-07 23:44:08 +00004464 if (!IsExecutable() || m_entry_point_address.IsValid())
4465 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004466
4467 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00004468 // /usr/include/mach-o.h, but it is basically:
4469 //
4470 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
4471 // uint32_t count - this is the count of longs in the thread state data
4472 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
4473 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00004474 //
Jim Ingham672e6f52011-03-07 23:44:08 +00004475 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
4476 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
4477 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
4478 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
4479 //
4480 // For now we hard-code the offsets and flavors we need:
4481 //
4482 //
4483
Greg Claytona1743492012-03-13 23:14:29 +00004484 ModuleSP module_sp(GetModule());
4485 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00004486 {
Greg Claytona1743492012-03-13 23:14:29 +00004487 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4488 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004489 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004490 uint32_t i;
4491 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
4492 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004493
Greg Claytona1743492012-03-13 23:14:29 +00004494 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00004495 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004496 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004497 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4498 break;
4499
4500 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00004501 {
Charles Davis510938e2013-08-27 05:04:57 +00004502 case LC_UNIXTHREAD:
4503 case LC_THREAD:
Jim Ingham672e6f52011-03-07 23:44:08 +00004504 {
Greg Claytona1743492012-03-13 23:14:29 +00004505 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00004506 {
Greg Claytona1743492012-03-13 23:14:29 +00004507 uint32_t flavor = m_data.GetU32(&offset);
4508 uint32_t count = m_data.GetU32(&offset);
4509 if (count == 0)
4510 {
4511 // We've gotten off somehow, log and exit;
4512 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00004513 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004514
Greg Claytona1743492012-03-13 23:14:29 +00004515 switch (m_header.cputype)
4516 {
Charles Davis510938e2013-08-27 05:04:57 +00004517 case llvm::MachO::CPU_TYPE_ARM:
Greg Claytona1743492012-03-13 23:14:29 +00004518 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
4519 {
4520 offset += 60; // This is the offset of pc in the GPR thread state data structure.
4521 start_address = m_data.GetU32(&offset);
4522 done = true;
4523 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004524 break;
Jason Molendaa3329782014-03-29 18:54:20 +00004525 case llvm::MachO::CPU_TYPE_ARM64:
4526 if (flavor == 6) // ARM_THREAD_STATE64 from mach/arm/thread_status.h
4527 {
4528 offset += 256; // This is the offset of pc in the GPR thread state data structure.
4529 start_address = m_data.GetU64(&offset);
4530 done = true;
4531 }
4532 break;
Charles Davis510938e2013-08-27 05:04:57 +00004533 case llvm::MachO::CPU_TYPE_I386:
Greg Claytona1743492012-03-13 23:14:29 +00004534 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
4535 {
4536 offset += 40; // This is the offset of eip in the GPR thread state data structure.
4537 start_address = m_data.GetU32(&offset);
4538 done = true;
4539 }
4540 break;
Charles Davis510938e2013-08-27 05:04:57 +00004541 case llvm::MachO::CPU_TYPE_X86_64:
Greg Claytona1743492012-03-13 23:14:29 +00004542 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
4543 {
4544 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
4545 start_address = m_data.GetU64(&offset);
4546 done = true;
4547 }
4548 break;
4549 default:
4550 return m_entry_point_address;
4551 }
4552 // Haven't found the GPR flavor yet, skip over the data for this flavor:
4553 if (done)
4554 break;
4555 offset += count * 4;
4556 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004557 }
Greg Claytona1743492012-03-13 23:14:29 +00004558 break;
Charles Davis510938e2013-08-27 05:04:57 +00004559 case LC_MAIN:
Sean Callanan226b70c2012-03-08 02:39:03 +00004560 {
Greg Claytona1743492012-03-13 23:14:29 +00004561 ConstString text_segment_name ("__TEXT");
4562 uint64_t entryoffset = m_data.GetU64(&offset);
4563 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
4564 if (text_segment_sp)
4565 {
4566 done = true;
4567 start_address = text_segment_sp->GetFileAddress() + entryoffset;
4568 }
Sean Callanan226b70c2012-03-08 02:39:03 +00004569 }
Greg Claytona1743492012-03-13 23:14:29 +00004570
4571 default:
4572 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00004573 }
Greg Claytona1743492012-03-13 23:14:29 +00004574 if (done)
4575 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00004576
Greg Claytona1743492012-03-13 23:14:29 +00004577 // Go to the next load command:
4578 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00004579 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004580
Greg Claytona1743492012-03-13 23:14:29 +00004581 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00004582 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004583 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00004584 // of this ObjectFile:
4585 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00004586 {
Greg Claytona1743492012-03-13 23:14:29 +00004587 m_entry_point_address.Clear();
4588 }
4589 }
4590 else
4591 {
4592 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
4593 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00004594
Greg Claytona1743492012-03-13 23:14:29 +00004595 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00004596
Greg Claytona1743492012-03-13 23:14:29 +00004597 if (module_sp)
4598 {
4599 SymbolContextList contexts;
4600 SymbolContext context;
4601 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
4602 {
4603 if (contexts.GetContextAtIndex(0, context))
4604 m_entry_point_address = context.symbol->GetAddress();
4605 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004606 }
4607 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004608 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004609
Jim Ingham672e6f52011-03-07 23:44:08 +00004610 return m_entry_point_address;
4611
4612}
4613
Greg Claytonc9660542012-02-05 02:38:54 +00004614lldb_private::Address
4615ObjectFileMachO::GetHeaderAddress ()
4616{
4617 lldb_private::Address header_addr;
4618 SectionList *section_list = GetSectionList();
4619 if (section_list)
4620 {
4621 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
4622 if (text_segment_sp)
4623 {
Greg Claytone72dfb32012-02-24 01:59:29 +00004624 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00004625 header_addr.SetOffset (0);
4626 }
4627 }
4628 return header_addr;
4629}
4630
Greg Claytonc3776bf2012-02-09 06:16:32 +00004631uint32_t
4632ObjectFileMachO::GetNumThreadContexts ()
4633{
Greg Claytona1743492012-03-13 23:14:29 +00004634 ModuleSP module_sp(GetModule());
4635 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004636 {
Greg Claytona1743492012-03-13 23:14:29 +00004637 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4638 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004639 {
Greg Claytona1743492012-03-13 23:14:29 +00004640 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00004641 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004642 FileRangeArray::Entry file_range;
4643 thread_command thread_cmd;
4644 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004645 {
Greg Claytona1743492012-03-13 23:14:29 +00004646 const uint32_t cmd_offset = offset;
4647 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
4648 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004649
Charles Davis510938e2013-08-27 05:04:57 +00004650 if (thread_cmd.cmd == LC_THREAD)
Greg Claytona1743492012-03-13 23:14:29 +00004651 {
4652 file_range.SetRangeBase (offset);
4653 file_range.SetByteSize (thread_cmd.cmdsize - 8);
4654 m_thread_context_offsets.Append (file_range);
4655 }
4656 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00004657 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004658 }
4659 }
4660 return m_thread_context_offsets.GetSize();
4661}
4662
4663lldb::RegisterContextSP
4664ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
4665{
Greg Claytonc3776bf2012-02-09 06:16:32 +00004666 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00004667
Greg Claytona1743492012-03-13 23:14:29 +00004668 ModuleSP module_sp(GetModule());
4669 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004670 {
Greg Claytona1743492012-03-13 23:14:29 +00004671 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4672 if (!m_thread_context_offsets_valid)
4673 GetNumThreadContexts ();
4674
4675 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00004676 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00004677 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004678
4679 DataExtractor data (m_data,
4680 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00004681 thread_context_file_range->GetByteSize());
4682
4683 switch (m_header.cputype)
4684 {
Jason Molendaa3329782014-03-29 18:54:20 +00004685 case llvm::MachO::CPU_TYPE_ARM64:
4686 reg_ctx_sp.reset (new RegisterContextDarwin_arm64_Mach (thread, data));
4687 break;
4688
Charles Davis510938e2013-08-27 05:04:57 +00004689 case llvm::MachO::CPU_TYPE_ARM:
Jim Ingham28eb5712012-10-12 17:34:26 +00004690 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
4691 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004692
Charles Davis510938e2013-08-27 05:04:57 +00004693 case llvm::MachO::CPU_TYPE_I386:
Jim Ingham28eb5712012-10-12 17:34:26 +00004694 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
4695 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004696
Charles Davis510938e2013-08-27 05:04:57 +00004697 case llvm::MachO::CPU_TYPE_X86_64:
Jim Ingham28eb5712012-10-12 17:34:26 +00004698 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
4699 break;
4700 }
Greg Claytona1743492012-03-13 23:14:29 +00004701 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004702 }
4703 return reg_ctx_sp;
4704}
4705
Greg Claytonc9660542012-02-05 02:38:54 +00004706
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004707ObjectFile::Type
4708ObjectFileMachO::CalculateType()
4709{
4710 switch (m_header.filetype)
4711 {
Charles Davis510938e2013-08-27 05:04:57 +00004712 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004713 if (GetAddressByteSize () == 4)
4714 {
4715 // 32 bit kexts are just object files, but they do have a valid
4716 // UUID load command.
4717 UUID uuid;
4718 if (GetUUID(&uuid))
4719 {
4720 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004721 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004722 // "OSKextGetCurrentIdentifier" as this is required of kexts
4723 if (m_strata == eStrataInvalid)
4724 m_strata = eStrataKernel;
4725 return eTypeSharedLibrary;
4726 }
4727 }
4728 return eTypeObjectFile;
4729
Charles Davis510938e2013-08-27 05:04:57 +00004730 case MH_EXECUTE: return eTypeExecutable; // 0x2u
4731 case MH_FVMLIB: return eTypeSharedLibrary; // 0x3u
4732 case MH_CORE: return eTypeCoreFile; // 0x4u
4733 case MH_PRELOAD: return eTypeSharedLibrary; // 0x5u
4734 case MH_DYLIB: return eTypeSharedLibrary; // 0x6u
4735 case MH_DYLINKER: return eTypeDynamicLinker; // 0x7u
4736 case MH_BUNDLE: return eTypeSharedLibrary; // 0x8u
4737 case MH_DYLIB_STUB: return eTypeStubLibrary; // 0x9u
4738 case MH_DSYM: return eTypeDebugInfo; // 0xAu
4739 case MH_KEXT_BUNDLE: return eTypeSharedLibrary; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004740 default:
4741 break;
4742 }
4743 return eTypeUnknown;
4744}
4745
4746ObjectFile::Strata
4747ObjectFileMachO::CalculateStrata()
4748{
4749 switch (m_header.filetype)
4750 {
Charles Davis510938e2013-08-27 05:04:57 +00004751 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004752 {
4753 // 32 bit kexts are just object files, but they do have a valid
4754 // UUID load command.
4755 UUID uuid;
4756 if (GetUUID(&uuid))
4757 {
4758 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004759 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004760 // "OSKextGetCurrentIdentifier" as this is required of kexts
4761 if (m_type == eTypeInvalid)
4762 m_type = eTypeSharedLibrary;
4763
4764 return eStrataKernel;
4765 }
4766 }
4767 return eStrataUnknown;
4768
Charles Davis510938e2013-08-27 05:04:57 +00004769 case MH_EXECUTE: // 0x2u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004770 // Check for the MH_DYLDLINK bit in the flags
Charles Davis510938e2013-08-27 05:04:57 +00004771 if (m_header.flags & MH_DYLDLINK)
Sean Callanan49bce8e2012-02-10 20:22:35 +00004772 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004773 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00004774 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004775 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00004776 {
4777 SectionList *section_list = GetSectionList();
4778 if (section_list)
4779 {
4780 static ConstString g_kld_section_name ("__KLD");
4781 if (section_list->FindSectionByName(g_kld_section_name))
4782 return eStrataKernel;
4783 }
4784 }
4785 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004786
Charles Davis510938e2013-08-27 05:04:57 +00004787 case MH_FVMLIB: return eStrataUser; // 0x3u
4788 case MH_CORE: return eStrataUnknown; // 0x4u
4789 case MH_PRELOAD: return eStrataRawImage; // 0x5u
4790 case MH_DYLIB: return eStrataUser; // 0x6u
4791 case MH_DYLINKER: return eStrataUser; // 0x7u
4792 case MH_BUNDLE: return eStrataUser; // 0x8u
4793 case MH_DYLIB_STUB: return eStrataUser; // 0x9u
4794 case MH_DSYM: return eStrataUnknown; // 0xAu
4795 case MH_KEXT_BUNDLE: return eStrataKernel; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004796 default:
4797 break;
4798 }
4799 return eStrataUnknown;
4800}
4801
4802
Greg Claytonc2ff9312012-02-22 19:41:02 +00004803uint32_t
4804ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4805{
Greg Claytona1743492012-03-13 23:14:29 +00004806 ModuleSP module_sp(GetModule());
4807 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004808 {
Greg Claytona1743492012-03-13 23:14:29 +00004809 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4810 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004811 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004812 uint32_t version_cmd = 0;
4813 uint64_t version = 0;
4814 uint32_t i;
4815 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004816 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004817 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004818 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4819 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004820
Charles Davis510938e2013-08-27 05:04:57 +00004821 if (load_cmd.cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004822 {
Greg Claytona1743492012-03-13 23:14:29 +00004823 if (version_cmd == 0)
4824 {
4825 version_cmd = load_cmd.cmd;
4826 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4827 break;
4828 version = load_cmd.dylib.current_version;
4829 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004830 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00004831 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00004832 }
Greg Claytona1743492012-03-13 23:14:29 +00004833 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004834 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004835
Charles Davis510938e2013-08-27 05:04:57 +00004836 if (version_cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004837 {
Greg Claytona1743492012-03-13 23:14:29 +00004838 if (versions != NULL && num_versions > 0)
4839 {
4840 if (num_versions > 0)
4841 versions[0] = (version & 0xFFFF0000ull) >> 16;
4842 if (num_versions > 1)
4843 versions[1] = (version & 0x0000FF00ull) >> 8;
4844 if (num_versions > 2)
4845 versions[2] = (version & 0x000000FFull);
4846 // Fill in an remaining version numbers with invalid values
4847 for (i=3; i<num_versions; ++i)
4848 versions[i] = UINT32_MAX;
4849 }
4850 // The LC_ID_DYLIB load command has a version with 3 version numbers
4851 // in it, so always return 3
4852 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004853 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00004854 }
4855 return false;
4856}
4857
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004858bool
Greg Clayton514487e2011-02-15 21:59:32 +00004859ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004860{
Greg Claytona1743492012-03-13 23:14:29 +00004861 ModuleSP module_sp(GetModule());
4862 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00004863 {
Greg Claytona1743492012-03-13 23:14:29 +00004864 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton7ab7f892014-05-29 21:33:45 +00004865 return GetArchitecture (m_header, m_data, MachHeaderSizeFromMagic(m_header.magic), arch);
Greg Clayton593577a2011-09-21 03:57:31 +00004866 }
Greg Claytona1743492012-03-13 23:14:29 +00004867 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004868}
4869
4870
Jason Molenda0e0954c2013-04-16 06:24:42 +00004871UUID
4872ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4873{
4874 UUID uuid;
4875 if (process)
4876 {
4877 addr_t all_image_infos = process->GetImageInfoAddress();
4878
4879 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4880 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4881 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4882 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4883
4884 Error err;
4885 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00004886 if (version_or_magic != static_cast<uint32_t>(-1)
Charles Davis510938e2013-08-27 05:04:57 +00004887 && version_or_magic != MH_MAGIC
4888 && version_or_magic != MH_CIGAM
4889 && version_or_magic != MH_MAGIC_64
4890 && version_or_magic != MH_CIGAM_64
Jason Molenda0e0954c2013-04-16 06:24:42 +00004891 && version_or_magic >= 13)
4892 {
4893 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4894 int wordsize = process->GetAddressByteSize();
4895 if (wordsize == 8)
4896 {
4897 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4898 }
4899 if (wordsize == 4)
4900 {
4901 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4902 }
4903 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4904 {
4905 uuid_t shared_cache_uuid;
4906 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4907 {
4908 uuid.SetBytes (shared_cache_uuid);
4909 }
4910 }
4911 }
4912 }
4913 return uuid;
4914}
4915
4916UUID
4917ObjectFileMachO::GetLLDBSharedCacheUUID ()
4918{
4919 UUID uuid;
Todd Fiala013434e2014-07-09 01:29:05 +00004920#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molenda0e0954c2013-04-16 06:24:42 +00004921 uint8_t *(*dyld_get_all_image_infos)(void);
4922 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4923 if (dyld_get_all_image_infos)
4924 {
4925 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4926 if (dyld_all_image_infos_address)
4927 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00004928 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4929 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00004930 {
Jason Molendaa3329782014-03-29 18:54:20 +00004931 uuid_t *sharedCacheUUID_address = 0;
4932 int wordsize = sizeof (uint8_t *);
4933 if (wordsize == 8)
4934 {
4935 sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 160); // sharedCacheUUID <mach-o/dyld_images.h>
4936 }
4937 else
4938 {
4939 sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84); // sharedCacheUUID <mach-o/dyld_images.h>
4940 }
Jason Molenda0e0954c2013-04-16 06:24:42 +00004941 uuid.SetBytes (sharedCacheUUID_address);
4942 }
4943 }
4944 }
4945#endif
4946 return uuid;
4947}
4948
Greg Clayton9b234982013-10-24 22:54:08 +00004949uint32_t
4950ObjectFileMachO::GetMinimumOSVersion (uint32_t *versions, uint32_t num_versions)
4951{
4952 if (m_min_os_versions.empty())
4953 {
4954 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
4955 bool success = false;
4956 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
4957 {
4958 const lldb::offset_t load_cmd_offset = offset;
4959
4960 version_min_command lc;
4961 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
4962 break;
4963 if (lc.cmd == LC_VERSION_MIN_MACOSX || lc.cmd == LC_VERSION_MIN_IPHONEOS)
4964 {
4965 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
4966 {
4967 const uint32_t xxxx = lc.version >> 16;
4968 const uint32_t yy = (lc.version >> 8) & 0xffu;
4969 const uint32_t zz = lc.version & 0xffu;
4970 if (xxxx)
4971 {
4972 m_min_os_versions.push_back(xxxx);
4973 if (yy)
4974 {
4975 m_min_os_versions.push_back(yy);
4976 if (zz)
4977 m_min_os_versions.push_back(zz);
4978 }
4979 }
4980 success = true;
4981 }
4982 }
4983 offset = load_cmd_offset + lc.cmdsize;
4984 }
4985
4986 if (success == false)
4987 {
4988 // Push an invalid value so we don't keep trying to
4989 m_min_os_versions.push_back(UINT32_MAX);
4990 }
4991 }
4992
4993 if (m_min_os_versions.size() > 1 || m_min_os_versions[0] != UINT32_MAX)
4994 {
4995 if (versions != NULL && num_versions > 0)
4996 {
4997 for (size_t i=0; i<num_versions; ++i)
4998 {
4999 if (i < m_min_os_versions.size())
5000 versions[i] = m_min_os_versions[i];
5001 else
5002 versions[i] = 0;
5003 }
5004 }
5005 return m_min_os_versions.size();
5006 }
5007 // Call the superclasses version that will empty out the data
5008 return ObjectFile::GetMinimumOSVersion (versions, num_versions);
5009}
5010
5011uint32_t
5012ObjectFileMachO::GetSDKVersion(uint32_t *versions, uint32_t num_versions)
5013{
5014 if (m_sdk_versions.empty())
5015 {
5016 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5017 bool success = false;
5018 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
5019 {
5020 const lldb::offset_t load_cmd_offset = offset;
5021
5022 version_min_command lc;
5023 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
5024 break;
5025 if (lc.cmd == LC_VERSION_MIN_MACOSX || lc.cmd == LC_VERSION_MIN_IPHONEOS)
5026 {
5027 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
5028 {
Todd Fialaebecb382014-09-04 19:31:52 +00005029 const uint32_t xxxx = lc.sdk >> 16;
5030 const uint32_t yy = (lc.sdk >> 8) & 0xffu;
5031 const uint32_t zz = lc.sdk & 0xffu;
Greg Clayton9b234982013-10-24 22:54:08 +00005032 if (xxxx)
5033 {
5034 m_sdk_versions.push_back(xxxx);
5035 if (yy)
5036 {
5037 m_sdk_versions.push_back(yy);
5038 if (zz)
5039 m_sdk_versions.push_back(zz);
5040 }
5041 }
5042 success = true;
5043 }
5044 }
5045 offset = load_cmd_offset + lc.cmdsize;
5046 }
5047
5048 if (success == false)
5049 {
5050 // Push an invalid value so we don't keep trying to
5051 m_sdk_versions.push_back(UINT32_MAX);
5052 }
5053 }
5054
5055 if (m_sdk_versions.size() > 1 || m_sdk_versions[0] != UINT32_MAX)
5056 {
5057 if (versions != NULL && num_versions > 0)
5058 {
5059 for (size_t i=0; i<num_versions; ++i)
5060 {
5061 if (i < m_sdk_versions.size())
5062 versions[i] = m_sdk_versions[i];
5063 else
5064 versions[i] = 0;
5065 }
5066 }
5067 return m_sdk_versions.size();
5068 }
5069 // Call the superclasses version that will empty out the data
5070 return ObjectFile::GetSDKVersion (versions, num_versions);
5071}
5072
Jason Molenda0e0954c2013-04-16 06:24:42 +00005073
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005074//------------------------------------------------------------------
5075// PluginInterface protocol
5076//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00005077lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005078ObjectFileMachO::GetPluginName()
5079{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005080 return GetPluginNameStatic();
5081}
5082
5083uint32_t
5084ObjectFileMachO::GetPluginVersion()
5085{
5086 return 1;
5087}
5088
Greg Clayton7524e092014-02-06 20:10:16 +00005089
5090bool
Greg Clayton751caf62014-02-07 22:54:47 +00005091ObjectFileMachO::SetLoadAddress (Target &target,
5092 lldb::addr_t value,
5093 bool value_is_offset)
Greg Clayton7524e092014-02-06 20:10:16 +00005094{
5095 bool changed = false;
5096 ModuleSP module_sp = GetModule();
5097 if (module_sp)
5098 {
5099 size_t num_loaded_sections = 0;
5100 SectionList *section_list = GetSectionList ();
5101 if (section_list)
5102 {
5103 lldb::addr_t mach_base_file_addr = LLDB_INVALID_ADDRESS;
5104 const size_t num_sections = section_list->GetSize();
5105
Greg Clayton751caf62014-02-07 22:54:47 +00005106 const bool is_memory_image = (bool)m_process_wp.lock();
5107 const Strata strata = GetStrata();
5108 static ConstString g_linkedit_segname ("__LINKEDIT");
5109 if (value_is_offset)
Greg Clayton7524e092014-02-06 20:10:16 +00005110 {
Greg Clayton751caf62014-02-07 22:54:47 +00005111 // "value" is an offset to apply to each top level segment
Greg Clayton7524e092014-02-06 20:10:16 +00005112 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5113 {
5114 // Iterate through the object file sections to find all
5115 // of the sections that size on disk (to avoid __PAGEZERO)
5116 // and load them
5117 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Greg Clayton751caf62014-02-07 22:54:47 +00005118 if (section_sp &&
5119 section_sp->GetFileSize() > 0 &&
5120 section_sp->IsThreadSpecific() == false &&
5121 module_sp.get() == section_sp->GetModule().get())
Greg Clayton7524e092014-02-06 20:10:16 +00005122 {
Greg Clayton751caf62014-02-07 22:54:47 +00005123 // Ignore __LINKEDIT and __DWARF segments
5124 if (section_sp->GetName() == g_linkedit_segname)
5125 {
5126 // Only map __LINKEDIT if we have an in memory image and this isn't
5127 // a kernel binary like a kext or mach_kernel.
5128 if (is_memory_image == false || strata == eStrataKernel)
5129 continue;
5130 }
5131 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
Greg Clayton7524e092014-02-06 20:10:16 +00005132 ++num_loaded_sections;
5133 }
5134 }
5135 }
Greg Clayton751caf62014-02-07 22:54:47 +00005136 else
5137 {
5138 // "value" is the new base address of the mach_header, adjust each
5139 // section accordingly
5140
5141 // First find the address of the mach header which is the first non-zero
5142 // file sized section whose file offset is zero as this will be subtracted
5143 // from each other valid section's vmaddr and then get "base_addr" added to
5144 // it when loading the module in the target
5145 for (size_t sect_idx = 0;
5146 sect_idx < num_sections && mach_base_file_addr == LLDB_INVALID_ADDRESS;
5147 ++sect_idx)
5148 {
5149 // Iterate through the object file sections to find all
5150 // of the sections that size on disk (to avoid __PAGEZERO)
5151 // and load them
5152 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
5153 if (section &&
5154 section->GetFileSize() > 0 &&
5155 section->GetFileOffset() == 0 &&
5156 section->IsThreadSpecific() == false &&
5157 module_sp.get() == section->GetModule().get())
5158 {
5159 // Ignore __LINKEDIT and __DWARF segments
5160 if (section->GetName() == g_linkedit_segname)
5161 {
5162 // Only map __LINKEDIT if we have an in memory image and this isn't
5163 // a kernel binary like a kext or mach_kernel.
5164 if (is_memory_image == false || strata == eStrataKernel)
5165 continue;
5166 }
5167 mach_base_file_addr = section->GetFileAddress();
5168 }
5169 }
5170
5171 if (mach_base_file_addr != LLDB_INVALID_ADDRESS)
5172 {
5173 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5174 {
5175 // Iterate through the object file sections to find all
5176 // of the sections that size on disk (to avoid __PAGEZERO)
5177 // and load them
5178 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
5179 if (section_sp &&
5180 section_sp->GetFileSize() > 0 &&
5181 section_sp->IsThreadSpecific() == false &&
5182 module_sp.get() == section_sp->GetModule().get())
5183 {
5184 // Ignore __LINKEDIT and __DWARF segments
5185 if (section_sp->GetName() == g_linkedit_segname)
5186 {
5187 // Only map __LINKEDIT if we have an in memory image and this isn't
5188 // a kernel binary like a kext or mach_kernel.
5189 if (is_memory_image == false || strata == eStrataKernel)
5190 continue;
5191 }
5192 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() - mach_base_file_addr + value))
5193 ++num_loaded_sections;
5194 }
5195 }
5196 }
5197 }
Greg Clayton7524e092014-02-06 20:10:16 +00005198 }
5199 changed = num_loaded_sections > 0;
5200 return num_loaded_sections > 0;
5201 }
5202 return changed;
5203}
5204
Greg Claytona2715cf2014-06-13 00:54:12 +00005205bool
5206ObjectFileMachO::SaveCore (const lldb::ProcessSP &process_sp,
5207 const FileSpec &outfile,
5208 Error &error)
5209{
5210 if (process_sp)
5211 {
5212 Target &target = process_sp->GetTarget();
5213 const ArchSpec target_arch = target.GetArchitecture();
5214 const llvm::Triple &target_triple = target_arch.GetTriple();
5215 if (target_triple.getVendor() == llvm::Triple::Apple &&
5216 (target_triple.getOS() == llvm::Triple::MacOSX ||
5217 target_triple.getOS() == llvm::Triple::IOS))
5218 {
5219 bool make_core = false;
5220 switch (target_arch.GetMachine())
5221 {
5222 case llvm::Triple::arm:
5223 case llvm::Triple::x86:
5224 case llvm::Triple::x86_64:
5225 make_core = true;
5226 break;
5227 default:
5228 error.SetErrorStringWithFormat ("unsupported core architecture: %s", target_triple.str().c_str());
5229 break;
5230 }
5231
5232 if (make_core)
5233 {
5234 std::vector<segment_command_64> segment_load_commands;
Saleem Abdulrasool3924d752014-06-13 03:30:39 +00005235// uint32_t range_info_idx = 0;
Greg Claytona2715cf2014-06-13 00:54:12 +00005236 MemoryRegionInfo range_info;
5237 Error range_error = process_sp->GetMemoryRegionInfo(0, range_info);
5238 if (range_error.Success())
5239 {
5240 while (range_info.GetRange().GetRangeBase() != LLDB_INVALID_ADDRESS)
5241 {
5242 const addr_t addr = range_info.GetRange().GetRangeBase();
5243 const addr_t size = range_info.GetRange().GetByteSize();
5244
5245 if (size == 0)
5246 break;
5247
5248 // Calculate correct protections
5249 uint32_t prot = 0;
5250 if (range_info.GetReadable() == MemoryRegionInfo::eYes)
5251 prot |= VM_PROT_READ;
5252 if (range_info.GetWritable() == MemoryRegionInfo::eYes)
5253 prot |= VM_PROT_WRITE;
5254 if (range_info.GetExecutable() == MemoryRegionInfo::eYes)
5255 prot |= VM_PROT_EXECUTE;
5256
5257// printf ("[%3u] [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") %c%c%c\n",
5258// range_info_idx,
5259// addr,
5260// size,
5261// (prot & VM_PROT_READ ) ? 'r' : '-',
5262// (prot & VM_PROT_WRITE ) ? 'w' : '-',
5263// (prot & VM_PROT_EXECUTE) ? 'x' : '-');
5264
5265 if (prot != 0)
5266 {
5267 segment_command_64 segment = {
5268 LC_SEGMENT_64, // uint32_t cmd;
5269 sizeof(segment), // uint32_t cmdsize;
5270 {0}, // char segname[16];
5271 addr, // uint64_t vmaddr;
5272 size, // uint64_t vmsize;
5273 0, // uint64_t fileoff;
5274 size, // uint64_t filesize;
5275 prot, // uint32_t maxprot;
5276 prot, // uint32_t initprot;
5277 0, // uint32_t nsects;
5278 0 }; // uint32_t flags;
5279 segment_load_commands.push_back(segment);
5280 }
5281 else
5282 {
5283 // No protections and a size of 1 used to be returned from old
5284 // debugservers when we asked about a region that was past the
5285 // last memory region and it indicates the end...
5286 if (size == 1)
5287 break;
5288 }
5289
5290 range_error = process_sp->GetMemoryRegionInfo(range_info.GetRange().GetRangeEnd(), range_info);
5291 if (range_error.Fail())
5292 break;
5293 }
5294
5295 const uint32_t addr_byte_size = target_arch.GetAddressByteSize();
5296 const ByteOrder byte_order = target_arch.GetByteOrder();
5297 StreamString buffer (Stream::eBinary,
5298 addr_byte_size,
5299 byte_order);
5300
5301 mach_header_64 mach_header;
5302 mach_header.magic = MH_MAGIC_64;
5303 mach_header.cputype = target_arch.GetMachOCPUType();
5304 mach_header.cpusubtype = target_arch.GetMachOCPUSubType();
5305 mach_header.filetype = MH_CORE;
5306 mach_header.ncmds = segment_load_commands.size();
5307 mach_header.flags = 0;
5308 mach_header.reserved = 0;
5309 ThreadList &thread_list = process_sp->GetThreadList();
5310 const uint32_t num_threads = thread_list.GetSize();
5311
5312 // Make an array of LC_THREAD data items. Each one contains
5313 // the contents of the LC_THREAD load command. The data doesn't
5314 // contain the load command + load command size, we will
5315 // add the load command and load command size as we emit the data.
5316 std::vector<StreamString> LC_THREAD_datas(num_threads);
5317 for (auto &LC_THREAD_data : LC_THREAD_datas)
5318 {
5319 LC_THREAD_data.GetFlags().Set(Stream::eBinary);
5320 LC_THREAD_data.SetAddressByteSize(addr_byte_size);
5321 LC_THREAD_data.SetByteOrder(byte_order);
5322 }
5323 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx)
5324 {
5325 ThreadSP thread_sp (thread_list.GetThreadAtIndex(thread_idx));
5326 if (thread_sp)
5327 {
5328 switch (mach_header.cputype)
5329 {
5330 case llvm::MachO::CPU_TYPE_ARM:
5331 //RegisterContextDarwin_arm_Mach::Create_LC_THREAD (thread_sp.get(), thread_load_commands);
5332 break;
5333
5334 case llvm::MachO::CPU_TYPE_I386:
5335 //RegisterContextDarwin_i386_Mach::Create_LC_THREAD (thread_sp.get(), thread_load_commands);
5336 break;
5337
5338 case llvm::MachO::CPU_TYPE_X86_64:
5339 RegisterContextDarwin_x86_64_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5340 break;
5341 }
5342
5343 }
5344 }
5345
5346 // The size of the load command is the size of the segments...
5347 mach_header.sizeofcmds = segment_load_commands.size() * segment_load_commands[0].cmdsize;
5348
5349 // and the size of all LC_THREAD load command
5350 for (const auto &LC_THREAD_data : LC_THREAD_datas)
5351 {
5352 mach_header.sizeofcmds += 8 + LC_THREAD_data.GetSize();
5353 }
5354
5355 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",
5356 mach_header.magic,
5357 mach_header.cputype,
5358 mach_header.cpusubtype,
5359 mach_header.filetype,
5360 mach_header.ncmds,
5361 mach_header.sizeofcmds,
5362 mach_header.flags,
5363 mach_header.reserved);
5364
5365 // Write the mach header
5366 buffer.PutHex32(mach_header.magic);
5367 buffer.PutHex32(mach_header.cputype);
5368 buffer.PutHex32(mach_header.cpusubtype);
5369 buffer.PutHex32(mach_header.filetype);
5370 buffer.PutHex32(mach_header.ncmds);
5371 buffer.PutHex32(mach_header.sizeofcmds);
5372 buffer.PutHex32(mach_header.flags);
5373 buffer.PutHex32(mach_header.reserved);
5374
5375 // Skip the mach header and all load commands and align to the next
5376 // 0x1000 byte boundary
5377 addr_t file_offset = buffer.GetSize() + mach_header.sizeofcmds;
5378 if (file_offset & 0x00000fff)
5379 {
5380 file_offset += 0x00001000ull;
5381 file_offset &= (~0x00001000ull + 1);
5382 }
5383
5384 for (auto &segment : segment_load_commands)
5385 {
5386 segment.fileoff = file_offset;
5387 file_offset += segment.filesize;
5388 }
5389
5390 // Write out all of the LC_THREAD load commands
5391 for (const auto &LC_THREAD_data : LC_THREAD_datas)
5392 {
5393 const size_t LC_THREAD_data_size = LC_THREAD_data.GetSize();
5394 buffer.PutHex32(LC_THREAD);
5395 buffer.PutHex32(8 + LC_THREAD_data_size); // cmd + cmdsize + data
5396 buffer.Write(LC_THREAD_data.GetData(), LC_THREAD_data_size);
5397 }
5398
5399 // Write out all of the segment load commands
5400 for (const auto &segment : segment_load_commands)
5401 {
5402 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",
5403 segment.cmd,
5404 segment.cmdsize,
5405 segment.vmaddr,
5406 segment.vmaddr + segment.vmsize,
5407 segment.fileoff,
5408 segment.filesize,
5409 segment.maxprot,
5410 segment.initprot,
5411 segment.nsects,
5412 segment.flags);
5413
5414 buffer.PutHex32(segment.cmd);
5415 buffer.PutHex32(segment.cmdsize);
5416 buffer.PutRawBytes(segment.segname, sizeof(segment.segname));
5417 buffer.PutHex64(segment.vmaddr);
5418 buffer.PutHex64(segment.vmsize);
5419 buffer.PutHex64(segment.fileoff);
5420 buffer.PutHex64(segment.filesize);
5421 buffer.PutHex32(segment.maxprot);
5422 buffer.PutHex32(segment.initprot);
5423 buffer.PutHex32(segment.nsects);
5424 buffer.PutHex32(segment.flags);
5425 }
5426
5427 File core_file;
5428 std::string core_file_path(outfile.GetPath());
5429 error = core_file.Open(core_file_path.c_str(),
5430 File::eOpenOptionWrite |
5431 File::eOpenOptionTruncate |
5432 File::eOpenOptionCanCreate);
5433 if (error.Success())
5434 {
5435 // Read 1 page at a time
5436 uint8_t bytes[0x1000];
5437 // Write the mach header and load commands out to the core file
5438 size_t bytes_written = buffer.GetString().size();
5439 error = core_file.Write(buffer.GetString().data(), bytes_written);
5440 if (error.Success())
5441 {
5442 // Now write the file data for all memory segments in the process
5443 for (const auto &segment : segment_load_commands)
5444 {
David Majnemerb98a5e02014-07-24 00:24:12 +00005445 if (core_file.SeekFromStart(segment.fileoff) == -1)
Greg Claytona2715cf2014-06-13 00:54:12 +00005446 {
5447 error.SetErrorStringWithFormat("unable to seek to offset 0x%" PRIx64 " in '%s'", segment.fileoff, core_file_path.c_str());
5448 break;
5449 }
5450
Joerg Sonnenberger968697d2014-07-18 11:58:19 +00005451 printf ("Saving data for segment at 0x%" PRIx64 "\n", segment.vmaddr);
Greg Claytona2715cf2014-06-13 00:54:12 +00005452 addr_t bytes_left = segment.vmsize;
5453 addr_t addr = segment.vmaddr;
5454 Error memory_read_error;
5455 while (bytes_left > 0 && error.Success())
5456 {
5457 const size_t bytes_to_read = bytes_left > sizeof(bytes) ? sizeof(bytes) : bytes_left;
5458 const size_t bytes_read = process_sp->ReadMemory(addr, bytes, bytes_to_read, memory_read_error);
5459 if (bytes_read == bytes_to_read)
5460 {
5461 size_t bytes_written = bytes_read;
5462 error = core_file.Write(bytes, bytes_written);
5463 bytes_left -= bytes_read;
5464 addr += bytes_read;
5465 }
5466 else
5467 {
5468 // Some pages within regions are not readable, those
5469 // should be zero filled
5470 memset (bytes, 0, bytes_to_read);
5471 size_t bytes_written = bytes_to_read;
5472 error = core_file.Write(bytes, bytes_written);
5473 bytes_left -= bytes_to_read;
5474 addr += bytes_to_read;
5475 }
5476 }
5477 }
5478 }
5479 }
5480 }
5481 else
5482 {
5483 error.SetErrorString("process doesn't support getting memory region info");
5484 }
5485 }
5486 return true; // This is the right plug to handle saving core files for this process
5487 }
5488 }
5489 return false;
5490}
5491