blob: b94ebca297d86f4aa19cb12c9885c0b7204ccc11 [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);
292 int flavor = data.GetU32 (&offset);
293 uint32_t count = data.GetU32 (&offset);
294 switch (flavor)
295 {
296 case GPRRegSet:
297 for (uint32_t i=0; i<count; ++i)
298 gpr.r[i] = data.GetU32(&offset);
299 SetError (GPRRegSet, Read, 0);
300 break;
301 case FPURegSet:
302 // TODO: fill in FPU regs....
303 //SetError (FPURegSet, Read, -1);
304 break;
305 case EXCRegSet:
306 exc.exception = data.GetU32(&offset);
307 exc.fsr = data.GetU32(&offset);
308 exc.far = data.GetU32(&offset);
309 SetError (EXCRegSet, Read, 0);
310 break;
311 }
312 }
313protected:
314 virtual int
315 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
316 {
317 return 0;
318 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000319
Greg Clayton9ce95382012-02-13 23:10:39 +0000320 virtual int
321 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
322 {
323 return 0;
324 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000325
Greg Clayton9ce95382012-02-13 23:10:39 +0000326 virtual int
327 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
328 {
329 return 0;
330 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000331
332 virtual int
333 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
334 {
335 return -1;
336 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000337
Greg Clayton9ce95382012-02-13 23:10:39 +0000338 virtual int
339 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
340 {
341 return 0;
342 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000343
Greg Clayton9ce95382012-02-13 23:10:39 +0000344 virtual int
345 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
346 {
347 return 0;
348 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000349
Greg Clayton9ce95382012-02-13 23:10:39 +0000350 virtual int
351 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
352 {
353 return 0;
354 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000355
Greg Claytonb5431d02012-10-30 23:57:32 +0000356 virtual int
357 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
358 {
359 return -1;
360 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000361};
362
Greg Claytonb1888f22011-03-19 01:12:21 +0000363#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000364
365void
366ObjectFileMachO::Initialize()
367{
368 PluginManager::RegisterPlugin (GetPluginNameStatic(),
369 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000370 CreateInstance,
Greg Clayton36b877d2013-04-24 22:29:28 +0000371 CreateMemoryInstance,
372 GetModuleSpecifications);
Chris Lattner24943d22010-06-08 16:52:24 +0000373}
374
375void
376ObjectFileMachO::Terminate()
377{
378 PluginManager::UnregisterPlugin (CreateInstance);
379}
380
381
382const char *
383ObjectFileMachO::GetPluginNameStatic()
384{
385 return "object-file.mach-o";
386}
387
388const char *
389ObjectFileMachO::GetPluginDescriptionStatic()
390{
391 return "Mach-o object file reader (32 and 64 bit)";
392}
393
Chris Lattner24943d22010-06-08 16:52:24 +0000394ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000395ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
396 DataBufferSP& data_sp,
397 lldb::offset_t data_offset,
398 const FileSpec* file,
399 lldb::offset_t file_offset,
400 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000401{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000402 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000403 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000404 data_sp = file->MemoryMapFileContents(file_offset, length);
405 data_offset = 0;
406 }
407
408 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
409 {
410 // Update the data to contain the entire file if it doesn't already
411 if (data_sp->GetByteSize() < length)
412 {
413 data_sp = file->MemoryMapFileContents(file_offset, length);
414 data_offset = 0;
415 }
Greg Clayton102b2c22013-04-18 22:45:39 +0000416 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 +0000417 if (objfile_ap.get() && objfile_ap->ParseHeader())
418 return objfile_ap.release();
419 }
420 return NULL;
421}
422
Greg Claytonb5a8f142012-02-05 02:38:54 +0000423ObjectFile *
Jason Molenda9badb6c2013-03-06 23:19:17 +0000424ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
425 DataBufferSP& data_sp,
426 const ProcessSP &process_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000427 lldb::addr_t header_addr)
428{
429 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
430 {
Greg Clayton102b2c22013-04-18 22:45:39 +0000431 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000432 if (objfile_ap.get() && objfile_ap->ParseHeader())
433 return objfile_ap.release();
434 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000435 return NULL;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000436}
437
Greg Clayton36b877d2013-04-24 22:29:28 +0000438size_t
439ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
440 lldb::DataBufferSP& data_sp,
441 lldb::offset_t data_offset,
442 lldb::offset_t file_offset,
443 lldb::offset_t length,
444 lldb_private::ModuleSpecList &specs)
445{
446 const size_t initial_count = specs.GetSize();
447
448 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
449 {
450 DataExtractor data;
451 data.SetData(data_sp);
452 llvm::MachO::mach_header header;
453 if (ParseHeader (data, &data_offset, header))
454 {
455 if (header.sizeofcmds >= data_sp->GetByteSize())
456 {
457 data_sp = file.ReadFileContents(file_offset, header.sizeofcmds);
458 data_offset = 0;
459 }
460 if (data_sp)
461 {
462 ModuleSpec spec;
463 spec.GetFileSpec() = file;
464 spec.GetArchitecture().SetArchitecture(eArchTypeMachO,
465 header.cputype,
466 header.cpusubtype);
467 if (spec.GetArchitecture().IsValid())
468 {
469 GetUUID (header, data, data_offset, spec.GetUUID());
470 specs.Append(spec);
471 }
472 }
473 }
474 }
475 return specs.GetSize() - initial_count;
476}
477
478
Greg Claytonb5a8f142012-02-05 02:38:54 +0000479
480const ConstString &
481ObjectFileMachO::GetSegmentNameTEXT()
482{
483 static ConstString g_segment_name_TEXT ("__TEXT");
484 return g_segment_name_TEXT;
485}
486
487const ConstString &
488ObjectFileMachO::GetSegmentNameDATA()
489{
490 static ConstString g_segment_name_DATA ("__DATA");
491 return g_segment_name_DATA;
492}
493
494const ConstString &
495ObjectFileMachO::GetSegmentNameOBJC()
496{
497 static ConstString g_segment_name_OBJC ("__OBJC");
498 return g_segment_name_OBJC;
499}
500
501const ConstString &
502ObjectFileMachO::GetSegmentNameLINKEDIT()
503{
504 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
505 return g_section_name_LINKEDIT;
506}
507
508const ConstString &
509ObjectFileMachO::GetSectionNameEHFrame()
510{
511 static ConstString g_section_name_eh_frame ("__eh_frame");
512 return g_section_name_eh_frame;
513}
514
515
Chris Lattner24943d22010-06-08 16:52:24 +0000516
517static uint32_t
518MachHeaderSizeFromMagic(uint32_t magic)
519{
520 switch (magic)
521 {
Greg Clayton1674b122010-07-21 22:12:05 +0000522 case HeaderMagic32:
523 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000524 return sizeof(struct mach_header);
525
Greg Clayton1674b122010-07-21 22:12:05 +0000526 case HeaderMagic64:
527 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000528 return sizeof(struct mach_header_64);
529 break;
530
531 default:
532 break;
533 }
534 return 0;
535}
536
537
538bool
Jason Molenda9badb6c2013-03-06 23:19:17 +0000539ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
540 lldb::addr_t data_offset,
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000541 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000542{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000543 DataExtractor data;
544 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000545 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000546 uint32_t magic = data.GetU32(&offset);
547 return MachHeaderSizeFromMagic(magic) != 0;
548}
549
550
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000551ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
552 DataBufferSP& data_sp,
553 lldb::offset_t data_offset,
554 const FileSpec* file,
555 lldb::offset_t file_offset,
556 lldb::offset_t length) :
557 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Clayton46c9a352012-02-09 06:16:32 +0000558 m_mach_segments(),
559 m_mach_sections(),
560 m_entry_point_address(),
561 m_thread_context_offsets(),
562 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000563{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000564 ::memset (&m_header, 0, sizeof(m_header));
565 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000566}
567
Greg Clayton3508c382012-02-24 01:59:29 +0000568ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000569 lldb::DataBufferSP& header_data_sp,
570 const lldb::ProcessSP &process_sp,
571 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000572 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Clayton46c9a352012-02-09 06:16:32 +0000573 m_mach_segments(),
574 m_mach_sections(),
575 m_entry_point_address(),
576 m_thread_context_offsets(),
577 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000578{
579 ::memset (&m_header, 0, sizeof(m_header));
580 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
581}
Chris Lattner24943d22010-06-08 16:52:24 +0000582
583ObjectFileMachO::~ObjectFileMachO()
584{
585}
586
Greg Clayton36b877d2013-04-24 22:29:28 +0000587bool
588ObjectFileMachO::ParseHeader (DataExtractor &data,
589 lldb::offset_t *data_offset_ptr,
590 llvm::MachO::mach_header &header)
591{
592 data.SetByteOrder (lldb::endian::InlHostByteOrder());
593 // Leave magic in the original byte order
594 header.magic = data.GetU32(data_offset_ptr);
595 bool can_parse = false;
596 bool is_64_bit = false;
597 switch (header.magic)
598 {
599 case HeaderMagic32:
600 data.SetByteOrder (lldb::endian::InlHostByteOrder());
601 data.SetAddressByteSize(4);
602 can_parse = true;
603 break;
604
605 case HeaderMagic64:
606 data.SetByteOrder (lldb::endian::InlHostByteOrder());
607 data.SetAddressByteSize(8);
608 can_parse = true;
609 is_64_bit = true;
610 break;
611
612 case HeaderMagic32Swapped:
613 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
614 data.SetAddressByteSize(4);
615 can_parse = true;
616 break;
617
618 case HeaderMagic64Swapped:
619 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
620 data.SetAddressByteSize(8);
621 is_64_bit = true;
622 can_parse = true;
623 break;
624
625 default:
626 break;
627 }
628
629 if (can_parse)
630 {
631 data.GetU32(data_offset_ptr, &header.cputype, 6);
632 if (is_64_bit)
633 *data_offset_ptr += 4;
634 return true;
635 }
636 else
637 {
638 memset(&header, 0, sizeof(header));
639 }
640 return false;
641}
Chris Lattner24943d22010-06-08 16:52:24 +0000642
643bool
644ObjectFileMachO::ParseHeader ()
645{
Greg Clayton9482f052012-03-13 23:14:29 +0000646 ModuleSP module_sp(GetModule());
647 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000648 {
Greg Clayton9482f052012-03-13 23:14:29 +0000649 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
650 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000651 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000652 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000653 // Leave magic in the original byte order
654 m_header.magic = m_data.GetU32(&offset);
655 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000656 {
Greg Clayton9482f052012-03-13 23:14:29 +0000657 case HeaderMagic32:
658 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
659 m_data.SetAddressByteSize(4);
660 can_parse = true;
661 break;
662
663 case HeaderMagic64:
664 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
665 m_data.SetAddressByteSize(8);
666 can_parse = true;
667 break;
668
669 case HeaderMagic32Swapped:
670 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
671 m_data.SetAddressByteSize(4);
672 can_parse = true;
673 break;
674
675 case HeaderMagic64Swapped:
676 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
677 m_data.SetAddressByteSize(8);
678 can_parse = true;
679 break;
680
681 default:
682 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000683 }
Greg Clayton9482f052012-03-13 23:14:29 +0000684
685 if (can_parse)
686 {
687 m_data.GetU32(&offset, &m_header.cputype, 6);
688
689 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +0000690
Greg Clayton21a25432012-11-16 21:36:10 +0000691 // Check if the module has a required architecture
692 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000693 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000694 return false;
695
Greg Clayton9482f052012-03-13 23:14:29 +0000696 if (SetModulesArchitecture (mach_arch))
697 {
698 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
699 if (m_data.GetByteSize() < header_and_lc_size)
700 {
701 DataBufferSP data_sp;
702 ProcessSP process_sp (m_process_wp.lock());
703 if (process_sp)
704 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000705 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000706 }
707 else
708 {
709 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000710 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000711 if (data_sp->GetByteSize() != header_and_lc_size)
712 return false;
713 }
714 if (data_sp)
715 m_data.SetData (data_sp);
716 }
717 }
718 return true;
719 }
720 else
721 {
722 memset(&m_header, 0, sizeof(struct mach_header));
723 }
Chris Lattner24943d22010-06-08 16:52:24 +0000724 }
725 return false;
726}
727
728
729ByteOrder
730ObjectFileMachO::GetByteOrder () const
731{
Chris Lattner24943d22010-06-08 16:52:24 +0000732 return m_data.GetByteOrder ();
733}
734
Jim Ingham7508e732010-08-09 23:31:02 +0000735bool
736ObjectFileMachO::IsExecutable() const
737{
738 return m_header.filetype == HeaderFileTypeExecutable;
739}
Chris Lattner24943d22010-06-08 16:52:24 +0000740
Greg Clayton36da2aa2013-01-25 18:06:21 +0000741uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000742ObjectFileMachO::GetAddressByteSize () const
743{
Chris Lattner24943d22010-06-08 16:52:24 +0000744 return m_data.GetAddressByteSize ();
745}
746
Greg Claytonb3448432011-03-24 21:19:54 +0000747AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000748ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
749{
750 Symtab *symtab = GetSymtab();
751 if (symtab)
752 {
753 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
754 if (symbol)
755 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000756 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000757 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000758 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000759 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000760 {
Greg Clayton3508c382012-02-24 01:59:29 +0000761 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000762 switch (section_type)
763 {
764 case eSectionTypeInvalid: return eAddressClassUnknown;
765 case eSectionTypeCode:
766 if (m_header.cputype == llvm::MachO::CPUTypeARM)
767 {
768 // For ARM we have a bit in the n_desc field of the symbol
769 // that tells us ARM/Thumb which is bit 0x0008.
770 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
771 return eAddressClassCodeAlternateISA;
772 }
773 return eAddressClassCode;
774
775 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000776 case eSectionTypeData:
777 case eSectionTypeDataCString:
778 case eSectionTypeDataCStringPointers:
779 case eSectionTypeDataSymbolAddress:
780 case eSectionTypeData4:
781 case eSectionTypeData8:
782 case eSectionTypeData16:
783 case eSectionTypeDataPointers:
784 case eSectionTypeZeroFill:
785 case eSectionTypeDataObjCMessageRefs:
786 case eSectionTypeDataObjCCFStrings:
787 return eAddressClassData;
788 case eSectionTypeDebug:
789 case eSectionTypeDWARFDebugAbbrev:
790 case eSectionTypeDWARFDebugAranges:
791 case eSectionTypeDWARFDebugFrame:
792 case eSectionTypeDWARFDebugInfo:
793 case eSectionTypeDWARFDebugLine:
794 case eSectionTypeDWARFDebugLoc:
795 case eSectionTypeDWARFDebugMacInfo:
796 case eSectionTypeDWARFDebugPubNames:
797 case eSectionTypeDWARFDebugPubTypes:
798 case eSectionTypeDWARFDebugRanges:
799 case eSectionTypeDWARFDebugStr:
800 case eSectionTypeDWARFAppleNames:
801 case eSectionTypeDWARFAppleTypes:
802 case eSectionTypeDWARFAppleNamespaces:
803 case eSectionTypeDWARFAppleObjC:
804 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000805 case eSectionTypeEHFrame: return eAddressClassRuntime;
806 case eSectionTypeOther: return eAddressClassUnknown;
807 }
808 }
809 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000810
Greg Claytonb3448432011-03-24 21:19:54 +0000811 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000812 switch (symbol_type)
813 {
814 case eSymbolTypeAny: return eAddressClassUnknown;
815 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000816
Greg Claytonb1888f22011-03-19 01:12:21 +0000817 case eSymbolTypeCode:
818 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000819 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000820 if (m_header.cputype == llvm::MachO::CPUTypeARM)
821 {
822 // For ARM we have a bit in the n_desc field of the symbol
823 // that tells us ARM/Thumb which is bit 0x0008.
824 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
825 return eAddressClassCodeAlternateISA;
826 }
827 return eAddressClassCode;
828
829 case eSymbolTypeData: return eAddressClassData;
830 case eSymbolTypeRuntime: return eAddressClassRuntime;
831 case eSymbolTypeException: return eAddressClassRuntime;
832 case eSymbolTypeSourceFile: return eAddressClassDebug;
833 case eSymbolTypeHeaderFile: return eAddressClassDebug;
834 case eSymbolTypeObjectFile: return eAddressClassDebug;
835 case eSymbolTypeCommonBlock: return eAddressClassDebug;
836 case eSymbolTypeBlock: return eAddressClassDebug;
837 case eSymbolTypeLocal: return eAddressClassData;
838 case eSymbolTypeParam: return eAddressClassData;
839 case eSymbolTypeVariable: return eAddressClassData;
840 case eSymbolTypeVariableType: return eAddressClassDebug;
841 case eSymbolTypeLineEntry: return eAddressClassDebug;
842 case eSymbolTypeLineHeader: return eAddressClassDebug;
843 case eSymbolTypeScopeBegin: return eAddressClassDebug;
844 case eSymbolTypeScopeEnd: return eAddressClassDebug;
845 case eSymbolTypeAdditional: return eAddressClassUnknown;
846 case eSymbolTypeCompiler: return eAddressClassDebug;
847 case eSymbolTypeInstrumentation:return eAddressClassDebug;
848 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000849 case eSymbolTypeObjCClass: return eAddressClassRuntime;
850 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
851 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000852 }
853 }
854 }
855 return eAddressClassUnknown;
856}
Chris Lattner24943d22010-06-08 16:52:24 +0000857
858Symtab *
859ObjectFileMachO::GetSymtab()
860{
Greg Clayton9482f052012-03-13 23:14:29 +0000861 ModuleSP module_sp(GetModule());
862 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000863 {
Greg Clayton9482f052012-03-13 23:14:29 +0000864 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
865 if (m_symtab_ap.get() == NULL)
866 {
867 m_symtab_ap.reset(new Symtab(this));
868 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
869 ParseSymtab (true);
870 m_symtab_ap->Finalize ();
871 }
Chris Lattner24943d22010-06-08 16:52:24 +0000872 }
873 return m_symtab_ap.get();
874}
875
876
877SectionList *
878ObjectFileMachO::GetSectionList()
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_sections_ap.get() == NULL)
885 {
886 m_sections_ap.reset(new SectionList());
887 ParseSections();
888 }
Chris Lattner24943d22010-06-08 16:52:24 +0000889 }
890 return m_sections_ap.get();
891}
892
893
894size_t
895ObjectFileMachO::ParseSections ()
896{
897 lldb::user_id_t segID = 0;
898 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000899 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000900 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000901 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000902 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000903 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000904 // First look up any LC_ENCRYPTION_INFO load commands
905 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
906 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000907 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000908 for (i=0; i<m_header.ncmds; ++i)
909 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000910 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000911 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000912 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000913
Greg Clayton54e33712012-05-25 18:09:55 +0000914 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000915 {
Greg Clayton54e33712012-05-25 18:09:55 +0000916 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
917 {
918 if (encryption_cmd.cryptid != 0)
919 {
920 EncryptedFileRanges::Entry entry;
921 entry.SetRangeBase(encryption_cmd.cryptoff);
922 entry.SetByteSize(encryption_cmd.cryptsize);
923 encrypted_file_ranges.Append(entry);
924 }
925 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000926 }
Greg Clayton54e33712012-05-25 18:09:55 +0000927 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000928 }
929
930 offset = MachHeaderSizeFromMagic(m_header.magic);
931
Greg Clayton54e33712012-05-25 18:09:55 +0000932 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000933 for (i=0; i<m_header.ncmds; ++i)
934 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000935 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000936 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
937 break;
938
Greg Clayton1674b122010-07-21 22:12:05 +0000939 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000940 {
941 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
942 {
943 load_cmd.vmaddr = m_data.GetAddress(&offset);
944 load_cmd.vmsize = m_data.GetAddress(&offset);
945 load_cmd.fileoff = m_data.GetAddress(&offset);
946 load_cmd.filesize = m_data.GetAddress(&offset);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000947 if (m_length != 0 && load_cmd.filesize != 0)
948 {
Greg Claytonbb759862013-04-16 16:51:19 +0000949 if (load_cmd.fileoff > m_length)
950 {
951 // We have a load command that says it extends past the end of hte file. This is likely
952 // a corrupt file. We don't have any way to return an error condition here (this method
953 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
954 // is null out the SectionList vector and if a process has been set up, dump a message
955 // to stdout. The most common case here is core file debugging with a truncated file.
956 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
957 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 ")",
958 i,
959 lc_segment_name,
960 load_cmd.fileoff,
961 m_length);
962 m_sections_ap->Clear();
963 return 0;
964 }
965
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000966 if (load_cmd.fileoff + load_cmd.filesize > m_length)
967 {
968 // We have a load command that says it extends past the end of hte file. This is likely
969 // a corrupt file. We don't have any way to return an error condition here (this method
970 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
971 // is null out the SectionList vector and if a process has been set up, dump a message
Greg Claytonbb759862013-04-16 16:51:19 +0000972 // to stdout. The most common case here is core file debugging with a truncated file.
973 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
974 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 ")",
975 i,
976 lc_segment_name,
977 load_cmd.fileoff + load_cmd.filesize,
978 m_length);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000979 m_sections_ap->Clear();
980 return 0;
981 }
982 }
Chris Lattner24943d22010-06-08 16:52:24 +0000983 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
984 {
Jason Molenda9badb6c2013-03-06 23:19:17 +0000985
Greg Clayton68ca8232011-01-25 02:58:48 +0000986 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
987
Chris Lattner24943d22010-06-08 16:52:24 +0000988 // Keep a list of mach segments around in case we need to
989 // get at data that isn't stored in the abstracted Sections.
990 m_mach_segments.push_back (load_cmd);
991
Greg Clayton36da2aa2013-01-25 18:06:21 +0000992 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 +0000993 // Use a segment ID of the segment index shifted left by 8 so they
994 // never conflict with any of the sections.
995 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000996 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000997 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000998 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000999 ++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
1000 segment_name, // Name of this section
1001 eSectionTypeContainer, // This section is a container of other sections.
1002 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1003 load_cmd.vmsize, // VM size in bytes of this section
1004 load_cmd.fileoff, // Offset to the data for this section in the file
1005 load_cmd.filesize, // Size in bytes of this section as found in the the file
1006 load_cmd.flags)); // Flags for this section
1007
Greg Clayton68ca8232011-01-25 02:58:48 +00001008 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001009 m_sections_ap->AddSection(segment_sp);
1010 }
1011
1012 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +00001013 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +00001014 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +00001015 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +00001016 // mach sections yet...
1017 if (m_mach_sections.empty())
1018 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +00001019 uint32_t segment_sect_idx;
1020 const lldb::user_id_t first_segment_sectID = sectID + 1;
1021
1022
Greg Clayton1674b122010-07-21 22:12:05 +00001023 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +00001024 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
1025 {
1026 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
1027 break;
1028 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1029 break;
1030 sect64.addr = m_data.GetAddress(&offset);
1031 sect64.size = m_data.GetAddress(&offset);
1032
1033 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1034 break;
1035
1036 // Keep a list of mach sections around in case we need to
1037 // get at data that isn't stored in the abstracted Sections.
1038 m_mach_sections.push_back (sect64);
1039
1040 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1041 if (!segment_name)
1042 {
1043 // We have a segment with no name so we need to conjure up
1044 // segments that correspond to the section's segname if there
1045 // isn't already such a section. If there is such a section,
1046 // we resize the section so that it spans all sections.
1047 // We also mark these sections as fake so address matches don't
1048 // hit if they land in the gaps between the child sections.
1049 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1050 segment_sp = m_sections_ap->FindSectionByName (segment_name);
1051 if (segment_sp.get())
1052 {
1053 Section *segment = segment_sp.get();
1054 // Grow the section size as needed.
1055 const lldb::addr_t sect64_min_addr = sect64.addr;
1056 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1057 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1058 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1059 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1060 if (sect64_min_addr >= curr_seg_min_addr)
1061 {
1062 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1063 // Only grow the section size if needed
1064 if (new_seg_byte_size > curr_seg_byte_size)
1065 segment->SetByteSize (new_seg_byte_size);
1066 }
1067 else
1068 {
1069 // We need to change the base address of the segment and
1070 // adjust the child section offsets for all existing children.
1071 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1072 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +00001073 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +00001074 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1075 }
Greg Clayton661825b2010-06-28 23:51:11 +00001076
1077 // Grow the section size as needed.
1078 if (sect64.offset)
1079 {
1080 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1081 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1082
1083 const lldb::addr_t section_min_file_offset = sect64.offset;
1084 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1085 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1086 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1087 segment->SetFileOffset (new_file_offset);
1088 segment->SetFileSize (new_file_size);
1089 }
Chris Lattner24943d22010-06-08 16:52:24 +00001090 }
1091 else
1092 {
1093 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +00001094 segment_sp.reset(new Section (segment_sp, // Parent section
1095 module_sp, // Module to which this section belongs
1096 ++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
1097 segment_name, // Name of this section
1098 eSectionTypeContainer, // This section is a container of other sections.
1099 sect64.addr, // File VM address == addresses as they are found in the object file
1100 sect64.size, // VM size in bytes of this section
1101 sect64.offset, // Offset to the data for this section in the file
1102 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1103 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +00001104 segment_sp->SetIsFake(true);
1105 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +00001106 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001107 }
1108 }
1109 assert (segment_sp.get());
1110
Greg Clayton1674b122010-07-21 22:12:05 +00001111 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +00001112 static ConstString g_sect_name_objc_data ("__objc_data");
1113 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1114 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1115 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1116 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1117 static ConstString g_sect_name_objc_const ("__objc_const");
1118 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1119 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001120
1121 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1122 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1123 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1124 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1125 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1126 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1127 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1128 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1129 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1130 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1131 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +00001132 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1133 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +00001134 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +00001135 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001136 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +00001137 static ConstString g_sect_name_DATA ("__DATA");
1138 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001139
Chris Lattner24943d22010-06-08 16:52:24 +00001140 SectionType sect_type = eSectionTypeOther;
1141
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001142 if (section_name == g_sect_name_dwarf_debug_abbrev)
1143 sect_type = eSectionTypeDWARFDebugAbbrev;
1144 else if (section_name == g_sect_name_dwarf_debug_aranges)
1145 sect_type = eSectionTypeDWARFDebugAranges;
1146 else if (section_name == g_sect_name_dwarf_debug_frame)
1147 sect_type = eSectionTypeDWARFDebugFrame;
1148 else if (section_name == g_sect_name_dwarf_debug_info)
1149 sect_type = eSectionTypeDWARFDebugInfo;
1150 else if (section_name == g_sect_name_dwarf_debug_line)
1151 sect_type = eSectionTypeDWARFDebugLine;
1152 else if (section_name == g_sect_name_dwarf_debug_loc)
1153 sect_type = eSectionTypeDWARFDebugLoc;
1154 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1155 sect_type = eSectionTypeDWARFDebugMacInfo;
1156 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1157 sect_type = eSectionTypeDWARFDebugPubNames;
1158 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1159 sect_type = eSectionTypeDWARFDebugPubTypes;
1160 else if (section_name == g_sect_name_dwarf_debug_ranges)
1161 sect_type = eSectionTypeDWARFDebugRanges;
1162 else if (section_name == g_sect_name_dwarf_debug_str)
1163 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001164 else if (section_name == g_sect_name_dwarf_apple_names)
1165 sect_type = eSectionTypeDWARFAppleNames;
1166 else if (section_name == g_sect_name_dwarf_apple_types)
1167 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001168 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1169 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001170 else if (section_name == g_sect_name_dwarf_apple_objc)
1171 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001172 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001173 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001174 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001175 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001176 else if (section_name == g_sect_name_eh_frame)
1177 sect_type = eSectionTypeEHFrame;
1178 else if (section_name == g_sect_name_cfstring)
1179 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001180 else if (section_name == g_sect_name_objc_data ||
1181 section_name == g_sect_name_objc_classrefs ||
1182 section_name == g_sect_name_objc_superrefs ||
1183 section_name == g_sect_name_objc_const ||
1184 section_name == g_sect_name_objc_classlist)
1185 {
1186 sect_type = eSectionTypeDataPointers;
1187 }
Chris Lattner24943d22010-06-08 16:52:24 +00001188
1189 if (sect_type == eSectionTypeOther)
1190 {
1191 switch (mach_sect_type)
1192 {
1193 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001194 case SectionTypeRegular:
1195 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001196 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001197 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001198 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001199 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001200 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001201 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001202 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1203 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1204 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1205 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1206 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1207 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1208 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1209 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1210 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1211 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1212 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1213 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1214 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1215 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1216 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1217 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001218 default: break;
1219 }
1220 }
1221
Greg Clayton3508c382012-02-24 01:59:29 +00001222 SectionSP section_sp(new Section (segment_sp,
1223 module_sp,
1224 ++sectID,
1225 section_name,
1226 sect_type,
1227 sect64.addr - segment_sp->GetFileAddress(),
1228 sect64.size,
1229 sect64.offset,
1230 sect64.offset == 0 ? 0 : sect64.size,
1231 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001232 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001233
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001234 bool section_is_encrypted = false;
1235 if (!segment_is_encrypted && load_cmd.filesize != 0)
1236 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001237
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001238 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001239 segment_sp->GetChildren().AddSection(section_sp);
1240
1241 if (segment_sp->IsFake())
1242 {
1243 segment_sp.reset();
1244 segment_name.Clear();
1245 }
1246 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001247 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001248 {
1249 if (first_segment_sectID <= sectID)
1250 {
1251 lldb::user_id_t sect_uid;
1252 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1253 {
1254 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1255 SectionSP next_section_sp;
1256 if (sect_uid + 1 <= sectID)
1257 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1258
1259 if (curr_section_sp.get())
1260 {
1261 if (curr_section_sp->GetByteSize() == 0)
1262 {
1263 if (next_section_sp.get() != NULL)
1264 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1265 else
1266 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1267 }
1268 }
1269 }
1270 }
1271 }
1272 }
1273 }
1274 }
Greg Clayton1674b122010-07-21 22:12:05 +00001275 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001276 {
1277 m_dysymtab.cmd = load_cmd.cmd;
1278 m_dysymtab.cmdsize = load_cmd.cmdsize;
1279 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1280 }
1281
1282 offset = load_cmd_offset + load_cmd.cmdsize;
1283 }
1284// if (dump_sections)
1285// {
1286// StreamFile s(stdout);
1287// m_sections_ap->Dump(&s, true);
1288// }
1289 return sectID; // Return the number of sections we registered with the module
1290}
1291
1292class MachSymtabSectionInfo
1293{
1294public:
1295
1296 MachSymtabSectionInfo (SectionList *section_list) :
1297 m_section_list (section_list),
1298 m_section_infos()
1299 {
1300 // Get the number of sections down to a depth of 1 to include
1301 // all segments and their sections, but no other sections that
1302 // may be added for debug map or
1303 m_section_infos.resize(section_list->GetNumSections(1));
1304 }
1305
1306
Greg Clayton3508c382012-02-24 01:59:29 +00001307 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001308 GetSection (uint8_t n_sect, addr_t file_addr)
1309 {
1310 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001311 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001312 if (n_sect < m_section_infos.size())
1313 {
Greg Clayton3508c382012-02-24 01:59:29 +00001314 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001315 {
Greg Clayton3508c382012-02-24 01:59:29 +00001316 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1317 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001318 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001319 {
Greg Clayton3508c382012-02-24 01:59:29 +00001320 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1321 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001322 }
1323 else
1324 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001325 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001326 }
Chris Lattner24943d22010-06-08 16:52:24 +00001327 }
1328 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001329 {
1330 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001331 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001332 }
1333 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1334 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1335 {
1336 // Symbol is in section with zero size, but has the same start
1337 // address as the section. This can happen with linker symbols
1338 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001339 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001340 }
Chris Lattner24943d22010-06-08 16:52:24 +00001341 }
Greg Clayton3508c382012-02-24 01:59:29 +00001342 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001343 }
1344
1345protected:
1346 struct SectionInfo
1347 {
1348 SectionInfo () :
1349 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001350 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001351 {
1352 }
1353
1354 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001355 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001356 };
1357 SectionList *m_section_list;
1358 std::vector<SectionInfo> m_section_infos;
1359};
1360
Chris Lattner24943d22010-06-08 16:52:24 +00001361size_t
1362ObjectFileMachO::ParseSymtab (bool minimize)
1363{
1364 Timer scoped_timer(__PRETTY_FUNCTION__,
1365 "ObjectFileMachO::ParseSymtab () module = %s",
1366 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001367 ModuleSP module_sp (GetModule());
1368 if (!module_sp)
1369 return 0;
1370
1371 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1372 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1373 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1374 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001375 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001376 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001377
Greg Clayton952e9dc2013-03-27 23:08:40 +00001378 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001379
Chris Lattner24943d22010-06-08 16:52:24 +00001380 for (i=0; i<m_header.ncmds; ++i)
1381 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001382 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001383 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001384 struct load_command lc;
1385 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001386 break;
1387 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001388 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001389 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001390 case LoadCommandSymtab:
1391 symtab_load_command.cmd = lc.cmd;
1392 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001393 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001394 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1395 return 0;
1396 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001397 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001398 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001399 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001400 return 0;
1401 }
1402
1403 if (symtab_load_command.stroff == 0)
1404 {
1405 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001406 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001407 return 0;
1408 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001409
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001410 if (symtab_load_command.nsyms == 0)
1411 {
1412 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001413 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001414 return 0;
1415 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001416
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001417 if (symtab_load_command.strsize == 0)
1418 {
1419 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001420 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001421 return 0;
1422 }
1423 break;
1424
1425 case LoadCommandFunctionStarts:
1426 function_starts_load_command.cmd = lc.cmd;
1427 function_starts_load_command.cmdsize = lc.cmdsize;
1428 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1429 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1430 break;
1431
1432 default:
1433 break;
1434 }
1435 offset = cmd_offset + lc.cmdsize;
1436 }
1437
1438 if (symtab_load_command.cmd)
1439 {
1440 Symtab *symtab = m_symtab_ap.get();
1441 SectionList *section_list = GetSectionList();
1442 if (section_list == NULL)
1443 return 0;
1444
1445 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001446 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001447
Greg Clayton36da2aa2013-01-25 18:06:21 +00001448 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1449 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001450 bool bit_width_32 = addr_byte_size == 4;
1451 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1452
Greg Clayton36da2aa2013-01-25 18:06:21 +00001453 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1454 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1455 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001456 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001457
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001458 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1459 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001460 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1461 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001462 {
Greg Claytondd29b972012-05-18 23:20:01 +00001463 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001464 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1465 // Reading mach file from memory in a process or core file...
1466
1467 if (linkedit_section_sp)
1468 {
1469 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1470 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1471 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001472 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001473
1474 bool data_was_read = false;
1475
1476#if defined (__APPLE__) && defined (__arm__)
1477 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001478 {
Greg Clayton29021d32012-04-18 05:19:20 +00001479 // This mach-o memory file is in the dyld shared cache. If this
1480 // program is not remote and this is iOS, then this process will
1481 // share the same shared cache as the process we are debugging and
1482 // we can read the entire __LINKEDIT from the address space in this
1483 // process. This is a needed optimization that is used for local iOS
1484 // debugging only since all shared libraries in the shared cache do
1485 // not have corresponding files that exist in the file system of the
1486 // device. They have been combined into a single file. This means we
1487 // always have to load these files from memory. All of the symbol and
1488 // string tables from all of the __LINKEDIT sections from the shared
1489 // libraries in the shared cache have been merged into a single large
1490 // symbol and string table. Reading all of this symbol and string table
1491 // data across can slow down debug launch times, so we optimize this by
1492 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda45c75502013-04-16 06:24:42 +00001493
1494 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1495 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1496 bool use_lldb_cache = true;
1497 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1498 {
1499 use_lldb_cache = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001500 ModuleSP module_sp (GetModule());
1501 if (module_sp)
1502 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1503
Jason Molenda45c75502013-04-16 06:24:42 +00001504 }
1505
Greg Clayton29021d32012-04-18 05:19:20 +00001506 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda45c75502013-04-16 06:24:42 +00001507 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton29021d32012-04-18 05:19:20 +00001508 {
1509 data_was_read = true;
1510 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001511 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001512 if (function_starts_load_command.cmd)
1513 {
1514 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1515 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1516 }
1517 }
1518 }
1519#endif
1520
1521 if (!data_was_read)
1522 {
1523 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1524 if (nlist_data_sp)
1525 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001526 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1527 //if (strtab_data_sp)
1528 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001529 if (m_dysymtab.nindirectsyms != 0)
1530 {
1531 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1532 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1533 if (indirect_syms_data_sp)
1534 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1535 }
Greg Clayton29021d32012-04-18 05:19:20 +00001536 if (function_starts_load_command.cmd)
1537 {
1538 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1539 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1540 if (func_start_data_sp)
1541 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1542 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001543 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001544 }
1545 }
1546 else
1547 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001548 nlist_data.SetData (m_data,
1549 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001550 nlist_data_byte_size);
1551 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001552 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001553 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001554 if (m_dysymtab.nindirectsyms != 0)
1555 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001556 indirect_symbol_index_data.SetData (m_data,
1557 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001558 m_dysymtab.nindirectsyms * 4);
1559 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001560 if (function_starts_load_command.cmd)
1561 {
1562 function_starts_data.SetData (m_data,
1563 function_starts_load_command.dataoff,
1564 function_starts_load_command.datasize);
1565 }
1566 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001567
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001568 if (nlist_data.GetByteSize() == 0)
1569 {
1570 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001571 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001572 return 0;
1573 }
1574
1575
Greg Clayton3a5dc012012-05-25 17:04:00 +00001576 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1577 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001578 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001579 if (process)
1580 {
1581 if (strtab_addr == LLDB_INVALID_ADDRESS)
1582 {
1583 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001584 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001585 return 0;
1586 }
1587 }
1588 else
Greg Claytondd29b972012-05-18 23:20:01 +00001589 {
1590 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001591 module_sp->LogMessage(log, "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001592 return 0;
1593 }
1594 }
Greg Claytondd29b972012-05-18 23:20:01 +00001595
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001596 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1597 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1598 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1599 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1600 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1601 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1602 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1603 SectionSP eh_frame_section_sp;
1604 if (text_section_sp.get())
1605 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1606 else
1607 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1608
Greg Claytond2653c22012-03-14 01:53:24 +00001609 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001610
1611 // lldb works best if it knows the start addresss of all functions in a module.
1612 // Linker symbols or debug info are normally the best source of information for start addr / size but
1613 // they may be stripped in a released binary.
Jason Molenda2120aef2013-04-16 00:18:44 +00001614 // Two additional sources of information exist in Mach-O binaries:
Jason Molendad7938392013-03-21 03:36:01 +00001615 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1616 // binary, relative to the text section.
1617 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1618 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1619 // Binaries built to run on older releases may need to use eh_frame information.
1620
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001621 if (text_section_sp && function_starts_data.GetByteSize())
1622 {
1623 FunctionStarts::Entry function_start_entry;
1624 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001625 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001626 function_start_entry.addr = text_section_sp->GetFileAddress();
1627 uint64_t delta;
1628 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1629 {
1630 // Now append the current entry
1631 function_start_entry.addr += delta;
1632 function_starts.Append(function_start_entry);
1633 }
Jason Molenda2120aef2013-04-16 00:18:44 +00001634 }
Jason Molendad7938392013-03-21 03:36:01 +00001635 else
1636 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001637 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1638 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1639 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1640 // the module.
1641 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001642 {
1643 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1644 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1645 eh_frame.GetFunctionAddressAndSizeVector (functions);
1646 addr_t text_base_addr = text_section_sp->GetFileAddress();
1647 size_t count = functions.GetSize();
1648 for (size_t i = 0; i < count; ++i)
1649 {
1650 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1651 if (func)
1652 {
1653 FunctionStarts::Entry function_start_entry;
1654 function_start_entry.addr = func->base - text_base_addr;
1655 function_starts.Append(function_start_entry);
1656 }
1657 }
1658 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001659 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001660
Greg Clayton36da2aa2013-01-25 18:06:21 +00001661 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001662
Greg Clayton36da2aa2013-01-25 18:06:21 +00001663 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 +00001664
Greg Clayton36da2aa2013-01-25 18:06:21 +00001665 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001666
1667 uint32_t N_SO_index = UINT32_MAX;
1668
1669 MachSymtabSectionInfo section_info (section_list);
1670 std::vector<uint32_t> N_FUN_indexes;
1671 std::vector<uint32_t> N_NSYM_indexes;
1672 std::vector<uint32_t> N_INCL_indexes;
1673 std::vector<uint32_t> N_BRAC_indexes;
1674 std::vector<uint32_t> N_COMM_indexes;
1675 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1676 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1677 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1678 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1679 // Any symbols that get merged into another will get an entry
1680 // in this map so we know
1681 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1682 uint32_t nlist_idx = 0;
1683 Symbol *symbol_ptr = NULL;
1684
1685 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001686 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001687 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001688 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001689 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001690
Jason Molendab62abd52012-06-21 01:51:02 +00001691#if defined (__APPLE__) && defined (__arm__)
1692
1693 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1694 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1695 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1696 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1697 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1698 // nlist parser to ignore all LOCAL symbols.
1699
1700 if (m_header.flags & 0x80000000u)
1701 {
1702 // Before we can start mapping the DSC, we need to make certain the target process is actually
1703 // using the cache we can find.
1704
Jason Molendab62abd52012-06-21 01:51:02 +00001705 // Next we need to determine the correct path for the dyld shared cache.
1706
1707 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1708 char dsc_path[PATH_MAX];
1709
1710 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001711 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1712 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001713 header_arch.GetArchitectureName());
1714
1715 FileSpec dsc_filespec(dsc_path, false);
1716
1717 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda2120aef2013-04-16 00:18:44 +00001718 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001719 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001720 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1721 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1722 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001723 uint32_t imagesOffset;
1724 uint32_t imagesCount;
1725 uint64_t dyldBaseAddress;
1726 uint64_t codeSignatureOffset;
1727 uint64_t codeSignatureSize;
1728 uint64_t slideInfoOffset;
1729 uint64_t slideInfoSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001730 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1731 uint64_t localSymbolsSize; // size of local symbols information
1732 };
1733 struct lldb_copy_dyld_cache_header_v1
1734 {
1735 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1736 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1737 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1738 uint32_t imagesOffset;
1739 uint32_t imagesCount;
1740 uint64_t dyldBaseAddress;
1741 uint64_t codeSignatureOffset;
1742 uint64_t codeSignatureSize;
1743 uint64_t slideInfoOffset;
1744 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001745 uint64_t localSymbolsOffset;
1746 uint64_t localSymbolsSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001747 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001748 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001749
Jason Molenda2120aef2013-04-16 00:18:44 +00001750 struct lldb_copy_dyld_cache_mapping_info
1751 {
1752 uint64_t address;
1753 uint64_t size;
1754 uint64_t fileOffset;
1755 uint32_t maxProt;
1756 uint32_t initProt;
1757 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001758
Greg Claytonab77dcb2012-09-05 22:30:51 +00001759 struct lldb_copy_dyld_cache_local_symbols_info
1760 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001761 uint32_t nlistOffset;
1762 uint32_t nlistCount;
1763 uint32_t stringsOffset;
1764 uint32_t stringsSize;
1765 uint32_t entriesOffset;
1766 uint32_t entriesCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001767 };
1768 struct lldb_copy_dyld_cache_local_symbols_entry
1769 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001770 uint32_t dylibOffset;
1771 uint32_t nlistStartIndex;
1772 uint32_t nlistCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001773 };
Jason Molendab62abd52012-06-21 01:51:02 +00001774
Jason Molendafd3b35d2012-06-22 03:28:35 +00001775 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1776 The dyld_cache_local_symbols_info structure gives us three things:
1777 1. The start and count of the nlist records in the dyld_shared_cache file
1778 2. The start and size of the strings for these nlist records
1779 3. The start and count of dyld_cache_local_symbols_entry entries
1780
1781 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1782 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001783 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001784 and the count of how many nlist records there are for this dylib/framework.
1785 */
1786
Jason Molendab62abd52012-06-21 01:51:02 +00001787 // Process the dsc header to find the unmapped symbols
1788 //
1789 // Save some VM space, do not map the entire cache in one shot.
1790
Jason Molenda6bcabae2013-03-06 23:17:36 +00001791 DataBufferSP dsc_data_sp;
1792 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1793
1794 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001795 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001796 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001797
Jason Molenda6bcabae2013-03-06 23:17:36 +00001798 char version_str[17];
1799 int version = -1;
1800 lldb::offset_t offset = 0;
1801 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1802 version_str[16] = '\0';
1803 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1804 {
1805 int v;
1806 if (::sscanf (version_str + 6, "%d", &v) == 1)
1807 {
1808 version = v;
1809 }
1810 }
1811
Jason Molenda45c75502013-04-16 06:24:42 +00001812 UUID dsc_uuid;
1813 if (version >= 1)
1814 {
1815 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1816 uint8_t uuid_bytes[sizeof (uuid_t)];
1817 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1818 dsc_uuid.SetBytes (uuid_bytes);
1819 }
1820
1821 bool uuid_match = true;
1822 if (dsc_uuid.IsValid() && process)
1823 {
1824 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1825
1826 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1827 {
1828 // The on-disk dyld_shared_cache file is not the same as the one in this
1829 // process' memory, don't use it.
1830 uuid_match = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001831 ModuleSP module_sp (GetModule());
1832 if (module_sp)
1833 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 +00001834 }
1835 }
1836
Jason Molenda9badb6c2013-03-06 23:19:17 +00001837 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001838
Jason Molendab62abd52012-06-21 01:51:02 +00001839 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1840
1841 // If the mappingOffset points to a location inside the header, we've
1842 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda45c75502013-04-16 06:24:42 +00001843 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendab62abd52012-06-21 01:51:02 +00001844 {
1845
Jason Molenda6bcabae2013-03-06 23:17:36 +00001846 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1847 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1848 offset = 0;
1849
1850 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1851 // in the shared library cache need to be adjusted by an offset to match up with the
1852 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1853 // recorded in mapping_offset_value.
1854 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1855
1856 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001857 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1858 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1859
Jason Molenda9badb6c2013-03-06 23:19:17 +00001860 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001861 {
1862 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001863 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001864 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001865 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001866
1867 offset = 0;
1868
1869 // Read the local_symbols_infos struct in one shot
1870 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1871 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1872
Jason Molendab62abd52012-06-21 01:51:02 +00001873 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1874
Jason Molenda6bcabae2013-03-06 23:17:36 +00001875 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001876
1877 offset = local_symbols_info.entriesOffset;
1878 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1879 {
1880 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1881 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1882 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1883 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1884
Jason Molenda9badb6c2013-03-06 23:19:17 +00001885 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001886 {
1887 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1888
1889 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1890 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1891 num_syms = symtab->GetNumSymbols();
1892
1893 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1894 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1895
Jason Molenda9badb6c2013-03-06 23:19:17 +00001896 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001897 {
1898 /////////////////////////////
1899 {
1900 struct nlist_64 nlist;
1901 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1902 break;
1903
1904 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1905 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1906 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1907 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1908 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1909
1910 SymbolType type = eSymbolTypeInvalid;
1911 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1912
1913 if (symbol_name == NULL)
1914 {
1915 // No symbol should be NULL, even the symbols with no
1916 // string values should have an offset zero which points
1917 // to an empty C-string
1918 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00001919 "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 +00001920 entry_index,
1921 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00001922 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendab62abd52012-06-21 01:51:02 +00001923 continue;
1924 }
1925 if (symbol_name[0] == '\0')
1926 symbol_name = NULL;
1927
1928 const char *symbol_name_non_abi_mangled = NULL;
1929
1930 SectionSP symbol_section;
1931 uint32_t symbol_byte_size = 0;
1932 bool add_nlist = true;
1933 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001934 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001935
1936 assert (sym_idx < num_syms);
1937
1938 sym[sym_idx].SetDebug (is_debug);
1939
1940 if (is_debug)
1941 {
1942 switch (nlist.n_type)
1943 {
1944 case StabGlobalSymbol:
1945 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1946 // Sometimes the N_GSYM value contains the address.
1947
1948 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1949 // have the same address, but we want to ensure that we always find only the real symbol,
1950 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1951 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1952 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1953 // same address.
1954
1955 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1956 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1957 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1958 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1959 add_nlist = false;
1960 else
1961 {
1962 sym[sym_idx].SetExternal(true);
1963 if (nlist.n_value != 0)
1964 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1965 type = eSymbolTypeData;
1966 }
1967 break;
1968
1969 case StabFunctionName:
1970 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1971 type = eSymbolTypeCompiler;
1972 break;
1973
1974 case StabFunction:
1975 // N_FUN -- procedure: name,,n_sect,linenumber,address
1976 if (symbol_name)
1977 {
1978 type = eSymbolTypeCode;
1979 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1980
1981 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1982 // We use the current number of symbols in the symbol table in lieu of
1983 // using nlist_idx in case we ever start trimming entries out
1984 N_FUN_indexes.push_back(sym_idx);
1985 }
1986 else
1987 {
1988 type = eSymbolTypeCompiler;
1989
1990 if ( !N_FUN_indexes.empty() )
1991 {
1992 // Copy the size of the function into the original STAB entry so we don't have
1993 // to hunt for it later
1994 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1995 N_FUN_indexes.pop_back();
1996 // We don't really need the end function STAB as it contains the size which
1997 // we already placed with the original symbol, so don't add it if we want a
1998 // minimal symbol table
1999 if (minimize)
2000 add_nlist = false;
2001 }
2002 }
2003 break;
2004
2005 case StabStaticSymbol:
2006 // N_STSYM -- static symbol: name,,n_sect,type,address
2007 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2008 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2009 type = eSymbolTypeData;
2010 break;
2011
2012 case StabLocalCommon:
2013 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2014 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2015 type = eSymbolTypeCommonBlock;
2016 break;
2017
2018 case StabBeginSymbol:
2019 // N_BNSYM
2020 // We use the current number of symbols in the symbol table in lieu of
2021 // using nlist_idx in case we ever start trimming entries out
2022 if (minimize)
2023 {
2024 // Skip these if we want minimal symbol tables
2025 add_nlist = false;
2026 }
2027 else
2028 {
2029 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2030 N_NSYM_indexes.push_back(sym_idx);
2031 type = eSymbolTypeScopeBegin;
2032 }
2033 break;
2034
2035 case StabEndSymbol:
2036 // N_ENSYM
2037 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2038 // so that we can always skip the entire symbol if we need to navigate
2039 // more quickly at the source level when parsing STABS
2040 if (minimize)
2041 {
2042 // Skip these if we want minimal symbol tables
2043 add_nlist = false;
2044 }
2045 else
2046 {
2047 if ( !N_NSYM_indexes.empty() )
2048 {
2049 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2050 symbol_ptr->SetByteSize(sym_idx + 1);
2051 symbol_ptr->SetSizeIsSibling(true);
2052 N_NSYM_indexes.pop_back();
2053 }
2054 type = eSymbolTypeScopeEnd;
2055 }
2056 break;
2057
2058
2059 case StabSourceFileOptions:
2060 // N_OPT - emitted with gcc2_compiled and in gcc source
2061 type = eSymbolTypeCompiler;
2062 break;
2063
2064 case StabRegisterSymbol:
2065 // N_RSYM - register sym: name,,NO_SECT,type,register
2066 type = eSymbolTypeVariable;
2067 break;
2068
2069 case StabSourceLine:
2070 // N_SLINE - src line: 0,,n_sect,linenumber,address
2071 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2072 type = eSymbolTypeLineEntry;
2073 break;
2074
2075 case StabStructureType:
2076 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2077 type = eSymbolTypeVariableType;
2078 break;
2079
2080 case StabSourceFileName:
2081 // N_SO - source file name
2082 type = eSymbolTypeSourceFile;
2083 if (symbol_name == NULL)
2084 {
2085 if (minimize)
2086 add_nlist = false;
2087 if (N_SO_index != UINT32_MAX)
2088 {
2089 // Set the size of the N_SO to the terminating index of this N_SO
2090 // so that we can always skip the entire N_SO if we need to navigate
2091 // more quickly at the source level when parsing STABS
2092 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2093 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2094 symbol_ptr->SetSizeIsSibling(true);
2095 }
2096 N_NSYM_indexes.clear();
2097 N_INCL_indexes.clear();
2098 N_BRAC_indexes.clear();
2099 N_COMM_indexes.clear();
2100 N_FUN_indexes.clear();
2101 N_SO_index = UINT32_MAX;
2102 }
2103 else
2104 {
2105 // We use the current number of symbols in the symbol table in lieu of
2106 // using nlist_idx in case we ever start trimming entries out
2107 const bool N_SO_has_full_path = symbol_name[0] == '/';
2108 if (N_SO_has_full_path)
2109 {
2110 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2111 {
2112 // We have two consecutive N_SO entries where the first contains a directory
2113 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00002114 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002115 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2116 add_nlist = false;
2117 }
2118 else
2119 {
2120 // This is the first entry in a N_SO that contains a directory or
2121 // a full path to the source file
2122 N_SO_index = sym_idx;
2123 }
2124 }
2125 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2126 {
2127 // This is usually the second N_SO entry that contains just the filename,
2128 // so here we combine it with the first one if we are minimizing the symbol table
2129 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2130 if (so_path && so_path[0])
2131 {
2132 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002133 const size_t double_slash_pos = full_so_path.find("//");
2134 if (double_slash_pos != std::string::npos)
2135 {
2136 // The linker has been generating bad N_SO entries with doubled up paths
2137 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2138 // and the second is the directory for the source file so you end up with
2139 // a path that looks like "/tmp/src//tmp/src/"
2140 FileSpec so_dir(so_path, false);
2141 if (!so_dir.Exists())
2142 {
2143 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2144 if (so_dir.Exists())
2145 {
2146 // Trim off the incorrect path
2147 full_so_path.erase(0, double_slash_pos + 1);
2148 }
2149 }
2150 }
Jason Molendab62abd52012-06-21 01:51:02 +00002151 if (*full_so_path.rbegin() != '/')
2152 full_so_path += '/';
2153 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00002154 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002155 add_nlist = false;
2156 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2157 }
2158 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002159 else
2160 {
2161 // This could be a relative path to a N_SO
2162 N_SO_index = sym_idx;
2163 }
Jason Molendab62abd52012-06-21 01:51:02 +00002164 }
Jason Molendab62abd52012-06-21 01:51:02 +00002165 break;
2166
2167 case StabObjectFileName:
2168 // N_OSO - object file name: name,,0,0,st_mtime
2169 type = eSymbolTypeObjectFile;
2170 break;
2171
2172 case StabLocalSymbol:
2173 // N_LSYM - local sym: name,,NO_SECT,type,offset
2174 type = eSymbolTypeLocal;
2175 break;
2176
2177 //----------------------------------------------------------------------
2178 // INCL scopes
2179 //----------------------------------------------------------------------
2180 case StabBeginIncludeFileName:
2181 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2182 // We use the current number of symbols in the symbol table in lieu of
2183 // using nlist_idx in case we ever start trimming entries out
2184 N_INCL_indexes.push_back(sym_idx);
2185 type = eSymbolTypeScopeBegin;
2186 break;
2187
2188 case StabEndIncludeFile:
2189 // N_EINCL - include file end: name,,NO_SECT,0,0
2190 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2191 // so that we can always skip the entire symbol if we need to navigate
2192 // more quickly at the source level when parsing STABS
2193 if ( !N_INCL_indexes.empty() )
2194 {
2195 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2196 symbol_ptr->SetByteSize(sym_idx + 1);
2197 symbol_ptr->SetSizeIsSibling(true);
2198 N_INCL_indexes.pop_back();
2199 }
2200 type = eSymbolTypeScopeEnd;
2201 break;
2202
2203 case StabIncludeFileName:
2204 // N_SOL - #included file name: name,,n_sect,0,address
2205 type = eSymbolTypeHeaderFile;
2206
2207 // We currently don't use the header files on darwin
2208 if (minimize)
2209 add_nlist = false;
2210 break;
2211
2212 case StabCompilerParameters:
2213 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2214 type = eSymbolTypeCompiler;
2215 break;
2216
2217 case StabCompilerVersion:
2218 // N_VERSION - compiler version: name,,NO_SECT,0,0
2219 type = eSymbolTypeCompiler;
2220 break;
2221
2222 case StabCompilerOptLevel:
2223 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2224 type = eSymbolTypeCompiler;
2225 break;
2226
2227 case StabParameter:
2228 // N_PSYM - parameter: name,,NO_SECT,type,offset
2229 type = eSymbolTypeVariable;
2230 break;
2231
2232 case StabAlternateEntry:
2233 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2234 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2235 type = eSymbolTypeLineEntry;
2236 break;
2237
2238 //----------------------------------------------------------------------
2239 // Left and Right Braces
2240 //----------------------------------------------------------------------
2241 case StabLeftBracket:
2242 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2243 // We use the current number of symbols in the symbol table in lieu of
2244 // using nlist_idx in case we ever start trimming entries out
2245 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2246 N_BRAC_indexes.push_back(sym_idx);
2247 type = eSymbolTypeScopeBegin;
2248 break;
2249
2250 case StabRightBracket:
2251 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2252 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2253 // so that we can always skip the entire symbol if we need to navigate
2254 // more quickly at the source level when parsing STABS
2255 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2256 if ( !N_BRAC_indexes.empty() )
2257 {
2258 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2259 symbol_ptr->SetByteSize(sym_idx + 1);
2260 symbol_ptr->SetSizeIsSibling(true);
2261 N_BRAC_indexes.pop_back();
2262 }
2263 type = eSymbolTypeScopeEnd;
2264 break;
2265
2266 case StabDeletedIncludeFile:
2267 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2268 type = eSymbolTypeHeaderFile;
2269 break;
2270
2271 //----------------------------------------------------------------------
2272 // COMM scopes
2273 //----------------------------------------------------------------------
2274 case StabBeginCommon:
2275 // N_BCOMM - begin common: name,,NO_SECT,0,0
2276 // We use the current number of symbols in the symbol table in lieu of
2277 // using nlist_idx in case we ever start trimming entries out
2278 type = eSymbolTypeScopeBegin;
2279 N_COMM_indexes.push_back(sym_idx);
2280 break;
2281
2282 case StabEndCommonLocal:
2283 // N_ECOML - end common (local name): 0,,n_sect,0,address
2284 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2285 // Fall through
2286
2287 case StabEndCommon:
2288 // N_ECOMM - end common: name,,n_sect,0,0
2289 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2290 // so that we can always skip the entire symbol if we need to navigate
2291 // more quickly at the source level when parsing STABS
2292 if ( !N_COMM_indexes.empty() )
2293 {
2294 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2295 symbol_ptr->SetByteSize(sym_idx + 1);
2296 symbol_ptr->SetSizeIsSibling(true);
2297 N_COMM_indexes.pop_back();
2298 }
2299 type = eSymbolTypeScopeEnd;
2300 break;
2301
2302 case StabLength:
2303 // N_LENG - second stab entry with length information
2304 type = eSymbolTypeAdditional;
2305 break;
2306
2307 default: break;
2308 }
2309 }
2310 else
2311 {
2312 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2313 uint8_t n_type = NlistMaskType & nlist.n_type;
2314 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2315
2316 switch (n_type)
2317 {
2318 case NListTypeIndirect: // N_INDR - Fall through
2319 case NListTypePreboundUndefined:// N_PBUD - Fall through
2320 case NListTypeUndefined: // N_UNDF
2321 type = eSymbolTypeUndefined;
2322 break;
2323
2324 case NListTypeAbsolute: // N_ABS
2325 type = eSymbolTypeAbsolute;
2326 break;
2327
2328 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002329 {
2330 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002331
Greg Clayton01e6a582012-11-27 01:52:16 +00002332 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002333 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002334 // TODO: warn about this?
2335 add_nlist = false;
2336 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002337 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002338
2339 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002340 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002341 type = eSymbolTypeException;
2342 }
2343 else
2344 {
2345 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002346
Greg Clayton01e6a582012-11-27 01:52:16 +00002347 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002348 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002349 case SectionTypeRegular: break; // regular section
2350 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2351 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2352 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2353 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2354 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2355 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2356 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2357 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2358 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2359 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2360 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2361 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2362 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2363 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2364 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2365 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2366 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002367 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002368
Greg Clayton01e6a582012-11-27 01:52:16 +00002369 if (type == eSymbolTypeInvalid)
2370 {
2371 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2372 if (symbol_section->IsDescendant (text_section_sp.get()))
2373 {
2374 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2375 SectionAttrUserSelfModifyingCode |
2376 SectionAttrSytemSomeInstructions))
2377 type = eSymbolTypeData;
2378 else
2379 type = eSymbolTypeCode;
2380 }
2381 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002382 {
2383 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2384 {
2385 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002386
Greg Clayton01e6a582012-11-27 01:52:16 +00002387 if (symbol_name &&
2388 symbol_name[0] == '_' &&
2389 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002390 symbol_name[2] == 'B')
2391 {
2392 llvm::StringRef symbol_name_ref(symbol_name);
2393 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2394 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2395 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2396 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2397 {
2398 symbol_name_non_abi_mangled = symbol_name + 1;
2399 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2400 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002401 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002402 }
2403 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2404 {
2405 symbol_name_non_abi_mangled = symbol_name + 1;
2406 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2407 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002408 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002409 }
2410 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2411 {
2412 symbol_name_non_abi_mangled = symbol_name + 1;
2413 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2414 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002415 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002416 }
2417 }
2418 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002419 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002420 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002421 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002422 }
2423 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002424 {
2425 type = eSymbolTypeData;
2426 }
2427 }
2428 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2429 {
2430 type = eSymbolTypeTrampoline;
2431 }
2432 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2433 {
2434 type = eSymbolTypeRuntime;
2435 if (symbol_name && symbol_name[0] == '.')
2436 {
2437 llvm::StringRef symbol_name_ref(symbol_name);
2438 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2439 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002440 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002441 symbol_name_non_abi_mangled = symbol_name;
2442 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2443 type = eSymbolTypeObjCClass;
2444 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002445 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002446 }
2447 }
2448 }
Jason Molendab62abd52012-06-21 01:51:02 +00002449 }
2450 }
Jason Molendab62abd52012-06-21 01:51:02 +00002451 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002452 }
Jason Molendab62abd52012-06-21 01:51:02 +00002453 }
2454
2455 if (add_nlist)
2456 {
2457 uint64_t symbol_value = nlist.n_value;
2458 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002459
Jason Molendab62abd52012-06-21 01:51:02 +00002460 if (symbol_name_non_abi_mangled)
2461 {
Jason Molenda292cca82012-07-20 03:35:44 +00002462 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2463 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002464 }
2465 else
2466 {
2467 if (symbol_name && symbol_name[0] == '_')
2468 {
2469 symbol_name_is_mangled = symbol_name[1] == '_';
2470 symbol_name++; // Skip the leading underscore
2471 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002472
Jason Molendab62abd52012-06-21 01:51:02 +00002473 if (symbol_name)
2474 {
Jason Molenda292cca82012-07-20 03:35:44 +00002475 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002476 }
2477 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002478
Jason Molendab62abd52012-06-21 01:51:02 +00002479 if (is_debug == false)
2480 {
2481 if (type == eSymbolTypeCode)
2482 {
2483 // See if we can find a N_FUN entry for any code symbols.
2484 // If we do find a match, and the name matches, then we
2485 // can merge the two into just the function symbol to avoid
2486 // duplicate entries in the symbol table
2487 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2488 if (pos != N_FUN_addr_to_sym_idx.end())
2489 {
2490 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2491 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2492 {
2493 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2494 // We just need the flags from the linker symbol, so put these flags
2495 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2496 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2497 sym[sym_idx].Clear();
2498 continue;
2499 }
2500 }
2501 }
2502 else if (type == eSymbolTypeData)
2503 {
2504 // See if we can find a N_STSYM entry for any data symbols.
2505 // If we do find a match, and the name matches, then we
2506 // can merge the two into just the Static symbol to avoid
2507 // duplicate entries in the symbol table
2508 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2509 if (pos != N_STSYM_addr_to_sym_idx.end())
2510 {
2511 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2512 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2513 {
2514 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2515 // We just need the flags from the linker symbol, so put these flags
2516 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2517 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2518 sym[sym_idx].Clear();
2519 continue;
2520 }
2521 }
2522 }
2523 }
2524 if (symbol_section)
2525 {
2526 const addr_t section_file_addr = symbol_section->GetFileAddress();
2527 if (symbol_byte_size == 0 && function_starts_count > 0)
2528 {
2529 addr_t symbol_lookup_file_addr = nlist.n_value;
2530 // Do an exact address match for non-ARM addresses, else get the closest since
2531 // the symbol might be a thumb symbol which has an address with bit zero set
2532 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2533 if (is_arm && func_start_entry)
2534 {
2535 // Verify that the function start address is the symbol address (ARM)
2536 // or the symbol address + 1 (thumb)
2537 if (func_start_entry->addr != symbol_lookup_file_addr &&
2538 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2539 {
2540 // Not the right entry, NULL it out...
2541 func_start_entry = NULL;
2542 }
2543 }
2544 if (func_start_entry)
2545 {
2546 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002547
Jason Molendab62abd52012-06-21 01:51:02 +00002548 addr_t symbol_file_addr = func_start_entry->addr;
2549 uint32_t symbol_flags = 0;
2550 if (is_arm)
2551 {
2552 if (symbol_file_addr & 1)
2553 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2554 symbol_file_addr &= 0xfffffffffffffffeull;
2555 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002556
Jason Molendab62abd52012-06-21 01:51:02 +00002557 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2558 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2559 if (next_func_start_entry)
2560 {
2561 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2562 // Be sure the clear the Thumb address bit when we calculate the size
2563 // from the current and next address
2564 if (is_arm)
2565 next_symbol_file_addr &= 0xfffffffffffffffeull;
2566 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2567 }
2568 else
2569 {
2570 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2571 }
2572 }
2573 }
2574 symbol_value -= section_file_addr;
2575 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002576
Jason Molendab62abd52012-06-21 01:51:02 +00002577 sym[sym_idx].SetID (nlist_idx);
2578 sym[sym_idx].SetType (type);
2579 sym[sym_idx].GetAddress().SetSection (symbol_section);
2580 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2581 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002582
Jason Molendab62abd52012-06-21 01:51:02 +00002583 if (symbol_byte_size > 0)
2584 sym[sym_idx].SetByteSize(symbol_byte_size);
2585
Greg Clayton01e6a582012-11-27 01:52:16 +00002586 if (demangled_is_synthesized)
2587 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002588 ++sym_idx;
2589 }
2590 else
2591 {
2592 sym[sym_idx].Clear();
2593 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002594
Jason Molendab62abd52012-06-21 01:51:02 +00002595 }
2596 /////////////////////////////
2597 }
2598 break; // No more entries to consider
2599 }
2600 }
2601 }
2602 }
2603 }
2604 }
2605 }
2606
2607 // Must reset this in case it was mutated above!
2608 nlist_data_offset = 0;
2609#endif
2610
2611 // If the sym array was not created while parsing the DSC unmapped
2612 // symbols, create it now.
2613 if (sym == NULL)
2614 {
2615 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2616 num_syms = symtab->GetNumSymbols();
2617 }
2618
2619 if (unmapped_local_symbols_found)
2620 {
2621 assert(m_dysymtab.ilocalsym == 0);
2622 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2623 nlist_idx = m_dysymtab.nlocalsym;
2624 }
2625 else
2626 {
2627 nlist_idx = 0;
2628 }
2629
2630 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002631 {
2632 struct nlist_64 nlist;
2633 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2634 break;
2635
2636 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2637 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2638 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2639 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2640 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2641
2642 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002643 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002644
Greg Clayton3a5dc012012-05-25 17:04:00 +00002645 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002646 {
2647 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002648
Greg Claytondd29b972012-05-18 23:20:01 +00002649 if (symbol_name == NULL)
2650 {
2651 // No symbol should be NULL, even the symbols with no
2652 // string values should have an offset zero which points
2653 // to an empty C-string
2654 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00002655 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002656 nlist_idx,
2657 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00002658 module_sp->GetFileSpec().GetPath().c_str());
Greg Claytondd29b972012-05-18 23:20:01 +00002659 continue;
2660 }
2661 if (symbol_name[0] == '\0')
2662 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002663 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002664 else
2665 {
2666 const addr_t str_addr = strtab_addr + nlist.n_strx;
2667 Error str_error;
2668 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2669 symbol_name = memory_symbol_name.c_str();
2670 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002671 const char *symbol_name_non_abi_mangled = NULL;
2672
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002673 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002674 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002675 bool add_nlist = true;
2676 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002677 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002678
2679 assert (sym_idx < num_syms);
2680
2681 sym[sym_idx].SetDebug (is_debug);
2682
2683 if (is_debug)
2684 {
2685 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002686 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002687 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002688 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2689 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002690
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002691 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2692 // have the same address, but we want to ensure that we always find only the real symbol,
2693 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2694 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2695 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2696 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002697
2698 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002699 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2700 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2701 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2702 add_nlist = false;
2703 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002704 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002705 sym[sym_idx].SetExternal(true);
2706 if (nlist.n_value != 0)
2707 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2708 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002709 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002710 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002711
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002712 case StabFunctionName:
2713 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2714 type = eSymbolTypeCompiler;
2715 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002716
Jason Molenda9badb6c2013-03-06 23:19:17 +00002717 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002718 // N_FUN -- procedure: name,,n_sect,linenumber,address
2719 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002720 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002721 type = eSymbolTypeCode;
2722 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002723
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002724 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2725 // We use the current number of symbols in the symbol table in lieu of
2726 // using nlist_idx in case we ever start trimming entries out
2727 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002728 }
2729 else
2730 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002731 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002732
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002733 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002734 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002735 // Copy the size of the function into the original STAB entry so we don't have
2736 // to hunt for it later
2737 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2738 N_FUN_indexes.pop_back();
2739 // We don't really need the end function STAB as it contains the size which
2740 // we already placed with the original symbol, so don't add it if we want a
2741 // minimal symbol table
2742 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002743 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002744 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002745 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002746 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002747
Jason Molenda9badb6c2013-03-06 23:19:17 +00002748 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002749 // N_STSYM -- static symbol: name,,n_sect,type,address
2750 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2751 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2752 type = eSymbolTypeData;
2753 break;
2754
2755 case StabLocalCommon:
2756 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2757 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2758 type = eSymbolTypeCommonBlock;
2759 break;
2760
2761 case StabBeginSymbol:
2762 // N_BNSYM
2763 // We use the current number of symbols in the symbol table in lieu of
2764 // using nlist_idx in case we ever start trimming entries out
2765 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002766 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002767 // Skip these if we want minimal symbol tables
2768 add_nlist = false;
2769 }
2770 else
2771 {
2772 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2773 N_NSYM_indexes.push_back(sym_idx);
2774 type = eSymbolTypeScopeBegin;
2775 }
2776 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002777
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002778 case StabEndSymbol:
2779 // N_ENSYM
2780 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2781 // so that we can always skip the entire symbol if we need to navigate
2782 // more quickly at the source level when parsing STABS
2783 if (minimize)
2784 {
2785 // Skip these if we want minimal symbol tables
2786 add_nlist = false;
2787 }
2788 else
2789 {
2790 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002791 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002792 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2793 symbol_ptr->SetByteSize(sym_idx + 1);
2794 symbol_ptr->SetSizeIsSibling(true);
2795 N_NSYM_indexes.pop_back();
2796 }
2797 type = eSymbolTypeScopeEnd;
2798 }
2799 break;
2800
2801
2802 case StabSourceFileOptions:
2803 // N_OPT - emitted with gcc2_compiled and in gcc source
2804 type = eSymbolTypeCompiler;
2805 break;
2806
2807 case StabRegisterSymbol:
2808 // N_RSYM - register sym: name,,NO_SECT,type,register
2809 type = eSymbolTypeVariable;
2810 break;
2811
2812 case StabSourceLine:
2813 // N_SLINE - src line: 0,,n_sect,linenumber,address
2814 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2815 type = eSymbolTypeLineEntry;
2816 break;
2817
2818 case StabStructureType:
2819 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2820 type = eSymbolTypeVariableType;
2821 break;
2822
2823 case StabSourceFileName:
2824 // N_SO - source file name
2825 type = eSymbolTypeSourceFile;
2826 if (symbol_name == NULL)
2827 {
2828 if (minimize)
2829 add_nlist = false;
2830 if (N_SO_index != UINT32_MAX)
2831 {
2832 // Set the size of the N_SO to the terminating index of this N_SO
2833 // so that we can always skip the entire N_SO if we need to navigate
2834 // more quickly at the source level when parsing STABS
2835 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2836 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2837 symbol_ptr->SetSizeIsSibling(true);
2838 }
2839 N_NSYM_indexes.clear();
2840 N_INCL_indexes.clear();
2841 N_BRAC_indexes.clear();
2842 N_COMM_indexes.clear();
2843 N_FUN_indexes.clear();
2844 N_SO_index = UINT32_MAX;
2845 }
2846 else
2847 {
2848 // We use the current number of symbols in the symbol table in lieu of
2849 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002850 const bool N_SO_has_full_path = symbol_name[0] == '/';
2851 if (N_SO_has_full_path)
2852 {
2853 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2854 {
2855 // We have two consecutive N_SO entries where the first contains a directory
2856 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002857 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002858 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2859 add_nlist = false;
2860 }
2861 else
2862 {
2863 // This is the first entry in a N_SO that contains a directory or
2864 // a full path to the source file
2865 N_SO_index = sym_idx;
2866 }
2867 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002868 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2869 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002870 // This is usually the second N_SO entry that contains just the filename,
2871 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002872 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2873 if (so_path && so_path[0])
2874 {
2875 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002876 const size_t double_slash_pos = full_so_path.find("//");
2877 if (double_slash_pos != std::string::npos)
2878 {
2879 // The linker has been generating bad N_SO entries with doubled up paths
2880 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2881 // and the second is the directory for the source file so you end up with
2882 // a path that looks like "/tmp/src//tmp/src/"
2883 FileSpec so_dir(so_path, false);
2884 if (!so_dir.Exists())
2885 {
2886 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2887 if (so_dir.Exists())
2888 {
2889 // Trim off the incorrect path
2890 full_so_path.erase(0, double_slash_pos + 1);
2891 }
2892 }
2893 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002894 if (*full_so_path.rbegin() != '/')
2895 full_so_path += '/';
2896 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002897 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002898 add_nlist = false;
2899 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2900 }
2901 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002902 else
2903 {
2904 // This could be a relative path to a N_SO
2905 N_SO_index = sym_idx;
2906 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002907 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002908
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002909 break;
2910
2911 case StabObjectFileName:
2912 // N_OSO - object file name: name,,0,0,st_mtime
2913 type = eSymbolTypeObjectFile;
2914 break;
2915
2916 case StabLocalSymbol:
2917 // N_LSYM - local sym: name,,NO_SECT,type,offset
2918 type = eSymbolTypeLocal;
2919 break;
2920
2921 //----------------------------------------------------------------------
2922 // INCL scopes
2923 //----------------------------------------------------------------------
2924 case StabBeginIncludeFileName:
2925 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2926 // We use the current number of symbols in the symbol table in lieu of
2927 // using nlist_idx in case we ever start trimming entries out
2928 N_INCL_indexes.push_back(sym_idx);
2929 type = eSymbolTypeScopeBegin;
2930 break;
2931
2932 case StabEndIncludeFile:
2933 // N_EINCL - include file end: name,,NO_SECT,0,0
2934 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2935 // so that we can always skip the entire symbol if we need to navigate
2936 // more quickly at the source level when parsing STABS
2937 if ( !N_INCL_indexes.empty() )
2938 {
2939 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2940 symbol_ptr->SetByteSize(sym_idx + 1);
2941 symbol_ptr->SetSizeIsSibling(true);
2942 N_INCL_indexes.pop_back();
2943 }
2944 type = eSymbolTypeScopeEnd;
2945 break;
2946
2947 case StabIncludeFileName:
2948 // N_SOL - #included file name: name,,n_sect,0,address
2949 type = eSymbolTypeHeaderFile;
2950
2951 // We currently don't use the header files on darwin
2952 if (minimize)
2953 add_nlist = false;
2954 break;
2955
Jason Molenda9badb6c2013-03-06 23:19:17 +00002956 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002957 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2958 type = eSymbolTypeCompiler;
2959 break;
2960
2961 case StabCompilerVersion:
2962 // N_VERSION - compiler version: name,,NO_SECT,0,0
2963 type = eSymbolTypeCompiler;
2964 break;
2965
2966 case StabCompilerOptLevel:
2967 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2968 type = eSymbolTypeCompiler;
2969 break;
2970
2971 case StabParameter:
2972 // N_PSYM - parameter: name,,NO_SECT,type,offset
2973 type = eSymbolTypeVariable;
2974 break;
2975
2976 case StabAlternateEntry:
2977 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2978 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2979 type = eSymbolTypeLineEntry;
2980 break;
2981
2982 //----------------------------------------------------------------------
2983 // Left and Right Braces
2984 //----------------------------------------------------------------------
2985 case StabLeftBracket:
2986 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2987 // We use the current number of symbols in the symbol table in lieu of
2988 // using nlist_idx in case we ever start trimming entries out
2989 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2990 N_BRAC_indexes.push_back(sym_idx);
2991 type = eSymbolTypeScopeBegin;
2992 break;
2993
2994 case StabRightBracket:
2995 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2996 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2997 // so that we can always skip the entire symbol if we need to navigate
2998 // more quickly at the source level when parsing STABS
2999 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3000 if ( !N_BRAC_indexes.empty() )
3001 {
3002 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3003 symbol_ptr->SetByteSize(sym_idx + 1);
3004 symbol_ptr->SetSizeIsSibling(true);
3005 N_BRAC_indexes.pop_back();
3006 }
3007 type = eSymbolTypeScopeEnd;
3008 break;
3009
3010 case StabDeletedIncludeFile:
3011 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3012 type = eSymbolTypeHeaderFile;
3013 break;
3014
3015 //----------------------------------------------------------------------
3016 // COMM scopes
3017 //----------------------------------------------------------------------
3018 case StabBeginCommon:
3019 // N_BCOMM - begin common: name,,NO_SECT,0,0
3020 // We use the current number of symbols in the symbol table in lieu of
3021 // using nlist_idx in case we ever start trimming entries out
3022 type = eSymbolTypeScopeBegin;
3023 N_COMM_indexes.push_back(sym_idx);
3024 break;
3025
3026 case StabEndCommonLocal:
3027 // N_ECOML - end common (local name): 0,,n_sect,0,address
3028 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3029 // Fall through
3030
3031 case StabEndCommon:
3032 // N_ECOMM - end common: name,,n_sect,0,0
3033 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3034 // so that we can always skip the entire symbol if we need to navigate
3035 // more quickly at the source level when parsing STABS
3036 if ( !N_COMM_indexes.empty() )
3037 {
3038 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3039 symbol_ptr->SetByteSize(sym_idx + 1);
3040 symbol_ptr->SetSizeIsSibling(true);
3041 N_COMM_indexes.pop_back();
3042 }
3043 type = eSymbolTypeScopeEnd;
3044 break;
3045
3046 case StabLength:
3047 // N_LENG - second stab entry with length information
3048 type = eSymbolTypeAdditional;
3049 break;
3050
3051 default: break;
3052 }
3053 }
3054 else
3055 {
3056 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3057 uint8_t n_type = NlistMaskType & nlist.n_type;
3058 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3059
3060 switch (n_type)
3061 {
3062 case NListTypeIndirect: // N_INDR - Fall through
3063 case NListTypePreboundUndefined:// N_PBUD - Fall through
3064 case NListTypeUndefined: // N_UNDF
3065 type = eSymbolTypeUndefined;
3066 break;
3067
3068 case NListTypeAbsolute: // N_ABS
3069 type = eSymbolTypeAbsolute;
3070 break;
3071
3072 case NListTypeSection: // N_SECT
3073 {
3074 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3075
Sean Callananb386d822012-08-09 00:50:26 +00003076 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003077 {
3078 // TODO: warn about this?
3079 add_nlist = false;
3080 break;
3081 }
3082
3083 if (TEXT_eh_frame_sectID == nlist.n_sect)
3084 {
3085 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00003086 }
3087 else
3088 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003089 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3090
3091 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00003092 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003093 case SectionTypeRegular: break; // regular section
3094 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3095 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3096 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3097 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3098 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3099 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3100 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3101 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3102 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3103 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3104 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3105 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3106 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3107 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3108 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3109 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3110 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00003111 }
Chris Lattner24943d22010-06-08 16:52:24 +00003112
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003113 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00003114 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003115 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3116 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003117 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003118 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3119 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003120 SectionAttrSytemSomeInstructions))
3121 type = eSymbolTypeData;
3122 else
3123 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00003124 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003125 else
3126 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003127 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003128 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00003129 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003130 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00003131
Jason Molenda9badb6c2013-03-06 23:19:17 +00003132 if (symbol_name &&
3133 symbol_name[0] == '_' &&
3134 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003135 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00003136 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003137 llvm::StringRef symbol_name_ref(symbol_name);
3138 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3139 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3140 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3141 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00003142 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003143 symbol_name_non_abi_mangled = symbol_name + 1;
3144 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3145 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003146 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003147 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003148 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003149 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003150 symbol_name_non_abi_mangled = symbol_name + 1;
3151 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3152 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003153 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003154 }
3155 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3156 {
3157 symbol_name_non_abi_mangled = symbol_name + 1;
3158 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3159 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003160 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003161 }
3162 }
3163 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003164 else
3165 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3166 {
3167 type = eSymbolTypeException;
3168 }
3169 else
3170 {
3171 type = eSymbolTypeData;
3172 }
3173 }
3174 else
3175 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3176 {
3177 type = eSymbolTypeTrampoline;
3178 }
3179 else
3180 if (symbol_section->IsDescendant(objc_section_sp.get()))
3181 {
3182 type = eSymbolTypeRuntime;
3183 if (symbol_name && symbol_name[0] == '.')
3184 {
3185 llvm::StringRef symbol_name_ref(symbol_name);
3186 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3187 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3188 {
3189 symbol_name_non_abi_mangled = symbol_name;
3190 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3191 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003192 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003193 }
3194 }
Chris Lattner24943d22010-06-08 16:52:24 +00003195 }
3196 }
3197 }
3198 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003199 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003200 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003201 }
3202
3203 if (add_nlist)
3204 {
3205 uint64_t symbol_value = nlist.n_value;
3206 bool symbol_name_is_mangled = false;
3207
3208 if (symbol_name_non_abi_mangled)
3209 {
Greg Claytonc0240042012-07-18 23:18:10 +00003210 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3211 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003212 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003213 else
3214 {
3215 if (symbol_name && symbol_name[0] == '_')
3216 {
3217 symbol_name_is_mangled = symbol_name[1] == '_';
3218 symbol_name++; // Skip the leading underscore
3219 }
3220
3221 if (symbol_name)
3222 {
Greg Claytonc0240042012-07-18 23:18:10 +00003223 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003224 }
3225 }
3226
3227 if (is_debug == false)
3228 {
3229 if (type == eSymbolTypeCode)
3230 {
3231 // See if we can find a N_FUN entry for any code symbols.
3232 // If we do find a match, and the name matches, then we
3233 // can merge the two into just the function symbol to avoid
3234 // duplicate entries in the symbol table
3235 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3236 if (pos != N_FUN_addr_to_sym_idx.end())
3237 {
3238 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3239 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3240 {
3241 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3242 // We just need the flags from the linker symbol, so put these flags
3243 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3244 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3245 sym[sym_idx].Clear();
3246 continue;
3247 }
3248 }
3249 }
3250 else if (type == eSymbolTypeData)
3251 {
3252 // See if we can find a N_STSYM entry for any data symbols.
3253 // If we do find a match, and the name matches, then we
3254 // can merge the two into just the Static symbol to avoid
3255 // duplicate entries in the symbol table
3256 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3257 if (pos != N_STSYM_addr_to_sym_idx.end())
3258 {
3259 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3260 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3261 {
3262 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3263 // We just need the flags from the linker symbol, so put these flags
3264 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3265 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3266 sym[sym_idx].Clear();
3267 continue;
3268 }
3269 }
3270 }
3271 }
3272 if (symbol_section)
3273 {
3274 const addr_t section_file_addr = symbol_section->GetFileAddress();
3275 if (symbol_byte_size == 0 && function_starts_count > 0)
3276 {
Greg Claytond2653c22012-03-14 01:53:24 +00003277 addr_t symbol_lookup_file_addr = nlist.n_value;
3278 // Do an exact address match for non-ARM addresses, else get the closest since
3279 // the symbol might be a thumb symbol which has an address with bit zero set
3280 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3281 if (is_arm && func_start_entry)
3282 {
3283 // Verify that the function start address is the symbol address (ARM)
3284 // or the symbol address + 1 (thumb)
3285 if (func_start_entry->addr != symbol_lookup_file_addr &&
3286 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3287 {
3288 // Not the right entry, NULL it out...
3289 func_start_entry = NULL;
3290 }
3291 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003292 if (func_start_entry)
3293 {
3294 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003295
Greg Claytond2653c22012-03-14 01:53:24 +00003296 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003297 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003298 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003299
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003300 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3301 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3302 if (next_func_start_entry)
3303 {
Greg Claytond2653c22012-03-14 01:53:24 +00003304 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3305 // Be sure the clear the Thumb address bit when we calculate the size
3306 // from the current and next address
3307 if (is_arm)
3308 next_symbol_file_addr &= 0xfffffffffffffffeull;
3309 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 +00003310 }
3311 else
3312 {
Greg Claytond2653c22012-03-14 01:53:24 +00003313 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003314 }
3315 }
3316 }
3317 symbol_value -= section_file_addr;
3318 }
3319
3320 sym[sym_idx].SetID (nlist_idx);
3321 sym[sym_idx].SetType (type);
3322 sym[sym_idx].GetAddress().SetSection (symbol_section);
3323 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3324 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3325
3326 if (symbol_byte_size > 0)
3327 sym[sym_idx].SetByteSize(symbol_byte_size);
3328
Greg Clayton01e6a582012-11-27 01:52:16 +00003329 if (demangled_is_synthesized)
3330 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3331
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003332 ++sym_idx;
3333 }
3334 else
3335 {
3336 sym[sym_idx].Clear();
3337 }
3338
3339 }
3340
3341 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3342 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3343 // such entries by figuring out what the address for the global is by looking up this non-STAB
3344 // entry and copying the value into the debug symbol's value to save us the hassle in the
3345 // debug symbol parser.
3346
3347 Symbol *global_symbol = NULL;
3348 for (nlist_idx = 0;
3349 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3350 nlist_idx++)
3351 {
3352 if (global_symbol->GetAddress().GetFileAddress() == 0)
3353 {
3354 std::vector<uint32_t> indexes;
3355 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3356 {
3357 std::vector<uint32_t>::const_iterator pos;
3358 std::vector<uint32_t>::const_iterator end = indexes.end();
3359 for (pos = indexes.begin(); pos != end; ++pos)
3360 {
3361 symbol_ptr = symtab->SymbolAtIndex(*pos);
3362 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3363 {
3364 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3365 break;
3366 }
3367 }
3368 }
Chris Lattner24943d22010-06-08 16:52:24 +00003369 }
3370 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003371
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003372 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3373
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003374 if (function_starts_count > 0)
3375 {
3376 char synthetic_function_symbol[PATH_MAX];
3377 uint32_t num_synthetic_function_symbols = 0;
3378 for (i=0; i<function_starts_count; ++i)
3379 {
3380 if (function_starts.GetEntryRef (i).data == false)
3381 ++num_synthetic_function_symbols;
3382 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003383
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003384 if (num_synthetic_function_symbols > 0)
3385 {
3386 if (num_syms < sym_idx + num_synthetic_function_symbols)
3387 {
3388 num_syms = sym_idx + num_synthetic_function_symbols;
3389 sym = symtab->Resize (num_syms);
3390 }
3391 uint32_t synthetic_function_symbol_idx = 0;
3392 for (i=0; i<function_starts_count; ++i)
3393 {
3394 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3395 if (func_start_entry->data == false)
3396 {
Greg Claytond2653c22012-03-14 01:53:24 +00003397 addr_t symbol_file_addr = func_start_entry->addr;
3398 uint32_t symbol_flags = 0;
3399 if (is_arm)
3400 {
3401 if (symbol_file_addr & 1)
3402 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3403 symbol_file_addr &= 0xfffffffffffffffeull;
3404 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003405 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003406 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003407 {
3408 SectionSP symbol_section (symbol_addr.GetSection());
3409 uint32_t symbol_byte_size = 0;
3410 if (symbol_section)
3411 {
3412 const addr_t section_file_addr = symbol_section->GetFileAddress();
3413 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3414 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3415 if (next_func_start_entry)
3416 {
Greg Claytond2653c22012-03-14 01:53:24 +00003417 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3418 if (is_arm)
3419 next_symbol_file_addr &= 0xfffffffffffffffeull;
3420 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 +00003421 }
3422 else
3423 {
Greg Claytond2653c22012-03-14 01:53:24 +00003424 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003425 }
3426 snprintf (synthetic_function_symbol,
3427 sizeof(synthetic_function_symbol),
3428 "___lldb_unnamed_function%u$$%s",
3429 ++synthetic_function_symbol_idx,
3430 module_sp->GetFileSpec().GetFilename().GetCString());
3431 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003432 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003433 sym[sym_idx].SetType (eSymbolTypeCode);
3434 sym[sym_idx].SetIsSynthetic (true);
3435 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003436 if (symbol_flags)
3437 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003438 if (symbol_byte_size)
3439 sym[sym_idx].SetByteSize (symbol_byte_size);
3440 ++sym_idx;
3441 }
3442 }
3443 }
3444 }
3445 }
3446 }
3447
3448 // Trim our symbols down to just what we ended up with after
3449 // removing any symbols.
3450 if (sym_idx < num_syms)
3451 {
3452 num_syms = sym_idx;
3453 sym = symtab->Resize (num_syms);
3454 }
3455
3456 // Now synthesize indirect symbols
3457 if (m_dysymtab.nindirectsyms != 0)
3458 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003459 if (indirect_symbol_index_data.GetByteSize())
3460 {
3461 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3462
3463 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3464 {
3465 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3466 {
3467 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3468 if (symbol_stub_byte_size == 0)
3469 continue;
3470
3471 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3472
3473 if (num_symbol_stubs == 0)
3474 continue;
3475
3476 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3477 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3478 {
3479 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3480 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 +00003481 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003482 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3483 {
3484 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3485 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3486 continue;
3487
3488 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3489 Symbol *stub_symbol = NULL;
3490 if (index_pos != end_index_pos)
3491 {
3492 // We have a remapping from the original nlist index to
3493 // a current symbol index, so just look this up by index
3494 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3495 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003496 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003497 {
3498 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003499 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003500 // S_SYMBOL_STUBS
3501 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3502 }
3503
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003504 if (stub_symbol)
3505 {
3506 Address so_addr(symbol_stub_addr, section_list);
3507
3508 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3509 {
3510 // Change the external symbol into a trampoline that makes sense
3511 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3512 // can re-use them so we don't have to make up a synthetic symbol
3513 // for no good reason.
3514 stub_symbol->SetType (eSymbolTypeTrampoline);
3515 stub_symbol->SetExternal (false);
3516 stub_symbol->GetAddress() = so_addr;
3517 stub_symbol->SetByteSize (symbol_stub_byte_size);
3518 }
3519 else
3520 {
3521 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003522 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003523 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003524 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003525 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003526 stub_symbol = NULL; // this pointer no longer valid
3527 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003528 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003529 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003530 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3531 sym[sym_idx].SetIsSynthetic (true);
3532 sym[sym_idx].GetAddress() = so_addr;
3533 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3534 ++sym_idx;
3535 }
3536 }
Greg Claytond4330e62012-09-05 01:38:55 +00003537 else
3538 {
3539 if (log)
3540 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3541 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003542 }
3543 }
3544 }
3545 }
3546 }
3547 }
3548 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003549 }
3550 return 0;
3551}
3552
3553
3554void
3555ObjectFileMachO::Dump (Stream *s)
3556{
Greg Clayton9482f052012-03-13 23:14:29 +00003557 ModuleSP module_sp(GetModule());
3558 if (module_sp)
3559 {
3560 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3561 s->Printf("%p: ", this);
3562 s->Indent();
3563 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3564 s->PutCString("ObjectFileMachO64");
3565 else
3566 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003567
Greg Clayton9482f052012-03-13 23:14:29 +00003568 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003569
Greg Clayton9482f052012-03-13 23:14:29 +00003570 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003571
Greg Clayton9482f052012-03-13 23:14:29 +00003572 if (m_sections_ap.get())
3573 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003574
Greg Clayton9482f052012-03-13 23:14:29 +00003575 if (m_symtab_ap.get())
3576 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3577 }
Chris Lattner24943d22010-06-08 16:52:24 +00003578}
3579
Greg Clayton36b877d2013-04-24 22:29:28 +00003580bool
3581ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3582 const lldb_private::DataExtractor &data,
3583 lldb::offset_t lc_offset,
3584 lldb_private::UUID& uuid)
3585{
3586 uint32_t i;
3587 struct uuid_command load_cmd;
3588
3589 lldb::offset_t offset = lc_offset;
3590 for (i=0; i<header.ncmds; ++i)
3591 {
3592 const lldb::offset_t cmd_offset = offset;
3593 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3594 break;
3595
3596 if (load_cmd.cmd == LoadCommandUUID)
3597 {
3598 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3599
3600 if (uuid_bytes)
3601 {
3602 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3603 // We pretend these object files have no UUID to prevent crashing.
3604
3605 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3606 0x3b, 0xa8,
3607 0x4b, 0x16,
3608 0xb6, 0xa4,
3609 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3610
3611 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3612 return false;
3613
3614 uuid.SetBytes (uuid_bytes);
3615 return true;
3616 }
3617 return false;
3618 }
3619 offset = cmd_offset + load_cmd.cmdsize;
3620 }
3621 return false;
3622}
Chris Lattner24943d22010-06-08 16:52:24 +00003623
3624bool
Greg Clayton0467c782011-02-04 18:53:10 +00003625ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003626{
Greg Clayton9482f052012-03-13 23:14:29 +00003627 ModuleSP module_sp(GetModule());
3628 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003629 {
Greg Clayton9482f052012-03-13 23:14:29 +00003630 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00003631 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton36b877d2013-04-24 22:29:28 +00003632 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner24943d22010-06-08 16:52:24 +00003633 }
3634 return false;
3635}
3636
3637
3638uint32_t
3639ObjectFileMachO::GetDependentModules (FileSpecList& files)
3640{
Chris Lattner24943d22010-06-08 16:52:24 +00003641 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003642 ModuleSP module_sp(GetModule());
3643 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003644 {
Greg Clayton9482f052012-03-13 23:14:29 +00003645 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3646 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003647 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003648 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3649 uint32_t i;
3650 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003651 {
Greg Clayton9482f052012-03-13 23:14:29 +00003652 const uint32_t cmd_offset = offset;
3653 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3654 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003655
Greg Clayton9482f052012-03-13 23:14:29 +00003656 switch (load_cmd.cmd)
3657 {
3658 case LoadCommandDylibLoad:
3659 case LoadCommandDylibLoadWeak:
3660 case LoadCommandDylibReexport:
3661 case LoadCommandDynamicLinkerLoad:
3662 case LoadCommandFixedVMShlibLoad:
3663 case LoadCommandDylibLoadUpward:
3664 {
3665 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3666 const char *path = m_data.PeekCStr(name_offset);
3667 // Skip any path that starts with '@' since these are usually:
3668 // @executable_path/.../file
3669 // @rpath/.../file
3670 if (path && path[0] != '@')
3671 {
3672 FileSpec file_spec(path, resolve_path);
3673 if (files.AppendIfUnique(file_spec))
3674 count++;
3675 }
3676 }
3677 break;
3678
3679 default:
3680 break;
3681 }
3682 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003683 }
Chris Lattner24943d22010-06-08 16:52:24 +00003684 }
3685 return count;
3686}
3687
Jim Ingham28775942011-03-07 23:44:08 +00003688lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003689ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003690{
3691 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3692 // is initialized to an invalid address, so we can just return that.
3693 // 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 +00003694
Jim Ingham28775942011-03-07 23:44:08 +00003695 if (!IsExecutable() || m_entry_point_address.IsValid())
3696 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003697
3698 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003699 // /usr/include/mach-o.h, but it is basically:
3700 //
3701 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3702 // uint32_t count - this is the count of longs in the thread state data
3703 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3704 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003705 //
Jim Ingham28775942011-03-07 23:44:08 +00003706 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3707 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3708 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3709 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3710 //
3711 // For now we hard-code the offsets and flavors we need:
3712 //
3713 //
3714
Greg Clayton9482f052012-03-13 23:14:29 +00003715 ModuleSP module_sp(GetModule());
3716 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003717 {
Greg Clayton9482f052012-03-13 23:14:29 +00003718 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3719 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003720 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003721 uint32_t i;
3722 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3723 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003724
Greg Clayton9482f052012-03-13 23:14:29 +00003725 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003726 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003727 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003728 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3729 break;
3730
3731 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003732 {
Greg Clayton9482f052012-03-13 23:14:29 +00003733 case LoadCommandUnixThread:
3734 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003735 {
Greg Clayton9482f052012-03-13 23:14:29 +00003736 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003737 {
Greg Clayton9482f052012-03-13 23:14:29 +00003738 uint32_t flavor = m_data.GetU32(&offset);
3739 uint32_t count = m_data.GetU32(&offset);
3740 if (count == 0)
3741 {
3742 // We've gotten off somehow, log and exit;
3743 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003744 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003745
Greg Clayton9482f052012-03-13 23:14:29 +00003746 switch (m_header.cputype)
3747 {
3748 case llvm::MachO::CPUTypeARM:
3749 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3750 {
3751 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3752 start_address = m_data.GetU32(&offset);
3753 done = true;
3754 }
Jim Ingham28775942011-03-07 23:44:08 +00003755 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003756 case llvm::MachO::CPUTypeI386:
3757 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3758 {
3759 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3760 start_address = m_data.GetU32(&offset);
3761 done = true;
3762 }
3763 break;
3764 case llvm::MachO::CPUTypeX86_64:
3765 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3766 {
3767 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3768 start_address = m_data.GetU64(&offset);
3769 done = true;
3770 }
3771 break;
3772 default:
3773 return m_entry_point_address;
3774 }
3775 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3776 if (done)
3777 break;
3778 offset += count * 4;
3779 }
Jim Ingham28775942011-03-07 23:44:08 +00003780 }
Greg Clayton9482f052012-03-13 23:14:29 +00003781 break;
3782 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003783 {
Greg Clayton9482f052012-03-13 23:14:29 +00003784 ConstString text_segment_name ("__TEXT");
3785 uint64_t entryoffset = m_data.GetU64(&offset);
3786 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3787 if (text_segment_sp)
3788 {
3789 done = true;
3790 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3791 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003792 }
Greg Clayton9482f052012-03-13 23:14:29 +00003793
3794 default:
3795 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003796 }
Greg Clayton9482f052012-03-13 23:14:29 +00003797 if (done)
3798 break;
Jim Ingham28775942011-03-07 23:44:08 +00003799
Greg Clayton9482f052012-03-13 23:14:29 +00003800 // Go to the next load command:
3801 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003802 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003803
Greg Clayton9482f052012-03-13 23:14:29 +00003804 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003805 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003806 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003807 // of this ObjectFile:
3808 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003809 {
Greg Clayton9482f052012-03-13 23:14:29 +00003810 m_entry_point_address.Clear();
3811 }
3812 }
3813 else
3814 {
3815 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3816 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003817
Greg Clayton9482f052012-03-13 23:14:29 +00003818 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003819
Greg Clayton9482f052012-03-13 23:14:29 +00003820 if (module_sp)
3821 {
3822 SymbolContextList contexts;
3823 SymbolContext context;
3824 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3825 {
3826 if (contexts.GetContextAtIndex(0, context))
3827 m_entry_point_address = context.symbol->GetAddress();
3828 }
Greg Clayton3508c382012-02-24 01:59:29 +00003829 }
3830 }
Jim Ingham28775942011-03-07 23:44:08 +00003831 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003832
Jim Ingham28775942011-03-07 23:44:08 +00003833 return m_entry_point_address;
3834
3835}
3836
Greg Claytonb5a8f142012-02-05 02:38:54 +00003837lldb_private::Address
3838ObjectFileMachO::GetHeaderAddress ()
3839{
3840 lldb_private::Address header_addr;
3841 SectionList *section_list = GetSectionList();
3842 if (section_list)
3843 {
3844 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3845 if (text_segment_sp)
3846 {
Greg Clayton3508c382012-02-24 01:59:29 +00003847 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003848 header_addr.SetOffset (0);
3849 }
3850 }
3851 return header_addr;
3852}
3853
Greg Clayton46c9a352012-02-09 06:16:32 +00003854uint32_t
3855ObjectFileMachO::GetNumThreadContexts ()
3856{
Greg Clayton9482f052012-03-13 23:14:29 +00003857 ModuleSP module_sp(GetModule());
3858 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003859 {
Greg Clayton9482f052012-03-13 23:14:29 +00003860 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3861 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003862 {
Greg Clayton9482f052012-03-13 23:14:29 +00003863 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003864 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003865 FileRangeArray::Entry file_range;
3866 thread_command thread_cmd;
3867 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003868 {
Greg Clayton9482f052012-03-13 23:14:29 +00003869 const uint32_t cmd_offset = offset;
3870 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3871 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003872
Greg Clayton9482f052012-03-13 23:14:29 +00003873 if (thread_cmd.cmd == LoadCommandThread)
3874 {
3875 file_range.SetRangeBase (offset);
3876 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3877 m_thread_context_offsets.Append (file_range);
3878 }
3879 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003880 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003881 }
3882 }
3883 return m_thread_context_offsets.GetSize();
3884}
3885
3886lldb::RegisterContextSP
3887ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3888{
Greg Clayton46c9a352012-02-09 06:16:32 +00003889 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003890
Greg Clayton9482f052012-03-13 23:14:29 +00003891 ModuleSP module_sp(GetModule());
3892 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003893 {
Greg Clayton9482f052012-03-13 23:14:29 +00003894 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3895 if (!m_thread_context_offsets_valid)
3896 GetNumThreadContexts ();
3897
3898 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003899 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003900 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003901
3902 DataExtractor data (m_data,
3903 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003904 thread_context_file_range->GetByteSize());
3905
3906 switch (m_header.cputype)
3907 {
3908 case llvm::MachO::CPUTypeARM:
3909 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3910 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003911
Jim Ingham6f01c932012-10-12 17:34:26 +00003912 case llvm::MachO::CPUTypeI386:
3913 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3914 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003915
Jim Ingham6f01c932012-10-12 17:34:26 +00003916 case llvm::MachO::CPUTypeX86_64:
3917 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3918 break;
3919 }
Greg Clayton9482f052012-03-13 23:14:29 +00003920 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003921 }
3922 return reg_ctx_sp;
3923}
3924
Greg Claytonb5a8f142012-02-05 02:38:54 +00003925
Greg Claytonca319972011-07-09 00:41:34 +00003926ObjectFile::Type
3927ObjectFileMachO::CalculateType()
3928{
3929 switch (m_header.filetype)
3930 {
3931 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3932 if (GetAddressByteSize () == 4)
3933 {
3934 // 32 bit kexts are just object files, but they do have a valid
3935 // UUID load command.
3936 UUID uuid;
3937 if (GetUUID(&uuid))
3938 {
3939 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003940 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003941 // "OSKextGetCurrentIdentifier" as this is required of kexts
3942 if (m_strata == eStrataInvalid)
3943 m_strata = eStrataKernel;
3944 return eTypeSharedLibrary;
3945 }
3946 }
3947 return eTypeObjectFile;
3948
3949 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3950 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3951 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3952 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3953 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3954 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3955 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3956 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3957 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3958 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3959 default:
3960 break;
3961 }
3962 return eTypeUnknown;
3963}
3964
3965ObjectFile::Strata
3966ObjectFileMachO::CalculateStrata()
3967{
3968 switch (m_header.filetype)
3969 {
3970 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3971 {
3972 // 32 bit kexts are just object files, but they do have a valid
3973 // UUID load command.
3974 UUID uuid;
3975 if (GetUUID(&uuid))
3976 {
3977 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003978 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003979 // "OSKextGetCurrentIdentifier" as this is required of kexts
3980 if (m_type == eTypeInvalid)
3981 m_type = eTypeSharedLibrary;
3982
3983 return eStrataKernel;
3984 }
3985 }
3986 return eStrataUnknown;
3987
3988 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3989 // Check for the MH_DYLDLINK bit in the flags
3990 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003991 {
Greg Claytonca319972011-07-09 00:41:34 +00003992 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003993 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003994 else
Sean Callananac725af2012-02-10 20:22:35 +00003995 {
3996 SectionList *section_list = GetSectionList();
3997 if (section_list)
3998 {
3999 static ConstString g_kld_section_name ("__KLD");
4000 if (section_list->FindSectionByName(g_kld_section_name))
4001 return eStrataKernel;
4002 }
4003 }
4004 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00004005
4006 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4007 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00004008 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00004009 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4010 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4011 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4012 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4013 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4014 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4015 default:
4016 break;
4017 }
4018 return eStrataUnknown;
4019}
4020
4021
Greg Clayton49f4bf22012-02-22 19:41:02 +00004022uint32_t
4023ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4024{
Greg Clayton9482f052012-03-13 23:14:29 +00004025 ModuleSP module_sp(GetModule());
4026 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004027 {
Greg Clayton9482f052012-03-13 23:14:29 +00004028 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4029 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00004030 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00004031 uint32_t version_cmd = 0;
4032 uint64_t version = 0;
4033 uint32_t i;
4034 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004035 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00004036 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00004037 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4038 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00004039
Greg Clayton9482f052012-03-13 23:14:29 +00004040 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004041 {
Greg Clayton9482f052012-03-13 23:14:29 +00004042 if (version_cmd == 0)
4043 {
4044 version_cmd = load_cmd.cmd;
4045 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4046 break;
4047 version = load_cmd.dylib.current_version;
4048 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004049 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00004050 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00004051 }
Greg Clayton9482f052012-03-13 23:14:29 +00004052 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004053 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004054
Greg Clayton9482f052012-03-13 23:14:29 +00004055 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004056 {
Greg Clayton9482f052012-03-13 23:14:29 +00004057 if (versions != NULL && num_versions > 0)
4058 {
4059 if (num_versions > 0)
4060 versions[0] = (version & 0xFFFF0000ull) >> 16;
4061 if (num_versions > 1)
4062 versions[1] = (version & 0x0000FF00ull) >> 8;
4063 if (num_versions > 2)
4064 versions[2] = (version & 0x000000FFull);
4065 // Fill in an remaining version numbers with invalid values
4066 for (i=3; i<num_versions; ++i)
4067 versions[i] = UINT32_MAX;
4068 }
4069 // The LC_ID_DYLIB load command has a version with 3 version numbers
4070 // in it, so always return 3
4071 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004072 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00004073 }
4074 return false;
4075}
4076
Chris Lattner24943d22010-06-08 16:52:24 +00004077bool
Greg Clayton395fc332011-02-15 21:59:32 +00004078ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00004079{
Greg Clayton9482f052012-03-13 23:14:29 +00004080 ModuleSP module_sp(GetModule());
4081 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004082 {
Greg Clayton9482f052012-03-13 23:14:29 +00004083 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4084 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00004085
Greg Clayton9482f052012-03-13 23:14:29 +00004086 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00004087 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00004088 // unknown to make sure we use the DynamicLoaderStatic()...
4089 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4090 {
4091 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4092 }
4093 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004094 }
Greg Clayton9482f052012-03-13 23:14:29 +00004095 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00004096}
4097
4098
Jason Molenda45c75502013-04-16 06:24:42 +00004099UUID
4100ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4101{
4102 UUID uuid;
4103 if (process)
4104 {
4105 addr_t all_image_infos = process->GetImageInfoAddress();
4106
4107 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4108 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4109 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4110 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4111
4112 Error err;
4113 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4114 if (version_or_magic != -1
4115 && version_or_magic != HeaderMagic32
4116 && version_or_magic != HeaderMagic32Swapped
4117 && version_or_magic != HeaderMagic64
4118 && version_or_magic != HeaderMagic64Swapped
4119 && version_or_magic >= 13)
4120 {
4121 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4122 int wordsize = process->GetAddressByteSize();
4123 if (wordsize == 8)
4124 {
4125 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4126 }
4127 if (wordsize == 4)
4128 {
4129 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4130 }
4131 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4132 {
4133 uuid_t shared_cache_uuid;
4134 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4135 {
4136 uuid.SetBytes (shared_cache_uuid);
4137 }
4138 }
4139 }
4140 }
4141 return uuid;
4142}
4143
4144UUID
4145ObjectFileMachO::GetLLDBSharedCacheUUID ()
4146{
4147 UUID uuid;
4148#if defined (__APPLE__) && defined (__arm__)
4149 uint8_t *(*dyld_get_all_image_infos)(void);
4150 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4151 if (dyld_get_all_image_infos)
4152 {
4153 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4154 if (dyld_all_image_infos_address)
4155 {
Jason Molenda6ff916f2013-04-16 21:42:58 +00004156 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4157 if (*version >= 13)
Jason Molenda45c75502013-04-16 06:24:42 +00004158 {
Jason Molenda2ceae992013-04-16 22:56:17 +00004159 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 +00004160 uuid.SetBytes (sharedCacheUUID_address);
4161 }
4162 }
4163 }
4164#endif
4165 return uuid;
4166}
4167
4168
Chris Lattner24943d22010-06-08 16:52:24 +00004169//------------------------------------------------------------------
4170// PluginInterface protocol
4171//------------------------------------------------------------------
4172const char *
4173ObjectFileMachO::GetPluginName()
4174{
4175 return "ObjectFileMachO";
4176}
4177
4178const char *
4179ObjectFileMachO::GetShortPluginName()
4180{
4181 return GetPluginNameStatic();
4182}
4183
4184uint32_t
4185ObjectFileMachO::GetPluginVersion()
4186{
4187 return 1;
4188}
4189