blob: 8ccca3943fd04b0efcf74162676d032a08971a8f [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/ArchSpec.h"
13#include "lldb/Core/DataBuffer.h"
Jason Molendaf6ce26f2013-04-10 05:58:57 +000014#include "lldb/Core/Debugger.h"
Greg Claytona2715cf2014-06-13 00:54:12 +000015#include "lldb/Core/Error.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/FileSpecList.h"
Greg Clayton3f839a32012-09-05 01:38:55 +000017#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000019#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/PluginManager.h"
Greg Clayton1eac0c72012-04-24 03:06:13 +000021#include "lldb/Core/RangeMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/Section.h"
23#include "lldb/Core/StreamFile.h"
24#include "lldb/Core/StreamString.h"
25#include "lldb/Core/Timer.h"
26#include "lldb/Core/UUID.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000027#include "lldb/Host/Host.h"
28#include "lldb/Host/FileSpec.h"
Jason Molenda5635f772013-03-21 03:36:01 +000029#include "lldb/Symbol/DWARFCallFrameInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Symbol/ObjectFile.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000031#include "lldb/Target/MemoryRegionInfo.h"
Greg Clayton26b47e22012-04-18 05:19:20 +000032#include "lldb/Target/Platform.h"
Greg Claytonc9660542012-02-05 02:38:54 +000033#include "lldb/Target/Process.h"
Greg Clayton7524e092014-02-06 20:10:16 +000034#include "lldb/Target/SectionLoadList.h"
Greg Clayton26b47e22012-04-18 05:19:20 +000035#include "lldb/Target/Target.h"
Greg Claytona2715cf2014-06-13 00:54:12 +000036#include "lldb/Target/Thread.h"
37#include "lldb/Target/ThreadList.h"
Greg Claytonc859e2d2012-02-13 23:10:39 +000038#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
Jason Molendaa3329782014-03-29 18:54:20 +000039#include "Plugins/Process/Utility/RegisterContextDarwin_arm64.h"
Greg Claytonc859e2d2012-02-13 23:10:39 +000040#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Claytonc3776bf2012-02-09 06:16:32 +000041#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042
Jim Ingham46d005d2014-04-02 22:53:21 +000043#include "lldb/Utility/SafeMachO.h"
44
45#include "ObjectFileMachO.h"
46
Todd Fiala013434e2014-07-09 01:29:05 +000047#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molenda0e0954c2013-04-16 06:24:42 +000048// GetLLDBSharedCacheUUID() needs to call dlsym()
49#include <dlfcn.h>
50#endif
51
Daniel Maleaffeb4b62013-04-17 19:24:22 +000052#ifndef __APPLE__
53#include "Utility/UuidCompatibility.h"
54#endif
55
Greg Claytonb887da12015-07-16 19:50:57 +000056#define THUMB_ADDRESS_BIT_MASK 0xfffffffffffffffeull
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);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000997 spec.SetObjectSize(length);
998
Greg Clayton7ab7f892014-05-29 21:33:45 +0000999 if (GetArchitecture (header, data, data_offset, spec.GetArchitecture()))
Jason Molendab000e4d2013-08-27 02:22:06 +00001000 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001001 if (spec.GetArchitecture().IsValid())
1002 {
1003 GetUUID (header, data, data_offset, spec.GetUUID());
1004 specs.Append(spec);
1005 }
Greg Claytonf4d6de62013-04-24 22:29:28 +00001006 }
1007 }
1008 }
1009 }
1010 return specs.GetSize() - initial_count;
1011}
1012
1013
Greg Claytonc9660542012-02-05 02:38:54 +00001014
1015const ConstString &
1016ObjectFileMachO::GetSegmentNameTEXT()
1017{
1018 static ConstString g_segment_name_TEXT ("__TEXT");
1019 return g_segment_name_TEXT;
1020}
1021
1022const ConstString &
1023ObjectFileMachO::GetSegmentNameDATA()
1024{
1025 static ConstString g_segment_name_DATA ("__DATA");
1026 return g_segment_name_DATA;
1027}
1028
1029const ConstString &
Greg Claytona381e102015-07-27 23:21:05 +00001030ObjectFileMachO::GetSegmentNameDATA_DIRTY()
1031{
1032 static ConstString g_segment_name ("__DATA_DIRTY");
1033 return g_segment_name;
1034}
1035
1036const ConstString &
1037ObjectFileMachO::GetSegmentNameDATA_CONST()
1038{
1039 static ConstString g_segment_name ("__DATA_CONST");
1040 return g_segment_name;
1041}
1042
1043const ConstString &
Greg Claytonc9660542012-02-05 02:38:54 +00001044ObjectFileMachO::GetSegmentNameOBJC()
1045{
1046 static ConstString g_segment_name_OBJC ("__OBJC");
1047 return g_segment_name_OBJC;
1048}
1049
1050const ConstString &
1051ObjectFileMachO::GetSegmentNameLINKEDIT()
1052{
1053 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
1054 return g_section_name_LINKEDIT;
1055}
1056
1057const ConstString &
1058ObjectFileMachO::GetSectionNameEHFrame()
1059{
1060 static ConstString g_section_name_eh_frame ("__eh_frame");
1061 return g_section_name_eh_frame;
1062}
1063
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064bool
Jason Molenda4e7511e2013-03-06 23:19:17 +00001065ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
1066 lldb::addr_t data_offset,
Greg Clayton44435ed2012-01-12 05:25:17 +00001067 lldb::addr_t data_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068{
Greg Clayton44435ed2012-01-12 05:25:17 +00001069 DataExtractor data;
1070 data.SetData (data_sp, data_offset, data_length);
Greg Claytonc7bece562013-01-25 18:06:21 +00001071 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072 uint32_t magic = data.GetU32(&offset);
1073 return MachHeaderSizeFromMagic(magic) != 0;
1074}
1075
1076
Greg Clayton5ce9c562013-02-06 17:22:03 +00001077ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
1078 DataBufferSP& data_sp,
1079 lldb::offset_t data_offset,
1080 const FileSpec* file,
1081 lldb::offset_t file_offset,
1082 lldb::offset_t length) :
1083 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
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)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089{
Greg Clayton72b77eb2011-02-04 21:13:05 +00001090 ::memset (&m_header, 0, sizeof(m_header));
1091 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092}
1093
Greg Claytone72dfb32012-02-24 01:59:29 +00001094ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +00001095 lldb::DataBufferSP& header_data_sp,
1096 const lldb::ProcessSP &process_sp,
1097 lldb::addr_t header_addr) :
Greg Claytone72dfb32012-02-24 01:59:29 +00001098 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonc3776bf2012-02-09 06:16:32 +00001099 m_mach_segments(),
1100 m_mach_sections(),
1101 m_entry_point_address(),
1102 m_thread_context_offsets(),
1103 m_thread_context_offsets_valid(false)
Greg Claytonc9660542012-02-05 02:38:54 +00001104{
1105 ::memset (&m_header, 0, sizeof(m_header));
1106 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
1107}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108
1109ObjectFileMachO::~ObjectFileMachO()
1110{
1111}
1112
Greg Claytonf4d6de62013-04-24 22:29:28 +00001113bool
1114ObjectFileMachO::ParseHeader (DataExtractor &data,
1115 lldb::offset_t *data_offset_ptr,
1116 llvm::MachO::mach_header &header)
1117{
1118 data.SetByteOrder (lldb::endian::InlHostByteOrder());
1119 // Leave magic in the original byte order
1120 header.magic = data.GetU32(data_offset_ptr);
1121 bool can_parse = false;
1122 bool is_64_bit = false;
1123 switch (header.magic)
1124 {
Charles Davis510938e2013-08-27 05:04:57 +00001125 case MH_MAGIC:
Greg Claytonf4d6de62013-04-24 22:29:28 +00001126 data.SetByteOrder (lldb::endian::InlHostByteOrder());
1127 data.SetAddressByteSize(4);
1128 can_parse = true;
1129 break;
1130
Charles Davis510938e2013-08-27 05:04:57 +00001131 case MH_MAGIC_64:
Greg Claytonf4d6de62013-04-24 22:29:28 +00001132 data.SetByteOrder (lldb::endian::InlHostByteOrder());
1133 data.SetAddressByteSize(8);
1134 can_parse = true;
1135 is_64_bit = true;
1136 break;
1137
Charles Davis510938e2013-08-27 05:04:57 +00001138 case MH_CIGAM:
Greg Claytonf4d6de62013-04-24 22:29:28 +00001139 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
1140 data.SetAddressByteSize(4);
1141 can_parse = true;
1142 break;
1143
Charles Davis510938e2013-08-27 05:04:57 +00001144 case MH_CIGAM_64:
Greg Claytonf4d6de62013-04-24 22:29:28 +00001145 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
1146 data.SetAddressByteSize(8);
1147 is_64_bit = true;
1148 can_parse = true;
1149 break;
1150
1151 default:
1152 break;
1153 }
1154
1155 if (can_parse)
1156 {
1157 data.GetU32(data_offset_ptr, &header.cputype, 6);
1158 if (is_64_bit)
1159 *data_offset_ptr += 4;
1160 return true;
1161 }
1162 else
1163 {
1164 memset(&header, 0, sizeof(header));
1165 }
1166 return false;
1167}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001168
1169bool
1170ObjectFileMachO::ParseHeader ()
1171{
Greg Claytona1743492012-03-13 23:14:29 +00001172 ModuleSP module_sp(GetModule());
1173 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174 {
Greg Claytona1743492012-03-13 23:14:29 +00001175 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
1176 bool can_parse = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00001177 lldb::offset_t offset = 0;
Greg Clayton7fb56d02011-02-01 01:31:41 +00001178 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +00001179 // Leave magic in the original byte order
1180 m_header.magic = m_data.GetU32(&offset);
1181 switch (m_header.magic)
Greg Claytonc9660542012-02-05 02:38:54 +00001182 {
Charles Davis510938e2013-08-27 05:04:57 +00001183 case MH_MAGIC:
Greg Claytona1743492012-03-13 23:14:29 +00001184 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
1185 m_data.SetAddressByteSize(4);
1186 can_parse = true;
1187 break;
1188
Charles Davis510938e2013-08-27 05:04:57 +00001189 case MH_MAGIC_64:
Greg Claytona1743492012-03-13 23:14:29 +00001190 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
1191 m_data.SetAddressByteSize(8);
1192 can_parse = true;
1193 break;
1194
Charles Davis510938e2013-08-27 05:04:57 +00001195 case MH_CIGAM:
Greg Claytona1743492012-03-13 23:14:29 +00001196 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
1197 m_data.SetAddressByteSize(4);
1198 can_parse = true;
1199 break;
1200
Charles Davis510938e2013-08-27 05:04:57 +00001201 case MH_CIGAM_64:
Greg Claytona1743492012-03-13 23:14:29 +00001202 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
1203 m_data.SetAddressByteSize(8);
1204 can_parse = true;
1205 break;
1206
1207 default:
1208 break;
Greg Claytonc9660542012-02-05 02:38:54 +00001209 }
Greg Claytona1743492012-03-13 23:14:29 +00001210
1211 if (can_parse)
1212 {
1213 m_data.GetU32(&offset, &m_header.cputype, 6);
1214
Greg Clayton7ab7f892014-05-29 21:33:45 +00001215
1216 ArchSpec mach_arch;
1217
1218 if (GetArchitecture (mach_arch))
Greg Claytona1743492012-03-13 23:14:29 +00001219 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001220 // Check if the module has a required architecture
1221 const ArchSpec &module_arch = module_sp->GetArchitecture();
1222 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
1223 return false;
1224
1225 if (SetModulesArchitecture (mach_arch))
Greg Claytona1743492012-03-13 23:14:29 +00001226 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001227 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
1228 if (m_data.GetByteSize() < header_and_lc_size)
Greg Claytona1743492012-03-13 23:14:29 +00001229 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001230 DataBufferSP data_sp;
1231 ProcessSP process_sp (m_process_wp.lock());
1232 if (process_sp)
1233 {
1234 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
1235 }
1236 else
1237 {
1238 // Read in all only the load command data from the file on disk
1239 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
1240 if (data_sp->GetByteSize() != header_and_lc_size)
1241 return false;
1242 }
1243 if (data_sp)
1244 m_data.SetData (data_sp);
Greg Claytona1743492012-03-13 23:14:29 +00001245 }
Greg Claytona1743492012-03-13 23:14:29 +00001246 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00001247 return true;
Greg Claytona1743492012-03-13 23:14:29 +00001248 }
Greg Claytona1743492012-03-13 23:14:29 +00001249 }
1250 else
1251 {
1252 memset(&m_header, 0, sizeof(struct mach_header));
1253 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254 }
1255 return false;
1256}
1257
1258
1259ByteOrder
1260ObjectFileMachO::GetByteOrder () const
1261{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262 return m_data.GetByteOrder ();
1263}
1264
Jim Ingham5aee1622010-08-09 23:31:02 +00001265bool
1266ObjectFileMachO::IsExecutable() const
1267{
Charles Davis510938e2013-08-27 05:04:57 +00001268 return m_header.filetype == MH_EXECUTE;
Jim Ingham5aee1622010-08-09 23:31:02 +00001269}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270
Greg Claytonc7bece562013-01-25 18:06:21 +00001271uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001272ObjectFileMachO::GetAddressByteSize () const
1273{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274 return m_data.GetAddressByteSize ();
1275}
1276
Greg Claytone0d378b2011-03-24 21:19:54 +00001277AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +00001278ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
1279{
1280 Symtab *symtab = GetSymtab();
1281 if (symtab)
1282 {
1283 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
1284 if (symbol)
1285 {
Greg Claytone7612132012-03-07 21:03:09 +00001286 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +00001287 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00001288 SectionSP section_sp (symbol->GetAddressRef().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +00001289 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +00001290 {
Charles Davis510938e2013-08-27 05:04:57 +00001291 const lldb::SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +00001292 switch (section_type)
1293 {
Jason Molendae589e7e2014-12-08 03:09:00 +00001294 case eSectionTypeInvalid:
1295 return eAddressClassUnknown;
1296
Greg Claytonded470d2011-03-19 01:12:21 +00001297 case eSectionTypeCode:
Charles Davis510938e2013-08-27 05:04:57 +00001298 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +00001299 {
1300 // For ARM we have a bit in the n_desc field of the symbol
1301 // that tells us ARM/Thumb which is bit 0x0008.
1302 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1303 return eAddressClassCodeAlternateISA;
1304 }
1305 return eAddressClassCode;
1306
Jason Molendae589e7e2014-12-08 03:09:00 +00001307 case eSectionTypeContainer:
1308 return eAddressClassUnknown;
1309
Greg Clayton5009f9d2011-10-27 17:55:14 +00001310 case eSectionTypeData:
1311 case eSectionTypeDataCString:
1312 case eSectionTypeDataCStringPointers:
1313 case eSectionTypeDataSymbolAddress:
1314 case eSectionTypeData4:
1315 case eSectionTypeData8:
1316 case eSectionTypeData16:
1317 case eSectionTypeDataPointers:
1318 case eSectionTypeZeroFill:
1319 case eSectionTypeDataObjCMessageRefs:
1320 case eSectionTypeDataObjCCFStrings:
Ryan Brown65d4d5c2015-09-16 21:20:44 +00001321 case eSectionTypeGoSymtab:
Greg Clayton5009f9d2011-10-27 17:55:14 +00001322 return eAddressClassData;
Jason Molendae589e7e2014-12-08 03:09:00 +00001323
Greg Clayton5009f9d2011-10-27 17:55:14 +00001324 case eSectionTypeDebug:
1325 case eSectionTypeDWARFDebugAbbrev:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001326 case eSectionTypeDWARFDebugAddr:
Greg Clayton5009f9d2011-10-27 17:55:14 +00001327 case eSectionTypeDWARFDebugAranges:
1328 case eSectionTypeDWARFDebugFrame:
1329 case eSectionTypeDWARFDebugInfo:
1330 case eSectionTypeDWARFDebugLine:
1331 case eSectionTypeDWARFDebugLoc:
1332 case eSectionTypeDWARFDebugMacInfo:
1333 case eSectionTypeDWARFDebugPubNames:
1334 case eSectionTypeDWARFDebugPubTypes:
1335 case eSectionTypeDWARFDebugRanges:
1336 case eSectionTypeDWARFDebugStr:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001337 case eSectionTypeDWARFDebugStrOffsets:
Greg Clayton5009f9d2011-10-27 17:55:14 +00001338 case eSectionTypeDWARFAppleNames:
1339 case eSectionTypeDWARFAppleTypes:
1340 case eSectionTypeDWARFAppleNamespaces:
1341 case eSectionTypeDWARFAppleObjC:
1342 return eAddressClassDebug;
Jason Molendae589e7e2014-12-08 03:09:00 +00001343
1344 case eSectionTypeEHFrame:
Tamas Berghammer648f3c72015-09-30 13:50:14 +00001345 case eSectionTypeARMexidx:
1346 case eSectionTypeARMextab:
Jason Molendae589e7e2014-12-08 03:09:00 +00001347 case eSectionTypeCompactUnwind:
1348 return eAddressClassRuntime;
1349
Michael Sartaina7499c92013-07-01 19:45:50 +00001350 case eSectionTypeELFSymbolTable:
1351 case eSectionTypeELFDynamicSymbols:
1352 case eSectionTypeELFRelocationEntries:
1353 case eSectionTypeELFDynamicLinkInfo:
Jason Molendae589e7e2014-12-08 03:09:00 +00001354 case eSectionTypeOther:
1355 return eAddressClassUnknown;
Greg Claytonded470d2011-03-19 01:12:21 +00001356 }
1357 }
1358 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001359
Greg Claytone0d378b2011-03-24 21:19:54 +00001360 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +00001361 switch (symbol_type)
1362 {
1363 case eSymbolTypeAny: return eAddressClassUnknown;
1364 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda4e7511e2013-03-06 23:19:17 +00001365
Greg Claytonded470d2011-03-19 01:12:21 +00001366 case eSymbolTypeCode:
1367 case eSymbolTypeTrampoline:
Greg Clayton059f7242013-02-27 21:16:04 +00001368 case eSymbolTypeResolver:
Charles Davis510938e2013-08-27 05:04:57 +00001369 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +00001370 {
1371 // For ARM we have a bit in the n_desc field of the symbol
1372 // that tells us ARM/Thumb which is bit 0x0008.
1373 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1374 return eAddressClassCodeAlternateISA;
1375 }
1376 return eAddressClassCode;
1377
1378 case eSymbolTypeData: return eAddressClassData;
1379 case eSymbolTypeRuntime: return eAddressClassRuntime;
1380 case eSymbolTypeException: return eAddressClassRuntime;
1381 case eSymbolTypeSourceFile: return eAddressClassDebug;
1382 case eSymbolTypeHeaderFile: return eAddressClassDebug;
1383 case eSymbolTypeObjectFile: return eAddressClassDebug;
1384 case eSymbolTypeCommonBlock: return eAddressClassDebug;
1385 case eSymbolTypeBlock: return eAddressClassDebug;
1386 case eSymbolTypeLocal: return eAddressClassData;
1387 case eSymbolTypeParam: return eAddressClassData;
1388 case eSymbolTypeVariable: return eAddressClassData;
1389 case eSymbolTypeVariableType: return eAddressClassDebug;
1390 case eSymbolTypeLineEntry: return eAddressClassDebug;
1391 case eSymbolTypeLineHeader: return eAddressClassDebug;
1392 case eSymbolTypeScopeBegin: return eAddressClassDebug;
1393 case eSymbolTypeScopeEnd: return eAddressClassDebug;
1394 case eSymbolTypeAdditional: return eAddressClassUnknown;
1395 case eSymbolTypeCompiler: return eAddressClassDebug;
1396 case eSymbolTypeInstrumentation:return eAddressClassDebug;
1397 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +00001398 case eSymbolTypeObjCClass: return eAddressClassRuntime;
1399 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
1400 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Clayton9191db42013-10-21 18:40:51 +00001401 case eSymbolTypeReExported: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +00001402 }
1403 }
1404 }
1405 return eAddressClassUnknown;
1406}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407
1408Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +00001409ObjectFileMachO::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001410{
Greg Claytona1743492012-03-13 23:14:29 +00001411 ModuleSP module_sp(GetModule());
1412 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001413 {
Greg Claytona1743492012-03-13 23:14:29 +00001414 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
1415 if (m_symtab_ap.get() == NULL)
1416 {
1417 m_symtab_ap.reset(new Symtab(this));
1418 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton3046e662013-07-10 01:23:25 +00001419 ParseSymtab ();
Greg Claytona1743492012-03-13 23:14:29 +00001420 m_symtab_ap->Finalize ();
1421 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001422 }
1423 return m_symtab_ap.get();
1424}
1425
Greg Clayton3046e662013-07-10 01:23:25 +00001426bool
1427ObjectFileMachO::IsStripped ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428{
Greg Clayton3046e662013-07-10 01:23:25 +00001429 if (m_dysymtab.cmd == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001430 {
Greg Clayton3046e662013-07-10 01:23:25 +00001431 ModuleSP module_sp(GetModule());
1432 if (module_sp)
Greg Claytona1743492012-03-13 23:14:29 +00001433 {
Greg Clayton3046e662013-07-10 01:23:25 +00001434 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1435 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton4d78c402012-05-25 18:09:55 +00001436 {
Greg Clayton3046e662013-07-10 01:23:25 +00001437 const lldb::offset_t load_cmd_offset = offset;
1438
1439 load_command lc;
1440 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
1441 break;
Charles Davis510938e2013-08-27 05:04:57 +00001442 if (lc.cmd == LC_DYSYMTAB)
Greg Clayton4d78c402012-05-25 18:09:55 +00001443 {
Greg Clayton3046e662013-07-10 01:23:25 +00001444 m_dysymtab.cmd = lc.cmd;
1445 m_dysymtab.cmdsize = lc.cmdsize;
1446 if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
1447 {
1448 // Clear m_dysymtab if we were unable to read all items from the load command
1449 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
1450 }
Greg Clayton4d78c402012-05-25 18:09:55 +00001451 }
Greg Clayton3046e662013-07-10 01:23:25 +00001452 offset = load_cmd_offset + lc.cmdsize;
Greg Clayton4d78c402012-05-25 18:09:55 +00001453 }
Greg Clayton1eac0c72012-04-24 03:06:13 +00001454 }
Greg Clayton1eac0c72012-04-24 03:06:13 +00001455 }
Greg Clayton3046e662013-07-10 01:23:25 +00001456 if (m_dysymtab.cmd)
Greg Clayton93e28612013-10-11 22:03:48 +00001457 return m_dysymtab.nlocalsym <= 1;
Greg Clayton3046e662013-07-10 01:23:25 +00001458 return false;
1459}
Greg Clayton1eac0c72012-04-24 03:06:13 +00001460
Greg Clayton3046e662013-07-10 01:23:25 +00001461void
1462ObjectFileMachO::CreateSections (SectionList &unified_section_list)
1463{
1464 if (!m_sections_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001465 {
Greg Clayton3046e662013-07-10 01:23:25 +00001466 m_sections_ap.reset(new SectionList());
1467
Charles Davis510938e2013-08-27 05:04:57 +00001468 const bool is_dsym = (m_header.filetype == MH_DSYM);
Greg Clayton3046e662013-07-10 01:23:25 +00001469 lldb::user_id_t segID = 0;
1470 lldb::user_id_t sectID = 0;
1471 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1472 uint32_t i;
1473 const bool is_core = GetType() == eTypeCoreFile;
1474 //bool dump_sections = false;
1475 ModuleSP module_sp (GetModule());
1476 // First look up any LC_ENCRYPTION_INFO load commands
1477 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
1478 EncryptedFileRanges encrypted_file_ranges;
1479 encryption_info_command encryption_cmd;
1480 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001481 {
Greg Clayton3046e662013-07-10 01:23:25 +00001482 const lldb::offset_t load_cmd_offset = offset;
1483 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
1484 break;
1485
Jason Molendadfb02a92015-04-02 05:19:33 +00001486 // LC_ENCRYPTION_INFO and LC_ENCRYPTION_INFO_64 have the same sizes for
1487 // the 3 fields we care about, so treat them the same.
1488 if (encryption_cmd.cmd == LC_ENCRYPTION_INFO || encryption_cmd.cmd == LC_ENCRYPTION_INFO_64)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489 {
Greg Clayton3046e662013-07-10 01:23:25 +00001490 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
Jason Molendaf6ce26f2013-04-10 05:58:57 +00001491 {
Greg Clayton3046e662013-07-10 01:23:25 +00001492 if (encryption_cmd.cryptid != 0)
Greg Claytond37d6922013-04-16 16:51:19 +00001493 {
Greg Clayton3046e662013-07-10 01:23:25 +00001494 EncryptedFileRanges::Entry entry;
1495 entry.SetRangeBase(encryption_cmd.cryptoff);
1496 entry.SetByteSize(encryption_cmd.cryptsize);
1497 encrypted_file_ranges.Append(entry);
Jason Molendaf6ce26f2013-04-10 05:58:57 +00001498 }
1499 }
Greg Clayton3046e662013-07-10 01:23:25 +00001500 }
1501 offset = load_cmd_offset + encryption_cmd.cmdsize;
1502 }
1503
Jason Molenda05a09c62014-08-22 02:46:46 +00001504 bool section_file_addresses_changed = false;
1505
Greg Clayton3046e662013-07-10 01:23:25 +00001506 offset = MachHeaderSizeFromMagic(m_header.magic);
1507
1508 struct segment_command_64 load_cmd;
1509 for (i=0; i<m_header.ncmds; ++i)
1510 {
1511 const lldb::offset_t load_cmd_offset = offset;
1512 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1513 break;
1514
Charles Davis510938e2013-08-27 05:04:57 +00001515 if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64)
Greg Clayton3046e662013-07-10 01:23:25 +00001516 {
1517 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001518 {
Greg Clayton3046e662013-07-10 01:23:25 +00001519 bool add_section = true;
1520 bool add_to_unified = true;
1521 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 +00001522
Greg Clayton3046e662013-07-10 01:23:25 +00001523 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1524 if (is_dsym && unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001525 {
Greg Clayton3046e662013-07-10 01:23:25 +00001526 if (const_segname == GetSegmentNameLINKEDIT())
1527 {
1528 // We need to keep the __LINKEDIT segment private to this object file only
1529 add_to_unified = false;
1530 }
1531 else
1532 {
1533 // This is the dSYM file and this section has already been created by
1534 // the object file, no need to create it.
1535 add_section = false;
1536 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001537 }
Greg Clayton3046e662013-07-10 01:23:25 +00001538 load_cmd.vmaddr = m_data.GetAddress(&offset);
1539 load_cmd.vmsize = m_data.GetAddress(&offset);
1540 load_cmd.fileoff = m_data.GetAddress(&offset);
1541 load_cmd.filesize = m_data.GetAddress(&offset);
1542 if (m_length != 0 && load_cmd.filesize != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001543 {
Greg Clayton3046e662013-07-10 01:23:25 +00001544 if (load_cmd.fileoff > m_length)
1545 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001546 // 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 +00001547 // a corrupt file. We don't have any way to return an error condition here (this method
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001548 // was likely invoked from something like ObjectFile::GetSectionList()) -- all we can do
Greg Clayton3046e662013-07-10 01:23:25 +00001549 // is null out the SectionList vector and if a process has been set up, dump a message
1550 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001551 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001552 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 +00001553 i,
1554 lc_segment_name,
1555 load_cmd.fileoff,
1556 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001557
1558 load_cmd.fileoff = 0;
1559 load_cmd.filesize = 0;
1560 }
1561
1562 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1563 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001564 // 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 +00001565 // a corrupt file. We don't have any way to return an error condition here (this method
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001566 // was likely invoked from something like ObjectFile::GetSectionList()) -- all we can do
Greg Clayton3046e662013-07-10 01:23:25 +00001567 // is null out the SectionList vector and if a process has been set up, dump a message
1568 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001569 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001570 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 +00001571 i,
1572 lc_segment_name,
1573 load_cmd.fileoff + load_cmd.filesize,
1574 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001575
1576 // Tuncase the length
1577 load_cmd.filesize = m_length - load_cmd.fileoff;
1578 }
1579 }
1580 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1581 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001582
Charles Davis510938e2013-08-27 05:04:57 +00001583 const bool segment_is_encrypted = (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001584
Greg Clayton3046e662013-07-10 01:23:25 +00001585 // Keep a list of mach segments around in case we need to
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001586 // get at data that isn't stored in the abstracted Sections.
Greg Clayton3046e662013-07-10 01:23:25 +00001587 m_mach_segments.push_back (load_cmd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001588
Greg Clayton3046e662013-07-10 01:23:25 +00001589 // Use a segment ID of the segment index shifted left by 8 so they
1590 // never conflict with any of the sections.
1591 SectionSP segment_sp;
1592 if (add_section && (const_segname || is_core))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001593 {
Greg Clayton3046e662013-07-10 01:23:25 +00001594 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
1595 this, // Object file to which this sections belongs
1596 ++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
1597 const_segname, // Name of this section
1598 eSectionTypeContainer, // This section is a container of other sections.
1599 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1600 load_cmd.vmsize, // VM size in bytes of this section
1601 load_cmd.fileoff, // Offset to the data for this section in the file
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001602 load_cmd.filesize, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +00001603 0, // Segments have no alignment information
Greg Clayton3046e662013-07-10 01:23:25 +00001604 load_cmd.flags)); // Flags for this section
Greg Clayton8d38ac42010-06-28 23:51:11 +00001605
Greg Clayton3046e662013-07-10 01:23:25 +00001606 segment_sp->SetIsEncrypted (segment_is_encrypted);
1607 m_sections_ap->AddSection(segment_sp);
1608 if (add_to_unified)
1609 unified_section_list.AddSection(segment_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001610 }
Greg Clayton3046e662013-07-10 01:23:25 +00001611 else if (unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001612 {
Jason Molenda20eb31b2013-08-16 03:20:42 +00001613 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr)
1614 {
1615 // Check to see if the module was read from memory?
1616 if (module_sp->GetObjectFile()->GetHeaderAddress().IsValid())
1617 {
1618 // We have a module that is in memory and needs to have its
1619 // file address adjusted. We need to do this because when we
1620 // load a file from memory, its addresses will be slid already,
1621 // yet the addresses in the new symbol file will still be unslid.
1622 // Since everything is stored as section offset, this shouldn't
1623 // cause any problems.
Jason Molenda5894a732013-08-17 03:39:52 +00001624
1625 // Make sure we've parsed the symbol table from the
1626 // ObjectFile before we go around changing its Sections.
1627 module_sp->GetObjectFile()->GetSymtab();
1628 // eh_frame would present the same problems but we parse that on
1629 // a per-function basis as-needed so it's more difficult to
1630 // remove its use of the Sections. Realistically, the environments
1631 // where this code path will be taken will not have eh_frame sections.
1632
Jason Molenda20eb31b2013-08-16 03:20:42 +00001633 unified_section_sp->SetFileAddress(load_cmd.vmaddr);
Jason Molenda05a09c62014-08-22 02:46:46 +00001634
1635 // Notify the module that the section addresses have been changed once
1636 // we're done so any file-address caches can be updated.
1637 section_file_addresses_changed = true;
Jason Molenda20eb31b2013-08-16 03:20:42 +00001638 }
1639 }
Greg Clayton3046e662013-07-10 01:23:25 +00001640 m_sections_ap->AddSection(unified_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001641 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001642
Greg Clayton3046e662013-07-10 01:23:25 +00001643 struct section_64 sect64;
1644 ::memset (&sect64, 0, sizeof(sect64));
1645 // Push a section into our mach sections for the section at
Charles Davis510938e2013-08-27 05:04:57 +00001646 // index zero (NO_SECT) if we don't have any mach sections yet...
Greg Clayton3046e662013-07-10 01:23:25 +00001647 if (m_mach_sections.empty())
1648 m_mach_sections.push_back(sect64);
1649 uint32_t segment_sect_idx;
1650 const lldb::user_id_t first_segment_sectID = sectID + 1;
1651
1652
Charles Davis510938e2013-08-27 05:04:57 +00001653 const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8;
Greg Clayton3046e662013-07-10 01:23:25 +00001654 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655 {
Greg Clayton3046e662013-07-10 01:23:25 +00001656 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
Greg Clayton89411422010-10-08 00:21:05 +00001657 break;
Greg Clayton3046e662013-07-10 01:23:25 +00001658 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1659 break;
1660 sect64.addr = m_data.GetAddress(&offset);
1661 sect64.size = m_data.GetAddress(&offset);
1662
1663 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1664 break;
1665
1666 // Keep a list of mach sections around in case we need to
1667 // get at data that isn't stored in the abstracted Sections.
1668 m_mach_sections.push_back (sect64);
1669
1670 if (add_section)
1671 {
1672 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1673 if (!const_segname)
1674 {
1675 // We have a segment with no name so we need to conjure up
1676 // segments that correspond to the section's segname if there
1677 // isn't already such a section. If there is such a section,
1678 // we resize the section so that it spans all sections.
1679 // We also mark these sections as fake so address matches don't
1680 // hit if they land in the gaps between the child sections.
1681 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1682 segment_sp = unified_section_list.FindSectionByName (const_segname);
1683 if (segment_sp.get())
1684 {
1685 Section *segment = segment_sp.get();
1686 // Grow the section size as needed.
1687 const lldb::addr_t sect64_min_addr = sect64.addr;
1688 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1689 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1690 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1691 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1692 if (sect64_min_addr >= curr_seg_min_addr)
1693 {
1694 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1695 // Only grow the section size if needed
1696 if (new_seg_byte_size > curr_seg_byte_size)
1697 segment->SetByteSize (new_seg_byte_size);
1698 }
1699 else
1700 {
1701 // We need to change the base address of the segment and
1702 // adjust the child section offsets for all existing children.
1703 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1704 segment->Slide(slide_amount, false);
1705 segment->GetChildren().Slide(-slide_amount, false);
1706 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1707 }
1708
1709 // Grow the section size as needed.
1710 if (sect64.offset)
1711 {
1712 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1713 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1714
1715 const lldb::addr_t section_min_file_offset = sect64.offset;
1716 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1717 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1718 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1719 segment->SetFileOffset (new_file_offset);
1720 segment->SetFileSize (new_file_size);
1721 }
1722 }
1723 else
1724 {
1725 // Create a fake section for the section's named segment
1726 segment_sp.reset(new Section (segment_sp, // Parent section
1727 module_sp, // Module to which this section belongs
1728 this, // Object file to which this section belongs
1729 ++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
1730 const_segname, // Name of this section
1731 eSectionTypeContainer, // This section is a container of other sections.
1732 sect64.addr, // File VM address == addresses as they are found in the object file
1733 sect64.size, // VM size in bytes of this section
1734 sect64.offset, // Offset to the data for this section in the file
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001735 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +00001736 sect64.align,
Greg Clayton3046e662013-07-10 01:23:25 +00001737 load_cmd.flags)); // Flags for this section
1738 segment_sp->SetIsFake(true);
1739
1740 m_sections_ap->AddSection(segment_sp);
1741 if (add_to_unified)
1742 unified_section_list.AddSection(segment_sp);
1743 segment_sp->SetIsEncrypted (segment_is_encrypted);
1744 }
1745 }
1746 assert (segment_sp.get());
1747
Charles Davis510938e2013-08-27 05:04:57 +00001748 lldb::SectionType sect_type = eSectionTypeOther;
Greg Clayton3046e662013-07-10 01:23:25 +00001749
Greg Clayton38f9cc42014-06-16 22:53:16 +00001750 if (sect64.flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS))
1751 sect_type = eSectionTypeCode;
1752 else
Greg Clayton3046e662013-07-10 01:23:25 +00001753 {
Greg Clayton38f9cc42014-06-16 22:53:16 +00001754 uint32_t mach_sect_type = sect64.flags & SECTION_TYPE;
1755 static ConstString g_sect_name_objc_data ("__objc_data");
1756 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1757 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1758 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1759 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1760 static ConstString g_sect_name_objc_const ("__objc_const");
1761 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1762 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton3046e662013-07-10 01:23:25 +00001763
Greg Clayton38f9cc42014-06-16 22:53:16 +00001764 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1765 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1766 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1767 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1768 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1769 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1770 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1771 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1772 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1773 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1774 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1775 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1776 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1777 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1778 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1779 static ConstString g_sect_name_eh_frame ("__eh_frame");
Jason Molendae589e7e2014-12-08 03:09:00 +00001780 static ConstString g_sect_name_compact_unwind ("__unwind_info");
Greg Clayton38f9cc42014-06-16 22:53:16 +00001781 static ConstString g_sect_name_text ("__text");
1782 static ConstString g_sect_name_data ("__data");
Ryan Brown65d4d5c2015-09-16 21:20:44 +00001783 static ConstString g_sect_name_go_symtab ("__gosymtab");
Greg Clayton38f9cc42014-06-16 22:53:16 +00001784
1785
1786 if (section_name == g_sect_name_dwarf_debug_abbrev)
1787 sect_type = eSectionTypeDWARFDebugAbbrev;
1788 else if (section_name == g_sect_name_dwarf_debug_aranges)
1789 sect_type = eSectionTypeDWARFDebugAranges;
1790 else if (section_name == g_sect_name_dwarf_debug_frame)
1791 sect_type = eSectionTypeDWARFDebugFrame;
1792 else if (section_name == g_sect_name_dwarf_debug_info)
1793 sect_type = eSectionTypeDWARFDebugInfo;
1794 else if (section_name == g_sect_name_dwarf_debug_line)
1795 sect_type = eSectionTypeDWARFDebugLine;
1796 else if (section_name == g_sect_name_dwarf_debug_loc)
1797 sect_type = eSectionTypeDWARFDebugLoc;
1798 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1799 sect_type = eSectionTypeDWARFDebugMacInfo;
1800 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1801 sect_type = eSectionTypeDWARFDebugPubNames;
1802 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1803 sect_type = eSectionTypeDWARFDebugPubTypes;
1804 else if (section_name == g_sect_name_dwarf_debug_ranges)
1805 sect_type = eSectionTypeDWARFDebugRanges;
1806 else if (section_name == g_sect_name_dwarf_debug_str)
1807 sect_type = eSectionTypeDWARFDebugStr;
1808 else if (section_name == g_sect_name_dwarf_apple_names)
1809 sect_type = eSectionTypeDWARFAppleNames;
1810 else if (section_name == g_sect_name_dwarf_apple_types)
1811 sect_type = eSectionTypeDWARFAppleTypes;
1812 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1813 sect_type = eSectionTypeDWARFAppleNamespaces;
1814 else if (section_name == g_sect_name_dwarf_apple_objc)
1815 sect_type = eSectionTypeDWARFAppleObjC;
1816 else if (section_name == g_sect_name_objc_selrefs)
1817 sect_type = eSectionTypeDataCStringPointers;
1818 else if (section_name == g_sect_name_objc_msgrefs)
1819 sect_type = eSectionTypeDataObjCMessageRefs;
1820 else if (section_name == g_sect_name_eh_frame)
1821 sect_type = eSectionTypeEHFrame;
Jason Molendae589e7e2014-12-08 03:09:00 +00001822 else if (section_name == g_sect_name_compact_unwind)
1823 sect_type = eSectionTypeCompactUnwind;
Greg Clayton38f9cc42014-06-16 22:53:16 +00001824 else if (section_name == g_sect_name_cfstring)
1825 sect_type = eSectionTypeDataObjCCFStrings;
Ryan Brown65d4d5c2015-09-16 21:20:44 +00001826 else if (section_name == g_sect_name_go_symtab)
1827 sect_type = eSectionTypeGoSymtab;
Greg Clayton38f9cc42014-06-16 22:53:16 +00001828 else if (section_name == g_sect_name_objc_data ||
1829 section_name == g_sect_name_objc_classrefs ||
1830 section_name == g_sect_name_objc_superrefs ||
1831 section_name == g_sect_name_objc_const ||
1832 section_name == g_sect_name_objc_classlist)
Greg Clayton3046e662013-07-10 01:23:25 +00001833 {
Greg Clayton38f9cc42014-06-16 22:53:16 +00001834 sect_type = eSectionTypeDataPointers;
1835 }
1836
1837 if (sect_type == eSectionTypeOther)
1838 {
1839 switch (mach_sect_type)
1840 {
1841 // TODO: categorize sections by other flags for regular sections
1842 case S_REGULAR:
1843 if (section_name == g_sect_name_text)
1844 sect_type = eSectionTypeCode;
1845 else if (section_name == g_sect_name_data)
1846 sect_type = eSectionTypeData;
1847 else
1848 sect_type = eSectionTypeOther;
1849 break;
1850 case S_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1851 case S_CSTRING_LITERALS: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1852 case S_4BYTE_LITERALS: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1853 case S_8BYTE_LITERALS: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1854 case S_LITERAL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1855 case S_NON_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1856 case S_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1857 case S_SYMBOL_STUBS: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1858 case S_MOD_INIT_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1859 case S_MOD_TERM_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1860 case S_COALESCED: sect_type = eSectionTypeOther; break;
1861 case S_GB_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1862 case S_INTERPOSING: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1863 case S_16BYTE_LITERALS: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1864 case S_DTRACE_DOF: sect_type = eSectionTypeDebug; break;
1865 case S_LAZY_DYLIB_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break;
1866 default: break;
1867 }
Greg Clayton3046e662013-07-10 01:23:25 +00001868 }
1869 }
1870
1871 SectionSP section_sp(new Section (segment_sp,
1872 module_sp,
1873 this,
1874 ++sectID,
1875 section_name,
1876 sect_type,
1877 sect64.addr - segment_sp->GetFileAddress(),
1878 sect64.size,
1879 sect64.offset,
1880 sect64.offset == 0 ? 0 : sect64.size,
Greg Clayton48672af2014-06-24 22:22:43 +00001881 sect64.align,
Greg Clayton3046e662013-07-10 01:23:25 +00001882 sect64.flags));
1883 // Set the section to be encrypted to match the segment
1884
1885 bool section_is_encrypted = false;
1886 if (!segment_is_encrypted && load_cmd.filesize != 0)
1887 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1888
1889 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1890 segment_sp->GetChildren().AddSection(section_sp);
1891
1892 if (segment_sp->IsFake())
1893 {
1894 segment_sp.reset();
1895 const_segname.Clear();
1896 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001897 }
1898 }
Greg Clayton3046e662013-07-10 01:23:25 +00001899 if (segment_sp && is_dsym)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001900 {
Greg Clayton3046e662013-07-10 01:23:25 +00001901 if (first_segment_sectID <= sectID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001902 {
Greg Clayton3046e662013-07-10 01:23:25 +00001903 lldb::user_id_t sect_uid;
1904 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001905 {
Greg Clayton3046e662013-07-10 01:23:25 +00001906 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1907 SectionSP next_section_sp;
1908 if (sect_uid + 1 <= sectID)
1909 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1910
1911 if (curr_section_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001912 {
Greg Clayton3046e662013-07-10 01:23:25 +00001913 if (curr_section_sp->GetByteSize() == 0)
1914 {
1915 if (next_section_sp.get() != NULL)
1916 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1917 else
1918 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1919 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001920 }
1921 }
1922 }
1923 }
1924 }
1925 }
1926 }
Charles Davis510938e2013-08-27 05:04:57 +00001927 else if (load_cmd.cmd == LC_DYSYMTAB)
Greg Clayton3046e662013-07-10 01:23:25 +00001928 {
1929 m_dysymtab.cmd = load_cmd.cmd;
1930 m_dysymtab.cmdsize = load_cmd.cmdsize;
1931 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1932 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001933
Greg Clayton3046e662013-07-10 01:23:25 +00001934 offset = load_cmd_offset + load_cmd.cmdsize;
1935 }
Jason Molenda05a09c62014-08-22 02:46:46 +00001936
1937
1938 if (section_file_addresses_changed && module_sp.get())
1939 {
1940 module_sp->SectionFileAddressesChanged();
1941 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001942 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001943}
1944
1945class MachSymtabSectionInfo
1946{
1947public:
1948
1949 MachSymtabSectionInfo (SectionList *section_list) :
1950 m_section_list (section_list),
1951 m_section_infos()
1952 {
1953 // Get the number of sections down to a depth of 1 to include
1954 // all segments and their sections, but no other sections that
1955 // may be added for debug map or
1956 m_section_infos.resize(section_list->GetNumSections(1));
1957 }
1958
1959
Greg Claytone72dfb32012-02-24 01:59:29 +00001960 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001961 GetSection (uint8_t n_sect, addr_t file_addr)
1962 {
1963 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00001964 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965 if (n_sect < m_section_infos.size())
1966 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001967 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001968 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001969 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1970 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001971 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00001972 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001973 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1974 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00001975 }
1976 else
1977 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001978 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00001979 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001980 }
1981 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00001982 {
1983 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00001984 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001985 }
1986 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1987 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1988 {
1989 // Symbol is in section with zero size, but has the same start
1990 // address as the section. This can happen with linker symbols
1991 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00001992 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001993 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001994 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001995 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001996 }
1997
1998protected:
1999 struct SectionInfo
2000 {
2001 SectionInfo () :
2002 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00002003 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002004 {
2005 }
2006
2007 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00002008 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002009 };
2010 SectionList *m_section_list;
2011 std::vector<SectionInfo> m_section_infos;
2012};
2013
Greg Clayton9191db42013-10-21 18:40:51 +00002014struct TrieEntry
2015{
2016 TrieEntry () :
2017 name(),
2018 address(LLDB_INVALID_ADDRESS),
2019 flags (0),
2020 other(0),
2021 import_name()
2022 {
2023 }
2024
2025 void
2026 Clear ()
2027 {
2028 name.Clear();
2029 address = LLDB_INVALID_ADDRESS;
2030 flags = 0;
2031 other = 0;
2032 import_name.Clear();
2033 }
2034
2035 void
2036 Dump () const
2037 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002038 printf ("0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"",
2039 static_cast<unsigned long long>(address),
2040 static_cast<unsigned long long>(flags),
2041 static_cast<unsigned long long>(other), name.GetCString());
Greg Clayton9191db42013-10-21 18:40:51 +00002042 if (import_name)
2043 printf (" -> \"%s\"\n", import_name.GetCString());
2044 else
2045 printf ("\n");
2046 }
2047 ConstString name;
2048 uint64_t address;
2049 uint64_t flags;
2050 uint64_t other;
2051 ConstString import_name;
2052};
2053
2054struct TrieEntryWithOffset
2055{
2056 lldb::offset_t nodeOffset;
2057 TrieEntry entry;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002058
Greg Clayton9191db42013-10-21 18:40:51 +00002059 TrieEntryWithOffset (lldb::offset_t offset) :
2060 nodeOffset (offset),
2061 entry()
2062 {
2063 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002064
Greg Clayton9191db42013-10-21 18:40:51 +00002065 void
2066 Dump (uint32_t idx) const
2067 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002068 printf ("[%3u] 0x%16.16llx: ", idx,
2069 static_cast<unsigned long long>(nodeOffset));
Greg Clayton9191db42013-10-21 18:40:51 +00002070 entry.Dump();
2071 }
2072
2073 bool
2074 operator<(const TrieEntryWithOffset& other) const
2075 {
2076 return ( nodeOffset < other.nodeOffset );
2077 }
2078};
2079
2080static void
2081ParseTrieEntries (DataExtractor &data,
2082 lldb::offset_t offset,
Greg Claytonb887da12015-07-16 19:50:57 +00002083 const bool is_arm,
Greg Clayton9191db42013-10-21 18:40:51 +00002084 std::vector<llvm::StringRef> &nameSlices,
2085 std::set<lldb::addr_t> &resolver_addresses,
2086 std::vector<TrieEntryWithOffset>& output)
2087{
2088 if (!data.ValidOffset(offset))
2089 return;
2090
2091 const uint64_t terminalSize = data.GetULEB128(&offset);
2092 lldb::offset_t children_offset = offset + terminalSize;
2093 if ( terminalSize != 0 ) {
2094 TrieEntryWithOffset e (offset);
2095 e.entry.flags = data.GetULEB128(&offset);
2096 const char *import_name = NULL;
2097 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
2098 e.entry.address = 0;
2099 e.entry.other = data.GetULEB128(&offset); // dylib ordinal
2100 import_name = data.GetCStr(&offset);
2101 }
2102 else {
2103 e.entry.address = data.GetULEB128(&offset);
2104 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
2105 {
Greg Clayton9191db42013-10-21 18:40:51 +00002106 e.entry.other = data.GetULEB128(&offset);
Greg Claytonb887da12015-07-16 19:50:57 +00002107 uint64_t resolver_addr = e.entry.other;
2108 if (is_arm)
2109 resolver_addr &= THUMB_ADDRESS_BIT_MASK;
2110 resolver_addresses.insert(resolver_addr);
Greg Clayton9191db42013-10-21 18:40:51 +00002111 }
2112 else
2113 e.entry.other = 0;
2114 }
2115 // Only add symbols that are reexport symbols with a valid import name
2116 if (EXPORT_SYMBOL_FLAGS_REEXPORT & e.entry.flags && import_name && import_name[0])
2117 {
2118 std::string name;
2119 if (!nameSlices.empty())
2120 {
2121 for (auto name_slice: nameSlices)
2122 name.append(name_slice.data(), name_slice.size());
2123 }
2124 if (name.size() > 1)
2125 {
2126 // Skip the leading '_'
2127 e.entry.name.SetCStringWithLength(name.c_str() + 1,name.size() - 1);
2128 }
2129 if (import_name)
2130 {
2131 // Skip the leading '_'
2132 e.entry.import_name.SetCString(import_name+1);
2133 }
2134 output.push_back(e);
2135 }
2136 }
2137
2138 const uint8_t childrenCount = data.GetU8(&children_offset);
2139 for (uint8_t i=0; i < childrenCount; ++i) {
2140 nameSlices.push_back(data.GetCStr(&children_offset));
2141 lldb::offset_t childNodeOffset = data.GetULEB128(&children_offset);
2142 if (childNodeOffset)
2143 {
2144 ParseTrieEntries(data,
2145 childNodeOffset,
Greg Claytonb887da12015-07-16 19:50:57 +00002146 is_arm,
Greg Clayton9191db42013-10-21 18:40:51 +00002147 nameSlices,
2148 resolver_addresses,
2149 output);
2150 }
2151 nameSlices.pop_back();
2152 }
2153}
2154
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002155size_t
Greg Clayton3046e662013-07-10 01:23:25 +00002156ObjectFileMachO::ParseSymtab ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002157{
2158 Timer scoped_timer(__PRETTY_FUNCTION__,
2159 "ObjectFileMachO::ParseSymtab () module = %s",
2160 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002161 ModuleSP module_sp (GetModule());
2162 if (!module_sp)
2163 return 0;
2164
2165 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
2166 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
Greg Clayton9191db42013-10-21 18:40:51 +00002167 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 +00002168 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
2169 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00002170 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002171 uint32_t i;
Greg Clayton9191db42013-10-21 18:40:51 +00002172 FileSpecList dylib_files;
Greg Clayton5160ce52013-03-27 23:08:40 +00002173 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton1e28adf2015-02-25 17:25:02 +00002174 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2175 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2176 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
Greg Clayton77ccca72011-12-30 00:32:24 +00002177
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002178 for (i=0; i<m_header.ncmds; ++i)
2179 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002180 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002181 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002182 struct load_command lc;
2183 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002184 break;
2185 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002186 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002187 {
Charles Davis510938e2013-08-27 05:04:57 +00002188 case LC_SYMTAB:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002189 symtab_load_command.cmd = lc.cmd;
2190 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002191 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002192 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
2193 return 0;
2194 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002195 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002196 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002197 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002198 return 0;
2199 }
2200
2201 if (symtab_load_command.stroff == 0)
2202 {
2203 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002204 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002205 return 0;
2206 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002207
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002208 if (symtab_load_command.nsyms == 0)
2209 {
2210 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002211 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002212 return 0;
2213 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002214
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002215 if (symtab_load_command.strsize == 0)
2216 {
2217 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002218 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002219 return 0;
2220 }
2221 break;
2222
Greg Clayton9191db42013-10-21 18:40:51 +00002223 case LC_DYLD_INFO:
2224 case LC_DYLD_INFO_ONLY:
2225 if (m_data.GetU32(&offset, &dyld_info.rebase_off, 10))
2226 {
2227 dyld_info.cmd = lc.cmd;
2228 dyld_info.cmdsize = lc.cmdsize;
2229 }
2230 else
2231 {
2232 memset (&dyld_info, 0, sizeof(dyld_info));
2233 }
2234 break;
2235
2236 case LC_LOAD_DYLIB:
2237 case LC_LOAD_WEAK_DYLIB:
2238 case LC_REEXPORT_DYLIB:
2239 case LC_LOADFVMLIB:
2240 case LC_LOAD_UPWARD_DYLIB:
2241 {
2242 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
2243 const char *path = m_data.PeekCStr(name_offset);
2244 if (path)
2245 {
2246 FileSpec file_spec(path, false);
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002247 // Strip the path if there is @rpath, @executable, etc so we just use the basename
Greg Clayton9191db42013-10-21 18:40:51 +00002248 if (path[0] == '@')
2249 file_spec.GetDirectory().Clear();
Jim Inghamfbe0b9a2014-05-21 03:58:03 +00002250
2251 if (lc.cmd == LC_REEXPORT_DYLIB)
2252 {
2253 m_reexported_dylibs.AppendIfUnique(file_spec);
2254 }
Greg Clayton9191db42013-10-21 18:40:51 +00002255
2256 dylib_files.Append(file_spec);
2257 }
2258 }
2259 break;
2260
Charles Davis510938e2013-08-27 05:04:57 +00002261 case LC_FUNCTION_STARTS:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002262 function_starts_load_command.cmd = lc.cmd;
2263 function_starts_load_command.cmdsize = lc.cmdsize;
2264 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 +00002265 memset (&function_starts_load_command, 0, sizeof(function_starts_load_command));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002266 break;
2267
2268 default:
2269 break;
2270 }
2271 offset = cmd_offset + lc.cmdsize;
2272 }
2273
2274 if (symtab_load_command.cmd)
2275 {
2276 Symtab *symtab = m_symtab_ap.get();
2277 SectionList *section_list = GetSectionList();
2278 if (section_list == NULL)
2279 return 0;
2280
Greg Claytonc7bece562013-01-25 18:06:21 +00002281 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
2282 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002283 bool bit_width_32 = addr_byte_size == 4;
2284 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
2285
Greg Claytonc7bece562013-01-25 18:06:21 +00002286 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
2287 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
2288 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00002289 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00002290 DataExtractor dyld_trie_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002291
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002292 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
2293 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00002294 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
Greg Claytonfd814c52013-08-13 01:42:25 +00002295
2296 ProcessSP process_sp (m_process_wp.lock());
2297 Process *process = process_sp.get();
2298
Greg Clayton86eac942013-08-13 21:32:34 +00002299 uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete;
2300
Greg Clayton48672af2014-06-24 22:22:43 +00002301 if (process && m_header.filetype != llvm::MachO::MH_OBJECT)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002302 {
Greg Clayton4c82d422012-05-18 23:20:01 +00002303 Target &target = process->GetTarget();
Greg Claytonfd814c52013-08-13 01:42:25 +00002304
Greg Clayton86eac942013-08-13 21:32:34 +00002305 memory_module_load_level = target.GetMemoryModuleLoadLevel();
Greg Claytonfd814c52013-08-13 01:42:25 +00002306
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002307 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2308 // Reading mach file from memory in a process or core file...
2309
2310 if (linkedit_section_sp)
2311 {
Greg Clayton07347372015-06-08 21:53:11 +00002312 addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
2313 if (linkedit_load_addr == LLDB_INVALID_ADDRESS)
2314 {
2315 // We might be trying to access the symbol table before the __LINKEDIT's load
2316 // address has been set in the target. We can't fail to read the symbol table,
2317 // so calculate the right address manually
2318 linkedit_load_addr = CalculateSectionLoadAddressForMemoryImage(m_memory_addr, GetMachHeaderSection(), linkedit_section_sp.get());
2319 }
2320
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002321 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2322 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00002323 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00002324
2325 bool data_was_read = false;
2326
Todd Fiala013434e2014-07-09 01:29:05 +00002327#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molendaa3329782014-03-29 18:54:20 +00002328 if (m_header.flags & 0x80000000u && process->GetAddressByteSize() == sizeof (void*))
Greg Clayton77ccca72011-12-30 00:32:24 +00002329 {
Greg Clayton26b47e22012-04-18 05:19:20 +00002330 // This mach-o memory file is in the dyld shared cache. If this
2331 // program is not remote and this is iOS, then this process will
2332 // share the same shared cache as the process we are debugging and
2333 // we can read the entire __LINKEDIT from the address space in this
2334 // process. This is a needed optimization that is used for local iOS
2335 // debugging only since all shared libraries in the shared cache do
2336 // not have corresponding files that exist in the file system of the
2337 // device. They have been combined into a single file. This means we
2338 // always have to load these files from memory. All of the symbol and
2339 // string tables from all of the __LINKEDIT sections from the shared
2340 // libraries in the shared cache have been merged into a single large
2341 // symbol and string table. Reading all of this symbol and string table
2342 // data across can slow down debug launch times, so we optimize this by
2343 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00002344
2345 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
2346 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
2347 bool use_lldb_cache = true;
2348 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
2349 {
2350 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002351 ModuleSP module_sp (GetModule());
2352 if (module_sp)
2353 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
2354
Jason Molenda0e0954c2013-04-16 06:24:42 +00002355 }
2356
Greg Clayton26b47e22012-04-18 05:19:20 +00002357 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00002358 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00002359 {
2360 data_was_read = true;
2361 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00002362 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00002363 if (function_starts_load_command.cmd)
2364 {
2365 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2366 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
2367 }
2368 }
2369 }
2370#endif
2371
2372 if (!data_was_read)
2373 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002374 if (memory_module_load_level == eMemoryModuleLoadLevelComplete)
Jason Molendad34e6522013-02-05 22:31:24 +00002375 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002376 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
2377 if (nlist_data_sp)
2378 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2379 // Load strings individually from memory when loading from memory since shared cache
2380 // string tables contain strings for all symbols from all shared cached libraries
2381 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
2382 //if (strtab_data_sp)
2383 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
2384 if (m_dysymtab.nindirectsyms != 0)
2385 {
2386 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
2387 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
2388 if (indirect_syms_data_sp)
2389 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
2390 }
Jason Molendad34e6522013-02-05 22:31:24 +00002391 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002392
2393 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial)
Greg Clayton26b47e22012-04-18 05:19:20 +00002394 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002395 if (function_starts_load_command.cmd)
2396 {
2397 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2398 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
2399 if (func_start_data_sp)
2400 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
2401 }
Greg Clayton26b47e22012-04-18 05:19:20 +00002402 }
Greg Clayton77ccca72011-12-30 00:32:24 +00002403 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002404 }
2405 }
2406 else
2407 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002408 nlist_data.SetData (m_data,
2409 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002410 nlist_data_byte_size);
2411 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00002412 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002413 strtab_data_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00002414
2415 if (dyld_info.export_size > 0)
2416 {
2417 dyld_trie_data.SetData (m_data,
2418 dyld_info.export_off,
2419 dyld_info.export_size);
2420 }
2421
Jason Molendad34e6522013-02-05 22:31:24 +00002422 if (m_dysymtab.nindirectsyms != 0)
2423 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002424 indirect_symbol_index_data.SetData (m_data,
2425 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00002426 m_dysymtab.nindirectsyms * 4);
2427 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002428 if (function_starts_load_command.cmd)
2429 {
2430 function_starts_data.SetData (m_data,
2431 function_starts_load_command.dataoff,
2432 function_starts_load_command.datasize);
2433 }
2434 }
Greg Clayton77ccca72011-12-30 00:32:24 +00002435
Greg Clayton86eac942013-08-13 21:32:34 +00002436 if (nlist_data.GetByteSize() == 0 && memory_module_load_level == eMemoryModuleLoadLevelComplete)
2437 {
2438 if (log)
2439 module_sp->LogMessage(log, "failed to read nlist data");
2440 return 0;
2441 }
2442
2443
Greg Claytondebb8812012-05-25 17:04:00 +00002444 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2445 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00002446 {
Greg Claytondebb8812012-05-25 17:04:00 +00002447 if (process)
2448 {
2449 if (strtab_addr == LLDB_INVALID_ADDRESS)
2450 {
2451 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002452 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00002453 return 0;
2454 }
2455 }
2456 else
Greg Clayton4c82d422012-05-18 23:20:01 +00002457 {
2458 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002459 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00002460 return 0;
2461 }
2462 }
Greg Clayton4c82d422012-05-18 23:20:01 +00002463
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002464 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
2465 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
Greg Claytona381e102015-07-27 23:21:05 +00002466 const ConstString &g_segment_name_DATA_DIRTY = GetSegmentNameDATA_DIRTY();
2467 const ConstString &g_segment_name_DATA_CONST = GetSegmentNameDATA_CONST();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002468 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
2469 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
2470 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
2471 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
Greg Claytona381e102015-07-27 23:21:05 +00002472 SectionSP data_dirty_section_sp(section_list->FindSectionByName(g_segment_name_DATA_DIRTY));
2473 SectionSP data_const_section_sp(section_list->FindSectionByName(g_segment_name_DATA_CONST));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002474 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
2475 SectionSP eh_frame_section_sp;
2476 if (text_section_sp.get())
2477 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
2478 else
2479 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
2480
Charles Davis510938e2013-08-27 05:04:57 +00002481 const bool is_arm = (m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
Jason Molenda5635f772013-03-21 03:36:01 +00002482
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002483 // lldb works best if it knows the start address of all functions in a module.
Jason Molenda5635f772013-03-21 03:36:01 +00002484 // Linker symbols or debug info are normally the best source of information for start addr / size but
2485 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00002486 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00002487 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
2488 // binary, relative to the text section.
2489 // eh_frame - the eh_frame FDEs have the start addr & size of each function
2490 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
2491 // Binaries built to run on older releases may need to use eh_frame information.
2492
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002493 if (text_section_sp && function_starts_data.GetByteSize())
2494 {
2495 FunctionStarts::Entry function_start_entry;
2496 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00002497 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002498 function_start_entry.addr = text_section_sp->GetFileAddress();
2499 uint64_t delta;
2500 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
2501 {
2502 // Now append the current entry
2503 function_start_entry.addr += delta;
2504 function_starts.Append(function_start_entry);
2505 }
Jason Molendad63d3c72013-04-16 00:18:44 +00002506 }
Jason Molenda5635f772013-03-21 03:36:01 +00002507 else
2508 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00002509 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
2510 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
2511 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
2512 // the module.
2513 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00002514 {
Jason Molendaa18f7072015-08-15 01:21:01 +00002515 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindEHFrame, true);
Jason Molenda5635f772013-03-21 03:36:01 +00002516 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
2517 eh_frame.GetFunctionAddressAndSizeVector (functions);
2518 addr_t text_base_addr = text_section_sp->GetFileAddress();
2519 size_t count = functions.GetSize();
2520 for (size_t i = 0; i < count; ++i)
2521 {
2522 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
2523 if (func)
2524 {
2525 FunctionStarts::Entry function_start_entry;
2526 function_start_entry.addr = func->base - text_base_addr;
2527 function_starts.Append(function_start_entry);
2528 }
2529 }
2530 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002531 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002532
Greg Claytonc7bece562013-01-25 18:06:21 +00002533 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002534
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +00002535 const user_id_t TEXT_eh_frame_sectID =
2536 eh_frame_section_sp.get() ? eh_frame_section_sp->GetID()
2537 : static_cast<user_id_t>(NO_SECT);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002538
Greg Claytonc7bece562013-01-25 18:06:21 +00002539 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002540
2541 uint32_t N_SO_index = UINT32_MAX;
2542
2543 MachSymtabSectionInfo section_info (section_list);
2544 std::vector<uint32_t> N_FUN_indexes;
2545 std::vector<uint32_t> N_NSYM_indexes;
2546 std::vector<uint32_t> N_INCL_indexes;
2547 std::vector<uint32_t> N_BRAC_indexes;
2548 std::vector<uint32_t> N_COMM_indexes;
Greg Claytond81088c2014-01-16 01:38:29 +00002549 typedef std::multimap <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002550 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00002551 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002552 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2553 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00002554 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002555 // Any symbols that get merged into another will get an entry
2556 // in this map so we know
2557 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2558 uint32_t nlist_idx = 0;
2559 Symbol *symbol_ptr = NULL;
2560
2561 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00002562 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00002563 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00002564 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00002565 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002566
Jim Inghamea3ac272014-01-10 22:55:37 +00002567 std::vector<TrieEntryWithOffset> trie_entries;
2568 std::set<lldb::addr_t> resolver_addresses;
2569
2570 if (dyld_trie_data.GetByteSize() > 0)
2571 {
2572 std::vector<llvm::StringRef> nameSlices;
2573 ParseTrieEntries (dyld_trie_data,
2574 0,
Greg Claytonb887da12015-07-16 19:50:57 +00002575 is_arm,
Jim Inghamea3ac272014-01-10 22:55:37 +00002576 nameSlices,
2577 resolver_addresses,
2578 trie_entries);
2579
2580 ConstString text_segment_name ("__TEXT");
2581 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
2582 if (text_segment_sp)
2583 {
2584 const lldb::addr_t text_segment_file_addr = text_segment_sp->GetFileAddress();
2585 if (text_segment_file_addr != LLDB_INVALID_ADDRESS)
2586 {
2587 for (auto &e : trie_entries)
2588 e.entry.address += text_segment_file_addr;
2589 }
2590 }
2591 }
2592
Greg Claytonb65c6292015-02-20 22:20:05 +00002593 typedef std::set<ConstString> IndirectSymbols;
2594 IndirectSymbols indirect_symbol_names;
2595
Todd Fiala013434e2014-07-09 01:29:05 +00002596#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molendaa5609c82012-06-21 01:51:02 +00002597
2598 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
2599 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
2600 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
2601 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
2602 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
2603 // nlist parser to ignore all LOCAL symbols.
2604
2605 if (m_header.flags & 0x80000000u)
2606 {
2607 // Before we can start mapping the DSC, we need to make certain the target process is actually
2608 // using the cache we can find.
2609
Jason Molendaa5609c82012-06-21 01:51:02 +00002610 // Next we need to determine the correct path for the dyld shared cache.
2611
Greg Clayton7ab7f892014-05-29 21:33:45 +00002612 ArchSpec header_arch;
2613 GetArchitecture(header_arch);
Jason Molendaa5609c82012-06-21 01:51:02 +00002614 char dsc_path[PATH_MAX];
2615
2616 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00002617 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
2618 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00002619 header_arch.GetArchitectureName());
2620
2621 FileSpec dsc_filespec(dsc_path, false);
2622
2623 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molendad63d3c72013-04-16 00:18:44 +00002624 struct lldb_copy_dyld_cache_header_v0
Greg Clayton946f8902012-09-05 22:30:51 +00002625 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002626 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
2627 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
2628 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda4e7511e2013-03-06 23:19:17 +00002629 uint32_t imagesOffset;
2630 uint32_t imagesCount;
2631 uint64_t dyldBaseAddress;
2632 uint64_t codeSignatureOffset;
2633 uint64_t codeSignatureSize;
2634 uint64_t slideInfoOffset;
2635 uint64_t slideInfoSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00002636 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
2637 uint64_t localSymbolsSize; // size of local symbols information
2638 };
2639 struct lldb_copy_dyld_cache_header_v1
2640 {
2641 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
2642 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
2643 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
2644 uint32_t imagesOffset;
2645 uint32_t imagesCount;
2646 uint64_t dyldBaseAddress;
2647 uint64_t codeSignatureOffset;
2648 uint64_t codeSignatureSize;
2649 uint64_t slideInfoOffset;
2650 uint64_t slideInfoSize;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002651 uint64_t localSymbolsOffset;
2652 uint64_t localSymbolsSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00002653 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Clayton946f8902012-09-05 22:30:51 +00002654 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00002655
Jason Molendad63d3c72013-04-16 00:18:44 +00002656 struct lldb_copy_dyld_cache_mapping_info
2657 {
2658 uint64_t address;
2659 uint64_t size;
2660 uint64_t fileOffset;
2661 uint32_t maxProt;
2662 uint32_t initProt;
2663 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00002664
Greg Clayton946f8902012-09-05 22:30:51 +00002665 struct lldb_copy_dyld_cache_local_symbols_info
2666 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002667 uint32_t nlistOffset;
2668 uint32_t nlistCount;
2669 uint32_t stringsOffset;
2670 uint32_t stringsSize;
2671 uint32_t entriesOffset;
2672 uint32_t entriesCount;
Greg Clayton946f8902012-09-05 22:30:51 +00002673 };
2674 struct lldb_copy_dyld_cache_local_symbols_entry
2675 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002676 uint32_t dylibOffset;
2677 uint32_t nlistStartIndex;
2678 uint32_t nlistCount;
Greg Clayton946f8902012-09-05 22:30:51 +00002679 };
Jason Molendaa5609c82012-06-21 01:51:02 +00002680
Jason Molendaf8130862012-06-22 03:28:35 +00002681 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
2682 The dyld_cache_local_symbols_info structure gives us three things:
2683 1. The start and count of the nlist records in the dyld_shared_cache file
2684 2. The start and size of the strings for these nlist records
2685 3. The start and count of dyld_cache_local_symbols_entry entries
2686
2687 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
2688 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002689 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00002690 and the count of how many nlist records there are for this dylib/framework.
2691 */
2692
Jason Molendaa5609c82012-06-21 01:51:02 +00002693 // Process the dsc header to find the unmapped symbols
2694 //
2695 // Save some VM space, do not map the entire cache in one shot.
2696
Jason Molenda255f9bb2013-03-06 23:17:36 +00002697 DataBufferSP dsc_data_sp;
Greg Clayton736888c2015-02-23 23:47:09 +00002698 dsc_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
Jason Molenda255f9bb2013-03-06 23:17:36 +00002699
2700 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00002701 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002702 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002703
Jason Molenda255f9bb2013-03-06 23:17:36 +00002704 char version_str[17];
2705 int version = -1;
2706 lldb::offset_t offset = 0;
2707 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
2708 version_str[16] = '\0';
2709 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
2710 {
2711 int v;
2712 if (::sscanf (version_str + 6, "%d", &v) == 1)
2713 {
2714 version = v;
2715 }
2716 }
2717
Jason Molenda0e0954c2013-04-16 06:24:42 +00002718 UUID dsc_uuid;
2719 if (version >= 1)
2720 {
2721 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
2722 uint8_t uuid_bytes[sizeof (uuid_t)];
2723 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
2724 dsc_uuid.SetBytes (uuid_bytes);
2725 }
2726
2727 bool uuid_match = true;
2728 if (dsc_uuid.IsValid() && process)
2729 {
2730 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
2731
2732 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
2733 {
2734 // The on-disk dyld_shared_cache file is not the same as the one in this
2735 // process' memory, don't use it.
2736 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002737 ModuleSP module_sp (GetModule());
2738 if (module_sp)
2739 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 +00002740 }
2741 }
2742
Jason Molenda4e7511e2013-03-06 23:19:17 +00002743 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00002744
Jason Molendaa5609c82012-06-21 01:51:02 +00002745 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
2746
2747 // If the mappingOffset points to a location inside the header, we've
2748 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda0e0954c2013-04-16 06:24:42 +00002749 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendaa5609c82012-06-21 01:51:02 +00002750 {
2751
Greg Clayton736888c2015-02-23 23:47:09 +00002752 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 +00002753 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
2754 offset = 0;
2755
2756 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
2757 // in the shared library cache need to be adjusted by an offset to match up with the
2758 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
2759 // recorded in mapping_offset_value.
2760 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
2761
2762 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00002763 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
2764 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
2765
Jason Molenda4e7511e2013-03-06 23:19:17 +00002766 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00002767 {
2768 // Map the local symbols
Greg Clayton736888c2015-02-23 23:47:09 +00002769 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00002770 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002771 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002772
2773 offset = 0;
2774
Greg Claytonb65c6292015-02-20 22:20:05 +00002775 typedef std::map<ConstString, uint16_t> UndefinedNameToDescMap;
2776 typedef std::map<uint32_t, ConstString> SymbolIndexToName;
2777 UndefinedNameToDescMap undefined_name_to_desc;
2778 SymbolIndexToName reexport_shlib_needs_fixup;
2779
2780
Jason Molendaa5609c82012-06-21 01:51:02 +00002781 // Read the local_symbols_infos struct in one shot
2782 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
2783 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
2784
Jason Molendaa5609c82012-06-21 01:51:02 +00002785 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
2786
Jason Molenda255f9bb2013-03-06 23:17:36 +00002787 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002788
2789 offset = local_symbols_info.entriesOffset;
2790 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
2791 {
2792 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
2793 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
2794 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
2795 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
2796
Jason Molenda4e7511e2013-03-06 23:19:17 +00002797 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00002798 {
2799 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
2800
2801 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
2802 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
2803 num_syms = symtab->GetNumSymbols();
2804
2805 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
2806 uint32_t string_table_offset = local_symbols_info.stringsOffset;
2807
Jason Molenda4e7511e2013-03-06 23:19:17 +00002808 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00002809 {
2810 /////////////////////////////
2811 {
2812 struct nlist_64 nlist;
2813 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2814 break;
2815
2816 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2817 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2818 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2819 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2820 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2821
2822 SymbolType type = eSymbolTypeInvalid;
2823 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2824
2825 if (symbol_name == NULL)
2826 {
2827 // No symbol should be NULL, even the symbols with no
2828 // string values should have an offset zero which points
2829 // to an empty C-string
2830 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002831 "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 +00002832 entry_index,
2833 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002834 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00002835 continue;
2836 }
2837 if (symbol_name[0] == '\0')
2838 symbol_name = NULL;
2839
2840 const char *symbol_name_non_abi_mangled = NULL;
2841
2842 SectionSP symbol_section;
2843 uint32_t symbol_byte_size = 0;
2844 bool add_nlist = true;
Charles Davis510938e2013-08-27 05:04:57 +00002845 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002846 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002847 bool is_gsym = false;
Greg Clayton60038be2015-02-14 00:51:13 +00002848 bool set_value = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002849
2850 assert (sym_idx < num_syms);
2851
2852 sym[sym_idx].SetDebug (is_debug);
2853
2854 if (is_debug)
2855 {
2856 switch (nlist.n_type)
2857 {
Charles Davis510938e2013-08-27 05:04:57 +00002858 case N_GSYM:
2859 // global symbol: name,,NO_SECT,type,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002860 // Sometimes the N_GSYM value contains the address.
2861
2862 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2863 // have the same address, but we want to ensure that we always find only the real symbol,
2864 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2865 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2866 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2867 // same address.
2868
Greg Clayton1e28adf2015-02-25 17:25:02 +00002869 is_gsym = true;
2870 sym[sym_idx].SetExternal(true);
2871
2872 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O')
2873 {
2874 llvm::StringRef symbol_name_ref(symbol_name);
2875 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2876 {
2877 symbol_name_non_abi_mangled = symbol_name + 1;
2878 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2879 type = eSymbolTypeObjCClass;
2880 demangled_is_synthesized = true;
2881
2882 }
2883 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2884 {
2885 symbol_name_non_abi_mangled = symbol_name + 1;
2886 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2887 type = eSymbolTypeObjCMetaClass;
2888 demangled_is_synthesized = true;
2889 }
2890 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2891 {
2892 symbol_name_non_abi_mangled = symbol_name + 1;
2893 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2894 type = eSymbolTypeObjCIVar;
2895 demangled_is_synthesized = true;
2896 }
2897 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002898 else
2899 {
Jason Molendaa5609c82012-06-21 01:51:02 +00002900 if (nlist.n_value != 0)
2901 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2902 type = eSymbolTypeData;
2903 }
2904 break;
2905
Charles Davis510938e2013-08-27 05:04:57 +00002906 case N_FNAME:
2907 // procedure name (f77 kludge): name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002908 type = eSymbolTypeCompiler;
2909 break;
2910
Charles Davis510938e2013-08-27 05:04:57 +00002911 case N_FUN:
2912 // procedure: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002913 if (symbol_name)
2914 {
2915 type = eSymbolTypeCode;
2916 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2917
Greg Claytond81088c2014-01-16 01:38:29 +00002918 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002919 // We use the current number of symbols in the symbol table in lieu of
2920 // using nlist_idx in case we ever start trimming entries out
2921 N_FUN_indexes.push_back(sym_idx);
2922 }
2923 else
2924 {
2925 type = eSymbolTypeCompiler;
2926
2927 if ( !N_FUN_indexes.empty() )
2928 {
2929 // Copy the size of the function into the original STAB entry so we don't have
2930 // to hunt for it later
2931 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2932 N_FUN_indexes.pop_back();
2933 // We don't really need the end function STAB as it contains the size which
2934 // we already placed with the original symbol, so don't add it if we want a
2935 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002936 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002937 }
2938 }
2939 break;
2940
Charles Davis510938e2013-08-27 05:04:57 +00002941 case N_STSYM:
2942 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00002943 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002944 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2945 type = eSymbolTypeData;
2946 break;
2947
Charles Davis510938e2013-08-27 05:04:57 +00002948 case N_LCSYM:
2949 // .lcomm symbol: name,,n_sect,type,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002950 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2951 type = eSymbolTypeCommonBlock;
2952 break;
2953
Charles Davis510938e2013-08-27 05:04:57 +00002954 case N_BNSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002955 // We use the current number of symbols in the symbol table in lieu of
2956 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00002957 // Skip these if we want minimal symbol tables
2958 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002959 break;
2960
Charles Davis510938e2013-08-27 05:04:57 +00002961 case N_ENSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002962 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2963 // so that we can always skip the entire symbol if we need to navigate
2964 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00002965 // Skip these if we want minimal symbol tables
2966 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002967 break;
2968
2969
Charles Davis510938e2013-08-27 05:04:57 +00002970 case N_OPT:
2971 // emitted with gcc2_compiled and in gcc source
Jason Molendaa5609c82012-06-21 01:51:02 +00002972 type = eSymbolTypeCompiler;
2973 break;
2974
Charles Davis510938e2013-08-27 05:04:57 +00002975 case N_RSYM:
2976 // register sym: name,,NO_SECT,type,register
Jason Molendaa5609c82012-06-21 01:51:02 +00002977 type = eSymbolTypeVariable;
2978 break;
2979
Charles Davis510938e2013-08-27 05:04:57 +00002980 case N_SLINE:
2981 // src line: 0,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002982 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2983 type = eSymbolTypeLineEntry;
2984 break;
2985
Charles Davis510938e2013-08-27 05:04:57 +00002986 case N_SSYM:
2987 // structure elt: name,,NO_SECT,type,struct_offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002988 type = eSymbolTypeVariableType;
2989 break;
2990
Charles Davis510938e2013-08-27 05:04:57 +00002991 case N_SO:
2992 // source file name
Jason Molendaa5609c82012-06-21 01:51:02 +00002993 type = eSymbolTypeSourceFile;
2994 if (symbol_name == NULL)
2995 {
Greg Clayton3046e662013-07-10 01:23:25 +00002996 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002997 if (N_SO_index != UINT32_MAX)
2998 {
2999 // Set the size of the N_SO to the terminating index of this N_SO
3000 // so that we can always skip the entire N_SO if we need to navigate
3001 // more quickly at the source level when parsing STABS
3002 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00003003 symbol_ptr->SetByteSize(sym_idx);
Jason Molendaa5609c82012-06-21 01:51:02 +00003004 symbol_ptr->SetSizeIsSibling(true);
3005 }
3006 N_NSYM_indexes.clear();
3007 N_INCL_indexes.clear();
3008 N_BRAC_indexes.clear();
3009 N_COMM_indexes.clear();
3010 N_FUN_indexes.clear();
3011 N_SO_index = UINT32_MAX;
3012 }
3013 else
3014 {
3015 // We use the current number of symbols in the symbol table in lieu of
3016 // using nlist_idx in case we ever start trimming entries out
3017 const bool N_SO_has_full_path = symbol_name[0] == '/';
3018 if (N_SO_has_full_path)
3019 {
Greg Clayton3046e662013-07-10 01:23:25 +00003020 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00003021 {
3022 // We have two consecutive N_SO entries where the first contains a directory
3023 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00003024 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00003025 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3026 add_nlist = false;
3027 }
3028 else
3029 {
3030 // This is the first entry in a N_SO that contains a directory or
3031 // a full path to the source file
3032 N_SO_index = sym_idx;
3033 }
3034 }
Greg Clayton3046e662013-07-10 01:23:25 +00003035 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00003036 {
3037 // This is usually the second N_SO entry that contains just the filename,
3038 // so here we combine it with the first one if we are minimizing the symbol table
3039 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
3040 if (so_path && so_path[0])
3041 {
3042 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00003043 const size_t double_slash_pos = full_so_path.find("//");
3044 if (double_slash_pos != std::string::npos)
3045 {
3046 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00003047 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Clayton0662d962012-09-07 20:29:13 +00003048 // and the second is the directory for the source file so you end up with
3049 // a path that looks like "/tmp/src//tmp/src/"
3050 FileSpec so_dir(so_path, false);
3051 if (!so_dir.Exists())
3052 {
3053 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3054 if (so_dir.Exists())
3055 {
3056 // Trim off the incorrect path
3057 full_so_path.erase(0, double_slash_pos + 1);
3058 }
3059 }
3060 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003061 if (*full_so_path.rbegin() != '/')
3062 full_so_path += '/';
3063 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00003064 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00003065 add_nlist = false;
3066 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3067 }
3068 }
Greg Clayton946f8902012-09-05 22:30:51 +00003069 else
3070 {
3071 // This could be a relative path to a N_SO
3072 N_SO_index = sym_idx;
3073 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003074 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003075 break;
3076
Charles Davis510938e2013-08-27 05:04:57 +00003077 case N_OSO:
3078 // object file name: name,,0,0,st_mtime
Jason Molendaa5609c82012-06-21 01:51:02 +00003079 type = eSymbolTypeObjectFile;
3080 break;
3081
Charles Davis510938e2013-08-27 05:04:57 +00003082 case N_LSYM:
3083 // local sym: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00003084 type = eSymbolTypeLocal;
3085 break;
3086
3087 //----------------------------------------------------------------------
3088 // INCL scopes
3089 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003090 case N_BINCL:
3091 // include file beginning: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00003092 // We use the current number of symbols in the symbol table in lieu of
3093 // using nlist_idx in case we ever start trimming entries out
3094 N_INCL_indexes.push_back(sym_idx);
3095 type = eSymbolTypeScopeBegin;
3096 break;
3097
Charles Davis510938e2013-08-27 05:04:57 +00003098 case N_EINCL:
3099 // include file end: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003100 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3101 // so that we can always skip the entire symbol if we need to navigate
3102 // more quickly at the source level when parsing STABS
3103 if ( !N_INCL_indexes.empty() )
3104 {
3105 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3106 symbol_ptr->SetByteSize(sym_idx + 1);
3107 symbol_ptr->SetSizeIsSibling(true);
3108 N_INCL_indexes.pop_back();
3109 }
3110 type = eSymbolTypeScopeEnd;
3111 break;
3112
Charles Davis510938e2013-08-27 05:04:57 +00003113 case N_SOL:
3114 // #included file name: name,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003115 type = eSymbolTypeHeaderFile;
3116
3117 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00003118 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00003119 break;
3120
Charles Davis510938e2013-08-27 05:04:57 +00003121 case N_PARAMS:
3122 // compiler parameters: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003123 type = eSymbolTypeCompiler;
3124 break;
3125
Charles Davis510938e2013-08-27 05:04:57 +00003126 case N_VERSION:
3127 // compiler version: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003128 type = eSymbolTypeCompiler;
3129 break;
3130
Charles Davis510938e2013-08-27 05:04:57 +00003131 case N_OLEVEL:
3132 // compiler -O level: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003133 type = eSymbolTypeCompiler;
3134 break;
3135
Charles Davis510938e2013-08-27 05:04:57 +00003136 case N_PSYM:
3137 // parameter: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00003138 type = eSymbolTypeVariable;
3139 break;
3140
Charles Davis510938e2013-08-27 05:04:57 +00003141 case N_ENTRY:
3142 // alternate entry: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003143 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3144 type = eSymbolTypeLineEntry;
3145 break;
3146
3147 //----------------------------------------------------------------------
3148 // Left and Right Braces
3149 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003150 case N_LBRAC:
3151 // left bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003152 // We use the current number of symbols in the symbol table in lieu of
3153 // using nlist_idx in case we ever start trimming entries out
3154 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3155 N_BRAC_indexes.push_back(sym_idx);
3156 type = eSymbolTypeScopeBegin;
3157 break;
3158
Charles Davis510938e2013-08-27 05:04:57 +00003159 case N_RBRAC:
3160 // right bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003161 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3162 // so that we can always skip the entire symbol if we need to navigate
3163 // more quickly at the source level when parsing STABS
3164 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3165 if ( !N_BRAC_indexes.empty() )
3166 {
3167 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3168 symbol_ptr->SetByteSize(sym_idx + 1);
3169 symbol_ptr->SetSizeIsSibling(true);
3170 N_BRAC_indexes.pop_back();
3171 }
3172 type = eSymbolTypeScopeEnd;
3173 break;
3174
Charles Davis510938e2013-08-27 05:04:57 +00003175 case N_EXCL:
3176 // deleted include file: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00003177 type = eSymbolTypeHeaderFile;
3178 break;
3179
3180 //----------------------------------------------------------------------
3181 // COMM scopes
3182 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003183 case N_BCOMM:
3184 // begin common: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003185 // We use the current number of symbols in the symbol table in lieu of
3186 // using nlist_idx in case we ever start trimming entries out
3187 type = eSymbolTypeScopeBegin;
3188 N_COMM_indexes.push_back(sym_idx);
3189 break;
3190
Charles Davis510938e2013-08-27 05:04:57 +00003191 case N_ECOML:
3192 // end common (local name): 0,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003193 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3194 // Fall through
3195
Charles Davis510938e2013-08-27 05:04:57 +00003196 case N_ECOMM:
3197 // end common: name,,n_sect,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003198 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3199 // so that we can always skip the entire symbol if we need to navigate
3200 // more quickly at the source level when parsing STABS
3201 if ( !N_COMM_indexes.empty() )
3202 {
3203 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3204 symbol_ptr->SetByteSize(sym_idx + 1);
3205 symbol_ptr->SetSizeIsSibling(true);
3206 N_COMM_indexes.pop_back();
3207 }
3208 type = eSymbolTypeScopeEnd;
3209 break;
3210
Charles Davis510938e2013-08-27 05:04:57 +00003211 case N_LENG:
3212 // second stab entry with length information
Jason Molendaa5609c82012-06-21 01:51:02 +00003213 type = eSymbolTypeAdditional;
3214 break;
3215
3216 default: break;
3217 }
3218 }
3219 else
3220 {
Charles Davis510938e2013-08-27 05:04:57 +00003221 //uint8_t n_pext = N_PEXT & nlist.n_type;
3222 uint8_t n_type = N_TYPE & nlist.n_type;
3223 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Jason Molendaa5609c82012-06-21 01:51:02 +00003224
3225 switch (n_type)
3226 {
Greg Clayton60038be2015-02-14 00:51:13 +00003227 case N_INDR:
3228 {
3229 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
3230 if (reexport_name_cstr && reexport_name_cstr[0])
3231 {
3232 type = eSymbolTypeReExported;
3233 ConstString reexport_name(reexport_name_cstr + ((reexport_name_cstr[0] == '_') ? 1 : 0));
3234 sym[sym_idx].SetReExportedSymbolName(reexport_name);
3235 set_value = false;
3236 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3237 indirect_symbol_names.insert(ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0)));
3238 }
3239 else
3240 type = eSymbolTypeUndefined;
3241 }
3242 break;
3243
Charles Davis510938e2013-08-27 05:04:57 +00003244 case N_UNDF:
Greg Clayton786ad182015-03-03 01:40:46 +00003245 if (symbol_name && symbol_name[0])
Greg Clayton60038be2015-02-14 00:51:13 +00003246 {
3247 ConstString undefined_name(symbol_name + ((symbol_name[0] == '_') ? 1 : 0));
3248 undefined_name_to_desc[undefined_name] = nlist.n_desc;
3249 }
3250 // Fall through
3251 case N_PBUD:
Jason Molendaa5609c82012-06-21 01:51:02 +00003252 type = eSymbolTypeUndefined;
3253 break;
3254
Charles Davis510938e2013-08-27 05:04:57 +00003255 case N_ABS:
Jason Molendaa5609c82012-06-21 01:51:02 +00003256 type = eSymbolTypeAbsolute;
3257 break;
3258
Charles Davis510938e2013-08-27 05:04:57 +00003259 case N_SECT:
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003260 {
3261 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00003262
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003263 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00003264 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003265 // TODO: warn about this?
3266 add_nlist = false;
3267 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00003268 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003269
3270 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00003271 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003272 type = eSymbolTypeException;
3273 }
3274 else
3275 {
Charles Davis510938e2013-08-27 05:04:57 +00003276 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003277
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003278 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00003279 {
Charles Davis510938e2013-08-27 05:04:57 +00003280 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
3281 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
3282 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
3283 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3284 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3285 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3286 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3287 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3288 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
Charles Davis510938e2013-08-27 05:04:57 +00003289 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3290 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
3291 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
3292 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton38f9cc42014-06-16 22:53:16 +00003293 default:
3294 switch (symbol_section->GetType())
3295 {
3296 case lldb::eSectionTypeCode:
3297 type = eSymbolTypeCode;
3298 break;
3299 case eSectionTypeData:
3300 case eSectionTypeDataCString: // Inlined C string data
3301 case eSectionTypeDataCStringPointers: // Pointers to C string data
3302 case eSectionTypeDataSymbolAddress: // Address of a symbol in the symbol table
3303 case eSectionTypeData4:
3304 case eSectionTypeData8:
3305 case eSectionTypeData16:
Greg Clayton38f9cc42014-06-16 22:53:16 +00003306 type = eSymbolTypeData;
3307 break;
3308 default:
3309 break;
3310 }
3311 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00003312 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003313
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003314 if (type == eSymbolTypeInvalid)
3315 {
3316 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3317 if (symbol_section->IsDescendant (text_section_sp.get()))
3318 {
Charles Davis510938e2013-08-27 05:04:57 +00003319 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
3320 S_ATTR_SELF_MODIFYING_CODE |
3321 S_ATTR_SOME_INSTRUCTIONS))
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003322 type = eSymbolTypeData;
3323 else
3324 type = eSymbolTypeCode;
3325 }
Greg Claytona381e102015-07-27 23:21:05 +00003326 else if (symbol_section->IsDescendant(data_section_sp.get()) ||
3327 symbol_section->IsDescendant(data_dirty_section_sp.get()) ||
3328 symbol_section->IsDescendant(data_const_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00003329 {
3330 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
3331 {
3332 type = eSymbolTypeRuntime;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003333
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003334 if (symbol_name &&
3335 symbol_name[0] == '_' &&
3336 symbol_name[1] == 'O' &&
Jason Molendaa5609c82012-06-21 01:51:02 +00003337 symbol_name[2] == 'B')
3338 {
3339 llvm::StringRef symbol_name_ref(symbol_name);
Jason Molendaa5609c82012-06-21 01:51:02 +00003340 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3341 {
3342 symbol_name_non_abi_mangled = symbol_name + 1;
3343 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3344 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003345 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003346 }
3347 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3348 {
3349 symbol_name_non_abi_mangled = symbol_name + 1;
3350 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3351 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003352 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003353 }
3354 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3355 {
3356 symbol_name_non_abi_mangled = symbol_name + 1;
3357 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3358 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003359 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003360 }
3361 }
3362 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003363 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00003364 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003365 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00003366 }
3367 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003368 {
3369 type = eSymbolTypeData;
3370 }
3371 }
3372 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3373 {
3374 type = eSymbolTypeTrampoline;
3375 }
3376 else if (symbol_section->IsDescendant(objc_section_sp.get()))
3377 {
3378 type = eSymbolTypeRuntime;
3379 if (symbol_name && symbol_name[0] == '.')
3380 {
3381 llvm::StringRef symbol_name_ref(symbol_name);
3382 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3383 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00003384 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003385 symbol_name_non_abi_mangled = symbol_name;
3386 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3387 type = eSymbolTypeObjCClass;
3388 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003389 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003390 }
3391 }
3392 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003393 }
3394 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003395 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003396 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003397 }
3398
3399 if (add_nlist)
3400 {
3401 uint64_t symbol_value = nlist.n_value;
Jason Molendaa5609c82012-06-21 01:51:02 +00003402 if (symbol_name_non_abi_mangled)
3403 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00003404 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3405 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00003406 }
3407 else
3408 {
Greg Clayton3046e662013-07-10 01:23:25 +00003409 bool symbol_name_is_mangled = false;
3410
Jason Molendaa5609c82012-06-21 01:51:02 +00003411 if (symbol_name && symbol_name[0] == '_')
3412 {
3413 symbol_name_is_mangled = symbol_name[1] == '_';
3414 symbol_name++; // Skip the leading underscore
3415 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003416
Jason Molendaa5609c82012-06-21 01:51:02 +00003417 if (symbol_name)
3418 {
Greg Claytondacc4a92013-05-14 22:19:37 +00003419 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00003420 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00003421 if (is_gsym && is_debug)
Greg Clayton14cd13c2015-07-01 23:29:06 +00003422 {
3423 const char *gsym_name = sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString();
3424 if (gsym_name)
3425 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
3426 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003427 }
3428 }
3429 if (symbol_section)
3430 {
3431 const addr_t section_file_addr = symbol_section->GetFileAddress();
3432 if (symbol_byte_size == 0 && function_starts_count > 0)
3433 {
3434 addr_t symbol_lookup_file_addr = nlist.n_value;
3435 // Do an exact address match for non-ARM addresses, else get the closest since
3436 // the symbol might be a thumb symbol which has an address with bit zero set
3437 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3438 if (is_arm && func_start_entry)
3439 {
3440 // Verify that the function start address is the symbol address (ARM)
3441 // or the symbol address + 1 (thumb)
3442 if (func_start_entry->addr != symbol_lookup_file_addr &&
3443 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3444 {
3445 // Not the right entry, NULL it out...
3446 func_start_entry = NULL;
3447 }
3448 }
3449 if (func_start_entry)
3450 {
3451 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003452
Jason Molendaa5609c82012-06-21 01:51:02 +00003453 addr_t symbol_file_addr = func_start_entry->addr;
3454 uint32_t symbol_flags = 0;
3455 if (is_arm)
3456 {
3457 if (symbol_file_addr & 1)
3458 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
Greg Claytonb887da12015-07-16 19:50:57 +00003459 symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Jason Molendaa5609c82012-06-21 01:51:02 +00003460 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003461
Jason Molendaa5609c82012-06-21 01:51:02 +00003462 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3463 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3464 if (next_func_start_entry)
3465 {
3466 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3467 // Be sure the clear the Thumb address bit when we calculate the size
3468 // from the current and next address
3469 if (is_arm)
Greg Claytonb887da12015-07-16 19:50:57 +00003470 next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Jason Molendaa5609c82012-06-21 01:51:02 +00003471 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3472 }
3473 else
3474 {
3475 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3476 }
3477 }
3478 }
3479 symbol_value -= section_file_addr;
3480 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003481
Greg Claytondacc4a92013-05-14 22:19:37 +00003482 if (is_debug == false)
3483 {
3484 if (type == eSymbolTypeCode)
3485 {
3486 // See if we can find a N_FUN entry for any code symbols.
3487 // If we do find a match, and the name matches, then we
3488 // can merge the two into just the function symbol to avoid
3489 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003490 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3491 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3492 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00003493 {
Greg Claytond81088c2014-01-16 01:38:29 +00003494 bool found_it = false;
3495 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00003496 {
Greg Claytond81088c2014-01-16 01:38:29 +00003497 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3498 {
3499 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3500 // We just need the flags from the linker symbol, so put these flags
3501 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3502 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3503 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3504 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3505 sym[pos->second].SetType (eSymbolTypeResolver);
3506 sym[sym_idx].Clear();
3507 found_it = true;
3508 break;
3509 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003510 }
Greg Claytond81088c2014-01-16 01:38:29 +00003511 if (found_it)
3512 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00003513 }
Jim Inghamea3ac272014-01-10 22:55:37 +00003514 else
3515 {
Greg Claytond81088c2014-01-16 01:38:29 +00003516 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00003517 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00003518 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003519 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00003520 else if (type == eSymbolTypeData ||
3521 type == eSymbolTypeObjCClass ||
3522 type == eSymbolTypeObjCMetaClass ||
3523 type == eSymbolTypeObjCIVar )
Greg Claytondacc4a92013-05-14 22:19:37 +00003524 {
3525 // See if we can find a N_STSYM entry for any data symbols.
3526 // If we do find a match, and the name matches, then we
3527 // can merge the two into just the Static symbol to avoid
3528 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003529 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3530 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
3531 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00003532 {
Greg Claytond81088c2014-01-16 01:38:29 +00003533 bool found_it = false;
3534 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00003535 {
Greg Claytond81088c2014-01-16 01:38:29 +00003536 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3537 {
3538 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3539 // We just need the flags from the linker symbol, so put these flags
3540 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3541 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3542 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3543 sym[sym_idx].Clear();
3544 found_it = true;
3545 break;
3546 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003547 }
Greg Claytond81088c2014-01-16 01:38:29 +00003548 if (found_it)
3549 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00003550 }
3551 else
3552 {
Greg Clayton14cd13c2015-07-01 23:29:06 +00003553 const char *gsym_name = sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString();
3554 if (gsym_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00003555 {
Greg Clayton14cd13c2015-07-01 23:29:06 +00003556 // Combine N_GSYM stab entries with the non stab symbol
3557 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(gsym_name);
3558 if (pos != N_GSYM_name_to_sym_idx.end())
3559 {
3560 const uint32_t GSYM_sym_idx = pos->second;
3561 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3562 // Copy the address, because often the N_GSYM address has an invalid address of zero
3563 // when the global is a common symbol
3564 sym[GSYM_sym_idx].GetAddressRef().SetSection (symbol_section);
3565 sym[GSYM_sym_idx].GetAddressRef().SetOffset (symbol_value);
3566 // We just need the flags from the linker symbol, so put these flags
3567 // into the N_GSYM flags to avoid duplicate symbols in the symbol table
3568 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3569 sym[sym_idx].Clear();
3570 continue;
3571 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003572 }
3573 }
3574 }
3575 }
3576
Jason Molendaa5609c82012-06-21 01:51:02 +00003577 sym[sym_idx].SetID (nlist_idx);
3578 sym[sym_idx].SetType (type);
Greg Clayton60038be2015-02-14 00:51:13 +00003579 if (set_value)
3580 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00003581 sym[sym_idx].GetAddressRef().SetSection (symbol_section);
3582 sym[sym_idx].GetAddressRef().SetOffset (symbol_value);
Greg Clayton60038be2015-02-14 00:51:13 +00003583 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003584 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003585
Jason Molendaa5609c82012-06-21 01:51:02 +00003586 if (symbol_byte_size > 0)
3587 sym[sym_idx].SetByteSize(symbol_byte_size);
3588
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003589 if (demangled_is_synthesized)
3590 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00003591 ++sym_idx;
3592 }
3593 else
3594 {
3595 sym[sym_idx].Clear();
3596 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003597
Jason Molendaa5609c82012-06-21 01:51:02 +00003598 }
3599 /////////////////////////////
3600 }
3601 break; // No more entries to consider
3602 }
3603 }
Greg Clayton60038be2015-02-14 00:51:13 +00003604
3605 for (const auto &pos :reexport_shlib_needs_fixup)
3606 {
3607 const auto undef_pos = undefined_name_to_desc.find(pos.second);
3608 if (undef_pos != undefined_name_to_desc.end())
3609 {
3610 const uint8_t dylib_ordinal = llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
3611 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
3612 sym[pos.first].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(dylib_ordinal-1));
3613 }
3614 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003615 }
3616 }
3617 }
3618 }
3619 }
3620
3621 // Must reset this in case it was mutated above!
3622 nlist_data_offset = 0;
3623#endif
Jim Inghamea3ac272014-01-10 22:55:37 +00003624
Greg Claytonfd814c52013-08-13 01:42:25 +00003625 if (nlist_data.GetByteSize() > 0)
Jason Molendaa5609c82012-06-21 01:51:02 +00003626 {
Jason Molendaa5609c82012-06-21 01:51:02 +00003627
Greg Claytonfd814c52013-08-13 01:42:25 +00003628 // If the sym array was not created while parsing the DSC unmapped
3629 // symbols, create it now.
3630 if (sym == NULL)
Greg Clayton4c82d422012-05-18 23:20:01 +00003631 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003632 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
3633 num_syms = symtab->GetNumSymbols();
3634 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003635
Greg Claytonfd814c52013-08-13 01:42:25 +00003636 if (unmapped_local_symbols_found)
3637 {
3638 assert(m_dysymtab.ilocalsym == 0);
3639 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
3640 nlist_idx = m_dysymtab.nlocalsym;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003641 }
Greg Claytondebb8812012-05-25 17:04:00 +00003642 else
3643 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003644 nlist_idx = 0;
Greg Claytondebb8812012-05-25 17:04:00 +00003645 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003646
Greg Clayton60038be2015-02-14 00:51:13 +00003647 typedef std::map<ConstString, uint16_t> UndefinedNameToDescMap;
3648 typedef std::map<uint32_t, ConstString> SymbolIndexToName;
3649 UndefinedNameToDescMap undefined_name_to_desc;
3650 SymbolIndexToName reexport_shlib_needs_fixup;
Greg Claytonfd814c52013-08-13 01:42:25 +00003651 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003652 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003653 struct nlist_64 nlist;
3654 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
3655 break;
3656
3657 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
3658 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
3659 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
3660 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
3661 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
3662
3663 SymbolType type = eSymbolTypeInvalid;
3664 const char *symbol_name = NULL;
3665
3666 if (have_strtab_data)
Greg Clayton77ccca72011-12-30 00:32:24 +00003667 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003668 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003669
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003670 if (symbol_name == NULL)
3671 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003672 // No symbol should be NULL, even the symbols with no
3673 // string values should have an offset zero which points
3674 // to an empty C-string
3675 Host::SystemLog (Host::eSystemLogError,
3676 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
3677 nlist_idx,
3678 nlist.n_strx,
3679 module_sp->GetFileSpec().GetPath().c_str());
3680 continue;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003681 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003682 if (symbol_name[0] == '\0')
3683 symbol_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003684 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003685 else
3686 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003687 const addr_t str_addr = strtab_addr + nlist.n_strx;
3688 Error str_error;
3689 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
3690 symbol_name = memory_symbol_name.c_str();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003691 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003692 const char *symbol_name_non_abi_mangled = NULL;
3693
3694 SectionSP symbol_section;
3695 lldb::addr_t symbol_byte_size = 0;
3696 bool add_nlist = true;
3697 bool is_gsym = false;
Charles Davis510938e2013-08-27 05:04:57 +00003698 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00003699 bool demangled_is_synthesized = false;
Greg Clayton60038be2015-02-14 00:51:13 +00003700 bool set_value = true;
Greg Claytonfd814c52013-08-13 01:42:25 +00003701 assert (sym_idx < num_syms);
3702
3703 sym[sym_idx].SetDebug (is_debug);
3704
3705 if (is_debug)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003706 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003707 switch (nlist.n_type)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003708 {
Charles Davis510938e2013-08-27 05:04:57 +00003709 case N_GSYM:
3710 // global symbol: name,,NO_SECT,type,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003711 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003712
Greg Claytonfd814c52013-08-13 01:42:25 +00003713 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
3714 // have the same address, but we want to ensure that we always find only the real symbol,
3715 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
3716 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
3717 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
3718 // same address.
Greg Clayton1e28adf2015-02-25 17:25:02 +00003719 is_gsym = true;
3720 sym[sym_idx].SetExternal(true);
Greg Clayton29e08cb2012-03-14 01:53:24 +00003721
Greg Clayton1e28adf2015-02-25 17:25:02 +00003722 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O')
3723 {
3724 llvm::StringRef symbol_name_ref(symbol_name);
3725 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3726 {
3727 symbol_name_non_abi_mangled = symbol_name + 1;
3728 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3729 type = eSymbolTypeObjCClass;
3730 demangled_is_synthesized = true;
3731
3732 }
3733 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3734 {
3735 symbol_name_non_abi_mangled = symbol_name + 1;
3736 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3737 type = eSymbolTypeObjCMetaClass;
3738 demangled_is_synthesized = true;
3739 }
3740 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3741 {
3742 symbol_name_non_abi_mangled = symbol_name + 1;
3743 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3744 type = eSymbolTypeObjCIVar;
3745 demangled_is_synthesized = true;
3746 }
3747 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003748 else
3749 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003750 if (nlist.n_value != 0)
3751 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3752 type = eSymbolTypeData;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003753 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003754 break;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003755
Charles Davis510938e2013-08-27 05:04:57 +00003756 case N_FNAME:
3757 // procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003758 type = eSymbolTypeCompiler;
3759 break;
3760
Charles Davis510938e2013-08-27 05:04:57 +00003761 case N_FUN:
3762 // procedure: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003763 if (symbol_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00003764 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003765 type = eSymbolTypeCode;
3766 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3767
Greg Claytond81088c2014-01-16 01:38:29 +00003768 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003769 // We use the current number of symbols in the symbol table in lieu of
3770 // using nlist_idx in case we ever start trimming entries out
3771 N_FUN_indexes.push_back(sym_idx);
Greg Claytondacc4a92013-05-14 22:19:37 +00003772 }
3773 else
3774 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003775 type = eSymbolTypeCompiler;
3776
3777 if ( !N_FUN_indexes.empty() )
Greg Claytondacc4a92013-05-14 22:19:37 +00003778 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003779 // Copy the size of the function into the original STAB entry so we don't have
3780 // to hunt for it later
3781 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
3782 N_FUN_indexes.pop_back();
3783 // We don't really need the end function STAB as it contains the size which
3784 // we already placed with the original symbol, so don't add it if we want a
3785 // minimal symbol table
3786 add_nlist = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00003787 }
3788 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003789 break;
3790
Charles Davis510938e2013-08-27 05:04:57 +00003791 case N_STSYM:
3792 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00003793 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003794 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3795 type = eSymbolTypeData;
3796 break;
3797
Charles Davis510938e2013-08-27 05:04:57 +00003798 case N_LCSYM:
3799 // .lcomm symbol: name,,n_sect,type,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003800 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3801 type = eSymbolTypeCommonBlock;
3802 break;
3803
Charles Davis510938e2013-08-27 05:04:57 +00003804 case N_BNSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003805 // We use the current number of symbols in the symbol table in lieu of
3806 // using nlist_idx in case we ever start trimming entries out
3807 // Skip these if we want minimal symbol tables
3808 add_nlist = false;
3809 break;
3810
Charles Davis510938e2013-08-27 05:04:57 +00003811 case N_ENSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003812 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
3813 // so that we can always skip the entire symbol if we need to navigate
3814 // more quickly at the source level when parsing STABS
3815 // Skip these if we want minimal symbol tables
3816 add_nlist = false;
3817 break;
3818
3819
Charles Davis510938e2013-08-27 05:04:57 +00003820 case N_OPT:
3821 // emitted with gcc2_compiled and in gcc source
Greg Claytonfd814c52013-08-13 01:42:25 +00003822 type = eSymbolTypeCompiler;
3823 break;
3824
Charles Davis510938e2013-08-27 05:04:57 +00003825 case N_RSYM:
3826 // register sym: name,,NO_SECT,type,register
Greg Claytonfd814c52013-08-13 01:42:25 +00003827 type = eSymbolTypeVariable;
3828 break;
3829
Charles Davis510938e2013-08-27 05:04:57 +00003830 case N_SLINE:
3831 // src line: 0,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003832 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3833 type = eSymbolTypeLineEntry;
3834 break;
3835
Charles Davis510938e2013-08-27 05:04:57 +00003836 case N_SSYM:
3837 // structure elt: name,,NO_SECT,type,struct_offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003838 type = eSymbolTypeVariableType;
3839 break;
3840
Charles Davis510938e2013-08-27 05:04:57 +00003841 case N_SO:
3842 // source file name
Greg Claytonfd814c52013-08-13 01:42:25 +00003843 type = eSymbolTypeSourceFile;
3844 if (symbol_name == NULL)
3845 {
3846 add_nlist = false;
3847 if (N_SO_index != UINT32_MAX)
3848 {
3849 // Set the size of the N_SO to the terminating index of this N_SO
3850 // so that we can always skip the entire N_SO if we need to navigate
3851 // more quickly at the source level when parsing STABS
3852 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
3853 symbol_ptr->SetByteSize(sym_idx);
3854 symbol_ptr->SetSizeIsSibling(true);
3855 }
3856 N_NSYM_indexes.clear();
3857 N_INCL_indexes.clear();
3858 N_BRAC_indexes.clear();
3859 N_COMM_indexes.clear();
3860 N_FUN_indexes.clear();
3861 N_SO_index = UINT32_MAX;
3862 }
3863 else
3864 {
3865 // We use the current number of symbols in the symbol table in lieu of
3866 // using nlist_idx in case we ever start trimming entries out
3867 const bool N_SO_has_full_path = symbol_name[0] == '/';
3868 if (N_SO_has_full_path)
3869 {
3870 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3871 {
3872 // We have two consecutive N_SO entries where the first contains a directory
3873 // and the second contains a full path.
3874 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
3875 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3876 add_nlist = false;
3877 }
3878 else
3879 {
3880 // This is the first entry in a N_SO that contains a directory or
3881 // a full path to the source file
3882 N_SO_index = sym_idx;
3883 }
3884 }
3885 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3886 {
3887 // This is usually the second N_SO entry that contains just the filename,
3888 // so here we combine it with the first one if we are minimizing the symbol table
Greg Claytonddaf6a72015-07-08 22:32:23 +00003889 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName(lldb::eLanguageTypeUnknown).AsCString();
Greg Claytonfd814c52013-08-13 01:42:25 +00003890 if (so_path && so_path[0])
3891 {
3892 std::string full_so_path (so_path);
3893 const size_t double_slash_pos = full_so_path.find("//");
3894 if (double_slash_pos != std::string::npos)
3895 {
3896 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00003897 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Claytonfd814c52013-08-13 01:42:25 +00003898 // and the second is the directory for the source file so you end up with
3899 // a path that looks like "/tmp/src//tmp/src/"
3900 FileSpec so_dir(so_path, false);
3901 if (!so_dir.Exists())
3902 {
3903 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3904 if (so_dir.Exists())
3905 {
3906 // Trim off the incorrect path
3907 full_so_path.erase(0, double_slash_pos + 1);
3908 }
3909 }
3910 }
3911 if (*full_so_path.rbegin() != '/')
3912 full_so_path += '/';
3913 full_so_path += symbol_name;
3914 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
3915 add_nlist = false;
3916 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3917 }
3918 }
3919 else
3920 {
3921 // This could be a relative path to a N_SO
3922 N_SO_index = sym_idx;
3923 }
3924 }
3925
3926 break;
3927
Charles Davis510938e2013-08-27 05:04:57 +00003928 case N_OSO:
3929 // object file name: name,,0,0,st_mtime
Greg Claytonfd814c52013-08-13 01:42:25 +00003930 type = eSymbolTypeObjectFile;
3931 break;
3932
Charles Davis510938e2013-08-27 05:04:57 +00003933 case N_LSYM:
3934 // local sym: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003935 type = eSymbolTypeLocal;
3936 break;
3937
3938 //----------------------------------------------------------------------
3939 // INCL scopes
3940 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003941 case N_BINCL:
3942 // include file beginning: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00003943 // We use the current number of symbols in the symbol table in lieu of
3944 // using nlist_idx in case we ever start trimming entries out
3945 N_INCL_indexes.push_back(sym_idx);
3946 type = eSymbolTypeScopeBegin;
3947 break;
3948
Charles Davis510938e2013-08-27 05:04:57 +00003949 case N_EINCL:
3950 // include file end: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003951 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3952 // so that we can always skip the entire symbol if we need to navigate
3953 // more quickly at the source level when parsing STABS
3954 if ( !N_INCL_indexes.empty() )
3955 {
3956 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3957 symbol_ptr->SetByteSize(sym_idx + 1);
3958 symbol_ptr->SetSizeIsSibling(true);
3959 N_INCL_indexes.pop_back();
3960 }
3961 type = eSymbolTypeScopeEnd;
3962 break;
3963
Charles Davis510938e2013-08-27 05:04:57 +00003964 case N_SOL:
3965 // #included file name: name,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003966 type = eSymbolTypeHeaderFile;
3967
3968 // We currently don't use the header files on darwin
3969 add_nlist = false;
3970 break;
3971
Charles Davis510938e2013-08-27 05:04:57 +00003972 case N_PARAMS:
3973 // compiler parameters: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003974 type = eSymbolTypeCompiler;
3975 break;
3976
Charles Davis510938e2013-08-27 05:04:57 +00003977 case N_VERSION:
3978 // compiler version: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003979 type = eSymbolTypeCompiler;
3980 break;
3981
Charles Davis510938e2013-08-27 05:04:57 +00003982 case N_OLEVEL:
3983 // compiler -O level: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003984 type = eSymbolTypeCompiler;
3985 break;
3986
Charles Davis510938e2013-08-27 05:04:57 +00003987 case N_PSYM:
3988 // parameter: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003989 type = eSymbolTypeVariable;
3990 break;
3991
Charles Davis510938e2013-08-27 05:04:57 +00003992 case N_ENTRY:
3993 // alternate entry: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003994 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3995 type = eSymbolTypeLineEntry;
3996 break;
3997
3998 //----------------------------------------------------------------------
3999 // Left and Right Braces
4000 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00004001 case N_LBRAC:
4002 // left bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004003 // We use the current number of symbols in the symbol table in lieu of
4004 // using nlist_idx in case we ever start trimming entries out
4005 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4006 N_BRAC_indexes.push_back(sym_idx);
4007 type = eSymbolTypeScopeBegin;
4008 break;
4009
Charles Davis510938e2013-08-27 05:04:57 +00004010 case N_RBRAC:
4011 // right bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004012 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
4013 // so that we can always skip the entire symbol if we need to navigate
4014 // more quickly at the source level when parsing STABS
4015 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4016 if ( !N_BRAC_indexes.empty() )
4017 {
4018 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
4019 symbol_ptr->SetByteSize(sym_idx + 1);
4020 symbol_ptr->SetSizeIsSibling(true);
4021 N_BRAC_indexes.pop_back();
4022 }
4023 type = eSymbolTypeScopeEnd;
4024 break;
4025
Charles Davis510938e2013-08-27 05:04:57 +00004026 case N_EXCL:
4027 // deleted include file: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00004028 type = eSymbolTypeHeaderFile;
4029 break;
4030
4031 //----------------------------------------------------------------------
4032 // COMM scopes
4033 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00004034 case N_BCOMM:
4035 // begin common: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004036 // We use the current number of symbols in the symbol table in lieu of
4037 // using nlist_idx in case we ever start trimming entries out
4038 type = eSymbolTypeScopeBegin;
4039 N_COMM_indexes.push_back(sym_idx);
4040 break;
4041
Charles Davis510938e2013-08-27 05:04:57 +00004042 case N_ECOML:
4043 // end common (local name): 0,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004044 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4045 // Fall through
4046
Charles Davis510938e2013-08-27 05:04:57 +00004047 case N_ECOMM:
4048 // end common: name,,n_sect,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004049 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
4050 // so that we can always skip the entire symbol if we need to navigate
4051 // more quickly at the source level when parsing STABS
4052 if ( !N_COMM_indexes.empty() )
4053 {
4054 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
4055 symbol_ptr->SetByteSize(sym_idx + 1);
4056 symbol_ptr->SetSizeIsSibling(true);
4057 N_COMM_indexes.pop_back();
4058 }
4059 type = eSymbolTypeScopeEnd;
4060 break;
4061
Charles Davis510938e2013-08-27 05:04:57 +00004062 case N_LENG:
4063 // second stab entry with length information
Greg Claytonfd814c52013-08-13 01:42:25 +00004064 type = eSymbolTypeAdditional;
4065 break;
4066
4067 default: break;
4068 }
4069 }
4070 else
4071 {
Charles Davis510938e2013-08-27 05:04:57 +00004072 //uint8_t n_pext = N_PEXT & nlist.n_type;
4073 uint8_t n_type = N_TYPE & nlist.n_type;
4074 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00004075
4076 switch (n_type)
4077 {
Greg Clayton60038be2015-02-14 00:51:13 +00004078 case N_INDR:
4079 {
4080 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
4081 if (reexport_name_cstr && reexport_name_cstr[0])
4082 {
4083 type = eSymbolTypeReExported;
4084 ConstString reexport_name(reexport_name_cstr + ((reexport_name_cstr[0] == '_') ? 1 : 0));
4085 sym[sym_idx].SetReExportedSymbolName(reexport_name);
4086 set_value = false;
4087 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
4088 indirect_symbol_names.insert(ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0)));
4089 }
4090 else
4091 type = eSymbolTypeUndefined;
4092 }
4093 break;
4094
Charles Davis510938e2013-08-27 05:04:57 +00004095 case N_UNDF:
Greg Clayton786ad182015-03-03 01:40:46 +00004096 if (symbol_name && symbol_name[0])
Greg Clayton60038be2015-02-14 00:51:13 +00004097 {
4098 ConstString undefined_name(symbol_name + ((symbol_name[0] == '_') ? 1 : 0));
4099 undefined_name_to_desc[undefined_name] = nlist.n_desc;
4100 }
4101 // Fall through
4102 case N_PBUD:
Greg Claytonfd814c52013-08-13 01:42:25 +00004103 type = eSymbolTypeUndefined;
4104 break;
4105
Charles Davis510938e2013-08-27 05:04:57 +00004106 case N_ABS:
Greg Claytonfd814c52013-08-13 01:42:25 +00004107 type = eSymbolTypeAbsolute;
4108 break;
4109
Charles Davis510938e2013-08-27 05:04:57 +00004110 case N_SECT:
Greg Claytonfd814c52013-08-13 01:42:25 +00004111 {
4112 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4113
4114 if (!symbol_section)
4115 {
4116 // TODO: warn about this?
4117 add_nlist = false;
4118 break;
4119 }
4120
4121 if (TEXT_eh_frame_sectID == nlist.n_sect)
4122 {
4123 type = eSymbolTypeException;
4124 }
4125 else
4126 {
Charles Davis510938e2013-08-27 05:04:57 +00004127 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Greg Claytonfd814c52013-08-13 01:42:25 +00004128
4129 switch (section_type)
4130 {
Charles Davis510938e2013-08-27 05:04:57 +00004131 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
4132 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
4133 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
4134 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
4135 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
4136 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
4137 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
4138 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
4139 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
Charles Davis510938e2013-08-27 05:04:57 +00004140 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
4141 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
4142 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
4143 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton38f9cc42014-06-16 22:53:16 +00004144 default:
4145 switch (symbol_section->GetType())
4146 {
4147 case lldb::eSectionTypeCode:
4148 type = eSymbolTypeCode;
4149 break;
4150 case eSectionTypeData:
4151 case eSectionTypeDataCString: // Inlined C string data
4152 case eSectionTypeDataCStringPointers: // Pointers to C string data
4153 case eSectionTypeDataSymbolAddress: // Address of a symbol in the symbol table
4154 case eSectionTypeData4:
4155 case eSectionTypeData8:
4156 case eSectionTypeData16:
Greg Clayton38f9cc42014-06-16 22:53:16 +00004157 type = eSymbolTypeData;
4158 break;
4159 default:
4160 break;
4161 }
4162 break;
Greg Claytonfd814c52013-08-13 01:42:25 +00004163 }
4164
4165 if (type == eSymbolTypeInvalid)
4166 {
4167 const char *symbol_sect_name = symbol_section->GetName().AsCString();
4168 if (symbol_section->IsDescendant (text_section_sp.get()))
4169 {
Charles Davis510938e2013-08-27 05:04:57 +00004170 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
4171 S_ATTR_SELF_MODIFYING_CODE |
4172 S_ATTR_SOME_INSTRUCTIONS))
Greg Claytonfd814c52013-08-13 01:42:25 +00004173 type = eSymbolTypeData;
4174 else
4175 type = eSymbolTypeCode;
4176 }
4177 else
Greg Claytona381e102015-07-27 23:21:05 +00004178 if (symbol_section->IsDescendant(data_section_sp.get()) ||
4179 symbol_section->IsDescendant(data_dirty_section_sp.get()) ||
4180 symbol_section->IsDescendant(data_const_section_sp.get()))
Greg Claytonfd814c52013-08-13 01:42:25 +00004181 {
4182 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
4183 {
4184 type = eSymbolTypeRuntime;
4185
4186 if (symbol_name &&
4187 symbol_name[0] == '_' &&
4188 symbol_name[1] == 'O' &&
4189 symbol_name[2] == 'B')
4190 {
4191 llvm::StringRef symbol_name_ref(symbol_name);
Greg Claytonfd814c52013-08-13 01:42:25 +00004192 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
4193 {
4194 symbol_name_non_abi_mangled = symbol_name + 1;
4195 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
4196 type = eSymbolTypeObjCClass;
4197 demangled_is_synthesized = true;
4198 }
4199 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
4200 {
4201 symbol_name_non_abi_mangled = symbol_name + 1;
4202 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
4203 type = eSymbolTypeObjCMetaClass;
4204 demangled_is_synthesized = true;
4205 }
4206 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
4207 {
4208 symbol_name_non_abi_mangled = symbol_name + 1;
4209 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
4210 type = eSymbolTypeObjCIVar;
4211 demangled_is_synthesized = true;
4212 }
4213 }
4214 }
4215 else
4216 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
4217 {
4218 type = eSymbolTypeException;
4219 }
4220 else
4221 {
4222 type = eSymbolTypeData;
4223 }
4224 }
4225 else
4226 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
4227 {
4228 type = eSymbolTypeTrampoline;
4229 }
4230 else
4231 if (symbol_section->IsDescendant(objc_section_sp.get()))
4232 {
4233 type = eSymbolTypeRuntime;
4234 if (symbol_name && symbol_name[0] == '.')
4235 {
4236 llvm::StringRef symbol_name_ref(symbol_name);
4237 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
4238 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
4239 {
4240 symbol_name_non_abi_mangled = symbol_name;
4241 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
4242 type = eSymbolTypeObjCClass;
4243 demangled_is_synthesized = true;
4244 }
4245 }
4246 }
4247 }
4248 }
4249 }
4250 break;
Greg Claytondacc4a92013-05-14 22:19:37 +00004251 }
4252 }
4253
Greg Claytonfd814c52013-08-13 01:42:25 +00004254 if (add_nlist)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004255 {
Greg Claytonfd814c52013-08-13 01:42:25 +00004256 uint64_t symbol_value = nlist.n_value;
4257
4258 if (symbol_name_non_abi_mangled)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004259 {
Greg Claytonfd814c52013-08-13 01:42:25 +00004260 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
4261 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
4262 }
4263 else
4264 {
4265 bool symbol_name_is_mangled = false;
4266
4267 if (symbol_name && symbol_name[0] == '_')
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004268 {
Greg Claytonfd814c52013-08-13 01:42:25 +00004269 symbol_name_is_mangled = symbol_name[1] == '_';
4270 symbol_name++; // Skip the leading underscore
4271 }
4272
4273 if (symbol_name)
4274 {
4275 ConstString const_symbol_name(symbol_name);
4276 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Claytonfd814c52013-08-13 01:42:25 +00004277 }
4278 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00004279
4280 if (is_gsym)
Greg Clayton14cd13c2015-07-01 23:29:06 +00004281 {
Greg Claytonddaf6a72015-07-08 22:32:23 +00004282 const char *gsym_name = sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled).GetCString();
Greg Clayton14cd13c2015-07-01 23:29:06 +00004283 if (gsym_name)
4284 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
4285 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00004286
Greg Claytonfd814c52013-08-13 01:42:25 +00004287 if (symbol_section)
4288 {
4289 const addr_t section_file_addr = symbol_section->GetFileAddress();
4290 if (symbol_byte_size == 0 && function_starts_count > 0)
4291 {
4292 addr_t symbol_lookup_file_addr = nlist.n_value;
4293 // Do an exact address match for non-ARM addresses, else get the closest since
4294 // the symbol might be a thumb symbol which has an address with bit zero set
4295 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
4296 if (is_arm && func_start_entry)
4297 {
4298 // Verify that the function start address is the symbol address (ARM)
4299 // or the symbol address + 1 (thumb)
4300 if (func_start_entry->addr != symbol_lookup_file_addr &&
4301 func_start_entry->addr != (symbol_lookup_file_addr + 1))
4302 {
4303 // Not the right entry, NULL it out...
4304 func_start_entry = NULL;
4305 }
4306 }
4307 if (func_start_entry)
4308 {
4309 func_start_entry->data = true;
4310
4311 addr_t symbol_file_addr = func_start_entry->addr;
4312 if (is_arm)
Greg Claytonb887da12015-07-16 19:50:57 +00004313 symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Greg Claytonfd814c52013-08-13 01:42:25 +00004314
4315 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
4316 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
4317 if (next_func_start_entry)
4318 {
4319 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4320 // Be sure the clear the Thumb address bit when we calculate the size
4321 // from the current and next address
4322 if (is_arm)
Greg Claytonb887da12015-07-16 19:50:57 +00004323 next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Greg Claytonfd814c52013-08-13 01:42:25 +00004324 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
4325 }
4326 else
4327 {
4328 symbol_byte_size = section_end_file_addr - symbol_file_addr;
4329 }
4330 }
4331 }
4332 symbol_value -= section_file_addr;
4333 }
4334
4335 if (is_debug == false)
4336 {
4337 if (type == eSymbolTypeCode)
4338 {
4339 // See if we can find a N_FUN entry for any code symbols.
4340 // If we do find a match, and the name matches, then we
4341 // can merge the two into just the function symbol to avoid
4342 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00004343 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
4344 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
4345 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00004346 {
Greg Claytond81088c2014-01-16 01:38:29 +00004347 bool found_it = false;
4348 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00004349 {
Greg Claytonddaf6a72015-07-08 22:32:23 +00004350 if (sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled))
Greg Claytond81088c2014-01-16 01:38:29 +00004351 {
4352 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4353 // We just need the flags from the linker symbol, so put these flags
4354 // into the N_FUN flags to avoid duplicate symbols in the symbol table
4355 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4356 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4357 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
4358 sym[pos->second].SetType (eSymbolTypeResolver);
4359 sym[sym_idx].Clear();
4360 found_it = true;
4361 break;
4362 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004363 }
Greg Claytond81088c2014-01-16 01:38:29 +00004364 if (found_it)
4365 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00004366 }
Jim Inghamea3ac272014-01-10 22:55:37 +00004367 else
4368 {
4369 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00004370 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00004371 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004372 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00004373 else if (type == eSymbolTypeData ||
4374 type == eSymbolTypeObjCClass ||
4375 type == eSymbolTypeObjCMetaClass ||
4376 type == eSymbolTypeObjCIVar )
Greg Claytonfd814c52013-08-13 01:42:25 +00004377 {
4378 // See if we can find a N_STSYM entry for any data symbols.
4379 // If we do find a match, and the name matches, then we
4380 // can merge the two into just the Static symbol to avoid
4381 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00004382 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
4383 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
4384 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00004385 {
Greg Claytond81088c2014-01-16 01:38:29 +00004386 bool found_it = false;
4387 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00004388 {
Greg Claytonddaf6a72015-07-08 22:32:23 +00004389 if (sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled))
Greg Claytond81088c2014-01-16 01:38:29 +00004390 {
4391 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4392 // We just need the flags from the linker symbol, so put these flags
4393 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
4394 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4395 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4396 sym[sym_idx].Clear();
4397 found_it = true;
4398 break;
4399 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004400 }
Greg Claytond81088c2014-01-16 01:38:29 +00004401 if (found_it)
4402 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00004403 }
4404 else
4405 {
4406 // Combine N_GSYM stab entries with the non stab symbol
Greg Claytonddaf6a72015-07-08 22:32:23 +00004407 const char *gsym_name = sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled).GetCString();
Greg Clayton14cd13c2015-07-01 23:29:06 +00004408 if (gsym_name)
Greg Claytonfd814c52013-08-13 01:42:25 +00004409 {
Greg Clayton14cd13c2015-07-01 23:29:06 +00004410 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(gsym_name);
4411 if (pos != N_GSYM_name_to_sym_idx.end())
4412 {
4413 const uint32_t GSYM_sym_idx = pos->second;
4414 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
4415 // Copy the address, because often the N_GSYM address has an invalid address of zero
4416 // when the global is a common symbol
4417 sym[GSYM_sym_idx].GetAddressRef().SetSection (symbol_section);
4418 sym[GSYM_sym_idx].GetAddressRef().SetOffset (symbol_value);
4419 // We just need the flags from the linker symbol, so put these flags
4420 // into the N_GSYM flags to avoid duplicate symbols in the symbol table
4421 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4422 sym[sym_idx].Clear();
4423 continue;
4424 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004425 }
4426 }
4427 }
4428 }
4429
4430 sym[sym_idx].SetID (nlist_idx);
4431 sym[sym_idx].SetType (type);
Greg Clayton60038be2015-02-14 00:51:13 +00004432 if (set_value)
4433 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00004434 sym[sym_idx].GetAddressRef().SetSection (symbol_section);
4435 sym[sym_idx].GetAddressRef().SetOffset (symbol_value);
Greg Clayton60038be2015-02-14 00:51:13 +00004436 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004437 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4438
4439 if (symbol_byte_size > 0)
4440 sym[sym_idx].SetByteSize(symbol_byte_size);
4441
4442 if (demangled_is_synthesized)
4443 sym[sym_idx].SetDemangledNameIsSynthesized(true);
4444
4445 ++sym_idx;
4446 }
4447 else
4448 {
4449 sym[sym_idx].Clear();
4450 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004451 }
Greg Clayton60038be2015-02-14 00:51:13 +00004452
4453 for (const auto &pos :reexport_shlib_needs_fixup)
4454 {
4455 const auto undef_pos = undefined_name_to_desc.find(pos.second);
4456 if (undef_pos != undefined_name_to_desc.end())
4457 {
4458 const uint8_t dylib_ordinal = llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
4459 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
4460 sym[pos.first].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(dylib_ordinal-1));
4461 }
4462 }
4463
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004464 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004465
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004466 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
4467
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004468 if (function_starts_count > 0)
4469 {
4470 char synthetic_function_symbol[PATH_MAX];
4471 uint32_t num_synthetic_function_symbols = 0;
4472 for (i=0; i<function_starts_count; ++i)
4473 {
4474 if (function_starts.GetEntryRef (i).data == false)
4475 ++num_synthetic_function_symbols;
4476 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004477
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004478 if (num_synthetic_function_symbols > 0)
4479 {
4480 if (num_syms < sym_idx + num_synthetic_function_symbols)
4481 {
4482 num_syms = sym_idx + num_synthetic_function_symbols;
4483 sym = symtab->Resize (num_syms);
4484 }
4485 uint32_t synthetic_function_symbol_idx = 0;
4486 for (i=0; i<function_starts_count; ++i)
4487 {
4488 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
4489 if (func_start_entry->data == false)
4490 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004491 addr_t symbol_file_addr = func_start_entry->addr;
4492 uint32_t symbol_flags = 0;
4493 if (is_arm)
4494 {
4495 if (symbol_file_addr & 1)
4496 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
Greg Claytonb887da12015-07-16 19:50:57 +00004497 symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004498 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004499 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004500 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004501 {
4502 SectionSP symbol_section (symbol_addr.GetSection());
4503 uint32_t symbol_byte_size = 0;
4504 if (symbol_section)
4505 {
4506 const addr_t section_file_addr = symbol_section->GetFileAddress();
4507 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
4508 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
4509 if (next_func_start_entry)
4510 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004511 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4512 if (is_arm)
Greg Claytonb887da12015-07-16 19:50:57 +00004513 next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004514 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 +00004515 }
4516 else
4517 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004518 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004519 }
4520 snprintf (synthetic_function_symbol,
4521 sizeof(synthetic_function_symbol),
4522 "___lldb_unnamed_function%u$$%s",
4523 ++synthetic_function_symbol_idx,
4524 module_sp->GetFileSpec().GetFilename().GetCString());
4525 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Clayton037520e2012-07-18 23:18:10 +00004526 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004527 sym[sym_idx].SetType (eSymbolTypeCode);
4528 sym[sym_idx].SetIsSynthetic (true);
Greg Clayton358cf1e2015-06-25 21:46:34 +00004529 sym[sym_idx].GetAddressRef() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004530 if (symbol_flags)
4531 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004532 if (symbol_byte_size)
4533 sym[sym_idx].SetByteSize (symbol_byte_size);
4534 ++sym_idx;
4535 }
4536 }
4537 }
4538 }
4539 }
4540 }
4541
4542 // Trim our symbols down to just what we ended up with after
4543 // removing any symbols.
4544 if (sym_idx < num_syms)
4545 {
4546 num_syms = sym_idx;
4547 sym = symtab->Resize (num_syms);
4548 }
4549
4550 // Now synthesize indirect symbols
4551 if (m_dysymtab.nindirectsyms != 0)
4552 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004553 if (indirect_symbol_index_data.GetByteSize())
4554 {
4555 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
4556
4557 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
4558 {
Charles Davis510938e2013-08-27 05:04:57 +00004559 if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) == S_SYMBOL_STUBS)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004560 {
4561 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
4562 if (symbol_stub_byte_size == 0)
4563 continue;
4564
4565 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
4566
4567 if (num_symbol_stubs == 0)
4568 continue;
4569
4570 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
4571 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
4572 {
4573 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
4574 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 +00004575 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004576 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
4577 {
4578 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Charles Davis510938e2013-08-27 05:04:57 +00004579 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004580 continue;
4581
4582 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
4583 Symbol *stub_symbol = NULL;
4584 if (index_pos != end_index_pos)
4585 {
4586 // We have a remapping from the original nlist index to
4587 // a current symbol index, so just look this up by index
4588 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
4589 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004590 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004591 {
4592 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00004593 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004594 // S_SYMBOL_STUBS
4595 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
4596 }
4597
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004598 if (stub_symbol)
4599 {
4600 Address so_addr(symbol_stub_addr, section_list);
4601
4602 if (stub_symbol->GetType() == eSymbolTypeUndefined)
4603 {
4604 // Change the external symbol into a trampoline that makes sense
4605 // These symbols were N_UNDF N_EXT, and are useless to us, so we
4606 // can re-use them so we don't have to make up a synthetic symbol
4607 // for no good reason.
Greg Clayton9191db42013-10-21 18:40:51 +00004608 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4609 stub_symbol->SetType (eSymbolTypeTrampoline);
4610 else
4611 stub_symbol->SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004612 stub_symbol->SetExternal (false);
Greg Clayton358cf1e2015-06-25 21:46:34 +00004613 stub_symbol->GetAddressRef() = so_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004614 stub_symbol->SetByteSize (symbol_stub_byte_size);
4615 }
4616 else
4617 {
4618 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00004619 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004620 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00004621 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004622 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00004623 stub_symbol = NULL; // this pointer no longer valid
4624 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004625 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00004626 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton9191db42013-10-21 18:40:51 +00004627 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4628 sym[sym_idx].SetType (eSymbolTypeTrampoline);
4629 else
4630 sym[sym_idx].SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004631 sym[sym_idx].SetIsSynthetic (true);
Greg Clayton358cf1e2015-06-25 21:46:34 +00004632 sym[sym_idx].GetAddressRef() = so_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004633 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
4634 ++sym_idx;
4635 }
4636 }
Greg Clayton3f839a32012-09-05 01:38:55 +00004637 else
4638 {
4639 if (log)
4640 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
4641 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004642 }
4643 }
4644 }
4645 }
4646 }
4647 }
Greg Clayton9191db42013-10-21 18:40:51 +00004648
4649
4650 if (!trie_entries.empty())
4651 {
4652 for (const auto &e : trie_entries)
4653 {
4654 if (e.entry.import_name)
4655 {
Greg Clayton60038be2015-02-14 00:51:13 +00004656 // Only add indirect symbols from the Trie entries if we
4657 // didn't have a N_INDR nlist entry for this already
4658 if (indirect_symbol_names.find(e.entry.name) == indirect_symbol_names.end())
Greg Clayton9191db42013-10-21 18:40:51 +00004659 {
Greg Clayton60038be2015-02-14 00:51:13 +00004660 // Make a synthetic symbol to describe re-exported symbol.
4661 if (sym_idx >= num_syms)
4662 sym = symtab->Resize (++num_syms);
4663 sym[sym_idx].SetID (synthetic_sym_id++);
4664 sym[sym_idx].GetMangled() = Mangled(e.entry.name);
4665 sym[sym_idx].SetType (eSymbolTypeReExported);
4666 sym[sym_idx].SetIsSynthetic (true);
4667 sym[sym_idx].SetReExportedSymbolName(e.entry.import_name);
4668 if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize())
4669 {
4670 sym[sym_idx].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(e.entry.other-1));
4671 }
4672 ++sym_idx;
Greg Clayton9191db42013-10-21 18:40:51 +00004673 }
Greg Clayton9191db42013-10-21 18:40:51 +00004674 }
4675 }
4676 }
4677
4678
Greg Clayton3046e662013-07-10 01:23:25 +00004679
4680// StreamFile s(stdout, false);
4681// s.Printf ("Symbol table before CalculateSymbolSizes():\n");
4682// symtab->Dump(&s, NULL, eSortOrderNone);
4683 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
4684 symtab->CalculateSymbolSizes();
4685
4686// s.Printf ("Symbol table after CalculateSymbolSizes():\n");
4687// symtab->Dump(&s, NULL, eSortOrderNone);
4688
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004689 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004690 }
4691 return 0;
4692}
4693
4694
4695void
4696ObjectFileMachO::Dump (Stream *s)
4697{
Greg Claytona1743492012-03-13 23:14:29 +00004698 ModuleSP module_sp(GetModule());
4699 if (module_sp)
4700 {
4701 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004702 s->Printf("%p: ", static_cast<void*>(this));
Greg Claytona1743492012-03-13 23:14:29 +00004703 s->Indent();
Charles Davis510938e2013-08-27 05:04:57 +00004704 if (m_header.magic == MH_MAGIC_64 || m_header.magic == MH_CIGAM_64)
Greg Claytona1743492012-03-13 23:14:29 +00004705 s->PutCString("ObjectFileMachO64");
4706 else
4707 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004708
Greg Clayton7ab7f892014-05-29 21:33:45 +00004709 ArchSpec header_arch;
4710 GetArchitecture(header_arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004711
Greg Claytona1743492012-03-13 23:14:29 +00004712 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004713
Greg Clayton3046e662013-07-10 01:23:25 +00004714 SectionList *sections = GetSectionList();
4715 if (sections)
4716 sections->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004717
Greg Claytona1743492012-03-13 23:14:29 +00004718 if (m_symtab_ap.get())
4719 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
4720 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004721}
4722
Greg Claytonf4d6de62013-04-24 22:29:28 +00004723bool
4724ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
4725 const lldb_private::DataExtractor &data,
4726 lldb::offset_t lc_offset,
4727 lldb_private::UUID& uuid)
4728{
4729 uint32_t i;
4730 struct uuid_command load_cmd;
4731
4732 lldb::offset_t offset = lc_offset;
4733 for (i=0; i<header.ncmds; ++i)
4734 {
4735 const lldb::offset_t cmd_offset = offset;
4736 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4737 break;
4738
Charles Davis510938e2013-08-27 05:04:57 +00004739 if (load_cmd.cmd == LC_UUID)
Greg Claytonf4d6de62013-04-24 22:29:28 +00004740 {
4741 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
4742
4743 if (uuid_bytes)
4744 {
4745 // OpenCL on Mac OS X uses the same UUID for each of its object files.
4746 // We pretend these object files have no UUID to prevent crashing.
4747
4748 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
4749 0x3b, 0xa8,
4750 0x4b, 0x16,
4751 0xb6, 0xa4,
4752 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
4753
4754 if (!memcmp(uuid_bytes, opencl_uuid, 16))
4755 return false;
4756
4757 uuid.SetBytes (uuid_bytes);
4758 return true;
4759 }
4760 return false;
4761 }
4762 offset = cmd_offset + load_cmd.cmdsize;
4763 }
4764 return false;
4765}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004766
Greg Clayton7ab7f892014-05-29 21:33:45 +00004767
4768bool
4769ObjectFileMachO::GetArchitecture (const llvm::MachO::mach_header &header,
4770 const lldb_private::DataExtractor &data,
4771 lldb::offset_t lc_offset,
4772 ArchSpec &arch)
4773{
4774 arch.SetArchitecture (eArchTypeMachO, header.cputype, header.cpusubtype);
4775
4776 if (arch.IsValid())
4777 {
4778 llvm::Triple &triple = arch.GetTriple();
4779 if (header.filetype == MH_PRELOAD)
4780 {
4781 // Set OS to "unknown" - this is a standalone binary with no dyld et al
4782 triple.setOS(llvm::Triple::UnknownOS);
4783 return true;
4784 }
4785 else
4786 {
4787 struct load_command load_cmd;
4788
4789 lldb::offset_t offset = lc_offset;
4790 for (uint32_t i=0; i<header.ncmds; ++i)
4791 {
4792 const lldb::offset_t cmd_offset = offset;
4793 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4794 break;
4795
4796 switch (load_cmd.cmd)
4797 {
4798 case LC_VERSION_MIN_IPHONEOS:
4799 triple.setOS (llvm::Triple::IOS);
4800 return true;
4801
4802 case LC_VERSION_MIN_MACOSX:
4803 triple.setOS (llvm::Triple::MacOSX);
4804 return true;
4805
4806 default:
4807 break;
4808 }
4809
4810 offset = cmd_offset + load_cmd.cmdsize;
4811 }
4812
Greg Claytona3a6c122014-07-29 18:04:57 +00004813 // Only set the OS to iOS for ARM, we don't want to set it for x86 and x86_64.
4814 // We do this because we now have MacOSX or iOS as the OS value for x86 and
4815 // x86_64 for normal desktop (MacOSX) and simulator (iOS) binaries. And if
4816 // we compare a "x86_64-apple-ios" to a "x86_64-apple-" triple, it will say
4817 // it is compatible (because the OS is unspecified in the second one and will
4818 // match anything in the first
Greg Clayton7ab7f892014-05-29 21:33:45 +00004819 if (header.cputype == CPU_TYPE_ARM || header.cputype == CPU_TYPE_ARM64)
4820 triple.setOS (llvm::Triple::IOS);
Greg Clayton7ab7f892014-05-29 21:33:45 +00004821 }
4822 }
4823 return arch.IsValid();
4824}
4825
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004826bool
Greg Clayton60830262011-02-04 18:53:10 +00004827ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004828{
Greg Claytona1743492012-03-13 23:14:29 +00004829 ModuleSP module_sp(GetModule());
4830 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004831 {
Greg Claytona1743492012-03-13 23:14:29 +00004832 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004833 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00004834 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004835 }
4836 return false;
4837}
4838
4839
4840uint32_t
4841ObjectFileMachO::GetDependentModules (FileSpecList& files)
4842{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004843 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00004844 ModuleSP module_sp(GetModule());
4845 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004846 {
Greg Claytona1743492012-03-13 23:14:29 +00004847 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4848 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004849 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton5a271952015-06-02 22:43:29 +00004850 std::vector<std::string> rpath_paths;
4851 std::vector<std::string> rpath_relative_paths;
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004852 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 +00004853 uint32_t i;
4854 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004855 {
Greg Claytona1743492012-03-13 23:14:29 +00004856 const uint32_t cmd_offset = offset;
4857 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4858 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004859
Greg Claytona1743492012-03-13 23:14:29 +00004860 switch (load_cmd.cmd)
4861 {
Greg Clayton5a271952015-06-02 22:43:29 +00004862 case LC_RPATH:
Charles Davis510938e2013-08-27 05:04:57 +00004863 case LC_LOAD_DYLIB:
4864 case LC_LOAD_WEAK_DYLIB:
4865 case LC_REEXPORT_DYLIB:
4866 case LC_LOAD_DYLINKER:
4867 case LC_LOADFVMLIB:
4868 case LC_LOAD_UPWARD_DYLIB:
Greg Claytona1743492012-03-13 23:14:29 +00004869 {
4870 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
4871 const char *path = m_data.PeekCStr(name_offset);
Greg Clayton5a271952015-06-02 22:43:29 +00004872 if (path)
Greg Claytona1743492012-03-13 23:14:29 +00004873 {
Greg Clayton5a271952015-06-02 22:43:29 +00004874 if (load_cmd.cmd == LC_RPATH)
4875 rpath_paths.push_back(path);
4876 else
4877 {
4878 if (path[0] == '@')
4879 {
4880 if (strncmp(path, "@rpath", strlen("@rpath")) == 0)
4881 rpath_relative_paths.push_back(path + strlen("@rpath"));
4882 }
4883 else
4884 {
4885 FileSpec file_spec(path, resolve_path);
4886 if (files.AppendIfUnique(file_spec))
4887 count++;
4888 }
4889 }
Greg Claytona1743492012-03-13 23:14:29 +00004890 }
4891 }
4892 break;
4893
4894 default:
4895 break;
4896 }
4897 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004898 }
Greg Clayton5a271952015-06-02 22:43:29 +00004899
4900 if (!rpath_paths.empty())
4901 {
4902 // Fixup all LC_RPATH values to be absolute paths
4903 FileSpec this_file_spec(m_file);
4904 this_file_spec.ResolvePath();
4905 std::string loader_path("@loader_path");
4906 std::string executable_path("@executable_path");
4907 for (auto &rpath : rpath_paths)
4908 {
4909 if (rpath.find(loader_path) == 0)
4910 {
4911 rpath.erase(0, loader_path.size());
4912 rpath.insert(0, this_file_spec.GetDirectory().GetCString());
4913 }
4914 else if (rpath.find(executable_path) == 0)
4915 {
4916 rpath.erase(0, executable_path.size());
4917 rpath.insert(0, this_file_spec.GetDirectory().GetCString());
4918 }
4919 }
4920
4921 for (const auto &rpath_relative_path : rpath_relative_paths)
4922 {
4923 for (const auto &rpath : rpath_paths)
4924 {
4925 std::string path = rpath;
4926 path += rpath_relative_path;
4927 // It is OK to resolve this path because we must find a file on
4928 // disk for us to accept it anyway if it is rpath relative.
4929 FileSpec file_spec(path, true);
4930 // Remove any redundant parts of the path (like "../foo") since
4931 // LC_RPATH values often contain "..".
4932 file_spec.NormalizePath ();
4933 if (file_spec.Exists() && files.AppendIfUnique(file_spec))
4934 {
4935 count++;
4936 break;
4937 }
4938 }
4939 }
4940 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004941 }
4942 return count;
4943}
4944
Jim Ingham672e6f52011-03-07 23:44:08 +00004945lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00004946ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00004947{
4948 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
4949 // is initialized to an invalid address, so we can just return that.
4950 // 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 +00004951
Jim Ingham672e6f52011-03-07 23:44:08 +00004952 if (!IsExecutable() || m_entry_point_address.IsValid())
4953 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004954
4955 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00004956 // /usr/include/mach-o.h, but it is basically:
4957 //
4958 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
4959 // uint32_t count - this is the count of longs in the thread state data
4960 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
4961 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00004962 //
Jim Ingham672e6f52011-03-07 23:44:08 +00004963 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
4964 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
4965 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
4966 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
4967 //
4968 // For now we hard-code the offsets and flavors we need:
4969 //
4970 //
4971
Greg Claytona1743492012-03-13 23:14:29 +00004972 ModuleSP module_sp(GetModule());
4973 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00004974 {
Greg Claytona1743492012-03-13 23:14:29 +00004975 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4976 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004977 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004978 uint32_t i;
4979 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
4980 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004981
Greg Claytona1743492012-03-13 23:14:29 +00004982 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00004983 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004984 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004985 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4986 break;
4987
4988 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00004989 {
Charles Davis510938e2013-08-27 05:04:57 +00004990 case LC_UNIXTHREAD:
4991 case LC_THREAD:
Jim Ingham672e6f52011-03-07 23:44:08 +00004992 {
Greg Claytona1743492012-03-13 23:14:29 +00004993 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00004994 {
Greg Claytona1743492012-03-13 23:14:29 +00004995 uint32_t flavor = m_data.GetU32(&offset);
4996 uint32_t count = m_data.GetU32(&offset);
4997 if (count == 0)
4998 {
4999 // We've gotten off somehow, log and exit;
5000 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00005001 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005002
Greg Claytona1743492012-03-13 23:14:29 +00005003 switch (m_header.cputype)
5004 {
Charles Davis510938e2013-08-27 05:04:57 +00005005 case llvm::MachO::CPU_TYPE_ARM:
Greg Claytona1743492012-03-13 23:14:29 +00005006 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
5007 {
5008 offset += 60; // This is the offset of pc in the GPR thread state data structure.
5009 start_address = m_data.GetU32(&offset);
5010 done = true;
5011 }
Jim Ingham672e6f52011-03-07 23:44:08 +00005012 break;
Jason Molendaa3329782014-03-29 18:54:20 +00005013 case llvm::MachO::CPU_TYPE_ARM64:
5014 if (flavor == 6) // ARM_THREAD_STATE64 from mach/arm/thread_status.h
5015 {
5016 offset += 256; // This is the offset of pc in the GPR thread state data structure.
5017 start_address = m_data.GetU64(&offset);
5018 done = true;
5019 }
5020 break;
Charles Davis510938e2013-08-27 05:04:57 +00005021 case llvm::MachO::CPU_TYPE_I386:
Greg Claytona1743492012-03-13 23:14:29 +00005022 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
5023 {
5024 offset += 40; // This is the offset of eip in the GPR thread state data structure.
5025 start_address = m_data.GetU32(&offset);
5026 done = true;
5027 }
5028 break;
Charles Davis510938e2013-08-27 05:04:57 +00005029 case llvm::MachO::CPU_TYPE_X86_64:
Greg Claytona1743492012-03-13 23:14:29 +00005030 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
5031 {
5032 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
5033 start_address = m_data.GetU64(&offset);
5034 done = true;
5035 }
5036 break;
5037 default:
5038 return m_entry_point_address;
5039 }
5040 // Haven't found the GPR flavor yet, skip over the data for this flavor:
5041 if (done)
5042 break;
5043 offset += count * 4;
5044 }
Jim Ingham672e6f52011-03-07 23:44:08 +00005045 }
Greg Claytona1743492012-03-13 23:14:29 +00005046 break;
Charles Davis510938e2013-08-27 05:04:57 +00005047 case LC_MAIN:
Sean Callanan226b70c2012-03-08 02:39:03 +00005048 {
Greg Claytona1743492012-03-13 23:14:29 +00005049 ConstString text_segment_name ("__TEXT");
5050 uint64_t entryoffset = m_data.GetU64(&offset);
5051 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
5052 if (text_segment_sp)
5053 {
5054 done = true;
5055 start_address = text_segment_sp->GetFileAddress() + entryoffset;
5056 }
Sean Callanan226b70c2012-03-08 02:39:03 +00005057 }
Greg Claytona1743492012-03-13 23:14:29 +00005058
5059 default:
5060 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00005061 }
Greg Claytona1743492012-03-13 23:14:29 +00005062 if (done)
5063 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00005064
Greg Claytona1743492012-03-13 23:14:29 +00005065 // Go to the next load command:
5066 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00005067 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005068
Greg Claytona1743492012-03-13 23:14:29 +00005069 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00005070 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00005071 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00005072 // of this ObjectFile:
5073 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00005074 {
Greg Claytona1743492012-03-13 23:14:29 +00005075 m_entry_point_address.Clear();
5076 }
5077 }
5078 else
5079 {
5080 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
5081 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00005082
Greg Claytona1743492012-03-13 23:14:29 +00005083 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00005084
Greg Claytona1743492012-03-13 23:14:29 +00005085 if (module_sp)
5086 {
5087 SymbolContextList contexts;
5088 SymbolContext context;
5089 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
5090 {
5091 if (contexts.GetContextAtIndex(0, context))
5092 m_entry_point_address = context.symbol->GetAddress();
5093 }
Greg Claytone72dfb32012-02-24 01:59:29 +00005094 }
5095 }
Jim Ingham672e6f52011-03-07 23:44:08 +00005096 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005097
Jim Ingham672e6f52011-03-07 23:44:08 +00005098 return m_entry_point_address;
5099
5100}
5101
Greg Claytonc9660542012-02-05 02:38:54 +00005102lldb_private::Address
5103ObjectFileMachO::GetHeaderAddress ()
5104{
5105 lldb_private::Address header_addr;
5106 SectionList *section_list = GetSectionList();
5107 if (section_list)
5108 {
5109 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
5110 if (text_segment_sp)
5111 {
Greg Claytone72dfb32012-02-24 01:59:29 +00005112 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00005113 header_addr.SetOffset (0);
5114 }
5115 }
5116 return header_addr;
5117}
5118
Greg Claytonc3776bf2012-02-09 06:16:32 +00005119uint32_t
5120ObjectFileMachO::GetNumThreadContexts ()
5121{
Greg Claytona1743492012-03-13 23:14:29 +00005122 ModuleSP module_sp(GetModule());
5123 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005124 {
Greg Claytona1743492012-03-13 23:14:29 +00005125 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5126 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005127 {
Greg Claytona1743492012-03-13 23:14:29 +00005128 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00005129 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00005130 FileRangeArray::Entry file_range;
5131 thread_command thread_cmd;
5132 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005133 {
Greg Claytona1743492012-03-13 23:14:29 +00005134 const uint32_t cmd_offset = offset;
5135 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
5136 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005137
Charles Davis510938e2013-08-27 05:04:57 +00005138 if (thread_cmd.cmd == LC_THREAD)
Greg Claytona1743492012-03-13 23:14:29 +00005139 {
5140 file_range.SetRangeBase (offset);
5141 file_range.SetByteSize (thread_cmd.cmdsize - 8);
5142 m_thread_context_offsets.Append (file_range);
5143 }
5144 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00005145 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00005146 }
5147 }
5148 return m_thread_context_offsets.GetSize();
5149}
5150
5151lldb::RegisterContextSP
5152ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
5153{
Greg Claytonc3776bf2012-02-09 06:16:32 +00005154 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00005155
Greg Claytona1743492012-03-13 23:14:29 +00005156 ModuleSP module_sp(GetModule());
5157 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005158 {
Greg Claytona1743492012-03-13 23:14:29 +00005159 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5160 if (!m_thread_context_offsets_valid)
5161 GetNumThreadContexts ();
5162
5163 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00005164 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00005165 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00005166
5167 DataExtractor data (m_data,
5168 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00005169 thread_context_file_range->GetByteSize());
5170
5171 switch (m_header.cputype)
5172 {
Jason Molendaa3329782014-03-29 18:54:20 +00005173 case llvm::MachO::CPU_TYPE_ARM64:
5174 reg_ctx_sp.reset (new RegisterContextDarwin_arm64_Mach (thread, data));
5175 break;
5176
Charles Davis510938e2013-08-27 05:04:57 +00005177 case llvm::MachO::CPU_TYPE_ARM:
Jim Ingham28eb5712012-10-12 17:34:26 +00005178 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
5179 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005180
Charles Davis510938e2013-08-27 05:04:57 +00005181 case llvm::MachO::CPU_TYPE_I386:
Jim Ingham28eb5712012-10-12 17:34:26 +00005182 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
5183 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005184
Charles Davis510938e2013-08-27 05:04:57 +00005185 case llvm::MachO::CPU_TYPE_X86_64:
Jim Ingham28eb5712012-10-12 17:34:26 +00005186 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
5187 break;
5188 }
Greg Claytona1743492012-03-13 23:14:29 +00005189 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00005190 }
5191 return reg_ctx_sp;
5192}
5193
Greg Claytonc9660542012-02-05 02:38:54 +00005194
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005195ObjectFile::Type
5196ObjectFileMachO::CalculateType()
5197{
5198 switch (m_header.filetype)
5199 {
Charles Davis510938e2013-08-27 05:04:57 +00005200 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005201 if (GetAddressByteSize () == 4)
5202 {
5203 // 32 bit kexts are just object files, but they do have a valid
5204 // UUID load command.
5205 UUID uuid;
5206 if (GetUUID(&uuid))
5207 {
5208 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00005209 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005210 // "OSKextGetCurrentIdentifier" as this is required of kexts
5211 if (m_strata == eStrataInvalid)
5212 m_strata = eStrataKernel;
5213 return eTypeSharedLibrary;
5214 }
5215 }
5216 return eTypeObjectFile;
5217
Charles Davis510938e2013-08-27 05:04:57 +00005218 case MH_EXECUTE: return eTypeExecutable; // 0x2u
5219 case MH_FVMLIB: return eTypeSharedLibrary; // 0x3u
5220 case MH_CORE: return eTypeCoreFile; // 0x4u
5221 case MH_PRELOAD: return eTypeSharedLibrary; // 0x5u
5222 case MH_DYLIB: return eTypeSharedLibrary; // 0x6u
5223 case MH_DYLINKER: return eTypeDynamicLinker; // 0x7u
5224 case MH_BUNDLE: return eTypeSharedLibrary; // 0x8u
5225 case MH_DYLIB_STUB: return eTypeStubLibrary; // 0x9u
5226 case MH_DSYM: return eTypeDebugInfo; // 0xAu
5227 case MH_KEXT_BUNDLE: return eTypeSharedLibrary; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005228 default:
5229 break;
5230 }
5231 return eTypeUnknown;
5232}
5233
5234ObjectFile::Strata
5235ObjectFileMachO::CalculateStrata()
5236{
5237 switch (m_header.filetype)
5238 {
Charles Davis510938e2013-08-27 05:04:57 +00005239 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005240 {
5241 // 32 bit kexts are just object files, but they do have a valid
5242 // UUID load command.
5243 UUID uuid;
5244 if (GetUUID(&uuid))
5245 {
5246 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00005247 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005248 // "OSKextGetCurrentIdentifier" as this is required of kexts
5249 if (m_type == eTypeInvalid)
5250 m_type = eTypeSharedLibrary;
5251
5252 return eStrataKernel;
5253 }
5254 }
5255 return eStrataUnknown;
5256
Charles Davis510938e2013-08-27 05:04:57 +00005257 case MH_EXECUTE: // 0x2u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005258 // Check for the MH_DYLDLINK bit in the flags
Charles Davis510938e2013-08-27 05:04:57 +00005259 if (m_header.flags & MH_DYLDLINK)
Sean Callanan49bce8e2012-02-10 20:22:35 +00005260 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005261 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00005262 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005263 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00005264 {
5265 SectionList *section_list = GetSectionList();
5266 if (section_list)
5267 {
5268 static ConstString g_kld_section_name ("__KLD");
5269 if (section_list->FindSectionByName(g_kld_section_name))
5270 return eStrataKernel;
5271 }
5272 }
5273 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005274
Charles Davis510938e2013-08-27 05:04:57 +00005275 case MH_FVMLIB: return eStrataUser; // 0x3u
5276 case MH_CORE: return eStrataUnknown; // 0x4u
5277 case MH_PRELOAD: return eStrataRawImage; // 0x5u
5278 case MH_DYLIB: return eStrataUser; // 0x6u
5279 case MH_DYLINKER: return eStrataUser; // 0x7u
5280 case MH_BUNDLE: return eStrataUser; // 0x8u
5281 case MH_DYLIB_STUB: return eStrataUser; // 0x9u
5282 case MH_DSYM: return eStrataUnknown; // 0xAu
5283 case MH_KEXT_BUNDLE: return eStrataKernel; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005284 default:
5285 break;
5286 }
5287 return eStrataUnknown;
5288}
5289
5290
Greg Claytonc2ff9312012-02-22 19:41:02 +00005291uint32_t
5292ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
5293{
Greg Claytona1743492012-03-13 23:14:29 +00005294 ModuleSP module_sp(GetModule());
5295 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005296 {
Greg Claytona1743492012-03-13 23:14:29 +00005297 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5298 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00005299 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00005300 uint32_t version_cmd = 0;
5301 uint64_t version = 0;
5302 uint32_t i;
5303 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005304 {
Greg Claytonc7bece562013-01-25 18:06:21 +00005305 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00005306 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
5307 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005308
Charles Davis510938e2013-08-27 05:04:57 +00005309 if (load_cmd.cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005310 {
Greg Claytona1743492012-03-13 23:14:29 +00005311 if (version_cmd == 0)
5312 {
5313 version_cmd = load_cmd.cmd;
5314 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
5315 break;
5316 version = load_cmd.dylib.current_version;
5317 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005318 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00005319 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00005320 }
Greg Claytona1743492012-03-13 23:14:29 +00005321 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00005322 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005323
Charles Davis510938e2013-08-27 05:04:57 +00005324 if (version_cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005325 {
Greg Claytona1743492012-03-13 23:14:29 +00005326 if (versions != NULL && num_versions > 0)
5327 {
5328 if (num_versions > 0)
5329 versions[0] = (version & 0xFFFF0000ull) >> 16;
5330 if (num_versions > 1)
5331 versions[1] = (version & 0x0000FF00ull) >> 8;
5332 if (num_versions > 2)
5333 versions[2] = (version & 0x000000FFull);
5334 // Fill in an remaining version numbers with invalid values
5335 for (i=3; i<num_versions; ++i)
5336 versions[i] = UINT32_MAX;
5337 }
5338 // The LC_ID_DYLIB load command has a version with 3 version numbers
5339 // in it, so always return 3
5340 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00005341 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00005342 }
5343 return false;
5344}
5345
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005346bool
Greg Clayton514487e2011-02-15 21:59:32 +00005347ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005348{
Greg Claytona1743492012-03-13 23:14:29 +00005349 ModuleSP module_sp(GetModule());
5350 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00005351 {
Greg Claytona1743492012-03-13 23:14:29 +00005352 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton7ab7f892014-05-29 21:33:45 +00005353 return GetArchitecture (m_header, m_data, MachHeaderSizeFromMagic(m_header.magic), arch);
Greg Clayton593577a2011-09-21 03:57:31 +00005354 }
Greg Claytona1743492012-03-13 23:14:29 +00005355 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005356}
5357
5358
Jason Molenda0e0954c2013-04-16 06:24:42 +00005359UUID
5360ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
5361{
5362 UUID uuid;
5363 if (process)
5364 {
5365 addr_t all_image_infos = process->GetImageInfoAddress();
5366
5367 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
5368 // or it may be the address of the dyld_all_image_infos structure (want). The first four
5369 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
5370 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
5371
5372 Error err;
5373 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00005374 if (version_or_magic != static_cast<uint32_t>(-1)
Charles Davis510938e2013-08-27 05:04:57 +00005375 && version_or_magic != MH_MAGIC
5376 && version_or_magic != MH_CIGAM
5377 && version_or_magic != MH_MAGIC_64
5378 && version_or_magic != MH_CIGAM_64
Jason Molenda0e0954c2013-04-16 06:24:42 +00005379 && version_or_magic >= 13)
5380 {
5381 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
5382 int wordsize = process->GetAddressByteSize();
5383 if (wordsize == 8)
5384 {
5385 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
5386 }
5387 if (wordsize == 4)
5388 {
5389 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
5390 }
5391 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
5392 {
5393 uuid_t shared_cache_uuid;
5394 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
5395 {
5396 uuid.SetBytes (shared_cache_uuid);
5397 }
5398 }
5399 }
5400 }
5401 return uuid;
5402}
5403
5404UUID
5405ObjectFileMachO::GetLLDBSharedCacheUUID ()
5406{
5407 UUID uuid;
Todd Fiala013434e2014-07-09 01:29:05 +00005408#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molenda0e0954c2013-04-16 06:24:42 +00005409 uint8_t *(*dyld_get_all_image_infos)(void);
5410 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
5411 if (dyld_get_all_image_infos)
5412 {
5413 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
5414 if (dyld_all_image_infos_address)
5415 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00005416 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
5417 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00005418 {
Jason Molendaa3329782014-03-29 18:54:20 +00005419 uuid_t *sharedCacheUUID_address = 0;
5420 int wordsize = sizeof (uint8_t *);
5421 if (wordsize == 8)
5422 {
5423 sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 160); // sharedCacheUUID <mach-o/dyld_images.h>
5424 }
5425 else
5426 {
5427 sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84); // sharedCacheUUID <mach-o/dyld_images.h>
5428 }
Jason Molenda0e0954c2013-04-16 06:24:42 +00005429 uuid.SetBytes (sharedCacheUUID_address);
5430 }
5431 }
5432 }
5433#endif
5434 return uuid;
5435}
5436
Greg Clayton9b234982013-10-24 22:54:08 +00005437uint32_t
5438ObjectFileMachO::GetMinimumOSVersion (uint32_t *versions, uint32_t num_versions)
5439{
5440 if (m_min_os_versions.empty())
5441 {
5442 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5443 bool success = false;
5444 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
5445 {
5446 const lldb::offset_t load_cmd_offset = offset;
5447
5448 version_min_command lc;
5449 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
5450 break;
5451 if (lc.cmd == LC_VERSION_MIN_MACOSX || lc.cmd == LC_VERSION_MIN_IPHONEOS)
5452 {
5453 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
5454 {
5455 const uint32_t xxxx = lc.version >> 16;
5456 const uint32_t yy = (lc.version >> 8) & 0xffu;
5457 const uint32_t zz = lc.version & 0xffu;
5458 if (xxxx)
5459 {
5460 m_min_os_versions.push_back(xxxx);
Greg Clayton8031d282015-02-04 00:40:25 +00005461 m_min_os_versions.push_back(yy);
5462 m_min_os_versions.push_back(zz);
Greg Clayton9b234982013-10-24 22:54:08 +00005463 }
5464 success = true;
5465 }
5466 }
5467 offset = load_cmd_offset + lc.cmdsize;
5468 }
5469
5470 if (success == false)
5471 {
5472 // Push an invalid value so we don't keep trying to
5473 m_min_os_versions.push_back(UINT32_MAX);
5474 }
5475 }
5476
5477 if (m_min_os_versions.size() > 1 || m_min_os_versions[0] != UINT32_MAX)
5478 {
5479 if (versions != NULL && num_versions > 0)
5480 {
5481 for (size_t i=0; i<num_versions; ++i)
5482 {
5483 if (i < m_min_os_versions.size())
5484 versions[i] = m_min_os_versions[i];
5485 else
5486 versions[i] = 0;
5487 }
5488 }
5489 return m_min_os_versions.size();
5490 }
5491 // Call the superclasses version that will empty out the data
5492 return ObjectFile::GetMinimumOSVersion (versions, num_versions);
5493}
5494
5495uint32_t
5496ObjectFileMachO::GetSDKVersion(uint32_t *versions, uint32_t num_versions)
5497{
5498 if (m_sdk_versions.empty())
5499 {
5500 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5501 bool success = false;
5502 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
5503 {
5504 const lldb::offset_t load_cmd_offset = offset;
5505
5506 version_min_command lc;
5507 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
5508 break;
5509 if (lc.cmd == LC_VERSION_MIN_MACOSX || lc.cmd == LC_VERSION_MIN_IPHONEOS)
5510 {
5511 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
5512 {
Todd Fialaebecb382014-09-04 19:31:52 +00005513 const uint32_t xxxx = lc.sdk >> 16;
5514 const uint32_t yy = (lc.sdk >> 8) & 0xffu;
5515 const uint32_t zz = lc.sdk & 0xffu;
Greg Clayton9b234982013-10-24 22:54:08 +00005516 if (xxxx)
5517 {
5518 m_sdk_versions.push_back(xxxx);
Greg Clayton8031d282015-02-04 00:40:25 +00005519 m_sdk_versions.push_back(yy);
5520 m_sdk_versions.push_back(zz);
Greg Clayton9b234982013-10-24 22:54:08 +00005521 }
5522 success = true;
5523 }
5524 }
5525 offset = load_cmd_offset + lc.cmdsize;
5526 }
5527
5528 if (success == false)
5529 {
5530 // Push an invalid value so we don't keep trying to
5531 m_sdk_versions.push_back(UINT32_MAX);
5532 }
5533 }
5534
5535 if (m_sdk_versions.size() > 1 || m_sdk_versions[0] != UINT32_MAX)
5536 {
5537 if (versions != NULL && num_versions > 0)
5538 {
5539 for (size_t i=0; i<num_versions; ++i)
5540 {
5541 if (i < m_sdk_versions.size())
5542 versions[i] = m_sdk_versions[i];
5543 else
5544 versions[i] = 0;
5545 }
5546 }
5547 return m_sdk_versions.size();
5548 }
5549 // Call the superclasses version that will empty out the data
5550 return ObjectFile::GetSDKVersion (versions, num_versions);
5551}
5552
Jason Molenda0e0954c2013-04-16 06:24:42 +00005553
Greg Clayton08928f32015-02-05 02:01:34 +00005554bool
5555ObjectFileMachO::GetIsDynamicLinkEditor()
5556{
5557 return m_header.filetype == llvm::MachO::MH_DYLINKER;
5558}
5559
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005560//------------------------------------------------------------------
5561// PluginInterface protocol
5562//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00005563lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005564ObjectFileMachO::GetPluginName()
5565{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005566 return GetPluginNameStatic();
5567}
5568
5569uint32_t
5570ObjectFileMachO::GetPluginVersion()
5571{
5572 return 1;
5573}
5574
Greg Clayton7524e092014-02-06 20:10:16 +00005575
Greg Clayton07347372015-06-08 21:53:11 +00005576Section *
5577ObjectFileMachO::GetMachHeaderSection()
5578{
5579 // Find the first address of the mach header which is the first non-zero
5580 // file sized section whose file offset is zero. This is the base file address
5581 // of the mach-o file which can be subtracted from the vmaddr of the other
5582 // segments found in memory and added to the load address
5583 ModuleSP module_sp = GetModule();
5584 if (module_sp)
5585 {
5586 SectionList *section_list = GetSectionList ();
5587 if (section_list)
5588 {
5589 lldb::addr_t mach_base_file_addr = LLDB_INVALID_ADDRESS;
5590 const size_t num_sections = section_list->GetSize();
5591
5592 for (size_t sect_idx = 0;
5593 sect_idx < num_sections && mach_base_file_addr == LLDB_INVALID_ADDRESS;
5594 ++sect_idx)
5595 {
5596 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
5597 if (section &&
5598 section->GetFileSize() > 0 &&
5599 section->GetFileOffset() == 0 &&
5600 section->IsThreadSpecific() == false &&
5601 module_sp.get() == section->GetModule().get())
5602 {
5603 return section;
5604 }
5605 }
5606 }
5607 }
5608 return nullptr;
5609}
5610
5611lldb::addr_t
5612ObjectFileMachO::CalculateSectionLoadAddressForMemoryImage(lldb::addr_t mach_header_load_address, const Section *mach_header_section, const Section *section)
5613{
5614 ModuleSP module_sp = GetModule();
5615 if (module_sp && mach_header_section && section && mach_header_load_address != LLDB_INVALID_ADDRESS)
5616 {
5617 lldb::addr_t mach_header_file_addr = mach_header_section->GetFileAddress();
5618 if (mach_header_file_addr != LLDB_INVALID_ADDRESS)
5619 {
5620 if (section &&
5621 section->GetFileSize() > 0 &&
5622 section->IsThreadSpecific() == false &&
5623 module_sp.get() == section->GetModule().get())
5624 {
5625 // Ignore __LINKEDIT and __DWARF segments
5626 if (section->GetName() == GetSegmentNameLINKEDIT())
5627 {
5628 // Only map __LINKEDIT if we have an in memory image and this isn't
5629 // a kernel binary like a kext or mach_kernel.
5630 const bool is_memory_image = (bool)m_process_wp.lock();
5631 const Strata strata = GetStrata();
5632 if (is_memory_image == false || strata == eStrataKernel)
5633 return LLDB_INVALID_ADDRESS;
5634 }
5635 return section->GetFileAddress() - mach_header_file_addr + mach_header_load_address;
5636 }
5637 }
5638 }
5639 return LLDB_INVALID_ADDRESS;
5640}
5641
Greg Clayton7524e092014-02-06 20:10:16 +00005642bool
Greg Clayton751caf62014-02-07 22:54:47 +00005643ObjectFileMachO::SetLoadAddress (Target &target,
5644 lldb::addr_t value,
5645 bool value_is_offset)
Greg Clayton7524e092014-02-06 20:10:16 +00005646{
Greg Clayton7524e092014-02-06 20:10:16 +00005647 ModuleSP module_sp = GetModule();
5648 if (module_sp)
5649 {
5650 size_t num_loaded_sections = 0;
5651 SectionList *section_list = GetSectionList ();
5652 if (section_list)
5653 {
Greg Clayton7524e092014-02-06 20:10:16 +00005654 const size_t num_sections = section_list->GetSize();
5655
Greg Clayton751caf62014-02-07 22:54:47 +00005656 if (value_is_offset)
Greg Clayton7524e092014-02-06 20:10:16 +00005657 {
Greg Clayton751caf62014-02-07 22:54:47 +00005658 // "value" is an offset to apply to each top level segment
Greg Clayton7524e092014-02-06 20:10:16 +00005659 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5660 {
5661 // Iterate through the object file sections to find all
5662 // of the sections that size on disk (to avoid __PAGEZERO)
5663 // and load them
5664 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Greg Clayton751caf62014-02-07 22:54:47 +00005665 if (section_sp &&
5666 section_sp->GetFileSize() > 0 &&
5667 section_sp->IsThreadSpecific() == false &&
5668 module_sp.get() == section_sp->GetModule().get())
Greg Clayton7524e092014-02-06 20:10:16 +00005669 {
Greg Clayton751caf62014-02-07 22:54:47 +00005670 // Ignore __LINKEDIT and __DWARF segments
Greg Clayton07347372015-06-08 21:53:11 +00005671 if (section_sp->GetName() == GetSegmentNameLINKEDIT())
Greg Clayton751caf62014-02-07 22:54:47 +00005672 {
5673 // Only map __LINKEDIT if we have an in memory image and this isn't
5674 // a kernel binary like a kext or mach_kernel.
Greg Clayton07347372015-06-08 21:53:11 +00005675 const bool is_memory_image = (bool)m_process_wp.lock();
5676 const Strata strata = GetStrata();
Greg Clayton751caf62014-02-07 22:54:47 +00005677 if (is_memory_image == false || strata == eStrataKernel)
5678 continue;
5679 }
5680 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
Greg Clayton7524e092014-02-06 20:10:16 +00005681 ++num_loaded_sections;
5682 }
5683 }
5684 }
Greg Clayton751caf62014-02-07 22:54:47 +00005685 else
5686 {
5687 // "value" is the new base address of the mach_header, adjust each
5688 // section accordingly
5689
Greg Clayton07347372015-06-08 21:53:11 +00005690 Section *mach_header_section = GetMachHeaderSection();
5691 if (mach_header_section)
Greg Clayton751caf62014-02-07 22:54:47 +00005692 {
5693 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5694 {
Greg Clayton751caf62014-02-07 22:54:47 +00005695 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Greg Clayton07347372015-06-08 21:53:11 +00005696
5697 lldb::addr_t section_load_addr = CalculateSectionLoadAddressForMemoryImage(value, mach_header_section, section_sp.get());
5698 if (section_load_addr != LLDB_INVALID_ADDRESS)
Greg Clayton751caf62014-02-07 22:54:47 +00005699 {
Greg Clayton07347372015-06-08 21:53:11 +00005700 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_load_addr))
Greg Clayton751caf62014-02-07 22:54:47 +00005701 ++num_loaded_sections;
5702 }
5703 }
5704 }
5705 }
Greg Clayton7524e092014-02-06 20:10:16 +00005706 }
Greg Clayton7524e092014-02-06 20:10:16 +00005707 return num_loaded_sections > 0;
5708 }
Jason Molenda5cf1e232014-10-16 07:41:32 +00005709 return false;
Greg Clayton7524e092014-02-06 20:10:16 +00005710}
5711
Greg Claytona2715cf2014-06-13 00:54:12 +00005712bool
5713ObjectFileMachO::SaveCore (const lldb::ProcessSP &process_sp,
5714 const FileSpec &outfile,
5715 Error &error)
5716{
5717 if (process_sp)
5718 {
5719 Target &target = process_sp->GetTarget();
5720 const ArchSpec target_arch = target.GetArchitecture();
5721 const llvm::Triple &target_triple = target_arch.GetTriple();
5722 if (target_triple.getVendor() == llvm::Triple::Apple &&
5723 (target_triple.getOS() == llvm::Triple::MacOSX ||
5724 target_triple.getOS() == llvm::Triple::IOS))
5725 {
5726 bool make_core = false;
5727 switch (target_arch.GetMachine())
5728 {
Jason Molenda16dc86d2015-06-25 23:58:25 +00005729 case llvm::Triple::aarch64:
Jason Molenda4b0c1182014-11-12 02:39:14 +00005730 case llvm::Triple::arm:
Jason Molenda6d9fe8c2015-08-21 00:13:37 +00005731 case llvm::Triple::thumb:
Greg Claytona2715cf2014-06-13 00:54:12 +00005732 case llvm::Triple::x86:
5733 case llvm::Triple::x86_64:
5734 make_core = true;
5735 break;
5736 default:
5737 error.SetErrorStringWithFormat ("unsupported core architecture: %s", target_triple.str().c_str());
5738 break;
5739 }
5740
5741 if (make_core)
5742 {
5743 std::vector<segment_command_64> segment_load_commands;
Saleem Abdulrasool3924d752014-06-13 03:30:39 +00005744// uint32_t range_info_idx = 0;
Greg Claytona2715cf2014-06-13 00:54:12 +00005745 MemoryRegionInfo range_info;
5746 Error range_error = process_sp->GetMemoryRegionInfo(0, range_info);
Jason Molendad20359d2014-11-11 10:59:15 +00005747 const uint32_t addr_byte_size = target_arch.GetAddressByteSize();
5748 const ByteOrder byte_order = target_arch.GetByteOrder();
Greg Claytona2715cf2014-06-13 00:54:12 +00005749 if (range_error.Success())
5750 {
5751 while (range_info.GetRange().GetRangeBase() != LLDB_INVALID_ADDRESS)
5752 {
5753 const addr_t addr = range_info.GetRange().GetRangeBase();
5754 const addr_t size = range_info.GetRange().GetByteSize();
5755
5756 if (size == 0)
5757 break;
5758
5759 // Calculate correct protections
5760 uint32_t prot = 0;
5761 if (range_info.GetReadable() == MemoryRegionInfo::eYes)
5762 prot |= VM_PROT_READ;
5763 if (range_info.GetWritable() == MemoryRegionInfo::eYes)
5764 prot |= VM_PROT_WRITE;
5765 if (range_info.GetExecutable() == MemoryRegionInfo::eYes)
5766 prot |= VM_PROT_EXECUTE;
5767
5768// printf ("[%3u] [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") %c%c%c\n",
5769// range_info_idx,
5770// addr,
5771// size,
5772// (prot & VM_PROT_READ ) ? 'r' : '-',
5773// (prot & VM_PROT_WRITE ) ? 'w' : '-',
5774// (prot & VM_PROT_EXECUTE) ? 'x' : '-');
5775
5776 if (prot != 0)
5777 {
Jason Molendad20359d2014-11-11 10:59:15 +00005778 uint32_t cmd_type = LC_SEGMENT_64;
5779 uint32_t segment_size = sizeof (segment_command_64);
5780 if (addr_byte_size == 4)
5781 {
5782 cmd_type = LC_SEGMENT;
5783 segment_size = sizeof (segment_command);
5784 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005785 segment_command_64 segment = {
Jason Molendad20359d2014-11-11 10:59:15 +00005786 cmd_type, // uint32_t cmd;
5787 segment_size, // uint32_t cmdsize;
Greg Claytona2715cf2014-06-13 00:54:12 +00005788 {0}, // char segname[16];
Jason Molendad20359d2014-11-11 10:59:15 +00005789 addr, // uint64_t vmaddr; // uint32_t for 32-bit Mach-O
5790 size, // uint64_t vmsize; // uint32_t for 32-bit Mach-O
5791 0, // uint64_t fileoff; // uint32_t for 32-bit Mach-O
5792 size, // uint64_t filesize; // uint32_t for 32-bit Mach-O
Greg Claytona2715cf2014-06-13 00:54:12 +00005793 prot, // uint32_t maxprot;
5794 prot, // uint32_t initprot;
5795 0, // uint32_t nsects;
5796 0 }; // uint32_t flags;
5797 segment_load_commands.push_back(segment);
5798 }
5799 else
5800 {
5801 // No protections and a size of 1 used to be returned from old
5802 // debugservers when we asked about a region that was past the
5803 // last memory region and it indicates the end...
5804 if (size == 1)
5805 break;
5806 }
5807
5808 range_error = process_sp->GetMemoryRegionInfo(range_info.GetRange().GetRangeEnd(), range_info);
5809 if (range_error.Fail())
5810 break;
5811 }
5812
Greg Claytona2715cf2014-06-13 00:54:12 +00005813 StreamString buffer (Stream::eBinary,
5814 addr_byte_size,
5815 byte_order);
5816
5817 mach_header_64 mach_header;
Jason Molendad20359d2014-11-11 10:59:15 +00005818 if (addr_byte_size == 8)
5819 {
5820 mach_header.magic = MH_MAGIC_64;
5821 }
5822 else
5823 {
5824 mach_header.magic = MH_MAGIC;
5825 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005826 mach_header.cputype = target_arch.GetMachOCPUType();
5827 mach_header.cpusubtype = target_arch.GetMachOCPUSubType();
5828 mach_header.filetype = MH_CORE;
5829 mach_header.ncmds = segment_load_commands.size();
5830 mach_header.flags = 0;
5831 mach_header.reserved = 0;
5832 ThreadList &thread_list = process_sp->GetThreadList();
5833 const uint32_t num_threads = thread_list.GetSize();
5834
5835 // Make an array of LC_THREAD data items. Each one contains
5836 // the contents of the LC_THREAD load command. The data doesn't
5837 // contain the load command + load command size, we will
5838 // add the load command and load command size as we emit the data.
5839 std::vector<StreamString> LC_THREAD_datas(num_threads);
5840 for (auto &LC_THREAD_data : LC_THREAD_datas)
5841 {
5842 LC_THREAD_data.GetFlags().Set(Stream::eBinary);
5843 LC_THREAD_data.SetAddressByteSize(addr_byte_size);
5844 LC_THREAD_data.SetByteOrder(byte_order);
5845 }
5846 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx)
5847 {
5848 ThreadSP thread_sp (thread_list.GetThreadAtIndex(thread_idx));
5849 if (thread_sp)
5850 {
5851 switch (mach_header.cputype)
5852 {
Jason Molenda22952582014-11-12 01:11:36 +00005853 case llvm::MachO::CPU_TYPE_ARM64:
5854 RegisterContextDarwin_arm64_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
Greg Claytona2715cf2014-06-13 00:54:12 +00005855 break;
Jason Molenda22952582014-11-12 01:11:36 +00005856
5857 case llvm::MachO::CPU_TYPE_ARM:
5858 RegisterContextDarwin_arm_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5859 break;
5860
Greg Claytona2715cf2014-06-13 00:54:12 +00005861 case llvm::MachO::CPU_TYPE_I386:
Jason Molendad20359d2014-11-11 10:59:15 +00005862 RegisterContextDarwin_i386_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
Greg Claytona2715cf2014-06-13 00:54:12 +00005863 break;
5864
5865 case llvm::MachO::CPU_TYPE_X86_64:
5866 RegisterContextDarwin_x86_64_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5867 break;
5868 }
5869
5870 }
5871 }
5872
5873 // The size of the load command is the size of the segments...
Jason Molendad20359d2014-11-11 10:59:15 +00005874 if (addr_byte_size == 8)
5875 {
5876 mach_header.sizeofcmds = segment_load_commands.size() * sizeof (struct segment_command_64);
5877 }
5878 else
5879 {
5880 mach_header.sizeofcmds = segment_load_commands.size() * sizeof (struct segment_command);
5881 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005882
5883 // and the size of all LC_THREAD load command
5884 for (const auto &LC_THREAD_data : LC_THREAD_datas)
5885 {
Greg Claytone37df2e2014-09-16 20:50:29 +00005886 ++mach_header.ncmds;
Greg Claytona2715cf2014-06-13 00:54:12 +00005887 mach_header.sizeofcmds += 8 + LC_THREAD_data.GetSize();
5888 }
5889
5890 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",
5891 mach_header.magic,
5892 mach_header.cputype,
5893 mach_header.cpusubtype,
5894 mach_header.filetype,
5895 mach_header.ncmds,
5896 mach_header.sizeofcmds,
5897 mach_header.flags,
5898 mach_header.reserved);
5899
5900 // Write the mach header
5901 buffer.PutHex32(mach_header.magic);
5902 buffer.PutHex32(mach_header.cputype);
5903 buffer.PutHex32(mach_header.cpusubtype);
5904 buffer.PutHex32(mach_header.filetype);
5905 buffer.PutHex32(mach_header.ncmds);
5906 buffer.PutHex32(mach_header.sizeofcmds);
5907 buffer.PutHex32(mach_header.flags);
Jason Molendad20359d2014-11-11 10:59:15 +00005908 if (addr_byte_size == 8)
5909 {
5910 buffer.PutHex32(mach_header.reserved);
5911 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005912
5913 // Skip the mach header and all load commands and align to the next
5914 // 0x1000 byte boundary
5915 addr_t file_offset = buffer.GetSize() + mach_header.sizeofcmds;
5916 if (file_offset & 0x00000fff)
5917 {
5918 file_offset += 0x00001000ull;
5919 file_offset &= (~0x00001000ull + 1);
5920 }
5921
5922 for (auto &segment : segment_load_commands)
5923 {
5924 segment.fileoff = file_offset;
5925 file_offset += segment.filesize;
5926 }
5927
5928 // Write out all of the LC_THREAD load commands
5929 for (const auto &LC_THREAD_data : LC_THREAD_datas)
5930 {
5931 const size_t LC_THREAD_data_size = LC_THREAD_data.GetSize();
5932 buffer.PutHex32(LC_THREAD);
5933 buffer.PutHex32(8 + LC_THREAD_data_size); // cmd + cmdsize + data
5934 buffer.Write(LC_THREAD_data.GetData(), LC_THREAD_data_size);
5935 }
5936
5937 // Write out all of the segment load commands
5938 for (const auto &segment : segment_load_commands)
5939 {
5940 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",
5941 segment.cmd,
5942 segment.cmdsize,
5943 segment.vmaddr,
5944 segment.vmaddr + segment.vmsize,
5945 segment.fileoff,
5946 segment.filesize,
5947 segment.maxprot,
5948 segment.initprot,
5949 segment.nsects,
5950 segment.flags);
5951
5952 buffer.PutHex32(segment.cmd);
5953 buffer.PutHex32(segment.cmdsize);
5954 buffer.PutRawBytes(segment.segname, sizeof(segment.segname));
Jason Molendad20359d2014-11-11 10:59:15 +00005955 if (addr_byte_size == 8)
5956 {
5957 buffer.PutHex64(segment.vmaddr);
5958 buffer.PutHex64(segment.vmsize);
5959 buffer.PutHex64(segment.fileoff);
5960 buffer.PutHex64(segment.filesize);
5961 }
5962 else
5963 {
5964 buffer.PutHex32(static_cast<uint32_t>(segment.vmaddr));
5965 buffer.PutHex32(static_cast<uint32_t>(segment.vmsize));
5966 buffer.PutHex32(static_cast<uint32_t>(segment.fileoff));
5967 buffer.PutHex32(static_cast<uint32_t>(segment.filesize));
5968 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005969 buffer.PutHex32(segment.maxprot);
5970 buffer.PutHex32(segment.initprot);
5971 buffer.PutHex32(segment.nsects);
5972 buffer.PutHex32(segment.flags);
5973 }
5974
5975 File core_file;
5976 std::string core_file_path(outfile.GetPath());
5977 error = core_file.Open(core_file_path.c_str(),
5978 File::eOpenOptionWrite |
5979 File::eOpenOptionTruncate |
5980 File::eOpenOptionCanCreate);
5981 if (error.Success())
5982 {
5983 // Read 1 page at a time
5984 uint8_t bytes[0x1000];
5985 // Write the mach header and load commands out to the core file
5986 size_t bytes_written = buffer.GetString().size();
5987 error = core_file.Write(buffer.GetString().data(), bytes_written);
5988 if (error.Success())
5989 {
5990 // Now write the file data for all memory segments in the process
5991 for (const auto &segment : segment_load_commands)
5992 {
David Majnemerb98a5e02014-07-24 00:24:12 +00005993 if (core_file.SeekFromStart(segment.fileoff) == -1)
Greg Claytona2715cf2014-06-13 00:54:12 +00005994 {
5995 error.SetErrorStringWithFormat("unable to seek to offset 0x%" PRIx64 " in '%s'", segment.fileoff, core_file_path.c_str());
5996 break;
5997 }
5998
Jason Molendad20359d2014-11-11 10:59:15 +00005999 printf ("Saving %" PRId64 " bytes of data for memory region at 0x%" PRIx64 "\n", segment.vmsize, segment.vmaddr);
Greg Claytona2715cf2014-06-13 00:54:12 +00006000 addr_t bytes_left = segment.vmsize;
6001 addr_t addr = segment.vmaddr;
6002 Error memory_read_error;
6003 while (bytes_left > 0 && error.Success())
6004 {
6005 const size_t bytes_to_read = bytes_left > sizeof(bytes) ? sizeof(bytes) : bytes_left;
6006 const size_t bytes_read = process_sp->ReadMemory(addr, bytes, bytes_to_read, memory_read_error);
6007 if (bytes_read == bytes_to_read)
6008 {
6009 size_t bytes_written = bytes_read;
6010 error = core_file.Write(bytes, bytes_written);
6011 bytes_left -= bytes_read;
6012 addr += bytes_read;
6013 }
6014 else
6015 {
6016 // Some pages within regions are not readable, those
6017 // should be zero filled
6018 memset (bytes, 0, bytes_to_read);
6019 size_t bytes_written = bytes_to_read;
6020 error = core_file.Write(bytes, bytes_written);
6021 bytes_left -= bytes_to_read;
6022 addr += bytes_to_read;
6023 }
6024 }
6025 }
6026 }
6027 }
6028 }
6029 else
6030 {
6031 error.SetErrorString("process doesn't support getting memory region info");
6032 }
6033 }
6034 return true; // This is the right plug to handle saving core files for this process
6035 }
6036 }
6037 return false;
6038}
6039