blob: da32acd3e43e6eccb66a9c85d79c003f3a7ddb1a [file] [log] [blame]
Chris Lattner24943d22010-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 Clayton3f69eac2011-12-03 02:30:59 +000010#include "llvm/ADT/StringRef.h"
Jim Ingham28775942011-03-07 23:44:08 +000011#include "llvm/Support/MachO.h"
12
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "ObjectFileMachO.h"
14
Greg Claytond4330e62012-09-05 01:38:55 +000015#include "lldb/lldb-private-log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Core/ArchSpec.h"
17#include "lldb/Core/DataBuffer.h"
Jason Molendaadf9e3d2013-04-10 05:58:57 +000018#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/FileSpecList.h"
Greg Claytond4330e62012-09-05 01:38:55 +000020#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Module.h"
Greg Clayton36b877d2013-04-24 22:29:28 +000022#include "lldb/Core/ModuleSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/PluginManager.h"
Greg Clayton6f7f8da2012-04-24 03:06:13 +000024#include "lldb/Core/RangeMap.h"
Chris Lattner24943d22010-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 Claytondf6dc882012-01-05 03:57:59 +000030#include "lldb/Host/Host.h"
31#include "lldb/Host/FileSpec.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000032#include "lldb/Symbol/ClangNamespaceDecl.h"
Jason Molendad7938392013-03-21 03:36:01 +000033#include "lldb/Symbol/DWARFCallFrameInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000034#include "lldb/Symbol/ObjectFile.h"
Greg Clayton29021d32012-04-18 05:19:20 +000035#include "lldb/Target/Platform.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000036#include "lldb/Target/Process.h"
Greg Clayton29021d32012-04-18 05:19:20 +000037#include "lldb/Target/Target.h"
Greg Clayton9ce95382012-02-13 23:10:39 +000038#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
39#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Clayton46c9a352012-02-09 06:16:32 +000040#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner24943d22010-06-08 16:52:24 +000041
Jason Molenda45c75502013-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 Malea3e649052013-04-17 19:24:22 +000047#ifndef __APPLE__
48#include "Utility/UuidCompatibility.h"
49#endif
50
Chris Lattner24943d22010-06-08 16:52:24 +000051using namespace lldb;
52using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000053using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000054
Jason Molenda9badb6c2013-03-06 23:19:17 +000055class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
Greg Clayton46c9a352012-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 Clayton36da2aa2013-01-25 18:06:21 +000073 lldb::offset_t offset = 0;
Greg Clayton46c9a352012-02-09 06:16:32 +000074 SetError (GPRRegSet, Read, -1);
75 SetError (FPURegSet, Read, -1);
76 SetError (EXCRegSet, Read, -1);
Greg Clayton9ce95382012-02-13 23:10:39 +000077 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +000078
Greg Clayton9ce95382012-02-13 23:10:39 +000079 while (!done)
Greg Clayton46c9a352012-02-09 06:16:32 +000080 {
Greg Clayton9ce95382012-02-13 23:10:39 +000081 int flavor = data.GetU32 (&offset);
82 if (flavor == 0)
83 done = true;
84 else
Greg Clayton46c9a352012-02-09 06:16:32 +000085 {
Greg Clayton9ce95382012-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 Molenda9badb6c2013-03-06 23:19:17 +000095
Greg Clayton9ce95382012-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 Molenda9badb6c2013-03-06 23:19:17 +0000101
Greg Clayton9ce95382012-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 Molenda9badb6c2013-03-06 23:19:17 +0000116
Greg Clayton9ce95382012-02-13 23:10:39 +0000117 default:
118 done = true;
119 break;
120 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000121 }
Greg Clayton9ce95382012-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 Molenda9badb6c2013-03-06 23:19:17 +0000130
Greg Clayton9ce95382012-02-13 23:10:39 +0000131 virtual int
132 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
133 {
134 return 0;
135 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000136
Greg Clayton9ce95382012-02-13 23:10:39 +0000137 virtual int
138 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
139 {
140 return 0;
141 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000142
Greg Clayton9ce95382012-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 Molenda9badb6c2013-03-06 23:19:17 +0000148
Greg Clayton9ce95382012-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 Molenda9badb6c2013-03-06 23:19:17 +0000154
Greg Clayton9ce95382012-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 Clayton46c9a352012-02-09 06:16:32 +0000161
Greg Clayton9ce95382012-02-13 23:10:39 +0000162
Jason Molenda9badb6c2013-03-06 23:19:17 +0000163class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
Greg Clayton9ce95382012-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 Molenda9badb6c2013-03-06 23:19:17 +0000171
Greg Clayton9ce95382012-02-13 23:10:39 +0000172 virtual void
173 InvalidateAllRegisters ()
174 {
175 // Do nothing... registers are always valid...
176 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000177
Greg Clayton9ce95382012-02-13 23:10:39 +0000178 void
179 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
180 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000181 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-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 Molenda9badb6c2013-03-06 23:19:17 +0000186
Greg Clayton9ce95382012-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 Clayton46c9a352012-02-09 06:16:32 +0000193 {
Greg Clayton9ce95382012-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 Molenda9badb6c2013-03-06 23:19:17 +0000224
Greg Clayton9ce95382012-02-13 23:10:39 +0000225 default:
226 done = true;
227 break;
228 }
Greg Clayton46c9a352012-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 Molenda9badb6c2013-03-06 23:19:17 +0000238
Greg Clayton46c9a352012-02-09 06:16:32 +0000239 virtual int
240 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
241 {
242 return 0;
243 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000244
Greg Clayton46c9a352012-02-09 06:16:32 +0000245 virtual int
246 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
247 {
248 return 0;
249 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000250
Greg Clayton46c9a352012-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 Molenda9badb6c2013-03-06 23:19:17 +0000256
Greg Clayton46c9a352012-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 Molenda9badb6c2013-03-06 23:19:17 +0000262
Greg Clayton46c9a352012-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 Molenda9badb6c2013-03-06 23:19:17 +0000270class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Clayton9ce95382012-02-13 23:10:39 +0000271{
272public:
273 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonb5431d02012-10-30 23:57:32 +0000274 RegisterContextDarwin_arm (thread, 0)
Greg Clayton9ce95382012-02-13 23:10:39 +0000275 {
276 SetRegisterDataFrom_LC_THREAD (data);
277 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000278
Greg Clayton9ce95382012-02-13 23:10:39 +0000279 virtual void
280 InvalidateAllRegisters ()
281 {
282 // Do nothing... registers are always valid...
283 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000284
Greg Clayton9ce95382012-02-13 23:10:39 +0000285 void
286 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
287 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000288 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000289 SetError (GPRRegSet, Read, -1);
290 SetError (FPURegSet, Read, -1);
291 SetError (EXCRegSet, Read, -1);
Jason Molenda3b244b72013-05-14 03:25:58 +0000292 bool done = false;
293
294 while (!done)
Greg Clayton9ce95382012-02-13 23:10:39 +0000295 {
Jason Molenda3b244b72013-05-14 03:25:58 +0000296 int flavor = data.GetU32 (&offset);
297 uint32_t count = data.GetU32 (&offset);
298 switch (flavor)
299 {
300 case GPRRegSet:
301 for (uint32_t i=0; i<count; ++i)
302 gpr.r[i] = data.GetU32(&offset);
303 SetError (GPRRegSet, Read, 0);
304 break;
305
306 case FPURegSet:
307 {
308 uint32_t *d = &fpu.floats.s[0];
309 for (uint32_t i = 0; i < count && d < d + (sizeof (fpu.floats) / sizeof (uint32_t)); i++)
310 {
311 *d++ = data.GetU32(&offset);
312 }
313 SetError (FPURegSet, Read, 0);
314 }
315 break;
316
317 case EXCRegSet:
318 exc.exception = data.GetU32(&offset);
319 exc.fsr = data.GetU32(&offset);
320 exc.far = data.GetU32(&offset);
321 SetError (EXCRegSet, Read, 0);
322 done = true;
323 break;
324
325 // Unknown register set flavor, stop trying to parse.
326 default:
327 done = true;
328 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000329 }
330 }
331protected:
332 virtual int
333 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
334 {
335 return 0;
336 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000337
Greg Clayton9ce95382012-02-13 23:10:39 +0000338 virtual int
339 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
340 {
341 return 0;
342 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000343
Greg Clayton9ce95382012-02-13 23:10:39 +0000344 virtual int
345 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
346 {
347 return 0;
348 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000349
350 virtual int
351 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
352 {
353 return -1;
354 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000355
Greg Clayton9ce95382012-02-13 23:10:39 +0000356 virtual int
357 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
358 {
359 return 0;
360 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000361
Greg Clayton9ce95382012-02-13 23:10:39 +0000362 virtual int
363 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
364 {
365 return 0;
366 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000367
Greg Clayton9ce95382012-02-13 23:10:39 +0000368 virtual int
369 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
370 {
371 return 0;
372 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000373
Greg Claytonb5431d02012-10-30 23:57:32 +0000374 virtual int
375 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
376 {
377 return -1;
378 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000379};
380
Greg Claytonb1888f22011-03-19 01:12:21 +0000381#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000382
383void
384ObjectFileMachO::Initialize()
385{
386 PluginManager::RegisterPlugin (GetPluginNameStatic(),
387 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000388 CreateInstance,
Greg Clayton36b877d2013-04-24 22:29:28 +0000389 CreateMemoryInstance,
390 GetModuleSpecifications);
Chris Lattner24943d22010-06-08 16:52:24 +0000391}
392
393void
394ObjectFileMachO::Terminate()
395{
396 PluginManager::UnregisterPlugin (CreateInstance);
397}
398
399
Greg Clayton0e191602013-05-10 21:47:16 +0000400lldb_private::ConstString
Chris Lattner24943d22010-06-08 16:52:24 +0000401ObjectFileMachO::GetPluginNameStatic()
402{
Greg Clayton0e191602013-05-10 21:47:16 +0000403 static ConstString g_name("mach-o");
404 return g_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000405}
406
407const char *
408ObjectFileMachO::GetPluginDescriptionStatic()
409{
410 return "Mach-o object file reader (32 and 64 bit)";
411}
412
Chris Lattner24943d22010-06-08 16:52:24 +0000413ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000414ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
415 DataBufferSP& data_sp,
416 lldb::offset_t data_offset,
417 const FileSpec* file,
418 lldb::offset_t file_offset,
419 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000420{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000421 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000422 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000423 data_sp = file->MemoryMapFileContents(file_offset, length);
424 data_offset = 0;
425 }
426
427 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
428 {
429 // Update the data to contain the entire file if it doesn't already
430 if (data_sp->GetByteSize() < length)
431 {
432 data_sp = file->MemoryMapFileContents(file_offset, length);
433 data_offset = 0;
434 }
Greg Clayton102b2c22013-04-18 22:45:39 +0000435 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, data_offset, file, file_offset, length));
Chris Lattner24943d22010-06-08 16:52:24 +0000436 if (objfile_ap.get() && objfile_ap->ParseHeader())
437 return objfile_ap.release();
438 }
439 return NULL;
440}
441
Greg Claytonb5a8f142012-02-05 02:38:54 +0000442ObjectFile *
Jason Molenda9badb6c2013-03-06 23:19:17 +0000443ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
444 DataBufferSP& data_sp,
445 const ProcessSP &process_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000446 lldb::addr_t header_addr)
447{
448 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
449 {
Greg Clayton102b2c22013-04-18 22:45:39 +0000450 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000451 if (objfile_ap.get() && objfile_ap->ParseHeader())
452 return objfile_ap.release();
453 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000454 return NULL;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000455}
456
Greg Clayton36b877d2013-04-24 22:29:28 +0000457size_t
458ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
459 lldb::DataBufferSP& data_sp,
460 lldb::offset_t data_offset,
461 lldb::offset_t file_offset,
462 lldb::offset_t length,
463 lldb_private::ModuleSpecList &specs)
464{
465 const size_t initial_count = specs.GetSize();
466
467 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
468 {
469 DataExtractor data;
470 data.SetData(data_sp);
471 llvm::MachO::mach_header header;
472 if (ParseHeader (data, &data_offset, header))
473 {
474 if (header.sizeofcmds >= data_sp->GetByteSize())
475 {
476 data_sp = file.ReadFileContents(file_offset, header.sizeofcmds);
477 data_offset = 0;
478 }
479 if (data_sp)
480 {
481 ModuleSpec spec;
482 spec.GetFileSpec() = file;
483 spec.GetArchitecture().SetArchitecture(eArchTypeMachO,
484 header.cputype,
485 header.cpusubtype);
486 if (spec.GetArchitecture().IsValid())
487 {
488 GetUUID (header, data, data_offset, spec.GetUUID());
489 specs.Append(spec);
490 }
491 }
492 }
493 }
494 return specs.GetSize() - initial_count;
495}
496
497
Greg Claytonb5a8f142012-02-05 02:38:54 +0000498
499const ConstString &
500ObjectFileMachO::GetSegmentNameTEXT()
501{
502 static ConstString g_segment_name_TEXT ("__TEXT");
503 return g_segment_name_TEXT;
504}
505
506const ConstString &
507ObjectFileMachO::GetSegmentNameDATA()
508{
509 static ConstString g_segment_name_DATA ("__DATA");
510 return g_segment_name_DATA;
511}
512
513const ConstString &
514ObjectFileMachO::GetSegmentNameOBJC()
515{
516 static ConstString g_segment_name_OBJC ("__OBJC");
517 return g_segment_name_OBJC;
518}
519
520const ConstString &
521ObjectFileMachO::GetSegmentNameLINKEDIT()
522{
523 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
524 return g_section_name_LINKEDIT;
525}
526
527const ConstString &
528ObjectFileMachO::GetSectionNameEHFrame()
529{
530 static ConstString g_section_name_eh_frame ("__eh_frame");
531 return g_section_name_eh_frame;
532}
533
534
Chris Lattner24943d22010-06-08 16:52:24 +0000535
536static uint32_t
537MachHeaderSizeFromMagic(uint32_t magic)
538{
539 switch (magic)
540 {
Greg Clayton1674b122010-07-21 22:12:05 +0000541 case HeaderMagic32:
542 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000543 return sizeof(struct mach_header);
544
Greg Clayton1674b122010-07-21 22:12:05 +0000545 case HeaderMagic64:
546 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000547 return sizeof(struct mach_header_64);
548 break;
549
550 default:
551 break;
552 }
553 return 0;
554}
555
556
557bool
Jason Molenda9badb6c2013-03-06 23:19:17 +0000558ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
559 lldb::addr_t data_offset,
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000560 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000561{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000562 DataExtractor data;
563 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000564 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000565 uint32_t magic = data.GetU32(&offset);
566 return MachHeaderSizeFromMagic(magic) != 0;
567}
568
569
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000570ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
571 DataBufferSP& data_sp,
572 lldb::offset_t data_offset,
573 const FileSpec* file,
574 lldb::offset_t file_offset,
575 lldb::offset_t length) :
576 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Clayton46c9a352012-02-09 06:16:32 +0000577 m_mach_segments(),
578 m_mach_sections(),
579 m_entry_point_address(),
580 m_thread_context_offsets(),
581 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000582{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000583 ::memset (&m_header, 0, sizeof(m_header));
584 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000585}
586
Greg Clayton3508c382012-02-24 01:59:29 +0000587ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000588 lldb::DataBufferSP& header_data_sp,
589 const lldb::ProcessSP &process_sp,
590 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000591 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Clayton46c9a352012-02-09 06:16:32 +0000592 m_mach_segments(),
593 m_mach_sections(),
594 m_entry_point_address(),
595 m_thread_context_offsets(),
596 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000597{
598 ::memset (&m_header, 0, sizeof(m_header));
599 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
600}
Chris Lattner24943d22010-06-08 16:52:24 +0000601
602ObjectFileMachO::~ObjectFileMachO()
603{
604}
605
Greg Clayton36b877d2013-04-24 22:29:28 +0000606bool
607ObjectFileMachO::ParseHeader (DataExtractor &data,
608 lldb::offset_t *data_offset_ptr,
609 llvm::MachO::mach_header &header)
610{
611 data.SetByteOrder (lldb::endian::InlHostByteOrder());
612 // Leave magic in the original byte order
613 header.magic = data.GetU32(data_offset_ptr);
614 bool can_parse = false;
615 bool is_64_bit = false;
616 switch (header.magic)
617 {
618 case HeaderMagic32:
619 data.SetByteOrder (lldb::endian::InlHostByteOrder());
620 data.SetAddressByteSize(4);
621 can_parse = true;
622 break;
623
624 case HeaderMagic64:
625 data.SetByteOrder (lldb::endian::InlHostByteOrder());
626 data.SetAddressByteSize(8);
627 can_parse = true;
628 is_64_bit = true;
629 break;
630
631 case HeaderMagic32Swapped:
632 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
633 data.SetAddressByteSize(4);
634 can_parse = true;
635 break;
636
637 case HeaderMagic64Swapped:
638 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
639 data.SetAddressByteSize(8);
640 is_64_bit = true;
641 can_parse = true;
642 break;
643
644 default:
645 break;
646 }
647
648 if (can_parse)
649 {
650 data.GetU32(data_offset_ptr, &header.cputype, 6);
651 if (is_64_bit)
652 *data_offset_ptr += 4;
653 return true;
654 }
655 else
656 {
657 memset(&header, 0, sizeof(header));
658 }
659 return false;
660}
Chris Lattner24943d22010-06-08 16:52:24 +0000661
662bool
663ObjectFileMachO::ParseHeader ()
664{
Greg Clayton9482f052012-03-13 23:14:29 +0000665 ModuleSP module_sp(GetModule());
666 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000667 {
Greg Clayton9482f052012-03-13 23:14:29 +0000668 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
669 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000670 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000671 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000672 // Leave magic in the original byte order
673 m_header.magic = m_data.GetU32(&offset);
674 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000675 {
Greg Clayton9482f052012-03-13 23:14:29 +0000676 case HeaderMagic32:
677 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
678 m_data.SetAddressByteSize(4);
679 can_parse = true;
680 break;
681
682 case HeaderMagic64:
683 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
684 m_data.SetAddressByteSize(8);
685 can_parse = true;
686 break;
687
688 case HeaderMagic32Swapped:
689 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
690 m_data.SetAddressByteSize(4);
691 can_parse = true;
692 break;
693
694 case HeaderMagic64Swapped:
695 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
696 m_data.SetAddressByteSize(8);
697 can_parse = true;
698 break;
699
700 default:
701 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000702 }
Greg Clayton9482f052012-03-13 23:14:29 +0000703
704 if (can_parse)
705 {
706 m_data.GetU32(&offset, &m_header.cputype, 6);
707
708 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +0000709
Greg Clayton21a25432012-11-16 21:36:10 +0000710 // Check if the module has a required architecture
711 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000712 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000713 return false;
714
Greg Clayton9482f052012-03-13 23:14:29 +0000715 if (SetModulesArchitecture (mach_arch))
716 {
717 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
718 if (m_data.GetByteSize() < header_and_lc_size)
719 {
720 DataBufferSP data_sp;
721 ProcessSP process_sp (m_process_wp.lock());
722 if (process_sp)
723 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000724 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000725 }
726 else
727 {
728 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000729 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000730 if (data_sp->GetByteSize() != header_and_lc_size)
731 return false;
732 }
733 if (data_sp)
734 m_data.SetData (data_sp);
735 }
736 }
737 return true;
738 }
739 else
740 {
741 memset(&m_header, 0, sizeof(struct mach_header));
742 }
Chris Lattner24943d22010-06-08 16:52:24 +0000743 }
744 return false;
745}
746
747
748ByteOrder
749ObjectFileMachO::GetByteOrder () const
750{
Chris Lattner24943d22010-06-08 16:52:24 +0000751 return m_data.GetByteOrder ();
752}
753
Jim Ingham7508e732010-08-09 23:31:02 +0000754bool
755ObjectFileMachO::IsExecutable() const
756{
757 return m_header.filetype == HeaderFileTypeExecutable;
758}
Chris Lattner24943d22010-06-08 16:52:24 +0000759
Greg Clayton36da2aa2013-01-25 18:06:21 +0000760uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000761ObjectFileMachO::GetAddressByteSize () const
762{
Chris Lattner24943d22010-06-08 16:52:24 +0000763 return m_data.GetAddressByteSize ();
764}
765
Greg Claytonb3448432011-03-24 21:19:54 +0000766AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000767ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
768{
769 Symtab *symtab = GetSymtab();
770 if (symtab)
771 {
772 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
773 if (symbol)
774 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000775 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000776 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000777 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000778 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000779 {
Greg Clayton3508c382012-02-24 01:59:29 +0000780 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000781 switch (section_type)
782 {
783 case eSectionTypeInvalid: return eAddressClassUnknown;
784 case eSectionTypeCode:
785 if (m_header.cputype == llvm::MachO::CPUTypeARM)
786 {
787 // For ARM we have a bit in the n_desc field of the symbol
788 // that tells us ARM/Thumb which is bit 0x0008.
789 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
790 return eAddressClassCodeAlternateISA;
791 }
792 return eAddressClassCode;
793
794 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000795 case eSectionTypeData:
796 case eSectionTypeDataCString:
797 case eSectionTypeDataCStringPointers:
798 case eSectionTypeDataSymbolAddress:
799 case eSectionTypeData4:
800 case eSectionTypeData8:
801 case eSectionTypeData16:
802 case eSectionTypeDataPointers:
803 case eSectionTypeZeroFill:
804 case eSectionTypeDataObjCMessageRefs:
805 case eSectionTypeDataObjCCFStrings:
806 return eAddressClassData;
807 case eSectionTypeDebug:
808 case eSectionTypeDWARFDebugAbbrev:
809 case eSectionTypeDWARFDebugAranges:
810 case eSectionTypeDWARFDebugFrame:
811 case eSectionTypeDWARFDebugInfo:
812 case eSectionTypeDWARFDebugLine:
813 case eSectionTypeDWARFDebugLoc:
814 case eSectionTypeDWARFDebugMacInfo:
815 case eSectionTypeDWARFDebugPubNames:
816 case eSectionTypeDWARFDebugPubTypes:
817 case eSectionTypeDWARFDebugRanges:
818 case eSectionTypeDWARFDebugStr:
819 case eSectionTypeDWARFAppleNames:
820 case eSectionTypeDWARFAppleTypes:
821 case eSectionTypeDWARFAppleNamespaces:
822 case eSectionTypeDWARFAppleObjC:
823 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000824 case eSectionTypeEHFrame: return eAddressClassRuntime;
825 case eSectionTypeOther: return eAddressClassUnknown;
826 }
827 }
828 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000829
Greg Claytonb3448432011-03-24 21:19:54 +0000830 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000831 switch (symbol_type)
832 {
833 case eSymbolTypeAny: return eAddressClassUnknown;
834 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000835
Greg Claytonb1888f22011-03-19 01:12:21 +0000836 case eSymbolTypeCode:
837 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000838 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000839 if (m_header.cputype == llvm::MachO::CPUTypeARM)
840 {
841 // For ARM we have a bit in the n_desc field of the symbol
842 // that tells us ARM/Thumb which is bit 0x0008.
843 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
844 return eAddressClassCodeAlternateISA;
845 }
846 return eAddressClassCode;
847
848 case eSymbolTypeData: return eAddressClassData;
849 case eSymbolTypeRuntime: return eAddressClassRuntime;
850 case eSymbolTypeException: return eAddressClassRuntime;
851 case eSymbolTypeSourceFile: return eAddressClassDebug;
852 case eSymbolTypeHeaderFile: return eAddressClassDebug;
853 case eSymbolTypeObjectFile: return eAddressClassDebug;
854 case eSymbolTypeCommonBlock: return eAddressClassDebug;
855 case eSymbolTypeBlock: return eAddressClassDebug;
856 case eSymbolTypeLocal: return eAddressClassData;
857 case eSymbolTypeParam: return eAddressClassData;
858 case eSymbolTypeVariable: return eAddressClassData;
859 case eSymbolTypeVariableType: return eAddressClassDebug;
860 case eSymbolTypeLineEntry: return eAddressClassDebug;
861 case eSymbolTypeLineHeader: return eAddressClassDebug;
862 case eSymbolTypeScopeBegin: return eAddressClassDebug;
863 case eSymbolTypeScopeEnd: return eAddressClassDebug;
864 case eSymbolTypeAdditional: return eAddressClassUnknown;
865 case eSymbolTypeCompiler: return eAddressClassDebug;
866 case eSymbolTypeInstrumentation:return eAddressClassDebug;
867 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000868 case eSymbolTypeObjCClass: return eAddressClassRuntime;
869 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
870 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000871 }
872 }
873 }
874 return eAddressClassUnknown;
875}
Chris Lattner24943d22010-06-08 16:52:24 +0000876
877Symtab *
878ObjectFileMachO::GetSymtab()
879{
Greg Clayton9482f052012-03-13 23:14:29 +0000880 ModuleSP module_sp(GetModule());
881 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000882 {
Greg Clayton9482f052012-03-13 23:14:29 +0000883 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
884 if (m_symtab_ap.get() == NULL)
885 {
886 m_symtab_ap.reset(new Symtab(this));
887 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
888 ParseSymtab (true);
889 m_symtab_ap->Finalize ();
890 }
Chris Lattner24943d22010-06-08 16:52:24 +0000891 }
892 return m_symtab_ap.get();
893}
894
895
896SectionList *
897ObjectFileMachO::GetSectionList()
898{
Greg Clayton9482f052012-03-13 23:14:29 +0000899 ModuleSP module_sp(GetModule());
900 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000901 {
Greg Clayton9482f052012-03-13 23:14:29 +0000902 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
903 if (m_sections_ap.get() == NULL)
904 {
905 m_sections_ap.reset(new SectionList());
906 ParseSections();
907 }
Chris Lattner24943d22010-06-08 16:52:24 +0000908 }
909 return m_sections_ap.get();
910}
911
912
913size_t
914ObjectFileMachO::ParseSections ()
915{
916 lldb::user_id_t segID = 0;
917 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000918 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000919 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000920 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000921 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000922 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000923 // First look up any LC_ENCRYPTION_INFO load commands
924 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
925 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000926 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000927 for (i=0; i<m_header.ncmds; ++i)
928 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000929 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000930 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000931 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000932
Greg Clayton54e33712012-05-25 18:09:55 +0000933 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000934 {
Greg Clayton54e33712012-05-25 18:09:55 +0000935 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
936 {
937 if (encryption_cmd.cryptid != 0)
938 {
939 EncryptedFileRanges::Entry entry;
940 entry.SetRangeBase(encryption_cmd.cryptoff);
941 entry.SetByteSize(encryption_cmd.cryptsize);
942 encrypted_file_ranges.Append(entry);
943 }
944 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000945 }
Greg Clayton54e33712012-05-25 18:09:55 +0000946 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000947 }
948
949 offset = MachHeaderSizeFromMagic(m_header.magic);
950
Greg Clayton54e33712012-05-25 18:09:55 +0000951 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000952 for (i=0; i<m_header.ncmds; ++i)
953 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000954 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000955 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
956 break;
957
Greg Clayton1674b122010-07-21 22:12:05 +0000958 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000959 {
960 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
961 {
962 load_cmd.vmaddr = m_data.GetAddress(&offset);
963 load_cmd.vmsize = m_data.GetAddress(&offset);
964 load_cmd.fileoff = m_data.GetAddress(&offset);
965 load_cmd.filesize = m_data.GetAddress(&offset);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000966 if (m_length != 0 && load_cmd.filesize != 0)
967 {
Greg Claytonbb759862013-04-16 16:51:19 +0000968 if (load_cmd.fileoff > m_length)
969 {
970 // We have a load command that says it extends past the end of hte file. This is likely
971 // a corrupt file. We don't have any way to return an error condition here (this method
972 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
973 // is null out the SectionList vector and if a process has been set up, dump a message
974 // to stdout. The most common case here is core file debugging with a truncated file.
975 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
976 GetModule()->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 ")",
977 i,
978 lc_segment_name,
979 load_cmd.fileoff,
980 m_length);
981 m_sections_ap->Clear();
982 return 0;
983 }
984
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000985 if (load_cmd.fileoff + load_cmd.filesize > m_length)
986 {
987 // We have a load command that says it extends past the end of hte file. This is likely
988 // a corrupt file. We don't have any way to return an error condition here (this method
989 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
990 // is null out the SectionList vector and if a process has been set up, dump a message
Greg Claytonbb759862013-04-16 16:51:19 +0000991 // to stdout. The most common case here is core file debugging with a truncated file.
992 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
993 GetModule()->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff + filesize (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 ")",
994 i,
995 lc_segment_name,
996 load_cmd.fileoff + load_cmd.filesize,
997 m_length);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000998 m_sections_ap->Clear();
999 return 0;
1000 }
1001 }
Chris Lattner24943d22010-06-08 16:52:24 +00001002 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1003 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001004
Greg Clayton68ca8232011-01-25 02:58:48 +00001005 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
1006
Chris Lattner24943d22010-06-08 16:52:24 +00001007 // Keep a list of mach segments around in case we need to
1008 // get at data that isn't stored in the abstracted Sections.
1009 m_mach_segments.push_back (load_cmd);
1010
Greg Clayton36da2aa2013-01-25 18:06:21 +00001011 ConstString segment_name (load_cmd.segname, std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
Chris Lattner24943d22010-06-08 16:52:24 +00001012 // Use a segment ID of the segment index shifted left by 8 so they
1013 // never conflict with any of the sections.
1014 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +00001015 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +00001016 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001017 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +00001018 ++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
1019 segment_name, // Name of this section
1020 eSectionTypeContainer, // This section is a container of other sections.
1021 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1022 load_cmd.vmsize, // VM size in bytes of this section
1023 load_cmd.fileoff, // Offset to the data for this section in the file
1024 load_cmd.filesize, // Size in bytes of this section as found in the the file
1025 load_cmd.flags)); // Flags for this section
1026
Greg Clayton68ca8232011-01-25 02:58:48 +00001027 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001028 m_sections_ap->AddSection(segment_sp);
1029 }
1030
1031 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +00001032 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +00001033 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +00001034 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +00001035 // mach sections yet...
1036 if (m_mach_sections.empty())
1037 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +00001038 uint32_t segment_sect_idx;
1039 const lldb::user_id_t first_segment_sectID = sectID + 1;
1040
1041
Greg Clayton1674b122010-07-21 22:12:05 +00001042 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +00001043 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
1044 {
1045 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
1046 break;
1047 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1048 break;
1049 sect64.addr = m_data.GetAddress(&offset);
1050 sect64.size = m_data.GetAddress(&offset);
1051
1052 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1053 break;
1054
1055 // Keep a list of mach sections around in case we need to
1056 // get at data that isn't stored in the abstracted Sections.
1057 m_mach_sections.push_back (sect64);
1058
1059 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1060 if (!segment_name)
1061 {
1062 // We have a segment with no name so we need to conjure up
1063 // segments that correspond to the section's segname if there
1064 // isn't already such a section. If there is such a section,
1065 // we resize the section so that it spans all sections.
1066 // We also mark these sections as fake so address matches don't
1067 // hit if they land in the gaps between the child sections.
1068 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1069 segment_sp = m_sections_ap->FindSectionByName (segment_name);
1070 if (segment_sp.get())
1071 {
1072 Section *segment = segment_sp.get();
1073 // Grow the section size as needed.
1074 const lldb::addr_t sect64_min_addr = sect64.addr;
1075 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1076 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1077 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1078 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1079 if (sect64_min_addr >= curr_seg_min_addr)
1080 {
1081 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1082 // Only grow the section size if needed
1083 if (new_seg_byte_size > curr_seg_byte_size)
1084 segment->SetByteSize (new_seg_byte_size);
1085 }
1086 else
1087 {
1088 // We need to change the base address of the segment and
1089 // adjust the child section offsets for all existing children.
1090 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1091 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +00001092 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +00001093 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1094 }
Greg Clayton661825b2010-06-28 23:51:11 +00001095
1096 // Grow the section size as needed.
1097 if (sect64.offset)
1098 {
1099 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1100 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1101
1102 const lldb::addr_t section_min_file_offset = sect64.offset;
1103 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1104 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1105 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1106 segment->SetFileOffset (new_file_offset);
1107 segment->SetFileSize (new_file_size);
1108 }
Chris Lattner24943d22010-06-08 16:52:24 +00001109 }
1110 else
1111 {
1112 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +00001113 segment_sp.reset(new Section (segment_sp, // Parent section
1114 module_sp, // Module to which this section belongs
1115 ++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
1116 segment_name, // Name of this section
1117 eSectionTypeContainer, // This section is a container of other sections.
1118 sect64.addr, // File VM address == addresses as they are found in the object file
1119 sect64.size, // VM size in bytes of this section
1120 sect64.offset, // Offset to the data for this section in the file
1121 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1122 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +00001123 segment_sp->SetIsFake(true);
1124 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +00001125 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001126 }
1127 }
1128 assert (segment_sp.get());
1129
Greg Clayton1674b122010-07-21 22:12:05 +00001130 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +00001131 static ConstString g_sect_name_objc_data ("__objc_data");
1132 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1133 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1134 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1135 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1136 static ConstString g_sect_name_objc_const ("__objc_const");
1137 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1138 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001139
1140 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1141 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1142 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1143 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1144 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1145 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1146 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1147 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1148 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1149 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1150 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +00001151 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1152 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +00001153 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +00001154 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001155 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +00001156 static ConstString g_sect_name_DATA ("__DATA");
1157 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001158
Chris Lattner24943d22010-06-08 16:52:24 +00001159 SectionType sect_type = eSectionTypeOther;
1160
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001161 if (section_name == g_sect_name_dwarf_debug_abbrev)
1162 sect_type = eSectionTypeDWARFDebugAbbrev;
1163 else if (section_name == g_sect_name_dwarf_debug_aranges)
1164 sect_type = eSectionTypeDWARFDebugAranges;
1165 else if (section_name == g_sect_name_dwarf_debug_frame)
1166 sect_type = eSectionTypeDWARFDebugFrame;
1167 else if (section_name == g_sect_name_dwarf_debug_info)
1168 sect_type = eSectionTypeDWARFDebugInfo;
1169 else if (section_name == g_sect_name_dwarf_debug_line)
1170 sect_type = eSectionTypeDWARFDebugLine;
1171 else if (section_name == g_sect_name_dwarf_debug_loc)
1172 sect_type = eSectionTypeDWARFDebugLoc;
1173 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1174 sect_type = eSectionTypeDWARFDebugMacInfo;
1175 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1176 sect_type = eSectionTypeDWARFDebugPubNames;
1177 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1178 sect_type = eSectionTypeDWARFDebugPubTypes;
1179 else if (section_name == g_sect_name_dwarf_debug_ranges)
1180 sect_type = eSectionTypeDWARFDebugRanges;
1181 else if (section_name == g_sect_name_dwarf_debug_str)
1182 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001183 else if (section_name == g_sect_name_dwarf_apple_names)
1184 sect_type = eSectionTypeDWARFAppleNames;
1185 else if (section_name == g_sect_name_dwarf_apple_types)
1186 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001187 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1188 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001189 else if (section_name == g_sect_name_dwarf_apple_objc)
1190 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001191 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001192 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001193 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001194 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001195 else if (section_name == g_sect_name_eh_frame)
1196 sect_type = eSectionTypeEHFrame;
1197 else if (section_name == g_sect_name_cfstring)
1198 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001199 else if (section_name == g_sect_name_objc_data ||
1200 section_name == g_sect_name_objc_classrefs ||
1201 section_name == g_sect_name_objc_superrefs ||
1202 section_name == g_sect_name_objc_const ||
1203 section_name == g_sect_name_objc_classlist)
1204 {
1205 sect_type = eSectionTypeDataPointers;
1206 }
Chris Lattner24943d22010-06-08 16:52:24 +00001207
1208 if (sect_type == eSectionTypeOther)
1209 {
1210 switch (mach_sect_type)
1211 {
1212 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001213 case SectionTypeRegular:
1214 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001215 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001216 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001217 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001218 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001219 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001220 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001221 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1222 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1223 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1224 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1225 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1226 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1227 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1228 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1229 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1230 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1231 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1232 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1233 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1234 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1235 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1236 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001237 default: break;
1238 }
1239 }
1240
Greg Clayton3508c382012-02-24 01:59:29 +00001241 SectionSP section_sp(new Section (segment_sp,
1242 module_sp,
1243 ++sectID,
1244 section_name,
1245 sect_type,
1246 sect64.addr - segment_sp->GetFileAddress(),
1247 sect64.size,
1248 sect64.offset,
1249 sect64.offset == 0 ? 0 : sect64.size,
1250 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001251 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001252
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001253 bool section_is_encrypted = false;
1254 if (!segment_is_encrypted && load_cmd.filesize != 0)
1255 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001256
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001257 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001258 segment_sp->GetChildren().AddSection(section_sp);
1259
1260 if (segment_sp->IsFake())
1261 {
1262 segment_sp.reset();
1263 segment_name.Clear();
1264 }
1265 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001266 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001267 {
1268 if (first_segment_sectID <= sectID)
1269 {
1270 lldb::user_id_t sect_uid;
1271 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1272 {
1273 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1274 SectionSP next_section_sp;
1275 if (sect_uid + 1 <= sectID)
1276 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1277
1278 if (curr_section_sp.get())
1279 {
1280 if (curr_section_sp->GetByteSize() == 0)
1281 {
1282 if (next_section_sp.get() != NULL)
1283 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1284 else
1285 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1286 }
1287 }
1288 }
1289 }
1290 }
1291 }
1292 }
1293 }
Greg Clayton1674b122010-07-21 22:12:05 +00001294 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001295 {
1296 m_dysymtab.cmd = load_cmd.cmd;
1297 m_dysymtab.cmdsize = load_cmd.cmdsize;
1298 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1299 }
1300
1301 offset = load_cmd_offset + load_cmd.cmdsize;
1302 }
1303// if (dump_sections)
1304// {
1305// StreamFile s(stdout);
1306// m_sections_ap->Dump(&s, true);
1307// }
1308 return sectID; // Return the number of sections we registered with the module
1309}
1310
1311class MachSymtabSectionInfo
1312{
1313public:
1314
1315 MachSymtabSectionInfo (SectionList *section_list) :
1316 m_section_list (section_list),
1317 m_section_infos()
1318 {
1319 // Get the number of sections down to a depth of 1 to include
1320 // all segments and their sections, but no other sections that
1321 // may be added for debug map or
1322 m_section_infos.resize(section_list->GetNumSections(1));
1323 }
1324
1325
Greg Clayton3508c382012-02-24 01:59:29 +00001326 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001327 GetSection (uint8_t n_sect, addr_t file_addr)
1328 {
1329 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001330 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001331 if (n_sect < m_section_infos.size())
1332 {
Greg Clayton3508c382012-02-24 01:59:29 +00001333 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001334 {
Greg Clayton3508c382012-02-24 01:59:29 +00001335 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1336 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001337 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001338 {
Greg Clayton3508c382012-02-24 01:59:29 +00001339 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1340 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001341 }
1342 else
1343 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001344 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001345 }
Chris Lattner24943d22010-06-08 16:52:24 +00001346 }
1347 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001348 {
1349 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001350 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001351 }
1352 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1353 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1354 {
1355 // Symbol is in section with zero size, but has the same start
1356 // address as the section. This can happen with linker symbols
1357 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001358 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001359 }
Chris Lattner24943d22010-06-08 16:52:24 +00001360 }
Greg Clayton3508c382012-02-24 01:59:29 +00001361 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001362 }
1363
1364protected:
1365 struct SectionInfo
1366 {
1367 SectionInfo () :
1368 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001369 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001370 {
1371 }
1372
1373 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001374 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001375 };
1376 SectionList *m_section_list;
1377 std::vector<SectionInfo> m_section_infos;
1378};
1379
Chris Lattner24943d22010-06-08 16:52:24 +00001380size_t
1381ObjectFileMachO::ParseSymtab (bool minimize)
1382{
1383 Timer scoped_timer(__PRETTY_FUNCTION__,
1384 "ObjectFileMachO::ParseSymtab () module = %s",
1385 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001386 ModuleSP module_sp (GetModule());
1387 if (!module_sp)
1388 return 0;
1389
1390 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1391 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1392 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1393 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001394 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001395 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001396
Greg Clayton952e9dc2013-03-27 23:08:40 +00001397 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001398
Chris Lattner24943d22010-06-08 16:52:24 +00001399 for (i=0; i<m_header.ncmds; ++i)
1400 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001401 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001402 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001403 struct load_command lc;
1404 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001405 break;
1406 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001407 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001408 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001409 case LoadCommandSymtab:
1410 symtab_load_command.cmd = lc.cmd;
1411 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001412 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001413 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1414 return 0;
1415 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001416 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001417 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001418 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001419 return 0;
1420 }
1421
1422 if (symtab_load_command.stroff == 0)
1423 {
1424 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001425 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001426 return 0;
1427 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001428
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001429 if (symtab_load_command.nsyms == 0)
1430 {
1431 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001432 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001433 return 0;
1434 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001435
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001436 if (symtab_load_command.strsize == 0)
1437 {
1438 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001439 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001440 return 0;
1441 }
1442 break;
1443
1444 case LoadCommandFunctionStarts:
1445 function_starts_load_command.cmd = lc.cmd;
1446 function_starts_load_command.cmdsize = lc.cmdsize;
1447 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1448 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1449 break;
1450
1451 default:
1452 break;
1453 }
1454 offset = cmd_offset + lc.cmdsize;
1455 }
1456
1457 if (symtab_load_command.cmd)
1458 {
1459 Symtab *symtab = m_symtab_ap.get();
1460 SectionList *section_list = GetSectionList();
1461 if (section_list == NULL)
1462 return 0;
1463
1464 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001465 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001466
Greg Clayton36da2aa2013-01-25 18:06:21 +00001467 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1468 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001469 bool bit_width_32 = addr_byte_size == 4;
1470 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1471
Greg Clayton36da2aa2013-01-25 18:06:21 +00001472 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1473 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1474 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001475 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001476
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001477 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1478 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001479 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1480 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001481 {
Greg Claytondd29b972012-05-18 23:20:01 +00001482 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001483 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1484 // Reading mach file from memory in a process or core file...
1485
1486 if (linkedit_section_sp)
1487 {
1488 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1489 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1490 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001491 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001492
1493 bool data_was_read = false;
1494
1495#if defined (__APPLE__) && defined (__arm__)
1496 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001497 {
Greg Clayton29021d32012-04-18 05:19:20 +00001498 // This mach-o memory file is in the dyld shared cache. If this
1499 // program is not remote and this is iOS, then this process will
1500 // share the same shared cache as the process we are debugging and
1501 // we can read the entire __LINKEDIT from the address space in this
1502 // process. This is a needed optimization that is used for local iOS
1503 // debugging only since all shared libraries in the shared cache do
1504 // not have corresponding files that exist in the file system of the
1505 // device. They have been combined into a single file. This means we
1506 // always have to load these files from memory. All of the symbol and
1507 // string tables from all of the __LINKEDIT sections from the shared
1508 // libraries in the shared cache have been merged into a single large
1509 // symbol and string table. Reading all of this symbol and string table
1510 // data across can slow down debug launch times, so we optimize this by
1511 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda45c75502013-04-16 06:24:42 +00001512
1513 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1514 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1515 bool use_lldb_cache = true;
1516 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1517 {
1518 use_lldb_cache = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001519 ModuleSP module_sp (GetModule());
1520 if (module_sp)
1521 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1522
Jason Molenda45c75502013-04-16 06:24:42 +00001523 }
1524
Greg Clayton29021d32012-04-18 05:19:20 +00001525 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda45c75502013-04-16 06:24:42 +00001526 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton29021d32012-04-18 05:19:20 +00001527 {
1528 data_was_read = true;
1529 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001530 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001531 if (function_starts_load_command.cmd)
1532 {
1533 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1534 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1535 }
1536 }
1537 }
1538#endif
1539
1540 if (!data_was_read)
1541 {
1542 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1543 if (nlist_data_sp)
1544 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001545 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1546 //if (strtab_data_sp)
1547 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001548 if (m_dysymtab.nindirectsyms != 0)
1549 {
1550 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1551 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1552 if (indirect_syms_data_sp)
1553 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1554 }
Greg Clayton29021d32012-04-18 05:19:20 +00001555 if (function_starts_load_command.cmd)
1556 {
1557 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1558 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1559 if (func_start_data_sp)
1560 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1561 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001562 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001563 }
1564 }
1565 else
1566 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001567 nlist_data.SetData (m_data,
1568 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001569 nlist_data_byte_size);
1570 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001571 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001572 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001573 if (m_dysymtab.nindirectsyms != 0)
1574 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001575 indirect_symbol_index_data.SetData (m_data,
1576 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001577 m_dysymtab.nindirectsyms * 4);
1578 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001579 if (function_starts_load_command.cmd)
1580 {
1581 function_starts_data.SetData (m_data,
1582 function_starts_load_command.dataoff,
1583 function_starts_load_command.datasize);
1584 }
1585 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001586
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001587 if (nlist_data.GetByteSize() == 0)
1588 {
1589 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001590 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001591 return 0;
1592 }
1593
1594
Greg Clayton3a5dc012012-05-25 17:04:00 +00001595 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1596 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001597 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001598 if (process)
1599 {
1600 if (strtab_addr == LLDB_INVALID_ADDRESS)
1601 {
1602 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001603 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001604 return 0;
1605 }
1606 }
1607 else
Greg Claytondd29b972012-05-18 23:20:01 +00001608 {
1609 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001610 module_sp->LogMessage(log, "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001611 return 0;
1612 }
1613 }
Greg Claytondd29b972012-05-18 23:20:01 +00001614
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001615 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1616 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1617 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1618 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1619 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1620 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1621 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1622 SectionSP eh_frame_section_sp;
1623 if (text_section_sp.get())
1624 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1625 else
1626 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1627
Greg Claytond2653c22012-03-14 01:53:24 +00001628 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001629
1630 // lldb works best if it knows the start addresss of all functions in a module.
1631 // Linker symbols or debug info are normally the best source of information for start addr / size but
1632 // they may be stripped in a released binary.
Jason Molenda2120aef2013-04-16 00:18:44 +00001633 // Two additional sources of information exist in Mach-O binaries:
Jason Molendad7938392013-03-21 03:36:01 +00001634 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1635 // binary, relative to the text section.
1636 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1637 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1638 // Binaries built to run on older releases may need to use eh_frame information.
1639
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001640 if (text_section_sp && function_starts_data.GetByteSize())
1641 {
1642 FunctionStarts::Entry function_start_entry;
1643 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001644 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001645 function_start_entry.addr = text_section_sp->GetFileAddress();
1646 uint64_t delta;
1647 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1648 {
1649 // Now append the current entry
1650 function_start_entry.addr += delta;
1651 function_starts.Append(function_start_entry);
1652 }
Jason Molenda2120aef2013-04-16 00:18:44 +00001653 }
Jason Molendad7938392013-03-21 03:36:01 +00001654 else
1655 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001656 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1657 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1658 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1659 // the module.
1660 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001661 {
1662 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1663 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1664 eh_frame.GetFunctionAddressAndSizeVector (functions);
1665 addr_t text_base_addr = text_section_sp->GetFileAddress();
1666 size_t count = functions.GetSize();
1667 for (size_t i = 0; i < count; ++i)
1668 {
1669 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1670 if (func)
1671 {
1672 FunctionStarts::Entry function_start_entry;
1673 function_start_entry.addr = func->base - text_base_addr;
1674 function_starts.Append(function_start_entry);
1675 }
1676 }
1677 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001678 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001679
Greg Clayton36da2aa2013-01-25 18:06:21 +00001680 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001681
Greg Clayton36da2aa2013-01-25 18:06:21 +00001682 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001683
Greg Clayton36da2aa2013-01-25 18:06:21 +00001684 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001685
1686 uint32_t N_SO_index = UINT32_MAX;
1687
1688 MachSymtabSectionInfo section_info (section_list);
1689 std::vector<uint32_t> N_FUN_indexes;
1690 std::vector<uint32_t> N_NSYM_indexes;
1691 std::vector<uint32_t> N_INCL_indexes;
1692 std::vector<uint32_t> N_BRAC_indexes;
1693 std::vector<uint32_t> N_COMM_indexes;
1694 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1695 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1696 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1697 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1698 // Any symbols that get merged into another will get an entry
1699 // in this map so we know
1700 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1701 uint32_t nlist_idx = 0;
1702 Symbol *symbol_ptr = NULL;
1703
1704 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001705 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001706 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001707 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001708 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001709
Jason Molendab62abd52012-06-21 01:51:02 +00001710#if defined (__APPLE__) && defined (__arm__)
1711
1712 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1713 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1714 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1715 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1716 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1717 // nlist parser to ignore all LOCAL symbols.
1718
1719 if (m_header.flags & 0x80000000u)
1720 {
1721 // Before we can start mapping the DSC, we need to make certain the target process is actually
1722 // using the cache we can find.
1723
Jason Molendab62abd52012-06-21 01:51:02 +00001724 // Next we need to determine the correct path for the dyld shared cache.
1725
1726 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1727 char dsc_path[PATH_MAX];
1728
1729 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001730 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1731 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001732 header_arch.GetArchitectureName());
1733
1734 FileSpec dsc_filespec(dsc_path, false);
1735
1736 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda2120aef2013-04-16 00:18:44 +00001737 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001738 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001739 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1740 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1741 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001742 uint32_t imagesOffset;
1743 uint32_t imagesCount;
1744 uint64_t dyldBaseAddress;
1745 uint64_t codeSignatureOffset;
1746 uint64_t codeSignatureSize;
1747 uint64_t slideInfoOffset;
1748 uint64_t slideInfoSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001749 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1750 uint64_t localSymbolsSize; // size of local symbols information
1751 };
1752 struct lldb_copy_dyld_cache_header_v1
1753 {
1754 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1755 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1756 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1757 uint32_t imagesOffset;
1758 uint32_t imagesCount;
1759 uint64_t dyldBaseAddress;
1760 uint64_t codeSignatureOffset;
1761 uint64_t codeSignatureSize;
1762 uint64_t slideInfoOffset;
1763 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001764 uint64_t localSymbolsOffset;
1765 uint64_t localSymbolsSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001766 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001767 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001768
Jason Molenda2120aef2013-04-16 00:18:44 +00001769 struct lldb_copy_dyld_cache_mapping_info
1770 {
1771 uint64_t address;
1772 uint64_t size;
1773 uint64_t fileOffset;
1774 uint32_t maxProt;
1775 uint32_t initProt;
1776 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001777
Greg Claytonab77dcb2012-09-05 22:30:51 +00001778 struct lldb_copy_dyld_cache_local_symbols_info
1779 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001780 uint32_t nlistOffset;
1781 uint32_t nlistCount;
1782 uint32_t stringsOffset;
1783 uint32_t stringsSize;
1784 uint32_t entriesOffset;
1785 uint32_t entriesCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001786 };
1787 struct lldb_copy_dyld_cache_local_symbols_entry
1788 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001789 uint32_t dylibOffset;
1790 uint32_t nlistStartIndex;
1791 uint32_t nlistCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001792 };
Jason Molendab62abd52012-06-21 01:51:02 +00001793
Jason Molendafd3b35d2012-06-22 03:28:35 +00001794 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1795 The dyld_cache_local_symbols_info structure gives us three things:
1796 1. The start and count of the nlist records in the dyld_shared_cache file
1797 2. The start and size of the strings for these nlist records
1798 3. The start and count of dyld_cache_local_symbols_entry entries
1799
1800 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1801 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001802 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001803 and the count of how many nlist records there are for this dylib/framework.
1804 */
1805
Jason Molendab62abd52012-06-21 01:51:02 +00001806 // Process the dsc header to find the unmapped symbols
1807 //
1808 // Save some VM space, do not map the entire cache in one shot.
1809
Jason Molenda6bcabae2013-03-06 23:17:36 +00001810 DataBufferSP dsc_data_sp;
1811 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1812
1813 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001814 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001815 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001816
Jason Molenda6bcabae2013-03-06 23:17:36 +00001817 char version_str[17];
1818 int version = -1;
1819 lldb::offset_t offset = 0;
1820 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1821 version_str[16] = '\0';
1822 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1823 {
1824 int v;
1825 if (::sscanf (version_str + 6, "%d", &v) == 1)
1826 {
1827 version = v;
1828 }
1829 }
1830
Jason Molenda45c75502013-04-16 06:24:42 +00001831 UUID dsc_uuid;
1832 if (version >= 1)
1833 {
1834 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1835 uint8_t uuid_bytes[sizeof (uuid_t)];
1836 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1837 dsc_uuid.SetBytes (uuid_bytes);
1838 }
1839
1840 bool uuid_match = true;
1841 if (dsc_uuid.IsValid() && process)
1842 {
1843 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1844
1845 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1846 {
1847 // The on-disk dyld_shared_cache file is not the same as the one in this
1848 // process' memory, don't use it.
1849 uuid_match = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001850 ModuleSP module_sp (GetModule());
1851 if (module_sp)
1852 module_sp->ReportWarning ("process shared cache does not match on-disk dyld_shared_cache file, some symbol names will be missing.");
Jason Molenda45c75502013-04-16 06:24:42 +00001853 }
1854 }
1855
Jason Molenda9badb6c2013-03-06 23:19:17 +00001856 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001857
Jason Molendab62abd52012-06-21 01:51:02 +00001858 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1859
1860 // If the mappingOffset points to a location inside the header, we've
1861 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda45c75502013-04-16 06:24:42 +00001862 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendab62abd52012-06-21 01:51:02 +00001863 {
1864
Jason Molenda6bcabae2013-03-06 23:17:36 +00001865 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1866 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1867 offset = 0;
1868
1869 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1870 // in the shared library cache need to be adjusted by an offset to match up with the
1871 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1872 // recorded in mapping_offset_value.
1873 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1874
1875 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001876 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1877 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1878
Jason Molenda9badb6c2013-03-06 23:19:17 +00001879 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001880 {
1881 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001882 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001883 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001884 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001885
1886 offset = 0;
1887
1888 // Read the local_symbols_infos struct in one shot
1889 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1890 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1891
Jason Molendab62abd52012-06-21 01:51:02 +00001892 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1893
Jason Molenda6bcabae2013-03-06 23:17:36 +00001894 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001895
1896 offset = local_symbols_info.entriesOffset;
1897 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1898 {
1899 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1900 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1901 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1902 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1903
Jason Molenda9badb6c2013-03-06 23:19:17 +00001904 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001905 {
1906 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1907
1908 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1909 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1910 num_syms = symtab->GetNumSymbols();
1911
1912 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1913 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1914
Jason Molenda9badb6c2013-03-06 23:19:17 +00001915 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001916 {
1917 /////////////////////////////
1918 {
1919 struct nlist_64 nlist;
1920 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1921 break;
1922
1923 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1924 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1925 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1926 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1927 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1928
1929 SymbolType type = eSymbolTypeInvalid;
1930 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1931
1932 if (symbol_name == NULL)
1933 {
1934 // No symbol should be NULL, even the symbols with no
1935 // string values should have an offset zero which points
1936 // to an empty C-string
1937 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00001938 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Jason Molendab62abd52012-06-21 01:51:02 +00001939 entry_index,
1940 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00001941 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendab62abd52012-06-21 01:51:02 +00001942 continue;
1943 }
1944 if (symbol_name[0] == '\0')
1945 symbol_name = NULL;
1946
1947 const char *symbol_name_non_abi_mangled = NULL;
1948
1949 SectionSP symbol_section;
1950 uint32_t symbol_byte_size = 0;
1951 bool add_nlist = true;
1952 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001953 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001954
1955 assert (sym_idx < num_syms);
1956
1957 sym[sym_idx].SetDebug (is_debug);
1958
1959 if (is_debug)
1960 {
1961 switch (nlist.n_type)
1962 {
1963 case StabGlobalSymbol:
1964 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1965 // Sometimes the N_GSYM value contains the address.
1966
1967 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1968 // have the same address, but we want to ensure that we always find only the real symbol,
1969 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1970 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1971 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1972 // same address.
1973
1974 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1975 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1976 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1977 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1978 add_nlist = false;
1979 else
1980 {
1981 sym[sym_idx].SetExternal(true);
1982 if (nlist.n_value != 0)
1983 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1984 type = eSymbolTypeData;
1985 }
1986 break;
1987
1988 case StabFunctionName:
1989 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1990 type = eSymbolTypeCompiler;
1991 break;
1992
1993 case StabFunction:
1994 // N_FUN -- procedure: name,,n_sect,linenumber,address
1995 if (symbol_name)
1996 {
1997 type = eSymbolTypeCode;
1998 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1999
2000 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2001 // We use the current number of symbols in the symbol table in lieu of
2002 // using nlist_idx in case we ever start trimming entries out
2003 N_FUN_indexes.push_back(sym_idx);
2004 }
2005 else
2006 {
2007 type = eSymbolTypeCompiler;
2008
2009 if ( !N_FUN_indexes.empty() )
2010 {
2011 // Copy the size of the function into the original STAB entry so we don't have
2012 // to hunt for it later
2013 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2014 N_FUN_indexes.pop_back();
2015 // We don't really need the end function STAB as it contains the size which
2016 // we already placed with the original symbol, so don't add it if we want a
2017 // minimal symbol table
2018 if (minimize)
2019 add_nlist = false;
2020 }
2021 }
2022 break;
2023
2024 case StabStaticSymbol:
2025 // N_STSYM -- static symbol: name,,n_sect,type,address
2026 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2027 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2028 type = eSymbolTypeData;
2029 break;
2030
2031 case StabLocalCommon:
2032 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2033 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2034 type = eSymbolTypeCommonBlock;
2035 break;
2036
2037 case StabBeginSymbol:
2038 // N_BNSYM
2039 // We use the current number of symbols in the symbol table in lieu of
2040 // using nlist_idx in case we ever start trimming entries out
2041 if (minimize)
2042 {
2043 // Skip these if we want minimal symbol tables
2044 add_nlist = false;
2045 }
2046 else
2047 {
2048 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2049 N_NSYM_indexes.push_back(sym_idx);
2050 type = eSymbolTypeScopeBegin;
2051 }
2052 break;
2053
2054 case StabEndSymbol:
2055 // N_ENSYM
2056 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2057 // so that we can always skip the entire symbol if we need to navigate
2058 // more quickly at the source level when parsing STABS
2059 if (minimize)
2060 {
2061 // Skip these if we want minimal symbol tables
2062 add_nlist = false;
2063 }
2064 else
2065 {
2066 if ( !N_NSYM_indexes.empty() )
2067 {
2068 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2069 symbol_ptr->SetByteSize(sym_idx + 1);
2070 symbol_ptr->SetSizeIsSibling(true);
2071 N_NSYM_indexes.pop_back();
2072 }
2073 type = eSymbolTypeScopeEnd;
2074 }
2075 break;
2076
2077
2078 case StabSourceFileOptions:
2079 // N_OPT - emitted with gcc2_compiled and in gcc source
2080 type = eSymbolTypeCompiler;
2081 break;
2082
2083 case StabRegisterSymbol:
2084 // N_RSYM - register sym: name,,NO_SECT,type,register
2085 type = eSymbolTypeVariable;
2086 break;
2087
2088 case StabSourceLine:
2089 // N_SLINE - src line: 0,,n_sect,linenumber,address
2090 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2091 type = eSymbolTypeLineEntry;
2092 break;
2093
2094 case StabStructureType:
2095 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2096 type = eSymbolTypeVariableType;
2097 break;
2098
2099 case StabSourceFileName:
2100 // N_SO - source file name
2101 type = eSymbolTypeSourceFile;
2102 if (symbol_name == NULL)
2103 {
2104 if (minimize)
2105 add_nlist = false;
2106 if (N_SO_index != UINT32_MAX)
2107 {
2108 // Set the size of the N_SO to the terminating index of this N_SO
2109 // so that we can always skip the entire N_SO if we need to navigate
2110 // more quickly at the source level when parsing STABS
2111 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2112 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2113 symbol_ptr->SetSizeIsSibling(true);
2114 }
2115 N_NSYM_indexes.clear();
2116 N_INCL_indexes.clear();
2117 N_BRAC_indexes.clear();
2118 N_COMM_indexes.clear();
2119 N_FUN_indexes.clear();
2120 N_SO_index = UINT32_MAX;
2121 }
2122 else
2123 {
2124 // We use the current number of symbols in the symbol table in lieu of
2125 // using nlist_idx in case we ever start trimming entries out
2126 const bool N_SO_has_full_path = symbol_name[0] == '/';
2127 if (N_SO_has_full_path)
2128 {
2129 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2130 {
2131 // We have two consecutive N_SO entries where the first contains a directory
2132 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00002133 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002134 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2135 add_nlist = false;
2136 }
2137 else
2138 {
2139 // This is the first entry in a N_SO that contains a directory or
2140 // a full path to the source file
2141 N_SO_index = sym_idx;
2142 }
2143 }
2144 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2145 {
2146 // This is usually the second N_SO entry that contains just the filename,
2147 // so here we combine it with the first one if we are minimizing the symbol table
2148 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2149 if (so_path && so_path[0])
2150 {
2151 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002152 const size_t double_slash_pos = full_so_path.find("//");
2153 if (double_slash_pos != std::string::npos)
2154 {
2155 // The linker has been generating bad N_SO entries with doubled up paths
2156 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2157 // and the second is the directory for the source file so you end up with
2158 // a path that looks like "/tmp/src//tmp/src/"
2159 FileSpec so_dir(so_path, false);
2160 if (!so_dir.Exists())
2161 {
2162 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2163 if (so_dir.Exists())
2164 {
2165 // Trim off the incorrect path
2166 full_so_path.erase(0, double_slash_pos + 1);
2167 }
2168 }
2169 }
Jason Molendab62abd52012-06-21 01:51:02 +00002170 if (*full_so_path.rbegin() != '/')
2171 full_so_path += '/';
2172 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00002173 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002174 add_nlist = false;
2175 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2176 }
2177 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002178 else
2179 {
2180 // This could be a relative path to a N_SO
2181 N_SO_index = sym_idx;
2182 }
Jason Molendab62abd52012-06-21 01:51:02 +00002183 }
Jason Molendab62abd52012-06-21 01:51:02 +00002184 break;
2185
2186 case StabObjectFileName:
2187 // N_OSO - object file name: name,,0,0,st_mtime
2188 type = eSymbolTypeObjectFile;
2189 break;
2190
2191 case StabLocalSymbol:
2192 // N_LSYM - local sym: name,,NO_SECT,type,offset
2193 type = eSymbolTypeLocal;
2194 break;
2195
2196 //----------------------------------------------------------------------
2197 // INCL scopes
2198 //----------------------------------------------------------------------
2199 case StabBeginIncludeFileName:
2200 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2201 // We use the current number of symbols in the symbol table in lieu of
2202 // using nlist_idx in case we ever start trimming entries out
2203 N_INCL_indexes.push_back(sym_idx);
2204 type = eSymbolTypeScopeBegin;
2205 break;
2206
2207 case StabEndIncludeFile:
2208 // N_EINCL - include file end: name,,NO_SECT,0,0
2209 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2210 // so that we can always skip the entire symbol if we need to navigate
2211 // more quickly at the source level when parsing STABS
2212 if ( !N_INCL_indexes.empty() )
2213 {
2214 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2215 symbol_ptr->SetByteSize(sym_idx + 1);
2216 symbol_ptr->SetSizeIsSibling(true);
2217 N_INCL_indexes.pop_back();
2218 }
2219 type = eSymbolTypeScopeEnd;
2220 break;
2221
2222 case StabIncludeFileName:
2223 // N_SOL - #included file name: name,,n_sect,0,address
2224 type = eSymbolTypeHeaderFile;
2225
2226 // We currently don't use the header files on darwin
2227 if (minimize)
2228 add_nlist = false;
2229 break;
2230
2231 case StabCompilerParameters:
2232 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2233 type = eSymbolTypeCompiler;
2234 break;
2235
2236 case StabCompilerVersion:
2237 // N_VERSION - compiler version: name,,NO_SECT,0,0
2238 type = eSymbolTypeCompiler;
2239 break;
2240
2241 case StabCompilerOptLevel:
2242 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2243 type = eSymbolTypeCompiler;
2244 break;
2245
2246 case StabParameter:
2247 // N_PSYM - parameter: name,,NO_SECT,type,offset
2248 type = eSymbolTypeVariable;
2249 break;
2250
2251 case StabAlternateEntry:
2252 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2253 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2254 type = eSymbolTypeLineEntry;
2255 break;
2256
2257 //----------------------------------------------------------------------
2258 // Left and Right Braces
2259 //----------------------------------------------------------------------
2260 case StabLeftBracket:
2261 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2262 // We use the current number of symbols in the symbol table in lieu of
2263 // using nlist_idx in case we ever start trimming entries out
2264 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2265 N_BRAC_indexes.push_back(sym_idx);
2266 type = eSymbolTypeScopeBegin;
2267 break;
2268
2269 case StabRightBracket:
2270 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2271 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2272 // so that we can always skip the entire symbol if we need to navigate
2273 // more quickly at the source level when parsing STABS
2274 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2275 if ( !N_BRAC_indexes.empty() )
2276 {
2277 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2278 symbol_ptr->SetByteSize(sym_idx + 1);
2279 symbol_ptr->SetSizeIsSibling(true);
2280 N_BRAC_indexes.pop_back();
2281 }
2282 type = eSymbolTypeScopeEnd;
2283 break;
2284
2285 case StabDeletedIncludeFile:
2286 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2287 type = eSymbolTypeHeaderFile;
2288 break;
2289
2290 //----------------------------------------------------------------------
2291 // COMM scopes
2292 //----------------------------------------------------------------------
2293 case StabBeginCommon:
2294 // N_BCOMM - begin common: name,,NO_SECT,0,0
2295 // We use the current number of symbols in the symbol table in lieu of
2296 // using nlist_idx in case we ever start trimming entries out
2297 type = eSymbolTypeScopeBegin;
2298 N_COMM_indexes.push_back(sym_idx);
2299 break;
2300
2301 case StabEndCommonLocal:
2302 // N_ECOML - end common (local name): 0,,n_sect,0,address
2303 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2304 // Fall through
2305
2306 case StabEndCommon:
2307 // N_ECOMM - end common: name,,n_sect,0,0
2308 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2309 // so that we can always skip the entire symbol if we need to navigate
2310 // more quickly at the source level when parsing STABS
2311 if ( !N_COMM_indexes.empty() )
2312 {
2313 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2314 symbol_ptr->SetByteSize(sym_idx + 1);
2315 symbol_ptr->SetSizeIsSibling(true);
2316 N_COMM_indexes.pop_back();
2317 }
2318 type = eSymbolTypeScopeEnd;
2319 break;
2320
2321 case StabLength:
2322 // N_LENG - second stab entry with length information
2323 type = eSymbolTypeAdditional;
2324 break;
2325
2326 default: break;
2327 }
2328 }
2329 else
2330 {
2331 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2332 uint8_t n_type = NlistMaskType & nlist.n_type;
2333 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2334
2335 switch (n_type)
2336 {
2337 case NListTypeIndirect: // N_INDR - Fall through
2338 case NListTypePreboundUndefined:// N_PBUD - Fall through
2339 case NListTypeUndefined: // N_UNDF
2340 type = eSymbolTypeUndefined;
2341 break;
2342
2343 case NListTypeAbsolute: // N_ABS
2344 type = eSymbolTypeAbsolute;
2345 break;
2346
2347 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002348 {
2349 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002350
Greg Clayton01e6a582012-11-27 01:52:16 +00002351 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002352 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002353 // TODO: warn about this?
2354 add_nlist = false;
2355 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002356 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002357
2358 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002359 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002360 type = eSymbolTypeException;
2361 }
2362 else
2363 {
2364 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002365
Greg Clayton01e6a582012-11-27 01:52:16 +00002366 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002367 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002368 case SectionTypeRegular: break; // regular section
2369 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2370 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2371 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2372 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2373 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2374 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2375 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2376 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2377 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2378 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2379 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2380 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2381 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2382 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2383 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2384 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2385 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002386 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002387
Greg Clayton01e6a582012-11-27 01:52:16 +00002388 if (type == eSymbolTypeInvalid)
2389 {
2390 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2391 if (symbol_section->IsDescendant (text_section_sp.get()))
2392 {
2393 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2394 SectionAttrUserSelfModifyingCode |
2395 SectionAttrSytemSomeInstructions))
2396 type = eSymbolTypeData;
2397 else
2398 type = eSymbolTypeCode;
2399 }
2400 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002401 {
2402 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2403 {
2404 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002405
Greg Clayton01e6a582012-11-27 01:52:16 +00002406 if (symbol_name &&
2407 symbol_name[0] == '_' &&
2408 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002409 symbol_name[2] == 'B')
2410 {
2411 llvm::StringRef symbol_name_ref(symbol_name);
2412 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2413 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2414 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2415 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2416 {
2417 symbol_name_non_abi_mangled = symbol_name + 1;
2418 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2419 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002420 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002421 }
2422 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2423 {
2424 symbol_name_non_abi_mangled = symbol_name + 1;
2425 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2426 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002427 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002428 }
2429 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2430 {
2431 symbol_name_non_abi_mangled = symbol_name + 1;
2432 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2433 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002434 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002435 }
2436 }
2437 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002438 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002439 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002440 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002441 }
2442 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002443 {
2444 type = eSymbolTypeData;
2445 }
2446 }
2447 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2448 {
2449 type = eSymbolTypeTrampoline;
2450 }
2451 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2452 {
2453 type = eSymbolTypeRuntime;
2454 if (symbol_name && symbol_name[0] == '.')
2455 {
2456 llvm::StringRef symbol_name_ref(symbol_name);
2457 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2458 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002459 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002460 symbol_name_non_abi_mangled = symbol_name;
2461 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2462 type = eSymbolTypeObjCClass;
2463 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002464 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002465 }
2466 }
2467 }
Jason Molendab62abd52012-06-21 01:51:02 +00002468 }
2469 }
Jason Molendab62abd52012-06-21 01:51:02 +00002470 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002471 }
Jason Molendab62abd52012-06-21 01:51:02 +00002472 }
2473
2474 if (add_nlist)
2475 {
2476 uint64_t symbol_value = nlist.n_value;
2477 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002478
Jason Molendab62abd52012-06-21 01:51:02 +00002479 if (symbol_name_non_abi_mangled)
2480 {
Jason Molenda292cca82012-07-20 03:35:44 +00002481 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2482 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002483 }
2484 else
2485 {
2486 if (symbol_name && symbol_name[0] == '_')
2487 {
2488 symbol_name_is_mangled = symbol_name[1] == '_';
2489 symbol_name++; // Skip the leading underscore
2490 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002491
Jason Molendab62abd52012-06-21 01:51:02 +00002492 if (symbol_name)
2493 {
Jason Molenda292cca82012-07-20 03:35:44 +00002494 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002495 }
2496 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002497
Jason Molendab62abd52012-06-21 01:51:02 +00002498 if (is_debug == false)
2499 {
2500 if (type == eSymbolTypeCode)
2501 {
2502 // See if we can find a N_FUN entry for any code symbols.
2503 // If we do find a match, and the name matches, then we
2504 // can merge the two into just the function symbol to avoid
2505 // duplicate entries in the symbol table
2506 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2507 if (pos != N_FUN_addr_to_sym_idx.end())
2508 {
2509 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2510 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2511 {
2512 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2513 // We just need the flags from the linker symbol, so put these flags
2514 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2515 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2516 sym[sym_idx].Clear();
2517 continue;
2518 }
2519 }
2520 }
2521 else if (type == eSymbolTypeData)
2522 {
2523 // See if we can find a N_STSYM entry for any data symbols.
2524 // If we do find a match, and the name matches, then we
2525 // can merge the two into just the Static symbol to avoid
2526 // duplicate entries in the symbol table
2527 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2528 if (pos != N_STSYM_addr_to_sym_idx.end())
2529 {
2530 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2531 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2532 {
2533 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2534 // We just need the flags from the linker symbol, so put these flags
2535 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2536 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2537 sym[sym_idx].Clear();
2538 continue;
2539 }
2540 }
2541 }
2542 }
2543 if (symbol_section)
2544 {
2545 const addr_t section_file_addr = symbol_section->GetFileAddress();
2546 if (symbol_byte_size == 0 && function_starts_count > 0)
2547 {
2548 addr_t symbol_lookup_file_addr = nlist.n_value;
2549 // Do an exact address match for non-ARM addresses, else get the closest since
2550 // the symbol might be a thumb symbol which has an address with bit zero set
2551 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2552 if (is_arm && func_start_entry)
2553 {
2554 // Verify that the function start address is the symbol address (ARM)
2555 // or the symbol address + 1 (thumb)
2556 if (func_start_entry->addr != symbol_lookup_file_addr &&
2557 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2558 {
2559 // Not the right entry, NULL it out...
2560 func_start_entry = NULL;
2561 }
2562 }
2563 if (func_start_entry)
2564 {
2565 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002566
Jason Molendab62abd52012-06-21 01:51:02 +00002567 addr_t symbol_file_addr = func_start_entry->addr;
2568 uint32_t symbol_flags = 0;
2569 if (is_arm)
2570 {
2571 if (symbol_file_addr & 1)
2572 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2573 symbol_file_addr &= 0xfffffffffffffffeull;
2574 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002575
Jason Molendab62abd52012-06-21 01:51:02 +00002576 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2577 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2578 if (next_func_start_entry)
2579 {
2580 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2581 // Be sure the clear the Thumb address bit when we calculate the size
2582 // from the current and next address
2583 if (is_arm)
2584 next_symbol_file_addr &= 0xfffffffffffffffeull;
2585 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2586 }
2587 else
2588 {
2589 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2590 }
2591 }
2592 }
2593 symbol_value -= section_file_addr;
2594 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002595
Jason Molendab62abd52012-06-21 01:51:02 +00002596 sym[sym_idx].SetID (nlist_idx);
2597 sym[sym_idx].SetType (type);
2598 sym[sym_idx].GetAddress().SetSection (symbol_section);
2599 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2600 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002601
Jason Molendab62abd52012-06-21 01:51:02 +00002602 if (symbol_byte_size > 0)
2603 sym[sym_idx].SetByteSize(symbol_byte_size);
2604
Greg Clayton01e6a582012-11-27 01:52:16 +00002605 if (demangled_is_synthesized)
2606 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002607 ++sym_idx;
2608 }
2609 else
2610 {
2611 sym[sym_idx].Clear();
2612 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002613
Jason Molendab62abd52012-06-21 01:51:02 +00002614 }
2615 /////////////////////////////
2616 }
2617 break; // No more entries to consider
2618 }
2619 }
2620 }
2621 }
2622 }
2623 }
2624 }
2625
2626 // Must reset this in case it was mutated above!
2627 nlist_data_offset = 0;
2628#endif
2629
2630 // If the sym array was not created while parsing the DSC unmapped
2631 // symbols, create it now.
2632 if (sym == NULL)
2633 {
2634 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2635 num_syms = symtab->GetNumSymbols();
2636 }
2637
2638 if (unmapped_local_symbols_found)
2639 {
2640 assert(m_dysymtab.ilocalsym == 0);
2641 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2642 nlist_idx = m_dysymtab.nlocalsym;
2643 }
2644 else
2645 {
2646 nlist_idx = 0;
2647 }
2648
2649 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002650 {
2651 struct nlist_64 nlist;
2652 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2653 break;
2654
2655 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2656 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2657 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2658 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2659 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2660
2661 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002662 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002663
Greg Clayton3a5dc012012-05-25 17:04:00 +00002664 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002665 {
2666 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002667
Greg Claytondd29b972012-05-18 23:20:01 +00002668 if (symbol_name == NULL)
2669 {
2670 // No symbol should be NULL, even the symbols with no
2671 // string values should have an offset zero which points
2672 // to an empty C-string
2673 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00002674 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002675 nlist_idx,
2676 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00002677 module_sp->GetFileSpec().GetPath().c_str());
Greg Claytondd29b972012-05-18 23:20:01 +00002678 continue;
2679 }
2680 if (symbol_name[0] == '\0')
2681 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002682 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002683 else
2684 {
2685 const addr_t str_addr = strtab_addr + nlist.n_strx;
2686 Error str_error;
2687 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2688 symbol_name = memory_symbol_name.c_str();
2689 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002690 const char *symbol_name_non_abi_mangled = NULL;
2691
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002692 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002693 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002694 bool add_nlist = true;
2695 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002696 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002697
2698 assert (sym_idx < num_syms);
2699
2700 sym[sym_idx].SetDebug (is_debug);
2701
2702 if (is_debug)
2703 {
2704 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002705 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002706 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002707 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2708 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002709
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002710 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2711 // have the same address, but we want to ensure that we always find only the real symbol,
2712 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2713 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2714 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2715 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002716
2717 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002718 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2719 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2720 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2721 add_nlist = false;
2722 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002723 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002724 sym[sym_idx].SetExternal(true);
2725 if (nlist.n_value != 0)
2726 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2727 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002728 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002729 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002730
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002731 case StabFunctionName:
2732 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2733 type = eSymbolTypeCompiler;
2734 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002735
Jason Molenda9badb6c2013-03-06 23:19:17 +00002736 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002737 // N_FUN -- procedure: name,,n_sect,linenumber,address
2738 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002739 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002740 type = eSymbolTypeCode;
2741 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002742
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002743 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2744 // We use the current number of symbols in the symbol table in lieu of
2745 // using nlist_idx in case we ever start trimming entries out
2746 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002747 }
2748 else
2749 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002750 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002751
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002752 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002753 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002754 // Copy the size of the function into the original STAB entry so we don't have
2755 // to hunt for it later
2756 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2757 N_FUN_indexes.pop_back();
2758 // We don't really need the end function STAB as it contains the size which
2759 // we already placed with the original symbol, so don't add it if we want a
2760 // minimal symbol table
2761 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002762 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002763 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002764 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002765 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002766
Jason Molenda9badb6c2013-03-06 23:19:17 +00002767 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002768 // N_STSYM -- static symbol: name,,n_sect,type,address
2769 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2770 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2771 type = eSymbolTypeData;
2772 break;
2773
2774 case StabLocalCommon:
2775 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2776 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2777 type = eSymbolTypeCommonBlock;
2778 break;
2779
2780 case StabBeginSymbol:
2781 // N_BNSYM
2782 // We use the current number of symbols in the symbol table in lieu of
2783 // using nlist_idx in case we ever start trimming entries out
2784 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002785 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002786 // Skip these if we want minimal symbol tables
2787 add_nlist = false;
2788 }
2789 else
2790 {
2791 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2792 N_NSYM_indexes.push_back(sym_idx);
2793 type = eSymbolTypeScopeBegin;
2794 }
2795 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002796
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002797 case StabEndSymbol:
2798 // N_ENSYM
2799 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2800 // so that we can always skip the entire symbol if we need to navigate
2801 // more quickly at the source level when parsing STABS
2802 if (minimize)
2803 {
2804 // Skip these if we want minimal symbol tables
2805 add_nlist = false;
2806 }
2807 else
2808 {
2809 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002810 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002811 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2812 symbol_ptr->SetByteSize(sym_idx + 1);
2813 symbol_ptr->SetSizeIsSibling(true);
2814 N_NSYM_indexes.pop_back();
2815 }
2816 type = eSymbolTypeScopeEnd;
2817 }
2818 break;
2819
2820
2821 case StabSourceFileOptions:
2822 // N_OPT - emitted with gcc2_compiled and in gcc source
2823 type = eSymbolTypeCompiler;
2824 break;
2825
2826 case StabRegisterSymbol:
2827 // N_RSYM - register sym: name,,NO_SECT,type,register
2828 type = eSymbolTypeVariable;
2829 break;
2830
2831 case StabSourceLine:
2832 // N_SLINE - src line: 0,,n_sect,linenumber,address
2833 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2834 type = eSymbolTypeLineEntry;
2835 break;
2836
2837 case StabStructureType:
2838 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2839 type = eSymbolTypeVariableType;
2840 break;
2841
2842 case StabSourceFileName:
2843 // N_SO - source file name
2844 type = eSymbolTypeSourceFile;
2845 if (symbol_name == NULL)
2846 {
2847 if (minimize)
2848 add_nlist = false;
2849 if (N_SO_index != UINT32_MAX)
2850 {
2851 // Set the size of the N_SO to the terminating index of this N_SO
2852 // so that we can always skip the entire N_SO if we need to navigate
2853 // more quickly at the source level when parsing STABS
2854 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2855 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2856 symbol_ptr->SetSizeIsSibling(true);
2857 }
2858 N_NSYM_indexes.clear();
2859 N_INCL_indexes.clear();
2860 N_BRAC_indexes.clear();
2861 N_COMM_indexes.clear();
2862 N_FUN_indexes.clear();
2863 N_SO_index = UINT32_MAX;
2864 }
2865 else
2866 {
2867 // We use the current number of symbols in the symbol table in lieu of
2868 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002869 const bool N_SO_has_full_path = symbol_name[0] == '/';
2870 if (N_SO_has_full_path)
2871 {
2872 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2873 {
2874 // We have two consecutive N_SO entries where the first contains a directory
2875 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002876 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002877 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2878 add_nlist = false;
2879 }
2880 else
2881 {
2882 // This is the first entry in a N_SO that contains a directory or
2883 // a full path to the source file
2884 N_SO_index = sym_idx;
2885 }
2886 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002887 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2888 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002889 // This is usually the second N_SO entry that contains just the filename,
2890 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002891 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2892 if (so_path && so_path[0])
2893 {
2894 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002895 const size_t double_slash_pos = full_so_path.find("//");
2896 if (double_slash_pos != std::string::npos)
2897 {
2898 // The linker has been generating bad N_SO entries with doubled up paths
2899 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2900 // and the second is the directory for the source file so you end up with
2901 // a path that looks like "/tmp/src//tmp/src/"
2902 FileSpec so_dir(so_path, false);
2903 if (!so_dir.Exists())
2904 {
2905 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2906 if (so_dir.Exists())
2907 {
2908 // Trim off the incorrect path
2909 full_so_path.erase(0, double_slash_pos + 1);
2910 }
2911 }
2912 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002913 if (*full_so_path.rbegin() != '/')
2914 full_so_path += '/';
2915 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002916 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002917 add_nlist = false;
2918 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2919 }
2920 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002921 else
2922 {
2923 // This could be a relative path to a N_SO
2924 N_SO_index = sym_idx;
2925 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002926 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002927
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002928 break;
2929
2930 case StabObjectFileName:
2931 // N_OSO - object file name: name,,0,0,st_mtime
2932 type = eSymbolTypeObjectFile;
2933 break;
2934
2935 case StabLocalSymbol:
2936 // N_LSYM - local sym: name,,NO_SECT,type,offset
2937 type = eSymbolTypeLocal;
2938 break;
2939
2940 //----------------------------------------------------------------------
2941 // INCL scopes
2942 //----------------------------------------------------------------------
2943 case StabBeginIncludeFileName:
2944 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2945 // We use the current number of symbols in the symbol table in lieu of
2946 // using nlist_idx in case we ever start trimming entries out
2947 N_INCL_indexes.push_back(sym_idx);
2948 type = eSymbolTypeScopeBegin;
2949 break;
2950
2951 case StabEndIncludeFile:
2952 // N_EINCL - include file end: name,,NO_SECT,0,0
2953 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2954 // so that we can always skip the entire symbol if we need to navigate
2955 // more quickly at the source level when parsing STABS
2956 if ( !N_INCL_indexes.empty() )
2957 {
2958 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2959 symbol_ptr->SetByteSize(sym_idx + 1);
2960 symbol_ptr->SetSizeIsSibling(true);
2961 N_INCL_indexes.pop_back();
2962 }
2963 type = eSymbolTypeScopeEnd;
2964 break;
2965
2966 case StabIncludeFileName:
2967 // N_SOL - #included file name: name,,n_sect,0,address
2968 type = eSymbolTypeHeaderFile;
2969
2970 // We currently don't use the header files on darwin
2971 if (minimize)
2972 add_nlist = false;
2973 break;
2974
Jason Molenda9badb6c2013-03-06 23:19:17 +00002975 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002976 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2977 type = eSymbolTypeCompiler;
2978 break;
2979
2980 case StabCompilerVersion:
2981 // N_VERSION - compiler version: name,,NO_SECT,0,0
2982 type = eSymbolTypeCompiler;
2983 break;
2984
2985 case StabCompilerOptLevel:
2986 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2987 type = eSymbolTypeCompiler;
2988 break;
2989
2990 case StabParameter:
2991 // N_PSYM - parameter: name,,NO_SECT,type,offset
2992 type = eSymbolTypeVariable;
2993 break;
2994
2995 case StabAlternateEntry:
2996 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2997 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2998 type = eSymbolTypeLineEntry;
2999 break;
3000
3001 //----------------------------------------------------------------------
3002 // Left and Right Braces
3003 //----------------------------------------------------------------------
3004 case StabLeftBracket:
3005 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
3006 // We use the current number of symbols in the symbol table in lieu of
3007 // using nlist_idx in case we ever start trimming entries out
3008 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3009 N_BRAC_indexes.push_back(sym_idx);
3010 type = eSymbolTypeScopeBegin;
3011 break;
3012
3013 case StabRightBracket:
3014 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
3015 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3016 // so that we can always skip the entire symbol if we need to navigate
3017 // more quickly at the source level when parsing STABS
3018 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3019 if ( !N_BRAC_indexes.empty() )
3020 {
3021 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3022 symbol_ptr->SetByteSize(sym_idx + 1);
3023 symbol_ptr->SetSizeIsSibling(true);
3024 N_BRAC_indexes.pop_back();
3025 }
3026 type = eSymbolTypeScopeEnd;
3027 break;
3028
3029 case StabDeletedIncludeFile:
3030 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3031 type = eSymbolTypeHeaderFile;
3032 break;
3033
3034 //----------------------------------------------------------------------
3035 // COMM scopes
3036 //----------------------------------------------------------------------
3037 case StabBeginCommon:
3038 // N_BCOMM - begin common: name,,NO_SECT,0,0
3039 // We use the current number of symbols in the symbol table in lieu of
3040 // using nlist_idx in case we ever start trimming entries out
3041 type = eSymbolTypeScopeBegin;
3042 N_COMM_indexes.push_back(sym_idx);
3043 break;
3044
3045 case StabEndCommonLocal:
3046 // N_ECOML - end common (local name): 0,,n_sect,0,address
3047 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3048 // Fall through
3049
3050 case StabEndCommon:
3051 // N_ECOMM - end common: name,,n_sect,0,0
3052 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3053 // so that we can always skip the entire symbol if we need to navigate
3054 // more quickly at the source level when parsing STABS
3055 if ( !N_COMM_indexes.empty() )
3056 {
3057 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3058 symbol_ptr->SetByteSize(sym_idx + 1);
3059 symbol_ptr->SetSizeIsSibling(true);
3060 N_COMM_indexes.pop_back();
3061 }
3062 type = eSymbolTypeScopeEnd;
3063 break;
3064
3065 case StabLength:
3066 // N_LENG - second stab entry with length information
3067 type = eSymbolTypeAdditional;
3068 break;
3069
3070 default: break;
3071 }
3072 }
3073 else
3074 {
3075 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3076 uint8_t n_type = NlistMaskType & nlist.n_type;
3077 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3078
3079 switch (n_type)
3080 {
3081 case NListTypeIndirect: // N_INDR - Fall through
3082 case NListTypePreboundUndefined:// N_PBUD - Fall through
3083 case NListTypeUndefined: // N_UNDF
3084 type = eSymbolTypeUndefined;
3085 break;
3086
3087 case NListTypeAbsolute: // N_ABS
3088 type = eSymbolTypeAbsolute;
3089 break;
3090
3091 case NListTypeSection: // N_SECT
3092 {
3093 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3094
Sean Callananb386d822012-08-09 00:50:26 +00003095 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003096 {
3097 // TODO: warn about this?
3098 add_nlist = false;
3099 break;
3100 }
3101
3102 if (TEXT_eh_frame_sectID == nlist.n_sect)
3103 {
3104 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00003105 }
3106 else
3107 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003108 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3109
3110 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00003111 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003112 case SectionTypeRegular: break; // regular section
3113 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3114 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3115 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3116 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3117 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3118 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3119 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3120 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3121 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3122 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3123 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3124 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3125 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3126 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3127 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3128 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3129 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00003130 }
Chris Lattner24943d22010-06-08 16:52:24 +00003131
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003132 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00003133 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003134 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3135 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003136 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003137 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3138 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003139 SectionAttrSytemSomeInstructions))
3140 type = eSymbolTypeData;
3141 else
3142 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00003143 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003144 else
3145 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003146 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003147 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00003148 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003149 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00003150
Jason Molenda9badb6c2013-03-06 23:19:17 +00003151 if (symbol_name &&
3152 symbol_name[0] == '_' &&
3153 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003154 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00003155 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003156 llvm::StringRef symbol_name_ref(symbol_name);
3157 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3158 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3159 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3160 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00003161 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003162 symbol_name_non_abi_mangled = symbol_name + 1;
3163 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3164 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003165 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003166 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003167 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003168 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003169 symbol_name_non_abi_mangled = symbol_name + 1;
3170 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3171 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003172 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003173 }
3174 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3175 {
3176 symbol_name_non_abi_mangled = symbol_name + 1;
3177 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3178 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003179 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003180 }
3181 }
3182 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003183 else
3184 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3185 {
3186 type = eSymbolTypeException;
3187 }
3188 else
3189 {
3190 type = eSymbolTypeData;
3191 }
3192 }
3193 else
3194 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3195 {
3196 type = eSymbolTypeTrampoline;
3197 }
3198 else
3199 if (symbol_section->IsDescendant(objc_section_sp.get()))
3200 {
3201 type = eSymbolTypeRuntime;
3202 if (symbol_name && symbol_name[0] == '.')
3203 {
3204 llvm::StringRef symbol_name_ref(symbol_name);
3205 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3206 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3207 {
3208 symbol_name_non_abi_mangled = symbol_name;
3209 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3210 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003211 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003212 }
3213 }
Chris Lattner24943d22010-06-08 16:52:24 +00003214 }
3215 }
3216 }
3217 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003218 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003219 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003220 }
3221
3222 if (add_nlist)
3223 {
3224 uint64_t symbol_value = nlist.n_value;
3225 bool symbol_name_is_mangled = false;
3226
3227 if (symbol_name_non_abi_mangled)
3228 {
Greg Claytonc0240042012-07-18 23:18:10 +00003229 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3230 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003231 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003232 else
3233 {
3234 if (symbol_name && symbol_name[0] == '_')
3235 {
3236 symbol_name_is_mangled = symbol_name[1] == '_';
3237 symbol_name++; // Skip the leading underscore
3238 }
3239
3240 if (symbol_name)
3241 {
Greg Claytonc0240042012-07-18 23:18:10 +00003242 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003243 }
3244 }
3245
3246 if (is_debug == false)
3247 {
3248 if (type == eSymbolTypeCode)
3249 {
3250 // See if we can find a N_FUN entry for any code symbols.
3251 // If we do find a match, and the name matches, then we
3252 // can merge the two into just the function symbol to avoid
3253 // duplicate entries in the symbol table
3254 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3255 if (pos != N_FUN_addr_to_sym_idx.end())
3256 {
3257 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3258 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3259 {
3260 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3261 // We just need the flags from the linker symbol, so put these flags
3262 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3263 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3264 sym[sym_idx].Clear();
3265 continue;
3266 }
3267 }
3268 }
3269 else if (type == eSymbolTypeData)
3270 {
3271 // See if we can find a N_STSYM entry for any data symbols.
3272 // If we do find a match, and the name matches, then we
3273 // can merge the two into just the Static symbol to avoid
3274 // duplicate entries in the symbol table
3275 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3276 if (pos != N_STSYM_addr_to_sym_idx.end())
3277 {
3278 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3279 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3280 {
3281 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3282 // We just need the flags from the linker symbol, so put these flags
3283 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3284 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3285 sym[sym_idx].Clear();
3286 continue;
3287 }
3288 }
3289 }
3290 }
3291 if (symbol_section)
3292 {
3293 const addr_t section_file_addr = symbol_section->GetFileAddress();
3294 if (symbol_byte_size == 0 && function_starts_count > 0)
3295 {
Greg Claytond2653c22012-03-14 01:53:24 +00003296 addr_t symbol_lookup_file_addr = nlist.n_value;
3297 // Do an exact address match for non-ARM addresses, else get the closest since
3298 // the symbol might be a thumb symbol which has an address with bit zero set
3299 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3300 if (is_arm && func_start_entry)
3301 {
3302 // Verify that the function start address is the symbol address (ARM)
3303 // or the symbol address + 1 (thumb)
3304 if (func_start_entry->addr != symbol_lookup_file_addr &&
3305 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3306 {
3307 // Not the right entry, NULL it out...
3308 func_start_entry = NULL;
3309 }
3310 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003311 if (func_start_entry)
3312 {
3313 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003314
Greg Claytond2653c22012-03-14 01:53:24 +00003315 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003316 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003317 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003318
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003319 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3320 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3321 if (next_func_start_entry)
3322 {
Greg Claytond2653c22012-03-14 01:53:24 +00003323 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3324 // Be sure the clear the Thumb address bit when we calculate the size
3325 // from the current and next address
3326 if (is_arm)
3327 next_symbol_file_addr &= 0xfffffffffffffffeull;
3328 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003329 }
3330 else
3331 {
Greg Claytond2653c22012-03-14 01:53:24 +00003332 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003333 }
3334 }
3335 }
3336 symbol_value -= section_file_addr;
3337 }
3338
3339 sym[sym_idx].SetID (nlist_idx);
3340 sym[sym_idx].SetType (type);
3341 sym[sym_idx].GetAddress().SetSection (symbol_section);
3342 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3343 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3344
3345 if (symbol_byte_size > 0)
3346 sym[sym_idx].SetByteSize(symbol_byte_size);
3347
Greg Clayton01e6a582012-11-27 01:52:16 +00003348 if (demangled_is_synthesized)
3349 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3350
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003351 ++sym_idx;
3352 }
3353 else
3354 {
3355 sym[sym_idx].Clear();
3356 }
3357
3358 }
3359
3360 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3361 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3362 // such entries by figuring out what the address for the global is by looking up this non-STAB
3363 // entry and copying the value into the debug symbol's value to save us the hassle in the
3364 // debug symbol parser.
3365
3366 Symbol *global_symbol = NULL;
3367 for (nlist_idx = 0;
3368 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3369 nlist_idx++)
3370 {
3371 if (global_symbol->GetAddress().GetFileAddress() == 0)
3372 {
3373 std::vector<uint32_t> indexes;
3374 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3375 {
3376 std::vector<uint32_t>::const_iterator pos;
3377 std::vector<uint32_t>::const_iterator end = indexes.end();
3378 for (pos = indexes.begin(); pos != end; ++pos)
3379 {
3380 symbol_ptr = symtab->SymbolAtIndex(*pos);
3381 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3382 {
3383 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3384 break;
3385 }
3386 }
3387 }
Chris Lattner24943d22010-06-08 16:52:24 +00003388 }
3389 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003390
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003391 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3392
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003393 if (function_starts_count > 0)
3394 {
3395 char synthetic_function_symbol[PATH_MAX];
3396 uint32_t num_synthetic_function_symbols = 0;
3397 for (i=0; i<function_starts_count; ++i)
3398 {
3399 if (function_starts.GetEntryRef (i).data == false)
3400 ++num_synthetic_function_symbols;
3401 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003402
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003403 if (num_synthetic_function_symbols > 0)
3404 {
3405 if (num_syms < sym_idx + num_synthetic_function_symbols)
3406 {
3407 num_syms = sym_idx + num_synthetic_function_symbols;
3408 sym = symtab->Resize (num_syms);
3409 }
3410 uint32_t synthetic_function_symbol_idx = 0;
3411 for (i=0; i<function_starts_count; ++i)
3412 {
3413 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3414 if (func_start_entry->data == false)
3415 {
Greg Claytond2653c22012-03-14 01:53:24 +00003416 addr_t symbol_file_addr = func_start_entry->addr;
3417 uint32_t symbol_flags = 0;
3418 if (is_arm)
3419 {
3420 if (symbol_file_addr & 1)
3421 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3422 symbol_file_addr &= 0xfffffffffffffffeull;
3423 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003424 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003425 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003426 {
3427 SectionSP symbol_section (symbol_addr.GetSection());
3428 uint32_t symbol_byte_size = 0;
3429 if (symbol_section)
3430 {
3431 const addr_t section_file_addr = symbol_section->GetFileAddress();
3432 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3433 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3434 if (next_func_start_entry)
3435 {
Greg Claytond2653c22012-03-14 01:53:24 +00003436 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3437 if (is_arm)
3438 next_symbol_file_addr &= 0xfffffffffffffffeull;
3439 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003440 }
3441 else
3442 {
Greg Claytond2653c22012-03-14 01:53:24 +00003443 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003444 }
3445 snprintf (synthetic_function_symbol,
3446 sizeof(synthetic_function_symbol),
3447 "___lldb_unnamed_function%u$$%s",
3448 ++synthetic_function_symbol_idx,
3449 module_sp->GetFileSpec().GetFilename().GetCString());
3450 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003451 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003452 sym[sym_idx].SetType (eSymbolTypeCode);
3453 sym[sym_idx].SetIsSynthetic (true);
3454 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003455 if (symbol_flags)
3456 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003457 if (symbol_byte_size)
3458 sym[sym_idx].SetByteSize (symbol_byte_size);
3459 ++sym_idx;
3460 }
3461 }
3462 }
3463 }
3464 }
3465 }
3466
3467 // Trim our symbols down to just what we ended up with after
3468 // removing any symbols.
3469 if (sym_idx < num_syms)
3470 {
3471 num_syms = sym_idx;
3472 sym = symtab->Resize (num_syms);
3473 }
3474
3475 // Now synthesize indirect symbols
3476 if (m_dysymtab.nindirectsyms != 0)
3477 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003478 if (indirect_symbol_index_data.GetByteSize())
3479 {
3480 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3481
3482 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3483 {
3484 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3485 {
3486 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3487 if (symbol_stub_byte_size == 0)
3488 continue;
3489
3490 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3491
3492 if (num_symbol_stubs == 0)
3493 continue;
3494
3495 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3496 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3497 {
3498 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3499 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
Greg Clayton36da2aa2013-01-25 18:06:21 +00003500 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003501 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3502 {
3503 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3504 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3505 continue;
3506
3507 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3508 Symbol *stub_symbol = NULL;
3509 if (index_pos != end_index_pos)
3510 {
3511 // We have a remapping from the original nlist index to
3512 // a current symbol index, so just look this up by index
3513 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3514 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003515 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003516 {
3517 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003518 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003519 // S_SYMBOL_STUBS
3520 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3521 }
3522
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003523 if (stub_symbol)
3524 {
3525 Address so_addr(symbol_stub_addr, section_list);
3526
3527 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3528 {
3529 // Change the external symbol into a trampoline that makes sense
3530 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3531 // can re-use them so we don't have to make up a synthetic symbol
3532 // for no good reason.
3533 stub_symbol->SetType (eSymbolTypeTrampoline);
3534 stub_symbol->SetExternal (false);
3535 stub_symbol->GetAddress() = so_addr;
3536 stub_symbol->SetByteSize (symbol_stub_byte_size);
3537 }
3538 else
3539 {
3540 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003541 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003542 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003543 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003544 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003545 stub_symbol = NULL; // this pointer no longer valid
3546 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003547 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003548 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003549 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3550 sym[sym_idx].SetIsSynthetic (true);
3551 sym[sym_idx].GetAddress() = so_addr;
3552 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3553 ++sym_idx;
3554 }
3555 }
Greg Claytond4330e62012-09-05 01:38:55 +00003556 else
3557 {
3558 if (log)
3559 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3560 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003561 }
3562 }
3563 }
3564 }
3565 }
3566 }
3567 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003568 }
3569 return 0;
3570}
3571
3572
3573void
3574ObjectFileMachO::Dump (Stream *s)
3575{
Greg Clayton9482f052012-03-13 23:14:29 +00003576 ModuleSP module_sp(GetModule());
3577 if (module_sp)
3578 {
3579 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3580 s->Printf("%p: ", this);
3581 s->Indent();
3582 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3583 s->PutCString("ObjectFileMachO64");
3584 else
3585 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003586
Greg Clayton9482f052012-03-13 23:14:29 +00003587 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003588
Greg Clayton9482f052012-03-13 23:14:29 +00003589 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003590
Greg Clayton9482f052012-03-13 23:14:29 +00003591 if (m_sections_ap.get())
3592 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003593
Greg Clayton9482f052012-03-13 23:14:29 +00003594 if (m_symtab_ap.get())
3595 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3596 }
Chris Lattner24943d22010-06-08 16:52:24 +00003597}
3598
Greg Clayton36b877d2013-04-24 22:29:28 +00003599bool
3600ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3601 const lldb_private::DataExtractor &data,
3602 lldb::offset_t lc_offset,
3603 lldb_private::UUID& uuid)
3604{
3605 uint32_t i;
3606 struct uuid_command load_cmd;
3607
3608 lldb::offset_t offset = lc_offset;
3609 for (i=0; i<header.ncmds; ++i)
3610 {
3611 const lldb::offset_t cmd_offset = offset;
3612 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3613 break;
3614
3615 if (load_cmd.cmd == LoadCommandUUID)
3616 {
3617 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3618
3619 if (uuid_bytes)
3620 {
3621 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3622 // We pretend these object files have no UUID to prevent crashing.
3623
3624 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3625 0x3b, 0xa8,
3626 0x4b, 0x16,
3627 0xb6, 0xa4,
3628 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3629
3630 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3631 return false;
3632
3633 uuid.SetBytes (uuid_bytes);
3634 return true;
3635 }
3636 return false;
3637 }
3638 offset = cmd_offset + load_cmd.cmdsize;
3639 }
3640 return false;
3641}
Chris Lattner24943d22010-06-08 16:52:24 +00003642
3643bool
Greg Clayton0467c782011-02-04 18:53:10 +00003644ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003645{
Greg Clayton9482f052012-03-13 23:14:29 +00003646 ModuleSP module_sp(GetModule());
3647 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003648 {
Greg Clayton9482f052012-03-13 23:14:29 +00003649 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00003650 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton36b877d2013-04-24 22:29:28 +00003651 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner24943d22010-06-08 16:52:24 +00003652 }
3653 return false;
3654}
3655
3656
3657uint32_t
3658ObjectFileMachO::GetDependentModules (FileSpecList& files)
3659{
Chris Lattner24943d22010-06-08 16:52:24 +00003660 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003661 ModuleSP module_sp(GetModule());
3662 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003663 {
Greg Clayton9482f052012-03-13 23:14:29 +00003664 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3665 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003666 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003667 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3668 uint32_t i;
3669 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003670 {
Greg Clayton9482f052012-03-13 23:14:29 +00003671 const uint32_t cmd_offset = offset;
3672 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3673 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003674
Greg Clayton9482f052012-03-13 23:14:29 +00003675 switch (load_cmd.cmd)
3676 {
3677 case LoadCommandDylibLoad:
3678 case LoadCommandDylibLoadWeak:
3679 case LoadCommandDylibReexport:
3680 case LoadCommandDynamicLinkerLoad:
3681 case LoadCommandFixedVMShlibLoad:
3682 case LoadCommandDylibLoadUpward:
3683 {
3684 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3685 const char *path = m_data.PeekCStr(name_offset);
3686 // Skip any path that starts with '@' since these are usually:
3687 // @executable_path/.../file
3688 // @rpath/.../file
3689 if (path && path[0] != '@')
3690 {
3691 FileSpec file_spec(path, resolve_path);
3692 if (files.AppendIfUnique(file_spec))
3693 count++;
3694 }
3695 }
3696 break;
3697
3698 default:
3699 break;
3700 }
3701 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003702 }
Chris Lattner24943d22010-06-08 16:52:24 +00003703 }
3704 return count;
3705}
3706
Jim Ingham28775942011-03-07 23:44:08 +00003707lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003708ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003709{
3710 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3711 // is initialized to an invalid address, so we can just return that.
3712 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003713
Jim Ingham28775942011-03-07 23:44:08 +00003714 if (!IsExecutable() || m_entry_point_address.IsValid())
3715 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003716
3717 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003718 // /usr/include/mach-o.h, but it is basically:
3719 //
3720 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3721 // uint32_t count - this is the count of longs in the thread state data
3722 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3723 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003724 //
Jim Ingham28775942011-03-07 23:44:08 +00003725 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3726 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3727 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3728 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3729 //
3730 // For now we hard-code the offsets and flavors we need:
3731 //
3732 //
3733
Greg Clayton9482f052012-03-13 23:14:29 +00003734 ModuleSP module_sp(GetModule());
3735 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003736 {
Greg Clayton9482f052012-03-13 23:14:29 +00003737 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3738 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003739 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003740 uint32_t i;
3741 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3742 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003743
Greg Clayton9482f052012-03-13 23:14:29 +00003744 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003745 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003746 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003747 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3748 break;
3749
3750 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003751 {
Greg Clayton9482f052012-03-13 23:14:29 +00003752 case LoadCommandUnixThread:
3753 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003754 {
Greg Clayton9482f052012-03-13 23:14:29 +00003755 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003756 {
Greg Clayton9482f052012-03-13 23:14:29 +00003757 uint32_t flavor = m_data.GetU32(&offset);
3758 uint32_t count = m_data.GetU32(&offset);
3759 if (count == 0)
3760 {
3761 // We've gotten off somehow, log and exit;
3762 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003763 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003764
Greg Clayton9482f052012-03-13 23:14:29 +00003765 switch (m_header.cputype)
3766 {
3767 case llvm::MachO::CPUTypeARM:
3768 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3769 {
3770 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3771 start_address = m_data.GetU32(&offset);
3772 done = true;
3773 }
Jim Ingham28775942011-03-07 23:44:08 +00003774 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003775 case llvm::MachO::CPUTypeI386:
3776 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3777 {
3778 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3779 start_address = m_data.GetU32(&offset);
3780 done = true;
3781 }
3782 break;
3783 case llvm::MachO::CPUTypeX86_64:
3784 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3785 {
3786 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3787 start_address = m_data.GetU64(&offset);
3788 done = true;
3789 }
3790 break;
3791 default:
3792 return m_entry_point_address;
3793 }
3794 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3795 if (done)
3796 break;
3797 offset += count * 4;
3798 }
Jim Ingham28775942011-03-07 23:44:08 +00003799 }
Greg Clayton9482f052012-03-13 23:14:29 +00003800 break;
3801 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003802 {
Greg Clayton9482f052012-03-13 23:14:29 +00003803 ConstString text_segment_name ("__TEXT");
3804 uint64_t entryoffset = m_data.GetU64(&offset);
3805 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3806 if (text_segment_sp)
3807 {
3808 done = true;
3809 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3810 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003811 }
Greg Clayton9482f052012-03-13 23:14:29 +00003812
3813 default:
3814 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003815 }
Greg Clayton9482f052012-03-13 23:14:29 +00003816 if (done)
3817 break;
Jim Ingham28775942011-03-07 23:44:08 +00003818
Greg Clayton9482f052012-03-13 23:14:29 +00003819 // Go to the next load command:
3820 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003821 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003822
Greg Clayton9482f052012-03-13 23:14:29 +00003823 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003824 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003825 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003826 // of this ObjectFile:
3827 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003828 {
Greg Clayton9482f052012-03-13 23:14:29 +00003829 m_entry_point_address.Clear();
3830 }
3831 }
3832 else
3833 {
3834 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3835 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003836
Greg Clayton9482f052012-03-13 23:14:29 +00003837 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003838
Greg Clayton9482f052012-03-13 23:14:29 +00003839 if (module_sp)
3840 {
3841 SymbolContextList contexts;
3842 SymbolContext context;
3843 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3844 {
3845 if (contexts.GetContextAtIndex(0, context))
3846 m_entry_point_address = context.symbol->GetAddress();
3847 }
Greg Clayton3508c382012-02-24 01:59:29 +00003848 }
3849 }
Jim Ingham28775942011-03-07 23:44:08 +00003850 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003851
Jim Ingham28775942011-03-07 23:44:08 +00003852 return m_entry_point_address;
3853
3854}
3855
Greg Claytonb5a8f142012-02-05 02:38:54 +00003856lldb_private::Address
3857ObjectFileMachO::GetHeaderAddress ()
3858{
3859 lldb_private::Address header_addr;
3860 SectionList *section_list = GetSectionList();
3861 if (section_list)
3862 {
3863 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3864 if (text_segment_sp)
3865 {
Greg Clayton3508c382012-02-24 01:59:29 +00003866 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003867 header_addr.SetOffset (0);
3868 }
3869 }
3870 return header_addr;
3871}
3872
Greg Clayton46c9a352012-02-09 06:16:32 +00003873uint32_t
3874ObjectFileMachO::GetNumThreadContexts ()
3875{
Greg Clayton9482f052012-03-13 23:14:29 +00003876 ModuleSP module_sp(GetModule());
3877 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003878 {
Greg Clayton9482f052012-03-13 23:14:29 +00003879 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3880 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003881 {
Greg Clayton9482f052012-03-13 23:14:29 +00003882 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003883 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003884 FileRangeArray::Entry file_range;
3885 thread_command thread_cmd;
3886 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003887 {
Greg Clayton9482f052012-03-13 23:14:29 +00003888 const uint32_t cmd_offset = offset;
3889 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3890 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003891
Greg Clayton9482f052012-03-13 23:14:29 +00003892 if (thread_cmd.cmd == LoadCommandThread)
3893 {
3894 file_range.SetRangeBase (offset);
3895 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3896 m_thread_context_offsets.Append (file_range);
3897 }
3898 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003899 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003900 }
3901 }
3902 return m_thread_context_offsets.GetSize();
3903}
3904
3905lldb::RegisterContextSP
3906ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3907{
Greg Clayton46c9a352012-02-09 06:16:32 +00003908 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003909
Greg Clayton9482f052012-03-13 23:14:29 +00003910 ModuleSP module_sp(GetModule());
3911 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003912 {
Greg Clayton9482f052012-03-13 23:14:29 +00003913 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3914 if (!m_thread_context_offsets_valid)
3915 GetNumThreadContexts ();
3916
3917 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003918 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003919 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003920
3921 DataExtractor data (m_data,
3922 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003923 thread_context_file_range->GetByteSize());
3924
3925 switch (m_header.cputype)
3926 {
3927 case llvm::MachO::CPUTypeARM:
3928 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3929 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003930
Jim Ingham6f01c932012-10-12 17:34:26 +00003931 case llvm::MachO::CPUTypeI386:
3932 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3933 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003934
Jim Ingham6f01c932012-10-12 17:34:26 +00003935 case llvm::MachO::CPUTypeX86_64:
3936 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3937 break;
3938 }
Greg Clayton9482f052012-03-13 23:14:29 +00003939 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003940 }
3941 return reg_ctx_sp;
3942}
3943
Greg Claytonb5a8f142012-02-05 02:38:54 +00003944
Greg Claytonca319972011-07-09 00:41:34 +00003945ObjectFile::Type
3946ObjectFileMachO::CalculateType()
3947{
3948 switch (m_header.filetype)
3949 {
3950 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3951 if (GetAddressByteSize () == 4)
3952 {
3953 // 32 bit kexts are just object files, but they do have a valid
3954 // UUID load command.
3955 UUID uuid;
3956 if (GetUUID(&uuid))
3957 {
3958 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003959 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003960 // "OSKextGetCurrentIdentifier" as this is required of kexts
3961 if (m_strata == eStrataInvalid)
3962 m_strata = eStrataKernel;
3963 return eTypeSharedLibrary;
3964 }
3965 }
3966 return eTypeObjectFile;
3967
3968 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3969 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3970 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3971 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3972 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3973 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3974 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3975 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3976 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3977 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3978 default:
3979 break;
3980 }
3981 return eTypeUnknown;
3982}
3983
3984ObjectFile::Strata
3985ObjectFileMachO::CalculateStrata()
3986{
3987 switch (m_header.filetype)
3988 {
3989 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3990 {
3991 // 32 bit kexts are just object files, but they do have a valid
3992 // UUID load command.
3993 UUID uuid;
3994 if (GetUUID(&uuid))
3995 {
3996 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003997 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003998 // "OSKextGetCurrentIdentifier" as this is required of kexts
3999 if (m_type == eTypeInvalid)
4000 m_type = eTypeSharedLibrary;
4001
4002 return eStrataKernel;
4003 }
4004 }
4005 return eStrataUnknown;
4006
4007 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
4008 // Check for the MH_DYLDLINK bit in the flags
4009 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00004010 {
Greg Claytonca319972011-07-09 00:41:34 +00004011 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00004012 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004013 else
Sean Callananac725af2012-02-10 20:22:35 +00004014 {
4015 SectionList *section_list = GetSectionList();
4016 if (section_list)
4017 {
4018 static ConstString g_kld_section_name ("__KLD");
4019 if (section_list->FindSectionByName(g_kld_section_name))
4020 return eStrataKernel;
4021 }
4022 }
4023 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00004024
4025 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4026 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00004027 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00004028 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4029 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4030 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4031 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4032 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4033 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4034 default:
4035 break;
4036 }
4037 return eStrataUnknown;
4038}
4039
4040
Greg Clayton49f4bf22012-02-22 19:41:02 +00004041uint32_t
4042ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4043{
Greg Clayton9482f052012-03-13 23:14:29 +00004044 ModuleSP module_sp(GetModule());
4045 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004046 {
Greg Clayton9482f052012-03-13 23:14:29 +00004047 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4048 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00004049 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00004050 uint32_t version_cmd = 0;
4051 uint64_t version = 0;
4052 uint32_t i;
4053 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004054 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00004055 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00004056 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4057 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00004058
Greg Clayton9482f052012-03-13 23:14:29 +00004059 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004060 {
Greg Clayton9482f052012-03-13 23:14:29 +00004061 if (version_cmd == 0)
4062 {
4063 version_cmd = load_cmd.cmd;
4064 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4065 break;
4066 version = load_cmd.dylib.current_version;
4067 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004068 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00004069 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00004070 }
Greg Clayton9482f052012-03-13 23:14:29 +00004071 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004072 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004073
Greg Clayton9482f052012-03-13 23:14:29 +00004074 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004075 {
Greg Clayton9482f052012-03-13 23:14:29 +00004076 if (versions != NULL && num_versions > 0)
4077 {
4078 if (num_versions > 0)
4079 versions[0] = (version & 0xFFFF0000ull) >> 16;
4080 if (num_versions > 1)
4081 versions[1] = (version & 0x0000FF00ull) >> 8;
4082 if (num_versions > 2)
4083 versions[2] = (version & 0x000000FFull);
4084 // Fill in an remaining version numbers with invalid values
4085 for (i=3; i<num_versions; ++i)
4086 versions[i] = UINT32_MAX;
4087 }
4088 // The LC_ID_DYLIB load command has a version with 3 version numbers
4089 // in it, so always return 3
4090 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004091 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00004092 }
4093 return false;
4094}
4095
Chris Lattner24943d22010-06-08 16:52:24 +00004096bool
Greg Clayton395fc332011-02-15 21:59:32 +00004097ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00004098{
Greg Clayton9482f052012-03-13 23:14:29 +00004099 ModuleSP module_sp(GetModule());
4100 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004101 {
Greg Clayton9482f052012-03-13 23:14:29 +00004102 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4103 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00004104
Greg Clayton9482f052012-03-13 23:14:29 +00004105 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00004106 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00004107 // unknown to make sure we use the DynamicLoaderStatic()...
4108 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4109 {
4110 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4111 }
4112 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004113 }
Greg Clayton9482f052012-03-13 23:14:29 +00004114 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00004115}
4116
4117
Jason Molenda45c75502013-04-16 06:24:42 +00004118UUID
4119ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4120{
4121 UUID uuid;
4122 if (process)
4123 {
4124 addr_t all_image_infos = process->GetImageInfoAddress();
4125
4126 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4127 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4128 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4129 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4130
4131 Error err;
4132 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4133 if (version_or_magic != -1
4134 && version_or_magic != HeaderMagic32
4135 && version_or_magic != HeaderMagic32Swapped
4136 && version_or_magic != HeaderMagic64
4137 && version_or_magic != HeaderMagic64Swapped
4138 && version_or_magic >= 13)
4139 {
4140 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4141 int wordsize = process->GetAddressByteSize();
4142 if (wordsize == 8)
4143 {
4144 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4145 }
4146 if (wordsize == 4)
4147 {
4148 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4149 }
4150 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4151 {
4152 uuid_t shared_cache_uuid;
4153 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4154 {
4155 uuid.SetBytes (shared_cache_uuid);
4156 }
4157 }
4158 }
4159 }
4160 return uuid;
4161}
4162
4163UUID
4164ObjectFileMachO::GetLLDBSharedCacheUUID ()
4165{
4166 UUID uuid;
4167#if defined (__APPLE__) && defined (__arm__)
4168 uint8_t *(*dyld_get_all_image_infos)(void);
4169 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4170 if (dyld_get_all_image_infos)
4171 {
4172 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4173 if (dyld_all_image_infos_address)
4174 {
Jason Molenda6ff916f2013-04-16 21:42:58 +00004175 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4176 if (*version >= 13)
Jason Molenda45c75502013-04-16 06:24:42 +00004177 {
Jason Molenda2ceae992013-04-16 22:56:17 +00004178 uuid_t *sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84); // sharedCacheUUID <mach-o/dyld_images.h>
Jason Molenda45c75502013-04-16 06:24:42 +00004179 uuid.SetBytes (sharedCacheUUID_address);
4180 }
4181 }
4182 }
4183#endif
4184 return uuid;
4185}
4186
4187
Chris Lattner24943d22010-06-08 16:52:24 +00004188//------------------------------------------------------------------
4189// PluginInterface protocol
4190//------------------------------------------------------------------
Greg Clayton0e191602013-05-10 21:47:16 +00004191lldb_private::ConstString
Chris Lattner24943d22010-06-08 16:52:24 +00004192ObjectFileMachO::GetPluginName()
4193{
Chris Lattner24943d22010-06-08 16:52:24 +00004194 return GetPluginNameStatic();
4195}
4196
4197uint32_t
4198ObjectFileMachO::GetPluginVersion()
4199{
4200 return 1;
4201}
4202