blob: 88213c750e46ade4a1df204a1e7049bef802045a [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ObjectFileMachO.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Greg Clayton456809c2011-12-03 02:30:59 +000010#include "llvm/ADT/StringRef.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000011#include "llvm/Support/MachO.h"
12
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "ObjectFileMachO.h"
14
Greg Clayton3f839a32012-09-05 01:38:55 +000015#include "lldb/lldb-private-log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/ArchSpec.h"
17#include "lldb/Core/DataBuffer.h"
Jason Molendaf6ce26f2013-04-10 05:58:57 +000018#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/FileSpecList.h"
Greg Clayton3f839a32012-09-05 01:38:55 +000020#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000022#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/PluginManager.h"
Greg Clayton1eac0c72012-04-24 03:06:13 +000024#include "lldb/Core/RangeMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Section.h"
26#include "lldb/Core/StreamFile.h"
27#include "lldb/Core/StreamString.h"
28#include "lldb/Core/Timer.h"
29#include "lldb/Core/UUID.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000030#include "lldb/Host/Host.h"
31#include "lldb/Host/FileSpec.h"
Sean Callananb6d70eb2011-10-12 02:08:07 +000032#include "lldb/Symbol/ClangNamespaceDecl.h"
Jason Molenda5635f772013-03-21 03:36:01 +000033#include "lldb/Symbol/DWARFCallFrameInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Symbol/ObjectFile.h"
Greg Clayton26b47e22012-04-18 05:19:20 +000035#include "lldb/Target/Platform.h"
Greg Claytonc9660542012-02-05 02:38:54 +000036#include "lldb/Target/Process.h"
Greg Clayton26b47e22012-04-18 05:19:20 +000037#include "lldb/Target/Target.h"
Greg Claytonc859e2d2012-02-13 23:10:39 +000038#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
39#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Claytonc3776bf2012-02-09 06:16:32 +000040#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041
Jason Molenda0e0954c2013-04-16 06:24:42 +000042#if defined (__APPLE__) && defined (__arm__)
43// GetLLDBSharedCacheUUID() needs to call dlsym()
44#include <dlfcn.h>
45#endif
46
Daniel Maleaffeb4b62013-04-17 19:24:22 +000047#ifndef __APPLE__
48#include "Utility/UuidCompatibility.h"
49#endif
50
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051using namespace lldb;
52using namespace lldb_private;
Greg Claytone1a916a2010-07-21 22:12:05 +000053using namespace llvm::MachO;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
Jason Molenda4e7511e2013-03-06 23:19:17 +000055class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
Greg Claytonc3776bf2012-02-09 06:16:32 +000056{
57public:
58 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
59 RegisterContextDarwin_x86_64 (thread, 0)
60 {
61 SetRegisterDataFrom_LC_THREAD (data);
62 }
63
64 virtual void
65 InvalidateAllRegisters ()
66 {
67 // Do nothing... registers are always valid...
68 }
69
70 void
71 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
72 {
Greg Claytonc7bece562013-01-25 18:06:21 +000073 lldb::offset_t offset = 0;
Greg Claytonc3776bf2012-02-09 06:16:32 +000074 SetError (GPRRegSet, Read, -1);
75 SetError (FPURegSet, Read, -1);
76 SetError (EXCRegSet, Read, -1);
Greg Claytonc859e2d2012-02-13 23:10:39 +000077 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +000078
Greg Claytonc859e2d2012-02-13 23:10:39 +000079 while (!done)
Greg Claytonc3776bf2012-02-09 06:16:32 +000080 {
Greg Claytonc859e2d2012-02-13 23:10:39 +000081 int flavor = data.GetU32 (&offset);
82 if (flavor == 0)
83 done = true;
84 else
Greg Claytonc3776bf2012-02-09 06:16:32 +000085 {
Greg Claytonc859e2d2012-02-13 23:10:39 +000086 uint32_t i;
87 uint32_t count = data.GetU32 (&offset);
88 switch (flavor)
89 {
90 case GPRRegSet:
91 for (i=0; i<count; ++i)
92 (&gpr.rax)[i] = data.GetU64(&offset);
93 SetError (GPRRegSet, Read, 0);
94 done = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +000095
Greg Claytonc859e2d2012-02-13 23:10:39 +000096 break;
97 case FPURegSet:
98 // TODO: fill in FPU regs....
99 //SetError (FPURegSet, Read, -1);
100 done = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000101
Greg Claytonc859e2d2012-02-13 23:10:39 +0000102 break;
103 case EXCRegSet:
104 exc.trapno = data.GetU32(&offset);
105 exc.err = data.GetU32(&offset);
106 exc.faultvaddr = data.GetU64(&offset);
107 SetError (EXCRegSet, Read, 0);
108 done = true;
109 break;
110 case 7:
111 case 8:
112 case 9:
113 // fancy flavors that encapsulate of the the above
114 // falvors...
115 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000116
Greg Claytonc859e2d2012-02-13 23:10:39 +0000117 default:
118 done = true;
119 break;
120 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000121 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000122 }
123 }
124protected:
125 virtual int
126 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
127 {
128 return 0;
129 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000130
Greg Claytonc859e2d2012-02-13 23:10:39 +0000131 virtual int
132 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
133 {
134 return 0;
135 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000136
Greg Claytonc859e2d2012-02-13 23:10:39 +0000137 virtual int
138 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
139 {
140 return 0;
141 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000142
Greg Claytonc859e2d2012-02-13 23:10:39 +0000143 virtual int
144 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
145 {
146 return 0;
147 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000148
Greg Claytonc859e2d2012-02-13 23:10:39 +0000149 virtual int
150 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
151 {
152 return 0;
153 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000154
Greg Claytonc859e2d2012-02-13 23:10:39 +0000155 virtual int
156 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
157 {
158 return 0;
159 }
160};
Greg Claytonc3776bf2012-02-09 06:16:32 +0000161
Greg Claytonc859e2d2012-02-13 23:10:39 +0000162
Jason Molenda4e7511e2013-03-06 23:19:17 +0000163class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
Greg Claytonc859e2d2012-02-13 23:10:39 +0000164{
165public:
166 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
167 RegisterContextDarwin_i386 (thread, 0)
168 {
169 SetRegisterDataFrom_LC_THREAD (data);
170 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000171
Greg Claytonc859e2d2012-02-13 23:10:39 +0000172 virtual void
173 InvalidateAllRegisters ()
174 {
175 // Do nothing... registers are always valid...
176 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000177
Greg Claytonc859e2d2012-02-13 23:10:39 +0000178 void
179 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
180 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000181 lldb::offset_t offset = 0;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000182 SetError (GPRRegSet, Read, -1);
183 SetError (FPURegSet, Read, -1);
184 SetError (EXCRegSet, Read, -1);
185 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000186
Greg Claytonc859e2d2012-02-13 23:10:39 +0000187 while (!done)
188 {
189 int flavor = data.GetU32 (&offset);
190 if (flavor == 0)
191 done = true;
192 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000193 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000194 uint32_t i;
195 uint32_t count = data.GetU32 (&offset);
196 switch (flavor)
197 {
198 case GPRRegSet:
199 for (i=0; i<count; ++i)
200 (&gpr.eax)[i] = data.GetU32(&offset);
201 SetError (GPRRegSet, Read, 0);
202 done = true;
203
204 break;
205 case FPURegSet:
206 // TODO: fill in FPU regs....
207 //SetError (FPURegSet, Read, -1);
208 done = true;
209
210 break;
211 case EXCRegSet:
212 exc.trapno = data.GetU32(&offset);
213 exc.err = data.GetU32(&offset);
214 exc.faultvaddr = data.GetU32(&offset);
215 SetError (EXCRegSet, Read, 0);
216 done = true;
217 break;
218 case 7:
219 case 8:
220 case 9:
221 // fancy flavors that encapsulate of the the above
222 // falvors...
223 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000224
Greg Claytonc859e2d2012-02-13 23:10:39 +0000225 default:
226 done = true;
227 break;
228 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000229 }
230 }
231 }
232protected:
233 virtual int
234 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
235 {
236 return 0;
237 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000238
Greg Claytonc3776bf2012-02-09 06:16:32 +0000239 virtual int
240 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
241 {
242 return 0;
243 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000244
Greg Claytonc3776bf2012-02-09 06:16:32 +0000245 virtual int
246 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
247 {
248 return 0;
249 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000250
Greg Claytonc3776bf2012-02-09 06:16:32 +0000251 virtual int
252 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
253 {
254 return 0;
255 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000256
Greg Claytonc3776bf2012-02-09 06:16:32 +0000257 virtual int
258 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
259 {
260 return 0;
261 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000262
Greg Claytonc3776bf2012-02-09 06:16:32 +0000263 virtual int
264 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
265 {
266 return 0;
267 }
268};
269
Jason Molenda4e7511e2013-03-06 23:19:17 +0000270class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Claytonc859e2d2012-02-13 23:10:39 +0000271{
272public:
273 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonc2807462012-10-30 23:57:32 +0000274 RegisterContextDarwin_arm (thread, 0)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000275 {
276 SetRegisterDataFrom_LC_THREAD (data);
277 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000278
Greg Claytonc859e2d2012-02-13 23:10:39 +0000279 virtual void
280 InvalidateAllRegisters ()
281 {
282 // Do nothing... registers are always valid...
283 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000284
Greg Claytonc859e2d2012-02-13 23:10:39 +0000285 void
286 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
287 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000288 lldb::offset_t offset = 0;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000289 SetError (GPRRegSet, Read, -1);
290 SetError (FPURegSet, Read, -1);
291 SetError (EXCRegSet, Read, -1);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000292 bool done = false;
293
294 while (!done)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000295 {
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000296 int flavor = data.GetU32 (&offset);
297 uint32_t count = data.GetU32 (&offset);
Jason Molendaddf91772013-05-14 04:50:47 +0000298 lldb::offset_t next_thread_state = offset + (count * 4);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000299 switch (flavor)
300 {
301 case GPRRegSet:
302 for (uint32_t i=0; i<count; ++i)
Jason Molendaddf91772013-05-14 04:50:47 +0000303 {
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000304 gpr.r[i] = data.GetU32(&offset);
Jason Molendaddf91772013-05-14 04:50:47 +0000305 }
306
307 // Note that gpr.cpsr is also copied by the above loop; this loop technically extends
308 // one element past the end of the gpr.r[] array.
309
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000310 SetError (GPRRegSet, Read, 0);
Jason Molendaddf91772013-05-14 04:50:47 +0000311 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000312 break;
313
314 case FPURegSet:
315 {
Jason Molenda663d2e12013-05-14 03:52:22 +0000316 uint8_t *fpu_reg_buf = (uint8_t*) &fpu.floats.s[0];
317 const int fpu_reg_buf_size = sizeof (fpu.floats);
318 if (data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000319 {
Jason Molenda663d2e12013-05-14 03:52:22 +0000320 offset += fpu_reg_buf_size;
321 fpu.fpscr = data.GetU32(&offset);
322 SetError (FPURegSet, Read, 0);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000323 }
Jason Molenda663d2e12013-05-14 03:52:22 +0000324 else
325 {
326 done = true;
327 }
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000328 }
Jason Molendaddf91772013-05-14 04:50:47 +0000329 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000330 break;
331
332 case EXCRegSet:
Jason Molendaddf91772013-05-14 04:50:47 +0000333 if (count == 3)
334 {
335 exc.exception = data.GetU32(&offset);
336 exc.fsr = data.GetU32(&offset);
337 exc.far = data.GetU32(&offset);
338 SetError (EXCRegSet, Read, 0);
339 }
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000340 done = true;
Jason Molendaddf91772013-05-14 04:50:47 +0000341 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000342 break;
343
344 // Unknown register set flavor, stop trying to parse.
345 default:
346 done = true;
347 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000348 }
349 }
350protected:
351 virtual int
352 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
353 {
Jason Molendaddf91772013-05-14 04:50:47 +0000354 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000355 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000356
Greg Claytonc859e2d2012-02-13 23:10:39 +0000357 virtual int
358 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
359 {
Jason Molendaddf91772013-05-14 04:50:47 +0000360 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000361 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000362
Greg Claytonc859e2d2012-02-13 23:10:39 +0000363 virtual int
364 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
365 {
Jason Molendaddf91772013-05-14 04:50:47 +0000366 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000367 }
Greg Claytonc2807462012-10-30 23:57:32 +0000368
369 virtual int
370 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
371 {
372 return -1;
373 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000374
Greg Claytonc859e2d2012-02-13 23:10:39 +0000375 virtual int
376 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
377 {
378 return 0;
379 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000380
Greg Claytonc859e2d2012-02-13 23:10:39 +0000381 virtual int
382 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
383 {
384 return 0;
385 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000386
Greg Claytonc859e2d2012-02-13 23:10:39 +0000387 virtual int
388 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
389 {
390 return 0;
391 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000392
Greg Claytonc2807462012-10-30 23:57:32 +0000393 virtual int
394 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
395 {
396 return -1;
397 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000398};
399
Greg Clayton9aae0a12013-05-15 19:52:08 +0000400static uint32_t
401MachHeaderSizeFromMagic(uint32_t magic)
402{
403 switch (magic)
404 {
405 case HeaderMagic32:
406 case HeaderMagic32Swapped:
407 return sizeof(struct mach_header);
408
409 case HeaderMagic64:
410 case HeaderMagic64Swapped:
411 return sizeof(struct mach_header_64);
412 break;
413
414 default:
415 break;
416 }
417 return 0;
418}
419
Greg Claytonded470d2011-03-19 01:12:21 +0000420#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421
422void
423ObjectFileMachO::Initialize()
424{
425 PluginManager::RegisterPlugin (GetPluginNameStatic(),
426 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +0000427 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000428 CreateMemoryInstance,
429 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430}
431
432void
433ObjectFileMachO::Terminate()
434{
435 PluginManager::UnregisterPlugin (CreateInstance);
436}
437
438
Greg Clayton57abc5d2013-05-10 21:47:16 +0000439lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440ObjectFileMachO::GetPluginNameStatic()
441{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000442 static ConstString g_name("mach-o");
443 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444}
445
446const char *
447ObjectFileMachO::GetPluginDescriptionStatic()
448{
449 return "Mach-o object file reader (32 and 64 bit)";
450}
451
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452ObjectFile *
Greg Clayton5ce9c562013-02-06 17:22:03 +0000453ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
454 DataBufferSP& data_sp,
455 lldb::offset_t data_offset,
456 const FileSpec* file,
457 lldb::offset_t file_offset,
458 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000460 if (!data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000462 data_sp = file->MemoryMapFileContents(file_offset, length);
463 data_offset = 0;
464 }
465
466 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
467 {
468 // Update the data to contain the entire file if it doesn't already
469 if (data_sp->GetByteSize() < length)
470 {
471 data_sp = file->MemoryMapFileContents(file_offset, length);
472 data_offset = 0;
473 }
Greg Clayton7b0992d2013-04-18 22:45:39 +0000474 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, data_offset, file, file_offset, length));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475 if (objfile_ap.get() && objfile_ap->ParseHeader())
476 return objfile_ap.release();
477 }
478 return NULL;
479}
480
Greg Claytonc9660542012-02-05 02:38:54 +0000481ObjectFile *
Jason Molenda4e7511e2013-03-06 23:19:17 +0000482ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
483 DataBufferSP& data_sp,
484 const ProcessSP &process_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000485 lldb::addr_t header_addr)
486{
487 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
488 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000489 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonc9660542012-02-05 02:38:54 +0000490 if (objfile_ap.get() && objfile_ap->ParseHeader())
491 return objfile_ap.release();
492 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000493 return NULL;
Greg Claytonc9660542012-02-05 02:38:54 +0000494}
495
Greg Claytonf4d6de62013-04-24 22:29:28 +0000496size_t
497ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
498 lldb::DataBufferSP& data_sp,
499 lldb::offset_t data_offset,
500 lldb::offset_t file_offset,
501 lldb::offset_t length,
502 lldb_private::ModuleSpecList &specs)
503{
504 const size_t initial_count = specs.GetSize();
505
506 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
507 {
508 DataExtractor data;
509 data.SetData(data_sp);
510 llvm::MachO::mach_header header;
511 if (ParseHeader (data, &data_offset, header))
512 {
513 if (header.sizeofcmds >= data_sp->GetByteSize())
514 {
515 data_sp = file.ReadFileContents(file_offset, header.sizeofcmds);
Greg Clayton2540a8a2013-07-12 22:07:46 +0000516 data.SetData(data_sp);
517 data_offset = MachHeaderSizeFromMagic(header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +0000518 }
519 if (data_sp)
520 {
521 ModuleSpec spec;
522 spec.GetFileSpec() = file;
523 spec.GetArchitecture().SetArchitecture(eArchTypeMachO,
524 header.cputype,
525 header.cpusubtype);
526 if (spec.GetArchitecture().IsValid())
527 {
528 GetUUID (header, data, data_offset, spec.GetUUID());
529 specs.Append(spec);
530 }
531 }
532 }
533 }
534 return specs.GetSize() - initial_count;
535}
536
537
Greg Claytonc9660542012-02-05 02:38:54 +0000538
539const ConstString &
540ObjectFileMachO::GetSegmentNameTEXT()
541{
542 static ConstString g_segment_name_TEXT ("__TEXT");
543 return g_segment_name_TEXT;
544}
545
546const ConstString &
547ObjectFileMachO::GetSegmentNameDATA()
548{
549 static ConstString g_segment_name_DATA ("__DATA");
550 return g_segment_name_DATA;
551}
552
553const ConstString &
554ObjectFileMachO::GetSegmentNameOBJC()
555{
556 static ConstString g_segment_name_OBJC ("__OBJC");
557 return g_segment_name_OBJC;
558}
559
560const ConstString &
561ObjectFileMachO::GetSegmentNameLINKEDIT()
562{
563 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
564 return g_section_name_LINKEDIT;
565}
566
567const ConstString &
568ObjectFileMachO::GetSectionNameEHFrame()
569{
570 static ConstString g_section_name_eh_frame ("__eh_frame");
571 return g_section_name_eh_frame;
572}
573
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574bool
Jason Molenda4e7511e2013-03-06 23:19:17 +0000575ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
576 lldb::addr_t data_offset,
Greg Clayton44435ed2012-01-12 05:25:17 +0000577 lldb::addr_t data_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578{
Greg Clayton44435ed2012-01-12 05:25:17 +0000579 DataExtractor data;
580 data.SetData (data_sp, data_offset, data_length);
Greg Claytonc7bece562013-01-25 18:06:21 +0000581 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582 uint32_t magic = data.GetU32(&offset);
583 return MachHeaderSizeFromMagic(magic) != 0;
584}
585
586
Greg Clayton5ce9c562013-02-06 17:22:03 +0000587ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
588 DataBufferSP& data_sp,
589 lldb::offset_t data_offset,
590 const FileSpec* file,
591 lldb::offset_t file_offset,
592 lldb::offset_t length) :
593 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000594 m_mach_segments(),
595 m_mach_sections(),
596 m_entry_point_address(),
597 m_thread_context_offsets(),
598 m_thread_context_offsets_valid(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599{
Greg Clayton72b77eb2011-02-04 21:13:05 +0000600 ::memset (&m_header, 0, sizeof(m_header));
601 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602}
603
Greg Claytone72dfb32012-02-24 01:59:29 +0000604ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000605 lldb::DataBufferSP& header_data_sp,
606 const lldb::ProcessSP &process_sp,
607 lldb::addr_t header_addr) :
Greg Claytone72dfb32012-02-24 01:59:29 +0000608 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000609 m_mach_segments(),
610 m_mach_sections(),
611 m_entry_point_address(),
612 m_thread_context_offsets(),
613 m_thread_context_offsets_valid(false)
Greg Claytonc9660542012-02-05 02:38:54 +0000614{
615 ::memset (&m_header, 0, sizeof(m_header));
616 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
617}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618
619ObjectFileMachO::~ObjectFileMachO()
620{
621}
622
Greg Claytonf4d6de62013-04-24 22:29:28 +0000623bool
624ObjectFileMachO::ParseHeader (DataExtractor &data,
625 lldb::offset_t *data_offset_ptr,
626 llvm::MachO::mach_header &header)
627{
628 data.SetByteOrder (lldb::endian::InlHostByteOrder());
629 // Leave magic in the original byte order
630 header.magic = data.GetU32(data_offset_ptr);
631 bool can_parse = false;
632 bool is_64_bit = false;
633 switch (header.magic)
634 {
635 case HeaderMagic32:
636 data.SetByteOrder (lldb::endian::InlHostByteOrder());
637 data.SetAddressByteSize(4);
638 can_parse = true;
639 break;
640
641 case HeaderMagic64:
642 data.SetByteOrder (lldb::endian::InlHostByteOrder());
643 data.SetAddressByteSize(8);
644 can_parse = true;
645 is_64_bit = true;
646 break;
647
648 case HeaderMagic32Swapped:
649 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
650 data.SetAddressByteSize(4);
651 can_parse = true;
652 break;
653
654 case HeaderMagic64Swapped:
655 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
656 data.SetAddressByteSize(8);
657 is_64_bit = true;
658 can_parse = true;
659 break;
660
661 default:
662 break;
663 }
664
665 if (can_parse)
666 {
667 data.GetU32(data_offset_ptr, &header.cputype, 6);
668 if (is_64_bit)
669 *data_offset_ptr += 4;
670 return true;
671 }
672 else
673 {
674 memset(&header, 0, sizeof(header));
675 }
676 return false;
677}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678
679bool
680ObjectFileMachO::ParseHeader ()
681{
Greg Claytona1743492012-03-13 23:14:29 +0000682 ModuleSP module_sp(GetModule());
683 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 {
Greg Claytona1743492012-03-13 23:14:29 +0000685 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
686 bool can_parse = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000687 lldb::offset_t offset = 0;
Greg Clayton7fb56d02011-02-01 01:31:41 +0000688 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +0000689 // Leave magic in the original byte order
690 m_header.magic = m_data.GetU32(&offset);
691 switch (m_header.magic)
Greg Claytonc9660542012-02-05 02:38:54 +0000692 {
Greg Claytona1743492012-03-13 23:14:29 +0000693 case HeaderMagic32:
694 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
695 m_data.SetAddressByteSize(4);
696 can_parse = true;
697 break;
698
699 case HeaderMagic64:
700 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
701 m_data.SetAddressByteSize(8);
702 can_parse = true;
703 break;
704
705 case HeaderMagic32Swapped:
706 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
707 m_data.SetAddressByteSize(4);
708 can_parse = true;
709 break;
710
711 case HeaderMagic64Swapped:
712 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
713 m_data.SetAddressByteSize(8);
714 can_parse = true;
715 break;
716
717 default:
718 break;
Greg Claytonc9660542012-02-05 02:38:54 +0000719 }
Greg Claytona1743492012-03-13 23:14:29 +0000720
721 if (can_parse)
722 {
723 m_data.GetU32(&offset, &m_header.cputype, 6);
724
725 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +0000726
Greg Claytond97ec1c2012-11-16 21:36:10 +0000727 // Check if the module has a required architecture
728 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callananbf4b7be2012-12-13 22:07:14 +0000729 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Claytond97ec1c2012-11-16 21:36:10 +0000730 return false;
731
Greg Claytona1743492012-03-13 23:14:29 +0000732 if (SetModulesArchitecture (mach_arch))
733 {
734 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
735 if (m_data.GetByteSize() < header_and_lc_size)
736 {
737 DataBufferSP data_sp;
738 ProcessSP process_sp (m_process_wp.lock());
739 if (process_sp)
740 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000741 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Claytona1743492012-03-13 23:14:29 +0000742 }
743 else
744 {
745 // Read in all only the load command data from the file on disk
Greg Clayton5ce9c562013-02-06 17:22:03 +0000746 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Claytona1743492012-03-13 23:14:29 +0000747 if (data_sp->GetByteSize() != header_and_lc_size)
748 return false;
749 }
750 if (data_sp)
751 m_data.SetData (data_sp);
752 }
753 }
754 return true;
755 }
756 else
757 {
758 memset(&m_header, 0, sizeof(struct mach_header));
759 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760 }
761 return false;
762}
763
764
765ByteOrder
766ObjectFileMachO::GetByteOrder () const
767{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768 return m_data.GetByteOrder ();
769}
770
Jim Ingham5aee1622010-08-09 23:31:02 +0000771bool
772ObjectFileMachO::IsExecutable() const
773{
774 return m_header.filetype == HeaderFileTypeExecutable;
775}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776
Greg Claytonc7bece562013-01-25 18:06:21 +0000777uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000778ObjectFileMachO::GetAddressByteSize () const
779{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780 return m_data.GetAddressByteSize ();
781}
782
Greg Claytone0d378b2011-03-24 21:19:54 +0000783AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +0000784ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
785{
786 Symtab *symtab = GetSymtab();
787 if (symtab)
788 {
789 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
790 if (symbol)
791 {
Greg Claytone7612132012-03-07 21:03:09 +0000792 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +0000793 {
Greg Claytone7612132012-03-07 21:03:09 +0000794 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +0000795 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +0000796 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000797 const SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000798 switch (section_type)
799 {
800 case eSectionTypeInvalid: return eAddressClassUnknown;
801 case eSectionTypeCode:
802 if (m_header.cputype == llvm::MachO::CPUTypeARM)
803 {
804 // For ARM we have a bit in the n_desc field of the symbol
805 // that tells us ARM/Thumb which is bit 0x0008.
806 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
807 return eAddressClassCodeAlternateISA;
808 }
809 return eAddressClassCode;
810
811 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton5009f9d2011-10-27 17:55:14 +0000812 case eSectionTypeData:
813 case eSectionTypeDataCString:
814 case eSectionTypeDataCStringPointers:
815 case eSectionTypeDataSymbolAddress:
816 case eSectionTypeData4:
817 case eSectionTypeData8:
818 case eSectionTypeData16:
819 case eSectionTypeDataPointers:
820 case eSectionTypeZeroFill:
821 case eSectionTypeDataObjCMessageRefs:
822 case eSectionTypeDataObjCCFStrings:
823 return eAddressClassData;
824 case eSectionTypeDebug:
825 case eSectionTypeDWARFDebugAbbrev:
826 case eSectionTypeDWARFDebugAranges:
827 case eSectionTypeDWARFDebugFrame:
828 case eSectionTypeDWARFDebugInfo:
829 case eSectionTypeDWARFDebugLine:
830 case eSectionTypeDWARFDebugLoc:
831 case eSectionTypeDWARFDebugMacInfo:
832 case eSectionTypeDWARFDebugPubNames:
833 case eSectionTypeDWARFDebugPubTypes:
834 case eSectionTypeDWARFDebugRanges:
835 case eSectionTypeDWARFDebugStr:
836 case eSectionTypeDWARFAppleNames:
837 case eSectionTypeDWARFAppleTypes:
838 case eSectionTypeDWARFAppleNamespaces:
839 case eSectionTypeDWARFAppleObjC:
840 return eAddressClassDebug;
Greg Claytonded470d2011-03-19 01:12:21 +0000841 case eSectionTypeEHFrame: return eAddressClassRuntime;
Michael Sartaina7499c92013-07-01 19:45:50 +0000842 case eSectionTypeELFSymbolTable:
843 case eSectionTypeELFDynamicSymbols:
844 case eSectionTypeELFRelocationEntries:
845 case eSectionTypeELFDynamicLinkInfo:
Greg Claytonded470d2011-03-19 01:12:21 +0000846 case eSectionTypeOther: return eAddressClassUnknown;
847 }
848 }
849 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000850
Greg Claytone0d378b2011-03-24 21:19:54 +0000851 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000852 switch (symbol_type)
853 {
854 case eSymbolTypeAny: return eAddressClassUnknown;
855 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000856
Greg Claytonded470d2011-03-19 01:12:21 +0000857 case eSymbolTypeCode:
858 case eSymbolTypeTrampoline:
Greg Clayton059f7242013-02-27 21:16:04 +0000859 case eSymbolTypeResolver:
Greg Claytonded470d2011-03-19 01:12:21 +0000860 if (m_header.cputype == llvm::MachO::CPUTypeARM)
861 {
862 // For ARM we have a bit in the n_desc field of the symbol
863 // that tells us ARM/Thumb which is bit 0x0008.
864 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
865 return eAddressClassCodeAlternateISA;
866 }
867 return eAddressClassCode;
868
869 case eSymbolTypeData: return eAddressClassData;
870 case eSymbolTypeRuntime: return eAddressClassRuntime;
871 case eSymbolTypeException: return eAddressClassRuntime;
872 case eSymbolTypeSourceFile: return eAddressClassDebug;
873 case eSymbolTypeHeaderFile: return eAddressClassDebug;
874 case eSymbolTypeObjectFile: return eAddressClassDebug;
875 case eSymbolTypeCommonBlock: return eAddressClassDebug;
876 case eSymbolTypeBlock: return eAddressClassDebug;
877 case eSymbolTypeLocal: return eAddressClassData;
878 case eSymbolTypeParam: return eAddressClassData;
879 case eSymbolTypeVariable: return eAddressClassData;
880 case eSymbolTypeVariableType: return eAddressClassDebug;
881 case eSymbolTypeLineEntry: return eAddressClassDebug;
882 case eSymbolTypeLineHeader: return eAddressClassDebug;
883 case eSymbolTypeScopeBegin: return eAddressClassDebug;
884 case eSymbolTypeScopeEnd: return eAddressClassDebug;
885 case eSymbolTypeAdditional: return eAddressClassUnknown;
886 case eSymbolTypeCompiler: return eAddressClassDebug;
887 case eSymbolTypeInstrumentation:return eAddressClassDebug;
888 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +0000889 case eSymbolTypeObjCClass: return eAddressClassRuntime;
890 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
891 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +0000892 }
893 }
894 }
895 return eAddressClassUnknown;
896}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897
898Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +0000899ObjectFileMachO::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900{
Greg Claytona1743492012-03-13 23:14:29 +0000901 ModuleSP module_sp(GetModule());
902 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903 {
Greg Claytona1743492012-03-13 23:14:29 +0000904 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
905 if (m_symtab_ap.get() == NULL)
906 {
907 m_symtab_ap.reset(new Symtab(this));
908 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton3046e662013-07-10 01:23:25 +0000909 ParseSymtab ();
Greg Claytona1743492012-03-13 23:14:29 +0000910 m_symtab_ap->Finalize ();
911 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000912 }
913 return m_symtab_ap.get();
914}
915
Greg Clayton3046e662013-07-10 01:23:25 +0000916bool
917ObjectFileMachO::IsStripped ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918{
Greg Clayton3046e662013-07-10 01:23:25 +0000919 if (m_dysymtab.cmd == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000920 {
Greg Clayton3046e662013-07-10 01:23:25 +0000921 ModuleSP module_sp(GetModule());
922 if (module_sp)
Greg Claytona1743492012-03-13 23:14:29 +0000923 {
Greg Clayton3046e662013-07-10 01:23:25 +0000924 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
925 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton4d78c402012-05-25 18:09:55 +0000926 {
Greg Clayton3046e662013-07-10 01:23:25 +0000927 const lldb::offset_t load_cmd_offset = offset;
928
929 load_command lc;
930 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
931 break;
932 if (lc.cmd == LoadCommandDynamicSymtabInfo)
Greg Clayton4d78c402012-05-25 18:09:55 +0000933 {
Greg Clayton3046e662013-07-10 01:23:25 +0000934 m_dysymtab.cmd = lc.cmd;
935 m_dysymtab.cmdsize = lc.cmdsize;
936 if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
937 {
938 // Clear m_dysymtab if we were unable to read all items from the load command
939 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
940 }
Greg Clayton4d78c402012-05-25 18:09:55 +0000941 }
Greg Clayton3046e662013-07-10 01:23:25 +0000942 offset = load_cmd_offset + lc.cmdsize;
Greg Clayton4d78c402012-05-25 18:09:55 +0000943 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000944 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000945 }
Greg Clayton3046e662013-07-10 01:23:25 +0000946 if (m_dysymtab.cmd)
947 return m_dysymtab.nlocalsym == 0;
948 return false;
949}
Greg Clayton1eac0c72012-04-24 03:06:13 +0000950
Greg Clayton3046e662013-07-10 01:23:25 +0000951void
952ObjectFileMachO::CreateSections (SectionList &unified_section_list)
953{
954 if (!m_sections_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000955 {
Greg Clayton3046e662013-07-10 01:23:25 +0000956 m_sections_ap.reset(new SectionList());
957
958 const bool is_dsym = (m_header.filetype == HeaderFileTypeDSYM);
959 lldb::user_id_t segID = 0;
960 lldb::user_id_t sectID = 0;
961 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
962 uint32_t i;
963 const bool is_core = GetType() == eTypeCoreFile;
964 //bool dump_sections = false;
965 ModuleSP module_sp (GetModule());
966 // First look up any LC_ENCRYPTION_INFO load commands
967 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
968 EncryptedFileRanges encrypted_file_ranges;
969 encryption_info_command encryption_cmd;
970 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971 {
Greg Clayton3046e662013-07-10 01:23:25 +0000972 const lldb::offset_t load_cmd_offset = offset;
973 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
974 break;
975
976 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977 {
Greg Clayton3046e662013-07-10 01:23:25 +0000978 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000979 {
Greg Clayton3046e662013-07-10 01:23:25 +0000980 if (encryption_cmd.cryptid != 0)
Greg Claytond37d6922013-04-16 16:51:19 +0000981 {
Greg Clayton3046e662013-07-10 01:23:25 +0000982 EncryptedFileRanges::Entry entry;
983 entry.SetRangeBase(encryption_cmd.cryptoff);
984 entry.SetByteSize(encryption_cmd.cryptsize);
985 encrypted_file_ranges.Append(entry);
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000986 }
987 }
Greg Clayton3046e662013-07-10 01:23:25 +0000988 }
989 offset = load_cmd_offset + encryption_cmd.cmdsize;
990 }
991
992 offset = MachHeaderSizeFromMagic(m_header.magic);
993
994 struct segment_command_64 load_cmd;
995 for (i=0; i<m_header.ncmds; ++i)
996 {
997 const lldb::offset_t load_cmd_offset = offset;
998 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
999 break;
1000
1001 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
1002 {
1003 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004 {
Greg Clayton3046e662013-07-10 01:23:25 +00001005 bool add_section = true;
1006 bool add_to_unified = true;
1007 ConstString const_segname (load_cmd.segname, std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
Jason Molenda4e7511e2013-03-06 23:19:17 +00001008
Greg Clayton3046e662013-07-10 01:23:25 +00001009 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1010 if (is_dsym && unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011 {
Greg Clayton3046e662013-07-10 01:23:25 +00001012 if (const_segname == GetSegmentNameLINKEDIT())
1013 {
1014 // We need to keep the __LINKEDIT segment private to this object file only
1015 add_to_unified = false;
1016 }
1017 else
1018 {
1019 // This is the dSYM file and this section has already been created by
1020 // the object file, no need to create it.
1021 add_section = false;
1022 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023 }
Greg Clayton3046e662013-07-10 01:23:25 +00001024 load_cmd.vmaddr = m_data.GetAddress(&offset);
1025 load_cmd.vmsize = m_data.GetAddress(&offset);
1026 load_cmd.fileoff = m_data.GetAddress(&offset);
1027 load_cmd.filesize = m_data.GetAddress(&offset);
1028 if (m_length != 0 && load_cmd.filesize != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029 {
Greg Clayton3046e662013-07-10 01:23:25 +00001030 if (load_cmd.fileoff > m_length)
1031 {
1032 // We have a load command that says it extends past the end of hte file. This is likely
1033 // a corrupt file. We don't have any way to return an error condition here (this method
1034 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1035 // is null out the SectionList vector and if a process has been set up, dump a message
1036 // to stdout. The most common case here is core file debugging with a truncated file.
1037 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda20eb31b2013-08-16 03:20:42 +00001038 module_sp->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 ")",
1039 i,
1040 lc_segment_name,
1041 load_cmd.fileoff,
1042 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001043
1044 load_cmd.fileoff = 0;
1045 load_cmd.filesize = 0;
1046 }
1047
1048 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1049 {
1050 // We have a load command that says it extends past the end of hte file. This is likely
1051 // a corrupt file. We don't have any way to return an error condition here (this method
1052 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1053 // is null out the SectionList vector and if a process has been set up, dump a message
1054 // to stdout. The most common case here is core file debugging with a truncated file.
1055 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda20eb31b2013-08-16 03:20:42 +00001056 module_sp->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff + filesize (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 "), the segment will be truncated",
1057 i,
1058 lc_segment_name,
1059 load_cmd.fileoff + load_cmd.filesize,
1060 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001061
1062 // Tuncase the length
1063 load_cmd.filesize = m_length - load_cmd.fileoff;
1064 }
1065 }
1066 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1067 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068
Greg Clayton3046e662013-07-10 01:23:25 +00001069 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070
Greg Clayton3046e662013-07-10 01:23:25 +00001071 // Keep a list of mach segments around in case we need to
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072 // get at data that isn't stored in the abstracted Sections.
Greg Clayton3046e662013-07-10 01:23:25 +00001073 m_mach_segments.push_back (load_cmd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074
Greg Clayton3046e662013-07-10 01:23:25 +00001075 // Use a segment ID of the segment index shifted left by 8 so they
1076 // never conflict with any of the sections.
1077 SectionSP segment_sp;
1078 if (add_section && (const_segname || is_core))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079 {
Greg Clayton3046e662013-07-10 01:23:25 +00001080 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
1081 this, // Object file to which this sections belongs
1082 ++segID << 8, // Section ID is the 1 based segment index shifted right by 8 bits as not to collide with any of the 256 section IDs that are possible
1083 const_segname, // Name of this section
1084 eSectionTypeContainer, // This section is a container of other sections.
1085 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1086 load_cmd.vmsize, // VM size in bytes of this section
1087 load_cmd.fileoff, // Offset to the data for this section in the file
1088 load_cmd.filesize, // Size in bytes of this section as found in the the file
1089 load_cmd.flags)); // Flags for this section
Greg Clayton8d38ac42010-06-28 23:51:11 +00001090
Greg Clayton3046e662013-07-10 01:23:25 +00001091 segment_sp->SetIsEncrypted (segment_is_encrypted);
1092 m_sections_ap->AddSection(segment_sp);
1093 if (add_to_unified)
1094 unified_section_list.AddSection(segment_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095 }
Greg Clayton3046e662013-07-10 01:23:25 +00001096 else if (unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001097 {
Jason Molenda20eb31b2013-08-16 03:20:42 +00001098 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr)
1099 {
1100 // Check to see if the module was read from memory?
1101 if (module_sp->GetObjectFile()->GetHeaderAddress().IsValid())
1102 {
1103 // We have a module that is in memory and needs to have its
1104 // file address adjusted. We need to do this because when we
1105 // load a file from memory, its addresses will be slid already,
1106 // yet the addresses in the new symbol file will still be unslid.
1107 // Since everything is stored as section offset, this shouldn't
1108 // cause any problems.
Jason Molenda5894a732013-08-17 03:39:52 +00001109
1110 // Make sure we've parsed the symbol table from the
1111 // ObjectFile before we go around changing its Sections.
1112 module_sp->GetObjectFile()->GetSymtab();
1113 // eh_frame would present the same problems but we parse that on
1114 // a per-function basis as-needed so it's more difficult to
1115 // remove its use of the Sections. Realistically, the environments
1116 // where this code path will be taken will not have eh_frame sections.
1117
Jason Molenda20eb31b2013-08-16 03:20:42 +00001118 unified_section_sp->SetFileAddress(load_cmd.vmaddr);
1119 }
1120 }
Greg Clayton3046e662013-07-10 01:23:25 +00001121 m_sections_ap->AddSection(unified_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123
Greg Clayton3046e662013-07-10 01:23:25 +00001124 struct section_64 sect64;
1125 ::memset (&sect64, 0, sizeof(sect64));
1126 // Push a section into our mach sections for the section at
1127 // index zero (NListSectionNoSection) if we don't have any
1128 // mach sections yet...
1129 if (m_mach_sections.empty())
1130 m_mach_sections.push_back(sect64);
1131 uint32_t segment_sect_idx;
1132 const lldb::user_id_t first_segment_sectID = sectID + 1;
1133
1134
1135 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
1136 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001137 {
Greg Clayton3046e662013-07-10 01:23:25 +00001138 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
Greg Clayton89411422010-10-08 00:21:05 +00001139 break;
Greg Clayton3046e662013-07-10 01:23:25 +00001140 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1141 break;
1142 sect64.addr = m_data.GetAddress(&offset);
1143 sect64.size = m_data.GetAddress(&offset);
1144
1145 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1146 break;
1147
1148 // Keep a list of mach sections around in case we need to
1149 // get at data that isn't stored in the abstracted Sections.
1150 m_mach_sections.push_back (sect64);
1151
1152 if (add_section)
1153 {
1154 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1155 if (!const_segname)
1156 {
1157 // We have a segment with no name so we need to conjure up
1158 // segments that correspond to the section's segname if there
1159 // isn't already such a section. If there is such a section,
1160 // we resize the section so that it spans all sections.
1161 // We also mark these sections as fake so address matches don't
1162 // hit if they land in the gaps between the child sections.
1163 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1164 segment_sp = unified_section_list.FindSectionByName (const_segname);
1165 if (segment_sp.get())
1166 {
1167 Section *segment = segment_sp.get();
1168 // Grow the section size as needed.
1169 const lldb::addr_t sect64_min_addr = sect64.addr;
1170 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1171 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1172 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1173 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1174 if (sect64_min_addr >= curr_seg_min_addr)
1175 {
1176 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1177 // Only grow the section size if needed
1178 if (new_seg_byte_size > curr_seg_byte_size)
1179 segment->SetByteSize (new_seg_byte_size);
1180 }
1181 else
1182 {
1183 // We need to change the base address of the segment and
1184 // adjust the child section offsets for all existing children.
1185 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1186 segment->Slide(slide_amount, false);
1187 segment->GetChildren().Slide(-slide_amount, false);
1188 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1189 }
1190
1191 // Grow the section size as needed.
1192 if (sect64.offset)
1193 {
1194 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1195 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1196
1197 const lldb::addr_t section_min_file_offset = sect64.offset;
1198 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1199 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1200 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1201 segment->SetFileOffset (new_file_offset);
1202 segment->SetFileSize (new_file_size);
1203 }
1204 }
1205 else
1206 {
1207 // Create a fake section for the section's named segment
1208 segment_sp.reset(new Section (segment_sp, // Parent section
1209 module_sp, // Module to which this section belongs
1210 this, // Object file to which this section belongs
1211 ++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
1212 const_segname, // Name of this section
1213 eSectionTypeContainer, // This section is a container of other sections.
1214 sect64.addr, // File VM address == addresses as they are found in the object file
1215 sect64.size, // VM size in bytes of this section
1216 sect64.offset, // Offset to the data for this section in the file
1217 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1218 load_cmd.flags)); // Flags for this section
1219 segment_sp->SetIsFake(true);
1220
1221 m_sections_ap->AddSection(segment_sp);
1222 if (add_to_unified)
1223 unified_section_list.AddSection(segment_sp);
1224 segment_sp->SetIsEncrypted (segment_is_encrypted);
1225 }
1226 }
1227 assert (segment_sp.get());
1228
1229 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
1230 static ConstString g_sect_name_objc_data ("__objc_data");
1231 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1232 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1233 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1234 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1235 static ConstString g_sect_name_objc_const ("__objc_const");
1236 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1237 static ConstString g_sect_name_cfstring ("__cfstring");
1238
1239 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1240 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1241 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1242 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1243 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1244 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1245 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1246 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1247 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1248 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1249 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1250 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1251 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1252 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1253 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1254 static ConstString g_sect_name_eh_frame ("__eh_frame");
1255 static ConstString g_sect_name_DATA ("__DATA");
1256 static ConstString g_sect_name_TEXT ("__TEXT");
1257
1258 SectionType sect_type = eSectionTypeOther;
1259
1260 if (section_name == g_sect_name_dwarf_debug_abbrev)
1261 sect_type = eSectionTypeDWARFDebugAbbrev;
1262 else if (section_name == g_sect_name_dwarf_debug_aranges)
1263 sect_type = eSectionTypeDWARFDebugAranges;
1264 else if (section_name == g_sect_name_dwarf_debug_frame)
1265 sect_type = eSectionTypeDWARFDebugFrame;
1266 else if (section_name == g_sect_name_dwarf_debug_info)
1267 sect_type = eSectionTypeDWARFDebugInfo;
1268 else if (section_name == g_sect_name_dwarf_debug_line)
1269 sect_type = eSectionTypeDWARFDebugLine;
1270 else if (section_name == g_sect_name_dwarf_debug_loc)
1271 sect_type = eSectionTypeDWARFDebugLoc;
1272 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1273 sect_type = eSectionTypeDWARFDebugMacInfo;
1274 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1275 sect_type = eSectionTypeDWARFDebugPubNames;
1276 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1277 sect_type = eSectionTypeDWARFDebugPubTypes;
1278 else if (section_name == g_sect_name_dwarf_debug_ranges)
1279 sect_type = eSectionTypeDWARFDebugRanges;
1280 else if (section_name == g_sect_name_dwarf_debug_str)
1281 sect_type = eSectionTypeDWARFDebugStr;
1282 else if (section_name == g_sect_name_dwarf_apple_names)
1283 sect_type = eSectionTypeDWARFAppleNames;
1284 else if (section_name == g_sect_name_dwarf_apple_types)
1285 sect_type = eSectionTypeDWARFAppleTypes;
1286 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1287 sect_type = eSectionTypeDWARFAppleNamespaces;
1288 else if (section_name == g_sect_name_dwarf_apple_objc)
1289 sect_type = eSectionTypeDWARFAppleObjC;
1290 else if (section_name == g_sect_name_objc_selrefs)
1291 sect_type = eSectionTypeDataCStringPointers;
1292 else if (section_name == g_sect_name_objc_msgrefs)
1293 sect_type = eSectionTypeDataObjCMessageRefs;
1294 else if (section_name == g_sect_name_eh_frame)
1295 sect_type = eSectionTypeEHFrame;
1296 else if (section_name == g_sect_name_cfstring)
1297 sect_type = eSectionTypeDataObjCCFStrings;
1298 else if (section_name == g_sect_name_objc_data ||
1299 section_name == g_sect_name_objc_classrefs ||
1300 section_name == g_sect_name_objc_superrefs ||
1301 section_name == g_sect_name_objc_const ||
1302 section_name == g_sect_name_objc_classlist)
1303 {
1304 sect_type = eSectionTypeDataPointers;
1305 }
1306
1307 if (sect_type == eSectionTypeOther)
1308 {
1309 switch (mach_sect_type)
1310 {
1311 // TODO: categorize sections by other flags for regular sections
1312 case SectionTypeRegular:
1313 if (segment_sp->GetName() == g_sect_name_TEXT)
1314 sect_type = eSectionTypeCode;
1315 else if (segment_sp->GetName() == g_sect_name_DATA)
1316 sect_type = eSectionTypeData;
1317 else
1318 sect_type = eSectionTypeOther;
1319 break;
1320 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1321 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1322 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1323 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1324 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1325 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1326 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1327 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1328 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1329 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1330 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1331 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1332 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1333 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1334 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1335 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
1336 default: break;
1337 }
1338 }
1339
1340 SectionSP section_sp(new Section (segment_sp,
1341 module_sp,
1342 this,
1343 ++sectID,
1344 section_name,
1345 sect_type,
1346 sect64.addr - segment_sp->GetFileAddress(),
1347 sect64.size,
1348 sect64.offset,
1349 sect64.offset == 0 ? 0 : sect64.size,
1350 sect64.flags));
1351 // Set the section to be encrypted to match the segment
1352
1353 bool section_is_encrypted = false;
1354 if (!segment_is_encrypted && load_cmd.filesize != 0)
1355 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1356
1357 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1358 segment_sp->GetChildren().AddSection(section_sp);
1359
1360 if (segment_sp->IsFake())
1361 {
1362 segment_sp.reset();
1363 const_segname.Clear();
1364 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365 }
1366 }
Greg Clayton3046e662013-07-10 01:23:25 +00001367 if (segment_sp && is_dsym)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001368 {
Greg Clayton3046e662013-07-10 01:23:25 +00001369 if (first_segment_sectID <= sectID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001370 {
Greg Clayton3046e662013-07-10 01:23:25 +00001371 lldb::user_id_t sect_uid;
1372 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001373 {
Greg Clayton3046e662013-07-10 01:23:25 +00001374 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1375 SectionSP next_section_sp;
1376 if (sect_uid + 1 <= sectID)
1377 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1378
1379 if (curr_section_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001380 {
Greg Clayton3046e662013-07-10 01:23:25 +00001381 if (curr_section_sp->GetByteSize() == 0)
1382 {
1383 if (next_section_sp.get() != NULL)
1384 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1385 else
1386 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1387 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001388 }
1389 }
1390 }
1391 }
1392 }
1393 }
1394 }
Greg Clayton3046e662013-07-10 01:23:25 +00001395 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
1396 {
1397 m_dysymtab.cmd = load_cmd.cmd;
1398 m_dysymtab.cmdsize = load_cmd.cmdsize;
1399 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1400 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401
Greg Clayton3046e662013-07-10 01:23:25 +00001402 offset = load_cmd_offset + load_cmd.cmdsize;
1403 }
1404
1405// StreamFile s(stdout, false); // REMOVE THIS LINE
1406// s.Printf ("Sections for %s:\n", m_file.GetPath().c_str());// REMOVE THIS LINE
1407// m_sections_ap->Dump(&s, NULL, true, UINT32_MAX);// REMOVE THIS LINE
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001409}
1410
1411class MachSymtabSectionInfo
1412{
1413public:
1414
1415 MachSymtabSectionInfo (SectionList *section_list) :
1416 m_section_list (section_list),
1417 m_section_infos()
1418 {
1419 // Get the number of sections down to a depth of 1 to include
1420 // all segments and their sections, but no other sections that
1421 // may be added for debug map or
1422 m_section_infos.resize(section_list->GetNumSections(1));
1423 }
1424
1425
Greg Claytone72dfb32012-02-24 01:59:29 +00001426 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001427 GetSection (uint8_t n_sect, addr_t file_addr)
1428 {
1429 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00001430 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001431 if (n_sect < m_section_infos.size())
1432 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001433 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001434 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001435 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1436 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001437 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00001438 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001439 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1440 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00001441 }
1442 else
1443 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001444 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00001445 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001446 }
1447 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00001448 {
1449 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00001450 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001451 }
1452 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1453 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1454 {
1455 // Symbol is in section with zero size, but has the same start
1456 // address as the section. This can happen with linker symbols
1457 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00001458 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001459 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001460 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001461 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001462 }
1463
1464protected:
1465 struct SectionInfo
1466 {
1467 SectionInfo () :
1468 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001469 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001470 {
1471 }
1472
1473 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00001474 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001475 };
1476 SectionList *m_section_list;
1477 std::vector<SectionInfo> m_section_infos;
1478};
1479
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480size_t
Greg Clayton3046e662013-07-10 01:23:25 +00001481ObjectFileMachO::ParseSymtab ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001482{
1483 Timer scoped_timer(__PRETTY_FUNCTION__,
1484 "ObjectFileMachO::ParseSymtab () module = %s",
1485 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001486 ModuleSP module_sp (GetModule());
1487 if (!module_sp)
1488 return 0;
1489
1490 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1491 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1492 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1493 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00001494 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495 uint32_t i;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001496
Greg Clayton5160ce52013-03-27 23:08:40 +00001497 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton77ccca72011-12-30 00:32:24 +00001498
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001499 for (i=0; i<m_header.ncmds; ++i)
1500 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001501 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001503 struct load_command lc;
1504 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001505 break;
1506 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001507 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001509 case LoadCommandSymtab:
1510 symtab_load_command.cmd = lc.cmd;
1511 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001512 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001513 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1514 return 0;
1515 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001516 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001517 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001518 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001519 return 0;
1520 }
1521
1522 if (symtab_load_command.stroff == 0)
1523 {
1524 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001525 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001526 return 0;
1527 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001528
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001529 if (symtab_load_command.nsyms == 0)
1530 {
1531 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001532 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001533 return 0;
1534 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001535
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001536 if (symtab_load_command.strsize == 0)
1537 {
1538 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001539 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001540 return 0;
1541 }
1542 break;
1543
1544 case LoadCommandFunctionStarts:
1545 function_starts_load_command.cmd = lc.cmd;
1546 function_starts_load_command.cmdsize = lc.cmdsize;
1547 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001548 memset (&function_starts_load_command, 0, sizeof(function_starts_load_command));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001549 break;
1550
1551 default:
1552 break;
1553 }
1554 offset = cmd_offset + lc.cmdsize;
1555 }
1556
1557 if (symtab_load_command.cmd)
1558 {
1559 Symtab *symtab = m_symtab_ap.get();
1560 SectionList *section_list = GetSectionList();
1561 if (section_list == NULL)
1562 return 0;
1563
Greg Claytonc7bece562013-01-25 18:06:21 +00001564 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1565 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001566 bool bit_width_32 = addr_byte_size == 4;
1567 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1568
Greg Claytonc7bece562013-01-25 18:06:21 +00001569 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1570 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1571 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00001572 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00001573
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001574 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1575 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00001576 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
Greg Claytonfd814c52013-08-13 01:42:25 +00001577
1578 ProcessSP process_sp (m_process_wp.lock());
1579 Process *process = process_sp.get();
1580
Greg Clayton86eac942013-08-13 21:32:34 +00001581 uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete;
1582
Greg Clayton4c82d422012-05-18 23:20:01 +00001583 if (process)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001584 {
Greg Clayton4c82d422012-05-18 23:20:01 +00001585 Target &target = process->GetTarget();
Greg Claytonfd814c52013-08-13 01:42:25 +00001586
Greg Clayton86eac942013-08-13 21:32:34 +00001587 memory_module_load_level = target.GetMemoryModuleLoadLevel();
Greg Claytonfd814c52013-08-13 01:42:25 +00001588
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001589 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1590 // Reading mach file from memory in a process or core file...
1591
1592 if (linkedit_section_sp)
1593 {
1594 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1595 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1596 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00001597 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00001598
1599 bool data_was_read = false;
1600
1601#if defined (__APPLE__) && defined (__arm__)
1602 if (m_header.flags & 0x80000000u)
Greg Clayton77ccca72011-12-30 00:32:24 +00001603 {
Greg Clayton26b47e22012-04-18 05:19:20 +00001604 // This mach-o memory file is in the dyld shared cache. If this
1605 // program is not remote and this is iOS, then this process will
1606 // share the same shared cache as the process we are debugging and
1607 // we can read the entire __LINKEDIT from the address space in this
1608 // process. This is a needed optimization that is used for local iOS
1609 // debugging only since all shared libraries in the shared cache do
1610 // not have corresponding files that exist in the file system of the
1611 // device. They have been combined into a single file. This means we
1612 // always have to load these files from memory. All of the symbol and
1613 // string tables from all of the __LINKEDIT sections from the shared
1614 // libraries in the shared cache have been merged into a single large
1615 // symbol and string table. Reading all of this symbol and string table
1616 // data across can slow down debug launch times, so we optimize this by
1617 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00001618
1619 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1620 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1621 bool use_lldb_cache = true;
1622 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1623 {
1624 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00001625 ModuleSP module_sp (GetModule());
1626 if (module_sp)
1627 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1628
Jason Molenda0e0954c2013-04-16 06:24:42 +00001629 }
1630
Greg Clayton26b47e22012-04-18 05:19:20 +00001631 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00001632 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00001633 {
1634 data_was_read = true;
1635 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00001636 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00001637 if (function_starts_load_command.cmd)
1638 {
1639 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1640 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1641 }
1642 }
1643 }
1644#endif
1645
1646 if (!data_was_read)
1647 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001648 if (memory_module_load_level == eMemoryModuleLoadLevelComplete)
Jason Molendad34e6522013-02-05 22:31:24 +00001649 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001650 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1651 if (nlist_data_sp)
1652 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
1653 // Load strings individually from memory when loading from memory since shared cache
1654 // string tables contain strings for all symbols from all shared cached libraries
1655 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1656 //if (strtab_data_sp)
1657 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
1658 if (m_dysymtab.nindirectsyms != 0)
1659 {
1660 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1661 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1662 if (indirect_syms_data_sp)
1663 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1664 }
Jason Molendad34e6522013-02-05 22:31:24 +00001665 }
Greg Claytonfd814c52013-08-13 01:42:25 +00001666
1667 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial)
Greg Clayton26b47e22012-04-18 05:19:20 +00001668 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001669 if (function_starts_load_command.cmd)
1670 {
1671 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1672 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1673 if (func_start_data_sp)
1674 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1675 }
Greg Clayton26b47e22012-04-18 05:19:20 +00001676 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001677 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001678 }
1679 }
1680 else
1681 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001682 nlist_data.SetData (m_data,
1683 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001684 nlist_data_byte_size);
1685 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00001686 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001687 strtab_data_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00001688 if (m_dysymtab.nindirectsyms != 0)
1689 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001690 indirect_symbol_index_data.SetData (m_data,
1691 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00001692 m_dysymtab.nindirectsyms * 4);
1693 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001694 if (function_starts_load_command.cmd)
1695 {
1696 function_starts_data.SetData (m_data,
1697 function_starts_load_command.dataoff,
1698 function_starts_load_command.datasize);
1699 }
1700 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001701
Greg Clayton86eac942013-08-13 21:32:34 +00001702 if (nlist_data.GetByteSize() == 0 && memory_module_load_level == eMemoryModuleLoadLevelComplete)
1703 {
1704 if (log)
1705 module_sp->LogMessage(log, "failed to read nlist data");
1706 return 0;
1707 }
1708
1709
Greg Claytondebb8812012-05-25 17:04:00 +00001710 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1711 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00001712 {
Greg Claytondebb8812012-05-25 17:04:00 +00001713 if (process)
1714 {
1715 if (strtab_addr == LLDB_INVALID_ADDRESS)
1716 {
1717 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001718 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00001719 return 0;
1720 }
1721 }
1722 else
Greg Clayton4c82d422012-05-18 23:20:01 +00001723 {
1724 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001725 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00001726 return 0;
1727 }
1728 }
Greg Clayton4c82d422012-05-18 23:20:01 +00001729
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001730 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1731 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1732 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1733 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1734 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1735 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1736 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1737 SectionSP eh_frame_section_sp;
1738 if (text_section_sp.get())
1739 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1740 else
1741 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1742
Greg Clayton29e08cb2012-03-14 01:53:24 +00001743 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molenda5635f772013-03-21 03:36:01 +00001744
1745 // lldb works best if it knows the start addresss of all functions in a module.
1746 // Linker symbols or debug info are normally the best source of information for start addr / size but
1747 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00001748 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00001749 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1750 // binary, relative to the text section.
1751 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1752 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1753 // Binaries built to run on older releases may need to use eh_frame information.
1754
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001755 if (text_section_sp && function_starts_data.GetByteSize())
1756 {
1757 FunctionStarts::Entry function_start_entry;
1758 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00001759 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001760 function_start_entry.addr = text_section_sp->GetFileAddress();
1761 uint64_t delta;
1762 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1763 {
1764 // Now append the current entry
1765 function_start_entry.addr += delta;
1766 function_starts.Append(function_start_entry);
1767 }
Jason Molendad63d3c72013-04-16 00:18:44 +00001768 }
Jason Molenda5635f772013-03-21 03:36:01 +00001769 else
1770 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00001771 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1772 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1773 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1774 // the module.
1775 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00001776 {
1777 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1778 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1779 eh_frame.GetFunctionAddressAndSizeVector (functions);
1780 addr_t text_base_addr = text_section_sp->GetFileAddress();
1781 size_t count = functions.GetSize();
1782 for (size_t i = 0; i < count; ++i)
1783 {
1784 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1785 if (func)
1786 {
1787 FunctionStarts::Entry function_start_entry;
1788 function_start_entry.addr = func->base - text_base_addr;
1789 function_starts.Append(function_start_entry);
1790 }
1791 }
1792 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001793 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001794
Greg Claytonc7bece562013-01-25 18:06:21 +00001795 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001796
Greg Claytonc7bece562013-01-25 18:06:21 +00001797 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 +00001798
Greg Claytonc7bece562013-01-25 18:06:21 +00001799 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001800
1801 uint32_t N_SO_index = UINT32_MAX;
1802
1803 MachSymtabSectionInfo section_info (section_list);
1804 std::vector<uint32_t> N_FUN_indexes;
1805 std::vector<uint32_t> N_NSYM_indexes;
1806 std::vector<uint32_t> N_INCL_indexes;
1807 std::vector<uint32_t> N_BRAC_indexes;
1808 std::vector<uint32_t> N_COMM_indexes;
1809 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1810 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00001811 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001812 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1813 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00001814 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001815 // Any symbols that get merged into another will get an entry
1816 // in this map so we know
1817 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1818 uint32_t nlist_idx = 0;
1819 Symbol *symbol_ptr = NULL;
1820
1821 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00001822 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00001823 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00001824 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00001825 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001826
Jason Molendaa5609c82012-06-21 01:51:02 +00001827#if defined (__APPLE__) && defined (__arm__)
1828
1829 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1830 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1831 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1832 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1833 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1834 // nlist parser to ignore all LOCAL symbols.
1835
1836 if (m_header.flags & 0x80000000u)
1837 {
1838 // Before we can start mapping the DSC, we need to make certain the target process is actually
1839 // using the cache we can find.
1840
Jason Molendaa5609c82012-06-21 01:51:02 +00001841 // Next we need to determine the correct path for the dyld shared cache.
1842
1843 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1844 char dsc_path[PATH_MAX];
1845
1846 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00001847 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1848 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00001849 header_arch.GetArchitectureName());
1850
1851 FileSpec dsc_filespec(dsc_path, false);
1852
1853 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molendad63d3c72013-04-16 00:18:44 +00001854 struct lldb_copy_dyld_cache_header_v0
Greg Clayton946f8902012-09-05 22:30:51 +00001855 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001856 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1857 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1858 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda4e7511e2013-03-06 23:19:17 +00001859 uint32_t imagesOffset;
1860 uint32_t imagesCount;
1861 uint64_t dyldBaseAddress;
1862 uint64_t codeSignatureOffset;
1863 uint64_t codeSignatureSize;
1864 uint64_t slideInfoOffset;
1865 uint64_t slideInfoSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00001866 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1867 uint64_t localSymbolsSize; // size of local symbols information
1868 };
1869 struct lldb_copy_dyld_cache_header_v1
1870 {
1871 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1872 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1873 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1874 uint32_t imagesOffset;
1875 uint32_t imagesCount;
1876 uint64_t dyldBaseAddress;
1877 uint64_t codeSignatureOffset;
1878 uint64_t codeSignatureSize;
1879 uint64_t slideInfoOffset;
1880 uint64_t slideInfoSize;
Jason Molenda4e7511e2013-03-06 23:19:17 +00001881 uint64_t localSymbolsOffset;
1882 uint64_t localSymbolsSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00001883 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Clayton946f8902012-09-05 22:30:51 +00001884 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00001885
Jason Molendad63d3c72013-04-16 00:18:44 +00001886 struct lldb_copy_dyld_cache_mapping_info
1887 {
1888 uint64_t address;
1889 uint64_t size;
1890 uint64_t fileOffset;
1891 uint32_t maxProt;
1892 uint32_t initProt;
1893 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00001894
Greg Clayton946f8902012-09-05 22:30:51 +00001895 struct lldb_copy_dyld_cache_local_symbols_info
1896 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001897 uint32_t nlistOffset;
1898 uint32_t nlistCount;
1899 uint32_t stringsOffset;
1900 uint32_t stringsSize;
1901 uint32_t entriesOffset;
1902 uint32_t entriesCount;
Greg Clayton946f8902012-09-05 22:30:51 +00001903 };
1904 struct lldb_copy_dyld_cache_local_symbols_entry
1905 {
Jason Molendad63d3c72013-04-16 00:18:44 +00001906 uint32_t dylibOffset;
1907 uint32_t nlistStartIndex;
1908 uint32_t nlistCount;
Greg Clayton946f8902012-09-05 22:30:51 +00001909 };
Jason Molendaa5609c82012-06-21 01:51:02 +00001910
Jason Molendaf8130862012-06-22 03:28:35 +00001911 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1912 The dyld_cache_local_symbols_info structure gives us three things:
1913 1. The start and count of the nlist records in the dyld_shared_cache file
1914 2. The start and size of the strings for these nlist records
1915 3. The start and count of dyld_cache_local_symbols_entry entries
1916
1917 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1918 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00001919 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00001920 and the count of how many nlist records there are for this dylib/framework.
1921 */
1922
Jason Molendaa5609c82012-06-21 01:51:02 +00001923 // Process the dsc header to find the unmapped symbols
1924 //
1925 // Save some VM space, do not map the entire cache in one shot.
1926
Jason Molenda255f9bb2013-03-06 23:17:36 +00001927 DataBufferSP dsc_data_sp;
1928 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1929
1930 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00001931 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001932 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00001933
Jason Molenda255f9bb2013-03-06 23:17:36 +00001934 char version_str[17];
1935 int version = -1;
1936 lldb::offset_t offset = 0;
1937 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1938 version_str[16] = '\0';
1939 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1940 {
1941 int v;
1942 if (::sscanf (version_str + 6, "%d", &v) == 1)
1943 {
1944 version = v;
1945 }
1946 }
1947
Jason Molenda0e0954c2013-04-16 06:24:42 +00001948 UUID dsc_uuid;
1949 if (version >= 1)
1950 {
1951 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1952 uint8_t uuid_bytes[sizeof (uuid_t)];
1953 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1954 dsc_uuid.SetBytes (uuid_bytes);
1955 }
1956
1957 bool uuid_match = true;
1958 if (dsc_uuid.IsValid() && process)
1959 {
1960 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1961
1962 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1963 {
1964 // The on-disk dyld_shared_cache file is not the same as the one in this
1965 // process' memory, don't use it.
1966 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00001967 ModuleSP module_sp (GetModule());
1968 if (module_sp)
1969 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 +00001970 }
1971 }
1972
Jason Molenda4e7511e2013-03-06 23:19:17 +00001973 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00001974
Jason Molendaa5609c82012-06-21 01:51:02 +00001975 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1976
1977 // If the mappingOffset points to a location inside the header, we've
1978 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda0e0954c2013-04-16 06:24:42 +00001979 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendaa5609c82012-06-21 01:51:02 +00001980 {
1981
Jason Molenda255f9bb2013-03-06 23:17:36 +00001982 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1983 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1984 offset = 0;
1985
1986 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1987 // in the shared library cache need to be adjusted by an offset to match up with the
1988 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1989 // recorded in mapping_offset_value.
1990 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1991
1992 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00001993 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1994 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1995
Jason Molenda4e7511e2013-03-06 23:19:17 +00001996 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00001997 {
1998 // Map the local symbols
Jason Molenda4e7511e2013-03-06 23:19:17 +00001999 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00002000 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002001 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002002
2003 offset = 0;
2004
2005 // Read the local_symbols_infos struct in one shot
2006 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
2007 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
2008
Jason Molendaa5609c82012-06-21 01:51:02 +00002009 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
2010
Jason Molenda255f9bb2013-03-06 23:17:36 +00002011 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002012
2013 offset = local_symbols_info.entriesOffset;
2014 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
2015 {
2016 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
2017 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
2018 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
2019 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
2020
Jason Molenda4e7511e2013-03-06 23:19:17 +00002021 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00002022 {
2023 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
2024
2025 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
2026 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
2027 num_syms = symtab->GetNumSymbols();
2028
2029 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
2030 uint32_t string_table_offset = local_symbols_info.stringsOffset;
2031
Jason Molenda4e7511e2013-03-06 23:19:17 +00002032 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00002033 {
2034 /////////////////////////////
2035 {
2036 struct nlist_64 nlist;
2037 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2038 break;
2039
2040 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2041 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2042 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2043 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2044 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2045
2046 SymbolType type = eSymbolTypeInvalid;
2047 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2048
2049 if (symbol_name == NULL)
2050 {
2051 // No symbol should be NULL, even the symbols with no
2052 // string values should have an offset zero which points
2053 // to an empty C-string
2054 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002055 "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 +00002056 entry_index,
2057 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002058 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00002059 continue;
2060 }
2061 if (symbol_name[0] == '\0')
2062 symbol_name = NULL;
2063
2064 const char *symbol_name_non_abi_mangled = NULL;
2065
2066 SectionSP symbol_section;
2067 uint32_t symbol_byte_size = 0;
2068 bool add_nlist = true;
2069 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002070 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002071 bool is_gsym = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002072
2073 assert (sym_idx < num_syms);
2074
2075 sym[sym_idx].SetDebug (is_debug);
2076
2077 if (is_debug)
2078 {
2079 switch (nlist.n_type)
2080 {
2081 case StabGlobalSymbol:
2082 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2083 // Sometimes the N_GSYM value contains the address.
2084
2085 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2086 // have the same address, but we want to ensure that we always find only the real symbol,
2087 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2088 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2089 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2090 // same address.
2091
2092 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2093 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2094 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2095 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2096 add_nlist = false;
2097 else
2098 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002099 is_gsym = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002100 sym[sym_idx].SetExternal(true);
2101 if (nlist.n_value != 0)
2102 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2103 type = eSymbolTypeData;
2104 }
2105 break;
2106
2107 case StabFunctionName:
2108 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2109 type = eSymbolTypeCompiler;
2110 break;
2111
2112 case StabFunction:
2113 // N_FUN -- procedure: name,,n_sect,linenumber,address
2114 if (symbol_name)
2115 {
2116 type = eSymbolTypeCode;
2117 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2118
2119 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2120 // We use the current number of symbols in the symbol table in lieu of
2121 // using nlist_idx in case we ever start trimming entries out
2122 N_FUN_indexes.push_back(sym_idx);
2123 }
2124 else
2125 {
2126 type = eSymbolTypeCompiler;
2127
2128 if ( !N_FUN_indexes.empty() )
2129 {
2130 // Copy the size of the function into the original STAB entry so we don't have
2131 // to hunt for it later
2132 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2133 N_FUN_indexes.pop_back();
2134 // We don't really need the end function STAB as it contains the size which
2135 // we already placed with the original symbol, so don't add it if we want a
2136 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002137 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002138 }
2139 }
2140 break;
2141
2142 case StabStaticSymbol:
2143 // N_STSYM -- static symbol: name,,n_sect,type,address
2144 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2145 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2146 type = eSymbolTypeData;
2147 break;
2148
2149 case StabLocalCommon:
2150 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2151 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2152 type = eSymbolTypeCommonBlock;
2153 break;
2154
2155 case StabBeginSymbol:
2156 // N_BNSYM
2157 // We use the current number of symbols in the symbol table in lieu of
2158 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00002159 // Skip these if we want minimal symbol tables
2160 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002161 break;
2162
2163 case StabEndSymbol:
2164 // N_ENSYM
2165 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2166 // so that we can always skip the entire symbol if we need to navigate
2167 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00002168 // Skip these if we want minimal symbol tables
2169 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002170 break;
2171
2172
2173 case StabSourceFileOptions:
2174 // N_OPT - emitted with gcc2_compiled and in gcc source
2175 type = eSymbolTypeCompiler;
2176 break;
2177
2178 case StabRegisterSymbol:
2179 // N_RSYM - register sym: name,,NO_SECT,type,register
2180 type = eSymbolTypeVariable;
2181 break;
2182
2183 case StabSourceLine:
2184 // N_SLINE - src line: 0,,n_sect,linenumber,address
2185 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2186 type = eSymbolTypeLineEntry;
2187 break;
2188
2189 case StabStructureType:
2190 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2191 type = eSymbolTypeVariableType;
2192 break;
2193
2194 case StabSourceFileName:
2195 // N_SO - source file name
2196 type = eSymbolTypeSourceFile;
2197 if (symbol_name == NULL)
2198 {
Greg Clayton3046e662013-07-10 01:23:25 +00002199 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002200 if (N_SO_index != UINT32_MAX)
2201 {
2202 // Set the size of the N_SO to the terminating index of this N_SO
2203 // so that we can always skip the entire N_SO if we need to navigate
2204 // more quickly at the source level when parsing STABS
2205 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00002206 symbol_ptr->SetByteSize(sym_idx);
Jason Molendaa5609c82012-06-21 01:51:02 +00002207 symbol_ptr->SetSizeIsSibling(true);
2208 }
2209 N_NSYM_indexes.clear();
2210 N_INCL_indexes.clear();
2211 N_BRAC_indexes.clear();
2212 N_COMM_indexes.clear();
2213 N_FUN_indexes.clear();
2214 N_SO_index = UINT32_MAX;
2215 }
2216 else
2217 {
2218 // We use the current number of symbols in the symbol table in lieu of
2219 // using nlist_idx in case we ever start trimming entries out
2220 const bool N_SO_has_full_path = symbol_name[0] == '/';
2221 if (N_SO_has_full_path)
2222 {
Greg Clayton3046e662013-07-10 01:23:25 +00002223 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002224 {
2225 // We have two consecutive N_SO entries where the first contains a directory
2226 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00002227 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002228 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2229 add_nlist = false;
2230 }
2231 else
2232 {
2233 // This is the first entry in a N_SO that contains a directory or
2234 // a full path to the source file
2235 N_SO_index = sym_idx;
2236 }
2237 }
Greg Clayton3046e662013-07-10 01:23:25 +00002238 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002239 {
2240 // This is usually the second N_SO entry that contains just the filename,
2241 // so here we combine it with the first one if we are minimizing the symbol table
2242 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2243 if (so_path && so_path[0])
2244 {
2245 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00002246 const size_t double_slash_pos = full_so_path.find("//");
2247 if (double_slash_pos != std::string::npos)
2248 {
2249 // The linker has been generating bad N_SO entries with doubled up paths
2250 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2251 // and the second is the directory for the source file so you end up with
2252 // a path that looks like "/tmp/src//tmp/src/"
2253 FileSpec so_dir(so_path, false);
2254 if (!so_dir.Exists())
2255 {
2256 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2257 if (so_dir.Exists())
2258 {
2259 // Trim off the incorrect path
2260 full_so_path.erase(0, double_slash_pos + 1);
2261 }
2262 }
2263 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002264 if (*full_so_path.rbegin() != '/')
2265 full_so_path += '/';
2266 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00002267 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002268 add_nlist = false;
2269 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2270 }
2271 }
Greg Clayton946f8902012-09-05 22:30:51 +00002272 else
2273 {
2274 // This could be a relative path to a N_SO
2275 N_SO_index = sym_idx;
2276 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002277 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002278 break;
2279
2280 case StabObjectFileName:
2281 // N_OSO - object file name: name,,0,0,st_mtime
2282 type = eSymbolTypeObjectFile;
2283 break;
2284
2285 case StabLocalSymbol:
2286 // N_LSYM - local sym: name,,NO_SECT,type,offset
2287 type = eSymbolTypeLocal;
2288 break;
2289
2290 //----------------------------------------------------------------------
2291 // INCL scopes
2292 //----------------------------------------------------------------------
2293 case StabBeginIncludeFileName:
2294 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2295 // We use the current number of symbols in the symbol table in lieu of
2296 // using nlist_idx in case we ever start trimming entries out
2297 N_INCL_indexes.push_back(sym_idx);
2298 type = eSymbolTypeScopeBegin;
2299 break;
2300
2301 case StabEndIncludeFile:
2302 // N_EINCL - include file end: name,,NO_SECT,0,0
2303 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2304 // so that we can always skip the entire symbol if we need to navigate
2305 // more quickly at the source level when parsing STABS
2306 if ( !N_INCL_indexes.empty() )
2307 {
2308 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2309 symbol_ptr->SetByteSize(sym_idx + 1);
2310 symbol_ptr->SetSizeIsSibling(true);
2311 N_INCL_indexes.pop_back();
2312 }
2313 type = eSymbolTypeScopeEnd;
2314 break;
2315
2316 case StabIncludeFileName:
2317 // N_SOL - #included file name: name,,n_sect,0,address
2318 type = eSymbolTypeHeaderFile;
2319
2320 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00002321 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002322 break;
2323
2324 case StabCompilerParameters:
2325 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2326 type = eSymbolTypeCompiler;
2327 break;
2328
2329 case StabCompilerVersion:
2330 // N_VERSION - compiler version: name,,NO_SECT,0,0
2331 type = eSymbolTypeCompiler;
2332 break;
2333
2334 case StabCompilerOptLevel:
2335 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2336 type = eSymbolTypeCompiler;
2337 break;
2338
2339 case StabParameter:
2340 // N_PSYM - parameter: name,,NO_SECT,type,offset
2341 type = eSymbolTypeVariable;
2342 break;
2343
2344 case StabAlternateEntry:
2345 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2346 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2347 type = eSymbolTypeLineEntry;
2348 break;
2349
2350 //----------------------------------------------------------------------
2351 // Left and Right Braces
2352 //----------------------------------------------------------------------
2353 case StabLeftBracket:
2354 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2355 // We use the current number of symbols in the symbol table in lieu of
2356 // using nlist_idx in case we ever start trimming entries out
2357 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2358 N_BRAC_indexes.push_back(sym_idx);
2359 type = eSymbolTypeScopeBegin;
2360 break;
2361
2362 case StabRightBracket:
2363 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2364 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2365 // so that we can always skip the entire symbol if we need to navigate
2366 // more quickly at the source level when parsing STABS
2367 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2368 if ( !N_BRAC_indexes.empty() )
2369 {
2370 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2371 symbol_ptr->SetByteSize(sym_idx + 1);
2372 symbol_ptr->SetSizeIsSibling(true);
2373 N_BRAC_indexes.pop_back();
2374 }
2375 type = eSymbolTypeScopeEnd;
2376 break;
2377
2378 case StabDeletedIncludeFile:
2379 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2380 type = eSymbolTypeHeaderFile;
2381 break;
2382
2383 //----------------------------------------------------------------------
2384 // COMM scopes
2385 //----------------------------------------------------------------------
2386 case StabBeginCommon:
2387 // N_BCOMM - begin common: name,,NO_SECT,0,0
2388 // We use the current number of symbols in the symbol table in lieu of
2389 // using nlist_idx in case we ever start trimming entries out
2390 type = eSymbolTypeScopeBegin;
2391 N_COMM_indexes.push_back(sym_idx);
2392 break;
2393
2394 case StabEndCommonLocal:
2395 // N_ECOML - end common (local name): 0,,n_sect,0,address
2396 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2397 // Fall through
2398
2399 case StabEndCommon:
2400 // N_ECOMM - end common: name,,n_sect,0,0
2401 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2402 // so that we can always skip the entire symbol if we need to navigate
2403 // more quickly at the source level when parsing STABS
2404 if ( !N_COMM_indexes.empty() )
2405 {
2406 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2407 symbol_ptr->SetByteSize(sym_idx + 1);
2408 symbol_ptr->SetSizeIsSibling(true);
2409 N_COMM_indexes.pop_back();
2410 }
2411 type = eSymbolTypeScopeEnd;
2412 break;
2413
2414 case StabLength:
2415 // N_LENG - second stab entry with length information
2416 type = eSymbolTypeAdditional;
2417 break;
2418
2419 default: break;
2420 }
2421 }
2422 else
2423 {
2424 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2425 uint8_t n_type = NlistMaskType & nlist.n_type;
2426 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2427
2428 switch (n_type)
2429 {
2430 case NListTypeIndirect: // N_INDR - Fall through
2431 case NListTypePreboundUndefined:// N_PBUD - Fall through
2432 case NListTypeUndefined: // N_UNDF
2433 type = eSymbolTypeUndefined;
2434 break;
2435
2436 case NListTypeAbsolute: // N_ABS
2437 type = eSymbolTypeAbsolute;
2438 break;
2439
2440 case NListTypeSection: // N_SECT
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002441 {
2442 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002443
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002444 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00002445 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002446 // TODO: warn about this?
2447 add_nlist = false;
2448 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002449 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002450
2451 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00002452 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002453 type = eSymbolTypeException;
2454 }
2455 else
2456 {
2457 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002458
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002459 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00002460 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002461 case SectionTypeRegular: break; // regular section
2462 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2463 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2464 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2465 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2466 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2467 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2468 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2469 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2470 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2471 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2472 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2473 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2474 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2475 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2476 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2477 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2478 default: break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002479 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002480
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002481 if (type == eSymbolTypeInvalid)
2482 {
2483 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2484 if (symbol_section->IsDescendant (text_section_sp.get()))
2485 {
2486 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2487 SectionAttrUserSelfModifyingCode |
2488 SectionAttrSytemSomeInstructions))
2489 type = eSymbolTypeData;
2490 else
2491 type = eSymbolTypeCode;
2492 }
2493 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00002494 {
2495 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2496 {
2497 type = eSymbolTypeRuntime;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002498
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002499 if (symbol_name &&
2500 symbol_name[0] == '_' &&
2501 symbol_name[1] == 'O' &&
Jason Molendaa5609c82012-06-21 01:51:02 +00002502 symbol_name[2] == 'B')
2503 {
2504 llvm::StringRef symbol_name_ref(symbol_name);
2505 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2506 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2507 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2508 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2509 {
2510 symbol_name_non_abi_mangled = symbol_name + 1;
2511 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2512 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002513 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002514 }
2515 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2516 {
2517 symbol_name_non_abi_mangled = symbol_name + 1;
2518 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2519 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002520 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002521 }
2522 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2523 {
2524 symbol_name_non_abi_mangled = symbol_name + 1;
2525 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2526 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002527 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002528 }
2529 }
2530 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002531 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00002532 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002533 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00002534 }
2535 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002536 {
2537 type = eSymbolTypeData;
2538 }
2539 }
2540 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2541 {
2542 type = eSymbolTypeTrampoline;
2543 }
2544 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2545 {
2546 type = eSymbolTypeRuntime;
2547 if (symbol_name && symbol_name[0] == '.')
2548 {
2549 llvm::StringRef symbol_name_ref(symbol_name);
2550 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2551 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00002552 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002553 symbol_name_non_abi_mangled = symbol_name;
2554 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2555 type = eSymbolTypeObjCClass;
2556 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002557 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002558 }
2559 }
2560 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002561 }
2562 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002563 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002564 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002565 }
2566
2567 if (add_nlist)
2568 {
2569 uint64_t symbol_value = nlist.n_value;
Jason Molendaa5609c82012-06-21 01:51:02 +00002570 if (symbol_name_non_abi_mangled)
2571 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00002572 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2573 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00002574 }
2575 else
2576 {
Greg Clayton3046e662013-07-10 01:23:25 +00002577 bool symbol_name_is_mangled = false;
2578
Jason Molendaa5609c82012-06-21 01:51:02 +00002579 if (symbol_name && symbol_name[0] == '_')
2580 {
2581 symbol_name_is_mangled = symbol_name[1] == '_';
2582 symbol_name++; // Skip the leading underscore
2583 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002584
Jason Molendaa5609c82012-06-21 01:51:02 +00002585 if (symbol_name)
2586 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002587 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00002588 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00002589 if (is_gsym && is_debug)
2590 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
Jason Molendaa5609c82012-06-21 01:51:02 +00002591 }
2592 }
2593 if (symbol_section)
2594 {
2595 const addr_t section_file_addr = symbol_section->GetFileAddress();
2596 if (symbol_byte_size == 0 && function_starts_count > 0)
2597 {
2598 addr_t symbol_lookup_file_addr = nlist.n_value;
2599 // Do an exact address match for non-ARM addresses, else get the closest since
2600 // the symbol might be a thumb symbol which has an address with bit zero set
2601 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2602 if (is_arm && func_start_entry)
2603 {
2604 // Verify that the function start address is the symbol address (ARM)
2605 // or the symbol address + 1 (thumb)
2606 if (func_start_entry->addr != symbol_lookup_file_addr &&
2607 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2608 {
2609 // Not the right entry, NULL it out...
2610 func_start_entry = NULL;
2611 }
2612 }
2613 if (func_start_entry)
2614 {
2615 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002616
Jason Molendaa5609c82012-06-21 01:51:02 +00002617 addr_t symbol_file_addr = func_start_entry->addr;
2618 uint32_t symbol_flags = 0;
2619 if (is_arm)
2620 {
2621 if (symbol_file_addr & 1)
2622 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2623 symbol_file_addr &= 0xfffffffffffffffeull;
2624 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002625
Jason Molendaa5609c82012-06-21 01:51:02 +00002626 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2627 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2628 if (next_func_start_entry)
2629 {
2630 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2631 // Be sure the clear the Thumb address bit when we calculate the size
2632 // from the current and next address
2633 if (is_arm)
2634 next_symbol_file_addr &= 0xfffffffffffffffeull;
2635 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2636 }
2637 else
2638 {
2639 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2640 }
2641 }
2642 }
2643 symbol_value -= section_file_addr;
2644 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002645
Greg Claytondacc4a92013-05-14 22:19:37 +00002646 if (is_debug == false)
2647 {
2648 if (type == eSymbolTypeCode)
2649 {
2650 // See if we can find a N_FUN entry for any code symbols.
2651 // If we do find a match, and the name matches, then we
2652 // can merge the two into just the function symbol to avoid
2653 // duplicate entries in the symbol table
2654 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2655 if (pos != N_FUN_addr_to_sym_idx.end())
2656 {
Greg Clayton3046e662013-07-10 01:23:25 +00002657 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
Greg Claytondacc4a92013-05-14 22:19:37 +00002658 {
2659 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2660 // We just need the flags from the linker symbol, so put these flags
2661 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2662 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2663 sym[sym_idx].Clear();
2664 continue;
2665 }
2666 }
2667 }
2668 else if (type == eSymbolTypeData)
2669 {
2670 // See if we can find a N_STSYM entry for any data symbols.
2671 // If we do find a match, and the name matches, then we
2672 // can merge the two into just the Static symbol to avoid
2673 // duplicate entries in the symbol table
2674 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2675 if (pos != N_STSYM_addr_to_sym_idx.end())
2676 {
Greg Clayton3046e662013-07-10 01:23:25 +00002677 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
Greg Claytondacc4a92013-05-14 22:19:37 +00002678 {
2679 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2680 // We just need the flags from the linker symbol, so put these flags
2681 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2682 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2683 sym[sym_idx].Clear();
2684 continue;
2685 }
2686 }
2687 else
2688 {
2689 // Combine N_GSYM stab entries with the non stab symbol
Greg Clayton3046e662013-07-10 01:23:25 +00002690 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 +00002691 if (pos != N_GSYM_name_to_sym_idx.end())
2692 {
2693 const uint32_t GSYM_sym_idx = pos->second;
2694 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
2695 // Copy the address, because often the N_GSYM address has an invalid address of zero
2696 // when the global is a common symbol
2697 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
2698 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
2699 // We just need the flags from the linker symbol, so put these flags
2700 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2701 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2702 sym[sym_idx].Clear();
2703 continue;
2704 }
2705 }
2706 }
2707 }
2708
Jason Molendaa5609c82012-06-21 01:51:02 +00002709 sym[sym_idx].SetID (nlist_idx);
2710 sym[sym_idx].SetType (type);
2711 sym[sym_idx].GetAddress().SetSection (symbol_section);
2712 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2713 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002714
Jason Molendaa5609c82012-06-21 01:51:02 +00002715 if (symbol_byte_size > 0)
2716 sym[sym_idx].SetByteSize(symbol_byte_size);
2717
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002718 if (demangled_is_synthesized)
2719 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00002720 ++sym_idx;
2721 }
2722 else
2723 {
2724 sym[sym_idx].Clear();
2725 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002726
Jason Molendaa5609c82012-06-21 01:51:02 +00002727 }
2728 /////////////////////////////
2729 }
2730 break; // No more entries to consider
2731 }
2732 }
2733 }
2734 }
2735 }
2736 }
2737 }
2738
2739 // Must reset this in case it was mutated above!
2740 nlist_data_offset = 0;
2741#endif
Greg Claytonfd814c52013-08-13 01:42:25 +00002742
2743 if (nlist_data.GetByteSize() > 0)
Jason Molendaa5609c82012-06-21 01:51:02 +00002744 {
Jason Molendaa5609c82012-06-21 01:51:02 +00002745
Greg Claytonfd814c52013-08-13 01:42:25 +00002746 // If the sym array was not created while parsing the DSC unmapped
2747 // symbols, create it now.
2748 if (sym == NULL)
Greg Clayton4c82d422012-05-18 23:20:01 +00002749 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002750 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2751 num_syms = symtab->GetNumSymbols();
2752 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002753
Greg Claytonfd814c52013-08-13 01:42:25 +00002754 if (unmapped_local_symbols_found)
2755 {
2756 assert(m_dysymtab.ilocalsym == 0);
2757 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2758 nlist_idx = m_dysymtab.nlocalsym;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002759 }
Greg Claytondebb8812012-05-25 17:04:00 +00002760 else
2761 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002762 nlist_idx = 0;
Greg Claytondebb8812012-05-25 17:04:00 +00002763 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002764
Greg Claytonfd814c52013-08-13 01:42:25 +00002765 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002766 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002767 struct nlist_64 nlist;
2768 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2769 break;
2770
2771 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2772 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2773 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2774 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2775 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2776
2777 SymbolType type = eSymbolTypeInvalid;
2778 const char *symbol_name = NULL;
2779
2780 if (have_strtab_data)
Greg Clayton77ccca72011-12-30 00:32:24 +00002781 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002782 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002783
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002784 if (symbol_name == NULL)
2785 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002786 // No symbol should be NULL, even the symbols with no
2787 // string values should have an offset zero which points
2788 // to an empty C-string
2789 Host::SystemLog (Host::eSystemLogError,
2790 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
2791 nlist_idx,
2792 nlist.n_strx,
2793 module_sp->GetFileSpec().GetPath().c_str());
2794 continue;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002795 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002796 if (symbol_name[0] == '\0')
2797 symbol_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002798 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002799 else
2800 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002801 const addr_t str_addr = strtab_addr + nlist.n_strx;
2802 Error str_error;
2803 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2804 symbol_name = memory_symbol_name.c_str();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002805 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002806 const char *symbol_name_non_abi_mangled = NULL;
2807
2808 SectionSP symbol_section;
2809 lldb::addr_t symbol_byte_size = 0;
2810 bool add_nlist = true;
2811 bool is_gsym = false;
2812 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
2813 bool demangled_is_synthesized = false;
2814
2815 assert (sym_idx < num_syms);
2816
2817 sym[sym_idx].SetDebug (is_debug);
2818
2819 if (is_debug)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002820 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002821 switch (nlist.n_type)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002822 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002823 case StabGlobalSymbol:
2824 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2825 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002826
Greg Claytonfd814c52013-08-13 01:42:25 +00002827 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2828 // have the same address, but we want to ensure that we always find only the real symbol,
2829 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2830 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2831 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2832 // same address.
Greg Clayton29e08cb2012-03-14 01:53:24 +00002833
Greg Claytonfd814c52013-08-13 01:42:25 +00002834 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2835 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2836 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2837 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2838 add_nlist = false;
2839 else
2840 {
2841 is_gsym = true;
2842 sym[sym_idx].SetExternal(true);
2843 if (nlist.n_value != 0)
2844 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2845 type = eSymbolTypeData;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002846 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002847 break;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002848
Greg Claytonfd814c52013-08-13 01:42:25 +00002849 case StabFunctionName:
2850 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2851 type = eSymbolTypeCompiler;
2852 break;
2853
2854 case StabFunction:
2855 // N_FUN -- procedure: name,,n_sect,linenumber,address
2856 if (symbol_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00002857 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002858 type = eSymbolTypeCode;
2859 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2860
2861 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2862 // We use the current number of symbols in the symbol table in lieu of
2863 // using nlist_idx in case we ever start trimming entries out
2864 N_FUN_indexes.push_back(sym_idx);
Greg Claytondacc4a92013-05-14 22:19:37 +00002865 }
2866 else
2867 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002868 type = eSymbolTypeCompiler;
2869
2870 if ( !N_FUN_indexes.empty() )
Greg Claytondacc4a92013-05-14 22:19:37 +00002871 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002872 // Copy the size of the function into the original STAB entry so we don't have
2873 // to hunt for it later
2874 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2875 N_FUN_indexes.pop_back();
2876 // We don't really need the end function STAB as it contains the size which
2877 // we already placed with the original symbol, so don't add it if we want a
2878 // minimal symbol table
2879 add_nlist = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002880 }
2881 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002882 break;
2883
2884 case StabStaticSymbol:
2885 // N_STSYM -- static symbol: name,,n_sect,type,address
2886 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2887 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2888 type = eSymbolTypeData;
2889 break;
2890
2891 case StabLocalCommon:
2892 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2893 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2894 type = eSymbolTypeCommonBlock;
2895 break;
2896
2897 case StabBeginSymbol:
2898 // N_BNSYM
2899 // We use the current number of symbols in the symbol table in lieu of
2900 // using nlist_idx in case we ever start trimming entries out
2901 // Skip these if we want minimal symbol tables
2902 add_nlist = false;
2903 break;
2904
2905 case StabEndSymbol:
2906 // N_ENSYM
2907 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2908 // so that we can always skip the entire symbol if we need to navigate
2909 // more quickly at the source level when parsing STABS
2910 // Skip these if we want minimal symbol tables
2911 add_nlist = false;
2912 break;
2913
2914
2915 case StabSourceFileOptions:
2916 // N_OPT - emitted with gcc2_compiled and in gcc source
2917 type = eSymbolTypeCompiler;
2918 break;
2919
2920 case StabRegisterSymbol:
2921 // N_RSYM - register sym: name,,NO_SECT,type,register
2922 type = eSymbolTypeVariable;
2923 break;
2924
2925 case StabSourceLine:
2926 // N_SLINE - src line: 0,,n_sect,linenumber,address
2927 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2928 type = eSymbolTypeLineEntry;
2929 break;
2930
2931 case StabStructureType:
2932 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2933 type = eSymbolTypeVariableType;
2934 break;
2935
2936 case StabSourceFileName:
2937 // N_SO - source file name
2938 type = eSymbolTypeSourceFile;
2939 if (symbol_name == NULL)
2940 {
2941 add_nlist = false;
2942 if (N_SO_index != UINT32_MAX)
2943 {
2944 // Set the size of the N_SO to the terminating index of this N_SO
2945 // so that we can always skip the entire N_SO if we need to navigate
2946 // more quickly at the source level when parsing STABS
2947 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2948 symbol_ptr->SetByteSize(sym_idx);
2949 symbol_ptr->SetSizeIsSibling(true);
2950 }
2951 N_NSYM_indexes.clear();
2952 N_INCL_indexes.clear();
2953 N_BRAC_indexes.clear();
2954 N_COMM_indexes.clear();
2955 N_FUN_indexes.clear();
2956 N_SO_index = UINT32_MAX;
2957 }
2958 else
2959 {
2960 // We use the current number of symbols in the symbol table in lieu of
2961 // using nlist_idx in case we ever start trimming entries out
2962 const bool N_SO_has_full_path = symbol_name[0] == '/';
2963 if (N_SO_has_full_path)
2964 {
2965 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2966 {
2967 // We have two consecutive N_SO entries where the first contains a directory
2968 // and the second contains a full path.
2969 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
2970 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2971 add_nlist = false;
2972 }
2973 else
2974 {
2975 // This is the first entry in a N_SO that contains a directory or
2976 // a full path to the source file
2977 N_SO_index = sym_idx;
2978 }
2979 }
2980 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2981 {
2982 // This is usually the second N_SO entry that contains just the filename,
2983 // so here we combine it with the first one if we are minimizing the symbol table
2984 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2985 if (so_path && so_path[0])
2986 {
2987 std::string full_so_path (so_path);
2988 const size_t double_slash_pos = full_so_path.find("//");
2989 if (double_slash_pos != std::string::npos)
2990 {
2991 // The linker has been generating bad N_SO entries with doubled up paths
2992 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2993 // and the second is the directory for the source file so you end up with
2994 // a path that looks like "/tmp/src//tmp/src/"
2995 FileSpec so_dir(so_path, false);
2996 if (!so_dir.Exists())
2997 {
2998 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2999 if (so_dir.Exists())
3000 {
3001 // Trim off the incorrect path
3002 full_so_path.erase(0, double_slash_pos + 1);
3003 }
3004 }
3005 }
3006 if (*full_so_path.rbegin() != '/')
3007 full_so_path += '/';
3008 full_so_path += symbol_name;
3009 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
3010 add_nlist = false;
3011 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3012 }
3013 }
3014 else
3015 {
3016 // This could be a relative path to a N_SO
3017 N_SO_index = sym_idx;
3018 }
3019 }
3020
3021 break;
3022
3023 case StabObjectFileName:
3024 // N_OSO - object file name: name,,0,0,st_mtime
3025 type = eSymbolTypeObjectFile;
3026 break;
3027
3028 case StabLocalSymbol:
3029 // N_LSYM - local sym: name,,NO_SECT,type,offset
3030 type = eSymbolTypeLocal;
3031 break;
3032
3033 //----------------------------------------------------------------------
3034 // INCL scopes
3035 //----------------------------------------------------------------------
3036 case StabBeginIncludeFileName:
3037 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
3038 // We use the current number of symbols in the symbol table in lieu of
3039 // using nlist_idx in case we ever start trimming entries out
3040 N_INCL_indexes.push_back(sym_idx);
3041 type = eSymbolTypeScopeBegin;
3042 break;
3043
3044 case StabEndIncludeFile:
3045 // N_EINCL - include file end: name,,NO_SECT,0,0
3046 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3047 // so that we can always skip the entire symbol if we need to navigate
3048 // more quickly at the source level when parsing STABS
3049 if ( !N_INCL_indexes.empty() )
3050 {
3051 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3052 symbol_ptr->SetByteSize(sym_idx + 1);
3053 symbol_ptr->SetSizeIsSibling(true);
3054 N_INCL_indexes.pop_back();
3055 }
3056 type = eSymbolTypeScopeEnd;
3057 break;
3058
3059 case StabIncludeFileName:
3060 // N_SOL - #included file name: name,,n_sect,0,address
3061 type = eSymbolTypeHeaderFile;
3062
3063 // We currently don't use the header files on darwin
3064 add_nlist = false;
3065 break;
3066
3067 case StabCompilerParameters:
3068 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
3069 type = eSymbolTypeCompiler;
3070 break;
3071
3072 case StabCompilerVersion:
3073 // N_VERSION - compiler version: name,,NO_SECT,0,0
3074 type = eSymbolTypeCompiler;
3075 break;
3076
3077 case StabCompilerOptLevel:
3078 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
3079 type = eSymbolTypeCompiler;
3080 break;
3081
3082 case StabParameter:
3083 // N_PSYM - parameter: name,,NO_SECT,type,offset
3084 type = eSymbolTypeVariable;
3085 break;
3086
3087 case StabAlternateEntry:
3088 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
3089 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3090 type = eSymbolTypeLineEntry;
3091 break;
3092
3093 //----------------------------------------------------------------------
3094 // Left and Right Braces
3095 //----------------------------------------------------------------------
3096 case StabLeftBracket:
3097 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
3098 // We use the current number of symbols in the symbol table in lieu of
3099 // using nlist_idx in case we ever start trimming entries out
3100 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3101 N_BRAC_indexes.push_back(sym_idx);
3102 type = eSymbolTypeScopeBegin;
3103 break;
3104
3105 case StabRightBracket:
3106 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
3107 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3108 // so that we can always skip the entire symbol if we need to navigate
3109 // more quickly at the source level when parsing STABS
3110 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3111 if ( !N_BRAC_indexes.empty() )
3112 {
3113 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3114 symbol_ptr->SetByteSize(sym_idx + 1);
3115 symbol_ptr->SetSizeIsSibling(true);
3116 N_BRAC_indexes.pop_back();
3117 }
3118 type = eSymbolTypeScopeEnd;
3119 break;
3120
3121 case StabDeletedIncludeFile:
3122 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3123 type = eSymbolTypeHeaderFile;
3124 break;
3125
3126 //----------------------------------------------------------------------
3127 // COMM scopes
3128 //----------------------------------------------------------------------
3129 case StabBeginCommon:
3130 // N_BCOMM - begin common: name,,NO_SECT,0,0
3131 // We use the current number of symbols in the symbol table in lieu of
3132 // using nlist_idx in case we ever start trimming entries out
3133 type = eSymbolTypeScopeBegin;
3134 N_COMM_indexes.push_back(sym_idx);
3135 break;
3136
3137 case StabEndCommonLocal:
3138 // N_ECOML - end common (local name): 0,,n_sect,0,address
3139 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3140 // Fall through
3141
3142 case StabEndCommon:
3143 // N_ECOMM - end common: name,,n_sect,0,0
3144 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3145 // so that we can always skip the entire symbol if we need to navigate
3146 // more quickly at the source level when parsing STABS
3147 if ( !N_COMM_indexes.empty() )
3148 {
3149 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3150 symbol_ptr->SetByteSize(sym_idx + 1);
3151 symbol_ptr->SetSizeIsSibling(true);
3152 N_COMM_indexes.pop_back();
3153 }
3154 type = eSymbolTypeScopeEnd;
3155 break;
3156
3157 case StabLength:
3158 // N_LENG - second stab entry with length information
3159 type = eSymbolTypeAdditional;
3160 break;
3161
3162 default: break;
3163 }
3164 }
3165 else
3166 {
3167 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3168 uint8_t n_type = NlistMaskType & nlist.n_type;
3169 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3170
3171 switch (n_type)
3172 {
3173 case NListTypeIndirect: // N_INDR - Fall through
3174 case NListTypePreboundUndefined:// N_PBUD - Fall through
3175 case NListTypeUndefined: // N_UNDF
3176 type = eSymbolTypeUndefined;
3177 break;
3178
3179 case NListTypeAbsolute: // N_ABS
3180 type = eSymbolTypeAbsolute;
3181 break;
3182
3183 case NListTypeSection: // N_SECT
3184 {
3185 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3186
3187 if (!symbol_section)
3188 {
3189 // TODO: warn about this?
3190 add_nlist = false;
3191 break;
3192 }
3193
3194 if (TEXT_eh_frame_sectID == nlist.n_sect)
3195 {
3196 type = eSymbolTypeException;
3197 }
3198 else
3199 {
3200 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3201
3202 switch (section_type)
3203 {
3204 case SectionTypeRegular: break; // regular section
3205 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3206 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3207 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3208 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3209 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3210 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3211 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3212 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3213 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3214 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3215 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3216 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3217 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3218 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3219 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3220 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3221 default: break;
3222 }
3223
3224 if (type == eSymbolTypeInvalid)
3225 {
3226 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3227 if (symbol_section->IsDescendant (text_section_sp.get()))
3228 {
3229 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3230 SectionAttrUserSelfModifyingCode |
3231 SectionAttrSytemSomeInstructions))
3232 type = eSymbolTypeData;
3233 else
3234 type = eSymbolTypeCode;
3235 }
3236 else
3237 if (symbol_section->IsDescendant(data_section_sp.get()))
3238 {
3239 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
3240 {
3241 type = eSymbolTypeRuntime;
3242
3243 if (symbol_name &&
3244 symbol_name[0] == '_' &&
3245 symbol_name[1] == 'O' &&
3246 symbol_name[2] == 'B')
3247 {
3248 llvm::StringRef symbol_name_ref(symbol_name);
3249 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3250 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3251 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3252 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3253 {
3254 symbol_name_non_abi_mangled = symbol_name + 1;
3255 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3256 type = eSymbolTypeObjCClass;
3257 demangled_is_synthesized = true;
3258 }
3259 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3260 {
3261 symbol_name_non_abi_mangled = symbol_name + 1;
3262 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3263 type = eSymbolTypeObjCMetaClass;
3264 demangled_is_synthesized = true;
3265 }
3266 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3267 {
3268 symbol_name_non_abi_mangled = symbol_name + 1;
3269 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3270 type = eSymbolTypeObjCIVar;
3271 demangled_is_synthesized = true;
3272 }
3273 }
3274 }
3275 else
3276 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3277 {
3278 type = eSymbolTypeException;
3279 }
3280 else
3281 {
3282 type = eSymbolTypeData;
3283 }
3284 }
3285 else
3286 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3287 {
3288 type = eSymbolTypeTrampoline;
3289 }
3290 else
3291 if (symbol_section->IsDescendant(objc_section_sp.get()))
3292 {
3293 type = eSymbolTypeRuntime;
3294 if (symbol_name && symbol_name[0] == '.')
3295 {
3296 llvm::StringRef symbol_name_ref(symbol_name);
3297 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3298 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3299 {
3300 symbol_name_non_abi_mangled = symbol_name;
3301 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3302 type = eSymbolTypeObjCClass;
3303 demangled_is_synthesized = true;
3304 }
3305 }
3306 }
3307 }
3308 }
3309 }
3310 break;
Greg Claytondacc4a92013-05-14 22:19:37 +00003311 }
3312 }
3313
Greg Claytonfd814c52013-08-13 01:42:25 +00003314 if (add_nlist)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003315 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003316 uint64_t symbol_value = nlist.n_value;
3317
3318 if (symbol_name_non_abi_mangled)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003319 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003320 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3321 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
3322 }
3323 else
3324 {
3325 bool symbol_name_is_mangled = false;
3326
3327 if (symbol_name && symbol_name[0] == '_')
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003328 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003329 symbol_name_is_mangled = symbol_name[1] == '_';
3330 symbol_name++; // Skip the leading underscore
3331 }
3332
3333 if (symbol_name)
3334 {
3335 ConstString const_symbol_name(symbol_name);
3336 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
3337 if (is_gsym && is_debug)
3338 {
3339 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
3340 }
3341 }
3342 }
3343 if (symbol_section)
3344 {
3345 const addr_t section_file_addr = symbol_section->GetFileAddress();
3346 if (symbol_byte_size == 0 && function_starts_count > 0)
3347 {
3348 addr_t symbol_lookup_file_addr = nlist.n_value;
3349 // Do an exact address match for non-ARM addresses, else get the closest since
3350 // the symbol might be a thumb symbol which has an address with bit zero set
3351 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3352 if (is_arm && func_start_entry)
3353 {
3354 // Verify that the function start address is the symbol address (ARM)
3355 // or the symbol address + 1 (thumb)
3356 if (func_start_entry->addr != symbol_lookup_file_addr &&
3357 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3358 {
3359 // Not the right entry, NULL it out...
3360 func_start_entry = NULL;
3361 }
3362 }
3363 if (func_start_entry)
3364 {
3365 func_start_entry->data = true;
3366
3367 addr_t symbol_file_addr = func_start_entry->addr;
3368 if (is_arm)
3369 symbol_file_addr &= 0xfffffffffffffffeull;
3370
3371 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3372 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3373 if (next_func_start_entry)
3374 {
3375 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3376 // Be sure the clear the Thumb address bit when we calculate the size
3377 // from the current and next address
3378 if (is_arm)
3379 next_symbol_file_addr &= 0xfffffffffffffffeull;
3380 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3381 }
3382 else
3383 {
3384 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3385 }
3386 }
3387 }
3388 symbol_value -= section_file_addr;
3389 }
3390
3391 if (is_debug == false)
3392 {
3393 if (type == eSymbolTypeCode)
3394 {
3395 // See if we can find a N_FUN entry for any code symbols.
3396 // If we do find a match, and the name matches, then we
3397 // can merge the two into just the function symbol to avoid
3398 // duplicate entries in the symbol table
3399 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3400 if (pos != N_FUN_addr_to_sym_idx.end())
3401 {
3402 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3403 {
3404 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3405 // We just need the flags from the linker symbol, so put these flags
3406 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3407 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3408 sym[sym_idx].Clear();
3409 continue;
3410 }
3411 }
3412 }
3413 else if (type == eSymbolTypeData)
3414 {
3415 // See if we can find a N_STSYM entry for any data symbols.
3416 // If we do find a match, and the name matches, then we
3417 // can merge the two into just the Static symbol to avoid
3418 // duplicate entries in the symbol table
3419 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3420 if (pos != N_STSYM_addr_to_sym_idx.end())
3421 {
3422 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3423 {
3424 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3425 // We just need the flags from the linker symbol, so put these flags
3426 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3427 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3428 sym[sym_idx].Clear();
3429 continue;
3430 }
3431 }
3432 else
3433 {
3434 // Combine N_GSYM stab entries with the non stab symbol
3435 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
3436 if (pos != N_GSYM_name_to_sym_idx.end())
3437 {
3438 const uint32_t GSYM_sym_idx = pos->second;
3439 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3440 // Copy the address, because often the N_GSYM address has an invalid address of zero
3441 // when the global is a common symbol
3442 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3443 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3444 // We just need the flags from the linker symbol, so put these flags
3445 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3446 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3447 sym[sym_idx].Clear();
3448 continue;
3449 }
3450 }
3451 }
3452 }
3453
3454 sym[sym_idx].SetID (nlist_idx);
3455 sym[sym_idx].SetType (type);
3456 sym[sym_idx].GetAddress().SetSection (symbol_section);
3457 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3458 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3459
3460 if (symbol_byte_size > 0)
3461 sym[sym_idx].SetByteSize(symbol_byte_size);
3462
3463 if (demangled_is_synthesized)
3464 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3465
3466 ++sym_idx;
3467 }
3468 else
3469 {
3470 sym[sym_idx].Clear();
3471 }
3472
3473 }
3474
3475 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3476 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3477 // such entries by figuring out what the address for the global is by looking up this non-STAB
3478 // entry and copying the value into the debug symbol's value to save us the hassle in the
3479 // debug symbol parser.
3480
3481 Symbol *global_symbol = NULL;
3482 for (nlist_idx = 0;
3483 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3484 nlist_idx++)
3485 {
3486 if (global_symbol->GetAddress().GetFileAddress() == 0)
3487 {
3488 std::vector<uint32_t> indexes;
3489 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3490 {
3491 std::vector<uint32_t>::const_iterator pos;
3492 std::vector<uint32_t>::const_iterator end = indexes.end();
3493 for (pos = indexes.begin(); pos != end; ++pos)
3494 {
3495 symbol_ptr = symtab->SymbolAtIndex(*pos);
3496 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3497 {
3498 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3499 break;
3500 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003501 }
3502 }
3503 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003504 }
3505 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003506
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003507 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3508
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003509 if (function_starts_count > 0)
3510 {
3511 char synthetic_function_symbol[PATH_MAX];
3512 uint32_t num_synthetic_function_symbols = 0;
3513 for (i=0; i<function_starts_count; ++i)
3514 {
3515 if (function_starts.GetEntryRef (i).data == false)
3516 ++num_synthetic_function_symbols;
3517 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003518
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003519 if (num_synthetic_function_symbols > 0)
3520 {
3521 if (num_syms < sym_idx + num_synthetic_function_symbols)
3522 {
3523 num_syms = sym_idx + num_synthetic_function_symbols;
3524 sym = symtab->Resize (num_syms);
3525 }
3526 uint32_t synthetic_function_symbol_idx = 0;
3527 for (i=0; i<function_starts_count; ++i)
3528 {
3529 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3530 if (func_start_entry->data == false)
3531 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003532 addr_t symbol_file_addr = func_start_entry->addr;
3533 uint32_t symbol_flags = 0;
3534 if (is_arm)
3535 {
3536 if (symbol_file_addr & 1)
3537 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3538 symbol_file_addr &= 0xfffffffffffffffeull;
3539 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003540 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003541 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003542 {
3543 SectionSP symbol_section (symbol_addr.GetSection());
3544 uint32_t symbol_byte_size = 0;
3545 if (symbol_section)
3546 {
3547 const addr_t section_file_addr = symbol_section->GetFileAddress();
3548 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3549 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3550 if (next_func_start_entry)
3551 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003552 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3553 if (is_arm)
3554 next_symbol_file_addr &= 0xfffffffffffffffeull;
3555 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 +00003556 }
3557 else
3558 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003559 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003560 }
3561 snprintf (synthetic_function_symbol,
3562 sizeof(synthetic_function_symbol),
3563 "___lldb_unnamed_function%u$$%s",
3564 ++synthetic_function_symbol_idx,
3565 module_sp->GetFileSpec().GetFilename().GetCString());
3566 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Clayton037520e2012-07-18 23:18:10 +00003567 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003568 sym[sym_idx].SetType (eSymbolTypeCode);
3569 sym[sym_idx].SetIsSynthetic (true);
3570 sym[sym_idx].GetAddress() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003571 if (symbol_flags)
3572 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003573 if (symbol_byte_size)
3574 sym[sym_idx].SetByteSize (symbol_byte_size);
3575 ++sym_idx;
3576 }
3577 }
3578 }
3579 }
3580 }
3581 }
3582
3583 // Trim our symbols down to just what we ended up with after
3584 // removing any symbols.
3585 if (sym_idx < num_syms)
3586 {
3587 num_syms = sym_idx;
3588 sym = symtab->Resize (num_syms);
3589 }
3590
3591 // Now synthesize indirect symbols
3592 if (m_dysymtab.nindirectsyms != 0)
3593 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003594 if (indirect_symbol_index_data.GetByteSize())
3595 {
3596 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3597
3598 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3599 {
3600 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3601 {
3602 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3603 if (symbol_stub_byte_size == 0)
3604 continue;
3605
3606 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3607
3608 if (num_symbol_stubs == 0)
3609 continue;
3610
3611 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3612 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3613 {
3614 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3615 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 +00003616 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003617 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3618 {
3619 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3620 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3621 continue;
3622
3623 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3624 Symbol *stub_symbol = NULL;
3625 if (index_pos != end_index_pos)
3626 {
3627 // We have a remapping from the original nlist index to
3628 // a current symbol index, so just look this up by index
3629 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3630 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003631 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003632 {
3633 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00003634 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003635 // S_SYMBOL_STUBS
3636 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3637 }
3638
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003639 if (stub_symbol)
3640 {
3641 Address so_addr(symbol_stub_addr, section_list);
3642
3643 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3644 {
3645 // Change the external symbol into a trampoline that makes sense
3646 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3647 // can re-use them so we don't have to make up a synthetic symbol
3648 // for no good reason.
3649 stub_symbol->SetType (eSymbolTypeTrampoline);
3650 stub_symbol->SetExternal (false);
3651 stub_symbol->GetAddress() = so_addr;
3652 stub_symbol->SetByteSize (symbol_stub_byte_size);
3653 }
3654 else
3655 {
3656 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00003657 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003658 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00003659 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003660 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00003661 stub_symbol = NULL; // this pointer no longer valid
3662 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003663 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00003664 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003665 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3666 sym[sym_idx].SetIsSynthetic (true);
3667 sym[sym_idx].GetAddress() = so_addr;
3668 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3669 ++sym_idx;
3670 }
3671 }
Greg Clayton3f839a32012-09-05 01:38:55 +00003672 else
3673 {
3674 if (log)
3675 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3676 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003677 }
3678 }
3679 }
3680 }
3681 }
3682 }
Greg Clayton3046e662013-07-10 01:23:25 +00003683
3684// StreamFile s(stdout, false);
3685// s.Printf ("Symbol table before CalculateSymbolSizes():\n");
3686// symtab->Dump(&s, NULL, eSortOrderNone);
3687 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
3688 symtab->CalculateSymbolSizes();
3689
3690// s.Printf ("Symbol table after CalculateSymbolSizes():\n");
3691// symtab->Dump(&s, NULL, eSortOrderNone);
3692
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003693 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003694 }
3695 return 0;
3696}
3697
3698
3699void
3700ObjectFileMachO::Dump (Stream *s)
3701{
Greg Claytona1743492012-03-13 23:14:29 +00003702 ModuleSP module_sp(GetModule());
3703 if (module_sp)
3704 {
3705 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3706 s->Printf("%p: ", this);
3707 s->Indent();
3708 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3709 s->PutCString("ObjectFileMachO64");
3710 else
3711 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003712
Greg Claytona1743492012-03-13 23:14:29 +00003713 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003714
Greg Claytona1743492012-03-13 23:14:29 +00003715 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003716
Greg Clayton3046e662013-07-10 01:23:25 +00003717 SectionList *sections = GetSectionList();
3718 if (sections)
3719 sections->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003720
Greg Claytona1743492012-03-13 23:14:29 +00003721 if (m_symtab_ap.get())
3722 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3723 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003724}
3725
Greg Claytonf4d6de62013-04-24 22:29:28 +00003726bool
3727ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3728 const lldb_private::DataExtractor &data,
3729 lldb::offset_t lc_offset,
3730 lldb_private::UUID& uuid)
3731{
3732 uint32_t i;
3733 struct uuid_command load_cmd;
3734
3735 lldb::offset_t offset = lc_offset;
3736 for (i=0; i<header.ncmds; ++i)
3737 {
3738 const lldb::offset_t cmd_offset = offset;
3739 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3740 break;
3741
3742 if (load_cmd.cmd == LoadCommandUUID)
3743 {
3744 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3745
3746 if (uuid_bytes)
3747 {
3748 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3749 // We pretend these object files have no UUID to prevent crashing.
3750
3751 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3752 0x3b, 0xa8,
3753 0x4b, 0x16,
3754 0xb6, 0xa4,
3755 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3756
3757 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3758 return false;
3759
3760 uuid.SetBytes (uuid_bytes);
3761 return true;
3762 }
3763 return false;
3764 }
3765 offset = cmd_offset + load_cmd.cmdsize;
3766 }
3767 return false;
3768}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003769
3770bool
Greg Clayton60830262011-02-04 18:53:10 +00003771ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003772{
Greg Claytona1743492012-03-13 23:14:29 +00003773 ModuleSP module_sp(GetModule());
3774 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003775 {
Greg Claytona1743492012-03-13 23:14:29 +00003776 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00003777 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00003778 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003779 }
3780 return false;
3781}
3782
3783
3784uint32_t
3785ObjectFileMachO::GetDependentModules (FileSpecList& files)
3786{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003787 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00003788 ModuleSP module_sp(GetModule());
3789 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003790 {
Greg Claytona1743492012-03-13 23:14:29 +00003791 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3792 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00003793 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003794 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3795 uint32_t i;
3796 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003797 {
Greg Claytona1743492012-03-13 23:14:29 +00003798 const uint32_t cmd_offset = offset;
3799 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3800 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003801
Greg Claytona1743492012-03-13 23:14:29 +00003802 switch (load_cmd.cmd)
3803 {
3804 case LoadCommandDylibLoad:
3805 case LoadCommandDylibLoadWeak:
3806 case LoadCommandDylibReexport:
3807 case LoadCommandDynamicLinkerLoad:
3808 case LoadCommandFixedVMShlibLoad:
3809 case LoadCommandDylibLoadUpward:
3810 {
3811 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3812 const char *path = m_data.PeekCStr(name_offset);
3813 // Skip any path that starts with '@' since these are usually:
3814 // @executable_path/.../file
3815 // @rpath/.../file
3816 if (path && path[0] != '@')
3817 {
3818 FileSpec file_spec(path, resolve_path);
3819 if (files.AppendIfUnique(file_spec))
3820 count++;
3821 }
3822 }
3823 break;
3824
3825 default:
3826 break;
3827 }
3828 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003829 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003830 }
3831 return count;
3832}
3833
Jim Ingham672e6f52011-03-07 23:44:08 +00003834lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00003835ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00003836{
3837 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3838 // is initialized to an invalid address, so we can just return that.
3839 // 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 +00003840
Jim Ingham672e6f52011-03-07 23:44:08 +00003841 if (!IsExecutable() || m_entry_point_address.IsValid())
3842 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003843
3844 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00003845 // /usr/include/mach-o.h, but it is basically:
3846 //
3847 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3848 // uint32_t count - this is the count of longs in the thread state data
3849 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3850 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00003851 //
Jim Ingham672e6f52011-03-07 23:44:08 +00003852 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3853 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3854 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3855 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3856 //
3857 // For now we hard-code the offsets and flavors we need:
3858 //
3859 //
3860
Greg Claytona1743492012-03-13 23:14:29 +00003861 ModuleSP module_sp(GetModule());
3862 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00003863 {
Greg Claytona1743492012-03-13 23:14:29 +00003864 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3865 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00003866 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00003867 uint32_t i;
3868 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3869 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003870
Greg Claytona1743492012-03-13 23:14:29 +00003871 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00003872 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003873 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00003874 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3875 break;
3876
3877 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00003878 {
Greg Claytona1743492012-03-13 23:14:29 +00003879 case LoadCommandUnixThread:
3880 case LoadCommandThread:
Jim Ingham672e6f52011-03-07 23:44:08 +00003881 {
Greg Claytona1743492012-03-13 23:14:29 +00003882 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00003883 {
Greg Claytona1743492012-03-13 23:14:29 +00003884 uint32_t flavor = m_data.GetU32(&offset);
3885 uint32_t count = m_data.GetU32(&offset);
3886 if (count == 0)
3887 {
3888 // We've gotten off somehow, log and exit;
3889 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00003890 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003891
Greg Claytona1743492012-03-13 23:14:29 +00003892 switch (m_header.cputype)
3893 {
3894 case llvm::MachO::CPUTypeARM:
3895 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3896 {
3897 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3898 start_address = m_data.GetU32(&offset);
3899 done = true;
3900 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003901 break;
Greg Claytona1743492012-03-13 23:14:29 +00003902 case llvm::MachO::CPUTypeI386:
3903 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3904 {
3905 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3906 start_address = m_data.GetU32(&offset);
3907 done = true;
3908 }
3909 break;
3910 case llvm::MachO::CPUTypeX86_64:
3911 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3912 {
3913 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3914 start_address = m_data.GetU64(&offset);
3915 done = true;
3916 }
3917 break;
3918 default:
3919 return m_entry_point_address;
3920 }
3921 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3922 if (done)
3923 break;
3924 offset += count * 4;
3925 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003926 }
Greg Claytona1743492012-03-13 23:14:29 +00003927 break;
3928 case LoadCommandMain:
Sean Callanan226b70c2012-03-08 02:39:03 +00003929 {
Greg Claytona1743492012-03-13 23:14:29 +00003930 ConstString text_segment_name ("__TEXT");
3931 uint64_t entryoffset = m_data.GetU64(&offset);
3932 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3933 if (text_segment_sp)
3934 {
3935 done = true;
3936 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3937 }
Sean Callanan226b70c2012-03-08 02:39:03 +00003938 }
Greg Claytona1743492012-03-13 23:14:29 +00003939
3940 default:
3941 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00003942 }
Greg Claytona1743492012-03-13 23:14:29 +00003943 if (done)
3944 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00003945
Greg Claytona1743492012-03-13 23:14:29 +00003946 // Go to the next load command:
3947 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00003948 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003949
Greg Claytona1743492012-03-13 23:14:29 +00003950 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00003951 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00003952 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00003953 // of this ObjectFile:
3954 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00003955 {
Greg Claytona1743492012-03-13 23:14:29 +00003956 m_entry_point_address.Clear();
3957 }
3958 }
3959 else
3960 {
3961 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3962 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003963
Greg Claytona1743492012-03-13 23:14:29 +00003964 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00003965
Greg Claytona1743492012-03-13 23:14:29 +00003966 if (module_sp)
3967 {
3968 SymbolContextList contexts;
3969 SymbolContext context;
3970 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3971 {
3972 if (contexts.GetContextAtIndex(0, context))
3973 m_entry_point_address = context.symbol->GetAddress();
3974 }
Greg Claytone72dfb32012-02-24 01:59:29 +00003975 }
3976 }
Jim Ingham672e6f52011-03-07 23:44:08 +00003977 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003978
Jim Ingham672e6f52011-03-07 23:44:08 +00003979 return m_entry_point_address;
3980
3981}
3982
Greg Claytonc9660542012-02-05 02:38:54 +00003983lldb_private::Address
3984ObjectFileMachO::GetHeaderAddress ()
3985{
3986 lldb_private::Address header_addr;
3987 SectionList *section_list = GetSectionList();
3988 if (section_list)
3989 {
3990 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3991 if (text_segment_sp)
3992 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003993 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00003994 header_addr.SetOffset (0);
3995 }
3996 }
3997 return header_addr;
3998}
3999
Greg Claytonc3776bf2012-02-09 06:16:32 +00004000uint32_t
4001ObjectFileMachO::GetNumThreadContexts ()
4002{
Greg Claytona1743492012-03-13 23:14:29 +00004003 ModuleSP module_sp(GetModule());
4004 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004005 {
Greg Claytona1743492012-03-13 23:14:29 +00004006 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4007 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004008 {
Greg Claytona1743492012-03-13 23:14:29 +00004009 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00004010 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004011 FileRangeArray::Entry file_range;
4012 thread_command thread_cmd;
4013 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004014 {
Greg Claytona1743492012-03-13 23:14:29 +00004015 const uint32_t cmd_offset = offset;
4016 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
4017 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004018
Greg Claytona1743492012-03-13 23:14:29 +00004019 if (thread_cmd.cmd == LoadCommandThread)
4020 {
4021 file_range.SetRangeBase (offset);
4022 file_range.SetByteSize (thread_cmd.cmdsize - 8);
4023 m_thread_context_offsets.Append (file_range);
4024 }
4025 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00004026 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004027 }
4028 }
4029 return m_thread_context_offsets.GetSize();
4030}
4031
4032lldb::RegisterContextSP
4033ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
4034{
Greg Claytonc3776bf2012-02-09 06:16:32 +00004035 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00004036
Greg Claytona1743492012-03-13 23:14:29 +00004037 ModuleSP module_sp(GetModule());
4038 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004039 {
Greg Claytona1743492012-03-13 23:14:29 +00004040 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4041 if (!m_thread_context_offsets_valid)
4042 GetNumThreadContexts ();
4043
4044 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00004045 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00004046 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004047
4048 DataExtractor data (m_data,
4049 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00004050 thread_context_file_range->GetByteSize());
4051
4052 switch (m_header.cputype)
4053 {
4054 case llvm::MachO::CPUTypeARM:
4055 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
4056 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004057
Jim Ingham28eb5712012-10-12 17:34:26 +00004058 case llvm::MachO::CPUTypeI386:
4059 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
4060 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004061
Jim Ingham28eb5712012-10-12 17:34:26 +00004062 case llvm::MachO::CPUTypeX86_64:
4063 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
4064 break;
4065 }
Greg Claytona1743492012-03-13 23:14:29 +00004066 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004067 }
4068 return reg_ctx_sp;
4069}
4070
Greg Claytonc9660542012-02-05 02:38:54 +00004071
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004072ObjectFile::Type
4073ObjectFileMachO::CalculateType()
4074{
4075 switch (m_header.filetype)
4076 {
4077 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4078 if (GetAddressByteSize () == 4)
4079 {
4080 // 32 bit kexts are just object files, but they do have a valid
4081 // UUID load command.
4082 UUID uuid;
4083 if (GetUUID(&uuid))
4084 {
4085 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004086 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004087 // "OSKextGetCurrentIdentifier" as this is required of kexts
4088 if (m_strata == eStrataInvalid)
4089 m_strata = eStrataKernel;
4090 return eTypeSharedLibrary;
4091 }
4092 }
4093 return eTypeObjectFile;
4094
4095 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
4096 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
4097 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
4098 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
4099 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
4100 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
4101 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
4102 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
4103 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
4104 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
4105 default:
4106 break;
4107 }
4108 return eTypeUnknown;
4109}
4110
4111ObjectFile::Strata
4112ObjectFileMachO::CalculateStrata()
4113{
4114 switch (m_header.filetype)
4115 {
4116 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4117 {
4118 // 32 bit kexts are just object files, but they do have a valid
4119 // UUID load command.
4120 UUID uuid;
4121 if (GetUUID(&uuid))
4122 {
4123 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004124 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004125 // "OSKextGetCurrentIdentifier" as this is required of kexts
4126 if (m_type == eTypeInvalid)
4127 m_type = eTypeSharedLibrary;
4128
4129 return eStrataKernel;
4130 }
4131 }
4132 return eStrataUnknown;
4133
4134 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
4135 // Check for the MH_DYLDLINK bit in the flags
4136 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callanan49bce8e2012-02-10 20:22:35 +00004137 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004138 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00004139 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004140 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00004141 {
4142 SectionList *section_list = GetSectionList();
4143 if (section_list)
4144 {
4145 static ConstString g_kld_section_name ("__KLD");
4146 if (section_list->FindSectionByName(g_kld_section_name))
4147 return eStrataKernel;
4148 }
4149 }
4150 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004151
4152 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4153 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callanan49bce8e2012-02-10 20:22:35 +00004154 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004155 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4156 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4157 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4158 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4159 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4160 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4161 default:
4162 break;
4163 }
4164 return eStrataUnknown;
4165}
4166
4167
Greg Claytonc2ff9312012-02-22 19:41:02 +00004168uint32_t
4169ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4170{
Greg Claytona1743492012-03-13 23:14:29 +00004171 ModuleSP module_sp(GetModule());
4172 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004173 {
Greg Claytona1743492012-03-13 23:14:29 +00004174 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4175 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004176 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004177 uint32_t version_cmd = 0;
4178 uint64_t version = 0;
4179 uint32_t i;
4180 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004181 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004182 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004183 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4184 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004185
Greg Claytona1743492012-03-13 23:14:29 +00004186 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004187 {
Greg Claytona1743492012-03-13 23:14:29 +00004188 if (version_cmd == 0)
4189 {
4190 version_cmd = load_cmd.cmd;
4191 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4192 break;
4193 version = load_cmd.dylib.current_version;
4194 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004195 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00004196 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00004197 }
Greg Claytona1743492012-03-13 23:14:29 +00004198 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004199 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004200
Greg Claytona1743492012-03-13 23:14:29 +00004201 if (version_cmd == LoadCommandDylibIdent)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004202 {
Greg Claytona1743492012-03-13 23:14:29 +00004203 if (versions != NULL && num_versions > 0)
4204 {
4205 if (num_versions > 0)
4206 versions[0] = (version & 0xFFFF0000ull) >> 16;
4207 if (num_versions > 1)
4208 versions[1] = (version & 0x0000FF00ull) >> 8;
4209 if (num_versions > 2)
4210 versions[2] = (version & 0x000000FFull);
4211 // Fill in an remaining version numbers with invalid values
4212 for (i=3; i<num_versions; ++i)
4213 versions[i] = UINT32_MAX;
4214 }
4215 // The LC_ID_DYLIB load command has a version with 3 version numbers
4216 // in it, so always return 3
4217 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004218 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00004219 }
4220 return false;
4221}
4222
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004223bool
Greg Clayton514487e2011-02-15 21:59:32 +00004224ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004225{
Greg Claytona1743492012-03-13 23:14:29 +00004226 ModuleSP module_sp(GetModule());
4227 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00004228 {
Greg Claytona1743492012-03-13 23:14:29 +00004229 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4230 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +00004231
Greg Claytona1743492012-03-13 23:14:29 +00004232 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda4e7511e2013-03-06 23:19:17 +00004233 // debugs at the addresses in the file itself. Below we set the OS to
Greg Claytona1743492012-03-13 23:14:29 +00004234 // unknown to make sure we use the DynamicLoaderStatic()...
4235 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4236 {
4237 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4238 }
4239 return true;
Greg Clayton593577a2011-09-21 03:57:31 +00004240 }
Greg Claytona1743492012-03-13 23:14:29 +00004241 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004242}
4243
4244
Jason Molenda0e0954c2013-04-16 06:24:42 +00004245UUID
4246ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4247{
4248 UUID uuid;
4249 if (process)
4250 {
4251 addr_t all_image_infos = process->GetImageInfoAddress();
4252
4253 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4254 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4255 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4256 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4257
4258 Error err;
4259 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4260 if (version_or_magic != -1
4261 && version_or_magic != HeaderMagic32
4262 && version_or_magic != HeaderMagic32Swapped
4263 && version_or_magic != HeaderMagic64
4264 && version_or_magic != HeaderMagic64Swapped
4265 && version_or_magic >= 13)
4266 {
4267 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4268 int wordsize = process->GetAddressByteSize();
4269 if (wordsize == 8)
4270 {
4271 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4272 }
4273 if (wordsize == 4)
4274 {
4275 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4276 }
4277 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4278 {
4279 uuid_t shared_cache_uuid;
4280 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4281 {
4282 uuid.SetBytes (shared_cache_uuid);
4283 }
4284 }
4285 }
4286 }
4287 return uuid;
4288}
4289
4290UUID
4291ObjectFileMachO::GetLLDBSharedCacheUUID ()
4292{
4293 UUID uuid;
4294#if defined (__APPLE__) && defined (__arm__)
4295 uint8_t *(*dyld_get_all_image_infos)(void);
4296 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4297 if (dyld_get_all_image_infos)
4298 {
4299 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4300 if (dyld_all_image_infos_address)
4301 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00004302 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4303 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00004304 {
Jason Molenda42b69fa2013-04-16 22:56:17 +00004305 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 +00004306 uuid.SetBytes (sharedCacheUUID_address);
4307 }
4308 }
4309 }
4310#endif
4311 return uuid;
4312}
4313
4314
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004315//------------------------------------------------------------------
4316// PluginInterface protocol
4317//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00004318lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004319ObjectFileMachO::GetPluginName()
4320{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004321 return GetPluginNameStatic();
4322}
4323
4324uint32_t
4325ObjectFileMachO::GetPluginVersion()
4326{
4327 return 1;
4328}
4329