blob: 5cbb31ef5b22ae740f28f97dbb16585d565758a4 [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
Greg Clayton456809c2011-12-03 02:30:59 +000010#include "llvm/ADT/StringRef.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000011#include "llvm/Support/MachO.h"
12
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "ObjectFileMachO.h"
14
Greg Clayton3f839a32012-09-05 01:38:55 +000015#include "lldb/lldb-private-log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/ArchSpec.h"
17#include "lldb/Core/DataBuffer.h"
Jason Molendaf6ce26f2013-04-10 05:58:57 +000018#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/FileSpecList.h"
Greg Clayton3f839a32012-09-05 01:38:55 +000020#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000022#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/PluginManager.h"
Greg Clayton1eac0c72012-04-24 03:06:13 +000024#include "lldb/Core/RangeMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Section.h"
26#include "lldb/Core/StreamFile.h"
27#include "lldb/Core/StreamString.h"
28#include "lldb/Core/Timer.h"
29#include "lldb/Core/UUID.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000030#include "lldb/Host/Host.h"
31#include "lldb/Host/FileSpec.h"
Sean Callananb6d70eb2011-10-12 02:08:07 +000032#include "lldb/Symbol/ClangNamespaceDecl.h"
Jason Molenda5635f772013-03-21 03:36:01 +000033#include "lldb/Symbol/DWARFCallFrameInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Symbol/ObjectFile.h"
Greg Clayton26b47e22012-04-18 05:19:20 +000035#include "lldb/Target/Platform.h"
Greg Claytonc9660542012-02-05 02:38:54 +000036#include "lldb/Target/Process.h"
Greg Clayton7524e092014-02-06 20:10:16 +000037#include "lldb/Target/SectionLoadList.h"
Greg Clayton26b47e22012-04-18 05:19:20 +000038#include "lldb/Target/Target.h"
Greg Claytonc859e2d2012-02-13 23:10:39 +000039#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
40#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
Jason Molenda0e0954c2013-04-16 06:24:42 +000043#if defined (__APPLE__) && defined (__arm__)
44// GetLLDBSharedCacheUUID() needs to call dlsym()
45#include <dlfcn.h>
46#endif
47
Daniel Maleaffeb4b62013-04-17 19:24:22 +000048#ifndef __APPLE__
49#include "Utility/UuidCompatibility.h"
50#endif
51
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052using namespace lldb;
53using namespace lldb_private;
Greg Claytone1a916a2010-07-21 22:12:05 +000054using namespace llvm::MachO;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
Jason Molenda4e7511e2013-03-06 23:19:17 +000056class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
Greg Claytonc3776bf2012-02-09 06:16:32 +000057{
58public:
59 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
60 RegisterContextDarwin_x86_64 (thread, 0)
61 {
62 SetRegisterDataFrom_LC_THREAD (data);
63 }
64
65 virtual void
66 InvalidateAllRegisters ()
67 {
68 // Do nothing... registers are always valid...
69 }
70
71 void
72 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
73 {
Greg Claytonc7bece562013-01-25 18:06:21 +000074 lldb::offset_t offset = 0;
Greg Claytonc3776bf2012-02-09 06:16:32 +000075 SetError (GPRRegSet, Read, -1);
76 SetError (FPURegSet, Read, -1);
77 SetError (EXCRegSet, Read, -1);
Greg Claytonc859e2d2012-02-13 23:10:39 +000078 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +000079
Greg Claytonc859e2d2012-02-13 23:10:39 +000080 while (!done)
Greg Claytonc3776bf2012-02-09 06:16:32 +000081 {
Greg Claytonc859e2d2012-02-13 23:10:39 +000082 int flavor = data.GetU32 (&offset);
83 if (flavor == 0)
84 done = true;
85 else
Greg Claytonc3776bf2012-02-09 06:16:32 +000086 {
Greg Claytonc859e2d2012-02-13 23:10:39 +000087 uint32_t i;
88 uint32_t count = data.GetU32 (&offset);
89 switch (flavor)
90 {
91 case GPRRegSet:
92 for (i=0; i<count; ++i)
93 (&gpr.rax)[i] = data.GetU64(&offset);
94 SetError (GPRRegSet, Read, 0);
95 done = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +000096
Greg Claytonc859e2d2012-02-13 23:10:39 +000097 break;
98 case FPURegSet:
99 // TODO: fill in FPU regs....
100 //SetError (FPURegSet, Read, -1);
101 done = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000102
Greg Claytonc859e2d2012-02-13 23:10:39 +0000103 break;
104 case EXCRegSet:
105 exc.trapno = data.GetU32(&offset);
106 exc.err = data.GetU32(&offset);
107 exc.faultvaddr = data.GetU64(&offset);
108 SetError (EXCRegSet, Read, 0);
109 done = true;
110 break;
111 case 7:
112 case 8:
113 case 9:
114 // fancy flavors that encapsulate of the the above
115 // falvors...
116 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000117
Greg Claytonc859e2d2012-02-13 23:10:39 +0000118 default:
119 done = true;
120 break;
121 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000122 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000123 }
124 }
125protected:
126 virtual int
127 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
128 {
129 return 0;
130 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000131
Greg Claytonc859e2d2012-02-13 23:10:39 +0000132 virtual int
133 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
134 {
135 return 0;
136 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000137
Greg Claytonc859e2d2012-02-13 23:10:39 +0000138 virtual int
139 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
140 {
141 return 0;
142 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000143
Greg Claytonc859e2d2012-02-13 23:10:39 +0000144 virtual int
145 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
146 {
147 return 0;
148 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000149
Greg Claytonc859e2d2012-02-13 23:10:39 +0000150 virtual int
151 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
152 {
153 return 0;
154 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000155
Greg Claytonc859e2d2012-02-13 23:10:39 +0000156 virtual int
157 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
158 {
159 return 0;
160 }
161};
Greg Claytonc3776bf2012-02-09 06:16:32 +0000162
Greg Claytonc859e2d2012-02-13 23:10:39 +0000163
Jason Molenda4e7511e2013-03-06 23:19:17 +0000164class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
Greg Claytonc859e2d2012-02-13 23:10:39 +0000165{
166public:
167 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
168 RegisterContextDarwin_i386 (thread, 0)
169 {
170 SetRegisterDataFrom_LC_THREAD (data);
171 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000172
Greg Claytonc859e2d2012-02-13 23:10:39 +0000173 virtual void
174 InvalidateAllRegisters ()
175 {
176 // Do nothing... registers are always valid...
177 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000178
Greg Claytonc859e2d2012-02-13 23:10:39 +0000179 void
180 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
181 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000182 lldb::offset_t offset = 0;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000183 SetError (GPRRegSet, Read, -1);
184 SetError (FPURegSet, Read, -1);
185 SetError (EXCRegSet, Read, -1);
186 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000187
Greg Claytonc859e2d2012-02-13 23:10:39 +0000188 while (!done)
189 {
190 int flavor = data.GetU32 (&offset);
191 if (flavor == 0)
192 done = true;
193 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000194 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000195 uint32_t i;
196 uint32_t count = data.GetU32 (&offset);
197 switch (flavor)
198 {
199 case GPRRegSet:
200 for (i=0; i<count; ++i)
201 (&gpr.eax)[i] = data.GetU32(&offset);
202 SetError (GPRRegSet, Read, 0);
203 done = true;
204
205 break;
206 case FPURegSet:
207 // TODO: fill in FPU regs....
208 //SetError (FPURegSet, Read, -1);
209 done = true;
210
211 break;
212 case EXCRegSet:
213 exc.trapno = data.GetU32(&offset);
214 exc.err = data.GetU32(&offset);
215 exc.faultvaddr = data.GetU32(&offset);
216 SetError (EXCRegSet, Read, 0);
217 done = true;
218 break;
219 case 7:
220 case 8:
221 case 9:
222 // fancy flavors that encapsulate of the the above
223 // falvors...
224 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000225
Greg Claytonc859e2d2012-02-13 23:10:39 +0000226 default:
227 done = true;
228 break;
229 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000230 }
231 }
232 }
233protected:
234 virtual int
235 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
236 {
237 return 0;
238 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000239
Greg Claytonc3776bf2012-02-09 06:16:32 +0000240 virtual int
241 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
242 {
243 return 0;
244 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000245
Greg Claytonc3776bf2012-02-09 06:16:32 +0000246 virtual int
247 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
248 {
249 return 0;
250 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000251
Greg Claytonc3776bf2012-02-09 06:16:32 +0000252 virtual int
253 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
254 {
255 return 0;
256 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000257
Greg Claytonc3776bf2012-02-09 06:16:32 +0000258 virtual int
259 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
260 {
261 return 0;
262 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000263
Greg Claytonc3776bf2012-02-09 06:16:32 +0000264 virtual int
265 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
266 {
267 return 0;
268 }
269};
270
Jason Molenda4e7511e2013-03-06 23:19:17 +0000271class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Claytonc859e2d2012-02-13 23:10:39 +0000272{
273public:
274 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonc2807462012-10-30 23:57:32 +0000275 RegisterContextDarwin_arm (thread, 0)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000276 {
277 SetRegisterDataFrom_LC_THREAD (data);
278 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000279
Greg Claytonc859e2d2012-02-13 23:10:39 +0000280 virtual void
281 InvalidateAllRegisters ()
282 {
283 // Do nothing... registers are always valid...
284 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000285
Greg Claytonc859e2d2012-02-13 23:10:39 +0000286 void
287 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
288 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000289 lldb::offset_t offset = 0;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000290 SetError (GPRRegSet, Read, -1);
291 SetError (FPURegSet, Read, -1);
292 SetError (EXCRegSet, Read, -1);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000293 bool done = false;
294
295 while (!done)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000296 {
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000297 int flavor = data.GetU32 (&offset);
298 uint32_t count = data.GetU32 (&offset);
Jason Molendaddf91772013-05-14 04:50:47 +0000299 lldb::offset_t next_thread_state = offset + (count * 4);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000300 switch (flavor)
301 {
302 case GPRRegSet:
303 for (uint32_t i=0; i<count; ++i)
Jason Molendaddf91772013-05-14 04:50:47 +0000304 {
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000305 gpr.r[i] = data.GetU32(&offset);
Jason Molendaddf91772013-05-14 04:50:47 +0000306 }
307
308 // Note that gpr.cpsr is also copied by the above loop; this loop technically extends
309 // one element past the end of the gpr.r[] array.
310
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000311 SetError (GPRRegSet, Read, 0);
Jason Molendaddf91772013-05-14 04:50:47 +0000312 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000313 break;
314
315 case FPURegSet:
316 {
Jason Molenda663d2e12013-05-14 03:52:22 +0000317 uint8_t *fpu_reg_buf = (uint8_t*) &fpu.floats.s[0];
318 const int fpu_reg_buf_size = sizeof (fpu.floats);
319 if (data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000320 {
Jason Molenda663d2e12013-05-14 03:52:22 +0000321 offset += fpu_reg_buf_size;
322 fpu.fpscr = data.GetU32(&offset);
323 SetError (FPURegSet, Read, 0);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000324 }
Jason Molenda663d2e12013-05-14 03:52:22 +0000325 else
326 {
327 done = true;
328 }
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000329 }
Jason Molendaddf91772013-05-14 04:50:47 +0000330 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000331 break;
332
333 case EXCRegSet:
Jason Molendaddf91772013-05-14 04:50:47 +0000334 if (count == 3)
335 {
336 exc.exception = data.GetU32(&offset);
337 exc.fsr = data.GetU32(&offset);
338 exc.far = data.GetU32(&offset);
339 SetError (EXCRegSet, Read, 0);
340 }
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000341 done = true;
Jason Molendaddf91772013-05-14 04:50:47 +0000342 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000343 break;
344
345 // Unknown register set flavor, stop trying to parse.
346 default:
347 done = true;
348 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000349 }
350 }
351protected:
352 virtual int
353 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
354 {
Jason Molendaddf91772013-05-14 04:50:47 +0000355 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000356 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000357
Greg Claytonc859e2d2012-02-13 23:10:39 +0000358 virtual int
359 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
360 {
Jason Molendaddf91772013-05-14 04:50:47 +0000361 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000362 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000363
Greg Claytonc859e2d2012-02-13 23:10:39 +0000364 virtual int
365 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
366 {
Jason Molendaddf91772013-05-14 04:50:47 +0000367 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000368 }
Greg Claytonc2807462012-10-30 23:57:32 +0000369
370 virtual int
371 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
372 {
373 return -1;
374 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000375
Greg Claytonc859e2d2012-02-13 23:10:39 +0000376 virtual int
377 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
378 {
379 return 0;
380 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000381
Greg Claytonc859e2d2012-02-13 23:10:39 +0000382 virtual int
383 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
384 {
385 return 0;
386 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000387
Greg Claytonc859e2d2012-02-13 23:10:39 +0000388 virtual int
389 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
390 {
391 return 0;
392 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000393
Greg Claytonc2807462012-10-30 23:57:32 +0000394 virtual int
395 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
396 {
397 return -1;
398 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000399};
400
Greg Clayton9aae0a12013-05-15 19:52:08 +0000401static uint32_t
402MachHeaderSizeFromMagic(uint32_t magic)
403{
404 switch (magic)
405 {
Charles Davis510938e2013-08-27 05:04:57 +0000406 case MH_MAGIC:
407 case MH_CIGAM:
Greg Clayton9aae0a12013-05-15 19:52:08 +0000408 return sizeof(struct mach_header);
409
Charles Davis510938e2013-08-27 05:04:57 +0000410 case MH_MAGIC_64:
411 case MH_CIGAM_64:
Greg Clayton9aae0a12013-05-15 19:52:08 +0000412 return sizeof(struct mach_header_64);
413 break;
414
415 default:
416 break;
417 }
418 return 0;
419}
420
Greg Claytonded470d2011-03-19 01:12:21 +0000421#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422
423void
424ObjectFileMachO::Initialize()
425{
426 PluginManager::RegisterPlugin (GetPluginNameStatic(),
427 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +0000428 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000429 CreateMemoryInstance,
430 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431}
432
433void
434ObjectFileMachO::Terminate()
435{
436 PluginManager::UnregisterPlugin (CreateInstance);
437}
438
439
Greg Clayton57abc5d2013-05-10 21:47:16 +0000440lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441ObjectFileMachO::GetPluginNameStatic()
442{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000443 static ConstString g_name("mach-o");
444 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445}
446
447const char *
448ObjectFileMachO::GetPluginDescriptionStatic()
449{
450 return "Mach-o object file reader (32 and 64 bit)";
451}
452
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453ObjectFile *
Greg Clayton5ce9c562013-02-06 17:22:03 +0000454ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
455 DataBufferSP& data_sp,
456 lldb::offset_t data_offset,
457 const FileSpec* file,
458 lldb::offset_t file_offset,
459 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000461 if (!data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000463 data_sp = file->MemoryMapFileContents(file_offset, length);
464 data_offset = 0;
465 }
466
467 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
468 {
469 // Update the data to contain the entire file if it doesn't already
470 if (data_sp->GetByteSize() < length)
471 {
472 data_sp = file->MemoryMapFileContents(file_offset, length);
473 data_offset = 0;
474 }
Greg Clayton7b0992d2013-04-18 22:45:39 +0000475 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 +0000476 if (objfile_ap.get() && objfile_ap->ParseHeader())
477 return objfile_ap.release();
478 }
479 return NULL;
480}
481
Greg Claytonc9660542012-02-05 02:38:54 +0000482ObjectFile *
Jason Molenda4e7511e2013-03-06 23:19:17 +0000483ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
484 DataBufferSP& data_sp,
485 const ProcessSP &process_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000486 lldb::addr_t header_addr)
487{
488 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
489 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000490 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonc9660542012-02-05 02:38:54 +0000491 if (objfile_ap.get() && objfile_ap->ParseHeader())
492 return objfile_ap.release();
493 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000494 return NULL;
Greg Claytonc9660542012-02-05 02:38:54 +0000495}
496
Greg Claytonf4d6de62013-04-24 22:29:28 +0000497size_t
498ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
499 lldb::DataBufferSP& data_sp,
500 lldb::offset_t data_offset,
501 lldb::offset_t file_offset,
502 lldb::offset_t length,
503 lldb_private::ModuleSpecList &specs)
504{
505 const size_t initial_count = specs.GetSize();
506
507 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
508 {
509 DataExtractor data;
510 data.SetData(data_sp);
511 llvm::MachO::mach_header header;
512 if (ParseHeader (data, &data_offset, header))
513 {
514 if (header.sizeofcmds >= data_sp->GetByteSize())
515 {
516 data_sp = file.ReadFileContents(file_offset, header.sizeofcmds);
Greg Clayton2540a8a2013-07-12 22:07:46 +0000517 data.SetData(data_sp);
518 data_offset = MachHeaderSizeFromMagic(header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +0000519 }
520 if (data_sp)
521 {
522 ModuleSpec spec;
523 spec.GetFileSpec() = file;
524 spec.GetArchitecture().SetArchitecture(eArchTypeMachO,
525 header.cputype,
526 header.cpusubtype);
Charles Davis510938e2013-08-27 05:04:57 +0000527 if (header.filetype == MH_PRELOAD) // 0x5u
Jason Molendab000e4d2013-08-27 02:22:06 +0000528 {
529 // Set OS to "unknown" - this is a standalone binary with no dyld et al
530 spec.GetArchitecture().GetTriple().setOS (llvm::Triple::UnknownOS);
531 }
Greg Claytonf4d6de62013-04-24 22:29:28 +0000532 if (spec.GetArchitecture().IsValid())
533 {
534 GetUUID (header, data, data_offset, spec.GetUUID());
535 specs.Append(spec);
536 }
537 }
538 }
539 }
540 return specs.GetSize() - initial_count;
541}
542
543
Greg Claytonc9660542012-02-05 02:38:54 +0000544
545const ConstString &
546ObjectFileMachO::GetSegmentNameTEXT()
547{
548 static ConstString g_segment_name_TEXT ("__TEXT");
549 return g_segment_name_TEXT;
550}
551
552const ConstString &
553ObjectFileMachO::GetSegmentNameDATA()
554{
555 static ConstString g_segment_name_DATA ("__DATA");
556 return g_segment_name_DATA;
557}
558
559const ConstString &
560ObjectFileMachO::GetSegmentNameOBJC()
561{
562 static ConstString g_segment_name_OBJC ("__OBJC");
563 return g_segment_name_OBJC;
564}
565
566const ConstString &
567ObjectFileMachO::GetSegmentNameLINKEDIT()
568{
569 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
570 return g_section_name_LINKEDIT;
571}
572
573const ConstString &
574ObjectFileMachO::GetSectionNameEHFrame()
575{
576 static ConstString g_section_name_eh_frame ("__eh_frame");
577 return g_section_name_eh_frame;
578}
579
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580bool
Jason Molenda4e7511e2013-03-06 23:19:17 +0000581ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
582 lldb::addr_t data_offset,
Greg Clayton44435ed2012-01-12 05:25:17 +0000583 lldb::addr_t data_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584{
Greg Clayton44435ed2012-01-12 05:25:17 +0000585 DataExtractor data;
586 data.SetData (data_sp, data_offset, data_length);
Greg Claytonc7bece562013-01-25 18:06:21 +0000587 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588 uint32_t magic = data.GetU32(&offset);
589 return MachHeaderSizeFromMagic(magic) != 0;
590}
591
592
Greg Clayton5ce9c562013-02-06 17:22:03 +0000593ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
594 DataBufferSP& data_sp,
595 lldb::offset_t data_offset,
596 const FileSpec* file,
597 lldb::offset_t file_offset,
598 lldb::offset_t length) :
599 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000600 m_mach_segments(),
601 m_mach_sections(),
602 m_entry_point_address(),
603 m_thread_context_offsets(),
604 m_thread_context_offsets_valid(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605{
Greg Clayton72b77eb2011-02-04 21:13:05 +0000606 ::memset (&m_header, 0, sizeof(m_header));
607 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608}
609
Greg Claytone72dfb32012-02-24 01:59:29 +0000610ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000611 lldb::DataBufferSP& header_data_sp,
612 const lldb::ProcessSP &process_sp,
613 lldb::addr_t header_addr) :
Greg Claytone72dfb32012-02-24 01:59:29 +0000614 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000615 m_mach_segments(),
616 m_mach_sections(),
617 m_entry_point_address(),
618 m_thread_context_offsets(),
619 m_thread_context_offsets_valid(false)
Greg Claytonc9660542012-02-05 02:38:54 +0000620{
621 ::memset (&m_header, 0, sizeof(m_header));
622 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
623}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624
625ObjectFileMachO::~ObjectFileMachO()
626{
627}
628
Greg Claytonf4d6de62013-04-24 22:29:28 +0000629bool
630ObjectFileMachO::ParseHeader (DataExtractor &data,
631 lldb::offset_t *data_offset_ptr,
632 llvm::MachO::mach_header &header)
633{
634 data.SetByteOrder (lldb::endian::InlHostByteOrder());
635 // Leave magic in the original byte order
636 header.magic = data.GetU32(data_offset_ptr);
637 bool can_parse = false;
638 bool is_64_bit = false;
639 switch (header.magic)
640 {
Charles Davis510938e2013-08-27 05:04:57 +0000641 case MH_MAGIC:
Greg Claytonf4d6de62013-04-24 22:29:28 +0000642 data.SetByteOrder (lldb::endian::InlHostByteOrder());
643 data.SetAddressByteSize(4);
644 can_parse = true;
645 break;
646
Charles Davis510938e2013-08-27 05:04:57 +0000647 case MH_MAGIC_64:
Greg Claytonf4d6de62013-04-24 22:29:28 +0000648 data.SetByteOrder (lldb::endian::InlHostByteOrder());
649 data.SetAddressByteSize(8);
650 can_parse = true;
651 is_64_bit = true;
652 break;
653
Charles Davis510938e2013-08-27 05:04:57 +0000654 case MH_CIGAM:
Greg Claytonf4d6de62013-04-24 22:29:28 +0000655 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
656 data.SetAddressByteSize(4);
657 can_parse = true;
658 break;
659
Charles Davis510938e2013-08-27 05:04:57 +0000660 case MH_CIGAM_64:
Greg Claytonf4d6de62013-04-24 22:29:28 +0000661 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
662 data.SetAddressByteSize(8);
663 is_64_bit = true;
664 can_parse = true;
665 break;
666
667 default:
668 break;
669 }
670
671 if (can_parse)
672 {
673 data.GetU32(data_offset_ptr, &header.cputype, 6);
674 if (is_64_bit)
675 *data_offset_ptr += 4;
676 return true;
677 }
678 else
679 {
680 memset(&header, 0, sizeof(header));
681 }
682 return false;
683}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684
685bool
686ObjectFileMachO::ParseHeader ()
687{
Greg Claytona1743492012-03-13 23:14:29 +0000688 ModuleSP module_sp(GetModule());
689 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690 {
Greg Claytona1743492012-03-13 23:14:29 +0000691 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
692 bool can_parse = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000693 lldb::offset_t offset = 0;
Greg Clayton7fb56d02011-02-01 01:31:41 +0000694 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +0000695 // Leave magic in the original byte order
696 m_header.magic = m_data.GetU32(&offset);
697 switch (m_header.magic)
Greg Claytonc9660542012-02-05 02:38:54 +0000698 {
Charles Davis510938e2013-08-27 05:04:57 +0000699 case MH_MAGIC:
Greg Claytona1743492012-03-13 23:14:29 +0000700 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
701 m_data.SetAddressByteSize(4);
702 can_parse = true;
703 break;
704
Charles Davis510938e2013-08-27 05:04:57 +0000705 case MH_MAGIC_64:
Greg Claytona1743492012-03-13 23:14:29 +0000706 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
707 m_data.SetAddressByteSize(8);
708 can_parse = true;
709 break;
710
Charles Davis510938e2013-08-27 05:04:57 +0000711 case MH_CIGAM:
Greg Claytona1743492012-03-13 23:14:29 +0000712 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
713 m_data.SetAddressByteSize(4);
714 can_parse = true;
715 break;
716
Charles Davis510938e2013-08-27 05:04:57 +0000717 case MH_CIGAM_64:
Greg Claytona1743492012-03-13 23:14:29 +0000718 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
719 m_data.SetAddressByteSize(8);
720 can_parse = true;
721 break;
722
723 default:
724 break;
Greg Claytonc9660542012-02-05 02:38:54 +0000725 }
Greg Claytona1743492012-03-13 23:14:29 +0000726
727 if (can_parse)
728 {
729 m_data.GetU32(&offset, &m_header.cputype, 6);
730
731 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +0000732
Greg Claytond97ec1c2012-11-16 21:36:10 +0000733 // Check if the module has a required architecture
734 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callananbf4b7be2012-12-13 22:07:14 +0000735 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Claytond97ec1c2012-11-16 21:36:10 +0000736 return false;
737
Greg Claytona1743492012-03-13 23:14:29 +0000738 if (SetModulesArchitecture (mach_arch))
739 {
740 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
741 if (m_data.GetByteSize() < header_and_lc_size)
742 {
743 DataBufferSP data_sp;
744 ProcessSP process_sp (m_process_wp.lock());
745 if (process_sp)
746 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000747 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Claytona1743492012-03-13 23:14:29 +0000748 }
749 else
750 {
751 // Read in all only the load command data from the file on disk
Greg Clayton5ce9c562013-02-06 17:22:03 +0000752 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Claytona1743492012-03-13 23:14:29 +0000753 if (data_sp->GetByteSize() != header_and_lc_size)
754 return false;
755 }
756 if (data_sp)
757 m_data.SetData (data_sp);
758 }
759 }
760 return true;
761 }
762 else
763 {
764 memset(&m_header, 0, sizeof(struct mach_header));
765 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766 }
767 return false;
768}
769
770
771ByteOrder
772ObjectFileMachO::GetByteOrder () const
773{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774 return m_data.GetByteOrder ();
775}
776
Jim Ingham5aee1622010-08-09 23:31:02 +0000777bool
778ObjectFileMachO::IsExecutable() const
779{
Charles Davis510938e2013-08-27 05:04:57 +0000780 return m_header.filetype == MH_EXECUTE;
Jim Ingham5aee1622010-08-09 23:31:02 +0000781}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782
Greg Claytonc7bece562013-01-25 18:06:21 +0000783uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784ObjectFileMachO::GetAddressByteSize () const
785{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786 return m_data.GetAddressByteSize ();
787}
788
Greg Claytone0d378b2011-03-24 21:19:54 +0000789AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +0000790ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
791{
792 Symtab *symtab = GetSymtab();
793 if (symtab)
794 {
795 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
796 if (symbol)
797 {
Greg Claytone7612132012-03-07 21:03:09 +0000798 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +0000799 {
Greg Claytone7612132012-03-07 21:03:09 +0000800 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +0000801 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +0000802 {
Charles Davis510938e2013-08-27 05:04:57 +0000803 const lldb::SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000804 switch (section_type)
805 {
806 case eSectionTypeInvalid: return eAddressClassUnknown;
807 case eSectionTypeCode:
Charles Davis510938e2013-08-27 05:04:57 +0000808 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +0000809 {
810 // For ARM we have a bit in the n_desc field of the symbol
811 // that tells us ARM/Thumb which is bit 0x0008.
812 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
813 return eAddressClassCodeAlternateISA;
814 }
815 return eAddressClassCode;
816
817 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton5009f9d2011-10-27 17:55:14 +0000818 case eSectionTypeData:
819 case eSectionTypeDataCString:
820 case eSectionTypeDataCStringPointers:
821 case eSectionTypeDataSymbolAddress:
822 case eSectionTypeData4:
823 case eSectionTypeData8:
824 case eSectionTypeData16:
825 case eSectionTypeDataPointers:
826 case eSectionTypeZeroFill:
827 case eSectionTypeDataObjCMessageRefs:
828 case eSectionTypeDataObjCCFStrings:
829 return eAddressClassData;
830 case eSectionTypeDebug:
831 case eSectionTypeDWARFDebugAbbrev:
832 case eSectionTypeDWARFDebugAranges:
833 case eSectionTypeDWARFDebugFrame:
834 case eSectionTypeDWARFDebugInfo:
835 case eSectionTypeDWARFDebugLine:
836 case eSectionTypeDWARFDebugLoc:
837 case eSectionTypeDWARFDebugMacInfo:
838 case eSectionTypeDWARFDebugPubNames:
839 case eSectionTypeDWARFDebugPubTypes:
840 case eSectionTypeDWARFDebugRanges:
841 case eSectionTypeDWARFDebugStr:
842 case eSectionTypeDWARFAppleNames:
843 case eSectionTypeDWARFAppleTypes:
844 case eSectionTypeDWARFAppleNamespaces:
845 case eSectionTypeDWARFAppleObjC:
846 return eAddressClassDebug;
Greg Claytonded470d2011-03-19 01:12:21 +0000847 case eSectionTypeEHFrame: return eAddressClassRuntime;
Michael Sartaina7499c92013-07-01 19:45:50 +0000848 case eSectionTypeELFSymbolTable:
849 case eSectionTypeELFDynamicSymbols:
850 case eSectionTypeELFRelocationEntries:
851 case eSectionTypeELFDynamicLinkInfo:
Greg Claytonded470d2011-03-19 01:12:21 +0000852 case eSectionTypeOther: return eAddressClassUnknown;
853 }
854 }
855 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000856
Greg Claytone0d378b2011-03-24 21:19:54 +0000857 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000858 switch (symbol_type)
859 {
860 case eSymbolTypeAny: return eAddressClassUnknown;
861 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000862
Greg Claytonded470d2011-03-19 01:12:21 +0000863 case eSymbolTypeCode:
864 case eSymbolTypeTrampoline:
Greg Clayton059f7242013-02-27 21:16:04 +0000865 case eSymbolTypeResolver:
Charles Davis510938e2013-08-27 05:04:57 +0000866 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +0000867 {
868 // For ARM we have a bit in the n_desc field of the symbol
869 // that tells us ARM/Thumb which is bit 0x0008.
870 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
871 return eAddressClassCodeAlternateISA;
872 }
873 return eAddressClassCode;
874
875 case eSymbolTypeData: return eAddressClassData;
876 case eSymbolTypeRuntime: return eAddressClassRuntime;
877 case eSymbolTypeException: return eAddressClassRuntime;
878 case eSymbolTypeSourceFile: return eAddressClassDebug;
879 case eSymbolTypeHeaderFile: return eAddressClassDebug;
880 case eSymbolTypeObjectFile: return eAddressClassDebug;
881 case eSymbolTypeCommonBlock: return eAddressClassDebug;
882 case eSymbolTypeBlock: return eAddressClassDebug;
883 case eSymbolTypeLocal: return eAddressClassData;
884 case eSymbolTypeParam: return eAddressClassData;
885 case eSymbolTypeVariable: return eAddressClassData;
886 case eSymbolTypeVariableType: return eAddressClassDebug;
887 case eSymbolTypeLineEntry: return eAddressClassDebug;
888 case eSymbolTypeLineHeader: return eAddressClassDebug;
889 case eSymbolTypeScopeBegin: return eAddressClassDebug;
890 case eSymbolTypeScopeEnd: return eAddressClassDebug;
891 case eSymbolTypeAdditional: return eAddressClassUnknown;
892 case eSymbolTypeCompiler: return eAddressClassDebug;
893 case eSymbolTypeInstrumentation:return eAddressClassDebug;
894 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +0000895 case eSymbolTypeObjCClass: return eAddressClassRuntime;
896 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
897 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Clayton9191db42013-10-21 18:40:51 +0000898 case eSymbolTypeReExported: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +0000899 }
900 }
901 }
902 return eAddressClassUnknown;
903}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904
905Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +0000906ObjectFileMachO::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907{
Greg Claytona1743492012-03-13 23:14:29 +0000908 ModuleSP module_sp(GetModule());
909 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910 {
Greg Claytona1743492012-03-13 23:14:29 +0000911 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
912 if (m_symtab_ap.get() == NULL)
913 {
914 m_symtab_ap.reset(new Symtab(this));
915 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton3046e662013-07-10 01:23:25 +0000916 ParseSymtab ();
Greg Claytona1743492012-03-13 23:14:29 +0000917 m_symtab_ap->Finalize ();
918 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919 }
920 return m_symtab_ap.get();
921}
922
Greg Clayton3046e662013-07-10 01:23:25 +0000923bool
924ObjectFileMachO::IsStripped ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925{
Greg Clayton3046e662013-07-10 01:23:25 +0000926 if (m_dysymtab.cmd == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927 {
Greg Clayton3046e662013-07-10 01:23:25 +0000928 ModuleSP module_sp(GetModule());
929 if (module_sp)
Greg Claytona1743492012-03-13 23:14:29 +0000930 {
Greg Clayton3046e662013-07-10 01:23:25 +0000931 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
932 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton4d78c402012-05-25 18:09:55 +0000933 {
Greg Clayton3046e662013-07-10 01:23:25 +0000934 const lldb::offset_t load_cmd_offset = offset;
935
936 load_command lc;
937 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
938 break;
Charles Davis510938e2013-08-27 05:04:57 +0000939 if (lc.cmd == LC_DYSYMTAB)
Greg Clayton4d78c402012-05-25 18:09:55 +0000940 {
Greg Clayton3046e662013-07-10 01:23:25 +0000941 m_dysymtab.cmd = lc.cmd;
942 m_dysymtab.cmdsize = lc.cmdsize;
943 if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
944 {
945 // Clear m_dysymtab if we were unable to read all items from the load command
946 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
947 }
Greg Clayton4d78c402012-05-25 18:09:55 +0000948 }
Greg Clayton3046e662013-07-10 01:23:25 +0000949 offset = load_cmd_offset + lc.cmdsize;
Greg Clayton4d78c402012-05-25 18:09:55 +0000950 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000951 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000952 }
Greg Clayton3046e662013-07-10 01:23:25 +0000953 if (m_dysymtab.cmd)
Greg Clayton93e28612013-10-11 22:03:48 +0000954 return m_dysymtab.nlocalsym <= 1;
Greg Clayton3046e662013-07-10 01:23:25 +0000955 return false;
956}
Greg Clayton1eac0c72012-04-24 03:06:13 +0000957
Greg Clayton3046e662013-07-10 01:23:25 +0000958void
959ObjectFileMachO::CreateSections (SectionList &unified_section_list)
960{
961 if (!m_sections_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962 {
Greg Clayton3046e662013-07-10 01:23:25 +0000963 m_sections_ap.reset(new SectionList());
964
Charles Davis510938e2013-08-27 05:04:57 +0000965 const bool is_dsym = (m_header.filetype == MH_DSYM);
Greg Clayton3046e662013-07-10 01:23:25 +0000966 lldb::user_id_t segID = 0;
967 lldb::user_id_t sectID = 0;
968 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
969 uint32_t i;
970 const bool is_core = GetType() == eTypeCoreFile;
971 //bool dump_sections = false;
972 ModuleSP module_sp (GetModule());
973 // First look up any LC_ENCRYPTION_INFO load commands
974 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
975 EncryptedFileRanges encrypted_file_ranges;
976 encryption_info_command encryption_cmd;
977 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000978 {
Greg Clayton3046e662013-07-10 01:23:25 +0000979 const lldb::offset_t load_cmd_offset = offset;
980 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
981 break;
982
Charles Davis510938e2013-08-27 05:04:57 +0000983 if (encryption_cmd.cmd == LC_ENCRYPTION_INFO)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000984 {
Greg Clayton3046e662013-07-10 01:23:25 +0000985 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000986 {
Greg Clayton3046e662013-07-10 01:23:25 +0000987 if (encryption_cmd.cryptid != 0)
Greg Claytond37d6922013-04-16 16:51:19 +0000988 {
Greg Clayton3046e662013-07-10 01:23:25 +0000989 EncryptedFileRanges::Entry entry;
990 entry.SetRangeBase(encryption_cmd.cryptoff);
991 entry.SetByteSize(encryption_cmd.cryptsize);
992 encrypted_file_ranges.Append(entry);
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000993 }
994 }
Greg Clayton3046e662013-07-10 01:23:25 +0000995 }
996 offset = load_cmd_offset + encryption_cmd.cmdsize;
997 }
998
999 offset = MachHeaderSizeFromMagic(m_header.magic);
1000
1001 struct segment_command_64 load_cmd;
1002 for (i=0; i<m_header.ncmds; ++i)
1003 {
1004 const lldb::offset_t load_cmd_offset = offset;
1005 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1006 break;
1007
Charles Davis510938e2013-08-27 05:04:57 +00001008 if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64)
Greg Clayton3046e662013-07-10 01:23:25 +00001009 {
1010 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011 {
Greg Clayton3046e662013-07-10 01:23:25 +00001012 bool add_section = true;
1013 bool add_to_unified = true;
1014 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 +00001015
Greg Clayton3046e662013-07-10 01:23:25 +00001016 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1017 if (is_dsym && unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018 {
Greg Clayton3046e662013-07-10 01:23:25 +00001019 if (const_segname == GetSegmentNameLINKEDIT())
1020 {
1021 // We need to keep the __LINKEDIT segment private to this object file only
1022 add_to_unified = false;
1023 }
1024 else
1025 {
1026 // This is the dSYM file and this section has already been created by
1027 // the object file, no need to create it.
1028 add_section = false;
1029 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030 }
Greg Clayton3046e662013-07-10 01:23:25 +00001031 load_cmd.vmaddr = m_data.GetAddress(&offset);
1032 load_cmd.vmsize = m_data.GetAddress(&offset);
1033 load_cmd.fileoff = m_data.GetAddress(&offset);
1034 load_cmd.filesize = m_data.GetAddress(&offset);
1035 if (m_length != 0 && load_cmd.filesize != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001036 {
Greg Clayton3046e662013-07-10 01:23:25 +00001037 if (load_cmd.fileoff > m_length)
1038 {
1039 // We have a load command that says it extends past the end of hte file. This is likely
1040 // a corrupt file. We don't have any way to return an error condition here (this method
1041 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1042 // is null out the SectionList vector and if a process has been set up, dump a message
1043 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001044 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001045 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 +00001046 i,
1047 lc_segment_name,
1048 load_cmd.fileoff,
1049 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001050
1051 load_cmd.fileoff = 0;
1052 load_cmd.filesize = 0;
1053 }
1054
1055 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1056 {
1057 // We have a load command that says it extends past the end of hte file. This is likely
1058 // a corrupt file. We don't have any way to return an error condition here (this method
1059 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1060 // is null out the SectionList vector and if a process has been set up, dump a message
1061 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001062 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001063 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 +00001064 i,
1065 lc_segment_name,
1066 load_cmd.fileoff + load_cmd.filesize,
1067 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001068
1069 // Tuncase the length
1070 load_cmd.filesize = m_length - load_cmd.fileoff;
1071 }
1072 }
1073 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1074 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075
Charles Davis510938e2013-08-27 05:04:57 +00001076 const bool segment_is_encrypted = (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001077
Greg Clayton3046e662013-07-10 01:23:25 +00001078 // Keep a list of mach segments around in case we need to
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079 // get at data that isn't stored in the abstracted Sections.
Greg Clayton3046e662013-07-10 01:23:25 +00001080 m_mach_segments.push_back (load_cmd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081
Greg Clayton3046e662013-07-10 01:23:25 +00001082 // Use a segment ID of the segment index shifted left by 8 so they
1083 // never conflict with any of the sections.
1084 SectionSP segment_sp;
1085 if (add_section && (const_segname || is_core))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086 {
Greg Clayton3046e662013-07-10 01:23:25 +00001087 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
1088 this, // Object file to which this sections belongs
1089 ++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
1090 const_segname, // Name of this section
1091 eSectionTypeContainer, // This section is a container of other sections.
1092 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1093 load_cmd.vmsize, // VM size in bytes of this section
1094 load_cmd.fileoff, // Offset to the data for this section in the file
1095 load_cmd.filesize, // Size in bytes of this section as found in the the file
1096 load_cmd.flags)); // Flags for this section
Greg Clayton8d38ac42010-06-28 23:51:11 +00001097
Greg Clayton3046e662013-07-10 01:23:25 +00001098 segment_sp->SetIsEncrypted (segment_is_encrypted);
1099 m_sections_ap->AddSection(segment_sp);
1100 if (add_to_unified)
1101 unified_section_list.AddSection(segment_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001102 }
Greg Clayton3046e662013-07-10 01:23:25 +00001103 else if (unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104 {
Jason Molenda20eb31b2013-08-16 03:20:42 +00001105 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr)
1106 {
1107 // Check to see if the module was read from memory?
1108 if (module_sp->GetObjectFile()->GetHeaderAddress().IsValid())
1109 {
1110 // We have a module that is in memory and needs to have its
1111 // file address adjusted. We need to do this because when we
1112 // load a file from memory, its addresses will be slid already,
1113 // yet the addresses in the new symbol file will still be unslid.
1114 // Since everything is stored as section offset, this shouldn't
1115 // cause any problems.
Jason Molenda5894a732013-08-17 03:39:52 +00001116
1117 // Make sure we've parsed the symbol table from the
1118 // ObjectFile before we go around changing its Sections.
1119 module_sp->GetObjectFile()->GetSymtab();
1120 // eh_frame would present the same problems but we parse that on
1121 // a per-function basis as-needed so it's more difficult to
1122 // remove its use of the Sections. Realistically, the environments
1123 // where this code path will be taken will not have eh_frame sections.
1124
Jason Molenda20eb31b2013-08-16 03:20:42 +00001125 unified_section_sp->SetFileAddress(load_cmd.vmaddr);
1126 }
1127 }
Greg Clayton3046e662013-07-10 01:23:25 +00001128 m_sections_ap->AddSection(unified_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001130
Greg Clayton3046e662013-07-10 01:23:25 +00001131 struct section_64 sect64;
1132 ::memset (&sect64, 0, sizeof(sect64));
1133 // Push a section into our mach sections for the section at
Charles Davis510938e2013-08-27 05:04:57 +00001134 // index zero (NO_SECT) if we don't have any mach sections yet...
Greg Clayton3046e662013-07-10 01:23:25 +00001135 if (m_mach_sections.empty())
1136 m_mach_sections.push_back(sect64);
1137 uint32_t segment_sect_idx;
1138 const lldb::user_id_t first_segment_sectID = sectID + 1;
1139
1140
Charles Davis510938e2013-08-27 05:04:57 +00001141 const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8;
Greg Clayton3046e662013-07-10 01:23:25 +00001142 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001143 {
Greg Clayton3046e662013-07-10 01:23:25 +00001144 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
Greg Clayton89411422010-10-08 00:21:05 +00001145 break;
Greg Clayton3046e662013-07-10 01:23:25 +00001146 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1147 break;
1148 sect64.addr = m_data.GetAddress(&offset);
1149 sect64.size = m_data.GetAddress(&offset);
1150
1151 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1152 break;
1153
1154 // Keep a list of mach sections around in case we need to
1155 // get at data that isn't stored in the abstracted Sections.
1156 m_mach_sections.push_back (sect64);
1157
1158 if (add_section)
1159 {
1160 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1161 if (!const_segname)
1162 {
1163 // We have a segment with no name so we need to conjure up
1164 // segments that correspond to the section's segname if there
1165 // isn't already such a section. If there is such a section,
1166 // we resize the section so that it spans all sections.
1167 // We also mark these sections as fake so address matches don't
1168 // hit if they land in the gaps between the child sections.
1169 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1170 segment_sp = unified_section_list.FindSectionByName (const_segname);
1171 if (segment_sp.get())
1172 {
1173 Section *segment = segment_sp.get();
1174 // Grow the section size as needed.
1175 const lldb::addr_t sect64_min_addr = sect64.addr;
1176 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1177 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1178 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1179 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1180 if (sect64_min_addr >= curr_seg_min_addr)
1181 {
1182 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1183 // Only grow the section size if needed
1184 if (new_seg_byte_size > curr_seg_byte_size)
1185 segment->SetByteSize (new_seg_byte_size);
1186 }
1187 else
1188 {
1189 // We need to change the base address of the segment and
1190 // adjust the child section offsets for all existing children.
1191 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1192 segment->Slide(slide_amount, false);
1193 segment->GetChildren().Slide(-slide_amount, false);
1194 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1195 }
1196
1197 // Grow the section size as needed.
1198 if (sect64.offset)
1199 {
1200 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1201 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1202
1203 const lldb::addr_t section_min_file_offset = sect64.offset;
1204 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1205 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1206 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1207 segment->SetFileOffset (new_file_offset);
1208 segment->SetFileSize (new_file_size);
1209 }
1210 }
1211 else
1212 {
1213 // Create a fake section for the section's named segment
1214 segment_sp.reset(new Section (segment_sp, // Parent section
1215 module_sp, // Module to which this section belongs
1216 this, // Object file to which this section belongs
1217 ++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
1218 const_segname, // Name of this section
1219 eSectionTypeContainer, // This section is a container of other sections.
1220 sect64.addr, // File VM address == addresses as they are found in the object file
1221 sect64.size, // VM size in bytes of this section
1222 sect64.offset, // Offset to the data for this section in the file
1223 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1224 load_cmd.flags)); // Flags for this section
1225 segment_sp->SetIsFake(true);
1226
1227 m_sections_ap->AddSection(segment_sp);
1228 if (add_to_unified)
1229 unified_section_list.AddSection(segment_sp);
1230 segment_sp->SetIsEncrypted (segment_is_encrypted);
1231 }
1232 }
1233 assert (segment_sp.get());
1234
Charles Davis510938e2013-08-27 05:04:57 +00001235 uint32_t mach_sect_type = sect64.flags & SECTION_TYPE;
Greg Clayton3046e662013-07-10 01:23:25 +00001236 static ConstString g_sect_name_objc_data ("__objc_data");
1237 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1238 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1239 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1240 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1241 static ConstString g_sect_name_objc_const ("__objc_const");
1242 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1243 static ConstString g_sect_name_cfstring ("__cfstring");
1244
1245 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1246 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1247 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1248 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1249 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1250 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1251 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1252 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1253 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1254 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1255 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1256 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1257 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1258 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1259 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1260 static ConstString g_sect_name_eh_frame ("__eh_frame");
1261 static ConstString g_sect_name_DATA ("__DATA");
1262 static ConstString g_sect_name_TEXT ("__TEXT");
1263
Charles Davis510938e2013-08-27 05:04:57 +00001264 lldb::SectionType sect_type = eSectionTypeOther;
Greg Clayton3046e662013-07-10 01:23:25 +00001265
1266 if (section_name == g_sect_name_dwarf_debug_abbrev)
1267 sect_type = eSectionTypeDWARFDebugAbbrev;
1268 else if (section_name == g_sect_name_dwarf_debug_aranges)
1269 sect_type = eSectionTypeDWARFDebugAranges;
1270 else if (section_name == g_sect_name_dwarf_debug_frame)
1271 sect_type = eSectionTypeDWARFDebugFrame;
1272 else if (section_name == g_sect_name_dwarf_debug_info)
1273 sect_type = eSectionTypeDWARFDebugInfo;
1274 else if (section_name == g_sect_name_dwarf_debug_line)
1275 sect_type = eSectionTypeDWARFDebugLine;
1276 else if (section_name == g_sect_name_dwarf_debug_loc)
1277 sect_type = eSectionTypeDWARFDebugLoc;
1278 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1279 sect_type = eSectionTypeDWARFDebugMacInfo;
1280 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1281 sect_type = eSectionTypeDWARFDebugPubNames;
1282 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1283 sect_type = eSectionTypeDWARFDebugPubTypes;
1284 else if (section_name == g_sect_name_dwarf_debug_ranges)
1285 sect_type = eSectionTypeDWARFDebugRanges;
1286 else if (section_name == g_sect_name_dwarf_debug_str)
1287 sect_type = eSectionTypeDWARFDebugStr;
1288 else if (section_name == g_sect_name_dwarf_apple_names)
1289 sect_type = eSectionTypeDWARFAppleNames;
1290 else if (section_name == g_sect_name_dwarf_apple_types)
1291 sect_type = eSectionTypeDWARFAppleTypes;
1292 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1293 sect_type = eSectionTypeDWARFAppleNamespaces;
1294 else if (section_name == g_sect_name_dwarf_apple_objc)
1295 sect_type = eSectionTypeDWARFAppleObjC;
1296 else if (section_name == g_sect_name_objc_selrefs)
1297 sect_type = eSectionTypeDataCStringPointers;
1298 else if (section_name == g_sect_name_objc_msgrefs)
1299 sect_type = eSectionTypeDataObjCMessageRefs;
1300 else if (section_name == g_sect_name_eh_frame)
1301 sect_type = eSectionTypeEHFrame;
1302 else if (section_name == g_sect_name_cfstring)
1303 sect_type = eSectionTypeDataObjCCFStrings;
1304 else if (section_name == g_sect_name_objc_data ||
1305 section_name == g_sect_name_objc_classrefs ||
1306 section_name == g_sect_name_objc_superrefs ||
1307 section_name == g_sect_name_objc_const ||
1308 section_name == g_sect_name_objc_classlist)
1309 {
1310 sect_type = eSectionTypeDataPointers;
1311 }
1312
1313 if (sect_type == eSectionTypeOther)
1314 {
1315 switch (mach_sect_type)
1316 {
1317 // TODO: categorize sections by other flags for regular sections
Charles Davis510938e2013-08-27 05:04:57 +00001318 case S_REGULAR:
Greg Clayton3046e662013-07-10 01:23:25 +00001319 if (segment_sp->GetName() == g_sect_name_TEXT)
1320 sect_type = eSectionTypeCode;
1321 else if (segment_sp->GetName() == g_sect_name_DATA)
1322 sect_type = eSectionTypeData;
1323 else
1324 sect_type = eSectionTypeOther;
1325 break;
Charles Davis510938e2013-08-27 05:04:57 +00001326 case S_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1327 case S_CSTRING_LITERALS: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1328 case S_4BYTE_LITERALS: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1329 case S_8BYTE_LITERALS: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1330 case S_LITERAL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1331 case S_NON_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1332 case S_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1333 case S_SYMBOL_STUBS: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1334 case S_MOD_INIT_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1335 case S_MOD_TERM_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1336 case S_COALESCED: sect_type = eSectionTypeOther; break;
1337 case S_GB_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1338 case S_INTERPOSING: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1339 case S_16BYTE_LITERALS: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1340 case S_DTRACE_DOF: sect_type = eSectionTypeDebug; break;
1341 case S_LAZY_DYLIB_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break;
Greg Clayton3046e662013-07-10 01:23:25 +00001342 default: break;
1343 }
1344 }
1345
1346 SectionSP section_sp(new Section (segment_sp,
1347 module_sp,
1348 this,
1349 ++sectID,
1350 section_name,
1351 sect_type,
1352 sect64.addr - segment_sp->GetFileAddress(),
1353 sect64.size,
1354 sect64.offset,
1355 sect64.offset == 0 ? 0 : sect64.size,
1356 sect64.flags));
1357 // Set the section to be encrypted to match the segment
1358
1359 bool section_is_encrypted = false;
1360 if (!segment_is_encrypted && load_cmd.filesize != 0)
1361 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1362
1363 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1364 segment_sp->GetChildren().AddSection(section_sp);
1365
1366 if (segment_sp->IsFake())
1367 {
1368 segment_sp.reset();
1369 const_segname.Clear();
1370 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001371 }
1372 }
Greg Clayton3046e662013-07-10 01:23:25 +00001373 if (segment_sp && is_dsym)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374 {
Greg Clayton3046e662013-07-10 01:23:25 +00001375 if (first_segment_sectID <= sectID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001376 {
Greg Clayton3046e662013-07-10 01:23:25 +00001377 lldb::user_id_t sect_uid;
1378 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379 {
Greg Clayton3046e662013-07-10 01:23:25 +00001380 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1381 SectionSP next_section_sp;
1382 if (sect_uid + 1 <= sectID)
1383 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1384
1385 if (curr_section_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001386 {
Greg Clayton3046e662013-07-10 01:23:25 +00001387 if (curr_section_sp->GetByteSize() == 0)
1388 {
1389 if (next_section_sp.get() != NULL)
1390 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1391 else
1392 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1393 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001394 }
1395 }
1396 }
1397 }
1398 }
1399 }
1400 }
Charles Davis510938e2013-08-27 05:04:57 +00001401 else if (load_cmd.cmd == LC_DYSYMTAB)
Greg Clayton3046e662013-07-10 01:23:25 +00001402 {
1403 m_dysymtab.cmd = load_cmd.cmd;
1404 m_dysymtab.cmdsize = load_cmd.cmdsize;
1405 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1406 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407
Greg Clayton3046e662013-07-10 01:23:25 +00001408 offset = load_cmd_offset + load_cmd.cmdsize;
1409 }
1410
1411// StreamFile s(stdout, false); // REMOVE THIS LINE
1412// s.Printf ("Sections for %s:\n", m_file.GetPath().c_str());// REMOVE THIS LINE
1413// m_sections_ap->Dump(&s, NULL, true, UINT32_MAX);// REMOVE THIS LINE
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001415}
1416
1417class MachSymtabSectionInfo
1418{
1419public:
1420
1421 MachSymtabSectionInfo (SectionList *section_list) :
1422 m_section_list (section_list),
1423 m_section_infos()
1424 {
1425 // Get the number of sections down to a depth of 1 to include
1426 // all segments and their sections, but no other sections that
1427 // may be added for debug map or
1428 m_section_infos.resize(section_list->GetNumSections(1));
1429 }
1430
1431
Greg Claytone72dfb32012-02-24 01:59:29 +00001432 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433 GetSection (uint8_t n_sect, addr_t file_addr)
1434 {
1435 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00001436 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437 if (n_sect < m_section_infos.size())
1438 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001439 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001440 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001441 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1442 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001443 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00001444 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001445 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1446 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00001447 }
1448 else
1449 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001450 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00001451 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001452 }
1453 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00001454 {
1455 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00001456 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001457 }
1458 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1459 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1460 {
1461 // Symbol is in section with zero size, but has the same start
1462 // address as the section. This can happen with linker symbols
1463 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00001464 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001465 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001466 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001467 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001468 }
1469
1470protected:
1471 struct SectionInfo
1472 {
1473 SectionInfo () :
1474 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001475 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476 {
1477 }
1478
1479 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00001480 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001481 };
1482 SectionList *m_section_list;
1483 std::vector<SectionInfo> m_section_infos;
1484};
1485
Greg Clayton9191db42013-10-21 18:40:51 +00001486struct TrieEntry
1487{
1488 TrieEntry () :
1489 name(),
1490 address(LLDB_INVALID_ADDRESS),
1491 flags (0),
1492 other(0),
1493 import_name()
1494 {
1495 }
1496
1497 void
1498 Clear ()
1499 {
1500 name.Clear();
1501 address = LLDB_INVALID_ADDRESS;
1502 flags = 0;
1503 other = 0;
1504 import_name.Clear();
1505 }
1506
1507 void
1508 Dump () const
1509 {
1510 printf ("0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"", address, flags, other, name.GetCString());
1511 if (import_name)
1512 printf (" -> \"%s\"\n", import_name.GetCString());
1513 else
1514 printf ("\n");
1515 }
1516 ConstString name;
1517 uint64_t address;
1518 uint64_t flags;
1519 uint64_t other;
1520 ConstString import_name;
1521};
1522
1523struct TrieEntryWithOffset
1524{
1525 lldb::offset_t nodeOffset;
1526 TrieEntry entry;
1527
1528 TrieEntryWithOffset (lldb::offset_t offset) :
1529 nodeOffset (offset),
1530 entry()
1531 {
1532 }
1533
1534 void
1535 Dump (uint32_t idx) const
1536 {
1537 printf ("[%3u] 0x%16.16llx: ", idx, nodeOffset);
1538 entry.Dump();
1539 }
1540
1541 bool
1542 operator<(const TrieEntryWithOffset& other) const
1543 {
1544 return ( nodeOffset < other.nodeOffset );
1545 }
1546};
1547
1548static void
1549ParseTrieEntries (DataExtractor &data,
1550 lldb::offset_t offset,
1551 std::vector<llvm::StringRef> &nameSlices,
1552 std::set<lldb::addr_t> &resolver_addresses,
1553 std::vector<TrieEntryWithOffset>& output)
1554{
1555 if (!data.ValidOffset(offset))
1556 return;
1557
1558 const uint64_t terminalSize = data.GetULEB128(&offset);
1559 lldb::offset_t children_offset = offset + terminalSize;
1560 if ( terminalSize != 0 ) {
1561 TrieEntryWithOffset e (offset);
1562 e.entry.flags = data.GetULEB128(&offset);
1563 const char *import_name = NULL;
1564 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
1565 e.entry.address = 0;
1566 e.entry.other = data.GetULEB128(&offset); // dylib ordinal
1567 import_name = data.GetCStr(&offset);
1568 }
1569 else {
1570 e.entry.address = data.GetULEB128(&offset);
1571 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
1572 {
Jim Inghamea3ac272014-01-10 22:55:37 +00001573 //resolver_addresses.insert(e.entry.address);
Greg Clayton9191db42013-10-21 18:40:51 +00001574 e.entry.other = data.GetULEB128(&offset);
Jim Inghamea3ac272014-01-10 22:55:37 +00001575 resolver_addresses.insert(e.entry.other);
Greg Clayton9191db42013-10-21 18:40:51 +00001576 }
1577 else
1578 e.entry.other = 0;
1579 }
1580 // Only add symbols that are reexport symbols with a valid import name
1581 if (EXPORT_SYMBOL_FLAGS_REEXPORT & e.entry.flags && import_name && import_name[0])
1582 {
1583 std::string name;
1584 if (!nameSlices.empty())
1585 {
1586 for (auto name_slice: nameSlices)
1587 name.append(name_slice.data(), name_slice.size());
1588 }
1589 if (name.size() > 1)
1590 {
1591 // Skip the leading '_'
1592 e.entry.name.SetCStringWithLength(name.c_str() + 1,name.size() - 1);
1593 }
1594 if (import_name)
1595 {
1596 // Skip the leading '_'
1597 e.entry.import_name.SetCString(import_name+1);
1598 }
1599 output.push_back(e);
1600 }
1601 }
1602
1603 const uint8_t childrenCount = data.GetU8(&children_offset);
1604 for (uint8_t i=0; i < childrenCount; ++i) {
1605 nameSlices.push_back(data.GetCStr(&children_offset));
1606 lldb::offset_t childNodeOffset = data.GetULEB128(&children_offset);
1607 if (childNodeOffset)
1608 {
1609 ParseTrieEntries(data,
1610 childNodeOffset,
1611 nameSlices,
1612 resolver_addresses,
1613 output);
1614 }
1615 nameSlices.pop_back();
1616 }
1617}
1618
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001619size_t
Greg Clayton3046e662013-07-10 01:23:25 +00001620ObjectFileMachO::ParseSymtab ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001621{
1622 Timer scoped_timer(__PRETTY_FUNCTION__,
1623 "ObjectFileMachO::ParseSymtab () module = %s",
1624 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001625 ModuleSP module_sp (GetModule());
1626 if (!module_sp)
1627 return 0;
1628
1629 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1630 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
Greg Clayton9191db42013-10-21 18:40:51 +00001631 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 +00001632 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1633 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00001634 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001635 uint32_t i;
Greg Clayton9191db42013-10-21 18:40:51 +00001636 FileSpecList dylib_files;
Greg Clayton5160ce52013-03-27 23:08:40 +00001637 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton77ccca72011-12-30 00:32:24 +00001638
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639 for (i=0; i<m_header.ncmds; ++i)
1640 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001641 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001642 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001643 struct load_command lc;
1644 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001645 break;
1646 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001647 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001648 {
Charles Davis510938e2013-08-27 05:04:57 +00001649 case LC_SYMTAB:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001650 symtab_load_command.cmd = lc.cmd;
1651 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001652 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001653 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1654 return 0;
1655 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001656 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001657 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001658 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001659 return 0;
1660 }
1661
1662 if (symtab_load_command.stroff == 0)
1663 {
1664 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001665 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001666 return 0;
1667 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001668
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001669 if (symtab_load_command.nsyms == 0)
1670 {
1671 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001672 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001673 return 0;
1674 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001675
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001676 if (symtab_load_command.strsize == 0)
1677 {
1678 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001679 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001680 return 0;
1681 }
1682 break;
1683
Greg Clayton9191db42013-10-21 18:40:51 +00001684 case LC_DYLD_INFO:
1685 case LC_DYLD_INFO_ONLY:
1686 if (m_data.GetU32(&offset, &dyld_info.rebase_off, 10))
1687 {
1688 dyld_info.cmd = lc.cmd;
1689 dyld_info.cmdsize = lc.cmdsize;
1690 }
1691 else
1692 {
1693 memset (&dyld_info, 0, sizeof(dyld_info));
1694 }
1695 break;
1696
1697 case LC_LOAD_DYLIB:
1698 case LC_LOAD_WEAK_DYLIB:
1699 case LC_REEXPORT_DYLIB:
1700 case LC_LOADFVMLIB:
1701 case LC_LOAD_UPWARD_DYLIB:
1702 {
1703 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1704 const char *path = m_data.PeekCStr(name_offset);
1705 if (path)
1706 {
1707 FileSpec file_spec(path, false);
1708 // Strip the path if there is @rpath, @executanble, etc so we just use the basename
1709 if (path[0] == '@')
1710 file_spec.GetDirectory().Clear();
1711
1712 dylib_files.Append(file_spec);
1713 }
1714 }
1715 break;
1716
Charles Davis510938e2013-08-27 05:04:57 +00001717 case LC_FUNCTION_STARTS:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001718 function_starts_load_command.cmd = lc.cmd;
1719 function_starts_load_command.cmdsize = lc.cmdsize;
1720 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 +00001721 memset (&function_starts_load_command, 0, sizeof(function_starts_load_command));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001722 break;
1723
1724 default:
1725 break;
1726 }
1727 offset = cmd_offset + lc.cmdsize;
1728 }
1729
1730 if (symtab_load_command.cmd)
1731 {
1732 Symtab *symtab = m_symtab_ap.get();
1733 SectionList *section_list = GetSectionList();
1734 if (section_list == NULL)
1735 return 0;
1736
Greg Claytonc7bece562013-01-25 18:06:21 +00001737 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1738 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001739 bool bit_width_32 = addr_byte_size == 4;
1740 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1741
Greg Claytonc7bece562013-01-25 18:06:21 +00001742 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1743 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1744 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00001745 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00001746 DataExtractor dyld_trie_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00001747
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001748 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1749 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00001750 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
Greg Claytonfd814c52013-08-13 01:42:25 +00001751
1752 ProcessSP process_sp (m_process_wp.lock());
1753 Process *process = process_sp.get();
1754
Greg Clayton86eac942013-08-13 21:32:34 +00001755 uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete;
1756
Greg Clayton4c82d422012-05-18 23:20:01 +00001757 if (process)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001758 {
Greg Clayton4c82d422012-05-18 23:20:01 +00001759 Target &target = process->GetTarget();
Greg Claytonfd814c52013-08-13 01:42:25 +00001760
Greg Clayton86eac942013-08-13 21:32:34 +00001761 memory_module_load_level = target.GetMemoryModuleLoadLevel();
Greg Claytonfd814c52013-08-13 01:42:25 +00001762
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001763 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1764 // Reading mach file from memory in a process or core file...
1765
1766 if (linkedit_section_sp)
1767 {
1768 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1769 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1770 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00001771 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00001772
1773 bool data_was_read = false;
1774
1775#if defined (__APPLE__) && defined (__arm__)
1776 if (m_header.flags & 0x80000000u)
Greg Clayton77ccca72011-12-30 00:32:24 +00001777 {
Greg Clayton26b47e22012-04-18 05:19:20 +00001778 // This mach-o memory file is in the dyld shared cache. If this
1779 // program is not remote and this is iOS, then this process will
1780 // share the same shared cache as the process we are debugging and
1781 // we can read the entire __LINKEDIT from the address space in this
1782 // process. This is a needed optimization that is used for local iOS
1783 // debugging only since all shared libraries in the shared cache do
1784 // not have corresponding files that exist in the file system of the
1785 // device. They have been combined into a single file. This means we
1786 // always have to load these files from memory. All of the symbol and
1787 // string tables from all of the __LINKEDIT sections from the shared
1788 // libraries in the shared cache have been merged into a single large
1789 // symbol and string table. Reading all of this symbol and string table
1790 // data across can slow down debug launch times, so we optimize this by
1791 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00001792
1793 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1794 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1795 bool use_lldb_cache = true;
1796 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1797 {
1798 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00001799 ModuleSP module_sp (GetModule());
1800 if (module_sp)
1801 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1802
Jason Molenda0e0954c2013-04-16 06:24:42 +00001803 }
1804
Greg Clayton26b47e22012-04-18 05:19:20 +00001805 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00001806 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00001807 {
1808 data_was_read = true;
1809 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00001810 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00001811 if (function_starts_load_command.cmd)
1812 {
1813 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1814 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1815 }
1816 }
1817 }
1818#endif
1819
1820 if (!data_was_read)
1821 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001822 if (memory_module_load_level == eMemoryModuleLoadLevelComplete)
Jason Molendad34e6522013-02-05 22:31:24 +00001823 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001824 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1825 if (nlist_data_sp)
1826 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
1827 // Load strings individually from memory when loading from memory since shared cache
1828 // string tables contain strings for all symbols from all shared cached libraries
1829 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1830 //if (strtab_data_sp)
1831 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
1832 if (m_dysymtab.nindirectsyms != 0)
1833 {
1834 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1835 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1836 if (indirect_syms_data_sp)
1837 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1838 }
Jason Molendad34e6522013-02-05 22:31:24 +00001839 }
Greg Claytonfd814c52013-08-13 01:42:25 +00001840
1841 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial)
Greg Clayton26b47e22012-04-18 05:19:20 +00001842 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001843 if (function_starts_load_command.cmd)
1844 {
1845 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1846 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1847 if (func_start_data_sp)
1848 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1849 }
Greg Clayton26b47e22012-04-18 05:19:20 +00001850 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001851 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001852 }
1853 }
1854 else
1855 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001856 nlist_data.SetData (m_data,
1857 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001858 nlist_data_byte_size);
1859 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00001860 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001861 strtab_data_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00001862
1863 if (dyld_info.export_size > 0)
1864 {
1865 dyld_trie_data.SetData (m_data,
1866 dyld_info.export_off,
1867 dyld_info.export_size);
1868 }
1869
Jason Molendad34e6522013-02-05 22:31:24 +00001870 if (m_dysymtab.nindirectsyms != 0)
1871 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001872 indirect_symbol_index_data.SetData (m_data,
1873 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00001874 m_dysymtab.nindirectsyms * 4);
1875 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001876 if (function_starts_load_command.cmd)
1877 {
1878 function_starts_data.SetData (m_data,
1879 function_starts_load_command.dataoff,
1880 function_starts_load_command.datasize);
1881 }
1882 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001883
Greg Clayton86eac942013-08-13 21:32:34 +00001884 if (nlist_data.GetByteSize() == 0 && memory_module_load_level == eMemoryModuleLoadLevelComplete)
1885 {
1886 if (log)
1887 module_sp->LogMessage(log, "failed to read nlist data");
1888 return 0;
1889 }
1890
1891
Greg Claytondebb8812012-05-25 17:04:00 +00001892 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1893 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00001894 {
Greg Claytondebb8812012-05-25 17:04:00 +00001895 if (process)
1896 {
1897 if (strtab_addr == LLDB_INVALID_ADDRESS)
1898 {
1899 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001900 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00001901 return 0;
1902 }
1903 }
1904 else
Greg Clayton4c82d422012-05-18 23:20:01 +00001905 {
1906 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001907 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00001908 return 0;
1909 }
1910 }
Greg Clayton4c82d422012-05-18 23:20:01 +00001911
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001912 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1913 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1914 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1915 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1916 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1917 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1918 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1919 SectionSP eh_frame_section_sp;
1920 if (text_section_sp.get())
1921 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1922 else
1923 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1924
Charles Davis510938e2013-08-27 05:04:57 +00001925 const bool is_arm = (m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
Jason Molenda5635f772013-03-21 03:36:01 +00001926
1927 // lldb works best if it knows the start addresss of all functions in a module.
1928 // Linker symbols or debug info are normally the best source of information for start addr / size but
1929 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00001930 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00001931 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1932 // binary, relative to the text section.
1933 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1934 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1935 // Binaries built to run on older releases may need to use eh_frame information.
1936
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001937 if (text_section_sp && function_starts_data.GetByteSize())
1938 {
1939 FunctionStarts::Entry function_start_entry;
1940 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00001941 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001942 function_start_entry.addr = text_section_sp->GetFileAddress();
1943 uint64_t delta;
1944 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1945 {
1946 // Now append the current entry
1947 function_start_entry.addr += delta;
1948 function_starts.Append(function_start_entry);
1949 }
Jason Molendad63d3c72013-04-16 00:18:44 +00001950 }
Jason Molenda5635f772013-03-21 03:36:01 +00001951 else
1952 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00001953 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1954 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1955 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1956 // the module.
1957 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00001958 {
1959 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1960 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1961 eh_frame.GetFunctionAddressAndSizeVector (functions);
1962 addr_t text_base_addr = text_section_sp->GetFileAddress();
1963 size_t count = functions.GetSize();
1964 for (size_t i = 0; i < count; ++i)
1965 {
1966 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1967 if (func)
1968 {
1969 FunctionStarts::Entry function_start_entry;
1970 function_start_entry.addr = func->base - text_base_addr;
1971 function_starts.Append(function_start_entry);
1972 }
1973 }
1974 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001975 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001976
Greg Claytonc7bece562013-01-25 18:06:21 +00001977 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001978
Charles Davis510938e2013-08-27 05:04:57 +00001979 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NO_SECT;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001980
Greg Claytonc7bece562013-01-25 18:06:21 +00001981 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001982
1983 uint32_t N_SO_index = UINT32_MAX;
1984
1985 MachSymtabSectionInfo section_info (section_list);
1986 std::vector<uint32_t> N_FUN_indexes;
1987 std::vector<uint32_t> N_NSYM_indexes;
1988 std::vector<uint32_t> N_INCL_indexes;
1989 std::vector<uint32_t> N_BRAC_indexes;
1990 std::vector<uint32_t> N_COMM_indexes;
Greg Claytond81088c2014-01-16 01:38:29 +00001991 typedef std::multimap <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001992 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00001993 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001994 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1995 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00001996 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001997 // Any symbols that get merged into another will get an entry
1998 // in this map so we know
1999 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2000 uint32_t nlist_idx = 0;
2001 Symbol *symbol_ptr = NULL;
2002
2003 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00002004 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00002005 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00002006 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00002007 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002008
Jim Inghamea3ac272014-01-10 22:55:37 +00002009 std::vector<TrieEntryWithOffset> trie_entries;
2010 std::set<lldb::addr_t> resolver_addresses;
2011
2012 if (dyld_trie_data.GetByteSize() > 0)
2013 {
2014 std::vector<llvm::StringRef> nameSlices;
2015 ParseTrieEntries (dyld_trie_data,
2016 0,
2017 nameSlices,
2018 resolver_addresses,
2019 trie_entries);
2020
2021 ConstString text_segment_name ("__TEXT");
2022 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
2023 if (text_segment_sp)
2024 {
2025 const lldb::addr_t text_segment_file_addr = text_segment_sp->GetFileAddress();
2026 if (text_segment_file_addr != LLDB_INVALID_ADDRESS)
2027 {
2028 for (auto &e : trie_entries)
2029 e.entry.address += text_segment_file_addr;
2030 }
2031 }
2032 }
2033
Jason Molendaa5609c82012-06-21 01:51:02 +00002034#if defined (__APPLE__) && defined (__arm__)
2035
2036 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
2037 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
2038 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
2039 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
2040 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
2041 // nlist parser to ignore all LOCAL symbols.
2042
2043 if (m_header.flags & 0x80000000u)
2044 {
2045 // Before we can start mapping the DSC, we need to make certain the target process is actually
2046 // using the cache we can find.
2047
Jason Molendaa5609c82012-06-21 01:51:02 +00002048 // Next we need to determine the correct path for the dyld shared cache.
2049
2050 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
2051 char dsc_path[PATH_MAX];
2052
2053 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00002054 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
2055 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00002056 header_arch.GetArchitectureName());
2057
2058 FileSpec dsc_filespec(dsc_path, false);
2059
2060 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molendad63d3c72013-04-16 00:18:44 +00002061 struct lldb_copy_dyld_cache_header_v0
Greg Clayton946f8902012-09-05 22:30:51 +00002062 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002063 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
2064 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
2065 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda4e7511e2013-03-06 23:19:17 +00002066 uint32_t imagesOffset;
2067 uint32_t imagesCount;
2068 uint64_t dyldBaseAddress;
2069 uint64_t codeSignatureOffset;
2070 uint64_t codeSignatureSize;
2071 uint64_t slideInfoOffset;
2072 uint64_t slideInfoSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00002073 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
2074 uint64_t localSymbolsSize; // size of local symbols information
2075 };
2076 struct lldb_copy_dyld_cache_header_v1
2077 {
2078 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
2079 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
2080 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
2081 uint32_t imagesOffset;
2082 uint32_t imagesCount;
2083 uint64_t dyldBaseAddress;
2084 uint64_t codeSignatureOffset;
2085 uint64_t codeSignatureSize;
2086 uint64_t slideInfoOffset;
2087 uint64_t slideInfoSize;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002088 uint64_t localSymbolsOffset;
2089 uint64_t localSymbolsSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00002090 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Clayton946f8902012-09-05 22:30:51 +00002091 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00002092
Jason Molendad63d3c72013-04-16 00:18:44 +00002093 struct lldb_copy_dyld_cache_mapping_info
2094 {
2095 uint64_t address;
2096 uint64_t size;
2097 uint64_t fileOffset;
2098 uint32_t maxProt;
2099 uint32_t initProt;
2100 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00002101
Greg Clayton946f8902012-09-05 22:30:51 +00002102 struct lldb_copy_dyld_cache_local_symbols_info
2103 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002104 uint32_t nlistOffset;
2105 uint32_t nlistCount;
2106 uint32_t stringsOffset;
2107 uint32_t stringsSize;
2108 uint32_t entriesOffset;
2109 uint32_t entriesCount;
Greg Clayton946f8902012-09-05 22:30:51 +00002110 };
2111 struct lldb_copy_dyld_cache_local_symbols_entry
2112 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002113 uint32_t dylibOffset;
2114 uint32_t nlistStartIndex;
2115 uint32_t nlistCount;
Greg Clayton946f8902012-09-05 22:30:51 +00002116 };
Jason Molendaa5609c82012-06-21 01:51:02 +00002117
Jason Molendaf8130862012-06-22 03:28:35 +00002118 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
2119 The dyld_cache_local_symbols_info structure gives us three things:
2120 1. The start and count of the nlist records in the dyld_shared_cache file
2121 2. The start and size of the strings for these nlist records
2122 3. The start and count of dyld_cache_local_symbols_entry entries
2123
2124 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
2125 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002126 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00002127 and the count of how many nlist records there are for this dylib/framework.
2128 */
2129
Jason Molendaa5609c82012-06-21 01:51:02 +00002130 // Process the dsc header to find the unmapped symbols
2131 //
2132 // Save some VM space, do not map the entire cache in one shot.
2133
Jason Molenda255f9bb2013-03-06 23:17:36 +00002134 DataBufferSP dsc_data_sp;
2135 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
2136
2137 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00002138 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002139 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002140
Jason Molenda255f9bb2013-03-06 23:17:36 +00002141 char version_str[17];
2142 int version = -1;
2143 lldb::offset_t offset = 0;
2144 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
2145 version_str[16] = '\0';
2146 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
2147 {
2148 int v;
2149 if (::sscanf (version_str + 6, "%d", &v) == 1)
2150 {
2151 version = v;
2152 }
2153 }
2154
Jason Molenda0e0954c2013-04-16 06:24:42 +00002155 UUID dsc_uuid;
2156 if (version >= 1)
2157 {
2158 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
2159 uint8_t uuid_bytes[sizeof (uuid_t)];
2160 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
2161 dsc_uuid.SetBytes (uuid_bytes);
2162 }
2163
2164 bool uuid_match = true;
2165 if (dsc_uuid.IsValid() && process)
2166 {
2167 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
2168
2169 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
2170 {
2171 // The on-disk dyld_shared_cache file is not the same as the one in this
2172 // process' memory, don't use it.
2173 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002174 ModuleSP module_sp (GetModule());
2175 if (module_sp)
2176 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 +00002177 }
2178 }
2179
Jason Molenda4e7511e2013-03-06 23:19:17 +00002180 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00002181
Jason Molendaa5609c82012-06-21 01:51:02 +00002182 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
2183
2184 // If the mappingOffset points to a location inside the header, we've
2185 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda0e0954c2013-04-16 06:24:42 +00002186 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendaa5609c82012-06-21 01:51:02 +00002187 {
2188
Jason Molenda255f9bb2013-03-06 23:17:36 +00002189 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
2190 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
2191 offset = 0;
2192
2193 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
2194 // in the shared library cache need to be adjusted by an offset to match up with the
2195 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
2196 // recorded in mapping_offset_value.
2197 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
2198
2199 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00002200 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
2201 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
2202
Jason Molenda4e7511e2013-03-06 23:19:17 +00002203 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00002204 {
2205 // Map the local symbols
Jason Molenda4e7511e2013-03-06 23:19:17 +00002206 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00002207 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002208 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002209
2210 offset = 0;
2211
2212 // Read the local_symbols_infos struct in one shot
2213 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
2214 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
2215
Jason Molendaa5609c82012-06-21 01:51:02 +00002216 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
2217
Jason Molenda255f9bb2013-03-06 23:17:36 +00002218 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002219
2220 offset = local_symbols_info.entriesOffset;
2221 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
2222 {
2223 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
2224 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
2225 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
2226 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
2227
Jason Molenda4e7511e2013-03-06 23:19:17 +00002228 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00002229 {
2230 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
2231
2232 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
2233 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
2234 num_syms = symtab->GetNumSymbols();
2235
2236 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
2237 uint32_t string_table_offset = local_symbols_info.stringsOffset;
2238
Jason Molenda4e7511e2013-03-06 23:19:17 +00002239 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00002240 {
2241 /////////////////////////////
2242 {
2243 struct nlist_64 nlist;
2244 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2245 break;
2246
2247 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2248 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2249 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2250 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2251 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2252
2253 SymbolType type = eSymbolTypeInvalid;
2254 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2255
2256 if (symbol_name == NULL)
2257 {
2258 // No symbol should be NULL, even the symbols with no
2259 // string values should have an offset zero which points
2260 // to an empty C-string
2261 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002262 "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 +00002263 entry_index,
2264 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002265 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00002266 continue;
2267 }
2268 if (symbol_name[0] == '\0')
2269 symbol_name = NULL;
2270
2271 const char *symbol_name_non_abi_mangled = NULL;
2272
2273 SectionSP symbol_section;
2274 uint32_t symbol_byte_size = 0;
2275 bool add_nlist = true;
Charles Davis510938e2013-08-27 05:04:57 +00002276 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002277 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002278 bool is_gsym = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002279
2280 assert (sym_idx < num_syms);
2281
2282 sym[sym_idx].SetDebug (is_debug);
2283
2284 if (is_debug)
2285 {
2286 switch (nlist.n_type)
2287 {
Charles Davis510938e2013-08-27 05:04:57 +00002288 case N_GSYM:
2289 // global symbol: name,,NO_SECT,type,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002290 // Sometimes the N_GSYM value contains the address.
2291
2292 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2293 // have the same address, but we want to ensure that we always find only the real symbol,
2294 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2295 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2296 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2297 // same address.
2298
2299 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2300 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2301 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2302 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2303 add_nlist = false;
2304 else
2305 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002306 is_gsym = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002307 sym[sym_idx].SetExternal(true);
2308 if (nlist.n_value != 0)
2309 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2310 type = eSymbolTypeData;
2311 }
2312 break;
2313
Charles Davis510938e2013-08-27 05:04:57 +00002314 case N_FNAME:
2315 // procedure name (f77 kludge): name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002316 type = eSymbolTypeCompiler;
2317 break;
2318
Charles Davis510938e2013-08-27 05:04:57 +00002319 case N_FUN:
2320 // procedure: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002321 if (symbol_name)
2322 {
2323 type = eSymbolTypeCode;
2324 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2325
Greg Claytond81088c2014-01-16 01:38:29 +00002326 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002327 // We use the current number of symbols in the symbol table in lieu of
2328 // using nlist_idx in case we ever start trimming entries out
2329 N_FUN_indexes.push_back(sym_idx);
2330 }
2331 else
2332 {
2333 type = eSymbolTypeCompiler;
2334
2335 if ( !N_FUN_indexes.empty() )
2336 {
2337 // Copy the size of the function into the original STAB entry so we don't have
2338 // to hunt for it later
2339 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2340 N_FUN_indexes.pop_back();
2341 // We don't really need the end function STAB as it contains the size which
2342 // we already placed with the original symbol, so don't add it if we want a
2343 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002344 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002345 }
2346 }
2347 break;
2348
Charles Davis510938e2013-08-27 05:04:57 +00002349 case N_STSYM:
2350 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00002351 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002352 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2353 type = eSymbolTypeData;
2354 break;
2355
Charles Davis510938e2013-08-27 05:04:57 +00002356 case N_LCSYM:
2357 // .lcomm symbol: name,,n_sect,type,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002358 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2359 type = eSymbolTypeCommonBlock;
2360 break;
2361
Charles Davis510938e2013-08-27 05:04:57 +00002362 case N_BNSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002363 // We use the current number of symbols in the symbol table in lieu of
2364 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00002365 // Skip these if we want minimal symbol tables
2366 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002367 break;
2368
Charles Davis510938e2013-08-27 05:04:57 +00002369 case N_ENSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002370 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2371 // so that we can always skip the entire symbol if we need to navigate
2372 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00002373 // Skip these if we want minimal symbol tables
2374 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002375 break;
2376
2377
Charles Davis510938e2013-08-27 05:04:57 +00002378 case N_OPT:
2379 // emitted with gcc2_compiled and in gcc source
Jason Molendaa5609c82012-06-21 01:51:02 +00002380 type = eSymbolTypeCompiler;
2381 break;
2382
Charles Davis510938e2013-08-27 05:04:57 +00002383 case N_RSYM:
2384 // register sym: name,,NO_SECT,type,register
Jason Molendaa5609c82012-06-21 01:51:02 +00002385 type = eSymbolTypeVariable;
2386 break;
2387
Charles Davis510938e2013-08-27 05:04:57 +00002388 case N_SLINE:
2389 // src line: 0,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002390 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2391 type = eSymbolTypeLineEntry;
2392 break;
2393
Charles Davis510938e2013-08-27 05:04:57 +00002394 case N_SSYM:
2395 // structure elt: name,,NO_SECT,type,struct_offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002396 type = eSymbolTypeVariableType;
2397 break;
2398
Charles Davis510938e2013-08-27 05:04:57 +00002399 case N_SO:
2400 // source file name
Jason Molendaa5609c82012-06-21 01:51:02 +00002401 type = eSymbolTypeSourceFile;
2402 if (symbol_name == NULL)
2403 {
Greg Clayton3046e662013-07-10 01:23:25 +00002404 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002405 if (N_SO_index != UINT32_MAX)
2406 {
2407 // Set the size of the N_SO to the terminating index of this N_SO
2408 // so that we can always skip the entire N_SO if we need to navigate
2409 // more quickly at the source level when parsing STABS
2410 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00002411 symbol_ptr->SetByteSize(sym_idx);
Jason Molendaa5609c82012-06-21 01:51:02 +00002412 symbol_ptr->SetSizeIsSibling(true);
2413 }
2414 N_NSYM_indexes.clear();
2415 N_INCL_indexes.clear();
2416 N_BRAC_indexes.clear();
2417 N_COMM_indexes.clear();
2418 N_FUN_indexes.clear();
2419 N_SO_index = UINT32_MAX;
2420 }
2421 else
2422 {
2423 // We use the current number of symbols in the symbol table in lieu of
2424 // using nlist_idx in case we ever start trimming entries out
2425 const bool N_SO_has_full_path = symbol_name[0] == '/';
2426 if (N_SO_has_full_path)
2427 {
Greg Clayton3046e662013-07-10 01:23:25 +00002428 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002429 {
2430 // We have two consecutive N_SO entries where the first contains a directory
2431 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00002432 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002433 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2434 add_nlist = false;
2435 }
2436 else
2437 {
2438 // This is the first entry in a N_SO that contains a directory or
2439 // a full path to the source file
2440 N_SO_index = sym_idx;
2441 }
2442 }
Greg Clayton3046e662013-07-10 01:23:25 +00002443 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002444 {
2445 // This is usually the second N_SO entry that contains just the filename,
2446 // so here we combine it with the first one if we are minimizing the symbol table
2447 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2448 if (so_path && so_path[0])
2449 {
2450 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00002451 const size_t double_slash_pos = full_so_path.find("//");
2452 if (double_slash_pos != std::string::npos)
2453 {
2454 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00002455 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Clayton0662d962012-09-07 20:29:13 +00002456 // and the second is the directory for the source file so you end up with
2457 // a path that looks like "/tmp/src//tmp/src/"
2458 FileSpec so_dir(so_path, false);
2459 if (!so_dir.Exists())
2460 {
2461 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2462 if (so_dir.Exists())
2463 {
2464 // Trim off the incorrect path
2465 full_so_path.erase(0, double_slash_pos + 1);
2466 }
2467 }
2468 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002469 if (*full_so_path.rbegin() != '/')
2470 full_so_path += '/';
2471 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00002472 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002473 add_nlist = false;
2474 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2475 }
2476 }
Greg Clayton946f8902012-09-05 22:30:51 +00002477 else
2478 {
2479 // This could be a relative path to a N_SO
2480 N_SO_index = sym_idx;
2481 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002482 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002483 break;
2484
Charles Davis510938e2013-08-27 05:04:57 +00002485 case N_OSO:
2486 // object file name: name,,0,0,st_mtime
Jason Molendaa5609c82012-06-21 01:51:02 +00002487 type = eSymbolTypeObjectFile;
2488 break;
2489
Charles Davis510938e2013-08-27 05:04:57 +00002490 case N_LSYM:
2491 // local sym: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002492 type = eSymbolTypeLocal;
2493 break;
2494
2495 //----------------------------------------------------------------------
2496 // INCL scopes
2497 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00002498 case N_BINCL:
2499 // include file beginning: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00002500 // We use the current number of symbols in the symbol table in lieu of
2501 // using nlist_idx in case we ever start trimming entries out
2502 N_INCL_indexes.push_back(sym_idx);
2503 type = eSymbolTypeScopeBegin;
2504 break;
2505
Charles Davis510938e2013-08-27 05:04:57 +00002506 case N_EINCL:
2507 // include file end: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002508 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2509 // so that we can always skip the entire symbol if we need to navigate
2510 // more quickly at the source level when parsing STABS
2511 if ( !N_INCL_indexes.empty() )
2512 {
2513 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2514 symbol_ptr->SetByteSize(sym_idx + 1);
2515 symbol_ptr->SetSizeIsSibling(true);
2516 N_INCL_indexes.pop_back();
2517 }
2518 type = eSymbolTypeScopeEnd;
2519 break;
2520
Charles Davis510938e2013-08-27 05:04:57 +00002521 case N_SOL:
2522 // #included file name: name,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002523 type = eSymbolTypeHeaderFile;
2524
2525 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00002526 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002527 break;
2528
Charles Davis510938e2013-08-27 05:04:57 +00002529 case N_PARAMS:
2530 // compiler parameters: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002531 type = eSymbolTypeCompiler;
2532 break;
2533
Charles Davis510938e2013-08-27 05:04:57 +00002534 case N_VERSION:
2535 // compiler version: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002536 type = eSymbolTypeCompiler;
2537 break;
2538
Charles Davis510938e2013-08-27 05:04:57 +00002539 case N_OLEVEL:
2540 // compiler -O level: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002541 type = eSymbolTypeCompiler;
2542 break;
2543
Charles Davis510938e2013-08-27 05:04:57 +00002544 case N_PSYM:
2545 // parameter: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002546 type = eSymbolTypeVariable;
2547 break;
2548
Charles Davis510938e2013-08-27 05:04:57 +00002549 case N_ENTRY:
2550 // alternate entry: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002551 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2552 type = eSymbolTypeLineEntry;
2553 break;
2554
2555 //----------------------------------------------------------------------
2556 // Left and Right Braces
2557 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00002558 case N_LBRAC:
2559 // left bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002560 // We use the current number of symbols in the symbol table in lieu of
2561 // using nlist_idx in case we ever start trimming entries out
2562 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2563 N_BRAC_indexes.push_back(sym_idx);
2564 type = eSymbolTypeScopeBegin;
2565 break;
2566
Charles Davis510938e2013-08-27 05:04:57 +00002567 case N_RBRAC:
2568 // right bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002569 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2570 // so that we can always skip the entire symbol if we need to navigate
2571 // more quickly at the source level when parsing STABS
2572 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2573 if ( !N_BRAC_indexes.empty() )
2574 {
2575 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2576 symbol_ptr->SetByteSize(sym_idx + 1);
2577 symbol_ptr->SetSizeIsSibling(true);
2578 N_BRAC_indexes.pop_back();
2579 }
2580 type = eSymbolTypeScopeEnd;
2581 break;
2582
Charles Davis510938e2013-08-27 05:04:57 +00002583 case N_EXCL:
2584 // deleted include file: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00002585 type = eSymbolTypeHeaderFile;
2586 break;
2587
2588 //----------------------------------------------------------------------
2589 // COMM scopes
2590 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00002591 case N_BCOMM:
2592 // begin common: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002593 // We use the current number of symbols in the symbol table in lieu of
2594 // using nlist_idx in case we ever start trimming entries out
2595 type = eSymbolTypeScopeBegin;
2596 N_COMM_indexes.push_back(sym_idx);
2597 break;
2598
Charles Davis510938e2013-08-27 05:04:57 +00002599 case N_ECOML:
2600 // end common (local name): 0,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002601 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2602 // Fall through
2603
Charles Davis510938e2013-08-27 05:04:57 +00002604 case N_ECOMM:
2605 // end common: name,,n_sect,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002606 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2607 // so that we can always skip the entire symbol if we need to navigate
2608 // more quickly at the source level when parsing STABS
2609 if ( !N_COMM_indexes.empty() )
2610 {
2611 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2612 symbol_ptr->SetByteSize(sym_idx + 1);
2613 symbol_ptr->SetSizeIsSibling(true);
2614 N_COMM_indexes.pop_back();
2615 }
2616 type = eSymbolTypeScopeEnd;
2617 break;
2618
Charles Davis510938e2013-08-27 05:04:57 +00002619 case N_LENG:
2620 // second stab entry with length information
Jason Molendaa5609c82012-06-21 01:51:02 +00002621 type = eSymbolTypeAdditional;
2622 break;
2623
2624 default: break;
2625 }
2626 }
2627 else
2628 {
Charles Davis510938e2013-08-27 05:04:57 +00002629 //uint8_t n_pext = N_PEXT & nlist.n_type;
2630 uint8_t n_type = N_TYPE & nlist.n_type;
2631 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Jason Molendaa5609c82012-06-21 01:51:02 +00002632
2633 switch (n_type)
2634 {
Charles Davis510938e2013-08-27 05:04:57 +00002635 case N_INDR: // Fall through
2636 case N_PBUD: // Fall through
2637 case N_UNDF:
Jason Molendaa5609c82012-06-21 01:51:02 +00002638 type = eSymbolTypeUndefined;
2639 break;
2640
Charles Davis510938e2013-08-27 05:04:57 +00002641 case N_ABS:
Jason Molendaa5609c82012-06-21 01:51:02 +00002642 type = eSymbolTypeAbsolute;
2643 break;
2644
Charles Davis510938e2013-08-27 05:04:57 +00002645 case N_SECT:
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002646 {
2647 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002648
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002649 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00002650 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002651 // TODO: warn about this?
2652 add_nlist = false;
2653 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002654 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002655
2656 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00002657 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002658 type = eSymbolTypeException;
2659 }
2660 else
2661 {
Charles Davis510938e2013-08-27 05:04:57 +00002662 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002663
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002664 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00002665 {
Charles Davis510938e2013-08-27 05:04:57 +00002666 case S_REGULAR: break; // regular section
2667 //case S_ZEROFILL: type = eSymbolTypeData; break; // zero fill on demand section
2668 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
2669 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
2670 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
2671 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2672 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2673 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2674 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2675 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2676 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
2677 //case S_COALESCED: type = eSymbolType; break; // section contains symbols that are to be coalesced
2678 //case S_GB_ZEROFILL: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2679 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2680 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
2681 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
2682 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002683 default: break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002684 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002685
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002686 if (type == eSymbolTypeInvalid)
2687 {
2688 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2689 if (symbol_section->IsDescendant (text_section_sp.get()))
2690 {
Charles Davis510938e2013-08-27 05:04:57 +00002691 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
2692 S_ATTR_SELF_MODIFYING_CODE |
2693 S_ATTR_SOME_INSTRUCTIONS))
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002694 type = eSymbolTypeData;
2695 else
2696 type = eSymbolTypeCode;
2697 }
2698 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00002699 {
2700 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2701 {
2702 type = eSymbolTypeRuntime;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002703
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002704 if (symbol_name &&
2705 symbol_name[0] == '_' &&
2706 symbol_name[1] == 'O' &&
Jason Molendaa5609c82012-06-21 01:51:02 +00002707 symbol_name[2] == 'B')
2708 {
2709 llvm::StringRef symbol_name_ref(symbol_name);
2710 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2711 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2712 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2713 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2714 {
2715 symbol_name_non_abi_mangled = symbol_name + 1;
2716 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2717 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002718 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002719 }
2720 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2721 {
2722 symbol_name_non_abi_mangled = symbol_name + 1;
2723 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2724 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002725 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002726 }
2727 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2728 {
2729 symbol_name_non_abi_mangled = symbol_name + 1;
2730 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2731 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002732 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002733 }
2734 }
2735 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002736 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00002737 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002738 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00002739 }
2740 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002741 {
2742 type = eSymbolTypeData;
2743 }
2744 }
2745 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2746 {
2747 type = eSymbolTypeTrampoline;
2748 }
2749 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2750 {
2751 type = eSymbolTypeRuntime;
2752 if (symbol_name && symbol_name[0] == '.')
2753 {
2754 llvm::StringRef symbol_name_ref(symbol_name);
2755 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2756 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00002757 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002758 symbol_name_non_abi_mangled = symbol_name;
2759 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2760 type = eSymbolTypeObjCClass;
2761 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002762 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002763 }
2764 }
2765 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002766 }
2767 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002768 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002769 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002770 }
2771
2772 if (add_nlist)
2773 {
2774 uint64_t symbol_value = nlist.n_value;
Jason Molendaa5609c82012-06-21 01:51:02 +00002775 if (symbol_name_non_abi_mangled)
2776 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00002777 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2778 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00002779 }
2780 else
2781 {
Greg Clayton3046e662013-07-10 01:23:25 +00002782 bool symbol_name_is_mangled = false;
2783
Jason Molendaa5609c82012-06-21 01:51:02 +00002784 if (symbol_name && symbol_name[0] == '_')
2785 {
2786 symbol_name_is_mangled = symbol_name[1] == '_';
2787 symbol_name++; // Skip the leading underscore
2788 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002789
Jason Molendaa5609c82012-06-21 01:51:02 +00002790 if (symbol_name)
2791 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002792 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00002793 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00002794 if (is_gsym && is_debug)
2795 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
Jason Molendaa5609c82012-06-21 01:51:02 +00002796 }
2797 }
2798 if (symbol_section)
2799 {
2800 const addr_t section_file_addr = symbol_section->GetFileAddress();
2801 if (symbol_byte_size == 0 && function_starts_count > 0)
2802 {
2803 addr_t symbol_lookup_file_addr = nlist.n_value;
2804 // Do an exact address match for non-ARM addresses, else get the closest since
2805 // the symbol might be a thumb symbol which has an address with bit zero set
2806 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2807 if (is_arm && func_start_entry)
2808 {
2809 // Verify that the function start address is the symbol address (ARM)
2810 // or the symbol address + 1 (thumb)
2811 if (func_start_entry->addr != symbol_lookup_file_addr &&
2812 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2813 {
2814 // Not the right entry, NULL it out...
2815 func_start_entry = NULL;
2816 }
2817 }
2818 if (func_start_entry)
2819 {
2820 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002821
Jason Molendaa5609c82012-06-21 01:51:02 +00002822 addr_t symbol_file_addr = func_start_entry->addr;
2823 uint32_t symbol_flags = 0;
2824 if (is_arm)
2825 {
2826 if (symbol_file_addr & 1)
2827 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2828 symbol_file_addr &= 0xfffffffffffffffeull;
2829 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002830
Jason Molendaa5609c82012-06-21 01:51:02 +00002831 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2832 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2833 if (next_func_start_entry)
2834 {
2835 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2836 // Be sure the clear the Thumb address bit when we calculate the size
2837 // from the current and next address
2838 if (is_arm)
2839 next_symbol_file_addr &= 0xfffffffffffffffeull;
2840 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2841 }
2842 else
2843 {
2844 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2845 }
2846 }
2847 }
2848 symbol_value -= section_file_addr;
2849 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002850
Greg Claytondacc4a92013-05-14 22:19:37 +00002851 if (is_debug == false)
2852 {
2853 if (type == eSymbolTypeCode)
2854 {
2855 // See if we can find a N_FUN entry for any code symbols.
2856 // If we do find a match, and the name matches, then we
2857 // can merge the two into just the function symbol to avoid
2858 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00002859 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
2860 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
2861 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00002862 {
Greg Claytond81088c2014-01-16 01:38:29 +00002863 bool found_it = false;
2864 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00002865 {
Greg Claytond81088c2014-01-16 01:38:29 +00002866 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
2867 {
2868 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2869 // We just need the flags from the linker symbol, so put these flags
2870 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2871 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
2872 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2873 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
2874 sym[pos->second].SetType (eSymbolTypeResolver);
2875 sym[sym_idx].Clear();
2876 found_it = true;
2877 break;
2878 }
Greg Claytondacc4a92013-05-14 22:19:37 +00002879 }
Greg Claytond81088c2014-01-16 01:38:29 +00002880 if (found_it)
2881 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00002882 }
Jim Inghamea3ac272014-01-10 22:55:37 +00002883 else
2884 {
Greg Claytond81088c2014-01-16 01:38:29 +00002885 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00002886 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00002887 }
Greg Claytondacc4a92013-05-14 22:19:37 +00002888 }
2889 else if (type == eSymbolTypeData)
2890 {
2891 // See if we can find a N_STSYM entry for any data symbols.
2892 // If we do find a match, and the name matches, then we
2893 // can merge the two into just the Static symbol to avoid
2894 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00002895 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
2896 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
2897 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00002898 {
Greg Claytond81088c2014-01-16 01:38:29 +00002899 bool found_it = false;
2900 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00002901 {
Greg Claytond81088c2014-01-16 01:38:29 +00002902 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
2903 {
2904 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2905 // We just need the flags from the linker symbol, so put these flags
2906 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2907 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
2908 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2909 sym[sym_idx].Clear();
2910 found_it = true;
2911 break;
2912 }
Greg Claytondacc4a92013-05-14 22:19:37 +00002913 }
Greg Claytond81088c2014-01-16 01:38:29 +00002914 if (found_it)
2915 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00002916 }
2917 else
2918 {
2919 // Combine N_GSYM stab entries with the non stab symbol
Greg Clayton3046e662013-07-10 01:23:25 +00002920 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
Greg Claytondacc4a92013-05-14 22:19:37 +00002921 if (pos != N_GSYM_name_to_sym_idx.end())
2922 {
2923 const uint32_t GSYM_sym_idx = pos->second;
2924 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
2925 // Copy the address, because often the N_GSYM address has an invalid address of zero
2926 // when the global is a common symbol
2927 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
2928 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
2929 // We just need the flags from the linker symbol, so put these flags
2930 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2931 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2932 sym[sym_idx].Clear();
2933 continue;
2934 }
2935 }
2936 }
2937 }
2938
Jason Molendaa5609c82012-06-21 01:51:02 +00002939 sym[sym_idx].SetID (nlist_idx);
2940 sym[sym_idx].SetType (type);
2941 sym[sym_idx].GetAddress().SetSection (symbol_section);
2942 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2943 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002944
Jason Molendaa5609c82012-06-21 01:51:02 +00002945 if (symbol_byte_size > 0)
2946 sym[sym_idx].SetByteSize(symbol_byte_size);
2947
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002948 if (demangled_is_synthesized)
2949 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00002950 ++sym_idx;
2951 }
2952 else
2953 {
2954 sym[sym_idx].Clear();
2955 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002956
Jason Molendaa5609c82012-06-21 01:51:02 +00002957 }
2958 /////////////////////////////
2959 }
2960 break; // No more entries to consider
2961 }
2962 }
2963 }
2964 }
2965 }
2966 }
2967 }
2968
2969 // Must reset this in case it was mutated above!
2970 nlist_data_offset = 0;
2971#endif
Jim Inghamea3ac272014-01-10 22:55:37 +00002972
Greg Claytonfd814c52013-08-13 01:42:25 +00002973 if (nlist_data.GetByteSize() > 0)
Jason Molendaa5609c82012-06-21 01:51:02 +00002974 {
Jason Molendaa5609c82012-06-21 01:51:02 +00002975
Greg Claytonfd814c52013-08-13 01:42:25 +00002976 // If the sym array was not created while parsing the DSC unmapped
2977 // symbols, create it now.
2978 if (sym == NULL)
Greg Clayton4c82d422012-05-18 23:20:01 +00002979 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002980 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2981 num_syms = symtab->GetNumSymbols();
2982 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002983
Greg Claytonfd814c52013-08-13 01:42:25 +00002984 if (unmapped_local_symbols_found)
2985 {
2986 assert(m_dysymtab.ilocalsym == 0);
2987 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2988 nlist_idx = m_dysymtab.nlocalsym;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002989 }
Greg Claytondebb8812012-05-25 17:04:00 +00002990 else
2991 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002992 nlist_idx = 0;
Greg Claytondebb8812012-05-25 17:04:00 +00002993 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002994
Greg Claytonfd814c52013-08-13 01:42:25 +00002995 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002996 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002997 struct nlist_64 nlist;
2998 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2999 break;
3000
3001 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
3002 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
3003 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
3004 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
3005 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
3006
3007 SymbolType type = eSymbolTypeInvalid;
3008 const char *symbol_name = NULL;
3009
3010 if (have_strtab_data)
Greg Clayton77ccca72011-12-30 00:32:24 +00003011 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003012 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003013
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003014 if (symbol_name == NULL)
3015 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003016 // No symbol should be NULL, even the symbols with no
3017 // string values should have an offset zero which points
3018 // to an empty C-string
3019 Host::SystemLog (Host::eSystemLogError,
3020 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
3021 nlist_idx,
3022 nlist.n_strx,
3023 module_sp->GetFileSpec().GetPath().c_str());
3024 continue;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003025 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003026 if (symbol_name[0] == '\0')
3027 symbol_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003028 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003029 else
3030 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003031 const addr_t str_addr = strtab_addr + nlist.n_strx;
3032 Error str_error;
3033 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
3034 symbol_name = memory_symbol_name.c_str();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003035 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003036 const char *symbol_name_non_abi_mangled = NULL;
3037
3038 SectionSP symbol_section;
3039 lldb::addr_t symbol_byte_size = 0;
3040 bool add_nlist = true;
3041 bool is_gsym = false;
Charles Davis510938e2013-08-27 05:04:57 +00003042 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00003043 bool demangled_is_synthesized = false;
3044
3045 assert (sym_idx < num_syms);
3046
3047 sym[sym_idx].SetDebug (is_debug);
3048
3049 if (is_debug)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003050 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003051 switch (nlist.n_type)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003052 {
Charles Davis510938e2013-08-27 05:04:57 +00003053 case N_GSYM:
3054 // global symbol: name,,NO_SECT,type,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003055 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003056
Greg Claytonfd814c52013-08-13 01:42:25 +00003057 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
3058 // have the same address, but we want to ensure that we always find only the real symbol,
3059 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
3060 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
3061 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
3062 // same address.
Greg Clayton29e08cb2012-03-14 01:53:24 +00003063
Greg Claytonfd814c52013-08-13 01:42:25 +00003064 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
3065 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
3066 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
3067 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
3068 add_nlist = false;
3069 else
3070 {
3071 is_gsym = true;
3072 sym[sym_idx].SetExternal(true);
3073 if (nlist.n_value != 0)
3074 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3075 type = eSymbolTypeData;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003076 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003077 break;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003078
Charles Davis510938e2013-08-27 05:04:57 +00003079 case N_FNAME:
3080 // procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003081 type = eSymbolTypeCompiler;
3082 break;
3083
Charles Davis510938e2013-08-27 05:04:57 +00003084 case N_FUN:
3085 // procedure: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003086 if (symbol_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00003087 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003088 type = eSymbolTypeCode;
3089 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3090
Greg Claytond81088c2014-01-16 01:38:29 +00003091 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +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_FUN_indexes.push_back(sym_idx);
Greg Claytondacc4a92013-05-14 22:19:37 +00003095 }
3096 else
3097 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003098 type = eSymbolTypeCompiler;
3099
3100 if ( !N_FUN_indexes.empty() )
Greg Claytondacc4a92013-05-14 22:19:37 +00003101 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003102 // Copy the size of the function into the original STAB entry so we don't have
3103 // to hunt for it later
3104 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
3105 N_FUN_indexes.pop_back();
3106 // We don't really need the end function STAB as it contains the size which
3107 // we already placed with the original symbol, so don't add it if we want a
3108 // minimal symbol table
3109 add_nlist = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00003110 }
3111 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003112 break;
3113
Charles Davis510938e2013-08-27 05:04:57 +00003114 case N_STSYM:
3115 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00003116 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003117 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3118 type = eSymbolTypeData;
3119 break;
3120
Charles Davis510938e2013-08-27 05:04:57 +00003121 case N_LCSYM:
3122 // .lcomm symbol: name,,n_sect,type,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003123 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3124 type = eSymbolTypeCommonBlock;
3125 break;
3126
Charles Davis510938e2013-08-27 05:04:57 +00003127 case N_BNSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003128 // We use the current number of symbols in the symbol table in lieu of
3129 // using nlist_idx in case we ever start trimming entries out
3130 // Skip these if we want minimal symbol tables
3131 add_nlist = false;
3132 break;
3133
Charles Davis510938e2013-08-27 05:04:57 +00003134 case N_ENSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003135 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
3136 // so that we can always skip the entire symbol if we need to navigate
3137 // more quickly at the source level when parsing STABS
3138 // Skip these if we want minimal symbol tables
3139 add_nlist = false;
3140 break;
3141
3142
Charles Davis510938e2013-08-27 05:04:57 +00003143 case N_OPT:
3144 // emitted with gcc2_compiled and in gcc source
Greg Claytonfd814c52013-08-13 01:42:25 +00003145 type = eSymbolTypeCompiler;
3146 break;
3147
Charles Davis510938e2013-08-27 05:04:57 +00003148 case N_RSYM:
3149 // register sym: name,,NO_SECT,type,register
Greg Claytonfd814c52013-08-13 01:42:25 +00003150 type = eSymbolTypeVariable;
3151 break;
3152
Charles Davis510938e2013-08-27 05:04:57 +00003153 case N_SLINE:
3154 // src line: 0,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003155 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3156 type = eSymbolTypeLineEntry;
3157 break;
3158
Charles Davis510938e2013-08-27 05:04:57 +00003159 case N_SSYM:
3160 // structure elt: name,,NO_SECT,type,struct_offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003161 type = eSymbolTypeVariableType;
3162 break;
3163
Charles Davis510938e2013-08-27 05:04:57 +00003164 case N_SO:
3165 // source file name
Greg Claytonfd814c52013-08-13 01:42:25 +00003166 type = eSymbolTypeSourceFile;
3167 if (symbol_name == NULL)
3168 {
3169 add_nlist = false;
3170 if (N_SO_index != UINT32_MAX)
3171 {
3172 // Set the size of the N_SO to the terminating index of this N_SO
3173 // so that we can always skip the entire N_SO if we need to navigate
3174 // more quickly at the source level when parsing STABS
3175 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
3176 symbol_ptr->SetByteSize(sym_idx);
3177 symbol_ptr->SetSizeIsSibling(true);
3178 }
3179 N_NSYM_indexes.clear();
3180 N_INCL_indexes.clear();
3181 N_BRAC_indexes.clear();
3182 N_COMM_indexes.clear();
3183 N_FUN_indexes.clear();
3184 N_SO_index = UINT32_MAX;
3185 }
3186 else
3187 {
3188 // We use the current number of symbols in the symbol table in lieu of
3189 // using nlist_idx in case we ever start trimming entries out
3190 const bool N_SO_has_full_path = symbol_name[0] == '/';
3191 if (N_SO_has_full_path)
3192 {
3193 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3194 {
3195 // We have two consecutive N_SO entries where the first contains a directory
3196 // and the second contains a full path.
3197 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
3198 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3199 add_nlist = false;
3200 }
3201 else
3202 {
3203 // This is the first entry in a N_SO that contains a directory or
3204 // a full path to the source file
3205 N_SO_index = sym_idx;
3206 }
3207 }
3208 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3209 {
3210 // This is usually the second N_SO entry that contains just the filename,
3211 // so here we combine it with the first one if we are minimizing the symbol table
3212 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
3213 if (so_path && so_path[0])
3214 {
3215 std::string full_so_path (so_path);
3216 const size_t double_slash_pos = full_so_path.find("//");
3217 if (double_slash_pos != std::string::npos)
3218 {
3219 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00003220 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Claytonfd814c52013-08-13 01:42:25 +00003221 // and the second is the directory for the source file so you end up with
3222 // a path that looks like "/tmp/src//tmp/src/"
3223 FileSpec so_dir(so_path, false);
3224 if (!so_dir.Exists())
3225 {
3226 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3227 if (so_dir.Exists())
3228 {
3229 // Trim off the incorrect path
3230 full_so_path.erase(0, double_slash_pos + 1);
3231 }
3232 }
3233 }
3234 if (*full_so_path.rbegin() != '/')
3235 full_so_path += '/';
3236 full_so_path += symbol_name;
3237 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
3238 add_nlist = false;
3239 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3240 }
3241 }
3242 else
3243 {
3244 // This could be a relative path to a N_SO
3245 N_SO_index = sym_idx;
3246 }
3247 }
3248
3249 break;
3250
Charles Davis510938e2013-08-27 05:04:57 +00003251 case N_OSO:
3252 // object file name: name,,0,0,st_mtime
Greg Claytonfd814c52013-08-13 01:42:25 +00003253 type = eSymbolTypeObjectFile;
3254 break;
3255
Charles Davis510938e2013-08-27 05:04:57 +00003256 case N_LSYM:
3257 // local sym: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003258 type = eSymbolTypeLocal;
3259 break;
3260
3261 //----------------------------------------------------------------------
3262 // INCL scopes
3263 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003264 case N_BINCL:
3265 // include file beginning: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00003266 // We use the current number of symbols in the symbol table in lieu of
3267 // using nlist_idx in case we ever start trimming entries out
3268 N_INCL_indexes.push_back(sym_idx);
3269 type = eSymbolTypeScopeBegin;
3270 break;
3271
Charles Davis510938e2013-08-27 05:04:57 +00003272 case N_EINCL:
3273 // include file end: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003274 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3275 // so that we can always skip the entire symbol if we need to navigate
3276 // more quickly at the source level when parsing STABS
3277 if ( !N_INCL_indexes.empty() )
3278 {
3279 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3280 symbol_ptr->SetByteSize(sym_idx + 1);
3281 symbol_ptr->SetSizeIsSibling(true);
3282 N_INCL_indexes.pop_back();
3283 }
3284 type = eSymbolTypeScopeEnd;
3285 break;
3286
Charles Davis510938e2013-08-27 05:04:57 +00003287 case N_SOL:
3288 // #included file name: name,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003289 type = eSymbolTypeHeaderFile;
3290
3291 // We currently don't use the header files on darwin
3292 add_nlist = false;
3293 break;
3294
Charles Davis510938e2013-08-27 05:04:57 +00003295 case N_PARAMS:
3296 // compiler parameters: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003297 type = eSymbolTypeCompiler;
3298 break;
3299
Charles Davis510938e2013-08-27 05:04:57 +00003300 case N_VERSION:
3301 // compiler version: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003302 type = eSymbolTypeCompiler;
3303 break;
3304
Charles Davis510938e2013-08-27 05:04:57 +00003305 case N_OLEVEL:
3306 // compiler -O level: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003307 type = eSymbolTypeCompiler;
3308 break;
3309
Charles Davis510938e2013-08-27 05:04:57 +00003310 case N_PSYM:
3311 // parameter: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003312 type = eSymbolTypeVariable;
3313 break;
3314
Charles Davis510938e2013-08-27 05:04:57 +00003315 case N_ENTRY:
3316 // alternate entry: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003317 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3318 type = eSymbolTypeLineEntry;
3319 break;
3320
3321 //----------------------------------------------------------------------
3322 // Left and Right Braces
3323 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003324 case N_LBRAC:
3325 // left bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003326 // We use the current number of symbols in the symbol table in lieu of
3327 // using nlist_idx in case we ever start trimming entries out
3328 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3329 N_BRAC_indexes.push_back(sym_idx);
3330 type = eSymbolTypeScopeBegin;
3331 break;
3332
Charles Davis510938e2013-08-27 05:04:57 +00003333 case N_RBRAC:
3334 // right bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003335 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3336 // so that we can always skip the entire symbol if we need to navigate
3337 // more quickly at the source level when parsing STABS
3338 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3339 if ( !N_BRAC_indexes.empty() )
3340 {
3341 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3342 symbol_ptr->SetByteSize(sym_idx + 1);
3343 symbol_ptr->SetSizeIsSibling(true);
3344 N_BRAC_indexes.pop_back();
3345 }
3346 type = eSymbolTypeScopeEnd;
3347 break;
3348
Charles Davis510938e2013-08-27 05:04:57 +00003349 case N_EXCL:
3350 // deleted include file: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00003351 type = eSymbolTypeHeaderFile;
3352 break;
3353
3354 //----------------------------------------------------------------------
3355 // COMM scopes
3356 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003357 case N_BCOMM:
3358 // begin common: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003359 // We use the current number of symbols in the symbol table in lieu of
3360 // using nlist_idx in case we ever start trimming entries out
3361 type = eSymbolTypeScopeBegin;
3362 N_COMM_indexes.push_back(sym_idx);
3363 break;
3364
Charles Davis510938e2013-08-27 05:04:57 +00003365 case N_ECOML:
3366 // end common (local name): 0,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003367 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3368 // Fall through
3369
Charles Davis510938e2013-08-27 05:04:57 +00003370 case N_ECOMM:
3371 // end common: name,,n_sect,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003372 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3373 // so that we can always skip the entire symbol if we need to navigate
3374 // more quickly at the source level when parsing STABS
3375 if ( !N_COMM_indexes.empty() )
3376 {
3377 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3378 symbol_ptr->SetByteSize(sym_idx + 1);
3379 symbol_ptr->SetSizeIsSibling(true);
3380 N_COMM_indexes.pop_back();
3381 }
3382 type = eSymbolTypeScopeEnd;
3383 break;
3384
Charles Davis510938e2013-08-27 05:04:57 +00003385 case N_LENG:
3386 // second stab entry with length information
Greg Claytonfd814c52013-08-13 01:42:25 +00003387 type = eSymbolTypeAdditional;
3388 break;
3389
3390 default: break;
3391 }
3392 }
3393 else
3394 {
Charles Davis510938e2013-08-27 05:04:57 +00003395 //uint8_t n_pext = N_PEXT & nlist.n_type;
3396 uint8_t n_type = N_TYPE & nlist.n_type;
3397 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00003398
3399 switch (n_type)
3400 {
Charles Davis510938e2013-08-27 05:04:57 +00003401 case N_INDR:// Fall through
3402 case N_PBUD:// Fall through
3403 case N_UNDF:
Greg Claytonfd814c52013-08-13 01:42:25 +00003404 type = eSymbolTypeUndefined;
3405 break;
3406
Charles Davis510938e2013-08-27 05:04:57 +00003407 case N_ABS:
Greg Claytonfd814c52013-08-13 01:42:25 +00003408 type = eSymbolTypeAbsolute;
3409 break;
3410
Charles Davis510938e2013-08-27 05:04:57 +00003411 case N_SECT:
Greg Claytonfd814c52013-08-13 01:42:25 +00003412 {
3413 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3414
3415 if (!symbol_section)
3416 {
3417 // TODO: warn about this?
3418 add_nlist = false;
3419 break;
3420 }
3421
3422 if (TEXT_eh_frame_sectID == nlist.n_sect)
3423 {
3424 type = eSymbolTypeException;
3425 }
3426 else
3427 {
Charles Davis510938e2013-08-27 05:04:57 +00003428 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Greg Claytonfd814c52013-08-13 01:42:25 +00003429
3430 switch (section_type)
3431 {
Charles Davis510938e2013-08-27 05:04:57 +00003432 case S_REGULAR: break; // regular section
3433 //case S_ZEROFILL: type = eSymbolTypeData; break; // zero fill on demand section
3434 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
3435 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
3436 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
3437 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3438 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3439 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3440 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3441 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3442 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
3443 //case S_COALESCED: type = eSymbolType; break; // section contains symbols that are to be coalesced
3444 //case S_GB_ZEROFILL: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3445 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3446 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
3447 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
3448 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Claytonfd814c52013-08-13 01:42:25 +00003449 default: break;
3450 }
3451
3452 if (type == eSymbolTypeInvalid)
3453 {
3454 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3455 if (symbol_section->IsDescendant (text_section_sp.get()))
3456 {
Charles Davis510938e2013-08-27 05:04:57 +00003457 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
3458 S_ATTR_SELF_MODIFYING_CODE |
3459 S_ATTR_SOME_INSTRUCTIONS))
Greg Claytonfd814c52013-08-13 01:42:25 +00003460 type = eSymbolTypeData;
3461 else
3462 type = eSymbolTypeCode;
3463 }
3464 else
3465 if (symbol_section->IsDescendant(data_section_sp.get()))
3466 {
3467 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
3468 {
3469 type = eSymbolTypeRuntime;
3470
3471 if (symbol_name &&
3472 symbol_name[0] == '_' &&
3473 symbol_name[1] == 'O' &&
3474 symbol_name[2] == 'B')
3475 {
3476 llvm::StringRef symbol_name_ref(symbol_name);
3477 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3478 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3479 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3480 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3481 {
3482 symbol_name_non_abi_mangled = symbol_name + 1;
3483 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3484 type = eSymbolTypeObjCClass;
3485 demangled_is_synthesized = true;
3486 }
3487 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3488 {
3489 symbol_name_non_abi_mangled = symbol_name + 1;
3490 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3491 type = eSymbolTypeObjCMetaClass;
3492 demangled_is_synthesized = true;
3493 }
3494 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3495 {
3496 symbol_name_non_abi_mangled = symbol_name + 1;
3497 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3498 type = eSymbolTypeObjCIVar;
3499 demangled_is_synthesized = true;
3500 }
3501 }
3502 }
3503 else
3504 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3505 {
3506 type = eSymbolTypeException;
3507 }
3508 else
3509 {
3510 type = eSymbolTypeData;
3511 }
3512 }
3513 else
3514 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3515 {
3516 type = eSymbolTypeTrampoline;
3517 }
3518 else
3519 if (symbol_section->IsDescendant(objc_section_sp.get()))
3520 {
3521 type = eSymbolTypeRuntime;
3522 if (symbol_name && symbol_name[0] == '.')
3523 {
3524 llvm::StringRef symbol_name_ref(symbol_name);
3525 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3526 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3527 {
3528 symbol_name_non_abi_mangled = symbol_name;
3529 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3530 type = eSymbolTypeObjCClass;
3531 demangled_is_synthesized = true;
3532 }
3533 }
3534 }
3535 }
3536 }
3537 }
3538 break;
Greg Claytondacc4a92013-05-14 22:19:37 +00003539 }
3540 }
3541
Greg Claytonfd814c52013-08-13 01:42:25 +00003542 if (add_nlist)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003543 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003544 uint64_t symbol_value = nlist.n_value;
3545
3546 if (symbol_name_non_abi_mangled)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003547 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003548 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3549 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
3550 }
3551 else
3552 {
3553 bool symbol_name_is_mangled = false;
3554
3555 if (symbol_name && symbol_name[0] == '_')
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003556 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003557 symbol_name_is_mangled = symbol_name[1] == '_';
3558 symbol_name++; // Skip the leading underscore
3559 }
3560
3561 if (symbol_name)
3562 {
3563 ConstString const_symbol_name(symbol_name);
3564 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
3565 if (is_gsym && is_debug)
3566 {
3567 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
3568 }
3569 }
3570 }
3571 if (symbol_section)
3572 {
3573 const addr_t section_file_addr = symbol_section->GetFileAddress();
3574 if (symbol_byte_size == 0 && function_starts_count > 0)
3575 {
3576 addr_t symbol_lookup_file_addr = nlist.n_value;
3577 // Do an exact address match for non-ARM addresses, else get the closest since
3578 // the symbol might be a thumb symbol which has an address with bit zero set
3579 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3580 if (is_arm && func_start_entry)
3581 {
3582 // Verify that the function start address is the symbol address (ARM)
3583 // or the symbol address + 1 (thumb)
3584 if (func_start_entry->addr != symbol_lookup_file_addr &&
3585 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3586 {
3587 // Not the right entry, NULL it out...
3588 func_start_entry = NULL;
3589 }
3590 }
3591 if (func_start_entry)
3592 {
3593 func_start_entry->data = true;
3594
3595 addr_t symbol_file_addr = func_start_entry->addr;
3596 if (is_arm)
3597 symbol_file_addr &= 0xfffffffffffffffeull;
3598
3599 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3600 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3601 if (next_func_start_entry)
3602 {
3603 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3604 // Be sure the clear the Thumb address bit when we calculate the size
3605 // from the current and next address
3606 if (is_arm)
3607 next_symbol_file_addr &= 0xfffffffffffffffeull;
3608 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3609 }
3610 else
3611 {
3612 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3613 }
3614 }
3615 }
3616 symbol_value -= section_file_addr;
3617 }
3618
3619 if (is_debug == false)
3620 {
3621 if (type == eSymbolTypeCode)
3622 {
3623 // See if we can find a N_FUN entry for any code symbols.
3624 // If we do find a match, and the name matches, then we
3625 // can merge the two into just the function symbol to avoid
3626 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003627 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3628 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3629 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00003630 {
Greg Claytond81088c2014-01-16 01:38:29 +00003631 bool found_it = false;
3632 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00003633 {
Greg Claytond81088c2014-01-16 01:38:29 +00003634 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3635 {
3636 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3637 // We just need the flags from the linker symbol, so put these flags
3638 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3639 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3640 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3641 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3642 sym[pos->second].SetType (eSymbolTypeResolver);
3643 sym[sym_idx].Clear();
3644 found_it = true;
3645 break;
3646 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003647 }
Greg Claytond81088c2014-01-16 01:38:29 +00003648 if (found_it)
3649 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00003650 }
Jim Inghamea3ac272014-01-10 22:55:37 +00003651 else
3652 {
3653 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00003654 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00003655 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003656 }
3657 else if (type == eSymbolTypeData)
3658 {
3659 // See if we can find a N_STSYM entry for any data symbols.
3660 // If we do find a match, and the name matches, then we
3661 // can merge the two into just the Static symbol to avoid
3662 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003663 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3664 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
3665 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00003666 {
Greg Claytond81088c2014-01-16 01:38:29 +00003667 bool found_it = false;
3668 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00003669 {
Greg Claytond81088c2014-01-16 01:38:29 +00003670 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3671 {
3672 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3673 // We just need the flags from the linker symbol, so put these flags
3674 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3675 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3676 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3677 sym[sym_idx].Clear();
3678 found_it = true;
3679 break;
3680 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003681 }
Greg Claytond81088c2014-01-16 01:38:29 +00003682 if (found_it)
3683 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00003684 }
3685 else
3686 {
3687 // Combine N_GSYM stab entries with the non stab symbol
3688 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
3689 if (pos != N_GSYM_name_to_sym_idx.end())
3690 {
3691 const uint32_t GSYM_sym_idx = pos->second;
3692 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3693 // Copy the address, because often the N_GSYM address has an invalid address of zero
3694 // when the global is a common symbol
3695 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3696 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3697 // We just need the flags from the linker symbol, so put these flags
3698 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3699 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3700 sym[sym_idx].Clear();
3701 continue;
3702 }
3703 }
3704 }
3705 }
3706
3707 sym[sym_idx].SetID (nlist_idx);
3708 sym[sym_idx].SetType (type);
3709 sym[sym_idx].GetAddress().SetSection (symbol_section);
3710 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3711 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3712
3713 if (symbol_byte_size > 0)
3714 sym[sym_idx].SetByteSize(symbol_byte_size);
3715
3716 if (demangled_is_synthesized)
3717 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3718
3719 ++sym_idx;
3720 }
3721 else
3722 {
3723 sym[sym_idx].Clear();
3724 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003725 }
3726 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003727
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003728 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3729
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003730 if (function_starts_count > 0)
3731 {
3732 char synthetic_function_symbol[PATH_MAX];
3733 uint32_t num_synthetic_function_symbols = 0;
3734 for (i=0; i<function_starts_count; ++i)
3735 {
3736 if (function_starts.GetEntryRef (i).data == false)
3737 ++num_synthetic_function_symbols;
3738 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003739
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003740 if (num_synthetic_function_symbols > 0)
3741 {
3742 if (num_syms < sym_idx + num_synthetic_function_symbols)
3743 {
3744 num_syms = sym_idx + num_synthetic_function_symbols;
3745 sym = symtab->Resize (num_syms);
3746 }
3747 uint32_t synthetic_function_symbol_idx = 0;
3748 for (i=0; i<function_starts_count; ++i)
3749 {
3750 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3751 if (func_start_entry->data == false)
3752 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003753 addr_t symbol_file_addr = func_start_entry->addr;
3754 uint32_t symbol_flags = 0;
3755 if (is_arm)
3756 {
3757 if (symbol_file_addr & 1)
3758 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3759 symbol_file_addr &= 0xfffffffffffffffeull;
3760 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003761 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003762 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003763 {
3764 SectionSP symbol_section (symbol_addr.GetSection());
3765 uint32_t symbol_byte_size = 0;
3766 if (symbol_section)
3767 {
3768 const addr_t section_file_addr = symbol_section->GetFileAddress();
3769 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3770 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3771 if (next_func_start_entry)
3772 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003773 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3774 if (is_arm)
3775 next_symbol_file_addr &= 0xfffffffffffffffeull;
3776 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 +00003777 }
3778 else
3779 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003780 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003781 }
3782 snprintf (synthetic_function_symbol,
3783 sizeof(synthetic_function_symbol),
3784 "___lldb_unnamed_function%u$$%s",
3785 ++synthetic_function_symbol_idx,
3786 module_sp->GetFileSpec().GetFilename().GetCString());
3787 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Clayton037520e2012-07-18 23:18:10 +00003788 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003789 sym[sym_idx].SetType (eSymbolTypeCode);
3790 sym[sym_idx].SetIsSynthetic (true);
3791 sym[sym_idx].GetAddress() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003792 if (symbol_flags)
3793 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003794 if (symbol_byte_size)
3795 sym[sym_idx].SetByteSize (symbol_byte_size);
3796 ++sym_idx;
3797 }
3798 }
3799 }
3800 }
3801 }
3802 }
3803
3804 // Trim our symbols down to just what we ended up with after
3805 // removing any symbols.
3806 if (sym_idx < num_syms)
3807 {
3808 num_syms = sym_idx;
3809 sym = symtab->Resize (num_syms);
3810 }
3811
3812 // Now synthesize indirect symbols
3813 if (m_dysymtab.nindirectsyms != 0)
3814 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003815 if (indirect_symbol_index_data.GetByteSize())
3816 {
3817 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3818
3819 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3820 {
Charles Davis510938e2013-08-27 05:04:57 +00003821 if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) == S_SYMBOL_STUBS)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003822 {
3823 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3824 if (symbol_stub_byte_size == 0)
3825 continue;
3826
3827 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3828
3829 if (num_symbol_stubs == 0)
3830 continue;
3831
3832 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3833 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3834 {
3835 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3836 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 +00003837 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003838 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3839 {
3840 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Charles Davis510938e2013-08-27 05:04:57 +00003841 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003842 continue;
3843
3844 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3845 Symbol *stub_symbol = NULL;
3846 if (index_pos != end_index_pos)
3847 {
3848 // We have a remapping from the original nlist index to
3849 // a current symbol index, so just look this up by index
3850 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3851 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003852 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003853 {
3854 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00003855 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003856 // S_SYMBOL_STUBS
3857 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3858 }
3859
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003860 if (stub_symbol)
3861 {
3862 Address so_addr(symbol_stub_addr, section_list);
3863
3864 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3865 {
3866 // Change the external symbol into a trampoline that makes sense
3867 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3868 // can re-use them so we don't have to make up a synthetic symbol
3869 // for no good reason.
Greg Clayton9191db42013-10-21 18:40:51 +00003870 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
3871 stub_symbol->SetType (eSymbolTypeTrampoline);
3872 else
3873 stub_symbol->SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003874 stub_symbol->SetExternal (false);
3875 stub_symbol->GetAddress() = so_addr;
3876 stub_symbol->SetByteSize (symbol_stub_byte_size);
3877 }
3878 else
3879 {
3880 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00003881 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003882 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00003883 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003884 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00003885 stub_symbol = NULL; // this pointer no longer valid
3886 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003887 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00003888 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton9191db42013-10-21 18:40:51 +00003889 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
3890 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3891 else
3892 sym[sym_idx].SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003893 sym[sym_idx].SetIsSynthetic (true);
3894 sym[sym_idx].GetAddress() = so_addr;
3895 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3896 ++sym_idx;
3897 }
3898 }
Greg Clayton3f839a32012-09-05 01:38:55 +00003899 else
3900 {
3901 if (log)
3902 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3903 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003904 }
3905 }
3906 }
3907 }
3908 }
3909 }
Greg Clayton9191db42013-10-21 18:40:51 +00003910
3911
3912 if (!trie_entries.empty())
3913 {
3914 for (const auto &e : trie_entries)
3915 {
3916 if (e.entry.import_name)
3917 {
3918 // Make a synthetic symbol to describe re-exported symbol.
3919 if (sym_idx >= num_syms)
3920 sym = symtab->Resize (++num_syms);
3921 sym[sym_idx].SetID (synthetic_sym_id++);
3922 sym[sym_idx].GetMangled() = Mangled(e.entry.name);
3923 sym[sym_idx].SetType (eSymbolTypeReExported);
3924 sym[sym_idx].SetIsSynthetic (true);
3925 sym[sym_idx].SetReExportedSymbolName(e.entry.import_name);
3926 if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize())
3927 {
3928 sym[sym_idx].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(e.entry.other-1));
3929 }
3930 ++sym_idx;
3931 }
3932 }
3933 }
3934
3935
Greg Clayton3046e662013-07-10 01:23:25 +00003936
3937// StreamFile s(stdout, false);
3938// s.Printf ("Symbol table before CalculateSymbolSizes():\n");
3939// symtab->Dump(&s, NULL, eSortOrderNone);
3940 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
3941 symtab->CalculateSymbolSizes();
3942
3943// s.Printf ("Symbol table after CalculateSymbolSizes():\n");
3944// symtab->Dump(&s, NULL, eSortOrderNone);
3945
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003946 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003947 }
3948 return 0;
3949}
3950
3951
3952void
3953ObjectFileMachO::Dump (Stream *s)
3954{
Greg Claytona1743492012-03-13 23:14:29 +00003955 ModuleSP module_sp(GetModule());
3956 if (module_sp)
3957 {
3958 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3959 s->Printf("%p: ", this);
3960 s->Indent();
Charles Davis510938e2013-08-27 05:04:57 +00003961 if (m_header.magic == MH_MAGIC_64 || m_header.magic == MH_CIGAM_64)
Greg Claytona1743492012-03-13 23:14:29 +00003962 s->PutCString("ObjectFileMachO64");
3963 else
3964 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003965
Greg Claytona1743492012-03-13 23:14:29 +00003966 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003967
Greg Claytona1743492012-03-13 23:14:29 +00003968 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003969
Greg Clayton3046e662013-07-10 01:23:25 +00003970 SectionList *sections = GetSectionList();
3971 if (sections)
3972 sections->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003973
Greg Claytona1743492012-03-13 23:14:29 +00003974 if (m_symtab_ap.get())
3975 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3976 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003977}
3978
Greg Claytonf4d6de62013-04-24 22:29:28 +00003979bool
3980ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3981 const lldb_private::DataExtractor &data,
3982 lldb::offset_t lc_offset,
3983 lldb_private::UUID& uuid)
3984{
3985 uint32_t i;
3986 struct uuid_command load_cmd;
3987
3988 lldb::offset_t offset = lc_offset;
3989 for (i=0; i<header.ncmds; ++i)
3990 {
3991 const lldb::offset_t cmd_offset = offset;
3992 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3993 break;
3994
Charles Davis510938e2013-08-27 05:04:57 +00003995 if (load_cmd.cmd == LC_UUID)
Greg Claytonf4d6de62013-04-24 22:29:28 +00003996 {
3997 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3998
3999 if (uuid_bytes)
4000 {
4001 // OpenCL on Mac OS X uses the same UUID for each of its object files.
4002 // We pretend these object files have no UUID to prevent crashing.
4003
4004 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
4005 0x3b, 0xa8,
4006 0x4b, 0x16,
4007 0xb6, 0xa4,
4008 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
4009
4010 if (!memcmp(uuid_bytes, opencl_uuid, 16))
4011 return false;
4012
4013 uuid.SetBytes (uuid_bytes);
4014 return true;
4015 }
4016 return false;
4017 }
4018 offset = cmd_offset + load_cmd.cmdsize;
4019 }
4020 return false;
4021}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004022
4023bool
Greg Clayton60830262011-02-04 18:53:10 +00004024ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004025{
Greg Claytona1743492012-03-13 23:14:29 +00004026 ModuleSP module_sp(GetModule());
4027 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004028 {
Greg Claytona1743492012-03-13 23:14:29 +00004029 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004030 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00004031 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004032 }
4033 return false;
4034}
4035
4036
4037uint32_t
4038ObjectFileMachO::GetDependentModules (FileSpecList& files)
4039{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004040 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00004041 ModuleSP module_sp(GetModule());
4042 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004043 {
Greg Claytona1743492012-03-13 23:14:29 +00004044 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4045 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004046 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004047 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
4048 uint32_t i;
4049 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004050 {
Greg Claytona1743492012-03-13 23:14:29 +00004051 const uint32_t cmd_offset = offset;
4052 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4053 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004054
Greg Claytona1743492012-03-13 23:14:29 +00004055 switch (load_cmd.cmd)
4056 {
Charles Davis510938e2013-08-27 05:04:57 +00004057 case LC_LOAD_DYLIB:
4058 case LC_LOAD_WEAK_DYLIB:
4059 case LC_REEXPORT_DYLIB:
4060 case LC_LOAD_DYLINKER:
4061 case LC_LOADFVMLIB:
4062 case LC_LOAD_UPWARD_DYLIB:
Greg Claytona1743492012-03-13 23:14:29 +00004063 {
4064 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
4065 const char *path = m_data.PeekCStr(name_offset);
4066 // Skip any path that starts with '@' since these are usually:
4067 // @executable_path/.../file
4068 // @rpath/.../file
4069 if (path && path[0] != '@')
4070 {
4071 FileSpec file_spec(path, resolve_path);
4072 if (files.AppendIfUnique(file_spec))
4073 count++;
4074 }
4075 }
4076 break;
4077
4078 default:
4079 break;
4080 }
4081 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004082 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004083 }
4084 return count;
4085}
4086
Jim Ingham672e6f52011-03-07 23:44:08 +00004087lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00004088ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00004089{
4090 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
4091 // is initialized to an invalid address, so we can just return that.
4092 // 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 +00004093
Jim Ingham672e6f52011-03-07 23:44:08 +00004094 if (!IsExecutable() || m_entry_point_address.IsValid())
4095 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004096
4097 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00004098 // /usr/include/mach-o.h, but it is basically:
4099 //
4100 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
4101 // uint32_t count - this is the count of longs in the thread state data
4102 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
4103 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00004104 //
Jim Ingham672e6f52011-03-07 23:44:08 +00004105 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
4106 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
4107 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
4108 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
4109 //
4110 // For now we hard-code the offsets and flavors we need:
4111 //
4112 //
4113
Greg Claytona1743492012-03-13 23:14:29 +00004114 ModuleSP module_sp(GetModule());
4115 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00004116 {
Greg Claytona1743492012-03-13 23:14:29 +00004117 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4118 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004119 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004120 uint32_t i;
4121 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
4122 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004123
Greg Claytona1743492012-03-13 23:14:29 +00004124 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00004125 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004126 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004127 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4128 break;
4129
4130 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00004131 {
Charles Davis510938e2013-08-27 05:04:57 +00004132 case LC_UNIXTHREAD:
4133 case LC_THREAD:
Jim Ingham672e6f52011-03-07 23:44:08 +00004134 {
Greg Claytona1743492012-03-13 23:14:29 +00004135 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00004136 {
Greg Claytona1743492012-03-13 23:14:29 +00004137 uint32_t flavor = m_data.GetU32(&offset);
4138 uint32_t count = m_data.GetU32(&offset);
4139 if (count == 0)
4140 {
4141 // We've gotten off somehow, log and exit;
4142 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00004143 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004144
Greg Claytona1743492012-03-13 23:14:29 +00004145 switch (m_header.cputype)
4146 {
Charles Davis510938e2013-08-27 05:04:57 +00004147 case llvm::MachO::CPU_TYPE_ARM:
Greg Claytona1743492012-03-13 23:14:29 +00004148 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
4149 {
4150 offset += 60; // This is the offset of pc in the GPR thread state data structure.
4151 start_address = m_data.GetU32(&offset);
4152 done = true;
4153 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004154 break;
Charles Davis510938e2013-08-27 05:04:57 +00004155 case llvm::MachO::CPU_TYPE_I386:
Greg Claytona1743492012-03-13 23:14:29 +00004156 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
4157 {
4158 offset += 40; // This is the offset of eip in the GPR thread state data structure.
4159 start_address = m_data.GetU32(&offset);
4160 done = true;
4161 }
4162 break;
Charles Davis510938e2013-08-27 05:04:57 +00004163 case llvm::MachO::CPU_TYPE_X86_64:
Greg Claytona1743492012-03-13 23:14:29 +00004164 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
4165 {
4166 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
4167 start_address = m_data.GetU64(&offset);
4168 done = true;
4169 }
4170 break;
4171 default:
4172 return m_entry_point_address;
4173 }
4174 // Haven't found the GPR flavor yet, skip over the data for this flavor:
4175 if (done)
4176 break;
4177 offset += count * 4;
4178 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004179 }
Greg Claytona1743492012-03-13 23:14:29 +00004180 break;
Charles Davis510938e2013-08-27 05:04:57 +00004181 case LC_MAIN:
Sean Callanan226b70c2012-03-08 02:39:03 +00004182 {
Greg Claytona1743492012-03-13 23:14:29 +00004183 ConstString text_segment_name ("__TEXT");
4184 uint64_t entryoffset = m_data.GetU64(&offset);
4185 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
4186 if (text_segment_sp)
4187 {
4188 done = true;
4189 start_address = text_segment_sp->GetFileAddress() + entryoffset;
4190 }
Sean Callanan226b70c2012-03-08 02:39:03 +00004191 }
Greg Claytona1743492012-03-13 23:14:29 +00004192
4193 default:
4194 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00004195 }
Greg Claytona1743492012-03-13 23:14:29 +00004196 if (done)
4197 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00004198
Greg Claytona1743492012-03-13 23:14:29 +00004199 // Go to the next load command:
4200 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00004201 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004202
Greg Claytona1743492012-03-13 23:14:29 +00004203 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00004204 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004205 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00004206 // of this ObjectFile:
4207 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00004208 {
Greg Claytona1743492012-03-13 23:14:29 +00004209 m_entry_point_address.Clear();
4210 }
4211 }
4212 else
4213 {
4214 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
4215 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00004216
Greg Claytona1743492012-03-13 23:14:29 +00004217 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00004218
Greg Claytona1743492012-03-13 23:14:29 +00004219 if (module_sp)
4220 {
4221 SymbolContextList contexts;
4222 SymbolContext context;
4223 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
4224 {
4225 if (contexts.GetContextAtIndex(0, context))
4226 m_entry_point_address = context.symbol->GetAddress();
4227 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004228 }
4229 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004230 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004231
Jim Ingham672e6f52011-03-07 23:44:08 +00004232 return m_entry_point_address;
4233
4234}
4235
Greg Claytonc9660542012-02-05 02:38:54 +00004236lldb_private::Address
4237ObjectFileMachO::GetHeaderAddress ()
4238{
4239 lldb_private::Address header_addr;
4240 SectionList *section_list = GetSectionList();
4241 if (section_list)
4242 {
4243 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
4244 if (text_segment_sp)
4245 {
Greg Claytone72dfb32012-02-24 01:59:29 +00004246 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00004247 header_addr.SetOffset (0);
4248 }
4249 }
4250 return header_addr;
4251}
4252
Greg Claytonc3776bf2012-02-09 06:16:32 +00004253uint32_t
4254ObjectFileMachO::GetNumThreadContexts ()
4255{
Greg Claytona1743492012-03-13 23:14:29 +00004256 ModuleSP module_sp(GetModule());
4257 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004258 {
Greg Claytona1743492012-03-13 23:14:29 +00004259 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4260 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004261 {
Greg Claytona1743492012-03-13 23:14:29 +00004262 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00004263 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004264 FileRangeArray::Entry file_range;
4265 thread_command thread_cmd;
4266 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004267 {
Greg Claytona1743492012-03-13 23:14:29 +00004268 const uint32_t cmd_offset = offset;
4269 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
4270 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004271
Charles Davis510938e2013-08-27 05:04:57 +00004272 if (thread_cmd.cmd == LC_THREAD)
Greg Claytona1743492012-03-13 23:14:29 +00004273 {
4274 file_range.SetRangeBase (offset);
4275 file_range.SetByteSize (thread_cmd.cmdsize - 8);
4276 m_thread_context_offsets.Append (file_range);
4277 }
4278 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00004279 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004280 }
4281 }
4282 return m_thread_context_offsets.GetSize();
4283}
4284
4285lldb::RegisterContextSP
4286ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
4287{
Greg Claytonc3776bf2012-02-09 06:16:32 +00004288 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00004289
Greg Claytona1743492012-03-13 23:14:29 +00004290 ModuleSP module_sp(GetModule());
4291 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004292 {
Greg Claytona1743492012-03-13 23:14:29 +00004293 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4294 if (!m_thread_context_offsets_valid)
4295 GetNumThreadContexts ();
4296
4297 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00004298 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00004299 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004300
4301 DataExtractor data (m_data,
4302 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00004303 thread_context_file_range->GetByteSize());
4304
4305 switch (m_header.cputype)
4306 {
Charles Davis510938e2013-08-27 05:04:57 +00004307 case llvm::MachO::CPU_TYPE_ARM:
Jim Ingham28eb5712012-10-12 17:34:26 +00004308 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
4309 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004310
Charles Davis510938e2013-08-27 05:04:57 +00004311 case llvm::MachO::CPU_TYPE_I386:
Jim Ingham28eb5712012-10-12 17:34:26 +00004312 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
4313 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004314
Charles Davis510938e2013-08-27 05:04:57 +00004315 case llvm::MachO::CPU_TYPE_X86_64:
Jim Ingham28eb5712012-10-12 17:34:26 +00004316 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
4317 break;
4318 }
Greg Claytona1743492012-03-13 23:14:29 +00004319 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004320 }
4321 return reg_ctx_sp;
4322}
4323
Greg Claytonc9660542012-02-05 02:38:54 +00004324
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004325ObjectFile::Type
4326ObjectFileMachO::CalculateType()
4327{
4328 switch (m_header.filetype)
4329 {
Charles Davis510938e2013-08-27 05:04:57 +00004330 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004331 if (GetAddressByteSize () == 4)
4332 {
4333 // 32 bit kexts are just object files, but they do have a valid
4334 // UUID load command.
4335 UUID uuid;
4336 if (GetUUID(&uuid))
4337 {
4338 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004339 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004340 // "OSKextGetCurrentIdentifier" as this is required of kexts
4341 if (m_strata == eStrataInvalid)
4342 m_strata = eStrataKernel;
4343 return eTypeSharedLibrary;
4344 }
4345 }
4346 return eTypeObjectFile;
4347
Charles Davis510938e2013-08-27 05:04:57 +00004348 case MH_EXECUTE: return eTypeExecutable; // 0x2u
4349 case MH_FVMLIB: return eTypeSharedLibrary; // 0x3u
4350 case MH_CORE: return eTypeCoreFile; // 0x4u
4351 case MH_PRELOAD: return eTypeSharedLibrary; // 0x5u
4352 case MH_DYLIB: return eTypeSharedLibrary; // 0x6u
4353 case MH_DYLINKER: return eTypeDynamicLinker; // 0x7u
4354 case MH_BUNDLE: return eTypeSharedLibrary; // 0x8u
4355 case MH_DYLIB_STUB: return eTypeStubLibrary; // 0x9u
4356 case MH_DSYM: return eTypeDebugInfo; // 0xAu
4357 case MH_KEXT_BUNDLE: return eTypeSharedLibrary; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004358 default:
4359 break;
4360 }
4361 return eTypeUnknown;
4362}
4363
4364ObjectFile::Strata
4365ObjectFileMachO::CalculateStrata()
4366{
4367 switch (m_header.filetype)
4368 {
Charles Davis510938e2013-08-27 05:04:57 +00004369 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004370 {
4371 // 32 bit kexts are just object files, but they do have a valid
4372 // UUID load command.
4373 UUID uuid;
4374 if (GetUUID(&uuid))
4375 {
4376 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004377 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004378 // "OSKextGetCurrentIdentifier" as this is required of kexts
4379 if (m_type == eTypeInvalid)
4380 m_type = eTypeSharedLibrary;
4381
4382 return eStrataKernel;
4383 }
4384 }
4385 return eStrataUnknown;
4386
Charles Davis510938e2013-08-27 05:04:57 +00004387 case MH_EXECUTE: // 0x2u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004388 // Check for the MH_DYLDLINK bit in the flags
Charles Davis510938e2013-08-27 05:04:57 +00004389 if (m_header.flags & MH_DYLDLINK)
Sean Callanan49bce8e2012-02-10 20:22:35 +00004390 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004391 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00004392 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004393 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00004394 {
4395 SectionList *section_list = GetSectionList();
4396 if (section_list)
4397 {
4398 static ConstString g_kld_section_name ("__KLD");
4399 if (section_list->FindSectionByName(g_kld_section_name))
4400 return eStrataKernel;
4401 }
4402 }
4403 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004404
Charles Davis510938e2013-08-27 05:04:57 +00004405 case MH_FVMLIB: return eStrataUser; // 0x3u
4406 case MH_CORE: return eStrataUnknown; // 0x4u
4407 case MH_PRELOAD: return eStrataRawImage; // 0x5u
4408 case MH_DYLIB: return eStrataUser; // 0x6u
4409 case MH_DYLINKER: return eStrataUser; // 0x7u
4410 case MH_BUNDLE: return eStrataUser; // 0x8u
4411 case MH_DYLIB_STUB: return eStrataUser; // 0x9u
4412 case MH_DSYM: return eStrataUnknown; // 0xAu
4413 case MH_KEXT_BUNDLE: return eStrataKernel; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004414 default:
4415 break;
4416 }
4417 return eStrataUnknown;
4418}
4419
4420
Greg Claytonc2ff9312012-02-22 19:41:02 +00004421uint32_t
4422ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4423{
Greg Claytona1743492012-03-13 23:14:29 +00004424 ModuleSP module_sp(GetModule());
4425 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004426 {
Greg Claytona1743492012-03-13 23:14:29 +00004427 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4428 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004429 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004430 uint32_t version_cmd = 0;
4431 uint64_t version = 0;
4432 uint32_t i;
4433 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004434 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004435 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004436 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4437 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004438
Charles Davis510938e2013-08-27 05:04:57 +00004439 if (load_cmd.cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004440 {
Greg Claytona1743492012-03-13 23:14:29 +00004441 if (version_cmd == 0)
4442 {
4443 version_cmd = load_cmd.cmd;
4444 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4445 break;
4446 version = load_cmd.dylib.current_version;
4447 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004448 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00004449 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00004450 }
Greg Claytona1743492012-03-13 23:14:29 +00004451 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004452 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004453
Charles Davis510938e2013-08-27 05:04:57 +00004454 if (version_cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004455 {
Greg Claytona1743492012-03-13 23:14:29 +00004456 if (versions != NULL && num_versions > 0)
4457 {
4458 if (num_versions > 0)
4459 versions[0] = (version & 0xFFFF0000ull) >> 16;
4460 if (num_versions > 1)
4461 versions[1] = (version & 0x0000FF00ull) >> 8;
4462 if (num_versions > 2)
4463 versions[2] = (version & 0x000000FFull);
4464 // Fill in an remaining version numbers with invalid values
4465 for (i=3; i<num_versions; ++i)
4466 versions[i] = UINT32_MAX;
4467 }
4468 // The LC_ID_DYLIB load command has a version with 3 version numbers
4469 // in it, so always return 3
4470 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004471 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00004472 }
4473 return false;
4474}
4475
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004476bool
Greg Clayton514487e2011-02-15 21:59:32 +00004477ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004478{
Greg Claytona1743492012-03-13 23:14:29 +00004479 ModuleSP module_sp(GetModule());
4480 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00004481 {
Greg Claytona1743492012-03-13 23:14:29 +00004482 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4483 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +00004484
Greg Claytona1743492012-03-13 23:14:29 +00004485 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda4e7511e2013-03-06 23:19:17 +00004486 // debugs at the addresses in the file itself. Below we set the OS to
Greg Claytona1743492012-03-13 23:14:29 +00004487 // unknown to make sure we use the DynamicLoaderStatic()...
Charles Davis510938e2013-08-27 05:04:57 +00004488 if (m_header.filetype == MH_PRELOAD)
Greg Claytona1743492012-03-13 23:14:29 +00004489 {
4490 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4491 }
4492 return true;
Greg Clayton593577a2011-09-21 03:57:31 +00004493 }
Greg Claytona1743492012-03-13 23:14:29 +00004494 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004495}
4496
4497
Jason Molenda0e0954c2013-04-16 06:24:42 +00004498UUID
4499ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4500{
4501 UUID uuid;
4502 if (process)
4503 {
4504 addr_t all_image_infos = process->GetImageInfoAddress();
4505
4506 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4507 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4508 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4509 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4510
4511 Error err;
4512 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4513 if (version_or_magic != -1
Charles Davis510938e2013-08-27 05:04:57 +00004514 && version_or_magic != MH_MAGIC
4515 && version_or_magic != MH_CIGAM
4516 && version_or_magic != MH_MAGIC_64
4517 && version_or_magic != MH_CIGAM_64
Jason Molenda0e0954c2013-04-16 06:24:42 +00004518 && version_or_magic >= 13)
4519 {
4520 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4521 int wordsize = process->GetAddressByteSize();
4522 if (wordsize == 8)
4523 {
4524 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4525 }
4526 if (wordsize == 4)
4527 {
4528 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4529 }
4530 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4531 {
4532 uuid_t shared_cache_uuid;
4533 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4534 {
4535 uuid.SetBytes (shared_cache_uuid);
4536 }
4537 }
4538 }
4539 }
4540 return uuid;
4541}
4542
4543UUID
4544ObjectFileMachO::GetLLDBSharedCacheUUID ()
4545{
4546 UUID uuid;
4547#if defined (__APPLE__) && defined (__arm__)
4548 uint8_t *(*dyld_get_all_image_infos)(void);
4549 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4550 if (dyld_get_all_image_infos)
4551 {
4552 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4553 if (dyld_all_image_infos_address)
4554 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00004555 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4556 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00004557 {
Jason Molenda42b69fa2013-04-16 22:56:17 +00004558 uuid_t *sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84); // sharedCacheUUID <mach-o/dyld_images.h>
Jason Molenda0e0954c2013-04-16 06:24:42 +00004559 uuid.SetBytes (sharedCacheUUID_address);
4560 }
4561 }
4562 }
4563#endif
4564 return uuid;
4565}
4566
Greg Clayton9b234982013-10-24 22:54:08 +00004567uint32_t
4568ObjectFileMachO::GetMinimumOSVersion (uint32_t *versions, uint32_t num_versions)
4569{
4570 if (m_min_os_versions.empty())
4571 {
4572 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
4573 bool success = false;
4574 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
4575 {
4576 const lldb::offset_t load_cmd_offset = offset;
4577
4578 version_min_command lc;
4579 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
4580 break;
4581 if (lc.cmd == LC_VERSION_MIN_MACOSX || lc.cmd == LC_VERSION_MIN_IPHONEOS)
4582 {
4583 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
4584 {
4585 const uint32_t xxxx = lc.version >> 16;
4586 const uint32_t yy = (lc.version >> 8) & 0xffu;
4587 const uint32_t zz = lc.version & 0xffu;
4588 if (xxxx)
4589 {
4590 m_min_os_versions.push_back(xxxx);
4591 if (yy)
4592 {
4593 m_min_os_versions.push_back(yy);
4594 if (zz)
4595 m_min_os_versions.push_back(zz);
4596 }
4597 }
4598 success = true;
4599 }
4600 }
4601 offset = load_cmd_offset + lc.cmdsize;
4602 }
4603
4604 if (success == false)
4605 {
4606 // Push an invalid value so we don't keep trying to
4607 m_min_os_versions.push_back(UINT32_MAX);
4608 }
4609 }
4610
4611 if (m_min_os_versions.size() > 1 || m_min_os_versions[0] != UINT32_MAX)
4612 {
4613 if (versions != NULL && num_versions > 0)
4614 {
4615 for (size_t i=0; i<num_versions; ++i)
4616 {
4617 if (i < m_min_os_versions.size())
4618 versions[i] = m_min_os_versions[i];
4619 else
4620 versions[i] = 0;
4621 }
4622 }
4623 return m_min_os_versions.size();
4624 }
4625 // Call the superclasses version that will empty out the data
4626 return ObjectFile::GetMinimumOSVersion (versions, num_versions);
4627}
4628
4629uint32_t
4630ObjectFileMachO::GetSDKVersion(uint32_t *versions, uint32_t num_versions)
4631{
4632 if (m_sdk_versions.empty())
4633 {
4634 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
4635 bool success = false;
4636 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
4637 {
4638 const lldb::offset_t load_cmd_offset = offset;
4639
4640 version_min_command lc;
4641 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
4642 break;
4643 if (lc.cmd == LC_VERSION_MIN_MACOSX || lc.cmd == LC_VERSION_MIN_IPHONEOS)
4644 {
4645 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
4646 {
4647 const uint32_t xxxx = lc.reserved >> 16;
4648 const uint32_t yy = (lc.reserved >> 8) & 0xffu;
4649 const uint32_t zz = lc.reserved & 0xffu;
4650 if (xxxx)
4651 {
4652 m_sdk_versions.push_back(xxxx);
4653 if (yy)
4654 {
4655 m_sdk_versions.push_back(yy);
4656 if (zz)
4657 m_sdk_versions.push_back(zz);
4658 }
4659 }
4660 success = true;
4661 }
4662 }
4663 offset = load_cmd_offset + lc.cmdsize;
4664 }
4665
4666 if (success == false)
4667 {
4668 // Push an invalid value so we don't keep trying to
4669 m_sdk_versions.push_back(UINT32_MAX);
4670 }
4671 }
4672
4673 if (m_sdk_versions.size() > 1 || m_sdk_versions[0] != UINT32_MAX)
4674 {
4675 if (versions != NULL && num_versions > 0)
4676 {
4677 for (size_t i=0; i<num_versions; ++i)
4678 {
4679 if (i < m_sdk_versions.size())
4680 versions[i] = m_sdk_versions[i];
4681 else
4682 versions[i] = 0;
4683 }
4684 }
4685 return m_sdk_versions.size();
4686 }
4687 // Call the superclasses version that will empty out the data
4688 return ObjectFile::GetSDKVersion (versions, num_versions);
4689}
4690
Jason Molenda0e0954c2013-04-16 06:24:42 +00004691
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004692//------------------------------------------------------------------
4693// PluginInterface protocol
4694//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00004695lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004696ObjectFileMachO::GetPluginName()
4697{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004698 return GetPluginNameStatic();
4699}
4700
4701uint32_t
4702ObjectFileMachO::GetPluginVersion()
4703{
4704 return 1;
4705}
4706
Greg Clayton7524e092014-02-06 20:10:16 +00004707
4708bool
4709ObjectFileMachO::SetLoadAddress(Target &target, addr_t base_addr)
4710{
4711 bool changed = false;
4712 ModuleSP module_sp = GetModule();
4713 if (module_sp)
4714 {
4715 size_t num_loaded_sections = 0;
4716 SectionList *section_list = GetSectionList ();
4717 if (section_list)
4718 {
4719 lldb::addr_t mach_base_file_addr = LLDB_INVALID_ADDRESS;
4720 const size_t num_sections = section_list->GetSize();
4721
4722 // First find the address of the mach header which is the first non-zero
4723 // file sized section whose file offset is zero as this will be subtracted
4724 // from each other valid section's vmaddr and then get "base_addr" added to
4725 // it when loading the module in the target
4726 for (size_t sect_idx = 0;
4727 sect_idx < num_sections && mach_base_file_addr == LLDB_INVALID_ADDRESS;
4728 ++sect_idx)
4729 {
4730 // Iterate through the object file sections to find all
4731 // of the sections that size on disk (to avoid __PAGEZERO)
4732 // and load them
4733 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
4734 if (section && section->GetFileSize() > 0 && section->GetFileOffset() == 0)
4735 {
4736 mach_base_file_addr = section->GetFileAddress();
4737 }
4738 }
4739
4740 if (mach_base_file_addr != LLDB_INVALID_ADDRESS)
4741 {
4742 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
4743 {
4744 // Iterate through the object file sections to find all
4745 // of the sections that size on disk (to avoid __PAGEZERO)
4746 // and load them
4747 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
4748 if (section_sp && section_sp->GetFileSize() > 0 && !section_sp->IsThreadSpecific())
4749 {
4750 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() - mach_base_file_addr + base_addr))
4751 ++num_loaded_sections;
4752 }
4753 }
4754 }
4755 }
4756 changed = num_loaded_sections > 0;
4757 return num_loaded_sections > 0;
4758 }
4759 return changed;
4760}
4761