blob: 9ca6d7bc048956eef513ef4afe8888a846b0b1e1 [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"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/FileSpecList.h"
Greg Claytond4330e62012-09-05 01:38:55 +000019#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Core/Module.h"
21#include "lldb/Core/PluginManager.h"
Greg Clayton6f7f8da2012-04-24 03:06:13 +000022#include "lldb/Core/RangeMap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/Section.h"
24#include "lldb/Core/StreamFile.h"
25#include "lldb/Core/StreamString.h"
26#include "lldb/Core/Timer.h"
27#include "lldb/Core/UUID.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000028#include "lldb/Host/Host.h"
29#include "lldb/Host/FileSpec.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000030#include "lldb/Symbol/ClangNamespaceDecl.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Symbol/ObjectFile.h"
Greg Clayton29021d32012-04-18 05:19:20 +000032#include "lldb/Target/Platform.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000033#include "lldb/Target/Process.h"
Greg Clayton29021d32012-04-18 05:19:20 +000034#include "lldb/Target/Target.h"
Greg Clayton9ce95382012-02-13 23:10:39 +000035#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
36#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Clayton46c9a352012-02-09 06:16:32 +000037#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner24943d22010-06-08 16:52:24 +000038
Chris Lattner24943d22010-06-08 16:52:24 +000039using namespace lldb;
40using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000041using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000042
Greg Clayton46c9a352012-02-09 06:16:32 +000043class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
44{
45public:
46 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
47 RegisterContextDarwin_x86_64 (thread, 0)
48 {
49 SetRegisterDataFrom_LC_THREAD (data);
50 }
51
52 virtual void
53 InvalidateAllRegisters ()
54 {
55 // Do nothing... registers are always valid...
56 }
57
58 void
59 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
60 {
Greg Clayton36da2aa2013-01-25 18:06:21 +000061 lldb::offset_t offset = 0;
Greg Clayton46c9a352012-02-09 06:16:32 +000062 SetError (GPRRegSet, Read, -1);
63 SetError (FPURegSet, Read, -1);
64 SetError (EXCRegSet, Read, -1);
Greg Clayton9ce95382012-02-13 23:10:39 +000065 bool done = false;
66
67 while (!done)
Greg Clayton46c9a352012-02-09 06:16:32 +000068 {
Greg Clayton9ce95382012-02-13 23:10:39 +000069 int flavor = data.GetU32 (&offset);
70 if (flavor == 0)
71 done = true;
72 else
Greg Clayton46c9a352012-02-09 06:16:32 +000073 {
Greg Clayton9ce95382012-02-13 23:10:39 +000074 uint32_t i;
75 uint32_t count = data.GetU32 (&offset);
76 switch (flavor)
77 {
78 case GPRRegSet:
79 for (i=0; i<count; ++i)
80 (&gpr.rax)[i] = data.GetU64(&offset);
81 SetError (GPRRegSet, Read, 0);
82 done = true;
83
84 break;
85 case FPURegSet:
86 // TODO: fill in FPU regs....
87 //SetError (FPURegSet, Read, -1);
88 done = true;
89
90 break;
91 case EXCRegSet:
92 exc.trapno = data.GetU32(&offset);
93 exc.err = data.GetU32(&offset);
94 exc.faultvaddr = data.GetU64(&offset);
95 SetError (EXCRegSet, Read, 0);
96 done = true;
97 break;
98 case 7:
99 case 8:
100 case 9:
101 // fancy flavors that encapsulate of the the above
102 // falvors...
103 break;
104
105 default:
106 done = true;
107 break;
108 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000109 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000110 }
111 }
112protected:
113 virtual int
114 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
115 {
116 return 0;
117 }
118
119 virtual int
120 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
121 {
122 return 0;
123 }
124
125 virtual int
126 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
127 {
128 return 0;
129 }
130
131 virtual int
132 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
133 {
134 return 0;
135 }
136
137 virtual int
138 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
139 {
140 return 0;
141 }
142
143 virtual int
144 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
145 {
146 return 0;
147 }
148};
Greg Clayton46c9a352012-02-09 06:16:32 +0000149
Greg Clayton9ce95382012-02-13 23:10:39 +0000150
151class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
152{
153public:
154 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
155 RegisterContextDarwin_i386 (thread, 0)
156 {
157 SetRegisterDataFrom_LC_THREAD (data);
158 }
159
160 virtual void
161 InvalidateAllRegisters ()
162 {
163 // Do nothing... registers are always valid...
164 }
165
166 void
167 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
168 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000169 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000170 SetError (GPRRegSet, Read, -1);
171 SetError (FPURegSet, Read, -1);
172 SetError (EXCRegSet, Read, -1);
173 bool done = false;
174
175 while (!done)
176 {
177 int flavor = data.GetU32 (&offset);
178 if (flavor == 0)
179 done = true;
180 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000181 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000182 uint32_t i;
183 uint32_t count = data.GetU32 (&offset);
184 switch (flavor)
185 {
186 case GPRRegSet:
187 for (i=0; i<count; ++i)
188 (&gpr.eax)[i] = data.GetU32(&offset);
189 SetError (GPRRegSet, Read, 0);
190 done = true;
191
192 break;
193 case FPURegSet:
194 // TODO: fill in FPU regs....
195 //SetError (FPURegSet, Read, -1);
196 done = true;
197
198 break;
199 case EXCRegSet:
200 exc.trapno = data.GetU32(&offset);
201 exc.err = data.GetU32(&offset);
202 exc.faultvaddr = data.GetU32(&offset);
203 SetError (EXCRegSet, Read, 0);
204 done = true;
205 break;
206 case 7:
207 case 8:
208 case 9:
209 // fancy flavors that encapsulate of the the above
210 // falvors...
211 break;
212
213 default:
214 done = true;
215 break;
216 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000217 }
218 }
219 }
220protected:
221 virtual int
222 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
223 {
224 return 0;
225 }
226
227 virtual int
228 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
229 {
230 return 0;
231 }
232
233 virtual int
234 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
235 {
236 return 0;
237 }
238
239 virtual int
240 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
241 {
242 return 0;
243 }
244
245 virtual int
246 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
247 {
248 return 0;
249 }
250
251 virtual int
252 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
253 {
254 return 0;
255 }
256};
257
Greg Clayton9ce95382012-02-13 23:10:39 +0000258class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
259{
260public:
261 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonb5431d02012-10-30 23:57:32 +0000262 RegisterContextDarwin_arm (thread, 0)
Greg Clayton9ce95382012-02-13 23:10:39 +0000263 {
264 SetRegisterDataFrom_LC_THREAD (data);
265 }
266
267 virtual void
268 InvalidateAllRegisters ()
269 {
270 // Do nothing... registers are always valid...
271 }
272
273 void
274 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
275 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000276 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000277 SetError (GPRRegSet, Read, -1);
278 SetError (FPURegSet, Read, -1);
279 SetError (EXCRegSet, Read, -1);
280 int flavor = data.GetU32 (&offset);
281 uint32_t count = data.GetU32 (&offset);
282 switch (flavor)
283 {
284 case GPRRegSet:
285 for (uint32_t i=0; i<count; ++i)
286 gpr.r[i] = data.GetU32(&offset);
287 SetError (GPRRegSet, Read, 0);
288 break;
289 case FPURegSet:
290 // TODO: fill in FPU regs....
291 //SetError (FPURegSet, Read, -1);
292 break;
293 case EXCRegSet:
294 exc.exception = data.GetU32(&offset);
295 exc.fsr = data.GetU32(&offset);
296 exc.far = data.GetU32(&offset);
297 SetError (EXCRegSet, Read, 0);
298 break;
299 }
300 }
301protected:
302 virtual int
303 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
304 {
305 return 0;
306 }
307
308 virtual int
309 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
310 {
311 return 0;
312 }
313
314 virtual int
315 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
316 {
317 return 0;
318 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000319
320 virtual int
321 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
322 {
323 return -1;
324 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000325
326 virtual int
327 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
328 {
329 return 0;
330 }
331
332 virtual int
333 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
334 {
335 return 0;
336 }
337
338 virtual int
339 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
340 {
341 return 0;
342 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000343
344 virtual int
345 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
346 {
347 return -1;
348 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000349};
350
Greg Claytonb1888f22011-03-19 01:12:21 +0000351#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000352
353void
354ObjectFileMachO::Initialize()
355{
356 PluginManager::RegisterPlugin (GetPluginNameStatic(),
357 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000358 CreateInstance,
359 CreateMemoryInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000360}
361
362void
363ObjectFileMachO::Terminate()
364{
365 PluginManager::UnregisterPlugin (CreateInstance);
366}
367
368
369const char *
370ObjectFileMachO::GetPluginNameStatic()
371{
372 return "object-file.mach-o";
373}
374
375const char *
376ObjectFileMachO::GetPluginDescriptionStatic()
377{
378 return "Mach-o object file reader (32 and 64 bit)";
379}
380
Chris Lattner24943d22010-06-08 16:52:24 +0000381ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000382ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
383 DataBufferSP& data_sp,
384 lldb::offset_t data_offset,
385 const FileSpec* file,
386 lldb::offset_t file_offset,
387 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000388{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000389 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000390 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000391 data_sp = file->MemoryMapFileContents(file_offset, length);
392 data_offset = 0;
393 }
394
395 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
396 {
397 // Update the data to contain the entire file if it doesn't already
398 if (data_sp->GetByteSize() < length)
399 {
400 data_sp = file->MemoryMapFileContents(file_offset, length);
401 data_offset = 0;
402 }
403 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, data_offset, file, file_offset, length));
Chris Lattner24943d22010-06-08 16:52:24 +0000404 if (objfile_ap.get() && objfile_ap->ParseHeader())
405 return objfile_ap.release();
406 }
407 return NULL;
408}
409
Greg Claytonb5a8f142012-02-05 02:38:54 +0000410ObjectFile *
Greg Clayton3508c382012-02-24 01:59:29 +0000411ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000412 DataBufferSP& data_sp,
413 const ProcessSP &process_sp,
414 lldb::addr_t header_addr)
415{
416 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
417 {
Greg Clayton3508c382012-02-24 01:59:29 +0000418 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000419 if (objfile_ap.get() && objfile_ap->ParseHeader())
420 return objfile_ap.release();
421 }
422 return NULL;
423}
424
425
426const ConstString &
427ObjectFileMachO::GetSegmentNameTEXT()
428{
429 static ConstString g_segment_name_TEXT ("__TEXT");
430 return g_segment_name_TEXT;
431}
432
433const ConstString &
434ObjectFileMachO::GetSegmentNameDATA()
435{
436 static ConstString g_segment_name_DATA ("__DATA");
437 return g_segment_name_DATA;
438}
439
440const ConstString &
441ObjectFileMachO::GetSegmentNameOBJC()
442{
443 static ConstString g_segment_name_OBJC ("__OBJC");
444 return g_segment_name_OBJC;
445}
446
447const ConstString &
448ObjectFileMachO::GetSegmentNameLINKEDIT()
449{
450 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
451 return g_section_name_LINKEDIT;
452}
453
454const ConstString &
455ObjectFileMachO::GetSectionNameEHFrame()
456{
457 static ConstString g_section_name_eh_frame ("__eh_frame");
458 return g_section_name_eh_frame;
459}
460
461
Chris Lattner24943d22010-06-08 16:52:24 +0000462
463static uint32_t
464MachHeaderSizeFromMagic(uint32_t magic)
465{
466 switch (magic)
467 {
Greg Clayton1674b122010-07-21 22:12:05 +0000468 case HeaderMagic32:
469 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000470 return sizeof(struct mach_header);
471
Greg Clayton1674b122010-07-21 22:12:05 +0000472 case HeaderMagic64:
473 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000474 return sizeof(struct mach_header_64);
475 break;
476
477 default:
478 break;
479 }
480 return 0;
481}
482
483
484bool
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000485ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
486 lldb::addr_t data_offset,
487 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000488{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000489 DataExtractor data;
490 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000491 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000492 uint32_t magic = data.GetU32(&offset);
493 return MachHeaderSizeFromMagic(magic) != 0;
494}
495
496
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000497ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
498 DataBufferSP& data_sp,
499 lldb::offset_t data_offset,
500 const FileSpec* file,
501 lldb::offset_t file_offset,
502 lldb::offset_t length) :
503 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Chris Lattner24943d22010-06-08 16:52:24 +0000504 m_sections_ap(),
Jim Ingham28775942011-03-07 23:44:08 +0000505 m_symtab_ap(),
Greg Clayton46c9a352012-02-09 06:16:32 +0000506 m_mach_segments(),
507 m_mach_sections(),
508 m_entry_point_address(),
509 m_thread_context_offsets(),
510 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000511{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000512 ::memset (&m_header, 0, sizeof(m_header));
513 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000514}
515
Greg Clayton3508c382012-02-24 01:59:29 +0000516ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000517 lldb::DataBufferSP& header_data_sp,
518 const lldb::ProcessSP &process_sp,
519 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000520 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000521 m_sections_ap(),
522 m_symtab_ap(),
Greg Clayton46c9a352012-02-09 06:16:32 +0000523 m_mach_segments(),
524 m_mach_sections(),
525 m_entry_point_address(),
526 m_thread_context_offsets(),
527 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000528{
529 ::memset (&m_header, 0, sizeof(m_header));
530 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
531}
Chris Lattner24943d22010-06-08 16:52:24 +0000532
533ObjectFileMachO::~ObjectFileMachO()
534{
535}
536
537
538bool
539ObjectFileMachO::ParseHeader ()
540{
Greg Clayton9482f052012-03-13 23:14:29 +0000541 ModuleSP module_sp(GetModule());
542 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000543 {
Greg Clayton9482f052012-03-13 23:14:29 +0000544 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
545 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000546 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000547 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000548 // Leave magic in the original byte order
549 m_header.magic = m_data.GetU32(&offset);
550 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000551 {
Greg Clayton9482f052012-03-13 23:14:29 +0000552 case HeaderMagic32:
553 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
554 m_data.SetAddressByteSize(4);
555 can_parse = true;
556 break;
557
558 case HeaderMagic64:
559 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
560 m_data.SetAddressByteSize(8);
561 can_parse = true;
562 break;
563
564 case HeaderMagic32Swapped:
565 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
566 m_data.SetAddressByteSize(4);
567 can_parse = true;
568 break;
569
570 case HeaderMagic64Swapped:
571 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
572 m_data.SetAddressByteSize(8);
573 can_parse = true;
574 break;
575
576 default:
577 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000578 }
Greg Clayton9482f052012-03-13 23:14:29 +0000579
580 if (can_parse)
581 {
582 m_data.GetU32(&offset, &m_header.cputype, 6);
583
584 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
585
Greg Clayton21a25432012-11-16 21:36:10 +0000586 // Check if the module has a required architecture
587 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000588 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000589 return false;
590
Greg Clayton9482f052012-03-13 23:14:29 +0000591 if (SetModulesArchitecture (mach_arch))
592 {
593 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
594 if (m_data.GetByteSize() < header_and_lc_size)
595 {
596 DataBufferSP data_sp;
597 ProcessSP process_sp (m_process_wp.lock());
598 if (process_sp)
599 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000600 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000601 }
602 else
603 {
604 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000605 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000606 if (data_sp->GetByteSize() != header_and_lc_size)
607 return false;
608 }
609 if (data_sp)
610 m_data.SetData (data_sp);
611 }
612 }
613 return true;
614 }
615 else
616 {
617 memset(&m_header, 0, sizeof(struct mach_header));
618 }
Chris Lattner24943d22010-06-08 16:52:24 +0000619 }
620 return false;
621}
622
623
624ByteOrder
625ObjectFileMachO::GetByteOrder () const
626{
Chris Lattner24943d22010-06-08 16:52:24 +0000627 return m_data.GetByteOrder ();
628}
629
Jim Ingham7508e732010-08-09 23:31:02 +0000630bool
631ObjectFileMachO::IsExecutable() const
632{
633 return m_header.filetype == HeaderFileTypeExecutable;
634}
Chris Lattner24943d22010-06-08 16:52:24 +0000635
Greg Clayton36da2aa2013-01-25 18:06:21 +0000636uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000637ObjectFileMachO::GetAddressByteSize () const
638{
Chris Lattner24943d22010-06-08 16:52:24 +0000639 return m_data.GetAddressByteSize ();
640}
641
Greg Claytonb3448432011-03-24 21:19:54 +0000642AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000643ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
644{
645 Symtab *symtab = GetSymtab();
646 if (symtab)
647 {
648 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
649 if (symbol)
650 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000651 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000652 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000653 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000654 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000655 {
Greg Clayton3508c382012-02-24 01:59:29 +0000656 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000657 switch (section_type)
658 {
659 case eSectionTypeInvalid: return eAddressClassUnknown;
660 case eSectionTypeCode:
661 if (m_header.cputype == llvm::MachO::CPUTypeARM)
662 {
663 // For ARM we have a bit in the n_desc field of the symbol
664 // that tells us ARM/Thumb which is bit 0x0008.
665 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
666 return eAddressClassCodeAlternateISA;
667 }
668 return eAddressClassCode;
669
670 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000671 case eSectionTypeData:
672 case eSectionTypeDataCString:
673 case eSectionTypeDataCStringPointers:
674 case eSectionTypeDataSymbolAddress:
675 case eSectionTypeData4:
676 case eSectionTypeData8:
677 case eSectionTypeData16:
678 case eSectionTypeDataPointers:
679 case eSectionTypeZeroFill:
680 case eSectionTypeDataObjCMessageRefs:
681 case eSectionTypeDataObjCCFStrings:
682 return eAddressClassData;
683 case eSectionTypeDebug:
684 case eSectionTypeDWARFDebugAbbrev:
685 case eSectionTypeDWARFDebugAranges:
686 case eSectionTypeDWARFDebugFrame:
687 case eSectionTypeDWARFDebugInfo:
688 case eSectionTypeDWARFDebugLine:
689 case eSectionTypeDWARFDebugLoc:
690 case eSectionTypeDWARFDebugMacInfo:
691 case eSectionTypeDWARFDebugPubNames:
692 case eSectionTypeDWARFDebugPubTypes:
693 case eSectionTypeDWARFDebugRanges:
694 case eSectionTypeDWARFDebugStr:
695 case eSectionTypeDWARFAppleNames:
696 case eSectionTypeDWARFAppleTypes:
697 case eSectionTypeDWARFAppleNamespaces:
698 case eSectionTypeDWARFAppleObjC:
699 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000700 case eSectionTypeEHFrame: return eAddressClassRuntime;
701 case eSectionTypeOther: return eAddressClassUnknown;
702 }
703 }
704 }
705
Greg Claytonb3448432011-03-24 21:19:54 +0000706 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000707 switch (symbol_type)
708 {
709 case eSymbolTypeAny: return eAddressClassUnknown;
710 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Greg Claytonb1888f22011-03-19 01:12:21 +0000711
712 case eSymbolTypeCode:
713 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000714 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000715 if (m_header.cputype == llvm::MachO::CPUTypeARM)
716 {
717 // For ARM we have a bit in the n_desc field of the symbol
718 // that tells us ARM/Thumb which is bit 0x0008.
719 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
720 return eAddressClassCodeAlternateISA;
721 }
722 return eAddressClassCode;
723
724 case eSymbolTypeData: return eAddressClassData;
725 case eSymbolTypeRuntime: return eAddressClassRuntime;
726 case eSymbolTypeException: return eAddressClassRuntime;
727 case eSymbolTypeSourceFile: return eAddressClassDebug;
728 case eSymbolTypeHeaderFile: return eAddressClassDebug;
729 case eSymbolTypeObjectFile: return eAddressClassDebug;
730 case eSymbolTypeCommonBlock: return eAddressClassDebug;
731 case eSymbolTypeBlock: return eAddressClassDebug;
732 case eSymbolTypeLocal: return eAddressClassData;
733 case eSymbolTypeParam: return eAddressClassData;
734 case eSymbolTypeVariable: return eAddressClassData;
735 case eSymbolTypeVariableType: return eAddressClassDebug;
736 case eSymbolTypeLineEntry: return eAddressClassDebug;
737 case eSymbolTypeLineHeader: return eAddressClassDebug;
738 case eSymbolTypeScopeBegin: return eAddressClassDebug;
739 case eSymbolTypeScopeEnd: return eAddressClassDebug;
740 case eSymbolTypeAdditional: return eAddressClassUnknown;
741 case eSymbolTypeCompiler: return eAddressClassDebug;
742 case eSymbolTypeInstrumentation:return eAddressClassDebug;
743 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000744 case eSymbolTypeObjCClass: return eAddressClassRuntime;
745 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
746 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000747 }
748 }
749 }
750 return eAddressClassUnknown;
751}
Chris Lattner24943d22010-06-08 16:52:24 +0000752
753Symtab *
754ObjectFileMachO::GetSymtab()
755{
Greg Clayton9482f052012-03-13 23:14:29 +0000756 ModuleSP module_sp(GetModule());
757 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000758 {
Greg Clayton9482f052012-03-13 23:14:29 +0000759 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
760 if (m_symtab_ap.get() == NULL)
761 {
762 m_symtab_ap.reset(new Symtab(this));
763 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
764 ParseSymtab (true);
765 m_symtab_ap->Finalize ();
766 }
Chris Lattner24943d22010-06-08 16:52:24 +0000767 }
768 return m_symtab_ap.get();
769}
770
771
772SectionList *
773ObjectFileMachO::GetSectionList()
774{
Greg Clayton9482f052012-03-13 23:14:29 +0000775 ModuleSP module_sp(GetModule());
776 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000777 {
Greg Clayton9482f052012-03-13 23:14:29 +0000778 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
779 if (m_sections_ap.get() == NULL)
780 {
781 m_sections_ap.reset(new SectionList());
782 ParseSections();
783 }
Chris Lattner24943d22010-06-08 16:52:24 +0000784 }
785 return m_sections_ap.get();
786}
787
788
789size_t
790ObjectFileMachO::ParseSections ()
791{
792 lldb::user_id_t segID = 0;
793 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000794 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000795 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000796 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000797 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000798 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000799 // First look up any LC_ENCRYPTION_INFO load commands
800 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
801 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000802 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000803 for (i=0; i<m_header.ncmds; ++i)
804 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000805 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000806 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000807 break;
808
Greg Clayton54e33712012-05-25 18:09:55 +0000809 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000810 {
Greg Clayton54e33712012-05-25 18:09:55 +0000811 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
812 {
813 if (encryption_cmd.cryptid != 0)
814 {
815 EncryptedFileRanges::Entry entry;
816 entry.SetRangeBase(encryption_cmd.cryptoff);
817 entry.SetByteSize(encryption_cmd.cryptsize);
818 encrypted_file_ranges.Append(entry);
819 }
820 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000821 }
Greg Clayton54e33712012-05-25 18:09:55 +0000822 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000823 }
824
825 offset = MachHeaderSizeFromMagic(m_header.magic);
826
Greg Clayton54e33712012-05-25 18:09:55 +0000827 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000828 for (i=0; i<m_header.ncmds; ++i)
829 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000830 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000831 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
832 break;
833
Greg Clayton1674b122010-07-21 22:12:05 +0000834 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000835 {
836 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
837 {
838 load_cmd.vmaddr = m_data.GetAddress(&offset);
839 load_cmd.vmsize = m_data.GetAddress(&offset);
840 load_cmd.fileoff = m_data.GetAddress(&offset);
841 load_cmd.filesize = m_data.GetAddress(&offset);
842 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
843 {
Greg Clayton68ca8232011-01-25 02:58:48 +0000844
845 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
846
Chris Lattner24943d22010-06-08 16:52:24 +0000847 // Keep a list of mach segments around in case we need to
848 // get at data that isn't stored in the abstracted Sections.
849 m_mach_segments.push_back (load_cmd);
850
Greg Clayton36da2aa2013-01-25 18:06:21 +0000851 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 +0000852 // Use a segment ID of the segment index shifted left by 8 so they
853 // never conflict with any of the sections.
854 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000855 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000856 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000857 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000858 ++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
859 segment_name, // Name of this section
860 eSectionTypeContainer, // This section is a container of other sections.
861 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
862 load_cmd.vmsize, // VM size in bytes of this section
863 load_cmd.fileoff, // Offset to the data for this section in the file
864 load_cmd.filesize, // Size in bytes of this section as found in the the file
865 load_cmd.flags)); // Flags for this section
866
Greg Clayton68ca8232011-01-25 02:58:48 +0000867 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000868 m_sections_ap->AddSection(segment_sp);
869 }
870
871 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000872 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000873 // Push a section into our mach sections for the section at
Greg Clayton6af4fad2010-10-06 01:26:32 +0000874 // index zero (NListSectionNoSection) if we don't have any
875 // mach sections yet...
876 if (m_mach_sections.empty())
877 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000878 uint32_t segment_sect_idx;
879 const lldb::user_id_t first_segment_sectID = sectID + 1;
880
881
Greg Clayton1674b122010-07-21 22:12:05 +0000882 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000883 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
884 {
885 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
886 break;
887 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
888 break;
889 sect64.addr = m_data.GetAddress(&offset);
890 sect64.size = m_data.GetAddress(&offset);
891
892 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
893 break;
894
895 // Keep a list of mach sections around in case we need to
896 // get at data that isn't stored in the abstracted Sections.
897 m_mach_sections.push_back (sect64);
898
899 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
900 if (!segment_name)
901 {
902 // We have a segment with no name so we need to conjure up
903 // segments that correspond to the section's segname if there
904 // isn't already such a section. If there is such a section,
905 // we resize the section so that it spans all sections.
906 // We also mark these sections as fake so address matches don't
907 // hit if they land in the gaps between the child sections.
908 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
909 segment_sp = m_sections_ap->FindSectionByName (segment_name);
910 if (segment_sp.get())
911 {
912 Section *segment = segment_sp.get();
913 // Grow the section size as needed.
914 const lldb::addr_t sect64_min_addr = sect64.addr;
915 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
916 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
917 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
918 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
919 if (sect64_min_addr >= curr_seg_min_addr)
920 {
921 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
922 // Only grow the section size if needed
923 if (new_seg_byte_size > curr_seg_byte_size)
924 segment->SetByteSize (new_seg_byte_size);
925 }
926 else
927 {
928 // We need to change the base address of the segment and
929 // adjust the child section offsets for all existing children.
930 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
931 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +0000932 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000933 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
934 }
Greg Clayton661825b2010-06-28 23:51:11 +0000935
936 // Grow the section size as needed.
937 if (sect64.offset)
938 {
939 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
940 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
941
942 const lldb::addr_t section_min_file_offset = sect64.offset;
943 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
944 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
945 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
946 segment->SetFileOffset (new_file_offset);
947 segment->SetFileSize (new_file_size);
948 }
Chris Lattner24943d22010-06-08 16:52:24 +0000949 }
950 else
951 {
952 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +0000953 segment_sp.reset(new Section (segment_sp, // Parent section
954 module_sp, // Module to which this section belongs
955 ++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
956 segment_name, // Name of this section
957 eSectionTypeContainer, // This section is a container of other sections.
958 sect64.addr, // File VM address == addresses as they are found in the object file
959 sect64.size, // VM size in bytes of this section
960 sect64.offset, // Offset to the data for this section in the file
961 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
962 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +0000963 segment_sp->SetIsFake(true);
964 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000965 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000966 }
967 }
968 assert (segment_sp.get());
969
Greg Clayton1674b122010-07-21 22:12:05 +0000970 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000971 static ConstString g_sect_name_objc_data ("__objc_data");
972 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
973 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
974 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
975 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
976 static ConstString g_sect_name_objc_const ("__objc_const");
977 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
978 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000979
980 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
981 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
982 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
983 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
984 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
985 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
986 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
987 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
988 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
989 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
990 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +0000991 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
992 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +0000993 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +0000994 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000995 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +0000996 static ConstString g_sect_name_DATA ("__DATA");
997 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000998
Chris Lattner24943d22010-06-08 16:52:24 +0000999 SectionType sect_type = eSectionTypeOther;
1000
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001001 if (section_name == g_sect_name_dwarf_debug_abbrev)
1002 sect_type = eSectionTypeDWARFDebugAbbrev;
1003 else if (section_name == g_sect_name_dwarf_debug_aranges)
1004 sect_type = eSectionTypeDWARFDebugAranges;
1005 else if (section_name == g_sect_name_dwarf_debug_frame)
1006 sect_type = eSectionTypeDWARFDebugFrame;
1007 else if (section_name == g_sect_name_dwarf_debug_info)
1008 sect_type = eSectionTypeDWARFDebugInfo;
1009 else if (section_name == g_sect_name_dwarf_debug_line)
1010 sect_type = eSectionTypeDWARFDebugLine;
1011 else if (section_name == g_sect_name_dwarf_debug_loc)
1012 sect_type = eSectionTypeDWARFDebugLoc;
1013 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1014 sect_type = eSectionTypeDWARFDebugMacInfo;
1015 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1016 sect_type = eSectionTypeDWARFDebugPubNames;
1017 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1018 sect_type = eSectionTypeDWARFDebugPubTypes;
1019 else if (section_name == g_sect_name_dwarf_debug_ranges)
1020 sect_type = eSectionTypeDWARFDebugRanges;
1021 else if (section_name == g_sect_name_dwarf_debug_str)
1022 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001023 else if (section_name == g_sect_name_dwarf_apple_names)
1024 sect_type = eSectionTypeDWARFAppleNames;
1025 else if (section_name == g_sect_name_dwarf_apple_types)
1026 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001027 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1028 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001029 else if (section_name == g_sect_name_dwarf_apple_objc)
1030 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001031 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001032 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001033 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001034 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001035 else if (section_name == g_sect_name_eh_frame)
1036 sect_type = eSectionTypeEHFrame;
1037 else if (section_name == g_sect_name_cfstring)
1038 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001039 else if (section_name == g_sect_name_objc_data ||
1040 section_name == g_sect_name_objc_classrefs ||
1041 section_name == g_sect_name_objc_superrefs ||
1042 section_name == g_sect_name_objc_const ||
1043 section_name == g_sect_name_objc_classlist)
1044 {
1045 sect_type = eSectionTypeDataPointers;
1046 }
Chris Lattner24943d22010-06-08 16:52:24 +00001047
1048 if (sect_type == eSectionTypeOther)
1049 {
1050 switch (mach_sect_type)
1051 {
1052 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001053 case SectionTypeRegular:
1054 if (segment_sp->GetName() == g_sect_name_TEXT)
1055 sect_type = eSectionTypeCode;
1056 else if (segment_sp->GetName() == g_sect_name_DATA)
1057 sect_type = eSectionTypeData;
1058 else
1059 sect_type = eSectionTypeOther;
1060 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001061 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1062 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1063 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1064 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1065 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1066 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1067 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1068 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1069 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1070 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1071 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1072 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1073 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1074 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1075 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1076 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001077 default: break;
1078 }
1079 }
1080
Greg Clayton3508c382012-02-24 01:59:29 +00001081 SectionSP section_sp(new Section (segment_sp,
1082 module_sp,
1083 ++sectID,
1084 section_name,
1085 sect_type,
1086 sect64.addr - segment_sp->GetFileAddress(),
1087 sect64.size,
1088 sect64.offset,
1089 sect64.offset == 0 ? 0 : sect64.size,
1090 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001091 // Set the section to be encrypted to match the segment
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001092
1093 bool section_is_encrypted = false;
1094 if (!segment_is_encrypted && load_cmd.filesize != 0)
1095 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001096
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001097 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001098 segment_sp->GetChildren().AddSection(section_sp);
1099
1100 if (segment_sp->IsFake())
1101 {
1102 segment_sp.reset();
1103 segment_name.Clear();
1104 }
1105 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001106 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001107 {
1108 if (first_segment_sectID <= sectID)
1109 {
1110 lldb::user_id_t sect_uid;
1111 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1112 {
1113 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1114 SectionSP next_section_sp;
1115 if (sect_uid + 1 <= sectID)
1116 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1117
1118 if (curr_section_sp.get())
1119 {
1120 if (curr_section_sp->GetByteSize() == 0)
1121 {
1122 if (next_section_sp.get() != NULL)
1123 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1124 else
1125 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1126 }
1127 }
1128 }
1129 }
1130 }
1131 }
1132 }
1133 }
Greg Clayton1674b122010-07-21 22:12:05 +00001134 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001135 {
1136 m_dysymtab.cmd = load_cmd.cmd;
1137 m_dysymtab.cmdsize = load_cmd.cmdsize;
1138 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1139 }
1140
1141 offset = load_cmd_offset + load_cmd.cmdsize;
1142 }
1143// if (dump_sections)
1144// {
1145// StreamFile s(stdout);
1146// m_sections_ap->Dump(&s, true);
1147// }
1148 return sectID; // Return the number of sections we registered with the module
1149}
1150
1151class MachSymtabSectionInfo
1152{
1153public:
1154
1155 MachSymtabSectionInfo (SectionList *section_list) :
1156 m_section_list (section_list),
1157 m_section_infos()
1158 {
1159 // Get the number of sections down to a depth of 1 to include
1160 // all segments and their sections, but no other sections that
1161 // may be added for debug map or
1162 m_section_infos.resize(section_list->GetNumSections(1));
1163 }
1164
1165
Greg Clayton3508c382012-02-24 01:59:29 +00001166 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001167 GetSection (uint8_t n_sect, addr_t file_addr)
1168 {
1169 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001170 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001171 if (n_sect < m_section_infos.size())
1172 {
Greg Clayton3508c382012-02-24 01:59:29 +00001173 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001174 {
Greg Clayton3508c382012-02-24 01:59:29 +00001175 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1176 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001177 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001178 {
Greg Clayton3508c382012-02-24 01:59:29 +00001179 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1180 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001181 }
1182 else
1183 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001184 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001185 }
Chris Lattner24943d22010-06-08 16:52:24 +00001186 }
1187 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001188 {
1189 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001190 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001191 }
1192 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1193 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1194 {
1195 // Symbol is in section with zero size, but has the same start
1196 // address as the section. This can happen with linker symbols
1197 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001198 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001199 }
Chris Lattner24943d22010-06-08 16:52:24 +00001200 }
Greg Clayton3508c382012-02-24 01:59:29 +00001201 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001202 }
1203
1204protected:
1205 struct SectionInfo
1206 {
1207 SectionInfo () :
1208 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001209 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001210 {
1211 }
1212
1213 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001214 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001215 };
1216 SectionList *m_section_list;
1217 std::vector<SectionInfo> m_section_infos;
1218};
1219
Chris Lattner24943d22010-06-08 16:52:24 +00001220size_t
1221ObjectFileMachO::ParseSymtab (bool minimize)
1222{
1223 Timer scoped_timer(__PRETTY_FUNCTION__,
1224 "ObjectFileMachO::ParseSymtab () module = %s",
1225 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001226 ModuleSP module_sp (GetModule());
1227 if (!module_sp)
1228 return 0;
1229
1230 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1231 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1232 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1233 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001234 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001235 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001236
Greg Clayton0fea0512011-12-30 00:32:24 +00001237 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
1238
Chris Lattner24943d22010-06-08 16:52:24 +00001239 for (i=0; i<m_header.ncmds; ++i)
1240 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001241 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001242 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001243 struct load_command lc;
1244 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001245 break;
1246 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001247 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001248 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001249 case LoadCommandSymtab:
1250 symtab_load_command.cmd = lc.cmd;
1251 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001252 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001253 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1254 return 0;
1255 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001256 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001257 if (log)
1258 module_sp->LogMessage(log.get(), "LC_SYMTAB.symoff == 0");
1259 return 0;
1260 }
1261
1262 if (symtab_load_command.stroff == 0)
1263 {
1264 if (log)
1265 module_sp->LogMessage(log.get(), "LC_SYMTAB.stroff == 0");
1266 return 0;
1267 }
1268
1269 if (symtab_load_command.nsyms == 0)
1270 {
1271 if (log)
1272 module_sp->LogMessage(log.get(), "LC_SYMTAB.nsyms == 0");
1273 return 0;
1274 }
1275
1276 if (symtab_load_command.strsize == 0)
1277 {
1278 if (log)
1279 module_sp->LogMessage(log.get(), "LC_SYMTAB.strsize == 0");
1280 return 0;
1281 }
1282 break;
1283
1284 case LoadCommandFunctionStarts:
1285 function_starts_load_command.cmd = lc.cmd;
1286 function_starts_load_command.cmdsize = lc.cmdsize;
1287 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1288 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1289 break;
1290
1291 default:
1292 break;
1293 }
1294 offset = cmd_offset + lc.cmdsize;
1295 }
1296
1297 if (symtab_load_command.cmd)
1298 {
1299 Symtab *symtab = m_symtab_ap.get();
1300 SectionList *section_list = GetSectionList();
1301 if (section_list == NULL)
1302 return 0;
1303
1304 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001305 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001306
Greg Clayton36da2aa2013-01-25 18:06:21 +00001307 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1308 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001309 bool bit_width_32 = addr_byte_size == 4;
1310 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1311
Greg Clayton36da2aa2013-01-25 18:06:21 +00001312 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1313 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1314 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001315 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001316
1317 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1318 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001319 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1320 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001321 {
Greg Claytondd29b972012-05-18 23:20:01 +00001322 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001323 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1324 // Reading mach file from memory in a process or core file...
1325
1326 if (linkedit_section_sp)
1327 {
1328 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1329 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1330 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001331 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001332
1333 bool data_was_read = false;
1334
1335#if defined (__APPLE__) && defined (__arm__)
1336 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001337 {
Greg Clayton29021d32012-04-18 05:19:20 +00001338 // This mach-o memory file is in the dyld shared cache. If this
1339 // program is not remote and this is iOS, then this process will
1340 // share the same shared cache as the process we are debugging and
1341 // we can read the entire __LINKEDIT from the address space in this
1342 // process. This is a needed optimization that is used for local iOS
1343 // debugging only since all shared libraries in the shared cache do
1344 // not have corresponding files that exist in the file system of the
1345 // device. They have been combined into a single file. This means we
1346 // always have to load these files from memory. All of the symbol and
1347 // string tables from all of the __LINKEDIT sections from the shared
1348 // libraries in the shared cache have been merged into a single large
1349 // symbol and string table. Reading all of this symbol and string table
1350 // data across can slow down debug launch times, so we optimize this by
1351 // reading the memory for the __LINKEDIT section from this process.
1352 PlatformSP platform_sp (target.GetPlatform());
Jason Molendab76f77e2012-12-13 01:13:00 +00001353 if (platform_sp && platform_sp->IsHost())
Greg Clayton29021d32012-04-18 05:19:20 +00001354 {
1355 data_was_read = true;
1356 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001357 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001358 if (function_starts_load_command.cmd)
1359 {
1360 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1361 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1362 }
1363 }
1364 }
1365#endif
1366
1367 if (!data_was_read)
1368 {
1369 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1370 if (nlist_data_sp)
1371 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001372 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1373 //if (strtab_data_sp)
1374 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001375 if (m_dysymtab.nindirectsyms != 0)
1376 {
1377 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1378 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1379 if (indirect_syms_data_sp)
1380 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1381 }
Greg Clayton29021d32012-04-18 05:19:20 +00001382 if (function_starts_load_command.cmd)
1383 {
1384 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1385 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1386 if (func_start_data_sp)
1387 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1388 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001389 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001390 }
1391 }
1392 else
1393 {
1394 nlist_data.SetData (m_data,
1395 symtab_load_command.symoff,
1396 nlist_data_byte_size);
1397 strtab_data.SetData (m_data,
1398 symtab_load_command.stroff,
1399 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001400 if (m_dysymtab.nindirectsyms != 0)
1401 {
1402 indirect_symbol_index_data.SetData (m_data,
1403 m_dysymtab.indirectsymoff,
1404 m_dysymtab.nindirectsyms * 4);
1405 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001406 if (function_starts_load_command.cmd)
1407 {
1408 function_starts_data.SetData (m_data,
1409 function_starts_load_command.dataoff,
1410 function_starts_load_command.datasize);
1411 }
1412 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001413
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001414 if (nlist_data.GetByteSize() == 0)
1415 {
1416 if (log)
1417 module_sp->LogMessage(log.get(), "failed to read nlist data");
1418 return 0;
1419 }
1420
1421
Greg Clayton3a5dc012012-05-25 17:04:00 +00001422 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1423 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001424 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001425 if (process)
1426 {
1427 if (strtab_addr == LLDB_INVALID_ADDRESS)
1428 {
1429 if (log)
1430 module_sp->LogMessage(log.get(), "failed to locate the strtab in memory");
1431 return 0;
1432 }
1433 }
1434 else
Greg Claytondd29b972012-05-18 23:20:01 +00001435 {
1436 if (log)
Greg Clayton3a5dc012012-05-25 17:04:00 +00001437 module_sp->LogMessage(log.get(), "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001438 return 0;
1439 }
1440 }
Greg Claytondd29b972012-05-18 23:20:01 +00001441
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001442 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1443 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1444 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1445 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1446 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1447 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1448 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1449 SectionSP eh_frame_section_sp;
1450 if (text_section_sp.get())
1451 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1452 else
1453 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1454
Greg Claytond2653c22012-03-14 01:53:24 +00001455 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001456 if (text_section_sp && function_starts_data.GetByteSize())
1457 {
1458 FunctionStarts::Entry function_start_entry;
1459 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001460 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001461 function_start_entry.addr = text_section_sp->GetFileAddress();
1462 uint64_t delta;
1463 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1464 {
1465 // Now append the current entry
1466 function_start_entry.addr += delta;
1467 function_starts.Append(function_start_entry);
1468 }
1469 }
1470
Greg Clayton36da2aa2013-01-25 18:06:21 +00001471 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001472
Greg Clayton36da2aa2013-01-25 18:06:21 +00001473 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 +00001474
Greg Clayton36da2aa2013-01-25 18:06:21 +00001475 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001476
1477 uint32_t N_SO_index = UINT32_MAX;
1478
1479 MachSymtabSectionInfo section_info (section_list);
1480 std::vector<uint32_t> N_FUN_indexes;
1481 std::vector<uint32_t> N_NSYM_indexes;
1482 std::vector<uint32_t> N_INCL_indexes;
1483 std::vector<uint32_t> N_BRAC_indexes;
1484 std::vector<uint32_t> N_COMM_indexes;
1485 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1486 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1487 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1488 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1489 // Any symbols that get merged into another will get an entry
1490 // in this map so we know
1491 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1492 uint32_t nlist_idx = 0;
1493 Symbol *symbol_ptr = NULL;
1494
1495 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001496 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001497 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001498 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001499 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001500
Jason Molendab62abd52012-06-21 01:51:02 +00001501#if defined (__APPLE__) && defined (__arm__)
1502
1503 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1504 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1505 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1506 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1507 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1508 // nlist parser to ignore all LOCAL symbols.
1509
1510 if (m_header.flags & 0x80000000u)
1511 {
1512 // Before we can start mapping the DSC, we need to make certain the target process is actually
1513 // using the cache we can find.
1514
1515 /*
1516 * TODO (FIXME!)
1517 *
1518 * Consider the case of testing with a separate DSC file.
1519 * If we go through the normal code paths, we will give symbols for the wrong DSC, and
1520 * that is bad. We need to read the target process' all_image_infos struct, and look
1521 * at the values of the processDetachedFromSharedRegion field. If that is set, we should skip
1522 * this code section.
1523 */
1524
1525 // Next we need to determine the correct path for the dyld shared cache.
1526
1527 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1528 char dsc_path[PATH_MAX];
1529
1530 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
1531 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1532 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
1533 header_arch.GetArchitectureName());
1534
1535 FileSpec dsc_filespec(dsc_path, false);
1536
1537 // We need definitions of two structures in the on-disk DSC, copy them here manually
Greg Claytonab77dcb2012-09-05 22:30:51 +00001538 struct lldb_copy_dyld_cache_header
1539 {
1540 char magic[16];
1541 uint32_t mappingOffset;
1542 uint32_t mappingCount;
1543 uint32_t imagesOffset;
1544 uint32_t imagesCount;
1545 uint64_t dyldBaseAddress;
1546 uint64_t codeSignatureOffset;
1547 uint64_t codeSignatureSize;
1548 uint64_t slideInfoOffset;
1549 uint64_t slideInfoSize;
1550 uint64_t localSymbolsOffset;
1551 uint64_t localSymbolsSize;
1552 };
1553 struct lldb_copy_dyld_cache_local_symbols_info
1554 {
1555 uint32_t nlistOffset;
1556 uint32_t nlistCount;
1557 uint32_t stringsOffset;
1558 uint32_t stringsSize;
1559 uint32_t entriesOffset;
1560 uint32_t entriesCount;
1561 };
1562 struct lldb_copy_dyld_cache_local_symbols_entry
1563 {
1564 uint32_t dylibOffset;
1565 uint32_t nlistStartIndex;
1566 uint32_t nlistCount;
1567 };
Jason Molendab62abd52012-06-21 01:51:02 +00001568
Jason Molendafd3b35d2012-06-22 03:28:35 +00001569 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1570 The dyld_cache_local_symbols_info structure gives us three things:
1571 1. The start and count of the nlist records in the dyld_shared_cache file
1572 2. The start and size of the strings for these nlist records
1573 3. The start and count of dyld_cache_local_symbols_entry entries
1574
1575 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1576 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
1577 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
1578 and the count of how many nlist records there are for this dylib/framework.
1579 */
1580
Jason Molendab62abd52012-06-21 01:51:02 +00001581 // Process the dsc header to find the unmapped symbols
1582 //
1583 // Save some VM space, do not map the entire cache in one shot.
1584
1585 if (DataBufferSP dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header)))
1586 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001587 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001588
Jason Molenda90b9cc82013-01-26 07:06:09 +00001589 lldb::offset_t offset = offsetof (struct lldb_copy_dyld_cache_header, mappingOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001590 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1591
1592 // If the mappingOffset points to a location inside the header, we've
1593 // opened an old dyld shared cache, and should not proceed further.
1594 if (mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header))
1595 {
1596
Jason Molendafd3b35d2012-06-22 03:28:35 +00001597 offset = offsetof (struct lldb_copy_dyld_cache_header, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001598 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1599 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1600
1601 if (localSymbolsOffset && localSymbolsSize)
1602 {
1603 // Map the local symbols
1604 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
1605 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001606 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001607
1608 offset = 0;
1609
1610 // Read the local_symbols_infos struct in one shot
1611 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1612 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1613
1614 // The local_symbols_infos offsets are offsets into local symbols memory, NOT file offsets!
1615 // We first need to identify the local "entry" that matches the current header.
1616 // The "entry" is stored as a file offset in the dyld_shared_cache, so we need to
1617 // adjust the raw m_header value by slide and 0x30000000.
1618
1619 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1620
1621 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - 0x30000000);
1622
1623 offset = local_symbols_info.entriesOffset;
1624 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1625 {
1626 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1627 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1628 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1629 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1630
1631 if (header_file_offset == local_symbols_entry.dylibOffset)
1632 {
1633 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1634
1635 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1636 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1637 num_syms = symtab->GetNumSymbols();
1638
1639 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1640 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1641
1642 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
1643 {
1644 /////////////////////////////
1645 {
1646 struct nlist_64 nlist;
1647 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1648 break;
1649
1650 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1651 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1652 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1653 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1654 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1655
1656 SymbolType type = eSymbolTypeInvalid;
1657 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1658
1659 if (symbol_name == NULL)
1660 {
1661 // No symbol should be NULL, even the symbols with no
1662 // string values should have an offset zero which points
1663 // to an empty C-string
1664 Host::SystemLog (Host::eSystemLogError,
1665 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1666 entry_index,
1667 nlist.n_strx,
1668 module_sp->GetFileSpec().GetDirectory().GetCString(),
1669 module_sp->GetFileSpec().GetFilename().GetCString());
1670 continue;
1671 }
1672 if (symbol_name[0] == '\0')
1673 symbol_name = NULL;
1674
1675 const char *symbol_name_non_abi_mangled = NULL;
1676
1677 SectionSP symbol_section;
1678 uint32_t symbol_byte_size = 0;
1679 bool add_nlist = true;
1680 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001681 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001682
1683 assert (sym_idx < num_syms);
1684
1685 sym[sym_idx].SetDebug (is_debug);
1686
1687 if (is_debug)
1688 {
1689 switch (nlist.n_type)
1690 {
1691 case StabGlobalSymbol:
1692 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1693 // Sometimes the N_GSYM value contains the address.
1694
1695 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1696 // have the same address, but we want to ensure that we always find only the real symbol,
1697 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1698 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1699 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1700 // same address.
1701
1702 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1703 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1704 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1705 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1706 add_nlist = false;
1707 else
1708 {
1709 sym[sym_idx].SetExternal(true);
1710 if (nlist.n_value != 0)
1711 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1712 type = eSymbolTypeData;
1713 }
1714 break;
1715
1716 case StabFunctionName:
1717 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1718 type = eSymbolTypeCompiler;
1719 break;
1720
1721 case StabFunction:
1722 // N_FUN -- procedure: name,,n_sect,linenumber,address
1723 if (symbol_name)
1724 {
1725 type = eSymbolTypeCode;
1726 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1727
1728 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1729 // We use the current number of symbols in the symbol table in lieu of
1730 // using nlist_idx in case we ever start trimming entries out
1731 N_FUN_indexes.push_back(sym_idx);
1732 }
1733 else
1734 {
1735 type = eSymbolTypeCompiler;
1736
1737 if ( !N_FUN_indexes.empty() )
1738 {
1739 // Copy the size of the function into the original STAB entry so we don't have
1740 // to hunt for it later
1741 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1742 N_FUN_indexes.pop_back();
1743 // We don't really need the end function STAB as it contains the size which
1744 // we already placed with the original symbol, so don't add it if we want a
1745 // minimal symbol table
1746 if (minimize)
1747 add_nlist = false;
1748 }
1749 }
1750 break;
1751
1752 case StabStaticSymbol:
1753 // N_STSYM -- static symbol: name,,n_sect,type,address
1754 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1755 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1756 type = eSymbolTypeData;
1757 break;
1758
1759 case StabLocalCommon:
1760 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1761 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1762 type = eSymbolTypeCommonBlock;
1763 break;
1764
1765 case StabBeginSymbol:
1766 // N_BNSYM
1767 // We use the current number of symbols in the symbol table in lieu of
1768 // using nlist_idx in case we ever start trimming entries out
1769 if (minimize)
1770 {
1771 // Skip these if we want minimal symbol tables
1772 add_nlist = false;
1773 }
1774 else
1775 {
1776 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1777 N_NSYM_indexes.push_back(sym_idx);
1778 type = eSymbolTypeScopeBegin;
1779 }
1780 break;
1781
1782 case StabEndSymbol:
1783 // N_ENSYM
1784 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1785 // so that we can always skip the entire symbol if we need to navigate
1786 // more quickly at the source level when parsing STABS
1787 if (minimize)
1788 {
1789 // Skip these if we want minimal symbol tables
1790 add_nlist = false;
1791 }
1792 else
1793 {
1794 if ( !N_NSYM_indexes.empty() )
1795 {
1796 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1797 symbol_ptr->SetByteSize(sym_idx + 1);
1798 symbol_ptr->SetSizeIsSibling(true);
1799 N_NSYM_indexes.pop_back();
1800 }
1801 type = eSymbolTypeScopeEnd;
1802 }
1803 break;
1804
1805
1806 case StabSourceFileOptions:
1807 // N_OPT - emitted with gcc2_compiled and in gcc source
1808 type = eSymbolTypeCompiler;
1809 break;
1810
1811 case StabRegisterSymbol:
1812 // N_RSYM - register sym: name,,NO_SECT,type,register
1813 type = eSymbolTypeVariable;
1814 break;
1815
1816 case StabSourceLine:
1817 // N_SLINE - src line: 0,,n_sect,linenumber,address
1818 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1819 type = eSymbolTypeLineEntry;
1820 break;
1821
1822 case StabStructureType:
1823 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1824 type = eSymbolTypeVariableType;
1825 break;
1826
1827 case StabSourceFileName:
1828 // N_SO - source file name
1829 type = eSymbolTypeSourceFile;
1830 if (symbol_name == NULL)
1831 {
1832 if (minimize)
1833 add_nlist = false;
1834 if (N_SO_index != UINT32_MAX)
1835 {
1836 // Set the size of the N_SO to the terminating index of this N_SO
1837 // so that we can always skip the entire N_SO if we need to navigate
1838 // more quickly at the source level when parsing STABS
1839 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1840 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1841 symbol_ptr->SetSizeIsSibling(true);
1842 }
1843 N_NSYM_indexes.clear();
1844 N_INCL_indexes.clear();
1845 N_BRAC_indexes.clear();
1846 N_COMM_indexes.clear();
1847 N_FUN_indexes.clear();
1848 N_SO_index = UINT32_MAX;
1849 }
1850 else
1851 {
1852 // We use the current number of symbols in the symbol table in lieu of
1853 // using nlist_idx in case we ever start trimming entries out
1854 const bool N_SO_has_full_path = symbol_name[0] == '/';
1855 if (N_SO_has_full_path)
1856 {
1857 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1858 {
1859 // We have two consecutive N_SO entries where the first contains a directory
1860 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00001861 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001862 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1863 add_nlist = false;
1864 }
1865 else
1866 {
1867 // This is the first entry in a N_SO that contains a directory or
1868 // a full path to the source file
1869 N_SO_index = sym_idx;
1870 }
1871 }
1872 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1873 {
1874 // This is usually the second N_SO entry that contains just the filename,
1875 // so here we combine it with the first one if we are minimizing the symbol table
1876 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1877 if (so_path && so_path[0])
1878 {
1879 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00001880 const size_t double_slash_pos = full_so_path.find("//");
1881 if (double_slash_pos != std::string::npos)
1882 {
1883 // The linker has been generating bad N_SO entries with doubled up paths
1884 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
1885 // and the second is the directory for the source file so you end up with
1886 // a path that looks like "/tmp/src//tmp/src/"
1887 FileSpec so_dir(so_path, false);
1888 if (!so_dir.Exists())
1889 {
1890 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
1891 if (so_dir.Exists())
1892 {
1893 // Trim off the incorrect path
1894 full_so_path.erase(0, double_slash_pos + 1);
1895 }
1896 }
1897 }
Jason Molendab62abd52012-06-21 01:51:02 +00001898 if (*full_so_path.rbegin() != '/')
1899 full_so_path += '/';
1900 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00001901 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001902 add_nlist = false;
1903 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1904 }
1905 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00001906 else
1907 {
1908 // This could be a relative path to a N_SO
1909 N_SO_index = sym_idx;
1910 }
Jason Molendab62abd52012-06-21 01:51:02 +00001911 }
Jason Molendab62abd52012-06-21 01:51:02 +00001912 break;
1913
1914 case StabObjectFileName:
1915 // N_OSO - object file name: name,,0,0,st_mtime
1916 type = eSymbolTypeObjectFile;
1917 break;
1918
1919 case StabLocalSymbol:
1920 // N_LSYM - local sym: name,,NO_SECT,type,offset
1921 type = eSymbolTypeLocal;
1922 break;
1923
1924 //----------------------------------------------------------------------
1925 // INCL scopes
1926 //----------------------------------------------------------------------
1927 case StabBeginIncludeFileName:
1928 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
1929 // We use the current number of symbols in the symbol table in lieu of
1930 // using nlist_idx in case we ever start trimming entries out
1931 N_INCL_indexes.push_back(sym_idx);
1932 type = eSymbolTypeScopeBegin;
1933 break;
1934
1935 case StabEndIncludeFile:
1936 // N_EINCL - include file end: name,,NO_SECT,0,0
1937 // Set the size of the N_BINCL to the terminating index of this N_EINCL
1938 // so that we can always skip the entire symbol if we need to navigate
1939 // more quickly at the source level when parsing STABS
1940 if ( !N_INCL_indexes.empty() )
1941 {
1942 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1943 symbol_ptr->SetByteSize(sym_idx + 1);
1944 symbol_ptr->SetSizeIsSibling(true);
1945 N_INCL_indexes.pop_back();
1946 }
1947 type = eSymbolTypeScopeEnd;
1948 break;
1949
1950 case StabIncludeFileName:
1951 // N_SOL - #included file name: name,,n_sect,0,address
1952 type = eSymbolTypeHeaderFile;
1953
1954 // We currently don't use the header files on darwin
1955 if (minimize)
1956 add_nlist = false;
1957 break;
1958
1959 case StabCompilerParameters:
1960 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
1961 type = eSymbolTypeCompiler;
1962 break;
1963
1964 case StabCompilerVersion:
1965 // N_VERSION - compiler version: name,,NO_SECT,0,0
1966 type = eSymbolTypeCompiler;
1967 break;
1968
1969 case StabCompilerOptLevel:
1970 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
1971 type = eSymbolTypeCompiler;
1972 break;
1973
1974 case StabParameter:
1975 // N_PSYM - parameter: name,,NO_SECT,type,offset
1976 type = eSymbolTypeVariable;
1977 break;
1978
1979 case StabAlternateEntry:
1980 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
1981 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1982 type = eSymbolTypeLineEntry;
1983 break;
1984
1985 //----------------------------------------------------------------------
1986 // Left and Right Braces
1987 //----------------------------------------------------------------------
1988 case StabLeftBracket:
1989 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
1990 // We use the current number of symbols in the symbol table in lieu of
1991 // using nlist_idx in case we ever start trimming entries out
1992 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1993 N_BRAC_indexes.push_back(sym_idx);
1994 type = eSymbolTypeScopeBegin;
1995 break;
1996
1997 case StabRightBracket:
1998 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
1999 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2000 // so that we can always skip the entire symbol if we need to navigate
2001 // more quickly at the source level when parsing STABS
2002 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2003 if ( !N_BRAC_indexes.empty() )
2004 {
2005 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2006 symbol_ptr->SetByteSize(sym_idx + 1);
2007 symbol_ptr->SetSizeIsSibling(true);
2008 N_BRAC_indexes.pop_back();
2009 }
2010 type = eSymbolTypeScopeEnd;
2011 break;
2012
2013 case StabDeletedIncludeFile:
2014 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2015 type = eSymbolTypeHeaderFile;
2016 break;
2017
2018 //----------------------------------------------------------------------
2019 // COMM scopes
2020 //----------------------------------------------------------------------
2021 case StabBeginCommon:
2022 // N_BCOMM - begin common: name,,NO_SECT,0,0
2023 // We use the current number of symbols in the symbol table in lieu of
2024 // using nlist_idx in case we ever start trimming entries out
2025 type = eSymbolTypeScopeBegin;
2026 N_COMM_indexes.push_back(sym_idx);
2027 break;
2028
2029 case StabEndCommonLocal:
2030 // N_ECOML - end common (local name): 0,,n_sect,0,address
2031 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2032 // Fall through
2033
2034 case StabEndCommon:
2035 // N_ECOMM - end common: name,,n_sect,0,0
2036 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2037 // so that we can always skip the entire symbol if we need to navigate
2038 // more quickly at the source level when parsing STABS
2039 if ( !N_COMM_indexes.empty() )
2040 {
2041 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2042 symbol_ptr->SetByteSize(sym_idx + 1);
2043 symbol_ptr->SetSizeIsSibling(true);
2044 N_COMM_indexes.pop_back();
2045 }
2046 type = eSymbolTypeScopeEnd;
2047 break;
2048
2049 case StabLength:
2050 // N_LENG - second stab entry with length information
2051 type = eSymbolTypeAdditional;
2052 break;
2053
2054 default: break;
2055 }
2056 }
2057 else
2058 {
2059 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2060 uint8_t n_type = NlistMaskType & nlist.n_type;
2061 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2062
2063 switch (n_type)
2064 {
2065 case NListTypeIndirect: // N_INDR - Fall through
2066 case NListTypePreboundUndefined:// N_PBUD - Fall through
2067 case NListTypeUndefined: // N_UNDF
2068 type = eSymbolTypeUndefined;
2069 break;
2070
2071 case NListTypeAbsolute: // N_ABS
2072 type = eSymbolTypeAbsolute;
2073 break;
2074
2075 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002076 {
2077 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002078
Greg Clayton01e6a582012-11-27 01:52:16 +00002079 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002080 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002081 // TODO: warn about this?
2082 add_nlist = false;
2083 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002084 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002085
2086 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002087 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002088 type = eSymbolTypeException;
2089 }
2090 else
2091 {
2092 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2093
2094 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002095 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002096 case SectionTypeRegular: break; // regular section
2097 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2098 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2099 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2100 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2101 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2102 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2103 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2104 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2105 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2106 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2107 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2108 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2109 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2110 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2111 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2112 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2113 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002114 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002115
2116 if (type == eSymbolTypeInvalid)
2117 {
2118 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2119 if (symbol_section->IsDescendant (text_section_sp.get()))
2120 {
2121 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2122 SectionAttrUserSelfModifyingCode |
2123 SectionAttrSytemSomeInstructions))
2124 type = eSymbolTypeData;
2125 else
2126 type = eSymbolTypeCode;
2127 }
2128 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002129 {
2130 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2131 {
2132 type = eSymbolTypeRuntime;
2133
Greg Clayton01e6a582012-11-27 01:52:16 +00002134 if (symbol_name &&
2135 symbol_name[0] == '_' &&
2136 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002137 symbol_name[2] == 'B')
2138 {
2139 llvm::StringRef symbol_name_ref(symbol_name);
2140 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2141 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2142 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2143 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2144 {
2145 symbol_name_non_abi_mangled = symbol_name + 1;
2146 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2147 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002148 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002149 }
2150 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2151 {
2152 symbol_name_non_abi_mangled = symbol_name + 1;
2153 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2154 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002155 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002156 }
2157 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2158 {
2159 symbol_name_non_abi_mangled = symbol_name + 1;
2160 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2161 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002162 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002163 }
2164 }
2165 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002166 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002167 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002168 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002169 }
2170 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002171 {
2172 type = eSymbolTypeData;
2173 }
2174 }
2175 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2176 {
2177 type = eSymbolTypeTrampoline;
2178 }
2179 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2180 {
2181 type = eSymbolTypeRuntime;
2182 if (symbol_name && symbol_name[0] == '.')
2183 {
2184 llvm::StringRef symbol_name_ref(symbol_name);
2185 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2186 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002187 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002188 symbol_name_non_abi_mangled = symbol_name;
2189 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2190 type = eSymbolTypeObjCClass;
2191 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002192 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002193 }
2194 }
2195 }
Jason Molendab62abd52012-06-21 01:51:02 +00002196 }
2197 }
Jason Molendab62abd52012-06-21 01:51:02 +00002198 break;
2199 }
2200 }
2201
2202 if (add_nlist)
2203 {
2204 uint64_t symbol_value = nlist.n_value;
2205 bool symbol_name_is_mangled = false;
2206
2207 if (symbol_name_non_abi_mangled)
2208 {
Jason Molenda292cca82012-07-20 03:35:44 +00002209 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2210 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002211 }
2212 else
2213 {
2214 if (symbol_name && symbol_name[0] == '_')
2215 {
2216 symbol_name_is_mangled = symbol_name[1] == '_';
2217 symbol_name++; // Skip the leading underscore
2218 }
2219
2220 if (symbol_name)
2221 {
Jason Molenda292cca82012-07-20 03:35:44 +00002222 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002223 }
2224 }
2225
2226 if (is_debug == false)
2227 {
2228 if (type == eSymbolTypeCode)
2229 {
2230 // See if we can find a N_FUN entry for any code symbols.
2231 // If we do find a match, and the name matches, then we
2232 // can merge the two into just the function symbol to avoid
2233 // duplicate entries in the symbol table
2234 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2235 if (pos != N_FUN_addr_to_sym_idx.end())
2236 {
2237 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2238 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2239 {
2240 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2241 // We just need the flags from the linker symbol, so put these flags
2242 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2243 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2244 sym[sym_idx].Clear();
2245 continue;
2246 }
2247 }
2248 }
2249 else if (type == eSymbolTypeData)
2250 {
2251 // See if we can find a N_STSYM entry for any data symbols.
2252 // If we do find a match, and the name matches, then we
2253 // can merge the two into just the Static symbol to avoid
2254 // duplicate entries in the symbol table
2255 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2256 if (pos != N_STSYM_addr_to_sym_idx.end())
2257 {
2258 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2259 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2260 {
2261 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2262 // We just need the flags from the linker symbol, so put these flags
2263 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2264 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2265 sym[sym_idx].Clear();
2266 continue;
2267 }
2268 }
2269 }
2270 }
2271 if (symbol_section)
2272 {
2273 const addr_t section_file_addr = symbol_section->GetFileAddress();
2274 if (symbol_byte_size == 0 && function_starts_count > 0)
2275 {
2276 addr_t symbol_lookup_file_addr = nlist.n_value;
2277 // Do an exact address match for non-ARM addresses, else get the closest since
2278 // the symbol might be a thumb symbol which has an address with bit zero set
2279 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2280 if (is_arm && func_start_entry)
2281 {
2282 // Verify that the function start address is the symbol address (ARM)
2283 // or the symbol address + 1 (thumb)
2284 if (func_start_entry->addr != symbol_lookup_file_addr &&
2285 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2286 {
2287 // Not the right entry, NULL it out...
2288 func_start_entry = NULL;
2289 }
2290 }
2291 if (func_start_entry)
2292 {
2293 func_start_entry->data = true;
2294
2295 addr_t symbol_file_addr = func_start_entry->addr;
2296 uint32_t symbol_flags = 0;
2297 if (is_arm)
2298 {
2299 if (symbol_file_addr & 1)
2300 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2301 symbol_file_addr &= 0xfffffffffffffffeull;
2302 }
2303
2304 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2305 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2306 if (next_func_start_entry)
2307 {
2308 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2309 // Be sure the clear the Thumb address bit when we calculate the size
2310 // from the current and next address
2311 if (is_arm)
2312 next_symbol_file_addr &= 0xfffffffffffffffeull;
2313 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2314 }
2315 else
2316 {
2317 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2318 }
2319 }
2320 }
2321 symbol_value -= section_file_addr;
2322 }
2323
2324 sym[sym_idx].SetID (nlist_idx);
2325 sym[sym_idx].SetType (type);
2326 sym[sym_idx].GetAddress().SetSection (symbol_section);
2327 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2328 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2329
2330 if (symbol_byte_size > 0)
2331 sym[sym_idx].SetByteSize(symbol_byte_size);
2332
Greg Clayton01e6a582012-11-27 01:52:16 +00002333 if (demangled_is_synthesized)
2334 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002335 ++sym_idx;
2336 }
2337 else
2338 {
2339 sym[sym_idx].Clear();
2340 }
2341
2342 }
2343 /////////////////////////////
2344 }
2345 break; // No more entries to consider
2346 }
2347 }
2348 }
2349 }
2350 }
2351 }
2352 }
2353
2354 // Must reset this in case it was mutated above!
2355 nlist_data_offset = 0;
2356#endif
2357
2358 // If the sym array was not created while parsing the DSC unmapped
2359 // symbols, create it now.
2360 if (sym == NULL)
2361 {
2362 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2363 num_syms = symtab->GetNumSymbols();
2364 }
2365
2366 if (unmapped_local_symbols_found)
2367 {
2368 assert(m_dysymtab.ilocalsym == 0);
2369 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2370 nlist_idx = m_dysymtab.nlocalsym;
2371 }
2372 else
2373 {
2374 nlist_idx = 0;
2375 }
2376
2377 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002378 {
2379 struct nlist_64 nlist;
2380 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2381 break;
2382
2383 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2384 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2385 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2386 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2387 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2388
2389 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002390 const char *symbol_name = NULL;
2391
Greg Clayton3a5dc012012-05-25 17:04:00 +00002392 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002393 {
2394 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
2395
2396 if (symbol_name == NULL)
2397 {
2398 // No symbol should be NULL, even the symbols with no
2399 // string values should have an offset zero which points
2400 // to an empty C-string
2401 Host::SystemLog (Host::eSystemLogError,
2402 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
2403 nlist_idx,
2404 nlist.n_strx,
2405 module_sp->GetFileSpec().GetDirectory().GetCString(),
2406 module_sp->GetFileSpec().GetFilename().GetCString());
2407 continue;
2408 }
2409 if (symbol_name[0] == '\0')
2410 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002411 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002412 else
2413 {
2414 const addr_t str_addr = strtab_addr + nlist.n_strx;
2415 Error str_error;
2416 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2417 symbol_name = memory_symbol_name.c_str();
2418 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002419 const char *symbol_name_non_abi_mangled = NULL;
2420
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002421 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002422 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002423 bool add_nlist = true;
2424 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002425 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002426
2427 assert (sym_idx < num_syms);
2428
2429 sym[sym_idx].SetDebug (is_debug);
2430
2431 if (is_debug)
2432 {
2433 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002434 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002435 case StabGlobalSymbol:
2436 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2437 // Sometimes the N_GSYM value contains the address.
2438
2439 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2440 // have the same address, but we want to ensure that we always find only the real symbol,
2441 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2442 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2443 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2444 // same address.
2445
2446 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2447 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2448 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2449 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2450 add_nlist = false;
2451 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002452 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002453 sym[sym_idx].SetExternal(true);
2454 if (nlist.n_value != 0)
2455 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2456 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002457 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002458 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002459
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002460 case StabFunctionName:
2461 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2462 type = eSymbolTypeCompiler;
2463 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002464
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002465 case StabFunction:
2466 // N_FUN -- procedure: name,,n_sect,linenumber,address
2467 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002468 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002469 type = eSymbolTypeCode;
2470 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2471
2472 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2473 // We use the current number of symbols in the symbol table in lieu of
2474 // using nlist_idx in case we ever start trimming entries out
2475 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002476 }
2477 else
2478 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002479 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002480
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002481 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002482 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002483 // Copy the size of the function into the original STAB entry so we don't have
2484 // to hunt for it later
2485 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2486 N_FUN_indexes.pop_back();
2487 // We don't really need the end function STAB as it contains the size which
2488 // we already placed with the original symbol, so don't add it if we want a
2489 // minimal symbol table
2490 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002491 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002492 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002493 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002494 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002495
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002496 case StabStaticSymbol:
2497 // N_STSYM -- static symbol: name,,n_sect,type,address
2498 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2499 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2500 type = eSymbolTypeData;
2501 break;
2502
2503 case StabLocalCommon:
2504 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2505 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2506 type = eSymbolTypeCommonBlock;
2507 break;
2508
2509 case StabBeginSymbol:
2510 // N_BNSYM
2511 // We use the current number of symbols in the symbol table in lieu of
2512 // using nlist_idx in case we ever start trimming entries out
2513 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002514 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002515 // Skip these if we want minimal symbol tables
2516 add_nlist = false;
2517 }
2518 else
2519 {
2520 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2521 N_NSYM_indexes.push_back(sym_idx);
2522 type = eSymbolTypeScopeBegin;
2523 }
2524 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002525
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002526 case StabEndSymbol:
2527 // N_ENSYM
2528 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2529 // so that we can always skip the entire symbol if we need to navigate
2530 // more quickly at the source level when parsing STABS
2531 if (minimize)
2532 {
2533 // Skip these if we want minimal symbol tables
2534 add_nlist = false;
2535 }
2536 else
2537 {
2538 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002539 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002540 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2541 symbol_ptr->SetByteSize(sym_idx + 1);
2542 symbol_ptr->SetSizeIsSibling(true);
2543 N_NSYM_indexes.pop_back();
2544 }
2545 type = eSymbolTypeScopeEnd;
2546 }
2547 break;
2548
2549
2550 case StabSourceFileOptions:
2551 // N_OPT - emitted with gcc2_compiled and in gcc source
2552 type = eSymbolTypeCompiler;
2553 break;
2554
2555 case StabRegisterSymbol:
2556 // N_RSYM - register sym: name,,NO_SECT,type,register
2557 type = eSymbolTypeVariable;
2558 break;
2559
2560 case StabSourceLine:
2561 // N_SLINE - src line: 0,,n_sect,linenumber,address
2562 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2563 type = eSymbolTypeLineEntry;
2564 break;
2565
2566 case StabStructureType:
2567 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2568 type = eSymbolTypeVariableType;
2569 break;
2570
2571 case StabSourceFileName:
2572 // N_SO - source file name
2573 type = eSymbolTypeSourceFile;
2574 if (symbol_name == NULL)
2575 {
2576 if (minimize)
2577 add_nlist = false;
2578 if (N_SO_index != UINT32_MAX)
2579 {
2580 // Set the size of the N_SO to the terminating index of this N_SO
2581 // so that we can always skip the entire N_SO if we need to navigate
2582 // more quickly at the source level when parsing STABS
2583 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2584 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2585 symbol_ptr->SetSizeIsSibling(true);
2586 }
2587 N_NSYM_indexes.clear();
2588 N_INCL_indexes.clear();
2589 N_BRAC_indexes.clear();
2590 N_COMM_indexes.clear();
2591 N_FUN_indexes.clear();
2592 N_SO_index = UINT32_MAX;
2593 }
2594 else
2595 {
2596 // We use the current number of symbols in the symbol table in lieu of
2597 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002598 const bool N_SO_has_full_path = symbol_name[0] == '/';
2599 if (N_SO_has_full_path)
2600 {
2601 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2602 {
2603 // We have two consecutive N_SO entries where the first contains a directory
2604 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002605 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002606 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2607 add_nlist = false;
2608 }
2609 else
2610 {
2611 // This is the first entry in a N_SO that contains a directory or
2612 // a full path to the source file
2613 N_SO_index = sym_idx;
2614 }
2615 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002616 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2617 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002618 // This is usually the second N_SO entry that contains just the filename,
2619 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002620 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2621 if (so_path && so_path[0])
2622 {
2623 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002624 const size_t double_slash_pos = full_so_path.find("//");
2625 if (double_slash_pos != std::string::npos)
2626 {
2627 // The linker has been generating bad N_SO entries with doubled up paths
2628 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2629 // and the second is the directory for the source file so you end up with
2630 // a path that looks like "/tmp/src//tmp/src/"
2631 FileSpec so_dir(so_path, false);
2632 if (!so_dir.Exists())
2633 {
2634 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2635 if (so_dir.Exists())
2636 {
2637 // Trim off the incorrect path
2638 full_so_path.erase(0, double_slash_pos + 1);
2639 }
2640 }
2641 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002642 if (*full_so_path.rbegin() != '/')
2643 full_so_path += '/';
2644 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002645 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002646 add_nlist = false;
2647 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2648 }
2649 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002650 else
2651 {
2652 // This could be a relative path to a N_SO
2653 N_SO_index = sym_idx;
2654 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002655 }
2656
2657 break;
2658
2659 case StabObjectFileName:
2660 // N_OSO - object file name: name,,0,0,st_mtime
2661 type = eSymbolTypeObjectFile;
2662 break;
2663
2664 case StabLocalSymbol:
2665 // N_LSYM - local sym: name,,NO_SECT,type,offset
2666 type = eSymbolTypeLocal;
2667 break;
2668
2669 //----------------------------------------------------------------------
2670 // INCL scopes
2671 //----------------------------------------------------------------------
2672 case StabBeginIncludeFileName:
2673 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2674 // We use the current number of symbols in the symbol table in lieu of
2675 // using nlist_idx in case we ever start trimming entries out
2676 N_INCL_indexes.push_back(sym_idx);
2677 type = eSymbolTypeScopeBegin;
2678 break;
2679
2680 case StabEndIncludeFile:
2681 // N_EINCL - include file end: name,,NO_SECT,0,0
2682 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2683 // so that we can always skip the entire symbol if we need to navigate
2684 // more quickly at the source level when parsing STABS
2685 if ( !N_INCL_indexes.empty() )
2686 {
2687 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2688 symbol_ptr->SetByteSize(sym_idx + 1);
2689 symbol_ptr->SetSizeIsSibling(true);
2690 N_INCL_indexes.pop_back();
2691 }
2692 type = eSymbolTypeScopeEnd;
2693 break;
2694
2695 case StabIncludeFileName:
2696 // N_SOL - #included file name: name,,n_sect,0,address
2697 type = eSymbolTypeHeaderFile;
2698
2699 // We currently don't use the header files on darwin
2700 if (minimize)
2701 add_nlist = false;
2702 break;
2703
2704 case StabCompilerParameters:
2705 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2706 type = eSymbolTypeCompiler;
2707 break;
2708
2709 case StabCompilerVersion:
2710 // N_VERSION - compiler version: name,,NO_SECT,0,0
2711 type = eSymbolTypeCompiler;
2712 break;
2713
2714 case StabCompilerOptLevel:
2715 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2716 type = eSymbolTypeCompiler;
2717 break;
2718
2719 case StabParameter:
2720 // N_PSYM - parameter: name,,NO_SECT,type,offset
2721 type = eSymbolTypeVariable;
2722 break;
2723
2724 case StabAlternateEntry:
2725 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2726 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2727 type = eSymbolTypeLineEntry;
2728 break;
2729
2730 //----------------------------------------------------------------------
2731 // Left and Right Braces
2732 //----------------------------------------------------------------------
2733 case StabLeftBracket:
2734 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2735 // We use the current number of symbols in the symbol table in lieu of
2736 // using nlist_idx in case we ever start trimming entries out
2737 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2738 N_BRAC_indexes.push_back(sym_idx);
2739 type = eSymbolTypeScopeBegin;
2740 break;
2741
2742 case StabRightBracket:
2743 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2744 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2745 // so that we can always skip the entire symbol if we need to navigate
2746 // more quickly at the source level when parsing STABS
2747 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2748 if ( !N_BRAC_indexes.empty() )
2749 {
2750 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2751 symbol_ptr->SetByteSize(sym_idx + 1);
2752 symbol_ptr->SetSizeIsSibling(true);
2753 N_BRAC_indexes.pop_back();
2754 }
2755 type = eSymbolTypeScopeEnd;
2756 break;
2757
2758 case StabDeletedIncludeFile:
2759 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2760 type = eSymbolTypeHeaderFile;
2761 break;
2762
2763 //----------------------------------------------------------------------
2764 // COMM scopes
2765 //----------------------------------------------------------------------
2766 case StabBeginCommon:
2767 // N_BCOMM - begin common: name,,NO_SECT,0,0
2768 // We use the current number of symbols in the symbol table in lieu of
2769 // using nlist_idx in case we ever start trimming entries out
2770 type = eSymbolTypeScopeBegin;
2771 N_COMM_indexes.push_back(sym_idx);
2772 break;
2773
2774 case StabEndCommonLocal:
2775 // N_ECOML - end common (local name): 0,,n_sect,0,address
2776 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2777 // Fall through
2778
2779 case StabEndCommon:
2780 // N_ECOMM - end common: name,,n_sect,0,0
2781 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2782 // so that we can always skip the entire symbol if we need to navigate
2783 // more quickly at the source level when parsing STABS
2784 if ( !N_COMM_indexes.empty() )
2785 {
2786 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2787 symbol_ptr->SetByteSize(sym_idx + 1);
2788 symbol_ptr->SetSizeIsSibling(true);
2789 N_COMM_indexes.pop_back();
2790 }
2791 type = eSymbolTypeScopeEnd;
2792 break;
2793
2794 case StabLength:
2795 // N_LENG - second stab entry with length information
2796 type = eSymbolTypeAdditional;
2797 break;
2798
2799 default: break;
2800 }
2801 }
2802 else
2803 {
2804 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2805 uint8_t n_type = NlistMaskType & nlist.n_type;
2806 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2807
2808 switch (n_type)
2809 {
2810 case NListTypeIndirect: // N_INDR - Fall through
2811 case NListTypePreboundUndefined:// N_PBUD - Fall through
2812 case NListTypeUndefined: // N_UNDF
2813 type = eSymbolTypeUndefined;
2814 break;
2815
2816 case NListTypeAbsolute: // N_ABS
2817 type = eSymbolTypeAbsolute;
2818 break;
2819
2820 case NListTypeSection: // N_SECT
2821 {
2822 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2823
Sean Callananb386d822012-08-09 00:50:26 +00002824 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002825 {
2826 // TODO: warn about this?
2827 add_nlist = false;
2828 break;
2829 }
2830
2831 if (TEXT_eh_frame_sectID == nlist.n_sect)
2832 {
2833 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00002834 }
2835 else
2836 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002837 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2838
2839 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00002840 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002841 case SectionTypeRegular: break; // regular section
2842 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2843 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2844 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2845 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2846 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2847 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2848 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2849 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2850 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2851 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2852 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2853 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2854 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2855 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2856 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2857 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2858 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002859 }
Chris Lattner24943d22010-06-08 16:52:24 +00002860
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002861 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002862 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002863 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2864 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002865 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002866 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2867 SectionAttrUserSelfModifyingCode |
2868 SectionAttrSytemSomeInstructions))
2869 type = eSymbolTypeData;
2870 else
2871 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00002872 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002873 else
2874 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002875 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002876 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00002877 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002878 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00002879
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002880 if (symbol_name &&
2881 symbol_name[0] == '_' &&
2882 symbol_name[1] == 'O' &&
2883 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00002884 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002885 llvm::StringRef symbol_name_ref(symbol_name);
2886 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2887 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2888 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2889 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00002890 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002891 symbol_name_non_abi_mangled = symbol_name + 1;
2892 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2893 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002894 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00002895 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002896 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00002897 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002898 symbol_name_non_abi_mangled = symbol_name + 1;
2899 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2900 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002901 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002902 }
2903 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2904 {
2905 symbol_name_non_abi_mangled = symbol_name + 1;
2906 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2907 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002908 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00002909 }
2910 }
2911 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002912 else
2913 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
2914 {
2915 type = eSymbolTypeException;
2916 }
2917 else
2918 {
2919 type = eSymbolTypeData;
2920 }
2921 }
2922 else
2923 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2924 {
2925 type = eSymbolTypeTrampoline;
2926 }
2927 else
2928 if (symbol_section->IsDescendant(objc_section_sp.get()))
2929 {
2930 type = eSymbolTypeRuntime;
2931 if (symbol_name && symbol_name[0] == '.')
2932 {
2933 llvm::StringRef symbol_name_ref(symbol_name);
2934 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2935 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
2936 {
2937 symbol_name_non_abi_mangled = symbol_name;
2938 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2939 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002940 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002941 }
2942 }
Chris Lattner24943d22010-06-08 16:52:24 +00002943 }
2944 }
2945 }
2946 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002947 break;
2948 }
2949 }
2950
2951 if (add_nlist)
2952 {
2953 uint64_t symbol_value = nlist.n_value;
2954 bool symbol_name_is_mangled = false;
2955
2956 if (symbol_name_non_abi_mangled)
2957 {
Greg Claytonc0240042012-07-18 23:18:10 +00002958 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2959 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00002960 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002961 else
2962 {
2963 if (symbol_name && symbol_name[0] == '_')
2964 {
2965 symbol_name_is_mangled = symbol_name[1] == '_';
2966 symbol_name++; // Skip the leading underscore
2967 }
2968
2969 if (symbol_name)
2970 {
Greg Claytonc0240042012-07-18 23:18:10 +00002971 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002972 }
2973 }
2974
2975 if (is_debug == false)
2976 {
2977 if (type == eSymbolTypeCode)
2978 {
2979 // See if we can find a N_FUN entry for any code symbols.
2980 // If we do find a match, and the name matches, then we
2981 // can merge the two into just the function symbol to avoid
2982 // duplicate entries in the symbol table
2983 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2984 if (pos != N_FUN_addr_to_sym_idx.end())
2985 {
2986 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2987 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2988 {
2989 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2990 // We just need the flags from the linker symbol, so put these flags
2991 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2992 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2993 sym[sym_idx].Clear();
2994 continue;
2995 }
2996 }
2997 }
2998 else if (type == eSymbolTypeData)
2999 {
3000 // See if we can find a N_STSYM entry for any data symbols.
3001 // If we do find a match, and the name matches, then we
3002 // can merge the two into just the Static symbol to avoid
3003 // duplicate entries in the symbol table
3004 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3005 if (pos != N_STSYM_addr_to_sym_idx.end())
3006 {
3007 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3008 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3009 {
3010 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3011 // We just need the flags from the linker symbol, so put these flags
3012 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3013 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3014 sym[sym_idx].Clear();
3015 continue;
3016 }
3017 }
3018 }
3019 }
3020 if (symbol_section)
3021 {
3022 const addr_t section_file_addr = symbol_section->GetFileAddress();
3023 if (symbol_byte_size == 0 && function_starts_count > 0)
3024 {
Greg Claytond2653c22012-03-14 01:53:24 +00003025 addr_t symbol_lookup_file_addr = nlist.n_value;
3026 // Do an exact address match for non-ARM addresses, else get the closest since
3027 // the symbol might be a thumb symbol which has an address with bit zero set
3028 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3029 if (is_arm && func_start_entry)
3030 {
3031 // Verify that the function start address is the symbol address (ARM)
3032 // or the symbol address + 1 (thumb)
3033 if (func_start_entry->addr != symbol_lookup_file_addr &&
3034 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3035 {
3036 // Not the right entry, NULL it out...
3037 func_start_entry = NULL;
3038 }
3039 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003040 if (func_start_entry)
3041 {
3042 func_start_entry->data = true;
Greg Claytond2653c22012-03-14 01:53:24 +00003043
3044 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003045 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003046 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003047
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003048 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3049 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3050 if (next_func_start_entry)
3051 {
Greg Claytond2653c22012-03-14 01:53:24 +00003052 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3053 // Be sure the clear the Thumb address bit when we calculate the size
3054 // from the current and next address
3055 if (is_arm)
3056 next_symbol_file_addr &= 0xfffffffffffffffeull;
3057 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 +00003058 }
3059 else
3060 {
Greg Claytond2653c22012-03-14 01:53:24 +00003061 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003062 }
3063 }
3064 }
3065 symbol_value -= section_file_addr;
3066 }
3067
3068 sym[sym_idx].SetID (nlist_idx);
3069 sym[sym_idx].SetType (type);
3070 sym[sym_idx].GetAddress().SetSection (symbol_section);
3071 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3072 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3073
3074 if (symbol_byte_size > 0)
3075 sym[sym_idx].SetByteSize(symbol_byte_size);
3076
Greg Clayton01e6a582012-11-27 01:52:16 +00003077 if (demangled_is_synthesized)
3078 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3079
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003080 ++sym_idx;
3081 }
3082 else
3083 {
3084 sym[sym_idx].Clear();
3085 }
3086
3087 }
3088
3089 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3090 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3091 // such entries by figuring out what the address for the global is by looking up this non-STAB
3092 // entry and copying the value into the debug symbol's value to save us the hassle in the
3093 // debug symbol parser.
3094
3095 Symbol *global_symbol = NULL;
3096 for (nlist_idx = 0;
3097 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3098 nlist_idx++)
3099 {
3100 if (global_symbol->GetAddress().GetFileAddress() == 0)
3101 {
3102 std::vector<uint32_t> indexes;
3103 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3104 {
3105 std::vector<uint32_t>::const_iterator pos;
3106 std::vector<uint32_t>::const_iterator end = indexes.end();
3107 for (pos = indexes.begin(); pos != end; ++pos)
3108 {
3109 symbol_ptr = symtab->SymbolAtIndex(*pos);
3110 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3111 {
3112 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3113 break;
3114 }
3115 }
3116 }
Chris Lattner24943d22010-06-08 16:52:24 +00003117 }
3118 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003119
3120 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3121
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003122 if (function_starts_count > 0)
3123 {
3124 char synthetic_function_symbol[PATH_MAX];
3125 uint32_t num_synthetic_function_symbols = 0;
3126 for (i=0; i<function_starts_count; ++i)
3127 {
3128 if (function_starts.GetEntryRef (i).data == false)
3129 ++num_synthetic_function_symbols;
3130 }
3131
3132 if (num_synthetic_function_symbols > 0)
3133 {
3134 if (num_syms < sym_idx + num_synthetic_function_symbols)
3135 {
3136 num_syms = sym_idx + num_synthetic_function_symbols;
3137 sym = symtab->Resize (num_syms);
3138 }
3139 uint32_t synthetic_function_symbol_idx = 0;
3140 for (i=0; i<function_starts_count; ++i)
3141 {
3142 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3143 if (func_start_entry->data == false)
3144 {
Greg Claytond2653c22012-03-14 01:53:24 +00003145 addr_t symbol_file_addr = func_start_entry->addr;
3146 uint32_t symbol_flags = 0;
3147 if (is_arm)
3148 {
3149 if (symbol_file_addr & 1)
3150 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3151 symbol_file_addr &= 0xfffffffffffffffeull;
3152 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003153 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003154 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003155 {
3156 SectionSP symbol_section (symbol_addr.GetSection());
3157 uint32_t symbol_byte_size = 0;
3158 if (symbol_section)
3159 {
3160 const addr_t section_file_addr = symbol_section->GetFileAddress();
3161 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3162 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3163 if (next_func_start_entry)
3164 {
Greg Claytond2653c22012-03-14 01:53:24 +00003165 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3166 if (is_arm)
3167 next_symbol_file_addr &= 0xfffffffffffffffeull;
3168 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 +00003169 }
3170 else
3171 {
Greg Claytond2653c22012-03-14 01:53:24 +00003172 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003173 }
3174 snprintf (synthetic_function_symbol,
3175 sizeof(synthetic_function_symbol),
3176 "___lldb_unnamed_function%u$$%s",
3177 ++synthetic_function_symbol_idx,
3178 module_sp->GetFileSpec().GetFilename().GetCString());
3179 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003180 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003181 sym[sym_idx].SetType (eSymbolTypeCode);
3182 sym[sym_idx].SetIsSynthetic (true);
3183 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003184 if (symbol_flags)
3185 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003186 if (symbol_byte_size)
3187 sym[sym_idx].SetByteSize (symbol_byte_size);
3188 ++sym_idx;
3189 }
3190 }
3191 }
3192 }
3193 }
3194 }
3195
3196 // Trim our symbols down to just what we ended up with after
3197 // removing any symbols.
3198 if (sym_idx < num_syms)
3199 {
3200 num_syms = sym_idx;
3201 sym = symtab->Resize (num_syms);
3202 }
3203
3204 // Now synthesize indirect symbols
3205 if (m_dysymtab.nindirectsyms != 0)
3206 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003207 if (indirect_symbol_index_data.GetByteSize())
3208 {
3209 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3210
3211 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3212 {
3213 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3214 {
3215 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3216 if (symbol_stub_byte_size == 0)
3217 continue;
3218
3219 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3220
3221 if (num_symbol_stubs == 0)
3222 continue;
3223
3224 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3225 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3226 {
3227 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3228 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 +00003229 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003230 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3231 {
3232 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3233 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3234 continue;
3235
3236 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3237 Symbol *stub_symbol = NULL;
3238 if (index_pos != end_index_pos)
3239 {
3240 // We have a remapping from the original nlist index to
3241 // a current symbol index, so just look this up by index
3242 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3243 }
3244 else
3245 {
3246 // We need to lookup a symbol using the original nlist
3247 // symbol index since this index is coming from the
3248 // S_SYMBOL_STUBS
3249 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3250 }
3251
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003252 if (stub_symbol)
3253 {
3254 Address so_addr(symbol_stub_addr, section_list);
3255
3256 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3257 {
3258 // Change the external symbol into a trampoline that makes sense
3259 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3260 // can re-use them so we don't have to make up a synthetic symbol
3261 // for no good reason.
3262 stub_symbol->SetType (eSymbolTypeTrampoline);
3263 stub_symbol->SetExternal (false);
3264 stub_symbol->GetAddress() = so_addr;
3265 stub_symbol->SetByteSize (symbol_stub_byte_size);
3266 }
3267 else
3268 {
3269 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003270 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003271 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003272 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003273 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003274 stub_symbol = NULL; // this pointer no longer valid
3275 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003276 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003277 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003278 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3279 sym[sym_idx].SetIsSynthetic (true);
3280 sym[sym_idx].GetAddress() = so_addr;
3281 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3282 ++sym_idx;
3283 }
3284 }
Greg Claytond4330e62012-09-05 01:38:55 +00003285 else
3286 {
3287 if (log)
3288 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3289 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003290 }
3291 }
3292 }
3293 }
3294 }
3295 }
3296 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003297 }
3298 return 0;
3299}
3300
3301
3302void
3303ObjectFileMachO::Dump (Stream *s)
3304{
Greg Clayton9482f052012-03-13 23:14:29 +00003305 ModuleSP module_sp(GetModule());
3306 if (module_sp)
3307 {
3308 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3309 s->Printf("%p: ", this);
3310 s->Indent();
3311 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3312 s->PutCString("ObjectFileMachO64");
3313 else
3314 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003315
Greg Clayton9482f052012-03-13 23:14:29 +00003316 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003317
Greg Clayton9482f052012-03-13 23:14:29 +00003318 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003319
Greg Clayton9482f052012-03-13 23:14:29 +00003320 if (m_sections_ap.get())
3321 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003322
Greg Clayton9482f052012-03-13 23:14:29 +00003323 if (m_symtab_ap.get())
3324 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3325 }
Chris Lattner24943d22010-06-08 16:52:24 +00003326}
3327
3328
3329bool
Greg Clayton0467c782011-02-04 18:53:10 +00003330ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003331{
Greg Clayton9482f052012-03-13 23:14:29 +00003332 ModuleSP module_sp(GetModule());
3333 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003334 {
Greg Clayton9482f052012-03-13 23:14:29 +00003335 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3336 struct uuid_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003337 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003338 uint32_t i;
3339 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003340 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003341 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003342 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3343 break;
3344
3345 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00003346 {
Greg Clayton9482f052012-03-13 23:14:29 +00003347 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
Sean Callanana5689d52012-07-12 18:04:03 +00003348
Greg Clayton9482f052012-03-13 23:14:29 +00003349 if (uuid_bytes)
3350 {
Sean Callanana5689d52012-07-12 18:04:03 +00003351 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3352 // We pretend these object files have no UUID to prevent crashing.
3353
3354 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3355 0x3b, 0xa8,
3356 0x4b, 0x16,
3357 0xb6, 0xa4,
3358 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3359
3360 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3361 return false;
3362
Greg Clayton9482f052012-03-13 23:14:29 +00003363 uuid->SetBytes (uuid_bytes);
3364 return true;
3365 }
3366 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003367 }
Greg Clayton9482f052012-03-13 23:14:29 +00003368 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003369 }
Chris Lattner24943d22010-06-08 16:52:24 +00003370 }
3371 return false;
3372}
3373
3374
3375uint32_t
3376ObjectFileMachO::GetDependentModules (FileSpecList& files)
3377{
Chris Lattner24943d22010-06-08 16:52:24 +00003378 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003379 ModuleSP module_sp(GetModule());
3380 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003381 {
Greg Clayton9482f052012-03-13 23:14:29 +00003382 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3383 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003384 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003385 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3386 uint32_t i;
3387 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003388 {
Greg Clayton9482f052012-03-13 23:14:29 +00003389 const uint32_t cmd_offset = offset;
3390 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3391 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003392
Greg Clayton9482f052012-03-13 23:14:29 +00003393 switch (load_cmd.cmd)
3394 {
3395 case LoadCommandDylibLoad:
3396 case LoadCommandDylibLoadWeak:
3397 case LoadCommandDylibReexport:
3398 case LoadCommandDynamicLinkerLoad:
3399 case LoadCommandFixedVMShlibLoad:
3400 case LoadCommandDylibLoadUpward:
3401 {
3402 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3403 const char *path = m_data.PeekCStr(name_offset);
3404 // Skip any path that starts with '@' since these are usually:
3405 // @executable_path/.../file
3406 // @rpath/.../file
3407 if (path && path[0] != '@')
3408 {
3409 FileSpec file_spec(path, resolve_path);
3410 if (files.AppendIfUnique(file_spec))
3411 count++;
3412 }
3413 }
3414 break;
3415
3416 default:
3417 break;
3418 }
3419 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003420 }
Chris Lattner24943d22010-06-08 16:52:24 +00003421 }
3422 return count;
3423}
3424
Jim Ingham28775942011-03-07 23:44:08 +00003425lldb_private::Address
3426ObjectFileMachO::GetEntryPointAddress ()
3427{
3428 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3429 // is initialized to an invalid address, so we can just return that.
3430 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
3431
3432 if (!IsExecutable() || m_entry_point_address.IsValid())
3433 return m_entry_point_address;
3434
3435 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
3436 // /usr/include/mach-o.h, but it is basically:
3437 //
3438 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3439 // uint32_t count - this is the count of longs in the thread state data
3440 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3441 // <repeat this trio>
3442 //
3443 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3444 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3445 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3446 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3447 //
3448 // For now we hard-code the offsets and flavors we need:
3449 //
3450 //
3451
Greg Clayton9482f052012-03-13 23:14:29 +00003452 ModuleSP module_sp(GetModule());
3453 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003454 {
Greg Clayton9482f052012-03-13 23:14:29 +00003455 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3456 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003457 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003458 uint32_t i;
3459 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3460 bool done = false;
3461
3462 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003463 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003464 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003465 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3466 break;
3467
3468 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003469 {
Greg Clayton9482f052012-03-13 23:14:29 +00003470 case LoadCommandUnixThread:
3471 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003472 {
Greg Clayton9482f052012-03-13 23:14:29 +00003473 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003474 {
Greg Clayton9482f052012-03-13 23:14:29 +00003475 uint32_t flavor = m_data.GetU32(&offset);
3476 uint32_t count = m_data.GetU32(&offset);
3477 if (count == 0)
3478 {
3479 // We've gotten off somehow, log and exit;
3480 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003481 }
Greg Clayton9482f052012-03-13 23:14:29 +00003482
3483 switch (m_header.cputype)
3484 {
3485 case llvm::MachO::CPUTypeARM:
3486 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3487 {
3488 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3489 start_address = m_data.GetU32(&offset);
3490 done = true;
3491 }
Jim Ingham28775942011-03-07 23:44:08 +00003492 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003493 case llvm::MachO::CPUTypeI386:
3494 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3495 {
3496 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3497 start_address = m_data.GetU32(&offset);
3498 done = true;
3499 }
3500 break;
3501 case llvm::MachO::CPUTypeX86_64:
3502 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3503 {
3504 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3505 start_address = m_data.GetU64(&offset);
3506 done = true;
3507 }
3508 break;
3509 default:
3510 return m_entry_point_address;
3511 }
3512 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3513 if (done)
3514 break;
3515 offset += count * 4;
3516 }
Jim Ingham28775942011-03-07 23:44:08 +00003517 }
Greg Clayton9482f052012-03-13 23:14:29 +00003518 break;
3519 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003520 {
Greg Clayton9482f052012-03-13 23:14:29 +00003521 ConstString text_segment_name ("__TEXT");
3522 uint64_t entryoffset = m_data.GetU64(&offset);
3523 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3524 if (text_segment_sp)
3525 {
3526 done = true;
3527 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3528 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003529 }
Greg Clayton9482f052012-03-13 23:14:29 +00003530
3531 default:
3532 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003533 }
Greg Clayton9482f052012-03-13 23:14:29 +00003534 if (done)
3535 break;
Jim Ingham28775942011-03-07 23:44:08 +00003536
Greg Clayton9482f052012-03-13 23:14:29 +00003537 // Go to the next load command:
3538 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003539 }
Jim Ingham28775942011-03-07 23:44:08 +00003540
Greg Clayton9482f052012-03-13 23:14:29 +00003541 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003542 {
Greg Clayton9482f052012-03-13 23:14:29 +00003543 // We got the start address from the load commands, so now resolve that address in the sections
3544 // of this ObjectFile:
3545 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003546 {
Greg Clayton9482f052012-03-13 23:14:29 +00003547 m_entry_point_address.Clear();
3548 }
3549 }
3550 else
3551 {
3552 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3553 // "start" symbol in the main executable.
3554
3555 ModuleSP module_sp (GetModule());
3556
3557 if (module_sp)
3558 {
3559 SymbolContextList contexts;
3560 SymbolContext context;
3561 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3562 {
3563 if (contexts.GetContextAtIndex(0, context))
3564 m_entry_point_address = context.symbol->GetAddress();
3565 }
Greg Clayton3508c382012-02-24 01:59:29 +00003566 }
3567 }
Jim Ingham28775942011-03-07 23:44:08 +00003568 }
3569
3570 return m_entry_point_address;
3571
3572}
3573
Greg Claytonb5a8f142012-02-05 02:38:54 +00003574lldb_private::Address
3575ObjectFileMachO::GetHeaderAddress ()
3576{
3577 lldb_private::Address header_addr;
3578 SectionList *section_list = GetSectionList();
3579 if (section_list)
3580 {
3581 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3582 if (text_segment_sp)
3583 {
Greg Clayton3508c382012-02-24 01:59:29 +00003584 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003585 header_addr.SetOffset (0);
3586 }
3587 }
3588 return header_addr;
3589}
3590
Greg Clayton46c9a352012-02-09 06:16:32 +00003591uint32_t
3592ObjectFileMachO::GetNumThreadContexts ()
3593{
Greg Clayton9482f052012-03-13 23:14:29 +00003594 ModuleSP module_sp(GetModule());
3595 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003596 {
Greg Clayton9482f052012-03-13 23:14:29 +00003597 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3598 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003599 {
Greg Clayton9482f052012-03-13 23:14:29 +00003600 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003601 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003602 FileRangeArray::Entry file_range;
3603 thread_command thread_cmd;
3604 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003605 {
Greg Clayton9482f052012-03-13 23:14:29 +00003606 const uint32_t cmd_offset = offset;
3607 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3608 break;
3609
3610 if (thread_cmd.cmd == LoadCommandThread)
3611 {
3612 file_range.SetRangeBase (offset);
3613 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3614 m_thread_context_offsets.Append (file_range);
3615 }
3616 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003617 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003618 }
3619 }
3620 return m_thread_context_offsets.GetSize();
3621}
3622
3623lldb::RegisterContextSP
3624ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3625{
Greg Clayton46c9a352012-02-09 06:16:32 +00003626 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003627
Greg Clayton9482f052012-03-13 23:14:29 +00003628 ModuleSP module_sp(GetModule());
3629 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003630 {
Greg Clayton9482f052012-03-13 23:14:29 +00003631 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3632 if (!m_thread_context_offsets_valid)
3633 GetNumThreadContexts ();
3634
3635 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003636 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003637 {
Jim Ingham6f01c932012-10-12 17:34:26 +00003638
3639 DataExtractor data (m_data,
3640 thread_context_file_range->GetRangeBase(),
3641 thread_context_file_range->GetByteSize());
3642
3643 switch (m_header.cputype)
3644 {
3645 case llvm::MachO::CPUTypeARM:
3646 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3647 break;
3648
3649 case llvm::MachO::CPUTypeI386:
3650 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3651 break;
3652
3653 case llvm::MachO::CPUTypeX86_64:
3654 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3655 break;
3656 }
Greg Clayton9482f052012-03-13 23:14:29 +00003657 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003658 }
3659 return reg_ctx_sp;
3660}
3661
Greg Claytonb5a8f142012-02-05 02:38:54 +00003662
Greg Claytonca319972011-07-09 00:41:34 +00003663ObjectFile::Type
3664ObjectFileMachO::CalculateType()
3665{
3666 switch (m_header.filetype)
3667 {
3668 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3669 if (GetAddressByteSize () == 4)
3670 {
3671 // 32 bit kexts are just object files, but they do have a valid
3672 // UUID load command.
3673 UUID uuid;
3674 if (GetUUID(&uuid))
3675 {
3676 // this checking for the UUID load command is not enough
3677 // we could eventually look for the symbol named
3678 // "OSKextGetCurrentIdentifier" as this is required of kexts
3679 if (m_strata == eStrataInvalid)
3680 m_strata = eStrataKernel;
3681 return eTypeSharedLibrary;
3682 }
3683 }
3684 return eTypeObjectFile;
3685
3686 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3687 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3688 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3689 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3690 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3691 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3692 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3693 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3694 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3695 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3696 default:
3697 break;
3698 }
3699 return eTypeUnknown;
3700}
3701
3702ObjectFile::Strata
3703ObjectFileMachO::CalculateStrata()
3704{
3705 switch (m_header.filetype)
3706 {
3707 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3708 {
3709 // 32 bit kexts are just object files, but they do have a valid
3710 // UUID load command.
3711 UUID uuid;
3712 if (GetUUID(&uuid))
3713 {
3714 // this checking for the UUID load command is not enough
3715 // we could eventually look for the symbol named
3716 // "OSKextGetCurrentIdentifier" as this is required of kexts
3717 if (m_type == eTypeInvalid)
3718 m_type = eTypeSharedLibrary;
3719
3720 return eStrataKernel;
3721 }
3722 }
3723 return eStrataUnknown;
3724
3725 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3726 // Check for the MH_DYLDLINK bit in the flags
3727 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003728 {
Greg Claytonca319972011-07-09 00:41:34 +00003729 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003730 }
3731 else
3732 {
3733 SectionList *section_list = GetSectionList();
3734 if (section_list)
3735 {
3736 static ConstString g_kld_section_name ("__KLD");
3737 if (section_list->FindSectionByName(g_kld_section_name))
3738 return eStrataKernel;
3739 }
3740 }
3741 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00003742
3743 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
3744 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00003745 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00003746 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
3747 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
3748 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
3749 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
3750 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
3751 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
3752 default:
3753 break;
3754 }
3755 return eStrataUnknown;
3756}
3757
3758
Greg Clayton49f4bf22012-02-22 19:41:02 +00003759uint32_t
3760ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
3761{
Greg Clayton9482f052012-03-13 23:14:29 +00003762 ModuleSP module_sp(GetModule());
3763 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003764 {
Greg Clayton9482f052012-03-13 23:14:29 +00003765 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3766 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003767 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003768 uint32_t version_cmd = 0;
3769 uint64_t version = 0;
3770 uint32_t i;
3771 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003772 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003773 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003774 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3775 break;
3776
3777 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003778 {
Greg Clayton9482f052012-03-13 23:14:29 +00003779 if (version_cmd == 0)
3780 {
3781 version_cmd = load_cmd.cmd;
3782 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
3783 break;
3784 version = load_cmd.dylib.current_version;
3785 }
3786 break; // Break for now unless there is another more complete version
3787 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00003788 }
Greg Clayton9482f052012-03-13 23:14:29 +00003789 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003790 }
Greg Clayton9482f052012-03-13 23:14:29 +00003791
3792 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003793 {
Greg Clayton9482f052012-03-13 23:14:29 +00003794 if (versions != NULL && num_versions > 0)
3795 {
3796 if (num_versions > 0)
3797 versions[0] = (version & 0xFFFF0000ull) >> 16;
3798 if (num_versions > 1)
3799 versions[1] = (version & 0x0000FF00ull) >> 8;
3800 if (num_versions > 2)
3801 versions[2] = (version & 0x000000FFull);
3802 // Fill in an remaining version numbers with invalid values
3803 for (i=3; i<num_versions; ++i)
3804 versions[i] = UINT32_MAX;
3805 }
3806 // The LC_ID_DYLIB load command has a version with 3 version numbers
3807 // in it, so always return 3
3808 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003809 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00003810 }
3811 return false;
3812}
3813
Chris Lattner24943d22010-06-08 16:52:24 +00003814bool
Greg Clayton395fc332011-02-15 21:59:32 +00003815ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00003816{
Greg Clayton9482f052012-03-13 23:14:29 +00003817 ModuleSP module_sp(GetModule());
3818 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003819 {
Greg Clayton9482f052012-03-13 23:14:29 +00003820 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3821 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
3822
3823 // Files with type MH_PRELOAD are currently used in cases where the image
3824 // debugs at the addresses in the file itself. Below we set the OS to
3825 // unknown to make sure we use the DynamicLoaderStatic()...
3826 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
3827 {
3828 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
3829 }
3830 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003831 }
Greg Clayton9482f052012-03-13 23:14:29 +00003832 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003833}
3834
3835
3836//------------------------------------------------------------------
3837// PluginInterface protocol
3838//------------------------------------------------------------------
3839const char *
3840ObjectFileMachO::GetPluginName()
3841{
3842 return "ObjectFileMachO";
3843}
3844
3845const char *
3846ObjectFileMachO::GetShortPluginName()
3847{
3848 return GetPluginNameStatic();
3849}
3850
3851uint32_t
3852ObjectFileMachO::GetPluginVersion()
3853{
3854 return 1;
3855}
3856