blob: c59da7b16a17042bb89e45abedbe8897b91d5ecc [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 Clayton9aae0a12013-05-15 19:52:08 +0000516 data_offset = MachHeaderSizeFromMagic(header.magic) + file_offset;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000517 }
518 if (data_sp)
519 {
520 ModuleSpec spec;
521 spec.GetFileSpec() = file;
522 spec.GetArchitecture().SetArchitecture(eArchTypeMachO,
523 header.cputype,
524 header.cpusubtype);
525 if (spec.GetArchitecture().IsValid())
526 {
527 GetUUID (header, data, data_offset, spec.GetUUID());
528 specs.Append(spec);
529 }
530 }
531 }
532 }
533 return specs.GetSize() - initial_count;
534}
535
536
Greg Claytonc9660542012-02-05 02:38:54 +0000537
538const ConstString &
539ObjectFileMachO::GetSegmentNameTEXT()
540{
541 static ConstString g_segment_name_TEXT ("__TEXT");
542 return g_segment_name_TEXT;
543}
544
545const ConstString &
546ObjectFileMachO::GetSegmentNameDATA()
547{
548 static ConstString g_segment_name_DATA ("__DATA");
549 return g_segment_name_DATA;
550}
551
552const ConstString &
553ObjectFileMachO::GetSegmentNameOBJC()
554{
555 static ConstString g_segment_name_OBJC ("__OBJC");
556 return g_segment_name_OBJC;
557}
558
559const ConstString &
560ObjectFileMachO::GetSegmentNameLINKEDIT()
561{
562 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
563 return g_section_name_LINKEDIT;
564}
565
566const ConstString &
567ObjectFileMachO::GetSectionNameEHFrame()
568{
569 static ConstString g_section_name_eh_frame ("__eh_frame");
570 return g_section_name_eh_frame;
571}
572
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573bool
Jason Molenda4e7511e2013-03-06 23:19:17 +0000574ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
575 lldb::addr_t data_offset,
Greg Clayton44435ed2012-01-12 05:25:17 +0000576 lldb::addr_t data_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577{
Greg Clayton44435ed2012-01-12 05:25:17 +0000578 DataExtractor data;
579 data.SetData (data_sp, data_offset, data_length);
Greg Claytonc7bece562013-01-25 18:06:21 +0000580 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 uint32_t magic = data.GetU32(&offset);
582 return MachHeaderSizeFromMagic(magic) != 0;
583}
584
585
Greg Clayton5ce9c562013-02-06 17:22:03 +0000586ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
587 DataBufferSP& data_sp,
588 lldb::offset_t data_offset,
589 const FileSpec* file,
590 lldb::offset_t file_offset,
591 lldb::offset_t length) :
592 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000593 m_mach_segments(),
594 m_mach_sections(),
595 m_entry_point_address(),
596 m_thread_context_offsets(),
597 m_thread_context_offsets_valid(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598{
Greg Clayton72b77eb2011-02-04 21:13:05 +0000599 ::memset (&m_header, 0, sizeof(m_header));
600 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601}
602
Greg Claytone72dfb32012-02-24 01:59:29 +0000603ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000604 lldb::DataBufferSP& header_data_sp,
605 const lldb::ProcessSP &process_sp,
606 lldb::addr_t header_addr) :
Greg Claytone72dfb32012-02-24 01:59:29 +0000607 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000608 m_mach_segments(),
609 m_mach_sections(),
610 m_entry_point_address(),
611 m_thread_context_offsets(),
612 m_thread_context_offsets_valid(false)
Greg Claytonc9660542012-02-05 02:38:54 +0000613{
614 ::memset (&m_header, 0, sizeof(m_header));
615 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
616}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617
618ObjectFileMachO::~ObjectFileMachO()
619{
620}
621
Greg Claytonf4d6de62013-04-24 22:29:28 +0000622bool
623ObjectFileMachO::ParseHeader (DataExtractor &data,
624 lldb::offset_t *data_offset_ptr,
625 llvm::MachO::mach_header &header)
626{
627 data.SetByteOrder (lldb::endian::InlHostByteOrder());
628 // Leave magic in the original byte order
629 header.magic = data.GetU32(data_offset_ptr);
630 bool can_parse = false;
631 bool is_64_bit = false;
632 switch (header.magic)
633 {
634 case HeaderMagic32:
635 data.SetByteOrder (lldb::endian::InlHostByteOrder());
636 data.SetAddressByteSize(4);
637 can_parse = true;
638 break;
639
640 case HeaderMagic64:
641 data.SetByteOrder (lldb::endian::InlHostByteOrder());
642 data.SetAddressByteSize(8);
643 can_parse = true;
644 is_64_bit = true;
645 break;
646
647 case HeaderMagic32Swapped:
648 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
649 data.SetAddressByteSize(4);
650 can_parse = true;
651 break;
652
653 case HeaderMagic64Swapped:
654 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
655 data.SetAddressByteSize(8);
656 is_64_bit = true;
657 can_parse = true;
658 break;
659
660 default:
661 break;
662 }
663
664 if (can_parse)
665 {
666 data.GetU32(data_offset_ptr, &header.cputype, 6);
667 if (is_64_bit)
668 *data_offset_ptr += 4;
669 return true;
670 }
671 else
672 {
673 memset(&header, 0, sizeof(header));
674 }
675 return false;
676}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677
678bool
679ObjectFileMachO::ParseHeader ()
680{
Greg Claytona1743492012-03-13 23:14:29 +0000681 ModuleSP module_sp(GetModule());
682 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 {
Greg Claytona1743492012-03-13 23:14:29 +0000684 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
685 bool can_parse = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000686 lldb::offset_t offset = 0;
Greg Clayton7fb56d02011-02-01 01:31:41 +0000687 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +0000688 // Leave magic in the original byte order
689 m_header.magic = m_data.GetU32(&offset);
690 switch (m_header.magic)
Greg Claytonc9660542012-02-05 02:38:54 +0000691 {
Greg Claytona1743492012-03-13 23:14:29 +0000692 case HeaderMagic32:
693 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
694 m_data.SetAddressByteSize(4);
695 can_parse = true;
696 break;
697
698 case HeaderMagic64:
699 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
700 m_data.SetAddressByteSize(8);
701 can_parse = true;
702 break;
703
704 case HeaderMagic32Swapped:
705 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
706 m_data.SetAddressByteSize(4);
707 can_parse = true;
708 break;
709
710 case HeaderMagic64Swapped:
711 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
712 m_data.SetAddressByteSize(8);
713 can_parse = true;
714 break;
715
716 default:
717 break;
Greg Claytonc9660542012-02-05 02:38:54 +0000718 }
Greg Claytona1743492012-03-13 23:14:29 +0000719
720 if (can_parse)
721 {
722 m_data.GetU32(&offset, &m_header.cputype, 6);
723
724 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +0000725
Greg Claytond97ec1c2012-11-16 21:36:10 +0000726 // Check if the module has a required architecture
727 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callananbf4b7be2012-12-13 22:07:14 +0000728 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Claytond97ec1c2012-11-16 21:36:10 +0000729 return false;
730
Greg Claytona1743492012-03-13 23:14:29 +0000731 if (SetModulesArchitecture (mach_arch))
732 {
733 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
734 if (m_data.GetByteSize() < header_and_lc_size)
735 {
736 DataBufferSP data_sp;
737 ProcessSP process_sp (m_process_wp.lock());
738 if (process_sp)
739 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000740 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Claytona1743492012-03-13 23:14:29 +0000741 }
742 else
743 {
744 // Read in all only the load command data from the file on disk
Greg Clayton5ce9c562013-02-06 17:22:03 +0000745 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Claytona1743492012-03-13 23:14:29 +0000746 if (data_sp->GetByteSize() != header_and_lc_size)
747 return false;
748 }
749 if (data_sp)
750 m_data.SetData (data_sp);
751 }
752 }
753 return true;
754 }
755 else
756 {
757 memset(&m_header, 0, sizeof(struct mach_header));
758 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759 }
760 return false;
761}
762
763
764ByteOrder
765ObjectFileMachO::GetByteOrder () const
766{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767 return m_data.GetByteOrder ();
768}
769
Jim Ingham5aee1622010-08-09 23:31:02 +0000770bool
771ObjectFileMachO::IsExecutable() const
772{
773 return m_header.filetype == HeaderFileTypeExecutable;
774}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775
Greg Claytonc7bece562013-01-25 18:06:21 +0000776uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777ObjectFileMachO::GetAddressByteSize () const
778{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779 return m_data.GetAddressByteSize ();
780}
781
Greg Claytone0d378b2011-03-24 21:19:54 +0000782AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +0000783ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
784{
785 Symtab *symtab = GetSymtab();
786 if (symtab)
787 {
788 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
789 if (symbol)
790 {
Greg Claytone7612132012-03-07 21:03:09 +0000791 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +0000792 {
Greg Claytone7612132012-03-07 21:03:09 +0000793 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +0000794 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +0000795 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000796 const SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000797 switch (section_type)
798 {
799 case eSectionTypeInvalid: return eAddressClassUnknown;
800 case eSectionTypeCode:
801 if (m_header.cputype == llvm::MachO::CPUTypeARM)
802 {
803 // For ARM we have a bit in the n_desc field of the symbol
804 // that tells us ARM/Thumb which is bit 0x0008.
805 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
806 return eAddressClassCodeAlternateISA;
807 }
808 return eAddressClassCode;
809
810 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton5009f9d2011-10-27 17:55:14 +0000811 case eSectionTypeData:
812 case eSectionTypeDataCString:
813 case eSectionTypeDataCStringPointers:
814 case eSectionTypeDataSymbolAddress:
815 case eSectionTypeData4:
816 case eSectionTypeData8:
817 case eSectionTypeData16:
818 case eSectionTypeDataPointers:
819 case eSectionTypeZeroFill:
820 case eSectionTypeDataObjCMessageRefs:
821 case eSectionTypeDataObjCCFStrings:
822 return eAddressClassData;
823 case eSectionTypeDebug:
824 case eSectionTypeDWARFDebugAbbrev:
825 case eSectionTypeDWARFDebugAranges:
826 case eSectionTypeDWARFDebugFrame:
827 case eSectionTypeDWARFDebugInfo:
828 case eSectionTypeDWARFDebugLine:
829 case eSectionTypeDWARFDebugLoc:
830 case eSectionTypeDWARFDebugMacInfo:
831 case eSectionTypeDWARFDebugPubNames:
832 case eSectionTypeDWARFDebugPubTypes:
833 case eSectionTypeDWARFDebugRanges:
834 case eSectionTypeDWARFDebugStr:
835 case eSectionTypeDWARFAppleNames:
836 case eSectionTypeDWARFAppleTypes:
837 case eSectionTypeDWARFAppleNamespaces:
838 case eSectionTypeDWARFAppleObjC:
839 return eAddressClassDebug;
Greg Claytonded470d2011-03-19 01:12:21 +0000840 case eSectionTypeEHFrame: return eAddressClassRuntime;
Michael Sartaina7499c92013-07-01 19:45:50 +0000841 case eSectionTypeELFSymbolTable:
842 case eSectionTypeELFDynamicSymbols:
843 case eSectionTypeELFRelocationEntries:
844 case eSectionTypeELFDynamicLinkInfo:
Greg Claytonded470d2011-03-19 01:12:21 +0000845 case eSectionTypeOther: return eAddressClassUnknown;
846 }
847 }
848 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000849
Greg Claytone0d378b2011-03-24 21:19:54 +0000850 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000851 switch (symbol_type)
852 {
853 case eSymbolTypeAny: return eAddressClassUnknown;
854 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000855
Greg Claytonded470d2011-03-19 01:12:21 +0000856 case eSymbolTypeCode:
857 case eSymbolTypeTrampoline:
Greg Clayton059f7242013-02-27 21:16:04 +0000858 case eSymbolTypeResolver:
Greg Claytonded470d2011-03-19 01:12:21 +0000859 if (m_header.cputype == llvm::MachO::CPUTypeARM)
860 {
861 // For ARM we have a bit in the n_desc field of the symbol
862 // that tells us ARM/Thumb which is bit 0x0008.
863 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
864 return eAddressClassCodeAlternateISA;
865 }
866 return eAddressClassCode;
867
868 case eSymbolTypeData: return eAddressClassData;
869 case eSymbolTypeRuntime: return eAddressClassRuntime;
870 case eSymbolTypeException: return eAddressClassRuntime;
871 case eSymbolTypeSourceFile: return eAddressClassDebug;
872 case eSymbolTypeHeaderFile: return eAddressClassDebug;
873 case eSymbolTypeObjectFile: return eAddressClassDebug;
874 case eSymbolTypeCommonBlock: return eAddressClassDebug;
875 case eSymbolTypeBlock: return eAddressClassDebug;
876 case eSymbolTypeLocal: return eAddressClassData;
877 case eSymbolTypeParam: return eAddressClassData;
878 case eSymbolTypeVariable: return eAddressClassData;
879 case eSymbolTypeVariableType: return eAddressClassDebug;
880 case eSymbolTypeLineEntry: return eAddressClassDebug;
881 case eSymbolTypeLineHeader: return eAddressClassDebug;
882 case eSymbolTypeScopeBegin: return eAddressClassDebug;
883 case eSymbolTypeScopeEnd: return eAddressClassDebug;
884 case eSymbolTypeAdditional: return eAddressClassUnknown;
885 case eSymbolTypeCompiler: return eAddressClassDebug;
886 case eSymbolTypeInstrumentation:return eAddressClassDebug;
887 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +0000888 case eSymbolTypeObjCClass: return eAddressClassRuntime;
889 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
890 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +0000891 }
892 }
893 }
894 return eAddressClassUnknown;
895}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000896
897Symtab *
Michael Sartaina7499c92013-07-01 19:45:50 +0000898ObjectFileMachO::GetSymtab(uint32_t flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899{
Greg Claytona1743492012-03-13 23:14:29 +0000900 ModuleSP module_sp(GetModule());
901 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902 {
Greg Claytona1743492012-03-13 23:14:29 +0000903 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
904 if (m_symtab_ap.get() == NULL)
905 {
906 m_symtab_ap.reset(new Symtab(this));
907 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
908 ParseSymtab (true);
909 m_symtab_ap->Finalize ();
910 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911 }
912 return m_symtab_ap.get();
913}
914
915
916SectionList *
917ObjectFileMachO::GetSectionList()
918{
Greg Claytona1743492012-03-13 23:14:29 +0000919 ModuleSP module_sp(GetModule());
920 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000921 {
Greg Claytona1743492012-03-13 23:14:29 +0000922 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
923 if (m_sections_ap.get() == NULL)
924 {
925 m_sections_ap.reset(new SectionList());
926 ParseSections();
927 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928 }
929 return m_sections_ap.get();
930}
931
932
933size_t
934ObjectFileMachO::ParseSections ()
935{
936 lldb::user_id_t segID = 0;
937 lldb::user_id_t sectID = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000938 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939 uint32_t i;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000940 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941 //bool dump_sections = false;
Greg Claytone72dfb32012-02-24 01:59:29 +0000942 ModuleSP module_sp (GetModule());
Greg Clayton1eac0c72012-04-24 03:06:13 +0000943 // First look up any LC_ENCRYPTION_INFO load commands
944 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
945 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton4d78c402012-05-25 18:09:55 +0000946 encryption_info_command encryption_cmd;
Greg Clayton1eac0c72012-04-24 03:06:13 +0000947 for (i=0; i<m_header.ncmds; ++i)
948 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000949 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton4d78c402012-05-25 18:09:55 +0000950 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton1eac0c72012-04-24 03:06:13 +0000951 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000952
Greg Clayton4d78c402012-05-25 18:09:55 +0000953 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton1eac0c72012-04-24 03:06:13 +0000954 {
Greg Clayton4d78c402012-05-25 18:09:55 +0000955 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
956 {
957 if (encryption_cmd.cryptid != 0)
958 {
959 EncryptedFileRanges::Entry entry;
960 entry.SetRangeBase(encryption_cmd.cryptoff);
961 entry.SetByteSize(encryption_cmd.cryptsize);
962 encrypted_file_ranges.Append(entry);
963 }
964 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000965 }
Greg Clayton4d78c402012-05-25 18:09:55 +0000966 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton1eac0c72012-04-24 03:06:13 +0000967 }
968
969 offset = MachHeaderSizeFromMagic(m_header.magic);
970
Greg Clayton4d78c402012-05-25 18:09:55 +0000971 struct segment_command_64 load_cmd;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972 for (i=0; i<m_header.ncmds; ++i)
973 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000974 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000975 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
976 break;
977
Greg Claytone1a916a2010-07-21 22:12:05 +0000978 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000979 {
980 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
981 {
982 load_cmd.vmaddr = m_data.GetAddress(&offset);
983 load_cmd.vmsize = m_data.GetAddress(&offset);
984 load_cmd.fileoff = m_data.GetAddress(&offset);
985 load_cmd.filesize = m_data.GetAddress(&offset);
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000986 if (m_length != 0 && load_cmd.filesize != 0)
987 {
Greg Claytond37d6922013-04-16 16:51:19 +0000988 if (load_cmd.fileoff > m_length)
989 {
990 // We have a load command that says it extends past the end of hte file. This is likely
991 // a corrupt file. We don't have any way to return an error condition here (this method
992 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
993 // is null out the SectionList vector and if a process has been set up, dump a message
994 // to stdout. The most common case here is core file debugging with a truncated file.
995 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
996 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 ")",
997 i,
998 lc_segment_name,
999 load_cmd.fileoff,
1000 m_length);
Greg Claytona3f14d82013-06-04 20:27:06 +00001001
1002 load_cmd.fileoff = 0;
1003 load_cmd.filesize = 0;
Greg Claytond37d6922013-04-16 16:51:19 +00001004 }
1005
Jason Molendaf6ce26f2013-04-10 05:58:57 +00001006 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1007 {
1008 // We have a load command that says it extends past the end of hte file. This is likely
1009 // a corrupt file. We don't have any way to return an error condition here (this method
1010 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1011 // is null out the SectionList vector and if a process has been set up, dump a message
Greg Claytond37d6922013-04-16 16:51:19 +00001012 // to stdout. The most common case here is core file debugging with a truncated file.
1013 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Greg Claytona3f14d82013-06-04 20:27:06 +00001014 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",
Greg Claytond37d6922013-04-16 16:51:19 +00001015 i,
1016 lc_segment_name,
1017 load_cmd.fileoff + load_cmd.filesize,
1018 m_length);
Greg Claytona3f14d82013-06-04 20:27:06 +00001019
1020 // Tuncase the length
1021 load_cmd.filesize = m_length - load_cmd.fileoff;
Jason Molendaf6ce26f2013-04-10 05:58:57 +00001022 }
1023 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001024 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1025 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001026
Greg Clayton414f5d32011-01-25 02:58:48 +00001027 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
1028
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029 // Keep a list of mach segments around in case we need to
1030 // get at data that isn't stored in the abstracted Sections.
1031 m_mach_segments.push_back (load_cmd);
1032
Greg Claytonc7bece562013-01-25 18:06:21 +00001033 ConstString segment_name (load_cmd.segname, std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001034 // Use a segment ID of the segment index shifted left by 8 so they
1035 // never conflict with any of the sections.
1036 SectionSP segment_sp;
Greg Claytonc3776bf2012-02-09 06:16:32 +00001037 if (segment_name || is_core)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038 {
Greg Clayton1eac0c72012-04-24 03:06:13 +00001039 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Michael Sartaina7499c92013-07-01 19:45:50 +00001040 this, // Object file to which this sections belongs
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 ++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
1042 segment_name, // Name of this section
1043 eSectionTypeContainer, // This section is a container of other sections.
1044 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1045 load_cmd.vmsize, // VM size in bytes of this section
1046 load_cmd.fileoff, // Offset to the data for this section in the file
1047 load_cmd.filesize, // Size in bytes of this section as found in the the file
1048 load_cmd.flags)); // Flags for this section
1049
Greg Clayton414f5d32011-01-25 02:58:48 +00001050 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051 m_sections_ap->AddSection(segment_sp);
1052 }
1053
1054 struct section_64 sect64;
Greg Clayton72b77eb2011-02-04 21:13:05 +00001055 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001056 // Push a section into our mach sections for the section at
Jason Molenda4e7511e2013-03-06 23:19:17 +00001057 // index zero (NListSectionNoSection) if we don't have any
Greg Claytonf4abd0d2010-10-06 01:26:32 +00001058 // mach sections yet...
1059 if (m_mach_sections.empty())
1060 m_mach_sections.push_back(sect64);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001061 uint32_t segment_sect_idx;
1062 const lldb::user_id_t first_segment_sectID = sectID + 1;
1063
1064
Greg Claytone1a916a2010-07-21 22:12:05 +00001065 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
1067 {
1068 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
1069 break;
1070 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1071 break;
1072 sect64.addr = m_data.GetAddress(&offset);
1073 sect64.size = m_data.GetAddress(&offset);
1074
1075 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1076 break;
1077
1078 // Keep a list of mach sections around in case we need to
1079 // get at data that isn't stored in the abstracted Sections.
1080 m_mach_sections.push_back (sect64);
1081
1082 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1083 if (!segment_name)
1084 {
1085 // We have a segment with no name so we need to conjure up
1086 // segments that correspond to the section's segname if there
1087 // isn't already such a section. If there is such a section,
1088 // we resize the section so that it spans all sections.
1089 // We also mark these sections as fake so address matches don't
1090 // hit if they land in the gaps between the child sections.
1091 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1092 segment_sp = m_sections_ap->FindSectionByName (segment_name);
1093 if (segment_sp.get())
1094 {
1095 Section *segment = segment_sp.get();
1096 // Grow the section size as needed.
1097 const lldb::addr_t sect64_min_addr = sect64.addr;
1098 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1099 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1100 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1101 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1102 if (sect64_min_addr >= curr_seg_min_addr)
1103 {
1104 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1105 // Only grow the section size if needed
1106 if (new_seg_byte_size > curr_seg_byte_size)
1107 segment->SetByteSize (new_seg_byte_size);
1108 }
1109 else
1110 {
1111 // We need to change the base address of the segment and
1112 // adjust the child section offsets for all existing children.
1113 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1114 segment->Slide(slide_amount, false);
Sean Callanan56775362012-06-08 02:16:08 +00001115 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001116 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1117 }
Greg Clayton8d38ac42010-06-28 23:51:11 +00001118
1119 // Grow the section size as needed.
1120 if (sect64.offset)
1121 {
1122 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1123 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1124
1125 const lldb::addr_t section_min_file_offset = sect64.offset;
1126 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1127 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1128 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1129 segment->SetFileOffset (new_file_offset);
1130 segment->SetFileSize (new_file_size);
1131 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132 }
1133 else
1134 {
1135 // Create a fake section for the section's named segment
Greg Claytone72dfb32012-02-24 01:59:29 +00001136 segment_sp.reset(new Section (segment_sp, // Parent section
Michael Sartaina7499c92013-07-01 19:45:50 +00001137 module_sp, // Module to which this section belongs
1138 this, // Object file to which this section belongs
Greg Claytone72dfb32012-02-24 01:59:29 +00001139 ++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
1140 segment_name, // Name of this section
1141 eSectionTypeContainer, // This section is a container of other sections.
1142 sect64.addr, // File VM address == addresses as they are found in the object file
1143 sect64.size, // VM size in bytes of this section
1144 sect64.offset, // Offset to the data for this section in the file
1145 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1146 load_cmd.flags)); // Flags for this section
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147 segment_sp->SetIsFake(true);
1148 m_sections_ap->AddSection(segment_sp);
Greg Clayton414f5d32011-01-25 02:58:48 +00001149 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001150 }
1151 }
1152 assert (segment_sp.get());
1153
Greg Claytone1a916a2010-07-21 22:12:05 +00001154 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155 static ConstString g_sect_name_objc_data ("__objc_data");
1156 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1157 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1158 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1159 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1160 static ConstString g_sect_name_objc_const ("__objc_const");
1161 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1162 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001163
1164 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1165 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1166 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1167 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1168 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1169 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1170 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1171 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1172 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1173 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1174 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Clayton17674402011-09-28 17:06:40 +00001175 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1176 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton7f995132011-10-04 22:41:51 +00001177 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton5009f9d2011-10-27 17:55:14 +00001178 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001179 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton89411422010-10-08 00:21:05 +00001180 static ConstString g_sect_name_DATA ("__DATA");
1181 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton4ceb9982010-07-21 22:54:26 +00001182
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 SectionType sect_type = eSectionTypeOther;
1184
Greg Clayton4ceb9982010-07-21 22:54:26 +00001185 if (section_name == g_sect_name_dwarf_debug_abbrev)
1186 sect_type = eSectionTypeDWARFDebugAbbrev;
1187 else if (section_name == g_sect_name_dwarf_debug_aranges)
1188 sect_type = eSectionTypeDWARFDebugAranges;
1189 else if (section_name == g_sect_name_dwarf_debug_frame)
1190 sect_type = eSectionTypeDWARFDebugFrame;
1191 else if (section_name == g_sect_name_dwarf_debug_info)
1192 sect_type = eSectionTypeDWARFDebugInfo;
1193 else if (section_name == g_sect_name_dwarf_debug_line)
1194 sect_type = eSectionTypeDWARFDebugLine;
1195 else if (section_name == g_sect_name_dwarf_debug_loc)
1196 sect_type = eSectionTypeDWARFDebugLoc;
1197 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1198 sect_type = eSectionTypeDWARFDebugMacInfo;
1199 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1200 sect_type = eSectionTypeDWARFDebugPubNames;
1201 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1202 sect_type = eSectionTypeDWARFDebugPubTypes;
1203 else if (section_name == g_sect_name_dwarf_debug_ranges)
1204 sect_type = eSectionTypeDWARFDebugRanges;
1205 else if (section_name == g_sect_name_dwarf_debug_str)
1206 sect_type = eSectionTypeDWARFDebugStr;
Greg Clayton17674402011-09-28 17:06:40 +00001207 else if (section_name == g_sect_name_dwarf_apple_names)
1208 sect_type = eSectionTypeDWARFAppleNames;
1209 else if (section_name == g_sect_name_dwarf_apple_types)
1210 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton7f995132011-10-04 22:41:51 +00001211 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1212 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton5009f9d2011-10-27 17:55:14 +00001213 else if (section_name == g_sect_name_dwarf_apple_objc)
1214 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton4ceb9982010-07-21 22:54:26 +00001215 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton4ceb9982010-07-21 22:54:26 +00001219 else if (section_name == g_sect_name_eh_frame)
1220 sect_type = eSectionTypeEHFrame;
1221 else if (section_name == g_sect_name_cfstring)
1222 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001223 else if (section_name == g_sect_name_objc_data ||
1224 section_name == g_sect_name_objc_classrefs ||
1225 section_name == g_sect_name_objc_superrefs ||
1226 section_name == g_sect_name_objc_const ||
1227 section_name == g_sect_name_objc_classlist)
1228 {
1229 sect_type = eSectionTypeDataPointers;
1230 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231
1232 if (sect_type == eSectionTypeOther)
1233 {
1234 switch (mach_sect_type)
1235 {
1236 // TODO: categorize sections by other flags for regular sections
Greg Clayton89411422010-10-08 00:21:05 +00001237 case SectionTypeRegular:
1238 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda4e7511e2013-03-06 23:19:17 +00001239 sect_type = eSectionTypeCode;
Greg Clayton89411422010-10-08 00:21:05 +00001240 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda4e7511e2013-03-06 23:19:17 +00001241 sect_type = eSectionTypeData;
Greg Clayton89411422010-10-08 00:21:05 +00001242 else
Jason Molenda4e7511e2013-03-06 23:19:17 +00001243 sect_type = eSectionTypeOther;
Greg Clayton89411422010-10-08 00:21:05 +00001244 break;
Greg Claytone1a916a2010-07-21 22:12:05 +00001245 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1246 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1247 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1248 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1249 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1250 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1251 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1252 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1253 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1254 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1255 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1256 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1257 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1258 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1259 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1260 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001261 default: break;
1262 }
1263 }
1264
Greg Claytone72dfb32012-02-24 01:59:29 +00001265 SectionSP section_sp(new Section (segment_sp,
1266 module_sp,
Michael Sartaina7499c92013-07-01 19:45:50 +00001267 this,
Greg Claytone72dfb32012-02-24 01:59:29 +00001268 ++sectID,
1269 section_name,
1270 sect_type,
1271 sect64.addr - segment_sp->GetFileAddress(),
1272 sect64.size,
1273 sect64.offset,
1274 sect64.offset == 0 ? 0 : sect64.size,
1275 sect64.flags));
Greg Clayton414f5d32011-01-25 02:58:48 +00001276 // Set the section to be encrypted to match the segment
Jason Molenda4e7511e2013-03-06 23:19:17 +00001277
Greg Clayton1eac0c72012-04-24 03:06:13 +00001278 bool section_is_encrypted = false;
1279 if (!segment_is_encrypted && load_cmd.filesize != 0)
1280 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton414f5d32011-01-25 02:58:48 +00001281
Greg Clayton1eac0c72012-04-24 03:06:13 +00001282 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283 segment_sp->GetChildren().AddSection(section_sp);
1284
1285 if (segment_sp->IsFake())
1286 {
1287 segment_sp.reset();
1288 segment_name.Clear();
1289 }
1290 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001291 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001292 {
1293 if (first_segment_sectID <= sectID)
1294 {
1295 lldb::user_id_t sect_uid;
1296 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1297 {
1298 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1299 SectionSP next_section_sp;
1300 if (sect_uid + 1 <= sectID)
1301 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1302
1303 if (curr_section_sp.get())
1304 {
1305 if (curr_section_sp->GetByteSize() == 0)
1306 {
1307 if (next_section_sp.get() != NULL)
1308 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1309 else
1310 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1311 }
1312 }
1313 }
1314 }
1315 }
1316 }
1317 }
1318 }
Greg Claytone1a916a2010-07-21 22:12:05 +00001319 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001320 {
1321 m_dysymtab.cmd = load_cmd.cmd;
1322 m_dysymtab.cmdsize = load_cmd.cmdsize;
1323 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1324 }
1325
1326 offset = load_cmd_offset + load_cmd.cmdsize;
1327 }
1328// if (dump_sections)
1329// {
1330// StreamFile s(stdout);
1331// m_sections_ap->Dump(&s, true);
1332// }
1333 return sectID; // Return the number of sections we registered with the module
1334}
1335
1336class MachSymtabSectionInfo
1337{
1338public:
1339
1340 MachSymtabSectionInfo (SectionList *section_list) :
1341 m_section_list (section_list),
1342 m_section_infos()
1343 {
1344 // Get the number of sections down to a depth of 1 to include
1345 // all segments and their sections, but no other sections that
1346 // may be added for debug map or
1347 m_section_infos.resize(section_list->GetNumSections(1));
1348 }
1349
1350
Greg Claytone72dfb32012-02-24 01:59:29 +00001351 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001352 GetSection (uint8_t n_sect, addr_t file_addr)
1353 {
1354 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00001355 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001356 if (n_sect < m_section_infos.size())
1357 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001358 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001360 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1361 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001362 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00001363 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001364 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1365 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00001366 }
1367 else
1368 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001369 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00001370 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001371 }
1372 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00001373 {
1374 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00001375 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001376 }
1377 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1378 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1379 {
1380 // Symbol is in section with zero size, but has the same start
1381 // address as the section. This can happen with linker symbols
1382 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00001383 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001384 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001386 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001387 }
1388
1389protected:
1390 struct SectionInfo
1391 {
1392 SectionInfo () :
1393 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001394 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395 {
1396 }
1397
1398 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00001399 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001400 };
1401 SectionList *m_section_list;
1402 std::vector<SectionInfo> m_section_infos;
1403};
1404
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405size_t
1406ObjectFileMachO::ParseSymtab (bool minimize)
1407{
1408 Timer scoped_timer(__PRETTY_FUNCTION__,
1409 "ObjectFileMachO::ParseSymtab () module = %s",
1410 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001411 ModuleSP module_sp (GetModule());
1412 if (!module_sp)
1413 return 0;
1414
1415 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1416 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1417 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1418 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00001419 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001420 uint32_t i;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001421
Greg Clayton5160ce52013-03-27 23:08:40 +00001422 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton77ccca72011-12-30 00:32:24 +00001423
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001424 for (i=0; i<m_header.ncmds; ++i)
1425 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001426 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001427 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001428 struct load_command lc;
1429 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001430 break;
1431 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001432 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001434 case LoadCommandSymtab:
1435 symtab_load_command.cmd = lc.cmd;
1436 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001438 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1439 return 0;
1440 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001441 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001442 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001443 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001444 return 0;
1445 }
1446
1447 if (symtab_load_command.stroff == 0)
1448 {
1449 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001450 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001451 return 0;
1452 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001453
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001454 if (symtab_load_command.nsyms == 0)
1455 {
1456 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001457 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001458 return 0;
1459 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001460
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001461 if (symtab_load_command.strsize == 0)
1462 {
1463 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001464 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001465 return 0;
1466 }
1467 break;
1468
1469 case LoadCommandFunctionStarts:
1470 function_starts_load_command.cmd = lc.cmd;
1471 function_starts_load_command.cmdsize = lc.cmdsize;
1472 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1473 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1474 break;
1475
1476 default:
1477 break;
1478 }
1479 offset = cmd_offset + lc.cmdsize;
1480 }
1481
1482 if (symtab_load_command.cmd)
1483 {
1484 Symtab *symtab = m_symtab_ap.get();
1485 SectionList *section_list = GetSectionList();
1486 if (section_list == NULL)
1487 return 0;
1488
1489 ProcessSP process_sp (m_process_wp.lock());
Greg Clayton4c82d422012-05-18 23:20:01 +00001490 Process *process = process_sp.get();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001491
Greg Claytonc7bece562013-01-25 18:06:21 +00001492 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1493 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001494 bool bit_width_32 = addr_byte_size == 4;
1495 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1496
Greg Claytonc7bece562013-01-25 18:06:21 +00001497 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1498 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1499 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00001500 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00001501
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001502 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1503 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00001504 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1505 if (process)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001506 {
Greg Clayton4c82d422012-05-18 23:20:01 +00001507 Target &target = process->GetTarget();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001508 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1509 // Reading mach file from memory in a process or core file...
1510
1511 if (linkedit_section_sp)
1512 {
1513 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1514 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1515 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00001516 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00001517
1518 bool data_was_read = false;
1519
1520#if defined (__APPLE__) && defined (__arm__)
1521 if (m_header.flags & 0x80000000u)
Greg Clayton77ccca72011-12-30 00:32:24 +00001522 {
Greg Clayton26b47e22012-04-18 05:19:20 +00001523 // This mach-o memory file is in the dyld shared cache. If this
1524 // program is not remote and this is iOS, then this process will
1525 // share the same shared cache as the process we are debugging and
1526 // we can read the entire __LINKEDIT from the address space in this
1527 // process. This is a needed optimization that is used for local iOS
1528 // debugging only since all shared libraries in the shared cache do
1529 // not have corresponding files that exist in the file system of the
1530 // device. They have been combined into a single file. This means we
1531 // always have to load these files from memory. All of the symbol and
1532 // string tables from all of the __LINKEDIT sections from the shared
1533 // libraries in the shared cache have been merged into a single large
1534 // symbol and string table. Reading all of this symbol and string table
1535 // data across can slow down debug launch times, so we optimize this by
1536 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00001537
1538 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1539 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1540 bool use_lldb_cache = true;
1541 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1542 {
1543 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00001544 ModuleSP module_sp (GetModule());
1545 if (module_sp)
1546 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1547
Jason Molenda0e0954c2013-04-16 06:24:42 +00001548 }
1549
Greg Clayton26b47e22012-04-18 05:19:20 +00001550 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00001551 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00001552 {
1553 data_was_read = true;
1554 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00001555 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00001556 if (function_starts_load_command.cmd)
1557 {
1558 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1559 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1560 }
1561 }
1562 }
1563#endif
1564
1565 if (!data_was_read)
1566 {
1567 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1568 if (nlist_data_sp)
1569 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Clayton4c82d422012-05-18 23:20:01 +00001570 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1571 //if (strtab_data_sp)
1572 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molendad34e6522013-02-05 22:31:24 +00001573 if (m_dysymtab.nindirectsyms != 0)
1574 {
1575 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1576 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1577 if (indirect_syms_data_sp)
1578 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1579 }
Greg Clayton26b47e22012-04-18 05:19:20 +00001580 if (function_starts_load_command.cmd)
1581 {
1582 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1583 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1584 if (func_start_data_sp)
1585 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1586 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001587 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001588 }
1589 }
1590 else
1591 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001592 nlist_data.SetData (m_data,
1593 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001594 nlist_data_byte_size);
1595 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00001596 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001597 strtab_data_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00001598 if (m_dysymtab.nindirectsyms != 0)
1599 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001600 indirect_symbol_index_data.SetData (m_data,
1601 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00001602 m_dysymtab.nindirectsyms * 4);
1603 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001604 if (function_starts_load_command.cmd)
1605 {
1606 function_starts_data.SetData (m_data,
1607 function_starts_load_command.dataoff,
1608 function_starts_load_command.datasize);
1609 }
1610 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001611
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001612 if (nlist_data.GetByteSize() == 0)
1613 {
1614 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001615 module_sp->LogMessage(log, "failed to read nlist data");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001616 return 0;
1617 }
1618
1619
Greg Claytondebb8812012-05-25 17:04:00 +00001620 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1621 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00001622 {
Greg Claytondebb8812012-05-25 17:04:00 +00001623 if (process)
1624 {
1625 if (strtab_addr == LLDB_INVALID_ADDRESS)
1626 {
1627 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001628 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00001629 return 0;
1630 }
1631 }
1632 else
Greg Clayton4c82d422012-05-18 23:20:01 +00001633 {
1634 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001635 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00001636 return 0;
1637 }
1638 }
Greg Clayton4c82d422012-05-18 23:20:01 +00001639
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001640 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1641 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1642 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1643 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1644 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1645 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1646 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1647 SectionSP eh_frame_section_sp;
1648 if (text_section_sp.get())
1649 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1650 else
1651 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1652
Greg Clayton29e08cb2012-03-14 01:53:24 +00001653 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molenda5635f772013-03-21 03:36:01 +00001654
1655 // lldb works best if it knows the start addresss of all functions in a module.
1656 // Linker symbols or debug info are normally the best source of information for start addr / size but
1657 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00001658 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00001659 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1660 // binary, relative to the text section.
1661 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1662 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1663 // Binaries built to run on older releases may need to use eh_frame information.
1664
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001665 if (text_section_sp && function_starts_data.GetByteSize())
1666 {
1667 FunctionStarts::Entry function_start_entry;
1668 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00001669 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001670 function_start_entry.addr = text_section_sp->GetFileAddress();
1671 uint64_t delta;
1672 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1673 {
1674 // Now append the current entry
1675 function_start_entry.addr += delta;
1676 function_starts.Append(function_start_entry);
1677 }
Jason Molendad63d3c72013-04-16 00:18:44 +00001678 }
Jason Molenda5635f772013-03-21 03:36:01 +00001679 else
1680 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00001681 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1682 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1683 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1684 // the module.
1685 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00001686 {
1687 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1688 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1689 eh_frame.GetFunctionAddressAndSizeVector (functions);
1690 addr_t text_base_addr = text_section_sp->GetFileAddress();
1691 size_t count = functions.GetSize();
1692 for (size_t i = 0; i < count; ++i)
1693 {
1694 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1695 if (func)
1696 {
1697 FunctionStarts::Entry function_start_entry;
1698 function_start_entry.addr = func->base - text_base_addr;
1699 function_starts.Append(function_start_entry);
1700 }
1701 }
1702 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001703 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001704
Greg Claytonc7bece562013-01-25 18:06:21 +00001705 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001706
Greg Claytonc7bece562013-01-25 18:06:21 +00001707 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 +00001708
Greg Claytonc7bece562013-01-25 18:06:21 +00001709 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001710
1711 uint32_t N_SO_index = UINT32_MAX;
1712
1713 MachSymtabSectionInfo section_info (section_list);
1714 std::vector<uint32_t> N_FUN_indexes;
1715 std::vector<uint32_t> N_NSYM_indexes;
1716 std::vector<uint32_t> N_INCL_indexes;
1717 std::vector<uint32_t> N_BRAC_indexes;
1718 std::vector<uint32_t> N_COMM_indexes;
1719 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1720 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00001721 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001722 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1723 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00001724 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001725 // Any symbols that get merged into another will get an entry
1726 // in this map so we know
1727 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1728 uint32_t nlist_idx = 0;
1729 Symbol *symbol_ptr = NULL;
1730
1731 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00001732 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00001733 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00001734 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00001735 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001736
Jason Molendaa5609c82012-06-21 01:51:02 +00001737#if defined (__APPLE__) && defined (__arm__)
1738
1739 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1740 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1741 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1742 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1743 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1744 // nlist parser to ignore all LOCAL symbols.
1745
1746 if (m_header.flags & 0x80000000u)
1747 {
1748 // Before we can start mapping the DSC, we need to make certain the target process is actually
1749 // using the cache we can find.
1750
Jason Molendaa5609c82012-06-21 01:51:02 +00001751 // Next we need to determine the correct path for the dyld shared cache.
1752
1753 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1754 char dsc_path[PATH_MAX];
1755
1756 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00001757 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1758 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00001759 header_arch.GetArchitectureName());
1760
1761 FileSpec dsc_filespec(dsc_path, false);
1762
1763 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molendad63d3c72013-04-16 00:18:44 +00001764 struct lldb_copy_dyld_cache_header_v0
Greg Clayton946f8902012-09-05 22:30:51 +00001765 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001766 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1767 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1768 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda4e7511e2013-03-06 23:19:17 +00001769 uint32_t imagesOffset;
1770 uint32_t imagesCount;
1771 uint64_t dyldBaseAddress;
1772 uint64_t codeSignatureOffset;
1773 uint64_t codeSignatureSize;
1774 uint64_t slideInfoOffset;
1775 uint64_t slideInfoSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00001776 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1777 uint64_t localSymbolsSize; // size of local symbols information
1778 };
1779 struct lldb_copy_dyld_cache_header_v1
1780 {
1781 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1782 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1783 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1784 uint32_t imagesOffset;
1785 uint32_t imagesCount;
1786 uint64_t dyldBaseAddress;
1787 uint64_t codeSignatureOffset;
1788 uint64_t codeSignatureSize;
1789 uint64_t slideInfoOffset;
1790 uint64_t slideInfoSize;
Jason Molenda4e7511e2013-03-06 23:19:17 +00001791 uint64_t localSymbolsOffset;
1792 uint64_t localSymbolsSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00001793 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Clayton946f8902012-09-05 22:30:51 +00001794 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00001795
Jason Molendad63d3c72013-04-16 00:18:44 +00001796 struct lldb_copy_dyld_cache_mapping_info
1797 {
1798 uint64_t address;
1799 uint64_t size;
1800 uint64_t fileOffset;
1801 uint32_t maxProt;
1802 uint32_t initProt;
1803 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00001804
Greg Clayton946f8902012-09-05 22:30:51 +00001805 struct lldb_copy_dyld_cache_local_symbols_info
1806 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001807 uint32_t nlistOffset;
1808 uint32_t nlistCount;
1809 uint32_t stringsOffset;
1810 uint32_t stringsSize;
1811 uint32_t entriesOffset;
1812 uint32_t entriesCount;
Greg Clayton946f8902012-09-05 22:30:51 +00001813 };
1814 struct lldb_copy_dyld_cache_local_symbols_entry
1815 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001816 uint32_t dylibOffset;
1817 uint32_t nlistStartIndex;
1818 uint32_t nlistCount;
Greg Clayton946f8902012-09-05 22:30:51 +00001819 };
Jason Molendaa5609c82012-06-21 01:51:02 +00001820
Jason Molendaf8130862012-06-22 03:28:35 +00001821 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1822 The dyld_cache_local_symbols_info structure gives us three things:
1823 1. The start and count of the nlist records in the dyld_shared_cache file
1824 2. The start and size of the strings for these nlist records
1825 3. The start and count of dyld_cache_local_symbols_entry entries
1826
1827 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1828 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00001829 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00001830 and the count of how many nlist records there are for this dylib/framework.
1831 */
1832
Jason Molendaa5609c82012-06-21 01:51:02 +00001833 // Process the dsc header to find the unmapped symbols
1834 //
1835 // Save some VM space, do not map the entire cache in one shot.
1836
Jason Molenda255f9bb2013-03-06 23:17:36 +00001837 DataBufferSP dsc_data_sp;
1838 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1839
1840 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00001841 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001842 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00001843
Jason Molenda255f9bb2013-03-06 23:17:36 +00001844 char version_str[17];
1845 int version = -1;
1846 lldb::offset_t offset = 0;
1847 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1848 version_str[16] = '\0';
1849 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1850 {
1851 int v;
1852 if (::sscanf (version_str + 6, "%d", &v) == 1)
1853 {
1854 version = v;
1855 }
1856 }
1857
Jason Molenda0e0954c2013-04-16 06:24:42 +00001858 UUID dsc_uuid;
1859 if (version >= 1)
1860 {
1861 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1862 uint8_t uuid_bytes[sizeof (uuid_t)];
1863 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1864 dsc_uuid.SetBytes (uuid_bytes);
1865 }
1866
1867 bool uuid_match = true;
1868 if (dsc_uuid.IsValid() && process)
1869 {
1870 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1871
1872 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1873 {
1874 // The on-disk dyld_shared_cache file is not the same as the one in this
1875 // process' memory, don't use it.
1876 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00001877 ModuleSP module_sp (GetModule());
1878 if (module_sp)
1879 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 +00001880 }
1881 }
1882
Jason Molenda4e7511e2013-03-06 23:19:17 +00001883 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00001884
Jason Molendaa5609c82012-06-21 01:51:02 +00001885 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1886
1887 // If the mappingOffset points to a location inside the header, we've
1888 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda0e0954c2013-04-16 06:24:42 +00001889 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendaa5609c82012-06-21 01:51:02 +00001890 {
1891
Jason Molenda255f9bb2013-03-06 23:17:36 +00001892 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1893 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1894 offset = 0;
1895
1896 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1897 // in the shared library cache need to be adjusted by an offset to match up with the
1898 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1899 // recorded in mapping_offset_value.
1900 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1901
1902 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00001903 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1904 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1905
Jason Molenda4e7511e2013-03-06 23:19:17 +00001906 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00001907 {
1908 // Map the local symbols
Jason Molenda4e7511e2013-03-06 23:19:17 +00001909 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00001910 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001911 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00001912
1913 offset = 0;
1914
1915 // Read the local_symbols_infos struct in one shot
1916 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1917 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1918
Jason Molendaa5609c82012-06-21 01:51:02 +00001919 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1920
Jason Molenda255f9bb2013-03-06 23:17:36 +00001921 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00001922
1923 offset = local_symbols_info.entriesOffset;
1924 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1925 {
1926 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1927 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1928 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1929 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1930
Jason Molenda4e7511e2013-03-06 23:19:17 +00001931 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00001932 {
1933 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1934
1935 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1936 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1937 num_syms = symtab->GetNumSymbols();
1938
1939 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1940 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1941
Jason Molenda4e7511e2013-03-06 23:19:17 +00001942 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00001943 {
1944 /////////////////////////////
1945 {
1946 struct nlist_64 nlist;
1947 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1948 break;
1949
1950 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1951 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1952 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1953 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1954 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1955
1956 SymbolType type = eSymbolTypeInvalid;
1957 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1958
1959 if (symbol_name == NULL)
1960 {
1961 // No symbol should be NULL, even the symbols with no
1962 // string values should have an offset zero which points
1963 // to an empty C-string
1964 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001965 "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 +00001966 entry_index,
1967 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001968 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00001969 continue;
1970 }
1971 if (symbol_name[0] == '\0')
1972 symbol_name = NULL;
1973
1974 const char *symbol_name_non_abi_mangled = NULL;
1975
1976 SectionSP symbol_section;
1977 uint32_t symbol_byte_size = 0;
1978 bool add_nlist = true;
1979 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00001980 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00001981 bool is_gsym = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00001982
1983 assert (sym_idx < num_syms);
1984
1985 sym[sym_idx].SetDebug (is_debug);
1986
1987 if (is_debug)
1988 {
1989 switch (nlist.n_type)
1990 {
1991 case StabGlobalSymbol:
1992 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1993 // Sometimes the N_GSYM value contains the address.
1994
1995 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1996 // have the same address, but we want to ensure that we always find only the real symbol,
1997 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1998 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1999 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2000 // same address.
2001
2002 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2003 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2004 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2005 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2006 add_nlist = false;
2007 else
2008 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002009 is_gsym = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002010 sym[sym_idx].SetExternal(true);
2011 if (nlist.n_value != 0)
2012 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2013 type = eSymbolTypeData;
2014 }
2015 break;
2016
2017 case StabFunctionName:
2018 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2019 type = eSymbolTypeCompiler;
2020 break;
2021
2022 case StabFunction:
2023 // N_FUN -- procedure: name,,n_sect,linenumber,address
2024 if (symbol_name)
2025 {
2026 type = eSymbolTypeCode;
2027 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2028
2029 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2030 // We use the current number of symbols in the symbol table in lieu of
2031 // using nlist_idx in case we ever start trimming entries out
2032 N_FUN_indexes.push_back(sym_idx);
2033 }
2034 else
2035 {
2036 type = eSymbolTypeCompiler;
2037
2038 if ( !N_FUN_indexes.empty() )
2039 {
2040 // Copy the size of the function into the original STAB entry so we don't have
2041 // to hunt for it later
2042 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2043 N_FUN_indexes.pop_back();
2044 // We don't really need the end function STAB as it contains the size which
2045 // we already placed with the original symbol, so don't add it if we want a
2046 // minimal symbol table
2047 if (minimize)
2048 add_nlist = false;
2049 }
2050 }
2051 break;
2052
2053 case StabStaticSymbol:
2054 // N_STSYM -- static symbol: name,,n_sect,type,address
2055 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2056 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2057 type = eSymbolTypeData;
2058 break;
2059
2060 case StabLocalCommon:
2061 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2062 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2063 type = eSymbolTypeCommonBlock;
2064 break;
2065
2066 case StabBeginSymbol:
2067 // N_BNSYM
2068 // We use the current number of symbols in the symbol table in lieu of
2069 // using nlist_idx in case we ever start trimming entries out
2070 if (minimize)
2071 {
2072 // Skip these if we want minimal symbol tables
2073 add_nlist = false;
2074 }
2075 else
2076 {
2077 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2078 N_NSYM_indexes.push_back(sym_idx);
2079 type = eSymbolTypeScopeBegin;
2080 }
2081 break;
2082
2083 case StabEndSymbol:
2084 // N_ENSYM
2085 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2086 // so that we can always skip the entire symbol if we need to navigate
2087 // more quickly at the source level when parsing STABS
2088 if (minimize)
2089 {
2090 // Skip these if we want minimal symbol tables
2091 add_nlist = false;
2092 }
2093 else
2094 {
2095 if ( !N_NSYM_indexes.empty() )
2096 {
2097 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2098 symbol_ptr->SetByteSize(sym_idx + 1);
2099 symbol_ptr->SetSizeIsSibling(true);
2100 N_NSYM_indexes.pop_back();
2101 }
2102 type = eSymbolTypeScopeEnd;
2103 }
2104 break;
2105
2106
2107 case StabSourceFileOptions:
2108 // N_OPT - emitted with gcc2_compiled and in gcc source
2109 type = eSymbolTypeCompiler;
2110 break;
2111
2112 case StabRegisterSymbol:
2113 // N_RSYM - register sym: name,,NO_SECT,type,register
2114 type = eSymbolTypeVariable;
2115 break;
2116
2117 case StabSourceLine:
2118 // N_SLINE - src line: 0,,n_sect,linenumber,address
2119 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2120 type = eSymbolTypeLineEntry;
2121 break;
2122
2123 case StabStructureType:
2124 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2125 type = eSymbolTypeVariableType;
2126 break;
2127
2128 case StabSourceFileName:
2129 // N_SO - source file name
2130 type = eSymbolTypeSourceFile;
2131 if (symbol_name == NULL)
2132 {
2133 if (minimize)
2134 add_nlist = false;
2135 if (N_SO_index != UINT32_MAX)
2136 {
2137 // Set the size of the N_SO to the terminating index of this N_SO
2138 // so that we can always skip the entire N_SO if we need to navigate
2139 // more quickly at the source level when parsing STABS
2140 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2141 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2142 symbol_ptr->SetSizeIsSibling(true);
2143 }
2144 N_NSYM_indexes.clear();
2145 N_INCL_indexes.clear();
2146 N_BRAC_indexes.clear();
2147 N_COMM_indexes.clear();
2148 N_FUN_indexes.clear();
2149 N_SO_index = UINT32_MAX;
2150 }
2151 else
2152 {
2153 // We use the current number of symbols in the symbol table in lieu of
2154 // using nlist_idx in case we ever start trimming entries out
2155 const bool N_SO_has_full_path = symbol_name[0] == '/';
2156 if (N_SO_has_full_path)
2157 {
2158 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2159 {
2160 // We have two consecutive N_SO entries where the first contains a directory
2161 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00002162 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002163 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2164 add_nlist = false;
2165 }
2166 else
2167 {
2168 // This is the first entry in a N_SO that contains a directory or
2169 // a full path to the source file
2170 N_SO_index = sym_idx;
2171 }
2172 }
2173 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2174 {
2175 // This is usually the second N_SO entry that contains just the filename,
2176 // so here we combine it with the first one if we are minimizing the symbol table
2177 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2178 if (so_path && so_path[0])
2179 {
2180 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00002181 const size_t double_slash_pos = full_so_path.find("//");
2182 if (double_slash_pos != std::string::npos)
2183 {
2184 // The linker has been generating bad N_SO entries with doubled up paths
2185 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2186 // and the second is the directory for the source file so you end up with
2187 // a path that looks like "/tmp/src//tmp/src/"
2188 FileSpec so_dir(so_path, false);
2189 if (!so_dir.Exists())
2190 {
2191 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2192 if (so_dir.Exists())
2193 {
2194 // Trim off the incorrect path
2195 full_so_path.erase(0, double_slash_pos + 1);
2196 }
2197 }
2198 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002199 if (*full_so_path.rbegin() != '/')
2200 full_so_path += '/';
2201 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00002202 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002203 add_nlist = false;
2204 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2205 }
2206 }
Greg Clayton946f8902012-09-05 22:30:51 +00002207 else
2208 {
2209 // This could be a relative path to a N_SO
2210 N_SO_index = sym_idx;
2211 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002212 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002213 break;
2214
2215 case StabObjectFileName:
2216 // N_OSO - object file name: name,,0,0,st_mtime
2217 type = eSymbolTypeObjectFile;
2218 break;
2219
2220 case StabLocalSymbol:
2221 // N_LSYM - local sym: name,,NO_SECT,type,offset
2222 type = eSymbolTypeLocal;
2223 break;
2224
2225 //----------------------------------------------------------------------
2226 // INCL scopes
2227 //----------------------------------------------------------------------
2228 case StabBeginIncludeFileName:
2229 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2230 // We use the current number of symbols in the symbol table in lieu of
2231 // using nlist_idx in case we ever start trimming entries out
2232 N_INCL_indexes.push_back(sym_idx);
2233 type = eSymbolTypeScopeBegin;
2234 break;
2235
2236 case StabEndIncludeFile:
2237 // N_EINCL - include file end: name,,NO_SECT,0,0
2238 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2239 // so that we can always skip the entire symbol if we need to navigate
2240 // more quickly at the source level when parsing STABS
2241 if ( !N_INCL_indexes.empty() )
2242 {
2243 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2244 symbol_ptr->SetByteSize(sym_idx + 1);
2245 symbol_ptr->SetSizeIsSibling(true);
2246 N_INCL_indexes.pop_back();
2247 }
2248 type = eSymbolTypeScopeEnd;
2249 break;
2250
2251 case StabIncludeFileName:
2252 // N_SOL - #included file name: name,,n_sect,0,address
2253 type = eSymbolTypeHeaderFile;
2254
2255 // We currently don't use the header files on darwin
2256 if (minimize)
2257 add_nlist = false;
2258 break;
2259
2260 case StabCompilerParameters:
2261 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2262 type = eSymbolTypeCompiler;
2263 break;
2264
2265 case StabCompilerVersion:
2266 // N_VERSION - compiler version: name,,NO_SECT,0,0
2267 type = eSymbolTypeCompiler;
2268 break;
2269
2270 case StabCompilerOptLevel:
2271 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2272 type = eSymbolTypeCompiler;
2273 break;
2274
2275 case StabParameter:
2276 // N_PSYM - parameter: name,,NO_SECT,type,offset
2277 type = eSymbolTypeVariable;
2278 break;
2279
2280 case StabAlternateEntry:
2281 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2282 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2283 type = eSymbolTypeLineEntry;
2284 break;
2285
2286 //----------------------------------------------------------------------
2287 // Left and Right Braces
2288 //----------------------------------------------------------------------
2289 case StabLeftBracket:
2290 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2291 // We use the current number of symbols in the symbol table in lieu of
2292 // using nlist_idx in case we ever start trimming entries out
2293 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2294 N_BRAC_indexes.push_back(sym_idx);
2295 type = eSymbolTypeScopeBegin;
2296 break;
2297
2298 case StabRightBracket:
2299 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2300 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2301 // so that we can always skip the entire symbol if we need to navigate
2302 // more quickly at the source level when parsing STABS
2303 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2304 if ( !N_BRAC_indexes.empty() )
2305 {
2306 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2307 symbol_ptr->SetByteSize(sym_idx + 1);
2308 symbol_ptr->SetSizeIsSibling(true);
2309 N_BRAC_indexes.pop_back();
2310 }
2311 type = eSymbolTypeScopeEnd;
2312 break;
2313
2314 case StabDeletedIncludeFile:
2315 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2316 type = eSymbolTypeHeaderFile;
2317 break;
2318
2319 //----------------------------------------------------------------------
2320 // COMM scopes
2321 //----------------------------------------------------------------------
2322 case StabBeginCommon:
2323 // N_BCOMM - begin common: name,,NO_SECT,0,0
2324 // We use the current number of symbols in the symbol table in lieu of
2325 // using nlist_idx in case we ever start trimming entries out
2326 type = eSymbolTypeScopeBegin;
2327 N_COMM_indexes.push_back(sym_idx);
2328 break;
2329
2330 case StabEndCommonLocal:
2331 // N_ECOML - end common (local name): 0,,n_sect,0,address
2332 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2333 // Fall through
2334
2335 case StabEndCommon:
2336 // N_ECOMM - end common: name,,n_sect,0,0
2337 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2338 // so that we can always skip the entire symbol if we need to navigate
2339 // more quickly at the source level when parsing STABS
2340 if ( !N_COMM_indexes.empty() )
2341 {
2342 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2343 symbol_ptr->SetByteSize(sym_idx + 1);
2344 symbol_ptr->SetSizeIsSibling(true);
2345 N_COMM_indexes.pop_back();
2346 }
2347 type = eSymbolTypeScopeEnd;
2348 break;
2349
2350 case StabLength:
2351 // N_LENG - second stab entry with length information
2352 type = eSymbolTypeAdditional;
2353 break;
2354
2355 default: break;
2356 }
2357 }
2358 else
2359 {
2360 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2361 uint8_t n_type = NlistMaskType & nlist.n_type;
2362 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2363
2364 switch (n_type)
2365 {
2366 case NListTypeIndirect: // N_INDR - Fall through
2367 case NListTypePreboundUndefined:// N_PBUD - Fall through
2368 case NListTypeUndefined: // N_UNDF
2369 type = eSymbolTypeUndefined;
2370 break;
2371
2372 case NListTypeAbsolute: // N_ABS
2373 type = eSymbolTypeAbsolute;
2374 break;
2375
2376 case NListTypeSection: // N_SECT
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002377 {
2378 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002379
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002380 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00002381 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002382 // TODO: warn about this?
2383 add_nlist = false;
2384 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002385 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002386
2387 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00002388 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002389 type = eSymbolTypeException;
2390 }
2391 else
2392 {
2393 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002394
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002395 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00002396 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002397 case SectionTypeRegular: break; // regular section
2398 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2399 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2400 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2401 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2402 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2403 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2404 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2405 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2406 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2407 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2408 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2409 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2410 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2411 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2412 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2413 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2414 default: break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002415 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002416
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002417 if (type == eSymbolTypeInvalid)
2418 {
2419 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2420 if (symbol_section->IsDescendant (text_section_sp.get()))
2421 {
2422 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2423 SectionAttrUserSelfModifyingCode |
2424 SectionAttrSytemSomeInstructions))
2425 type = eSymbolTypeData;
2426 else
2427 type = eSymbolTypeCode;
2428 }
2429 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00002430 {
2431 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2432 {
2433 type = eSymbolTypeRuntime;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002434
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002435 if (symbol_name &&
2436 symbol_name[0] == '_' &&
2437 symbol_name[1] == 'O' &&
Jason Molendaa5609c82012-06-21 01:51:02 +00002438 symbol_name[2] == 'B')
2439 {
2440 llvm::StringRef symbol_name_ref(symbol_name);
2441 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2442 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2443 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2444 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2445 {
2446 symbol_name_non_abi_mangled = symbol_name + 1;
2447 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2448 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002449 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002450 }
2451 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2452 {
2453 symbol_name_non_abi_mangled = symbol_name + 1;
2454 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2455 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002456 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002457 }
2458 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2459 {
2460 symbol_name_non_abi_mangled = symbol_name + 1;
2461 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2462 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002463 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002464 }
2465 }
2466 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002467 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00002468 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002469 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00002470 }
2471 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002472 {
2473 type = eSymbolTypeData;
2474 }
2475 }
2476 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2477 {
2478 type = eSymbolTypeTrampoline;
2479 }
2480 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2481 {
2482 type = eSymbolTypeRuntime;
2483 if (symbol_name && symbol_name[0] == '.')
2484 {
2485 llvm::StringRef symbol_name_ref(symbol_name);
2486 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2487 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00002488 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002489 symbol_name_non_abi_mangled = symbol_name;
2490 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2491 type = eSymbolTypeObjCClass;
2492 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002493 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002494 }
2495 }
2496 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002497 }
2498 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002499 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002500 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002501 }
2502
2503 if (add_nlist)
2504 {
2505 uint64_t symbol_value = nlist.n_value;
2506 bool symbol_name_is_mangled = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002507
Jason Molendaa5609c82012-06-21 01:51:02 +00002508 if (symbol_name_non_abi_mangled)
2509 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00002510 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2511 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00002512 }
2513 else
2514 {
2515 if (symbol_name && symbol_name[0] == '_')
2516 {
2517 symbol_name_is_mangled = symbol_name[1] == '_';
2518 symbol_name++; // Skip the leading underscore
2519 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002520
Jason Molendaa5609c82012-06-21 01:51:02 +00002521 if (symbol_name)
2522 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002523 ConstString const_symbol_name(symbol_name);
2524 if (is_gsym)
2525 N_GSYM_name_to_sym_idx[const_symbol_name.GetCString()] = sym_idx;
2526 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Jason Molendaa5609c82012-06-21 01:51:02 +00002527 }
2528 }
2529 if (symbol_section)
2530 {
2531 const addr_t section_file_addr = symbol_section->GetFileAddress();
2532 if (symbol_byte_size == 0 && function_starts_count > 0)
2533 {
2534 addr_t symbol_lookup_file_addr = nlist.n_value;
2535 // Do an exact address match for non-ARM addresses, else get the closest since
2536 // the symbol might be a thumb symbol which has an address with bit zero set
2537 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2538 if (is_arm && func_start_entry)
2539 {
2540 // Verify that the function start address is the symbol address (ARM)
2541 // or the symbol address + 1 (thumb)
2542 if (func_start_entry->addr != symbol_lookup_file_addr &&
2543 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2544 {
2545 // Not the right entry, NULL it out...
2546 func_start_entry = NULL;
2547 }
2548 }
2549 if (func_start_entry)
2550 {
2551 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002552
Jason Molendaa5609c82012-06-21 01:51:02 +00002553 addr_t symbol_file_addr = func_start_entry->addr;
2554 uint32_t symbol_flags = 0;
2555 if (is_arm)
2556 {
2557 if (symbol_file_addr & 1)
2558 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2559 symbol_file_addr &= 0xfffffffffffffffeull;
2560 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002561
Jason Molendaa5609c82012-06-21 01:51:02 +00002562 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2563 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2564 if (next_func_start_entry)
2565 {
2566 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2567 // Be sure the clear the Thumb address bit when we calculate the size
2568 // from the current and next address
2569 if (is_arm)
2570 next_symbol_file_addr &= 0xfffffffffffffffeull;
2571 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2572 }
2573 else
2574 {
2575 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2576 }
2577 }
2578 }
2579 symbol_value -= section_file_addr;
2580 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002581
Greg Claytondacc4a92013-05-14 22:19:37 +00002582 if (is_debug == false)
2583 {
2584 if (type == eSymbolTypeCode)
2585 {
2586 // See if we can find a N_FUN entry for any code symbols.
2587 // If we do find a match, and the name matches, then we
2588 // can merge the two into just the function symbol to avoid
2589 // duplicate entries in the symbol table
2590 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2591 if (pos != N_FUN_addr_to_sym_idx.end())
2592 {
2593 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2594 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2595 {
2596 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2597 // We just need the flags from the linker symbol, so put these flags
2598 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2599 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2600 sym[sym_idx].Clear();
2601 continue;
2602 }
2603 }
2604 }
2605 else if (type == eSymbolTypeData)
2606 {
2607 // See if we can find a N_STSYM entry for any data symbols.
2608 // If we do find a match, and the name matches, then we
2609 // can merge the two into just the Static symbol to avoid
2610 // duplicate entries in the symbol table
2611 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2612 if (pos != N_STSYM_addr_to_sym_idx.end())
2613 {
2614 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2615 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2616 {
2617 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2618 // We just need the flags from the linker symbol, so put these flags
2619 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2620 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2621 sym[sym_idx].Clear();
2622 continue;
2623 }
2624 }
2625 else
2626 {
2627 // Combine N_GSYM stab entries with the non stab symbol
2628 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetMangledName().GetCString());
2629 if (pos != N_GSYM_name_to_sym_idx.end())
2630 {
2631 const uint32_t GSYM_sym_idx = pos->second;
2632 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
2633 // Copy the address, because often the N_GSYM address has an invalid address of zero
2634 // when the global is a common symbol
2635 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
2636 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
2637 // We just need the flags from the linker symbol, so put these flags
2638 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2639 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2640 sym[sym_idx].Clear();
2641 continue;
2642 }
2643 }
2644 }
2645 }
2646
Jason Molendaa5609c82012-06-21 01:51:02 +00002647 sym[sym_idx].SetID (nlist_idx);
2648 sym[sym_idx].SetType (type);
2649 sym[sym_idx].GetAddress().SetSection (symbol_section);
2650 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2651 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002652
Jason Molendaa5609c82012-06-21 01:51:02 +00002653 if (symbol_byte_size > 0)
2654 sym[sym_idx].SetByteSize(symbol_byte_size);
2655
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002656 if (demangled_is_synthesized)
2657 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00002658 ++sym_idx;
2659 }
2660 else
2661 {
2662 sym[sym_idx].Clear();
2663 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002664
Jason Molendaa5609c82012-06-21 01:51:02 +00002665 }
2666 /////////////////////////////
2667 }
2668 break; // No more entries to consider
2669 }
2670 }
2671 }
2672 }
2673 }
2674 }
2675 }
2676
2677 // Must reset this in case it was mutated above!
2678 nlist_data_offset = 0;
2679#endif
2680
2681 // If the sym array was not created while parsing the DSC unmapped
2682 // symbols, create it now.
2683 if (sym == NULL)
2684 {
2685 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2686 num_syms = symtab->GetNumSymbols();
2687 }
2688
2689 if (unmapped_local_symbols_found)
2690 {
2691 assert(m_dysymtab.ilocalsym == 0);
2692 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2693 nlist_idx = m_dysymtab.nlocalsym;
2694 }
2695 else
2696 {
2697 nlist_idx = 0;
2698 }
2699
2700 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002701 {
2702 struct nlist_64 nlist;
2703 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2704 break;
2705
2706 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2707 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2708 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2709 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2710 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2711
2712 SymbolType type = eSymbolTypeInvalid;
Greg Clayton4c82d422012-05-18 23:20:01 +00002713 const char *symbol_name = NULL;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002714
Greg Claytondebb8812012-05-25 17:04:00 +00002715 if (have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00002716 {
2717 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002718
Greg Clayton4c82d422012-05-18 23:20:01 +00002719 if (symbol_name == NULL)
2720 {
2721 // No symbol should be NULL, even the symbols with no
2722 // string values should have an offset zero which points
2723 // to an empty C-string
2724 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002725 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Greg Clayton4c82d422012-05-18 23:20:01 +00002726 nlist_idx,
2727 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002728 module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton4c82d422012-05-18 23:20:01 +00002729 continue;
2730 }
2731 if (symbol_name[0] == '\0')
2732 symbol_name = NULL;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002733 }
Greg Claytondebb8812012-05-25 17:04:00 +00002734 else
2735 {
2736 const addr_t str_addr = strtab_addr + nlist.n_strx;
2737 Error str_error;
2738 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2739 symbol_name = memory_symbol_name.c_str();
2740 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002741 const char *symbol_name_non_abi_mangled = NULL;
2742
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002743 SectionSP symbol_section;
Greg Claytonc7bece562013-01-25 18:06:21 +00002744 lldb::addr_t symbol_byte_size = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002745 bool add_nlist = true;
Greg Claytondacc4a92013-05-14 22:19:37 +00002746 bool is_gsym = false;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002747 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002748 bool demangled_is_synthesized = false;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002749
2750 assert (sym_idx < num_syms);
2751
2752 sym[sym_idx].SetDebug (is_debug);
2753
2754 if (is_debug)
2755 {
2756 switch (nlist.n_type)
Greg Clayton77ccca72011-12-30 00:32:24 +00002757 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002758 case StabGlobalSymbol:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002759 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2760 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002761
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002762 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2763 // have the same address, but we want to ensure that we always find only the real symbol,
2764 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2765 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2766 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2767 // same address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002768
2769 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002770 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2771 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2772 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2773 add_nlist = false;
2774 else
Greg Claytonc9660542012-02-05 02:38:54 +00002775 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002776 is_gsym = true;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002777 sym[sym_idx].SetExternal(true);
2778 if (nlist.n_value != 0)
2779 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2780 type = eSymbolTypeData;
Greg Claytonc9660542012-02-05 02:38:54 +00002781 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002782 break;
Greg Claytonc9660542012-02-05 02:38:54 +00002783
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002784 case StabFunctionName:
2785 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2786 type = eSymbolTypeCompiler;
2787 break;
Greg Clayton77ccca72011-12-30 00:32:24 +00002788
Jason Molenda4e7511e2013-03-06 23:19:17 +00002789 case StabFunction:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002790 // N_FUN -- procedure: name,,n_sect,linenumber,address
2791 if (symbol_name)
Greg Clayton4f8e8692011-10-31 20:50:40 +00002792 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002793 type = eSymbolTypeCode;
2794 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002795
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002796 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2797 // We use the current number of symbols in the symbol table in lieu of
2798 // using nlist_idx in case we ever start trimming entries out
2799 N_FUN_indexes.push_back(sym_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002800 }
2801 else
2802 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002803 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002804
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002805 if ( !N_FUN_indexes.empty() )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002806 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002807 // Copy the size of the function into the original STAB entry so we don't have
2808 // to hunt for it later
2809 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2810 N_FUN_indexes.pop_back();
2811 // We don't really need the end function STAB as it contains the size which
2812 // we already placed with the original symbol, so don't add it if we want a
2813 // minimal symbol table
2814 if (minimize)
Greg Clayton456809c2011-12-03 02:30:59 +00002815 add_nlist = false;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002816 }
Greg Clayton456809c2011-12-03 02:30:59 +00002817 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002818 break;
Greg Clayton456809c2011-12-03 02:30:59 +00002819
Jason Molenda4e7511e2013-03-06 23:19:17 +00002820 case StabStaticSymbol:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002821 // N_STSYM -- static symbol: name,,n_sect,type,address
2822 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2823 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2824 type = eSymbolTypeData;
2825 break;
2826
2827 case StabLocalCommon:
2828 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2829 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2830 type = eSymbolTypeCommonBlock;
2831 break;
2832
2833 case StabBeginSymbol:
2834 // N_BNSYM
2835 // We use the current number of symbols in the symbol table in lieu of
2836 // using nlist_idx in case we ever start trimming entries out
2837 if (minimize)
Greg Clayton456809c2011-12-03 02:30:59 +00002838 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002839 // Skip these if we want minimal symbol tables
2840 add_nlist = false;
2841 }
2842 else
2843 {
2844 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2845 N_NSYM_indexes.push_back(sym_idx);
2846 type = eSymbolTypeScopeBegin;
2847 }
2848 break;
Greg Clayton456809c2011-12-03 02:30:59 +00002849
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002850 case StabEndSymbol:
2851 // N_ENSYM
2852 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2853 // so that we can always skip the entire symbol if we need to navigate
2854 // more quickly at the source level when parsing STABS
2855 if (minimize)
2856 {
2857 // Skip these if we want minimal symbol tables
2858 add_nlist = false;
2859 }
2860 else
2861 {
2862 if ( !N_NSYM_indexes.empty() )
Greg Clayton456809c2011-12-03 02:30:59 +00002863 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002864 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2865 symbol_ptr->SetByteSize(sym_idx + 1);
2866 symbol_ptr->SetSizeIsSibling(true);
2867 N_NSYM_indexes.pop_back();
2868 }
2869 type = eSymbolTypeScopeEnd;
2870 }
2871 break;
2872
2873
2874 case StabSourceFileOptions:
2875 // N_OPT - emitted with gcc2_compiled and in gcc source
2876 type = eSymbolTypeCompiler;
2877 break;
2878
2879 case StabRegisterSymbol:
2880 // N_RSYM - register sym: name,,NO_SECT,type,register
2881 type = eSymbolTypeVariable;
2882 break;
2883
2884 case StabSourceLine:
2885 // N_SLINE - src line: 0,,n_sect,linenumber,address
2886 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2887 type = eSymbolTypeLineEntry;
2888 break;
2889
2890 case StabStructureType:
2891 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2892 type = eSymbolTypeVariableType;
2893 break;
2894
2895 case StabSourceFileName:
2896 // N_SO - source file name
2897 type = eSymbolTypeSourceFile;
2898 if (symbol_name == NULL)
2899 {
2900 if (minimize)
2901 add_nlist = false;
2902 if (N_SO_index != UINT32_MAX)
2903 {
2904 // Set the size of the N_SO to the terminating index of this N_SO
2905 // so that we can always skip the entire N_SO if we need to navigate
2906 // more quickly at the source level when parsing STABS
2907 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2908 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2909 symbol_ptr->SetSizeIsSibling(true);
2910 }
2911 N_NSYM_indexes.clear();
2912 N_INCL_indexes.clear();
2913 N_BRAC_indexes.clear();
2914 N_COMM_indexes.clear();
2915 N_FUN_indexes.clear();
2916 N_SO_index = UINT32_MAX;
2917 }
2918 else
2919 {
2920 // We use the current number of symbols in the symbol table in lieu of
2921 // using nlist_idx in case we ever start trimming entries out
Greg Clayton177b8552012-05-30 20:20:34 +00002922 const bool N_SO_has_full_path = symbol_name[0] == '/';
2923 if (N_SO_has_full_path)
2924 {
2925 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2926 {
2927 // We have two consecutive N_SO entries where the first contains a directory
2928 // and the second contains a full path.
Greg Clayton037520e2012-07-18 23:18:10 +00002929 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton177b8552012-05-30 20:20:34 +00002930 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2931 add_nlist = false;
2932 }
2933 else
2934 {
2935 // This is the first entry in a N_SO that contains a directory or
2936 // a full path to the source file
2937 N_SO_index = sym_idx;
2938 }
2939 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002940 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2941 {
Greg Clayton177b8552012-05-30 20:20:34 +00002942 // This is usually the second N_SO entry that contains just the filename,
2943 // so here we combine it with the first one if we are minimizing the symbol table
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002944 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2945 if (so_path && so_path[0])
2946 {
2947 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00002948 const size_t double_slash_pos = full_so_path.find("//");
2949 if (double_slash_pos != std::string::npos)
2950 {
2951 // The linker has been generating bad N_SO entries with doubled up paths
2952 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2953 // and the second is the directory for the source file so you end up with
2954 // a path that looks like "/tmp/src//tmp/src/"
2955 FileSpec so_dir(so_path, false);
2956 if (!so_dir.Exists())
2957 {
2958 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2959 if (so_dir.Exists())
2960 {
2961 // Trim off the incorrect path
2962 full_so_path.erase(0, double_slash_pos + 1);
2963 }
2964 }
2965 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002966 if (*full_so_path.rbegin() != '/')
2967 full_so_path += '/';
2968 full_so_path += symbol_name;
Greg Clayton037520e2012-07-18 23:18:10 +00002969 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002970 add_nlist = false;
2971 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2972 }
2973 }
Greg Clayton946f8902012-09-05 22:30:51 +00002974 else
2975 {
2976 // This could be a relative path to a N_SO
2977 N_SO_index = sym_idx;
2978 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002979 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002980
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002981 break;
2982
2983 case StabObjectFileName:
2984 // N_OSO - object file name: name,,0,0,st_mtime
2985 type = eSymbolTypeObjectFile;
2986 break;
2987
2988 case StabLocalSymbol:
2989 // N_LSYM - local sym: name,,NO_SECT,type,offset
2990 type = eSymbolTypeLocal;
2991 break;
2992
2993 //----------------------------------------------------------------------
2994 // INCL scopes
2995 //----------------------------------------------------------------------
2996 case StabBeginIncludeFileName:
2997 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2998 // We use the current number of symbols in the symbol table in lieu of
2999 // using nlist_idx in case we ever start trimming entries out
3000 N_INCL_indexes.push_back(sym_idx);
3001 type = eSymbolTypeScopeBegin;
3002 break;
3003
3004 case StabEndIncludeFile:
3005 // N_EINCL - include file end: name,,NO_SECT,0,0
3006 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3007 // so that we can always skip the entire symbol if we need to navigate
3008 // more quickly at the source level when parsing STABS
3009 if ( !N_INCL_indexes.empty() )
3010 {
3011 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3012 symbol_ptr->SetByteSize(sym_idx + 1);
3013 symbol_ptr->SetSizeIsSibling(true);
3014 N_INCL_indexes.pop_back();
3015 }
3016 type = eSymbolTypeScopeEnd;
3017 break;
3018
3019 case StabIncludeFileName:
3020 // N_SOL - #included file name: name,,n_sect,0,address
3021 type = eSymbolTypeHeaderFile;
3022
3023 // We currently don't use the header files on darwin
3024 if (minimize)
3025 add_nlist = false;
3026 break;
3027
Jason Molenda4e7511e2013-03-06 23:19:17 +00003028 case StabCompilerParameters:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003029 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
3030 type = eSymbolTypeCompiler;
3031 break;
3032
3033 case StabCompilerVersion:
3034 // N_VERSION - compiler version: name,,NO_SECT,0,0
3035 type = eSymbolTypeCompiler;
3036 break;
3037
3038 case StabCompilerOptLevel:
3039 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
3040 type = eSymbolTypeCompiler;
3041 break;
3042
3043 case StabParameter:
3044 // N_PSYM - parameter: name,,NO_SECT,type,offset
3045 type = eSymbolTypeVariable;
3046 break;
3047
3048 case StabAlternateEntry:
3049 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
3050 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3051 type = eSymbolTypeLineEntry;
3052 break;
3053
3054 //----------------------------------------------------------------------
3055 // Left and Right Braces
3056 //----------------------------------------------------------------------
3057 case StabLeftBracket:
3058 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
3059 // We use the current number of symbols in the symbol table in lieu of
3060 // using nlist_idx in case we ever start trimming entries out
3061 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3062 N_BRAC_indexes.push_back(sym_idx);
3063 type = eSymbolTypeScopeBegin;
3064 break;
3065
3066 case StabRightBracket:
3067 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
3068 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3069 // so that we can always skip the entire symbol if we need to navigate
3070 // more quickly at the source level when parsing STABS
3071 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3072 if ( !N_BRAC_indexes.empty() )
3073 {
3074 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3075 symbol_ptr->SetByteSize(sym_idx + 1);
3076 symbol_ptr->SetSizeIsSibling(true);
3077 N_BRAC_indexes.pop_back();
3078 }
3079 type = eSymbolTypeScopeEnd;
3080 break;
3081
3082 case StabDeletedIncludeFile:
3083 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3084 type = eSymbolTypeHeaderFile;
3085 break;
3086
3087 //----------------------------------------------------------------------
3088 // COMM scopes
3089 //----------------------------------------------------------------------
3090 case StabBeginCommon:
3091 // N_BCOMM - begin common: name,,NO_SECT,0,0
3092 // We use the current number of symbols in the symbol table in lieu of
3093 // using nlist_idx in case we ever start trimming entries out
3094 type = eSymbolTypeScopeBegin;
3095 N_COMM_indexes.push_back(sym_idx);
3096 break;
3097
3098 case StabEndCommonLocal:
3099 // N_ECOML - end common (local name): 0,,n_sect,0,address
3100 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3101 // Fall through
3102
3103 case StabEndCommon:
3104 // N_ECOMM - end common: name,,n_sect,0,0
3105 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3106 // so that we can always skip the entire symbol if we need to navigate
3107 // more quickly at the source level when parsing STABS
3108 if ( !N_COMM_indexes.empty() )
3109 {
3110 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3111 symbol_ptr->SetByteSize(sym_idx + 1);
3112 symbol_ptr->SetSizeIsSibling(true);
3113 N_COMM_indexes.pop_back();
3114 }
3115 type = eSymbolTypeScopeEnd;
3116 break;
3117
3118 case StabLength:
3119 // N_LENG - second stab entry with length information
3120 type = eSymbolTypeAdditional;
3121 break;
3122
3123 default: break;
3124 }
3125 }
3126 else
3127 {
3128 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3129 uint8_t n_type = NlistMaskType & nlist.n_type;
3130 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3131
3132 switch (n_type)
3133 {
3134 case NListTypeIndirect: // N_INDR - Fall through
3135 case NListTypePreboundUndefined:// N_PBUD - Fall through
3136 case NListTypeUndefined: // N_UNDF
3137 type = eSymbolTypeUndefined;
3138 break;
3139
3140 case NListTypeAbsolute: // N_ABS
3141 type = eSymbolTypeAbsolute;
3142 break;
3143
3144 case NListTypeSection: // N_SECT
3145 {
3146 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3147
Sean Callanan9a028512012-08-09 00:50:26 +00003148 if (!symbol_section)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003149 {
3150 // TODO: warn about this?
3151 add_nlist = false;
3152 break;
3153 }
3154
3155 if (TEXT_eh_frame_sectID == nlist.n_sect)
3156 {
3157 type = eSymbolTypeException;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003158 }
3159 else
3160 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003161 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3162
3163 switch (section_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003164 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003165 case SectionTypeRegular: break; // regular section
3166 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3167 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3168 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3169 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3170 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3171 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3172 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3173 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3174 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3175 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3176 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3177 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3178 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3179 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3180 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3181 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3182 default: break;
Greg Clayton456809c2011-12-03 02:30:59 +00003183 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003184
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003185 if (type == eSymbolTypeInvalid)
Greg Clayton456809c2011-12-03 02:30:59 +00003186 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003187 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3188 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton928d8292010-09-08 16:38:06 +00003189 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00003190 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3191 SectionAttrUserSelfModifyingCode |
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003192 SectionAttrSytemSomeInstructions))
3193 type = eSymbolTypeData;
3194 else
3195 type = eSymbolTypeCode;
Greg Clayton928d8292010-09-08 16:38:06 +00003196 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003197 else
3198 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton928d8292010-09-08 16:38:06 +00003199 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003200 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00003201 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003202 type = eSymbolTypeRuntime;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003203
Jason Molenda4e7511e2013-03-06 23:19:17 +00003204 if (symbol_name &&
3205 symbol_name[0] == '_' &&
3206 symbol_name[1] == 'O' &&
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003207 symbol_name[2] == 'B')
Greg Clayton0c38b0d2010-09-12 05:25:16 +00003208 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003209 llvm::StringRef symbol_name_ref(symbol_name);
3210 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3211 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3212 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3213 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003214 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003215 symbol_name_non_abi_mangled = symbol_name + 1;
3216 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3217 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003218 demangled_is_synthesized = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003219 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003220 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003221 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003222 symbol_name_non_abi_mangled = symbol_name + 1;
3223 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3224 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003225 demangled_is_synthesized = true;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003226 }
3227 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3228 {
3229 symbol_name_non_abi_mangled = symbol_name + 1;
3230 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3231 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003232 demangled_is_synthesized = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003233 }
3234 }
3235 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003236 else
3237 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3238 {
3239 type = eSymbolTypeException;
3240 }
3241 else
3242 {
3243 type = eSymbolTypeData;
3244 }
3245 }
3246 else
3247 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3248 {
3249 type = eSymbolTypeTrampoline;
3250 }
3251 else
3252 if (symbol_section->IsDescendant(objc_section_sp.get()))
3253 {
3254 type = eSymbolTypeRuntime;
3255 if (symbol_name && symbol_name[0] == '.')
3256 {
3257 llvm::StringRef symbol_name_ref(symbol_name);
3258 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3259 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3260 {
3261 symbol_name_non_abi_mangled = symbol_name;
3262 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3263 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003264 demangled_is_synthesized = true;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003265 }
3266 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003267 }
3268 }
3269 }
3270 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003271 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003272 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003273 }
3274
3275 if (add_nlist)
3276 {
3277 uint64_t symbol_value = nlist.n_value;
3278 bool symbol_name_is_mangled = false;
3279
3280 if (symbol_name_non_abi_mangled)
3281 {
Greg Clayton037520e2012-07-18 23:18:10 +00003282 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3283 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003284 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003285 else
3286 {
3287 if (symbol_name && symbol_name[0] == '_')
3288 {
3289 symbol_name_is_mangled = symbol_name[1] == '_';
3290 symbol_name++; // Skip the leading underscore
3291 }
3292
3293 if (symbol_name)
3294 {
Greg Claytondacc4a92013-05-14 22:19:37 +00003295 ConstString const_symbol_name(symbol_name);
3296 if (is_gsym)
3297 N_GSYM_name_to_sym_idx[const_symbol_name.GetCString()] = sym_idx;
3298 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003299 }
3300 }
3301 if (symbol_section)
3302 {
3303 const addr_t section_file_addr = symbol_section->GetFileAddress();
3304 if (symbol_byte_size == 0 && function_starts_count > 0)
3305 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003306 addr_t symbol_lookup_file_addr = nlist.n_value;
3307 // Do an exact address match for non-ARM addresses, else get the closest since
3308 // the symbol might be a thumb symbol which has an address with bit zero set
3309 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3310 if (is_arm && func_start_entry)
3311 {
3312 // Verify that the function start address is the symbol address (ARM)
3313 // or the symbol address + 1 (thumb)
3314 if (func_start_entry->addr != symbol_lookup_file_addr &&
3315 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3316 {
3317 // Not the right entry, NULL it out...
3318 func_start_entry = NULL;
3319 }
3320 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003321 if (func_start_entry)
3322 {
3323 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003324
Greg Clayton29e08cb2012-03-14 01:53:24 +00003325 addr_t symbol_file_addr = func_start_entry->addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003326 if (is_arm)
Greg Clayton29e08cb2012-03-14 01:53:24 +00003327 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003328
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003329 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3330 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3331 if (next_func_start_entry)
3332 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003333 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3334 // Be sure the clear the Thumb address bit when we calculate the size
3335 // from the current and next address
3336 if (is_arm)
3337 next_symbol_file_addr &= 0xfffffffffffffffeull;
3338 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 +00003339 }
3340 else
3341 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003342 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003343 }
3344 }
3345 }
3346 symbol_value -= section_file_addr;
3347 }
3348
Greg Claytondacc4a92013-05-14 22:19:37 +00003349 if (is_debug == false)
3350 {
3351 if (type == eSymbolTypeCode)
3352 {
3353 // See if we can find a N_FUN entry for any code symbols.
3354 // If we do find a match, and the name matches, then we
3355 // can merge the two into just the function symbol to avoid
3356 // duplicate entries in the symbol table
3357 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3358 if (pos != N_FUN_addr_to_sym_idx.end())
3359 {
3360 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3361 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3362 {
3363 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3364 // We just need the flags from the linker symbol, so put these flags
3365 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3366 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3367 sym[sym_idx].Clear();
3368 continue;
3369 }
3370 }
3371 }
3372 else if (type == eSymbolTypeData)
3373 {
3374 // See if we can find a N_STSYM entry for any data symbols.
3375 // If we do find a match, and the name matches, then we
3376 // can merge the two into just the Static symbol to avoid
3377 // duplicate entries in the symbol table
3378 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3379 if (pos != N_STSYM_addr_to_sym_idx.end())
3380 {
3381 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3382 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3383 {
3384 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3385 // We just need the flags from the linker symbol, so put these flags
3386 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3387 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3388 sym[sym_idx].Clear();
3389 continue;
3390 }
3391 }
3392 else
3393 {
3394 // Combine N_GSYM stab entries with the non stab symbol
3395 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetMangledName().GetCString());
3396 if (pos != N_GSYM_name_to_sym_idx.end())
3397 {
3398 const uint32_t GSYM_sym_idx = pos->second;
3399 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3400 // Copy the address, because often the N_GSYM address has an invalid address of zero
3401 // when the global is a common symbol
3402 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3403 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3404 // We just need the flags from the linker symbol, so put these flags
3405 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3406 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3407 sym[sym_idx].Clear();
3408 continue;
3409 }
3410 }
3411 }
3412 }
3413
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003414 sym[sym_idx].SetID (nlist_idx);
3415 sym[sym_idx].SetType (type);
3416 sym[sym_idx].GetAddress().SetSection (symbol_section);
3417 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3418 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3419
3420 if (symbol_byte_size > 0)
3421 sym[sym_idx].SetByteSize(symbol_byte_size);
3422
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003423 if (demangled_is_synthesized)
3424 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3425
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003426 ++sym_idx;
3427 }
3428 else
3429 {
3430 sym[sym_idx].Clear();
3431 }
3432
3433 }
3434
3435 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3436 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3437 // such entries by figuring out what the address for the global is by looking up this non-STAB
3438 // entry and copying the value into the debug symbol's value to save us the hassle in the
3439 // debug symbol parser.
3440
3441 Symbol *global_symbol = NULL;
3442 for (nlist_idx = 0;
3443 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3444 nlist_idx++)
3445 {
3446 if (global_symbol->GetAddress().GetFileAddress() == 0)
3447 {
3448 std::vector<uint32_t> indexes;
3449 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3450 {
3451 std::vector<uint32_t>::const_iterator pos;
3452 std::vector<uint32_t>::const_iterator end = indexes.end();
3453 for (pos = indexes.begin(); pos != end; ++pos)
3454 {
3455 symbol_ptr = symtab->SymbolAtIndex(*pos);
3456 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3457 {
3458 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3459 break;
3460 }
3461 }
3462 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003463 }
3464 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003465
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003466 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3467
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003468 if (function_starts_count > 0)
3469 {
3470 char synthetic_function_symbol[PATH_MAX];
3471 uint32_t num_synthetic_function_symbols = 0;
3472 for (i=0; i<function_starts_count; ++i)
3473 {
3474 if (function_starts.GetEntryRef (i).data == false)
3475 ++num_synthetic_function_symbols;
3476 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003477
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003478 if (num_synthetic_function_symbols > 0)
3479 {
3480 if (num_syms < sym_idx + num_synthetic_function_symbols)
3481 {
3482 num_syms = sym_idx + num_synthetic_function_symbols;
3483 sym = symtab->Resize (num_syms);
3484 }
3485 uint32_t synthetic_function_symbol_idx = 0;
3486 for (i=0; i<function_starts_count; ++i)
3487 {
3488 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3489 if (func_start_entry->data == false)
3490 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003491 addr_t symbol_file_addr = func_start_entry->addr;
3492 uint32_t symbol_flags = 0;
3493 if (is_arm)
3494 {
3495 if (symbol_file_addr & 1)
3496 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3497 symbol_file_addr &= 0xfffffffffffffffeull;
3498 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003499 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003500 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003501 {
3502 SectionSP symbol_section (symbol_addr.GetSection());
3503 uint32_t symbol_byte_size = 0;
3504 if (symbol_section)
3505 {
3506 const addr_t section_file_addr = symbol_section->GetFileAddress();
3507 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3508 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3509 if (next_func_start_entry)
3510 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003511 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3512 if (is_arm)
3513 next_symbol_file_addr &= 0xfffffffffffffffeull;
3514 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 +00003515 }
3516 else
3517 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003518 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003519 }
3520 snprintf (synthetic_function_symbol,
3521 sizeof(synthetic_function_symbol),
3522 "___lldb_unnamed_function%u$$%s",
3523 ++synthetic_function_symbol_idx,
3524 module_sp->GetFileSpec().GetFilename().GetCString());
3525 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Clayton037520e2012-07-18 23:18:10 +00003526 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003527 sym[sym_idx].SetType (eSymbolTypeCode);
3528 sym[sym_idx].SetIsSynthetic (true);
3529 sym[sym_idx].GetAddress() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003530 if (symbol_flags)
3531 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003532 if (symbol_byte_size)
3533 sym[sym_idx].SetByteSize (symbol_byte_size);
3534 ++sym_idx;
3535 }
3536 }
3537 }
3538 }
3539 }
3540 }
3541
3542 // Trim our symbols down to just what we ended up with after
3543 // removing any symbols.
3544 if (sym_idx < num_syms)
3545 {
3546 num_syms = sym_idx;
3547 sym = symtab->Resize (num_syms);
3548 }
3549
3550 // Now synthesize indirect symbols
3551 if (m_dysymtab.nindirectsyms != 0)
3552 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003553 if (indirect_symbol_index_data.GetByteSize())
3554 {
3555 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3556
3557 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3558 {
3559 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3560 {
3561 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3562 if (symbol_stub_byte_size == 0)
3563 continue;
3564
3565 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3566
3567 if (num_symbol_stubs == 0)
3568 continue;
3569
3570 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3571 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3572 {
3573 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3574 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 +00003575 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003576 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3577 {
3578 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3579 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3580 continue;
3581
3582 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3583 Symbol *stub_symbol = NULL;
3584 if (index_pos != end_index_pos)
3585 {
3586 // We have a remapping from the original nlist index to
3587 // a current symbol index, so just look this up by index
3588 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3589 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003590 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003591 {
3592 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00003593 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003594 // S_SYMBOL_STUBS
3595 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3596 }
3597
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003598 if (stub_symbol)
3599 {
3600 Address so_addr(symbol_stub_addr, section_list);
3601
3602 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3603 {
3604 // Change the external symbol into a trampoline that makes sense
3605 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3606 // can re-use them so we don't have to make up a synthetic symbol
3607 // for no good reason.
3608 stub_symbol->SetType (eSymbolTypeTrampoline);
3609 stub_symbol->SetExternal (false);
3610 stub_symbol->GetAddress() = so_addr;
3611 stub_symbol->SetByteSize (symbol_stub_byte_size);
3612 }
3613 else
3614 {
3615 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00003616 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003617 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00003618 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003619 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00003620 stub_symbol = NULL; // this pointer no longer valid
3621 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003622 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00003623 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003624 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3625 sym[sym_idx].SetIsSynthetic (true);
3626 sym[sym_idx].GetAddress() = so_addr;
3627 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3628 ++sym_idx;
3629 }
3630 }
Greg Clayton3f839a32012-09-05 01:38:55 +00003631 else
3632 {
3633 if (log)
3634 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3635 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003636 }
3637 }
3638 }
3639 }
3640 }
3641 }
3642 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003643 }
3644 return 0;
3645}
3646
3647
3648void
3649ObjectFileMachO::Dump (Stream *s)
3650{
Greg Claytona1743492012-03-13 23:14:29 +00003651 ModuleSP module_sp(GetModule());
3652 if (module_sp)
3653 {
3654 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3655 s->Printf("%p: ", this);
3656 s->Indent();
3657 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3658 s->PutCString("ObjectFileMachO64");
3659 else
3660 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003661
Greg Claytona1743492012-03-13 23:14:29 +00003662 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003663
Greg Claytona1743492012-03-13 23:14:29 +00003664 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003665
Greg Claytona1743492012-03-13 23:14:29 +00003666 if (m_sections_ap.get())
3667 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003668
Greg Claytona1743492012-03-13 23:14:29 +00003669 if (m_symtab_ap.get())
3670 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3671 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003672}
3673
Greg Claytonf4d6de62013-04-24 22:29:28 +00003674bool
3675ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3676 const lldb_private::DataExtractor &data,
3677 lldb::offset_t lc_offset,
3678 lldb_private::UUID& uuid)
3679{
3680 uint32_t i;
3681 struct uuid_command load_cmd;
3682
3683 lldb::offset_t offset = lc_offset;
3684 for (i=0; i<header.ncmds; ++i)
3685 {
3686 const lldb::offset_t cmd_offset = offset;
3687 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3688 break;
3689
3690 if (load_cmd.cmd == LoadCommandUUID)
3691 {
3692 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3693
3694 if (uuid_bytes)
3695 {
3696 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3697 // We pretend these object files have no UUID to prevent crashing.
3698
3699 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3700 0x3b, 0xa8,
3701 0x4b, 0x16,
3702 0xb6, 0xa4,
3703 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3704
3705 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3706 return false;
3707
3708 uuid.SetBytes (uuid_bytes);
3709 return true;
3710 }
3711 return false;
3712 }
3713 offset = cmd_offset + load_cmd.cmdsize;
3714 }
3715 return false;
3716}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003717
3718bool
Greg Clayton60830262011-02-04 18:53:10 +00003719ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003720{
Greg Claytona1743492012-03-13 23:14:29 +00003721 ModuleSP module_sp(GetModule());
3722 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003723 {
Greg Claytona1743492012-03-13 23:14:29 +00003724 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00003725 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00003726 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003727 }
3728 return false;
3729}
3730
3731
3732uint32_t
3733ObjectFileMachO::GetDependentModules (FileSpecList& files)
3734{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003735 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00003736 ModuleSP module_sp(GetModule());
3737 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003738 {
Greg Claytona1743492012-03-13 23:14:29 +00003739 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3740 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00003741 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003742 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3743 uint32_t i;
3744 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003745 {
Greg Claytona1743492012-03-13 23:14:29 +00003746 const uint32_t cmd_offset = offset;
3747 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3748 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003749
Greg Claytona1743492012-03-13 23:14:29 +00003750 switch (load_cmd.cmd)
3751 {
3752 case LoadCommandDylibLoad:
3753 case LoadCommandDylibLoadWeak:
3754 case LoadCommandDylibReexport:
3755 case LoadCommandDynamicLinkerLoad:
3756 case LoadCommandFixedVMShlibLoad:
3757 case LoadCommandDylibLoadUpward:
3758 {
3759 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3760 const char *path = m_data.PeekCStr(name_offset);
3761 // Skip any path that starts with '@' since these are usually:
3762 // @executable_path/.../file
3763 // @rpath/.../file
3764 if (path && path[0] != '@')
3765 {
3766 FileSpec file_spec(path, resolve_path);
3767 if (files.AppendIfUnique(file_spec))
3768 count++;
3769 }
3770 }
3771 break;
3772
3773 default:
3774 break;
3775 }
3776 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003777 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003778 }
3779 return count;
3780}
3781
Jim Ingham672e6f52011-03-07 23:44:08 +00003782lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00003783ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00003784{
3785 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3786 // is initialized to an invalid address, so we can just return that.
3787 // 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 +00003788
Jim Ingham672e6f52011-03-07 23:44:08 +00003789 if (!IsExecutable() || m_entry_point_address.IsValid())
3790 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003791
3792 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00003793 // /usr/include/mach-o.h, but it is basically:
3794 //
3795 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3796 // uint32_t count - this is the count of longs in the thread state data
3797 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3798 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00003799 //
Jim Ingham672e6f52011-03-07 23:44:08 +00003800 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3801 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3802 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3803 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3804 //
3805 // For now we hard-code the offsets and flavors we need:
3806 //
3807 //
3808
Greg Claytona1743492012-03-13 23:14:29 +00003809 ModuleSP module_sp(GetModule());
3810 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00003811 {
Greg Claytona1743492012-03-13 23:14:29 +00003812 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3813 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00003814 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003815 uint32_t i;
3816 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3817 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003818
Greg Claytona1743492012-03-13 23:14:29 +00003819 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00003820 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003821 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00003822 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3823 break;
3824
3825 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00003826 {
Greg Claytona1743492012-03-13 23:14:29 +00003827 case LoadCommandUnixThread:
3828 case LoadCommandThread:
Jim Ingham672e6f52011-03-07 23:44:08 +00003829 {
Greg Claytona1743492012-03-13 23:14:29 +00003830 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00003831 {
Greg Claytona1743492012-03-13 23:14:29 +00003832 uint32_t flavor = m_data.GetU32(&offset);
3833 uint32_t count = m_data.GetU32(&offset);
3834 if (count == 0)
3835 {
3836 // We've gotten off somehow, log and exit;
3837 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00003838 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003839
Greg Claytona1743492012-03-13 23:14:29 +00003840 switch (m_header.cputype)
3841 {
3842 case llvm::MachO::CPUTypeARM:
3843 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3844 {
3845 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3846 start_address = m_data.GetU32(&offset);
3847 done = true;
3848 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003849 break;
Greg Claytona1743492012-03-13 23:14:29 +00003850 case llvm::MachO::CPUTypeI386:
3851 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3852 {
3853 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3854 start_address = m_data.GetU32(&offset);
3855 done = true;
3856 }
3857 break;
3858 case llvm::MachO::CPUTypeX86_64:
3859 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3860 {
3861 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3862 start_address = m_data.GetU64(&offset);
3863 done = true;
3864 }
3865 break;
3866 default:
3867 return m_entry_point_address;
3868 }
3869 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3870 if (done)
3871 break;
3872 offset += count * 4;
3873 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003874 }
Greg Claytona1743492012-03-13 23:14:29 +00003875 break;
3876 case LoadCommandMain:
Sean Callanan226b70c2012-03-08 02:39:03 +00003877 {
Greg Claytona1743492012-03-13 23:14:29 +00003878 ConstString text_segment_name ("__TEXT");
3879 uint64_t entryoffset = m_data.GetU64(&offset);
3880 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3881 if (text_segment_sp)
3882 {
3883 done = true;
3884 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3885 }
Sean Callanan226b70c2012-03-08 02:39:03 +00003886 }
Greg Claytona1743492012-03-13 23:14:29 +00003887
3888 default:
3889 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00003890 }
Greg Claytona1743492012-03-13 23:14:29 +00003891 if (done)
3892 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00003893
Greg Claytona1743492012-03-13 23:14:29 +00003894 // Go to the next load command:
3895 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00003896 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003897
Greg Claytona1743492012-03-13 23:14:29 +00003898 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00003899 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00003900 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00003901 // of this ObjectFile:
3902 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00003903 {
Greg Claytona1743492012-03-13 23:14:29 +00003904 m_entry_point_address.Clear();
3905 }
3906 }
3907 else
3908 {
3909 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3910 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003911
Greg Claytona1743492012-03-13 23:14:29 +00003912 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00003913
Greg Claytona1743492012-03-13 23:14:29 +00003914 if (module_sp)
3915 {
3916 SymbolContextList contexts;
3917 SymbolContext context;
3918 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3919 {
3920 if (contexts.GetContextAtIndex(0, context))
3921 m_entry_point_address = context.symbol->GetAddress();
3922 }
Greg Claytone72dfb32012-02-24 01:59:29 +00003923 }
3924 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003925 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003926
Jim Ingham672e6f52011-03-07 23:44:08 +00003927 return m_entry_point_address;
3928
3929}
3930
Greg Claytonc9660542012-02-05 02:38:54 +00003931lldb_private::Address
3932ObjectFileMachO::GetHeaderAddress ()
3933{
3934 lldb_private::Address header_addr;
3935 SectionList *section_list = GetSectionList();
3936 if (section_list)
3937 {
3938 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3939 if (text_segment_sp)
3940 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003941 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00003942 header_addr.SetOffset (0);
3943 }
3944 }
3945 return header_addr;
3946}
3947
Greg Claytonc3776bf2012-02-09 06:16:32 +00003948uint32_t
3949ObjectFileMachO::GetNumThreadContexts ()
3950{
Greg Claytona1743492012-03-13 23:14:29 +00003951 ModuleSP module_sp(GetModule());
3952 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00003953 {
Greg Claytona1743492012-03-13 23:14:29 +00003954 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3955 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00003956 {
Greg Claytona1743492012-03-13 23:14:29 +00003957 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00003958 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003959 FileRangeArray::Entry file_range;
3960 thread_command thread_cmd;
3961 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00003962 {
Greg Claytona1743492012-03-13 23:14:29 +00003963 const uint32_t cmd_offset = offset;
3964 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3965 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003966
Greg Claytona1743492012-03-13 23:14:29 +00003967 if (thread_cmd.cmd == LoadCommandThread)
3968 {
3969 file_range.SetRangeBase (offset);
3970 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3971 m_thread_context_offsets.Append (file_range);
3972 }
3973 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00003974 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00003975 }
3976 }
3977 return m_thread_context_offsets.GetSize();
3978}
3979
3980lldb::RegisterContextSP
3981ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3982{
Greg Claytonc3776bf2012-02-09 06:16:32 +00003983 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00003984
Greg Claytona1743492012-03-13 23:14:29 +00003985 ModuleSP module_sp(GetModule());
3986 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00003987 {
Greg Claytona1743492012-03-13 23:14:29 +00003988 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3989 if (!m_thread_context_offsets_valid)
3990 GetNumThreadContexts ();
3991
3992 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00003993 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00003994 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00003995
3996 DataExtractor data (m_data,
3997 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00003998 thread_context_file_range->GetByteSize());
3999
4000 switch (m_header.cputype)
4001 {
4002 case llvm::MachO::CPUTypeARM:
4003 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
4004 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004005
Jim Ingham28eb5712012-10-12 17:34:26 +00004006 case llvm::MachO::CPUTypeI386:
4007 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
4008 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004009
Jim Ingham28eb5712012-10-12 17:34:26 +00004010 case llvm::MachO::CPUTypeX86_64:
4011 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
4012 break;
4013 }
Greg Claytona1743492012-03-13 23:14:29 +00004014 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004015 }
4016 return reg_ctx_sp;
4017}
4018
Greg Claytonc9660542012-02-05 02:38:54 +00004019
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004020ObjectFile::Type
4021ObjectFileMachO::CalculateType()
4022{
4023 switch (m_header.filetype)
4024 {
4025 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4026 if (GetAddressByteSize () == 4)
4027 {
4028 // 32 bit kexts are just object files, but they do have a valid
4029 // UUID load command.
4030 UUID uuid;
4031 if (GetUUID(&uuid))
4032 {
4033 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004034 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004035 // "OSKextGetCurrentIdentifier" as this is required of kexts
4036 if (m_strata == eStrataInvalid)
4037 m_strata = eStrataKernel;
4038 return eTypeSharedLibrary;
4039 }
4040 }
4041 return eTypeObjectFile;
4042
4043 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
4044 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
4045 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
4046 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
4047 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
4048 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
4049 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
4050 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
4051 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
4052 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
4053 default:
4054 break;
4055 }
4056 return eTypeUnknown;
4057}
4058
4059ObjectFile::Strata
4060ObjectFileMachO::CalculateStrata()
4061{
4062 switch (m_header.filetype)
4063 {
4064 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4065 {
4066 // 32 bit kexts are just object files, but they do have a valid
4067 // UUID load command.
4068 UUID uuid;
4069 if (GetUUID(&uuid))
4070 {
4071 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004072 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004073 // "OSKextGetCurrentIdentifier" as this is required of kexts
4074 if (m_type == eTypeInvalid)
4075 m_type = eTypeSharedLibrary;
4076
4077 return eStrataKernel;
4078 }
4079 }
4080 return eStrataUnknown;
4081
4082 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
4083 // Check for the MH_DYLDLINK bit in the flags
4084 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callanan49bce8e2012-02-10 20:22:35 +00004085 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004086 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00004087 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004088 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00004089 {
4090 SectionList *section_list = GetSectionList();
4091 if (section_list)
4092 {
4093 static ConstString g_kld_section_name ("__KLD");
4094 if (section_list->FindSectionByName(g_kld_section_name))
4095 return eStrataKernel;
4096 }
4097 }
4098 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004099
4100 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4101 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callanan49bce8e2012-02-10 20:22:35 +00004102 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004103 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4104 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4105 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4106 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4107 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4108 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4109 default:
4110 break;
4111 }
4112 return eStrataUnknown;
4113}
4114
4115
Greg Claytonc2ff9312012-02-22 19:41:02 +00004116uint32_t
4117ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4118{
Greg Claytona1743492012-03-13 23:14:29 +00004119 ModuleSP module_sp(GetModule());
4120 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004121 {
Greg Claytona1743492012-03-13 23:14:29 +00004122 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4123 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004124 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004125 uint32_t version_cmd = 0;
4126 uint64_t version = 0;
4127 uint32_t i;
4128 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004129 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004130 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004131 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4132 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004133
Greg Claytona1743492012-03-13 23:14:29 +00004134 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004135 {
Greg Claytona1743492012-03-13 23:14:29 +00004136 if (version_cmd == 0)
4137 {
4138 version_cmd = load_cmd.cmd;
4139 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4140 break;
4141 version = load_cmd.dylib.current_version;
4142 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004143 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00004144 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00004145 }
Greg Claytona1743492012-03-13 23:14:29 +00004146 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004147 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004148
Greg Claytona1743492012-03-13 23:14:29 +00004149 if (version_cmd == LoadCommandDylibIdent)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004150 {
Greg Claytona1743492012-03-13 23:14:29 +00004151 if (versions != NULL && num_versions > 0)
4152 {
4153 if (num_versions > 0)
4154 versions[0] = (version & 0xFFFF0000ull) >> 16;
4155 if (num_versions > 1)
4156 versions[1] = (version & 0x0000FF00ull) >> 8;
4157 if (num_versions > 2)
4158 versions[2] = (version & 0x000000FFull);
4159 // Fill in an remaining version numbers with invalid values
4160 for (i=3; i<num_versions; ++i)
4161 versions[i] = UINT32_MAX;
4162 }
4163 // The LC_ID_DYLIB load command has a version with 3 version numbers
4164 // in it, so always return 3
4165 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004166 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00004167 }
4168 return false;
4169}
4170
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004171bool
Greg Clayton514487e2011-02-15 21:59:32 +00004172ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004173{
Greg Claytona1743492012-03-13 23:14:29 +00004174 ModuleSP module_sp(GetModule());
4175 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00004176 {
Greg Claytona1743492012-03-13 23:14:29 +00004177 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4178 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +00004179
Greg Claytona1743492012-03-13 23:14:29 +00004180 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda4e7511e2013-03-06 23:19:17 +00004181 // debugs at the addresses in the file itself. Below we set the OS to
Greg Claytona1743492012-03-13 23:14:29 +00004182 // unknown to make sure we use the DynamicLoaderStatic()...
4183 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4184 {
4185 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4186 }
4187 return true;
Greg Clayton593577a2011-09-21 03:57:31 +00004188 }
Greg Claytona1743492012-03-13 23:14:29 +00004189 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004190}
4191
4192
Jason Molenda0e0954c2013-04-16 06:24:42 +00004193UUID
4194ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4195{
4196 UUID uuid;
4197 if (process)
4198 {
4199 addr_t all_image_infos = process->GetImageInfoAddress();
4200
4201 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4202 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4203 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4204 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4205
4206 Error err;
4207 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4208 if (version_or_magic != -1
4209 && version_or_magic != HeaderMagic32
4210 && version_or_magic != HeaderMagic32Swapped
4211 && version_or_magic != HeaderMagic64
4212 && version_or_magic != HeaderMagic64Swapped
4213 && version_or_magic >= 13)
4214 {
4215 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4216 int wordsize = process->GetAddressByteSize();
4217 if (wordsize == 8)
4218 {
4219 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4220 }
4221 if (wordsize == 4)
4222 {
4223 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4224 }
4225 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4226 {
4227 uuid_t shared_cache_uuid;
4228 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4229 {
4230 uuid.SetBytes (shared_cache_uuid);
4231 }
4232 }
4233 }
4234 }
4235 return uuid;
4236}
4237
4238UUID
4239ObjectFileMachO::GetLLDBSharedCacheUUID ()
4240{
4241 UUID uuid;
4242#if defined (__APPLE__) && defined (__arm__)
4243 uint8_t *(*dyld_get_all_image_infos)(void);
4244 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4245 if (dyld_get_all_image_infos)
4246 {
4247 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4248 if (dyld_all_image_infos_address)
4249 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00004250 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4251 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00004252 {
Jason Molenda42b69fa2013-04-16 22:56:17 +00004253 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 +00004254 uuid.SetBytes (sharedCacheUUID_address);
4255 }
4256 }
4257 }
4258#endif
4259 return uuid;
4260}
4261
4262
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004263//------------------------------------------------------------------
4264// PluginInterface protocol
4265//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00004266lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004267ObjectFileMachO::GetPluginName()
4268{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004269 return GetPluginNameStatic();
4270}
4271
4272uint32_t
4273ObjectFileMachO::GetPluginVersion()
4274{
4275 return 1;
4276}
4277