blob: ff66ba93faf28f511478f2f8883e1e042c917637 [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);
526 if (spec.GetArchitecture().IsValid())
527 {
528 GetUUID (header, data, data_offset, spec.GetUUID());
529 specs.Append(spec);
530 }
531 }
532 }
533 }
534 return specs.GetSize() - initial_count;
535}
536
537
Greg Claytonc9660542012-02-05 02:38:54 +0000538
539const ConstString &
540ObjectFileMachO::GetSegmentNameTEXT()
541{
542 static ConstString g_segment_name_TEXT ("__TEXT");
543 return g_segment_name_TEXT;
544}
545
546const ConstString &
547ObjectFileMachO::GetSegmentNameDATA()
548{
549 static ConstString g_segment_name_DATA ("__DATA");
550 return g_segment_name_DATA;
551}
552
553const ConstString &
554ObjectFileMachO::GetSegmentNameOBJC()
555{
556 static ConstString g_segment_name_OBJC ("__OBJC");
557 return g_segment_name_OBJC;
558}
559
560const ConstString &
561ObjectFileMachO::GetSegmentNameLINKEDIT()
562{
563 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
564 return g_section_name_LINKEDIT;
565}
566
567const ConstString &
568ObjectFileMachO::GetSectionNameEHFrame()
569{
570 static ConstString g_section_name_eh_frame ("__eh_frame");
571 return g_section_name_eh_frame;
572}
573
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574bool
Jason Molenda4e7511e2013-03-06 23:19:17 +0000575ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
576 lldb::addr_t data_offset,
Greg Clayton44435ed2012-01-12 05:25:17 +0000577 lldb::addr_t data_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578{
Greg Clayton44435ed2012-01-12 05:25:17 +0000579 DataExtractor data;
580 data.SetData (data_sp, data_offset, data_length);
Greg Claytonc7bece562013-01-25 18:06:21 +0000581 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582 uint32_t magic = data.GetU32(&offset);
583 return MachHeaderSizeFromMagic(magic) != 0;
584}
585
586
Greg Clayton5ce9c562013-02-06 17:22:03 +0000587ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
588 DataBufferSP& data_sp,
589 lldb::offset_t data_offset,
590 const FileSpec* file,
591 lldb::offset_t file_offset,
592 lldb::offset_t length) :
593 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000594 m_mach_segments(),
595 m_mach_sections(),
596 m_entry_point_address(),
597 m_thread_context_offsets(),
598 m_thread_context_offsets_valid(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599{
Greg Clayton72b77eb2011-02-04 21:13:05 +0000600 ::memset (&m_header, 0, sizeof(m_header));
601 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602}
603
Greg Claytone72dfb32012-02-24 01:59:29 +0000604ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000605 lldb::DataBufferSP& header_data_sp,
606 const lldb::ProcessSP &process_sp,
607 lldb::addr_t header_addr) :
Greg Claytone72dfb32012-02-24 01:59:29 +0000608 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000609 m_mach_segments(),
610 m_mach_sections(),
611 m_entry_point_address(),
612 m_thread_context_offsets(),
613 m_thread_context_offsets_valid(false)
Greg Claytonc9660542012-02-05 02:38:54 +0000614{
615 ::memset (&m_header, 0, sizeof(m_header));
616 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
617}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618
619ObjectFileMachO::~ObjectFileMachO()
620{
621}
622
Greg Claytonf4d6de62013-04-24 22:29:28 +0000623bool
624ObjectFileMachO::ParseHeader (DataExtractor &data,
625 lldb::offset_t *data_offset_ptr,
626 llvm::MachO::mach_header &header)
627{
628 data.SetByteOrder (lldb::endian::InlHostByteOrder());
629 // Leave magic in the original byte order
630 header.magic = data.GetU32(data_offset_ptr);
631 bool can_parse = false;
632 bool is_64_bit = false;
633 switch (header.magic)
634 {
635 case HeaderMagic32:
636 data.SetByteOrder (lldb::endian::InlHostByteOrder());
637 data.SetAddressByteSize(4);
638 can_parse = true;
639 break;
640
641 case HeaderMagic64:
642 data.SetByteOrder (lldb::endian::InlHostByteOrder());
643 data.SetAddressByteSize(8);
644 can_parse = true;
645 is_64_bit = true;
646 break;
647
648 case HeaderMagic32Swapped:
649 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
650 data.SetAddressByteSize(4);
651 can_parse = true;
652 break;
653
654 case HeaderMagic64Swapped:
655 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
656 data.SetAddressByteSize(8);
657 is_64_bit = true;
658 can_parse = true;
659 break;
660
661 default:
662 break;
663 }
664
665 if (can_parse)
666 {
667 data.GetU32(data_offset_ptr, &header.cputype, 6);
668 if (is_64_bit)
669 *data_offset_ptr += 4;
670 return true;
671 }
672 else
673 {
674 memset(&header, 0, sizeof(header));
675 }
676 return false;
677}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678
679bool
680ObjectFileMachO::ParseHeader ()
681{
Greg Claytona1743492012-03-13 23:14:29 +0000682 ModuleSP module_sp(GetModule());
683 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 {
Greg Claytona1743492012-03-13 23:14:29 +0000685 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
686 bool can_parse = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000687 lldb::offset_t offset = 0;
Greg Clayton7fb56d02011-02-01 01:31:41 +0000688 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +0000689 // Leave magic in the original byte order
690 m_header.magic = m_data.GetU32(&offset);
691 switch (m_header.magic)
Greg Claytonc9660542012-02-05 02:38:54 +0000692 {
Greg Claytona1743492012-03-13 23:14:29 +0000693 case HeaderMagic32:
694 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
695 m_data.SetAddressByteSize(4);
696 can_parse = true;
697 break;
698
699 case HeaderMagic64:
700 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
701 m_data.SetAddressByteSize(8);
702 can_parse = true;
703 break;
704
705 case HeaderMagic32Swapped:
706 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
707 m_data.SetAddressByteSize(4);
708 can_parse = true;
709 break;
710
711 case HeaderMagic64Swapped:
712 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
713 m_data.SetAddressByteSize(8);
714 can_parse = true;
715 break;
716
717 default:
718 break;
Greg Claytonc9660542012-02-05 02:38:54 +0000719 }
Greg Claytona1743492012-03-13 23:14:29 +0000720
721 if (can_parse)
722 {
723 m_data.GetU32(&offset, &m_header.cputype, 6);
724
725 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +0000726
Greg Claytond97ec1c2012-11-16 21:36:10 +0000727 // Check if the module has a required architecture
728 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callananbf4b7be2012-12-13 22:07:14 +0000729 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Claytond97ec1c2012-11-16 21:36:10 +0000730 return false;
731
Greg Claytona1743492012-03-13 23:14:29 +0000732 if (SetModulesArchitecture (mach_arch))
733 {
734 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
735 if (m_data.GetByteSize() < header_and_lc_size)
736 {
737 DataBufferSP data_sp;
738 ProcessSP process_sp (m_process_wp.lock());
739 if (process_sp)
740 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000741 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Claytona1743492012-03-13 23:14:29 +0000742 }
743 else
744 {
745 // Read in all only the load command data from the file on disk
Greg Clayton5ce9c562013-02-06 17:22:03 +0000746 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Claytona1743492012-03-13 23:14:29 +0000747 if (data_sp->GetByteSize() != header_and_lc_size)
748 return false;
749 }
750 if (data_sp)
751 m_data.SetData (data_sp);
752 }
753 }
754 return true;
755 }
756 else
757 {
758 memset(&m_header, 0, sizeof(struct mach_header));
759 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760 }
761 return false;
762}
763
764
765ByteOrder
766ObjectFileMachO::GetByteOrder () const
767{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768 return m_data.GetByteOrder ();
769}
770
Jim Ingham5aee1622010-08-09 23:31:02 +0000771bool
772ObjectFileMachO::IsExecutable() const
773{
774 return m_header.filetype == HeaderFileTypeExecutable;
775}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776
Greg Claytonc7bece562013-01-25 18:06:21 +0000777uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000778ObjectFileMachO::GetAddressByteSize () const
779{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780 return m_data.GetAddressByteSize ();
781}
782
Greg Claytone0d378b2011-03-24 21:19:54 +0000783AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +0000784ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
785{
786 Symtab *symtab = GetSymtab();
787 if (symtab)
788 {
789 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
790 if (symbol)
791 {
Greg Claytone7612132012-03-07 21:03:09 +0000792 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +0000793 {
Greg Claytone7612132012-03-07 21:03:09 +0000794 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +0000795 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +0000796 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000797 const SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000798 switch (section_type)
799 {
800 case eSectionTypeInvalid: return eAddressClassUnknown;
801 case eSectionTypeCode:
802 if (m_header.cputype == llvm::MachO::CPUTypeARM)
803 {
804 // For ARM we have a bit in the n_desc field of the symbol
805 // that tells us ARM/Thumb which is bit 0x0008.
806 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
807 return eAddressClassCodeAlternateISA;
808 }
809 return eAddressClassCode;
810
811 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton5009f9d2011-10-27 17:55:14 +0000812 case eSectionTypeData:
813 case eSectionTypeDataCString:
814 case eSectionTypeDataCStringPointers:
815 case eSectionTypeDataSymbolAddress:
816 case eSectionTypeData4:
817 case eSectionTypeData8:
818 case eSectionTypeData16:
819 case eSectionTypeDataPointers:
820 case eSectionTypeZeroFill:
821 case eSectionTypeDataObjCMessageRefs:
822 case eSectionTypeDataObjCCFStrings:
823 return eAddressClassData;
824 case eSectionTypeDebug:
825 case eSectionTypeDWARFDebugAbbrev:
826 case eSectionTypeDWARFDebugAranges:
827 case eSectionTypeDWARFDebugFrame:
828 case eSectionTypeDWARFDebugInfo:
829 case eSectionTypeDWARFDebugLine:
830 case eSectionTypeDWARFDebugLoc:
831 case eSectionTypeDWARFDebugMacInfo:
832 case eSectionTypeDWARFDebugPubNames:
833 case eSectionTypeDWARFDebugPubTypes:
834 case eSectionTypeDWARFDebugRanges:
835 case eSectionTypeDWARFDebugStr:
836 case eSectionTypeDWARFAppleNames:
837 case eSectionTypeDWARFAppleTypes:
838 case eSectionTypeDWARFAppleNamespaces:
839 case eSectionTypeDWARFAppleObjC:
840 return eAddressClassDebug;
Greg Claytonded470d2011-03-19 01:12:21 +0000841 case eSectionTypeEHFrame: return eAddressClassRuntime;
Michael Sartaina7499c92013-07-01 19:45:50 +0000842 case eSectionTypeELFSymbolTable:
843 case eSectionTypeELFDynamicSymbols:
844 case eSectionTypeELFRelocationEntries:
845 case eSectionTypeELFDynamicLinkInfo:
Greg Claytonded470d2011-03-19 01:12:21 +0000846 case eSectionTypeOther: return eAddressClassUnknown;
847 }
848 }
849 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000850
Greg Claytone0d378b2011-03-24 21:19:54 +0000851 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000852 switch (symbol_type)
853 {
854 case eSymbolTypeAny: return eAddressClassUnknown;
855 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000856
Greg Claytonded470d2011-03-19 01:12:21 +0000857 case eSymbolTypeCode:
858 case eSymbolTypeTrampoline:
Greg Clayton059f7242013-02-27 21:16:04 +0000859 case eSymbolTypeResolver:
Greg Claytonded470d2011-03-19 01:12:21 +0000860 if (m_header.cputype == llvm::MachO::CPUTypeARM)
861 {
862 // For ARM we have a bit in the n_desc field of the symbol
863 // that tells us ARM/Thumb which is bit 0x0008.
864 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
865 return eAddressClassCodeAlternateISA;
866 }
867 return eAddressClassCode;
868
869 case eSymbolTypeData: return eAddressClassData;
870 case eSymbolTypeRuntime: return eAddressClassRuntime;
871 case eSymbolTypeException: return eAddressClassRuntime;
872 case eSymbolTypeSourceFile: return eAddressClassDebug;
873 case eSymbolTypeHeaderFile: return eAddressClassDebug;
874 case eSymbolTypeObjectFile: return eAddressClassDebug;
875 case eSymbolTypeCommonBlock: return eAddressClassDebug;
876 case eSymbolTypeBlock: return eAddressClassDebug;
877 case eSymbolTypeLocal: return eAddressClassData;
878 case eSymbolTypeParam: return eAddressClassData;
879 case eSymbolTypeVariable: return eAddressClassData;
880 case eSymbolTypeVariableType: return eAddressClassDebug;
881 case eSymbolTypeLineEntry: return eAddressClassDebug;
882 case eSymbolTypeLineHeader: return eAddressClassDebug;
883 case eSymbolTypeScopeBegin: return eAddressClassDebug;
884 case eSymbolTypeScopeEnd: return eAddressClassDebug;
885 case eSymbolTypeAdditional: return eAddressClassUnknown;
886 case eSymbolTypeCompiler: return eAddressClassDebug;
887 case eSymbolTypeInstrumentation:return eAddressClassDebug;
888 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +0000889 case eSymbolTypeObjCClass: return eAddressClassRuntime;
890 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
891 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +0000892 }
893 }
894 }
895 return eAddressClassUnknown;
896}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897
898Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +0000899ObjectFileMachO::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900{
Greg Claytona1743492012-03-13 23:14:29 +0000901 ModuleSP module_sp(GetModule());
902 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903 {
Greg Claytona1743492012-03-13 23:14:29 +0000904 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
905 if (m_symtab_ap.get() == NULL)
906 {
907 m_symtab_ap.reset(new Symtab(this));
908 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton3046e662013-07-10 01:23:25 +0000909 ParseSymtab ();
Greg Claytona1743492012-03-13 23:14:29 +0000910 m_symtab_ap->Finalize ();
911 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000912 }
913 return m_symtab_ap.get();
914}
915
Greg Clayton3046e662013-07-10 01:23:25 +0000916bool
917ObjectFileMachO::IsStripped ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918{
Greg Clayton3046e662013-07-10 01:23:25 +0000919 if (m_dysymtab.cmd == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000920 {
Greg Clayton3046e662013-07-10 01:23:25 +0000921 ModuleSP module_sp(GetModule());
922 if (module_sp)
Greg Claytona1743492012-03-13 23:14:29 +0000923 {
Greg Clayton3046e662013-07-10 01:23:25 +0000924 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
925 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton4d78c402012-05-25 18:09:55 +0000926 {
Greg Clayton3046e662013-07-10 01:23:25 +0000927 const lldb::offset_t load_cmd_offset = offset;
928
929 load_command lc;
930 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
931 break;
932 if (lc.cmd == LoadCommandDynamicSymtabInfo)
Greg Clayton4d78c402012-05-25 18:09:55 +0000933 {
Greg Clayton3046e662013-07-10 01:23:25 +0000934 m_dysymtab.cmd = lc.cmd;
935 m_dysymtab.cmdsize = lc.cmdsize;
936 if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
937 {
938 // Clear m_dysymtab if we were unable to read all items from the load command
939 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
940 }
Greg Clayton4d78c402012-05-25 18:09:55 +0000941 }
Greg Clayton3046e662013-07-10 01:23:25 +0000942 offset = load_cmd_offset + lc.cmdsize;
Greg Clayton4d78c402012-05-25 18:09:55 +0000943 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000944 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000945 }
Greg Clayton3046e662013-07-10 01:23:25 +0000946 if (m_dysymtab.cmd)
947 return m_dysymtab.nlocalsym == 0;
948 return false;
949}
Greg Clayton1eac0c72012-04-24 03:06:13 +0000950
Greg Clayton3046e662013-07-10 01:23:25 +0000951void
952ObjectFileMachO::CreateSections (SectionList &unified_section_list)
953{
954 if (!m_sections_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000955 {
Greg Clayton3046e662013-07-10 01:23:25 +0000956 m_sections_ap.reset(new SectionList());
957
958 const bool is_dsym = (m_header.filetype == HeaderFileTypeDSYM);
959 lldb::user_id_t segID = 0;
960 lldb::user_id_t sectID = 0;
961 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
962 uint32_t i;
963 const bool is_core = GetType() == eTypeCoreFile;
964 //bool dump_sections = false;
965 ModuleSP module_sp (GetModule());
966 // First look up any LC_ENCRYPTION_INFO load commands
967 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
968 EncryptedFileRanges encrypted_file_ranges;
969 encryption_info_command encryption_cmd;
970 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971 {
Greg Clayton3046e662013-07-10 01:23:25 +0000972 const lldb::offset_t load_cmd_offset = offset;
973 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
974 break;
975
976 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977 {
Greg Clayton3046e662013-07-10 01:23:25 +0000978 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000979 {
Greg Clayton3046e662013-07-10 01:23:25 +0000980 if (encryption_cmd.cryptid != 0)
Greg Claytond37d6922013-04-16 16:51:19 +0000981 {
Greg Clayton3046e662013-07-10 01:23:25 +0000982 EncryptedFileRanges::Entry entry;
983 entry.SetRangeBase(encryption_cmd.cryptoff);
984 entry.SetByteSize(encryption_cmd.cryptsize);
985 encrypted_file_ranges.Append(entry);
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000986 }
987 }
Greg Clayton3046e662013-07-10 01:23:25 +0000988 }
989 offset = load_cmd_offset + encryption_cmd.cmdsize;
990 }
991
992 offset = MachHeaderSizeFromMagic(m_header.magic);
993
994 struct segment_command_64 load_cmd;
995 for (i=0; i<m_header.ncmds; ++i)
996 {
997 const lldb::offset_t load_cmd_offset = offset;
998 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
999 break;
1000
1001 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
1002 {
1003 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004 {
Greg Clayton3046e662013-07-10 01:23:25 +00001005 bool add_section = true;
1006 bool add_to_unified = true;
1007 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 +00001008
Greg Clayton3046e662013-07-10 01:23:25 +00001009 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1010 if (is_dsym && unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011 {
Greg Clayton3046e662013-07-10 01:23:25 +00001012 if (const_segname == GetSegmentNameLINKEDIT())
1013 {
1014 // We need to keep the __LINKEDIT segment private to this object file only
1015 add_to_unified = false;
1016 }
1017 else
1018 {
1019 // This is the dSYM file and this section has already been created by
1020 // the object file, no need to create it.
1021 add_section = false;
1022 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023 }
Greg Clayton3046e662013-07-10 01:23:25 +00001024 load_cmd.vmaddr = m_data.GetAddress(&offset);
1025 load_cmd.vmsize = m_data.GetAddress(&offset);
1026 load_cmd.fileoff = m_data.GetAddress(&offset);
1027 load_cmd.filesize = m_data.GetAddress(&offset);
1028 if (m_length != 0 && load_cmd.filesize != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029 {
Greg Clayton3046e662013-07-10 01:23:25 +00001030 if (load_cmd.fileoff > m_length)
1031 {
1032 // We have a load command that says it extends past the end of hte file. This is likely
1033 // a corrupt file. We don't have any way to return an error condition here (this method
1034 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1035 // is null out the SectionList vector and if a process has been set up, dump a message
1036 // to stdout. The most common case here is core file debugging with a truncated file.
1037 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
1038 GetModule()->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 ")",
1039 i,
1040 lc_segment_name,
1041 load_cmd.fileoff,
1042 m_length);
1043
1044 load_cmd.fileoff = 0;
1045 load_cmd.filesize = 0;
1046 }
1047
1048 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1049 {
1050 // We have a load command that says it extends past the end of hte file. This is likely
1051 // a corrupt file. We don't have any way to return an error condition here (this method
1052 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1053 // is null out the SectionList vector and if a process has been set up, dump a message
1054 // to stdout. The most common case here is core file debugging with a truncated file.
1055 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
1056 GetModule()->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",
1057 i,
1058 lc_segment_name,
1059 load_cmd.fileoff + load_cmd.filesize,
1060 m_length);
1061
1062 // Tuncase the length
1063 load_cmd.filesize = m_length - load_cmd.fileoff;
1064 }
1065 }
1066 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1067 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068
Greg Clayton3046e662013-07-10 01:23:25 +00001069 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070
Greg Clayton3046e662013-07-10 01:23:25 +00001071 // Keep a list of mach segments around in case we need to
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072 // get at data that isn't stored in the abstracted Sections.
Greg Clayton3046e662013-07-10 01:23:25 +00001073 m_mach_segments.push_back (load_cmd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074
Greg Clayton3046e662013-07-10 01:23:25 +00001075 // Use a segment ID of the segment index shifted left by 8 so they
1076 // never conflict with any of the sections.
1077 SectionSP segment_sp;
1078 if (add_section && (const_segname || is_core))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079 {
Greg Clayton3046e662013-07-10 01:23:25 +00001080 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
1081 this, // Object file to which this sections belongs
1082 ++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
1083 const_segname, // Name of this section
1084 eSectionTypeContainer, // This section is a container of other sections.
1085 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1086 load_cmd.vmsize, // VM size in bytes of this section
1087 load_cmd.fileoff, // Offset to the data for this section in the file
1088 load_cmd.filesize, // Size in bytes of this section as found in the the file
1089 load_cmd.flags)); // Flags for this section
Greg Clayton8d38ac42010-06-28 23:51:11 +00001090
Greg Clayton3046e662013-07-10 01:23:25 +00001091 segment_sp->SetIsEncrypted (segment_is_encrypted);
1092 m_sections_ap->AddSection(segment_sp);
1093 if (add_to_unified)
1094 unified_section_list.AddSection(segment_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095 }
Greg Clayton3046e662013-07-10 01:23:25 +00001096 else if (unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001097 {
Greg Clayton3046e662013-07-10 01:23:25 +00001098 m_sections_ap->AddSection(unified_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100
Greg Clayton3046e662013-07-10 01:23:25 +00001101 struct section_64 sect64;
1102 ::memset (&sect64, 0, sizeof(sect64));
1103 // Push a section into our mach sections for the section at
1104 // index zero (NListSectionNoSection) if we don't have any
1105 // mach sections yet...
1106 if (m_mach_sections.empty())
1107 m_mach_sections.push_back(sect64);
1108 uint32_t segment_sect_idx;
1109 const lldb::user_id_t first_segment_sectID = sectID + 1;
1110
1111
1112 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
1113 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114 {
Greg Clayton3046e662013-07-10 01:23:25 +00001115 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
Greg Clayton89411422010-10-08 00:21:05 +00001116 break;
Greg Clayton3046e662013-07-10 01:23:25 +00001117 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1118 break;
1119 sect64.addr = m_data.GetAddress(&offset);
1120 sect64.size = m_data.GetAddress(&offset);
1121
1122 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1123 break;
1124
1125 // Keep a list of mach sections around in case we need to
1126 // get at data that isn't stored in the abstracted Sections.
1127 m_mach_sections.push_back (sect64);
1128
1129 if (add_section)
1130 {
1131 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1132 if (!const_segname)
1133 {
1134 // We have a segment with no name so we need to conjure up
1135 // segments that correspond to the section's segname if there
1136 // isn't already such a section. If there is such a section,
1137 // we resize the section so that it spans all sections.
1138 // We also mark these sections as fake so address matches don't
1139 // hit if they land in the gaps between the child sections.
1140 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1141 segment_sp = unified_section_list.FindSectionByName (const_segname);
1142 if (segment_sp.get())
1143 {
1144 Section *segment = segment_sp.get();
1145 // Grow the section size as needed.
1146 const lldb::addr_t sect64_min_addr = sect64.addr;
1147 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1148 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1149 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1150 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1151 if (sect64_min_addr >= curr_seg_min_addr)
1152 {
1153 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1154 // Only grow the section size if needed
1155 if (new_seg_byte_size > curr_seg_byte_size)
1156 segment->SetByteSize (new_seg_byte_size);
1157 }
1158 else
1159 {
1160 // We need to change the base address of the segment and
1161 // adjust the child section offsets for all existing children.
1162 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1163 segment->Slide(slide_amount, false);
1164 segment->GetChildren().Slide(-slide_amount, false);
1165 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1166 }
1167
1168 // Grow the section size as needed.
1169 if (sect64.offset)
1170 {
1171 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1172 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1173
1174 const lldb::addr_t section_min_file_offset = sect64.offset;
1175 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1176 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1177 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1178 segment->SetFileOffset (new_file_offset);
1179 segment->SetFileSize (new_file_size);
1180 }
1181 }
1182 else
1183 {
1184 // Create a fake section for the section's named segment
1185 segment_sp.reset(new Section (segment_sp, // Parent section
1186 module_sp, // Module to which this section belongs
1187 this, // Object file to which this section belongs
1188 ++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
1189 const_segname, // Name of this section
1190 eSectionTypeContainer, // This section is a container of other sections.
1191 sect64.addr, // File VM address == addresses as they are found in the object file
1192 sect64.size, // VM size in bytes of this section
1193 sect64.offset, // Offset to the data for this section in the file
1194 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1195 load_cmd.flags)); // Flags for this section
1196 segment_sp->SetIsFake(true);
1197
1198 m_sections_ap->AddSection(segment_sp);
1199 if (add_to_unified)
1200 unified_section_list.AddSection(segment_sp);
1201 segment_sp->SetIsEncrypted (segment_is_encrypted);
1202 }
1203 }
1204 assert (segment_sp.get());
1205
1206 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
1207 static ConstString g_sect_name_objc_data ("__objc_data");
1208 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1209 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1210 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1211 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1212 static ConstString g_sect_name_objc_const ("__objc_const");
1213 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1214 static ConstString g_sect_name_cfstring ("__cfstring");
1215
1216 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1217 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1218 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1219 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1220 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1221 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1222 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1223 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1224 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1225 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1226 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1227 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1228 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1229 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1230 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1231 static ConstString g_sect_name_eh_frame ("__eh_frame");
1232 static ConstString g_sect_name_DATA ("__DATA");
1233 static ConstString g_sect_name_TEXT ("__TEXT");
1234
1235 SectionType sect_type = eSectionTypeOther;
1236
1237 if (section_name == g_sect_name_dwarf_debug_abbrev)
1238 sect_type = eSectionTypeDWARFDebugAbbrev;
1239 else if (section_name == g_sect_name_dwarf_debug_aranges)
1240 sect_type = eSectionTypeDWARFDebugAranges;
1241 else if (section_name == g_sect_name_dwarf_debug_frame)
1242 sect_type = eSectionTypeDWARFDebugFrame;
1243 else if (section_name == g_sect_name_dwarf_debug_info)
1244 sect_type = eSectionTypeDWARFDebugInfo;
1245 else if (section_name == g_sect_name_dwarf_debug_line)
1246 sect_type = eSectionTypeDWARFDebugLine;
1247 else if (section_name == g_sect_name_dwarf_debug_loc)
1248 sect_type = eSectionTypeDWARFDebugLoc;
1249 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1250 sect_type = eSectionTypeDWARFDebugMacInfo;
1251 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1252 sect_type = eSectionTypeDWARFDebugPubNames;
1253 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1254 sect_type = eSectionTypeDWARFDebugPubTypes;
1255 else if (section_name == g_sect_name_dwarf_debug_ranges)
1256 sect_type = eSectionTypeDWARFDebugRanges;
1257 else if (section_name == g_sect_name_dwarf_debug_str)
1258 sect_type = eSectionTypeDWARFDebugStr;
1259 else if (section_name == g_sect_name_dwarf_apple_names)
1260 sect_type = eSectionTypeDWARFAppleNames;
1261 else if (section_name == g_sect_name_dwarf_apple_types)
1262 sect_type = eSectionTypeDWARFAppleTypes;
1263 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1264 sect_type = eSectionTypeDWARFAppleNamespaces;
1265 else if (section_name == g_sect_name_dwarf_apple_objc)
1266 sect_type = eSectionTypeDWARFAppleObjC;
1267 else if (section_name == g_sect_name_objc_selrefs)
1268 sect_type = eSectionTypeDataCStringPointers;
1269 else if (section_name == g_sect_name_objc_msgrefs)
1270 sect_type = eSectionTypeDataObjCMessageRefs;
1271 else if (section_name == g_sect_name_eh_frame)
1272 sect_type = eSectionTypeEHFrame;
1273 else if (section_name == g_sect_name_cfstring)
1274 sect_type = eSectionTypeDataObjCCFStrings;
1275 else if (section_name == g_sect_name_objc_data ||
1276 section_name == g_sect_name_objc_classrefs ||
1277 section_name == g_sect_name_objc_superrefs ||
1278 section_name == g_sect_name_objc_const ||
1279 section_name == g_sect_name_objc_classlist)
1280 {
1281 sect_type = eSectionTypeDataPointers;
1282 }
1283
1284 if (sect_type == eSectionTypeOther)
1285 {
1286 switch (mach_sect_type)
1287 {
1288 // TODO: categorize sections by other flags for regular sections
1289 case SectionTypeRegular:
1290 if (segment_sp->GetName() == g_sect_name_TEXT)
1291 sect_type = eSectionTypeCode;
1292 else if (segment_sp->GetName() == g_sect_name_DATA)
1293 sect_type = eSectionTypeData;
1294 else
1295 sect_type = eSectionTypeOther;
1296 break;
1297 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1298 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1299 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1300 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1301 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1302 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1303 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1304 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1305 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1306 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1307 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1308 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1309 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1310 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1311 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1312 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
1313 default: break;
1314 }
1315 }
1316
1317 SectionSP section_sp(new Section (segment_sp,
1318 module_sp,
1319 this,
1320 ++sectID,
1321 section_name,
1322 sect_type,
1323 sect64.addr - segment_sp->GetFileAddress(),
1324 sect64.size,
1325 sect64.offset,
1326 sect64.offset == 0 ? 0 : sect64.size,
1327 sect64.flags));
1328 // Set the section to be encrypted to match the segment
1329
1330 bool section_is_encrypted = false;
1331 if (!segment_is_encrypted && load_cmd.filesize != 0)
1332 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1333
1334 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1335 segment_sp->GetChildren().AddSection(section_sp);
1336
1337 if (segment_sp->IsFake())
1338 {
1339 segment_sp.reset();
1340 const_segname.Clear();
1341 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001342 }
1343 }
Greg Clayton3046e662013-07-10 01:23:25 +00001344 if (segment_sp && is_dsym)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001345 {
Greg Clayton3046e662013-07-10 01:23:25 +00001346 if (first_segment_sectID <= sectID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001347 {
Greg Clayton3046e662013-07-10 01:23:25 +00001348 lldb::user_id_t sect_uid;
1349 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001350 {
Greg Clayton3046e662013-07-10 01:23:25 +00001351 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1352 SectionSP next_section_sp;
1353 if (sect_uid + 1 <= sectID)
1354 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1355
1356 if (curr_section_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357 {
Greg Clayton3046e662013-07-10 01:23:25 +00001358 if (curr_section_sp->GetByteSize() == 0)
1359 {
1360 if (next_section_sp.get() != NULL)
1361 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1362 else
1363 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1364 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365 }
1366 }
1367 }
1368 }
1369 }
1370 }
1371 }
Greg Clayton3046e662013-07-10 01:23:25 +00001372 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
1373 {
1374 m_dysymtab.cmd = load_cmd.cmd;
1375 m_dysymtab.cmdsize = load_cmd.cmdsize;
1376 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1377 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378
Greg Clayton3046e662013-07-10 01:23:25 +00001379 offset = load_cmd_offset + load_cmd.cmdsize;
1380 }
1381
1382// StreamFile s(stdout, false); // REMOVE THIS LINE
1383// s.Printf ("Sections for %s:\n", m_file.GetPath().c_str());// REMOVE THIS LINE
1384// m_sections_ap->Dump(&s, NULL, true, UINT32_MAX);// REMOVE THIS LINE
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001386}
1387
1388class MachSymtabSectionInfo
1389{
1390public:
1391
1392 MachSymtabSectionInfo (SectionList *section_list) :
1393 m_section_list (section_list),
1394 m_section_infos()
1395 {
1396 // Get the number of sections down to a depth of 1 to include
1397 // all segments and their sections, but no other sections that
1398 // may be added for debug map or
1399 m_section_infos.resize(section_list->GetNumSections(1));
1400 }
1401
1402
Greg Claytone72dfb32012-02-24 01:59:29 +00001403 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001404 GetSection (uint8_t n_sect, addr_t file_addr)
1405 {
1406 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00001407 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 if (n_sect < m_section_infos.size())
1409 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001410 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001412 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1413 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001414 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00001415 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001416 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1417 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00001418 }
1419 else
1420 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001421 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00001422 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001423 }
1424 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00001425 {
1426 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00001427 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001428 }
1429 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1430 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1431 {
1432 // Symbol is in section with zero size, but has the same start
1433 // address as the section. This can happen with linker symbols
1434 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00001435 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001436 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001438 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439 }
1440
1441protected:
1442 struct SectionInfo
1443 {
1444 SectionInfo () :
1445 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001446 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447 {
1448 }
1449
1450 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00001451 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001452 };
1453 SectionList *m_section_list;
1454 std::vector<SectionInfo> m_section_infos;
1455};
1456
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001457size_t
Greg Clayton3046e662013-07-10 01:23:25 +00001458ObjectFileMachO::ParseSymtab ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001459{
1460 Timer scoped_timer(__PRETTY_FUNCTION__,
1461 "ObjectFileMachO::ParseSymtab () module = %s",
1462 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001463 ModuleSP module_sp (GetModule());
1464 if (!module_sp)
1465 return 0;
1466
1467 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1468 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1469 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1470 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00001471 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472 uint32_t i;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001473
Greg Clayton5160ce52013-03-27 23:08:40 +00001474 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton77ccca72011-12-30 00:32:24 +00001475
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476 for (i=0; i<m_header.ncmds; ++i)
1477 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001478 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001479 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001480 struct load_command lc;
1481 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001482 break;
1483 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001484 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001485 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001486 case LoadCommandSymtab:
1487 symtab_load_command.cmd = lc.cmd;
1488 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001490 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1491 return 0;
1492 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001493 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001494 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001495 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001496 return 0;
1497 }
1498
1499 if (symtab_load_command.stroff == 0)
1500 {
1501 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001502 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001503 return 0;
1504 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001505
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001506 if (symtab_load_command.nsyms == 0)
1507 {
1508 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001509 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001510 return 0;
1511 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001512
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001513 if (symtab_load_command.strsize == 0)
1514 {
1515 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001516 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001517 return 0;
1518 }
1519 break;
1520
1521 case LoadCommandFunctionStarts:
1522 function_starts_load_command.cmd = lc.cmd;
1523 function_starts_load_command.cmdsize = lc.cmdsize;
1524 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1525 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1526 break;
1527
1528 default:
1529 break;
1530 }
1531 offset = cmd_offset + lc.cmdsize;
1532 }
1533
1534 if (symtab_load_command.cmd)
1535 {
1536 Symtab *symtab = m_symtab_ap.get();
1537 SectionList *section_list = GetSectionList();
1538 if (section_list == NULL)
1539 return 0;
1540
Greg Claytonc7bece562013-01-25 18:06:21 +00001541 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1542 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001543 bool bit_width_32 = addr_byte_size == 4;
1544 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1545
Greg Claytonc7bece562013-01-25 18:06:21 +00001546 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1547 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1548 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00001549 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00001550
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001551 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1552 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00001553 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
Greg Claytonfd814c52013-08-13 01:42:25 +00001554
1555 ProcessSP process_sp (m_process_wp.lock());
1556 Process *process = process_sp.get();
1557
Greg Clayton4c82d422012-05-18 23:20:01 +00001558 if (process)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001559 {
Greg Clayton4c82d422012-05-18 23:20:01 +00001560 Target &target = process->GetTarget();
Greg Claytonfd814c52013-08-13 01:42:25 +00001561
1562 const uint32_t memory_module_load_level = target.GetMemoryModuleLoadLevel();
1563
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001564 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1565 // Reading mach file from memory in a process or core file...
1566
1567 if (linkedit_section_sp)
1568 {
1569 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1570 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1571 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00001572 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00001573
1574 bool data_was_read = false;
1575
1576#if defined (__APPLE__) && defined (__arm__)
1577 if (m_header.flags & 0x80000000u)
Greg Clayton77ccca72011-12-30 00:32:24 +00001578 {
Greg Clayton26b47e22012-04-18 05:19:20 +00001579 // This mach-o memory file is in the dyld shared cache. If this
1580 // program is not remote and this is iOS, then this process will
1581 // share the same shared cache as the process we are debugging and
1582 // we can read the entire __LINKEDIT from the address space in this
1583 // process. This is a needed optimization that is used for local iOS
1584 // debugging only since all shared libraries in the shared cache do
1585 // not have corresponding files that exist in the file system of the
1586 // device. They have been combined into a single file. This means we
1587 // always have to load these files from memory. All of the symbol and
1588 // string tables from all of the __LINKEDIT sections from the shared
1589 // libraries in the shared cache have been merged into a single large
1590 // symbol and string table. Reading all of this symbol and string table
1591 // data across can slow down debug launch times, so we optimize this by
1592 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00001593
1594 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1595 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1596 bool use_lldb_cache = true;
1597 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1598 {
1599 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00001600 ModuleSP module_sp (GetModule());
1601 if (module_sp)
1602 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1603
Jason Molenda0e0954c2013-04-16 06:24:42 +00001604 }
1605
Greg Clayton26b47e22012-04-18 05:19:20 +00001606 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00001607 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00001608 {
1609 data_was_read = true;
1610 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00001611 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00001612 if (function_starts_load_command.cmd)
1613 {
1614 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1615 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1616 }
1617 }
1618 }
1619#endif
1620
1621 if (!data_was_read)
1622 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001623 if (memory_module_load_level == eMemoryModuleLoadLevelComplete)
Jason Molendad34e6522013-02-05 22:31:24 +00001624 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001625 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1626 if (nlist_data_sp)
1627 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
1628 // Load strings individually from memory when loading from memory since shared cache
1629 // string tables contain strings for all symbols from all shared cached libraries
1630 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1631 //if (strtab_data_sp)
1632 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
1633 if (m_dysymtab.nindirectsyms != 0)
1634 {
1635 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1636 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1637 if (indirect_syms_data_sp)
1638 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1639 }
Jason Molendad34e6522013-02-05 22:31:24 +00001640 }
Greg Claytonfd814c52013-08-13 01:42:25 +00001641
1642 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial)
Greg Clayton26b47e22012-04-18 05:19:20 +00001643 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001644 if (function_starts_load_command.cmd)
1645 {
1646 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1647 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1648 if (func_start_data_sp)
1649 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1650 }
Greg Clayton26b47e22012-04-18 05:19:20 +00001651 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001652 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001653 }
1654 }
1655 else
1656 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001657 nlist_data.SetData (m_data,
1658 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001659 nlist_data_byte_size);
1660 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00001661 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001662 strtab_data_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00001663 if (m_dysymtab.nindirectsyms != 0)
1664 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001665 indirect_symbol_index_data.SetData (m_data,
1666 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00001667 m_dysymtab.nindirectsyms * 4);
1668 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001669 if (function_starts_load_command.cmd)
1670 {
1671 function_starts_data.SetData (m_data,
1672 function_starts_load_command.dataoff,
1673 function_starts_load_command.datasize);
1674 }
1675 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001676
Greg Claytondebb8812012-05-25 17:04:00 +00001677 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1678 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00001679 {
Greg Claytondebb8812012-05-25 17:04:00 +00001680 if (process)
1681 {
1682 if (strtab_addr == LLDB_INVALID_ADDRESS)
1683 {
1684 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001685 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00001686 return 0;
1687 }
1688 }
1689 else
Greg Clayton4c82d422012-05-18 23:20:01 +00001690 {
1691 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001692 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00001693 return 0;
1694 }
1695 }
Greg Clayton4c82d422012-05-18 23:20:01 +00001696
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001697 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1698 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1699 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1700 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1701 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1702 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1703 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1704 SectionSP eh_frame_section_sp;
1705 if (text_section_sp.get())
1706 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1707 else
1708 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1709
Greg Clayton29e08cb2012-03-14 01:53:24 +00001710 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molenda5635f772013-03-21 03:36:01 +00001711
1712 // lldb works best if it knows the start addresss of all functions in a module.
1713 // Linker symbols or debug info are normally the best source of information for start addr / size but
1714 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00001715 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00001716 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1717 // binary, relative to the text section.
1718 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1719 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1720 // Binaries built to run on older releases may need to use eh_frame information.
1721
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001722 if (text_section_sp && function_starts_data.GetByteSize())
1723 {
1724 FunctionStarts::Entry function_start_entry;
1725 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00001726 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001727 function_start_entry.addr = text_section_sp->GetFileAddress();
1728 uint64_t delta;
1729 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1730 {
1731 // Now append the current entry
1732 function_start_entry.addr += delta;
1733 function_starts.Append(function_start_entry);
1734 }
Jason Molendad63d3c72013-04-16 00:18:44 +00001735 }
Jason Molenda5635f772013-03-21 03:36:01 +00001736 else
1737 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00001738 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1739 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1740 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1741 // the module.
1742 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00001743 {
1744 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1745 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1746 eh_frame.GetFunctionAddressAndSizeVector (functions);
1747 addr_t text_base_addr = text_section_sp->GetFileAddress();
1748 size_t count = functions.GetSize();
1749 for (size_t i = 0; i < count; ++i)
1750 {
1751 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1752 if (func)
1753 {
1754 FunctionStarts::Entry function_start_entry;
1755 function_start_entry.addr = func->base - text_base_addr;
1756 function_starts.Append(function_start_entry);
1757 }
1758 }
1759 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001760 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001761
Greg Claytonc7bece562013-01-25 18:06:21 +00001762 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001763
Greg Claytonc7bece562013-01-25 18:06:21 +00001764 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 +00001765
Greg Claytonc7bece562013-01-25 18:06:21 +00001766 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001767
1768 uint32_t N_SO_index = UINT32_MAX;
1769
1770 MachSymtabSectionInfo section_info (section_list);
1771 std::vector<uint32_t> N_FUN_indexes;
1772 std::vector<uint32_t> N_NSYM_indexes;
1773 std::vector<uint32_t> N_INCL_indexes;
1774 std::vector<uint32_t> N_BRAC_indexes;
1775 std::vector<uint32_t> N_COMM_indexes;
1776 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1777 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00001778 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001779 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1780 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00001781 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001782 // Any symbols that get merged into another will get an entry
1783 // in this map so we know
1784 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1785 uint32_t nlist_idx = 0;
1786 Symbol *symbol_ptr = NULL;
1787
1788 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00001789 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00001790 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00001791 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00001792 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001793
Jason Molendaa5609c82012-06-21 01:51:02 +00001794#if defined (__APPLE__) && defined (__arm__)
1795
1796 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1797 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1798 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1799 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1800 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1801 // nlist parser to ignore all LOCAL symbols.
1802
1803 if (m_header.flags & 0x80000000u)
1804 {
1805 // Before we can start mapping the DSC, we need to make certain the target process is actually
1806 // using the cache we can find.
1807
Jason Molendaa5609c82012-06-21 01:51:02 +00001808 // Next we need to determine the correct path for the dyld shared cache.
1809
1810 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1811 char dsc_path[PATH_MAX];
1812
1813 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00001814 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1815 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00001816 header_arch.GetArchitectureName());
1817
1818 FileSpec dsc_filespec(dsc_path, false);
1819
1820 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molendad63d3c72013-04-16 00:18:44 +00001821 struct lldb_copy_dyld_cache_header_v0
Greg Clayton946f8902012-09-05 22:30:51 +00001822 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001823 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1824 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1825 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda4e7511e2013-03-06 23:19:17 +00001826 uint32_t imagesOffset;
1827 uint32_t imagesCount;
1828 uint64_t dyldBaseAddress;
1829 uint64_t codeSignatureOffset;
1830 uint64_t codeSignatureSize;
1831 uint64_t slideInfoOffset;
1832 uint64_t slideInfoSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00001833 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1834 uint64_t localSymbolsSize; // size of local symbols information
1835 };
1836 struct lldb_copy_dyld_cache_header_v1
1837 {
1838 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1839 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1840 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1841 uint32_t imagesOffset;
1842 uint32_t imagesCount;
1843 uint64_t dyldBaseAddress;
1844 uint64_t codeSignatureOffset;
1845 uint64_t codeSignatureSize;
1846 uint64_t slideInfoOffset;
1847 uint64_t slideInfoSize;
Jason Molenda4e7511e2013-03-06 23:19:17 +00001848 uint64_t localSymbolsOffset;
1849 uint64_t localSymbolsSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00001850 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Clayton946f8902012-09-05 22:30:51 +00001851 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00001852
Jason Molendad63d3c72013-04-16 00:18:44 +00001853 struct lldb_copy_dyld_cache_mapping_info
1854 {
1855 uint64_t address;
1856 uint64_t size;
1857 uint64_t fileOffset;
1858 uint32_t maxProt;
1859 uint32_t initProt;
1860 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00001861
Greg Clayton946f8902012-09-05 22:30:51 +00001862 struct lldb_copy_dyld_cache_local_symbols_info
1863 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001864 uint32_t nlistOffset;
1865 uint32_t nlistCount;
1866 uint32_t stringsOffset;
1867 uint32_t stringsSize;
1868 uint32_t entriesOffset;
1869 uint32_t entriesCount;
Greg Clayton946f8902012-09-05 22:30:51 +00001870 };
1871 struct lldb_copy_dyld_cache_local_symbols_entry
1872 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001873 uint32_t dylibOffset;
1874 uint32_t nlistStartIndex;
1875 uint32_t nlistCount;
Greg Clayton946f8902012-09-05 22:30:51 +00001876 };
Jason Molendaa5609c82012-06-21 01:51:02 +00001877
Jason Molendaf8130862012-06-22 03:28:35 +00001878 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1879 The dyld_cache_local_symbols_info structure gives us three things:
1880 1. The start and count of the nlist records in the dyld_shared_cache file
1881 2. The start and size of the strings for these nlist records
1882 3. The start and count of dyld_cache_local_symbols_entry entries
1883
1884 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1885 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00001886 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00001887 and the count of how many nlist records there are for this dylib/framework.
1888 */
1889
Jason Molendaa5609c82012-06-21 01:51:02 +00001890 // Process the dsc header to find the unmapped symbols
1891 //
1892 // Save some VM space, do not map the entire cache in one shot.
1893
Jason Molenda255f9bb2013-03-06 23:17:36 +00001894 DataBufferSP dsc_data_sp;
1895 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1896
1897 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00001898 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001899 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00001900
Jason Molenda255f9bb2013-03-06 23:17:36 +00001901 char version_str[17];
1902 int version = -1;
1903 lldb::offset_t offset = 0;
1904 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1905 version_str[16] = '\0';
1906 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1907 {
1908 int v;
1909 if (::sscanf (version_str + 6, "%d", &v) == 1)
1910 {
1911 version = v;
1912 }
1913 }
1914
Jason Molenda0e0954c2013-04-16 06:24:42 +00001915 UUID dsc_uuid;
1916 if (version >= 1)
1917 {
1918 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1919 uint8_t uuid_bytes[sizeof (uuid_t)];
1920 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1921 dsc_uuid.SetBytes (uuid_bytes);
1922 }
1923
1924 bool uuid_match = true;
1925 if (dsc_uuid.IsValid() && process)
1926 {
1927 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1928
1929 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1930 {
1931 // The on-disk dyld_shared_cache file is not the same as the one in this
1932 // process' memory, don't use it.
1933 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00001934 ModuleSP module_sp (GetModule());
1935 if (module_sp)
1936 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 +00001937 }
1938 }
1939
Jason Molenda4e7511e2013-03-06 23:19:17 +00001940 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00001941
Jason Molendaa5609c82012-06-21 01:51:02 +00001942 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1943
1944 // If the mappingOffset points to a location inside the header, we've
1945 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda0e0954c2013-04-16 06:24:42 +00001946 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendaa5609c82012-06-21 01:51:02 +00001947 {
1948
Jason Molenda255f9bb2013-03-06 23:17:36 +00001949 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1950 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1951 offset = 0;
1952
1953 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1954 // in the shared library cache need to be adjusted by an offset to match up with the
1955 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1956 // recorded in mapping_offset_value.
1957 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1958
1959 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00001960 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1961 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1962
Jason Molenda4e7511e2013-03-06 23:19:17 +00001963 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00001964 {
1965 // Map the local symbols
Jason Molenda4e7511e2013-03-06 23:19:17 +00001966 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00001967 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001968 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00001969
1970 offset = 0;
1971
1972 // Read the local_symbols_infos struct in one shot
1973 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1974 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1975
Jason Molendaa5609c82012-06-21 01:51:02 +00001976 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1977
Jason Molenda255f9bb2013-03-06 23:17:36 +00001978 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00001979
1980 offset = local_symbols_info.entriesOffset;
1981 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1982 {
1983 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1984 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1985 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1986 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1987
Jason Molenda4e7511e2013-03-06 23:19:17 +00001988 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00001989 {
1990 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1991
1992 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1993 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1994 num_syms = symtab->GetNumSymbols();
1995
1996 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1997 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1998
Jason Molenda4e7511e2013-03-06 23:19:17 +00001999 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00002000 {
2001 /////////////////////////////
2002 {
2003 struct nlist_64 nlist;
2004 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2005 break;
2006
2007 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2008 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2009 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2010 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2011 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2012
2013 SymbolType type = eSymbolTypeInvalid;
2014 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2015
2016 if (symbol_name == NULL)
2017 {
2018 // No symbol should be NULL, even the symbols with no
2019 // string values should have an offset zero which points
2020 // to an empty C-string
2021 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002022 "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 +00002023 entry_index,
2024 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002025 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00002026 continue;
2027 }
2028 if (symbol_name[0] == '\0')
2029 symbol_name = NULL;
2030
2031 const char *symbol_name_non_abi_mangled = NULL;
2032
2033 SectionSP symbol_section;
2034 uint32_t symbol_byte_size = 0;
2035 bool add_nlist = true;
2036 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002037 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002038 bool is_gsym = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002039
2040 assert (sym_idx < num_syms);
2041
2042 sym[sym_idx].SetDebug (is_debug);
2043
2044 if (is_debug)
2045 {
2046 switch (nlist.n_type)
2047 {
2048 case StabGlobalSymbol:
2049 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2050 // Sometimes the N_GSYM value contains the address.
2051
2052 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2053 // have the same address, but we want to ensure that we always find only the real symbol,
2054 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2055 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2056 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2057 // same address.
2058
2059 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2060 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2061 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2062 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2063 add_nlist = false;
2064 else
2065 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002066 is_gsym = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002067 sym[sym_idx].SetExternal(true);
2068 if (nlist.n_value != 0)
2069 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2070 type = eSymbolTypeData;
2071 }
2072 break;
2073
2074 case StabFunctionName:
2075 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2076 type = eSymbolTypeCompiler;
2077 break;
2078
2079 case StabFunction:
2080 // N_FUN -- procedure: name,,n_sect,linenumber,address
2081 if (symbol_name)
2082 {
2083 type = eSymbolTypeCode;
2084 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2085
2086 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2087 // We use the current number of symbols in the symbol table in lieu of
2088 // using nlist_idx in case we ever start trimming entries out
2089 N_FUN_indexes.push_back(sym_idx);
2090 }
2091 else
2092 {
2093 type = eSymbolTypeCompiler;
2094
2095 if ( !N_FUN_indexes.empty() )
2096 {
2097 // Copy the size of the function into the original STAB entry so we don't have
2098 // to hunt for it later
2099 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2100 N_FUN_indexes.pop_back();
2101 // We don't really need the end function STAB as it contains the size which
2102 // we already placed with the original symbol, so don't add it if we want a
2103 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002104 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002105 }
2106 }
2107 break;
2108
2109 case StabStaticSymbol:
2110 // N_STSYM -- static symbol: name,,n_sect,type,address
2111 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2112 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2113 type = eSymbolTypeData;
2114 break;
2115
2116 case StabLocalCommon:
2117 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2118 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2119 type = eSymbolTypeCommonBlock;
2120 break;
2121
2122 case StabBeginSymbol:
2123 // N_BNSYM
2124 // We use the current number of symbols in the symbol table in lieu of
2125 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00002126 // Skip these if we want minimal symbol tables
2127 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002128 break;
2129
2130 case StabEndSymbol:
2131 // N_ENSYM
2132 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2133 // so that we can always skip the entire symbol if we need to navigate
2134 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00002135 // Skip these if we want minimal symbol tables
2136 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002137 break;
2138
2139
2140 case StabSourceFileOptions:
2141 // N_OPT - emitted with gcc2_compiled and in gcc source
2142 type = eSymbolTypeCompiler;
2143 break;
2144
2145 case StabRegisterSymbol:
2146 // N_RSYM - register sym: name,,NO_SECT,type,register
2147 type = eSymbolTypeVariable;
2148 break;
2149
2150 case StabSourceLine:
2151 // N_SLINE - src line: 0,,n_sect,linenumber,address
2152 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2153 type = eSymbolTypeLineEntry;
2154 break;
2155
2156 case StabStructureType:
2157 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2158 type = eSymbolTypeVariableType;
2159 break;
2160
2161 case StabSourceFileName:
2162 // N_SO - source file name
2163 type = eSymbolTypeSourceFile;
2164 if (symbol_name == NULL)
2165 {
Greg Clayton3046e662013-07-10 01:23:25 +00002166 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002167 if (N_SO_index != UINT32_MAX)
2168 {
2169 // Set the size of the N_SO to the terminating index of this N_SO
2170 // so that we can always skip the entire N_SO if we need to navigate
2171 // more quickly at the source level when parsing STABS
2172 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00002173 symbol_ptr->SetByteSize(sym_idx);
Jason Molendaa5609c82012-06-21 01:51:02 +00002174 symbol_ptr->SetSizeIsSibling(true);
2175 }
2176 N_NSYM_indexes.clear();
2177 N_INCL_indexes.clear();
2178 N_BRAC_indexes.clear();
2179 N_COMM_indexes.clear();
2180 N_FUN_indexes.clear();
2181 N_SO_index = UINT32_MAX;
2182 }
2183 else
2184 {
2185 // We use the current number of symbols in the symbol table in lieu of
2186 // using nlist_idx in case we ever start trimming entries out
2187 const bool N_SO_has_full_path = symbol_name[0] == '/';
2188 if (N_SO_has_full_path)
2189 {
Greg Clayton3046e662013-07-10 01:23:25 +00002190 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002191 {
2192 // We have two consecutive N_SO entries where the first contains a directory
2193 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00002194 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002195 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2196 add_nlist = false;
2197 }
2198 else
2199 {
2200 // This is the first entry in a N_SO that contains a directory or
2201 // a full path to the source file
2202 N_SO_index = sym_idx;
2203 }
2204 }
Greg Clayton3046e662013-07-10 01:23:25 +00002205 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002206 {
2207 // This is usually the second N_SO entry that contains just the filename,
2208 // so here we combine it with the first one if we are minimizing the symbol table
2209 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2210 if (so_path && so_path[0])
2211 {
2212 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00002213 const size_t double_slash_pos = full_so_path.find("//");
2214 if (double_slash_pos != std::string::npos)
2215 {
2216 // The linker has been generating bad N_SO entries with doubled up paths
2217 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2218 // and the second is the directory for the source file so you end up with
2219 // a path that looks like "/tmp/src//tmp/src/"
2220 FileSpec so_dir(so_path, false);
2221 if (!so_dir.Exists())
2222 {
2223 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2224 if (so_dir.Exists())
2225 {
2226 // Trim off the incorrect path
2227 full_so_path.erase(0, double_slash_pos + 1);
2228 }
2229 }
2230 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002231 if (*full_so_path.rbegin() != '/')
2232 full_so_path += '/';
2233 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00002234 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002235 add_nlist = false;
2236 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2237 }
2238 }
Greg Clayton946f8902012-09-05 22:30:51 +00002239 else
2240 {
2241 // This could be a relative path to a N_SO
2242 N_SO_index = sym_idx;
2243 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002244 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002245 break;
2246
2247 case StabObjectFileName:
2248 // N_OSO - object file name: name,,0,0,st_mtime
2249 type = eSymbolTypeObjectFile;
2250 break;
2251
2252 case StabLocalSymbol:
2253 // N_LSYM - local sym: name,,NO_SECT,type,offset
2254 type = eSymbolTypeLocal;
2255 break;
2256
2257 //----------------------------------------------------------------------
2258 // INCL scopes
2259 //----------------------------------------------------------------------
2260 case StabBeginIncludeFileName:
2261 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2262 // We use the current number of symbols in the symbol table in lieu of
2263 // using nlist_idx in case we ever start trimming entries out
2264 N_INCL_indexes.push_back(sym_idx);
2265 type = eSymbolTypeScopeBegin;
2266 break;
2267
2268 case StabEndIncludeFile:
2269 // N_EINCL - include file end: name,,NO_SECT,0,0
2270 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2271 // so that we can always skip the entire symbol if we need to navigate
2272 // more quickly at the source level when parsing STABS
2273 if ( !N_INCL_indexes.empty() )
2274 {
2275 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2276 symbol_ptr->SetByteSize(sym_idx + 1);
2277 symbol_ptr->SetSizeIsSibling(true);
2278 N_INCL_indexes.pop_back();
2279 }
2280 type = eSymbolTypeScopeEnd;
2281 break;
2282
2283 case StabIncludeFileName:
2284 // N_SOL - #included file name: name,,n_sect,0,address
2285 type = eSymbolTypeHeaderFile;
2286
2287 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00002288 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002289 break;
2290
2291 case StabCompilerParameters:
2292 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2293 type = eSymbolTypeCompiler;
2294 break;
2295
2296 case StabCompilerVersion:
2297 // N_VERSION - compiler version: name,,NO_SECT,0,0
2298 type = eSymbolTypeCompiler;
2299 break;
2300
2301 case StabCompilerOptLevel:
2302 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2303 type = eSymbolTypeCompiler;
2304 break;
2305
2306 case StabParameter:
2307 // N_PSYM - parameter: name,,NO_SECT,type,offset
2308 type = eSymbolTypeVariable;
2309 break;
2310
2311 case StabAlternateEntry:
2312 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2313 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2314 type = eSymbolTypeLineEntry;
2315 break;
2316
2317 //----------------------------------------------------------------------
2318 // Left and Right Braces
2319 //----------------------------------------------------------------------
2320 case StabLeftBracket:
2321 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2322 // We use the current number of symbols in the symbol table in lieu of
2323 // using nlist_idx in case we ever start trimming entries out
2324 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2325 N_BRAC_indexes.push_back(sym_idx);
2326 type = eSymbolTypeScopeBegin;
2327 break;
2328
2329 case StabRightBracket:
2330 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2331 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2332 // so that we can always skip the entire symbol if we need to navigate
2333 // more quickly at the source level when parsing STABS
2334 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2335 if ( !N_BRAC_indexes.empty() )
2336 {
2337 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2338 symbol_ptr->SetByteSize(sym_idx + 1);
2339 symbol_ptr->SetSizeIsSibling(true);
2340 N_BRAC_indexes.pop_back();
2341 }
2342 type = eSymbolTypeScopeEnd;
2343 break;
2344
2345 case StabDeletedIncludeFile:
2346 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2347 type = eSymbolTypeHeaderFile;
2348 break;
2349
2350 //----------------------------------------------------------------------
2351 // COMM scopes
2352 //----------------------------------------------------------------------
2353 case StabBeginCommon:
2354 // N_BCOMM - begin common: name,,NO_SECT,0,0
2355 // We use the current number of symbols in the symbol table in lieu of
2356 // using nlist_idx in case we ever start trimming entries out
2357 type = eSymbolTypeScopeBegin;
2358 N_COMM_indexes.push_back(sym_idx);
2359 break;
2360
2361 case StabEndCommonLocal:
2362 // N_ECOML - end common (local name): 0,,n_sect,0,address
2363 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2364 // Fall through
2365
2366 case StabEndCommon:
2367 // N_ECOMM - end common: name,,n_sect,0,0
2368 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2369 // so that we can always skip the entire symbol if we need to navigate
2370 // more quickly at the source level when parsing STABS
2371 if ( !N_COMM_indexes.empty() )
2372 {
2373 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2374 symbol_ptr->SetByteSize(sym_idx + 1);
2375 symbol_ptr->SetSizeIsSibling(true);
2376 N_COMM_indexes.pop_back();
2377 }
2378 type = eSymbolTypeScopeEnd;
2379 break;
2380
2381 case StabLength:
2382 // N_LENG - second stab entry with length information
2383 type = eSymbolTypeAdditional;
2384 break;
2385
2386 default: break;
2387 }
2388 }
2389 else
2390 {
2391 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2392 uint8_t n_type = NlistMaskType & nlist.n_type;
2393 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2394
2395 switch (n_type)
2396 {
2397 case NListTypeIndirect: // N_INDR - Fall through
2398 case NListTypePreboundUndefined:// N_PBUD - Fall through
2399 case NListTypeUndefined: // N_UNDF
2400 type = eSymbolTypeUndefined;
2401 break;
2402
2403 case NListTypeAbsolute: // N_ABS
2404 type = eSymbolTypeAbsolute;
2405 break;
2406
2407 case NListTypeSection: // N_SECT
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002408 {
2409 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002410
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002411 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00002412 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002413 // TODO: warn about this?
2414 add_nlist = false;
2415 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002416 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002417
2418 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00002419 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002420 type = eSymbolTypeException;
2421 }
2422 else
2423 {
2424 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002425
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002426 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00002427 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002428 case SectionTypeRegular: break; // regular section
2429 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2430 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2431 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2432 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2433 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2434 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2435 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2436 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2437 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2438 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2439 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2440 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2441 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2442 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2443 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2444 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2445 default: break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002446 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002447
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002448 if (type == eSymbolTypeInvalid)
2449 {
2450 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2451 if (symbol_section->IsDescendant (text_section_sp.get()))
2452 {
2453 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2454 SectionAttrUserSelfModifyingCode |
2455 SectionAttrSytemSomeInstructions))
2456 type = eSymbolTypeData;
2457 else
2458 type = eSymbolTypeCode;
2459 }
2460 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00002461 {
2462 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2463 {
2464 type = eSymbolTypeRuntime;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002465
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002466 if (symbol_name &&
2467 symbol_name[0] == '_' &&
2468 symbol_name[1] == 'O' &&
Jason Molendaa5609c82012-06-21 01:51:02 +00002469 symbol_name[2] == 'B')
2470 {
2471 llvm::StringRef symbol_name_ref(symbol_name);
2472 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2473 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2474 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2475 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2476 {
2477 symbol_name_non_abi_mangled = symbol_name + 1;
2478 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2479 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002480 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002481 }
2482 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2483 {
2484 symbol_name_non_abi_mangled = symbol_name + 1;
2485 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2486 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002487 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002488 }
2489 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2490 {
2491 symbol_name_non_abi_mangled = symbol_name + 1;
2492 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2493 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002494 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002495 }
2496 }
2497 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002498 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00002499 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002500 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00002501 }
2502 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002503 {
2504 type = eSymbolTypeData;
2505 }
2506 }
2507 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2508 {
2509 type = eSymbolTypeTrampoline;
2510 }
2511 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2512 {
2513 type = eSymbolTypeRuntime;
2514 if (symbol_name && symbol_name[0] == '.')
2515 {
2516 llvm::StringRef symbol_name_ref(symbol_name);
2517 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2518 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00002519 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002520 symbol_name_non_abi_mangled = symbol_name;
2521 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2522 type = eSymbolTypeObjCClass;
2523 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002524 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002525 }
2526 }
2527 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002528 }
2529 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002530 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002531 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002532 }
2533
2534 if (add_nlist)
2535 {
2536 uint64_t symbol_value = nlist.n_value;
Jason Molendaa5609c82012-06-21 01:51:02 +00002537 if (symbol_name_non_abi_mangled)
2538 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00002539 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2540 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00002541 }
2542 else
2543 {
Greg Clayton3046e662013-07-10 01:23:25 +00002544 bool symbol_name_is_mangled = false;
2545
Jason Molendaa5609c82012-06-21 01:51:02 +00002546 if (symbol_name && symbol_name[0] == '_')
2547 {
2548 symbol_name_is_mangled = symbol_name[1] == '_';
2549 symbol_name++; // Skip the leading underscore
2550 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002551
Jason Molendaa5609c82012-06-21 01:51:02 +00002552 if (symbol_name)
2553 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002554 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00002555 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00002556 if (is_gsym && is_debug)
2557 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
Jason Molendaa5609c82012-06-21 01:51:02 +00002558 }
2559 }
2560 if (symbol_section)
2561 {
2562 const addr_t section_file_addr = symbol_section->GetFileAddress();
2563 if (symbol_byte_size == 0 && function_starts_count > 0)
2564 {
2565 addr_t symbol_lookup_file_addr = nlist.n_value;
2566 // Do an exact address match for non-ARM addresses, else get the closest since
2567 // the symbol might be a thumb symbol which has an address with bit zero set
2568 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2569 if (is_arm && func_start_entry)
2570 {
2571 // Verify that the function start address is the symbol address (ARM)
2572 // or the symbol address + 1 (thumb)
2573 if (func_start_entry->addr != symbol_lookup_file_addr &&
2574 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2575 {
2576 // Not the right entry, NULL it out...
2577 func_start_entry = NULL;
2578 }
2579 }
2580 if (func_start_entry)
2581 {
2582 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002583
Jason Molendaa5609c82012-06-21 01:51:02 +00002584 addr_t symbol_file_addr = func_start_entry->addr;
2585 uint32_t symbol_flags = 0;
2586 if (is_arm)
2587 {
2588 if (symbol_file_addr & 1)
2589 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2590 symbol_file_addr &= 0xfffffffffffffffeull;
2591 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002592
Jason Molendaa5609c82012-06-21 01:51:02 +00002593 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2594 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2595 if (next_func_start_entry)
2596 {
2597 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2598 // Be sure the clear the Thumb address bit when we calculate the size
2599 // from the current and next address
2600 if (is_arm)
2601 next_symbol_file_addr &= 0xfffffffffffffffeull;
2602 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2603 }
2604 else
2605 {
2606 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2607 }
2608 }
2609 }
2610 symbol_value -= section_file_addr;
2611 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002612
Greg Claytondacc4a92013-05-14 22:19:37 +00002613 if (is_debug == false)
2614 {
2615 if (type == eSymbolTypeCode)
2616 {
2617 // See if we can find a N_FUN entry for any code symbols.
2618 // If we do find a match, and the name matches, then we
2619 // can merge the two into just the function symbol to avoid
2620 // duplicate entries in the symbol table
2621 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2622 if (pos != N_FUN_addr_to_sym_idx.end())
2623 {
Greg Clayton3046e662013-07-10 01:23:25 +00002624 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
Greg Claytondacc4a92013-05-14 22:19:37 +00002625 {
2626 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2627 // We just need the flags from the linker symbol, so put these flags
2628 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2629 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2630 sym[sym_idx].Clear();
2631 continue;
2632 }
2633 }
2634 }
2635 else if (type == eSymbolTypeData)
2636 {
2637 // See if we can find a N_STSYM entry for any data symbols.
2638 // If we do find a match, and the name matches, then we
2639 // can merge the two into just the Static symbol to avoid
2640 // duplicate entries in the symbol table
2641 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2642 if (pos != N_STSYM_addr_to_sym_idx.end())
2643 {
Greg Clayton3046e662013-07-10 01:23:25 +00002644 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
Greg Claytondacc4a92013-05-14 22:19:37 +00002645 {
2646 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2647 // We just need the flags from the linker symbol, so put these flags
2648 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2649 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2650 sym[sym_idx].Clear();
2651 continue;
2652 }
2653 }
2654 else
2655 {
2656 // Combine N_GSYM stab entries with the non stab symbol
Greg Clayton3046e662013-07-10 01:23:25 +00002657 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 +00002658 if (pos != N_GSYM_name_to_sym_idx.end())
2659 {
2660 const uint32_t GSYM_sym_idx = pos->second;
2661 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
2662 // Copy the address, because often the N_GSYM address has an invalid address of zero
2663 // when the global is a common symbol
2664 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
2665 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
2666 // We just need the flags from the linker symbol, so put these flags
2667 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2668 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2669 sym[sym_idx].Clear();
2670 continue;
2671 }
2672 }
2673 }
2674 }
2675
Jason Molendaa5609c82012-06-21 01:51:02 +00002676 sym[sym_idx].SetID (nlist_idx);
2677 sym[sym_idx].SetType (type);
2678 sym[sym_idx].GetAddress().SetSection (symbol_section);
2679 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2680 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002681
Jason Molendaa5609c82012-06-21 01:51:02 +00002682 if (symbol_byte_size > 0)
2683 sym[sym_idx].SetByteSize(symbol_byte_size);
2684
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002685 if (demangled_is_synthesized)
2686 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00002687 ++sym_idx;
2688 }
2689 else
2690 {
2691 sym[sym_idx].Clear();
2692 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002693
Jason Molendaa5609c82012-06-21 01:51:02 +00002694 }
2695 /////////////////////////////
2696 }
2697 break; // No more entries to consider
2698 }
2699 }
2700 }
2701 }
2702 }
2703 }
2704 }
2705
2706 // Must reset this in case it was mutated above!
2707 nlist_data_offset = 0;
2708#endif
Greg Claytonfd814c52013-08-13 01:42:25 +00002709
2710 if (nlist_data.GetByteSize() > 0)
Jason Molendaa5609c82012-06-21 01:51:02 +00002711 {
Jason Molendaa5609c82012-06-21 01:51:02 +00002712
Greg Claytonfd814c52013-08-13 01:42:25 +00002713 // If the sym array was not created while parsing the DSC unmapped
2714 // symbols, create it now.
2715 if (sym == NULL)
Greg Clayton4c82d422012-05-18 23:20:01 +00002716 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002717 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2718 num_syms = symtab->GetNumSymbols();
2719 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002720
Greg Claytonfd814c52013-08-13 01:42:25 +00002721 if (unmapped_local_symbols_found)
2722 {
2723 assert(m_dysymtab.ilocalsym == 0);
2724 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2725 nlist_idx = m_dysymtab.nlocalsym;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002726 }
Greg Claytondebb8812012-05-25 17:04:00 +00002727 else
2728 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002729 nlist_idx = 0;
Greg Claytondebb8812012-05-25 17:04:00 +00002730 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002731
Greg Claytonfd814c52013-08-13 01:42:25 +00002732 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002733 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002734 struct nlist_64 nlist;
2735 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2736 break;
2737
2738 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2739 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2740 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2741 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2742 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2743
2744 SymbolType type = eSymbolTypeInvalid;
2745 const char *symbol_name = NULL;
2746
2747 if (have_strtab_data)
Greg Clayton77ccca72011-12-30 00:32:24 +00002748 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002749 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002750
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002751 if (symbol_name == NULL)
2752 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002753 // No symbol should be NULL, even the symbols with no
2754 // string values should have an offset zero which points
2755 // to an empty C-string
2756 Host::SystemLog (Host::eSystemLogError,
2757 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
2758 nlist_idx,
2759 nlist.n_strx,
2760 module_sp->GetFileSpec().GetPath().c_str());
2761 continue;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002762 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002763 if (symbol_name[0] == '\0')
2764 symbol_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002765 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002766 else
2767 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002768 const addr_t str_addr = strtab_addr + nlist.n_strx;
2769 Error str_error;
2770 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2771 symbol_name = memory_symbol_name.c_str();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002772 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002773 const char *symbol_name_non_abi_mangled = NULL;
2774
2775 SectionSP symbol_section;
2776 lldb::addr_t symbol_byte_size = 0;
2777 bool add_nlist = true;
2778 bool is_gsym = false;
2779 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
2780 bool demangled_is_synthesized = false;
2781
2782 assert (sym_idx < num_syms);
2783
2784 sym[sym_idx].SetDebug (is_debug);
2785
2786 if (is_debug)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002787 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002788 switch (nlist.n_type)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002789 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002790 case StabGlobalSymbol:
2791 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2792 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002793
Greg Claytonfd814c52013-08-13 01:42:25 +00002794 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2795 // have the same address, but we want to ensure that we always find only the real symbol,
2796 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2797 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2798 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2799 // same address.
Greg Clayton29e08cb2012-03-14 01:53:24 +00002800
Greg Claytonfd814c52013-08-13 01:42:25 +00002801 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2802 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2803 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2804 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2805 add_nlist = false;
2806 else
2807 {
2808 is_gsym = true;
2809 sym[sym_idx].SetExternal(true);
2810 if (nlist.n_value != 0)
2811 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2812 type = eSymbolTypeData;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002813 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002814 break;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002815
Greg Claytonfd814c52013-08-13 01:42:25 +00002816 case StabFunctionName:
2817 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2818 type = eSymbolTypeCompiler;
2819 break;
2820
2821 case StabFunction:
2822 // N_FUN -- procedure: name,,n_sect,linenumber,address
2823 if (symbol_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00002824 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002825 type = eSymbolTypeCode;
2826 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2827
2828 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2829 // We use the current number of symbols in the symbol table in lieu of
2830 // using nlist_idx in case we ever start trimming entries out
2831 N_FUN_indexes.push_back(sym_idx);
Greg Claytondacc4a92013-05-14 22:19:37 +00002832 }
2833 else
2834 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002835 type = eSymbolTypeCompiler;
2836
2837 if ( !N_FUN_indexes.empty() )
Greg Claytondacc4a92013-05-14 22:19:37 +00002838 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002839 // Copy the size of the function into the original STAB entry so we don't have
2840 // to hunt for it later
2841 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2842 N_FUN_indexes.pop_back();
2843 // We don't really need the end function STAB as it contains the size which
2844 // we already placed with the original symbol, so don't add it if we want a
2845 // minimal symbol table
2846 add_nlist = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002847 }
2848 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002849 break;
2850
2851 case StabStaticSymbol:
2852 // N_STSYM -- static symbol: name,,n_sect,type,address
2853 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2854 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2855 type = eSymbolTypeData;
2856 break;
2857
2858 case StabLocalCommon:
2859 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2860 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2861 type = eSymbolTypeCommonBlock;
2862 break;
2863
2864 case StabBeginSymbol:
2865 // N_BNSYM
2866 // We use the current number of symbols in the symbol table in lieu of
2867 // using nlist_idx in case we ever start trimming entries out
2868 // Skip these if we want minimal symbol tables
2869 add_nlist = false;
2870 break;
2871
2872 case StabEndSymbol:
2873 // N_ENSYM
2874 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2875 // so that we can always skip the entire symbol if we need to navigate
2876 // more quickly at the source level when parsing STABS
2877 // Skip these if we want minimal symbol tables
2878 add_nlist = false;
2879 break;
2880
2881
2882 case StabSourceFileOptions:
2883 // N_OPT - emitted with gcc2_compiled and in gcc source
2884 type = eSymbolTypeCompiler;
2885 break;
2886
2887 case StabRegisterSymbol:
2888 // N_RSYM - register sym: name,,NO_SECT,type,register
2889 type = eSymbolTypeVariable;
2890 break;
2891
2892 case StabSourceLine:
2893 // N_SLINE - src line: 0,,n_sect,linenumber,address
2894 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2895 type = eSymbolTypeLineEntry;
2896 break;
2897
2898 case StabStructureType:
2899 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2900 type = eSymbolTypeVariableType;
2901 break;
2902
2903 case StabSourceFileName:
2904 // N_SO - source file name
2905 type = eSymbolTypeSourceFile;
2906 if (symbol_name == NULL)
2907 {
2908 add_nlist = false;
2909 if (N_SO_index != UINT32_MAX)
2910 {
2911 // Set the size of the N_SO to the terminating index of this N_SO
2912 // so that we can always skip the entire N_SO if we need to navigate
2913 // more quickly at the source level when parsing STABS
2914 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2915 symbol_ptr->SetByteSize(sym_idx);
2916 symbol_ptr->SetSizeIsSibling(true);
2917 }
2918 N_NSYM_indexes.clear();
2919 N_INCL_indexes.clear();
2920 N_BRAC_indexes.clear();
2921 N_COMM_indexes.clear();
2922 N_FUN_indexes.clear();
2923 N_SO_index = UINT32_MAX;
2924 }
2925 else
2926 {
2927 // We use the current number of symbols in the symbol table in lieu of
2928 // using nlist_idx in case we ever start trimming entries out
2929 const bool N_SO_has_full_path = symbol_name[0] == '/';
2930 if (N_SO_has_full_path)
2931 {
2932 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2933 {
2934 // We have two consecutive N_SO entries where the first contains a directory
2935 // and the second contains a full path.
2936 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
2937 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2938 add_nlist = false;
2939 }
2940 else
2941 {
2942 // This is the first entry in a N_SO that contains a directory or
2943 // a full path to the source file
2944 N_SO_index = sym_idx;
2945 }
2946 }
2947 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2948 {
2949 // This is usually the second N_SO entry that contains just the filename,
2950 // so here we combine it with the first one if we are minimizing the symbol table
2951 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2952 if (so_path && so_path[0])
2953 {
2954 std::string full_so_path (so_path);
2955 const size_t double_slash_pos = full_so_path.find("//");
2956 if (double_slash_pos != std::string::npos)
2957 {
2958 // The linker has been generating bad N_SO entries with doubled up paths
2959 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2960 // and the second is the directory for the source file so you end up with
2961 // a path that looks like "/tmp/src//tmp/src/"
2962 FileSpec so_dir(so_path, false);
2963 if (!so_dir.Exists())
2964 {
2965 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2966 if (so_dir.Exists())
2967 {
2968 // Trim off the incorrect path
2969 full_so_path.erase(0, double_slash_pos + 1);
2970 }
2971 }
2972 }
2973 if (*full_so_path.rbegin() != '/')
2974 full_so_path += '/';
2975 full_so_path += symbol_name;
2976 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
2977 add_nlist = false;
2978 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2979 }
2980 }
2981 else
2982 {
2983 // This could be a relative path to a N_SO
2984 N_SO_index = sym_idx;
2985 }
2986 }
2987
2988 break;
2989
2990 case StabObjectFileName:
2991 // N_OSO - object file name: name,,0,0,st_mtime
2992 type = eSymbolTypeObjectFile;
2993 break;
2994
2995 case StabLocalSymbol:
2996 // N_LSYM - local sym: name,,NO_SECT,type,offset
2997 type = eSymbolTypeLocal;
2998 break;
2999
3000 //----------------------------------------------------------------------
3001 // INCL scopes
3002 //----------------------------------------------------------------------
3003 case StabBeginIncludeFileName:
3004 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
3005 // We use the current number of symbols in the symbol table in lieu of
3006 // using nlist_idx in case we ever start trimming entries out
3007 N_INCL_indexes.push_back(sym_idx);
3008 type = eSymbolTypeScopeBegin;
3009 break;
3010
3011 case StabEndIncludeFile:
3012 // N_EINCL - include file end: name,,NO_SECT,0,0
3013 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3014 // so that we can always skip the entire symbol if we need to navigate
3015 // more quickly at the source level when parsing STABS
3016 if ( !N_INCL_indexes.empty() )
3017 {
3018 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3019 symbol_ptr->SetByteSize(sym_idx + 1);
3020 symbol_ptr->SetSizeIsSibling(true);
3021 N_INCL_indexes.pop_back();
3022 }
3023 type = eSymbolTypeScopeEnd;
3024 break;
3025
3026 case StabIncludeFileName:
3027 // N_SOL - #included file name: name,,n_sect,0,address
3028 type = eSymbolTypeHeaderFile;
3029
3030 // We currently don't use the header files on darwin
3031 add_nlist = false;
3032 break;
3033
3034 case StabCompilerParameters:
3035 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
3036 type = eSymbolTypeCompiler;
3037 break;
3038
3039 case StabCompilerVersion:
3040 // N_VERSION - compiler version: name,,NO_SECT,0,0
3041 type = eSymbolTypeCompiler;
3042 break;
3043
3044 case StabCompilerOptLevel:
3045 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
3046 type = eSymbolTypeCompiler;
3047 break;
3048
3049 case StabParameter:
3050 // N_PSYM - parameter: name,,NO_SECT,type,offset
3051 type = eSymbolTypeVariable;
3052 break;
3053
3054 case StabAlternateEntry:
3055 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
3056 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3057 type = eSymbolTypeLineEntry;
3058 break;
3059
3060 //----------------------------------------------------------------------
3061 // Left and Right Braces
3062 //----------------------------------------------------------------------
3063 case StabLeftBracket:
3064 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
3065 // We use the current number of symbols in the symbol table in lieu of
3066 // using nlist_idx in case we ever start trimming entries out
3067 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3068 N_BRAC_indexes.push_back(sym_idx);
3069 type = eSymbolTypeScopeBegin;
3070 break;
3071
3072 case StabRightBracket:
3073 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
3074 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3075 // so that we can always skip the entire symbol if we need to navigate
3076 // more quickly at the source level when parsing STABS
3077 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3078 if ( !N_BRAC_indexes.empty() )
3079 {
3080 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3081 symbol_ptr->SetByteSize(sym_idx + 1);
3082 symbol_ptr->SetSizeIsSibling(true);
3083 N_BRAC_indexes.pop_back();
3084 }
3085 type = eSymbolTypeScopeEnd;
3086 break;
3087
3088 case StabDeletedIncludeFile:
3089 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3090 type = eSymbolTypeHeaderFile;
3091 break;
3092
3093 //----------------------------------------------------------------------
3094 // COMM scopes
3095 //----------------------------------------------------------------------
3096 case StabBeginCommon:
3097 // N_BCOMM - begin common: name,,NO_SECT,0,0
3098 // We use the current number of symbols in the symbol table in lieu of
3099 // using nlist_idx in case we ever start trimming entries out
3100 type = eSymbolTypeScopeBegin;
3101 N_COMM_indexes.push_back(sym_idx);
3102 break;
3103
3104 case StabEndCommonLocal:
3105 // N_ECOML - end common (local name): 0,,n_sect,0,address
3106 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3107 // Fall through
3108
3109 case StabEndCommon:
3110 // N_ECOMM - end common: name,,n_sect,0,0
3111 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3112 // so that we can always skip the entire symbol if we need to navigate
3113 // more quickly at the source level when parsing STABS
3114 if ( !N_COMM_indexes.empty() )
3115 {
3116 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3117 symbol_ptr->SetByteSize(sym_idx + 1);
3118 symbol_ptr->SetSizeIsSibling(true);
3119 N_COMM_indexes.pop_back();
3120 }
3121 type = eSymbolTypeScopeEnd;
3122 break;
3123
3124 case StabLength:
3125 // N_LENG - second stab entry with length information
3126 type = eSymbolTypeAdditional;
3127 break;
3128
3129 default: break;
3130 }
3131 }
3132 else
3133 {
3134 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3135 uint8_t n_type = NlistMaskType & nlist.n_type;
3136 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3137
3138 switch (n_type)
3139 {
3140 case NListTypeIndirect: // N_INDR - Fall through
3141 case NListTypePreboundUndefined:// N_PBUD - Fall through
3142 case NListTypeUndefined: // N_UNDF
3143 type = eSymbolTypeUndefined;
3144 break;
3145
3146 case NListTypeAbsolute: // N_ABS
3147 type = eSymbolTypeAbsolute;
3148 break;
3149
3150 case NListTypeSection: // N_SECT
3151 {
3152 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3153
3154 if (!symbol_section)
3155 {
3156 // TODO: warn about this?
3157 add_nlist = false;
3158 break;
3159 }
3160
3161 if (TEXT_eh_frame_sectID == nlist.n_sect)
3162 {
3163 type = eSymbolTypeException;
3164 }
3165 else
3166 {
3167 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3168
3169 switch (section_type)
3170 {
3171 case SectionTypeRegular: break; // regular section
3172 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3173 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3174 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3175 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3176 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3177 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3178 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3179 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3180 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3181 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3182 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3183 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3184 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3185 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3186 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3187 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3188 default: break;
3189 }
3190
3191 if (type == eSymbolTypeInvalid)
3192 {
3193 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3194 if (symbol_section->IsDescendant (text_section_sp.get()))
3195 {
3196 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3197 SectionAttrUserSelfModifyingCode |
3198 SectionAttrSytemSomeInstructions))
3199 type = eSymbolTypeData;
3200 else
3201 type = eSymbolTypeCode;
3202 }
3203 else
3204 if (symbol_section->IsDescendant(data_section_sp.get()))
3205 {
3206 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
3207 {
3208 type = eSymbolTypeRuntime;
3209
3210 if (symbol_name &&
3211 symbol_name[0] == '_' &&
3212 symbol_name[1] == 'O' &&
3213 symbol_name[2] == 'B')
3214 {
3215 llvm::StringRef symbol_name_ref(symbol_name);
3216 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3217 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3218 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3219 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3220 {
3221 symbol_name_non_abi_mangled = symbol_name + 1;
3222 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3223 type = eSymbolTypeObjCClass;
3224 demangled_is_synthesized = true;
3225 }
3226 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3227 {
3228 symbol_name_non_abi_mangled = symbol_name + 1;
3229 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3230 type = eSymbolTypeObjCMetaClass;
3231 demangled_is_synthesized = true;
3232 }
3233 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3234 {
3235 symbol_name_non_abi_mangled = symbol_name + 1;
3236 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3237 type = eSymbolTypeObjCIVar;
3238 demangled_is_synthesized = true;
3239 }
3240 }
3241 }
3242 else
3243 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3244 {
3245 type = eSymbolTypeException;
3246 }
3247 else
3248 {
3249 type = eSymbolTypeData;
3250 }
3251 }
3252 else
3253 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3254 {
3255 type = eSymbolTypeTrampoline;
3256 }
3257 else
3258 if (symbol_section->IsDescendant(objc_section_sp.get()))
3259 {
3260 type = eSymbolTypeRuntime;
3261 if (symbol_name && symbol_name[0] == '.')
3262 {
3263 llvm::StringRef symbol_name_ref(symbol_name);
3264 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3265 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3266 {
3267 symbol_name_non_abi_mangled = symbol_name;
3268 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3269 type = eSymbolTypeObjCClass;
3270 demangled_is_synthesized = true;
3271 }
3272 }
3273 }
3274 }
3275 }
3276 }
3277 break;
Greg Claytondacc4a92013-05-14 22:19:37 +00003278 }
3279 }
3280
Greg Claytonfd814c52013-08-13 01:42:25 +00003281 if (add_nlist)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003282 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003283 uint64_t symbol_value = nlist.n_value;
3284
3285 if (symbol_name_non_abi_mangled)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003286 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003287 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3288 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
3289 }
3290 else
3291 {
3292 bool symbol_name_is_mangled = false;
3293
3294 if (symbol_name && symbol_name[0] == '_')
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003295 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003296 symbol_name_is_mangled = symbol_name[1] == '_';
3297 symbol_name++; // Skip the leading underscore
3298 }
3299
3300 if (symbol_name)
3301 {
3302 ConstString const_symbol_name(symbol_name);
3303 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
3304 if (is_gsym && is_debug)
3305 {
3306 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
3307 }
3308 }
3309 }
3310 if (symbol_section)
3311 {
3312 const addr_t section_file_addr = symbol_section->GetFileAddress();
3313 if (symbol_byte_size == 0 && function_starts_count > 0)
3314 {
3315 addr_t symbol_lookup_file_addr = nlist.n_value;
3316 // Do an exact address match for non-ARM addresses, else get the closest since
3317 // the symbol might be a thumb symbol which has an address with bit zero set
3318 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3319 if (is_arm && func_start_entry)
3320 {
3321 // Verify that the function start address is the symbol address (ARM)
3322 // or the symbol address + 1 (thumb)
3323 if (func_start_entry->addr != symbol_lookup_file_addr &&
3324 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3325 {
3326 // Not the right entry, NULL it out...
3327 func_start_entry = NULL;
3328 }
3329 }
3330 if (func_start_entry)
3331 {
3332 func_start_entry->data = true;
3333
3334 addr_t symbol_file_addr = func_start_entry->addr;
3335 if (is_arm)
3336 symbol_file_addr &= 0xfffffffffffffffeull;
3337
3338 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3339 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3340 if (next_func_start_entry)
3341 {
3342 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3343 // Be sure the clear the Thumb address bit when we calculate the size
3344 // from the current and next address
3345 if (is_arm)
3346 next_symbol_file_addr &= 0xfffffffffffffffeull;
3347 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3348 }
3349 else
3350 {
3351 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3352 }
3353 }
3354 }
3355 symbol_value -= section_file_addr;
3356 }
3357
3358 if (is_debug == false)
3359 {
3360 if (type == eSymbolTypeCode)
3361 {
3362 // See if we can find a N_FUN entry for any code symbols.
3363 // If we do find a match, and the name matches, then we
3364 // can merge the two into just the function symbol to avoid
3365 // duplicate entries in the symbol table
3366 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3367 if (pos != N_FUN_addr_to_sym_idx.end())
3368 {
3369 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3370 {
3371 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3372 // We just need the flags from the linker symbol, so put these flags
3373 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3374 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3375 sym[sym_idx].Clear();
3376 continue;
3377 }
3378 }
3379 }
3380 else if (type == eSymbolTypeData)
3381 {
3382 // See if we can find a N_STSYM entry for any data symbols.
3383 // If we do find a match, and the name matches, then we
3384 // can merge the two into just the Static symbol to avoid
3385 // duplicate entries in the symbol table
3386 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3387 if (pos != N_STSYM_addr_to_sym_idx.end())
3388 {
3389 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3390 {
3391 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3392 // We just need the flags from the linker symbol, so put these flags
3393 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3394 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3395 sym[sym_idx].Clear();
3396 continue;
3397 }
3398 }
3399 else
3400 {
3401 // Combine N_GSYM stab entries with the non stab symbol
3402 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
3403 if (pos != N_GSYM_name_to_sym_idx.end())
3404 {
3405 const uint32_t GSYM_sym_idx = pos->second;
3406 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3407 // Copy the address, because often the N_GSYM address has an invalid address of zero
3408 // when the global is a common symbol
3409 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3410 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3411 // We just need the flags from the linker symbol, so put these flags
3412 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3413 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3414 sym[sym_idx].Clear();
3415 continue;
3416 }
3417 }
3418 }
3419 }
3420
3421 sym[sym_idx].SetID (nlist_idx);
3422 sym[sym_idx].SetType (type);
3423 sym[sym_idx].GetAddress().SetSection (symbol_section);
3424 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3425 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3426
3427 if (symbol_byte_size > 0)
3428 sym[sym_idx].SetByteSize(symbol_byte_size);
3429
3430 if (demangled_is_synthesized)
3431 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3432
3433 ++sym_idx;
3434 }
3435 else
3436 {
3437 sym[sym_idx].Clear();
3438 }
3439
3440 }
3441
3442 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3443 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3444 // such entries by figuring out what the address for the global is by looking up this non-STAB
3445 // entry and copying the value into the debug symbol's value to save us the hassle in the
3446 // debug symbol parser.
3447
3448 Symbol *global_symbol = NULL;
3449 for (nlist_idx = 0;
3450 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3451 nlist_idx++)
3452 {
3453 if (global_symbol->GetAddress().GetFileAddress() == 0)
3454 {
3455 std::vector<uint32_t> indexes;
3456 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3457 {
3458 std::vector<uint32_t>::const_iterator pos;
3459 std::vector<uint32_t>::const_iterator end = indexes.end();
3460 for (pos = indexes.begin(); pos != end; ++pos)
3461 {
3462 symbol_ptr = symtab->SymbolAtIndex(*pos);
3463 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3464 {
3465 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3466 break;
3467 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003468 }
3469 }
3470 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003471 }
3472 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003473
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003474 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3475
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003476 if (function_starts_count > 0)
3477 {
3478 char synthetic_function_symbol[PATH_MAX];
3479 uint32_t num_synthetic_function_symbols = 0;
3480 for (i=0; i<function_starts_count; ++i)
3481 {
3482 if (function_starts.GetEntryRef (i).data == false)
3483 ++num_synthetic_function_symbols;
3484 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003485
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003486 if (num_synthetic_function_symbols > 0)
3487 {
3488 if (num_syms < sym_idx + num_synthetic_function_symbols)
3489 {
3490 num_syms = sym_idx + num_synthetic_function_symbols;
3491 sym = symtab->Resize (num_syms);
3492 }
3493 uint32_t synthetic_function_symbol_idx = 0;
3494 for (i=0; i<function_starts_count; ++i)
3495 {
3496 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3497 if (func_start_entry->data == false)
3498 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003499 addr_t symbol_file_addr = func_start_entry->addr;
3500 uint32_t symbol_flags = 0;
3501 if (is_arm)
3502 {
3503 if (symbol_file_addr & 1)
3504 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3505 symbol_file_addr &= 0xfffffffffffffffeull;
3506 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003507 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003508 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003509 {
3510 SectionSP symbol_section (symbol_addr.GetSection());
3511 uint32_t symbol_byte_size = 0;
3512 if (symbol_section)
3513 {
3514 const addr_t section_file_addr = symbol_section->GetFileAddress();
3515 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3516 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3517 if (next_func_start_entry)
3518 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003519 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3520 if (is_arm)
3521 next_symbol_file_addr &= 0xfffffffffffffffeull;
3522 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 +00003523 }
3524 else
3525 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003526 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003527 }
3528 snprintf (synthetic_function_symbol,
3529 sizeof(synthetic_function_symbol),
3530 "___lldb_unnamed_function%u$$%s",
3531 ++synthetic_function_symbol_idx,
3532 module_sp->GetFileSpec().GetFilename().GetCString());
3533 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Clayton037520e2012-07-18 23:18:10 +00003534 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003535 sym[sym_idx].SetType (eSymbolTypeCode);
3536 sym[sym_idx].SetIsSynthetic (true);
3537 sym[sym_idx].GetAddress() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003538 if (symbol_flags)
3539 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003540 if (symbol_byte_size)
3541 sym[sym_idx].SetByteSize (symbol_byte_size);
3542 ++sym_idx;
3543 }
3544 }
3545 }
3546 }
3547 }
3548 }
3549
3550 // Trim our symbols down to just what we ended up with after
3551 // removing any symbols.
3552 if (sym_idx < num_syms)
3553 {
3554 num_syms = sym_idx;
3555 sym = symtab->Resize (num_syms);
3556 }
3557
3558 // Now synthesize indirect symbols
3559 if (m_dysymtab.nindirectsyms != 0)
3560 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003561 if (indirect_symbol_index_data.GetByteSize())
3562 {
3563 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3564
3565 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3566 {
3567 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3568 {
3569 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3570 if (symbol_stub_byte_size == 0)
3571 continue;
3572
3573 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3574
3575 if (num_symbol_stubs == 0)
3576 continue;
3577
3578 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3579 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3580 {
3581 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3582 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 +00003583 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003584 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3585 {
3586 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3587 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3588 continue;
3589
3590 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3591 Symbol *stub_symbol = NULL;
3592 if (index_pos != end_index_pos)
3593 {
3594 // We have a remapping from the original nlist index to
3595 // a current symbol index, so just look this up by index
3596 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3597 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003598 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003599 {
3600 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00003601 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003602 // S_SYMBOL_STUBS
3603 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3604 }
3605
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003606 if (stub_symbol)
3607 {
3608 Address so_addr(symbol_stub_addr, section_list);
3609
3610 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3611 {
3612 // Change the external symbol into a trampoline that makes sense
3613 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3614 // can re-use them so we don't have to make up a synthetic symbol
3615 // for no good reason.
3616 stub_symbol->SetType (eSymbolTypeTrampoline);
3617 stub_symbol->SetExternal (false);
3618 stub_symbol->GetAddress() = so_addr;
3619 stub_symbol->SetByteSize (symbol_stub_byte_size);
3620 }
3621 else
3622 {
3623 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00003624 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003625 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00003626 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003627 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00003628 stub_symbol = NULL; // this pointer no longer valid
3629 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003630 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00003631 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003632 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3633 sym[sym_idx].SetIsSynthetic (true);
3634 sym[sym_idx].GetAddress() = so_addr;
3635 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3636 ++sym_idx;
3637 }
3638 }
Greg Clayton3f839a32012-09-05 01:38:55 +00003639 else
3640 {
3641 if (log)
3642 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3643 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003644 }
3645 }
3646 }
3647 }
3648 }
3649 }
Greg Clayton3046e662013-07-10 01:23:25 +00003650
3651// StreamFile s(stdout, false);
3652// s.Printf ("Symbol table before CalculateSymbolSizes():\n");
3653// symtab->Dump(&s, NULL, eSortOrderNone);
3654 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
3655 symtab->CalculateSymbolSizes();
3656
3657// s.Printf ("Symbol table after CalculateSymbolSizes():\n");
3658// symtab->Dump(&s, NULL, eSortOrderNone);
3659
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003660 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003661 }
3662 return 0;
3663}
3664
3665
3666void
3667ObjectFileMachO::Dump (Stream *s)
3668{
Greg Claytona1743492012-03-13 23:14:29 +00003669 ModuleSP module_sp(GetModule());
3670 if (module_sp)
3671 {
3672 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3673 s->Printf("%p: ", this);
3674 s->Indent();
3675 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3676 s->PutCString("ObjectFileMachO64");
3677 else
3678 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003679
Greg Claytona1743492012-03-13 23:14:29 +00003680 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003681
Greg Claytona1743492012-03-13 23:14:29 +00003682 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003683
Greg Clayton3046e662013-07-10 01:23:25 +00003684 SectionList *sections = GetSectionList();
3685 if (sections)
3686 sections->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003687
Greg Claytona1743492012-03-13 23:14:29 +00003688 if (m_symtab_ap.get())
3689 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3690 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003691}
3692
Greg Claytonf4d6de62013-04-24 22:29:28 +00003693bool
3694ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3695 const lldb_private::DataExtractor &data,
3696 lldb::offset_t lc_offset,
3697 lldb_private::UUID& uuid)
3698{
3699 uint32_t i;
3700 struct uuid_command load_cmd;
3701
3702 lldb::offset_t offset = lc_offset;
3703 for (i=0; i<header.ncmds; ++i)
3704 {
3705 const lldb::offset_t cmd_offset = offset;
3706 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3707 break;
3708
3709 if (load_cmd.cmd == LoadCommandUUID)
3710 {
3711 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3712
3713 if (uuid_bytes)
3714 {
3715 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3716 // We pretend these object files have no UUID to prevent crashing.
3717
3718 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3719 0x3b, 0xa8,
3720 0x4b, 0x16,
3721 0xb6, 0xa4,
3722 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3723
3724 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3725 return false;
3726
3727 uuid.SetBytes (uuid_bytes);
3728 return true;
3729 }
3730 return false;
3731 }
3732 offset = cmd_offset + load_cmd.cmdsize;
3733 }
3734 return false;
3735}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003736
3737bool
Greg Clayton60830262011-02-04 18:53:10 +00003738ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003739{
Greg Claytona1743492012-03-13 23:14:29 +00003740 ModuleSP module_sp(GetModule());
3741 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003742 {
Greg Claytona1743492012-03-13 23:14:29 +00003743 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00003744 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00003745 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003746 }
3747 return false;
3748}
3749
3750
3751uint32_t
3752ObjectFileMachO::GetDependentModules (FileSpecList& files)
3753{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003754 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00003755 ModuleSP module_sp(GetModule());
3756 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003757 {
Greg Claytona1743492012-03-13 23:14:29 +00003758 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3759 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00003760 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003761 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3762 uint32_t i;
3763 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003764 {
Greg Claytona1743492012-03-13 23:14:29 +00003765 const uint32_t cmd_offset = offset;
3766 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3767 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003768
Greg Claytona1743492012-03-13 23:14:29 +00003769 switch (load_cmd.cmd)
3770 {
3771 case LoadCommandDylibLoad:
3772 case LoadCommandDylibLoadWeak:
3773 case LoadCommandDylibReexport:
3774 case LoadCommandDynamicLinkerLoad:
3775 case LoadCommandFixedVMShlibLoad:
3776 case LoadCommandDylibLoadUpward:
3777 {
3778 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3779 const char *path = m_data.PeekCStr(name_offset);
3780 // Skip any path that starts with '@' since these are usually:
3781 // @executable_path/.../file
3782 // @rpath/.../file
3783 if (path && path[0] != '@')
3784 {
3785 FileSpec file_spec(path, resolve_path);
3786 if (files.AppendIfUnique(file_spec))
3787 count++;
3788 }
3789 }
3790 break;
3791
3792 default:
3793 break;
3794 }
3795 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003796 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003797 }
3798 return count;
3799}
3800
Jim Ingham672e6f52011-03-07 23:44:08 +00003801lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00003802ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00003803{
3804 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3805 // is initialized to an invalid address, so we can just return that.
3806 // 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 +00003807
Jim Ingham672e6f52011-03-07 23:44:08 +00003808 if (!IsExecutable() || m_entry_point_address.IsValid())
3809 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003810
3811 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00003812 // /usr/include/mach-o.h, but it is basically:
3813 //
3814 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3815 // uint32_t count - this is the count of longs in the thread state data
3816 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3817 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00003818 //
Jim Ingham672e6f52011-03-07 23:44:08 +00003819 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3820 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3821 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3822 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3823 //
3824 // For now we hard-code the offsets and flavors we need:
3825 //
3826 //
3827
Greg Claytona1743492012-03-13 23:14:29 +00003828 ModuleSP module_sp(GetModule());
3829 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00003830 {
Greg Claytona1743492012-03-13 23:14:29 +00003831 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3832 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00003833 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003834 uint32_t i;
3835 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3836 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003837
Greg Claytona1743492012-03-13 23:14:29 +00003838 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00003839 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003840 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00003841 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3842 break;
3843
3844 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00003845 {
Greg Claytona1743492012-03-13 23:14:29 +00003846 case LoadCommandUnixThread:
3847 case LoadCommandThread:
Jim Ingham672e6f52011-03-07 23:44:08 +00003848 {
Greg Claytona1743492012-03-13 23:14:29 +00003849 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00003850 {
Greg Claytona1743492012-03-13 23:14:29 +00003851 uint32_t flavor = m_data.GetU32(&offset);
3852 uint32_t count = m_data.GetU32(&offset);
3853 if (count == 0)
3854 {
3855 // We've gotten off somehow, log and exit;
3856 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00003857 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003858
Greg Claytona1743492012-03-13 23:14:29 +00003859 switch (m_header.cputype)
3860 {
3861 case llvm::MachO::CPUTypeARM:
3862 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3863 {
3864 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3865 start_address = m_data.GetU32(&offset);
3866 done = true;
3867 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003868 break;
Greg Claytona1743492012-03-13 23:14:29 +00003869 case llvm::MachO::CPUTypeI386:
3870 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3871 {
3872 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3873 start_address = m_data.GetU32(&offset);
3874 done = true;
3875 }
3876 break;
3877 case llvm::MachO::CPUTypeX86_64:
3878 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3879 {
3880 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3881 start_address = m_data.GetU64(&offset);
3882 done = true;
3883 }
3884 break;
3885 default:
3886 return m_entry_point_address;
3887 }
3888 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3889 if (done)
3890 break;
3891 offset += count * 4;
3892 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003893 }
Greg Claytona1743492012-03-13 23:14:29 +00003894 break;
3895 case LoadCommandMain:
Sean Callanan226b70c2012-03-08 02:39:03 +00003896 {
Greg Claytona1743492012-03-13 23:14:29 +00003897 ConstString text_segment_name ("__TEXT");
3898 uint64_t entryoffset = m_data.GetU64(&offset);
3899 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3900 if (text_segment_sp)
3901 {
3902 done = true;
3903 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3904 }
Sean Callanan226b70c2012-03-08 02:39:03 +00003905 }
Greg Claytona1743492012-03-13 23:14:29 +00003906
3907 default:
3908 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00003909 }
Greg Claytona1743492012-03-13 23:14:29 +00003910 if (done)
3911 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00003912
Greg Claytona1743492012-03-13 23:14:29 +00003913 // Go to the next load command:
3914 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00003915 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003916
Greg Claytona1743492012-03-13 23:14:29 +00003917 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00003918 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00003919 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00003920 // of this ObjectFile:
3921 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00003922 {
Greg Claytona1743492012-03-13 23:14:29 +00003923 m_entry_point_address.Clear();
3924 }
3925 }
3926 else
3927 {
3928 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3929 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003930
Greg Claytona1743492012-03-13 23:14:29 +00003931 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00003932
Greg Claytona1743492012-03-13 23:14:29 +00003933 if (module_sp)
3934 {
3935 SymbolContextList contexts;
3936 SymbolContext context;
3937 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3938 {
3939 if (contexts.GetContextAtIndex(0, context))
3940 m_entry_point_address = context.symbol->GetAddress();
3941 }
Greg Claytone72dfb32012-02-24 01:59:29 +00003942 }
3943 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003944 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003945
Jim Ingham672e6f52011-03-07 23:44:08 +00003946 return m_entry_point_address;
3947
3948}
3949
Greg Claytonc9660542012-02-05 02:38:54 +00003950lldb_private::Address
3951ObjectFileMachO::GetHeaderAddress ()
3952{
3953 lldb_private::Address header_addr;
3954 SectionList *section_list = GetSectionList();
3955 if (section_list)
3956 {
3957 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3958 if (text_segment_sp)
3959 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003960 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00003961 header_addr.SetOffset (0);
3962 }
3963 }
3964 return header_addr;
3965}
3966
Greg Claytonc3776bf2012-02-09 06:16:32 +00003967uint32_t
3968ObjectFileMachO::GetNumThreadContexts ()
3969{
Greg Claytona1743492012-03-13 23:14:29 +00003970 ModuleSP module_sp(GetModule());
3971 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00003972 {
Greg Claytona1743492012-03-13 23:14:29 +00003973 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3974 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00003975 {
Greg Claytona1743492012-03-13 23:14:29 +00003976 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00003977 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003978 FileRangeArray::Entry file_range;
3979 thread_command thread_cmd;
3980 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00003981 {
Greg Claytona1743492012-03-13 23:14:29 +00003982 const uint32_t cmd_offset = offset;
3983 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3984 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003985
Greg Claytona1743492012-03-13 23:14:29 +00003986 if (thread_cmd.cmd == LoadCommandThread)
3987 {
3988 file_range.SetRangeBase (offset);
3989 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3990 m_thread_context_offsets.Append (file_range);
3991 }
3992 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00003993 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00003994 }
3995 }
3996 return m_thread_context_offsets.GetSize();
3997}
3998
3999lldb::RegisterContextSP
4000ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
4001{
Greg Claytonc3776bf2012-02-09 06:16:32 +00004002 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00004003
Greg Claytona1743492012-03-13 23:14:29 +00004004 ModuleSP module_sp(GetModule());
4005 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004006 {
Greg Claytona1743492012-03-13 23:14:29 +00004007 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4008 if (!m_thread_context_offsets_valid)
4009 GetNumThreadContexts ();
4010
4011 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00004012 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00004013 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004014
4015 DataExtractor data (m_data,
4016 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00004017 thread_context_file_range->GetByteSize());
4018
4019 switch (m_header.cputype)
4020 {
4021 case llvm::MachO::CPUTypeARM:
4022 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
4023 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004024
Jim Ingham28eb5712012-10-12 17:34:26 +00004025 case llvm::MachO::CPUTypeI386:
4026 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
4027 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004028
Jim Ingham28eb5712012-10-12 17:34:26 +00004029 case llvm::MachO::CPUTypeX86_64:
4030 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
4031 break;
4032 }
Greg Claytona1743492012-03-13 23:14:29 +00004033 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004034 }
4035 return reg_ctx_sp;
4036}
4037
Greg Claytonc9660542012-02-05 02:38:54 +00004038
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004039ObjectFile::Type
4040ObjectFileMachO::CalculateType()
4041{
4042 switch (m_header.filetype)
4043 {
4044 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4045 if (GetAddressByteSize () == 4)
4046 {
4047 // 32 bit kexts are just object files, but they do have a valid
4048 // UUID load command.
4049 UUID uuid;
4050 if (GetUUID(&uuid))
4051 {
4052 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004053 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004054 // "OSKextGetCurrentIdentifier" as this is required of kexts
4055 if (m_strata == eStrataInvalid)
4056 m_strata = eStrataKernel;
4057 return eTypeSharedLibrary;
4058 }
4059 }
4060 return eTypeObjectFile;
4061
4062 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
4063 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
4064 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
4065 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
4066 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
4067 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
4068 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
4069 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
4070 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
4071 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
4072 default:
4073 break;
4074 }
4075 return eTypeUnknown;
4076}
4077
4078ObjectFile::Strata
4079ObjectFileMachO::CalculateStrata()
4080{
4081 switch (m_header.filetype)
4082 {
4083 case HeaderFileTypeObject: // 0x1u MH_OBJECT
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_type == eTypeInvalid)
4094 m_type = eTypeSharedLibrary;
4095
4096 return eStrataKernel;
4097 }
4098 }
4099 return eStrataUnknown;
4100
4101 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
4102 // Check for the MH_DYLDLINK bit in the flags
4103 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callanan49bce8e2012-02-10 20:22:35 +00004104 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004105 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00004106 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004107 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00004108 {
4109 SectionList *section_list = GetSectionList();
4110 if (section_list)
4111 {
4112 static ConstString g_kld_section_name ("__KLD");
4113 if (section_list->FindSectionByName(g_kld_section_name))
4114 return eStrataKernel;
4115 }
4116 }
4117 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004118
4119 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4120 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callanan49bce8e2012-02-10 20:22:35 +00004121 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004122 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4123 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4124 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4125 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4126 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4127 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4128 default:
4129 break;
4130 }
4131 return eStrataUnknown;
4132}
4133
4134
Greg Claytonc2ff9312012-02-22 19:41:02 +00004135uint32_t
4136ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4137{
Greg Claytona1743492012-03-13 23:14:29 +00004138 ModuleSP module_sp(GetModule());
4139 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004140 {
Greg Claytona1743492012-03-13 23:14:29 +00004141 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4142 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004143 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004144 uint32_t version_cmd = 0;
4145 uint64_t version = 0;
4146 uint32_t i;
4147 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004148 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004149 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004150 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4151 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004152
Greg Claytona1743492012-03-13 23:14:29 +00004153 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004154 {
Greg Claytona1743492012-03-13 23:14:29 +00004155 if (version_cmd == 0)
4156 {
4157 version_cmd = load_cmd.cmd;
4158 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4159 break;
4160 version = load_cmd.dylib.current_version;
4161 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004162 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00004163 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00004164 }
Greg Claytona1743492012-03-13 23:14:29 +00004165 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004166 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004167
Greg Claytona1743492012-03-13 23:14:29 +00004168 if (version_cmd == LoadCommandDylibIdent)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004169 {
Greg Claytona1743492012-03-13 23:14:29 +00004170 if (versions != NULL && num_versions > 0)
4171 {
4172 if (num_versions > 0)
4173 versions[0] = (version & 0xFFFF0000ull) >> 16;
4174 if (num_versions > 1)
4175 versions[1] = (version & 0x0000FF00ull) >> 8;
4176 if (num_versions > 2)
4177 versions[2] = (version & 0x000000FFull);
4178 // Fill in an remaining version numbers with invalid values
4179 for (i=3; i<num_versions; ++i)
4180 versions[i] = UINT32_MAX;
4181 }
4182 // The LC_ID_DYLIB load command has a version with 3 version numbers
4183 // in it, so always return 3
4184 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004185 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00004186 }
4187 return false;
4188}
4189
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004190bool
Greg Clayton514487e2011-02-15 21:59:32 +00004191ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004192{
Greg Claytona1743492012-03-13 23:14:29 +00004193 ModuleSP module_sp(GetModule());
4194 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00004195 {
Greg Claytona1743492012-03-13 23:14:29 +00004196 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4197 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +00004198
Greg Claytona1743492012-03-13 23:14:29 +00004199 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda4e7511e2013-03-06 23:19:17 +00004200 // debugs at the addresses in the file itself. Below we set the OS to
Greg Claytona1743492012-03-13 23:14:29 +00004201 // unknown to make sure we use the DynamicLoaderStatic()...
4202 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4203 {
4204 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4205 }
4206 return true;
Greg Clayton593577a2011-09-21 03:57:31 +00004207 }
Greg Claytona1743492012-03-13 23:14:29 +00004208 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004209}
4210
4211
Jason Molenda0e0954c2013-04-16 06:24:42 +00004212UUID
4213ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4214{
4215 UUID uuid;
4216 if (process)
4217 {
4218 addr_t all_image_infos = process->GetImageInfoAddress();
4219
4220 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4221 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4222 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4223 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4224
4225 Error err;
4226 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4227 if (version_or_magic != -1
4228 && version_or_magic != HeaderMagic32
4229 && version_or_magic != HeaderMagic32Swapped
4230 && version_or_magic != HeaderMagic64
4231 && version_or_magic != HeaderMagic64Swapped
4232 && version_or_magic >= 13)
4233 {
4234 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4235 int wordsize = process->GetAddressByteSize();
4236 if (wordsize == 8)
4237 {
4238 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4239 }
4240 if (wordsize == 4)
4241 {
4242 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4243 }
4244 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4245 {
4246 uuid_t shared_cache_uuid;
4247 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4248 {
4249 uuid.SetBytes (shared_cache_uuid);
4250 }
4251 }
4252 }
4253 }
4254 return uuid;
4255}
4256
4257UUID
4258ObjectFileMachO::GetLLDBSharedCacheUUID ()
4259{
4260 UUID uuid;
4261#if defined (__APPLE__) && defined (__arm__)
4262 uint8_t *(*dyld_get_all_image_infos)(void);
4263 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4264 if (dyld_get_all_image_infos)
4265 {
4266 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4267 if (dyld_all_image_infos_address)
4268 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00004269 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4270 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00004271 {
Jason Molenda42b69fa2013-04-16 22:56:17 +00004272 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 +00004273 uuid.SetBytes (sharedCacheUUID_address);
4274 }
4275 }
4276 }
4277#endif
4278 return uuid;
4279}
4280
4281
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004282//------------------------------------------------------------------
4283// PluginInterface protocol
4284//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00004285lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004286ObjectFileMachO::GetPluginName()
4287{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004288 return GetPluginNameStatic();
4289}
4290
4291uint32_t
4292ObjectFileMachO::GetPluginVersion()
4293{
4294 return 1;
4295}
4296