blob: e1972a2f09eb80e5ddcbc176f536915662ec3ca0 [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 {
Charles Davis510938e2013-08-27 05:04:57 +0000405 case MH_MAGIC:
406 case MH_CIGAM:
Greg Clayton9aae0a12013-05-15 19:52:08 +0000407 return sizeof(struct mach_header);
408
Charles Davis510938e2013-08-27 05:04:57 +0000409 case MH_MAGIC_64:
410 case MH_CIGAM_64:
Greg Clayton9aae0a12013-05-15 19:52:08 +0000411 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);
Charles Davis510938e2013-08-27 05:04:57 +0000526 if (header.filetype == MH_PRELOAD) // 0x5u
Jason Molendab000e4d2013-08-27 02:22:06 +0000527 {
528 // Set OS to "unknown" - this is a standalone binary with no dyld et al
529 spec.GetArchitecture().GetTriple().setOS (llvm::Triple::UnknownOS);
530 }
Greg Claytonf4d6de62013-04-24 22:29:28 +0000531 if (spec.GetArchitecture().IsValid())
532 {
533 GetUUID (header, data, data_offset, spec.GetUUID());
534 specs.Append(spec);
535 }
536 }
537 }
538 }
539 return specs.GetSize() - initial_count;
540}
541
542
Greg Claytonc9660542012-02-05 02:38:54 +0000543
544const ConstString &
545ObjectFileMachO::GetSegmentNameTEXT()
546{
547 static ConstString g_segment_name_TEXT ("__TEXT");
548 return g_segment_name_TEXT;
549}
550
551const ConstString &
552ObjectFileMachO::GetSegmentNameDATA()
553{
554 static ConstString g_segment_name_DATA ("__DATA");
555 return g_segment_name_DATA;
556}
557
558const ConstString &
559ObjectFileMachO::GetSegmentNameOBJC()
560{
561 static ConstString g_segment_name_OBJC ("__OBJC");
562 return g_segment_name_OBJC;
563}
564
565const ConstString &
566ObjectFileMachO::GetSegmentNameLINKEDIT()
567{
568 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
569 return g_section_name_LINKEDIT;
570}
571
572const ConstString &
573ObjectFileMachO::GetSectionNameEHFrame()
574{
575 static ConstString g_section_name_eh_frame ("__eh_frame");
576 return g_section_name_eh_frame;
577}
578
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579bool
Jason Molenda4e7511e2013-03-06 23:19:17 +0000580ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
581 lldb::addr_t data_offset,
Greg Clayton44435ed2012-01-12 05:25:17 +0000582 lldb::addr_t data_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583{
Greg Clayton44435ed2012-01-12 05:25:17 +0000584 DataExtractor data;
585 data.SetData (data_sp, data_offset, data_length);
Greg Claytonc7bece562013-01-25 18:06:21 +0000586 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 uint32_t magic = data.GetU32(&offset);
588 return MachHeaderSizeFromMagic(magic) != 0;
589}
590
591
Greg Clayton5ce9c562013-02-06 17:22:03 +0000592ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
593 DataBufferSP& data_sp,
594 lldb::offset_t data_offset,
595 const FileSpec* file,
596 lldb::offset_t file_offset,
597 lldb::offset_t length) :
598 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000599 m_mach_segments(),
600 m_mach_sections(),
601 m_entry_point_address(),
602 m_thread_context_offsets(),
603 m_thread_context_offsets_valid(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604{
Greg Clayton72b77eb2011-02-04 21:13:05 +0000605 ::memset (&m_header, 0, sizeof(m_header));
606 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607}
608
Greg Claytone72dfb32012-02-24 01:59:29 +0000609ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000610 lldb::DataBufferSP& header_data_sp,
611 const lldb::ProcessSP &process_sp,
612 lldb::addr_t header_addr) :
Greg Claytone72dfb32012-02-24 01:59:29 +0000613 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000614 m_mach_segments(),
615 m_mach_sections(),
616 m_entry_point_address(),
617 m_thread_context_offsets(),
618 m_thread_context_offsets_valid(false)
Greg Claytonc9660542012-02-05 02:38:54 +0000619{
620 ::memset (&m_header, 0, sizeof(m_header));
621 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
622}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623
624ObjectFileMachO::~ObjectFileMachO()
625{
626}
627
Greg Claytonf4d6de62013-04-24 22:29:28 +0000628bool
629ObjectFileMachO::ParseHeader (DataExtractor &data,
630 lldb::offset_t *data_offset_ptr,
631 llvm::MachO::mach_header &header)
632{
633 data.SetByteOrder (lldb::endian::InlHostByteOrder());
634 // Leave magic in the original byte order
635 header.magic = data.GetU32(data_offset_ptr);
636 bool can_parse = false;
637 bool is_64_bit = false;
638 switch (header.magic)
639 {
Charles Davis510938e2013-08-27 05:04:57 +0000640 case MH_MAGIC:
Greg Claytonf4d6de62013-04-24 22:29:28 +0000641 data.SetByteOrder (lldb::endian::InlHostByteOrder());
642 data.SetAddressByteSize(4);
643 can_parse = true;
644 break;
645
Charles Davis510938e2013-08-27 05:04:57 +0000646 case MH_MAGIC_64:
Greg Claytonf4d6de62013-04-24 22:29:28 +0000647 data.SetByteOrder (lldb::endian::InlHostByteOrder());
648 data.SetAddressByteSize(8);
649 can_parse = true;
650 is_64_bit = true;
651 break;
652
Charles Davis510938e2013-08-27 05:04:57 +0000653 case MH_CIGAM:
Greg Claytonf4d6de62013-04-24 22:29:28 +0000654 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
655 data.SetAddressByteSize(4);
656 can_parse = true;
657 break;
658
Charles Davis510938e2013-08-27 05:04:57 +0000659 case MH_CIGAM_64:
Greg Claytonf4d6de62013-04-24 22:29:28 +0000660 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
661 data.SetAddressByteSize(8);
662 is_64_bit = true;
663 can_parse = true;
664 break;
665
666 default:
667 break;
668 }
669
670 if (can_parse)
671 {
672 data.GetU32(data_offset_ptr, &header.cputype, 6);
673 if (is_64_bit)
674 *data_offset_ptr += 4;
675 return true;
676 }
677 else
678 {
679 memset(&header, 0, sizeof(header));
680 }
681 return false;
682}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683
684bool
685ObjectFileMachO::ParseHeader ()
686{
Greg Claytona1743492012-03-13 23:14:29 +0000687 ModuleSP module_sp(GetModule());
688 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689 {
Greg Claytona1743492012-03-13 23:14:29 +0000690 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
691 bool can_parse = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000692 lldb::offset_t offset = 0;
Greg Clayton7fb56d02011-02-01 01:31:41 +0000693 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +0000694 // Leave magic in the original byte order
695 m_header.magic = m_data.GetU32(&offset);
696 switch (m_header.magic)
Greg Claytonc9660542012-02-05 02:38:54 +0000697 {
Charles Davis510938e2013-08-27 05:04:57 +0000698 case MH_MAGIC:
Greg Claytona1743492012-03-13 23:14:29 +0000699 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
700 m_data.SetAddressByteSize(4);
701 can_parse = true;
702 break;
703
Charles Davis510938e2013-08-27 05:04:57 +0000704 case MH_MAGIC_64:
Greg Claytona1743492012-03-13 23:14:29 +0000705 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
706 m_data.SetAddressByteSize(8);
707 can_parse = true;
708 break;
709
Charles Davis510938e2013-08-27 05:04:57 +0000710 case MH_CIGAM:
Greg Claytona1743492012-03-13 23:14:29 +0000711 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
712 m_data.SetAddressByteSize(4);
713 can_parse = true;
714 break;
715
Charles Davis510938e2013-08-27 05:04:57 +0000716 case MH_CIGAM_64:
Greg Claytona1743492012-03-13 23:14:29 +0000717 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
718 m_data.SetAddressByteSize(8);
719 can_parse = true;
720 break;
721
722 default:
723 break;
Greg Claytonc9660542012-02-05 02:38:54 +0000724 }
Greg Claytona1743492012-03-13 23:14:29 +0000725
726 if (can_parse)
727 {
728 m_data.GetU32(&offset, &m_header.cputype, 6);
729
730 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +0000731
Greg Claytond97ec1c2012-11-16 21:36:10 +0000732 // Check if the module has a required architecture
733 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callananbf4b7be2012-12-13 22:07:14 +0000734 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Claytond97ec1c2012-11-16 21:36:10 +0000735 return false;
736
Greg Claytona1743492012-03-13 23:14:29 +0000737 if (SetModulesArchitecture (mach_arch))
738 {
739 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
740 if (m_data.GetByteSize() < header_and_lc_size)
741 {
742 DataBufferSP data_sp;
743 ProcessSP process_sp (m_process_wp.lock());
744 if (process_sp)
745 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000746 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Claytona1743492012-03-13 23:14:29 +0000747 }
748 else
749 {
750 // Read in all only the load command data from the file on disk
Greg Clayton5ce9c562013-02-06 17:22:03 +0000751 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Claytona1743492012-03-13 23:14:29 +0000752 if (data_sp->GetByteSize() != header_and_lc_size)
753 return false;
754 }
755 if (data_sp)
756 m_data.SetData (data_sp);
757 }
758 }
759 return true;
760 }
761 else
762 {
763 memset(&m_header, 0, sizeof(struct mach_header));
764 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765 }
766 return false;
767}
768
769
770ByteOrder
771ObjectFileMachO::GetByteOrder () const
772{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773 return m_data.GetByteOrder ();
774}
775
Jim Ingham5aee1622010-08-09 23:31:02 +0000776bool
777ObjectFileMachO::IsExecutable() const
778{
Charles Davis510938e2013-08-27 05:04:57 +0000779 return m_header.filetype == MH_EXECUTE;
Jim Ingham5aee1622010-08-09 23:31:02 +0000780}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781
Greg Claytonc7bece562013-01-25 18:06:21 +0000782uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783ObjectFileMachO::GetAddressByteSize () const
784{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785 return m_data.GetAddressByteSize ();
786}
787
Greg Claytone0d378b2011-03-24 21:19:54 +0000788AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +0000789ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
790{
791 Symtab *symtab = GetSymtab();
792 if (symtab)
793 {
794 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
795 if (symbol)
796 {
Greg Claytone7612132012-03-07 21:03:09 +0000797 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +0000798 {
Greg Claytone7612132012-03-07 21:03:09 +0000799 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +0000800 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +0000801 {
Charles Davis510938e2013-08-27 05:04:57 +0000802 const lldb::SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000803 switch (section_type)
804 {
805 case eSectionTypeInvalid: return eAddressClassUnknown;
806 case eSectionTypeCode:
Charles Davis510938e2013-08-27 05:04:57 +0000807 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +0000808 {
809 // For ARM we have a bit in the n_desc field of the symbol
810 // that tells us ARM/Thumb which is bit 0x0008.
811 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
812 return eAddressClassCodeAlternateISA;
813 }
814 return eAddressClassCode;
815
816 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton5009f9d2011-10-27 17:55:14 +0000817 case eSectionTypeData:
818 case eSectionTypeDataCString:
819 case eSectionTypeDataCStringPointers:
820 case eSectionTypeDataSymbolAddress:
821 case eSectionTypeData4:
822 case eSectionTypeData8:
823 case eSectionTypeData16:
824 case eSectionTypeDataPointers:
825 case eSectionTypeZeroFill:
826 case eSectionTypeDataObjCMessageRefs:
827 case eSectionTypeDataObjCCFStrings:
828 return eAddressClassData;
829 case eSectionTypeDebug:
830 case eSectionTypeDWARFDebugAbbrev:
831 case eSectionTypeDWARFDebugAranges:
832 case eSectionTypeDWARFDebugFrame:
833 case eSectionTypeDWARFDebugInfo:
834 case eSectionTypeDWARFDebugLine:
835 case eSectionTypeDWARFDebugLoc:
836 case eSectionTypeDWARFDebugMacInfo:
837 case eSectionTypeDWARFDebugPubNames:
838 case eSectionTypeDWARFDebugPubTypes:
839 case eSectionTypeDWARFDebugRanges:
840 case eSectionTypeDWARFDebugStr:
841 case eSectionTypeDWARFAppleNames:
842 case eSectionTypeDWARFAppleTypes:
843 case eSectionTypeDWARFAppleNamespaces:
844 case eSectionTypeDWARFAppleObjC:
845 return eAddressClassDebug;
Greg Claytonded470d2011-03-19 01:12:21 +0000846 case eSectionTypeEHFrame: return eAddressClassRuntime;
Michael Sartaina7499c92013-07-01 19:45:50 +0000847 case eSectionTypeELFSymbolTable:
848 case eSectionTypeELFDynamicSymbols:
849 case eSectionTypeELFRelocationEntries:
850 case eSectionTypeELFDynamicLinkInfo:
Greg Claytonded470d2011-03-19 01:12:21 +0000851 case eSectionTypeOther: return eAddressClassUnknown;
852 }
853 }
854 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000855
Greg Claytone0d378b2011-03-24 21:19:54 +0000856 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000857 switch (symbol_type)
858 {
859 case eSymbolTypeAny: return eAddressClassUnknown;
860 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000861
Greg Claytonded470d2011-03-19 01:12:21 +0000862 case eSymbolTypeCode:
863 case eSymbolTypeTrampoline:
Greg Clayton059f7242013-02-27 21:16:04 +0000864 case eSymbolTypeResolver:
Charles Davis510938e2013-08-27 05:04:57 +0000865 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +0000866 {
867 // For ARM we have a bit in the n_desc field of the symbol
868 // that tells us ARM/Thumb which is bit 0x0008.
869 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
870 return eAddressClassCodeAlternateISA;
871 }
872 return eAddressClassCode;
873
874 case eSymbolTypeData: return eAddressClassData;
875 case eSymbolTypeRuntime: return eAddressClassRuntime;
876 case eSymbolTypeException: return eAddressClassRuntime;
877 case eSymbolTypeSourceFile: return eAddressClassDebug;
878 case eSymbolTypeHeaderFile: return eAddressClassDebug;
879 case eSymbolTypeObjectFile: return eAddressClassDebug;
880 case eSymbolTypeCommonBlock: return eAddressClassDebug;
881 case eSymbolTypeBlock: return eAddressClassDebug;
882 case eSymbolTypeLocal: return eAddressClassData;
883 case eSymbolTypeParam: return eAddressClassData;
884 case eSymbolTypeVariable: return eAddressClassData;
885 case eSymbolTypeVariableType: return eAddressClassDebug;
886 case eSymbolTypeLineEntry: return eAddressClassDebug;
887 case eSymbolTypeLineHeader: return eAddressClassDebug;
888 case eSymbolTypeScopeBegin: return eAddressClassDebug;
889 case eSymbolTypeScopeEnd: return eAddressClassDebug;
890 case eSymbolTypeAdditional: return eAddressClassUnknown;
891 case eSymbolTypeCompiler: return eAddressClassDebug;
892 case eSymbolTypeInstrumentation:return eAddressClassDebug;
893 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +0000894 case eSymbolTypeObjCClass: return eAddressClassRuntime;
895 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
896 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Clayton9191db42013-10-21 18:40:51 +0000897 case eSymbolTypeReExported: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +0000898 }
899 }
900 }
901 return eAddressClassUnknown;
902}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903
904Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +0000905ObjectFileMachO::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000906{
Greg Claytona1743492012-03-13 23:14:29 +0000907 ModuleSP module_sp(GetModule());
908 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000909 {
Greg Claytona1743492012-03-13 23:14:29 +0000910 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
911 if (m_symtab_ap.get() == NULL)
912 {
913 m_symtab_ap.reset(new Symtab(this));
914 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton3046e662013-07-10 01:23:25 +0000915 ParseSymtab ();
Greg Claytona1743492012-03-13 23:14:29 +0000916 m_symtab_ap->Finalize ();
917 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918 }
919 return m_symtab_ap.get();
920}
921
Greg Clayton3046e662013-07-10 01:23:25 +0000922bool
923ObjectFileMachO::IsStripped ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924{
Greg Clayton3046e662013-07-10 01:23:25 +0000925 if (m_dysymtab.cmd == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926 {
Greg Clayton3046e662013-07-10 01:23:25 +0000927 ModuleSP module_sp(GetModule());
928 if (module_sp)
Greg Claytona1743492012-03-13 23:14:29 +0000929 {
Greg Clayton3046e662013-07-10 01:23:25 +0000930 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
931 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton4d78c402012-05-25 18:09:55 +0000932 {
Greg Clayton3046e662013-07-10 01:23:25 +0000933 const lldb::offset_t load_cmd_offset = offset;
934
935 load_command lc;
936 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
937 break;
Charles Davis510938e2013-08-27 05:04:57 +0000938 if (lc.cmd == LC_DYSYMTAB)
Greg Clayton4d78c402012-05-25 18:09:55 +0000939 {
Greg Clayton3046e662013-07-10 01:23:25 +0000940 m_dysymtab.cmd = lc.cmd;
941 m_dysymtab.cmdsize = lc.cmdsize;
942 if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
943 {
944 // Clear m_dysymtab if we were unable to read all items from the load command
945 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
946 }
Greg Clayton4d78c402012-05-25 18:09:55 +0000947 }
Greg Clayton3046e662013-07-10 01:23:25 +0000948 offset = load_cmd_offset + lc.cmdsize;
Greg Clayton4d78c402012-05-25 18:09:55 +0000949 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000950 }
Greg Clayton1eac0c72012-04-24 03:06:13 +0000951 }
Greg Clayton3046e662013-07-10 01:23:25 +0000952 if (m_dysymtab.cmd)
Greg Clayton93e28612013-10-11 22:03:48 +0000953 return m_dysymtab.nlocalsym <= 1;
Greg Clayton3046e662013-07-10 01:23:25 +0000954 return false;
955}
Greg Clayton1eac0c72012-04-24 03:06:13 +0000956
Greg Clayton3046e662013-07-10 01:23:25 +0000957void
958ObjectFileMachO::CreateSections (SectionList &unified_section_list)
959{
960 if (!m_sections_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961 {
Greg Clayton3046e662013-07-10 01:23:25 +0000962 m_sections_ap.reset(new SectionList());
963
Charles Davis510938e2013-08-27 05:04:57 +0000964 const bool is_dsym = (m_header.filetype == MH_DSYM);
Greg Clayton3046e662013-07-10 01:23:25 +0000965 lldb::user_id_t segID = 0;
966 lldb::user_id_t sectID = 0;
967 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
968 uint32_t i;
969 const bool is_core = GetType() == eTypeCoreFile;
970 //bool dump_sections = false;
971 ModuleSP module_sp (GetModule());
972 // First look up any LC_ENCRYPTION_INFO load commands
973 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
974 EncryptedFileRanges encrypted_file_ranges;
975 encryption_info_command encryption_cmd;
976 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977 {
Greg Clayton3046e662013-07-10 01:23:25 +0000978 const lldb::offset_t load_cmd_offset = offset;
979 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
980 break;
981
Charles Davis510938e2013-08-27 05:04:57 +0000982 if (encryption_cmd.cmd == LC_ENCRYPTION_INFO)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983 {
Greg Clayton3046e662013-07-10 01:23:25 +0000984 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000985 {
Greg Clayton3046e662013-07-10 01:23:25 +0000986 if (encryption_cmd.cryptid != 0)
Greg Claytond37d6922013-04-16 16:51:19 +0000987 {
Greg Clayton3046e662013-07-10 01:23:25 +0000988 EncryptedFileRanges::Entry entry;
989 entry.SetRangeBase(encryption_cmd.cryptoff);
990 entry.SetByteSize(encryption_cmd.cryptsize);
991 encrypted_file_ranges.Append(entry);
Jason Molendaf6ce26f2013-04-10 05:58:57 +0000992 }
993 }
Greg Clayton3046e662013-07-10 01:23:25 +0000994 }
995 offset = load_cmd_offset + encryption_cmd.cmdsize;
996 }
997
998 offset = MachHeaderSizeFromMagic(m_header.magic);
999
1000 struct segment_command_64 load_cmd;
1001 for (i=0; i<m_header.ncmds; ++i)
1002 {
1003 const lldb::offset_t load_cmd_offset = offset;
1004 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1005 break;
1006
Charles Davis510938e2013-08-27 05:04:57 +00001007 if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64)
Greg Clayton3046e662013-07-10 01:23:25 +00001008 {
1009 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010 {
Greg Clayton3046e662013-07-10 01:23:25 +00001011 bool add_section = true;
1012 bool add_to_unified = true;
1013 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 +00001014
Greg Clayton3046e662013-07-10 01:23:25 +00001015 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1016 if (is_dsym && unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017 {
Greg Clayton3046e662013-07-10 01:23:25 +00001018 if (const_segname == GetSegmentNameLINKEDIT())
1019 {
1020 // We need to keep the __LINKEDIT segment private to this object file only
1021 add_to_unified = false;
1022 }
1023 else
1024 {
1025 // This is the dSYM file and this section has already been created by
1026 // the object file, no need to create it.
1027 add_section = false;
1028 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029 }
Greg Clayton3046e662013-07-10 01:23:25 +00001030 load_cmd.vmaddr = m_data.GetAddress(&offset);
1031 load_cmd.vmsize = m_data.GetAddress(&offset);
1032 load_cmd.fileoff = m_data.GetAddress(&offset);
1033 load_cmd.filesize = m_data.GetAddress(&offset);
1034 if (m_length != 0 && load_cmd.filesize != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001035 {
Greg Clayton3046e662013-07-10 01:23:25 +00001036 if (load_cmd.fileoff > m_length)
1037 {
1038 // We have a load command that says it extends past the end of hte file. This is likely
1039 // a corrupt file. We don't have any way to return an error condition here (this method
1040 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1041 // is null out the SectionList vector and if a process has been set up, dump a message
1042 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001043 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001044 module_sp->ReportWarning("load command %u %s has a fileoff (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 "), ignoring this section",
Jason Molenda20eb31b2013-08-16 03:20:42 +00001045 i,
1046 lc_segment_name,
1047 load_cmd.fileoff,
1048 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001049
1050 load_cmd.fileoff = 0;
1051 load_cmd.filesize = 0;
1052 }
1053
1054 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1055 {
1056 // We have a load command that says it extends past the end of hte file. This is likely
1057 // a corrupt file. We don't have any way to return an error condition here (this method
1058 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1059 // is null out the SectionList vector and if a process has been set up, dump a message
1060 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001061 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001062 GetModule()->ReportWarning("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 to match",
Charles Davis510938e2013-08-27 05:04:57 +00001063 i,
1064 lc_segment_name,
1065 load_cmd.fileoff + load_cmd.filesize,
1066 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001067
1068 // Tuncase the length
1069 load_cmd.filesize = m_length - load_cmd.fileoff;
1070 }
1071 }
1072 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1073 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074
Charles Davis510938e2013-08-27 05:04:57 +00001075 const bool segment_is_encrypted = (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076
Greg Clayton3046e662013-07-10 01:23:25 +00001077 // Keep a list of mach segments around in case we need to
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001078 // get at data that isn't stored in the abstracted Sections.
Greg Clayton3046e662013-07-10 01:23:25 +00001079 m_mach_segments.push_back (load_cmd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080
Greg Clayton3046e662013-07-10 01:23:25 +00001081 // Use a segment ID of the segment index shifted left by 8 so they
1082 // never conflict with any of the sections.
1083 SectionSP segment_sp;
1084 if (add_section && (const_segname || is_core))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001085 {
Greg Clayton3046e662013-07-10 01:23:25 +00001086 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
1087 this, // Object file to which this sections belongs
1088 ++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
1089 const_segname, // Name of this section
1090 eSectionTypeContainer, // This section is a container of other sections.
1091 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1092 load_cmd.vmsize, // VM size in bytes of this section
1093 load_cmd.fileoff, // Offset to the data for this section in the file
1094 load_cmd.filesize, // Size in bytes of this section as found in the the file
1095 load_cmd.flags)); // Flags for this section
Greg Clayton8d38ac42010-06-28 23:51:11 +00001096
Greg Clayton3046e662013-07-10 01:23:25 +00001097 segment_sp->SetIsEncrypted (segment_is_encrypted);
1098 m_sections_ap->AddSection(segment_sp);
1099 if (add_to_unified)
1100 unified_section_list.AddSection(segment_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101 }
Greg Clayton3046e662013-07-10 01:23:25 +00001102 else if (unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001103 {
Jason Molenda20eb31b2013-08-16 03:20:42 +00001104 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr)
1105 {
1106 // Check to see if the module was read from memory?
1107 if (module_sp->GetObjectFile()->GetHeaderAddress().IsValid())
1108 {
1109 // We have a module that is in memory and needs to have its
1110 // file address adjusted. We need to do this because when we
1111 // load a file from memory, its addresses will be slid already,
1112 // yet the addresses in the new symbol file will still be unslid.
1113 // Since everything is stored as section offset, this shouldn't
1114 // cause any problems.
Jason Molenda5894a732013-08-17 03:39:52 +00001115
1116 // Make sure we've parsed the symbol table from the
1117 // ObjectFile before we go around changing its Sections.
1118 module_sp->GetObjectFile()->GetSymtab();
1119 // eh_frame would present the same problems but we parse that on
1120 // a per-function basis as-needed so it's more difficult to
1121 // remove its use of the Sections. Realistically, the environments
1122 // where this code path will be taken will not have eh_frame sections.
1123
Jason Molenda20eb31b2013-08-16 03:20:42 +00001124 unified_section_sp->SetFileAddress(load_cmd.vmaddr);
1125 }
1126 }
Greg Clayton3046e662013-07-10 01:23:25 +00001127 m_sections_ap->AddSection(unified_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129
Greg Clayton3046e662013-07-10 01:23:25 +00001130 struct section_64 sect64;
1131 ::memset (&sect64, 0, sizeof(sect64));
1132 // Push a section into our mach sections for the section at
Charles Davis510938e2013-08-27 05:04:57 +00001133 // index zero (NO_SECT) if we don't have any mach sections yet...
Greg Clayton3046e662013-07-10 01:23:25 +00001134 if (m_mach_sections.empty())
1135 m_mach_sections.push_back(sect64);
1136 uint32_t segment_sect_idx;
1137 const lldb::user_id_t first_segment_sectID = sectID + 1;
1138
1139
Charles Davis510938e2013-08-27 05:04:57 +00001140 const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8;
Greg Clayton3046e662013-07-10 01:23:25 +00001141 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001142 {
Greg Clayton3046e662013-07-10 01:23:25 +00001143 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
Greg Clayton89411422010-10-08 00:21:05 +00001144 break;
Greg Clayton3046e662013-07-10 01:23:25 +00001145 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1146 break;
1147 sect64.addr = m_data.GetAddress(&offset);
1148 sect64.size = m_data.GetAddress(&offset);
1149
1150 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1151 break;
1152
1153 // Keep a list of mach sections around in case we need to
1154 // get at data that isn't stored in the abstracted Sections.
1155 m_mach_sections.push_back (sect64);
1156
1157 if (add_section)
1158 {
1159 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1160 if (!const_segname)
1161 {
1162 // We have a segment with no name so we need to conjure up
1163 // segments that correspond to the section's segname if there
1164 // isn't already such a section. If there is such a section,
1165 // we resize the section so that it spans all sections.
1166 // We also mark these sections as fake so address matches don't
1167 // hit if they land in the gaps between the child sections.
1168 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1169 segment_sp = unified_section_list.FindSectionByName (const_segname);
1170 if (segment_sp.get())
1171 {
1172 Section *segment = segment_sp.get();
1173 // Grow the section size as needed.
1174 const lldb::addr_t sect64_min_addr = sect64.addr;
1175 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1176 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1177 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1178 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1179 if (sect64_min_addr >= curr_seg_min_addr)
1180 {
1181 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1182 // Only grow the section size if needed
1183 if (new_seg_byte_size > curr_seg_byte_size)
1184 segment->SetByteSize (new_seg_byte_size);
1185 }
1186 else
1187 {
1188 // We need to change the base address of the segment and
1189 // adjust the child section offsets for all existing children.
1190 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1191 segment->Slide(slide_amount, false);
1192 segment->GetChildren().Slide(-slide_amount, false);
1193 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1194 }
1195
1196 // Grow the section size as needed.
1197 if (sect64.offset)
1198 {
1199 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1200 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1201
1202 const lldb::addr_t section_min_file_offset = sect64.offset;
1203 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1204 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1205 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1206 segment->SetFileOffset (new_file_offset);
1207 segment->SetFileSize (new_file_size);
1208 }
1209 }
1210 else
1211 {
1212 // Create a fake section for the section's named segment
1213 segment_sp.reset(new Section (segment_sp, // Parent section
1214 module_sp, // Module to which this section belongs
1215 this, // Object file to which this section belongs
1216 ++segID << 8, // Section ID is the 1 based segment index shifted right by 8 bits as not to collide with any of the 256 section IDs that are possible
1217 const_segname, // Name of this section
1218 eSectionTypeContainer, // This section is a container of other sections.
1219 sect64.addr, // File VM address == addresses as they are found in the object file
1220 sect64.size, // VM size in bytes of this section
1221 sect64.offset, // Offset to the data for this section in the file
1222 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1223 load_cmd.flags)); // Flags for this section
1224 segment_sp->SetIsFake(true);
1225
1226 m_sections_ap->AddSection(segment_sp);
1227 if (add_to_unified)
1228 unified_section_list.AddSection(segment_sp);
1229 segment_sp->SetIsEncrypted (segment_is_encrypted);
1230 }
1231 }
1232 assert (segment_sp.get());
1233
Charles Davis510938e2013-08-27 05:04:57 +00001234 uint32_t mach_sect_type = sect64.flags & SECTION_TYPE;
Greg Clayton3046e662013-07-10 01:23:25 +00001235 static ConstString g_sect_name_objc_data ("__objc_data");
1236 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1237 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1238 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1239 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1240 static ConstString g_sect_name_objc_const ("__objc_const");
1241 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1242 static ConstString g_sect_name_cfstring ("__cfstring");
1243
1244 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1245 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1246 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1247 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1248 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1249 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1250 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1251 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1252 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1253 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1254 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1255 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1256 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1257 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1258 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1259 static ConstString g_sect_name_eh_frame ("__eh_frame");
1260 static ConstString g_sect_name_DATA ("__DATA");
1261 static ConstString g_sect_name_TEXT ("__TEXT");
1262
Charles Davis510938e2013-08-27 05:04:57 +00001263 lldb::SectionType sect_type = eSectionTypeOther;
Greg Clayton3046e662013-07-10 01:23:25 +00001264
1265 if (section_name == g_sect_name_dwarf_debug_abbrev)
1266 sect_type = eSectionTypeDWARFDebugAbbrev;
1267 else if (section_name == g_sect_name_dwarf_debug_aranges)
1268 sect_type = eSectionTypeDWARFDebugAranges;
1269 else if (section_name == g_sect_name_dwarf_debug_frame)
1270 sect_type = eSectionTypeDWARFDebugFrame;
1271 else if (section_name == g_sect_name_dwarf_debug_info)
1272 sect_type = eSectionTypeDWARFDebugInfo;
1273 else if (section_name == g_sect_name_dwarf_debug_line)
1274 sect_type = eSectionTypeDWARFDebugLine;
1275 else if (section_name == g_sect_name_dwarf_debug_loc)
1276 sect_type = eSectionTypeDWARFDebugLoc;
1277 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1278 sect_type = eSectionTypeDWARFDebugMacInfo;
1279 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1280 sect_type = eSectionTypeDWARFDebugPubNames;
1281 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1282 sect_type = eSectionTypeDWARFDebugPubTypes;
1283 else if (section_name == g_sect_name_dwarf_debug_ranges)
1284 sect_type = eSectionTypeDWARFDebugRanges;
1285 else if (section_name == g_sect_name_dwarf_debug_str)
1286 sect_type = eSectionTypeDWARFDebugStr;
1287 else if (section_name == g_sect_name_dwarf_apple_names)
1288 sect_type = eSectionTypeDWARFAppleNames;
1289 else if (section_name == g_sect_name_dwarf_apple_types)
1290 sect_type = eSectionTypeDWARFAppleTypes;
1291 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1292 sect_type = eSectionTypeDWARFAppleNamespaces;
1293 else if (section_name == g_sect_name_dwarf_apple_objc)
1294 sect_type = eSectionTypeDWARFAppleObjC;
1295 else if (section_name == g_sect_name_objc_selrefs)
1296 sect_type = eSectionTypeDataCStringPointers;
1297 else if (section_name == g_sect_name_objc_msgrefs)
1298 sect_type = eSectionTypeDataObjCMessageRefs;
1299 else if (section_name == g_sect_name_eh_frame)
1300 sect_type = eSectionTypeEHFrame;
1301 else if (section_name == g_sect_name_cfstring)
1302 sect_type = eSectionTypeDataObjCCFStrings;
1303 else if (section_name == g_sect_name_objc_data ||
1304 section_name == g_sect_name_objc_classrefs ||
1305 section_name == g_sect_name_objc_superrefs ||
1306 section_name == g_sect_name_objc_const ||
1307 section_name == g_sect_name_objc_classlist)
1308 {
1309 sect_type = eSectionTypeDataPointers;
1310 }
1311
1312 if (sect_type == eSectionTypeOther)
1313 {
1314 switch (mach_sect_type)
1315 {
1316 // TODO: categorize sections by other flags for regular sections
Charles Davis510938e2013-08-27 05:04:57 +00001317 case S_REGULAR:
Greg Clayton3046e662013-07-10 01:23:25 +00001318 if (segment_sp->GetName() == g_sect_name_TEXT)
1319 sect_type = eSectionTypeCode;
1320 else if (segment_sp->GetName() == g_sect_name_DATA)
1321 sect_type = eSectionTypeData;
1322 else
1323 sect_type = eSectionTypeOther;
1324 break;
Charles Davis510938e2013-08-27 05:04:57 +00001325 case S_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1326 case S_CSTRING_LITERALS: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1327 case S_4BYTE_LITERALS: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1328 case S_8BYTE_LITERALS: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1329 case S_LITERAL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1330 case S_NON_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1331 case S_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1332 case S_SYMBOL_STUBS: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1333 case S_MOD_INIT_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1334 case S_MOD_TERM_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1335 case S_COALESCED: sect_type = eSectionTypeOther; break;
1336 case S_GB_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1337 case S_INTERPOSING: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1338 case S_16BYTE_LITERALS: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1339 case S_DTRACE_DOF: sect_type = eSectionTypeDebug; break;
1340 case S_LAZY_DYLIB_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break;
Greg Clayton3046e662013-07-10 01:23:25 +00001341 default: break;
1342 }
1343 }
1344
1345 SectionSP section_sp(new Section (segment_sp,
1346 module_sp,
1347 this,
1348 ++sectID,
1349 section_name,
1350 sect_type,
1351 sect64.addr - segment_sp->GetFileAddress(),
1352 sect64.size,
1353 sect64.offset,
1354 sect64.offset == 0 ? 0 : sect64.size,
1355 sect64.flags));
1356 // Set the section to be encrypted to match the segment
1357
1358 bool section_is_encrypted = false;
1359 if (!segment_is_encrypted && load_cmd.filesize != 0)
1360 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1361
1362 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1363 segment_sp->GetChildren().AddSection(section_sp);
1364
1365 if (segment_sp->IsFake())
1366 {
1367 segment_sp.reset();
1368 const_segname.Clear();
1369 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001370 }
1371 }
Greg Clayton3046e662013-07-10 01:23:25 +00001372 if (segment_sp && is_dsym)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001373 {
Greg Clayton3046e662013-07-10 01:23:25 +00001374 if (first_segment_sectID <= sectID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001375 {
Greg Clayton3046e662013-07-10 01:23:25 +00001376 lldb::user_id_t sect_uid;
1377 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378 {
Greg Clayton3046e662013-07-10 01:23:25 +00001379 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1380 SectionSP next_section_sp;
1381 if (sect_uid + 1 <= sectID)
1382 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1383
1384 if (curr_section_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385 {
Greg Clayton3046e662013-07-10 01:23:25 +00001386 if (curr_section_sp->GetByteSize() == 0)
1387 {
1388 if (next_section_sp.get() != NULL)
1389 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1390 else
1391 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1392 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393 }
1394 }
1395 }
1396 }
1397 }
1398 }
1399 }
Charles Davis510938e2013-08-27 05:04:57 +00001400 else if (load_cmd.cmd == LC_DYSYMTAB)
Greg Clayton3046e662013-07-10 01:23:25 +00001401 {
1402 m_dysymtab.cmd = load_cmd.cmd;
1403 m_dysymtab.cmdsize = load_cmd.cmdsize;
1404 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1405 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406
Greg Clayton3046e662013-07-10 01:23:25 +00001407 offset = load_cmd_offset + load_cmd.cmdsize;
1408 }
1409
1410// StreamFile s(stdout, false); // REMOVE THIS LINE
1411// s.Printf ("Sections for %s:\n", m_file.GetPath().c_str());// REMOVE THIS LINE
1412// m_sections_ap->Dump(&s, NULL, true, UINT32_MAX);// REMOVE THIS LINE
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001413 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414}
1415
1416class MachSymtabSectionInfo
1417{
1418public:
1419
1420 MachSymtabSectionInfo (SectionList *section_list) :
1421 m_section_list (section_list),
1422 m_section_infos()
1423 {
1424 // Get the number of sections down to a depth of 1 to include
1425 // all segments and their sections, but no other sections that
1426 // may be added for debug map or
1427 m_section_infos.resize(section_list->GetNumSections(1));
1428 }
1429
1430
Greg Claytone72dfb32012-02-24 01:59:29 +00001431 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001432 GetSection (uint8_t n_sect, addr_t file_addr)
1433 {
1434 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00001435 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436 if (n_sect < m_section_infos.size())
1437 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001438 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001440 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1441 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00001442 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00001443 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001444 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1445 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00001446 }
1447 else
1448 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001449 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00001450 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451 }
1452 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00001453 {
1454 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00001455 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001456 }
1457 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1458 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1459 {
1460 // Symbol is in section with zero size, but has the same start
1461 // address as the section. This can happen with linker symbols
1462 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00001463 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00001464 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001465 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001466 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467 }
1468
1469protected:
1470 struct SectionInfo
1471 {
1472 SectionInfo () :
1473 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001474 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001475 {
1476 }
1477
1478 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00001479 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480 };
1481 SectionList *m_section_list;
1482 std::vector<SectionInfo> m_section_infos;
1483};
1484
Greg Clayton9191db42013-10-21 18:40:51 +00001485struct TrieEntry
1486{
1487 TrieEntry () :
1488 name(),
1489 address(LLDB_INVALID_ADDRESS),
1490 flags (0),
1491 other(0),
1492 import_name()
1493 {
1494 }
1495
1496 void
1497 Clear ()
1498 {
1499 name.Clear();
1500 address = LLDB_INVALID_ADDRESS;
1501 flags = 0;
1502 other = 0;
1503 import_name.Clear();
1504 }
1505
1506 void
1507 Dump () const
1508 {
1509 printf ("0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"", address, flags, other, name.GetCString());
1510 if (import_name)
1511 printf (" -> \"%s\"\n", import_name.GetCString());
1512 else
1513 printf ("\n");
1514 }
1515 ConstString name;
1516 uint64_t address;
1517 uint64_t flags;
1518 uint64_t other;
1519 ConstString import_name;
1520};
1521
1522struct TrieEntryWithOffset
1523{
1524 lldb::offset_t nodeOffset;
1525 TrieEntry entry;
1526
1527 TrieEntryWithOffset (lldb::offset_t offset) :
1528 nodeOffset (offset),
1529 entry()
1530 {
1531 }
1532
1533 void
1534 Dump (uint32_t idx) const
1535 {
1536 printf ("[%3u] 0x%16.16llx: ", idx, nodeOffset);
1537 entry.Dump();
1538 }
1539
1540 bool
1541 operator<(const TrieEntryWithOffset& other) const
1542 {
1543 return ( nodeOffset < other.nodeOffset );
1544 }
1545};
1546
1547static void
1548ParseTrieEntries (DataExtractor &data,
1549 lldb::offset_t offset,
1550 std::vector<llvm::StringRef> &nameSlices,
1551 std::set<lldb::addr_t> &resolver_addresses,
1552 std::vector<TrieEntryWithOffset>& output)
1553{
1554 if (!data.ValidOffset(offset))
1555 return;
1556
1557 const uint64_t terminalSize = data.GetULEB128(&offset);
1558 lldb::offset_t children_offset = offset + terminalSize;
1559 if ( terminalSize != 0 ) {
1560 TrieEntryWithOffset e (offset);
1561 e.entry.flags = data.GetULEB128(&offset);
1562 const char *import_name = NULL;
1563 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
1564 e.entry.address = 0;
1565 e.entry.other = data.GetULEB128(&offset); // dylib ordinal
1566 import_name = data.GetCStr(&offset);
1567 }
1568 else {
1569 e.entry.address = data.GetULEB128(&offset);
1570 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
1571 {
Jim Inghamea3ac272014-01-10 22:55:37 +00001572 //resolver_addresses.insert(e.entry.address);
Greg Clayton9191db42013-10-21 18:40:51 +00001573 e.entry.other = data.GetULEB128(&offset);
Jim Inghamea3ac272014-01-10 22:55:37 +00001574 resolver_addresses.insert(e.entry.other);
Greg Clayton9191db42013-10-21 18:40:51 +00001575 }
1576 else
1577 e.entry.other = 0;
1578 }
1579 // Only add symbols that are reexport symbols with a valid import name
1580 if (EXPORT_SYMBOL_FLAGS_REEXPORT & e.entry.flags && import_name && import_name[0])
1581 {
1582 std::string name;
1583 if (!nameSlices.empty())
1584 {
1585 for (auto name_slice: nameSlices)
1586 name.append(name_slice.data(), name_slice.size());
1587 }
1588 if (name.size() > 1)
1589 {
1590 // Skip the leading '_'
1591 e.entry.name.SetCStringWithLength(name.c_str() + 1,name.size() - 1);
1592 }
1593 if (import_name)
1594 {
1595 // Skip the leading '_'
1596 e.entry.import_name.SetCString(import_name+1);
1597 }
1598 output.push_back(e);
1599 }
1600 }
1601
1602 const uint8_t childrenCount = data.GetU8(&children_offset);
1603 for (uint8_t i=0; i < childrenCount; ++i) {
1604 nameSlices.push_back(data.GetCStr(&children_offset));
1605 lldb::offset_t childNodeOffset = data.GetULEB128(&children_offset);
1606 if (childNodeOffset)
1607 {
1608 ParseTrieEntries(data,
1609 childNodeOffset,
1610 nameSlices,
1611 resolver_addresses,
1612 output);
1613 }
1614 nameSlices.pop_back();
1615 }
1616}
1617
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001618size_t
Greg Clayton3046e662013-07-10 01:23:25 +00001619ObjectFileMachO::ParseSymtab ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001620{
1621 Timer scoped_timer(__PRETTY_FUNCTION__,
1622 "ObjectFileMachO::ParseSymtab () module = %s",
1623 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001624 ModuleSP module_sp (GetModule());
1625 if (!module_sp)
1626 return 0;
1627
1628 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1629 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
Greg Clayton9191db42013-10-21 18:40:51 +00001630 struct dyld_info_command dyld_info = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001631 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1632 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00001633 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001634 uint32_t i;
Greg Clayton9191db42013-10-21 18:40:51 +00001635 FileSpecList dylib_files;
Greg Clayton5160ce52013-03-27 23:08:40 +00001636 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton77ccca72011-12-30 00:32:24 +00001637
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001638 for (i=0; i<m_header.ncmds; ++i)
1639 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001640 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001641 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001642 struct load_command lc;
1643 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001644 break;
1645 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001646 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001647 {
Charles Davis510938e2013-08-27 05:04:57 +00001648 case LC_SYMTAB:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001649 symtab_load_command.cmd = lc.cmd;
1650 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001651 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001652 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1653 return 0;
1654 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001656 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001657 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001658 return 0;
1659 }
1660
1661 if (symtab_load_command.stroff == 0)
1662 {
1663 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001664 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001665 return 0;
1666 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001667
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001668 if (symtab_load_command.nsyms == 0)
1669 {
1670 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001671 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001672 return 0;
1673 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001674
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001675 if (symtab_load_command.strsize == 0)
1676 {
1677 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001678 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001679 return 0;
1680 }
1681 break;
1682
Greg Clayton9191db42013-10-21 18:40:51 +00001683 case LC_DYLD_INFO:
1684 case LC_DYLD_INFO_ONLY:
1685 if (m_data.GetU32(&offset, &dyld_info.rebase_off, 10))
1686 {
1687 dyld_info.cmd = lc.cmd;
1688 dyld_info.cmdsize = lc.cmdsize;
1689 }
1690 else
1691 {
1692 memset (&dyld_info, 0, sizeof(dyld_info));
1693 }
1694 break;
1695
1696 case LC_LOAD_DYLIB:
1697 case LC_LOAD_WEAK_DYLIB:
1698 case LC_REEXPORT_DYLIB:
1699 case LC_LOADFVMLIB:
1700 case LC_LOAD_UPWARD_DYLIB:
1701 {
1702 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1703 const char *path = m_data.PeekCStr(name_offset);
1704 if (path)
1705 {
1706 FileSpec file_spec(path, false);
1707 // Strip the path if there is @rpath, @executanble, etc so we just use the basename
1708 if (path[0] == '@')
1709 file_spec.GetDirectory().Clear();
1710
1711 dylib_files.Append(file_spec);
1712 }
1713 }
1714 break;
1715
Charles Davis510938e2013-08-27 05:04:57 +00001716 case LC_FUNCTION_STARTS:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001717 function_starts_load_command.cmd = lc.cmd;
1718 function_starts_load_command.cmdsize = lc.cmdsize;
1719 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 +00001720 memset (&function_starts_load_command, 0, sizeof(function_starts_load_command));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001721 break;
1722
1723 default:
1724 break;
1725 }
1726 offset = cmd_offset + lc.cmdsize;
1727 }
1728
1729 if (symtab_load_command.cmd)
1730 {
1731 Symtab *symtab = m_symtab_ap.get();
1732 SectionList *section_list = GetSectionList();
1733 if (section_list == NULL)
1734 return 0;
1735
Greg Claytonc7bece562013-01-25 18:06:21 +00001736 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1737 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001738 bool bit_width_32 = addr_byte_size == 4;
1739 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1740
Greg Claytonc7bece562013-01-25 18:06:21 +00001741 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1742 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1743 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00001744 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00001745 DataExtractor dyld_trie_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00001746
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001747 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1748 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00001749 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
Greg Claytonfd814c52013-08-13 01:42:25 +00001750
1751 ProcessSP process_sp (m_process_wp.lock());
1752 Process *process = process_sp.get();
1753
Greg Clayton86eac942013-08-13 21:32:34 +00001754 uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete;
1755
Greg Clayton4c82d422012-05-18 23:20:01 +00001756 if (process)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001757 {
Greg Clayton4c82d422012-05-18 23:20:01 +00001758 Target &target = process->GetTarget();
Greg Claytonfd814c52013-08-13 01:42:25 +00001759
Greg Clayton86eac942013-08-13 21:32:34 +00001760 memory_module_load_level = target.GetMemoryModuleLoadLevel();
Greg Claytonfd814c52013-08-13 01:42:25 +00001761
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001762 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1763 // Reading mach file from memory in a process or core file...
1764
1765 if (linkedit_section_sp)
1766 {
1767 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1768 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1769 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00001770 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00001771
1772 bool data_was_read = false;
1773
1774#if defined (__APPLE__) && defined (__arm__)
1775 if (m_header.flags & 0x80000000u)
Greg Clayton77ccca72011-12-30 00:32:24 +00001776 {
Greg Clayton26b47e22012-04-18 05:19:20 +00001777 // This mach-o memory file is in the dyld shared cache. If this
1778 // program is not remote and this is iOS, then this process will
1779 // share the same shared cache as the process we are debugging and
1780 // we can read the entire __LINKEDIT from the address space in this
1781 // process. This is a needed optimization that is used for local iOS
1782 // debugging only since all shared libraries in the shared cache do
1783 // not have corresponding files that exist in the file system of the
1784 // device. They have been combined into a single file. This means we
1785 // always have to load these files from memory. All of the symbol and
1786 // string tables from all of the __LINKEDIT sections from the shared
1787 // libraries in the shared cache have been merged into a single large
1788 // symbol and string table. Reading all of this symbol and string table
1789 // data across can slow down debug launch times, so we optimize this by
1790 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00001791
1792 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1793 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1794 bool use_lldb_cache = true;
1795 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1796 {
1797 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00001798 ModuleSP module_sp (GetModule());
1799 if (module_sp)
1800 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1801
Jason Molenda0e0954c2013-04-16 06:24:42 +00001802 }
1803
Greg Clayton26b47e22012-04-18 05:19:20 +00001804 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00001805 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00001806 {
1807 data_was_read = true;
1808 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00001809 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00001810 if (function_starts_load_command.cmd)
1811 {
1812 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1813 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1814 }
1815 }
1816 }
1817#endif
1818
1819 if (!data_was_read)
1820 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001821 if (memory_module_load_level == eMemoryModuleLoadLevelComplete)
Jason Molendad34e6522013-02-05 22:31:24 +00001822 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001823 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1824 if (nlist_data_sp)
1825 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
1826 // Load strings individually from memory when loading from memory since shared cache
1827 // string tables contain strings for all symbols from all shared cached libraries
1828 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1829 //if (strtab_data_sp)
1830 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
1831 if (m_dysymtab.nindirectsyms != 0)
1832 {
1833 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1834 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1835 if (indirect_syms_data_sp)
1836 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1837 }
Jason Molendad34e6522013-02-05 22:31:24 +00001838 }
Greg Claytonfd814c52013-08-13 01:42:25 +00001839
1840 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial)
Greg Clayton26b47e22012-04-18 05:19:20 +00001841 {
Greg Claytonfd814c52013-08-13 01:42:25 +00001842 if (function_starts_load_command.cmd)
1843 {
1844 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1845 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1846 if (func_start_data_sp)
1847 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1848 }
Greg Clayton26b47e22012-04-18 05:19:20 +00001849 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001850 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001851 }
1852 }
1853 else
1854 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001855 nlist_data.SetData (m_data,
1856 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001857 nlist_data_byte_size);
1858 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00001859 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001860 strtab_data_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00001861
1862 if (dyld_info.export_size > 0)
1863 {
1864 dyld_trie_data.SetData (m_data,
1865 dyld_info.export_off,
1866 dyld_info.export_size);
1867 }
1868
Jason Molendad34e6522013-02-05 22:31:24 +00001869 if (m_dysymtab.nindirectsyms != 0)
1870 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00001871 indirect_symbol_index_data.SetData (m_data,
1872 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00001873 m_dysymtab.nindirectsyms * 4);
1874 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001875 if (function_starts_load_command.cmd)
1876 {
1877 function_starts_data.SetData (m_data,
1878 function_starts_load_command.dataoff,
1879 function_starts_load_command.datasize);
1880 }
1881 }
Greg Clayton77ccca72011-12-30 00:32:24 +00001882
Greg Clayton86eac942013-08-13 21:32:34 +00001883 if (nlist_data.GetByteSize() == 0 && memory_module_load_level == eMemoryModuleLoadLevelComplete)
1884 {
1885 if (log)
1886 module_sp->LogMessage(log, "failed to read nlist data");
1887 return 0;
1888 }
1889
1890
Greg Claytondebb8812012-05-25 17:04:00 +00001891 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1892 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00001893 {
Greg Claytondebb8812012-05-25 17:04:00 +00001894 if (process)
1895 {
1896 if (strtab_addr == LLDB_INVALID_ADDRESS)
1897 {
1898 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001899 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00001900 return 0;
1901 }
1902 }
1903 else
Greg Clayton4c82d422012-05-18 23:20:01 +00001904 {
1905 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00001906 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00001907 return 0;
1908 }
1909 }
Greg Clayton4c82d422012-05-18 23:20:01 +00001910
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001911 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1912 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1913 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1914 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1915 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1916 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1917 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1918 SectionSP eh_frame_section_sp;
1919 if (text_section_sp.get())
1920 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1921 else
1922 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1923
Charles Davis510938e2013-08-27 05:04:57 +00001924 const bool is_arm = (m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
Jason Molenda5635f772013-03-21 03:36:01 +00001925
1926 // lldb works best if it knows the start addresss of all functions in a module.
1927 // Linker symbols or debug info are normally the best source of information for start addr / size but
1928 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00001929 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00001930 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1931 // binary, relative to the text section.
1932 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1933 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1934 // Binaries built to run on older releases may need to use eh_frame information.
1935
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001936 if (text_section_sp && function_starts_data.GetByteSize())
1937 {
1938 FunctionStarts::Entry function_start_entry;
1939 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00001940 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001941 function_start_entry.addr = text_section_sp->GetFileAddress();
1942 uint64_t delta;
1943 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1944 {
1945 // Now append the current entry
1946 function_start_entry.addr += delta;
1947 function_starts.Append(function_start_entry);
1948 }
Jason Molendad63d3c72013-04-16 00:18:44 +00001949 }
Jason Molenda5635f772013-03-21 03:36:01 +00001950 else
1951 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00001952 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1953 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1954 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1955 // the module.
1956 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00001957 {
1958 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1959 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1960 eh_frame.GetFunctionAddressAndSizeVector (functions);
1961 addr_t text_base_addr = text_section_sp->GetFileAddress();
1962 size_t count = functions.GetSize();
1963 for (size_t i = 0; i < count; ++i)
1964 {
1965 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1966 if (func)
1967 {
1968 FunctionStarts::Entry function_start_entry;
1969 function_start_entry.addr = func->base - text_base_addr;
1970 function_starts.Append(function_start_entry);
1971 }
1972 }
1973 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001974 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001975
Greg Claytonc7bece562013-01-25 18:06:21 +00001976 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001977
Charles Davis510938e2013-08-27 05:04:57 +00001978 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NO_SECT;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001979
Greg Claytonc7bece562013-01-25 18:06:21 +00001980 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001981
1982 uint32_t N_SO_index = UINT32_MAX;
1983
1984 MachSymtabSectionInfo section_info (section_list);
1985 std::vector<uint32_t> N_FUN_indexes;
1986 std::vector<uint32_t> N_NSYM_indexes;
1987 std::vector<uint32_t> N_INCL_indexes;
1988 std::vector<uint32_t> N_BRAC_indexes;
1989 std::vector<uint32_t> N_COMM_indexes;
Greg Claytond81088c2014-01-16 01:38:29 +00001990 typedef std::multimap <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001991 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00001992 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001993 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1994 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00001995 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00001996 // Any symbols that get merged into another will get an entry
1997 // in this map so we know
1998 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1999 uint32_t nlist_idx = 0;
2000 Symbol *symbol_ptr = NULL;
2001
2002 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00002003 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00002004 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00002005 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00002006 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002007
Jim Inghamea3ac272014-01-10 22:55:37 +00002008 std::vector<TrieEntryWithOffset> trie_entries;
2009 std::set<lldb::addr_t> resolver_addresses;
2010
2011 if (dyld_trie_data.GetByteSize() > 0)
2012 {
2013 std::vector<llvm::StringRef> nameSlices;
2014 ParseTrieEntries (dyld_trie_data,
2015 0,
2016 nameSlices,
2017 resolver_addresses,
2018 trie_entries);
2019
2020 ConstString text_segment_name ("__TEXT");
2021 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
2022 if (text_segment_sp)
2023 {
2024 const lldb::addr_t text_segment_file_addr = text_segment_sp->GetFileAddress();
2025 if (text_segment_file_addr != LLDB_INVALID_ADDRESS)
2026 {
2027 for (auto &e : trie_entries)
2028 e.entry.address += text_segment_file_addr;
2029 }
2030 }
2031 }
2032
Jason Molendaa5609c82012-06-21 01:51:02 +00002033#if defined (__APPLE__) && defined (__arm__)
2034
2035 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
2036 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
2037 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
2038 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
2039 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
2040 // nlist parser to ignore all LOCAL symbols.
2041
2042 if (m_header.flags & 0x80000000u)
2043 {
2044 // Before we can start mapping the DSC, we need to make certain the target process is actually
2045 // using the cache we can find.
2046
Jason Molendaa5609c82012-06-21 01:51:02 +00002047 // Next we need to determine the correct path for the dyld shared cache.
2048
2049 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
2050 char dsc_path[PATH_MAX];
2051
2052 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00002053 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
2054 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00002055 header_arch.GetArchitectureName());
2056
2057 FileSpec dsc_filespec(dsc_path, false);
2058
2059 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molendad63d3c72013-04-16 00:18:44 +00002060 struct lldb_copy_dyld_cache_header_v0
Greg Clayton946f8902012-09-05 22:30:51 +00002061 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002062 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
2063 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
2064 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda4e7511e2013-03-06 23:19:17 +00002065 uint32_t imagesOffset;
2066 uint32_t imagesCount;
2067 uint64_t dyldBaseAddress;
2068 uint64_t codeSignatureOffset;
2069 uint64_t codeSignatureSize;
2070 uint64_t slideInfoOffset;
2071 uint64_t slideInfoSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00002072 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
2073 uint64_t localSymbolsSize; // size of local symbols information
2074 };
2075 struct lldb_copy_dyld_cache_header_v1
2076 {
2077 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
2078 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
2079 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
2080 uint32_t imagesOffset;
2081 uint32_t imagesCount;
2082 uint64_t dyldBaseAddress;
2083 uint64_t codeSignatureOffset;
2084 uint64_t codeSignatureSize;
2085 uint64_t slideInfoOffset;
2086 uint64_t slideInfoSize;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002087 uint64_t localSymbolsOffset;
2088 uint64_t localSymbolsSize;
Jason Molendad63d3c72013-04-16 00:18:44 +00002089 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Clayton946f8902012-09-05 22:30:51 +00002090 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00002091
Jason Molendad63d3c72013-04-16 00:18:44 +00002092 struct lldb_copy_dyld_cache_mapping_info
2093 {
2094 uint64_t address;
2095 uint64_t size;
2096 uint64_t fileOffset;
2097 uint32_t maxProt;
2098 uint32_t initProt;
2099 };
Jason Molenda255f9bb2013-03-06 23:17:36 +00002100
Greg Clayton946f8902012-09-05 22:30:51 +00002101 struct lldb_copy_dyld_cache_local_symbols_info
2102 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002103 uint32_t nlistOffset;
2104 uint32_t nlistCount;
2105 uint32_t stringsOffset;
2106 uint32_t stringsSize;
2107 uint32_t entriesOffset;
2108 uint32_t entriesCount;
Greg Clayton946f8902012-09-05 22:30:51 +00002109 };
2110 struct lldb_copy_dyld_cache_local_symbols_entry
2111 {
Jason Molendad63d3c72013-04-16 00:18:44 +00002112 uint32_t dylibOffset;
2113 uint32_t nlistStartIndex;
2114 uint32_t nlistCount;
Greg Clayton946f8902012-09-05 22:30:51 +00002115 };
Jason Molendaa5609c82012-06-21 01:51:02 +00002116
Jason Molendaf8130862012-06-22 03:28:35 +00002117 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
2118 The dyld_cache_local_symbols_info structure gives us three things:
2119 1. The start and count of the nlist records in the dyld_shared_cache file
2120 2. The start and size of the strings for these nlist records
2121 3. The start and count of dyld_cache_local_symbols_entry entries
2122
2123 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
2124 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002125 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00002126 and the count of how many nlist records there are for this dylib/framework.
2127 */
2128
Jason Molendaa5609c82012-06-21 01:51:02 +00002129 // Process the dsc header to find the unmapped symbols
2130 //
2131 // Save some VM space, do not map the entire cache in one shot.
2132
Jason Molenda255f9bb2013-03-06 23:17:36 +00002133 DataBufferSP dsc_data_sp;
2134 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
2135
2136 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00002137 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002138 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002139
Jason Molenda255f9bb2013-03-06 23:17:36 +00002140 char version_str[17];
2141 int version = -1;
2142 lldb::offset_t offset = 0;
2143 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
2144 version_str[16] = '\0';
2145 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
2146 {
2147 int v;
2148 if (::sscanf (version_str + 6, "%d", &v) == 1)
2149 {
2150 version = v;
2151 }
2152 }
2153
Jason Molenda0e0954c2013-04-16 06:24:42 +00002154 UUID dsc_uuid;
2155 if (version >= 1)
2156 {
2157 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
2158 uint8_t uuid_bytes[sizeof (uuid_t)];
2159 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
2160 dsc_uuid.SetBytes (uuid_bytes);
2161 }
2162
2163 bool uuid_match = true;
2164 if (dsc_uuid.IsValid() && process)
2165 {
2166 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
2167
2168 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
2169 {
2170 // The on-disk dyld_shared_cache file is not the same as the one in this
2171 // process' memory, don't use it.
2172 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002173 ModuleSP module_sp (GetModule());
2174 if (module_sp)
2175 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 +00002176 }
2177 }
2178
Jason Molenda4e7511e2013-03-06 23:19:17 +00002179 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00002180
Jason Molendaa5609c82012-06-21 01:51:02 +00002181 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
2182
2183 // If the mappingOffset points to a location inside the header, we've
2184 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda0e0954c2013-04-16 06:24:42 +00002185 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendaa5609c82012-06-21 01:51:02 +00002186 {
2187
Jason Molenda255f9bb2013-03-06 23:17:36 +00002188 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
2189 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
2190 offset = 0;
2191
2192 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
2193 // in the shared library cache need to be adjusted by an offset to match up with the
2194 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
2195 // recorded in mapping_offset_value.
2196 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
2197
2198 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00002199 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
2200 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
2201
Jason Molenda4e7511e2013-03-06 23:19:17 +00002202 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00002203 {
2204 // Map the local symbols
Jason Molenda4e7511e2013-03-06 23:19:17 +00002205 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00002206 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002207 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002208
2209 offset = 0;
2210
2211 // Read the local_symbols_infos struct in one shot
2212 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
2213 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
2214
Jason Molendaa5609c82012-06-21 01:51:02 +00002215 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
2216
Jason Molenda255f9bb2013-03-06 23:17:36 +00002217 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002218
2219 offset = local_symbols_info.entriesOffset;
2220 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
2221 {
2222 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
2223 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
2224 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
2225 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
2226
Jason Molenda4e7511e2013-03-06 23:19:17 +00002227 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00002228 {
2229 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
2230
2231 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
2232 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
2233 num_syms = symtab->GetNumSymbols();
2234
2235 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
2236 uint32_t string_table_offset = local_symbols_info.stringsOffset;
2237
Jason Molenda4e7511e2013-03-06 23:19:17 +00002238 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00002239 {
2240 /////////////////////////////
2241 {
2242 struct nlist_64 nlist;
2243 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2244 break;
2245
2246 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2247 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2248 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2249 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2250 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2251
2252 SymbolType type = eSymbolTypeInvalid;
2253 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2254
2255 if (symbol_name == NULL)
2256 {
2257 // No symbol should be NULL, even the symbols with no
2258 // string values should have an offset zero which points
2259 // to an empty C-string
2260 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002261 "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 +00002262 entry_index,
2263 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002264 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00002265 continue;
2266 }
2267 if (symbol_name[0] == '\0')
2268 symbol_name = NULL;
2269
2270 const char *symbol_name_non_abi_mangled = NULL;
2271
2272 SectionSP symbol_section;
2273 uint32_t symbol_byte_size = 0;
2274 bool add_nlist = true;
Charles Davis510938e2013-08-27 05:04:57 +00002275 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002276 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002277 bool is_gsym = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002278
2279 assert (sym_idx < num_syms);
2280
2281 sym[sym_idx].SetDebug (is_debug);
2282
2283 if (is_debug)
2284 {
2285 switch (nlist.n_type)
2286 {
Charles Davis510938e2013-08-27 05:04:57 +00002287 case N_GSYM:
2288 // global symbol: name,,NO_SECT,type,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002289 // Sometimes the N_GSYM value contains the address.
2290
2291 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2292 // have the same address, but we want to ensure that we always find only the real symbol,
2293 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2294 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2295 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2296 // same address.
2297
2298 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2299 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2300 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2301 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2302 add_nlist = false;
2303 else
2304 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002305 is_gsym = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002306 sym[sym_idx].SetExternal(true);
2307 if (nlist.n_value != 0)
2308 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2309 type = eSymbolTypeData;
2310 }
2311 break;
2312
Charles Davis510938e2013-08-27 05:04:57 +00002313 case N_FNAME:
2314 // procedure name (f77 kludge): name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002315 type = eSymbolTypeCompiler;
2316 break;
2317
Charles Davis510938e2013-08-27 05:04:57 +00002318 case N_FUN:
2319 // procedure: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002320 if (symbol_name)
2321 {
2322 type = eSymbolTypeCode;
2323 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2324
Greg Claytond81088c2014-01-16 01:38:29 +00002325 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002326 // We use the current number of symbols in the symbol table in lieu of
2327 // using nlist_idx in case we ever start trimming entries out
2328 N_FUN_indexes.push_back(sym_idx);
2329 }
2330 else
2331 {
2332 type = eSymbolTypeCompiler;
2333
2334 if ( !N_FUN_indexes.empty() )
2335 {
2336 // Copy the size of the function into the original STAB entry so we don't have
2337 // to hunt for it later
2338 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2339 N_FUN_indexes.pop_back();
2340 // We don't really need the end function STAB as it contains the size which
2341 // we already placed with the original symbol, so don't add it if we want a
2342 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002343 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002344 }
2345 }
2346 break;
2347
Charles Davis510938e2013-08-27 05:04:57 +00002348 case N_STSYM:
2349 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00002350 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002351 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2352 type = eSymbolTypeData;
2353 break;
2354
Charles Davis510938e2013-08-27 05:04:57 +00002355 case N_LCSYM:
2356 // .lcomm symbol: name,,n_sect,type,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002357 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2358 type = eSymbolTypeCommonBlock;
2359 break;
2360
Charles Davis510938e2013-08-27 05:04:57 +00002361 case N_BNSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002362 // We use the current number of symbols in the symbol table in lieu of
2363 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00002364 // Skip these if we want minimal symbol tables
2365 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002366 break;
2367
Charles Davis510938e2013-08-27 05:04:57 +00002368 case N_ENSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002369 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2370 // so that we can always skip the entire symbol if we need to navigate
2371 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00002372 // Skip these if we want minimal symbol tables
2373 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002374 break;
2375
2376
Charles Davis510938e2013-08-27 05:04:57 +00002377 case N_OPT:
2378 // emitted with gcc2_compiled and in gcc source
Jason Molendaa5609c82012-06-21 01:51:02 +00002379 type = eSymbolTypeCompiler;
2380 break;
2381
Charles Davis510938e2013-08-27 05:04:57 +00002382 case N_RSYM:
2383 // register sym: name,,NO_SECT,type,register
Jason Molendaa5609c82012-06-21 01:51:02 +00002384 type = eSymbolTypeVariable;
2385 break;
2386
Charles Davis510938e2013-08-27 05:04:57 +00002387 case N_SLINE:
2388 // src line: 0,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002389 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2390 type = eSymbolTypeLineEntry;
2391 break;
2392
Charles Davis510938e2013-08-27 05:04:57 +00002393 case N_SSYM:
2394 // structure elt: name,,NO_SECT,type,struct_offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002395 type = eSymbolTypeVariableType;
2396 break;
2397
Charles Davis510938e2013-08-27 05:04:57 +00002398 case N_SO:
2399 // source file name
Jason Molendaa5609c82012-06-21 01:51:02 +00002400 type = eSymbolTypeSourceFile;
2401 if (symbol_name == NULL)
2402 {
Greg Clayton3046e662013-07-10 01:23:25 +00002403 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002404 if (N_SO_index != UINT32_MAX)
2405 {
2406 // Set the size of the N_SO to the terminating index of this N_SO
2407 // so that we can always skip the entire N_SO if we need to navigate
2408 // more quickly at the source level when parsing STABS
2409 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00002410 symbol_ptr->SetByteSize(sym_idx);
Jason Molendaa5609c82012-06-21 01:51:02 +00002411 symbol_ptr->SetSizeIsSibling(true);
2412 }
2413 N_NSYM_indexes.clear();
2414 N_INCL_indexes.clear();
2415 N_BRAC_indexes.clear();
2416 N_COMM_indexes.clear();
2417 N_FUN_indexes.clear();
2418 N_SO_index = UINT32_MAX;
2419 }
2420 else
2421 {
2422 // We use the current number of symbols in the symbol table in lieu of
2423 // using nlist_idx in case we ever start trimming entries out
2424 const bool N_SO_has_full_path = symbol_name[0] == '/';
2425 if (N_SO_has_full_path)
2426 {
Greg Clayton3046e662013-07-10 01:23:25 +00002427 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002428 {
2429 // We have two consecutive N_SO entries where the first contains a directory
2430 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00002431 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002432 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2433 add_nlist = false;
2434 }
2435 else
2436 {
2437 // This is the first entry in a N_SO that contains a directory or
2438 // a full path to the source file
2439 N_SO_index = sym_idx;
2440 }
2441 }
Greg Clayton3046e662013-07-10 01:23:25 +00002442 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00002443 {
2444 // This is usually the second N_SO entry that contains just the filename,
2445 // so here we combine it with the first one if we are minimizing the symbol table
2446 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2447 if (so_path && so_path[0])
2448 {
2449 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00002450 const size_t double_slash_pos = full_so_path.find("//");
2451 if (double_slash_pos != std::string::npos)
2452 {
2453 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00002454 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Clayton0662d962012-09-07 20:29:13 +00002455 // and the second is the directory for the source file so you end up with
2456 // a path that looks like "/tmp/src//tmp/src/"
2457 FileSpec so_dir(so_path, false);
2458 if (!so_dir.Exists())
2459 {
2460 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2461 if (so_dir.Exists())
2462 {
2463 // Trim off the incorrect path
2464 full_so_path.erase(0, double_slash_pos + 1);
2465 }
2466 }
2467 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002468 if (*full_so_path.rbegin() != '/')
2469 full_so_path += '/';
2470 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00002471 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00002472 add_nlist = false;
2473 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2474 }
2475 }
Greg Clayton946f8902012-09-05 22:30:51 +00002476 else
2477 {
2478 // This could be a relative path to a N_SO
2479 N_SO_index = sym_idx;
2480 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002481 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002482 break;
2483
Charles Davis510938e2013-08-27 05:04:57 +00002484 case N_OSO:
2485 // object file name: name,,0,0,st_mtime
Jason Molendaa5609c82012-06-21 01:51:02 +00002486 type = eSymbolTypeObjectFile;
2487 break;
2488
Charles Davis510938e2013-08-27 05:04:57 +00002489 case N_LSYM:
2490 // local sym: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002491 type = eSymbolTypeLocal;
2492 break;
2493
2494 //----------------------------------------------------------------------
2495 // INCL scopes
2496 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00002497 case N_BINCL:
2498 // include file beginning: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00002499 // We use the current number of symbols in the symbol table in lieu of
2500 // using nlist_idx in case we ever start trimming entries out
2501 N_INCL_indexes.push_back(sym_idx);
2502 type = eSymbolTypeScopeBegin;
2503 break;
2504
Charles Davis510938e2013-08-27 05:04:57 +00002505 case N_EINCL:
2506 // include file end: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002507 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2508 // so that we can always skip the entire symbol if we need to navigate
2509 // more quickly at the source level when parsing STABS
2510 if ( !N_INCL_indexes.empty() )
2511 {
2512 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2513 symbol_ptr->SetByteSize(sym_idx + 1);
2514 symbol_ptr->SetSizeIsSibling(true);
2515 N_INCL_indexes.pop_back();
2516 }
2517 type = eSymbolTypeScopeEnd;
2518 break;
2519
Charles Davis510938e2013-08-27 05:04:57 +00002520 case N_SOL:
2521 // #included file name: name,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002522 type = eSymbolTypeHeaderFile;
2523
2524 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00002525 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002526 break;
2527
Charles Davis510938e2013-08-27 05:04:57 +00002528 case N_PARAMS:
2529 // compiler parameters: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002530 type = eSymbolTypeCompiler;
2531 break;
2532
Charles Davis510938e2013-08-27 05:04:57 +00002533 case N_VERSION:
2534 // compiler version: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002535 type = eSymbolTypeCompiler;
2536 break;
2537
Charles Davis510938e2013-08-27 05:04:57 +00002538 case N_OLEVEL:
2539 // compiler -O level: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002540 type = eSymbolTypeCompiler;
2541 break;
2542
Charles Davis510938e2013-08-27 05:04:57 +00002543 case N_PSYM:
2544 // parameter: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00002545 type = eSymbolTypeVariable;
2546 break;
2547
Charles Davis510938e2013-08-27 05:04:57 +00002548 case N_ENTRY:
2549 // alternate entry: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002550 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2551 type = eSymbolTypeLineEntry;
2552 break;
2553
2554 //----------------------------------------------------------------------
2555 // Left and Right Braces
2556 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00002557 case N_LBRAC:
2558 // left bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002559 // We use the current number of symbols in the symbol table in lieu of
2560 // using nlist_idx in case we ever start trimming entries out
2561 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2562 N_BRAC_indexes.push_back(sym_idx);
2563 type = eSymbolTypeScopeBegin;
2564 break;
2565
Charles Davis510938e2013-08-27 05:04:57 +00002566 case N_RBRAC:
2567 // right bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002568 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2569 // so that we can always skip the entire symbol if we need to navigate
2570 // more quickly at the source level when parsing STABS
2571 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2572 if ( !N_BRAC_indexes.empty() )
2573 {
2574 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2575 symbol_ptr->SetByteSize(sym_idx + 1);
2576 symbol_ptr->SetSizeIsSibling(true);
2577 N_BRAC_indexes.pop_back();
2578 }
2579 type = eSymbolTypeScopeEnd;
2580 break;
2581
Charles Davis510938e2013-08-27 05:04:57 +00002582 case N_EXCL:
2583 // deleted include file: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00002584 type = eSymbolTypeHeaderFile;
2585 break;
2586
2587 //----------------------------------------------------------------------
2588 // COMM scopes
2589 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00002590 case N_BCOMM:
2591 // begin common: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002592 // We use the current number of symbols in the symbol table in lieu of
2593 // using nlist_idx in case we ever start trimming entries out
2594 type = eSymbolTypeScopeBegin;
2595 N_COMM_indexes.push_back(sym_idx);
2596 break;
2597
Charles Davis510938e2013-08-27 05:04:57 +00002598 case N_ECOML:
2599 // end common (local name): 0,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002600 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2601 // Fall through
2602
Charles Davis510938e2013-08-27 05:04:57 +00002603 case N_ECOMM:
2604 // end common: name,,n_sect,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002605 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2606 // so that we can always skip the entire symbol if we need to navigate
2607 // more quickly at the source level when parsing STABS
2608 if ( !N_COMM_indexes.empty() )
2609 {
2610 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2611 symbol_ptr->SetByteSize(sym_idx + 1);
2612 symbol_ptr->SetSizeIsSibling(true);
2613 N_COMM_indexes.pop_back();
2614 }
2615 type = eSymbolTypeScopeEnd;
2616 break;
2617
Charles Davis510938e2013-08-27 05:04:57 +00002618 case N_LENG:
2619 // second stab entry with length information
Jason Molendaa5609c82012-06-21 01:51:02 +00002620 type = eSymbolTypeAdditional;
2621 break;
2622
2623 default: break;
2624 }
2625 }
2626 else
2627 {
Charles Davis510938e2013-08-27 05:04:57 +00002628 //uint8_t n_pext = N_PEXT & nlist.n_type;
2629 uint8_t n_type = N_TYPE & nlist.n_type;
2630 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Jason Molendaa5609c82012-06-21 01:51:02 +00002631
2632 switch (n_type)
2633 {
Charles Davis510938e2013-08-27 05:04:57 +00002634 case N_INDR: // Fall through
2635 case N_PBUD: // Fall through
2636 case N_UNDF:
Jason Molendaa5609c82012-06-21 01:51:02 +00002637 type = eSymbolTypeUndefined;
2638 break;
2639
Charles Davis510938e2013-08-27 05:04:57 +00002640 case N_ABS:
Jason Molendaa5609c82012-06-21 01:51:02 +00002641 type = eSymbolTypeAbsolute;
2642 break;
2643
Charles Davis510938e2013-08-27 05:04:57 +00002644 case N_SECT:
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002645 {
2646 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002647
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002648 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00002649 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002650 // TODO: warn about this?
2651 add_nlist = false;
2652 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002653 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002654
2655 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00002656 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002657 type = eSymbolTypeException;
2658 }
2659 else
2660 {
Charles Davis510938e2013-08-27 05:04:57 +00002661 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002662
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002663 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00002664 {
Charles Davis510938e2013-08-27 05:04:57 +00002665 case S_REGULAR: break; // regular section
2666 //case S_ZEROFILL: type = eSymbolTypeData; break; // zero fill on demand section
2667 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
2668 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
2669 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
2670 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2671 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2672 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2673 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2674 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2675 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
2676 //case S_COALESCED: type = eSymbolType; break; // section contains symbols that are to be coalesced
2677 //case S_GB_ZEROFILL: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2678 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2679 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
2680 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
2681 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002682 default: break;
Jason Molendaa5609c82012-06-21 01:51:02 +00002683 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002684
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002685 if (type == eSymbolTypeInvalid)
2686 {
2687 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2688 if (symbol_section->IsDescendant (text_section_sp.get()))
2689 {
Charles Davis510938e2013-08-27 05:04:57 +00002690 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
2691 S_ATTR_SELF_MODIFYING_CODE |
2692 S_ATTR_SOME_INSTRUCTIONS))
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002693 type = eSymbolTypeData;
2694 else
2695 type = eSymbolTypeCode;
2696 }
2697 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00002698 {
2699 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2700 {
2701 type = eSymbolTypeRuntime;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002702
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002703 if (symbol_name &&
2704 symbol_name[0] == '_' &&
2705 symbol_name[1] == 'O' &&
Jason Molendaa5609c82012-06-21 01:51:02 +00002706 symbol_name[2] == 'B')
2707 {
2708 llvm::StringRef symbol_name_ref(symbol_name);
2709 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2710 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2711 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2712 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2713 {
2714 symbol_name_non_abi_mangled = symbol_name + 1;
2715 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2716 type = eSymbolTypeObjCClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002717 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002718 }
2719 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2720 {
2721 symbol_name_non_abi_mangled = symbol_name + 1;
2722 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2723 type = eSymbolTypeObjCMetaClass;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002724 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002725 }
2726 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2727 {
2728 symbol_name_non_abi_mangled = symbol_name + 1;
2729 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2730 type = eSymbolTypeObjCIVar;
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002731 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002732 }
2733 }
2734 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002735 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00002736 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002737 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00002738 }
2739 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002740 {
2741 type = eSymbolTypeData;
2742 }
2743 }
2744 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2745 {
2746 type = eSymbolTypeTrampoline;
2747 }
2748 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2749 {
2750 type = eSymbolTypeRuntime;
2751 if (symbol_name && symbol_name[0] == '.')
2752 {
2753 llvm::StringRef symbol_name_ref(symbol_name);
2754 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2755 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00002756 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002757 symbol_name_non_abi_mangled = symbol_name;
2758 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2759 type = eSymbolTypeObjCClass;
2760 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002761 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002762 }
2763 }
2764 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002765 }
2766 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002767 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002768 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002769 }
2770
2771 if (add_nlist)
2772 {
2773 uint64_t symbol_value = nlist.n_value;
Jason Molendaa5609c82012-06-21 01:51:02 +00002774 if (symbol_name_non_abi_mangled)
2775 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00002776 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2777 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00002778 }
2779 else
2780 {
Greg Clayton3046e662013-07-10 01:23:25 +00002781 bool symbol_name_is_mangled = false;
2782
Jason Molendaa5609c82012-06-21 01:51:02 +00002783 if (symbol_name && symbol_name[0] == '_')
2784 {
2785 symbol_name_is_mangled = symbol_name[1] == '_';
2786 symbol_name++; // Skip the leading underscore
2787 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002788
Jason Molendaa5609c82012-06-21 01:51:02 +00002789 if (symbol_name)
2790 {
Greg Claytondacc4a92013-05-14 22:19:37 +00002791 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00002792 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00002793 if (is_gsym && is_debug)
2794 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
Jason Molendaa5609c82012-06-21 01:51:02 +00002795 }
2796 }
2797 if (symbol_section)
2798 {
2799 const addr_t section_file_addr = symbol_section->GetFileAddress();
2800 if (symbol_byte_size == 0 && function_starts_count > 0)
2801 {
2802 addr_t symbol_lookup_file_addr = nlist.n_value;
2803 // Do an exact address match for non-ARM addresses, else get the closest since
2804 // the symbol might be a thumb symbol which has an address with bit zero set
2805 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2806 if (is_arm && func_start_entry)
2807 {
2808 // Verify that the function start address is the symbol address (ARM)
2809 // or the symbol address + 1 (thumb)
2810 if (func_start_entry->addr != symbol_lookup_file_addr &&
2811 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2812 {
2813 // Not the right entry, NULL it out...
2814 func_start_entry = NULL;
2815 }
2816 }
2817 if (func_start_entry)
2818 {
2819 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00002820
Jason Molendaa5609c82012-06-21 01:51:02 +00002821 addr_t symbol_file_addr = func_start_entry->addr;
2822 uint32_t symbol_flags = 0;
2823 if (is_arm)
2824 {
2825 if (symbol_file_addr & 1)
2826 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2827 symbol_file_addr &= 0xfffffffffffffffeull;
2828 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002829
Jason Molendaa5609c82012-06-21 01:51:02 +00002830 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2831 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2832 if (next_func_start_entry)
2833 {
2834 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2835 // Be sure the clear the Thumb address bit when we calculate the size
2836 // from the current and next address
2837 if (is_arm)
2838 next_symbol_file_addr &= 0xfffffffffffffffeull;
2839 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2840 }
2841 else
2842 {
2843 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2844 }
2845 }
2846 }
2847 symbol_value -= section_file_addr;
2848 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002849
Greg Claytondacc4a92013-05-14 22:19:37 +00002850 if (is_debug == false)
2851 {
2852 if (type == eSymbolTypeCode)
2853 {
2854 // See if we can find a N_FUN entry for any code symbols.
2855 // If we do find a match, and the name matches, then we
2856 // can merge the two into just the function symbol to avoid
2857 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00002858 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
2859 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
2860 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00002861 {
Greg Claytond81088c2014-01-16 01:38:29 +00002862 bool found_it = false;
2863 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00002864 {
Greg Claytond81088c2014-01-16 01:38:29 +00002865 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
2866 {
2867 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2868 // We just need the flags from the linker symbol, so put these flags
2869 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2870 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
2871 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2872 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
2873 sym[pos->second].SetType (eSymbolTypeResolver);
2874 sym[sym_idx].Clear();
2875 found_it = true;
2876 break;
2877 }
Greg Claytondacc4a92013-05-14 22:19:37 +00002878 }
Greg Claytond81088c2014-01-16 01:38:29 +00002879 if (found_it)
2880 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00002881 }
Jim Inghamea3ac272014-01-10 22:55:37 +00002882 else
2883 {
Greg Claytond81088c2014-01-16 01:38:29 +00002884 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Jim Inghamea3ac272014-01-10 22:55:37 +00002885 sym[sym_idx].SetType (eSymbolTypeResolver);
2886 }
Greg Claytondacc4a92013-05-14 22:19:37 +00002887 }
2888 else if (type == eSymbolTypeData)
2889 {
2890 // See if we can find a N_STSYM entry for any data symbols.
2891 // If we do find a match, and the name matches, then we
2892 // can merge the two into just the Static symbol to avoid
2893 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00002894 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
2895 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
2896 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00002897 {
Greg Claytond81088c2014-01-16 01:38:29 +00002898 bool found_it = false;
2899 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00002900 {
Greg Claytond81088c2014-01-16 01:38:29 +00002901 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
2902 {
2903 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2904 // We just need the flags from the linker symbol, so put these flags
2905 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2906 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
2907 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2908 sym[sym_idx].Clear();
2909 found_it = true;
2910 break;
2911 }
Greg Claytondacc4a92013-05-14 22:19:37 +00002912 }
Greg Claytond81088c2014-01-16 01:38:29 +00002913 if (found_it)
2914 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00002915 }
2916 else
2917 {
2918 // Combine N_GSYM stab entries with the non stab symbol
Greg Clayton3046e662013-07-10 01:23:25 +00002919 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 +00002920 if (pos != N_GSYM_name_to_sym_idx.end())
2921 {
2922 const uint32_t GSYM_sym_idx = pos->second;
2923 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
2924 // Copy the address, because often the N_GSYM address has an invalid address of zero
2925 // when the global is a common symbol
2926 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
2927 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
2928 // We just need the flags from the linker symbol, so put these flags
2929 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2930 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2931 sym[sym_idx].Clear();
2932 continue;
2933 }
2934 }
2935 }
2936 }
2937
Jason Molendaa5609c82012-06-21 01:51:02 +00002938 sym[sym_idx].SetID (nlist_idx);
2939 sym[sym_idx].SetType (type);
2940 sym[sym_idx].GetAddress().SetSection (symbol_section);
2941 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2942 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002943
Jason Molendaa5609c82012-06-21 01:51:02 +00002944 if (symbol_byte_size > 0)
2945 sym[sym_idx].SetByteSize(symbol_byte_size);
2946
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002947 if (demangled_is_synthesized)
2948 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00002949 ++sym_idx;
2950 }
2951 else
2952 {
2953 sym[sym_idx].Clear();
2954 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002955
Jason Molendaa5609c82012-06-21 01:51:02 +00002956 }
2957 /////////////////////////////
2958 }
2959 break; // No more entries to consider
2960 }
2961 }
2962 }
2963 }
2964 }
2965 }
2966 }
2967
2968 // Must reset this in case it was mutated above!
2969 nlist_data_offset = 0;
2970#endif
Jim Inghamea3ac272014-01-10 22:55:37 +00002971
Greg Claytonfd814c52013-08-13 01:42:25 +00002972 if (nlist_data.GetByteSize() > 0)
Jason Molendaa5609c82012-06-21 01:51:02 +00002973 {
Jason Molendaa5609c82012-06-21 01:51:02 +00002974
Greg Claytonfd814c52013-08-13 01:42:25 +00002975 // If the sym array was not created while parsing the DSC unmapped
2976 // symbols, create it now.
2977 if (sym == NULL)
Greg Clayton4c82d422012-05-18 23:20:01 +00002978 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002979 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2980 num_syms = symtab->GetNumSymbols();
2981 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002982
Greg Claytonfd814c52013-08-13 01:42:25 +00002983 if (unmapped_local_symbols_found)
2984 {
2985 assert(m_dysymtab.ilocalsym == 0);
2986 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2987 nlist_idx = m_dysymtab.nlocalsym;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002988 }
Greg Claytondebb8812012-05-25 17:04:00 +00002989 else
2990 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002991 nlist_idx = 0;
Greg Claytondebb8812012-05-25 17:04:00 +00002992 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002993
Greg Claytonfd814c52013-08-13 01:42:25 +00002994 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002995 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002996 struct nlist_64 nlist;
2997 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2998 break;
2999
3000 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
3001 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
3002 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
3003 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
3004 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
3005
3006 SymbolType type = eSymbolTypeInvalid;
3007 const char *symbol_name = NULL;
3008
3009 if (have_strtab_data)
Greg Clayton77ccca72011-12-30 00:32:24 +00003010 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003011 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003012
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003013 if (symbol_name == NULL)
3014 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003015 // No symbol should be NULL, even the symbols with no
3016 // string values should have an offset zero which points
3017 // to an empty C-string
3018 Host::SystemLog (Host::eSystemLogError,
3019 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
3020 nlist_idx,
3021 nlist.n_strx,
3022 module_sp->GetFileSpec().GetPath().c_str());
3023 continue;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003024 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003025 if (symbol_name[0] == '\0')
3026 symbol_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003027 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003028 else
3029 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003030 const addr_t str_addr = strtab_addr + nlist.n_strx;
3031 Error str_error;
3032 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
3033 symbol_name = memory_symbol_name.c_str();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003034 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003035 const char *symbol_name_non_abi_mangled = NULL;
3036
3037 SectionSP symbol_section;
3038 lldb::addr_t symbol_byte_size = 0;
3039 bool add_nlist = true;
3040 bool is_gsym = false;
Charles Davis510938e2013-08-27 05:04:57 +00003041 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00003042 bool demangled_is_synthesized = false;
3043
3044 assert (sym_idx < num_syms);
3045
3046 sym[sym_idx].SetDebug (is_debug);
3047
3048 if (is_debug)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003049 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003050 switch (nlist.n_type)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003051 {
Charles Davis510938e2013-08-27 05:04:57 +00003052 case N_GSYM:
3053 // global symbol: name,,NO_SECT,type,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003054 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003055
Greg Claytonfd814c52013-08-13 01:42:25 +00003056 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
3057 // have the same address, but we want to ensure that we always find only the real symbol,
3058 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
3059 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
3060 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
3061 // same address.
Greg Clayton29e08cb2012-03-14 01:53:24 +00003062
Greg Claytonfd814c52013-08-13 01:42:25 +00003063 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
3064 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
3065 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
3066 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
3067 add_nlist = false;
3068 else
3069 {
3070 is_gsym = true;
3071 sym[sym_idx].SetExternal(true);
3072 if (nlist.n_value != 0)
3073 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3074 type = eSymbolTypeData;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003075 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003076 break;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003077
Charles Davis510938e2013-08-27 05:04:57 +00003078 case N_FNAME:
3079 // procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003080 type = eSymbolTypeCompiler;
3081 break;
3082
Charles Davis510938e2013-08-27 05:04:57 +00003083 case N_FUN:
3084 // procedure: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003085 if (symbol_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00003086 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003087 type = eSymbolTypeCode;
3088 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3089
Greg Claytond81088c2014-01-16 01:38:29 +00003090 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003091 // We use the current number of symbols in the symbol table in lieu of
3092 // using nlist_idx in case we ever start trimming entries out
3093 N_FUN_indexes.push_back(sym_idx);
Greg Claytondacc4a92013-05-14 22:19:37 +00003094 }
3095 else
3096 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003097 type = eSymbolTypeCompiler;
3098
3099 if ( !N_FUN_indexes.empty() )
Greg Claytondacc4a92013-05-14 22:19:37 +00003100 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003101 // Copy the size of the function into the original STAB entry so we don't have
3102 // to hunt for it later
3103 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
3104 N_FUN_indexes.pop_back();
3105 // We don't really need the end function STAB as it contains the size which
3106 // we already placed with the original symbol, so don't add it if we want a
3107 // minimal symbol table
3108 add_nlist = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00003109 }
3110 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003111 break;
3112
Charles Davis510938e2013-08-27 05:04:57 +00003113 case N_STSYM:
3114 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00003115 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003116 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3117 type = eSymbolTypeData;
3118 break;
3119
Charles Davis510938e2013-08-27 05:04:57 +00003120 case N_LCSYM:
3121 // .lcomm symbol: name,,n_sect,type,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003122 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3123 type = eSymbolTypeCommonBlock;
3124 break;
3125
Charles Davis510938e2013-08-27 05:04:57 +00003126 case N_BNSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003127 // We use the current number of symbols in the symbol table in lieu of
3128 // using nlist_idx in case we ever start trimming entries out
3129 // Skip these if we want minimal symbol tables
3130 add_nlist = false;
3131 break;
3132
Charles Davis510938e2013-08-27 05:04:57 +00003133 case N_ENSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003134 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
3135 // so that we can always skip the entire symbol if we need to navigate
3136 // more quickly at the source level when parsing STABS
3137 // Skip these if we want minimal symbol tables
3138 add_nlist = false;
3139 break;
3140
3141
Charles Davis510938e2013-08-27 05:04:57 +00003142 case N_OPT:
3143 // emitted with gcc2_compiled and in gcc source
Greg Claytonfd814c52013-08-13 01:42:25 +00003144 type = eSymbolTypeCompiler;
3145 break;
3146
Charles Davis510938e2013-08-27 05:04:57 +00003147 case N_RSYM:
3148 // register sym: name,,NO_SECT,type,register
Greg Claytonfd814c52013-08-13 01:42:25 +00003149 type = eSymbolTypeVariable;
3150 break;
3151
Charles Davis510938e2013-08-27 05:04:57 +00003152 case N_SLINE:
3153 // src line: 0,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003154 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3155 type = eSymbolTypeLineEntry;
3156 break;
3157
Charles Davis510938e2013-08-27 05:04:57 +00003158 case N_SSYM:
3159 // structure elt: name,,NO_SECT,type,struct_offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003160 type = eSymbolTypeVariableType;
3161 break;
3162
Charles Davis510938e2013-08-27 05:04:57 +00003163 case N_SO:
3164 // source file name
Greg Claytonfd814c52013-08-13 01:42:25 +00003165 type = eSymbolTypeSourceFile;
3166 if (symbol_name == NULL)
3167 {
3168 add_nlist = false;
3169 if (N_SO_index != UINT32_MAX)
3170 {
3171 // Set the size of the N_SO to the terminating index of this N_SO
3172 // so that we can always skip the entire N_SO if we need to navigate
3173 // more quickly at the source level when parsing STABS
3174 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
3175 symbol_ptr->SetByteSize(sym_idx);
3176 symbol_ptr->SetSizeIsSibling(true);
3177 }
3178 N_NSYM_indexes.clear();
3179 N_INCL_indexes.clear();
3180 N_BRAC_indexes.clear();
3181 N_COMM_indexes.clear();
3182 N_FUN_indexes.clear();
3183 N_SO_index = UINT32_MAX;
3184 }
3185 else
3186 {
3187 // We use the current number of symbols in the symbol table in lieu of
3188 // using nlist_idx in case we ever start trimming entries out
3189 const bool N_SO_has_full_path = symbol_name[0] == '/';
3190 if (N_SO_has_full_path)
3191 {
3192 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3193 {
3194 // We have two consecutive N_SO entries where the first contains a directory
3195 // and the second contains a full path.
3196 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
3197 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3198 add_nlist = false;
3199 }
3200 else
3201 {
3202 // This is the first entry in a N_SO that contains a directory or
3203 // a full path to the source file
3204 N_SO_index = sym_idx;
3205 }
3206 }
3207 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3208 {
3209 // This is usually the second N_SO entry that contains just the filename,
3210 // so here we combine it with the first one if we are minimizing the symbol table
3211 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
3212 if (so_path && so_path[0])
3213 {
3214 std::string full_so_path (so_path);
3215 const size_t double_slash_pos = full_so_path.find("//");
3216 if (double_slash_pos != std::string::npos)
3217 {
3218 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00003219 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Claytonfd814c52013-08-13 01:42:25 +00003220 // and the second is the directory for the source file so you end up with
3221 // a path that looks like "/tmp/src//tmp/src/"
3222 FileSpec so_dir(so_path, false);
3223 if (!so_dir.Exists())
3224 {
3225 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3226 if (so_dir.Exists())
3227 {
3228 // Trim off the incorrect path
3229 full_so_path.erase(0, double_slash_pos + 1);
3230 }
3231 }
3232 }
3233 if (*full_so_path.rbegin() != '/')
3234 full_so_path += '/';
3235 full_so_path += symbol_name;
3236 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
3237 add_nlist = false;
3238 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3239 }
3240 }
3241 else
3242 {
3243 // This could be a relative path to a N_SO
3244 N_SO_index = sym_idx;
3245 }
3246 }
3247
3248 break;
3249
Charles Davis510938e2013-08-27 05:04:57 +00003250 case N_OSO:
3251 // object file name: name,,0,0,st_mtime
Greg Claytonfd814c52013-08-13 01:42:25 +00003252 type = eSymbolTypeObjectFile;
3253 break;
3254
Charles Davis510938e2013-08-27 05:04:57 +00003255 case N_LSYM:
3256 // local sym: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003257 type = eSymbolTypeLocal;
3258 break;
3259
3260 //----------------------------------------------------------------------
3261 // INCL scopes
3262 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003263 case N_BINCL:
3264 // include file beginning: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00003265 // We use the current number of symbols in the symbol table in lieu of
3266 // using nlist_idx in case we ever start trimming entries out
3267 N_INCL_indexes.push_back(sym_idx);
3268 type = eSymbolTypeScopeBegin;
3269 break;
3270
Charles Davis510938e2013-08-27 05:04:57 +00003271 case N_EINCL:
3272 // include file end: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003273 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3274 // so that we can always skip the entire symbol if we need to navigate
3275 // more quickly at the source level when parsing STABS
3276 if ( !N_INCL_indexes.empty() )
3277 {
3278 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3279 symbol_ptr->SetByteSize(sym_idx + 1);
3280 symbol_ptr->SetSizeIsSibling(true);
3281 N_INCL_indexes.pop_back();
3282 }
3283 type = eSymbolTypeScopeEnd;
3284 break;
3285
Charles Davis510938e2013-08-27 05:04:57 +00003286 case N_SOL:
3287 // #included file name: name,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003288 type = eSymbolTypeHeaderFile;
3289
3290 // We currently don't use the header files on darwin
3291 add_nlist = false;
3292 break;
3293
Charles Davis510938e2013-08-27 05:04:57 +00003294 case N_PARAMS:
3295 // compiler parameters: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003296 type = eSymbolTypeCompiler;
3297 break;
3298
Charles Davis510938e2013-08-27 05:04:57 +00003299 case N_VERSION:
3300 // compiler version: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003301 type = eSymbolTypeCompiler;
3302 break;
3303
Charles Davis510938e2013-08-27 05:04:57 +00003304 case N_OLEVEL:
3305 // compiler -O level: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003306 type = eSymbolTypeCompiler;
3307 break;
3308
Charles Davis510938e2013-08-27 05:04:57 +00003309 case N_PSYM:
3310 // parameter: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003311 type = eSymbolTypeVariable;
3312 break;
3313
Charles Davis510938e2013-08-27 05:04:57 +00003314 case N_ENTRY:
3315 // alternate entry: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003316 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3317 type = eSymbolTypeLineEntry;
3318 break;
3319
3320 //----------------------------------------------------------------------
3321 // Left and Right Braces
3322 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003323 case N_LBRAC:
3324 // left bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003325 // We use the current number of symbols in the symbol table in lieu of
3326 // using nlist_idx in case we ever start trimming entries out
3327 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3328 N_BRAC_indexes.push_back(sym_idx);
3329 type = eSymbolTypeScopeBegin;
3330 break;
3331
Charles Davis510938e2013-08-27 05:04:57 +00003332 case N_RBRAC:
3333 // right bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003334 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3335 // so that we can always skip the entire symbol if we need to navigate
3336 // more quickly at the source level when parsing STABS
3337 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3338 if ( !N_BRAC_indexes.empty() )
3339 {
3340 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3341 symbol_ptr->SetByteSize(sym_idx + 1);
3342 symbol_ptr->SetSizeIsSibling(true);
3343 N_BRAC_indexes.pop_back();
3344 }
3345 type = eSymbolTypeScopeEnd;
3346 break;
3347
Charles Davis510938e2013-08-27 05:04:57 +00003348 case N_EXCL:
3349 // deleted include file: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00003350 type = eSymbolTypeHeaderFile;
3351 break;
3352
3353 //----------------------------------------------------------------------
3354 // COMM scopes
3355 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003356 case N_BCOMM:
3357 // begin common: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003358 // We use the current number of symbols in the symbol table in lieu of
3359 // using nlist_idx in case we ever start trimming entries out
3360 type = eSymbolTypeScopeBegin;
3361 N_COMM_indexes.push_back(sym_idx);
3362 break;
3363
Charles Davis510938e2013-08-27 05:04:57 +00003364 case N_ECOML:
3365 // end common (local name): 0,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003366 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3367 // Fall through
3368
Charles Davis510938e2013-08-27 05:04:57 +00003369 case N_ECOMM:
3370 // end common: name,,n_sect,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003371 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3372 // so that we can always skip the entire symbol if we need to navigate
3373 // more quickly at the source level when parsing STABS
3374 if ( !N_COMM_indexes.empty() )
3375 {
3376 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3377 symbol_ptr->SetByteSize(sym_idx + 1);
3378 symbol_ptr->SetSizeIsSibling(true);
3379 N_COMM_indexes.pop_back();
3380 }
3381 type = eSymbolTypeScopeEnd;
3382 break;
3383
Charles Davis510938e2013-08-27 05:04:57 +00003384 case N_LENG:
3385 // second stab entry with length information
Greg Claytonfd814c52013-08-13 01:42:25 +00003386 type = eSymbolTypeAdditional;
3387 break;
3388
3389 default: break;
3390 }
3391 }
3392 else
3393 {
Charles Davis510938e2013-08-27 05:04:57 +00003394 //uint8_t n_pext = N_PEXT & nlist.n_type;
3395 uint8_t n_type = N_TYPE & nlist.n_type;
3396 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00003397
3398 switch (n_type)
3399 {
Charles Davis510938e2013-08-27 05:04:57 +00003400 case N_INDR:// Fall through
3401 case N_PBUD:// Fall through
3402 case N_UNDF:
Greg Claytonfd814c52013-08-13 01:42:25 +00003403 type = eSymbolTypeUndefined;
3404 break;
3405
Charles Davis510938e2013-08-27 05:04:57 +00003406 case N_ABS:
Greg Claytonfd814c52013-08-13 01:42:25 +00003407 type = eSymbolTypeAbsolute;
3408 break;
3409
Charles Davis510938e2013-08-27 05:04:57 +00003410 case N_SECT:
Greg Claytonfd814c52013-08-13 01:42:25 +00003411 {
3412 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3413
3414 if (!symbol_section)
3415 {
3416 // TODO: warn about this?
3417 add_nlist = false;
3418 break;
3419 }
3420
3421 if (TEXT_eh_frame_sectID == nlist.n_sect)
3422 {
3423 type = eSymbolTypeException;
3424 }
3425 else
3426 {
Charles Davis510938e2013-08-27 05:04:57 +00003427 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Greg Claytonfd814c52013-08-13 01:42:25 +00003428
3429 switch (section_type)
3430 {
Charles Davis510938e2013-08-27 05:04:57 +00003431 case S_REGULAR: break; // regular section
3432 //case S_ZEROFILL: type = eSymbolTypeData; break; // zero fill on demand section
3433 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
3434 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
3435 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
3436 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3437 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3438 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3439 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3440 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3441 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
3442 //case S_COALESCED: type = eSymbolType; break; // section contains symbols that are to be coalesced
3443 //case S_GB_ZEROFILL: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3444 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3445 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
3446 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
3447 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Claytonfd814c52013-08-13 01:42:25 +00003448 default: break;
3449 }
3450
3451 if (type == eSymbolTypeInvalid)
3452 {
3453 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3454 if (symbol_section->IsDescendant (text_section_sp.get()))
3455 {
Charles Davis510938e2013-08-27 05:04:57 +00003456 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
3457 S_ATTR_SELF_MODIFYING_CODE |
3458 S_ATTR_SOME_INSTRUCTIONS))
Greg Claytonfd814c52013-08-13 01:42:25 +00003459 type = eSymbolTypeData;
3460 else
3461 type = eSymbolTypeCode;
3462 }
3463 else
3464 if (symbol_section->IsDescendant(data_section_sp.get()))
3465 {
3466 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
3467 {
3468 type = eSymbolTypeRuntime;
3469
3470 if (symbol_name &&
3471 symbol_name[0] == '_' &&
3472 symbol_name[1] == 'O' &&
3473 symbol_name[2] == 'B')
3474 {
3475 llvm::StringRef symbol_name_ref(symbol_name);
3476 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3477 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3478 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3479 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3480 {
3481 symbol_name_non_abi_mangled = symbol_name + 1;
3482 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3483 type = eSymbolTypeObjCClass;
3484 demangled_is_synthesized = true;
3485 }
3486 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3487 {
3488 symbol_name_non_abi_mangled = symbol_name + 1;
3489 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3490 type = eSymbolTypeObjCMetaClass;
3491 demangled_is_synthesized = true;
3492 }
3493 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3494 {
3495 symbol_name_non_abi_mangled = symbol_name + 1;
3496 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3497 type = eSymbolTypeObjCIVar;
3498 demangled_is_synthesized = true;
3499 }
3500 }
3501 }
3502 else
3503 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3504 {
3505 type = eSymbolTypeException;
3506 }
3507 else
3508 {
3509 type = eSymbolTypeData;
3510 }
3511 }
3512 else
3513 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3514 {
3515 type = eSymbolTypeTrampoline;
3516 }
3517 else
3518 if (symbol_section->IsDescendant(objc_section_sp.get()))
3519 {
3520 type = eSymbolTypeRuntime;
3521 if (symbol_name && symbol_name[0] == '.')
3522 {
3523 llvm::StringRef symbol_name_ref(symbol_name);
3524 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3525 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3526 {
3527 symbol_name_non_abi_mangled = symbol_name;
3528 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3529 type = eSymbolTypeObjCClass;
3530 demangled_is_synthesized = true;
3531 }
3532 }
3533 }
3534 }
3535 }
3536 }
3537 break;
Greg Claytondacc4a92013-05-14 22:19:37 +00003538 }
3539 }
3540
Greg Claytonfd814c52013-08-13 01:42:25 +00003541 if (add_nlist)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003542 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003543 uint64_t symbol_value = nlist.n_value;
3544
3545 if (symbol_name_non_abi_mangled)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003546 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003547 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3548 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
3549 }
3550 else
3551 {
3552 bool symbol_name_is_mangled = false;
3553
3554 if (symbol_name && symbol_name[0] == '_')
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003555 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003556 symbol_name_is_mangled = symbol_name[1] == '_';
3557 symbol_name++; // Skip the leading underscore
3558 }
3559
3560 if (symbol_name)
3561 {
3562 ConstString const_symbol_name(symbol_name);
3563 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
3564 if (is_gsym && is_debug)
3565 {
3566 N_GSYM_name_to_sym_idx[sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString()] = sym_idx;
3567 }
3568 }
3569 }
3570 if (symbol_section)
3571 {
3572 const addr_t section_file_addr = symbol_section->GetFileAddress();
3573 if (symbol_byte_size == 0 && function_starts_count > 0)
3574 {
3575 addr_t symbol_lookup_file_addr = nlist.n_value;
3576 // Do an exact address match for non-ARM addresses, else get the closest since
3577 // the symbol might be a thumb symbol which has an address with bit zero set
3578 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3579 if (is_arm && func_start_entry)
3580 {
3581 // Verify that the function start address is the symbol address (ARM)
3582 // or the symbol address + 1 (thumb)
3583 if (func_start_entry->addr != symbol_lookup_file_addr &&
3584 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3585 {
3586 // Not the right entry, NULL it out...
3587 func_start_entry = NULL;
3588 }
3589 }
3590 if (func_start_entry)
3591 {
3592 func_start_entry->data = true;
3593
3594 addr_t symbol_file_addr = func_start_entry->addr;
3595 if (is_arm)
3596 symbol_file_addr &= 0xfffffffffffffffeull;
3597
3598 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3599 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3600 if (next_func_start_entry)
3601 {
3602 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3603 // Be sure the clear the Thumb address bit when we calculate the size
3604 // from the current and next address
3605 if (is_arm)
3606 next_symbol_file_addr &= 0xfffffffffffffffeull;
3607 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3608 }
3609 else
3610 {
3611 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3612 }
3613 }
3614 }
3615 symbol_value -= section_file_addr;
3616 }
3617
3618 if (is_debug == false)
3619 {
3620 if (type == eSymbolTypeCode)
3621 {
3622 // See if we can find a N_FUN entry for any code symbols.
3623 // If we do find a match, and the name matches, then we
3624 // can merge the two into just the function symbol to avoid
3625 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003626 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3627 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3628 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00003629 {
Greg Claytond81088c2014-01-16 01:38:29 +00003630 bool found_it = false;
3631 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00003632 {
Greg Claytond81088c2014-01-16 01:38:29 +00003633 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3634 {
3635 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3636 // We just need the flags from the linker symbol, so put these flags
3637 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3638 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3639 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3640 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3641 sym[pos->second].SetType (eSymbolTypeResolver);
3642 sym[sym_idx].Clear();
3643 found_it = true;
3644 break;
3645 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003646 }
Greg Claytond81088c2014-01-16 01:38:29 +00003647 if (found_it)
3648 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00003649 }
Jim Inghamea3ac272014-01-10 22:55:37 +00003650 else
3651 {
3652 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3653 sym[sym_idx].SetType (eSymbolTypeResolver);
3654 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003655 }
3656 else if (type == eSymbolTypeData)
3657 {
3658 // See if we can find a N_STSYM entry for any data symbols.
3659 // If we do find a match, and the name matches, then we
3660 // can merge the two into just the Static symbol to avoid
3661 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003662 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3663 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
3664 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00003665 {
Greg Claytond81088c2014-01-16 01:38:29 +00003666 bool found_it = false;
3667 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00003668 {
Greg Claytond81088c2014-01-16 01:38:29 +00003669 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3670 {
3671 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3672 // We just need the flags from the linker symbol, so put these flags
3673 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3674 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3675 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3676 sym[sym_idx].Clear();
3677 found_it = true;
3678 break;
3679 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003680 }
Greg Claytond81088c2014-01-16 01:38:29 +00003681 if (found_it)
3682 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00003683 }
3684 else
3685 {
3686 // Combine N_GSYM stab entries with the non stab symbol
3687 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString());
3688 if (pos != N_GSYM_name_to_sym_idx.end())
3689 {
3690 const uint32_t GSYM_sym_idx = pos->second;
3691 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3692 // Copy the address, because often the N_GSYM address has an invalid address of zero
3693 // when the global is a common symbol
3694 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3695 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3696 // We just need the flags from the linker symbol, so put these flags
3697 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3698 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3699 sym[sym_idx].Clear();
3700 continue;
3701 }
3702 }
3703 }
3704 }
3705
3706 sym[sym_idx].SetID (nlist_idx);
3707 sym[sym_idx].SetType (type);
3708 sym[sym_idx].GetAddress().SetSection (symbol_section);
3709 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3710 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3711
3712 if (symbol_byte_size > 0)
3713 sym[sym_idx].SetByteSize(symbol_byte_size);
3714
3715 if (demangled_is_synthesized)
3716 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3717
3718 ++sym_idx;
3719 }
3720 else
3721 {
3722 sym[sym_idx].Clear();
3723 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003724 }
3725 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003726
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003727 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3728
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003729 if (function_starts_count > 0)
3730 {
3731 char synthetic_function_symbol[PATH_MAX];
3732 uint32_t num_synthetic_function_symbols = 0;
3733 for (i=0; i<function_starts_count; ++i)
3734 {
3735 if (function_starts.GetEntryRef (i).data == false)
3736 ++num_synthetic_function_symbols;
3737 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003738
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003739 if (num_synthetic_function_symbols > 0)
3740 {
3741 if (num_syms < sym_idx + num_synthetic_function_symbols)
3742 {
3743 num_syms = sym_idx + num_synthetic_function_symbols;
3744 sym = symtab->Resize (num_syms);
3745 }
3746 uint32_t synthetic_function_symbol_idx = 0;
3747 for (i=0; i<function_starts_count; ++i)
3748 {
3749 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3750 if (func_start_entry->data == false)
3751 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003752 addr_t symbol_file_addr = func_start_entry->addr;
3753 uint32_t symbol_flags = 0;
3754 if (is_arm)
3755 {
3756 if (symbol_file_addr & 1)
3757 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3758 symbol_file_addr &= 0xfffffffffffffffeull;
3759 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003760 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003761 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003762 {
3763 SectionSP symbol_section (symbol_addr.GetSection());
3764 uint32_t symbol_byte_size = 0;
3765 if (symbol_section)
3766 {
3767 const addr_t section_file_addr = symbol_section->GetFileAddress();
3768 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3769 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3770 if (next_func_start_entry)
3771 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003772 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3773 if (is_arm)
3774 next_symbol_file_addr &= 0xfffffffffffffffeull;
3775 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 +00003776 }
3777 else
3778 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00003779 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003780 }
3781 snprintf (synthetic_function_symbol,
3782 sizeof(synthetic_function_symbol),
3783 "___lldb_unnamed_function%u$$%s",
3784 ++synthetic_function_symbol_idx,
3785 module_sp->GetFileSpec().GetFilename().GetCString());
3786 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Clayton037520e2012-07-18 23:18:10 +00003787 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003788 sym[sym_idx].SetType (eSymbolTypeCode);
3789 sym[sym_idx].SetIsSynthetic (true);
3790 sym[sym_idx].GetAddress() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00003791 if (symbol_flags)
3792 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003793 if (symbol_byte_size)
3794 sym[sym_idx].SetByteSize (symbol_byte_size);
3795 ++sym_idx;
3796 }
3797 }
3798 }
3799 }
3800 }
3801 }
3802
3803 // Trim our symbols down to just what we ended up with after
3804 // removing any symbols.
3805 if (sym_idx < num_syms)
3806 {
3807 num_syms = sym_idx;
3808 sym = symtab->Resize (num_syms);
3809 }
3810
3811 // Now synthesize indirect symbols
3812 if (m_dysymtab.nindirectsyms != 0)
3813 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003814 if (indirect_symbol_index_data.GetByteSize())
3815 {
3816 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3817
3818 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3819 {
Charles Davis510938e2013-08-27 05:04:57 +00003820 if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) == S_SYMBOL_STUBS)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003821 {
3822 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3823 if (symbol_stub_byte_size == 0)
3824 continue;
3825
3826 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3827
3828 if (num_symbol_stubs == 0)
3829 continue;
3830
3831 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3832 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3833 {
3834 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3835 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 +00003836 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003837 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3838 {
3839 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Charles Davis510938e2013-08-27 05:04:57 +00003840 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003841 continue;
3842
3843 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3844 Symbol *stub_symbol = NULL;
3845 if (index_pos != end_index_pos)
3846 {
3847 // We have a remapping from the original nlist index to
3848 // a current symbol index, so just look this up by index
3849 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3850 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003851 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003852 {
3853 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00003854 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003855 // S_SYMBOL_STUBS
3856 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3857 }
3858
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003859 if (stub_symbol)
3860 {
3861 Address so_addr(symbol_stub_addr, section_list);
3862
3863 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3864 {
3865 // Change the external symbol into a trampoline that makes sense
3866 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3867 // can re-use them so we don't have to make up a synthetic symbol
3868 // for no good reason.
Greg Clayton9191db42013-10-21 18:40:51 +00003869 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
3870 stub_symbol->SetType (eSymbolTypeTrampoline);
3871 else
3872 stub_symbol->SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003873 stub_symbol->SetExternal (false);
3874 stub_symbol->GetAddress() = so_addr;
3875 stub_symbol->SetByteSize (symbol_stub_byte_size);
3876 }
3877 else
3878 {
3879 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00003880 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003881 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00003882 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003883 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00003884 stub_symbol = NULL; // this pointer no longer valid
3885 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003886 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00003887 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton9191db42013-10-21 18:40:51 +00003888 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
3889 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3890 else
3891 sym[sym_idx].SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003892 sym[sym_idx].SetIsSynthetic (true);
3893 sym[sym_idx].GetAddress() = so_addr;
3894 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3895 ++sym_idx;
3896 }
3897 }
Greg Clayton3f839a32012-09-05 01:38:55 +00003898 else
3899 {
3900 if (log)
3901 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3902 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003903 }
3904 }
3905 }
3906 }
3907 }
3908 }
Greg Clayton9191db42013-10-21 18:40:51 +00003909
3910
3911 if (!trie_entries.empty())
3912 {
3913 for (const auto &e : trie_entries)
3914 {
3915 if (e.entry.import_name)
3916 {
3917 // Make a synthetic symbol to describe re-exported symbol.
3918 if (sym_idx >= num_syms)
3919 sym = symtab->Resize (++num_syms);
3920 sym[sym_idx].SetID (synthetic_sym_id++);
3921 sym[sym_idx].GetMangled() = Mangled(e.entry.name);
3922 sym[sym_idx].SetType (eSymbolTypeReExported);
3923 sym[sym_idx].SetIsSynthetic (true);
3924 sym[sym_idx].SetReExportedSymbolName(e.entry.import_name);
3925 if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize())
3926 {
3927 sym[sym_idx].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(e.entry.other-1));
3928 }
3929 ++sym_idx;
3930 }
3931 }
3932 }
3933
3934
Greg Clayton3046e662013-07-10 01:23:25 +00003935
3936// StreamFile s(stdout, false);
3937// s.Printf ("Symbol table before CalculateSymbolSizes():\n");
3938// symtab->Dump(&s, NULL, eSortOrderNone);
3939 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
3940 symtab->CalculateSymbolSizes();
3941
3942// s.Printf ("Symbol table after CalculateSymbolSizes():\n");
3943// symtab->Dump(&s, NULL, eSortOrderNone);
3944
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003945 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003946 }
3947 return 0;
3948}
3949
3950
3951void
3952ObjectFileMachO::Dump (Stream *s)
3953{
Greg Claytona1743492012-03-13 23:14:29 +00003954 ModuleSP module_sp(GetModule());
3955 if (module_sp)
3956 {
3957 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3958 s->Printf("%p: ", this);
3959 s->Indent();
Charles Davis510938e2013-08-27 05:04:57 +00003960 if (m_header.magic == MH_MAGIC_64 || m_header.magic == MH_CIGAM_64)
Greg Claytona1743492012-03-13 23:14:29 +00003961 s->PutCString("ObjectFileMachO64");
3962 else
3963 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003964
Greg Claytona1743492012-03-13 23:14:29 +00003965 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003966
Greg Claytona1743492012-03-13 23:14:29 +00003967 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003968
Greg Clayton3046e662013-07-10 01:23:25 +00003969 SectionList *sections = GetSectionList();
3970 if (sections)
3971 sections->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003972
Greg Claytona1743492012-03-13 23:14:29 +00003973 if (m_symtab_ap.get())
3974 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3975 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003976}
3977
Greg Claytonf4d6de62013-04-24 22:29:28 +00003978bool
3979ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3980 const lldb_private::DataExtractor &data,
3981 lldb::offset_t lc_offset,
3982 lldb_private::UUID& uuid)
3983{
3984 uint32_t i;
3985 struct uuid_command load_cmd;
3986
3987 lldb::offset_t offset = lc_offset;
3988 for (i=0; i<header.ncmds; ++i)
3989 {
3990 const lldb::offset_t cmd_offset = offset;
3991 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3992 break;
3993
Charles Davis510938e2013-08-27 05:04:57 +00003994 if (load_cmd.cmd == LC_UUID)
Greg Claytonf4d6de62013-04-24 22:29:28 +00003995 {
3996 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3997
3998 if (uuid_bytes)
3999 {
4000 // OpenCL on Mac OS X uses the same UUID for each of its object files.
4001 // We pretend these object files have no UUID to prevent crashing.
4002
4003 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
4004 0x3b, 0xa8,
4005 0x4b, 0x16,
4006 0xb6, 0xa4,
4007 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
4008
4009 if (!memcmp(uuid_bytes, opencl_uuid, 16))
4010 return false;
4011
4012 uuid.SetBytes (uuid_bytes);
4013 return true;
4014 }
4015 return false;
4016 }
4017 offset = cmd_offset + load_cmd.cmdsize;
4018 }
4019 return false;
4020}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004021
4022bool
Greg Clayton60830262011-02-04 18:53:10 +00004023ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004024{
Greg Claytona1743492012-03-13 23:14:29 +00004025 ModuleSP module_sp(GetModule());
4026 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004027 {
Greg Claytona1743492012-03-13 23:14:29 +00004028 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004029 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00004030 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004031 }
4032 return false;
4033}
4034
4035
4036uint32_t
4037ObjectFileMachO::GetDependentModules (FileSpecList& files)
4038{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004039 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00004040 ModuleSP module_sp(GetModule());
4041 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004042 {
Greg Claytona1743492012-03-13 23:14:29 +00004043 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4044 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004045 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004046 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
4047 uint32_t i;
4048 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004049 {
Greg Claytona1743492012-03-13 23:14:29 +00004050 const uint32_t cmd_offset = offset;
4051 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4052 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004053
Greg Claytona1743492012-03-13 23:14:29 +00004054 switch (load_cmd.cmd)
4055 {
Charles Davis510938e2013-08-27 05:04:57 +00004056 case LC_LOAD_DYLIB:
4057 case LC_LOAD_WEAK_DYLIB:
4058 case LC_REEXPORT_DYLIB:
4059 case LC_LOAD_DYLINKER:
4060 case LC_LOADFVMLIB:
4061 case LC_LOAD_UPWARD_DYLIB:
Greg Claytona1743492012-03-13 23:14:29 +00004062 {
4063 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
4064 const char *path = m_data.PeekCStr(name_offset);
4065 // Skip any path that starts with '@' since these are usually:
4066 // @executable_path/.../file
4067 // @rpath/.../file
4068 if (path && path[0] != '@')
4069 {
4070 FileSpec file_spec(path, resolve_path);
4071 if (files.AppendIfUnique(file_spec))
4072 count++;
4073 }
4074 }
4075 break;
4076
4077 default:
4078 break;
4079 }
4080 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004081 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004082 }
4083 return count;
4084}
4085
Jim Ingham672e6f52011-03-07 23:44:08 +00004086lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00004087ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00004088{
4089 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
4090 // is initialized to an invalid address, so we can just return that.
4091 // 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 +00004092
Jim Ingham672e6f52011-03-07 23:44:08 +00004093 if (!IsExecutable() || m_entry_point_address.IsValid())
4094 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004095
4096 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00004097 // /usr/include/mach-o.h, but it is basically:
4098 //
4099 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
4100 // uint32_t count - this is the count of longs in the thread state data
4101 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
4102 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00004103 //
Jim Ingham672e6f52011-03-07 23:44:08 +00004104 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
4105 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
4106 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
4107 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
4108 //
4109 // For now we hard-code the offsets and flavors we need:
4110 //
4111 //
4112
Greg Claytona1743492012-03-13 23:14:29 +00004113 ModuleSP module_sp(GetModule());
4114 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00004115 {
Greg Claytona1743492012-03-13 23:14:29 +00004116 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4117 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004118 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004119 uint32_t i;
4120 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
4121 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004122
Greg Claytona1743492012-03-13 23:14:29 +00004123 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00004124 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004125 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004126 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4127 break;
4128
4129 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00004130 {
Charles Davis510938e2013-08-27 05:04:57 +00004131 case LC_UNIXTHREAD:
4132 case LC_THREAD:
Jim Ingham672e6f52011-03-07 23:44:08 +00004133 {
Greg Claytona1743492012-03-13 23:14:29 +00004134 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00004135 {
Greg Claytona1743492012-03-13 23:14:29 +00004136 uint32_t flavor = m_data.GetU32(&offset);
4137 uint32_t count = m_data.GetU32(&offset);
4138 if (count == 0)
4139 {
4140 // We've gotten off somehow, log and exit;
4141 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00004142 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004143
Greg Claytona1743492012-03-13 23:14:29 +00004144 switch (m_header.cputype)
4145 {
Charles Davis510938e2013-08-27 05:04:57 +00004146 case llvm::MachO::CPU_TYPE_ARM:
Greg Claytona1743492012-03-13 23:14:29 +00004147 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
4148 {
4149 offset += 60; // This is the offset of pc in the GPR thread state data structure.
4150 start_address = m_data.GetU32(&offset);
4151 done = true;
4152 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004153 break;
Charles Davis510938e2013-08-27 05:04:57 +00004154 case llvm::MachO::CPU_TYPE_I386:
Greg Claytona1743492012-03-13 23:14:29 +00004155 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
4156 {
4157 offset += 40; // This is the offset of eip in the GPR thread state data structure.
4158 start_address = m_data.GetU32(&offset);
4159 done = true;
4160 }
4161 break;
Charles Davis510938e2013-08-27 05:04:57 +00004162 case llvm::MachO::CPU_TYPE_X86_64:
Greg Claytona1743492012-03-13 23:14:29 +00004163 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
4164 {
4165 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
4166 start_address = m_data.GetU64(&offset);
4167 done = true;
4168 }
4169 break;
4170 default:
4171 return m_entry_point_address;
4172 }
4173 // Haven't found the GPR flavor yet, skip over the data for this flavor:
4174 if (done)
4175 break;
4176 offset += count * 4;
4177 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004178 }
Greg Claytona1743492012-03-13 23:14:29 +00004179 break;
Charles Davis510938e2013-08-27 05:04:57 +00004180 case LC_MAIN:
Sean Callanan226b70c2012-03-08 02:39:03 +00004181 {
Greg Claytona1743492012-03-13 23:14:29 +00004182 ConstString text_segment_name ("__TEXT");
4183 uint64_t entryoffset = m_data.GetU64(&offset);
4184 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
4185 if (text_segment_sp)
4186 {
4187 done = true;
4188 start_address = text_segment_sp->GetFileAddress() + entryoffset;
4189 }
Sean Callanan226b70c2012-03-08 02:39:03 +00004190 }
Greg Claytona1743492012-03-13 23:14:29 +00004191
4192 default:
4193 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00004194 }
Greg Claytona1743492012-03-13 23:14:29 +00004195 if (done)
4196 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00004197
Greg Claytona1743492012-03-13 23:14:29 +00004198 // Go to the next load command:
4199 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00004200 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004201
Greg Claytona1743492012-03-13 23:14:29 +00004202 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00004203 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004204 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00004205 // of this ObjectFile:
4206 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00004207 {
Greg Claytona1743492012-03-13 23:14:29 +00004208 m_entry_point_address.Clear();
4209 }
4210 }
4211 else
4212 {
4213 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
4214 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00004215
Greg Claytona1743492012-03-13 23:14:29 +00004216 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00004217
Greg Claytona1743492012-03-13 23:14:29 +00004218 if (module_sp)
4219 {
4220 SymbolContextList contexts;
4221 SymbolContext context;
4222 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
4223 {
4224 if (contexts.GetContextAtIndex(0, context))
4225 m_entry_point_address = context.symbol->GetAddress();
4226 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004227 }
4228 }
Jim Ingham672e6f52011-03-07 23:44:08 +00004229 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004230
Jim Ingham672e6f52011-03-07 23:44:08 +00004231 return m_entry_point_address;
4232
4233}
4234
Greg Claytonc9660542012-02-05 02:38:54 +00004235lldb_private::Address
4236ObjectFileMachO::GetHeaderAddress ()
4237{
4238 lldb_private::Address header_addr;
4239 SectionList *section_list = GetSectionList();
4240 if (section_list)
4241 {
4242 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
4243 if (text_segment_sp)
4244 {
Greg Claytone72dfb32012-02-24 01:59:29 +00004245 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00004246 header_addr.SetOffset (0);
4247 }
4248 }
4249 return header_addr;
4250}
4251
Greg Claytonc3776bf2012-02-09 06:16:32 +00004252uint32_t
4253ObjectFileMachO::GetNumThreadContexts ()
4254{
Greg Claytona1743492012-03-13 23:14:29 +00004255 ModuleSP module_sp(GetModule());
4256 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004257 {
Greg Claytona1743492012-03-13 23:14:29 +00004258 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4259 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004260 {
Greg Claytona1743492012-03-13 23:14:29 +00004261 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00004262 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004263 FileRangeArray::Entry file_range;
4264 thread_command thread_cmd;
4265 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004266 {
Greg Claytona1743492012-03-13 23:14:29 +00004267 const uint32_t cmd_offset = offset;
4268 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
4269 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004270
Charles Davis510938e2013-08-27 05:04:57 +00004271 if (thread_cmd.cmd == LC_THREAD)
Greg Claytona1743492012-03-13 23:14:29 +00004272 {
4273 file_range.SetRangeBase (offset);
4274 file_range.SetByteSize (thread_cmd.cmdsize - 8);
4275 m_thread_context_offsets.Append (file_range);
4276 }
4277 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00004278 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004279 }
4280 }
4281 return m_thread_context_offsets.GetSize();
4282}
4283
4284lldb::RegisterContextSP
4285ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
4286{
Greg Claytonc3776bf2012-02-09 06:16:32 +00004287 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00004288
Greg Claytona1743492012-03-13 23:14:29 +00004289 ModuleSP module_sp(GetModule());
4290 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00004291 {
Greg Claytona1743492012-03-13 23:14:29 +00004292 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4293 if (!m_thread_context_offsets_valid)
4294 GetNumThreadContexts ();
4295
4296 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00004297 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00004298 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00004299
4300 DataExtractor data (m_data,
4301 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00004302 thread_context_file_range->GetByteSize());
4303
4304 switch (m_header.cputype)
4305 {
Charles Davis510938e2013-08-27 05:04:57 +00004306 case llvm::MachO::CPU_TYPE_ARM:
Jim Ingham28eb5712012-10-12 17:34:26 +00004307 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
4308 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004309
Charles Davis510938e2013-08-27 05:04:57 +00004310 case llvm::MachO::CPU_TYPE_I386:
Jim Ingham28eb5712012-10-12 17:34:26 +00004311 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
4312 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004313
Charles Davis510938e2013-08-27 05:04:57 +00004314 case llvm::MachO::CPU_TYPE_X86_64:
Jim Ingham28eb5712012-10-12 17:34:26 +00004315 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
4316 break;
4317 }
Greg Claytona1743492012-03-13 23:14:29 +00004318 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00004319 }
4320 return reg_ctx_sp;
4321}
4322
Greg Claytonc9660542012-02-05 02:38:54 +00004323
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004324ObjectFile::Type
4325ObjectFileMachO::CalculateType()
4326{
4327 switch (m_header.filetype)
4328 {
Charles Davis510938e2013-08-27 05:04:57 +00004329 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004330 if (GetAddressByteSize () == 4)
4331 {
4332 // 32 bit kexts are just object files, but they do have a valid
4333 // UUID load command.
4334 UUID uuid;
4335 if (GetUUID(&uuid))
4336 {
4337 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004338 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004339 // "OSKextGetCurrentIdentifier" as this is required of kexts
4340 if (m_strata == eStrataInvalid)
4341 m_strata = eStrataKernel;
4342 return eTypeSharedLibrary;
4343 }
4344 }
4345 return eTypeObjectFile;
4346
Charles Davis510938e2013-08-27 05:04:57 +00004347 case MH_EXECUTE: return eTypeExecutable; // 0x2u
4348 case MH_FVMLIB: return eTypeSharedLibrary; // 0x3u
4349 case MH_CORE: return eTypeCoreFile; // 0x4u
4350 case MH_PRELOAD: return eTypeSharedLibrary; // 0x5u
4351 case MH_DYLIB: return eTypeSharedLibrary; // 0x6u
4352 case MH_DYLINKER: return eTypeDynamicLinker; // 0x7u
4353 case MH_BUNDLE: return eTypeSharedLibrary; // 0x8u
4354 case MH_DYLIB_STUB: return eTypeStubLibrary; // 0x9u
4355 case MH_DSYM: return eTypeDebugInfo; // 0xAu
4356 case MH_KEXT_BUNDLE: return eTypeSharedLibrary; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004357 default:
4358 break;
4359 }
4360 return eTypeUnknown;
4361}
4362
4363ObjectFile::Strata
4364ObjectFileMachO::CalculateStrata()
4365{
4366 switch (m_header.filetype)
4367 {
Charles Davis510938e2013-08-27 05:04:57 +00004368 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004369 {
4370 // 32 bit kexts are just object files, but they do have a valid
4371 // UUID load command.
4372 UUID uuid;
4373 if (GetUUID(&uuid))
4374 {
4375 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00004376 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004377 // "OSKextGetCurrentIdentifier" as this is required of kexts
4378 if (m_type == eTypeInvalid)
4379 m_type = eTypeSharedLibrary;
4380
4381 return eStrataKernel;
4382 }
4383 }
4384 return eStrataUnknown;
4385
Charles Davis510938e2013-08-27 05:04:57 +00004386 case MH_EXECUTE: // 0x2u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004387 // Check for the MH_DYLDLINK bit in the flags
Charles Davis510938e2013-08-27 05:04:57 +00004388 if (m_header.flags & MH_DYLDLINK)
Sean Callanan49bce8e2012-02-10 20:22:35 +00004389 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004390 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00004391 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004392 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00004393 {
4394 SectionList *section_list = GetSectionList();
4395 if (section_list)
4396 {
4397 static ConstString g_kld_section_name ("__KLD");
4398 if (section_list->FindSectionByName(g_kld_section_name))
4399 return eStrataKernel;
4400 }
4401 }
4402 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004403
Charles Davis510938e2013-08-27 05:04:57 +00004404 case MH_FVMLIB: return eStrataUser; // 0x3u
4405 case MH_CORE: return eStrataUnknown; // 0x4u
4406 case MH_PRELOAD: return eStrataRawImage; // 0x5u
4407 case MH_DYLIB: return eStrataUser; // 0x6u
4408 case MH_DYLINKER: return eStrataUser; // 0x7u
4409 case MH_BUNDLE: return eStrataUser; // 0x8u
4410 case MH_DYLIB_STUB: return eStrataUser; // 0x9u
4411 case MH_DSYM: return eStrataUnknown; // 0xAu
4412 case MH_KEXT_BUNDLE: return eStrataKernel; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00004413 default:
4414 break;
4415 }
4416 return eStrataUnknown;
4417}
4418
4419
Greg Claytonc2ff9312012-02-22 19:41:02 +00004420uint32_t
4421ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4422{
Greg Claytona1743492012-03-13 23:14:29 +00004423 ModuleSP module_sp(GetModule());
4424 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004425 {
Greg Claytona1743492012-03-13 23:14:29 +00004426 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4427 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004428 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00004429 uint32_t version_cmd = 0;
4430 uint64_t version = 0;
4431 uint32_t i;
4432 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004433 {
Greg Claytonc7bece562013-01-25 18:06:21 +00004434 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00004435 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4436 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00004437
Charles Davis510938e2013-08-27 05:04:57 +00004438 if (load_cmd.cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004439 {
Greg Claytona1743492012-03-13 23:14:29 +00004440 if (version_cmd == 0)
4441 {
4442 version_cmd = load_cmd.cmd;
4443 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4444 break;
4445 version = load_cmd.dylib.current_version;
4446 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004447 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00004448 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00004449 }
Greg Claytona1743492012-03-13 23:14:29 +00004450 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004451 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004452
Charles Davis510938e2013-08-27 05:04:57 +00004453 if (version_cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00004454 {
Greg Claytona1743492012-03-13 23:14:29 +00004455 if (versions != NULL && num_versions > 0)
4456 {
4457 if (num_versions > 0)
4458 versions[0] = (version & 0xFFFF0000ull) >> 16;
4459 if (num_versions > 1)
4460 versions[1] = (version & 0x0000FF00ull) >> 8;
4461 if (num_versions > 2)
4462 versions[2] = (version & 0x000000FFull);
4463 // Fill in an remaining version numbers with invalid values
4464 for (i=3; i<num_versions; ++i)
4465 versions[i] = UINT32_MAX;
4466 }
4467 // The LC_ID_DYLIB load command has a version with 3 version numbers
4468 // in it, so always return 3
4469 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00004470 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00004471 }
4472 return false;
4473}
4474
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004475bool
Greg Clayton514487e2011-02-15 21:59:32 +00004476ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004477{
Greg Claytona1743492012-03-13 23:14:29 +00004478 ModuleSP module_sp(GetModule());
4479 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00004480 {
Greg Claytona1743492012-03-13 23:14:29 +00004481 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4482 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda4e7511e2013-03-06 23:19:17 +00004483
Greg Claytona1743492012-03-13 23:14:29 +00004484 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda4e7511e2013-03-06 23:19:17 +00004485 // debugs at the addresses in the file itself. Below we set the OS to
Greg Claytona1743492012-03-13 23:14:29 +00004486 // unknown to make sure we use the DynamicLoaderStatic()...
Charles Davis510938e2013-08-27 05:04:57 +00004487 if (m_header.filetype == MH_PRELOAD)
Greg Claytona1743492012-03-13 23:14:29 +00004488 {
4489 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4490 }
4491 return true;
Greg Clayton593577a2011-09-21 03:57:31 +00004492 }
Greg Claytona1743492012-03-13 23:14:29 +00004493 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004494}
4495
4496
Jason Molenda0e0954c2013-04-16 06:24:42 +00004497UUID
4498ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4499{
4500 UUID uuid;
4501 if (process)
4502 {
4503 addr_t all_image_infos = process->GetImageInfoAddress();
4504
4505 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4506 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4507 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4508 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4509
4510 Error err;
4511 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4512 if (version_or_magic != -1
Charles Davis510938e2013-08-27 05:04:57 +00004513 && version_or_magic != MH_MAGIC
4514 && version_or_magic != MH_CIGAM
4515 && version_or_magic != MH_MAGIC_64
4516 && version_or_magic != MH_CIGAM_64
Jason Molenda0e0954c2013-04-16 06:24:42 +00004517 && version_or_magic >= 13)
4518 {
4519 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4520 int wordsize = process->GetAddressByteSize();
4521 if (wordsize == 8)
4522 {
4523 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4524 }
4525 if (wordsize == 4)
4526 {
4527 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4528 }
4529 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4530 {
4531 uuid_t shared_cache_uuid;
4532 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4533 {
4534 uuid.SetBytes (shared_cache_uuid);
4535 }
4536 }
4537 }
4538 }
4539 return uuid;
4540}
4541
4542UUID
4543ObjectFileMachO::GetLLDBSharedCacheUUID ()
4544{
4545 UUID uuid;
4546#if defined (__APPLE__) && defined (__arm__)
4547 uint8_t *(*dyld_get_all_image_infos)(void);
4548 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4549 if (dyld_get_all_image_infos)
4550 {
4551 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4552 if (dyld_all_image_infos_address)
4553 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00004554 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4555 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00004556 {
Jason Molenda42b69fa2013-04-16 22:56:17 +00004557 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 +00004558 uuid.SetBytes (sharedCacheUUID_address);
4559 }
4560 }
4561 }
4562#endif
4563 return uuid;
4564}
4565
Greg Clayton9b234982013-10-24 22:54:08 +00004566uint32_t
4567ObjectFileMachO::GetMinimumOSVersion (uint32_t *versions, uint32_t num_versions)
4568{
4569 if (m_min_os_versions.empty())
4570 {
4571 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
4572 bool success = false;
4573 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
4574 {
4575 const lldb::offset_t load_cmd_offset = offset;
4576
4577 version_min_command lc;
4578 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
4579 break;
4580 if (lc.cmd == LC_VERSION_MIN_MACOSX || lc.cmd == LC_VERSION_MIN_IPHONEOS)
4581 {
4582 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
4583 {
4584 const uint32_t xxxx = lc.version >> 16;
4585 const uint32_t yy = (lc.version >> 8) & 0xffu;
4586 const uint32_t zz = lc.version & 0xffu;
4587 if (xxxx)
4588 {
4589 m_min_os_versions.push_back(xxxx);
4590 if (yy)
4591 {
4592 m_min_os_versions.push_back(yy);
4593 if (zz)
4594 m_min_os_versions.push_back(zz);
4595 }
4596 }
4597 success = true;
4598 }
4599 }
4600 offset = load_cmd_offset + lc.cmdsize;
4601 }
4602
4603 if (success == false)
4604 {
4605 // Push an invalid value so we don't keep trying to
4606 m_min_os_versions.push_back(UINT32_MAX);
4607 }
4608 }
4609
4610 if (m_min_os_versions.size() > 1 || m_min_os_versions[0] != UINT32_MAX)
4611 {
4612 if (versions != NULL && num_versions > 0)
4613 {
4614 for (size_t i=0; i<num_versions; ++i)
4615 {
4616 if (i < m_min_os_versions.size())
4617 versions[i] = m_min_os_versions[i];
4618 else
4619 versions[i] = 0;
4620 }
4621 }
4622 return m_min_os_versions.size();
4623 }
4624 // Call the superclasses version that will empty out the data
4625 return ObjectFile::GetMinimumOSVersion (versions, num_versions);
4626}
4627
4628uint32_t
4629ObjectFileMachO::GetSDKVersion(uint32_t *versions, uint32_t num_versions)
4630{
4631 if (m_sdk_versions.empty())
4632 {
4633 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
4634 bool success = false;
4635 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
4636 {
4637 const lldb::offset_t load_cmd_offset = offset;
4638
4639 version_min_command lc;
4640 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
4641 break;
4642 if (lc.cmd == LC_VERSION_MIN_MACOSX || lc.cmd == LC_VERSION_MIN_IPHONEOS)
4643 {
4644 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
4645 {
4646 const uint32_t xxxx = lc.reserved >> 16;
4647 const uint32_t yy = (lc.reserved >> 8) & 0xffu;
4648 const uint32_t zz = lc.reserved & 0xffu;
4649 if (xxxx)
4650 {
4651 m_sdk_versions.push_back(xxxx);
4652 if (yy)
4653 {
4654 m_sdk_versions.push_back(yy);
4655 if (zz)
4656 m_sdk_versions.push_back(zz);
4657 }
4658 }
4659 success = true;
4660 }
4661 }
4662 offset = load_cmd_offset + lc.cmdsize;
4663 }
4664
4665 if (success == false)
4666 {
4667 // Push an invalid value so we don't keep trying to
4668 m_sdk_versions.push_back(UINT32_MAX);
4669 }
4670 }
4671
4672 if (m_sdk_versions.size() > 1 || m_sdk_versions[0] != UINT32_MAX)
4673 {
4674 if (versions != NULL && num_versions > 0)
4675 {
4676 for (size_t i=0; i<num_versions; ++i)
4677 {
4678 if (i < m_sdk_versions.size())
4679 versions[i] = m_sdk_versions[i];
4680 else
4681 versions[i] = 0;
4682 }
4683 }
4684 return m_sdk_versions.size();
4685 }
4686 // Call the superclasses version that will empty out the data
4687 return ObjectFile::GetSDKVersion (versions, num_versions);
4688}
4689
Jason Molenda0e0954c2013-04-16 06:24:42 +00004690
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004691//------------------------------------------------------------------
4692// PluginInterface protocol
4693//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00004694lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004695ObjectFileMachO::GetPluginName()
4696{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004697 return GetPluginNameStatic();
4698}
4699
4700uint32_t
4701ObjectFileMachO::GetPluginVersion()
4702{
4703 return 1;
4704}
4705