blob: 682a1c270969f65fd1a064f5e51add476c71026b [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
Jason Molendad20359d2014-11-11 10:59:15 +0000131
Greg Claytona2715cf2014-06-13 00:54:12 +0000132 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 }
Jason Molendad20359d2014-11-11 10:59:15 +0000159
Greg Claytona2715cf2014-06-13 00:54:12 +0000160 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
Jason Molenda22952582014-11-12 01:11:36 +0000169 data.PutHex32 (GPRWordCount);
Greg Claytona2715cf2014-06-13 00:54:12 +0000170 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);
Jason Molenda22952582014-11-12 01:11:36 +0000243 data.PutHex32 (EXCWordCount);
Greg Claytona2715cf2014-06-13 00:54:12 +0000244 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 }
Jason Molendad20359d2014-11-11 10:59:15 +0000360
361 static size_t
362 WriteRegister (RegisterContext *reg_ctx, const char *name, const char *alt_name, size_t reg_byte_size, Stream &data)
363 {
364 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
365 if (reg_info == NULL)
366 reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
367 if (reg_info)
368 {
369 lldb_private::RegisterValue reg_value;
370 if (reg_ctx->ReadRegister(reg_info, reg_value))
371 {
372 if (reg_info->byte_size >= reg_byte_size)
373 data.Write(reg_value.GetBytes(), reg_byte_size);
374 else
375 {
376 data.Write(reg_value.GetBytes(), reg_info->byte_size);
377 for (size_t i=0, n = reg_byte_size - reg_info->byte_size; i<n; ++ i)
378 data.PutChar(0);
379 }
380 return reg_byte_size;
381 }
382 }
383 // Just write zeros if all else fails
384 for (size_t i=0; i<reg_byte_size; ++ i)
385 data.PutChar(0);
386 return reg_byte_size;
387 }
388
389 static bool
390 Create_LC_THREAD (Thread *thread, Stream &data)
391 {
392 RegisterContextSP reg_ctx_sp (thread->GetRegisterContext());
393 if (reg_ctx_sp)
394 {
395 RegisterContext *reg_ctx = reg_ctx_sp.get();
396
397 data.PutHex32 (GPRRegSet); // Flavor
Jason Molenda22952582014-11-12 01:11:36 +0000398 data.PutHex32 (GPRWordCount);
Jason Molendad20359d2014-11-11 10:59:15 +0000399 WriteRegister (reg_ctx, "eax", NULL, 4, data);
400 WriteRegister (reg_ctx, "ebx", NULL, 4, data);
401 WriteRegister (reg_ctx, "ecx", NULL, 4, data);
402 WriteRegister (reg_ctx, "edx", NULL, 4, data);
403 WriteRegister (reg_ctx, "edi", NULL, 4, data);
404 WriteRegister (reg_ctx, "esi", NULL, 4, data);
405 WriteRegister (reg_ctx, "ebp", NULL, 4, data);
406 WriteRegister (reg_ctx, "esp", NULL, 4, data);
407 WriteRegister (reg_ctx, "ss", NULL, 4, data);
408 WriteRegister (reg_ctx, "eflags", NULL, 4, data);
409 WriteRegister (reg_ctx, "eip", NULL, 4, data);
410 WriteRegister (reg_ctx, "cs", NULL, 4, data);
411 WriteRegister (reg_ctx, "ds", NULL, 4, data);
412 WriteRegister (reg_ctx, "es", NULL, 4, data);
413 WriteRegister (reg_ctx, "fs", NULL, 4, data);
414 WriteRegister (reg_ctx, "gs", NULL, 4, data);
415
416 // Write out the EXC registers
417 data.PutHex32 (EXCRegSet);
Jason Molenda22952582014-11-12 01:11:36 +0000418 data.PutHex32 (EXCWordCount);
Jason Molendad20359d2014-11-11 10:59:15 +0000419 WriteRegister (reg_ctx, "trapno", NULL, 4, data);
420 WriteRegister (reg_ctx, "err", NULL, 4, data);
Jason Molenda22952582014-11-12 01:11:36 +0000421 WriteRegister (reg_ctx, "faultvaddr", NULL, 4, data);
Jason Molendad20359d2014-11-11 10:59:15 +0000422 return true;
423 }
424 return false;
425 }
426
Greg Claytonc3776bf2012-02-09 06:16:32 +0000427protected:
428 virtual int
429 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
430 {
431 return 0;
432 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000433
Greg Claytonc3776bf2012-02-09 06:16:32 +0000434 virtual int
435 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
436 {
437 return 0;
438 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000439
Greg Claytonc3776bf2012-02-09 06:16:32 +0000440 virtual int
441 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
442 {
443 return 0;
444 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000445
Greg Claytonc3776bf2012-02-09 06:16:32 +0000446 virtual int
447 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
448 {
449 return 0;
450 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000451
Greg Claytonc3776bf2012-02-09 06:16:32 +0000452 virtual int
453 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
454 {
455 return 0;
456 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000457
Greg Claytonc3776bf2012-02-09 06:16:32 +0000458 virtual int
459 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
460 {
461 return 0;
462 }
463};
464
Jason Molenda4e7511e2013-03-06 23:19:17 +0000465class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Claytonc859e2d2012-02-13 23:10:39 +0000466{
467public:
468 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonc2807462012-10-30 23:57:32 +0000469 RegisterContextDarwin_arm (thread, 0)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000470 {
471 SetRegisterDataFrom_LC_THREAD (data);
472 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000473
Greg Claytonc859e2d2012-02-13 23:10:39 +0000474 virtual void
475 InvalidateAllRegisters ()
476 {
477 // Do nothing... registers are always valid...
478 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000479
Greg Claytonc859e2d2012-02-13 23:10:39 +0000480 void
481 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
482 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000483 lldb::offset_t offset = 0;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000484 SetError (GPRRegSet, Read, -1);
485 SetError (FPURegSet, Read, -1);
486 SetError (EXCRegSet, Read, -1);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000487 bool done = false;
488
489 while (!done)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000490 {
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000491 int flavor = data.GetU32 (&offset);
492 uint32_t count = data.GetU32 (&offset);
Jason Molendaddf91772013-05-14 04:50:47 +0000493 lldb::offset_t next_thread_state = offset + (count * 4);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000494 switch (flavor)
495 {
496 case GPRRegSet:
497 for (uint32_t i=0; i<count; ++i)
Jason Molendaddf91772013-05-14 04:50:47 +0000498 {
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000499 gpr.r[i] = data.GetU32(&offset);
Jason Molendaddf91772013-05-14 04:50:47 +0000500 }
501
502 // Note that gpr.cpsr is also copied by the above loop; this loop technically extends
503 // one element past the end of the gpr.r[] array.
504
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000505 SetError (GPRRegSet, Read, 0);
Jason Molendaddf91772013-05-14 04:50:47 +0000506 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000507 break;
508
509 case FPURegSet:
510 {
Jason Molenda663d2e12013-05-14 03:52:22 +0000511 uint8_t *fpu_reg_buf = (uint8_t*) &fpu.floats.s[0];
512 const int fpu_reg_buf_size = sizeof (fpu.floats);
513 if (data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000514 {
Jason Molenda663d2e12013-05-14 03:52:22 +0000515 offset += fpu_reg_buf_size;
516 fpu.fpscr = data.GetU32(&offset);
517 SetError (FPURegSet, Read, 0);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000518 }
Jason Molenda663d2e12013-05-14 03:52:22 +0000519 else
520 {
521 done = true;
522 }
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000523 }
Jason Molendaddf91772013-05-14 04:50:47 +0000524 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000525 break;
526
527 case EXCRegSet:
Jason Molendaddf91772013-05-14 04:50:47 +0000528 if (count == 3)
529 {
530 exc.exception = data.GetU32(&offset);
531 exc.fsr = data.GetU32(&offset);
532 exc.far = data.GetU32(&offset);
533 SetError (EXCRegSet, Read, 0);
534 }
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000535 done = true;
Jason Molendaddf91772013-05-14 04:50:47 +0000536 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000537 break;
538
539 // Unknown register set flavor, stop trying to parse.
540 default:
541 done = true;
542 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000543 }
544 }
Jason Molenda22952582014-11-12 01:11:36 +0000545
546 static size_t
547 WriteRegister (RegisterContext *reg_ctx, const char *name, const char *alt_name, size_t reg_byte_size, Stream &data)
548 {
549 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
550 if (reg_info == NULL)
551 reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
552 if (reg_info)
553 {
554 lldb_private::RegisterValue reg_value;
555 if (reg_ctx->ReadRegister(reg_info, reg_value))
556 {
557 if (reg_info->byte_size >= reg_byte_size)
558 data.Write(reg_value.GetBytes(), reg_byte_size);
559 else
560 {
561 data.Write(reg_value.GetBytes(), reg_info->byte_size);
562 for (size_t i=0, n = reg_byte_size - reg_info->byte_size; i<n; ++ i)
563 data.PutChar(0);
564 }
565 return reg_byte_size;
566 }
567 }
568 // Just write zeros if all else fails
569 for (size_t i=0; i<reg_byte_size; ++ i)
570 data.PutChar(0);
571 return reg_byte_size;
572 }
573
574 static bool
575 Create_LC_THREAD (Thread *thread, Stream &data)
576 {
577 RegisterContextSP reg_ctx_sp (thread->GetRegisterContext());
578 if (reg_ctx_sp)
579 {
580 RegisterContext *reg_ctx = reg_ctx_sp.get();
581
582 data.PutHex32 (GPRRegSet); // Flavor
583 data.PutHex32 (GPRWordCount);
584 WriteRegister (reg_ctx, "r0", NULL, 4, data);
585 WriteRegister (reg_ctx, "r1", NULL, 4, data);
586 WriteRegister (reg_ctx, "r2", NULL, 4, data);
587 WriteRegister (reg_ctx, "r3", NULL, 4, data);
588 WriteRegister (reg_ctx, "r4", NULL, 4, data);
589 WriteRegister (reg_ctx, "r5", NULL, 4, data);
590 WriteRegister (reg_ctx, "r6", NULL, 4, data);
591 WriteRegister (reg_ctx, "r7", NULL, 4, data);
592 WriteRegister (reg_ctx, "r8", NULL, 4, data);
593 WriteRegister (reg_ctx, "r9", NULL, 4, data);
594 WriteRegister (reg_ctx, "r10", NULL, 4, data);
595 WriteRegister (reg_ctx, "r11", NULL, 4, data);
596 WriteRegister (reg_ctx, "r12", NULL, 4, data);
597 WriteRegister (reg_ctx, "sp", NULL, 4, data);
598 WriteRegister (reg_ctx, "lr", NULL, 4, data);
599 WriteRegister (reg_ctx, "pc", NULL, 4, data);
600 WriteRegister (reg_ctx, "cpsr", NULL, 4, data);
601
602 // Write out the EXC registers
603// data.PutHex32 (EXCRegSet);
604// data.PutHex32 (EXCWordCount);
605// WriteRegister (reg_ctx, "exception", NULL, 4, data);
606// WriteRegister (reg_ctx, "fsr", NULL, 4, data);
607// WriteRegister (reg_ctx, "far", NULL, 4, data);
608 return true;
609 }
610 return false;
611 }
612
Greg Claytonc859e2d2012-02-13 23:10:39 +0000613protected:
614 virtual int
615 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
616 {
Jason Molendaddf91772013-05-14 04:50:47 +0000617 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000618 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000619
Greg Claytonc859e2d2012-02-13 23:10:39 +0000620 virtual int
621 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
622 {
Jason Molendaddf91772013-05-14 04:50:47 +0000623 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000624 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000625
Greg Claytonc859e2d2012-02-13 23:10:39 +0000626 virtual int
627 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
628 {
Jason Molendaddf91772013-05-14 04:50:47 +0000629 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000630 }
Greg Claytonc2807462012-10-30 23:57:32 +0000631
632 virtual int
633 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
634 {
635 return -1;
636 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000637
Greg Claytonc859e2d2012-02-13 23:10:39 +0000638 virtual int
639 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
640 {
641 return 0;
642 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000643
Greg Claytonc859e2d2012-02-13 23:10:39 +0000644 virtual int
645 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
646 {
647 return 0;
648 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000649
Greg Claytonc859e2d2012-02-13 23:10:39 +0000650 virtual int
651 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
652 {
653 return 0;
654 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000655
Greg Claytonc2807462012-10-30 23:57:32 +0000656 virtual int
657 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
658 {
659 return -1;
660 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000661};
662
Jason Molendaa3329782014-03-29 18:54:20 +0000663class RegisterContextDarwin_arm64_Mach : public RegisterContextDarwin_arm64
664{
665public:
666 RegisterContextDarwin_arm64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
667 RegisterContextDarwin_arm64 (thread, 0)
668 {
669 SetRegisterDataFrom_LC_THREAD (data);
670 }
671
672 virtual void
673 InvalidateAllRegisters ()
674 {
675 // Do nothing... registers are always valid...
676 }
677
678 void
679 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
680 {
681 lldb::offset_t offset = 0;
682 SetError (GPRRegSet, Read, -1);
683 SetError (FPURegSet, Read, -1);
684 SetError (EXCRegSet, Read, -1);
685 bool done = false;
686 while (!done)
687 {
688 int flavor = data.GetU32 (&offset);
689 uint32_t count = data.GetU32 (&offset);
690 lldb::offset_t next_thread_state = offset + (count * 4);
691 switch (flavor)
692 {
693 case GPRRegSet:
694 // x0-x29 + fp + lr + sp + pc (== 33 64-bit registers) plus cpsr (1 32-bit register)
695 if (count >= (33 * 2) + 1)
696 {
697 for (uint32_t i=0; i<33; ++i)
698 gpr.x[i] = data.GetU64(&offset);
699 gpr.cpsr = data.GetU32(&offset);
700 SetError (GPRRegSet, Read, 0);
701 }
702 offset = next_thread_state;
703 break;
704 case FPURegSet:
705 {
706 uint8_t *fpu_reg_buf = (uint8_t*) &fpu.v[0];
707 const int fpu_reg_buf_size = sizeof (fpu);
708 if (fpu_reg_buf_size == count
709 && data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
710 {
711 SetError (FPURegSet, Read, 0);
712 }
713 else
714 {
715 done = true;
716 }
717 }
718 offset = next_thread_state;
719 break;
720 case EXCRegSet:
721 if (count == 4)
722 {
723 exc.far = data.GetU64(&offset);
724 exc.esr = data.GetU32(&offset);
725 exc.exception = data.GetU32(&offset);
726 SetError (EXCRegSet, Read, 0);
727 }
728 offset = next_thread_state;
729 break;
730 default:
731 done = true;
732 break;
733 }
734 }
735 }
Jason Molenda22952582014-11-12 01:11:36 +0000736
737 static size_t
738 WriteRegister (RegisterContext *reg_ctx, const char *name, const char *alt_name, size_t reg_byte_size, Stream &data)
739 {
740 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
741 if (reg_info == NULL)
742 reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
743 if (reg_info)
744 {
745 lldb_private::RegisterValue reg_value;
746 if (reg_ctx->ReadRegister(reg_info, reg_value))
747 {
748 if (reg_info->byte_size >= reg_byte_size)
749 data.Write(reg_value.GetBytes(), reg_byte_size);
750 else
751 {
752 data.Write(reg_value.GetBytes(), reg_info->byte_size);
753 for (size_t i=0, n = reg_byte_size - reg_info->byte_size; i<n; ++ i)
754 data.PutChar(0);
755 }
756 return reg_byte_size;
757 }
758 }
759 // Just write zeros if all else fails
760 for (size_t i=0; i<reg_byte_size; ++ i)
761 data.PutChar(0);
762 return reg_byte_size;
763 }
764
765 static bool
766 Create_LC_THREAD (Thread *thread, Stream &data)
767 {
768 RegisterContextSP reg_ctx_sp (thread->GetRegisterContext());
769 if (reg_ctx_sp)
770 {
771 RegisterContext *reg_ctx = reg_ctx_sp.get();
772
773 data.PutHex32 (GPRRegSet); // Flavor
774 data.PutHex32 (GPRWordCount);
775 WriteRegister (reg_ctx, "x0", NULL, 8, data);
776 WriteRegister (reg_ctx, "x1", NULL, 8, data);
777 WriteRegister (reg_ctx, "x2", NULL, 8, data);
778 WriteRegister (reg_ctx, "x3", NULL, 8, data);
779 WriteRegister (reg_ctx, "x4", NULL, 8, data);
780 WriteRegister (reg_ctx, "x5", NULL, 8, data);
781 WriteRegister (reg_ctx, "x6", NULL, 8, data);
782 WriteRegister (reg_ctx, "x7", NULL, 8, data);
783 WriteRegister (reg_ctx, "x8", NULL, 8, data);
784 WriteRegister (reg_ctx, "x9", NULL, 8, data);
785 WriteRegister (reg_ctx, "x10", NULL, 8, data);
786 WriteRegister (reg_ctx, "x11", NULL, 8, data);
787 WriteRegister (reg_ctx, "x12", NULL, 8, data);
788 WriteRegister (reg_ctx, "x13", NULL, 8, data);
789 WriteRegister (reg_ctx, "x14", NULL, 8, data);
790 WriteRegister (reg_ctx, "x15", NULL, 8, data);
791 WriteRegister (reg_ctx, "x16", NULL, 8, data);
792 WriteRegister (reg_ctx, "x17", NULL, 8, data);
793 WriteRegister (reg_ctx, "x18", NULL, 8, data);
794 WriteRegister (reg_ctx, "x19", NULL, 8, data);
795 WriteRegister (reg_ctx, "x20", NULL, 8, data);
796 WriteRegister (reg_ctx, "x21", NULL, 8, data);
797 WriteRegister (reg_ctx, "x22", NULL, 8, data);
798 WriteRegister (reg_ctx, "x23", NULL, 8, data);
799 WriteRegister (reg_ctx, "x24", NULL, 8, data);
800 WriteRegister (reg_ctx, "x25", NULL, 8, data);
801 WriteRegister (reg_ctx, "x26", NULL, 8, data);
802 WriteRegister (reg_ctx, "x27", NULL, 8, data);
803 WriteRegister (reg_ctx, "x28", NULL, 8, data);
804 WriteRegister (reg_ctx, "fp", NULL, 8, data);
805 WriteRegister (reg_ctx, "lr", NULL, 8, data);
806 WriteRegister (reg_ctx, "sp", NULL, 8, data);
807 WriteRegister (reg_ctx, "pc", NULL, 8, data);
808 WriteRegister (reg_ctx, "cpsr", NULL, 4, data);
809
810 // Write out the EXC registers
811// data.PutHex32 (EXCRegSet);
812// data.PutHex32 (EXCWordCount);
813// WriteRegister (reg_ctx, "far", NULL, 8, data);
814// WriteRegister (reg_ctx, "esr", NULL, 4, data);
815// WriteRegister (reg_ctx, "exception", NULL, 4, data);
816 return true;
817 }
818 return false;
819 }
820
Jason Molendaa3329782014-03-29 18:54:20 +0000821protected:
822 virtual int
823 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
824 {
825 return -1;
826 }
827
828 virtual int
829 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
830 {
831 return -1;
832 }
833
834 virtual int
835 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
836 {
837 return -1;
838 }
839
840 virtual int
841 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
842 {
843 return -1;
844 }
845
846 virtual int
847 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
848 {
849 return 0;
850 }
851
852 virtual int
853 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
854 {
855 return 0;
856 }
857
858 virtual int
859 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
860 {
861 return 0;
862 }
863
864 virtual int
865 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
866 {
867 return -1;
868 }
869};
870
Greg Clayton9aae0a12013-05-15 19:52:08 +0000871static uint32_t
872MachHeaderSizeFromMagic(uint32_t magic)
873{
874 switch (magic)
875 {
Charles Davis510938e2013-08-27 05:04:57 +0000876 case MH_MAGIC:
877 case MH_CIGAM:
Greg Clayton9aae0a12013-05-15 19:52:08 +0000878 return sizeof(struct mach_header);
879
Charles Davis510938e2013-08-27 05:04:57 +0000880 case MH_MAGIC_64:
881 case MH_CIGAM_64:
Greg Clayton9aae0a12013-05-15 19:52:08 +0000882 return sizeof(struct mach_header_64);
883 break;
884
885 default:
886 break;
887 }
888 return 0;
889}
890
Greg Claytonded470d2011-03-19 01:12:21 +0000891#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892
893void
894ObjectFileMachO::Initialize()
895{
896 PluginManager::RegisterPlugin (GetPluginNameStatic(),
897 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +0000898 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000899 CreateMemoryInstance,
Greg Claytona2715cf2014-06-13 00:54:12 +0000900 GetModuleSpecifications,
901 SaveCore);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902}
903
904void
905ObjectFileMachO::Terminate()
906{
907 PluginManager::UnregisterPlugin (CreateInstance);
908}
909
910
Greg Clayton57abc5d2013-05-10 21:47:16 +0000911lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000912ObjectFileMachO::GetPluginNameStatic()
913{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000914 static ConstString g_name("mach-o");
915 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000916}
917
918const char *
919ObjectFileMachO::GetPluginDescriptionStatic()
920{
921 return "Mach-o object file reader (32 and 64 bit)";
922}
923
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924ObjectFile *
Greg Clayton5ce9c562013-02-06 17:22:03 +0000925ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
926 DataBufferSP& data_sp,
927 lldb::offset_t data_offset,
928 const FileSpec* file,
929 lldb::offset_t file_offset,
930 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000932 if (!data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933 {
Greg Clayton736888c2015-02-23 23:47:09 +0000934 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
Greg Clayton5ce9c562013-02-06 17:22:03 +0000935 data_offset = 0;
936 }
937
938 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
939 {
940 // Update the data to contain the entire file if it doesn't already
941 if (data_sp->GetByteSize() < length)
942 {
Greg Clayton736888c2015-02-23 23:47:09 +0000943 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
Greg Clayton5ce9c562013-02-06 17:22:03 +0000944 data_offset = 0;
945 }
Greg Clayton7b0992d2013-04-18 22:45:39 +0000946 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 +0000947 if (objfile_ap.get() && objfile_ap->ParseHeader())
948 return objfile_ap.release();
949 }
950 return NULL;
951}
952
Greg Claytonc9660542012-02-05 02:38:54 +0000953ObjectFile *
Jason Molenda4e7511e2013-03-06 23:19:17 +0000954ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
955 DataBufferSP& data_sp,
956 const ProcessSP &process_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000957 lldb::addr_t header_addr)
958{
959 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
960 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000961 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonc9660542012-02-05 02:38:54 +0000962 if (objfile_ap.get() && objfile_ap->ParseHeader())
963 return objfile_ap.release();
964 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000965 return NULL;
Greg Claytonc9660542012-02-05 02:38:54 +0000966}
967
Greg Claytonf4d6de62013-04-24 22:29:28 +0000968size_t
969ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
970 lldb::DataBufferSP& data_sp,
971 lldb::offset_t data_offset,
972 lldb::offset_t file_offset,
973 lldb::offset_t length,
974 lldb_private::ModuleSpecList &specs)
975{
976 const size_t initial_count = specs.GetSize();
977
978 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
979 {
980 DataExtractor data;
981 data.SetData(data_sp);
982 llvm::MachO::mach_header header;
983 if (ParseHeader (data, &data_offset, header))
984 {
Jason Molenda48cd3332014-04-22 04:52:30 +0000985 size_t header_and_load_cmds = header.sizeofcmds + MachHeaderSizeFromMagic(header.magic);
986 if (header_and_load_cmds >= data_sp->GetByteSize())
Greg Claytonf4d6de62013-04-24 22:29:28 +0000987 {
Jason Molenda48cd3332014-04-22 04:52:30 +0000988 data_sp = file.ReadFileContents(file_offset, header_and_load_cmds);
Greg Clayton2540a8a2013-07-12 22:07:46 +0000989 data.SetData(data_sp);
990 data_offset = MachHeaderSizeFromMagic(header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +0000991 }
992 if (data_sp)
993 {
994 ModuleSpec spec;
995 spec.GetFileSpec() = file;
Greg Clayton7ab7f892014-05-29 21:33:45 +0000996 spec.SetObjectOffset(file_offset);
997
998 if (GetArchitecture (header, data, data_offset, spec.GetArchitecture()))
Jason Molendab000e4d2013-08-27 02:22:06 +0000999 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001000 if (spec.GetArchitecture().IsValid())
1001 {
1002 GetUUID (header, data, data_offset, spec.GetUUID());
1003 specs.Append(spec);
1004 }
Greg Claytonf4d6de62013-04-24 22:29:28 +00001005 }
1006 }
1007 }
1008 }
1009 return specs.GetSize() - initial_count;
1010}
1011
1012
Greg Claytonc9660542012-02-05 02:38:54 +00001013
1014const ConstString &
1015ObjectFileMachO::GetSegmentNameTEXT()
1016{
1017 static ConstString g_segment_name_TEXT ("__TEXT");
1018 return g_segment_name_TEXT;
1019}
1020
1021const ConstString &
1022ObjectFileMachO::GetSegmentNameDATA()
1023{
1024 static ConstString g_segment_name_DATA ("__DATA");
1025 return g_segment_name_DATA;
1026}
1027
1028const ConstString &
1029ObjectFileMachO::GetSegmentNameOBJC()
1030{
1031 static ConstString g_segment_name_OBJC ("__OBJC");
1032 return g_segment_name_OBJC;
1033}
1034
1035const ConstString &
1036ObjectFileMachO::GetSegmentNameLINKEDIT()
1037{
1038 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
1039 return g_section_name_LINKEDIT;
1040}
1041
1042const ConstString &
1043ObjectFileMachO::GetSectionNameEHFrame()
1044{
1045 static ConstString g_section_name_eh_frame ("__eh_frame");
1046 return g_section_name_eh_frame;
1047}
1048
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001049bool
Jason Molenda4e7511e2013-03-06 23:19:17 +00001050ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
1051 lldb::addr_t data_offset,
Greg Clayton44435ed2012-01-12 05:25:17 +00001052 lldb::addr_t data_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001053{
Greg Clayton44435ed2012-01-12 05:25:17 +00001054 DataExtractor data;
1055 data.SetData (data_sp, data_offset, data_length);
Greg Claytonc7bece562013-01-25 18:06:21 +00001056 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057 uint32_t magic = data.GetU32(&offset);
1058 return MachHeaderSizeFromMagic(magic) != 0;
1059}
1060
1061
Greg Clayton5ce9c562013-02-06 17:22:03 +00001062ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
1063 DataBufferSP& data_sp,
1064 lldb::offset_t data_offset,
1065 const FileSpec* file,
1066 lldb::offset_t file_offset,
1067 lldb::offset_t length) :
1068 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonc3776bf2012-02-09 06:16:32 +00001069 m_mach_segments(),
1070 m_mach_sections(),
1071 m_entry_point_address(),
1072 m_thread_context_offsets(),
1073 m_thread_context_offsets_valid(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074{
Greg Clayton72b77eb2011-02-04 21:13:05 +00001075 ::memset (&m_header, 0, sizeof(m_header));
1076 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001077}
1078
Greg Claytone72dfb32012-02-24 01:59:29 +00001079ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +00001080 lldb::DataBufferSP& header_data_sp,
1081 const lldb::ProcessSP &process_sp,
1082 lldb::addr_t header_addr) :
Greg Claytone72dfb32012-02-24 01:59:29 +00001083 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonc3776bf2012-02-09 06:16:32 +00001084 m_mach_segments(),
1085 m_mach_sections(),
1086 m_entry_point_address(),
1087 m_thread_context_offsets(),
1088 m_thread_context_offsets_valid(false)
Greg Claytonc9660542012-02-05 02:38:54 +00001089{
1090 ::memset (&m_header, 0, sizeof(m_header));
1091 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
1092}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001093
1094ObjectFileMachO::~ObjectFileMachO()
1095{
1096}
1097
Greg Claytonf4d6de62013-04-24 22:29:28 +00001098bool
1099ObjectFileMachO::ParseHeader (DataExtractor &data,
1100 lldb::offset_t *data_offset_ptr,
1101 llvm::MachO::mach_header &header)
1102{
1103 data.SetByteOrder (lldb::endian::InlHostByteOrder());
1104 // Leave magic in the original byte order
1105 header.magic = data.GetU32(data_offset_ptr);
1106 bool can_parse = false;
1107 bool is_64_bit = false;
1108 switch (header.magic)
1109 {
Charles Davis510938e2013-08-27 05:04:57 +00001110 case MH_MAGIC:
Greg Claytonf4d6de62013-04-24 22:29:28 +00001111 data.SetByteOrder (lldb::endian::InlHostByteOrder());
1112 data.SetAddressByteSize(4);
1113 can_parse = true;
1114 break;
1115
Charles Davis510938e2013-08-27 05:04:57 +00001116 case MH_MAGIC_64:
Greg Claytonf4d6de62013-04-24 22:29:28 +00001117 data.SetByteOrder (lldb::endian::InlHostByteOrder());
1118 data.SetAddressByteSize(8);
1119 can_parse = true;
1120 is_64_bit = true;
1121 break;
1122
Charles Davis510938e2013-08-27 05:04:57 +00001123 case MH_CIGAM:
Greg Claytonf4d6de62013-04-24 22:29:28 +00001124 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
1125 data.SetAddressByteSize(4);
1126 can_parse = true;
1127 break;
1128
Charles Davis510938e2013-08-27 05:04:57 +00001129 case MH_CIGAM_64:
Greg Claytonf4d6de62013-04-24 22:29:28 +00001130 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
1131 data.SetAddressByteSize(8);
1132 is_64_bit = true;
1133 can_parse = true;
1134 break;
1135
1136 default:
1137 break;
1138 }
1139
1140 if (can_parse)
1141 {
1142 data.GetU32(data_offset_ptr, &header.cputype, 6);
1143 if (is_64_bit)
1144 *data_offset_ptr += 4;
1145 return true;
1146 }
1147 else
1148 {
1149 memset(&header, 0, sizeof(header));
1150 }
1151 return false;
1152}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153
1154bool
1155ObjectFileMachO::ParseHeader ()
1156{
Greg Claytona1743492012-03-13 23:14:29 +00001157 ModuleSP module_sp(GetModule());
1158 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159 {
Greg Claytona1743492012-03-13 23:14:29 +00001160 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
1161 bool can_parse = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00001162 lldb::offset_t offset = 0;
Greg Clayton7fb56d02011-02-01 01:31:41 +00001163 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +00001164 // Leave magic in the original byte order
1165 m_header.magic = m_data.GetU32(&offset);
1166 switch (m_header.magic)
Greg Claytonc9660542012-02-05 02:38:54 +00001167 {
Charles Davis510938e2013-08-27 05:04:57 +00001168 case MH_MAGIC:
Greg Claytona1743492012-03-13 23:14:29 +00001169 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
1170 m_data.SetAddressByteSize(4);
1171 can_parse = true;
1172 break;
1173
Charles Davis510938e2013-08-27 05:04:57 +00001174 case MH_MAGIC_64:
Greg Claytona1743492012-03-13 23:14:29 +00001175 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
1176 m_data.SetAddressByteSize(8);
1177 can_parse = true;
1178 break;
1179
Charles Davis510938e2013-08-27 05:04:57 +00001180 case MH_CIGAM:
Greg Claytona1743492012-03-13 23:14:29 +00001181 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
1182 m_data.SetAddressByteSize(4);
1183 can_parse = true;
1184 break;
1185
Charles Davis510938e2013-08-27 05:04:57 +00001186 case MH_CIGAM_64:
Greg Claytona1743492012-03-13 23:14:29 +00001187 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
1188 m_data.SetAddressByteSize(8);
1189 can_parse = true;
1190 break;
1191
1192 default:
1193 break;
Greg Claytonc9660542012-02-05 02:38:54 +00001194 }
Greg Claytona1743492012-03-13 23:14:29 +00001195
1196 if (can_parse)
1197 {
1198 m_data.GetU32(&offset, &m_header.cputype, 6);
1199
Greg Clayton7ab7f892014-05-29 21:33:45 +00001200
1201 ArchSpec mach_arch;
1202
1203 if (GetArchitecture (mach_arch))
Greg Claytona1743492012-03-13 23:14:29 +00001204 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001205 // Check if the module has a required architecture
1206 const ArchSpec &module_arch = module_sp->GetArchitecture();
1207 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
1208 return false;
1209
1210 if (SetModulesArchitecture (mach_arch))
Greg Claytona1743492012-03-13 23:14:29 +00001211 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001212 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
1213 if (m_data.GetByteSize() < header_and_lc_size)
Greg Claytona1743492012-03-13 23:14:29 +00001214 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001215 DataBufferSP data_sp;
1216 ProcessSP process_sp (m_process_wp.lock());
1217 if (process_sp)
1218 {
1219 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
1220 }
1221 else
1222 {
1223 // Read in all only the load command data from the file on disk
1224 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
1225 if (data_sp->GetByteSize() != header_and_lc_size)
1226 return false;
1227 }
1228 if (data_sp)
1229 m_data.SetData (data_sp);
Greg Claytona1743492012-03-13 23:14:29 +00001230 }
Greg Claytona1743492012-03-13 23:14:29 +00001231 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00001232 return true;
Greg Claytona1743492012-03-13 23:14:29 +00001233 }
Greg Claytona1743492012-03-13 23:14:29 +00001234 }
1235 else
1236 {
1237 memset(&m_header, 0, sizeof(struct mach_header));
1238 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239 }
1240 return false;
1241}
1242
1243
1244ByteOrder
1245ObjectFileMachO::GetByteOrder () const
1246{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001247 return m_data.GetByteOrder ();
1248}
1249
Jim Ingham5aee1622010-08-09 23:31:02 +00001250bool
1251ObjectFileMachO::IsExecutable() const
1252{
Charles Davis510938e2013-08-27 05:04:57 +00001253 return m_header.filetype == MH_EXECUTE;
Jim Ingham5aee1622010-08-09 23:31:02 +00001254}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255
Greg Claytonc7bece562013-01-25 18:06:21 +00001256uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001257ObjectFileMachO::GetAddressByteSize () const
1258{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 return m_data.GetAddressByteSize ();
1260}
1261
Greg Claytone0d378b2011-03-24 21:19:54 +00001262AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +00001263ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
1264{
1265 Symtab *symtab = GetSymtab();
1266 if (symtab)
1267 {
1268 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
1269 if (symbol)
1270 {
Greg Claytone7612132012-03-07 21:03:09 +00001271 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +00001272 {
Greg Claytone7612132012-03-07 21:03:09 +00001273 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +00001274 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +00001275 {
Charles Davis510938e2013-08-27 05:04:57 +00001276 const lldb::SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +00001277 switch (section_type)
1278 {
Jason Molendae589e7e2014-12-08 03:09:00 +00001279 case eSectionTypeInvalid:
1280 return eAddressClassUnknown;
1281
Greg Claytonded470d2011-03-19 01:12:21 +00001282 case eSectionTypeCode:
Charles Davis510938e2013-08-27 05:04:57 +00001283 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +00001284 {
1285 // For ARM we have a bit in the n_desc field of the symbol
1286 // that tells us ARM/Thumb which is bit 0x0008.
1287 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1288 return eAddressClassCodeAlternateISA;
1289 }
1290 return eAddressClassCode;
1291
Jason Molendae589e7e2014-12-08 03:09:00 +00001292 case eSectionTypeContainer:
1293 return eAddressClassUnknown;
1294
Greg Clayton5009f9d2011-10-27 17:55:14 +00001295 case eSectionTypeData:
1296 case eSectionTypeDataCString:
1297 case eSectionTypeDataCStringPointers:
1298 case eSectionTypeDataSymbolAddress:
1299 case eSectionTypeData4:
1300 case eSectionTypeData8:
1301 case eSectionTypeData16:
1302 case eSectionTypeDataPointers:
1303 case eSectionTypeZeroFill:
1304 case eSectionTypeDataObjCMessageRefs:
1305 case eSectionTypeDataObjCCFStrings:
1306 return eAddressClassData;
Jason Molendae589e7e2014-12-08 03:09:00 +00001307
Greg Clayton5009f9d2011-10-27 17:55:14 +00001308 case eSectionTypeDebug:
1309 case eSectionTypeDWARFDebugAbbrev:
1310 case eSectionTypeDWARFDebugAranges:
1311 case eSectionTypeDWARFDebugFrame:
1312 case eSectionTypeDWARFDebugInfo:
1313 case eSectionTypeDWARFDebugLine:
1314 case eSectionTypeDWARFDebugLoc:
1315 case eSectionTypeDWARFDebugMacInfo:
1316 case eSectionTypeDWARFDebugPubNames:
1317 case eSectionTypeDWARFDebugPubTypes:
1318 case eSectionTypeDWARFDebugRanges:
1319 case eSectionTypeDWARFDebugStr:
1320 case eSectionTypeDWARFAppleNames:
1321 case eSectionTypeDWARFAppleTypes:
1322 case eSectionTypeDWARFAppleNamespaces:
1323 case eSectionTypeDWARFAppleObjC:
1324 return eAddressClassDebug;
Jason Molendae589e7e2014-12-08 03:09:00 +00001325
1326 case eSectionTypeEHFrame:
1327 case eSectionTypeCompactUnwind:
1328 return eAddressClassRuntime;
1329
Michael Sartaina7499c92013-07-01 19:45:50 +00001330 case eSectionTypeELFSymbolTable:
1331 case eSectionTypeELFDynamicSymbols:
1332 case eSectionTypeELFRelocationEntries:
1333 case eSectionTypeELFDynamicLinkInfo:
Jason Molendae589e7e2014-12-08 03:09:00 +00001334 case eSectionTypeOther:
1335 return eAddressClassUnknown;
Greg Claytonded470d2011-03-19 01:12:21 +00001336 }
1337 }
1338 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001339
Greg Claytone0d378b2011-03-24 21:19:54 +00001340 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +00001341 switch (symbol_type)
1342 {
1343 case eSymbolTypeAny: return eAddressClassUnknown;
1344 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda4e7511e2013-03-06 23:19:17 +00001345
Greg Claytonded470d2011-03-19 01:12:21 +00001346 case eSymbolTypeCode:
1347 case eSymbolTypeTrampoline:
Greg Clayton059f7242013-02-27 21:16:04 +00001348 case eSymbolTypeResolver:
Charles Davis510938e2013-08-27 05:04:57 +00001349 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +00001350 {
1351 // For ARM we have a bit in the n_desc field of the symbol
1352 // that tells us ARM/Thumb which is bit 0x0008.
1353 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1354 return eAddressClassCodeAlternateISA;
1355 }
1356 return eAddressClassCode;
1357
1358 case eSymbolTypeData: return eAddressClassData;
1359 case eSymbolTypeRuntime: return eAddressClassRuntime;
1360 case eSymbolTypeException: return eAddressClassRuntime;
1361 case eSymbolTypeSourceFile: return eAddressClassDebug;
1362 case eSymbolTypeHeaderFile: return eAddressClassDebug;
1363 case eSymbolTypeObjectFile: return eAddressClassDebug;
1364 case eSymbolTypeCommonBlock: return eAddressClassDebug;
1365 case eSymbolTypeBlock: return eAddressClassDebug;
1366 case eSymbolTypeLocal: return eAddressClassData;
1367 case eSymbolTypeParam: return eAddressClassData;
1368 case eSymbolTypeVariable: return eAddressClassData;
1369 case eSymbolTypeVariableType: return eAddressClassDebug;
1370 case eSymbolTypeLineEntry: return eAddressClassDebug;
1371 case eSymbolTypeLineHeader: return eAddressClassDebug;
1372 case eSymbolTypeScopeBegin: return eAddressClassDebug;
1373 case eSymbolTypeScopeEnd: return eAddressClassDebug;
1374 case eSymbolTypeAdditional: return eAddressClassUnknown;
1375 case eSymbolTypeCompiler: return eAddressClassDebug;
1376 case eSymbolTypeInstrumentation:return eAddressClassDebug;
1377 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +00001378 case eSymbolTypeObjCClass: return eAddressClassRuntime;
1379 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
1380 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Clayton9191db42013-10-21 18:40:51 +00001381 case eSymbolTypeReExported: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +00001382 }
1383 }
1384 }
1385 return eAddressClassUnknown;
1386}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001387
1388Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +00001389ObjectFileMachO::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390{
Greg Claytona1743492012-03-13 23:14:29 +00001391 ModuleSP module_sp(GetModule());
1392 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393 {
Greg Claytona1743492012-03-13 23:14:29 +00001394 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
1395 if (m_symtab_ap.get() == NULL)
1396 {
1397 m_symtab_ap.reset(new Symtab(this));
1398 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton3046e662013-07-10 01:23:25 +00001399 ParseSymtab ();
Greg Claytona1743492012-03-13 23:14:29 +00001400 m_symtab_ap->Finalize ();
1401 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001402 }
1403 return m_symtab_ap.get();
1404}
1405
Greg Clayton3046e662013-07-10 01:23:25 +00001406bool
1407ObjectFileMachO::IsStripped ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408{
Greg Clayton3046e662013-07-10 01:23:25 +00001409 if (m_dysymtab.cmd == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001410 {
Greg Clayton3046e662013-07-10 01:23:25 +00001411 ModuleSP module_sp(GetModule());
1412 if (module_sp)
Greg Claytona1743492012-03-13 23:14:29 +00001413 {
Greg Clayton3046e662013-07-10 01:23:25 +00001414 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1415 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton4d78c402012-05-25 18:09:55 +00001416 {
Greg Clayton3046e662013-07-10 01:23:25 +00001417 const lldb::offset_t load_cmd_offset = offset;
1418
1419 load_command lc;
1420 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
1421 break;
Charles Davis510938e2013-08-27 05:04:57 +00001422 if (lc.cmd == LC_DYSYMTAB)
Greg Clayton4d78c402012-05-25 18:09:55 +00001423 {
Greg Clayton3046e662013-07-10 01:23:25 +00001424 m_dysymtab.cmd = lc.cmd;
1425 m_dysymtab.cmdsize = lc.cmdsize;
1426 if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
1427 {
1428 // Clear m_dysymtab if we were unable to read all items from the load command
1429 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
1430 }
Greg Clayton4d78c402012-05-25 18:09:55 +00001431 }
Greg Clayton3046e662013-07-10 01:23:25 +00001432 offset = load_cmd_offset + lc.cmdsize;
Greg Clayton4d78c402012-05-25 18:09:55 +00001433 }
Greg Clayton1eac0c72012-04-24 03:06:13 +00001434 }
Greg Clayton1eac0c72012-04-24 03:06:13 +00001435 }
Greg Clayton3046e662013-07-10 01:23:25 +00001436 if (m_dysymtab.cmd)
Greg Clayton93e28612013-10-11 22:03:48 +00001437 return m_dysymtab.nlocalsym <= 1;
Greg Clayton3046e662013-07-10 01:23:25 +00001438 return false;
1439}
Greg Clayton1eac0c72012-04-24 03:06:13 +00001440
Greg Clayton3046e662013-07-10 01:23:25 +00001441void
1442ObjectFileMachO::CreateSections (SectionList &unified_section_list)
1443{
1444 if (!m_sections_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001445 {
Greg Clayton3046e662013-07-10 01:23:25 +00001446 m_sections_ap.reset(new SectionList());
1447
Charles Davis510938e2013-08-27 05:04:57 +00001448 const bool is_dsym = (m_header.filetype == MH_DSYM);
Greg Clayton3046e662013-07-10 01:23:25 +00001449 lldb::user_id_t segID = 0;
1450 lldb::user_id_t sectID = 0;
1451 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1452 uint32_t i;
1453 const bool is_core = GetType() == eTypeCoreFile;
1454 //bool dump_sections = false;
1455 ModuleSP module_sp (GetModule());
1456 // First look up any LC_ENCRYPTION_INFO load commands
1457 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
1458 EncryptedFileRanges encrypted_file_ranges;
1459 encryption_info_command encryption_cmd;
1460 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461 {
Greg Clayton3046e662013-07-10 01:23:25 +00001462 const lldb::offset_t load_cmd_offset = offset;
1463 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
1464 break;
1465
Charles Davis510938e2013-08-27 05:04:57 +00001466 if (encryption_cmd.cmd == LC_ENCRYPTION_INFO)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467 {
Greg Clayton3046e662013-07-10 01:23:25 +00001468 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
Jason Molendaf6ce26f2013-04-10 05:58:57 +00001469 {
Greg Clayton3046e662013-07-10 01:23:25 +00001470 if (encryption_cmd.cryptid != 0)
Greg Claytond37d6922013-04-16 16:51:19 +00001471 {
Greg Clayton3046e662013-07-10 01:23:25 +00001472 EncryptedFileRanges::Entry entry;
1473 entry.SetRangeBase(encryption_cmd.cryptoff);
1474 entry.SetByteSize(encryption_cmd.cryptsize);
1475 encrypted_file_ranges.Append(entry);
Jason Molendaf6ce26f2013-04-10 05:58:57 +00001476 }
1477 }
Greg Clayton3046e662013-07-10 01:23:25 +00001478 }
1479 offset = load_cmd_offset + encryption_cmd.cmdsize;
1480 }
1481
Jason Molenda05a09c62014-08-22 02:46:46 +00001482 bool section_file_addresses_changed = false;
1483
Greg Clayton3046e662013-07-10 01:23:25 +00001484 offset = MachHeaderSizeFromMagic(m_header.magic);
1485
1486 struct segment_command_64 load_cmd;
1487 for (i=0; i<m_header.ncmds; ++i)
1488 {
1489 const lldb::offset_t load_cmd_offset = offset;
1490 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1491 break;
1492
Charles Davis510938e2013-08-27 05:04:57 +00001493 if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64)
Greg Clayton3046e662013-07-10 01:23:25 +00001494 {
1495 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001496 {
Greg Clayton3046e662013-07-10 01:23:25 +00001497 bool add_section = true;
1498 bool add_to_unified = true;
1499 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 +00001500
Greg Clayton3046e662013-07-10 01:23:25 +00001501 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1502 if (is_dsym && unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503 {
Greg Clayton3046e662013-07-10 01:23:25 +00001504 if (const_segname == GetSegmentNameLINKEDIT())
1505 {
1506 // We need to keep the __LINKEDIT segment private to this object file only
1507 add_to_unified = false;
1508 }
1509 else
1510 {
1511 // This is the dSYM file and this section has already been created by
1512 // the object file, no need to create it.
1513 add_section = false;
1514 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001515 }
Greg Clayton3046e662013-07-10 01:23:25 +00001516 load_cmd.vmaddr = m_data.GetAddress(&offset);
1517 load_cmd.vmsize = m_data.GetAddress(&offset);
1518 load_cmd.fileoff = m_data.GetAddress(&offset);
1519 load_cmd.filesize = m_data.GetAddress(&offset);
1520 if (m_length != 0 && load_cmd.filesize != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001521 {
Greg Clayton3046e662013-07-10 01:23:25 +00001522 if (load_cmd.fileoff > m_length)
1523 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001524 // 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 +00001525 // a corrupt file. We don't have any way to return an error condition here (this method
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001526 // was likely invoked from something like ObjectFile::GetSectionList()) -- all we can do
Greg Clayton3046e662013-07-10 01:23:25 +00001527 // is null out the SectionList vector and if a process has been set up, dump a message
1528 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001529 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001530 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 +00001531 i,
1532 lc_segment_name,
1533 load_cmd.fileoff,
1534 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001535
1536 load_cmd.fileoff = 0;
1537 load_cmd.filesize = 0;
1538 }
1539
1540 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1541 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001542 // 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 +00001543 // a corrupt file. We don't have any way to return an error condition here (this method
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001544 // was likely invoked from something like ObjectFile::GetSectionList()) -- all we can do
Greg Clayton3046e662013-07-10 01:23:25 +00001545 // is null out the SectionList vector and if a process has been set up, dump a message
1546 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001547 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001548 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 +00001549 i,
1550 lc_segment_name,
1551 load_cmd.fileoff + load_cmd.filesize,
1552 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001553
1554 // Tuncase the length
1555 load_cmd.filesize = m_length - load_cmd.fileoff;
1556 }
1557 }
1558 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1559 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001560
Charles Davis510938e2013-08-27 05:04:57 +00001561 const bool segment_is_encrypted = (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001562
Greg Clayton3046e662013-07-10 01:23:25 +00001563 // Keep a list of mach segments around in case we need to
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001564 // get at data that isn't stored in the abstracted Sections.
Greg Clayton3046e662013-07-10 01:23:25 +00001565 m_mach_segments.push_back (load_cmd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001566
Greg Clayton3046e662013-07-10 01:23:25 +00001567 // Use a segment ID of the segment index shifted left by 8 so they
1568 // never conflict with any of the sections.
1569 SectionSP segment_sp;
1570 if (add_section && (const_segname || is_core))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571 {
Greg Clayton3046e662013-07-10 01:23:25 +00001572 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
1573 this, // Object file to which this sections belongs
1574 ++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
1575 const_segname, // Name of this section
1576 eSectionTypeContainer, // This section is a container of other sections.
1577 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1578 load_cmd.vmsize, // VM size in bytes of this section
1579 load_cmd.fileoff, // Offset to the data for this section in the file
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001580 load_cmd.filesize, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +00001581 0, // Segments have no alignment information
Greg Clayton3046e662013-07-10 01:23:25 +00001582 load_cmd.flags)); // Flags for this section
Greg Clayton8d38ac42010-06-28 23:51:11 +00001583
Greg Clayton3046e662013-07-10 01:23:25 +00001584 segment_sp->SetIsEncrypted (segment_is_encrypted);
1585 m_sections_ap->AddSection(segment_sp);
1586 if (add_to_unified)
1587 unified_section_list.AddSection(segment_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001588 }
Greg Clayton3046e662013-07-10 01:23:25 +00001589 else if (unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001590 {
Jason Molenda20eb31b2013-08-16 03:20:42 +00001591 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr)
1592 {
1593 // Check to see if the module was read from memory?
1594 if (module_sp->GetObjectFile()->GetHeaderAddress().IsValid())
1595 {
1596 // We have a module that is in memory and needs to have its
1597 // file address adjusted. We need to do this because when we
1598 // load a file from memory, its addresses will be slid already,
1599 // yet the addresses in the new symbol file will still be unslid.
1600 // Since everything is stored as section offset, this shouldn't
1601 // cause any problems.
Jason Molenda5894a732013-08-17 03:39:52 +00001602
1603 // Make sure we've parsed the symbol table from the
1604 // ObjectFile before we go around changing its Sections.
1605 module_sp->GetObjectFile()->GetSymtab();
1606 // eh_frame would present the same problems but we parse that on
1607 // a per-function basis as-needed so it's more difficult to
1608 // remove its use of the Sections. Realistically, the environments
1609 // where this code path will be taken will not have eh_frame sections.
1610
Jason Molenda20eb31b2013-08-16 03:20:42 +00001611 unified_section_sp->SetFileAddress(load_cmd.vmaddr);
Jason Molenda05a09c62014-08-22 02:46:46 +00001612
1613 // Notify the module that the section addresses have been changed once
1614 // we're done so any file-address caches can be updated.
1615 section_file_addresses_changed = true;
Jason Molenda20eb31b2013-08-16 03:20:42 +00001616 }
1617 }
Greg Clayton3046e662013-07-10 01:23:25 +00001618 m_sections_ap->AddSection(unified_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001619 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001620
Greg Clayton3046e662013-07-10 01:23:25 +00001621 struct section_64 sect64;
1622 ::memset (&sect64, 0, sizeof(sect64));
1623 // Push a section into our mach sections for the section at
Charles Davis510938e2013-08-27 05:04:57 +00001624 // index zero (NO_SECT) if we don't have any mach sections yet...
Greg Clayton3046e662013-07-10 01:23:25 +00001625 if (m_mach_sections.empty())
1626 m_mach_sections.push_back(sect64);
1627 uint32_t segment_sect_idx;
1628 const lldb::user_id_t first_segment_sectID = sectID + 1;
1629
1630
Charles Davis510938e2013-08-27 05:04:57 +00001631 const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8;
Greg Clayton3046e662013-07-10 01:23:25 +00001632 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001633 {
Greg Clayton3046e662013-07-10 01:23:25 +00001634 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
Greg Clayton89411422010-10-08 00:21:05 +00001635 break;
Greg Clayton3046e662013-07-10 01:23:25 +00001636 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1637 break;
1638 sect64.addr = m_data.GetAddress(&offset);
1639 sect64.size = m_data.GetAddress(&offset);
1640
1641 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1642 break;
1643
1644 // Keep a list of mach sections around in case we need to
1645 // get at data that isn't stored in the abstracted Sections.
1646 m_mach_sections.push_back (sect64);
1647
1648 if (add_section)
1649 {
1650 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1651 if (!const_segname)
1652 {
1653 // We have a segment with no name so we need to conjure up
1654 // segments that correspond to the section's segname if there
1655 // isn't already such a section. If there is such a section,
1656 // we resize the section so that it spans all sections.
1657 // We also mark these sections as fake so address matches don't
1658 // hit if they land in the gaps between the child sections.
1659 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1660 segment_sp = unified_section_list.FindSectionByName (const_segname);
1661 if (segment_sp.get())
1662 {
1663 Section *segment = segment_sp.get();
1664 // Grow the section size as needed.
1665 const lldb::addr_t sect64_min_addr = sect64.addr;
1666 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1667 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1668 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1669 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1670 if (sect64_min_addr >= curr_seg_min_addr)
1671 {
1672 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1673 // Only grow the section size if needed
1674 if (new_seg_byte_size > curr_seg_byte_size)
1675 segment->SetByteSize (new_seg_byte_size);
1676 }
1677 else
1678 {
1679 // We need to change the base address of the segment and
1680 // adjust the child section offsets for all existing children.
1681 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1682 segment->Slide(slide_amount, false);
1683 segment->GetChildren().Slide(-slide_amount, false);
1684 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1685 }
1686
1687 // Grow the section size as needed.
1688 if (sect64.offset)
1689 {
1690 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1691 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1692
1693 const lldb::addr_t section_min_file_offset = sect64.offset;
1694 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1695 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1696 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1697 segment->SetFileOffset (new_file_offset);
1698 segment->SetFileSize (new_file_size);
1699 }
1700 }
1701 else
1702 {
1703 // Create a fake section for the section's named segment
1704 segment_sp.reset(new Section (segment_sp, // Parent section
1705 module_sp, // Module to which this section belongs
1706 this, // Object file to which this section belongs
1707 ++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
1708 const_segname, // Name of this section
1709 eSectionTypeContainer, // This section is a container of other sections.
1710 sect64.addr, // File VM address == addresses as they are found in the object file
1711 sect64.size, // VM size in bytes of this section
1712 sect64.offset, // Offset to the data for this section in the file
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001713 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +00001714 sect64.align,
Greg Clayton3046e662013-07-10 01:23:25 +00001715 load_cmd.flags)); // Flags for this section
1716 segment_sp->SetIsFake(true);
1717
1718 m_sections_ap->AddSection(segment_sp);
1719 if (add_to_unified)
1720 unified_section_list.AddSection(segment_sp);
1721 segment_sp->SetIsEncrypted (segment_is_encrypted);
1722 }
1723 }
1724 assert (segment_sp.get());
1725
Charles Davis510938e2013-08-27 05:04:57 +00001726 lldb::SectionType sect_type = eSectionTypeOther;
Greg Clayton3046e662013-07-10 01:23:25 +00001727
Greg Clayton38f9cc42014-06-16 22:53:16 +00001728 if (sect64.flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS))
1729 sect_type = eSectionTypeCode;
1730 else
Greg Clayton3046e662013-07-10 01:23:25 +00001731 {
Greg Clayton38f9cc42014-06-16 22:53:16 +00001732 uint32_t mach_sect_type = sect64.flags & SECTION_TYPE;
1733 static ConstString g_sect_name_objc_data ("__objc_data");
1734 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1735 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1736 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1737 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1738 static ConstString g_sect_name_objc_const ("__objc_const");
1739 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1740 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton3046e662013-07-10 01:23:25 +00001741
Greg Clayton38f9cc42014-06-16 22:53:16 +00001742 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1743 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1744 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1745 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1746 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1747 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1748 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1749 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1750 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1751 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1752 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1753 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1754 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1755 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1756 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1757 static ConstString g_sect_name_eh_frame ("__eh_frame");
Jason Molendae589e7e2014-12-08 03:09:00 +00001758 static ConstString g_sect_name_compact_unwind ("__unwind_info");
Greg Clayton38f9cc42014-06-16 22:53:16 +00001759 static ConstString g_sect_name_text ("__text");
1760 static ConstString g_sect_name_data ("__data");
1761
1762
1763 if (section_name == g_sect_name_dwarf_debug_abbrev)
1764 sect_type = eSectionTypeDWARFDebugAbbrev;
1765 else if (section_name == g_sect_name_dwarf_debug_aranges)
1766 sect_type = eSectionTypeDWARFDebugAranges;
1767 else if (section_name == g_sect_name_dwarf_debug_frame)
1768 sect_type = eSectionTypeDWARFDebugFrame;
1769 else if (section_name == g_sect_name_dwarf_debug_info)
1770 sect_type = eSectionTypeDWARFDebugInfo;
1771 else if (section_name == g_sect_name_dwarf_debug_line)
1772 sect_type = eSectionTypeDWARFDebugLine;
1773 else if (section_name == g_sect_name_dwarf_debug_loc)
1774 sect_type = eSectionTypeDWARFDebugLoc;
1775 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1776 sect_type = eSectionTypeDWARFDebugMacInfo;
1777 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1778 sect_type = eSectionTypeDWARFDebugPubNames;
1779 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1780 sect_type = eSectionTypeDWARFDebugPubTypes;
1781 else if (section_name == g_sect_name_dwarf_debug_ranges)
1782 sect_type = eSectionTypeDWARFDebugRanges;
1783 else if (section_name == g_sect_name_dwarf_debug_str)
1784 sect_type = eSectionTypeDWARFDebugStr;
1785 else if (section_name == g_sect_name_dwarf_apple_names)
1786 sect_type = eSectionTypeDWARFAppleNames;
1787 else if (section_name == g_sect_name_dwarf_apple_types)
1788 sect_type = eSectionTypeDWARFAppleTypes;
1789 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1790 sect_type = eSectionTypeDWARFAppleNamespaces;
1791 else if (section_name == g_sect_name_dwarf_apple_objc)
1792 sect_type = eSectionTypeDWARFAppleObjC;
1793 else if (section_name == g_sect_name_objc_selrefs)
1794 sect_type = eSectionTypeDataCStringPointers;
1795 else if (section_name == g_sect_name_objc_msgrefs)
1796 sect_type = eSectionTypeDataObjCMessageRefs;
1797 else if (section_name == g_sect_name_eh_frame)
1798 sect_type = eSectionTypeEHFrame;
Jason Molendae589e7e2014-12-08 03:09:00 +00001799 else if (section_name == g_sect_name_compact_unwind)
1800 sect_type = eSectionTypeCompactUnwind;
Greg Clayton38f9cc42014-06-16 22:53:16 +00001801 else if (section_name == g_sect_name_cfstring)
1802 sect_type = eSectionTypeDataObjCCFStrings;
1803 else if (section_name == g_sect_name_objc_data ||
1804 section_name == g_sect_name_objc_classrefs ||
1805 section_name == g_sect_name_objc_superrefs ||
1806 section_name == g_sect_name_objc_const ||
1807 section_name == g_sect_name_objc_classlist)
Greg Clayton3046e662013-07-10 01:23:25 +00001808 {
Greg Clayton38f9cc42014-06-16 22:53:16 +00001809 sect_type = eSectionTypeDataPointers;
1810 }
1811
1812 if (sect_type == eSectionTypeOther)
1813 {
1814 switch (mach_sect_type)
1815 {
1816 // TODO: categorize sections by other flags for regular sections
1817 case S_REGULAR:
1818 if (section_name == g_sect_name_text)
1819 sect_type = eSectionTypeCode;
1820 else if (section_name == g_sect_name_data)
1821 sect_type = eSectionTypeData;
1822 else
1823 sect_type = eSectionTypeOther;
1824 break;
1825 case S_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1826 case S_CSTRING_LITERALS: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1827 case S_4BYTE_LITERALS: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1828 case S_8BYTE_LITERALS: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1829 case S_LITERAL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1830 case S_NON_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1831 case S_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1832 case S_SYMBOL_STUBS: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1833 case S_MOD_INIT_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1834 case S_MOD_TERM_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1835 case S_COALESCED: sect_type = eSectionTypeOther; break;
1836 case S_GB_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1837 case S_INTERPOSING: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1838 case S_16BYTE_LITERALS: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1839 case S_DTRACE_DOF: sect_type = eSectionTypeDebug; break;
1840 case S_LAZY_DYLIB_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break;
1841 default: break;
1842 }
Greg Clayton3046e662013-07-10 01:23:25 +00001843 }
1844 }
1845
1846 SectionSP section_sp(new Section (segment_sp,
1847 module_sp,
1848 this,
1849 ++sectID,
1850 section_name,
1851 sect_type,
1852 sect64.addr - segment_sp->GetFileAddress(),
1853 sect64.size,
1854 sect64.offset,
1855 sect64.offset == 0 ? 0 : sect64.size,
Greg Clayton48672af2014-06-24 22:22:43 +00001856 sect64.align,
Greg Clayton3046e662013-07-10 01:23:25 +00001857 sect64.flags));
1858 // Set the section to be encrypted to match the segment
1859
1860 bool section_is_encrypted = false;
1861 if (!segment_is_encrypted && load_cmd.filesize != 0)
1862 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1863
1864 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1865 segment_sp->GetChildren().AddSection(section_sp);
1866
1867 if (segment_sp->IsFake())
1868 {
1869 segment_sp.reset();
1870 const_segname.Clear();
1871 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001872 }
1873 }
Greg Clayton3046e662013-07-10 01:23:25 +00001874 if (segment_sp && is_dsym)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001875 {
Greg Clayton3046e662013-07-10 01:23:25 +00001876 if (first_segment_sectID <= sectID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877 {
Greg Clayton3046e662013-07-10 01:23:25 +00001878 lldb::user_id_t sect_uid;
1879 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001880 {
Greg Clayton3046e662013-07-10 01:23:25 +00001881 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1882 SectionSP next_section_sp;
1883 if (sect_uid + 1 <= sectID)
1884 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1885
1886 if (curr_section_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001887 {
Greg Clayton3046e662013-07-10 01:23:25 +00001888 if (curr_section_sp->GetByteSize() == 0)
1889 {
1890 if (next_section_sp.get() != NULL)
1891 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1892 else
1893 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1894 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001895 }
1896 }
1897 }
1898 }
1899 }
1900 }
1901 }
Charles Davis510938e2013-08-27 05:04:57 +00001902 else if (load_cmd.cmd == LC_DYSYMTAB)
Greg Clayton3046e662013-07-10 01:23:25 +00001903 {
1904 m_dysymtab.cmd = load_cmd.cmd;
1905 m_dysymtab.cmdsize = load_cmd.cmdsize;
1906 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1907 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001908
Greg Clayton3046e662013-07-10 01:23:25 +00001909 offset = load_cmd_offset + load_cmd.cmdsize;
1910 }
Jason Molenda05a09c62014-08-22 02:46:46 +00001911
1912
1913 if (section_file_addresses_changed && module_sp.get())
1914 {
1915 module_sp->SectionFileAddressesChanged();
1916 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001917 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001918}
1919
1920class MachSymtabSectionInfo
1921{
1922public:
1923
1924 MachSymtabSectionInfo (SectionList *section_list) :
1925 m_section_list (section_list),
1926 m_section_infos()
1927 {
1928 // Get the number of sections down to a depth of 1 to include
1929 // all segments and their sections, but no other sections that
1930 // may be added for debug map or
1931 m_section_infos.resize(section_list->GetNumSections(1));
1932 }
1933
1934
Greg Claytone72dfb32012-02-24 01:59:29 +00001935 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001936 GetSection (uint8_t n_sect, addr_t file_addr)
1937 {
1938 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00001939 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001940 if (n_sect < m_section_infos.size())
1941 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001942 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001943 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001944 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1945 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001946 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00001947 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001948 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1949 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00001950 }
1951 else
1952 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001953 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00001954 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001955 }
1956 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00001957 {
1958 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00001959 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001960 }
1961 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1962 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1963 {
1964 // Symbol is in section with zero size, but has the same start
1965 // address as the section. This can happen with linker symbols
1966 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00001967 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001968 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001970 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001971 }
1972
1973protected:
1974 struct SectionInfo
1975 {
1976 SectionInfo () :
1977 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001978 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001979 {
1980 }
1981
1982 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00001983 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001984 };
1985 SectionList *m_section_list;
1986 std::vector<SectionInfo> m_section_infos;
1987};
1988
Greg Clayton9191db42013-10-21 18:40:51 +00001989struct TrieEntry
1990{
1991 TrieEntry () :
1992 name(),
1993 address(LLDB_INVALID_ADDRESS),
1994 flags (0),
1995 other(0),
1996 import_name()
1997 {
1998 }
1999
2000 void
2001 Clear ()
2002 {
2003 name.Clear();
2004 address = LLDB_INVALID_ADDRESS;
2005 flags = 0;
2006 other = 0;
2007 import_name.Clear();
2008 }
2009
2010 void
2011 Dump () const
2012 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002013 printf ("0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"",
2014 static_cast<unsigned long long>(address),
2015 static_cast<unsigned long long>(flags),
2016 static_cast<unsigned long long>(other), name.GetCString());
Greg Clayton9191db42013-10-21 18:40:51 +00002017 if (import_name)
2018 printf (" -> \"%s\"\n", import_name.GetCString());
2019 else
2020 printf ("\n");
2021 }
2022 ConstString name;
2023 uint64_t address;
2024 uint64_t flags;
2025 uint64_t other;
2026 ConstString import_name;
2027};
2028
2029struct TrieEntryWithOffset
2030{
2031 lldb::offset_t nodeOffset;
2032 TrieEntry entry;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002033
Greg Clayton9191db42013-10-21 18:40:51 +00002034 TrieEntryWithOffset (lldb::offset_t offset) :
2035 nodeOffset (offset),
2036 entry()
2037 {
2038 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002039
Greg Clayton9191db42013-10-21 18:40:51 +00002040 void
2041 Dump (uint32_t idx) const
2042 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002043 printf ("[%3u] 0x%16.16llx: ", idx,
2044 static_cast<unsigned long long>(nodeOffset));
Greg Clayton9191db42013-10-21 18:40:51 +00002045 entry.Dump();
2046 }
2047
2048 bool
2049 operator<(const TrieEntryWithOffset& other) const
2050 {
2051 return ( nodeOffset < other.nodeOffset );
2052 }
2053};
2054
2055static void
2056ParseTrieEntries (DataExtractor &data,
2057 lldb::offset_t offset,
2058 std::vector<llvm::StringRef> &nameSlices,
2059 std::set<lldb::addr_t> &resolver_addresses,
2060 std::vector<TrieEntryWithOffset>& output)
2061{
2062 if (!data.ValidOffset(offset))
2063 return;
2064
2065 const uint64_t terminalSize = data.GetULEB128(&offset);
2066 lldb::offset_t children_offset = offset + terminalSize;
2067 if ( terminalSize != 0 ) {
2068 TrieEntryWithOffset e (offset);
2069 e.entry.flags = data.GetULEB128(&offset);
2070 const char *import_name = NULL;
2071 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
2072 e.entry.address = 0;
2073 e.entry.other = data.GetULEB128(&offset); // dylib ordinal
2074 import_name = data.GetCStr(&offset);
2075 }
2076 else {
2077 e.entry.address = data.GetULEB128(&offset);
2078 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
2079 {
Jim Inghamea3ac272014-01-10 22:55:37 +00002080 //resolver_addresses.insert(e.entry.address);
Greg Clayton9191db42013-10-21 18:40:51 +00002081 e.entry.other = data.GetULEB128(&offset);
Jim Inghamea3ac272014-01-10 22:55:37 +00002082 resolver_addresses.insert(e.entry.other);
Greg Clayton9191db42013-10-21 18:40:51 +00002083 }
2084 else
2085 e.entry.other = 0;
2086 }
2087 // Only add symbols that are reexport symbols with a valid import name
2088 if (EXPORT_SYMBOL_FLAGS_REEXPORT & e.entry.flags && import_name && import_name[0])
2089 {
2090 std::string name;
2091 if (!nameSlices.empty())
2092 {
2093 for (auto name_slice: nameSlices)
2094 name.append(name_slice.data(), name_slice.size());
2095 }
2096 if (name.size() > 1)
2097 {
2098 // Skip the leading '_'
2099 e.entry.name.SetCStringWithLength(name.c_str() + 1,name.size() - 1);
2100 }
2101 if (import_name)
2102 {
2103 // Skip the leading '_'
2104 e.entry.import_name.SetCString(import_name+1);
2105 }
2106 output.push_back(e);
2107 }
2108 }
2109
2110 const uint8_t childrenCount = data.GetU8(&children_offset);
2111 for (uint8_t i=0; i < childrenCount; ++i) {
2112 nameSlices.push_back(data.GetCStr(&children_offset));
2113 lldb::offset_t childNodeOffset = data.GetULEB128(&children_offset);
2114 if (childNodeOffset)
2115 {
2116 ParseTrieEntries(data,
2117 childNodeOffset,
2118 nameSlices,
2119 resolver_addresses,
2120 output);
2121 }
2122 nameSlices.pop_back();
2123 }
2124}
2125
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002126size_t
Greg Clayton3046e662013-07-10 01:23:25 +00002127ObjectFileMachO::ParseSymtab ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002128{
2129 Timer scoped_timer(__PRETTY_FUNCTION__,
2130 "ObjectFileMachO::ParseSymtab () module = %s",
2131 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002132 ModuleSP module_sp (GetModule());
2133 if (!module_sp)
2134 return 0;
2135
2136 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
2137 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
Greg Clayton9191db42013-10-21 18:40:51 +00002138 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 +00002139 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
2140 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00002141 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002142 uint32_t i;
Greg Clayton9191db42013-10-21 18:40:51 +00002143 FileSpecList dylib_files;
Greg Clayton5160ce52013-03-27 23:08:40 +00002144 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton1e28adf2015-02-25 17:25:02 +00002145 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2146 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2147 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
Greg Clayton77ccca72011-12-30 00:32:24 +00002148
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002149 for (i=0; i<m_header.ncmds; ++i)
2150 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002151 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002152 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002153 struct load_command lc;
2154 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002155 break;
2156 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002157 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002158 {
Charles Davis510938e2013-08-27 05:04:57 +00002159 case LC_SYMTAB:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002160 symtab_load_command.cmd = lc.cmd;
2161 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002162 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002163 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
2164 return 0;
2165 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002166 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002167 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002168 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002169 return 0;
2170 }
2171
2172 if (symtab_load_command.stroff == 0)
2173 {
2174 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002175 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002176 return 0;
2177 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002178
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002179 if (symtab_load_command.nsyms == 0)
2180 {
2181 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002182 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002183 return 0;
2184 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002185
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002186 if (symtab_load_command.strsize == 0)
2187 {
2188 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002189 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002190 return 0;
2191 }
2192 break;
2193
Greg Clayton9191db42013-10-21 18:40:51 +00002194 case LC_DYLD_INFO:
2195 case LC_DYLD_INFO_ONLY:
2196 if (m_data.GetU32(&offset, &dyld_info.rebase_off, 10))
2197 {
2198 dyld_info.cmd = lc.cmd;
2199 dyld_info.cmdsize = lc.cmdsize;
2200 }
2201 else
2202 {
2203 memset (&dyld_info, 0, sizeof(dyld_info));
2204 }
2205 break;
2206
2207 case LC_LOAD_DYLIB:
2208 case LC_LOAD_WEAK_DYLIB:
2209 case LC_REEXPORT_DYLIB:
2210 case LC_LOADFVMLIB:
2211 case LC_LOAD_UPWARD_DYLIB:
2212 {
2213 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
2214 const char *path = m_data.PeekCStr(name_offset);
2215 if (path)
2216 {
2217 FileSpec file_spec(path, false);
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002218 // Strip the path if there is @rpath, @executable, etc so we just use the basename
Greg Clayton9191db42013-10-21 18:40:51 +00002219 if (path[0] == '@')
2220 file_spec.GetDirectory().Clear();
Jim Inghamfbe0b9a2014-05-21 03:58:03 +00002221
2222 if (lc.cmd == LC_REEXPORT_DYLIB)
2223 {
2224 m_reexported_dylibs.AppendIfUnique(file_spec);
2225 }
Greg Clayton9191db42013-10-21 18:40:51 +00002226
2227 dylib_files.Append(file_spec);
2228 }
2229 }
2230 break;
2231
Charles Davis510938e2013-08-27 05:04:57 +00002232 case LC_FUNCTION_STARTS:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002233 function_starts_load_command.cmd = lc.cmd;
2234 function_starts_load_command.cmdsize = lc.cmdsize;
2235 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 +00002236 memset (&function_starts_load_command, 0, sizeof(function_starts_load_command));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002237 break;
2238
2239 default:
2240 break;
2241 }
2242 offset = cmd_offset + lc.cmdsize;
2243 }
2244
2245 if (symtab_load_command.cmd)
2246 {
2247 Symtab *symtab = m_symtab_ap.get();
2248 SectionList *section_list = GetSectionList();
2249 if (section_list == NULL)
2250 return 0;
2251
Greg Claytonc7bece562013-01-25 18:06:21 +00002252 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
2253 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002254 bool bit_width_32 = addr_byte_size == 4;
2255 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
2256
Greg Claytonc7bece562013-01-25 18:06:21 +00002257 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
2258 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
2259 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00002260 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00002261 DataExtractor dyld_trie_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002262
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002263 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
2264 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00002265 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
Greg Claytonfd814c52013-08-13 01:42:25 +00002266
2267 ProcessSP process_sp (m_process_wp.lock());
2268 Process *process = process_sp.get();
2269
Greg Clayton86eac942013-08-13 21:32:34 +00002270 uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete;
2271
Greg Clayton48672af2014-06-24 22:22:43 +00002272 if (process && m_header.filetype != llvm::MachO::MH_OBJECT)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002273 {
Greg Clayton4c82d422012-05-18 23:20:01 +00002274 Target &target = process->GetTarget();
Greg Claytonfd814c52013-08-13 01:42:25 +00002275
Greg Clayton86eac942013-08-13 21:32:34 +00002276 memory_module_load_level = target.GetMemoryModuleLoadLevel();
Greg Claytonfd814c52013-08-13 01:42:25 +00002277
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002278 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2279 // Reading mach file from memory in a process or core file...
2280
2281 if (linkedit_section_sp)
2282 {
2283 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
2284 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2285 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00002286 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00002287
2288 bool data_was_read = false;
2289
Todd Fiala013434e2014-07-09 01:29:05 +00002290#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molendaa3329782014-03-29 18:54:20 +00002291 if (m_header.flags & 0x80000000u && process->GetAddressByteSize() == sizeof (void*))
Greg Clayton77ccca72011-12-30 00:32:24 +00002292 {
Greg Clayton26b47e22012-04-18 05:19:20 +00002293 // This mach-o memory file is in the dyld shared cache. If this
2294 // program is not remote and this is iOS, then this process will
2295 // share the same shared cache as the process we are debugging and
2296 // we can read the entire __LINKEDIT from the address space in this
2297 // process. This is a needed optimization that is used for local iOS
2298 // debugging only since all shared libraries in the shared cache do
2299 // not have corresponding files that exist in the file system of the
2300 // device. They have been combined into a single file. This means we
2301 // always have to load these files from memory. All of the symbol and
2302 // string tables from all of the __LINKEDIT sections from the shared
2303 // libraries in the shared cache have been merged into a single large
2304 // symbol and string table. Reading all of this symbol and string table
2305 // data across can slow down debug launch times, so we optimize this by
2306 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00002307
2308 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
2309 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
2310 bool use_lldb_cache = true;
2311 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
2312 {
2313 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002314 ModuleSP module_sp (GetModule());
2315 if (module_sp)
2316 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
2317
Jason Molenda0e0954c2013-04-16 06:24:42 +00002318 }
2319
Greg Clayton26b47e22012-04-18 05:19:20 +00002320 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00002321 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00002322 {
2323 data_was_read = true;
2324 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00002325 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00002326 if (function_starts_load_command.cmd)
2327 {
2328 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2329 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
2330 }
2331 }
2332 }
2333#endif
2334
2335 if (!data_was_read)
2336 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002337 if (memory_module_load_level == eMemoryModuleLoadLevelComplete)
Jason Molendad34e6522013-02-05 22:31:24 +00002338 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002339 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
2340 if (nlist_data_sp)
2341 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2342 // Load strings individually from memory when loading from memory since shared cache
2343 // string tables contain strings for all symbols from all shared cached libraries
2344 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
2345 //if (strtab_data_sp)
2346 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
2347 if (m_dysymtab.nindirectsyms != 0)
2348 {
2349 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
2350 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
2351 if (indirect_syms_data_sp)
2352 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
2353 }
Jason Molendad34e6522013-02-05 22:31:24 +00002354 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002355
2356 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial)
Greg Clayton26b47e22012-04-18 05:19:20 +00002357 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002358 if (function_starts_load_command.cmd)
2359 {
2360 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2361 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
2362 if (func_start_data_sp)
2363 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
2364 }
Greg Clayton26b47e22012-04-18 05:19:20 +00002365 }
Greg Clayton77ccca72011-12-30 00:32:24 +00002366 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002367 }
2368 }
2369 else
2370 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002371 nlist_data.SetData (m_data,
2372 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002373 nlist_data_byte_size);
2374 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00002375 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002376 strtab_data_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00002377
2378 if (dyld_info.export_size > 0)
2379 {
2380 dyld_trie_data.SetData (m_data,
2381 dyld_info.export_off,
2382 dyld_info.export_size);
2383 }
2384
Jason Molendad34e6522013-02-05 22:31:24 +00002385 if (m_dysymtab.nindirectsyms != 0)
2386 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002387 indirect_symbol_index_data.SetData (m_data,
2388 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00002389 m_dysymtab.nindirectsyms * 4);
2390 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002391 if (function_starts_load_command.cmd)
2392 {
2393 function_starts_data.SetData (m_data,
2394 function_starts_load_command.dataoff,
2395 function_starts_load_command.datasize);
2396 }
2397 }
Greg Clayton77ccca72011-12-30 00:32:24 +00002398
Greg Clayton86eac942013-08-13 21:32:34 +00002399 if (nlist_data.GetByteSize() == 0 && memory_module_load_level == eMemoryModuleLoadLevelComplete)
2400 {
2401 if (log)
2402 module_sp->LogMessage(log, "failed to read nlist data");
2403 return 0;
2404 }
2405
2406
Greg Claytondebb8812012-05-25 17:04:00 +00002407 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2408 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00002409 {
Greg Claytondebb8812012-05-25 17:04:00 +00002410 if (process)
2411 {
2412 if (strtab_addr == LLDB_INVALID_ADDRESS)
2413 {
2414 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002415 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00002416 return 0;
2417 }
2418 }
2419 else
Greg Clayton4c82d422012-05-18 23:20:01 +00002420 {
2421 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002422 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00002423 return 0;
2424 }
2425 }
Greg Clayton4c82d422012-05-18 23:20:01 +00002426
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002427 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
2428 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
2429 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
2430 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
2431 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
2432 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
2433 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
2434 SectionSP eh_frame_section_sp;
2435 if (text_section_sp.get())
2436 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
2437 else
2438 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
2439
Charles Davis510938e2013-08-27 05:04:57 +00002440 const bool is_arm = (m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
Jason Molenda5635f772013-03-21 03:36:01 +00002441
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002442 // lldb works best if it knows the start address of all functions in a module.
Jason Molenda5635f772013-03-21 03:36:01 +00002443 // Linker symbols or debug info are normally the best source of information for start addr / size but
2444 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00002445 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00002446 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
2447 // binary, relative to the text section.
2448 // eh_frame - the eh_frame FDEs have the start addr & size of each function
2449 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
2450 // Binaries built to run on older releases may need to use eh_frame information.
2451
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002452 if (text_section_sp && function_starts_data.GetByteSize())
2453 {
2454 FunctionStarts::Entry function_start_entry;
2455 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00002456 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002457 function_start_entry.addr = text_section_sp->GetFileAddress();
2458 uint64_t delta;
2459 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
2460 {
2461 // Now append the current entry
2462 function_start_entry.addr += delta;
2463 function_starts.Append(function_start_entry);
2464 }
Jason Molendad63d3c72013-04-16 00:18:44 +00002465 }
Jason Molenda5635f772013-03-21 03:36:01 +00002466 else
2467 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00002468 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
2469 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
2470 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
2471 // the module.
2472 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00002473 {
2474 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
2475 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
2476 eh_frame.GetFunctionAddressAndSizeVector (functions);
2477 addr_t text_base_addr = text_section_sp->GetFileAddress();
2478 size_t count = functions.GetSize();
2479 for (size_t i = 0; i < count; ++i)
2480 {
2481 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
2482 if (func)
2483 {
2484 FunctionStarts::Entry function_start_entry;
2485 function_start_entry.addr = func->base - text_base_addr;
2486 function_starts.Append(function_start_entry);
2487 }
2488 }
2489 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002490 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002491
Greg Claytonc7bece562013-01-25 18:06:21 +00002492 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002493
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +00002494 const user_id_t TEXT_eh_frame_sectID =
2495 eh_frame_section_sp.get() ? eh_frame_section_sp->GetID()
2496 : static_cast<user_id_t>(NO_SECT);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002497
Greg Claytonc7bece562013-01-25 18:06:21 +00002498 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002499
2500 uint32_t N_SO_index = UINT32_MAX;
2501
2502 MachSymtabSectionInfo section_info (section_list);
2503 std::vector<uint32_t> N_FUN_indexes;
2504 std::vector<uint32_t> N_NSYM_indexes;
2505 std::vector<uint32_t> N_INCL_indexes;
2506 std::vector<uint32_t> N_BRAC_indexes;
2507 std::vector<uint32_t> N_COMM_indexes;
Greg Claytond81088c2014-01-16 01:38:29 +00002508 typedef std::multimap <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002509 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00002510 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002511 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2512 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00002513 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002514 // Any symbols that get merged into another will get an entry
2515 // in this map so we know
2516 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2517 uint32_t nlist_idx = 0;
2518 Symbol *symbol_ptr = NULL;
2519
2520 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00002521 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00002522 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00002523 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00002524 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002525
Jim Inghamea3ac272014-01-10 22:55:37 +00002526 std::vector<TrieEntryWithOffset> trie_entries;
2527 std::set<lldb::addr_t> resolver_addresses;
2528
2529 if (dyld_trie_data.GetByteSize() > 0)
2530 {
2531 std::vector<llvm::StringRef> nameSlices;
2532 ParseTrieEntries (dyld_trie_data,
2533 0,
2534 nameSlices,
2535 resolver_addresses,
2536 trie_entries);
2537
2538 ConstString text_segment_name ("__TEXT");
2539 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
2540 if (text_segment_sp)
2541 {
2542 const lldb::addr_t text_segment_file_addr = text_segment_sp->GetFileAddress();
2543 if (text_segment_file_addr != LLDB_INVALID_ADDRESS)
2544 {
2545 for (auto &e : trie_entries)
2546 e.entry.address += text_segment_file_addr;
2547 }
2548 }
2549 }
2550
Greg Claytonb65c6292015-02-20 22:20:05 +00002551 typedef std::set<ConstString> IndirectSymbols;
2552 IndirectSymbols indirect_symbol_names;
2553
Todd Fiala013434e2014-07-09 01:29:05 +00002554#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molendaa5609c82012-06-21 01:51:02 +00002555
2556 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
2557 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
2558 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
2559 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
2560 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
2561 // nlist parser to ignore all LOCAL symbols.
2562
2563 if (m_header.flags & 0x80000000u)
2564 {
2565 // Before we can start mapping the DSC, we need to make certain the target process is actually
2566 // using the cache we can find.
2567
Jason Molendaa5609c82012-06-21 01:51:02 +00002568 // Next we need to determine the correct path for the dyld shared cache.
2569
Greg Clayton7ab7f892014-05-29 21:33:45 +00002570 ArchSpec header_arch;
2571 GetArchitecture(header_arch);
Jason Molendaa5609c82012-06-21 01:51:02 +00002572 char dsc_path[PATH_MAX];
2573
2574 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00002575 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
2576 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00002577 header_arch.GetArchitectureName());
2578
2579 FileSpec dsc_filespec(dsc_path, false);
2580
2581 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molendad63d3c72013-04-16 00:18:44 +00002582 struct lldb_copy_dyld_cache_header_v0
Greg Clayton946f8902012-09-05 22:30:51 +00002583 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002584 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
2585 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
2586 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda4e7511e2013-03-06 23:19:17 +00002587 uint32_t imagesOffset;
2588 uint32_t imagesCount;
2589 uint64_t dyldBaseAddress;
2590 uint64_t codeSignatureOffset;
2591 uint64_t codeSignatureSize;
2592 uint64_t slideInfoOffset;
2593 uint64_t slideInfoSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00002594 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
2595 uint64_t localSymbolsSize; // size of local symbols information
2596 };
2597 struct lldb_copy_dyld_cache_header_v1
2598 {
2599 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
2600 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
2601 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
2602 uint32_t imagesOffset;
2603 uint32_t imagesCount;
2604 uint64_t dyldBaseAddress;
2605 uint64_t codeSignatureOffset;
2606 uint64_t codeSignatureSize;
2607 uint64_t slideInfoOffset;
2608 uint64_t slideInfoSize;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002609 uint64_t localSymbolsOffset;
2610 uint64_t localSymbolsSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00002611 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Clayton946f8902012-09-05 22:30:51 +00002612 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00002613
Jason Molendad63d3c72013-04-16 00:18:44 +00002614 struct lldb_copy_dyld_cache_mapping_info
2615 {
2616 uint64_t address;
2617 uint64_t size;
2618 uint64_t fileOffset;
2619 uint32_t maxProt;
2620 uint32_t initProt;
2621 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00002622
Greg Clayton946f8902012-09-05 22:30:51 +00002623 struct lldb_copy_dyld_cache_local_symbols_info
2624 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002625 uint32_t nlistOffset;
2626 uint32_t nlistCount;
2627 uint32_t stringsOffset;
2628 uint32_t stringsSize;
2629 uint32_t entriesOffset;
2630 uint32_t entriesCount;
Greg Clayton946f8902012-09-05 22:30:51 +00002631 };
2632 struct lldb_copy_dyld_cache_local_symbols_entry
2633 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002634 uint32_t dylibOffset;
2635 uint32_t nlistStartIndex;
2636 uint32_t nlistCount;
Greg Clayton946f8902012-09-05 22:30:51 +00002637 };
Jason Molendaa5609c82012-06-21 01:51:02 +00002638
Jason Molendaf8130862012-06-22 03:28:35 +00002639 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
2640 The dyld_cache_local_symbols_info structure gives us three things:
2641 1. The start and count of the nlist records in the dyld_shared_cache file
2642 2. The start and size of the strings for these nlist records
2643 3. The start and count of dyld_cache_local_symbols_entry entries
2644
2645 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
2646 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002647 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00002648 and the count of how many nlist records there are for this dylib/framework.
2649 */
2650
Jason Molendaa5609c82012-06-21 01:51:02 +00002651 // Process the dsc header to find the unmapped symbols
2652 //
2653 // Save some VM space, do not map the entire cache in one shot.
2654
Jason Molenda255f9bb2013-03-06 23:17:36 +00002655 DataBufferSP dsc_data_sp;
Greg Clayton736888c2015-02-23 23:47:09 +00002656 dsc_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
Jason Molenda255f9bb2013-03-06 23:17:36 +00002657
2658 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00002659 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002660 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002661
Jason Molenda255f9bb2013-03-06 23:17:36 +00002662 char version_str[17];
2663 int version = -1;
2664 lldb::offset_t offset = 0;
2665 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
2666 version_str[16] = '\0';
2667 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
2668 {
2669 int v;
2670 if (::sscanf (version_str + 6, "%d", &v) == 1)
2671 {
2672 version = v;
2673 }
2674 }
2675
Jason Molenda0e0954c2013-04-16 06:24:42 +00002676 UUID dsc_uuid;
2677 if (version >= 1)
2678 {
2679 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
2680 uint8_t uuid_bytes[sizeof (uuid_t)];
2681 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
2682 dsc_uuid.SetBytes (uuid_bytes);
2683 }
2684
2685 bool uuid_match = true;
2686 if (dsc_uuid.IsValid() && process)
2687 {
2688 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
2689
2690 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
2691 {
2692 // The on-disk dyld_shared_cache file is not the same as the one in this
2693 // process' memory, don't use it.
2694 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002695 ModuleSP module_sp (GetModule());
2696 if (module_sp)
2697 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 +00002698 }
2699 }
2700
Jason Molenda4e7511e2013-03-06 23:19:17 +00002701 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00002702
Jason Molendaa5609c82012-06-21 01:51:02 +00002703 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
2704
2705 // If the mappingOffset points to a location inside the header, we've
2706 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda0e0954c2013-04-16 06:24:42 +00002707 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendaa5609c82012-06-21 01:51:02 +00002708 {
2709
Greg Clayton736888c2015-02-23 23:47:09 +00002710 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
Jason Molenda255f9bb2013-03-06 23:17:36 +00002711 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
2712 offset = 0;
2713
2714 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
2715 // in the shared library cache need to be adjusted by an offset to match up with the
2716 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
2717 // recorded in mapping_offset_value.
2718 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
2719
2720 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00002721 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
2722 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
2723
Jason Molenda4e7511e2013-03-06 23:19:17 +00002724 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00002725 {
2726 // Map the local symbols
Greg Clayton736888c2015-02-23 23:47:09 +00002727 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00002728 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002729 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002730
2731 offset = 0;
2732
Greg Claytonb65c6292015-02-20 22:20:05 +00002733 typedef std::map<ConstString, uint16_t> UndefinedNameToDescMap;
2734 typedef std::map<uint32_t, ConstString> SymbolIndexToName;
2735 UndefinedNameToDescMap undefined_name_to_desc;
2736 SymbolIndexToName reexport_shlib_needs_fixup;
2737
2738
Jason Molendaa5609c82012-06-21 01:51:02 +00002739 // Read the local_symbols_infos struct in one shot
2740 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
2741 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
2742
Jason Molendaa5609c82012-06-21 01:51:02 +00002743 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
2744
Jason Molenda255f9bb2013-03-06 23:17:36 +00002745 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002746
2747 offset = local_symbols_info.entriesOffset;
2748 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
2749 {
2750 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
2751 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
2752 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
2753 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
2754
Jason Molenda4e7511e2013-03-06 23:19:17 +00002755 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00002756 {
2757 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
2758
2759 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
2760 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
2761 num_syms = symtab->GetNumSymbols();
2762
2763 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
2764 uint32_t string_table_offset = local_symbols_info.stringsOffset;
2765
Jason Molenda4e7511e2013-03-06 23:19:17 +00002766 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00002767 {
2768 /////////////////////////////
2769 {
2770 struct nlist_64 nlist;
2771 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2772 break;
2773
2774 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2775 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2776 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2777 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2778 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2779
2780 SymbolType type = eSymbolTypeInvalid;
2781 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2782
2783 if (symbol_name == NULL)
2784 {
2785 // No symbol should be NULL, even the symbols with no
2786 // string values should have an offset zero which points
2787 // to an empty C-string
2788 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002789 "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 +00002790 entry_index,
2791 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002792 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00002793 continue;
2794 }
2795 if (symbol_name[0] == '\0')
2796 symbol_name = NULL;
2797
2798 const char *symbol_name_non_abi_mangled = NULL;
2799
2800 SectionSP symbol_section;
2801 uint32_t symbol_byte_size = 0;
2802 bool add_nlist = true;
Charles Davis510938e2013-08-27 05:04:57 +00002803 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002804 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002805 bool is_gsym = false;
Greg Clayton60038be2015-02-14 00:51:13 +00002806 bool set_value = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002807
2808 assert (sym_idx < num_syms);
2809
2810 sym[sym_idx].SetDebug (is_debug);
2811
2812 if (is_debug)
2813 {
2814 switch (nlist.n_type)
2815 {
Charles Davis510938e2013-08-27 05:04:57 +00002816 case N_GSYM:
2817 // global symbol: name,,NO_SECT,type,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002818 // Sometimes the N_GSYM value contains the address.
2819
2820 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2821 // have the same address, but we want to ensure that we always find only the real symbol,
2822 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2823 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2824 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2825 // same address.
2826
Greg Clayton1e28adf2015-02-25 17:25:02 +00002827 is_gsym = true;
2828 sym[sym_idx].SetExternal(true);
2829
2830 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O')
2831 {
2832 llvm::StringRef symbol_name_ref(symbol_name);
2833 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2834 {
2835 symbol_name_non_abi_mangled = symbol_name + 1;
2836 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2837 type = eSymbolTypeObjCClass;
2838 demangled_is_synthesized = true;
2839
2840 }
2841 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2842 {
2843 symbol_name_non_abi_mangled = symbol_name + 1;
2844 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2845 type = eSymbolTypeObjCMetaClass;
2846 demangled_is_synthesized = true;
2847 }
2848 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2849 {
2850 symbol_name_non_abi_mangled = symbol_name + 1;
2851 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2852 type = eSymbolTypeObjCIVar;
2853 demangled_is_synthesized = true;
2854 }
2855 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002856 else
2857 {
Jason Molendaa5609c82012-06-21 01:51:02 +00002858 if (nlist.n_value != 0)
2859 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2860 type = eSymbolTypeData;
2861 }
2862 break;
2863
Charles Davis510938e2013-08-27 05:04:57 +00002864 case N_FNAME:
2865 // procedure name (f77 kludge): name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002866 type = eSymbolTypeCompiler;
2867 break;
2868
Charles Davis510938e2013-08-27 05:04:57 +00002869 case N_FUN:
2870 // procedure: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002871 if (symbol_name)
2872 {
2873 type = eSymbolTypeCode;
2874 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2875
Greg Claytond81088c2014-01-16 01:38:29 +00002876 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002877 // We use the current number of symbols in the symbol table in lieu of
2878 // using nlist_idx in case we ever start trimming entries out
2879 N_FUN_indexes.push_back(sym_idx);
2880 }
2881 else
2882 {
2883 type = eSymbolTypeCompiler;
2884
2885 if ( !N_FUN_indexes.empty() )
2886 {
2887 // Copy the size of the function into the original STAB entry so we don't have
2888 // to hunt for it later
2889 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2890 N_FUN_indexes.pop_back();
2891 // We don't really need the end function STAB as it contains the size which
2892 // we already placed with the original symbol, so don't add it if we want a
2893 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002894 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002895 }
2896 }
2897 break;
2898
Charles Davis510938e2013-08-27 05:04:57 +00002899 case N_STSYM:
2900 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00002901 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002902 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2903 type = eSymbolTypeData;
2904 break;
2905
Charles Davis510938e2013-08-27 05:04:57 +00002906 case N_LCSYM:
2907 // .lcomm symbol: name,,n_sect,type,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002908 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2909 type = eSymbolTypeCommonBlock;
2910 break;
2911
Charles Davis510938e2013-08-27 05:04:57 +00002912 case N_BNSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002913 // We use the current number of symbols in the symbol table in lieu of
2914 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00002915 // Skip these if we want minimal symbol tables
2916 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002917 break;
2918
Charles Davis510938e2013-08-27 05:04:57 +00002919 case N_ENSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002920 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2921 // so that we can always skip the entire symbol if we need to navigate
2922 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00002923 // Skip these if we want minimal symbol tables
2924 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002925 break;
2926
2927
Charles Davis510938e2013-08-27 05:04:57 +00002928 case N_OPT:
2929 // emitted with gcc2_compiled and in gcc source
Jason Molendaa5609c82012-06-21 01:51:02 +00002930 type = eSymbolTypeCompiler;
2931 break;
2932
Charles Davis510938e2013-08-27 05:04:57 +00002933 case N_RSYM:
2934 // register sym: name,,NO_SECT,type,register
Jason Molendaa5609c82012-06-21 01:51:02 +00002935 type = eSymbolTypeVariable;
2936 break;
2937
Charles Davis510938e2013-08-27 05:04:57 +00002938 case N_SLINE:
2939 // src line: 0,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002940 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2941 type = eSymbolTypeLineEntry;
2942 break;
2943
Charles Davis510938e2013-08-27 05:04:57 +00002944 case N_SSYM:
2945 // structure elt: name,,NO_SECT,type,struct_offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002946 type = eSymbolTypeVariableType;
2947 break;
2948
Charles Davis510938e2013-08-27 05:04:57 +00002949 case N_SO:
2950 // source file name
Jason Molendaa5609c82012-06-21 01:51:02 +00002951 type = eSymbolTypeSourceFile;
2952 if (symbol_name == NULL)
2953 {
Greg Clayton3046e662013-07-10 01:23:25 +00002954 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002955 if (N_SO_index != UINT32_MAX)
2956 {
2957 // Set the size of the N_SO to the terminating index of this N_SO
2958 // so that we can always skip the entire N_SO if we need to navigate
2959 // more quickly at the source level when parsing STABS
2960 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00002961 symbol_ptr->SetByteSize(sym_idx);
Jason Molendaa5609c82012-06-21 01:51:02 +00002962 symbol_ptr->SetSizeIsSibling(true);
2963 }
2964 N_NSYM_indexes.clear();
2965 N_INCL_indexes.clear();
2966 N_BRAC_indexes.clear();
2967 N_COMM_indexes.clear();
2968 N_FUN_indexes.clear();
2969 N_SO_index = UINT32_MAX;
2970 }
2971 else
2972 {
2973 // We use the current number of symbols in the symbol table in lieu of
2974 // using nlist_idx in case we ever start trimming entries out
2975 const bool N_SO_has_full_path = symbol_name[0] == '/';
2976 if (N_SO_has_full_path)
2977 {
Greg Clayton3046e662013-07-10 01:23:25 +00002978 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002979 {
2980 // We have two consecutive N_SO entries where the first contains a directory
2981 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00002982 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002983 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2984 add_nlist = false;
2985 }
2986 else
2987 {
2988 // This is the first entry in a N_SO that contains a directory or
2989 // a full path to the source file
2990 N_SO_index = sym_idx;
2991 }
2992 }
Greg Clayton3046e662013-07-10 01:23:25 +00002993 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002994 {
2995 // This is usually the second N_SO entry that contains just the filename,
2996 // so here we combine it with the first one if we are minimizing the symbol table
2997 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2998 if (so_path && so_path[0])
2999 {
3000 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00003001 const size_t double_slash_pos = full_so_path.find("//");
3002 if (double_slash_pos != std::string::npos)
3003 {
3004 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00003005 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Clayton0662d962012-09-07 20:29:13 +00003006 // and the second is the directory for the source file so you end up with
3007 // a path that looks like "/tmp/src//tmp/src/"
3008 FileSpec so_dir(so_path, false);
3009 if (!so_dir.Exists())
3010 {
3011 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3012 if (so_dir.Exists())
3013 {
3014 // Trim off the incorrect path
3015 full_so_path.erase(0, double_slash_pos + 1);
3016 }
3017 }
3018 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003019 if (*full_so_path.rbegin() != '/')
3020 full_so_path += '/';
3021 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00003022 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00003023 add_nlist = false;
3024 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3025 }
3026 }
Greg Clayton946f8902012-09-05 22:30:51 +00003027 else
3028 {
3029 // This could be a relative path to a N_SO
3030 N_SO_index = sym_idx;
3031 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003032 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003033 break;
3034
Charles Davis510938e2013-08-27 05:04:57 +00003035 case N_OSO:
3036 // object file name: name,,0,0,st_mtime
Jason Molendaa5609c82012-06-21 01:51:02 +00003037 type = eSymbolTypeObjectFile;
3038 break;
3039
Charles Davis510938e2013-08-27 05:04:57 +00003040 case N_LSYM:
3041 // local sym: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00003042 type = eSymbolTypeLocal;
3043 break;
3044
3045 //----------------------------------------------------------------------
3046 // INCL scopes
3047 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003048 case N_BINCL:
3049 // include file beginning: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00003050 // We use the current number of symbols in the symbol table in lieu of
3051 // using nlist_idx in case we ever start trimming entries out
3052 N_INCL_indexes.push_back(sym_idx);
3053 type = eSymbolTypeScopeBegin;
3054 break;
3055
Charles Davis510938e2013-08-27 05:04:57 +00003056 case N_EINCL:
3057 // include file end: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003058 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3059 // so that we can always skip the entire symbol if we need to navigate
3060 // more quickly at the source level when parsing STABS
3061 if ( !N_INCL_indexes.empty() )
3062 {
3063 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3064 symbol_ptr->SetByteSize(sym_idx + 1);
3065 symbol_ptr->SetSizeIsSibling(true);
3066 N_INCL_indexes.pop_back();
3067 }
3068 type = eSymbolTypeScopeEnd;
3069 break;
3070
Charles Davis510938e2013-08-27 05:04:57 +00003071 case N_SOL:
3072 // #included file name: name,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003073 type = eSymbolTypeHeaderFile;
3074
3075 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00003076 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00003077 break;
3078
Charles Davis510938e2013-08-27 05:04:57 +00003079 case N_PARAMS:
3080 // compiler parameters: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003081 type = eSymbolTypeCompiler;
3082 break;
3083
Charles Davis510938e2013-08-27 05:04:57 +00003084 case N_VERSION:
3085 // compiler version: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003086 type = eSymbolTypeCompiler;
3087 break;
3088
Charles Davis510938e2013-08-27 05:04:57 +00003089 case N_OLEVEL:
3090 // compiler -O level: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003091 type = eSymbolTypeCompiler;
3092 break;
3093
Charles Davis510938e2013-08-27 05:04:57 +00003094 case N_PSYM:
3095 // parameter: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00003096 type = eSymbolTypeVariable;
3097 break;
3098
Charles Davis510938e2013-08-27 05:04:57 +00003099 case N_ENTRY:
3100 // alternate entry: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003101 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3102 type = eSymbolTypeLineEntry;
3103 break;
3104
3105 //----------------------------------------------------------------------
3106 // Left and Right Braces
3107 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003108 case N_LBRAC:
3109 // left bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003110 // We use the current number of symbols in the symbol table in lieu of
3111 // using nlist_idx in case we ever start trimming entries out
3112 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3113 N_BRAC_indexes.push_back(sym_idx);
3114 type = eSymbolTypeScopeBegin;
3115 break;
3116
Charles Davis510938e2013-08-27 05:04:57 +00003117 case N_RBRAC:
3118 // right bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003119 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3120 // so that we can always skip the entire symbol if we need to navigate
3121 // more quickly at the source level when parsing STABS
3122 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3123 if ( !N_BRAC_indexes.empty() )
3124 {
3125 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3126 symbol_ptr->SetByteSize(sym_idx + 1);
3127 symbol_ptr->SetSizeIsSibling(true);
3128 N_BRAC_indexes.pop_back();
3129 }
3130 type = eSymbolTypeScopeEnd;
3131 break;
3132
Charles Davis510938e2013-08-27 05:04:57 +00003133 case N_EXCL:
3134 // deleted include file: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00003135 type = eSymbolTypeHeaderFile;
3136 break;
3137
3138 //----------------------------------------------------------------------
3139 // COMM scopes
3140 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003141 case N_BCOMM:
3142 // begin common: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003143 // We use the current number of symbols in the symbol table in lieu of
3144 // using nlist_idx in case we ever start trimming entries out
3145 type = eSymbolTypeScopeBegin;
3146 N_COMM_indexes.push_back(sym_idx);
3147 break;
3148
Charles Davis510938e2013-08-27 05:04:57 +00003149 case N_ECOML:
3150 // end common (local name): 0,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003151 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3152 // Fall through
3153
Charles Davis510938e2013-08-27 05:04:57 +00003154 case N_ECOMM:
3155 // end common: name,,n_sect,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003156 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3157 // so that we can always skip the entire symbol if we need to navigate
3158 // more quickly at the source level when parsing STABS
3159 if ( !N_COMM_indexes.empty() )
3160 {
3161 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3162 symbol_ptr->SetByteSize(sym_idx + 1);
3163 symbol_ptr->SetSizeIsSibling(true);
3164 N_COMM_indexes.pop_back();
3165 }
3166 type = eSymbolTypeScopeEnd;
3167 break;
3168
Charles Davis510938e2013-08-27 05:04:57 +00003169 case N_LENG:
3170 // second stab entry with length information
Jason Molendaa5609c82012-06-21 01:51:02 +00003171 type = eSymbolTypeAdditional;
3172 break;
3173
3174 default: break;
3175 }
3176 }
3177 else
3178 {
Charles Davis510938e2013-08-27 05:04:57 +00003179 //uint8_t n_pext = N_PEXT & nlist.n_type;
3180 uint8_t n_type = N_TYPE & nlist.n_type;
3181 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Jason Molendaa5609c82012-06-21 01:51:02 +00003182
3183 switch (n_type)
3184 {
Greg Clayton60038be2015-02-14 00:51:13 +00003185 case N_INDR:
3186 {
3187 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
3188 if (reexport_name_cstr && reexport_name_cstr[0])
3189 {
3190 type = eSymbolTypeReExported;
3191 ConstString reexport_name(reexport_name_cstr + ((reexport_name_cstr[0] == '_') ? 1 : 0));
3192 sym[sym_idx].SetReExportedSymbolName(reexport_name);
3193 set_value = false;
3194 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3195 indirect_symbol_names.insert(ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0)));
3196 }
3197 else
3198 type = eSymbolTypeUndefined;
3199 }
3200 break;
3201
Charles Davis510938e2013-08-27 05:04:57 +00003202 case N_UNDF:
Greg Clayton60038be2015-02-14 00:51:13 +00003203 {
3204 ConstString undefined_name(symbol_name + ((symbol_name[0] == '_') ? 1 : 0));
3205 undefined_name_to_desc[undefined_name] = nlist.n_desc;
3206 }
3207 // Fall through
3208 case N_PBUD:
Jason Molendaa5609c82012-06-21 01:51:02 +00003209 type = eSymbolTypeUndefined;
3210 break;
3211
Charles Davis510938e2013-08-27 05:04:57 +00003212 case N_ABS:
Jason Molendaa5609c82012-06-21 01:51:02 +00003213 type = eSymbolTypeAbsolute;
3214 break;
3215
Charles Davis510938e2013-08-27 05:04:57 +00003216 case N_SECT:
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003217 {
3218 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00003219
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003220 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00003221 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003222 // TODO: warn about this?
3223 add_nlist = false;
3224 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00003225 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003226
3227 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00003228 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003229 type = eSymbolTypeException;
3230 }
3231 else
3232 {
Charles Davis510938e2013-08-27 05:04:57 +00003233 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003234
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003235 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00003236 {
Charles Davis510938e2013-08-27 05:04:57 +00003237 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
3238 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
3239 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
3240 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3241 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3242 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3243 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3244 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3245 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
Charles Davis510938e2013-08-27 05:04:57 +00003246 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3247 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
3248 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
3249 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton38f9cc42014-06-16 22:53:16 +00003250 default:
3251 switch (symbol_section->GetType())
3252 {
3253 case lldb::eSectionTypeCode:
3254 type = eSymbolTypeCode;
3255 break;
3256 case eSectionTypeData:
3257 case eSectionTypeDataCString: // Inlined C string data
3258 case eSectionTypeDataCStringPointers: // Pointers to C string data
3259 case eSectionTypeDataSymbolAddress: // Address of a symbol in the symbol table
3260 case eSectionTypeData4:
3261 case eSectionTypeData8:
3262 case eSectionTypeData16:
Greg Clayton38f9cc42014-06-16 22:53:16 +00003263 type = eSymbolTypeData;
3264 break;
3265 default:
3266 break;
3267 }
3268 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00003269 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003270
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003271 if (type == eSymbolTypeInvalid)
3272 {
3273 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3274 if (symbol_section->IsDescendant (text_section_sp.get()))
3275 {
Charles Davis510938e2013-08-27 05:04:57 +00003276 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
3277 S_ATTR_SELF_MODIFYING_CODE |
3278 S_ATTR_SOME_INSTRUCTIONS))
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003279 type = eSymbolTypeData;
3280 else
3281 type = eSymbolTypeCode;
3282 }
3283 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00003284 {
3285 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
3286 {
3287 type = eSymbolTypeRuntime;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003288
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003289 if (symbol_name &&
3290 symbol_name[0] == '_' &&
3291 symbol_name[1] == 'O' &&
Jason Molendaa5609c82012-06-21 01:51:02 +00003292 symbol_name[2] == 'B')
3293 {
3294 llvm::StringRef symbol_name_ref(symbol_name);
Jason Molendaa5609c82012-06-21 01:51:02 +00003295 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3296 {
3297 symbol_name_non_abi_mangled = symbol_name + 1;
3298 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3299 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003300 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003301 }
3302 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3303 {
3304 symbol_name_non_abi_mangled = symbol_name + 1;
3305 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3306 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003307 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003308 }
3309 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3310 {
3311 symbol_name_non_abi_mangled = symbol_name + 1;
3312 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3313 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003314 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003315 }
3316 }
3317 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003318 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00003319 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003320 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00003321 }
3322 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003323 {
3324 type = eSymbolTypeData;
3325 }
3326 }
3327 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3328 {
3329 type = eSymbolTypeTrampoline;
3330 }
3331 else if (symbol_section->IsDescendant(objc_section_sp.get()))
3332 {
3333 type = eSymbolTypeRuntime;
3334 if (symbol_name && symbol_name[0] == '.')
3335 {
3336 llvm::StringRef symbol_name_ref(symbol_name);
3337 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3338 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00003339 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003340 symbol_name_non_abi_mangled = symbol_name;
3341 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3342 type = eSymbolTypeObjCClass;
3343 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003344 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003345 }
3346 }
3347 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003348 }
3349 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003350 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003351 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003352 }
3353
3354 if (add_nlist)
3355 {
3356 uint64_t symbol_value = nlist.n_value;
Jason Molendaa5609c82012-06-21 01:51:02 +00003357 if (symbol_name_non_abi_mangled)
3358 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00003359 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3360 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00003361 }
3362 else
3363 {
Greg Clayton3046e662013-07-10 01:23:25 +00003364 bool symbol_name_is_mangled = false;
3365
Jason Molendaa5609c82012-06-21 01:51:02 +00003366 if (symbol_name && symbol_name[0] == '_')
3367 {
3368 symbol_name_is_mangled = symbol_name[1] == '_';
3369 symbol_name++; // Skip the leading underscore
3370 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003371
Jason Molendaa5609c82012-06-21 01:51:02 +00003372 if (symbol_name)
3373 {
Greg Claytondacc4a92013-05-14 22:19:37 +00003374 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00003375 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00003376 if (is_gsym && is_debug)
3377 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
Jason Molendaa5609c82012-06-21 01:51:02 +00003378 }
3379 }
3380 if (symbol_section)
3381 {
3382 const addr_t section_file_addr = symbol_section->GetFileAddress();
3383 if (symbol_byte_size == 0 && function_starts_count > 0)
3384 {
3385 addr_t symbol_lookup_file_addr = nlist.n_value;
3386 // Do an exact address match for non-ARM addresses, else get the closest since
3387 // the symbol might be a thumb symbol which has an address with bit zero set
3388 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3389 if (is_arm && func_start_entry)
3390 {
3391 // Verify that the function start address is the symbol address (ARM)
3392 // or the symbol address + 1 (thumb)
3393 if (func_start_entry->addr != symbol_lookup_file_addr &&
3394 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3395 {
3396 // Not the right entry, NULL it out...
3397 func_start_entry = NULL;
3398 }
3399 }
3400 if (func_start_entry)
3401 {
3402 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003403
Jason Molendaa5609c82012-06-21 01:51:02 +00003404 addr_t symbol_file_addr = func_start_entry->addr;
3405 uint32_t symbol_flags = 0;
3406 if (is_arm)
3407 {
3408 if (symbol_file_addr & 1)
3409 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3410 symbol_file_addr &= 0xfffffffffffffffeull;
3411 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003412
Jason Molendaa5609c82012-06-21 01:51:02 +00003413 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3414 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3415 if (next_func_start_entry)
3416 {
3417 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3418 // Be sure the clear the Thumb address bit when we calculate the size
3419 // from the current and next address
3420 if (is_arm)
3421 next_symbol_file_addr &= 0xfffffffffffffffeull;
3422 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3423 }
3424 else
3425 {
3426 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3427 }
3428 }
3429 }
3430 symbol_value -= section_file_addr;
3431 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003432
Greg Claytondacc4a92013-05-14 22:19:37 +00003433 if (is_debug == false)
3434 {
3435 if (type == eSymbolTypeCode)
3436 {
3437 // See if we can find a N_FUN entry for any code symbols.
3438 // If we do find a match, and the name matches, then we
3439 // can merge the two into just the function symbol to avoid
3440 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003441 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3442 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3443 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00003444 {
Greg Claytond81088c2014-01-16 01:38:29 +00003445 bool found_it = false;
3446 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00003447 {
Greg Claytond81088c2014-01-16 01:38:29 +00003448 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3449 {
3450 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3451 // We just need the flags from the linker symbol, so put these flags
3452 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3453 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3454 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3455 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3456 sym[pos->second].SetType (eSymbolTypeResolver);
3457 sym[sym_idx].Clear();
3458 found_it = true;
3459 break;
3460 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003461 }
Greg Claytond81088c2014-01-16 01:38:29 +00003462 if (found_it)
3463 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00003464 }
Jim Inghamea3ac272014-01-10 22:55:37 +00003465 else
3466 {
Greg Claytond81088c2014-01-16 01:38:29 +00003467 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00003468 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00003469 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003470 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00003471 else if (type == eSymbolTypeData ||
3472 type == eSymbolTypeObjCClass ||
3473 type == eSymbolTypeObjCMetaClass ||
3474 type == eSymbolTypeObjCIVar )
Greg Claytondacc4a92013-05-14 22:19:37 +00003475 {
3476 // See if we can find a N_STSYM entry for any data symbols.
3477 // If we do find a match, and the name matches, then we
3478 // can merge the two into just the Static symbol to avoid
3479 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003480 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3481 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
3482 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00003483 {
Greg Claytond81088c2014-01-16 01:38:29 +00003484 bool found_it = false;
3485 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00003486 {
Greg Claytond81088c2014-01-16 01:38:29 +00003487 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3488 {
3489 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3490 // We just need the flags from the linker symbol, so put these flags
3491 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3492 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3493 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3494 sym[sym_idx].Clear();
3495 found_it = true;
3496 break;
3497 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003498 }
Greg Claytond81088c2014-01-16 01:38:29 +00003499 if (found_it)
3500 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00003501 }
3502 else
3503 {
3504 // Combine N_GSYM stab entries with the non stab symbol
Greg Clayton3046e662013-07-10 01:23:25 +00003505 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 +00003506 if (pos != N_GSYM_name_to_sym_idx.end())
3507 {
3508 const uint32_t GSYM_sym_idx = pos->second;
3509 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3510 // Copy the address, because often the N_GSYM address has an invalid address of zero
3511 // when the global is a common symbol
3512 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3513 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3514 // We just need the flags from the linker symbol, so put these flags
Greg Clayton1e28adf2015-02-25 17:25:02 +00003515 // into the N_GSYM flags to avoid duplicate symbols in the symbol table
Greg Claytondacc4a92013-05-14 22:19:37 +00003516 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3517 sym[sym_idx].Clear();
3518 continue;
3519 }
3520 }
3521 }
3522 }
3523
Jason Molendaa5609c82012-06-21 01:51:02 +00003524 sym[sym_idx].SetID (nlist_idx);
3525 sym[sym_idx].SetType (type);
Greg Clayton60038be2015-02-14 00:51:13 +00003526 if (set_value)
3527 {
3528 sym[sym_idx].GetAddress().SetSection (symbol_section);
3529 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3530 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003531 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003532
Jason Molendaa5609c82012-06-21 01:51:02 +00003533 if (symbol_byte_size > 0)
3534 sym[sym_idx].SetByteSize(symbol_byte_size);
3535
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003536 if (demangled_is_synthesized)
3537 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00003538 ++sym_idx;
3539 }
3540 else
3541 {
3542 sym[sym_idx].Clear();
3543 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003544
Jason Molendaa5609c82012-06-21 01:51:02 +00003545 }
3546 /////////////////////////////
3547 }
3548 break; // No more entries to consider
3549 }
3550 }
Greg Clayton60038be2015-02-14 00:51:13 +00003551
3552 for (const auto &pos :reexport_shlib_needs_fixup)
3553 {
3554 const auto undef_pos = undefined_name_to_desc.find(pos.second);
3555 if (undef_pos != undefined_name_to_desc.end())
3556 {
3557 const uint8_t dylib_ordinal = llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
3558 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
3559 sym[pos.first].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(dylib_ordinal-1));
3560 }
3561 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003562 }
3563 }
3564 }
3565 }
3566 }
3567
3568 // Must reset this in case it was mutated above!
3569 nlist_data_offset = 0;
3570#endif
Jim Inghamea3ac272014-01-10 22:55:37 +00003571
Greg Claytonfd814c52013-08-13 01:42:25 +00003572 if (nlist_data.GetByteSize() > 0)
Jason Molendaa5609c82012-06-21 01:51:02 +00003573 {
Jason Molendaa5609c82012-06-21 01:51:02 +00003574
Greg Claytonfd814c52013-08-13 01:42:25 +00003575 // If the sym array was not created while parsing the DSC unmapped
3576 // symbols, create it now.
3577 if (sym == NULL)
Greg Clayton4c82d422012-05-18 23:20:01 +00003578 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003579 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
3580 num_syms = symtab->GetNumSymbols();
3581 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003582
Greg Claytonfd814c52013-08-13 01:42:25 +00003583 if (unmapped_local_symbols_found)
3584 {
3585 assert(m_dysymtab.ilocalsym == 0);
3586 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
3587 nlist_idx = m_dysymtab.nlocalsym;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003588 }
Greg Claytondebb8812012-05-25 17:04:00 +00003589 else
3590 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003591 nlist_idx = 0;
Greg Claytondebb8812012-05-25 17:04:00 +00003592 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003593
Greg Clayton60038be2015-02-14 00:51:13 +00003594 typedef std::map<ConstString, uint16_t> UndefinedNameToDescMap;
3595 typedef std::map<uint32_t, ConstString> SymbolIndexToName;
3596 UndefinedNameToDescMap undefined_name_to_desc;
3597 SymbolIndexToName reexport_shlib_needs_fixup;
Greg Claytonfd814c52013-08-13 01:42:25 +00003598 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003599 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003600 struct nlist_64 nlist;
3601 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
3602 break;
3603
3604 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
3605 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
3606 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
3607 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
3608 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
3609
3610 SymbolType type = eSymbolTypeInvalid;
3611 const char *symbol_name = NULL;
3612
3613 if (have_strtab_data)
Greg Clayton77ccca72011-12-30 00:32:24 +00003614 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003615 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003616
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003617 if (symbol_name == NULL)
3618 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003619 // No symbol should be NULL, even the symbols with no
3620 // string values should have an offset zero which points
3621 // to an empty C-string
3622 Host::SystemLog (Host::eSystemLogError,
3623 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
3624 nlist_idx,
3625 nlist.n_strx,
3626 module_sp->GetFileSpec().GetPath().c_str());
3627 continue;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003628 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003629 if (symbol_name[0] == '\0')
3630 symbol_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003631 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003632 else
3633 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003634 const addr_t str_addr = strtab_addr + nlist.n_strx;
3635 Error str_error;
3636 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
3637 symbol_name = memory_symbol_name.c_str();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003638 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003639 const char *symbol_name_non_abi_mangled = NULL;
3640
3641 SectionSP symbol_section;
3642 lldb::addr_t symbol_byte_size = 0;
3643 bool add_nlist = true;
3644 bool is_gsym = false;
Charles Davis510938e2013-08-27 05:04:57 +00003645 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00003646 bool demangled_is_synthesized = false;
Greg Clayton60038be2015-02-14 00:51:13 +00003647 bool set_value = true;
Greg Claytonfd814c52013-08-13 01:42:25 +00003648 assert (sym_idx < num_syms);
3649
3650 sym[sym_idx].SetDebug (is_debug);
3651
3652 if (is_debug)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003653 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003654 switch (nlist.n_type)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003655 {
Charles Davis510938e2013-08-27 05:04:57 +00003656 case N_GSYM:
3657 // global symbol: name,,NO_SECT,type,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003658 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003659
Greg Claytonfd814c52013-08-13 01:42:25 +00003660 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
3661 // have the same address, but we want to ensure that we always find only the real symbol,
3662 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
3663 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
3664 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
3665 // same address.
Greg Clayton1e28adf2015-02-25 17:25:02 +00003666 is_gsym = true;
3667 sym[sym_idx].SetExternal(true);
Greg Clayton29e08cb2012-03-14 01:53:24 +00003668
Greg Clayton1e28adf2015-02-25 17:25:02 +00003669 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O')
3670 {
3671 llvm::StringRef symbol_name_ref(symbol_name);
3672 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3673 {
3674 symbol_name_non_abi_mangled = symbol_name + 1;
3675 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3676 type = eSymbolTypeObjCClass;
3677 demangled_is_synthesized = true;
3678
3679 }
3680 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3681 {
3682 symbol_name_non_abi_mangled = symbol_name + 1;
3683 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3684 type = eSymbolTypeObjCMetaClass;
3685 demangled_is_synthesized = true;
3686 }
3687 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3688 {
3689 symbol_name_non_abi_mangled = symbol_name + 1;
3690 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3691 type = eSymbolTypeObjCIVar;
3692 demangled_is_synthesized = true;
3693 }
3694 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003695 else
3696 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003697 if (nlist.n_value != 0)
3698 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3699 type = eSymbolTypeData;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003700 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003701 break;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003702
Charles Davis510938e2013-08-27 05:04:57 +00003703 case N_FNAME:
3704 // procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003705 type = eSymbolTypeCompiler;
3706 break;
3707
Charles Davis510938e2013-08-27 05:04:57 +00003708 case N_FUN:
3709 // procedure: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003710 if (symbol_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00003711 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003712 type = eSymbolTypeCode;
3713 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3714
Greg Claytond81088c2014-01-16 01:38:29 +00003715 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003716 // We use the current number of symbols in the symbol table in lieu of
3717 // using nlist_idx in case we ever start trimming entries out
3718 N_FUN_indexes.push_back(sym_idx);
Greg Claytondacc4a92013-05-14 22:19:37 +00003719 }
3720 else
3721 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003722 type = eSymbolTypeCompiler;
3723
3724 if ( !N_FUN_indexes.empty() )
Greg Claytondacc4a92013-05-14 22:19:37 +00003725 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003726 // Copy the size of the function into the original STAB entry so we don't have
3727 // to hunt for it later
3728 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
3729 N_FUN_indexes.pop_back();
3730 // We don't really need the end function STAB as it contains the size which
3731 // we already placed with the original symbol, so don't add it if we want a
3732 // minimal symbol table
3733 add_nlist = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00003734 }
3735 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003736 break;
3737
Charles Davis510938e2013-08-27 05:04:57 +00003738 case N_STSYM:
3739 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00003740 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003741 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3742 type = eSymbolTypeData;
3743 break;
3744
Charles Davis510938e2013-08-27 05:04:57 +00003745 case N_LCSYM:
3746 // .lcomm symbol: name,,n_sect,type,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003747 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3748 type = eSymbolTypeCommonBlock;
3749 break;
3750
Charles Davis510938e2013-08-27 05:04:57 +00003751 case N_BNSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003752 // We use the current number of symbols in the symbol table in lieu of
3753 // using nlist_idx in case we ever start trimming entries out
3754 // Skip these if we want minimal symbol tables
3755 add_nlist = false;
3756 break;
3757
Charles Davis510938e2013-08-27 05:04:57 +00003758 case N_ENSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003759 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
3760 // so that we can always skip the entire symbol if we need to navigate
3761 // more quickly at the source level when parsing STABS
3762 // Skip these if we want minimal symbol tables
3763 add_nlist = false;
3764 break;
3765
3766
Charles Davis510938e2013-08-27 05:04:57 +00003767 case N_OPT:
3768 // emitted with gcc2_compiled and in gcc source
Greg Claytonfd814c52013-08-13 01:42:25 +00003769 type = eSymbolTypeCompiler;
3770 break;
3771
Charles Davis510938e2013-08-27 05:04:57 +00003772 case N_RSYM:
3773 // register sym: name,,NO_SECT,type,register
Greg Claytonfd814c52013-08-13 01:42:25 +00003774 type = eSymbolTypeVariable;
3775 break;
3776
Charles Davis510938e2013-08-27 05:04:57 +00003777 case N_SLINE:
3778 // src line: 0,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003779 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3780 type = eSymbolTypeLineEntry;
3781 break;
3782
Charles Davis510938e2013-08-27 05:04:57 +00003783 case N_SSYM:
3784 // structure elt: name,,NO_SECT,type,struct_offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003785 type = eSymbolTypeVariableType;
3786 break;
3787
Charles Davis510938e2013-08-27 05:04:57 +00003788 case N_SO:
3789 // source file name
Greg Claytonfd814c52013-08-13 01:42:25 +00003790 type = eSymbolTypeSourceFile;
3791 if (symbol_name == NULL)
3792 {
3793 add_nlist = false;
3794 if (N_SO_index != UINT32_MAX)
3795 {
3796 // Set the size of the N_SO to the terminating index of this N_SO
3797 // so that we can always skip the entire N_SO if we need to navigate
3798 // more quickly at the source level when parsing STABS
3799 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
3800 symbol_ptr->SetByteSize(sym_idx);
3801 symbol_ptr->SetSizeIsSibling(true);
3802 }
3803 N_NSYM_indexes.clear();
3804 N_INCL_indexes.clear();
3805 N_BRAC_indexes.clear();
3806 N_COMM_indexes.clear();
3807 N_FUN_indexes.clear();
3808 N_SO_index = UINT32_MAX;
3809 }
3810 else
3811 {
3812 // We use the current number of symbols in the symbol table in lieu of
3813 // using nlist_idx in case we ever start trimming entries out
3814 const bool N_SO_has_full_path = symbol_name[0] == '/';
3815 if (N_SO_has_full_path)
3816 {
3817 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3818 {
3819 // We have two consecutive N_SO entries where the first contains a directory
3820 // and the second contains a full path.
3821 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
3822 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3823 add_nlist = false;
3824 }
3825 else
3826 {
3827 // This is the first entry in a N_SO that contains a directory or
3828 // a full path to the source file
3829 N_SO_index = sym_idx;
3830 }
3831 }
3832 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3833 {
3834 // This is usually the second N_SO entry that contains just the filename,
3835 // so here we combine it with the first one if we are minimizing the symbol table
3836 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
3837 if (so_path && so_path[0])
3838 {
3839 std::string full_so_path (so_path);
3840 const size_t double_slash_pos = full_so_path.find("//");
3841 if (double_slash_pos != std::string::npos)
3842 {
3843 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00003844 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Claytonfd814c52013-08-13 01:42:25 +00003845 // and the second is the directory for the source file so you end up with
3846 // a path that looks like "/tmp/src//tmp/src/"
3847 FileSpec so_dir(so_path, false);
3848 if (!so_dir.Exists())
3849 {
3850 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3851 if (so_dir.Exists())
3852 {
3853 // Trim off the incorrect path
3854 full_so_path.erase(0, double_slash_pos + 1);
3855 }
3856 }
3857 }
3858 if (*full_so_path.rbegin() != '/')
3859 full_so_path += '/';
3860 full_so_path += symbol_name;
3861 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
3862 add_nlist = false;
3863 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3864 }
3865 }
3866 else
3867 {
3868 // This could be a relative path to a N_SO
3869 N_SO_index = sym_idx;
3870 }
3871 }
3872
3873 break;
3874
Charles Davis510938e2013-08-27 05:04:57 +00003875 case N_OSO:
3876 // object file name: name,,0,0,st_mtime
Greg Claytonfd814c52013-08-13 01:42:25 +00003877 type = eSymbolTypeObjectFile;
3878 break;
3879
Charles Davis510938e2013-08-27 05:04:57 +00003880 case N_LSYM:
3881 // local sym: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003882 type = eSymbolTypeLocal;
3883 break;
3884
3885 //----------------------------------------------------------------------
3886 // INCL scopes
3887 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003888 case N_BINCL:
3889 // include file beginning: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00003890 // We use the current number of symbols in the symbol table in lieu of
3891 // using nlist_idx in case we ever start trimming entries out
3892 N_INCL_indexes.push_back(sym_idx);
3893 type = eSymbolTypeScopeBegin;
3894 break;
3895
Charles Davis510938e2013-08-27 05:04:57 +00003896 case N_EINCL:
3897 // include file end: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003898 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3899 // so that we can always skip the entire symbol if we need to navigate
3900 // more quickly at the source level when parsing STABS
3901 if ( !N_INCL_indexes.empty() )
3902 {
3903 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3904 symbol_ptr->SetByteSize(sym_idx + 1);
3905 symbol_ptr->SetSizeIsSibling(true);
3906 N_INCL_indexes.pop_back();
3907 }
3908 type = eSymbolTypeScopeEnd;
3909 break;
3910
Charles Davis510938e2013-08-27 05:04:57 +00003911 case N_SOL:
3912 // #included file name: name,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003913 type = eSymbolTypeHeaderFile;
3914
3915 // We currently don't use the header files on darwin
3916 add_nlist = false;
3917 break;
3918
Charles Davis510938e2013-08-27 05:04:57 +00003919 case N_PARAMS:
3920 // compiler parameters: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003921 type = eSymbolTypeCompiler;
3922 break;
3923
Charles Davis510938e2013-08-27 05:04:57 +00003924 case N_VERSION:
3925 // compiler version: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003926 type = eSymbolTypeCompiler;
3927 break;
3928
Charles Davis510938e2013-08-27 05:04:57 +00003929 case N_OLEVEL:
3930 // compiler -O level: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003931 type = eSymbolTypeCompiler;
3932 break;
3933
Charles Davis510938e2013-08-27 05:04:57 +00003934 case N_PSYM:
3935 // parameter: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003936 type = eSymbolTypeVariable;
3937 break;
3938
Charles Davis510938e2013-08-27 05:04:57 +00003939 case N_ENTRY:
3940 // alternate entry: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003941 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3942 type = eSymbolTypeLineEntry;
3943 break;
3944
3945 //----------------------------------------------------------------------
3946 // Left and Right Braces
3947 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003948 case N_LBRAC:
3949 // left bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003950 // We use the current number of symbols in the symbol table in lieu of
3951 // using nlist_idx in case we ever start trimming entries out
3952 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3953 N_BRAC_indexes.push_back(sym_idx);
3954 type = eSymbolTypeScopeBegin;
3955 break;
3956
Charles Davis510938e2013-08-27 05:04:57 +00003957 case N_RBRAC:
3958 // right bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003959 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3960 // so that we can always skip the entire symbol if we need to navigate
3961 // more quickly at the source level when parsing STABS
3962 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3963 if ( !N_BRAC_indexes.empty() )
3964 {
3965 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3966 symbol_ptr->SetByteSize(sym_idx + 1);
3967 symbol_ptr->SetSizeIsSibling(true);
3968 N_BRAC_indexes.pop_back();
3969 }
3970 type = eSymbolTypeScopeEnd;
3971 break;
3972
Charles Davis510938e2013-08-27 05:04:57 +00003973 case N_EXCL:
3974 // deleted include file: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00003975 type = eSymbolTypeHeaderFile;
3976 break;
3977
3978 //----------------------------------------------------------------------
3979 // COMM scopes
3980 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003981 case N_BCOMM:
3982 // begin common: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003983 // We use the current number of symbols in the symbol table in lieu of
3984 // using nlist_idx in case we ever start trimming entries out
3985 type = eSymbolTypeScopeBegin;
3986 N_COMM_indexes.push_back(sym_idx);
3987 break;
3988
Charles Davis510938e2013-08-27 05:04:57 +00003989 case N_ECOML:
3990 // end common (local name): 0,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003991 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3992 // Fall through
3993
Charles Davis510938e2013-08-27 05:04:57 +00003994 case N_ECOMM:
3995 // end common: name,,n_sect,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003996 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3997 // so that we can always skip the entire symbol if we need to navigate
3998 // more quickly at the source level when parsing STABS
3999 if ( !N_COMM_indexes.empty() )
4000 {
4001 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
4002 symbol_ptr->SetByteSize(sym_idx + 1);
4003 symbol_ptr->SetSizeIsSibling(true);
4004 N_COMM_indexes.pop_back();
4005 }
4006 type = eSymbolTypeScopeEnd;
4007 break;
4008
Charles Davis510938e2013-08-27 05:04:57 +00004009 case N_LENG:
4010 // second stab entry with length information
Greg Claytonfd814c52013-08-13 01:42:25 +00004011 type = eSymbolTypeAdditional;
4012 break;
4013
4014 default: break;
4015 }
4016 }
4017 else
4018 {
Charles Davis510938e2013-08-27 05:04:57 +00004019 //uint8_t n_pext = N_PEXT & nlist.n_type;
4020 uint8_t n_type = N_TYPE & nlist.n_type;
4021 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00004022
4023 switch (n_type)
4024 {
Greg Clayton60038be2015-02-14 00:51:13 +00004025 case N_INDR:
4026 {
4027 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
4028 if (reexport_name_cstr && reexport_name_cstr[0])
4029 {
4030 type = eSymbolTypeReExported;
4031 ConstString reexport_name(reexport_name_cstr + ((reexport_name_cstr[0] == '_') ? 1 : 0));
4032 sym[sym_idx].SetReExportedSymbolName(reexport_name);
4033 set_value = false;
4034 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
4035 indirect_symbol_names.insert(ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0)));
4036 }
4037 else
4038 type = eSymbolTypeUndefined;
4039 }
4040 break;
4041
Charles Davis510938e2013-08-27 05:04:57 +00004042 case N_UNDF:
Greg Clayton60038be2015-02-14 00:51:13 +00004043 {
4044 ConstString undefined_name(symbol_name + ((symbol_name[0] == '_') ? 1 : 0));
4045 undefined_name_to_desc[undefined_name] = nlist.n_desc;
4046 }
4047 // Fall through
4048 case N_PBUD:
Greg Claytonfd814c52013-08-13 01:42:25 +00004049 type = eSymbolTypeUndefined;
4050 break;
4051
Charles Davis510938e2013-08-27 05:04:57 +00004052 case N_ABS:
Greg Claytonfd814c52013-08-13 01:42:25 +00004053 type = eSymbolTypeAbsolute;
4054 break;
4055
Charles Davis510938e2013-08-27 05:04:57 +00004056 case N_SECT:
Greg Claytonfd814c52013-08-13 01:42:25 +00004057 {
4058 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4059
4060 if (!symbol_section)
4061 {
4062 // TODO: warn about this?
4063 add_nlist = false;
4064 break;
4065 }
4066
4067 if (TEXT_eh_frame_sectID == nlist.n_sect)
4068 {
4069 type = eSymbolTypeException;
4070 }
4071 else
4072 {
Charles Davis510938e2013-08-27 05:04:57 +00004073 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Greg Claytonfd814c52013-08-13 01:42:25 +00004074
4075 switch (section_type)
4076 {
Charles Davis510938e2013-08-27 05:04:57 +00004077 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
4078 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
4079 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
4080 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
4081 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
4082 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
4083 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
4084 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
4085 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
Charles Davis510938e2013-08-27 05:04:57 +00004086 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
4087 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
4088 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
4089 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton38f9cc42014-06-16 22:53:16 +00004090 default:
4091 switch (symbol_section->GetType())
4092 {
4093 case lldb::eSectionTypeCode:
4094 type = eSymbolTypeCode;
4095 break;
4096 case eSectionTypeData:
4097 case eSectionTypeDataCString: // Inlined C string data
4098 case eSectionTypeDataCStringPointers: // Pointers to C string data
4099 case eSectionTypeDataSymbolAddress: // Address of a symbol in the symbol table
4100 case eSectionTypeData4:
4101 case eSectionTypeData8:
4102 case eSectionTypeData16:
Greg Clayton38f9cc42014-06-16 22:53:16 +00004103 type = eSymbolTypeData;
4104 break;
4105 default:
4106 break;
4107 }
4108 break;
Greg Claytonfd814c52013-08-13 01:42:25 +00004109 }
4110
4111 if (type == eSymbolTypeInvalid)
4112 {
4113 const char *symbol_sect_name = symbol_section->GetName().AsCString();
4114 if (symbol_section->IsDescendant (text_section_sp.get()))
4115 {
Charles Davis510938e2013-08-27 05:04:57 +00004116 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
4117 S_ATTR_SELF_MODIFYING_CODE |
4118 S_ATTR_SOME_INSTRUCTIONS))
Greg Claytonfd814c52013-08-13 01:42:25 +00004119 type = eSymbolTypeData;
4120 else
4121 type = eSymbolTypeCode;
4122 }
4123 else
4124 if (symbol_section->IsDescendant(data_section_sp.get()))
4125 {
4126 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
4127 {
4128 type = eSymbolTypeRuntime;
4129
4130 if (symbol_name &&
4131 symbol_name[0] == '_' &&
4132 symbol_name[1] == 'O' &&
4133 symbol_name[2] == 'B')
4134 {
4135 llvm::StringRef symbol_name_ref(symbol_name);
Greg Claytonfd814c52013-08-13 01:42:25 +00004136 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
4137 {
4138 symbol_name_non_abi_mangled = symbol_name + 1;
4139 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
4140 type = eSymbolTypeObjCClass;
4141 demangled_is_synthesized = true;
4142 }
4143 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
4144 {
4145 symbol_name_non_abi_mangled = symbol_name + 1;
4146 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
4147 type = eSymbolTypeObjCMetaClass;
4148 demangled_is_synthesized = true;
4149 }
4150 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
4151 {
4152 symbol_name_non_abi_mangled = symbol_name + 1;
4153 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
4154 type = eSymbolTypeObjCIVar;
4155 demangled_is_synthesized = true;
4156 }
4157 }
4158 }
4159 else
4160 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
4161 {
4162 type = eSymbolTypeException;
4163 }
4164 else
4165 {
4166 type = eSymbolTypeData;
4167 }
4168 }
4169 else
4170 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
4171 {
4172 type = eSymbolTypeTrampoline;
4173 }
4174 else
4175 if (symbol_section->IsDescendant(objc_section_sp.get()))
4176 {
4177 type = eSymbolTypeRuntime;
4178 if (symbol_name && symbol_name[0] == '.')
4179 {
4180 llvm::StringRef symbol_name_ref(symbol_name);
4181 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
4182 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
4183 {
4184 symbol_name_non_abi_mangled = symbol_name;
4185 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
4186 type = eSymbolTypeObjCClass;
4187 demangled_is_synthesized = true;
4188 }
4189 }
4190 }
4191 }
4192 }
4193 }
4194 break;
Greg Claytondacc4a92013-05-14 22:19:37 +00004195 }
4196 }
4197
Greg Claytonfd814c52013-08-13 01:42:25 +00004198 if (add_nlist)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004199 {
Greg Claytonfd814c52013-08-13 01:42:25 +00004200 uint64_t symbol_value = nlist.n_value;
4201
4202 if (symbol_name_non_abi_mangled)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004203 {
Greg Claytonfd814c52013-08-13 01:42:25 +00004204 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
4205 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
4206 }
4207 else
4208 {
4209 bool symbol_name_is_mangled = false;
4210
4211 if (symbol_name && symbol_name[0] == '_')
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004212 {
Greg Claytonfd814c52013-08-13 01:42:25 +00004213 symbol_name_is_mangled = symbol_name[1] == '_';
4214 symbol_name++; // Skip the leading underscore
4215 }
4216
4217 if (symbol_name)
4218 {
4219 ConstString const_symbol_name(symbol_name);
4220 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Claytonfd814c52013-08-13 01:42:25 +00004221 }
4222 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00004223
4224 if (is_gsym)
4225 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
4226
Greg Claytonfd814c52013-08-13 01:42:25 +00004227 if (symbol_section)
4228 {
4229 const addr_t section_file_addr = symbol_section->GetFileAddress();
4230 if (symbol_byte_size == 0 && function_starts_count > 0)
4231 {
4232 addr_t symbol_lookup_file_addr = nlist.n_value;
4233 // Do an exact address match for non-ARM addresses, else get the closest since
4234 // the symbol might be a thumb symbol which has an address with bit zero set
4235 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
4236 if (is_arm && func_start_entry)
4237 {
4238 // Verify that the function start address is the symbol address (ARM)
4239 // or the symbol address + 1 (thumb)
4240 if (func_start_entry->addr != symbol_lookup_file_addr &&
4241 func_start_entry->addr != (symbol_lookup_file_addr + 1))
4242 {
4243 // Not the right entry, NULL it out...
4244 func_start_entry = NULL;
4245 }
4246 }
4247 if (func_start_entry)
4248 {
4249 func_start_entry->data = true;
4250
4251 addr_t symbol_file_addr = func_start_entry->addr;
4252 if (is_arm)
4253 symbol_file_addr &= 0xfffffffffffffffeull;
4254
4255 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
4256 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
4257 if (next_func_start_entry)
4258 {
4259 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4260 // Be sure the clear the Thumb address bit when we calculate the size
4261 // from the current and next address
4262 if (is_arm)
4263 next_symbol_file_addr &= 0xfffffffffffffffeull;
4264 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
4265 }
4266 else
4267 {
4268 symbol_byte_size = section_end_file_addr - symbol_file_addr;
4269 }
4270 }
4271 }
4272 symbol_value -= section_file_addr;
4273 }
4274
4275 if (is_debug == false)
4276 {
4277 if (type == eSymbolTypeCode)
4278 {
4279 // See if we can find a N_FUN entry for any code symbols.
4280 // If we do find a match, and the name matches, then we
4281 // can merge the two into just the function symbol to avoid
4282 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00004283 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
4284 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
4285 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00004286 {
Greg Claytond81088c2014-01-16 01:38:29 +00004287 bool found_it = false;
4288 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00004289 {
Greg Claytond81088c2014-01-16 01:38:29 +00004290 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
4291 {
4292 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4293 // We just need the flags from the linker symbol, so put these flags
4294 // into the N_FUN flags to avoid duplicate symbols in the symbol table
4295 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4296 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4297 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
4298 sym[pos->second].SetType (eSymbolTypeResolver);
4299 sym[sym_idx].Clear();
4300 found_it = true;
4301 break;
4302 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004303 }
Greg Claytond81088c2014-01-16 01:38:29 +00004304 if (found_it)
4305 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00004306 }
Jim Inghamea3ac272014-01-10 22:55:37 +00004307 else
4308 {
4309 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00004310 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00004311 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004312 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00004313 else if (type == eSymbolTypeData ||
4314 type == eSymbolTypeObjCClass ||
4315 type == eSymbolTypeObjCMetaClass ||
4316 type == eSymbolTypeObjCIVar )
Greg Claytonfd814c52013-08-13 01:42:25 +00004317 {
4318 // See if we can find a N_STSYM entry for any data symbols.
4319 // If we do find a match, and the name matches, then we
4320 // can merge the two into just the Static symbol to avoid
4321 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00004322 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
4323 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
4324 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00004325 {
Greg Claytond81088c2014-01-16 01:38:29 +00004326 bool found_it = false;
4327 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00004328 {
Greg Claytond81088c2014-01-16 01:38:29 +00004329 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
4330 {
4331 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4332 // We just need the flags from the linker symbol, so put these flags
4333 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
4334 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4335 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4336 sym[sym_idx].Clear();
4337 found_it = true;
4338 break;
4339 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004340 }
Greg Claytond81088c2014-01-16 01:38:29 +00004341 if (found_it)
4342 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00004343 }
4344 else
4345 {
4346 // Combine N_GSYM stab entries with the non stab symbol
4347 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
4348 if (pos != N_GSYM_name_to_sym_idx.end())
4349 {
4350 const uint32_t GSYM_sym_idx = pos->second;
4351 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
4352 // Copy the address, because often the N_GSYM address has an invalid address of zero
4353 // when the global is a common symbol
4354 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
4355 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
4356 // We just need the flags from the linker symbol, so put these flags
Greg Clayton1e28adf2015-02-25 17:25:02 +00004357 // into the N_GSYM flags to avoid duplicate symbols in the symbol table
Greg Claytonfd814c52013-08-13 01:42:25 +00004358 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4359 sym[sym_idx].Clear();
4360 continue;
4361 }
4362 }
4363 }
4364 }
4365
4366 sym[sym_idx].SetID (nlist_idx);
4367 sym[sym_idx].SetType (type);
Greg Clayton60038be2015-02-14 00:51:13 +00004368 if (set_value)
4369 {
4370 sym[sym_idx].GetAddress().SetSection (symbol_section);
4371 sym[sym_idx].GetAddress().SetOffset (symbol_value);
4372 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004373 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4374
4375 if (symbol_byte_size > 0)
4376 sym[sym_idx].SetByteSize(symbol_byte_size);
4377
4378 if (demangled_is_synthesized)
4379 sym[sym_idx].SetDemangledNameIsSynthesized(true);
4380
4381 ++sym_idx;
4382 }
4383 else
4384 {
4385 sym[sym_idx].Clear();
4386 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004387 }
Greg Clayton60038be2015-02-14 00:51:13 +00004388
4389 for (const auto &pos :reexport_shlib_needs_fixup)
4390 {
4391 const auto undef_pos = undefined_name_to_desc.find(pos.second);
4392 if (undef_pos != undefined_name_to_desc.end())
4393 {
4394 const uint8_t dylib_ordinal = llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
4395 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
4396 sym[pos.first].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(dylib_ordinal-1));
4397 }
4398 }
4399
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004400 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004401
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004402 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
4403
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004404 if (function_starts_count > 0)
4405 {
4406 char synthetic_function_symbol[PATH_MAX];
4407 uint32_t num_synthetic_function_symbols = 0;
4408 for (i=0; i<function_starts_count; ++i)
4409 {
4410 if (function_starts.GetEntryRef (i).data == false)
4411 ++num_synthetic_function_symbols;
4412 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004413
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004414 if (num_synthetic_function_symbols > 0)
4415 {
4416 if (num_syms < sym_idx + num_synthetic_function_symbols)
4417 {
4418 num_syms = sym_idx + num_synthetic_function_symbols;
4419 sym = symtab->Resize (num_syms);
4420 }
4421 uint32_t synthetic_function_symbol_idx = 0;
4422 for (i=0; i<function_starts_count; ++i)
4423 {
4424 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
4425 if (func_start_entry->data == false)
4426 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004427 addr_t symbol_file_addr = func_start_entry->addr;
4428 uint32_t symbol_flags = 0;
4429 if (is_arm)
4430 {
4431 if (symbol_file_addr & 1)
4432 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
4433 symbol_file_addr &= 0xfffffffffffffffeull;
4434 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004435 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004436 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004437 {
4438 SectionSP symbol_section (symbol_addr.GetSection());
4439 uint32_t symbol_byte_size = 0;
4440 if (symbol_section)
4441 {
4442 const addr_t section_file_addr = symbol_section->GetFileAddress();
4443 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
4444 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
4445 if (next_func_start_entry)
4446 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004447 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4448 if (is_arm)
4449 next_symbol_file_addr &= 0xfffffffffffffffeull;
4450 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 +00004451 }
4452 else
4453 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004454 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004455 }
4456 snprintf (synthetic_function_symbol,
4457 sizeof(synthetic_function_symbol),
4458 "___lldb_unnamed_function%u$$%s",
4459 ++synthetic_function_symbol_idx,
4460 module_sp->GetFileSpec().GetFilename().GetCString());
4461 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Clayton037520e2012-07-18 23:18:10 +00004462 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004463 sym[sym_idx].SetType (eSymbolTypeCode);
4464 sym[sym_idx].SetIsSynthetic (true);
4465 sym[sym_idx].GetAddress() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004466 if (symbol_flags)
4467 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004468 if (symbol_byte_size)
4469 sym[sym_idx].SetByteSize (symbol_byte_size);
4470 ++sym_idx;
4471 }
4472 }
4473 }
4474 }
4475 }
4476 }
4477
4478 // Trim our symbols down to just what we ended up with after
4479 // removing any symbols.
4480 if (sym_idx < num_syms)
4481 {
4482 num_syms = sym_idx;
4483 sym = symtab->Resize (num_syms);
4484 }
4485
4486 // Now synthesize indirect symbols
4487 if (m_dysymtab.nindirectsyms != 0)
4488 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004489 if (indirect_symbol_index_data.GetByteSize())
4490 {
4491 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
4492
4493 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
4494 {
Charles Davis510938e2013-08-27 05:04:57 +00004495 if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) == S_SYMBOL_STUBS)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004496 {
4497 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
4498 if (symbol_stub_byte_size == 0)
4499 continue;
4500
4501 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
4502
4503 if (num_symbol_stubs == 0)
4504 continue;
4505
4506 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
4507 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
4508 {
4509 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
4510 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 +00004511 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004512 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
4513 {
4514 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Charles Davis510938e2013-08-27 05:04:57 +00004515 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004516 continue;
4517
4518 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
4519 Symbol *stub_symbol = NULL;
4520 if (index_pos != end_index_pos)
4521 {
4522 // We have a remapping from the original nlist index to
4523 // a current symbol index, so just look this up by index
4524 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
4525 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004526 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004527 {
4528 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00004529 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004530 // S_SYMBOL_STUBS
4531 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
4532 }
4533
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004534 if (stub_symbol)
4535 {
4536 Address so_addr(symbol_stub_addr, section_list);
4537
4538 if (stub_symbol->GetType() == eSymbolTypeUndefined)
4539 {
4540 // Change the external symbol into a trampoline that makes sense
4541 // These symbols were N_UNDF N_EXT, and are useless to us, so we
4542 // can re-use them so we don't have to make up a synthetic symbol
4543 // for no good reason.
Greg Clayton9191db42013-10-21 18:40:51 +00004544 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4545 stub_symbol->SetType (eSymbolTypeTrampoline);
4546 else
4547 stub_symbol->SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004548 stub_symbol->SetExternal (false);
4549 stub_symbol->GetAddress() = so_addr;
4550 stub_symbol->SetByteSize (symbol_stub_byte_size);
4551 }
4552 else
4553 {
4554 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00004555 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004556 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00004557 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004558 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00004559 stub_symbol = NULL; // this pointer no longer valid
4560 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004561 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00004562 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton9191db42013-10-21 18:40:51 +00004563 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4564 sym[sym_idx].SetType (eSymbolTypeTrampoline);
4565 else
4566 sym[sym_idx].SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004567 sym[sym_idx].SetIsSynthetic (true);
4568 sym[sym_idx].GetAddress() = so_addr;
4569 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
4570 ++sym_idx;
4571 }
4572 }
Greg Clayton3f839a32012-09-05 01:38:55 +00004573 else
4574 {
4575 if (log)
4576 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
4577 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004578 }
4579 }
4580 }
4581 }
4582 }
4583 }
Greg Clayton9191db42013-10-21 18:40:51 +00004584
4585
4586 if (!trie_entries.empty())
4587 {
4588 for (const auto &e : trie_entries)
4589 {
4590 if (e.entry.import_name)
4591 {
Greg Clayton60038be2015-02-14 00:51:13 +00004592 // Only add indirect symbols from the Trie entries if we
4593 // didn't have a N_INDR nlist entry for this already
4594 if (indirect_symbol_names.find(e.entry.name) == indirect_symbol_names.end())
Greg Clayton9191db42013-10-21 18:40:51 +00004595 {
Greg Clayton60038be2015-02-14 00:51:13 +00004596 // Make a synthetic symbol to describe re-exported symbol.
4597 if (sym_idx >= num_syms)
4598 sym = symtab->Resize (++num_syms);
4599 sym[sym_idx].SetID (synthetic_sym_id++);
4600 sym[sym_idx].GetMangled() = Mangled(e.entry.name);
4601 sym[sym_idx].SetType (eSymbolTypeReExported);
4602 sym[sym_idx].SetIsSynthetic (true);
4603 sym[sym_idx].SetReExportedSymbolName(e.entry.import_name);
4604 if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize())
4605 {
4606 sym[sym_idx].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(e.entry.other-1));
4607 }
4608 ++sym_idx;
Greg Clayton9191db42013-10-21 18:40:51 +00004609 }
Greg Clayton9191db42013-10-21 18:40:51 +00004610 }
4611 }
4612 }
4613
4614
Greg Clayton3046e662013-07-10 01:23:25 +00004615
4616// StreamFile s(stdout, false);
4617// s.Printf ("Symbol table before CalculateSymbolSizes():\n");
4618// symtab->Dump(&s, NULL, eSortOrderNone);
4619 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
4620 symtab->CalculateSymbolSizes();
4621
4622// s.Printf ("Symbol table after CalculateSymbolSizes():\n");
4623// symtab->Dump(&s, NULL, eSortOrderNone);
4624
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004625 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004626 }
4627 return 0;
4628}
4629
4630
4631void
4632ObjectFileMachO::Dump (Stream *s)
4633{
Greg Claytona1743492012-03-13 23:14:29 +00004634 ModuleSP module_sp(GetModule());
4635 if (module_sp)
4636 {
4637 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004638 s->Printf("%p: ", static_cast<void*>(this));
Greg Claytona1743492012-03-13 23:14:29 +00004639 s->Indent();
Charles Davis510938e2013-08-27 05:04:57 +00004640 if (m_header.magic == MH_MAGIC_64 || m_header.magic == MH_CIGAM_64)
Greg Claytona1743492012-03-13 23:14:29 +00004641 s->PutCString("ObjectFileMachO64");
4642 else
4643 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004644
Greg Clayton7ab7f892014-05-29 21:33:45 +00004645 ArchSpec header_arch;
4646 GetArchitecture(header_arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004647
Greg Claytona1743492012-03-13 23:14:29 +00004648 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004649
Greg Clayton3046e662013-07-10 01:23:25 +00004650 SectionList *sections = GetSectionList();
4651 if (sections)
4652 sections->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004653
Greg Claytona1743492012-03-13 23:14:29 +00004654 if (m_symtab_ap.get())
4655 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
4656 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004657}
4658
Greg Claytonf4d6de62013-04-24 22:29:28 +00004659bool
4660ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
4661 const lldb_private::DataExtractor &data,
4662 lldb::offset_t lc_offset,
4663 lldb_private::UUID& uuid)
4664{
4665 uint32_t i;
4666 struct uuid_command load_cmd;
4667
4668 lldb::offset_t offset = lc_offset;
4669 for (i=0; i<header.ncmds; ++i)
4670 {
4671 const lldb::offset_t cmd_offset = offset;
4672 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4673 break;
4674
Charles Davis510938e2013-08-27 05:04:57 +00004675 if (load_cmd.cmd == LC_UUID)
Greg Claytonf4d6de62013-04-24 22:29:28 +00004676 {
4677 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
4678
4679 if (uuid_bytes)
4680 {
4681 // OpenCL on Mac OS X uses the same UUID for each of its object files.
4682 // We pretend these object files have no UUID to prevent crashing.
4683
4684 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
4685 0x3b, 0xa8,
4686 0x4b, 0x16,
4687 0xb6, 0xa4,
4688 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
4689
4690 if (!memcmp(uuid_bytes, opencl_uuid, 16))
4691 return false;
4692
4693 uuid.SetBytes (uuid_bytes);
4694 return true;
4695 }
4696 return false;
4697 }
4698 offset = cmd_offset + load_cmd.cmdsize;
4699 }
4700 return false;
4701}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004702
Greg Clayton7ab7f892014-05-29 21:33:45 +00004703
4704bool
4705ObjectFileMachO::GetArchitecture (const llvm::MachO::mach_header &header,
4706 const lldb_private::DataExtractor &data,
4707 lldb::offset_t lc_offset,
4708 ArchSpec &arch)
4709{
4710 arch.SetArchitecture (eArchTypeMachO, header.cputype, header.cpusubtype);
4711
4712 if (arch.IsValid())
4713 {
4714 llvm::Triple &triple = arch.GetTriple();
4715 if (header.filetype == MH_PRELOAD)
4716 {
4717 // Set OS to "unknown" - this is a standalone binary with no dyld et al
4718 triple.setOS(llvm::Triple::UnknownOS);
4719 return true;
4720 }
4721 else
4722 {
4723 struct load_command load_cmd;
4724
4725 lldb::offset_t offset = lc_offset;
4726 for (uint32_t i=0; i<header.ncmds; ++i)
4727 {
4728 const lldb::offset_t cmd_offset = offset;
4729 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4730 break;
4731
4732 switch (load_cmd.cmd)
4733 {
4734 case LC_VERSION_MIN_IPHONEOS:
4735 triple.setOS (llvm::Triple::IOS);
4736 return true;
4737
4738 case LC_VERSION_MIN_MACOSX:
4739 triple.setOS (llvm::Triple::MacOSX);
4740 return true;
4741
4742 default:
4743 break;
4744 }
4745
4746 offset = cmd_offset + load_cmd.cmdsize;
4747 }
4748
Greg Claytona3a6c122014-07-29 18:04:57 +00004749 // Only set the OS to iOS for ARM, we don't want to set it for x86 and x86_64.
4750 // We do this because we now have MacOSX or iOS as the OS value for x86 and
4751 // x86_64 for normal desktop (MacOSX) and simulator (iOS) binaries. And if
4752 // we compare a "x86_64-apple-ios" to a "x86_64-apple-" triple, it will say
4753 // it is compatible (because the OS is unspecified in the second one and will
4754 // match anything in the first
Greg Clayton7ab7f892014-05-29 21:33:45 +00004755 if (header.cputype == CPU_TYPE_ARM || header.cputype == CPU_TYPE_ARM64)
4756 triple.setOS (llvm::Triple::IOS);
Greg Clayton7ab7f892014-05-29 21:33:45 +00004757 }
4758 }
4759 return arch.IsValid();
4760}
4761
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004762bool
Greg Clayton60830262011-02-04 18:53:10 +00004763ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004764{
Greg Claytona1743492012-03-13 23:14:29 +00004765 ModuleSP module_sp(GetModule());
4766 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004767 {
Greg Claytona1743492012-03-13 23:14:29 +00004768 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004769 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00004770 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004771 }
4772 return false;
4773}
4774
4775
4776uint32_t
4777ObjectFileMachO::GetDependentModules (FileSpecList& files)
4778{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004779 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00004780 ModuleSP module_sp(GetModule());
4781 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004782 {
Greg Claytona1743492012-03-13 23:14:29 +00004783 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4784 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004785 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004786 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 +00004787 uint32_t i;
4788 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004789 {
Greg Claytona1743492012-03-13 23:14:29 +00004790 const uint32_t cmd_offset = offset;
4791 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4792 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004793
Greg Claytona1743492012-03-13 23:14:29 +00004794 switch (load_cmd.cmd)
4795 {
Charles Davis510938e2013-08-27 05:04:57 +00004796 case LC_LOAD_DYLIB:
4797 case LC_LOAD_WEAK_DYLIB:
4798 case LC_REEXPORT_DYLIB:
4799 case LC_LOAD_DYLINKER:
4800 case LC_LOADFVMLIB:
4801 case LC_LOAD_UPWARD_DYLIB:
Greg Claytona1743492012-03-13 23:14:29 +00004802 {
4803 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
4804 const char *path = m_data.PeekCStr(name_offset);
4805 // Skip any path that starts with '@' since these are usually:
4806 // @executable_path/.../file
4807 // @rpath/.../file
4808 if (path && path[0] != '@')
4809 {
4810 FileSpec file_spec(path, resolve_path);
4811 if (files.AppendIfUnique(file_spec))
4812 count++;
4813 }
4814 }
4815 break;
4816
4817 default:
4818 break;
4819 }
4820 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004821 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004822 }
4823 return count;
4824}
4825
Jim Ingham672e6f52011-03-07 23:44:08 +00004826lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00004827ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00004828{
4829 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
4830 // is initialized to an invalid address, so we can just return that.
4831 // 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 +00004832
Jim Ingham672e6f52011-03-07 23:44:08 +00004833 if (!IsExecutable() || m_entry_point_address.IsValid())
4834 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004835
4836 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00004837 // /usr/include/mach-o.h, but it is basically:
4838 //
4839 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
4840 // uint32_t count - this is the count of longs in the thread state data
4841 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
4842 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00004843 //
Jim Ingham672e6f52011-03-07 23:44:08 +00004844 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
4845 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
4846 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
4847 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
4848 //
4849 // For now we hard-code the offsets and flavors we need:
4850 //
4851 //
4852
Greg Claytona1743492012-03-13 23:14:29 +00004853 ModuleSP module_sp(GetModule());
4854 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00004855 {
Greg Claytona1743492012-03-13 23:14:29 +00004856 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4857 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004858 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004859 uint32_t i;
4860 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
4861 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004862
Greg Claytona1743492012-03-13 23:14:29 +00004863 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00004864 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004865 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004866 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4867 break;
4868
4869 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00004870 {
Charles Davis510938e2013-08-27 05:04:57 +00004871 case LC_UNIXTHREAD:
4872 case LC_THREAD:
Jim Ingham672e6f52011-03-07 23:44:08 +00004873 {
Greg Claytona1743492012-03-13 23:14:29 +00004874 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00004875 {
Greg Claytona1743492012-03-13 23:14:29 +00004876 uint32_t flavor = m_data.GetU32(&offset);
4877 uint32_t count = m_data.GetU32(&offset);
4878 if (count == 0)
4879 {
4880 // We've gotten off somehow, log and exit;
4881 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00004882 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004883
Greg Claytona1743492012-03-13 23:14:29 +00004884 switch (m_header.cputype)
4885 {
Charles Davis510938e2013-08-27 05:04:57 +00004886 case llvm::MachO::CPU_TYPE_ARM:
Greg Claytona1743492012-03-13 23:14:29 +00004887 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
4888 {
4889 offset += 60; // This is the offset of pc in the GPR thread state data structure.
4890 start_address = m_data.GetU32(&offset);
4891 done = true;
4892 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004893 break;
Jason Molendaa3329782014-03-29 18:54:20 +00004894 case llvm::MachO::CPU_TYPE_ARM64:
4895 if (flavor == 6) // ARM_THREAD_STATE64 from mach/arm/thread_status.h
4896 {
4897 offset += 256; // This is the offset of pc in the GPR thread state data structure.
4898 start_address = m_data.GetU64(&offset);
4899 done = true;
4900 }
4901 break;
Charles Davis510938e2013-08-27 05:04:57 +00004902 case llvm::MachO::CPU_TYPE_I386:
Greg Claytona1743492012-03-13 23:14:29 +00004903 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
4904 {
4905 offset += 40; // This is the offset of eip in the GPR thread state data structure.
4906 start_address = m_data.GetU32(&offset);
4907 done = true;
4908 }
4909 break;
Charles Davis510938e2013-08-27 05:04:57 +00004910 case llvm::MachO::CPU_TYPE_X86_64:
Greg Claytona1743492012-03-13 23:14:29 +00004911 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
4912 {
4913 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
4914 start_address = m_data.GetU64(&offset);
4915 done = true;
4916 }
4917 break;
4918 default:
4919 return m_entry_point_address;
4920 }
4921 // Haven't found the GPR flavor yet, skip over the data for this flavor:
4922 if (done)
4923 break;
4924 offset += count * 4;
4925 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004926 }
Greg Claytona1743492012-03-13 23:14:29 +00004927 break;
Charles Davis510938e2013-08-27 05:04:57 +00004928 case LC_MAIN:
Sean Callanan226b70c2012-03-08 02:39:03 +00004929 {
Greg Claytona1743492012-03-13 23:14:29 +00004930 ConstString text_segment_name ("__TEXT");
4931 uint64_t entryoffset = m_data.GetU64(&offset);
4932 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
4933 if (text_segment_sp)
4934 {
4935 done = true;
4936 start_address = text_segment_sp->GetFileAddress() + entryoffset;
4937 }
Sean Callanan226b70c2012-03-08 02:39:03 +00004938 }
Greg Claytona1743492012-03-13 23:14:29 +00004939
4940 default:
4941 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00004942 }
Greg Claytona1743492012-03-13 23:14:29 +00004943 if (done)
4944 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00004945
Greg Claytona1743492012-03-13 23:14:29 +00004946 // Go to the next load command:
4947 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00004948 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004949
Greg Claytona1743492012-03-13 23:14:29 +00004950 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00004951 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004952 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00004953 // of this ObjectFile:
4954 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00004955 {
Greg Claytona1743492012-03-13 23:14:29 +00004956 m_entry_point_address.Clear();
4957 }
4958 }
4959 else
4960 {
4961 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
4962 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00004963
Greg Claytona1743492012-03-13 23:14:29 +00004964 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00004965
Greg Claytona1743492012-03-13 23:14:29 +00004966 if (module_sp)
4967 {
4968 SymbolContextList contexts;
4969 SymbolContext context;
4970 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
4971 {
4972 if (contexts.GetContextAtIndex(0, context))
4973 m_entry_point_address = context.symbol->GetAddress();
4974 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004975 }
4976 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004977 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004978
Jim Ingham672e6f52011-03-07 23:44:08 +00004979 return m_entry_point_address;
4980
4981}
4982
Greg Claytonc9660542012-02-05 02:38:54 +00004983lldb_private::Address
4984ObjectFileMachO::GetHeaderAddress ()
4985{
4986 lldb_private::Address header_addr;
4987 SectionList *section_list = GetSectionList();
4988 if (section_list)
4989 {
4990 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
4991 if (text_segment_sp)
4992 {
Greg Claytone72dfb32012-02-24 01:59:29 +00004993 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00004994 header_addr.SetOffset (0);
4995 }
4996 }
4997 return header_addr;
4998}
4999
Greg Claytonc3776bf2012-02-09 06:16:32 +00005000uint32_t
5001ObjectFileMachO::GetNumThreadContexts ()
5002{
Greg Claytona1743492012-03-13 23:14:29 +00005003 ModuleSP module_sp(GetModule());
5004 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005005 {
Greg Claytona1743492012-03-13 23:14:29 +00005006 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5007 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005008 {
Greg Claytona1743492012-03-13 23:14:29 +00005009 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00005010 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00005011 FileRangeArray::Entry file_range;
5012 thread_command thread_cmd;
5013 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005014 {
Greg Claytona1743492012-03-13 23:14:29 +00005015 const uint32_t cmd_offset = offset;
5016 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
5017 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005018
Charles Davis510938e2013-08-27 05:04:57 +00005019 if (thread_cmd.cmd == LC_THREAD)
Greg Claytona1743492012-03-13 23:14:29 +00005020 {
5021 file_range.SetRangeBase (offset);
5022 file_range.SetByteSize (thread_cmd.cmdsize - 8);
5023 m_thread_context_offsets.Append (file_range);
5024 }
5025 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00005026 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00005027 }
5028 }
5029 return m_thread_context_offsets.GetSize();
5030}
5031
5032lldb::RegisterContextSP
5033ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
5034{
Greg Claytonc3776bf2012-02-09 06:16:32 +00005035 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00005036
Greg Claytona1743492012-03-13 23:14:29 +00005037 ModuleSP module_sp(GetModule());
5038 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005039 {
Greg Claytona1743492012-03-13 23:14:29 +00005040 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5041 if (!m_thread_context_offsets_valid)
5042 GetNumThreadContexts ();
5043
5044 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00005045 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00005046 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00005047
5048 DataExtractor data (m_data,
5049 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00005050 thread_context_file_range->GetByteSize());
5051
5052 switch (m_header.cputype)
5053 {
Jason Molendaa3329782014-03-29 18:54:20 +00005054 case llvm::MachO::CPU_TYPE_ARM64:
5055 reg_ctx_sp.reset (new RegisterContextDarwin_arm64_Mach (thread, data));
5056 break;
5057
Charles Davis510938e2013-08-27 05:04:57 +00005058 case llvm::MachO::CPU_TYPE_ARM:
Jim Ingham28eb5712012-10-12 17:34:26 +00005059 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
5060 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005061
Charles Davis510938e2013-08-27 05:04:57 +00005062 case llvm::MachO::CPU_TYPE_I386:
Jim Ingham28eb5712012-10-12 17:34:26 +00005063 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
5064 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005065
Charles Davis510938e2013-08-27 05:04:57 +00005066 case llvm::MachO::CPU_TYPE_X86_64:
Jim Ingham28eb5712012-10-12 17:34:26 +00005067 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
5068 break;
5069 }
Greg Claytona1743492012-03-13 23:14:29 +00005070 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00005071 }
5072 return reg_ctx_sp;
5073}
5074
Greg Claytonc9660542012-02-05 02:38:54 +00005075
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005076ObjectFile::Type
5077ObjectFileMachO::CalculateType()
5078{
5079 switch (m_header.filetype)
5080 {
Charles Davis510938e2013-08-27 05:04:57 +00005081 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005082 if (GetAddressByteSize () == 4)
5083 {
5084 // 32 bit kexts are just object files, but they do have a valid
5085 // UUID load command.
5086 UUID uuid;
5087 if (GetUUID(&uuid))
5088 {
5089 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00005090 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005091 // "OSKextGetCurrentIdentifier" as this is required of kexts
5092 if (m_strata == eStrataInvalid)
5093 m_strata = eStrataKernel;
5094 return eTypeSharedLibrary;
5095 }
5096 }
5097 return eTypeObjectFile;
5098
Charles Davis510938e2013-08-27 05:04:57 +00005099 case MH_EXECUTE: return eTypeExecutable; // 0x2u
5100 case MH_FVMLIB: return eTypeSharedLibrary; // 0x3u
5101 case MH_CORE: return eTypeCoreFile; // 0x4u
5102 case MH_PRELOAD: return eTypeSharedLibrary; // 0x5u
5103 case MH_DYLIB: return eTypeSharedLibrary; // 0x6u
5104 case MH_DYLINKER: return eTypeDynamicLinker; // 0x7u
5105 case MH_BUNDLE: return eTypeSharedLibrary; // 0x8u
5106 case MH_DYLIB_STUB: return eTypeStubLibrary; // 0x9u
5107 case MH_DSYM: return eTypeDebugInfo; // 0xAu
5108 case MH_KEXT_BUNDLE: return eTypeSharedLibrary; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005109 default:
5110 break;
5111 }
5112 return eTypeUnknown;
5113}
5114
5115ObjectFile::Strata
5116ObjectFileMachO::CalculateStrata()
5117{
5118 switch (m_header.filetype)
5119 {
Charles Davis510938e2013-08-27 05:04:57 +00005120 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005121 {
5122 // 32 bit kexts are just object files, but they do have a valid
5123 // UUID load command.
5124 UUID uuid;
5125 if (GetUUID(&uuid))
5126 {
5127 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00005128 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005129 // "OSKextGetCurrentIdentifier" as this is required of kexts
5130 if (m_type == eTypeInvalid)
5131 m_type = eTypeSharedLibrary;
5132
5133 return eStrataKernel;
5134 }
5135 }
5136 return eStrataUnknown;
5137
Charles Davis510938e2013-08-27 05:04:57 +00005138 case MH_EXECUTE: // 0x2u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005139 // Check for the MH_DYLDLINK bit in the flags
Charles Davis510938e2013-08-27 05:04:57 +00005140 if (m_header.flags & MH_DYLDLINK)
Sean Callanan49bce8e2012-02-10 20:22:35 +00005141 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005142 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00005143 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005144 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00005145 {
5146 SectionList *section_list = GetSectionList();
5147 if (section_list)
5148 {
5149 static ConstString g_kld_section_name ("__KLD");
5150 if (section_list->FindSectionByName(g_kld_section_name))
5151 return eStrataKernel;
5152 }
5153 }
5154 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005155
Charles Davis510938e2013-08-27 05:04:57 +00005156 case MH_FVMLIB: return eStrataUser; // 0x3u
5157 case MH_CORE: return eStrataUnknown; // 0x4u
5158 case MH_PRELOAD: return eStrataRawImage; // 0x5u
5159 case MH_DYLIB: return eStrataUser; // 0x6u
5160 case MH_DYLINKER: return eStrataUser; // 0x7u
5161 case MH_BUNDLE: return eStrataUser; // 0x8u
5162 case MH_DYLIB_STUB: return eStrataUser; // 0x9u
5163 case MH_DSYM: return eStrataUnknown; // 0xAu
5164 case MH_KEXT_BUNDLE: return eStrataKernel; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005165 default:
5166 break;
5167 }
5168 return eStrataUnknown;
5169}
5170
5171
Greg Claytonc2ff9312012-02-22 19:41:02 +00005172uint32_t
5173ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
5174{
Greg Claytona1743492012-03-13 23:14:29 +00005175 ModuleSP module_sp(GetModule());
5176 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005177 {
Greg Claytona1743492012-03-13 23:14:29 +00005178 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5179 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00005180 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00005181 uint32_t version_cmd = 0;
5182 uint64_t version = 0;
5183 uint32_t i;
5184 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005185 {
Greg Claytonc7bece562013-01-25 18:06:21 +00005186 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00005187 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
5188 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005189
Charles Davis510938e2013-08-27 05:04:57 +00005190 if (load_cmd.cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005191 {
Greg Claytona1743492012-03-13 23:14:29 +00005192 if (version_cmd == 0)
5193 {
5194 version_cmd = load_cmd.cmd;
5195 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
5196 break;
5197 version = load_cmd.dylib.current_version;
5198 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005199 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00005200 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00005201 }
Greg Claytona1743492012-03-13 23:14:29 +00005202 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00005203 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005204
Charles Davis510938e2013-08-27 05:04:57 +00005205 if (version_cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005206 {
Greg Claytona1743492012-03-13 23:14:29 +00005207 if (versions != NULL && num_versions > 0)
5208 {
5209 if (num_versions > 0)
5210 versions[0] = (version & 0xFFFF0000ull) >> 16;
5211 if (num_versions > 1)
5212 versions[1] = (version & 0x0000FF00ull) >> 8;
5213 if (num_versions > 2)
5214 versions[2] = (version & 0x000000FFull);
5215 // Fill in an remaining version numbers with invalid values
5216 for (i=3; i<num_versions; ++i)
5217 versions[i] = UINT32_MAX;
5218 }
5219 // The LC_ID_DYLIB load command has a version with 3 version numbers
5220 // in it, so always return 3
5221 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00005222 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00005223 }
5224 return false;
5225}
5226
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005227bool
Greg Clayton514487e2011-02-15 21:59:32 +00005228ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005229{
Greg Claytona1743492012-03-13 23:14:29 +00005230 ModuleSP module_sp(GetModule());
5231 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00005232 {
Greg Claytona1743492012-03-13 23:14:29 +00005233 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton7ab7f892014-05-29 21:33:45 +00005234 return GetArchitecture (m_header, m_data, MachHeaderSizeFromMagic(m_header.magic), arch);
Greg Clayton593577a2011-09-21 03:57:31 +00005235 }
Greg Claytona1743492012-03-13 23:14:29 +00005236 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005237}
5238
5239
Jason Molenda0e0954c2013-04-16 06:24:42 +00005240UUID
5241ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
5242{
5243 UUID uuid;
5244 if (process)
5245 {
5246 addr_t all_image_infos = process->GetImageInfoAddress();
5247
5248 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
5249 // or it may be the address of the dyld_all_image_infos structure (want). The first four
5250 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
5251 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
5252
5253 Error err;
5254 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00005255 if (version_or_magic != static_cast<uint32_t>(-1)
Charles Davis510938e2013-08-27 05:04:57 +00005256 && version_or_magic != MH_MAGIC
5257 && version_or_magic != MH_CIGAM
5258 && version_or_magic != MH_MAGIC_64
5259 && version_or_magic != MH_CIGAM_64
Jason Molenda0e0954c2013-04-16 06:24:42 +00005260 && version_or_magic >= 13)
5261 {
5262 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
5263 int wordsize = process->GetAddressByteSize();
5264 if (wordsize == 8)
5265 {
5266 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
5267 }
5268 if (wordsize == 4)
5269 {
5270 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
5271 }
5272 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
5273 {
5274 uuid_t shared_cache_uuid;
5275 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
5276 {
5277 uuid.SetBytes (shared_cache_uuid);
5278 }
5279 }
5280 }
5281 }
5282 return uuid;
5283}
5284
5285UUID
5286ObjectFileMachO::GetLLDBSharedCacheUUID ()
5287{
5288 UUID uuid;
Todd Fiala013434e2014-07-09 01:29:05 +00005289#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molenda0e0954c2013-04-16 06:24:42 +00005290 uint8_t *(*dyld_get_all_image_infos)(void);
5291 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
5292 if (dyld_get_all_image_infos)
5293 {
5294 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
5295 if (dyld_all_image_infos_address)
5296 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00005297 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
5298 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00005299 {
Jason Molendaa3329782014-03-29 18:54:20 +00005300 uuid_t *sharedCacheUUID_address = 0;
5301 int wordsize = sizeof (uint8_t *);
5302 if (wordsize == 8)
5303 {
5304 sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 160); // sharedCacheUUID <mach-o/dyld_images.h>
5305 }
5306 else
5307 {
5308 sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84); // sharedCacheUUID <mach-o/dyld_images.h>
5309 }
Jason Molenda0e0954c2013-04-16 06:24:42 +00005310 uuid.SetBytes (sharedCacheUUID_address);
5311 }
5312 }
5313 }
5314#endif
5315 return uuid;
5316}
5317
Greg Clayton9b234982013-10-24 22:54:08 +00005318uint32_t
5319ObjectFileMachO::GetMinimumOSVersion (uint32_t *versions, uint32_t num_versions)
5320{
5321 if (m_min_os_versions.empty())
5322 {
5323 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5324 bool success = false;
5325 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
5326 {
5327 const lldb::offset_t load_cmd_offset = offset;
5328
5329 version_min_command lc;
5330 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
5331 break;
5332 if (lc.cmd == LC_VERSION_MIN_MACOSX || lc.cmd == LC_VERSION_MIN_IPHONEOS)
5333 {
5334 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
5335 {
5336 const uint32_t xxxx = lc.version >> 16;
5337 const uint32_t yy = (lc.version >> 8) & 0xffu;
5338 const uint32_t zz = lc.version & 0xffu;
5339 if (xxxx)
5340 {
5341 m_min_os_versions.push_back(xxxx);
Greg Clayton8031d282015-02-04 00:40:25 +00005342 m_min_os_versions.push_back(yy);
5343 m_min_os_versions.push_back(zz);
Greg Clayton9b234982013-10-24 22:54:08 +00005344 }
5345 success = true;
5346 }
5347 }
5348 offset = load_cmd_offset + lc.cmdsize;
5349 }
5350
5351 if (success == false)
5352 {
5353 // Push an invalid value so we don't keep trying to
5354 m_min_os_versions.push_back(UINT32_MAX);
5355 }
5356 }
5357
5358 if (m_min_os_versions.size() > 1 || m_min_os_versions[0] != UINT32_MAX)
5359 {
5360 if (versions != NULL && num_versions > 0)
5361 {
5362 for (size_t i=0; i<num_versions; ++i)
5363 {
5364 if (i < m_min_os_versions.size())
5365 versions[i] = m_min_os_versions[i];
5366 else
5367 versions[i] = 0;
5368 }
5369 }
5370 return m_min_os_versions.size();
5371 }
5372 // Call the superclasses version that will empty out the data
5373 return ObjectFile::GetMinimumOSVersion (versions, num_versions);
5374}
5375
5376uint32_t
5377ObjectFileMachO::GetSDKVersion(uint32_t *versions, uint32_t num_versions)
5378{
5379 if (m_sdk_versions.empty())
5380 {
5381 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5382 bool success = false;
5383 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
5384 {
5385 const lldb::offset_t load_cmd_offset = offset;
5386
5387 version_min_command lc;
5388 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
5389 break;
5390 if (lc.cmd == LC_VERSION_MIN_MACOSX || lc.cmd == LC_VERSION_MIN_IPHONEOS)
5391 {
5392 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
5393 {
Todd Fialaebecb382014-09-04 19:31:52 +00005394 const uint32_t xxxx = lc.sdk >> 16;
5395 const uint32_t yy = (lc.sdk >> 8) & 0xffu;
5396 const uint32_t zz = lc.sdk & 0xffu;
Greg Clayton9b234982013-10-24 22:54:08 +00005397 if (xxxx)
5398 {
5399 m_sdk_versions.push_back(xxxx);
Greg Clayton8031d282015-02-04 00:40:25 +00005400 m_sdk_versions.push_back(yy);
5401 m_sdk_versions.push_back(zz);
Greg Clayton9b234982013-10-24 22:54:08 +00005402 }
5403 success = true;
5404 }
5405 }
5406 offset = load_cmd_offset + lc.cmdsize;
5407 }
5408
5409 if (success == false)
5410 {
5411 // Push an invalid value so we don't keep trying to
5412 m_sdk_versions.push_back(UINT32_MAX);
5413 }
5414 }
5415
5416 if (m_sdk_versions.size() > 1 || m_sdk_versions[0] != UINT32_MAX)
5417 {
5418 if (versions != NULL && num_versions > 0)
5419 {
5420 for (size_t i=0; i<num_versions; ++i)
5421 {
5422 if (i < m_sdk_versions.size())
5423 versions[i] = m_sdk_versions[i];
5424 else
5425 versions[i] = 0;
5426 }
5427 }
5428 return m_sdk_versions.size();
5429 }
5430 // Call the superclasses version that will empty out the data
5431 return ObjectFile::GetSDKVersion (versions, num_versions);
5432}
5433
Jason Molenda0e0954c2013-04-16 06:24:42 +00005434
Greg Clayton08928f32015-02-05 02:01:34 +00005435bool
5436ObjectFileMachO::GetIsDynamicLinkEditor()
5437{
5438 return m_header.filetype == llvm::MachO::MH_DYLINKER;
5439}
5440
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005441//------------------------------------------------------------------
5442// PluginInterface protocol
5443//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00005444lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005445ObjectFileMachO::GetPluginName()
5446{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005447 return GetPluginNameStatic();
5448}
5449
5450uint32_t
5451ObjectFileMachO::GetPluginVersion()
5452{
5453 return 1;
5454}
5455
Greg Clayton7524e092014-02-06 20:10:16 +00005456
5457bool
Greg Clayton751caf62014-02-07 22:54:47 +00005458ObjectFileMachO::SetLoadAddress (Target &target,
5459 lldb::addr_t value,
5460 bool value_is_offset)
Greg Clayton7524e092014-02-06 20:10:16 +00005461{
Greg Clayton7524e092014-02-06 20:10:16 +00005462 ModuleSP module_sp = GetModule();
5463 if (module_sp)
5464 {
5465 size_t num_loaded_sections = 0;
5466 SectionList *section_list = GetSectionList ();
5467 if (section_list)
5468 {
5469 lldb::addr_t mach_base_file_addr = LLDB_INVALID_ADDRESS;
5470 const size_t num_sections = section_list->GetSize();
5471
Greg Clayton751caf62014-02-07 22:54:47 +00005472 const bool is_memory_image = (bool)m_process_wp.lock();
5473 const Strata strata = GetStrata();
5474 static ConstString g_linkedit_segname ("__LINKEDIT");
5475 if (value_is_offset)
Greg Clayton7524e092014-02-06 20:10:16 +00005476 {
Greg Clayton751caf62014-02-07 22:54:47 +00005477 // "value" is an offset to apply to each top level segment
Greg Clayton7524e092014-02-06 20:10:16 +00005478 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5479 {
5480 // Iterate through the object file sections to find all
5481 // of the sections that size on disk (to avoid __PAGEZERO)
5482 // and load them
5483 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Greg Clayton751caf62014-02-07 22:54:47 +00005484 if (section_sp &&
5485 section_sp->GetFileSize() > 0 &&
5486 section_sp->IsThreadSpecific() == false &&
5487 module_sp.get() == section_sp->GetModule().get())
Greg Clayton7524e092014-02-06 20:10:16 +00005488 {
Greg Clayton751caf62014-02-07 22:54:47 +00005489 // Ignore __LINKEDIT and __DWARF segments
5490 if (section_sp->GetName() == g_linkedit_segname)
5491 {
5492 // Only map __LINKEDIT if we have an in memory image and this isn't
5493 // a kernel binary like a kext or mach_kernel.
5494 if (is_memory_image == false || strata == eStrataKernel)
5495 continue;
5496 }
5497 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
Greg Clayton7524e092014-02-06 20:10:16 +00005498 ++num_loaded_sections;
5499 }
5500 }
5501 }
Greg Clayton751caf62014-02-07 22:54:47 +00005502 else
5503 {
5504 // "value" is the new base address of the mach_header, adjust each
5505 // section accordingly
5506
5507 // First find the address of the mach header which is the first non-zero
5508 // file sized section whose file offset is zero as this will be subtracted
5509 // from each other valid section's vmaddr and then get "base_addr" added to
5510 // it when loading the module in the target
5511 for (size_t sect_idx = 0;
5512 sect_idx < num_sections && mach_base_file_addr == LLDB_INVALID_ADDRESS;
5513 ++sect_idx)
5514 {
5515 // Iterate through the object file sections to find all
5516 // of the sections that size on disk (to avoid __PAGEZERO)
5517 // and load them
5518 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
5519 if (section &&
5520 section->GetFileSize() > 0 &&
5521 section->GetFileOffset() == 0 &&
5522 section->IsThreadSpecific() == false &&
5523 module_sp.get() == section->GetModule().get())
5524 {
5525 // Ignore __LINKEDIT and __DWARF segments
5526 if (section->GetName() == g_linkedit_segname)
5527 {
5528 // Only map __LINKEDIT if we have an in memory image and this isn't
5529 // a kernel binary like a kext or mach_kernel.
5530 if (is_memory_image == false || strata == eStrataKernel)
5531 continue;
5532 }
5533 mach_base_file_addr = section->GetFileAddress();
5534 }
5535 }
5536
5537 if (mach_base_file_addr != LLDB_INVALID_ADDRESS)
5538 {
5539 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5540 {
5541 // Iterate through the object file sections to find all
5542 // of the sections that size on disk (to avoid __PAGEZERO)
5543 // and load them
5544 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
5545 if (section_sp &&
5546 section_sp->GetFileSize() > 0 &&
5547 section_sp->IsThreadSpecific() == false &&
5548 module_sp.get() == section_sp->GetModule().get())
5549 {
5550 // Ignore __LINKEDIT and __DWARF segments
5551 if (section_sp->GetName() == g_linkedit_segname)
5552 {
5553 // Only map __LINKEDIT if we have an in memory image and this isn't
5554 // a kernel binary like a kext or mach_kernel.
5555 if (is_memory_image == false || strata == eStrataKernel)
5556 continue;
5557 }
5558 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() - mach_base_file_addr + value))
5559 ++num_loaded_sections;
5560 }
5561 }
5562 }
5563 }
Greg Clayton7524e092014-02-06 20:10:16 +00005564 }
Greg Clayton7524e092014-02-06 20:10:16 +00005565 return num_loaded_sections > 0;
5566 }
Jason Molenda5cf1e232014-10-16 07:41:32 +00005567 return false;
Greg Clayton7524e092014-02-06 20:10:16 +00005568}
5569
Greg Claytona2715cf2014-06-13 00:54:12 +00005570bool
5571ObjectFileMachO::SaveCore (const lldb::ProcessSP &process_sp,
5572 const FileSpec &outfile,
5573 Error &error)
5574{
5575 if (process_sp)
5576 {
5577 Target &target = process_sp->GetTarget();
5578 const ArchSpec target_arch = target.GetArchitecture();
5579 const llvm::Triple &target_triple = target_arch.GetTriple();
5580 if (target_triple.getVendor() == llvm::Triple::Apple &&
5581 (target_triple.getOS() == llvm::Triple::MacOSX ||
5582 target_triple.getOS() == llvm::Triple::IOS))
5583 {
5584 bool make_core = false;
5585 switch (target_arch.GetMachine())
5586 {
Jason Molenda4b0c1182014-11-12 02:39:14 +00005587 // arm64 core file writing is having some problem with writing down the
5588 // dyld shared images info struct and/or the main executable binary. May
5589 // turn out to be a debugserver problem, not sure yet.
Jason Molenda22952582014-11-12 01:11:36 +00005590// case llvm::Triple::aarch64:
5591
Jason Molenda4b0c1182014-11-12 02:39:14 +00005592 case llvm::Triple::arm:
Greg Claytona2715cf2014-06-13 00:54:12 +00005593 case llvm::Triple::x86:
5594 case llvm::Triple::x86_64:
5595 make_core = true;
5596 break;
5597 default:
5598 error.SetErrorStringWithFormat ("unsupported core architecture: %s", target_triple.str().c_str());
5599 break;
5600 }
5601
5602 if (make_core)
5603 {
5604 std::vector<segment_command_64> segment_load_commands;
Saleem Abdulrasool3924d752014-06-13 03:30:39 +00005605// uint32_t range_info_idx = 0;
Greg Claytona2715cf2014-06-13 00:54:12 +00005606 MemoryRegionInfo range_info;
5607 Error range_error = process_sp->GetMemoryRegionInfo(0, range_info);
Jason Molendad20359d2014-11-11 10:59:15 +00005608 const uint32_t addr_byte_size = target_arch.GetAddressByteSize();
5609 const ByteOrder byte_order = target_arch.GetByteOrder();
Greg Claytona2715cf2014-06-13 00:54:12 +00005610 if (range_error.Success())
5611 {
5612 while (range_info.GetRange().GetRangeBase() != LLDB_INVALID_ADDRESS)
5613 {
5614 const addr_t addr = range_info.GetRange().GetRangeBase();
5615 const addr_t size = range_info.GetRange().GetByteSize();
5616
5617 if (size == 0)
5618 break;
5619
5620 // Calculate correct protections
5621 uint32_t prot = 0;
5622 if (range_info.GetReadable() == MemoryRegionInfo::eYes)
5623 prot |= VM_PROT_READ;
5624 if (range_info.GetWritable() == MemoryRegionInfo::eYes)
5625 prot |= VM_PROT_WRITE;
5626 if (range_info.GetExecutable() == MemoryRegionInfo::eYes)
5627 prot |= VM_PROT_EXECUTE;
5628
5629// printf ("[%3u] [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") %c%c%c\n",
5630// range_info_idx,
5631// addr,
5632// size,
5633// (prot & VM_PROT_READ ) ? 'r' : '-',
5634// (prot & VM_PROT_WRITE ) ? 'w' : '-',
5635// (prot & VM_PROT_EXECUTE) ? 'x' : '-');
5636
5637 if (prot != 0)
5638 {
Jason Molendad20359d2014-11-11 10:59:15 +00005639 uint32_t cmd_type = LC_SEGMENT_64;
5640 uint32_t segment_size = sizeof (segment_command_64);
5641 if (addr_byte_size == 4)
5642 {
5643 cmd_type = LC_SEGMENT;
5644 segment_size = sizeof (segment_command);
5645 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005646 segment_command_64 segment = {
Jason Molendad20359d2014-11-11 10:59:15 +00005647 cmd_type, // uint32_t cmd;
5648 segment_size, // uint32_t cmdsize;
Greg Claytona2715cf2014-06-13 00:54:12 +00005649 {0}, // char segname[16];
Jason Molendad20359d2014-11-11 10:59:15 +00005650 addr, // uint64_t vmaddr; // uint32_t for 32-bit Mach-O
5651 size, // uint64_t vmsize; // uint32_t for 32-bit Mach-O
5652 0, // uint64_t fileoff; // uint32_t for 32-bit Mach-O
5653 size, // uint64_t filesize; // uint32_t for 32-bit Mach-O
Greg Claytona2715cf2014-06-13 00:54:12 +00005654 prot, // uint32_t maxprot;
5655 prot, // uint32_t initprot;
5656 0, // uint32_t nsects;
5657 0 }; // uint32_t flags;
5658 segment_load_commands.push_back(segment);
5659 }
5660 else
5661 {
5662 // No protections and a size of 1 used to be returned from old
5663 // debugservers when we asked about a region that was past the
5664 // last memory region and it indicates the end...
5665 if (size == 1)
5666 break;
5667 }
5668
5669 range_error = process_sp->GetMemoryRegionInfo(range_info.GetRange().GetRangeEnd(), range_info);
5670 if (range_error.Fail())
5671 break;
5672 }
5673
Greg Claytona2715cf2014-06-13 00:54:12 +00005674 StreamString buffer (Stream::eBinary,
5675 addr_byte_size,
5676 byte_order);
5677
5678 mach_header_64 mach_header;
Jason Molendad20359d2014-11-11 10:59:15 +00005679 if (addr_byte_size == 8)
5680 {
5681 mach_header.magic = MH_MAGIC_64;
5682 }
5683 else
5684 {
5685 mach_header.magic = MH_MAGIC;
5686 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005687 mach_header.cputype = target_arch.GetMachOCPUType();
5688 mach_header.cpusubtype = target_arch.GetMachOCPUSubType();
5689 mach_header.filetype = MH_CORE;
5690 mach_header.ncmds = segment_load_commands.size();
5691 mach_header.flags = 0;
5692 mach_header.reserved = 0;
5693 ThreadList &thread_list = process_sp->GetThreadList();
5694 const uint32_t num_threads = thread_list.GetSize();
5695
5696 // Make an array of LC_THREAD data items. Each one contains
5697 // the contents of the LC_THREAD load command. The data doesn't
5698 // contain the load command + load command size, we will
5699 // add the load command and load command size as we emit the data.
5700 std::vector<StreamString> LC_THREAD_datas(num_threads);
5701 for (auto &LC_THREAD_data : LC_THREAD_datas)
5702 {
5703 LC_THREAD_data.GetFlags().Set(Stream::eBinary);
5704 LC_THREAD_data.SetAddressByteSize(addr_byte_size);
5705 LC_THREAD_data.SetByteOrder(byte_order);
5706 }
5707 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx)
5708 {
5709 ThreadSP thread_sp (thread_list.GetThreadAtIndex(thread_idx));
5710 if (thread_sp)
5711 {
5712 switch (mach_header.cputype)
5713 {
Jason Molenda22952582014-11-12 01:11:36 +00005714 case llvm::MachO::CPU_TYPE_ARM64:
5715 RegisterContextDarwin_arm64_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
Greg Claytona2715cf2014-06-13 00:54:12 +00005716 break;
Jason Molenda22952582014-11-12 01:11:36 +00005717
5718 case llvm::MachO::CPU_TYPE_ARM:
5719 RegisterContextDarwin_arm_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5720 break;
5721
Greg Claytona2715cf2014-06-13 00:54:12 +00005722 case llvm::MachO::CPU_TYPE_I386:
Jason Molendad20359d2014-11-11 10:59:15 +00005723 RegisterContextDarwin_i386_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
Greg Claytona2715cf2014-06-13 00:54:12 +00005724 break;
5725
5726 case llvm::MachO::CPU_TYPE_X86_64:
5727 RegisterContextDarwin_x86_64_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5728 break;
5729 }
5730
5731 }
5732 }
5733
5734 // The size of the load command is the size of the segments...
Jason Molendad20359d2014-11-11 10:59:15 +00005735 if (addr_byte_size == 8)
5736 {
5737 mach_header.sizeofcmds = segment_load_commands.size() * sizeof (struct segment_command_64);
5738 }
5739 else
5740 {
5741 mach_header.sizeofcmds = segment_load_commands.size() * sizeof (struct segment_command);
5742 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005743
5744 // and the size of all LC_THREAD load command
5745 for (const auto &LC_THREAD_data : LC_THREAD_datas)
5746 {
Greg Claytone37df2e2014-09-16 20:50:29 +00005747 ++mach_header.ncmds;
Greg Claytona2715cf2014-06-13 00:54:12 +00005748 mach_header.sizeofcmds += 8 + LC_THREAD_data.GetSize();
5749 }
5750
5751 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",
5752 mach_header.magic,
5753 mach_header.cputype,
5754 mach_header.cpusubtype,
5755 mach_header.filetype,
5756 mach_header.ncmds,
5757 mach_header.sizeofcmds,
5758 mach_header.flags,
5759 mach_header.reserved);
5760
5761 // Write the mach header
5762 buffer.PutHex32(mach_header.magic);
5763 buffer.PutHex32(mach_header.cputype);
5764 buffer.PutHex32(mach_header.cpusubtype);
5765 buffer.PutHex32(mach_header.filetype);
5766 buffer.PutHex32(mach_header.ncmds);
5767 buffer.PutHex32(mach_header.sizeofcmds);
5768 buffer.PutHex32(mach_header.flags);
Jason Molendad20359d2014-11-11 10:59:15 +00005769 if (addr_byte_size == 8)
5770 {
5771 buffer.PutHex32(mach_header.reserved);
5772 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005773
5774 // Skip the mach header and all load commands and align to the next
5775 // 0x1000 byte boundary
5776 addr_t file_offset = buffer.GetSize() + mach_header.sizeofcmds;
5777 if (file_offset & 0x00000fff)
5778 {
5779 file_offset += 0x00001000ull;
5780 file_offset &= (~0x00001000ull + 1);
5781 }
5782
5783 for (auto &segment : segment_load_commands)
5784 {
5785 segment.fileoff = file_offset;
5786 file_offset += segment.filesize;
5787 }
5788
5789 // Write out all of the LC_THREAD load commands
5790 for (const auto &LC_THREAD_data : LC_THREAD_datas)
5791 {
5792 const size_t LC_THREAD_data_size = LC_THREAD_data.GetSize();
5793 buffer.PutHex32(LC_THREAD);
5794 buffer.PutHex32(8 + LC_THREAD_data_size); // cmd + cmdsize + data
5795 buffer.Write(LC_THREAD_data.GetData(), LC_THREAD_data_size);
5796 }
5797
5798 // Write out all of the segment load commands
5799 for (const auto &segment : segment_load_commands)
5800 {
5801 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",
5802 segment.cmd,
5803 segment.cmdsize,
5804 segment.vmaddr,
5805 segment.vmaddr + segment.vmsize,
5806 segment.fileoff,
5807 segment.filesize,
5808 segment.maxprot,
5809 segment.initprot,
5810 segment.nsects,
5811 segment.flags);
5812
5813 buffer.PutHex32(segment.cmd);
5814 buffer.PutHex32(segment.cmdsize);
5815 buffer.PutRawBytes(segment.segname, sizeof(segment.segname));
Jason Molendad20359d2014-11-11 10:59:15 +00005816 if (addr_byte_size == 8)
5817 {
5818 buffer.PutHex64(segment.vmaddr);
5819 buffer.PutHex64(segment.vmsize);
5820 buffer.PutHex64(segment.fileoff);
5821 buffer.PutHex64(segment.filesize);
5822 }
5823 else
5824 {
5825 buffer.PutHex32(static_cast<uint32_t>(segment.vmaddr));
5826 buffer.PutHex32(static_cast<uint32_t>(segment.vmsize));
5827 buffer.PutHex32(static_cast<uint32_t>(segment.fileoff));
5828 buffer.PutHex32(static_cast<uint32_t>(segment.filesize));
5829 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005830 buffer.PutHex32(segment.maxprot);
5831 buffer.PutHex32(segment.initprot);
5832 buffer.PutHex32(segment.nsects);
5833 buffer.PutHex32(segment.flags);
5834 }
5835
5836 File core_file;
5837 std::string core_file_path(outfile.GetPath());
5838 error = core_file.Open(core_file_path.c_str(),
5839 File::eOpenOptionWrite |
5840 File::eOpenOptionTruncate |
5841 File::eOpenOptionCanCreate);
5842 if (error.Success())
5843 {
5844 // Read 1 page at a time
5845 uint8_t bytes[0x1000];
5846 // Write the mach header and load commands out to the core file
5847 size_t bytes_written = buffer.GetString().size();
5848 error = core_file.Write(buffer.GetString().data(), bytes_written);
5849 if (error.Success())
5850 {
5851 // Now write the file data for all memory segments in the process
5852 for (const auto &segment : segment_load_commands)
5853 {
David Majnemerb98a5e02014-07-24 00:24:12 +00005854 if (core_file.SeekFromStart(segment.fileoff) == -1)
Greg Claytona2715cf2014-06-13 00:54:12 +00005855 {
5856 error.SetErrorStringWithFormat("unable to seek to offset 0x%" PRIx64 " in '%s'", segment.fileoff, core_file_path.c_str());
5857 break;
5858 }
5859
Jason Molendad20359d2014-11-11 10:59:15 +00005860 printf ("Saving %" PRId64 " bytes of data for memory region at 0x%" PRIx64 "\n", segment.vmsize, segment.vmaddr);
Greg Claytona2715cf2014-06-13 00:54:12 +00005861 addr_t bytes_left = segment.vmsize;
5862 addr_t addr = segment.vmaddr;
5863 Error memory_read_error;
5864 while (bytes_left > 0 && error.Success())
5865 {
5866 const size_t bytes_to_read = bytes_left > sizeof(bytes) ? sizeof(bytes) : bytes_left;
5867 const size_t bytes_read = process_sp->ReadMemory(addr, bytes, bytes_to_read, memory_read_error);
5868 if (bytes_read == bytes_to_read)
5869 {
5870 size_t bytes_written = bytes_read;
5871 error = core_file.Write(bytes, bytes_written);
5872 bytes_left -= bytes_read;
5873 addr += bytes_read;
5874 }
5875 else
5876 {
5877 // Some pages within regions are not readable, those
5878 // should be zero filled
5879 memset (bytes, 0, bytes_to_read);
5880 size_t bytes_written = bytes_to_read;
5881 error = core_file.Write(bytes, bytes_written);
5882 bytes_left -= bytes_to_read;
5883 addr += bytes_to_read;
5884 }
5885 }
5886 }
5887 }
5888 }
5889 }
5890 else
5891 {
5892 error.SetErrorString("process doesn't support getting memory region info");
5893 }
5894 }
5895 return true; // This is the right plug to handle saving core files for this process
5896 }
5897 }
5898 return false;
5899}
5900