blob: bbe2aacd280ef5d04587d5f48a6ba25346bd3558 [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 *
Greg Clayton3046e662013-07-10 01:23:25 +0000898ObjectFileMachO::GetSymtab()
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());
Greg Clayton3046e662013-07-10 01:23:25 +0000908 ParseSymtab ();
Greg Claytona1743492012-03-13 23:14:29 +0000909 m_symtab_ap->Finalize ();
910 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911 }
912 return m_symtab_ap.get();
913}
914
Greg Clayton3046e662013-07-10 01:23:25 +0000915bool
916ObjectFileMachO::IsStripped ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000917{
Greg Clayton3046e662013-07-10 01:23:25 +0000918 if (m_dysymtab.cmd == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919 {
Greg Clayton3046e662013-07-10 01:23:25 +0000920 ModuleSP module_sp(GetModule());
921 if (module_sp)
Greg Claytona1743492012-03-13 23:14:29 +0000922 {
Greg Clayton3046e662013-07-10 01:23:25 +0000923 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
924 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton4d78c402012-05-25 18:09:55 +0000925 {
Greg Clayton3046e662013-07-10 01:23:25 +0000926 const lldb::offset_t load_cmd_offset = offset;
927
928 load_command lc;
929 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
930 break;
931 if (lc.cmd == LoadCommandDynamicSymtabInfo)
Greg Clayton4d78c402012-05-25 18:09:55 +0000932 {
Greg Clayton3046e662013-07-10 01:23:25 +0000933 m_dysymtab.cmd = lc.cmd;
934 m_dysymtab.cmdsize = lc.cmdsize;
935 if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
936 {
937 // Clear m_dysymtab if we were unable to read all items from the load command
938 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
939 }
Greg Clayton4d78c402012-05-25 18:09:55 +0000940 }
Greg Clayton3046e662013-07-10 01:23:25 +0000941 offset = load_cmd_offset + lc.cmdsize;
Greg Clayton4d78c402012-05-25 18:09:55 +0000942 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000943 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000944 }
Greg Clayton3046e662013-07-10 01:23:25 +0000945 if (m_dysymtab.cmd)
946 return m_dysymtab.nlocalsym == 0;
947 return false;
948}
Greg Clayton1eac0c72012-04-24 03:06:13 +0000949
Greg Clayton3046e662013-07-10 01:23:25 +0000950void
951ObjectFileMachO::CreateSections (SectionList &unified_section_list)
952{
953 if (!m_sections_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954 {
Greg Clayton3046e662013-07-10 01:23:25 +0000955 m_sections_ap.reset(new SectionList());
956
957 const bool is_dsym = (m_header.filetype == HeaderFileTypeDSYM);
958 lldb::user_id_t segID = 0;
959 lldb::user_id_t sectID = 0;
960 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
961 uint32_t i;
962 const bool is_core = GetType() == eTypeCoreFile;
963 //bool dump_sections = false;
964 ModuleSP module_sp (GetModule());
965 // First look up any LC_ENCRYPTION_INFO load commands
966 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
967 EncryptedFileRanges encrypted_file_ranges;
968 encryption_info_command encryption_cmd;
969 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000970 {
Greg Clayton3046e662013-07-10 01:23:25 +0000971 const lldb::offset_t load_cmd_offset = offset;
972 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
973 break;
974
975 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000976 {
Greg Clayton3046e662013-07-10 01:23:25 +0000977 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000978 {
Greg Clayton3046e662013-07-10 01:23:25 +0000979 if (encryption_cmd.cryptid != 0)
Greg Claytond37d6922013-04-16 16:51:19 +0000980 {
Greg Clayton3046e662013-07-10 01:23:25 +0000981 EncryptedFileRanges::Entry entry;
982 entry.SetRangeBase(encryption_cmd.cryptoff);
983 entry.SetByteSize(encryption_cmd.cryptsize);
984 encrypted_file_ranges.Append(entry);
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000985 }
986 }
Greg Clayton3046e662013-07-10 01:23:25 +0000987 }
988 offset = load_cmd_offset + encryption_cmd.cmdsize;
989 }
990
991 offset = MachHeaderSizeFromMagic(m_header.magic);
992
993 struct segment_command_64 load_cmd;
994 for (i=0; i<m_header.ncmds; ++i)
995 {
996 const lldb::offset_t load_cmd_offset = offset;
997 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
998 break;
999
1000 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
1001 {
1002 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003 {
Greg Clayton3046e662013-07-10 01:23:25 +00001004 bool add_section = true;
1005 bool add_to_unified = true;
1006 ConstString const_segname (load_cmd.segname, std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
Jason Molenda4e7511e2013-03-06 23:19:17 +00001007
Greg Clayton3046e662013-07-10 01:23:25 +00001008 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1009 if (is_dsym && unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010 {
Greg Clayton3046e662013-07-10 01:23:25 +00001011 if (const_segname == GetSegmentNameLINKEDIT())
1012 {
1013 // We need to keep the __LINKEDIT segment private to this object file only
1014 add_to_unified = false;
1015 }
1016 else
1017 {
1018 // This is the dSYM file and this section has already been created by
1019 // the object file, no need to create it.
1020 add_section = false;
1021 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022 }
Greg Clayton3046e662013-07-10 01:23:25 +00001023 load_cmd.vmaddr = m_data.GetAddress(&offset);
1024 load_cmd.vmsize = m_data.GetAddress(&offset);
1025 load_cmd.fileoff = m_data.GetAddress(&offset);
1026 load_cmd.filesize = m_data.GetAddress(&offset);
1027 if (m_length != 0 && load_cmd.filesize != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028 {
Greg Clayton3046e662013-07-10 01:23:25 +00001029 if (load_cmd.fileoff > m_length)
1030 {
1031 // We have a load command that says it extends past the end of hte file. This is likely
1032 // a corrupt file. We don't have any way to return an error condition here (this method
1033 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1034 // is null out the SectionList vector and if a process has been set up, dump a message
1035 // to stdout. The most common case here is core file debugging with a truncated file.
1036 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
1037 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 ")",
1038 i,
1039 lc_segment_name,
1040 load_cmd.fileoff,
1041 m_length);
1042
1043 load_cmd.fileoff = 0;
1044 load_cmd.filesize = 0;
1045 }
1046
1047 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1048 {
1049 // We have a load command that says it extends past the end of hte file. This is likely
1050 // a corrupt file. We don't have any way to return an error condition here (this method
1051 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1052 // is null out the SectionList vector and if a process has been set up, dump a message
1053 // to stdout. The most common case here is core file debugging with a truncated file.
1054 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
1055 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",
1056 i,
1057 lc_segment_name,
1058 load_cmd.fileoff + load_cmd.filesize,
1059 m_length);
1060
1061 // Tuncase the length
1062 load_cmd.filesize = m_length - load_cmd.fileoff;
1063 }
1064 }
1065 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1066 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001067
Greg Clayton3046e662013-07-10 01:23:25 +00001068 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001069
Greg Clayton3046e662013-07-10 01:23:25 +00001070 // Keep a list of mach segments around in case we need to
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001071 // get at data that isn't stored in the abstracted Sections.
Greg Clayton3046e662013-07-10 01:23:25 +00001072 m_mach_segments.push_back (load_cmd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073
Greg Clayton3046e662013-07-10 01:23:25 +00001074 // Use a segment ID of the segment index shifted left by 8 so they
1075 // never conflict with any of the sections.
1076 SectionSP segment_sp;
1077 if (add_section && (const_segname || is_core))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001078 {
Greg Clayton3046e662013-07-10 01:23:25 +00001079 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
1080 this, // Object file to which this sections belongs
1081 ++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
1082 const_segname, // Name of this section
1083 eSectionTypeContainer, // This section is a container of other sections.
1084 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1085 load_cmd.vmsize, // VM size in bytes of this section
1086 load_cmd.fileoff, // Offset to the data for this section in the file
1087 load_cmd.filesize, // Size in bytes of this section as found in the the file
1088 load_cmd.flags)); // Flags for this section
Greg Clayton8d38ac42010-06-28 23:51:11 +00001089
Greg Clayton3046e662013-07-10 01:23:25 +00001090 segment_sp->SetIsEncrypted (segment_is_encrypted);
1091 m_sections_ap->AddSection(segment_sp);
1092 if (add_to_unified)
1093 unified_section_list.AddSection(segment_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094 }
Greg Clayton3046e662013-07-10 01:23:25 +00001095 else if (unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096 {
Greg Clayton3046e662013-07-10 01:23:25 +00001097 m_sections_ap->AddSection(unified_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001098 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099
Greg Clayton3046e662013-07-10 01:23:25 +00001100 struct section_64 sect64;
1101 ::memset (&sect64, 0, sizeof(sect64));
1102 // Push a section into our mach sections for the section at
1103 // index zero (NListSectionNoSection) if we don't have any
1104 // mach sections yet...
1105 if (m_mach_sections.empty())
1106 m_mach_sections.push_back(sect64);
1107 uint32_t segment_sect_idx;
1108 const lldb::user_id_t first_segment_sectID = sectID + 1;
1109
1110
1111 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
1112 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001113 {
Greg Clayton3046e662013-07-10 01:23:25 +00001114 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
Greg Clayton89411422010-10-08 00:21:05 +00001115 break;
Greg Clayton3046e662013-07-10 01:23:25 +00001116 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1117 break;
1118 sect64.addr = m_data.GetAddress(&offset);
1119 sect64.size = m_data.GetAddress(&offset);
1120
1121 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1122 break;
1123
1124 // Keep a list of mach sections around in case we need to
1125 // get at data that isn't stored in the abstracted Sections.
1126 m_mach_sections.push_back (sect64);
1127
1128 if (add_section)
1129 {
1130 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1131 if (!const_segname)
1132 {
1133 // We have a segment with no name so we need to conjure up
1134 // segments that correspond to the section's segname if there
1135 // isn't already such a section. If there is such a section,
1136 // we resize the section so that it spans all sections.
1137 // We also mark these sections as fake so address matches don't
1138 // hit if they land in the gaps between the child sections.
1139 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1140 segment_sp = unified_section_list.FindSectionByName (const_segname);
1141 if (segment_sp.get())
1142 {
1143 Section *segment = segment_sp.get();
1144 // Grow the section size as needed.
1145 const lldb::addr_t sect64_min_addr = sect64.addr;
1146 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1147 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1148 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1149 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1150 if (sect64_min_addr >= curr_seg_min_addr)
1151 {
1152 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1153 // Only grow the section size if needed
1154 if (new_seg_byte_size > curr_seg_byte_size)
1155 segment->SetByteSize (new_seg_byte_size);
1156 }
1157 else
1158 {
1159 // We need to change the base address of the segment and
1160 // adjust the child section offsets for all existing children.
1161 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1162 segment->Slide(slide_amount, false);
1163 segment->GetChildren().Slide(-slide_amount, false);
1164 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1165 }
1166
1167 // Grow the section size as needed.
1168 if (sect64.offset)
1169 {
1170 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1171 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1172
1173 const lldb::addr_t section_min_file_offset = sect64.offset;
1174 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1175 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1176 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1177 segment->SetFileOffset (new_file_offset);
1178 segment->SetFileSize (new_file_size);
1179 }
1180 }
1181 else
1182 {
1183 // Create a fake section for the section's named segment
1184 segment_sp.reset(new Section (segment_sp, // Parent section
1185 module_sp, // Module to which this section belongs
1186 this, // Object file to which this section belongs
1187 ++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
1188 const_segname, // Name of this section
1189 eSectionTypeContainer, // This section is a container of other sections.
1190 sect64.addr, // File VM address == addresses as they are found in the object file
1191 sect64.size, // VM size in bytes of this section
1192 sect64.offset, // Offset to the data for this section in the file
1193 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1194 load_cmd.flags)); // Flags for this section
1195 segment_sp->SetIsFake(true);
1196
1197 m_sections_ap->AddSection(segment_sp);
1198 if (add_to_unified)
1199 unified_section_list.AddSection(segment_sp);
1200 segment_sp->SetIsEncrypted (segment_is_encrypted);
1201 }
1202 }
1203 assert (segment_sp.get());
1204
1205 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
1206 static ConstString g_sect_name_objc_data ("__objc_data");
1207 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1208 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1209 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1210 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1211 static ConstString g_sect_name_objc_const ("__objc_const");
1212 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1213 static ConstString g_sect_name_cfstring ("__cfstring");
1214
1215 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1216 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1217 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1218 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1219 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1220 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1221 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1222 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1223 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1224 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1225 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1226 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1227 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1228 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1229 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1230 static ConstString g_sect_name_eh_frame ("__eh_frame");
1231 static ConstString g_sect_name_DATA ("__DATA");
1232 static ConstString g_sect_name_TEXT ("__TEXT");
1233
1234 SectionType sect_type = eSectionTypeOther;
1235
1236 if (section_name == g_sect_name_dwarf_debug_abbrev)
1237 sect_type = eSectionTypeDWARFDebugAbbrev;
1238 else if (section_name == g_sect_name_dwarf_debug_aranges)
1239 sect_type = eSectionTypeDWARFDebugAranges;
1240 else if (section_name == g_sect_name_dwarf_debug_frame)
1241 sect_type = eSectionTypeDWARFDebugFrame;
1242 else if (section_name == g_sect_name_dwarf_debug_info)
1243 sect_type = eSectionTypeDWARFDebugInfo;
1244 else if (section_name == g_sect_name_dwarf_debug_line)
1245 sect_type = eSectionTypeDWARFDebugLine;
1246 else if (section_name == g_sect_name_dwarf_debug_loc)
1247 sect_type = eSectionTypeDWARFDebugLoc;
1248 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1249 sect_type = eSectionTypeDWARFDebugMacInfo;
1250 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1251 sect_type = eSectionTypeDWARFDebugPubNames;
1252 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1253 sect_type = eSectionTypeDWARFDebugPubTypes;
1254 else if (section_name == g_sect_name_dwarf_debug_ranges)
1255 sect_type = eSectionTypeDWARFDebugRanges;
1256 else if (section_name == g_sect_name_dwarf_debug_str)
1257 sect_type = eSectionTypeDWARFDebugStr;
1258 else if (section_name == g_sect_name_dwarf_apple_names)
1259 sect_type = eSectionTypeDWARFAppleNames;
1260 else if (section_name == g_sect_name_dwarf_apple_types)
1261 sect_type = eSectionTypeDWARFAppleTypes;
1262 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1263 sect_type = eSectionTypeDWARFAppleNamespaces;
1264 else if (section_name == g_sect_name_dwarf_apple_objc)
1265 sect_type = eSectionTypeDWARFAppleObjC;
1266 else if (section_name == g_sect_name_objc_selrefs)
1267 sect_type = eSectionTypeDataCStringPointers;
1268 else if (section_name == g_sect_name_objc_msgrefs)
1269 sect_type = eSectionTypeDataObjCMessageRefs;
1270 else if (section_name == g_sect_name_eh_frame)
1271 sect_type = eSectionTypeEHFrame;
1272 else if (section_name == g_sect_name_cfstring)
1273 sect_type = eSectionTypeDataObjCCFStrings;
1274 else if (section_name == g_sect_name_objc_data ||
1275 section_name == g_sect_name_objc_classrefs ||
1276 section_name == g_sect_name_objc_superrefs ||
1277 section_name == g_sect_name_objc_const ||
1278 section_name == g_sect_name_objc_classlist)
1279 {
1280 sect_type = eSectionTypeDataPointers;
1281 }
1282
1283 if (sect_type == eSectionTypeOther)
1284 {
1285 switch (mach_sect_type)
1286 {
1287 // TODO: categorize sections by other flags for regular sections
1288 case SectionTypeRegular:
1289 if (segment_sp->GetName() == g_sect_name_TEXT)
1290 sect_type = eSectionTypeCode;
1291 else if (segment_sp->GetName() == g_sect_name_DATA)
1292 sect_type = eSectionTypeData;
1293 else
1294 sect_type = eSectionTypeOther;
1295 break;
1296 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1297 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1298 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1299 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1300 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1301 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1302 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1303 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1304 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1305 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1306 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1307 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1308 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1309 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1310 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1311 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
1312 default: break;
1313 }
1314 }
1315
1316 SectionSP section_sp(new Section (segment_sp,
1317 module_sp,
1318 this,
1319 ++sectID,
1320 section_name,
1321 sect_type,
1322 sect64.addr - segment_sp->GetFileAddress(),
1323 sect64.size,
1324 sect64.offset,
1325 sect64.offset == 0 ? 0 : sect64.size,
1326 sect64.flags));
1327 // Set the section to be encrypted to match the segment
1328
1329 bool section_is_encrypted = false;
1330 if (!segment_is_encrypted && load_cmd.filesize != 0)
1331 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1332
1333 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1334 segment_sp->GetChildren().AddSection(section_sp);
1335
1336 if (segment_sp->IsFake())
1337 {
1338 segment_sp.reset();
1339 const_segname.Clear();
1340 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341 }
1342 }
Greg Clayton3046e662013-07-10 01:23:25 +00001343 if (segment_sp && is_dsym)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001344 {
Greg Clayton3046e662013-07-10 01:23:25 +00001345 if (first_segment_sectID <= sectID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001346 {
Greg Clayton3046e662013-07-10 01:23:25 +00001347 lldb::user_id_t sect_uid;
1348 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001349 {
Greg Clayton3046e662013-07-10 01:23:25 +00001350 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1351 SectionSP next_section_sp;
1352 if (sect_uid + 1 <= sectID)
1353 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1354
1355 if (curr_section_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001356 {
Greg Clayton3046e662013-07-10 01:23:25 +00001357 if (curr_section_sp->GetByteSize() == 0)
1358 {
1359 if (next_section_sp.get() != NULL)
1360 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1361 else
1362 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1363 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001364 }
1365 }
1366 }
1367 }
1368 }
1369 }
1370 }
Greg Clayton3046e662013-07-10 01:23:25 +00001371 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
1372 {
1373 m_dysymtab.cmd = load_cmd.cmd;
1374 m_dysymtab.cmdsize = load_cmd.cmdsize;
1375 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1376 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001377
Greg Clayton3046e662013-07-10 01:23:25 +00001378 offset = load_cmd_offset + load_cmd.cmdsize;
1379 }
1380
1381// StreamFile s(stdout, false); // REMOVE THIS LINE
1382// s.Printf ("Sections for %s:\n", m_file.GetPath().c_str());// REMOVE THIS LINE
1383// m_sections_ap->Dump(&s, NULL, true, UINT32_MAX);// REMOVE THIS LINE
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001384 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385}
1386
1387class MachSymtabSectionInfo
1388{
1389public:
1390
1391 MachSymtabSectionInfo (SectionList *section_list) :
1392 m_section_list (section_list),
1393 m_section_infos()
1394 {
1395 // Get the number of sections down to a depth of 1 to include
1396 // all segments and their sections, but no other sections that
1397 // may be added for debug map or
1398 m_section_infos.resize(section_list->GetNumSections(1));
1399 }
1400
1401
Greg Claytone72dfb32012-02-24 01:59:29 +00001402 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001403 GetSection (uint8_t n_sect, addr_t file_addr)
1404 {
1405 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00001406 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407 if (n_sect < m_section_infos.size())
1408 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001409 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001410 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001411 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1412 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001413 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00001414 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001415 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1416 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00001417 }
1418 else
1419 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001420 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00001421 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001422 }
1423 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00001424 {
1425 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00001426 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001427 }
1428 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1429 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1430 {
1431 // Symbol is in section with zero size, but has the same start
1432 // address as the section. This can happen with linker symbols
1433 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00001434 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001435 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001437 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001438 }
1439
1440protected:
1441 struct SectionInfo
1442 {
1443 SectionInfo () :
1444 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001445 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001446 {
1447 }
1448
1449 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00001450 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451 };
1452 SectionList *m_section_list;
1453 std::vector<SectionInfo> m_section_infos;
1454};
1455
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001456size_t
Greg Clayton3046e662013-07-10 01:23:25 +00001457ObjectFileMachO::ParseSymtab ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458{
1459 Timer scoped_timer(__PRETTY_FUNCTION__,
1460 "ObjectFileMachO::ParseSymtab () module = %s",
1461 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001462 ModuleSP module_sp (GetModule());
1463 if (!module_sp)
1464 return 0;
1465
1466 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1467 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1468 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1469 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00001470 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001471 uint32_t i;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001472
Greg Clayton5160ce52013-03-27 23:08:40 +00001473 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton77ccca72011-12-30 00:32:24 +00001474
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001475 for (i=0; i<m_header.ncmds; ++i)
1476 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001477 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001478 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001479 struct load_command lc;
1480 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001481 break;
1482 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001483 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001484 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001485 case LoadCommandSymtab:
1486 symtab_load_command.cmd = lc.cmd;
1487 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001488 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001489 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1490 return 0;
1491 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001492 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001493 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001494 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001495 return 0;
1496 }
1497
1498 if (symtab_load_command.stroff == 0)
1499 {
1500 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001501 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001502 return 0;
1503 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001504
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001505 if (symtab_load_command.nsyms == 0)
1506 {
1507 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001508 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001509 return 0;
1510 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001511
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001512 if (symtab_load_command.strsize == 0)
1513 {
1514 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001515 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001516 return 0;
1517 }
1518 break;
1519
1520 case LoadCommandFunctionStarts:
1521 function_starts_load_command.cmd = lc.cmd;
1522 function_starts_load_command.cmdsize = lc.cmdsize;
1523 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1524 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1525 break;
1526
1527 default:
1528 break;
1529 }
1530 offset = cmd_offset + lc.cmdsize;
1531 }
1532
1533 if (symtab_load_command.cmd)
1534 {
1535 Symtab *symtab = m_symtab_ap.get();
1536 SectionList *section_list = GetSectionList();
1537 if (section_list == NULL)
1538 return 0;
1539
1540 ProcessSP process_sp (m_process_wp.lock());
Greg Clayton4c82d422012-05-18 23:20:01 +00001541 Process *process = process_sp.get();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001542
Greg Claytonc7bece562013-01-25 18:06:21 +00001543 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1544 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001545 bool bit_width_32 = addr_byte_size == 4;
1546 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1547
Greg Claytonc7bece562013-01-25 18:06:21 +00001548 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1549 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1550 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00001551 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00001552
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001553 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1554 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00001555 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1556 if (process)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001557 {
Greg Clayton4c82d422012-05-18 23:20:01 +00001558 Target &target = process->GetTarget();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001559 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1560 // Reading mach file from memory in a process or core file...
1561
1562 if (linkedit_section_sp)
1563 {
1564 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1565 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1566 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00001567 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00001568
1569 bool data_was_read = false;
1570
1571#if defined (__APPLE__) && defined (__arm__)
1572 if (m_header.flags & 0x80000000u)
Greg Clayton77ccca72011-12-30 00:32:24 +00001573 {
Greg Clayton26b47e22012-04-18 05:19:20 +00001574 // This mach-o memory file is in the dyld shared cache. If this
1575 // program is not remote and this is iOS, then this process will
1576 // share the same shared cache as the process we are debugging and
1577 // we can read the entire __LINKEDIT from the address space in this
1578 // process. This is a needed optimization that is used for local iOS
1579 // debugging only since all shared libraries in the shared cache do
1580 // not have corresponding files that exist in the file system of the
1581 // device. They have been combined into a single file. This means we
1582 // always have to load these files from memory. All of the symbol and
1583 // string tables from all of the __LINKEDIT sections from the shared
1584 // libraries in the shared cache have been merged into a single large
1585 // symbol and string table. Reading all of this symbol and string table
1586 // data across can slow down debug launch times, so we optimize this by
1587 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00001588
1589 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1590 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1591 bool use_lldb_cache = true;
1592 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1593 {
1594 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00001595 ModuleSP module_sp (GetModule());
1596 if (module_sp)
1597 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1598
Jason Molenda0e0954c2013-04-16 06:24:42 +00001599 }
1600
Greg Clayton26b47e22012-04-18 05:19:20 +00001601 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00001602 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00001603 {
1604 data_was_read = true;
1605 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00001606 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00001607 if (function_starts_load_command.cmd)
1608 {
1609 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1610 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1611 }
1612 }
1613 }
1614#endif
1615
1616 if (!data_was_read)
1617 {
1618 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1619 if (nlist_data_sp)
1620 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Clayton4c82d422012-05-18 23:20:01 +00001621 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1622 //if (strtab_data_sp)
1623 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molendad34e6522013-02-05 22:31:24 +00001624 if (m_dysymtab.nindirectsyms != 0)
1625 {
1626 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1627 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1628 if (indirect_syms_data_sp)
1629 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1630 }
Greg Clayton26b47e22012-04-18 05:19:20 +00001631 if (function_starts_load_command.cmd)
1632 {
1633 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1634 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1635 if (func_start_data_sp)
1636 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1637 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001638 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001639 }
1640 }
1641 else
1642 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001643 nlist_data.SetData (m_data,
1644 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001645 nlist_data_byte_size);
1646 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00001647 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001648 strtab_data_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00001649 if (m_dysymtab.nindirectsyms != 0)
1650 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001651 indirect_symbol_index_data.SetData (m_data,
1652 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00001653 m_dysymtab.nindirectsyms * 4);
1654 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001655 if (function_starts_load_command.cmd)
1656 {
1657 function_starts_data.SetData (m_data,
1658 function_starts_load_command.dataoff,
1659 function_starts_load_command.datasize);
1660 }
1661 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001662
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001663 if (nlist_data.GetByteSize() == 0)
1664 {
1665 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001666 module_sp->LogMessage(log, "failed to read nlist data");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001667 return 0;
1668 }
1669
1670
Greg Claytondebb8812012-05-25 17:04:00 +00001671 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1672 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00001673 {
Greg Claytondebb8812012-05-25 17:04:00 +00001674 if (process)
1675 {
1676 if (strtab_addr == LLDB_INVALID_ADDRESS)
1677 {
1678 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001679 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00001680 return 0;
1681 }
1682 }
1683 else
Greg Clayton4c82d422012-05-18 23:20:01 +00001684 {
1685 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001686 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00001687 return 0;
1688 }
1689 }
Greg Clayton4c82d422012-05-18 23:20:01 +00001690
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001691 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1692 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1693 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1694 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1695 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1696 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1697 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1698 SectionSP eh_frame_section_sp;
1699 if (text_section_sp.get())
1700 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1701 else
1702 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1703
Greg Clayton29e08cb2012-03-14 01:53:24 +00001704 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molenda5635f772013-03-21 03:36:01 +00001705
1706 // lldb works best if it knows the start addresss of all functions in a module.
1707 // Linker symbols or debug info are normally the best source of information for start addr / size but
1708 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00001709 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00001710 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1711 // binary, relative to the text section.
1712 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1713 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1714 // Binaries built to run on older releases may need to use eh_frame information.
1715
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001716 if (text_section_sp && function_starts_data.GetByteSize())
1717 {
1718 FunctionStarts::Entry function_start_entry;
1719 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00001720 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001721 function_start_entry.addr = text_section_sp->GetFileAddress();
1722 uint64_t delta;
1723 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1724 {
1725 // Now append the current entry
1726 function_start_entry.addr += delta;
1727 function_starts.Append(function_start_entry);
1728 }
Jason Molendad63d3c72013-04-16 00:18:44 +00001729 }
Jason Molenda5635f772013-03-21 03:36:01 +00001730 else
1731 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00001732 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1733 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1734 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1735 // the module.
1736 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00001737 {
1738 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1739 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1740 eh_frame.GetFunctionAddressAndSizeVector (functions);
1741 addr_t text_base_addr = text_section_sp->GetFileAddress();
1742 size_t count = functions.GetSize();
1743 for (size_t i = 0; i < count; ++i)
1744 {
1745 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1746 if (func)
1747 {
1748 FunctionStarts::Entry function_start_entry;
1749 function_start_entry.addr = func->base - text_base_addr;
1750 function_starts.Append(function_start_entry);
1751 }
1752 }
1753 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001754 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001755
Greg Claytonc7bece562013-01-25 18:06:21 +00001756 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001757
Greg Claytonc7bece562013-01-25 18:06:21 +00001758 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 +00001759
Greg Claytonc7bece562013-01-25 18:06:21 +00001760 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001761
1762 uint32_t N_SO_index = UINT32_MAX;
1763
1764 MachSymtabSectionInfo section_info (section_list);
1765 std::vector<uint32_t> N_FUN_indexes;
1766 std::vector<uint32_t> N_NSYM_indexes;
1767 std::vector<uint32_t> N_INCL_indexes;
1768 std::vector<uint32_t> N_BRAC_indexes;
1769 std::vector<uint32_t> N_COMM_indexes;
1770 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1771 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00001772 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001773 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1774 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00001775 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001776 // Any symbols that get merged into another will get an entry
1777 // in this map so we know
1778 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1779 uint32_t nlist_idx = 0;
1780 Symbol *symbol_ptr = NULL;
1781
1782 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00001783 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00001784 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00001785 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00001786 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001787
Jason Molendaa5609c82012-06-21 01:51:02 +00001788#if defined (__APPLE__) && defined (__arm__)
1789
1790 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1791 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1792 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1793 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1794 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1795 // nlist parser to ignore all LOCAL symbols.
1796
1797 if (m_header.flags & 0x80000000u)
1798 {
1799 // Before we can start mapping the DSC, we need to make certain the target process is actually
1800 // using the cache we can find.
1801
Jason Molendaa5609c82012-06-21 01:51:02 +00001802 // Next we need to determine the correct path for the dyld shared cache.
1803
1804 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1805 char dsc_path[PATH_MAX];
1806
1807 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00001808 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1809 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00001810 header_arch.GetArchitectureName());
1811
1812 FileSpec dsc_filespec(dsc_path, false);
1813
1814 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molendad63d3c72013-04-16 00:18:44 +00001815 struct lldb_copy_dyld_cache_header_v0
Greg Clayton946f8902012-09-05 22:30:51 +00001816 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001817 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1818 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1819 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda4e7511e2013-03-06 23:19:17 +00001820 uint32_t imagesOffset;
1821 uint32_t imagesCount;
1822 uint64_t dyldBaseAddress;
1823 uint64_t codeSignatureOffset;
1824 uint64_t codeSignatureSize;
1825 uint64_t slideInfoOffset;
1826 uint64_t slideInfoSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00001827 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1828 uint64_t localSymbolsSize; // size of local symbols information
1829 };
1830 struct lldb_copy_dyld_cache_header_v1
1831 {
1832 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1833 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1834 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1835 uint32_t imagesOffset;
1836 uint32_t imagesCount;
1837 uint64_t dyldBaseAddress;
1838 uint64_t codeSignatureOffset;
1839 uint64_t codeSignatureSize;
1840 uint64_t slideInfoOffset;
1841 uint64_t slideInfoSize;
Jason Molenda4e7511e2013-03-06 23:19:17 +00001842 uint64_t localSymbolsOffset;
1843 uint64_t localSymbolsSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00001844 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Clayton946f8902012-09-05 22:30:51 +00001845 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00001846
Jason Molendad63d3c72013-04-16 00:18:44 +00001847 struct lldb_copy_dyld_cache_mapping_info
1848 {
1849 uint64_t address;
1850 uint64_t size;
1851 uint64_t fileOffset;
1852 uint32_t maxProt;
1853 uint32_t initProt;
1854 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00001855
Greg Clayton946f8902012-09-05 22:30:51 +00001856 struct lldb_copy_dyld_cache_local_symbols_info
1857 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001858 uint32_t nlistOffset;
1859 uint32_t nlistCount;
1860 uint32_t stringsOffset;
1861 uint32_t stringsSize;
1862 uint32_t entriesOffset;
1863 uint32_t entriesCount;
Greg Clayton946f8902012-09-05 22:30:51 +00001864 };
1865 struct lldb_copy_dyld_cache_local_symbols_entry
1866 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001867 uint32_t dylibOffset;
1868 uint32_t nlistStartIndex;
1869 uint32_t nlistCount;
Greg Clayton946f8902012-09-05 22:30:51 +00001870 };
Jason Molendaa5609c82012-06-21 01:51:02 +00001871
Jason Molendaf8130862012-06-22 03:28:35 +00001872 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1873 The dyld_cache_local_symbols_info structure gives us three things:
1874 1. The start and count of the nlist records in the dyld_shared_cache file
1875 2. The start and size of the strings for these nlist records
1876 3. The start and count of dyld_cache_local_symbols_entry entries
1877
1878 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1879 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00001880 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00001881 and the count of how many nlist records there are for this dylib/framework.
1882 */
1883
Jason Molendaa5609c82012-06-21 01:51:02 +00001884 // Process the dsc header to find the unmapped symbols
1885 //
1886 // Save some VM space, do not map the entire cache in one shot.
1887
Jason Molenda255f9bb2013-03-06 23:17:36 +00001888 DataBufferSP dsc_data_sp;
1889 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1890
1891 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00001892 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001893 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00001894
Jason Molenda255f9bb2013-03-06 23:17:36 +00001895 char version_str[17];
1896 int version = -1;
1897 lldb::offset_t offset = 0;
1898 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1899 version_str[16] = '\0';
1900 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1901 {
1902 int v;
1903 if (::sscanf (version_str + 6, "%d", &v) == 1)
1904 {
1905 version = v;
1906 }
1907 }
1908
Jason Molenda0e0954c2013-04-16 06:24:42 +00001909 UUID dsc_uuid;
1910 if (version >= 1)
1911 {
1912 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1913 uint8_t uuid_bytes[sizeof (uuid_t)];
1914 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1915 dsc_uuid.SetBytes (uuid_bytes);
1916 }
1917
1918 bool uuid_match = true;
1919 if (dsc_uuid.IsValid() && process)
1920 {
1921 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1922
1923 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1924 {
1925 // The on-disk dyld_shared_cache file is not the same as the one in this
1926 // process' memory, don't use it.
1927 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00001928 ModuleSP module_sp (GetModule());
1929 if (module_sp)
1930 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 +00001931 }
1932 }
1933
Jason Molenda4e7511e2013-03-06 23:19:17 +00001934 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00001935
Jason Molendaa5609c82012-06-21 01:51:02 +00001936 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1937
1938 // If the mappingOffset points to a location inside the header, we've
1939 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda0e0954c2013-04-16 06:24:42 +00001940 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendaa5609c82012-06-21 01:51:02 +00001941 {
1942
Jason Molenda255f9bb2013-03-06 23:17:36 +00001943 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1944 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1945 offset = 0;
1946
1947 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1948 // in the shared library cache need to be adjusted by an offset to match up with the
1949 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1950 // recorded in mapping_offset_value.
1951 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1952
1953 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00001954 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1955 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1956
Jason Molenda4e7511e2013-03-06 23:19:17 +00001957 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00001958 {
1959 // Map the local symbols
Jason Molenda4e7511e2013-03-06 23:19:17 +00001960 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00001961 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001962 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00001963
1964 offset = 0;
1965
1966 // Read the local_symbols_infos struct in one shot
1967 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1968 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1969
Jason Molendaa5609c82012-06-21 01:51:02 +00001970 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1971
Jason Molenda255f9bb2013-03-06 23:17:36 +00001972 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00001973
1974 offset = local_symbols_info.entriesOffset;
1975 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1976 {
1977 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1978 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1979 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1980 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1981
Jason Molenda4e7511e2013-03-06 23:19:17 +00001982 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00001983 {
1984 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1985
1986 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1987 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1988 num_syms = symtab->GetNumSymbols();
1989
1990 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1991 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1992
Jason Molenda4e7511e2013-03-06 23:19:17 +00001993 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00001994 {
1995 /////////////////////////////
1996 {
1997 struct nlist_64 nlist;
1998 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1999 break;
2000
2001 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2002 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2003 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2004 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2005 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2006
2007 SymbolType type = eSymbolTypeInvalid;
2008 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2009
2010 if (symbol_name == NULL)
2011 {
2012 // No symbol should be NULL, even the symbols with no
2013 // string values should have an offset zero which points
2014 // to an empty C-string
2015 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002016 "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 +00002017 entry_index,
2018 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002019 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00002020 continue;
2021 }
2022 if (symbol_name[0] == '\0')
2023 symbol_name = NULL;
2024
2025 const char *symbol_name_non_abi_mangled = NULL;
2026
2027 SectionSP symbol_section;
2028 uint32_t symbol_byte_size = 0;
2029 bool add_nlist = true;
2030 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002031 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002032 bool is_gsym = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002033
2034 assert (sym_idx < num_syms);
2035
2036 sym[sym_idx].SetDebug (is_debug);
2037
2038 if (is_debug)
2039 {
2040 switch (nlist.n_type)
2041 {
2042 case StabGlobalSymbol:
2043 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2044 // Sometimes the N_GSYM value contains the address.
2045
2046 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2047 // have the same address, but we want to ensure that we always find only the real symbol,
2048 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2049 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2050 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2051 // same address.
2052
2053 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2054 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2055 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2056 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2057 add_nlist = false;
2058 else
2059 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002060 is_gsym = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002061 sym[sym_idx].SetExternal(true);
2062 if (nlist.n_value != 0)
2063 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2064 type = eSymbolTypeData;
2065 }
2066 break;
2067
2068 case StabFunctionName:
2069 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2070 type = eSymbolTypeCompiler;
2071 break;
2072
2073 case StabFunction:
2074 // N_FUN -- procedure: name,,n_sect,linenumber,address
2075 if (symbol_name)
2076 {
2077 type = eSymbolTypeCode;
2078 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2079
2080 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2081 // We use the current number of symbols in the symbol table in lieu of
2082 // using nlist_idx in case we ever start trimming entries out
2083 N_FUN_indexes.push_back(sym_idx);
2084 }
2085 else
2086 {
2087 type = eSymbolTypeCompiler;
2088
2089 if ( !N_FUN_indexes.empty() )
2090 {
2091 // Copy the size of the function into the original STAB entry so we don't have
2092 // to hunt for it later
2093 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2094 N_FUN_indexes.pop_back();
2095 // We don't really need the end function STAB as it contains the size which
2096 // we already placed with the original symbol, so don't add it if we want a
2097 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002098 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002099 }
2100 }
2101 break;
2102
2103 case StabStaticSymbol:
2104 // N_STSYM -- static symbol: name,,n_sect,type,address
2105 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2106 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2107 type = eSymbolTypeData;
2108 break;
2109
2110 case StabLocalCommon:
2111 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2112 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2113 type = eSymbolTypeCommonBlock;
2114 break;
2115
2116 case StabBeginSymbol:
2117 // N_BNSYM
2118 // We use the current number of symbols in the symbol table in lieu of
2119 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00002120 // Skip these if we want minimal symbol tables
2121 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002122 break;
2123
2124 case StabEndSymbol:
2125 // N_ENSYM
2126 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2127 // so that we can always skip the entire symbol if we need to navigate
2128 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00002129 // Skip these if we want minimal symbol tables
2130 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002131 break;
2132
2133
2134 case StabSourceFileOptions:
2135 // N_OPT - emitted with gcc2_compiled and in gcc source
2136 type = eSymbolTypeCompiler;
2137 break;
2138
2139 case StabRegisterSymbol:
2140 // N_RSYM - register sym: name,,NO_SECT,type,register
2141 type = eSymbolTypeVariable;
2142 break;
2143
2144 case StabSourceLine:
2145 // N_SLINE - src line: 0,,n_sect,linenumber,address
2146 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2147 type = eSymbolTypeLineEntry;
2148 break;
2149
2150 case StabStructureType:
2151 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2152 type = eSymbolTypeVariableType;
2153 break;
2154
2155 case StabSourceFileName:
2156 // N_SO - source file name
2157 type = eSymbolTypeSourceFile;
2158 if (symbol_name == NULL)
2159 {
Greg Clayton3046e662013-07-10 01:23:25 +00002160 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002161 if (N_SO_index != UINT32_MAX)
2162 {
2163 // Set the size of the N_SO to the terminating index of this N_SO
2164 // so that we can always skip the entire N_SO if we need to navigate
2165 // more quickly at the source level when parsing STABS
2166 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00002167 symbol_ptr->SetByteSize(sym_idx);
Jason Molendaa5609c82012-06-21 01:51:02 +00002168 symbol_ptr->SetSizeIsSibling(true);
2169 }
2170 N_NSYM_indexes.clear();
2171 N_INCL_indexes.clear();
2172 N_BRAC_indexes.clear();
2173 N_COMM_indexes.clear();
2174 N_FUN_indexes.clear();
2175 N_SO_index = UINT32_MAX;
2176 }
2177 else
2178 {
2179 // We use the current number of symbols in the symbol table in lieu of
2180 // using nlist_idx in case we ever start trimming entries out
2181 const bool N_SO_has_full_path = symbol_name[0] == '/';
2182 if (N_SO_has_full_path)
2183 {
Greg Clayton3046e662013-07-10 01:23:25 +00002184 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002185 {
2186 // We have two consecutive N_SO entries where the first contains a directory
2187 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00002188 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002189 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2190 add_nlist = false;
2191 }
2192 else
2193 {
2194 // This is the first entry in a N_SO that contains a directory or
2195 // a full path to the source file
2196 N_SO_index = sym_idx;
2197 }
2198 }
Greg Clayton3046e662013-07-10 01:23:25 +00002199 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002200 {
2201 // This is usually the second N_SO entry that contains just the filename,
2202 // so here we combine it with the first one if we are minimizing the symbol table
2203 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2204 if (so_path && so_path[0])
2205 {
2206 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00002207 const size_t double_slash_pos = full_so_path.find("//");
2208 if (double_slash_pos != std::string::npos)
2209 {
2210 // The linker has been generating bad N_SO entries with doubled up paths
2211 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2212 // and the second is the directory for the source file so you end up with
2213 // a path that looks like "/tmp/src//tmp/src/"
2214 FileSpec so_dir(so_path, false);
2215 if (!so_dir.Exists())
2216 {
2217 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2218 if (so_dir.Exists())
2219 {
2220 // Trim off the incorrect path
2221 full_so_path.erase(0, double_slash_pos + 1);
2222 }
2223 }
2224 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002225 if (*full_so_path.rbegin() != '/')
2226 full_so_path += '/';
2227 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00002228 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002229 add_nlist = false;
2230 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2231 }
2232 }
Greg Clayton946f8902012-09-05 22:30:51 +00002233 else
2234 {
2235 // This could be a relative path to a N_SO
2236 N_SO_index = sym_idx;
2237 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002238 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002239 break;
2240
2241 case StabObjectFileName:
2242 // N_OSO - object file name: name,,0,0,st_mtime
2243 type = eSymbolTypeObjectFile;
2244 break;
2245
2246 case StabLocalSymbol:
2247 // N_LSYM - local sym: name,,NO_SECT,type,offset
2248 type = eSymbolTypeLocal;
2249 break;
2250
2251 //----------------------------------------------------------------------
2252 // INCL scopes
2253 //----------------------------------------------------------------------
2254 case StabBeginIncludeFileName:
2255 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2256 // We use the current number of symbols in the symbol table in lieu of
2257 // using nlist_idx in case we ever start trimming entries out
2258 N_INCL_indexes.push_back(sym_idx);
2259 type = eSymbolTypeScopeBegin;
2260 break;
2261
2262 case StabEndIncludeFile:
2263 // N_EINCL - include file end: name,,NO_SECT,0,0
2264 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2265 // so that we can always skip the entire symbol if we need to navigate
2266 // more quickly at the source level when parsing STABS
2267 if ( !N_INCL_indexes.empty() )
2268 {
2269 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2270 symbol_ptr->SetByteSize(sym_idx + 1);
2271 symbol_ptr->SetSizeIsSibling(true);
2272 N_INCL_indexes.pop_back();
2273 }
2274 type = eSymbolTypeScopeEnd;
2275 break;
2276
2277 case StabIncludeFileName:
2278 // N_SOL - #included file name: name,,n_sect,0,address
2279 type = eSymbolTypeHeaderFile;
2280
2281 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00002282 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002283 break;
2284
2285 case StabCompilerParameters:
2286 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2287 type = eSymbolTypeCompiler;
2288 break;
2289
2290 case StabCompilerVersion:
2291 // N_VERSION - compiler version: name,,NO_SECT,0,0
2292 type = eSymbolTypeCompiler;
2293 break;
2294
2295 case StabCompilerOptLevel:
2296 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2297 type = eSymbolTypeCompiler;
2298 break;
2299
2300 case StabParameter:
2301 // N_PSYM - parameter: name,,NO_SECT,type,offset
2302 type = eSymbolTypeVariable;
2303 break;
2304
2305 case StabAlternateEntry:
2306 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2307 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2308 type = eSymbolTypeLineEntry;
2309 break;
2310
2311 //----------------------------------------------------------------------
2312 // Left and Right Braces
2313 //----------------------------------------------------------------------
2314 case StabLeftBracket:
2315 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2316 // We use the current number of symbols in the symbol table in lieu of
2317 // using nlist_idx in case we ever start trimming entries out
2318 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2319 N_BRAC_indexes.push_back(sym_idx);
2320 type = eSymbolTypeScopeBegin;
2321 break;
2322
2323 case StabRightBracket:
2324 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2325 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2326 // so that we can always skip the entire symbol if we need to navigate
2327 // more quickly at the source level when parsing STABS
2328 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2329 if ( !N_BRAC_indexes.empty() )
2330 {
2331 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2332 symbol_ptr->SetByteSize(sym_idx + 1);
2333 symbol_ptr->SetSizeIsSibling(true);
2334 N_BRAC_indexes.pop_back();
2335 }
2336 type = eSymbolTypeScopeEnd;
2337 break;
2338
2339 case StabDeletedIncludeFile:
2340 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2341 type = eSymbolTypeHeaderFile;
2342 break;
2343
2344 //----------------------------------------------------------------------
2345 // COMM scopes
2346 //----------------------------------------------------------------------
2347 case StabBeginCommon:
2348 // N_BCOMM - begin common: name,,NO_SECT,0,0
2349 // We use the current number of symbols in the symbol table in lieu of
2350 // using nlist_idx in case we ever start trimming entries out
2351 type = eSymbolTypeScopeBegin;
2352 N_COMM_indexes.push_back(sym_idx);
2353 break;
2354
2355 case StabEndCommonLocal:
2356 // N_ECOML - end common (local name): 0,,n_sect,0,address
2357 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2358 // Fall through
2359
2360 case StabEndCommon:
2361 // N_ECOMM - end common: name,,n_sect,0,0
2362 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2363 // so that we can always skip the entire symbol if we need to navigate
2364 // more quickly at the source level when parsing STABS
2365 if ( !N_COMM_indexes.empty() )
2366 {
2367 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2368 symbol_ptr->SetByteSize(sym_idx + 1);
2369 symbol_ptr->SetSizeIsSibling(true);
2370 N_COMM_indexes.pop_back();
2371 }
2372 type = eSymbolTypeScopeEnd;
2373 break;
2374
2375 case StabLength:
2376 // N_LENG - second stab entry with length information
2377 type = eSymbolTypeAdditional;
2378 break;
2379
2380 default: break;
2381 }
2382 }
2383 else
2384 {
2385 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2386 uint8_t n_type = NlistMaskType & nlist.n_type;
2387 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2388
2389 switch (n_type)
2390 {
2391 case NListTypeIndirect: // N_INDR - Fall through
2392 case NListTypePreboundUndefined:// N_PBUD - Fall through
2393 case NListTypeUndefined: // N_UNDF
2394 type = eSymbolTypeUndefined;
2395 break;
2396
2397 case NListTypeAbsolute: // N_ABS
2398 type = eSymbolTypeAbsolute;
2399 break;
2400
2401 case NListTypeSection: // N_SECT
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002402 {
2403 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002404
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002405 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00002406 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002407 // TODO: warn about this?
2408 add_nlist = false;
2409 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002410 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002411
2412 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00002413 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002414 type = eSymbolTypeException;
2415 }
2416 else
2417 {
2418 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002419
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002420 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00002421 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002422 case SectionTypeRegular: break; // regular section
2423 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2424 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2425 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2426 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2427 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2428 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2429 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2430 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2431 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2432 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2433 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2434 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2435 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2436 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2437 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2438 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2439 default: break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002440 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002441
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002442 if (type == eSymbolTypeInvalid)
2443 {
2444 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2445 if (symbol_section->IsDescendant (text_section_sp.get()))
2446 {
2447 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2448 SectionAttrUserSelfModifyingCode |
2449 SectionAttrSytemSomeInstructions))
2450 type = eSymbolTypeData;
2451 else
2452 type = eSymbolTypeCode;
2453 }
2454 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00002455 {
2456 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2457 {
2458 type = eSymbolTypeRuntime;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002459
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002460 if (symbol_name &&
2461 symbol_name[0] == '_' &&
2462 symbol_name[1] == 'O' &&
Jason Molendaa5609c82012-06-21 01:51:02 +00002463 symbol_name[2] == 'B')
2464 {
2465 llvm::StringRef symbol_name_ref(symbol_name);
2466 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2467 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2468 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2469 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2470 {
2471 symbol_name_non_abi_mangled = symbol_name + 1;
2472 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2473 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002474 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002475 }
2476 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2477 {
2478 symbol_name_non_abi_mangled = symbol_name + 1;
2479 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2480 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002481 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002482 }
2483 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2484 {
2485 symbol_name_non_abi_mangled = symbol_name + 1;
2486 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2487 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002488 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002489 }
2490 }
2491 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002492 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00002493 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002494 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00002495 }
2496 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002497 {
2498 type = eSymbolTypeData;
2499 }
2500 }
2501 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2502 {
2503 type = eSymbolTypeTrampoline;
2504 }
2505 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2506 {
2507 type = eSymbolTypeRuntime;
2508 if (symbol_name && symbol_name[0] == '.')
2509 {
2510 llvm::StringRef symbol_name_ref(symbol_name);
2511 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2512 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00002513 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002514 symbol_name_non_abi_mangled = symbol_name;
2515 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2516 type = eSymbolTypeObjCClass;
2517 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002518 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002519 }
2520 }
2521 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002522 }
2523 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002524 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002525 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002526 }
2527
2528 if (add_nlist)
2529 {
2530 uint64_t symbol_value = nlist.n_value;
Jason Molendaa5609c82012-06-21 01:51:02 +00002531 if (symbol_name_non_abi_mangled)
2532 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00002533 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2534 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00002535 }
2536 else
2537 {
Greg Clayton3046e662013-07-10 01:23:25 +00002538 bool symbol_name_is_mangled = false;
2539
Jason Molendaa5609c82012-06-21 01:51:02 +00002540 if (symbol_name && symbol_name[0] == '_')
2541 {
2542 symbol_name_is_mangled = symbol_name[1] == '_';
2543 symbol_name++; // Skip the leading underscore
2544 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002545
Jason Molendaa5609c82012-06-21 01:51:02 +00002546 if (symbol_name)
2547 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002548 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00002549 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00002550 if (is_gsym && is_debug)
2551 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
Jason Molendaa5609c82012-06-21 01:51:02 +00002552 }
2553 }
2554 if (symbol_section)
2555 {
2556 const addr_t section_file_addr = symbol_section->GetFileAddress();
2557 if (symbol_byte_size == 0 && function_starts_count > 0)
2558 {
2559 addr_t symbol_lookup_file_addr = nlist.n_value;
2560 // Do an exact address match for non-ARM addresses, else get the closest since
2561 // the symbol might be a thumb symbol which has an address with bit zero set
2562 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2563 if (is_arm && func_start_entry)
2564 {
2565 // Verify that the function start address is the symbol address (ARM)
2566 // or the symbol address + 1 (thumb)
2567 if (func_start_entry->addr != symbol_lookup_file_addr &&
2568 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2569 {
2570 // Not the right entry, NULL it out...
2571 func_start_entry = NULL;
2572 }
2573 }
2574 if (func_start_entry)
2575 {
2576 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002577
Jason Molendaa5609c82012-06-21 01:51:02 +00002578 addr_t symbol_file_addr = func_start_entry->addr;
2579 uint32_t symbol_flags = 0;
2580 if (is_arm)
2581 {
2582 if (symbol_file_addr & 1)
2583 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2584 symbol_file_addr &= 0xfffffffffffffffeull;
2585 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002586
Jason Molendaa5609c82012-06-21 01:51:02 +00002587 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2588 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2589 if (next_func_start_entry)
2590 {
2591 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2592 // Be sure the clear the Thumb address bit when we calculate the size
2593 // from the current and next address
2594 if (is_arm)
2595 next_symbol_file_addr &= 0xfffffffffffffffeull;
2596 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2597 }
2598 else
2599 {
2600 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2601 }
2602 }
2603 }
2604 symbol_value -= section_file_addr;
2605 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002606
Greg Claytondacc4a92013-05-14 22:19:37 +00002607 if (is_debug == false)
2608 {
2609 if (type == eSymbolTypeCode)
2610 {
2611 // See if we can find a N_FUN entry for any code symbols.
2612 // If we do find a match, and the name matches, then we
2613 // can merge the two into just the function symbol to avoid
2614 // duplicate entries in the symbol table
2615 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2616 if (pos != N_FUN_addr_to_sym_idx.end())
2617 {
Greg Clayton3046e662013-07-10 01:23:25 +00002618 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
Greg Claytondacc4a92013-05-14 22:19:37 +00002619 {
2620 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2621 // We just need the flags from the linker symbol, so put these flags
2622 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2623 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2624 sym[sym_idx].Clear();
2625 continue;
2626 }
2627 }
2628 }
2629 else if (type == eSymbolTypeData)
2630 {
2631 // See if we can find a N_STSYM entry for any data symbols.
2632 // If we do find a match, and the name matches, then we
2633 // can merge the two into just the Static symbol to avoid
2634 // duplicate entries in the symbol table
2635 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2636 if (pos != N_STSYM_addr_to_sym_idx.end())
2637 {
Greg Clayton3046e662013-07-10 01:23:25 +00002638 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
Greg Claytondacc4a92013-05-14 22:19:37 +00002639 {
2640 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2641 // We just need the flags from the linker symbol, so put these flags
2642 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2643 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2644 sym[sym_idx].Clear();
2645 continue;
2646 }
2647 }
2648 else
2649 {
2650 // Combine N_GSYM stab entries with the non stab symbol
Greg Clayton3046e662013-07-10 01:23:25 +00002651 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
Greg Claytondacc4a92013-05-14 22:19:37 +00002652 if (pos != N_GSYM_name_to_sym_idx.end())
2653 {
2654 const uint32_t GSYM_sym_idx = pos->second;
2655 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
2656 // Copy the address, because often the N_GSYM address has an invalid address of zero
2657 // when the global is a common symbol
2658 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
2659 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
2660 // We just need the flags from the linker symbol, so put these flags
2661 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2662 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2663 sym[sym_idx].Clear();
2664 continue;
2665 }
2666 }
2667 }
2668 }
2669
Jason Molendaa5609c82012-06-21 01:51:02 +00002670 sym[sym_idx].SetID (nlist_idx);
2671 sym[sym_idx].SetType (type);
2672 sym[sym_idx].GetAddress().SetSection (symbol_section);
2673 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2674 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002675
Jason Molendaa5609c82012-06-21 01:51:02 +00002676 if (symbol_byte_size > 0)
2677 sym[sym_idx].SetByteSize(symbol_byte_size);
2678
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002679 if (demangled_is_synthesized)
2680 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00002681 ++sym_idx;
2682 }
2683 else
2684 {
2685 sym[sym_idx].Clear();
2686 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002687
Jason Molendaa5609c82012-06-21 01:51:02 +00002688 }
2689 /////////////////////////////
2690 }
2691 break; // No more entries to consider
2692 }
2693 }
2694 }
2695 }
2696 }
2697 }
2698 }
2699
2700 // Must reset this in case it was mutated above!
2701 nlist_data_offset = 0;
2702#endif
2703
2704 // If the sym array was not created while parsing the DSC unmapped
2705 // symbols, create it now.
2706 if (sym == NULL)
2707 {
2708 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2709 num_syms = symtab->GetNumSymbols();
2710 }
2711
2712 if (unmapped_local_symbols_found)
2713 {
2714 assert(m_dysymtab.ilocalsym == 0);
2715 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2716 nlist_idx = m_dysymtab.nlocalsym;
2717 }
2718 else
2719 {
2720 nlist_idx = 0;
2721 }
2722
2723 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002724 {
2725 struct nlist_64 nlist;
2726 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2727 break;
2728
2729 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2730 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2731 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2732 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2733 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2734
2735 SymbolType type = eSymbolTypeInvalid;
Greg Clayton4c82d422012-05-18 23:20:01 +00002736 const char *symbol_name = NULL;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002737
Greg Claytondebb8812012-05-25 17:04:00 +00002738 if (have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00002739 {
2740 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002741
Greg Clayton4c82d422012-05-18 23:20:01 +00002742 if (symbol_name == NULL)
2743 {
2744 // No symbol should be NULL, even the symbols with no
2745 // string values should have an offset zero which points
2746 // to an empty C-string
2747 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002748 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Greg Clayton4c82d422012-05-18 23:20:01 +00002749 nlist_idx,
2750 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002751 module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton4c82d422012-05-18 23:20:01 +00002752 continue;
2753 }
2754 if (symbol_name[0] == '\0')
2755 symbol_name = NULL;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002756 }
Greg Claytondebb8812012-05-25 17:04:00 +00002757 else
2758 {
2759 const addr_t str_addr = strtab_addr + nlist.n_strx;
2760 Error str_error;
2761 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2762 symbol_name = memory_symbol_name.c_str();
2763 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002764 const char *symbol_name_non_abi_mangled = NULL;
2765
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002766 SectionSP symbol_section;
Greg Claytonc7bece562013-01-25 18:06:21 +00002767 lldb::addr_t symbol_byte_size = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002768 bool add_nlist = true;
Greg Claytondacc4a92013-05-14 22:19:37 +00002769 bool is_gsym = false;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002770 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002771 bool demangled_is_synthesized = false;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002772
2773 assert (sym_idx < num_syms);
2774
2775 sym[sym_idx].SetDebug (is_debug);
2776
2777 if (is_debug)
2778 {
2779 switch (nlist.n_type)
Greg Clayton77ccca72011-12-30 00:32:24 +00002780 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002781 case StabGlobalSymbol:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002782 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2783 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002784
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002785 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2786 // have the same address, but we want to ensure that we always find only the real symbol,
2787 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2788 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2789 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2790 // same address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002791
2792 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002793 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2794 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2795 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2796 add_nlist = false;
2797 else
Greg Claytonc9660542012-02-05 02:38:54 +00002798 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002799 is_gsym = true;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002800 sym[sym_idx].SetExternal(true);
2801 if (nlist.n_value != 0)
2802 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2803 type = eSymbolTypeData;
Greg Claytonc9660542012-02-05 02:38:54 +00002804 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002805 break;
Greg Claytonc9660542012-02-05 02:38:54 +00002806
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002807 case StabFunctionName:
2808 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2809 type = eSymbolTypeCompiler;
2810 break;
Greg Clayton77ccca72011-12-30 00:32:24 +00002811
Jason Molenda4e7511e2013-03-06 23:19:17 +00002812 case StabFunction:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002813 // N_FUN -- procedure: name,,n_sect,linenumber,address
2814 if (symbol_name)
Greg Clayton4f8e8692011-10-31 20:50:40 +00002815 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002816 type = eSymbolTypeCode;
2817 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002818
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002819 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2820 // We use the current number of symbols in the symbol table in lieu of
2821 // using nlist_idx in case we ever start trimming entries out
2822 N_FUN_indexes.push_back(sym_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002823 }
2824 else
2825 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002826 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002827
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002828 if ( !N_FUN_indexes.empty() )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002829 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002830 // Copy the size of the function into the original STAB entry so we don't have
2831 // to hunt for it later
2832 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2833 N_FUN_indexes.pop_back();
2834 // We don't really need the end function STAB as it contains the size which
2835 // we already placed with the original symbol, so don't add it if we want a
2836 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002837 add_nlist = false;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002838 }
Greg Clayton456809c2011-12-03 02:30:59 +00002839 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002840 break;
Greg Clayton456809c2011-12-03 02:30:59 +00002841
Jason Molenda4e7511e2013-03-06 23:19:17 +00002842 case StabStaticSymbol:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002843 // N_STSYM -- static symbol: name,,n_sect,type,address
2844 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2845 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2846 type = eSymbolTypeData;
2847 break;
2848
2849 case StabLocalCommon:
2850 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2851 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2852 type = eSymbolTypeCommonBlock;
2853 break;
2854
2855 case StabBeginSymbol:
2856 // N_BNSYM
2857 // We use the current number of symbols in the symbol table in lieu of
2858 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00002859 // Skip these if we want minimal symbol tables
2860 add_nlist = false;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002861 break;
Greg Clayton456809c2011-12-03 02:30:59 +00002862
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002863 case StabEndSymbol:
2864 // N_ENSYM
2865 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2866 // so that we can always skip the entire symbol if we need to navigate
2867 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00002868 // Skip these if we want minimal symbol tables
2869 add_nlist = false;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002870 break;
2871
2872
2873 case StabSourceFileOptions:
2874 // N_OPT - emitted with gcc2_compiled and in gcc source
2875 type = eSymbolTypeCompiler;
2876 break;
2877
2878 case StabRegisterSymbol:
2879 // N_RSYM - register sym: name,,NO_SECT,type,register
2880 type = eSymbolTypeVariable;
2881 break;
2882
2883 case StabSourceLine:
2884 // N_SLINE - src line: 0,,n_sect,linenumber,address
2885 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2886 type = eSymbolTypeLineEntry;
2887 break;
2888
2889 case StabStructureType:
2890 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2891 type = eSymbolTypeVariableType;
2892 break;
2893
2894 case StabSourceFileName:
2895 // N_SO - source file name
2896 type = eSymbolTypeSourceFile;
2897 if (symbol_name == NULL)
2898 {
Greg Clayton3046e662013-07-10 01:23:25 +00002899 add_nlist = false;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002900 if (N_SO_index != UINT32_MAX)
2901 {
2902 // Set the size of the N_SO to the terminating index of this N_SO
2903 // so that we can always skip the entire N_SO if we need to navigate
2904 // more quickly at the source level when parsing STABS
2905 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00002906 symbol_ptr->SetByteSize(sym_idx);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002907 symbol_ptr->SetSizeIsSibling(true);
2908 }
2909 N_NSYM_indexes.clear();
2910 N_INCL_indexes.clear();
2911 N_BRAC_indexes.clear();
2912 N_COMM_indexes.clear();
2913 N_FUN_indexes.clear();
2914 N_SO_index = UINT32_MAX;
2915 }
2916 else
2917 {
2918 // We use the current number of symbols in the symbol table in lieu of
2919 // using nlist_idx in case we ever start trimming entries out
Greg Clayton177b8552012-05-30 20:20:34 +00002920 const bool N_SO_has_full_path = symbol_name[0] == '/';
2921 if (N_SO_has_full_path)
2922 {
Greg Clayton3046e662013-07-10 01:23:25 +00002923 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Greg Clayton177b8552012-05-30 20:20:34 +00002924 {
2925 // We have two consecutive N_SO entries where the first contains a directory
2926 // and the second contains a full path.
Greg Clayton037520e2012-07-18 23:18:10 +00002927 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton177b8552012-05-30 20:20:34 +00002928 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2929 add_nlist = false;
2930 }
2931 else
2932 {
2933 // This is the first entry in a N_SO that contains a directory or
2934 // a full path to the source file
2935 N_SO_index = sym_idx;
2936 }
2937 }
Greg Clayton3046e662013-07-10 01:23:25 +00002938 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002939 {
Greg Clayton177b8552012-05-30 20:20:34 +00002940 // This is usually the second N_SO entry that contains just the filename,
2941 // so here we combine it with the first one if we are minimizing the symbol table
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002942 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2943 if (so_path && so_path[0])
2944 {
2945 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00002946 const size_t double_slash_pos = full_so_path.find("//");
2947 if (double_slash_pos != std::string::npos)
2948 {
2949 // The linker has been generating bad N_SO entries with doubled up paths
2950 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2951 // and the second is the directory for the source file so you end up with
2952 // a path that looks like "/tmp/src//tmp/src/"
2953 FileSpec so_dir(so_path, false);
2954 if (!so_dir.Exists())
2955 {
2956 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2957 if (so_dir.Exists())
2958 {
2959 // Trim off the incorrect path
2960 full_so_path.erase(0, double_slash_pos + 1);
2961 }
2962 }
2963 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002964 if (*full_so_path.rbegin() != '/')
2965 full_so_path += '/';
2966 full_so_path += symbol_name;
Greg Clayton037520e2012-07-18 23:18:10 +00002967 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002968 add_nlist = false;
2969 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2970 }
2971 }
Greg Clayton946f8902012-09-05 22:30:51 +00002972 else
2973 {
2974 // This could be a relative path to a N_SO
2975 N_SO_index = sym_idx;
2976 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002977 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002978
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002979 break;
2980
2981 case StabObjectFileName:
2982 // N_OSO - object file name: name,,0,0,st_mtime
2983 type = eSymbolTypeObjectFile;
2984 break;
2985
2986 case StabLocalSymbol:
2987 // N_LSYM - local sym: name,,NO_SECT,type,offset
2988 type = eSymbolTypeLocal;
2989 break;
2990
2991 //----------------------------------------------------------------------
2992 // INCL scopes
2993 //----------------------------------------------------------------------
2994 case StabBeginIncludeFileName:
2995 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2996 // We use the current number of symbols in the symbol table in lieu of
2997 // using nlist_idx in case we ever start trimming entries out
2998 N_INCL_indexes.push_back(sym_idx);
2999 type = eSymbolTypeScopeBegin;
3000 break;
3001
3002 case StabEndIncludeFile:
3003 // N_EINCL - include file end: name,,NO_SECT,0,0
3004 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3005 // so that we can always skip the entire symbol if we need to navigate
3006 // more quickly at the source level when parsing STABS
3007 if ( !N_INCL_indexes.empty() )
3008 {
3009 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3010 symbol_ptr->SetByteSize(sym_idx + 1);
3011 symbol_ptr->SetSizeIsSibling(true);
3012 N_INCL_indexes.pop_back();
3013 }
3014 type = eSymbolTypeScopeEnd;
3015 break;
3016
3017 case StabIncludeFileName:
3018 // N_SOL - #included file name: name,,n_sect,0,address
3019 type = eSymbolTypeHeaderFile;
3020
3021 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00003022 add_nlist = false;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003023 break;
3024
Jason Molenda4e7511e2013-03-06 23:19:17 +00003025 case StabCompilerParameters:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003026 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
3027 type = eSymbolTypeCompiler;
3028 break;
3029
3030 case StabCompilerVersion:
3031 // N_VERSION - compiler version: name,,NO_SECT,0,0
3032 type = eSymbolTypeCompiler;
3033 break;
3034
3035 case StabCompilerOptLevel:
3036 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
3037 type = eSymbolTypeCompiler;
3038 break;
3039
3040 case StabParameter:
3041 // N_PSYM - parameter: name,,NO_SECT,type,offset
3042 type = eSymbolTypeVariable;
3043 break;
3044
3045 case StabAlternateEntry:
3046 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
3047 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3048 type = eSymbolTypeLineEntry;
3049 break;
3050
3051 //----------------------------------------------------------------------
3052 // Left and Right Braces
3053 //----------------------------------------------------------------------
3054 case StabLeftBracket:
3055 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
3056 // We use the current number of symbols in the symbol table in lieu of
3057 // using nlist_idx in case we ever start trimming entries out
3058 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3059 N_BRAC_indexes.push_back(sym_idx);
3060 type = eSymbolTypeScopeBegin;
3061 break;
3062
3063 case StabRightBracket:
3064 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
3065 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3066 // so that we can always skip the entire symbol if we need to navigate
3067 // more quickly at the source level when parsing STABS
3068 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3069 if ( !N_BRAC_indexes.empty() )
3070 {
3071 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3072 symbol_ptr->SetByteSize(sym_idx + 1);
3073 symbol_ptr->SetSizeIsSibling(true);
3074 N_BRAC_indexes.pop_back();
3075 }
3076 type = eSymbolTypeScopeEnd;
3077 break;
3078
3079 case StabDeletedIncludeFile:
3080 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3081 type = eSymbolTypeHeaderFile;
3082 break;
3083
3084 //----------------------------------------------------------------------
3085 // COMM scopes
3086 //----------------------------------------------------------------------
3087 case StabBeginCommon:
3088 // N_BCOMM - begin common: name,,NO_SECT,0,0
3089 // We use the current number of symbols in the symbol table in lieu of
3090 // using nlist_idx in case we ever start trimming entries out
3091 type = eSymbolTypeScopeBegin;
3092 N_COMM_indexes.push_back(sym_idx);
3093 break;
3094
3095 case StabEndCommonLocal:
3096 // N_ECOML - end common (local name): 0,,n_sect,0,address
3097 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3098 // Fall through
3099
3100 case StabEndCommon:
3101 // N_ECOMM - end common: name,,n_sect,0,0
3102 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3103 // so that we can always skip the entire symbol if we need to navigate
3104 // more quickly at the source level when parsing STABS
3105 if ( !N_COMM_indexes.empty() )
3106 {
3107 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3108 symbol_ptr->SetByteSize(sym_idx + 1);
3109 symbol_ptr->SetSizeIsSibling(true);
3110 N_COMM_indexes.pop_back();
3111 }
3112 type = eSymbolTypeScopeEnd;
3113 break;
3114
3115 case StabLength:
3116 // N_LENG - second stab entry with length information
3117 type = eSymbolTypeAdditional;
3118 break;
3119
3120 default: break;
3121 }
3122 }
3123 else
3124 {
3125 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3126 uint8_t n_type = NlistMaskType & nlist.n_type;
3127 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3128
3129 switch (n_type)
3130 {
3131 case NListTypeIndirect: // N_INDR - Fall through
3132 case NListTypePreboundUndefined:// N_PBUD - Fall through
3133 case NListTypeUndefined: // N_UNDF
3134 type = eSymbolTypeUndefined;
3135 break;
3136
3137 case NListTypeAbsolute: // N_ABS
3138 type = eSymbolTypeAbsolute;
3139 break;
3140
3141 case NListTypeSection: // N_SECT
3142 {
3143 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3144
Sean Callanan9a028512012-08-09 00:50:26 +00003145 if (!symbol_section)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003146 {
3147 // TODO: warn about this?
3148 add_nlist = false;
3149 break;
3150 }
3151
3152 if (TEXT_eh_frame_sectID == nlist.n_sect)
3153 {
3154 type = eSymbolTypeException;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003155 }
3156 else
3157 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003158 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3159
3160 switch (section_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003161 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003162 case SectionTypeRegular: break; // regular section
3163 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3164 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3165 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3166 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3167 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3168 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3169 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3170 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3171 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3172 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3173 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3174 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3175 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3176 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3177 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3178 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3179 default: break;
Greg Clayton456809c2011-12-03 02:30:59 +00003180 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003181
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003182 if (type == eSymbolTypeInvalid)
Greg Clayton456809c2011-12-03 02:30:59 +00003183 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003184 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3185 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton928d8292010-09-08 16:38:06 +00003186 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00003187 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3188 SectionAttrUserSelfModifyingCode |
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003189 SectionAttrSytemSomeInstructions))
3190 type = eSymbolTypeData;
3191 else
3192 type = eSymbolTypeCode;
Greg Clayton928d8292010-09-08 16:38:06 +00003193 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003194 else
3195 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton928d8292010-09-08 16:38:06 +00003196 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003197 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00003198 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003199 type = eSymbolTypeRuntime;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003200
Jason Molenda4e7511e2013-03-06 23:19:17 +00003201 if (symbol_name &&
3202 symbol_name[0] == '_' &&
3203 symbol_name[1] == 'O' &&
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003204 symbol_name[2] == 'B')
Greg Clayton0c38b0d2010-09-12 05:25:16 +00003205 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003206 llvm::StringRef symbol_name_ref(symbol_name);
3207 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3208 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3209 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3210 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003211 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003212 symbol_name_non_abi_mangled = symbol_name + 1;
3213 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3214 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003215 demangled_is_synthesized = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003216 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003217 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003218 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003219 symbol_name_non_abi_mangled = symbol_name + 1;
3220 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3221 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003222 demangled_is_synthesized = true;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003223 }
3224 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3225 {
3226 symbol_name_non_abi_mangled = symbol_name + 1;
3227 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3228 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003229 demangled_is_synthesized = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003230 }
3231 }
3232 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003233 else
3234 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3235 {
3236 type = eSymbolTypeException;
3237 }
3238 else
3239 {
3240 type = eSymbolTypeData;
3241 }
3242 }
3243 else
3244 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3245 {
3246 type = eSymbolTypeTrampoline;
3247 }
3248 else
3249 if (symbol_section->IsDescendant(objc_section_sp.get()))
3250 {
3251 type = eSymbolTypeRuntime;
3252 if (symbol_name && symbol_name[0] == '.')
3253 {
3254 llvm::StringRef symbol_name_ref(symbol_name);
3255 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3256 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3257 {
3258 symbol_name_non_abi_mangled = symbol_name;
3259 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3260 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003261 demangled_is_synthesized = true;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003262 }
3263 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003264 }
3265 }
3266 }
3267 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003268 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003269 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003270 }
3271
3272 if (add_nlist)
3273 {
3274 uint64_t symbol_value = nlist.n_value;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003275
3276 if (symbol_name_non_abi_mangled)
3277 {
Greg Clayton037520e2012-07-18 23:18:10 +00003278 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3279 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003280 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003281 else
3282 {
Greg Clayton3046e662013-07-10 01:23:25 +00003283 bool symbol_name_is_mangled = false;
3284
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003285 if (symbol_name && symbol_name[0] == '_')
3286 {
3287 symbol_name_is_mangled = symbol_name[1] == '_';
3288 symbol_name++; // Skip the leading underscore
3289 }
3290
3291 if (symbol_name)
3292 {
Greg Claytondacc4a92013-05-14 22:19:37 +00003293 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00003294 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00003295 if (is_gsym && is_debug)
3296 {
3297 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
3298 }
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 {
Greg Clayton3046e662013-07-10 01:23:25 +00003360 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
Greg Claytondacc4a92013-05-14 22:19:37 +00003361 {
3362 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3363 // We just need the flags from the linker symbol, so put these flags
3364 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3365 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3366 sym[sym_idx].Clear();
3367 continue;
3368 }
3369 }
3370 }
3371 else if (type == eSymbolTypeData)
3372 {
3373 // See if we can find a N_STSYM entry for any data symbols.
3374 // If we do find a match, and the name matches, then we
3375 // can merge the two into just the Static symbol to avoid
3376 // duplicate entries in the symbol table
3377 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3378 if (pos != N_STSYM_addr_to_sym_idx.end())
3379 {
Greg Clayton3046e662013-07-10 01:23:25 +00003380 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
Greg Claytondacc4a92013-05-14 22:19:37 +00003381 {
3382 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3383 // We just need the flags from the linker symbol, so put these flags
3384 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3385 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3386 sym[sym_idx].Clear();
3387 continue;
3388 }
3389 }
3390 else
3391 {
3392 // Combine N_GSYM stab entries with the non stab symbol
Greg Clayton3046e662013-07-10 01:23:25 +00003393 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
Greg Claytondacc4a92013-05-14 22:19:37 +00003394 if (pos != N_GSYM_name_to_sym_idx.end())
3395 {
3396 const uint32_t GSYM_sym_idx = pos->second;
3397 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3398 // Copy the address, because often the N_GSYM address has an invalid address of zero
3399 // when the global is a common symbol
3400 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3401 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3402 // We just need the flags from the linker symbol, so put these flags
3403 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3404 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3405 sym[sym_idx].Clear();
3406 continue;
3407 }
3408 }
3409 }
3410 }
3411
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003412 sym[sym_idx].SetID (nlist_idx);
3413 sym[sym_idx].SetType (type);
3414 sym[sym_idx].GetAddress().SetSection (symbol_section);
3415 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3416 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3417
3418 if (symbol_byte_size > 0)
3419 sym[sym_idx].SetByteSize(symbol_byte_size);
3420
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003421 if (demangled_is_synthesized)
3422 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3423
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003424 ++sym_idx;
3425 }
3426 else
3427 {
3428 sym[sym_idx].Clear();
3429 }
3430
3431 }
3432
3433 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3434 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3435 // such entries by figuring out what the address for the global is by looking up this non-STAB
3436 // entry and copying the value into the debug symbol's value to save us the hassle in the
3437 // debug symbol parser.
3438
3439 Symbol *global_symbol = NULL;
3440 for (nlist_idx = 0;
3441 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3442 nlist_idx++)
3443 {
3444 if (global_symbol->GetAddress().GetFileAddress() == 0)
3445 {
3446 std::vector<uint32_t> indexes;
3447 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3448 {
3449 std::vector<uint32_t>::const_iterator pos;
3450 std::vector<uint32_t>::const_iterator end = indexes.end();
3451 for (pos = indexes.begin(); pos != end; ++pos)
3452 {
3453 symbol_ptr = symtab->SymbolAtIndex(*pos);
3454 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3455 {
3456 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3457 break;
3458 }
3459 }
3460 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003461 }
3462 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003463
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003464 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3465
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003466 if (function_starts_count > 0)
3467 {
3468 char synthetic_function_symbol[PATH_MAX];
3469 uint32_t num_synthetic_function_symbols = 0;
3470 for (i=0; i<function_starts_count; ++i)
3471 {
3472 if (function_starts.GetEntryRef (i).data == false)
3473 ++num_synthetic_function_symbols;
3474 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003475
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003476 if (num_synthetic_function_symbols > 0)
3477 {
3478 if (num_syms < sym_idx + num_synthetic_function_symbols)
3479 {
3480 num_syms = sym_idx + num_synthetic_function_symbols;
3481 sym = symtab->Resize (num_syms);
3482 }
3483 uint32_t synthetic_function_symbol_idx = 0;
3484 for (i=0; i<function_starts_count; ++i)
3485 {
3486 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3487 if (func_start_entry->data == false)
3488 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003489 addr_t symbol_file_addr = func_start_entry->addr;
3490 uint32_t symbol_flags = 0;
3491 if (is_arm)
3492 {
3493 if (symbol_file_addr & 1)
3494 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3495 symbol_file_addr &= 0xfffffffffffffffeull;
3496 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003497 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003498 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003499 {
3500 SectionSP symbol_section (symbol_addr.GetSection());
3501 uint32_t symbol_byte_size = 0;
3502 if (symbol_section)
3503 {
3504 const addr_t section_file_addr = symbol_section->GetFileAddress();
3505 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3506 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3507 if (next_func_start_entry)
3508 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003509 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3510 if (is_arm)
3511 next_symbol_file_addr &= 0xfffffffffffffffeull;
3512 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 +00003513 }
3514 else
3515 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003516 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003517 }
3518 snprintf (synthetic_function_symbol,
3519 sizeof(synthetic_function_symbol),
3520 "___lldb_unnamed_function%u$$%s",
3521 ++synthetic_function_symbol_idx,
3522 module_sp->GetFileSpec().GetFilename().GetCString());
3523 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Clayton037520e2012-07-18 23:18:10 +00003524 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003525 sym[sym_idx].SetType (eSymbolTypeCode);
3526 sym[sym_idx].SetIsSynthetic (true);
3527 sym[sym_idx].GetAddress() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003528 if (symbol_flags)
3529 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003530 if (symbol_byte_size)
3531 sym[sym_idx].SetByteSize (symbol_byte_size);
3532 ++sym_idx;
3533 }
3534 }
3535 }
3536 }
3537 }
3538 }
3539
3540 // Trim our symbols down to just what we ended up with after
3541 // removing any symbols.
3542 if (sym_idx < num_syms)
3543 {
3544 num_syms = sym_idx;
3545 sym = symtab->Resize (num_syms);
3546 }
3547
3548 // Now synthesize indirect symbols
3549 if (m_dysymtab.nindirectsyms != 0)
3550 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003551 if (indirect_symbol_index_data.GetByteSize())
3552 {
3553 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3554
3555 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3556 {
3557 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3558 {
3559 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3560 if (symbol_stub_byte_size == 0)
3561 continue;
3562
3563 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3564
3565 if (num_symbol_stubs == 0)
3566 continue;
3567
3568 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3569 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3570 {
3571 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3572 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 +00003573 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003574 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3575 {
3576 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3577 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3578 continue;
3579
3580 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3581 Symbol *stub_symbol = NULL;
3582 if (index_pos != end_index_pos)
3583 {
3584 // We have a remapping from the original nlist index to
3585 // a current symbol index, so just look this up by index
3586 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3587 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003588 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003589 {
3590 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00003591 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003592 // S_SYMBOL_STUBS
3593 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3594 }
3595
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003596 if (stub_symbol)
3597 {
3598 Address so_addr(symbol_stub_addr, section_list);
3599
3600 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3601 {
3602 // Change the external symbol into a trampoline that makes sense
3603 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3604 // can re-use them so we don't have to make up a synthetic symbol
3605 // for no good reason.
3606 stub_symbol->SetType (eSymbolTypeTrampoline);
3607 stub_symbol->SetExternal (false);
3608 stub_symbol->GetAddress() = so_addr;
3609 stub_symbol->SetByteSize (symbol_stub_byte_size);
3610 }
3611 else
3612 {
3613 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00003614 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003615 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00003616 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003617 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00003618 stub_symbol = NULL; // this pointer no longer valid
3619 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003620 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00003621 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003622 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3623 sym[sym_idx].SetIsSynthetic (true);
3624 sym[sym_idx].GetAddress() = so_addr;
3625 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3626 ++sym_idx;
3627 }
3628 }
Greg Clayton3f839a32012-09-05 01:38:55 +00003629 else
3630 {
3631 if (log)
3632 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3633 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003634 }
3635 }
3636 }
3637 }
3638 }
3639 }
Greg Clayton3046e662013-07-10 01:23:25 +00003640
3641// StreamFile s(stdout, false);
3642// s.Printf ("Symbol table before CalculateSymbolSizes():\n");
3643// symtab->Dump(&s, NULL, eSortOrderNone);
3644 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
3645 symtab->CalculateSymbolSizes();
3646
3647// s.Printf ("Symbol table after CalculateSymbolSizes():\n");
3648// symtab->Dump(&s, NULL, eSortOrderNone);
3649
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003650 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003651 }
3652 return 0;
3653}
3654
3655
3656void
3657ObjectFileMachO::Dump (Stream *s)
3658{
Greg Claytona1743492012-03-13 23:14:29 +00003659 ModuleSP module_sp(GetModule());
3660 if (module_sp)
3661 {
3662 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3663 s->Printf("%p: ", this);
3664 s->Indent();
3665 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3666 s->PutCString("ObjectFileMachO64");
3667 else
3668 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003669
Greg Claytona1743492012-03-13 23:14:29 +00003670 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003671
Greg Claytona1743492012-03-13 23:14:29 +00003672 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003673
Greg Clayton3046e662013-07-10 01:23:25 +00003674 SectionList *sections = GetSectionList();
3675 if (sections)
3676 sections->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003677
Greg Claytona1743492012-03-13 23:14:29 +00003678 if (m_symtab_ap.get())
3679 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3680 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003681}
3682
Greg Claytonf4d6de62013-04-24 22:29:28 +00003683bool
3684ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3685 const lldb_private::DataExtractor &data,
3686 lldb::offset_t lc_offset,
3687 lldb_private::UUID& uuid)
3688{
3689 uint32_t i;
3690 struct uuid_command load_cmd;
3691
3692 lldb::offset_t offset = lc_offset;
3693 for (i=0; i<header.ncmds; ++i)
3694 {
3695 const lldb::offset_t cmd_offset = offset;
3696 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3697 break;
3698
3699 if (load_cmd.cmd == LoadCommandUUID)
3700 {
3701 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3702
3703 if (uuid_bytes)
3704 {
3705 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3706 // We pretend these object files have no UUID to prevent crashing.
3707
3708 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3709 0x3b, 0xa8,
3710 0x4b, 0x16,
3711 0xb6, 0xa4,
3712 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3713
3714 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3715 return false;
3716
3717 uuid.SetBytes (uuid_bytes);
3718 return true;
3719 }
3720 return false;
3721 }
3722 offset = cmd_offset + load_cmd.cmdsize;
3723 }
3724 return false;
3725}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003726
3727bool
Greg Clayton60830262011-02-04 18:53:10 +00003728ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003729{
Greg Claytona1743492012-03-13 23:14:29 +00003730 ModuleSP module_sp(GetModule());
3731 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003732 {
Greg Claytona1743492012-03-13 23:14:29 +00003733 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00003734 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00003735 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003736 }
3737 return false;
3738}
3739
3740
3741uint32_t
3742ObjectFileMachO::GetDependentModules (FileSpecList& files)
3743{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003744 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00003745 ModuleSP module_sp(GetModule());
3746 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003747 {
Greg Claytona1743492012-03-13 23:14:29 +00003748 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3749 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00003750 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003751 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3752 uint32_t i;
3753 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003754 {
Greg Claytona1743492012-03-13 23:14:29 +00003755 const uint32_t cmd_offset = offset;
3756 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3757 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003758
Greg Claytona1743492012-03-13 23:14:29 +00003759 switch (load_cmd.cmd)
3760 {
3761 case LoadCommandDylibLoad:
3762 case LoadCommandDylibLoadWeak:
3763 case LoadCommandDylibReexport:
3764 case LoadCommandDynamicLinkerLoad:
3765 case LoadCommandFixedVMShlibLoad:
3766 case LoadCommandDylibLoadUpward:
3767 {
3768 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3769 const char *path = m_data.PeekCStr(name_offset);
3770 // Skip any path that starts with '@' since these are usually:
3771 // @executable_path/.../file
3772 // @rpath/.../file
3773 if (path && path[0] != '@')
3774 {
3775 FileSpec file_spec(path, resolve_path);
3776 if (files.AppendIfUnique(file_spec))
3777 count++;
3778 }
3779 }
3780 break;
3781
3782 default:
3783 break;
3784 }
3785 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003786 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003787 }
3788 return count;
3789}
3790
Jim Ingham672e6f52011-03-07 23:44:08 +00003791lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00003792ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00003793{
3794 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3795 // is initialized to an invalid address, so we can just return that.
3796 // 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 +00003797
Jim Ingham672e6f52011-03-07 23:44:08 +00003798 if (!IsExecutable() || m_entry_point_address.IsValid())
3799 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003800
3801 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00003802 // /usr/include/mach-o.h, but it is basically:
3803 //
3804 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3805 // uint32_t count - this is the count of longs in the thread state data
3806 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3807 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00003808 //
Jim Ingham672e6f52011-03-07 23:44:08 +00003809 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3810 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3811 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3812 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3813 //
3814 // For now we hard-code the offsets and flavors we need:
3815 //
3816 //
3817
Greg Claytona1743492012-03-13 23:14:29 +00003818 ModuleSP module_sp(GetModule());
3819 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00003820 {
Greg Claytona1743492012-03-13 23:14:29 +00003821 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3822 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00003823 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003824 uint32_t i;
3825 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3826 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003827
Greg Claytona1743492012-03-13 23:14:29 +00003828 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00003829 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003830 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00003831 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3832 break;
3833
3834 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00003835 {
Greg Claytona1743492012-03-13 23:14:29 +00003836 case LoadCommandUnixThread:
3837 case LoadCommandThread:
Jim Ingham672e6f52011-03-07 23:44:08 +00003838 {
Greg Claytona1743492012-03-13 23:14:29 +00003839 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00003840 {
Greg Claytona1743492012-03-13 23:14:29 +00003841 uint32_t flavor = m_data.GetU32(&offset);
3842 uint32_t count = m_data.GetU32(&offset);
3843 if (count == 0)
3844 {
3845 // We've gotten off somehow, log and exit;
3846 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00003847 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003848
Greg Claytona1743492012-03-13 23:14:29 +00003849 switch (m_header.cputype)
3850 {
3851 case llvm::MachO::CPUTypeARM:
3852 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3853 {
3854 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3855 start_address = m_data.GetU32(&offset);
3856 done = true;
3857 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003858 break;
Greg Claytona1743492012-03-13 23:14:29 +00003859 case llvm::MachO::CPUTypeI386:
3860 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3861 {
3862 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3863 start_address = m_data.GetU32(&offset);
3864 done = true;
3865 }
3866 break;
3867 case llvm::MachO::CPUTypeX86_64:
3868 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3869 {
3870 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3871 start_address = m_data.GetU64(&offset);
3872 done = true;
3873 }
3874 break;
3875 default:
3876 return m_entry_point_address;
3877 }
3878 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3879 if (done)
3880 break;
3881 offset += count * 4;
3882 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003883 }
Greg Claytona1743492012-03-13 23:14:29 +00003884 break;
3885 case LoadCommandMain:
Sean Callanan226b70c2012-03-08 02:39:03 +00003886 {
Greg Claytona1743492012-03-13 23:14:29 +00003887 ConstString text_segment_name ("__TEXT");
3888 uint64_t entryoffset = m_data.GetU64(&offset);
3889 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3890 if (text_segment_sp)
3891 {
3892 done = true;
3893 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3894 }
Sean Callanan226b70c2012-03-08 02:39:03 +00003895 }
Greg Claytona1743492012-03-13 23:14:29 +00003896
3897 default:
3898 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00003899 }
Greg Claytona1743492012-03-13 23:14:29 +00003900 if (done)
3901 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00003902
Greg Claytona1743492012-03-13 23:14:29 +00003903 // Go to the next load command:
3904 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00003905 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003906
Greg Claytona1743492012-03-13 23:14:29 +00003907 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00003908 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00003909 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00003910 // of this ObjectFile:
3911 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00003912 {
Greg Claytona1743492012-03-13 23:14:29 +00003913 m_entry_point_address.Clear();
3914 }
3915 }
3916 else
3917 {
3918 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3919 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003920
Greg Claytona1743492012-03-13 23:14:29 +00003921 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00003922
Greg Claytona1743492012-03-13 23:14:29 +00003923 if (module_sp)
3924 {
3925 SymbolContextList contexts;
3926 SymbolContext context;
3927 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3928 {
3929 if (contexts.GetContextAtIndex(0, context))
3930 m_entry_point_address = context.symbol->GetAddress();
3931 }
Greg Claytone72dfb32012-02-24 01:59:29 +00003932 }
3933 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003934 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003935
Jim Ingham672e6f52011-03-07 23:44:08 +00003936 return m_entry_point_address;
3937
3938}
3939
Greg Claytonc9660542012-02-05 02:38:54 +00003940lldb_private::Address
3941ObjectFileMachO::GetHeaderAddress ()
3942{
3943 lldb_private::Address header_addr;
3944 SectionList *section_list = GetSectionList();
3945 if (section_list)
3946 {
3947 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3948 if (text_segment_sp)
3949 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003950 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00003951 header_addr.SetOffset (0);
3952 }
3953 }
3954 return header_addr;
3955}
3956
Greg Claytonc3776bf2012-02-09 06:16:32 +00003957uint32_t
3958ObjectFileMachO::GetNumThreadContexts ()
3959{
Greg Claytona1743492012-03-13 23:14:29 +00003960 ModuleSP module_sp(GetModule());
3961 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00003962 {
Greg Claytona1743492012-03-13 23:14:29 +00003963 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3964 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00003965 {
Greg Claytona1743492012-03-13 23:14:29 +00003966 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00003967 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003968 FileRangeArray::Entry file_range;
3969 thread_command thread_cmd;
3970 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00003971 {
Greg Claytona1743492012-03-13 23:14:29 +00003972 const uint32_t cmd_offset = offset;
3973 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3974 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003975
Greg Claytona1743492012-03-13 23:14:29 +00003976 if (thread_cmd.cmd == LoadCommandThread)
3977 {
3978 file_range.SetRangeBase (offset);
3979 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3980 m_thread_context_offsets.Append (file_range);
3981 }
3982 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00003983 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00003984 }
3985 }
3986 return m_thread_context_offsets.GetSize();
3987}
3988
3989lldb::RegisterContextSP
3990ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3991{
Greg Claytonc3776bf2012-02-09 06:16:32 +00003992 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00003993
Greg Claytona1743492012-03-13 23:14:29 +00003994 ModuleSP module_sp(GetModule());
3995 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00003996 {
Greg Claytona1743492012-03-13 23:14:29 +00003997 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3998 if (!m_thread_context_offsets_valid)
3999 GetNumThreadContexts ();
4000
4001 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00004002 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00004003 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004004
4005 DataExtractor data (m_data,
4006 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00004007 thread_context_file_range->GetByteSize());
4008
4009 switch (m_header.cputype)
4010 {
4011 case llvm::MachO::CPUTypeARM:
4012 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
4013 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004014
Jim Ingham28eb5712012-10-12 17:34:26 +00004015 case llvm::MachO::CPUTypeI386:
4016 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
4017 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004018
Jim Ingham28eb5712012-10-12 17:34:26 +00004019 case llvm::MachO::CPUTypeX86_64:
4020 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
4021 break;
4022 }
Greg Claytona1743492012-03-13 23:14:29 +00004023 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004024 }
4025 return reg_ctx_sp;
4026}
4027
Greg Claytonc9660542012-02-05 02:38:54 +00004028
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004029ObjectFile::Type
4030ObjectFileMachO::CalculateType()
4031{
4032 switch (m_header.filetype)
4033 {
4034 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4035 if (GetAddressByteSize () == 4)
4036 {
4037 // 32 bit kexts are just object files, but they do have a valid
4038 // UUID load command.
4039 UUID uuid;
4040 if (GetUUID(&uuid))
4041 {
4042 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004043 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004044 // "OSKextGetCurrentIdentifier" as this is required of kexts
4045 if (m_strata == eStrataInvalid)
4046 m_strata = eStrataKernel;
4047 return eTypeSharedLibrary;
4048 }
4049 }
4050 return eTypeObjectFile;
4051
4052 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
4053 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
4054 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
4055 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
4056 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
4057 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
4058 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
4059 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
4060 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
4061 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
4062 default:
4063 break;
4064 }
4065 return eTypeUnknown;
4066}
4067
4068ObjectFile::Strata
4069ObjectFileMachO::CalculateStrata()
4070{
4071 switch (m_header.filetype)
4072 {
4073 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4074 {
4075 // 32 bit kexts are just object files, but they do have a valid
4076 // UUID load command.
4077 UUID uuid;
4078 if (GetUUID(&uuid))
4079 {
4080 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004081 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004082 // "OSKextGetCurrentIdentifier" as this is required of kexts
4083 if (m_type == eTypeInvalid)
4084 m_type = eTypeSharedLibrary;
4085
4086 return eStrataKernel;
4087 }
4088 }
4089 return eStrataUnknown;
4090
4091 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
4092 // Check for the MH_DYLDLINK bit in the flags
4093 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callanan49bce8e2012-02-10 20:22:35 +00004094 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004095 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00004096 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004097 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00004098 {
4099 SectionList *section_list = GetSectionList();
4100 if (section_list)
4101 {
4102 static ConstString g_kld_section_name ("__KLD");
4103 if (section_list->FindSectionByName(g_kld_section_name))
4104 return eStrataKernel;
4105 }
4106 }
4107 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004108
4109 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4110 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callanan49bce8e2012-02-10 20:22:35 +00004111 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004112 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4113 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4114 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4115 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4116 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4117 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4118 default:
4119 break;
4120 }
4121 return eStrataUnknown;
4122}
4123
4124
Greg Claytonc2ff9312012-02-22 19:41:02 +00004125uint32_t
4126ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4127{
Greg Claytona1743492012-03-13 23:14:29 +00004128 ModuleSP module_sp(GetModule());
4129 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004130 {
Greg Claytona1743492012-03-13 23:14:29 +00004131 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4132 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004133 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004134 uint32_t version_cmd = 0;
4135 uint64_t version = 0;
4136 uint32_t i;
4137 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004138 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004139 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004140 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4141 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004142
Greg Claytona1743492012-03-13 23:14:29 +00004143 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004144 {
Greg Claytona1743492012-03-13 23:14:29 +00004145 if (version_cmd == 0)
4146 {
4147 version_cmd = load_cmd.cmd;
4148 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4149 break;
4150 version = load_cmd.dylib.current_version;
4151 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004152 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00004153 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00004154 }
Greg Claytona1743492012-03-13 23:14:29 +00004155 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004156 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004157
Greg Claytona1743492012-03-13 23:14:29 +00004158 if (version_cmd == LoadCommandDylibIdent)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004159 {
Greg Claytona1743492012-03-13 23:14:29 +00004160 if (versions != NULL && num_versions > 0)
4161 {
4162 if (num_versions > 0)
4163 versions[0] = (version & 0xFFFF0000ull) >> 16;
4164 if (num_versions > 1)
4165 versions[1] = (version & 0x0000FF00ull) >> 8;
4166 if (num_versions > 2)
4167 versions[2] = (version & 0x000000FFull);
4168 // Fill in an remaining version numbers with invalid values
4169 for (i=3; i<num_versions; ++i)
4170 versions[i] = UINT32_MAX;
4171 }
4172 // The LC_ID_DYLIB load command has a version with 3 version numbers
4173 // in it, so always return 3
4174 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004175 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00004176 }
4177 return false;
4178}
4179
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004180bool
Greg Clayton514487e2011-02-15 21:59:32 +00004181ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004182{
Greg Claytona1743492012-03-13 23:14:29 +00004183 ModuleSP module_sp(GetModule());
4184 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00004185 {
Greg Claytona1743492012-03-13 23:14:29 +00004186 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4187 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +00004188
Greg Claytona1743492012-03-13 23:14:29 +00004189 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda4e7511e2013-03-06 23:19:17 +00004190 // debugs at the addresses in the file itself. Below we set the OS to
Greg Claytona1743492012-03-13 23:14:29 +00004191 // unknown to make sure we use the DynamicLoaderStatic()...
4192 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4193 {
4194 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4195 }
4196 return true;
Greg Clayton593577a2011-09-21 03:57:31 +00004197 }
Greg Claytona1743492012-03-13 23:14:29 +00004198 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004199}
4200
4201
Jason Molenda0e0954c2013-04-16 06:24:42 +00004202UUID
4203ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4204{
4205 UUID uuid;
4206 if (process)
4207 {
4208 addr_t all_image_infos = process->GetImageInfoAddress();
4209
4210 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4211 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4212 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4213 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4214
4215 Error err;
4216 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4217 if (version_or_magic != -1
4218 && version_or_magic != HeaderMagic32
4219 && version_or_magic != HeaderMagic32Swapped
4220 && version_or_magic != HeaderMagic64
4221 && version_or_magic != HeaderMagic64Swapped
4222 && version_or_magic >= 13)
4223 {
4224 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4225 int wordsize = process->GetAddressByteSize();
4226 if (wordsize == 8)
4227 {
4228 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4229 }
4230 if (wordsize == 4)
4231 {
4232 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4233 }
4234 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4235 {
4236 uuid_t shared_cache_uuid;
4237 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4238 {
4239 uuid.SetBytes (shared_cache_uuid);
4240 }
4241 }
4242 }
4243 }
4244 return uuid;
4245}
4246
4247UUID
4248ObjectFileMachO::GetLLDBSharedCacheUUID ()
4249{
4250 UUID uuid;
4251#if defined (__APPLE__) && defined (__arm__)
4252 uint8_t *(*dyld_get_all_image_infos)(void);
4253 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4254 if (dyld_get_all_image_infos)
4255 {
4256 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4257 if (dyld_all_image_infos_address)
4258 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00004259 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4260 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00004261 {
Jason Molenda42b69fa2013-04-16 22:56:17 +00004262 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 +00004263 uuid.SetBytes (sharedCacheUUID_address);
4264 }
4265 }
4266 }
4267#endif
4268 return uuid;
4269}
4270
4271
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004272//------------------------------------------------------------------
4273// PluginInterface protocol
4274//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00004275lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004276ObjectFileMachO::GetPluginName()
4277{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004278 return GetPluginNameStatic();
4279}
4280
4281uint32_t
4282ObjectFileMachO::GetPluginVersion()
4283{
4284 return 1;
4285}
4286