blob: 9f1d154c5e552c6376f4d960d09c5b76a6c1e63d [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 Clayton26b47e22012-04-18 05:19:20 +000037#include "lldb/Target/Target.h"
Greg Claytonc859e2d2012-02-13 23:10:39 +000038#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
39#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Claytonc3776bf2012-02-09 06:16:32 +000040#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041
Jason Molenda0e0954c2013-04-16 06:24:42 +000042#if defined (__APPLE__) && defined (__arm__)
43// GetLLDBSharedCacheUUID() needs to call dlsym()
44#include <dlfcn.h>
45#endif
46
Daniel Maleaffeb4b62013-04-17 19:24:22 +000047#ifndef __APPLE__
48#include "Utility/UuidCompatibility.h"
49#endif
50
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051using namespace lldb;
52using namespace lldb_private;
Greg Claytone1a916a2010-07-21 22:12:05 +000053using namespace llvm::MachO;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
Jason Molenda4e7511e2013-03-06 23:19:17 +000055class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
Greg Claytonc3776bf2012-02-09 06:16:32 +000056{
57public:
58 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
59 RegisterContextDarwin_x86_64 (thread, 0)
60 {
61 SetRegisterDataFrom_LC_THREAD (data);
62 }
63
64 virtual void
65 InvalidateAllRegisters ()
66 {
67 // Do nothing... registers are always valid...
68 }
69
70 void
71 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
72 {
Greg Claytonc7bece562013-01-25 18:06:21 +000073 lldb::offset_t offset = 0;
Greg Claytonc3776bf2012-02-09 06:16:32 +000074 SetError (GPRRegSet, Read, -1);
75 SetError (FPURegSet, Read, -1);
76 SetError (EXCRegSet, Read, -1);
Greg Claytonc859e2d2012-02-13 23:10:39 +000077 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +000078
Greg Claytonc859e2d2012-02-13 23:10:39 +000079 while (!done)
Greg Claytonc3776bf2012-02-09 06:16:32 +000080 {
Greg Claytonc859e2d2012-02-13 23:10:39 +000081 int flavor = data.GetU32 (&offset);
82 if (flavor == 0)
83 done = true;
84 else
Greg Claytonc3776bf2012-02-09 06:16:32 +000085 {
Greg Claytonc859e2d2012-02-13 23:10:39 +000086 uint32_t i;
87 uint32_t count = data.GetU32 (&offset);
88 switch (flavor)
89 {
90 case GPRRegSet:
91 for (i=0; i<count; ++i)
92 (&gpr.rax)[i] = data.GetU64(&offset);
93 SetError (GPRRegSet, Read, 0);
94 done = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +000095
Greg Claytonc859e2d2012-02-13 23:10:39 +000096 break;
97 case FPURegSet:
98 // TODO: fill in FPU regs....
99 //SetError (FPURegSet, Read, -1);
100 done = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000101
Greg Claytonc859e2d2012-02-13 23:10:39 +0000102 break;
103 case EXCRegSet:
104 exc.trapno = data.GetU32(&offset);
105 exc.err = data.GetU32(&offset);
106 exc.faultvaddr = data.GetU64(&offset);
107 SetError (EXCRegSet, Read, 0);
108 done = true;
109 break;
110 case 7:
111 case 8:
112 case 9:
113 // fancy flavors that encapsulate of the the above
114 // falvors...
115 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000116
Greg Claytonc859e2d2012-02-13 23:10:39 +0000117 default:
118 done = true;
119 break;
120 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000121 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000122 }
123 }
124protected:
125 virtual int
126 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
127 {
128 return 0;
129 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000130
Greg Claytonc859e2d2012-02-13 23:10:39 +0000131 virtual int
132 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
133 {
134 return 0;
135 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000136
Greg Claytonc859e2d2012-02-13 23:10:39 +0000137 virtual int
138 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
139 {
140 return 0;
141 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000142
Greg Claytonc859e2d2012-02-13 23:10:39 +0000143 virtual int
144 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
145 {
146 return 0;
147 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000148
Greg Claytonc859e2d2012-02-13 23:10:39 +0000149 virtual int
150 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
151 {
152 return 0;
153 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000154
Greg Claytonc859e2d2012-02-13 23:10:39 +0000155 virtual int
156 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
157 {
158 return 0;
159 }
160};
Greg Claytonc3776bf2012-02-09 06:16:32 +0000161
Greg Claytonc859e2d2012-02-13 23:10:39 +0000162
Jason Molenda4e7511e2013-03-06 23:19:17 +0000163class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
Greg Claytonc859e2d2012-02-13 23:10:39 +0000164{
165public:
166 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
167 RegisterContextDarwin_i386 (thread, 0)
168 {
169 SetRegisterDataFrom_LC_THREAD (data);
170 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000171
Greg Claytonc859e2d2012-02-13 23:10:39 +0000172 virtual void
173 InvalidateAllRegisters ()
174 {
175 // Do nothing... registers are always valid...
176 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000177
Greg Claytonc859e2d2012-02-13 23:10:39 +0000178 void
179 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
180 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000181 lldb::offset_t offset = 0;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000182 SetError (GPRRegSet, Read, -1);
183 SetError (FPURegSet, Read, -1);
184 SetError (EXCRegSet, Read, -1);
185 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000186
Greg Claytonc859e2d2012-02-13 23:10:39 +0000187 while (!done)
188 {
189 int flavor = data.GetU32 (&offset);
190 if (flavor == 0)
191 done = true;
192 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000193 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000194 uint32_t i;
195 uint32_t count = data.GetU32 (&offset);
196 switch (flavor)
197 {
198 case GPRRegSet:
199 for (i=0; i<count; ++i)
200 (&gpr.eax)[i] = data.GetU32(&offset);
201 SetError (GPRRegSet, Read, 0);
202 done = true;
203
204 break;
205 case FPURegSet:
206 // TODO: fill in FPU regs....
207 //SetError (FPURegSet, Read, -1);
208 done = true;
209
210 break;
211 case EXCRegSet:
212 exc.trapno = data.GetU32(&offset);
213 exc.err = data.GetU32(&offset);
214 exc.faultvaddr = data.GetU32(&offset);
215 SetError (EXCRegSet, Read, 0);
216 done = true;
217 break;
218 case 7:
219 case 8:
220 case 9:
221 // fancy flavors that encapsulate of the the above
222 // falvors...
223 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000224
Greg Claytonc859e2d2012-02-13 23:10:39 +0000225 default:
226 done = true;
227 break;
228 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000229 }
230 }
231 }
232protected:
233 virtual int
234 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
235 {
236 return 0;
237 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000238
Greg Claytonc3776bf2012-02-09 06:16:32 +0000239 virtual int
240 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
241 {
242 return 0;
243 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000244
Greg Claytonc3776bf2012-02-09 06:16:32 +0000245 virtual int
246 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
247 {
248 return 0;
249 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000250
Greg Claytonc3776bf2012-02-09 06:16:32 +0000251 virtual int
252 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
253 {
254 return 0;
255 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000256
Greg Claytonc3776bf2012-02-09 06:16:32 +0000257 virtual int
258 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
259 {
260 return 0;
261 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000262
Greg Claytonc3776bf2012-02-09 06:16:32 +0000263 virtual int
264 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
265 {
266 return 0;
267 }
268};
269
Jason Molenda4e7511e2013-03-06 23:19:17 +0000270class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Claytonc859e2d2012-02-13 23:10:39 +0000271{
272public:
273 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonc2807462012-10-30 23:57:32 +0000274 RegisterContextDarwin_arm (thread, 0)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000275 {
276 SetRegisterDataFrom_LC_THREAD (data);
277 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000278
Greg Claytonc859e2d2012-02-13 23:10:39 +0000279 virtual void
280 InvalidateAllRegisters ()
281 {
282 // Do nothing... registers are always valid...
283 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000284
Greg Claytonc859e2d2012-02-13 23:10:39 +0000285 void
286 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
287 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000288 lldb::offset_t offset = 0;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000289 SetError (GPRRegSet, Read, -1);
290 SetError (FPURegSet, Read, -1);
291 SetError (EXCRegSet, Read, -1);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000292 bool done = false;
293
294 while (!done)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000295 {
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000296 int flavor = data.GetU32 (&offset);
297 uint32_t count = data.GetU32 (&offset);
Jason Molendaddf91772013-05-14 04:50:47 +0000298 lldb::offset_t next_thread_state = offset + (count * 4);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000299 switch (flavor)
300 {
301 case GPRRegSet:
302 for (uint32_t i=0; i<count; ++i)
Jason Molendaddf91772013-05-14 04:50:47 +0000303 {
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000304 gpr.r[i] = data.GetU32(&offset);
Jason Molendaddf91772013-05-14 04:50:47 +0000305 }
306
307 // Note that gpr.cpsr is also copied by the above loop; this loop technically extends
308 // one element past the end of the gpr.r[] array.
309
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000310 SetError (GPRRegSet, Read, 0);
Jason Molendaddf91772013-05-14 04:50:47 +0000311 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000312 break;
313
314 case FPURegSet:
315 {
Jason Molenda663d2e12013-05-14 03:52:22 +0000316 uint8_t *fpu_reg_buf = (uint8_t*) &fpu.floats.s[0];
317 const int fpu_reg_buf_size = sizeof (fpu.floats);
318 if (data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000319 {
Jason Molenda663d2e12013-05-14 03:52:22 +0000320 offset += fpu_reg_buf_size;
321 fpu.fpscr = data.GetU32(&offset);
322 SetError (FPURegSet, Read, 0);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000323 }
Jason Molenda663d2e12013-05-14 03:52:22 +0000324 else
325 {
326 done = true;
327 }
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000328 }
Jason Molendaddf91772013-05-14 04:50:47 +0000329 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000330 break;
331
332 case EXCRegSet:
Jason Molendaddf91772013-05-14 04:50:47 +0000333 if (count == 3)
334 {
335 exc.exception = data.GetU32(&offset);
336 exc.fsr = data.GetU32(&offset);
337 exc.far = data.GetU32(&offset);
338 SetError (EXCRegSet, Read, 0);
339 }
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000340 done = true;
Jason Molendaddf91772013-05-14 04:50:47 +0000341 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000342 break;
343
344 // Unknown register set flavor, stop trying to parse.
345 default:
346 done = true;
347 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000348 }
349 }
350protected:
351 virtual int
352 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
353 {
Jason Molendaddf91772013-05-14 04:50:47 +0000354 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000355 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000356
Greg Claytonc859e2d2012-02-13 23:10:39 +0000357 virtual int
358 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
359 {
Jason Molendaddf91772013-05-14 04:50:47 +0000360 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000361 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000362
Greg Claytonc859e2d2012-02-13 23:10:39 +0000363 virtual int
364 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
365 {
Jason Molendaddf91772013-05-14 04:50:47 +0000366 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000367 }
Greg Claytonc2807462012-10-30 23:57:32 +0000368
369 virtual int
370 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
371 {
372 return -1;
373 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000374
Greg Claytonc859e2d2012-02-13 23:10:39 +0000375 virtual int
376 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
377 {
378 return 0;
379 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000380
Greg Claytonc859e2d2012-02-13 23:10:39 +0000381 virtual int
382 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
383 {
384 return 0;
385 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000386
Greg Claytonc859e2d2012-02-13 23:10:39 +0000387 virtual int
388 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
389 {
390 return 0;
391 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000392
Greg Claytonc2807462012-10-30 23:57:32 +0000393 virtual int
394 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
395 {
396 return -1;
397 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000398};
399
Greg Clayton9aae0a12013-05-15 19:52:08 +0000400static uint32_t
401MachHeaderSizeFromMagic(uint32_t magic)
402{
403 switch (magic)
404 {
405 case HeaderMagic32:
406 case HeaderMagic32Swapped:
407 return sizeof(struct mach_header);
408
409 case HeaderMagic64:
410 case HeaderMagic64Swapped:
411 return sizeof(struct mach_header_64);
412 break;
413
414 default:
415 break;
416 }
417 return 0;
418}
419
Greg Claytonded470d2011-03-19 01:12:21 +0000420#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421
422void
423ObjectFileMachO::Initialize()
424{
425 PluginManager::RegisterPlugin (GetPluginNameStatic(),
426 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +0000427 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000428 CreateMemoryInstance,
429 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430}
431
432void
433ObjectFileMachO::Terminate()
434{
435 PluginManager::UnregisterPlugin (CreateInstance);
436}
437
438
Greg Clayton57abc5d2013-05-10 21:47:16 +0000439lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440ObjectFileMachO::GetPluginNameStatic()
441{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000442 static ConstString g_name("mach-o");
443 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444}
445
446const char *
447ObjectFileMachO::GetPluginDescriptionStatic()
448{
449 return "Mach-o object file reader (32 and 64 bit)";
450}
451
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452ObjectFile *
Greg Clayton5ce9c562013-02-06 17:22:03 +0000453ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
454 DataBufferSP& data_sp,
455 lldb::offset_t data_offset,
456 const FileSpec* file,
457 lldb::offset_t file_offset,
458 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000460 if (!data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000462 data_sp = file->MemoryMapFileContents(file_offset, length);
463 data_offset = 0;
464 }
465
466 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
467 {
468 // Update the data to contain the entire file if it doesn't already
469 if (data_sp->GetByteSize() < length)
470 {
471 data_sp = file->MemoryMapFileContents(file_offset, length);
472 data_offset = 0;
473 }
Greg Clayton7b0992d2013-04-18 22:45:39 +0000474 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 +0000475 if (objfile_ap.get() && objfile_ap->ParseHeader())
476 return objfile_ap.release();
477 }
478 return NULL;
479}
480
Greg Claytonc9660542012-02-05 02:38:54 +0000481ObjectFile *
Jason Molenda4e7511e2013-03-06 23:19:17 +0000482ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
483 DataBufferSP& data_sp,
484 const ProcessSP &process_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000485 lldb::addr_t header_addr)
486{
487 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
488 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000489 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonc9660542012-02-05 02:38:54 +0000490 if (objfile_ap.get() && objfile_ap->ParseHeader())
491 return objfile_ap.release();
492 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000493 return NULL;
Greg Claytonc9660542012-02-05 02:38:54 +0000494}
495
Greg Claytonf4d6de62013-04-24 22:29:28 +0000496size_t
497ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
498 lldb::DataBufferSP& data_sp,
499 lldb::offset_t data_offset,
500 lldb::offset_t file_offset,
501 lldb::offset_t length,
502 lldb_private::ModuleSpecList &specs)
503{
504 const size_t initial_count = specs.GetSize();
505
506 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
507 {
508 DataExtractor data;
509 data.SetData(data_sp);
510 llvm::MachO::mach_header header;
511 if (ParseHeader (data, &data_offset, header))
512 {
513 if (header.sizeofcmds >= data_sp->GetByteSize())
514 {
515 data_sp = file.ReadFileContents(file_offset, header.sizeofcmds);
Greg Clayton2540a8a2013-07-12 22:07:46 +0000516 data.SetData(data_sp);
517 data_offset = MachHeaderSizeFromMagic(header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +0000518 }
519 if (data_sp)
520 {
521 ModuleSpec spec;
522 spec.GetFileSpec() = file;
523 spec.GetArchitecture().SetArchitecture(eArchTypeMachO,
524 header.cputype,
525 header.cpusubtype);
Jason Molendab000e4d2013-08-27 02:22:06 +0000526 if (header.filetype == HeaderFileTypePreloadedExecutable) // 0x5u MH_PRELOAD
527 {
528 // Set OS to "unknown" - this is a standalone binary with no dyld et al
529 spec.GetArchitecture().GetTriple().setOS (llvm::Triple::UnknownOS);
530 }
Greg Claytonf4d6de62013-04-24 22:29:28 +0000531 if (spec.GetArchitecture().IsValid())
532 {
533 GetUUID (header, data, data_offset, spec.GetUUID());
534 specs.Append(spec);
535 }
536 }
537 }
538 }
539 return specs.GetSize() - initial_count;
540}
541
542
Greg Claytonc9660542012-02-05 02:38:54 +0000543
544const ConstString &
545ObjectFileMachO::GetSegmentNameTEXT()
546{
547 static ConstString g_segment_name_TEXT ("__TEXT");
548 return g_segment_name_TEXT;
549}
550
551const ConstString &
552ObjectFileMachO::GetSegmentNameDATA()
553{
554 static ConstString g_segment_name_DATA ("__DATA");
555 return g_segment_name_DATA;
556}
557
558const ConstString &
559ObjectFileMachO::GetSegmentNameOBJC()
560{
561 static ConstString g_segment_name_OBJC ("__OBJC");
562 return g_segment_name_OBJC;
563}
564
565const ConstString &
566ObjectFileMachO::GetSegmentNameLINKEDIT()
567{
568 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
569 return g_section_name_LINKEDIT;
570}
571
572const ConstString &
573ObjectFileMachO::GetSectionNameEHFrame()
574{
575 static ConstString g_section_name_eh_frame ("__eh_frame");
576 return g_section_name_eh_frame;
577}
578
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579bool
Jason Molenda4e7511e2013-03-06 23:19:17 +0000580ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
581 lldb::addr_t data_offset,
Greg Clayton44435ed2012-01-12 05:25:17 +0000582 lldb::addr_t data_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583{
Greg Clayton44435ed2012-01-12 05:25:17 +0000584 DataExtractor data;
585 data.SetData (data_sp, data_offset, data_length);
Greg Claytonc7bece562013-01-25 18:06:21 +0000586 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 uint32_t magic = data.GetU32(&offset);
588 return MachHeaderSizeFromMagic(magic) != 0;
589}
590
591
Greg Clayton5ce9c562013-02-06 17:22:03 +0000592ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
593 DataBufferSP& data_sp,
594 lldb::offset_t data_offset,
595 const FileSpec* file,
596 lldb::offset_t file_offset,
597 lldb::offset_t length) :
598 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000599 m_mach_segments(),
600 m_mach_sections(),
601 m_entry_point_address(),
602 m_thread_context_offsets(),
603 m_thread_context_offsets_valid(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604{
Greg Clayton72b77eb2011-02-04 21:13:05 +0000605 ::memset (&m_header, 0, sizeof(m_header));
606 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607}
608
Greg Claytone72dfb32012-02-24 01:59:29 +0000609ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000610 lldb::DataBufferSP& header_data_sp,
611 const lldb::ProcessSP &process_sp,
612 lldb::addr_t header_addr) :
Greg Claytone72dfb32012-02-24 01:59:29 +0000613 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000614 m_mach_segments(),
615 m_mach_sections(),
616 m_entry_point_address(),
617 m_thread_context_offsets(),
618 m_thread_context_offsets_valid(false)
Greg Claytonc9660542012-02-05 02:38:54 +0000619{
620 ::memset (&m_header, 0, sizeof(m_header));
621 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
622}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623
624ObjectFileMachO::~ObjectFileMachO()
625{
626}
627
Greg Claytonf4d6de62013-04-24 22:29:28 +0000628bool
629ObjectFileMachO::ParseHeader (DataExtractor &data,
630 lldb::offset_t *data_offset_ptr,
631 llvm::MachO::mach_header &header)
632{
633 data.SetByteOrder (lldb::endian::InlHostByteOrder());
634 // Leave magic in the original byte order
635 header.magic = data.GetU32(data_offset_ptr);
636 bool can_parse = false;
637 bool is_64_bit = false;
638 switch (header.magic)
639 {
640 case HeaderMagic32:
641 data.SetByteOrder (lldb::endian::InlHostByteOrder());
642 data.SetAddressByteSize(4);
643 can_parse = true;
644 break;
645
646 case HeaderMagic64:
647 data.SetByteOrder (lldb::endian::InlHostByteOrder());
648 data.SetAddressByteSize(8);
649 can_parse = true;
650 is_64_bit = true;
651 break;
652
653 case HeaderMagic32Swapped:
654 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
655 data.SetAddressByteSize(4);
656 can_parse = true;
657 break;
658
659 case HeaderMagic64Swapped:
660 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
661 data.SetAddressByteSize(8);
662 is_64_bit = true;
663 can_parse = true;
664 break;
665
666 default:
667 break;
668 }
669
670 if (can_parse)
671 {
672 data.GetU32(data_offset_ptr, &header.cputype, 6);
673 if (is_64_bit)
674 *data_offset_ptr += 4;
675 return true;
676 }
677 else
678 {
679 memset(&header, 0, sizeof(header));
680 }
681 return false;
682}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683
684bool
685ObjectFileMachO::ParseHeader ()
686{
Greg Claytona1743492012-03-13 23:14:29 +0000687 ModuleSP module_sp(GetModule());
688 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689 {
Greg Claytona1743492012-03-13 23:14:29 +0000690 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
691 bool can_parse = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000692 lldb::offset_t offset = 0;
Greg Clayton7fb56d02011-02-01 01:31:41 +0000693 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +0000694 // Leave magic in the original byte order
695 m_header.magic = m_data.GetU32(&offset);
696 switch (m_header.magic)
Greg Claytonc9660542012-02-05 02:38:54 +0000697 {
Greg Claytona1743492012-03-13 23:14:29 +0000698 case HeaderMagic32:
699 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
700 m_data.SetAddressByteSize(4);
701 can_parse = true;
702 break;
703
704 case HeaderMagic64:
705 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
706 m_data.SetAddressByteSize(8);
707 can_parse = true;
708 break;
709
710 case HeaderMagic32Swapped:
711 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
712 m_data.SetAddressByteSize(4);
713 can_parse = true;
714 break;
715
716 case HeaderMagic64Swapped:
717 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
718 m_data.SetAddressByteSize(8);
719 can_parse = true;
720 break;
721
722 default:
723 break;
Greg Claytonc9660542012-02-05 02:38:54 +0000724 }
Greg Claytona1743492012-03-13 23:14:29 +0000725
726 if (can_parse)
727 {
728 m_data.GetU32(&offset, &m_header.cputype, 6);
729
730 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +0000731
Greg Claytond97ec1c2012-11-16 21:36:10 +0000732 // Check if the module has a required architecture
733 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callananbf4b7be2012-12-13 22:07:14 +0000734 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Claytond97ec1c2012-11-16 21:36:10 +0000735 return false;
736
Greg Claytona1743492012-03-13 23:14:29 +0000737 if (SetModulesArchitecture (mach_arch))
738 {
739 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
740 if (m_data.GetByteSize() < header_and_lc_size)
741 {
742 DataBufferSP data_sp;
743 ProcessSP process_sp (m_process_wp.lock());
744 if (process_sp)
745 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000746 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Claytona1743492012-03-13 23:14:29 +0000747 }
748 else
749 {
750 // Read in all only the load command data from the file on disk
Greg Clayton5ce9c562013-02-06 17:22:03 +0000751 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Claytona1743492012-03-13 23:14:29 +0000752 if (data_sp->GetByteSize() != header_and_lc_size)
753 return false;
754 }
755 if (data_sp)
756 m_data.SetData (data_sp);
757 }
758 }
759 return true;
760 }
761 else
762 {
763 memset(&m_header, 0, sizeof(struct mach_header));
764 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765 }
766 return false;
767}
768
769
770ByteOrder
771ObjectFileMachO::GetByteOrder () const
772{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773 return m_data.GetByteOrder ();
774}
775
Jim Ingham5aee1622010-08-09 23:31:02 +0000776bool
777ObjectFileMachO::IsExecutable() const
778{
779 return m_header.filetype == HeaderFileTypeExecutable;
780}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781
Greg Claytonc7bece562013-01-25 18:06:21 +0000782uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783ObjectFileMachO::GetAddressByteSize () const
784{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785 return m_data.GetAddressByteSize ();
786}
787
Greg Claytone0d378b2011-03-24 21:19:54 +0000788AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +0000789ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
790{
791 Symtab *symtab = GetSymtab();
792 if (symtab)
793 {
794 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
795 if (symbol)
796 {
Greg Claytone7612132012-03-07 21:03:09 +0000797 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +0000798 {
Greg Claytone7612132012-03-07 21:03:09 +0000799 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +0000800 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +0000801 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000802 const SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000803 switch (section_type)
804 {
805 case eSectionTypeInvalid: return eAddressClassUnknown;
806 case eSectionTypeCode:
807 if (m_header.cputype == llvm::MachO::CPUTypeARM)
808 {
809 // For ARM we have a bit in the n_desc field of the symbol
810 // that tells us ARM/Thumb which is bit 0x0008.
811 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
812 return eAddressClassCodeAlternateISA;
813 }
814 return eAddressClassCode;
815
816 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton5009f9d2011-10-27 17:55:14 +0000817 case eSectionTypeData:
818 case eSectionTypeDataCString:
819 case eSectionTypeDataCStringPointers:
820 case eSectionTypeDataSymbolAddress:
821 case eSectionTypeData4:
822 case eSectionTypeData8:
823 case eSectionTypeData16:
824 case eSectionTypeDataPointers:
825 case eSectionTypeZeroFill:
826 case eSectionTypeDataObjCMessageRefs:
827 case eSectionTypeDataObjCCFStrings:
828 return eAddressClassData;
829 case eSectionTypeDebug:
830 case eSectionTypeDWARFDebugAbbrev:
831 case eSectionTypeDWARFDebugAranges:
832 case eSectionTypeDWARFDebugFrame:
833 case eSectionTypeDWARFDebugInfo:
834 case eSectionTypeDWARFDebugLine:
835 case eSectionTypeDWARFDebugLoc:
836 case eSectionTypeDWARFDebugMacInfo:
837 case eSectionTypeDWARFDebugPubNames:
838 case eSectionTypeDWARFDebugPubTypes:
839 case eSectionTypeDWARFDebugRanges:
840 case eSectionTypeDWARFDebugStr:
841 case eSectionTypeDWARFAppleNames:
842 case eSectionTypeDWARFAppleTypes:
843 case eSectionTypeDWARFAppleNamespaces:
844 case eSectionTypeDWARFAppleObjC:
845 return eAddressClassDebug;
Greg Claytonded470d2011-03-19 01:12:21 +0000846 case eSectionTypeEHFrame: return eAddressClassRuntime;
Michael Sartaina7499c92013-07-01 19:45:50 +0000847 case eSectionTypeELFSymbolTable:
848 case eSectionTypeELFDynamicSymbols:
849 case eSectionTypeELFRelocationEntries:
850 case eSectionTypeELFDynamicLinkInfo:
Greg Claytonded470d2011-03-19 01:12:21 +0000851 case eSectionTypeOther: return eAddressClassUnknown;
852 }
853 }
854 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000855
Greg Claytone0d378b2011-03-24 21:19:54 +0000856 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000857 switch (symbol_type)
858 {
859 case eSymbolTypeAny: return eAddressClassUnknown;
860 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000861
Greg Claytonded470d2011-03-19 01:12:21 +0000862 case eSymbolTypeCode:
863 case eSymbolTypeTrampoline:
Greg Clayton059f7242013-02-27 21:16:04 +0000864 case eSymbolTypeResolver:
Greg Claytonded470d2011-03-19 01:12:21 +0000865 if (m_header.cputype == llvm::MachO::CPUTypeARM)
866 {
867 // For ARM we have a bit in the n_desc field of the symbol
868 // that tells us ARM/Thumb which is bit 0x0008.
869 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
870 return eAddressClassCodeAlternateISA;
871 }
872 return eAddressClassCode;
873
874 case eSymbolTypeData: return eAddressClassData;
875 case eSymbolTypeRuntime: return eAddressClassRuntime;
876 case eSymbolTypeException: return eAddressClassRuntime;
877 case eSymbolTypeSourceFile: return eAddressClassDebug;
878 case eSymbolTypeHeaderFile: return eAddressClassDebug;
879 case eSymbolTypeObjectFile: return eAddressClassDebug;
880 case eSymbolTypeCommonBlock: return eAddressClassDebug;
881 case eSymbolTypeBlock: return eAddressClassDebug;
882 case eSymbolTypeLocal: return eAddressClassData;
883 case eSymbolTypeParam: return eAddressClassData;
884 case eSymbolTypeVariable: return eAddressClassData;
885 case eSymbolTypeVariableType: return eAddressClassDebug;
886 case eSymbolTypeLineEntry: return eAddressClassDebug;
887 case eSymbolTypeLineHeader: return eAddressClassDebug;
888 case eSymbolTypeScopeBegin: return eAddressClassDebug;
889 case eSymbolTypeScopeEnd: return eAddressClassDebug;
890 case eSymbolTypeAdditional: return eAddressClassUnknown;
891 case eSymbolTypeCompiler: return eAddressClassDebug;
892 case eSymbolTypeInstrumentation:return eAddressClassDebug;
893 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +0000894 case eSymbolTypeObjCClass: return eAddressClassRuntime;
895 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
896 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +0000897 }
898 }
899 }
900 return eAddressClassUnknown;
901}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902
903Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +0000904ObjectFileMachO::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000905{
Greg Claytona1743492012-03-13 23:14:29 +0000906 ModuleSP module_sp(GetModule());
907 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908 {
Greg Claytona1743492012-03-13 23:14:29 +0000909 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
910 if (m_symtab_ap.get() == NULL)
911 {
912 m_symtab_ap.reset(new Symtab(this));
913 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton3046e662013-07-10 01:23:25 +0000914 ParseSymtab ();
Greg Claytona1743492012-03-13 23:14:29 +0000915 m_symtab_ap->Finalize ();
916 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000917 }
918 return m_symtab_ap.get();
919}
920
Greg Clayton3046e662013-07-10 01:23:25 +0000921bool
922ObjectFileMachO::IsStripped ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000923{
Greg Clayton3046e662013-07-10 01:23:25 +0000924 if (m_dysymtab.cmd == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925 {
Greg Clayton3046e662013-07-10 01:23:25 +0000926 ModuleSP module_sp(GetModule());
927 if (module_sp)
Greg Claytona1743492012-03-13 23:14:29 +0000928 {
Greg Clayton3046e662013-07-10 01:23:25 +0000929 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
930 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton4d78c402012-05-25 18:09:55 +0000931 {
Greg Clayton3046e662013-07-10 01:23:25 +0000932 const lldb::offset_t load_cmd_offset = offset;
933
934 load_command lc;
935 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
936 break;
937 if (lc.cmd == LoadCommandDynamicSymtabInfo)
Greg Clayton4d78c402012-05-25 18:09:55 +0000938 {
Greg Clayton3046e662013-07-10 01:23:25 +0000939 m_dysymtab.cmd = lc.cmd;
940 m_dysymtab.cmdsize = lc.cmdsize;
941 if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
942 {
943 // Clear m_dysymtab if we were unable to read all items from the load command
944 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
945 }
Greg Clayton4d78c402012-05-25 18:09:55 +0000946 }
Greg Clayton3046e662013-07-10 01:23:25 +0000947 offset = load_cmd_offset + lc.cmdsize;
Greg Clayton4d78c402012-05-25 18:09:55 +0000948 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000949 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000950 }
Greg Clayton3046e662013-07-10 01:23:25 +0000951 if (m_dysymtab.cmd)
952 return m_dysymtab.nlocalsym == 0;
953 return false;
954}
Greg Clayton1eac0c72012-04-24 03:06:13 +0000955
Greg Clayton3046e662013-07-10 01:23:25 +0000956void
957ObjectFileMachO::CreateSections (SectionList &unified_section_list)
958{
959 if (!m_sections_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000960 {
Greg Clayton3046e662013-07-10 01:23:25 +0000961 m_sections_ap.reset(new SectionList());
962
963 const bool is_dsym = (m_header.filetype == HeaderFileTypeDSYM);
964 lldb::user_id_t segID = 0;
965 lldb::user_id_t sectID = 0;
966 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
967 uint32_t i;
968 const bool is_core = GetType() == eTypeCoreFile;
969 //bool dump_sections = false;
970 ModuleSP module_sp (GetModule());
971 // First look up any LC_ENCRYPTION_INFO load commands
972 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
973 EncryptedFileRanges encrypted_file_ranges;
974 encryption_info_command encryption_cmd;
975 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000976 {
Greg Clayton3046e662013-07-10 01:23:25 +0000977 const lldb::offset_t load_cmd_offset = offset;
978 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
979 break;
980
981 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000982 {
Greg Clayton3046e662013-07-10 01:23:25 +0000983 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000984 {
Greg Clayton3046e662013-07-10 01:23:25 +0000985 if (encryption_cmd.cryptid != 0)
Greg Claytond37d6922013-04-16 16:51:19 +0000986 {
Greg Clayton3046e662013-07-10 01:23:25 +0000987 EncryptedFileRanges::Entry entry;
988 entry.SetRangeBase(encryption_cmd.cryptoff);
989 entry.SetByteSize(encryption_cmd.cryptsize);
990 encrypted_file_ranges.Append(entry);
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000991 }
992 }
Greg Clayton3046e662013-07-10 01:23:25 +0000993 }
994 offset = load_cmd_offset + encryption_cmd.cmdsize;
995 }
996
997 offset = MachHeaderSizeFromMagic(m_header.magic);
998
999 struct segment_command_64 load_cmd;
1000 for (i=0; i<m_header.ncmds; ++i)
1001 {
1002 const lldb::offset_t load_cmd_offset = offset;
1003 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1004 break;
1005
1006 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
1007 {
1008 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009 {
Greg Clayton3046e662013-07-10 01:23:25 +00001010 bool add_section = true;
1011 bool add_to_unified = true;
1012 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 +00001013
Greg Clayton3046e662013-07-10 01:23:25 +00001014 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1015 if (is_dsym && unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016 {
Greg Clayton3046e662013-07-10 01:23:25 +00001017 if (const_segname == GetSegmentNameLINKEDIT())
1018 {
1019 // We need to keep the __LINKEDIT segment private to this object file only
1020 add_to_unified = false;
1021 }
1022 else
1023 {
1024 // This is the dSYM file and this section has already been created by
1025 // the object file, no need to create it.
1026 add_section = false;
1027 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028 }
Greg Clayton3046e662013-07-10 01:23:25 +00001029 load_cmd.vmaddr = m_data.GetAddress(&offset);
1030 load_cmd.vmsize = m_data.GetAddress(&offset);
1031 load_cmd.fileoff = m_data.GetAddress(&offset);
1032 load_cmd.filesize = m_data.GetAddress(&offset);
1033 if (m_length != 0 && load_cmd.filesize != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001034 {
Greg Clayton3046e662013-07-10 01:23:25 +00001035 if (load_cmd.fileoff > m_length)
1036 {
1037 // We have a load command that says it extends past the end of hte file. This is likely
1038 // a corrupt file. We don't have any way to return an error condition here (this method
1039 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1040 // is null out the SectionList vector and if a process has been set up, dump a message
1041 // to stdout. The most common case here is core file debugging with a truncated file.
1042 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda20eb31b2013-08-16 03:20:42 +00001043 module_sp->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 ")",
1044 i,
1045 lc_segment_name,
1046 load_cmd.fileoff,
1047 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001048
1049 load_cmd.fileoff = 0;
1050 load_cmd.filesize = 0;
1051 }
1052
1053 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1054 {
1055 // We have a load command that says it extends past the end of hte file. This is likely
1056 // a corrupt file. We don't have any way to return an error condition here (this method
1057 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1058 // is null out the SectionList vector and if a process has been set up, dump a message
1059 // to stdout. The most common case here is core file debugging with a truncated file.
1060 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda20eb31b2013-08-16 03:20:42 +00001061 module_sp->ReportError("is a corrupt mach-o file: 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",
1062 i,
1063 lc_segment_name,
1064 load_cmd.fileoff + load_cmd.filesize,
1065 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001066
1067 // Tuncase the length
1068 load_cmd.filesize = m_length - load_cmd.fileoff;
1069 }
1070 }
1071 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1072 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073
Greg Clayton3046e662013-07-10 01:23:25 +00001074 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075
Greg Clayton3046e662013-07-10 01:23:25 +00001076 // Keep a list of mach segments around in case we need to
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001077 // get at data that isn't stored in the abstracted Sections.
Greg Clayton3046e662013-07-10 01:23:25 +00001078 m_mach_segments.push_back (load_cmd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079
Greg Clayton3046e662013-07-10 01:23:25 +00001080 // Use a segment ID of the segment index shifted left by 8 so they
1081 // never conflict with any of the sections.
1082 SectionSP segment_sp;
1083 if (add_section && (const_segname || is_core))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084 {
Greg Clayton3046e662013-07-10 01:23:25 +00001085 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
1086 this, // Object file to which this sections belongs
1087 ++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
1088 const_segname, // Name of this section
1089 eSectionTypeContainer, // This section is a container of other sections.
1090 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1091 load_cmd.vmsize, // VM size in bytes of this section
1092 load_cmd.fileoff, // Offset to the data for this section in the file
1093 load_cmd.filesize, // Size in bytes of this section as found in the the file
1094 load_cmd.flags)); // Flags for this section
Greg Clayton8d38ac42010-06-28 23:51:11 +00001095
Greg Clayton3046e662013-07-10 01:23:25 +00001096 segment_sp->SetIsEncrypted (segment_is_encrypted);
1097 m_sections_ap->AddSection(segment_sp);
1098 if (add_to_unified)
1099 unified_section_list.AddSection(segment_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100 }
Greg Clayton3046e662013-07-10 01:23:25 +00001101 else if (unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001102 {
Jason Molenda20eb31b2013-08-16 03:20:42 +00001103 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr)
1104 {
1105 // Check to see if the module was read from memory?
1106 if (module_sp->GetObjectFile()->GetHeaderAddress().IsValid())
1107 {
1108 // We have a module that is in memory and needs to have its
1109 // file address adjusted. We need to do this because when we
1110 // load a file from memory, its addresses will be slid already,
1111 // yet the addresses in the new symbol file will still be unslid.
1112 // Since everything is stored as section offset, this shouldn't
1113 // cause any problems.
Jason Molenda5894a732013-08-17 03:39:52 +00001114
1115 // Make sure we've parsed the symbol table from the
1116 // ObjectFile before we go around changing its Sections.
1117 module_sp->GetObjectFile()->GetSymtab();
1118 // eh_frame would present the same problems but we parse that on
1119 // a per-function basis as-needed so it's more difficult to
1120 // remove its use of the Sections. Realistically, the environments
1121 // where this code path will be taken will not have eh_frame sections.
1122
Jason Molenda20eb31b2013-08-16 03:20:42 +00001123 unified_section_sp->SetFileAddress(load_cmd.vmaddr);
1124 }
1125 }
Greg Clayton3046e662013-07-10 01:23:25 +00001126 m_sections_ap->AddSection(unified_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128
Greg Clayton3046e662013-07-10 01:23:25 +00001129 struct section_64 sect64;
1130 ::memset (&sect64, 0, sizeof(sect64));
1131 // Push a section into our mach sections for the section at
1132 // index zero (NListSectionNoSection) if we don't have any
1133 // mach sections yet...
1134 if (m_mach_sections.empty())
1135 m_mach_sections.push_back(sect64);
1136 uint32_t segment_sect_idx;
1137 const lldb::user_id_t first_segment_sectID = sectID + 1;
1138
1139
1140 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
1141 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001142 {
Greg Clayton3046e662013-07-10 01:23:25 +00001143 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
Greg Clayton89411422010-10-08 00:21:05 +00001144 break;
Greg Clayton3046e662013-07-10 01:23:25 +00001145 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1146 break;
1147 sect64.addr = m_data.GetAddress(&offset);
1148 sect64.size = m_data.GetAddress(&offset);
1149
1150 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1151 break;
1152
1153 // Keep a list of mach sections around in case we need to
1154 // get at data that isn't stored in the abstracted Sections.
1155 m_mach_sections.push_back (sect64);
1156
1157 if (add_section)
1158 {
1159 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1160 if (!const_segname)
1161 {
1162 // We have a segment with no name so we need to conjure up
1163 // segments that correspond to the section's segname if there
1164 // isn't already such a section. If there is such a section,
1165 // we resize the section so that it spans all sections.
1166 // We also mark these sections as fake so address matches don't
1167 // hit if they land in the gaps between the child sections.
1168 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1169 segment_sp = unified_section_list.FindSectionByName (const_segname);
1170 if (segment_sp.get())
1171 {
1172 Section *segment = segment_sp.get();
1173 // Grow the section size as needed.
1174 const lldb::addr_t sect64_min_addr = sect64.addr;
1175 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1176 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1177 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1178 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1179 if (sect64_min_addr >= curr_seg_min_addr)
1180 {
1181 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1182 // Only grow the section size if needed
1183 if (new_seg_byte_size > curr_seg_byte_size)
1184 segment->SetByteSize (new_seg_byte_size);
1185 }
1186 else
1187 {
1188 // We need to change the base address of the segment and
1189 // adjust the child section offsets for all existing children.
1190 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1191 segment->Slide(slide_amount, false);
1192 segment->GetChildren().Slide(-slide_amount, false);
1193 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1194 }
1195
1196 // Grow the section size as needed.
1197 if (sect64.offset)
1198 {
1199 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1200 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1201
1202 const lldb::addr_t section_min_file_offset = sect64.offset;
1203 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1204 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1205 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1206 segment->SetFileOffset (new_file_offset);
1207 segment->SetFileSize (new_file_size);
1208 }
1209 }
1210 else
1211 {
1212 // Create a fake section for the section's named segment
1213 segment_sp.reset(new Section (segment_sp, // Parent section
1214 module_sp, // Module to which this section belongs
1215 this, // Object file to which this section belongs
1216 ++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
1217 const_segname, // Name of this section
1218 eSectionTypeContainer, // This section is a container of other sections.
1219 sect64.addr, // File VM address == addresses as they are found in the object file
1220 sect64.size, // VM size in bytes of this section
1221 sect64.offset, // Offset to the data for this section in the file
1222 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1223 load_cmd.flags)); // Flags for this section
1224 segment_sp->SetIsFake(true);
1225
1226 m_sections_ap->AddSection(segment_sp);
1227 if (add_to_unified)
1228 unified_section_list.AddSection(segment_sp);
1229 segment_sp->SetIsEncrypted (segment_is_encrypted);
1230 }
1231 }
1232 assert (segment_sp.get());
1233
1234 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
1235 static ConstString g_sect_name_objc_data ("__objc_data");
1236 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1237 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1238 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1239 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1240 static ConstString g_sect_name_objc_const ("__objc_const");
1241 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1242 static ConstString g_sect_name_cfstring ("__cfstring");
1243
1244 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1245 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1246 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1247 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1248 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1249 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1250 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1251 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1252 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1253 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1254 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1255 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1256 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1257 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1258 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1259 static ConstString g_sect_name_eh_frame ("__eh_frame");
1260 static ConstString g_sect_name_DATA ("__DATA");
1261 static ConstString g_sect_name_TEXT ("__TEXT");
1262
1263 SectionType sect_type = eSectionTypeOther;
1264
1265 if (section_name == g_sect_name_dwarf_debug_abbrev)
1266 sect_type = eSectionTypeDWARFDebugAbbrev;
1267 else if (section_name == g_sect_name_dwarf_debug_aranges)
1268 sect_type = eSectionTypeDWARFDebugAranges;
1269 else if (section_name == g_sect_name_dwarf_debug_frame)
1270 sect_type = eSectionTypeDWARFDebugFrame;
1271 else if (section_name == g_sect_name_dwarf_debug_info)
1272 sect_type = eSectionTypeDWARFDebugInfo;
1273 else if (section_name == g_sect_name_dwarf_debug_line)
1274 sect_type = eSectionTypeDWARFDebugLine;
1275 else if (section_name == g_sect_name_dwarf_debug_loc)
1276 sect_type = eSectionTypeDWARFDebugLoc;
1277 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1278 sect_type = eSectionTypeDWARFDebugMacInfo;
1279 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1280 sect_type = eSectionTypeDWARFDebugPubNames;
1281 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1282 sect_type = eSectionTypeDWARFDebugPubTypes;
1283 else if (section_name == g_sect_name_dwarf_debug_ranges)
1284 sect_type = eSectionTypeDWARFDebugRanges;
1285 else if (section_name == g_sect_name_dwarf_debug_str)
1286 sect_type = eSectionTypeDWARFDebugStr;
1287 else if (section_name == g_sect_name_dwarf_apple_names)
1288 sect_type = eSectionTypeDWARFAppleNames;
1289 else if (section_name == g_sect_name_dwarf_apple_types)
1290 sect_type = eSectionTypeDWARFAppleTypes;
1291 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1292 sect_type = eSectionTypeDWARFAppleNamespaces;
1293 else if (section_name == g_sect_name_dwarf_apple_objc)
1294 sect_type = eSectionTypeDWARFAppleObjC;
1295 else if (section_name == g_sect_name_objc_selrefs)
1296 sect_type = eSectionTypeDataCStringPointers;
1297 else if (section_name == g_sect_name_objc_msgrefs)
1298 sect_type = eSectionTypeDataObjCMessageRefs;
1299 else if (section_name == g_sect_name_eh_frame)
1300 sect_type = eSectionTypeEHFrame;
1301 else if (section_name == g_sect_name_cfstring)
1302 sect_type = eSectionTypeDataObjCCFStrings;
1303 else if (section_name == g_sect_name_objc_data ||
1304 section_name == g_sect_name_objc_classrefs ||
1305 section_name == g_sect_name_objc_superrefs ||
1306 section_name == g_sect_name_objc_const ||
1307 section_name == g_sect_name_objc_classlist)
1308 {
1309 sect_type = eSectionTypeDataPointers;
1310 }
1311
1312 if (sect_type == eSectionTypeOther)
1313 {
1314 switch (mach_sect_type)
1315 {
1316 // TODO: categorize sections by other flags for regular sections
1317 case SectionTypeRegular:
1318 if (segment_sp->GetName() == g_sect_name_TEXT)
1319 sect_type = eSectionTypeCode;
1320 else if (segment_sp->GetName() == g_sect_name_DATA)
1321 sect_type = eSectionTypeData;
1322 else
1323 sect_type = eSectionTypeOther;
1324 break;
1325 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1326 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1327 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1328 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1329 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1330 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1331 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1332 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1333 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1334 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1335 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1336 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1337 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1338 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1339 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1340 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
1341 default: break;
1342 }
1343 }
1344
1345 SectionSP section_sp(new Section (segment_sp,
1346 module_sp,
1347 this,
1348 ++sectID,
1349 section_name,
1350 sect_type,
1351 sect64.addr - segment_sp->GetFileAddress(),
1352 sect64.size,
1353 sect64.offset,
1354 sect64.offset == 0 ? 0 : sect64.size,
1355 sect64.flags));
1356 // Set the section to be encrypted to match the segment
1357
1358 bool section_is_encrypted = false;
1359 if (!segment_is_encrypted && load_cmd.filesize != 0)
1360 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1361
1362 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1363 segment_sp->GetChildren().AddSection(section_sp);
1364
1365 if (segment_sp->IsFake())
1366 {
1367 segment_sp.reset();
1368 const_segname.Clear();
1369 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001370 }
1371 }
Greg Clayton3046e662013-07-10 01:23:25 +00001372 if (segment_sp && is_dsym)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001373 {
Greg Clayton3046e662013-07-10 01:23:25 +00001374 if (first_segment_sectID <= sectID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001375 {
Greg Clayton3046e662013-07-10 01:23:25 +00001376 lldb::user_id_t sect_uid;
1377 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378 {
Greg Clayton3046e662013-07-10 01:23:25 +00001379 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1380 SectionSP next_section_sp;
1381 if (sect_uid + 1 <= sectID)
1382 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1383
1384 if (curr_section_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385 {
Greg Clayton3046e662013-07-10 01:23:25 +00001386 if (curr_section_sp->GetByteSize() == 0)
1387 {
1388 if (next_section_sp.get() != NULL)
1389 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1390 else
1391 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1392 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393 }
1394 }
1395 }
1396 }
1397 }
1398 }
1399 }
Greg Clayton3046e662013-07-10 01:23:25 +00001400 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
1401 {
1402 m_dysymtab.cmd = load_cmd.cmd;
1403 m_dysymtab.cmdsize = load_cmd.cmdsize;
1404 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1405 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406
Greg Clayton3046e662013-07-10 01:23:25 +00001407 offset = load_cmd_offset + load_cmd.cmdsize;
1408 }
1409
1410// StreamFile s(stdout, false); // REMOVE THIS LINE
1411// s.Printf ("Sections for %s:\n", m_file.GetPath().c_str());// REMOVE THIS LINE
1412// m_sections_ap->Dump(&s, NULL, true, UINT32_MAX);// REMOVE THIS LINE
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001413 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414}
1415
1416class MachSymtabSectionInfo
1417{
1418public:
1419
1420 MachSymtabSectionInfo (SectionList *section_list) :
1421 m_section_list (section_list),
1422 m_section_infos()
1423 {
1424 // Get the number of sections down to a depth of 1 to include
1425 // all segments and their sections, but no other sections that
1426 // may be added for debug map or
1427 m_section_infos.resize(section_list->GetNumSections(1));
1428 }
1429
1430
Greg Claytone72dfb32012-02-24 01:59:29 +00001431 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001432 GetSection (uint8_t n_sect, addr_t file_addr)
1433 {
1434 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00001435 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436 if (n_sect < m_section_infos.size())
1437 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001438 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001440 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1441 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001442 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00001443 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001444 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1445 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00001446 }
1447 else
1448 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001449 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00001450 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451 }
1452 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00001453 {
1454 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00001455 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001456 }
1457 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1458 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1459 {
1460 // Symbol is in section with zero size, but has the same start
1461 // address as the section. This can happen with linker symbols
1462 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00001463 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001464 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001465 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001466 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467 }
1468
1469protected:
1470 struct SectionInfo
1471 {
1472 SectionInfo () :
1473 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001474 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001475 {
1476 }
1477
1478 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00001479 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480 };
1481 SectionList *m_section_list;
1482 std::vector<SectionInfo> m_section_infos;
1483};
1484
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001485size_t
Greg Clayton3046e662013-07-10 01:23:25 +00001486ObjectFileMachO::ParseSymtab ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001487{
1488 Timer scoped_timer(__PRETTY_FUNCTION__,
1489 "ObjectFileMachO::ParseSymtab () module = %s",
1490 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001491 ModuleSP module_sp (GetModule());
1492 if (!module_sp)
1493 return 0;
1494
1495 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1496 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1497 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1498 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00001499 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001500 uint32_t i;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001501
Greg Clayton5160ce52013-03-27 23:08:40 +00001502 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton77ccca72011-12-30 00:32:24 +00001503
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001504 for (i=0; i<m_header.ncmds; ++i)
1505 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001506 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001507 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001508 struct load_command lc;
1509 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001510 break;
1511 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001512 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001513 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001514 case LoadCommandSymtab:
1515 symtab_load_command.cmd = lc.cmd;
1516 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001517 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001518 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1519 return 0;
1520 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001521 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001522 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001523 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001524 return 0;
1525 }
1526
1527 if (symtab_load_command.stroff == 0)
1528 {
1529 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001530 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001531 return 0;
1532 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001533
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001534 if (symtab_load_command.nsyms == 0)
1535 {
1536 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001537 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001538 return 0;
1539 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001540
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001541 if (symtab_load_command.strsize == 0)
1542 {
1543 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001544 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001545 return 0;
1546 }
1547 break;
1548
1549 case LoadCommandFunctionStarts:
1550 function_starts_load_command.cmd = lc.cmd;
1551 function_starts_load_command.cmdsize = lc.cmdsize;
1552 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 +00001553 memset (&function_starts_load_command, 0, sizeof(function_starts_load_command));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001554 break;
1555
1556 default:
1557 break;
1558 }
1559 offset = cmd_offset + lc.cmdsize;
1560 }
1561
1562 if (symtab_load_command.cmd)
1563 {
1564 Symtab *symtab = m_symtab_ap.get();
1565 SectionList *section_list = GetSectionList();
1566 if (section_list == NULL)
1567 return 0;
1568
Greg Claytonc7bece562013-01-25 18:06:21 +00001569 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1570 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001571 bool bit_width_32 = addr_byte_size == 4;
1572 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1573
Greg Claytonc7bece562013-01-25 18:06:21 +00001574 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1575 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1576 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00001577 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00001578
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001579 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1580 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00001581 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
Greg Claytonfd814c52013-08-13 01:42:25 +00001582
1583 ProcessSP process_sp (m_process_wp.lock());
1584 Process *process = process_sp.get();
1585
Greg Clayton86eac942013-08-13 21:32:34 +00001586 uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete;
1587
Greg Clayton4c82d422012-05-18 23:20:01 +00001588 if (process)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001589 {
Greg Clayton4c82d422012-05-18 23:20:01 +00001590 Target &target = process->GetTarget();
Greg Claytonfd814c52013-08-13 01:42:25 +00001591
Greg Clayton86eac942013-08-13 21:32:34 +00001592 memory_module_load_level = target.GetMemoryModuleLoadLevel();
Greg Claytonfd814c52013-08-13 01:42:25 +00001593
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001594 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1595 // Reading mach file from memory in a process or core file...
1596
1597 if (linkedit_section_sp)
1598 {
1599 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1600 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1601 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00001602 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00001603
1604 bool data_was_read = false;
1605
1606#if defined (__APPLE__) && defined (__arm__)
1607 if (m_header.flags & 0x80000000u)
Greg Clayton77ccca72011-12-30 00:32:24 +00001608 {
Greg Clayton26b47e22012-04-18 05:19:20 +00001609 // This mach-o memory file is in the dyld shared cache. If this
1610 // program is not remote and this is iOS, then this process will
1611 // share the same shared cache as the process we are debugging and
1612 // we can read the entire __LINKEDIT from the address space in this
1613 // process. This is a needed optimization that is used for local iOS
1614 // debugging only since all shared libraries in the shared cache do
1615 // not have corresponding files that exist in the file system of the
1616 // device. They have been combined into a single file. This means we
1617 // always have to load these files from memory. All of the symbol and
1618 // string tables from all of the __LINKEDIT sections from the shared
1619 // libraries in the shared cache have been merged into a single large
1620 // symbol and string table. Reading all of this symbol and string table
1621 // data across can slow down debug launch times, so we optimize this by
1622 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00001623
1624 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1625 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1626 bool use_lldb_cache = true;
1627 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1628 {
1629 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00001630 ModuleSP module_sp (GetModule());
1631 if (module_sp)
1632 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1633
Jason Molenda0e0954c2013-04-16 06:24:42 +00001634 }
1635
Greg Clayton26b47e22012-04-18 05:19:20 +00001636 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00001637 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00001638 {
1639 data_was_read = true;
1640 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00001641 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00001642 if (function_starts_load_command.cmd)
1643 {
1644 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1645 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1646 }
1647 }
1648 }
1649#endif
1650
1651 if (!data_was_read)
1652 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001653 if (memory_module_load_level == eMemoryModuleLoadLevelComplete)
Jason Molendad34e6522013-02-05 22:31:24 +00001654 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001655 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1656 if (nlist_data_sp)
1657 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
1658 // Load strings individually from memory when loading from memory since shared cache
1659 // string tables contain strings for all symbols from all shared cached libraries
1660 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1661 //if (strtab_data_sp)
1662 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
1663 if (m_dysymtab.nindirectsyms != 0)
1664 {
1665 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1666 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1667 if (indirect_syms_data_sp)
1668 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1669 }
Jason Molendad34e6522013-02-05 22:31:24 +00001670 }
Greg Claytonfd814c52013-08-13 01:42:25 +00001671
1672 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial)
Greg Clayton26b47e22012-04-18 05:19:20 +00001673 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001674 if (function_starts_load_command.cmd)
1675 {
1676 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1677 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1678 if (func_start_data_sp)
1679 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1680 }
Greg Clayton26b47e22012-04-18 05:19:20 +00001681 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001682 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001683 }
1684 }
1685 else
1686 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001687 nlist_data.SetData (m_data,
1688 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001689 nlist_data_byte_size);
1690 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00001691 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001692 strtab_data_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00001693 if (m_dysymtab.nindirectsyms != 0)
1694 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001695 indirect_symbol_index_data.SetData (m_data,
1696 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00001697 m_dysymtab.nindirectsyms * 4);
1698 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001699 if (function_starts_load_command.cmd)
1700 {
1701 function_starts_data.SetData (m_data,
1702 function_starts_load_command.dataoff,
1703 function_starts_load_command.datasize);
1704 }
1705 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001706
Greg Clayton86eac942013-08-13 21:32:34 +00001707 if (nlist_data.GetByteSize() == 0 && memory_module_load_level == eMemoryModuleLoadLevelComplete)
1708 {
1709 if (log)
1710 module_sp->LogMessage(log, "failed to read nlist data");
1711 return 0;
1712 }
1713
1714
Greg Claytondebb8812012-05-25 17:04:00 +00001715 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1716 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00001717 {
Greg Claytondebb8812012-05-25 17:04:00 +00001718 if (process)
1719 {
1720 if (strtab_addr == LLDB_INVALID_ADDRESS)
1721 {
1722 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001723 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00001724 return 0;
1725 }
1726 }
1727 else
Greg Clayton4c82d422012-05-18 23:20:01 +00001728 {
1729 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001730 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00001731 return 0;
1732 }
1733 }
Greg Clayton4c82d422012-05-18 23:20:01 +00001734
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001735 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1736 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1737 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1738 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1739 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1740 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1741 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1742 SectionSP eh_frame_section_sp;
1743 if (text_section_sp.get())
1744 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1745 else
1746 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1747
Greg Clayton29e08cb2012-03-14 01:53:24 +00001748 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molenda5635f772013-03-21 03:36:01 +00001749
1750 // lldb works best if it knows the start addresss of all functions in a module.
1751 // Linker symbols or debug info are normally the best source of information for start addr / size but
1752 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00001753 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00001754 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1755 // binary, relative to the text section.
1756 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1757 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1758 // Binaries built to run on older releases may need to use eh_frame information.
1759
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001760 if (text_section_sp && function_starts_data.GetByteSize())
1761 {
1762 FunctionStarts::Entry function_start_entry;
1763 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00001764 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001765 function_start_entry.addr = text_section_sp->GetFileAddress();
1766 uint64_t delta;
1767 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1768 {
1769 // Now append the current entry
1770 function_start_entry.addr += delta;
1771 function_starts.Append(function_start_entry);
1772 }
Jason Molendad63d3c72013-04-16 00:18:44 +00001773 }
Jason Molenda5635f772013-03-21 03:36:01 +00001774 else
1775 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00001776 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1777 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1778 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1779 // the module.
1780 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00001781 {
1782 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1783 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1784 eh_frame.GetFunctionAddressAndSizeVector (functions);
1785 addr_t text_base_addr = text_section_sp->GetFileAddress();
1786 size_t count = functions.GetSize();
1787 for (size_t i = 0; i < count; ++i)
1788 {
1789 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1790 if (func)
1791 {
1792 FunctionStarts::Entry function_start_entry;
1793 function_start_entry.addr = func->base - text_base_addr;
1794 function_starts.Append(function_start_entry);
1795 }
1796 }
1797 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001798 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001799
Greg Claytonc7bece562013-01-25 18:06:21 +00001800 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001801
Greg Claytonc7bece562013-01-25 18:06:21 +00001802 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001803
Greg Claytonc7bece562013-01-25 18:06:21 +00001804 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001805
1806 uint32_t N_SO_index = UINT32_MAX;
1807
1808 MachSymtabSectionInfo section_info (section_list);
1809 std::vector<uint32_t> N_FUN_indexes;
1810 std::vector<uint32_t> N_NSYM_indexes;
1811 std::vector<uint32_t> N_INCL_indexes;
1812 std::vector<uint32_t> N_BRAC_indexes;
1813 std::vector<uint32_t> N_COMM_indexes;
1814 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1815 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00001816 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001817 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1818 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00001819 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001820 // Any symbols that get merged into another will get an entry
1821 // in this map so we know
1822 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1823 uint32_t nlist_idx = 0;
1824 Symbol *symbol_ptr = NULL;
1825
1826 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00001827 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00001828 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00001829 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00001830 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001831
Jason Molendaa5609c82012-06-21 01:51:02 +00001832#if defined (__APPLE__) && defined (__arm__)
1833
1834 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1835 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1836 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1837 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1838 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1839 // nlist parser to ignore all LOCAL symbols.
1840
1841 if (m_header.flags & 0x80000000u)
1842 {
1843 // Before we can start mapping the DSC, we need to make certain the target process is actually
1844 // using the cache we can find.
1845
Jason Molendaa5609c82012-06-21 01:51:02 +00001846 // Next we need to determine the correct path for the dyld shared cache.
1847
1848 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1849 char dsc_path[PATH_MAX];
1850
1851 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00001852 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1853 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00001854 header_arch.GetArchitectureName());
1855
1856 FileSpec dsc_filespec(dsc_path, false);
1857
1858 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molendad63d3c72013-04-16 00:18:44 +00001859 struct lldb_copy_dyld_cache_header_v0
Greg Clayton946f8902012-09-05 22:30:51 +00001860 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001861 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1862 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1863 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda4e7511e2013-03-06 23:19:17 +00001864 uint32_t imagesOffset;
1865 uint32_t imagesCount;
1866 uint64_t dyldBaseAddress;
1867 uint64_t codeSignatureOffset;
1868 uint64_t codeSignatureSize;
1869 uint64_t slideInfoOffset;
1870 uint64_t slideInfoSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00001871 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1872 uint64_t localSymbolsSize; // size of local symbols information
1873 };
1874 struct lldb_copy_dyld_cache_header_v1
1875 {
1876 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1877 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1878 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1879 uint32_t imagesOffset;
1880 uint32_t imagesCount;
1881 uint64_t dyldBaseAddress;
1882 uint64_t codeSignatureOffset;
1883 uint64_t codeSignatureSize;
1884 uint64_t slideInfoOffset;
1885 uint64_t slideInfoSize;
Jason Molenda4e7511e2013-03-06 23:19:17 +00001886 uint64_t localSymbolsOffset;
1887 uint64_t localSymbolsSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00001888 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Clayton946f8902012-09-05 22:30:51 +00001889 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00001890
Jason Molendad63d3c72013-04-16 00:18:44 +00001891 struct lldb_copy_dyld_cache_mapping_info
1892 {
1893 uint64_t address;
1894 uint64_t size;
1895 uint64_t fileOffset;
1896 uint32_t maxProt;
1897 uint32_t initProt;
1898 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00001899
Greg Clayton946f8902012-09-05 22:30:51 +00001900 struct lldb_copy_dyld_cache_local_symbols_info
1901 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001902 uint32_t nlistOffset;
1903 uint32_t nlistCount;
1904 uint32_t stringsOffset;
1905 uint32_t stringsSize;
1906 uint32_t entriesOffset;
1907 uint32_t entriesCount;
Greg Clayton946f8902012-09-05 22:30:51 +00001908 };
1909 struct lldb_copy_dyld_cache_local_symbols_entry
1910 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001911 uint32_t dylibOffset;
1912 uint32_t nlistStartIndex;
1913 uint32_t nlistCount;
Greg Clayton946f8902012-09-05 22:30:51 +00001914 };
Jason Molendaa5609c82012-06-21 01:51:02 +00001915
Jason Molendaf8130862012-06-22 03:28:35 +00001916 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1917 The dyld_cache_local_symbols_info structure gives us three things:
1918 1. The start and count of the nlist records in the dyld_shared_cache file
1919 2. The start and size of the strings for these nlist records
1920 3. The start and count of dyld_cache_local_symbols_entry entries
1921
1922 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1923 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00001924 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00001925 and the count of how many nlist records there are for this dylib/framework.
1926 */
1927
Jason Molendaa5609c82012-06-21 01:51:02 +00001928 // Process the dsc header to find the unmapped symbols
1929 //
1930 // Save some VM space, do not map the entire cache in one shot.
1931
Jason Molenda255f9bb2013-03-06 23:17:36 +00001932 DataBufferSP dsc_data_sp;
1933 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1934
1935 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00001936 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001937 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00001938
Jason Molenda255f9bb2013-03-06 23:17:36 +00001939 char version_str[17];
1940 int version = -1;
1941 lldb::offset_t offset = 0;
1942 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1943 version_str[16] = '\0';
1944 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1945 {
1946 int v;
1947 if (::sscanf (version_str + 6, "%d", &v) == 1)
1948 {
1949 version = v;
1950 }
1951 }
1952
Jason Molenda0e0954c2013-04-16 06:24:42 +00001953 UUID dsc_uuid;
1954 if (version >= 1)
1955 {
1956 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1957 uint8_t uuid_bytes[sizeof (uuid_t)];
1958 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1959 dsc_uuid.SetBytes (uuid_bytes);
1960 }
1961
1962 bool uuid_match = true;
1963 if (dsc_uuid.IsValid() && process)
1964 {
1965 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1966
1967 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1968 {
1969 // The on-disk dyld_shared_cache file is not the same as the one in this
1970 // process' memory, don't use it.
1971 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00001972 ModuleSP module_sp (GetModule());
1973 if (module_sp)
1974 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 +00001975 }
1976 }
1977
Jason Molenda4e7511e2013-03-06 23:19:17 +00001978 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00001979
Jason Molendaa5609c82012-06-21 01:51:02 +00001980 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1981
1982 // If the mappingOffset points to a location inside the header, we've
1983 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda0e0954c2013-04-16 06:24:42 +00001984 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendaa5609c82012-06-21 01:51:02 +00001985 {
1986
Jason Molenda255f9bb2013-03-06 23:17:36 +00001987 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1988 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1989 offset = 0;
1990
1991 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1992 // in the shared library cache need to be adjusted by an offset to match up with the
1993 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1994 // recorded in mapping_offset_value.
1995 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1996
1997 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00001998 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1999 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
2000
Jason Molenda4e7511e2013-03-06 23:19:17 +00002001 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00002002 {
2003 // Map the local symbols
Jason Molenda4e7511e2013-03-06 23:19:17 +00002004 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00002005 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002006 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002007
2008 offset = 0;
2009
2010 // Read the local_symbols_infos struct in one shot
2011 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
2012 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
2013
Jason Molendaa5609c82012-06-21 01:51:02 +00002014 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
2015
Jason Molenda255f9bb2013-03-06 23:17:36 +00002016 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002017
2018 offset = local_symbols_info.entriesOffset;
2019 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
2020 {
2021 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
2022 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
2023 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
2024 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
2025
Jason Molenda4e7511e2013-03-06 23:19:17 +00002026 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00002027 {
2028 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
2029
2030 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
2031 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
2032 num_syms = symtab->GetNumSymbols();
2033
2034 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
2035 uint32_t string_table_offset = local_symbols_info.stringsOffset;
2036
Jason Molenda4e7511e2013-03-06 23:19:17 +00002037 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00002038 {
2039 /////////////////////////////
2040 {
2041 struct nlist_64 nlist;
2042 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2043 break;
2044
2045 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2046 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2047 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2048 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2049 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2050
2051 SymbolType type = eSymbolTypeInvalid;
2052 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2053
2054 if (symbol_name == NULL)
2055 {
2056 // No symbol should be NULL, even the symbols with no
2057 // string values should have an offset zero which points
2058 // to an empty C-string
2059 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002060 "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 +00002061 entry_index,
2062 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002063 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00002064 continue;
2065 }
2066 if (symbol_name[0] == '\0')
2067 symbol_name = NULL;
2068
2069 const char *symbol_name_non_abi_mangled = NULL;
2070
2071 SectionSP symbol_section;
2072 uint32_t symbol_byte_size = 0;
2073 bool add_nlist = true;
2074 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002075 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002076 bool is_gsym = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002077
2078 assert (sym_idx < num_syms);
2079
2080 sym[sym_idx].SetDebug (is_debug);
2081
2082 if (is_debug)
2083 {
2084 switch (nlist.n_type)
2085 {
2086 case StabGlobalSymbol:
2087 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2088 // Sometimes the N_GSYM value contains the address.
2089
2090 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2091 // have the same address, but we want to ensure that we always find only the real symbol,
2092 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2093 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2094 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2095 // same address.
2096
2097 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2098 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2099 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2100 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2101 add_nlist = false;
2102 else
2103 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002104 is_gsym = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002105 sym[sym_idx].SetExternal(true);
2106 if (nlist.n_value != 0)
2107 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2108 type = eSymbolTypeData;
2109 }
2110 break;
2111
2112 case StabFunctionName:
2113 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2114 type = eSymbolTypeCompiler;
2115 break;
2116
2117 case StabFunction:
2118 // N_FUN -- procedure: name,,n_sect,linenumber,address
2119 if (symbol_name)
2120 {
2121 type = eSymbolTypeCode;
2122 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2123
2124 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2125 // We use the current number of symbols in the symbol table in lieu of
2126 // using nlist_idx in case we ever start trimming entries out
2127 N_FUN_indexes.push_back(sym_idx);
2128 }
2129 else
2130 {
2131 type = eSymbolTypeCompiler;
2132
2133 if ( !N_FUN_indexes.empty() )
2134 {
2135 // Copy the size of the function into the original STAB entry so we don't have
2136 // to hunt for it later
2137 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2138 N_FUN_indexes.pop_back();
2139 // We don't really need the end function STAB as it contains the size which
2140 // we already placed with the original symbol, so don't add it if we want a
2141 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002142 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002143 }
2144 }
2145 break;
2146
2147 case StabStaticSymbol:
2148 // N_STSYM -- static symbol: name,,n_sect,type,address
2149 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2150 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2151 type = eSymbolTypeData;
2152 break;
2153
2154 case StabLocalCommon:
2155 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2156 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2157 type = eSymbolTypeCommonBlock;
2158 break;
2159
2160 case StabBeginSymbol:
2161 // N_BNSYM
2162 // We use the current number of symbols in the symbol table in lieu of
2163 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00002164 // Skip these if we want minimal symbol tables
2165 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002166 break;
2167
2168 case StabEndSymbol:
2169 // N_ENSYM
2170 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2171 // so that we can always skip the entire symbol if we need to navigate
2172 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00002173 // Skip these if we want minimal symbol tables
2174 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002175 break;
2176
2177
2178 case StabSourceFileOptions:
2179 // N_OPT - emitted with gcc2_compiled and in gcc source
2180 type = eSymbolTypeCompiler;
2181 break;
2182
2183 case StabRegisterSymbol:
2184 // N_RSYM - register sym: name,,NO_SECT,type,register
2185 type = eSymbolTypeVariable;
2186 break;
2187
2188 case StabSourceLine:
2189 // N_SLINE - src line: 0,,n_sect,linenumber,address
2190 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2191 type = eSymbolTypeLineEntry;
2192 break;
2193
2194 case StabStructureType:
2195 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2196 type = eSymbolTypeVariableType;
2197 break;
2198
2199 case StabSourceFileName:
2200 // N_SO - source file name
2201 type = eSymbolTypeSourceFile;
2202 if (symbol_name == NULL)
2203 {
Greg Clayton3046e662013-07-10 01:23:25 +00002204 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002205 if (N_SO_index != UINT32_MAX)
2206 {
2207 // Set the size of the N_SO to the terminating index of this N_SO
2208 // so that we can always skip the entire N_SO if we need to navigate
2209 // more quickly at the source level when parsing STABS
2210 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00002211 symbol_ptr->SetByteSize(sym_idx);
Jason Molendaa5609c82012-06-21 01:51:02 +00002212 symbol_ptr->SetSizeIsSibling(true);
2213 }
2214 N_NSYM_indexes.clear();
2215 N_INCL_indexes.clear();
2216 N_BRAC_indexes.clear();
2217 N_COMM_indexes.clear();
2218 N_FUN_indexes.clear();
2219 N_SO_index = UINT32_MAX;
2220 }
2221 else
2222 {
2223 // We use the current number of symbols in the symbol table in lieu of
2224 // using nlist_idx in case we ever start trimming entries out
2225 const bool N_SO_has_full_path = symbol_name[0] == '/';
2226 if (N_SO_has_full_path)
2227 {
Greg Clayton3046e662013-07-10 01:23:25 +00002228 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002229 {
2230 // We have two consecutive N_SO entries where the first contains a directory
2231 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00002232 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002233 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2234 add_nlist = false;
2235 }
2236 else
2237 {
2238 // This is the first entry in a N_SO that contains a directory or
2239 // a full path to the source file
2240 N_SO_index = sym_idx;
2241 }
2242 }
Greg Clayton3046e662013-07-10 01:23:25 +00002243 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002244 {
2245 // This is usually the second N_SO entry that contains just the filename,
2246 // so here we combine it with the first one if we are minimizing the symbol table
2247 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2248 if (so_path && so_path[0])
2249 {
2250 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00002251 const size_t double_slash_pos = full_so_path.find("//");
2252 if (double_slash_pos != std::string::npos)
2253 {
2254 // The linker has been generating bad N_SO entries with doubled up paths
2255 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2256 // and the second is the directory for the source file so you end up with
2257 // a path that looks like "/tmp/src//tmp/src/"
2258 FileSpec so_dir(so_path, false);
2259 if (!so_dir.Exists())
2260 {
2261 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2262 if (so_dir.Exists())
2263 {
2264 // Trim off the incorrect path
2265 full_so_path.erase(0, double_slash_pos + 1);
2266 }
2267 }
2268 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002269 if (*full_so_path.rbegin() != '/')
2270 full_so_path += '/';
2271 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00002272 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002273 add_nlist = false;
2274 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2275 }
2276 }
Greg Clayton946f8902012-09-05 22:30:51 +00002277 else
2278 {
2279 // This could be a relative path to a N_SO
2280 N_SO_index = sym_idx;
2281 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002282 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002283 break;
2284
2285 case StabObjectFileName:
2286 // N_OSO - object file name: name,,0,0,st_mtime
2287 type = eSymbolTypeObjectFile;
2288 break;
2289
2290 case StabLocalSymbol:
2291 // N_LSYM - local sym: name,,NO_SECT,type,offset
2292 type = eSymbolTypeLocal;
2293 break;
2294
2295 //----------------------------------------------------------------------
2296 // INCL scopes
2297 //----------------------------------------------------------------------
2298 case StabBeginIncludeFileName:
2299 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2300 // We use the current number of symbols in the symbol table in lieu of
2301 // using nlist_idx in case we ever start trimming entries out
2302 N_INCL_indexes.push_back(sym_idx);
2303 type = eSymbolTypeScopeBegin;
2304 break;
2305
2306 case StabEndIncludeFile:
2307 // N_EINCL - include file end: name,,NO_SECT,0,0
2308 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2309 // so that we can always skip the entire symbol if we need to navigate
2310 // more quickly at the source level when parsing STABS
2311 if ( !N_INCL_indexes.empty() )
2312 {
2313 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2314 symbol_ptr->SetByteSize(sym_idx + 1);
2315 symbol_ptr->SetSizeIsSibling(true);
2316 N_INCL_indexes.pop_back();
2317 }
2318 type = eSymbolTypeScopeEnd;
2319 break;
2320
2321 case StabIncludeFileName:
2322 // N_SOL - #included file name: name,,n_sect,0,address
2323 type = eSymbolTypeHeaderFile;
2324
2325 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00002326 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002327 break;
2328
2329 case StabCompilerParameters:
2330 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2331 type = eSymbolTypeCompiler;
2332 break;
2333
2334 case StabCompilerVersion:
2335 // N_VERSION - compiler version: name,,NO_SECT,0,0
2336 type = eSymbolTypeCompiler;
2337 break;
2338
2339 case StabCompilerOptLevel:
2340 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2341 type = eSymbolTypeCompiler;
2342 break;
2343
2344 case StabParameter:
2345 // N_PSYM - parameter: name,,NO_SECT,type,offset
2346 type = eSymbolTypeVariable;
2347 break;
2348
2349 case StabAlternateEntry:
2350 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2351 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2352 type = eSymbolTypeLineEntry;
2353 break;
2354
2355 //----------------------------------------------------------------------
2356 // Left and Right Braces
2357 //----------------------------------------------------------------------
2358 case StabLeftBracket:
2359 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2360 // We use the current number of symbols in the symbol table in lieu of
2361 // using nlist_idx in case we ever start trimming entries out
2362 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2363 N_BRAC_indexes.push_back(sym_idx);
2364 type = eSymbolTypeScopeBegin;
2365 break;
2366
2367 case StabRightBracket:
2368 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2369 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2370 // so that we can always skip the entire symbol if we need to navigate
2371 // more quickly at the source level when parsing STABS
2372 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2373 if ( !N_BRAC_indexes.empty() )
2374 {
2375 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2376 symbol_ptr->SetByteSize(sym_idx + 1);
2377 symbol_ptr->SetSizeIsSibling(true);
2378 N_BRAC_indexes.pop_back();
2379 }
2380 type = eSymbolTypeScopeEnd;
2381 break;
2382
2383 case StabDeletedIncludeFile:
2384 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2385 type = eSymbolTypeHeaderFile;
2386 break;
2387
2388 //----------------------------------------------------------------------
2389 // COMM scopes
2390 //----------------------------------------------------------------------
2391 case StabBeginCommon:
2392 // N_BCOMM - begin common: name,,NO_SECT,0,0
2393 // We use the current number of symbols in the symbol table in lieu of
2394 // using nlist_idx in case we ever start trimming entries out
2395 type = eSymbolTypeScopeBegin;
2396 N_COMM_indexes.push_back(sym_idx);
2397 break;
2398
2399 case StabEndCommonLocal:
2400 // N_ECOML - end common (local name): 0,,n_sect,0,address
2401 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2402 // Fall through
2403
2404 case StabEndCommon:
2405 // N_ECOMM - end common: name,,n_sect,0,0
2406 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2407 // so that we can always skip the entire symbol if we need to navigate
2408 // more quickly at the source level when parsing STABS
2409 if ( !N_COMM_indexes.empty() )
2410 {
2411 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2412 symbol_ptr->SetByteSize(sym_idx + 1);
2413 symbol_ptr->SetSizeIsSibling(true);
2414 N_COMM_indexes.pop_back();
2415 }
2416 type = eSymbolTypeScopeEnd;
2417 break;
2418
2419 case StabLength:
2420 // N_LENG - second stab entry with length information
2421 type = eSymbolTypeAdditional;
2422 break;
2423
2424 default: break;
2425 }
2426 }
2427 else
2428 {
2429 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2430 uint8_t n_type = NlistMaskType & nlist.n_type;
2431 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2432
2433 switch (n_type)
2434 {
2435 case NListTypeIndirect: // N_INDR - Fall through
2436 case NListTypePreboundUndefined:// N_PBUD - Fall through
2437 case NListTypeUndefined: // N_UNDF
2438 type = eSymbolTypeUndefined;
2439 break;
2440
2441 case NListTypeAbsolute: // N_ABS
2442 type = eSymbolTypeAbsolute;
2443 break;
2444
2445 case NListTypeSection: // N_SECT
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002446 {
2447 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002448
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002449 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00002450 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002451 // TODO: warn about this?
2452 add_nlist = false;
2453 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002454 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002455
2456 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00002457 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002458 type = eSymbolTypeException;
2459 }
2460 else
2461 {
2462 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002463
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002464 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00002465 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002466 case SectionTypeRegular: break; // regular section
2467 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2468 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2469 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2470 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2471 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2472 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2473 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2474 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2475 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2476 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2477 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2478 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2479 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2480 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2481 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2482 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2483 default: break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002484 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002485
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002486 if (type == eSymbolTypeInvalid)
2487 {
2488 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2489 if (symbol_section->IsDescendant (text_section_sp.get()))
2490 {
2491 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2492 SectionAttrUserSelfModifyingCode |
2493 SectionAttrSytemSomeInstructions))
2494 type = eSymbolTypeData;
2495 else
2496 type = eSymbolTypeCode;
2497 }
2498 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00002499 {
2500 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2501 {
2502 type = eSymbolTypeRuntime;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002503
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002504 if (symbol_name &&
2505 symbol_name[0] == '_' &&
2506 symbol_name[1] == 'O' &&
Jason Molendaa5609c82012-06-21 01:51:02 +00002507 symbol_name[2] == 'B')
2508 {
2509 llvm::StringRef symbol_name_ref(symbol_name);
2510 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2511 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2512 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2513 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2514 {
2515 symbol_name_non_abi_mangled = symbol_name + 1;
2516 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2517 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002518 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002519 }
2520 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2521 {
2522 symbol_name_non_abi_mangled = symbol_name + 1;
2523 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2524 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002525 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002526 }
2527 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2528 {
2529 symbol_name_non_abi_mangled = symbol_name + 1;
2530 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2531 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002532 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002533 }
2534 }
2535 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002536 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00002537 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002538 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00002539 }
2540 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002541 {
2542 type = eSymbolTypeData;
2543 }
2544 }
2545 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2546 {
2547 type = eSymbolTypeTrampoline;
2548 }
2549 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2550 {
2551 type = eSymbolTypeRuntime;
2552 if (symbol_name && symbol_name[0] == '.')
2553 {
2554 llvm::StringRef symbol_name_ref(symbol_name);
2555 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2556 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00002557 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002558 symbol_name_non_abi_mangled = symbol_name;
2559 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2560 type = eSymbolTypeObjCClass;
2561 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002562 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002563 }
2564 }
2565 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002566 }
2567 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002568 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002569 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002570 }
2571
2572 if (add_nlist)
2573 {
2574 uint64_t symbol_value = nlist.n_value;
Jason Molendaa5609c82012-06-21 01:51:02 +00002575 if (symbol_name_non_abi_mangled)
2576 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00002577 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2578 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00002579 }
2580 else
2581 {
Greg Clayton3046e662013-07-10 01:23:25 +00002582 bool symbol_name_is_mangled = false;
2583
Jason Molendaa5609c82012-06-21 01:51:02 +00002584 if (symbol_name && symbol_name[0] == '_')
2585 {
2586 symbol_name_is_mangled = symbol_name[1] == '_';
2587 symbol_name++; // Skip the leading underscore
2588 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002589
Jason Molendaa5609c82012-06-21 01:51:02 +00002590 if (symbol_name)
2591 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002592 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00002593 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00002594 if (is_gsym && is_debug)
2595 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
Jason Molendaa5609c82012-06-21 01:51:02 +00002596 }
2597 }
2598 if (symbol_section)
2599 {
2600 const addr_t section_file_addr = symbol_section->GetFileAddress();
2601 if (symbol_byte_size == 0 && function_starts_count > 0)
2602 {
2603 addr_t symbol_lookup_file_addr = nlist.n_value;
2604 // Do an exact address match for non-ARM addresses, else get the closest since
2605 // the symbol might be a thumb symbol which has an address with bit zero set
2606 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2607 if (is_arm && func_start_entry)
2608 {
2609 // Verify that the function start address is the symbol address (ARM)
2610 // or the symbol address + 1 (thumb)
2611 if (func_start_entry->addr != symbol_lookup_file_addr &&
2612 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2613 {
2614 // Not the right entry, NULL it out...
2615 func_start_entry = NULL;
2616 }
2617 }
2618 if (func_start_entry)
2619 {
2620 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002621
Jason Molendaa5609c82012-06-21 01:51:02 +00002622 addr_t symbol_file_addr = func_start_entry->addr;
2623 uint32_t symbol_flags = 0;
2624 if (is_arm)
2625 {
2626 if (symbol_file_addr & 1)
2627 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2628 symbol_file_addr &= 0xfffffffffffffffeull;
2629 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002630
Jason Molendaa5609c82012-06-21 01:51:02 +00002631 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2632 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2633 if (next_func_start_entry)
2634 {
2635 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2636 // Be sure the clear the Thumb address bit when we calculate the size
2637 // from the current and next address
2638 if (is_arm)
2639 next_symbol_file_addr &= 0xfffffffffffffffeull;
2640 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2641 }
2642 else
2643 {
2644 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2645 }
2646 }
2647 }
2648 symbol_value -= section_file_addr;
2649 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002650
Greg Claytondacc4a92013-05-14 22:19:37 +00002651 if (is_debug == false)
2652 {
2653 if (type == eSymbolTypeCode)
2654 {
2655 // See if we can find a N_FUN entry for any code symbols.
2656 // If we do find a match, and the name matches, then we
2657 // can merge the two into just the function symbol to avoid
2658 // duplicate entries in the symbol table
2659 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2660 if (pos != N_FUN_addr_to_sym_idx.end())
2661 {
Greg Clayton3046e662013-07-10 01:23:25 +00002662 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
Greg Claytondacc4a92013-05-14 22:19:37 +00002663 {
2664 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2665 // We just need the flags from the linker symbol, so put these flags
2666 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2667 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2668 sym[sym_idx].Clear();
2669 continue;
2670 }
2671 }
2672 }
2673 else if (type == eSymbolTypeData)
2674 {
2675 // See if we can find a N_STSYM entry for any data symbols.
2676 // If we do find a match, and the name matches, then we
2677 // can merge the two into just the Static symbol to avoid
2678 // duplicate entries in the symbol table
2679 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2680 if (pos != N_STSYM_addr_to_sym_idx.end())
2681 {
Greg Clayton3046e662013-07-10 01:23:25 +00002682 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
Greg Claytondacc4a92013-05-14 22:19:37 +00002683 {
2684 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2685 // We just need the flags from the linker symbol, so put these flags
2686 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2687 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2688 sym[sym_idx].Clear();
2689 continue;
2690 }
2691 }
2692 else
2693 {
2694 // Combine N_GSYM stab entries with the non stab symbol
Greg Clayton3046e662013-07-10 01:23:25 +00002695 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 +00002696 if (pos != N_GSYM_name_to_sym_idx.end())
2697 {
2698 const uint32_t GSYM_sym_idx = pos->second;
2699 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
2700 // Copy the address, because often the N_GSYM address has an invalid address of zero
2701 // when the global is a common symbol
2702 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
2703 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
2704 // We just need the flags from the linker symbol, so put these flags
2705 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2706 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2707 sym[sym_idx].Clear();
2708 continue;
2709 }
2710 }
2711 }
2712 }
2713
Jason Molendaa5609c82012-06-21 01:51:02 +00002714 sym[sym_idx].SetID (nlist_idx);
2715 sym[sym_idx].SetType (type);
2716 sym[sym_idx].GetAddress().SetSection (symbol_section);
2717 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2718 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002719
Jason Molendaa5609c82012-06-21 01:51:02 +00002720 if (symbol_byte_size > 0)
2721 sym[sym_idx].SetByteSize(symbol_byte_size);
2722
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002723 if (demangled_is_synthesized)
2724 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00002725 ++sym_idx;
2726 }
2727 else
2728 {
2729 sym[sym_idx].Clear();
2730 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002731
Jason Molendaa5609c82012-06-21 01:51:02 +00002732 }
2733 /////////////////////////////
2734 }
2735 break; // No more entries to consider
2736 }
2737 }
2738 }
2739 }
2740 }
2741 }
2742 }
2743
2744 // Must reset this in case it was mutated above!
2745 nlist_data_offset = 0;
2746#endif
Greg Claytonfd814c52013-08-13 01:42:25 +00002747
2748 if (nlist_data.GetByteSize() > 0)
Jason Molendaa5609c82012-06-21 01:51:02 +00002749 {
Jason Molendaa5609c82012-06-21 01:51:02 +00002750
Greg Claytonfd814c52013-08-13 01:42:25 +00002751 // If the sym array was not created while parsing the DSC unmapped
2752 // symbols, create it now.
2753 if (sym == NULL)
Greg Clayton4c82d422012-05-18 23:20:01 +00002754 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002755 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2756 num_syms = symtab->GetNumSymbols();
2757 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002758
Greg Claytonfd814c52013-08-13 01:42:25 +00002759 if (unmapped_local_symbols_found)
2760 {
2761 assert(m_dysymtab.ilocalsym == 0);
2762 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2763 nlist_idx = m_dysymtab.nlocalsym;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002764 }
Greg Claytondebb8812012-05-25 17:04:00 +00002765 else
2766 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002767 nlist_idx = 0;
Greg Claytondebb8812012-05-25 17:04:00 +00002768 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002769
Greg Claytonfd814c52013-08-13 01:42:25 +00002770 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002771 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002772 struct nlist_64 nlist;
2773 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2774 break;
2775
2776 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2777 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2778 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2779 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2780 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2781
2782 SymbolType type = eSymbolTypeInvalid;
2783 const char *symbol_name = NULL;
2784
2785 if (have_strtab_data)
Greg Clayton77ccca72011-12-30 00:32:24 +00002786 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002787 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002788
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002789 if (symbol_name == NULL)
2790 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002791 // No symbol should be NULL, even the symbols with no
2792 // string values should have an offset zero which points
2793 // to an empty C-string
2794 Host::SystemLog (Host::eSystemLogError,
2795 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
2796 nlist_idx,
2797 nlist.n_strx,
2798 module_sp->GetFileSpec().GetPath().c_str());
2799 continue;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002800 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002801 if (symbol_name[0] == '\0')
2802 symbol_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002803 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002804 else
2805 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002806 const addr_t str_addr = strtab_addr + nlist.n_strx;
2807 Error str_error;
2808 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2809 symbol_name = memory_symbol_name.c_str();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002810 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002811 const char *symbol_name_non_abi_mangled = NULL;
2812
2813 SectionSP symbol_section;
2814 lldb::addr_t symbol_byte_size = 0;
2815 bool add_nlist = true;
2816 bool is_gsym = false;
2817 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
2818 bool demangled_is_synthesized = false;
2819
2820 assert (sym_idx < num_syms);
2821
2822 sym[sym_idx].SetDebug (is_debug);
2823
2824 if (is_debug)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002825 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002826 switch (nlist.n_type)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002827 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002828 case StabGlobalSymbol:
2829 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2830 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002831
Greg Claytonfd814c52013-08-13 01:42:25 +00002832 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2833 // have the same address, but we want to ensure that we always find only the real symbol,
2834 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2835 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2836 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2837 // same address.
Greg Clayton29e08cb2012-03-14 01:53:24 +00002838
Greg Claytonfd814c52013-08-13 01:42:25 +00002839 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2840 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2841 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2842 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2843 add_nlist = false;
2844 else
2845 {
2846 is_gsym = true;
2847 sym[sym_idx].SetExternal(true);
2848 if (nlist.n_value != 0)
2849 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2850 type = eSymbolTypeData;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002851 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002852 break;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002853
Greg Claytonfd814c52013-08-13 01:42:25 +00002854 case StabFunctionName:
2855 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2856 type = eSymbolTypeCompiler;
2857 break;
2858
2859 case StabFunction:
2860 // N_FUN -- procedure: name,,n_sect,linenumber,address
2861 if (symbol_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00002862 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002863 type = eSymbolTypeCode;
2864 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2865
2866 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2867 // We use the current number of symbols in the symbol table in lieu of
2868 // using nlist_idx in case we ever start trimming entries out
2869 N_FUN_indexes.push_back(sym_idx);
Greg Claytondacc4a92013-05-14 22:19:37 +00002870 }
2871 else
2872 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002873 type = eSymbolTypeCompiler;
2874
2875 if ( !N_FUN_indexes.empty() )
Greg Claytondacc4a92013-05-14 22:19:37 +00002876 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002877 // Copy the size of the function into the original STAB entry so we don't have
2878 // to hunt for it later
2879 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2880 N_FUN_indexes.pop_back();
2881 // We don't really need the end function STAB as it contains the size which
2882 // we already placed with the original symbol, so don't add it if we want a
2883 // minimal symbol table
2884 add_nlist = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002885 }
2886 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002887 break;
2888
2889 case StabStaticSymbol:
2890 // N_STSYM -- static symbol: name,,n_sect,type,address
2891 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2892 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2893 type = eSymbolTypeData;
2894 break;
2895
2896 case StabLocalCommon:
2897 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2898 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2899 type = eSymbolTypeCommonBlock;
2900 break;
2901
2902 case StabBeginSymbol:
2903 // N_BNSYM
2904 // We use the current number of symbols in the symbol table in lieu of
2905 // using nlist_idx in case we ever start trimming entries out
2906 // Skip these if we want minimal symbol tables
2907 add_nlist = false;
2908 break;
2909
2910 case StabEndSymbol:
2911 // N_ENSYM
2912 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2913 // so that we can always skip the entire symbol if we need to navigate
2914 // more quickly at the source level when parsing STABS
2915 // Skip these if we want minimal symbol tables
2916 add_nlist = false;
2917 break;
2918
2919
2920 case StabSourceFileOptions:
2921 // N_OPT - emitted with gcc2_compiled and in gcc source
2922 type = eSymbolTypeCompiler;
2923 break;
2924
2925 case StabRegisterSymbol:
2926 // N_RSYM - register sym: name,,NO_SECT,type,register
2927 type = eSymbolTypeVariable;
2928 break;
2929
2930 case StabSourceLine:
2931 // N_SLINE - src line: 0,,n_sect,linenumber,address
2932 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2933 type = eSymbolTypeLineEntry;
2934 break;
2935
2936 case StabStructureType:
2937 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2938 type = eSymbolTypeVariableType;
2939 break;
2940
2941 case StabSourceFileName:
2942 // N_SO - source file name
2943 type = eSymbolTypeSourceFile;
2944 if (symbol_name == NULL)
2945 {
2946 add_nlist = false;
2947 if (N_SO_index != UINT32_MAX)
2948 {
2949 // Set the size of the N_SO to the terminating index of this N_SO
2950 // so that we can always skip the entire N_SO if we need to navigate
2951 // more quickly at the source level when parsing STABS
2952 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2953 symbol_ptr->SetByteSize(sym_idx);
2954 symbol_ptr->SetSizeIsSibling(true);
2955 }
2956 N_NSYM_indexes.clear();
2957 N_INCL_indexes.clear();
2958 N_BRAC_indexes.clear();
2959 N_COMM_indexes.clear();
2960 N_FUN_indexes.clear();
2961 N_SO_index = UINT32_MAX;
2962 }
2963 else
2964 {
2965 // We use the current number of symbols in the symbol table in lieu of
2966 // using nlist_idx in case we ever start trimming entries out
2967 const bool N_SO_has_full_path = symbol_name[0] == '/';
2968 if (N_SO_has_full_path)
2969 {
2970 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2971 {
2972 // We have two consecutive N_SO entries where the first contains a directory
2973 // and the second contains a full path.
2974 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
2975 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2976 add_nlist = false;
2977 }
2978 else
2979 {
2980 // This is the first entry in a N_SO that contains a directory or
2981 // a full path to the source file
2982 N_SO_index = sym_idx;
2983 }
2984 }
2985 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2986 {
2987 // This is usually the second N_SO entry that contains just the filename,
2988 // so here we combine it with the first one if we are minimizing the symbol table
2989 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2990 if (so_path && so_path[0])
2991 {
2992 std::string full_so_path (so_path);
2993 const size_t double_slash_pos = full_so_path.find("//");
2994 if (double_slash_pos != std::string::npos)
2995 {
2996 // The linker has been generating bad N_SO entries with doubled up paths
2997 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2998 // and the second is the directory for the source file so you end up with
2999 // a path that looks like "/tmp/src//tmp/src/"
3000 FileSpec so_dir(so_path, false);
3001 if (!so_dir.Exists())
3002 {
3003 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3004 if (so_dir.Exists())
3005 {
3006 // Trim off the incorrect path
3007 full_so_path.erase(0, double_slash_pos + 1);
3008 }
3009 }
3010 }
3011 if (*full_so_path.rbegin() != '/')
3012 full_so_path += '/';
3013 full_so_path += symbol_name;
3014 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
3015 add_nlist = false;
3016 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3017 }
3018 }
3019 else
3020 {
3021 // This could be a relative path to a N_SO
3022 N_SO_index = sym_idx;
3023 }
3024 }
3025
3026 break;
3027
3028 case StabObjectFileName:
3029 // N_OSO - object file name: name,,0,0,st_mtime
3030 type = eSymbolTypeObjectFile;
3031 break;
3032
3033 case StabLocalSymbol:
3034 // N_LSYM - local sym: name,,NO_SECT,type,offset
3035 type = eSymbolTypeLocal;
3036 break;
3037
3038 //----------------------------------------------------------------------
3039 // INCL scopes
3040 //----------------------------------------------------------------------
3041 case StabBeginIncludeFileName:
3042 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
3043 // We use the current number of symbols in the symbol table in lieu of
3044 // using nlist_idx in case we ever start trimming entries out
3045 N_INCL_indexes.push_back(sym_idx);
3046 type = eSymbolTypeScopeBegin;
3047 break;
3048
3049 case StabEndIncludeFile:
3050 // N_EINCL - include file end: name,,NO_SECT,0,0
3051 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3052 // so that we can always skip the entire symbol if we need to navigate
3053 // more quickly at the source level when parsing STABS
3054 if ( !N_INCL_indexes.empty() )
3055 {
3056 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3057 symbol_ptr->SetByteSize(sym_idx + 1);
3058 symbol_ptr->SetSizeIsSibling(true);
3059 N_INCL_indexes.pop_back();
3060 }
3061 type = eSymbolTypeScopeEnd;
3062 break;
3063
3064 case StabIncludeFileName:
3065 // N_SOL - #included file name: name,,n_sect,0,address
3066 type = eSymbolTypeHeaderFile;
3067
3068 // We currently don't use the header files on darwin
3069 add_nlist = false;
3070 break;
3071
3072 case StabCompilerParameters:
3073 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
3074 type = eSymbolTypeCompiler;
3075 break;
3076
3077 case StabCompilerVersion:
3078 // N_VERSION - compiler version: name,,NO_SECT,0,0
3079 type = eSymbolTypeCompiler;
3080 break;
3081
3082 case StabCompilerOptLevel:
3083 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
3084 type = eSymbolTypeCompiler;
3085 break;
3086
3087 case StabParameter:
3088 // N_PSYM - parameter: name,,NO_SECT,type,offset
3089 type = eSymbolTypeVariable;
3090 break;
3091
3092 case StabAlternateEntry:
3093 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
3094 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3095 type = eSymbolTypeLineEntry;
3096 break;
3097
3098 //----------------------------------------------------------------------
3099 // Left and Right Braces
3100 //----------------------------------------------------------------------
3101 case StabLeftBracket:
3102 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
3103 // We use the current number of symbols in the symbol table in lieu of
3104 // using nlist_idx in case we ever start trimming entries out
3105 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3106 N_BRAC_indexes.push_back(sym_idx);
3107 type = eSymbolTypeScopeBegin;
3108 break;
3109
3110 case StabRightBracket:
3111 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
3112 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3113 // so that we can always skip the entire symbol if we need to navigate
3114 // more quickly at the source level when parsing STABS
3115 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3116 if ( !N_BRAC_indexes.empty() )
3117 {
3118 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3119 symbol_ptr->SetByteSize(sym_idx + 1);
3120 symbol_ptr->SetSizeIsSibling(true);
3121 N_BRAC_indexes.pop_back();
3122 }
3123 type = eSymbolTypeScopeEnd;
3124 break;
3125
3126 case StabDeletedIncludeFile:
3127 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3128 type = eSymbolTypeHeaderFile;
3129 break;
3130
3131 //----------------------------------------------------------------------
3132 // COMM scopes
3133 //----------------------------------------------------------------------
3134 case StabBeginCommon:
3135 // N_BCOMM - begin common: name,,NO_SECT,0,0
3136 // We use the current number of symbols in the symbol table in lieu of
3137 // using nlist_idx in case we ever start trimming entries out
3138 type = eSymbolTypeScopeBegin;
3139 N_COMM_indexes.push_back(sym_idx);
3140 break;
3141
3142 case StabEndCommonLocal:
3143 // N_ECOML - end common (local name): 0,,n_sect,0,address
3144 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3145 // Fall through
3146
3147 case StabEndCommon:
3148 // N_ECOMM - end common: name,,n_sect,0,0
3149 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3150 // so that we can always skip the entire symbol if we need to navigate
3151 // more quickly at the source level when parsing STABS
3152 if ( !N_COMM_indexes.empty() )
3153 {
3154 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3155 symbol_ptr->SetByteSize(sym_idx + 1);
3156 symbol_ptr->SetSizeIsSibling(true);
3157 N_COMM_indexes.pop_back();
3158 }
3159 type = eSymbolTypeScopeEnd;
3160 break;
3161
3162 case StabLength:
3163 // N_LENG - second stab entry with length information
3164 type = eSymbolTypeAdditional;
3165 break;
3166
3167 default: break;
3168 }
3169 }
3170 else
3171 {
3172 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3173 uint8_t n_type = NlistMaskType & nlist.n_type;
3174 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3175
3176 switch (n_type)
3177 {
3178 case NListTypeIndirect: // N_INDR - Fall through
3179 case NListTypePreboundUndefined:// N_PBUD - Fall through
3180 case NListTypeUndefined: // N_UNDF
3181 type = eSymbolTypeUndefined;
3182 break;
3183
3184 case NListTypeAbsolute: // N_ABS
3185 type = eSymbolTypeAbsolute;
3186 break;
3187
3188 case NListTypeSection: // N_SECT
3189 {
3190 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3191
3192 if (!symbol_section)
3193 {
3194 // TODO: warn about this?
3195 add_nlist = false;
3196 break;
3197 }
3198
3199 if (TEXT_eh_frame_sectID == nlist.n_sect)
3200 {
3201 type = eSymbolTypeException;
3202 }
3203 else
3204 {
3205 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3206
3207 switch (section_type)
3208 {
3209 case SectionTypeRegular: break; // regular section
3210 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3211 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3212 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3213 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3214 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3215 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3216 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3217 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3218 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3219 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3220 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3221 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3222 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3223 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3224 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3225 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3226 default: break;
3227 }
3228
3229 if (type == eSymbolTypeInvalid)
3230 {
3231 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3232 if (symbol_section->IsDescendant (text_section_sp.get()))
3233 {
3234 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3235 SectionAttrUserSelfModifyingCode |
3236 SectionAttrSytemSomeInstructions))
3237 type = eSymbolTypeData;
3238 else
3239 type = eSymbolTypeCode;
3240 }
3241 else
3242 if (symbol_section->IsDescendant(data_section_sp.get()))
3243 {
3244 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
3245 {
3246 type = eSymbolTypeRuntime;
3247
3248 if (symbol_name &&
3249 symbol_name[0] == '_' &&
3250 symbol_name[1] == 'O' &&
3251 symbol_name[2] == 'B')
3252 {
3253 llvm::StringRef symbol_name_ref(symbol_name);
3254 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3255 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3256 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3257 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3258 {
3259 symbol_name_non_abi_mangled = symbol_name + 1;
3260 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3261 type = eSymbolTypeObjCClass;
3262 demangled_is_synthesized = true;
3263 }
3264 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3265 {
3266 symbol_name_non_abi_mangled = symbol_name + 1;
3267 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3268 type = eSymbolTypeObjCMetaClass;
3269 demangled_is_synthesized = true;
3270 }
3271 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3272 {
3273 symbol_name_non_abi_mangled = symbol_name + 1;
3274 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3275 type = eSymbolTypeObjCIVar;
3276 demangled_is_synthesized = true;
3277 }
3278 }
3279 }
3280 else
3281 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3282 {
3283 type = eSymbolTypeException;
3284 }
3285 else
3286 {
3287 type = eSymbolTypeData;
3288 }
3289 }
3290 else
3291 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3292 {
3293 type = eSymbolTypeTrampoline;
3294 }
3295 else
3296 if (symbol_section->IsDescendant(objc_section_sp.get()))
3297 {
3298 type = eSymbolTypeRuntime;
3299 if (symbol_name && symbol_name[0] == '.')
3300 {
3301 llvm::StringRef symbol_name_ref(symbol_name);
3302 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3303 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3304 {
3305 symbol_name_non_abi_mangled = symbol_name;
3306 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3307 type = eSymbolTypeObjCClass;
3308 demangled_is_synthesized = true;
3309 }
3310 }
3311 }
3312 }
3313 }
3314 }
3315 break;
Greg Claytondacc4a92013-05-14 22:19:37 +00003316 }
3317 }
3318
Greg Claytonfd814c52013-08-13 01:42:25 +00003319 if (add_nlist)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003320 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003321 uint64_t symbol_value = nlist.n_value;
3322
3323 if (symbol_name_non_abi_mangled)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003324 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003325 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3326 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
3327 }
3328 else
3329 {
3330 bool symbol_name_is_mangled = false;
3331
3332 if (symbol_name && symbol_name[0] == '_')
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003333 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003334 symbol_name_is_mangled = symbol_name[1] == '_';
3335 symbol_name++; // Skip the leading underscore
3336 }
3337
3338 if (symbol_name)
3339 {
3340 ConstString const_symbol_name(symbol_name);
3341 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
3342 if (is_gsym && is_debug)
3343 {
3344 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
3345 }
3346 }
3347 }
3348 if (symbol_section)
3349 {
3350 const addr_t section_file_addr = symbol_section->GetFileAddress();
3351 if (symbol_byte_size == 0 && function_starts_count > 0)
3352 {
3353 addr_t symbol_lookup_file_addr = nlist.n_value;
3354 // Do an exact address match for non-ARM addresses, else get the closest since
3355 // the symbol might be a thumb symbol which has an address with bit zero set
3356 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3357 if (is_arm && func_start_entry)
3358 {
3359 // Verify that the function start address is the symbol address (ARM)
3360 // or the symbol address + 1 (thumb)
3361 if (func_start_entry->addr != symbol_lookup_file_addr &&
3362 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3363 {
3364 // Not the right entry, NULL it out...
3365 func_start_entry = NULL;
3366 }
3367 }
3368 if (func_start_entry)
3369 {
3370 func_start_entry->data = true;
3371
3372 addr_t symbol_file_addr = func_start_entry->addr;
3373 if (is_arm)
3374 symbol_file_addr &= 0xfffffffffffffffeull;
3375
3376 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3377 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3378 if (next_func_start_entry)
3379 {
3380 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3381 // Be sure the clear the Thumb address bit when we calculate the size
3382 // from the current and next address
3383 if (is_arm)
3384 next_symbol_file_addr &= 0xfffffffffffffffeull;
3385 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3386 }
3387 else
3388 {
3389 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3390 }
3391 }
3392 }
3393 symbol_value -= section_file_addr;
3394 }
3395
3396 if (is_debug == false)
3397 {
3398 if (type == eSymbolTypeCode)
3399 {
3400 // See if we can find a N_FUN entry for any code symbols.
3401 // If we do find a match, and the name matches, then we
3402 // can merge the two into just the function symbol to avoid
3403 // duplicate entries in the symbol table
3404 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3405 if (pos != N_FUN_addr_to_sym_idx.end())
3406 {
3407 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3408 {
3409 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3410 // We just need the flags from the linker symbol, so put these flags
3411 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3412 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3413 sym[sym_idx].Clear();
3414 continue;
3415 }
3416 }
3417 }
3418 else if (type == eSymbolTypeData)
3419 {
3420 // See if we can find a N_STSYM entry for any data symbols.
3421 // If we do find a match, and the name matches, then we
3422 // can merge the two into just the Static symbol to avoid
3423 // duplicate entries in the symbol table
3424 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3425 if (pos != N_STSYM_addr_to_sym_idx.end())
3426 {
3427 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3428 {
3429 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3430 // We just need the flags from the linker symbol, so put these flags
3431 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3432 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3433 sym[sym_idx].Clear();
3434 continue;
3435 }
3436 }
3437 else
3438 {
3439 // Combine N_GSYM stab entries with the non stab symbol
3440 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
3441 if (pos != N_GSYM_name_to_sym_idx.end())
3442 {
3443 const uint32_t GSYM_sym_idx = pos->second;
3444 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3445 // Copy the address, because often the N_GSYM address has an invalid address of zero
3446 // when the global is a common symbol
3447 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3448 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3449 // We just need the flags from the linker symbol, so put these flags
3450 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3451 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3452 sym[sym_idx].Clear();
3453 continue;
3454 }
3455 }
3456 }
3457 }
3458
3459 sym[sym_idx].SetID (nlist_idx);
3460 sym[sym_idx].SetType (type);
3461 sym[sym_idx].GetAddress().SetSection (symbol_section);
3462 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3463 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3464
3465 if (symbol_byte_size > 0)
3466 sym[sym_idx].SetByteSize(symbol_byte_size);
3467
3468 if (demangled_is_synthesized)
3469 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3470
3471 ++sym_idx;
3472 }
3473 else
3474 {
3475 sym[sym_idx].Clear();
3476 }
3477
3478 }
3479
3480 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3481 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3482 // such entries by figuring out what the address for the global is by looking up this non-STAB
3483 // entry and copying the value into the debug symbol's value to save us the hassle in the
3484 // debug symbol parser.
3485
3486 Symbol *global_symbol = NULL;
3487 for (nlist_idx = 0;
3488 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3489 nlist_idx++)
3490 {
3491 if (global_symbol->GetAddress().GetFileAddress() == 0)
3492 {
3493 std::vector<uint32_t> indexes;
3494 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3495 {
3496 std::vector<uint32_t>::const_iterator pos;
3497 std::vector<uint32_t>::const_iterator end = indexes.end();
3498 for (pos = indexes.begin(); pos != end; ++pos)
3499 {
3500 symbol_ptr = symtab->SymbolAtIndex(*pos);
3501 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3502 {
3503 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3504 break;
3505 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003506 }
3507 }
3508 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003509 }
3510 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003511
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003512 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3513
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003514 if (function_starts_count > 0)
3515 {
3516 char synthetic_function_symbol[PATH_MAX];
3517 uint32_t num_synthetic_function_symbols = 0;
3518 for (i=0; i<function_starts_count; ++i)
3519 {
3520 if (function_starts.GetEntryRef (i).data == false)
3521 ++num_synthetic_function_symbols;
3522 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003523
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003524 if (num_synthetic_function_symbols > 0)
3525 {
3526 if (num_syms < sym_idx + num_synthetic_function_symbols)
3527 {
3528 num_syms = sym_idx + num_synthetic_function_symbols;
3529 sym = symtab->Resize (num_syms);
3530 }
3531 uint32_t synthetic_function_symbol_idx = 0;
3532 for (i=0; i<function_starts_count; ++i)
3533 {
3534 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3535 if (func_start_entry->data == false)
3536 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003537 addr_t symbol_file_addr = func_start_entry->addr;
3538 uint32_t symbol_flags = 0;
3539 if (is_arm)
3540 {
3541 if (symbol_file_addr & 1)
3542 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3543 symbol_file_addr &= 0xfffffffffffffffeull;
3544 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003545 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003546 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003547 {
3548 SectionSP symbol_section (symbol_addr.GetSection());
3549 uint32_t symbol_byte_size = 0;
3550 if (symbol_section)
3551 {
3552 const addr_t section_file_addr = symbol_section->GetFileAddress();
3553 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3554 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3555 if (next_func_start_entry)
3556 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003557 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3558 if (is_arm)
3559 next_symbol_file_addr &= 0xfffffffffffffffeull;
3560 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 +00003561 }
3562 else
3563 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003564 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003565 }
3566 snprintf (synthetic_function_symbol,
3567 sizeof(synthetic_function_symbol),
3568 "___lldb_unnamed_function%u$$%s",
3569 ++synthetic_function_symbol_idx,
3570 module_sp->GetFileSpec().GetFilename().GetCString());
3571 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Clayton037520e2012-07-18 23:18:10 +00003572 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003573 sym[sym_idx].SetType (eSymbolTypeCode);
3574 sym[sym_idx].SetIsSynthetic (true);
3575 sym[sym_idx].GetAddress() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003576 if (symbol_flags)
3577 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003578 if (symbol_byte_size)
3579 sym[sym_idx].SetByteSize (symbol_byte_size);
3580 ++sym_idx;
3581 }
3582 }
3583 }
3584 }
3585 }
3586 }
3587
3588 // Trim our symbols down to just what we ended up with after
3589 // removing any symbols.
3590 if (sym_idx < num_syms)
3591 {
3592 num_syms = sym_idx;
3593 sym = symtab->Resize (num_syms);
3594 }
3595
3596 // Now synthesize indirect symbols
3597 if (m_dysymtab.nindirectsyms != 0)
3598 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003599 if (indirect_symbol_index_data.GetByteSize())
3600 {
3601 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3602
3603 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3604 {
3605 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3606 {
3607 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3608 if (symbol_stub_byte_size == 0)
3609 continue;
3610
3611 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3612
3613 if (num_symbol_stubs == 0)
3614 continue;
3615
3616 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3617 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3618 {
3619 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3620 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 +00003621 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003622 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3623 {
3624 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3625 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3626 continue;
3627
3628 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3629 Symbol *stub_symbol = NULL;
3630 if (index_pos != end_index_pos)
3631 {
3632 // We have a remapping from the original nlist index to
3633 // a current symbol index, so just look this up by index
3634 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3635 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003636 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003637 {
3638 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00003639 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003640 // S_SYMBOL_STUBS
3641 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3642 }
3643
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003644 if (stub_symbol)
3645 {
3646 Address so_addr(symbol_stub_addr, section_list);
3647
3648 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3649 {
3650 // Change the external symbol into a trampoline that makes sense
3651 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3652 // can re-use them so we don't have to make up a synthetic symbol
3653 // for no good reason.
3654 stub_symbol->SetType (eSymbolTypeTrampoline);
3655 stub_symbol->SetExternal (false);
3656 stub_symbol->GetAddress() = so_addr;
3657 stub_symbol->SetByteSize (symbol_stub_byte_size);
3658 }
3659 else
3660 {
3661 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00003662 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003663 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00003664 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003665 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00003666 stub_symbol = NULL; // this pointer no longer valid
3667 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003668 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00003669 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003670 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3671 sym[sym_idx].SetIsSynthetic (true);
3672 sym[sym_idx].GetAddress() = so_addr;
3673 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3674 ++sym_idx;
3675 }
3676 }
Greg Clayton3f839a32012-09-05 01:38:55 +00003677 else
3678 {
3679 if (log)
3680 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3681 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003682 }
3683 }
3684 }
3685 }
3686 }
3687 }
Greg Clayton3046e662013-07-10 01:23:25 +00003688
3689// StreamFile s(stdout, false);
3690// s.Printf ("Symbol table before CalculateSymbolSizes():\n");
3691// symtab->Dump(&s, NULL, eSortOrderNone);
3692 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
3693 symtab->CalculateSymbolSizes();
3694
3695// s.Printf ("Symbol table after CalculateSymbolSizes():\n");
3696// symtab->Dump(&s, NULL, eSortOrderNone);
3697
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003698 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003699 }
3700 return 0;
3701}
3702
3703
3704void
3705ObjectFileMachO::Dump (Stream *s)
3706{
Greg Claytona1743492012-03-13 23:14:29 +00003707 ModuleSP module_sp(GetModule());
3708 if (module_sp)
3709 {
3710 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3711 s->Printf("%p: ", this);
3712 s->Indent();
3713 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3714 s->PutCString("ObjectFileMachO64");
3715 else
3716 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003717
Greg Claytona1743492012-03-13 23:14:29 +00003718 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003719
Greg Claytona1743492012-03-13 23:14:29 +00003720 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003721
Greg Clayton3046e662013-07-10 01:23:25 +00003722 SectionList *sections = GetSectionList();
3723 if (sections)
3724 sections->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003725
Greg Claytona1743492012-03-13 23:14:29 +00003726 if (m_symtab_ap.get())
3727 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3728 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003729}
3730
Greg Claytonf4d6de62013-04-24 22:29:28 +00003731bool
3732ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3733 const lldb_private::DataExtractor &data,
3734 lldb::offset_t lc_offset,
3735 lldb_private::UUID& uuid)
3736{
3737 uint32_t i;
3738 struct uuid_command load_cmd;
3739
3740 lldb::offset_t offset = lc_offset;
3741 for (i=0; i<header.ncmds; ++i)
3742 {
3743 const lldb::offset_t cmd_offset = offset;
3744 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3745 break;
3746
3747 if (load_cmd.cmd == LoadCommandUUID)
3748 {
3749 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3750
3751 if (uuid_bytes)
3752 {
3753 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3754 // We pretend these object files have no UUID to prevent crashing.
3755
3756 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3757 0x3b, 0xa8,
3758 0x4b, 0x16,
3759 0xb6, 0xa4,
3760 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3761
3762 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3763 return false;
3764
3765 uuid.SetBytes (uuid_bytes);
3766 return true;
3767 }
3768 return false;
3769 }
3770 offset = cmd_offset + load_cmd.cmdsize;
3771 }
3772 return false;
3773}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003774
3775bool
Greg Clayton60830262011-02-04 18:53:10 +00003776ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003777{
Greg Claytona1743492012-03-13 23:14:29 +00003778 ModuleSP module_sp(GetModule());
3779 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003780 {
Greg Claytona1743492012-03-13 23:14:29 +00003781 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00003782 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00003783 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003784 }
3785 return false;
3786}
3787
3788
3789uint32_t
3790ObjectFileMachO::GetDependentModules (FileSpecList& files)
3791{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003792 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00003793 ModuleSP module_sp(GetModule());
3794 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003795 {
Greg Claytona1743492012-03-13 23:14:29 +00003796 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3797 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00003798 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003799 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3800 uint32_t i;
3801 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003802 {
Greg Claytona1743492012-03-13 23:14:29 +00003803 const uint32_t cmd_offset = offset;
3804 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3805 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003806
Greg Claytona1743492012-03-13 23:14:29 +00003807 switch (load_cmd.cmd)
3808 {
3809 case LoadCommandDylibLoad:
3810 case LoadCommandDylibLoadWeak:
3811 case LoadCommandDylibReexport:
3812 case LoadCommandDynamicLinkerLoad:
3813 case LoadCommandFixedVMShlibLoad:
3814 case LoadCommandDylibLoadUpward:
3815 {
3816 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3817 const char *path = m_data.PeekCStr(name_offset);
3818 // Skip any path that starts with '@' since these are usually:
3819 // @executable_path/.../file
3820 // @rpath/.../file
3821 if (path && path[0] != '@')
3822 {
3823 FileSpec file_spec(path, resolve_path);
3824 if (files.AppendIfUnique(file_spec))
3825 count++;
3826 }
3827 }
3828 break;
3829
3830 default:
3831 break;
3832 }
3833 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003834 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003835 }
3836 return count;
3837}
3838
Jim Ingham672e6f52011-03-07 23:44:08 +00003839lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00003840ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00003841{
3842 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3843 // is initialized to an invalid address, so we can just return that.
3844 // 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 +00003845
Jim Ingham672e6f52011-03-07 23:44:08 +00003846 if (!IsExecutable() || m_entry_point_address.IsValid())
3847 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003848
3849 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00003850 // /usr/include/mach-o.h, but it is basically:
3851 //
3852 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3853 // uint32_t count - this is the count of longs in the thread state data
3854 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3855 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00003856 //
Jim Ingham672e6f52011-03-07 23:44:08 +00003857 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3858 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3859 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3860 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3861 //
3862 // For now we hard-code the offsets and flavors we need:
3863 //
3864 //
3865
Greg Claytona1743492012-03-13 23:14:29 +00003866 ModuleSP module_sp(GetModule());
3867 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00003868 {
Greg Claytona1743492012-03-13 23:14:29 +00003869 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3870 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00003871 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003872 uint32_t i;
3873 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3874 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003875
Greg Claytona1743492012-03-13 23:14:29 +00003876 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00003877 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003878 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00003879 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3880 break;
3881
3882 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00003883 {
Greg Claytona1743492012-03-13 23:14:29 +00003884 case LoadCommandUnixThread:
3885 case LoadCommandThread:
Jim Ingham672e6f52011-03-07 23:44:08 +00003886 {
Greg Claytona1743492012-03-13 23:14:29 +00003887 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00003888 {
Greg Claytona1743492012-03-13 23:14:29 +00003889 uint32_t flavor = m_data.GetU32(&offset);
3890 uint32_t count = m_data.GetU32(&offset);
3891 if (count == 0)
3892 {
3893 // We've gotten off somehow, log and exit;
3894 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00003895 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003896
Greg Claytona1743492012-03-13 23:14:29 +00003897 switch (m_header.cputype)
3898 {
3899 case llvm::MachO::CPUTypeARM:
3900 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3901 {
3902 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3903 start_address = m_data.GetU32(&offset);
3904 done = true;
3905 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003906 break;
Greg Claytona1743492012-03-13 23:14:29 +00003907 case llvm::MachO::CPUTypeI386:
3908 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3909 {
3910 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3911 start_address = m_data.GetU32(&offset);
3912 done = true;
3913 }
3914 break;
3915 case llvm::MachO::CPUTypeX86_64:
3916 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3917 {
3918 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3919 start_address = m_data.GetU64(&offset);
3920 done = true;
3921 }
3922 break;
3923 default:
3924 return m_entry_point_address;
3925 }
3926 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3927 if (done)
3928 break;
3929 offset += count * 4;
3930 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003931 }
Greg Claytona1743492012-03-13 23:14:29 +00003932 break;
3933 case LoadCommandMain:
Sean Callanan226b70c2012-03-08 02:39:03 +00003934 {
Greg Claytona1743492012-03-13 23:14:29 +00003935 ConstString text_segment_name ("__TEXT");
3936 uint64_t entryoffset = m_data.GetU64(&offset);
3937 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3938 if (text_segment_sp)
3939 {
3940 done = true;
3941 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3942 }
Sean Callanan226b70c2012-03-08 02:39:03 +00003943 }
Greg Claytona1743492012-03-13 23:14:29 +00003944
3945 default:
3946 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00003947 }
Greg Claytona1743492012-03-13 23:14:29 +00003948 if (done)
3949 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00003950
Greg Claytona1743492012-03-13 23:14:29 +00003951 // Go to the next load command:
3952 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00003953 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003954
Greg Claytona1743492012-03-13 23:14:29 +00003955 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00003956 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00003957 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00003958 // of this ObjectFile:
3959 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00003960 {
Greg Claytona1743492012-03-13 23:14:29 +00003961 m_entry_point_address.Clear();
3962 }
3963 }
3964 else
3965 {
3966 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3967 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003968
Greg Claytona1743492012-03-13 23:14:29 +00003969 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00003970
Greg Claytona1743492012-03-13 23:14:29 +00003971 if (module_sp)
3972 {
3973 SymbolContextList contexts;
3974 SymbolContext context;
3975 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3976 {
3977 if (contexts.GetContextAtIndex(0, context))
3978 m_entry_point_address = context.symbol->GetAddress();
3979 }
Greg Claytone72dfb32012-02-24 01:59:29 +00003980 }
3981 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003982 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003983
Jim Ingham672e6f52011-03-07 23:44:08 +00003984 return m_entry_point_address;
3985
3986}
3987
Greg Claytonc9660542012-02-05 02:38:54 +00003988lldb_private::Address
3989ObjectFileMachO::GetHeaderAddress ()
3990{
3991 lldb_private::Address header_addr;
3992 SectionList *section_list = GetSectionList();
3993 if (section_list)
3994 {
3995 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3996 if (text_segment_sp)
3997 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003998 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00003999 header_addr.SetOffset (0);
4000 }
4001 }
4002 return header_addr;
4003}
4004
Greg Claytonc3776bf2012-02-09 06:16:32 +00004005uint32_t
4006ObjectFileMachO::GetNumThreadContexts ()
4007{
Greg Claytona1743492012-03-13 23:14:29 +00004008 ModuleSP module_sp(GetModule());
4009 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004010 {
Greg Claytona1743492012-03-13 23:14:29 +00004011 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4012 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004013 {
Greg Claytona1743492012-03-13 23:14:29 +00004014 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00004015 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004016 FileRangeArray::Entry file_range;
4017 thread_command thread_cmd;
4018 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004019 {
Greg Claytona1743492012-03-13 23:14:29 +00004020 const uint32_t cmd_offset = offset;
4021 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
4022 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004023
Greg Claytona1743492012-03-13 23:14:29 +00004024 if (thread_cmd.cmd == LoadCommandThread)
4025 {
4026 file_range.SetRangeBase (offset);
4027 file_range.SetByteSize (thread_cmd.cmdsize - 8);
4028 m_thread_context_offsets.Append (file_range);
4029 }
4030 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00004031 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004032 }
4033 }
4034 return m_thread_context_offsets.GetSize();
4035}
4036
4037lldb::RegisterContextSP
4038ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
4039{
Greg Claytonc3776bf2012-02-09 06:16:32 +00004040 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00004041
Greg Claytona1743492012-03-13 23:14:29 +00004042 ModuleSP module_sp(GetModule());
4043 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004044 {
Greg Claytona1743492012-03-13 23:14:29 +00004045 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4046 if (!m_thread_context_offsets_valid)
4047 GetNumThreadContexts ();
4048
4049 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00004050 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00004051 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004052
4053 DataExtractor data (m_data,
4054 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00004055 thread_context_file_range->GetByteSize());
4056
4057 switch (m_header.cputype)
4058 {
4059 case llvm::MachO::CPUTypeARM:
4060 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
4061 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004062
Jim Ingham28eb5712012-10-12 17:34:26 +00004063 case llvm::MachO::CPUTypeI386:
4064 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
4065 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004066
Jim Ingham28eb5712012-10-12 17:34:26 +00004067 case llvm::MachO::CPUTypeX86_64:
4068 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
4069 break;
4070 }
Greg Claytona1743492012-03-13 23:14:29 +00004071 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004072 }
4073 return reg_ctx_sp;
4074}
4075
Greg Claytonc9660542012-02-05 02:38:54 +00004076
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004077ObjectFile::Type
4078ObjectFileMachO::CalculateType()
4079{
4080 switch (m_header.filetype)
4081 {
4082 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4083 if (GetAddressByteSize () == 4)
4084 {
4085 // 32 bit kexts are just object files, but they do have a valid
4086 // UUID load command.
4087 UUID uuid;
4088 if (GetUUID(&uuid))
4089 {
4090 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004091 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004092 // "OSKextGetCurrentIdentifier" as this is required of kexts
4093 if (m_strata == eStrataInvalid)
4094 m_strata = eStrataKernel;
4095 return eTypeSharedLibrary;
4096 }
4097 }
4098 return eTypeObjectFile;
4099
4100 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
4101 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
4102 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
4103 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
4104 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
4105 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
4106 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
4107 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
4108 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
4109 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
4110 default:
4111 break;
4112 }
4113 return eTypeUnknown;
4114}
4115
4116ObjectFile::Strata
4117ObjectFileMachO::CalculateStrata()
4118{
4119 switch (m_header.filetype)
4120 {
4121 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4122 {
4123 // 32 bit kexts are just object files, but they do have a valid
4124 // UUID load command.
4125 UUID uuid;
4126 if (GetUUID(&uuid))
4127 {
4128 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004129 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004130 // "OSKextGetCurrentIdentifier" as this is required of kexts
4131 if (m_type == eTypeInvalid)
4132 m_type = eTypeSharedLibrary;
4133
4134 return eStrataKernel;
4135 }
4136 }
4137 return eStrataUnknown;
4138
4139 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
4140 // Check for the MH_DYLDLINK bit in the flags
4141 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callanan49bce8e2012-02-10 20:22:35 +00004142 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004143 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00004144 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004145 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00004146 {
4147 SectionList *section_list = GetSectionList();
4148 if (section_list)
4149 {
4150 static ConstString g_kld_section_name ("__KLD");
4151 if (section_list->FindSectionByName(g_kld_section_name))
4152 return eStrataKernel;
4153 }
4154 }
4155 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004156
4157 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4158 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callanan49bce8e2012-02-10 20:22:35 +00004159 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004160 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4161 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4162 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4163 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4164 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4165 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4166 default:
4167 break;
4168 }
4169 return eStrataUnknown;
4170}
4171
4172
Greg Claytonc2ff9312012-02-22 19:41:02 +00004173uint32_t
4174ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4175{
Greg Claytona1743492012-03-13 23:14:29 +00004176 ModuleSP module_sp(GetModule());
4177 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004178 {
Greg Claytona1743492012-03-13 23:14:29 +00004179 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4180 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004181 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004182 uint32_t version_cmd = 0;
4183 uint64_t version = 0;
4184 uint32_t i;
4185 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004186 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004187 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004188 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4189 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004190
Greg Claytona1743492012-03-13 23:14:29 +00004191 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004192 {
Greg Claytona1743492012-03-13 23:14:29 +00004193 if (version_cmd == 0)
4194 {
4195 version_cmd = load_cmd.cmd;
4196 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4197 break;
4198 version = load_cmd.dylib.current_version;
4199 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004200 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00004201 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00004202 }
Greg Claytona1743492012-03-13 23:14:29 +00004203 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004204 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004205
Greg Claytona1743492012-03-13 23:14:29 +00004206 if (version_cmd == LoadCommandDylibIdent)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004207 {
Greg Claytona1743492012-03-13 23:14:29 +00004208 if (versions != NULL && num_versions > 0)
4209 {
4210 if (num_versions > 0)
4211 versions[0] = (version & 0xFFFF0000ull) >> 16;
4212 if (num_versions > 1)
4213 versions[1] = (version & 0x0000FF00ull) >> 8;
4214 if (num_versions > 2)
4215 versions[2] = (version & 0x000000FFull);
4216 // Fill in an remaining version numbers with invalid values
4217 for (i=3; i<num_versions; ++i)
4218 versions[i] = UINT32_MAX;
4219 }
4220 // The LC_ID_DYLIB load command has a version with 3 version numbers
4221 // in it, so always return 3
4222 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004223 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00004224 }
4225 return false;
4226}
4227
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004228bool
Greg Clayton514487e2011-02-15 21:59:32 +00004229ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004230{
Greg Claytona1743492012-03-13 23:14:29 +00004231 ModuleSP module_sp(GetModule());
4232 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00004233 {
Greg Claytona1743492012-03-13 23:14:29 +00004234 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4235 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +00004236
Greg Claytona1743492012-03-13 23:14:29 +00004237 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda4e7511e2013-03-06 23:19:17 +00004238 // debugs at the addresses in the file itself. Below we set the OS to
Greg Claytona1743492012-03-13 23:14:29 +00004239 // unknown to make sure we use the DynamicLoaderStatic()...
4240 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4241 {
4242 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4243 }
4244 return true;
Greg Clayton593577a2011-09-21 03:57:31 +00004245 }
Greg Claytona1743492012-03-13 23:14:29 +00004246 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004247}
4248
4249
Jason Molenda0e0954c2013-04-16 06:24:42 +00004250UUID
4251ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4252{
4253 UUID uuid;
4254 if (process)
4255 {
4256 addr_t all_image_infos = process->GetImageInfoAddress();
4257
4258 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4259 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4260 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4261 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4262
4263 Error err;
4264 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4265 if (version_or_magic != -1
4266 && version_or_magic != HeaderMagic32
4267 && version_or_magic != HeaderMagic32Swapped
4268 && version_or_magic != HeaderMagic64
4269 && version_or_magic != HeaderMagic64Swapped
4270 && version_or_magic >= 13)
4271 {
4272 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4273 int wordsize = process->GetAddressByteSize();
4274 if (wordsize == 8)
4275 {
4276 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4277 }
4278 if (wordsize == 4)
4279 {
4280 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4281 }
4282 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4283 {
4284 uuid_t shared_cache_uuid;
4285 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4286 {
4287 uuid.SetBytes (shared_cache_uuid);
4288 }
4289 }
4290 }
4291 }
4292 return uuid;
4293}
4294
4295UUID
4296ObjectFileMachO::GetLLDBSharedCacheUUID ()
4297{
4298 UUID uuid;
4299#if defined (__APPLE__) && defined (__arm__)
4300 uint8_t *(*dyld_get_all_image_infos)(void);
4301 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4302 if (dyld_get_all_image_infos)
4303 {
4304 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4305 if (dyld_all_image_infos_address)
4306 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00004307 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4308 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00004309 {
Jason Molenda42b69fa2013-04-16 22:56:17 +00004310 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 +00004311 uuid.SetBytes (sharedCacheUUID_address);
4312 }
4313 }
4314 }
4315#endif
4316 return uuid;
4317}
4318
4319
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004320//------------------------------------------------------------------
4321// PluginInterface protocol
4322//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00004323lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004324ObjectFileMachO::GetPluginName()
4325{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004326 return GetPluginNameStatic();
4327}
4328
4329uint32_t
4330ObjectFileMachO::GetPluginVersion()
4331{
4332 return 1;
4333}
4334