blob: 60609d1fe544b769a854e3b4a8f846a21c51f5f9 [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
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Core/ArchSpec.h"
16#include "lldb/Core/DataBuffer.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/FileSpecList.h"
18#include "lldb/Core/Module.h"
19#include "lldb/Core/PluginManager.h"
Greg Clayton6f7f8da2012-04-24 03:06:13 +000020#include "lldb/Core/RangeMap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Section.h"
22#include "lldb/Core/StreamFile.h"
23#include "lldb/Core/StreamString.h"
24#include "lldb/Core/Timer.h"
25#include "lldb/Core/UUID.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000026#include "lldb/Host/Host.h"
27#include "lldb/Host/FileSpec.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000028#include "lldb/Symbol/ClangNamespaceDecl.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Symbol/ObjectFile.h"
Greg Clayton29021d32012-04-18 05:19:20 +000030#include "lldb/Target/Platform.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000031#include "lldb/Target/Process.h"
Greg Clayton29021d32012-04-18 05:19:20 +000032#include "lldb/Target/Target.h"
Greg Clayton9ce95382012-02-13 23:10:39 +000033#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
34#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Clayton46c9a352012-02-09 06:16:32 +000035#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner24943d22010-06-08 16:52:24 +000036
Chris Lattner24943d22010-06-08 16:52:24 +000037using namespace lldb;
38using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000039using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000040
Greg Clayton46c9a352012-02-09 06:16:32 +000041class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
42{
43public:
44 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
45 RegisterContextDarwin_x86_64 (thread, 0)
46 {
47 SetRegisterDataFrom_LC_THREAD (data);
48 }
49
50 virtual void
51 InvalidateAllRegisters ()
52 {
53 // Do nothing... registers are always valid...
54 }
55
56 void
57 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
58 {
Greg Clayton46c9a352012-02-09 06:16:32 +000059 uint32_t offset = 0;
60 SetError (GPRRegSet, Read, -1);
61 SetError (FPURegSet, Read, -1);
62 SetError (EXCRegSet, Read, -1);
Greg Clayton9ce95382012-02-13 23:10:39 +000063 bool done = false;
64
65 while (!done)
Greg Clayton46c9a352012-02-09 06:16:32 +000066 {
Greg Clayton9ce95382012-02-13 23:10:39 +000067 int flavor = data.GetU32 (&offset);
68 if (flavor == 0)
69 done = true;
70 else
Greg Clayton46c9a352012-02-09 06:16:32 +000071 {
Greg Clayton9ce95382012-02-13 23:10:39 +000072 uint32_t i;
73 uint32_t count = data.GetU32 (&offset);
74 switch (flavor)
75 {
76 case GPRRegSet:
77 for (i=0; i<count; ++i)
78 (&gpr.rax)[i] = data.GetU64(&offset);
79 SetError (GPRRegSet, Read, 0);
80 done = true;
81
82 break;
83 case FPURegSet:
84 // TODO: fill in FPU regs....
85 //SetError (FPURegSet, Read, -1);
86 done = true;
87
88 break;
89 case EXCRegSet:
90 exc.trapno = data.GetU32(&offset);
91 exc.err = data.GetU32(&offset);
92 exc.faultvaddr = data.GetU64(&offset);
93 SetError (EXCRegSet, Read, 0);
94 done = true;
95 break;
96 case 7:
97 case 8:
98 case 9:
99 // fancy flavors that encapsulate of the the above
100 // falvors...
101 break;
102
103 default:
104 done = true;
105 break;
106 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000107 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000108 }
109 }
110protected:
111 virtual int
112 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
113 {
114 return 0;
115 }
116
117 virtual int
118 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
119 {
120 return 0;
121 }
122
123 virtual int
124 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
125 {
126 return 0;
127 }
128
129 virtual int
130 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
131 {
132 return 0;
133 }
134
135 virtual int
136 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
137 {
138 return 0;
139 }
140
141 virtual int
142 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
143 {
144 return 0;
145 }
146};
Greg Clayton46c9a352012-02-09 06:16:32 +0000147
Greg Clayton9ce95382012-02-13 23:10:39 +0000148
149class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
150{
151public:
152 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
153 RegisterContextDarwin_i386 (thread, 0)
154 {
155 SetRegisterDataFrom_LC_THREAD (data);
156 }
157
158 virtual void
159 InvalidateAllRegisters ()
160 {
161 // Do nothing... registers are always valid...
162 }
163
164 void
165 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
166 {
167 uint32_t offset = 0;
168 SetError (GPRRegSet, Read, -1);
169 SetError (FPURegSet, Read, -1);
170 SetError (EXCRegSet, Read, -1);
171 bool done = false;
172
173 while (!done)
174 {
175 int flavor = data.GetU32 (&offset);
176 if (flavor == 0)
177 done = true;
178 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000179 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000180 uint32_t i;
181 uint32_t count = data.GetU32 (&offset);
182 switch (flavor)
183 {
184 case GPRRegSet:
185 for (i=0; i<count; ++i)
186 (&gpr.eax)[i] = data.GetU32(&offset);
187 SetError (GPRRegSet, Read, 0);
188 done = true;
189
190 break;
191 case FPURegSet:
192 // TODO: fill in FPU regs....
193 //SetError (FPURegSet, Read, -1);
194 done = true;
195
196 break;
197 case EXCRegSet:
198 exc.trapno = data.GetU32(&offset);
199 exc.err = data.GetU32(&offset);
200 exc.faultvaddr = data.GetU32(&offset);
201 SetError (EXCRegSet, Read, 0);
202 done = true;
203 break;
204 case 7:
205 case 8:
206 case 9:
207 // fancy flavors that encapsulate of the the above
208 // falvors...
209 break;
210
211 default:
212 done = true;
213 break;
214 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000215 }
216 }
217 }
218protected:
219 virtual int
220 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
221 {
222 return 0;
223 }
224
225 virtual int
226 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
227 {
228 return 0;
229 }
230
231 virtual int
232 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
233 {
234 return 0;
235 }
236
237 virtual int
238 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
239 {
240 return 0;
241 }
242
243 virtual int
244 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
245 {
246 return 0;
247 }
248
249 virtual int
250 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
251 {
252 return 0;
253 }
254};
255
Greg Clayton9ce95382012-02-13 23:10:39 +0000256class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
257{
258public:
259 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
260 RegisterContextDarwin_arm (thread, 0)
261 {
262 SetRegisterDataFrom_LC_THREAD (data);
263 }
264
265 virtual void
266 InvalidateAllRegisters ()
267 {
268 // Do nothing... registers are always valid...
269 }
270
271 void
272 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
273 {
274 uint32_t offset = 0;
275 SetError (GPRRegSet, Read, -1);
276 SetError (FPURegSet, Read, -1);
277 SetError (EXCRegSet, Read, -1);
278 int flavor = data.GetU32 (&offset);
279 uint32_t count = data.GetU32 (&offset);
280 switch (flavor)
281 {
282 case GPRRegSet:
283 for (uint32_t i=0; i<count; ++i)
284 gpr.r[i] = data.GetU32(&offset);
285 SetError (GPRRegSet, Read, 0);
286 break;
287 case FPURegSet:
288 // TODO: fill in FPU regs....
289 //SetError (FPURegSet, Read, -1);
290 break;
291 case EXCRegSet:
292 exc.exception = data.GetU32(&offset);
293 exc.fsr = data.GetU32(&offset);
294 exc.far = data.GetU32(&offset);
295 SetError (EXCRegSet, Read, 0);
296 break;
297 }
298 }
299protected:
300 virtual int
301 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
302 {
303 return 0;
304 }
305
306 virtual int
307 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
308 {
309 return 0;
310 }
311
312 virtual int
313 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
314 {
315 return 0;
316 }
317
318 virtual int
319 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
320 {
321 return 0;
322 }
323
324 virtual int
325 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
326 {
327 return 0;
328 }
329
330 virtual int
331 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
332 {
333 return 0;
334 }
335};
336
Greg Claytonb1888f22011-03-19 01:12:21 +0000337#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000338
339void
340ObjectFileMachO::Initialize()
341{
342 PluginManager::RegisterPlugin (GetPluginNameStatic(),
343 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000344 CreateInstance,
345 CreateMemoryInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000346}
347
348void
349ObjectFileMachO::Terminate()
350{
351 PluginManager::UnregisterPlugin (CreateInstance);
352}
353
354
355const char *
356ObjectFileMachO::GetPluginNameStatic()
357{
358 return "object-file.mach-o";
359}
360
361const char *
362ObjectFileMachO::GetPluginDescriptionStatic()
363{
364 return "Mach-o object file reader (32 and 64 bit)";
365}
366
367
368ObjectFile *
Greg Clayton3508c382012-02-24 01:59:29 +0000369ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp, DataBufferSP& data_sp, const FileSpec* file, addr_t offset, addr_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000370{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000371 if (ObjectFileMachO::MagicBytesMatch(data_sp, offset, length))
Chris Lattner24943d22010-06-08 16:52:24 +0000372 {
Greg Clayton3508c382012-02-24 01:59:29 +0000373 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, file, offset, length));
Chris Lattner24943d22010-06-08 16:52:24 +0000374 if (objfile_ap.get() && objfile_ap->ParseHeader())
375 return objfile_ap.release();
376 }
377 return NULL;
378}
379
Greg Claytonb5a8f142012-02-05 02:38:54 +0000380ObjectFile *
Greg Clayton3508c382012-02-24 01:59:29 +0000381ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000382 DataBufferSP& data_sp,
383 const ProcessSP &process_sp,
384 lldb::addr_t header_addr)
385{
386 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
387 {
Greg Clayton3508c382012-02-24 01:59:29 +0000388 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000389 if (objfile_ap.get() && objfile_ap->ParseHeader())
390 return objfile_ap.release();
391 }
392 return NULL;
393}
394
395
396const ConstString &
397ObjectFileMachO::GetSegmentNameTEXT()
398{
399 static ConstString g_segment_name_TEXT ("__TEXT");
400 return g_segment_name_TEXT;
401}
402
403const ConstString &
404ObjectFileMachO::GetSegmentNameDATA()
405{
406 static ConstString g_segment_name_DATA ("__DATA");
407 return g_segment_name_DATA;
408}
409
410const ConstString &
411ObjectFileMachO::GetSegmentNameOBJC()
412{
413 static ConstString g_segment_name_OBJC ("__OBJC");
414 return g_segment_name_OBJC;
415}
416
417const ConstString &
418ObjectFileMachO::GetSegmentNameLINKEDIT()
419{
420 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
421 return g_section_name_LINKEDIT;
422}
423
424const ConstString &
425ObjectFileMachO::GetSectionNameEHFrame()
426{
427 static ConstString g_section_name_eh_frame ("__eh_frame");
428 return g_section_name_eh_frame;
429}
430
431
Chris Lattner24943d22010-06-08 16:52:24 +0000432
433static uint32_t
434MachHeaderSizeFromMagic(uint32_t magic)
435{
436 switch (magic)
437 {
Greg Clayton1674b122010-07-21 22:12:05 +0000438 case HeaderMagic32:
439 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000440 return sizeof(struct mach_header);
441
Greg Clayton1674b122010-07-21 22:12:05 +0000442 case HeaderMagic64:
443 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000444 return sizeof(struct mach_header_64);
445 break;
446
447 default:
448 break;
449 }
450 return 0;
451}
452
453
454bool
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000455ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
456 lldb::addr_t data_offset,
457 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000458{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000459 DataExtractor data;
460 data.SetData (data_sp, data_offset, data_length);
Chris Lattner24943d22010-06-08 16:52:24 +0000461 uint32_t offset = 0;
462 uint32_t magic = data.GetU32(&offset);
463 return MachHeaderSizeFromMagic(magic) != 0;
464}
465
466
Greg Clayton3508c382012-02-24 01:59:29 +0000467ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp, DataBufferSP& data_sp, const FileSpec* file, addr_t offset, addr_t length) :
468 ObjectFile(module_sp, file, offset, length, data_sp),
Chris Lattner24943d22010-06-08 16:52:24 +0000469 m_sections_ap(),
Jim Ingham28775942011-03-07 23:44:08 +0000470 m_symtab_ap(),
Greg Clayton46c9a352012-02-09 06:16:32 +0000471 m_mach_segments(),
472 m_mach_sections(),
473 m_entry_point_address(),
474 m_thread_context_offsets(),
475 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000476{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000477 ::memset (&m_header, 0, sizeof(m_header));
478 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000479}
480
Greg Clayton3508c382012-02-24 01:59:29 +0000481ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000482 lldb::DataBufferSP& header_data_sp,
483 const lldb::ProcessSP &process_sp,
484 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000485 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000486 m_sections_ap(),
487 m_symtab_ap(),
Greg Clayton46c9a352012-02-09 06:16:32 +0000488 m_mach_segments(),
489 m_mach_sections(),
490 m_entry_point_address(),
491 m_thread_context_offsets(),
492 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000493{
494 ::memset (&m_header, 0, sizeof(m_header));
495 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
496}
Chris Lattner24943d22010-06-08 16:52:24 +0000497
498ObjectFileMachO::~ObjectFileMachO()
499{
500}
501
502
503bool
504ObjectFileMachO::ParseHeader ()
505{
Greg Clayton9482f052012-03-13 23:14:29 +0000506 ModuleSP module_sp(GetModule());
507 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000508 {
Greg Clayton9482f052012-03-13 23:14:29 +0000509 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
510 bool can_parse = false;
511 uint32_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000512 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000513 // Leave magic in the original byte order
514 m_header.magic = m_data.GetU32(&offset);
515 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000516 {
Greg Clayton9482f052012-03-13 23:14:29 +0000517 case HeaderMagic32:
518 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
519 m_data.SetAddressByteSize(4);
520 can_parse = true;
521 break;
522
523 case HeaderMagic64:
524 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
525 m_data.SetAddressByteSize(8);
526 can_parse = true;
527 break;
528
529 case HeaderMagic32Swapped:
530 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
531 m_data.SetAddressByteSize(4);
532 can_parse = true;
533 break;
534
535 case HeaderMagic64Swapped:
536 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
537 m_data.SetAddressByteSize(8);
538 can_parse = true;
539 break;
540
541 default:
542 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000543 }
Greg Clayton9482f052012-03-13 23:14:29 +0000544
545 if (can_parse)
546 {
547 m_data.GetU32(&offset, &m_header.cputype, 6);
548
549 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
550
551 if (SetModulesArchitecture (mach_arch))
552 {
553 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
554 if (m_data.GetByteSize() < header_and_lc_size)
555 {
556 DataBufferSP data_sp;
557 ProcessSP process_sp (m_process_wp.lock());
558 if (process_sp)
559 {
560 data_sp = ReadMemory (process_sp, m_offset, header_and_lc_size);
561 }
562 else
563 {
564 // Read in all only the load command data from the file on disk
565 data_sp = m_file.ReadFileContents(m_offset, header_and_lc_size);
566 if (data_sp->GetByteSize() != header_and_lc_size)
567 return false;
568 }
569 if (data_sp)
570 m_data.SetData (data_sp);
571 }
572 }
573 return true;
574 }
575 else
576 {
577 memset(&m_header, 0, sizeof(struct mach_header));
578 }
Chris Lattner24943d22010-06-08 16:52:24 +0000579 }
580 return false;
581}
582
583
584ByteOrder
585ObjectFileMachO::GetByteOrder () const
586{
Chris Lattner24943d22010-06-08 16:52:24 +0000587 return m_data.GetByteOrder ();
588}
589
Jim Ingham7508e732010-08-09 23:31:02 +0000590bool
591ObjectFileMachO::IsExecutable() const
592{
593 return m_header.filetype == HeaderFileTypeExecutable;
594}
Chris Lattner24943d22010-06-08 16:52:24 +0000595
596size_t
597ObjectFileMachO::GetAddressByteSize () const
598{
Chris Lattner24943d22010-06-08 16:52:24 +0000599 return m_data.GetAddressByteSize ();
600}
601
Greg Claytonb3448432011-03-24 21:19:54 +0000602AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000603ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
604{
605 Symtab *symtab = GetSymtab();
606 if (symtab)
607 {
608 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
609 if (symbol)
610 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000611 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000612 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000613 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000614 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000615 {
Greg Clayton3508c382012-02-24 01:59:29 +0000616 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000617 switch (section_type)
618 {
619 case eSectionTypeInvalid: return eAddressClassUnknown;
620 case eSectionTypeCode:
621 if (m_header.cputype == llvm::MachO::CPUTypeARM)
622 {
623 // For ARM we have a bit in the n_desc field of the symbol
624 // that tells us ARM/Thumb which is bit 0x0008.
625 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
626 return eAddressClassCodeAlternateISA;
627 }
628 return eAddressClassCode;
629
630 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000631 case eSectionTypeData:
632 case eSectionTypeDataCString:
633 case eSectionTypeDataCStringPointers:
634 case eSectionTypeDataSymbolAddress:
635 case eSectionTypeData4:
636 case eSectionTypeData8:
637 case eSectionTypeData16:
638 case eSectionTypeDataPointers:
639 case eSectionTypeZeroFill:
640 case eSectionTypeDataObjCMessageRefs:
641 case eSectionTypeDataObjCCFStrings:
642 return eAddressClassData;
643 case eSectionTypeDebug:
644 case eSectionTypeDWARFDebugAbbrev:
645 case eSectionTypeDWARFDebugAranges:
646 case eSectionTypeDWARFDebugFrame:
647 case eSectionTypeDWARFDebugInfo:
648 case eSectionTypeDWARFDebugLine:
649 case eSectionTypeDWARFDebugLoc:
650 case eSectionTypeDWARFDebugMacInfo:
651 case eSectionTypeDWARFDebugPubNames:
652 case eSectionTypeDWARFDebugPubTypes:
653 case eSectionTypeDWARFDebugRanges:
654 case eSectionTypeDWARFDebugStr:
655 case eSectionTypeDWARFAppleNames:
656 case eSectionTypeDWARFAppleTypes:
657 case eSectionTypeDWARFAppleNamespaces:
658 case eSectionTypeDWARFAppleObjC:
659 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000660 case eSectionTypeEHFrame: return eAddressClassRuntime;
661 case eSectionTypeOther: return eAddressClassUnknown;
662 }
663 }
664 }
665
Greg Claytonb3448432011-03-24 21:19:54 +0000666 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000667 switch (symbol_type)
668 {
669 case eSymbolTypeAny: return eAddressClassUnknown;
670 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Greg Claytonb1888f22011-03-19 01:12:21 +0000671
672 case eSymbolTypeCode:
673 case eSymbolTypeTrampoline:
674 if (m_header.cputype == llvm::MachO::CPUTypeARM)
675 {
676 // For ARM we have a bit in the n_desc field of the symbol
677 // that tells us ARM/Thumb which is bit 0x0008.
678 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
679 return eAddressClassCodeAlternateISA;
680 }
681 return eAddressClassCode;
682
683 case eSymbolTypeData: return eAddressClassData;
684 case eSymbolTypeRuntime: return eAddressClassRuntime;
685 case eSymbolTypeException: return eAddressClassRuntime;
686 case eSymbolTypeSourceFile: return eAddressClassDebug;
687 case eSymbolTypeHeaderFile: return eAddressClassDebug;
688 case eSymbolTypeObjectFile: return eAddressClassDebug;
689 case eSymbolTypeCommonBlock: return eAddressClassDebug;
690 case eSymbolTypeBlock: return eAddressClassDebug;
691 case eSymbolTypeLocal: return eAddressClassData;
692 case eSymbolTypeParam: return eAddressClassData;
693 case eSymbolTypeVariable: return eAddressClassData;
694 case eSymbolTypeVariableType: return eAddressClassDebug;
695 case eSymbolTypeLineEntry: return eAddressClassDebug;
696 case eSymbolTypeLineHeader: return eAddressClassDebug;
697 case eSymbolTypeScopeBegin: return eAddressClassDebug;
698 case eSymbolTypeScopeEnd: return eAddressClassDebug;
699 case eSymbolTypeAdditional: return eAddressClassUnknown;
700 case eSymbolTypeCompiler: return eAddressClassDebug;
701 case eSymbolTypeInstrumentation:return eAddressClassDebug;
702 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000703 case eSymbolTypeObjCClass: return eAddressClassRuntime;
704 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
705 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000706 }
707 }
708 }
709 return eAddressClassUnknown;
710}
Chris Lattner24943d22010-06-08 16:52:24 +0000711
712Symtab *
713ObjectFileMachO::GetSymtab()
714{
Greg Clayton9482f052012-03-13 23:14:29 +0000715 ModuleSP module_sp(GetModule());
716 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000717 {
Greg Clayton9482f052012-03-13 23:14:29 +0000718 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
719 if (m_symtab_ap.get() == NULL)
720 {
721 m_symtab_ap.reset(new Symtab(this));
722 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
723 ParseSymtab (true);
724 m_symtab_ap->Finalize ();
725 }
Chris Lattner24943d22010-06-08 16:52:24 +0000726 }
727 return m_symtab_ap.get();
728}
729
730
731SectionList *
732ObjectFileMachO::GetSectionList()
733{
Greg Clayton9482f052012-03-13 23:14:29 +0000734 ModuleSP module_sp(GetModule());
735 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000736 {
Greg Clayton9482f052012-03-13 23:14:29 +0000737 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
738 if (m_sections_ap.get() == NULL)
739 {
740 m_sections_ap.reset(new SectionList());
741 ParseSections();
742 }
Chris Lattner24943d22010-06-08 16:52:24 +0000743 }
744 return m_sections_ap.get();
745}
746
747
748size_t
749ObjectFileMachO::ParseSections ()
750{
751 lldb::user_id_t segID = 0;
752 lldb::user_id_t sectID = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000753 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
754 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000755 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000756 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000757 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000758 // First look up any LC_ENCRYPTION_INFO load commands
759 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
760 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000761 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000762 for (i=0; i<m_header.ncmds; ++i)
763 {
764 const uint32_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000765 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000766 break;
767
Greg Clayton54e33712012-05-25 18:09:55 +0000768 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000769 {
Greg Clayton54e33712012-05-25 18:09:55 +0000770 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
771 {
772 if (encryption_cmd.cryptid != 0)
773 {
774 EncryptedFileRanges::Entry entry;
775 entry.SetRangeBase(encryption_cmd.cryptoff);
776 entry.SetByteSize(encryption_cmd.cryptsize);
777 encrypted_file_ranges.Append(entry);
778 }
779 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000780 }
Greg Clayton54e33712012-05-25 18:09:55 +0000781 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000782 }
783
784 offset = MachHeaderSizeFromMagic(m_header.magic);
785
Greg Clayton54e33712012-05-25 18:09:55 +0000786 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000787 for (i=0; i<m_header.ncmds; ++i)
788 {
789 const uint32_t load_cmd_offset = offset;
790 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
791 break;
792
Greg Clayton1674b122010-07-21 22:12:05 +0000793 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000794 {
795 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
796 {
797 load_cmd.vmaddr = m_data.GetAddress(&offset);
798 load_cmd.vmsize = m_data.GetAddress(&offset);
799 load_cmd.fileoff = m_data.GetAddress(&offset);
800 load_cmd.filesize = m_data.GetAddress(&offset);
801 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
802 {
Greg Clayton68ca8232011-01-25 02:58:48 +0000803
804 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
805
Chris Lattner24943d22010-06-08 16:52:24 +0000806 // Keep a list of mach segments around in case we need to
807 // get at data that isn't stored in the abstracted Sections.
808 m_mach_segments.push_back (load_cmd);
809
810 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
811 // Use a segment ID of the segment index shifted left by 8 so they
812 // never conflict with any of the sections.
813 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000814 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000815 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000816 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000817 ++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
818 segment_name, // Name of this section
819 eSectionTypeContainer, // This section is a container of other sections.
820 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
821 load_cmd.vmsize, // VM size in bytes of this section
822 load_cmd.fileoff, // Offset to the data for this section in the file
823 load_cmd.filesize, // Size in bytes of this section as found in the the file
824 load_cmd.flags)); // Flags for this section
825
Greg Clayton68ca8232011-01-25 02:58:48 +0000826 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000827 m_sections_ap->AddSection(segment_sp);
828 }
829
830 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000831 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000832 // Push a section into our mach sections for the section at
Greg Clayton6af4fad2010-10-06 01:26:32 +0000833 // index zero (NListSectionNoSection) if we don't have any
834 // mach sections yet...
835 if (m_mach_sections.empty())
836 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000837 uint32_t segment_sect_idx;
838 const lldb::user_id_t first_segment_sectID = sectID + 1;
839
840
Greg Clayton1674b122010-07-21 22:12:05 +0000841 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000842 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
843 {
844 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
845 break;
846 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
847 break;
848 sect64.addr = m_data.GetAddress(&offset);
849 sect64.size = m_data.GetAddress(&offset);
850
851 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
852 break;
853
854 // Keep a list of mach sections around in case we need to
855 // get at data that isn't stored in the abstracted Sections.
856 m_mach_sections.push_back (sect64);
857
858 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
859 if (!segment_name)
860 {
861 // We have a segment with no name so we need to conjure up
862 // segments that correspond to the section's segname if there
863 // isn't already such a section. If there is such a section,
864 // we resize the section so that it spans all sections.
865 // We also mark these sections as fake so address matches don't
866 // hit if they land in the gaps between the child sections.
867 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
868 segment_sp = m_sections_ap->FindSectionByName (segment_name);
869 if (segment_sp.get())
870 {
871 Section *segment = segment_sp.get();
872 // Grow the section size as needed.
873 const lldb::addr_t sect64_min_addr = sect64.addr;
874 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
875 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
876 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
877 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
878 if (sect64_min_addr >= curr_seg_min_addr)
879 {
880 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
881 // Only grow the section size if needed
882 if (new_seg_byte_size > curr_seg_byte_size)
883 segment->SetByteSize (new_seg_byte_size);
884 }
885 else
886 {
887 // We need to change the base address of the segment and
888 // adjust the child section offsets for all existing children.
889 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
890 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +0000891 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000892 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
893 }
Greg Clayton661825b2010-06-28 23:51:11 +0000894
895 // Grow the section size as needed.
896 if (sect64.offset)
897 {
898 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
899 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
900
901 const lldb::addr_t section_min_file_offset = sect64.offset;
902 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
903 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
904 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
905 segment->SetFileOffset (new_file_offset);
906 segment->SetFileSize (new_file_size);
907 }
Chris Lattner24943d22010-06-08 16:52:24 +0000908 }
909 else
910 {
911 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +0000912 segment_sp.reset(new Section (segment_sp, // Parent section
913 module_sp, // Module to which this section belongs
914 ++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
915 segment_name, // Name of this section
916 eSectionTypeContainer, // This section is a container of other sections.
917 sect64.addr, // File VM address == addresses as they are found in the object file
918 sect64.size, // VM size in bytes of this section
919 sect64.offset, // Offset to the data for this section in the file
920 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
921 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +0000922 segment_sp->SetIsFake(true);
923 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000924 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000925 }
926 }
927 assert (segment_sp.get());
928
Greg Clayton1674b122010-07-21 22:12:05 +0000929 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000930 static ConstString g_sect_name_objc_data ("__objc_data");
931 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
932 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
933 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
934 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
935 static ConstString g_sect_name_objc_const ("__objc_const");
936 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
937 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000938
939 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
940 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
941 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
942 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
943 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
944 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
945 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
946 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
947 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
948 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
949 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +0000950 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
951 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +0000952 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +0000953 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000954 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +0000955 static ConstString g_sect_name_DATA ("__DATA");
956 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000957
Chris Lattner24943d22010-06-08 16:52:24 +0000958 SectionType sect_type = eSectionTypeOther;
959
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000960 if (section_name == g_sect_name_dwarf_debug_abbrev)
961 sect_type = eSectionTypeDWARFDebugAbbrev;
962 else if (section_name == g_sect_name_dwarf_debug_aranges)
963 sect_type = eSectionTypeDWARFDebugAranges;
964 else if (section_name == g_sect_name_dwarf_debug_frame)
965 sect_type = eSectionTypeDWARFDebugFrame;
966 else if (section_name == g_sect_name_dwarf_debug_info)
967 sect_type = eSectionTypeDWARFDebugInfo;
968 else if (section_name == g_sect_name_dwarf_debug_line)
969 sect_type = eSectionTypeDWARFDebugLine;
970 else if (section_name == g_sect_name_dwarf_debug_loc)
971 sect_type = eSectionTypeDWARFDebugLoc;
972 else if (section_name == g_sect_name_dwarf_debug_macinfo)
973 sect_type = eSectionTypeDWARFDebugMacInfo;
974 else if (section_name == g_sect_name_dwarf_debug_pubnames)
975 sect_type = eSectionTypeDWARFDebugPubNames;
976 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
977 sect_type = eSectionTypeDWARFDebugPubTypes;
978 else if (section_name == g_sect_name_dwarf_debug_ranges)
979 sect_type = eSectionTypeDWARFDebugRanges;
980 else if (section_name == g_sect_name_dwarf_debug_str)
981 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +0000982 else if (section_name == g_sect_name_dwarf_apple_names)
983 sect_type = eSectionTypeDWARFAppleNames;
984 else if (section_name == g_sect_name_dwarf_apple_types)
985 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +0000986 else if (section_name == g_sect_name_dwarf_apple_namespaces)
987 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000988 else if (section_name == g_sect_name_dwarf_apple_objc)
989 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000990 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +0000991 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +0000992 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +0000993 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000994 else if (section_name == g_sect_name_eh_frame)
995 sect_type = eSectionTypeEHFrame;
996 else if (section_name == g_sect_name_cfstring)
997 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +0000998 else if (section_name == g_sect_name_objc_data ||
999 section_name == g_sect_name_objc_classrefs ||
1000 section_name == g_sect_name_objc_superrefs ||
1001 section_name == g_sect_name_objc_const ||
1002 section_name == g_sect_name_objc_classlist)
1003 {
1004 sect_type = eSectionTypeDataPointers;
1005 }
Chris Lattner24943d22010-06-08 16:52:24 +00001006
1007 if (sect_type == eSectionTypeOther)
1008 {
1009 switch (mach_sect_type)
1010 {
1011 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001012 case SectionTypeRegular:
1013 if (segment_sp->GetName() == g_sect_name_TEXT)
1014 sect_type = eSectionTypeCode;
1015 else if (segment_sp->GetName() == g_sect_name_DATA)
1016 sect_type = eSectionTypeData;
1017 else
1018 sect_type = eSectionTypeOther;
1019 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001020 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1021 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1022 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1023 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1024 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1025 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1026 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1027 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1028 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1029 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1030 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1031 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1032 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1033 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1034 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1035 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001036 default: break;
1037 }
1038 }
1039
Greg Clayton3508c382012-02-24 01:59:29 +00001040 SectionSP section_sp(new Section (segment_sp,
1041 module_sp,
1042 ++sectID,
1043 section_name,
1044 sect_type,
1045 sect64.addr - segment_sp->GetFileAddress(),
1046 sect64.size,
1047 sect64.offset,
1048 sect64.offset == 0 ? 0 : sect64.size,
1049 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001050 // Set the section to be encrypted to match the segment
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001051
1052 bool section_is_encrypted = false;
1053 if (!segment_is_encrypted && load_cmd.filesize != 0)
1054 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001055
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001056 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001057 segment_sp->GetChildren().AddSection(section_sp);
1058
1059 if (segment_sp->IsFake())
1060 {
1061 segment_sp.reset();
1062 segment_name.Clear();
1063 }
1064 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001065 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001066 {
1067 if (first_segment_sectID <= sectID)
1068 {
1069 lldb::user_id_t sect_uid;
1070 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1071 {
1072 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1073 SectionSP next_section_sp;
1074 if (sect_uid + 1 <= sectID)
1075 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1076
1077 if (curr_section_sp.get())
1078 {
1079 if (curr_section_sp->GetByteSize() == 0)
1080 {
1081 if (next_section_sp.get() != NULL)
1082 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1083 else
1084 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1085 }
1086 }
1087 }
1088 }
1089 }
1090 }
1091 }
1092 }
Greg Clayton1674b122010-07-21 22:12:05 +00001093 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001094 {
1095 m_dysymtab.cmd = load_cmd.cmd;
1096 m_dysymtab.cmdsize = load_cmd.cmdsize;
1097 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1098 }
1099
1100 offset = load_cmd_offset + load_cmd.cmdsize;
1101 }
1102// if (dump_sections)
1103// {
1104// StreamFile s(stdout);
1105// m_sections_ap->Dump(&s, true);
1106// }
1107 return sectID; // Return the number of sections we registered with the module
1108}
1109
1110class MachSymtabSectionInfo
1111{
1112public:
1113
1114 MachSymtabSectionInfo (SectionList *section_list) :
1115 m_section_list (section_list),
1116 m_section_infos()
1117 {
1118 // Get the number of sections down to a depth of 1 to include
1119 // all segments and their sections, but no other sections that
1120 // may be added for debug map or
1121 m_section_infos.resize(section_list->GetNumSections(1));
1122 }
1123
1124
Greg Clayton3508c382012-02-24 01:59:29 +00001125 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001126 GetSection (uint8_t n_sect, addr_t file_addr)
1127 {
1128 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001129 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001130 if (n_sect < m_section_infos.size())
1131 {
Greg Clayton3508c382012-02-24 01:59:29 +00001132 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001133 {
Greg Clayton3508c382012-02-24 01:59:29 +00001134 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1135 m_section_infos[n_sect].section_sp = section_sp;
1136 if (section_sp != NULL)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001137 {
Greg Clayton3508c382012-02-24 01:59:29 +00001138 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1139 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001140 }
1141 else
1142 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001143 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001144 }
Chris Lattner24943d22010-06-08 16:52:24 +00001145 }
1146 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001147 {
1148 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001149 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001150 }
1151 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1152 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1153 {
1154 // Symbol is in section with zero size, but has the same start
1155 // address as the section. This can happen with linker symbols
1156 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001157 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001158 }
Chris Lattner24943d22010-06-08 16:52:24 +00001159 }
Greg Clayton3508c382012-02-24 01:59:29 +00001160 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001161 }
1162
1163protected:
1164 struct SectionInfo
1165 {
1166 SectionInfo () :
1167 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001168 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001169 {
1170 }
1171
1172 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001173 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001174 };
1175 SectionList *m_section_list;
1176 std::vector<SectionInfo> m_section_infos;
1177};
1178
Chris Lattner24943d22010-06-08 16:52:24 +00001179size_t
1180ObjectFileMachO::ParseSymtab (bool minimize)
1181{
1182 Timer scoped_timer(__PRETTY_FUNCTION__,
1183 "ObjectFileMachO::ParseSymtab () module = %s",
1184 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001185 ModuleSP module_sp (GetModule());
1186 if (!module_sp)
1187 return 0;
1188
1189 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1190 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1191 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1192 FunctionStarts function_starts;
Chris Lattner24943d22010-06-08 16:52:24 +00001193 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1194 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001195
Greg Clayton0fea0512011-12-30 00:32:24 +00001196 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
1197
Chris Lattner24943d22010-06-08 16:52:24 +00001198 for (i=0; i<m_header.ncmds; ++i)
1199 {
1200 const uint32_t cmd_offset = offset;
1201 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001202 struct load_command lc;
1203 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001204 break;
1205 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001206 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001207 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001208 case LoadCommandSymtab:
1209 symtab_load_command.cmd = lc.cmd;
1210 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001211 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001212 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1213 return 0;
1214 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001215 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001216 if (log)
1217 module_sp->LogMessage(log.get(), "LC_SYMTAB.symoff == 0");
1218 return 0;
1219 }
1220
1221 if (symtab_load_command.stroff == 0)
1222 {
1223 if (log)
1224 module_sp->LogMessage(log.get(), "LC_SYMTAB.stroff == 0");
1225 return 0;
1226 }
1227
1228 if (symtab_load_command.nsyms == 0)
1229 {
1230 if (log)
1231 module_sp->LogMessage(log.get(), "LC_SYMTAB.nsyms == 0");
1232 return 0;
1233 }
1234
1235 if (symtab_load_command.strsize == 0)
1236 {
1237 if (log)
1238 module_sp->LogMessage(log.get(), "LC_SYMTAB.strsize == 0");
1239 return 0;
1240 }
1241 break;
1242
1243 case LoadCommandFunctionStarts:
1244 function_starts_load_command.cmd = lc.cmd;
1245 function_starts_load_command.cmdsize = lc.cmdsize;
1246 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1247 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1248 break;
1249
1250 default:
1251 break;
1252 }
1253 offset = cmd_offset + lc.cmdsize;
1254 }
1255
1256 if (symtab_load_command.cmd)
1257 {
1258 Symtab *symtab = m_symtab_ap.get();
1259 SectionList *section_list = GetSectionList();
1260 if (section_list == NULL)
1261 return 0;
1262
1263 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001264 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001265
1266 const size_t addr_byte_size = m_data.GetAddressByteSize();
1267 bool bit_width_32 = addr_byte_size == 4;
1268 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1269
1270 DataExtractor nlist_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1271 DataExtractor strtab_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1272 DataExtractor function_starts_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1273
1274 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1275 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001276 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1277 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001278 {
Greg Claytondd29b972012-05-18 23:20:01 +00001279 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001280 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1281 // Reading mach file from memory in a process or core file...
1282
1283 if (linkedit_section_sp)
1284 {
1285 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1286 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1287 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001288 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001289
1290 bool data_was_read = false;
1291
1292#if defined (__APPLE__) && defined (__arm__)
1293 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001294 {
Greg Clayton29021d32012-04-18 05:19:20 +00001295 // This mach-o memory file is in the dyld shared cache. If this
1296 // program is not remote and this is iOS, then this process will
1297 // share the same shared cache as the process we are debugging and
1298 // we can read the entire __LINKEDIT from the address space in this
1299 // process. This is a needed optimization that is used for local iOS
1300 // debugging only since all shared libraries in the shared cache do
1301 // not have corresponding files that exist in the file system of the
1302 // device. They have been combined into a single file. This means we
1303 // always have to load these files from memory. All of the symbol and
1304 // string tables from all of the __LINKEDIT sections from the shared
1305 // libraries in the shared cache have been merged into a single large
1306 // symbol and string table. Reading all of this symbol and string table
1307 // data across can slow down debug launch times, so we optimize this by
1308 // reading the memory for the __LINKEDIT section from this process.
1309 PlatformSP platform_sp (target.GetPlatform());
1310 if (platform_sp && platform_sp->IsHost())
1311 {
1312 data_was_read = true;
1313 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001314 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001315 if (function_starts_load_command.cmd)
1316 {
1317 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1318 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1319 }
1320 }
1321 }
1322#endif
1323
1324 if (!data_was_read)
1325 {
1326 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1327 if (nlist_data_sp)
1328 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001329 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1330 //if (strtab_data_sp)
1331 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Greg Clayton29021d32012-04-18 05:19:20 +00001332 if (function_starts_load_command.cmd)
1333 {
1334 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1335 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1336 if (func_start_data_sp)
1337 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1338 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001339 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001340 }
1341 }
1342 else
1343 {
1344 nlist_data.SetData (m_data,
1345 symtab_load_command.symoff,
1346 nlist_data_byte_size);
1347 strtab_data.SetData (m_data,
1348 symtab_load_command.stroff,
1349 strtab_data_byte_size);
1350 if (function_starts_load_command.cmd)
1351 {
1352 function_starts_data.SetData (m_data,
1353 function_starts_load_command.dataoff,
1354 function_starts_load_command.datasize);
1355 }
1356 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001357
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001358 if (nlist_data.GetByteSize() == 0)
1359 {
1360 if (log)
1361 module_sp->LogMessage(log.get(), "failed to read nlist data");
1362 return 0;
1363 }
1364
1365
Greg Clayton3a5dc012012-05-25 17:04:00 +00001366 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1367 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001368 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001369 if (process)
1370 {
1371 if (strtab_addr == LLDB_INVALID_ADDRESS)
1372 {
1373 if (log)
1374 module_sp->LogMessage(log.get(), "failed to locate the strtab in memory");
1375 return 0;
1376 }
1377 }
1378 else
Greg Claytondd29b972012-05-18 23:20:01 +00001379 {
1380 if (log)
Greg Clayton3a5dc012012-05-25 17:04:00 +00001381 module_sp->LogMessage(log.get(), "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001382 return 0;
1383 }
1384 }
Greg Claytondd29b972012-05-18 23:20:01 +00001385
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001386 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1387 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1388 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1389 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1390 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1391 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1392 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1393 SectionSP eh_frame_section_sp;
1394 if (text_section_sp.get())
1395 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1396 else
1397 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1398
Greg Claytond2653c22012-03-14 01:53:24 +00001399 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001400 if (text_section_sp && function_starts_data.GetByteSize())
1401 {
1402 FunctionStarts::Entry function_start_entry;
1403 function_start_entry.data = false;
1404 uint32_t function_start_offset = 0;
1405 function_start_entry.addr = text_section_sp->GetFileAddress();
1406 uint64_t delta;
1407 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1408 {
1409 // Now append the current entry
1410 function_start_entry.addr += delta;
1411 function_starts.Append(function_start_entry);
1412 }
1413 }
1414
1415 const uint32_t function_starts_count = function_starts.GetSize();
1416
1417 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
1418
1419 uint32_t nlist_data_offset = 0;
1420
1421 uint32_t N_SO_index = UINT32_MAX;
1422
1423 MachSymtabSectionInfo section_info (section_list);
1424 std::vector<uint32_t> N_FUN_indexes;
1425 std::vector<uint32_t> N_NSYM_indexes;
1426 std::vector<uint32_t> N_INCL_indexes;
1427 std::vector<uint32_t> N_BRAC_indexes;
1428 std::vector<uint32_t> N_COMM_indexes;
1429 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1430 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1431 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1432 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1433 // Any symbols that get merged into another will get an entry
1434 // in this map so we know
1435 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1436 uint32_t nlist_idx = 0;
1437 Symbol *symbol_ptr = NULL;
1438
1439 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001440 Symbol *sym = NULL;
1441 uint32_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001442 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001443 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001444
Jason Molendab62abd52012-06-21 01:51:02 +00001445#if defined (__APPLE__) && defined (__arm__)
1446
1447 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1448 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1449 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1450 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1451 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1452 // nlist parser to ignore all LOCAL symbols.
1453
1454 if (m_header.flags & 0x80000000u)
1455 {
1456 // Before we can start mapping the DSC, we need to make certain the target process is actually
1457 // using the cache we can find.
1458
1459 /*
1460 * TODO (FIXME!)
1461 *
1462 * Consider the case of testing with a separate DSC file.
1463 * If we go through the normal code paths, we will give symbols for the wrong DSC, and
1464 * that is bad. We need to read the target process' all_image_infos struct, and look
1465 * at the values of the processDetachedFromSharedRegion field. If that is set, we should skip
1466 * this code section.
1467 */
1468
1469 // Next we need to determine the correct path for the dyld shared cache.
1470
1471 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1472 char dsc_path[PATH_MAX];
1473
1474 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
1475 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1476 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
1477 header_arch.GetArchitectureName());
1478
1479 FileSpec dsc_filespec(dsc_path, false);
1480
1481 // We need definitions of two structures in the on-disk DSC, copy them here manually
1482struct lldb_copy_dyld_cache_header
1483{
1484 char magic[16];
1485 uint32_t mappingOffset;
1486 uint32_t mappingCount;
1487 uint32_t imagesOffset;
1488 uint32_t imagesCount;
1489 uint64_t dyldBaseAddress;
1490 uint64_t codeSignatureOffset;
1491 uint64_t codeSignatureSize;
1492 uint64_t slideInfoOffset;
1493 uint64_t slideInfoSize;
1494 uint64_t localSymbolsOffset;
1495 uint64_t localSymbolsSize;
1496};
1497
1498struct lldb_copy_dyld_cache_local_symbols_info
1499{
1500 uint32_t nlistOffset;
1501 uint32_t nlistCount;
1502 uint32_t stringsOffset;
1503 uint32_t stringsSize;
1504 uint32_t entriesOffset;
1505 uint32_t entriesCount;
1506};
1507
1508struct lldb_copy_dyld_cache_local_symbols_entry
1509{
1510 uint32_t dylibOffset;
1511 uint32_t nlistStartIndex;
1512 uint32_t nlistCount;
1513};
1514
1515 // Process the dsc header to find the unmapped symbols
1516 //
1517 // Save some VM space, do not map the entire cache in one shot.
1518
1519 if (DataBufferSP dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header)))
1520 {
1521 DataExtractor dsc_header_data(dsc_data_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1522
1523 struct lldb_copy_dyld_cache_header* dsc_header_dummy = NULL;
1524
1525 uint32_t offset = sizeof(dsc_header_dummy->magic);
1526 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1527
1528 // If the mappingOffset points to a location inside the header, we've
1529 // opened an old dyld shared cache, and should not proceed further.
1530 if (mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header))
1531 {
1532
1533 offset = (uint32_t)(uintptr_t)&dsc_header_dummy->localSymbolsOffset;
1534 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1535 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1536
1537 if (localSymbolsOffset && localSymbolsSize)
1538 {
1539 // Map the local symbols
1540 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
1541 {
1542 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1543
1544 offset = 0;
1545
1546 // Read the local_symbols_infos struct in one shot
1547 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1548 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1549
1550 // The local_symbols_infos offsets are offsets into local symbols memory, NOT file offsets!
1551 // We first need to identify the local "entry" that matches the current header.
1552 // The "entry" is stored as a file offset in the dyld_shared_cache, so we need to
1553 // adjust the raw m_header value by slide and 0x30000000.
1554
1555 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1556
1557 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - 0x30000000);
1558
1559 offset = local_symbols_info.entriesOffset;
1560 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1561 {
1562 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1563 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1564 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1565 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1566
1567 if (header_file_offset == local_symbols_entry.dylibOffset)
1568 {
1569 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1570
1571 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1572 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1573 num_syms = symtab->GetNumSymbols();
1574
1575 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1576 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1577
1578 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
1579 {
1580 /////////////////////////////
1581 {
1582 struct nlist_64 nlist;
1583 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1584 break;
1585
1586 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1587 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1588 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1589 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1590 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1591
1592 SymbolType type = eSymbolTypeInvalid;
1593 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1594
1595 if (symbol_name == NULL)
1596 {
1597 // No symbol should be NULL, even the symbols with no
1598 // string values should have an offset zero which points
1599 // to an empty C-string
1600 Host::SystemLog (Host::eSystemLogError,
1601 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1602 entry_index,
1603 nlist.n_strx,
1604 module_sp->GetFileSpec().GetDirectory().GetCString(),
1605 module_sp->GetFileSpec().GetFilename().GetCString());
1606 continue;
1607 }
1608 if (symbol_name[0] == '\0')
1609 symbol_name = NULL;
1610
1611 const char *symbol_name_non_abi_mangled = NULL;
1612
1613 SectionSP symbol_section;
1614 uint32_t symbol_byte_size = 0;
1615 bool add_nlist = true;
1616 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
1617
1618 assert (sym_idx < num_syms);
1619
1620 sym[sym_idx].SetDebug (is_debug);
1621
1622 if (is_debug)
1623 {
1624 switch (nlist.n_type)
1625 {
1626 case StabGlobalSymbol:
1627 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1628 // Sometimes the N_GSYM value contains the address.
1629
1630 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1631 // have the same address, but we want to ensure that we always find only the real symbol,
1632 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1633 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1634 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1635 // same address.
1636
1637 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1638 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1639 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1640 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1641 add_nlist = false;
1642 else
1643 {
1644 sym[sym_idx].SetExternal(true);
1645 if (nlist.n_value != 0)
1646 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1647 type = eSymbolTypeData;
1648 }
1649 break;
1650
1651 case StabFunctionName:
1652 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1653 type = eSymbolTypeCompiler;
1654 break;
1655
1656 case StabFunction:
1657 // N_FUN -- procedure: name,,n_sect,linenumber,address
1658 if (symbol_name)
1659 {
1660 type = eSymbolTypeCode;
1661 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1662
1663 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1664 // We use the current number of symbols in the symbol table in lieu of
1665 // using nlist_idx in case we ever start trimming entries out
1666 N_FUN_indexes.push_back(sym_idx);
1667 }
1668 else
1669 {
1670 type = eSymbolTypeCompiler;
1671
1672 if ( !N_FUN_indexes.empty() )
1673 {
1674 // Copy the size of the function into the original STAB entry so we don't have
1675 // to hunt for it later
1676 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1677 N_FUN_indexes.pop_back();
1678 // We don't really need the end function STAB as it contains the size which
1679 // we already placed with the original symbol, so don't add it if we want a
1680 // minimal symbol table
1681 if (minimize)
1682 add_nlist = false;
1683 }
1684 }
1685 break;
1686
1687 case StabStaticSymbol:
1688 // N_STSYM -- static symbol: name,,n_sect,type,address
1689 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1690 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1691 type = eSymbolTypeData;
1692 break;
1693
1694 case StabLocalCommon:
1695 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1696 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1697 type = eSymbolTypeCommonBlock;
1698 break;
1699
1700 case StabBeginSymbol:
1701 // N_BNSYM
1702 // We use the current number of symbols in the symbol table in lieu of
1703 // using nlist_idx in case we ever start trimming entries out
1704 if (minimize)
1705 {
1706 // Skip these if we want minimal symbol tables
1707 add_nlist = false;
1708 }
1709 else
1710 {
1711 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1712 N_NSYM_indexes.push_back(sym_idx);
1713 type = eSymbolTypeScopeBegin;
1714 }
1715 break;
1716
1717 case StabEndSymbol:
1718 // N_ENSYM
1719 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1720 // so that we can always skip the entire symbol if we need to navigate
1721 // more quickly at the source level when parsing STABS
1722 if (minimize)
1723 {
1724 // Skip these if we want minimal symbol tables
1725 add_nlist = false;
1726 }
1727 else
1728 {
1729 if ( !N_NSYM_indexes.empty() )
1730 {
1731 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1732 symbol_ptr->SetByteSize(sym_idx + 1);
1733 symbol_ptr->SetSizeIsSibling(true);
1734 N_NSYM_indexes.pop_back();
1735 }
1736 type = eSymbolTypeScopeEnd;
1737 }
1738 break;
1739
1740
1741 case StabSourceFileOptions:
1742 // N_OPT - emitted with gcc2_compiled and in gcc source
1743 type = eSymbolTypeCompiler;
1744 break;
1745
1746 case StabRegisterSymbol:
1747 // N_RSYM - register sym: name,,NO_SECT,type,register
1748 type = eSymbolTypeVariable;
1749 break;
1750
1751 case StabSourceLine:
1752 // N_SLINE - src line: 0,,n_sect,linenumber,address
1753 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1754 type = eSymbolTypeLineEntry;
1755 break;
1756
1757 case StabStructureType:
1758 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1759 type = eSymbolTypeVariableType;
1760 break;
1761
1762 case StabSourceFileName:
1763 // N_SO - source file name
1764 type = eSymbolTypeSourceFile;
1765 if (symbol_name == NULL)
1766 {
1767 if (minimize)
1768 add_nlist = false;
1769 if (N_SO_index != UINT32_MAX)
1770 {
1771 // Set the size of the N_SO to the terminating index of this N_SO
1772 // so that we can always skip the entire N_SO if we need to navigate
1773 // more quickly at the source level when parsing STABS
1774 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1775 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1776 symbol_ptr->SetSizeIsSibling(true);
1777 }
1778 N_NSYM_indexes.clear();
1779 N_INCL_indexes.clear();
1780 N_BRAC_indexes.clear();
1781 N_COMM_indexes.clear();
1782 N_FUN_indexes.clear();
1783 N_SO_index = UINT32_MAX;
1784 }
1785 else
1786 {
1787 // We use the current number of symbols in the symbol table in lieu of
1788 // using nlist_idx in case we ever start trimming entries out
1789 const bool N_SO_has_full_path = symbol_name[0] == '/';
1790 if (N_SO_has_full_path)
1791 {
1792 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1793 {
1794 // We have two consecutive N_SO entries where the first contains a directory
1795 // and the second contains a full path.
1796 sym[sym_idx - 1].GetMangled().SetValue(symbol_name, false);
1797 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1798 add_nlist = false;
1799 }
1800 else
1801 {
1802 // This is the first entry in a N_SO that contains a directory or
1803 // a full path to the source file
1804 N_SO_index = sym_idx;
1805 }
1806 }
1807 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1808 {
1809 // This is usually the second N_SO entry that contains just the filename,
1810 // so here we combine it with the first one if we are minimizing the symbol table
1811 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1812 if (so_path && so_path[0])
1813 {
1814 std::string full_so_path (so_path);
1815 if (*full_so_path.rbegin() != '/')
1816 full_so_path += '/';
1817 full_so_path += symbol_name;
1818 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
1819 add_nlist = false;
1820 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1821 }
1822 }
1823 }
1824
1825 break;
1826
1827 case StabObjectFileName:
1828 // N_OSO - object file name: name,,0,0,st_mtime
1829 type = eSymbolTypeObjectFile;
1830 break;
1831
1832 case StabLocalSymbol:
1833 // N_LSYM - local sym: name,,NO_SECT,type,offset
1834 type = eSymbolTypeLocal;
1835 break;
1836
1837 //----------------------------------------------------------------------
1838 // INCL scopes
1839 //----------------------------------------------------------------------
1840 case StabBeginIncludeFileName:
1841 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
1842 // We use the current number of symbols in the symbol table in lieu of
1843 // using nlist_idx in case we ever start trimming entries out
1844 N_INCL_indexes.push_back(sym_idx);
1845 type = eSymbolTypeScopeBegin;
1846 break;
1847
1848 case StabEndIncludeFile:
1849 // N_EINCL - include file end: name,,NO_SECT,0,0
1850 // Set the size of the N_BINCL to the terminating index of this N_EINCL
1851 // so that we can always skip the entire symbol if we need to navigate
1852 // more quickly at the source level when parsing STABS
1853 if ( !N_INCL_indexes.empty() )
1854 {
1855 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1856 symbol_ptr->SetByteSize(sym_idx + 1);
1857 symbol_ptr->SetSizeIsSibling(true);
1858 N_INCL_indexes.pop_back();
1859 }
1860 type = eSymbolTypeScopeEnd;
1861 break;
1862
1863 case StabIncludeFileName:
1864 // N_SOL - #included file name: name,,n_sect,0,address
1865 type = eSymbolTypeHeaderFile;
1866
1867 // We currently don't use the header files on darwin
1868 if (minimize)
1869 add_nlist = false;
1870 break;
1871
1872 case StabCompilerParameters:
1873 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
1874 type = eSymbolTypeCompiler;
1875 break;
1876
1877 case StabCompilerVersion:
1878 // N_VERSION - compiler version: name,,NO_SECT,0,0
1879 type = eSymbolTypeCompiler;
1880 break;
1881
1882 case StabCompilerOptLevel:
1883 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
1884 type = eSymbolTypeCompiler;
1885 break;
1886
1887 case StabParameter:
1888 // N_PSYM - parameter: name,,NO_SECT,type,offset
1889 type = eSymbolTypeVariable;
1890 break;
1891
1892 case StabAlternateEntry:
1893 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
1894 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1895 type = eSymbolTypeLineEntry;
1896 break;
1897
1898 //----------------------------------------------------------------------
1899 // Left and Right Braces
1900 //----------------------------------------------------------------------
1901 case StabLeftBracket:
1902 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
1903 // We use the current number of symbols in the symbol table in lieu of
1904 // using nlist_idx in case we ever start trimming entries out
1905 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1906 N_BRAC_indexes.push_back(sym_idx);
1907 type = eSymbolTypeScopeBegin;
1908 break;
1909
1910 case StabRightBracket:
1911 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
1912 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
1913 // so that we can always skip the entire symbol if we need to navigate
1914 // more quickly at the source level when parsing STABS
1915 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1916 if ( !N_BRAC_indexes.empty() )
1917 {
1918 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1919 symbol_ptr->SetByteSize(sym_idx + 1);
1920 symbol_ptr->SetSizeIsSibling(true);
1921 N_BRAC_indexes.pop_back();
1922 }
1923 type = eSymbolTypeScopeEnd;
1924 break;
1925
1926 case StabDeletedIncludeFile:
1927 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
1928 type = eSymbolTypeHeaderFile;
1929 break;
1930
1931 //----------------------------------------------------------------------
1932 // COMM scopes
1933 //----------------------------------------------------------------------
1934 case StabBeginCommon:
1935 // N_BCOMM - begin common: name,,NO_SECT,0,0
1936 // We use the current number of symbols in the symbol table in lieu of
1937 // using nlist_idx in case we ever start trimming entries out
1938 type = eSymbolTypeScopeBegin;
1939 N_COMM_indexes.push_back(sym_idx);
1940 break;
1941
1942 case StabEndCommonLocal:
1943 // N_ECOML - end common (local name): 0,,n_sect,0,address
1944 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1945 // Fall through
1946
1947 case StabEndCommon:
1948 // N_ECOMM - end common: name,,n_sect,0,0
1949 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1950 // so that we can always skip the entire symbol if we need to navigate
1951 // more quickly at the source level when parsing STABS
1952 if ( !N_COMM_indexes.empty() )
1953 {
1954 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1955 symbol_ptr->SetByteSize(sym_idx + 1);
1956 symbol_ptr->SetSizeIsSibling(true);
1957 N_COMM_indexes.pop_back();
1958 }
1959 type = eSymbolTypeScopeEnd;
1960 break;
1961
1962 case StabLength:
1963 // N_LENG - second stab entry with length information
1964 type = eSymbolTypeAdditional;
1965 break;
1966
1967 default: break;
1968 }
1969 }
1970 else
1971 {
1972 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
1973 uint8_t n_type = NlistMaskType & nlist.n_type;
1974 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
1975
1976 switch (n_type)
1977 {
1978 case NListTypeIndirect: // N_INDR - Fall through
1979 case NListTypePreboundUndefined:// N_PBUD - Fall through
1980 case NListTypeUndefined: // N_UNDF
1981 type = eSymbolTypeUndefined;
1982 break;
1983
1984 case NListTypeAbsolute: // N_ABS
1985 type = eSymbolTypeAbsolute;
1986 break;
1987
1988 case NListTypeSection: // N_SECT
1989 {
1990 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1991
1992 if (symbol_section == NULL)
1993 {
1994 // TODO: warn about this?
1995 add_nlist = false;
1996 break;
1997 }
1998
1999 if (TEXT_eh_frame_sectID == nlist.n_sect)
2000 {
2001 type = eSymbolTypeException;
2002 }
2003 else
2004 {
2005 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2006
2007 switch (section_type)
2008 {
2009 case SectionTypeRegular: break; // regular section
2010 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2011 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2012 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2013 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2014 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2015 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2016 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2017 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2018 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2019 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2020 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2021 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2022 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2023 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2024 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2025 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2026 default: break;
2027 }
2028
2029 if (type == eSymbolTypeInvalid)
2030 {
2031 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2032 if (symbol_section->IsDescendant (text_section_sp.get()))
2033 {
2034 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2035 SectionAttrUserSelfModifyingCode |
2036 SectionAttrSytemSomeInstructions))
2037 type = eSymbolTypeData;
2038 else
2039 type = eSymbolTypeCode;
2040 }
2041 else
2042 if (symbol_section->IsDescendant(data_section_sp.get()))
2043 {
2044 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2045 {
2046 type = eSymbolTypeRuntime;
2047
2048 if (symbol_name &&
2049 symbol_name[0] == '_' &&
2050 symbol_name[1] == 'O' &&
2051 symbol_name[2] == 'B')
2052 {
2053 llvm::StringRef symbol_name_ref(symbol_name);
2054 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2055 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2056 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2057 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2058 {
2059 symbol_name_non_abi_mangled = symbol_name + 1;
2060 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2061 type = eSymbolTypeObjCClass;
2062 }
2063 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2064 {
2065 symbol_name_non_abi_mangled = symbol_name + 1;
2066 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2067 type = eSymbolTypeObjCMetaClass;
2068 }
2069 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2070 {
2071 symbol_name_non_abi_mangled = symbol_name + 1;
2072 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2073 type = eSymbolTypeObjCIVar;
2074 }
2075 }
2076 }
2077 else
2078 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
2079 {
2080 type = eSymbolTypeException;
2081 }
2082 else
2083 {
2084 type = eSymbolTypeData;
2085 }
2086 }
2087 else
2088 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2089 {
2090 type = eSymbolTypeTrampoline;
2091 }
2092 else
2093 if (symbol_section->IsDescendant(objc_section_sp.get()))
2094 {
2095 type = eSymbolTypeRuntime;
2096 if (symbol_name && symbol_name[0] == '.')
2097 {
2098 llvm::StringRef symbol_name_ref(symbol_name);
2099 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2100 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
2101 {
2102 symbol_name_non_abi_mangled = symbol_name;
2103 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2104 type = eSymbolTypeObjCClass;
2105 }
2106 }
2107 }
2108 }
2109 }
2110 }
2111 break;
2112 }
2113 }
2114
2115 if (add_nlist)
2116 {
2117 uint64_t symbol_value = nlist.n_value;
2118 bool symbol_name_is_mangled = false;
2119
2120 if (symbol_name_non_abi_mangled)
2121 {
2122 sym[sym_idx].GetMangled().SetMangledName (symbol_name_non_abi_mangled);
2123 sym[sym_idx].GetMangled().SetDemangledName (symbol_name);
2124 }
2125 else
2126 {
2127 if (symbol_name && symbol_name[0] == '_')
2128 {
2129 symbol_name_is_mangled = symbol_name[1] == '_';
2130 symbol_name++; // Skip the leading underscore
2131 }
2132
2133 if (symbol_name)
2134 {
2135 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
2136 }
2137 }
2138
2139 if (is_debug == false)
2140 {
2141 if (type == eSymbolTypeCode)
2142 {
2143 // See if we can find a N_FUN entry for any code symbols.
2144 // If we do find a match, and the name matches, then we
2145 // can merge the two into just the function symbol to avoid
2146 // duplicate entries in the symbol table
2147 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2148 if (pos != N_FUN_addr_to_sym_idx.end())
2149 {
2150 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2151 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2152 {
2153 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2154 // We just need the flags from the linker symbol, so put these flags
2155 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2156 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2157 sym[sym_idx].Clear();
2158 continue;
2159 }
2160 }
2161 }
2162 else if (type == eSymbolTypeData)
2163 {
2164 // See if we can find a N_STSYM entry for any data symbols.
2165 // If we do find a match, and the name matches, then we
2166 // can merge the two into just the Static symbol to avoid
2167 // duplicate entries in the symbol table
2168 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2169 if (pos != N_STSYM_addr_to_sym_idx.end())
2170 {
2171 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2172 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2173 {
2174 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2175 // We just need the flags from the linker symbol, so put these flags
2176 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2177 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2178 sym[sym_idx].Clear();
2179 continue;
2180 }
2181 }
2182 }
2183 }
2184 if (symbol_section)
2185 {
2186 const addr_t section_file_addr = symbol_section->GetFileAddress();
2187 if (symbol_byte_size == 0 && function_starts_count > 0)
2188 {
2189 addr_t symbol_lookup_file_addr = nlist.n_value;
2190 // Do an exact address match for non-ARM addresses, else get the closest since
2191 // the symbol might be a thumb symbol which has an address with bit zero set
2192 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2193 if (is_arm && func_start_entry)
2194 {
2195 // Verify that the function start address is the symbol address (ARM)
2196 // or the symbol address + 1 (thumb)
2197 if (func_start_entry->addr != symbol_lookup_file_addr &&
2198 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2199 {
2200 // Not the right entry, NULL it out...
2201 func_start_entry = NULL;
2202 }
2203 }
2204 if (func_start_entry)
2205 {
2206 func_start_entry->data = true;
2207
2208 addr_t symbol_file_addr = func_start_entry->addr;
2209 uint32_t symbol_flags = 0;
2210 if (is_arm)
2211 {
2212 if (symbol_file_addr & 1)
2213 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2214 symbol_file_addr &= 0xfffffffffffffffeull;
2215 }
2216
2217 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2218 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2219 if (next_func_start_entry)
2220 {
2221 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2222 // Be sure the clear the Thumb address bit when we calculate the size
2223 // from the current and next address
2224 if (is_arm)
2225 next_symbol_file_addr &= 0xfffffffffffffffeull;
2226 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2227 }
2228 else
2229 {
2230 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2231 }
2232 }
2233 }
2234 symbol_value -= section_file_addr;
2235 }
2236
2237 sym[sym_idx].SetID (nlist_idx);
2238 sym[sym_idx].SetType (type);
2239 sym[sym_idx].GetAddress().SetSection (symbol_section);
2240 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2241 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2242
2243 if (symbol_byte_size > 0)
2244 sym[sym_idx].SetByteSize(symbol_byte_size);
2245
2246 ++sym_idx;
2247 }
2248 else
2249 {
2250 sym[sym_idx].Clear();
2251 }
2252
2253 }
2254 /////////////////////////////
2255 }
2256 break; // No more entries to consider
2257 }
2258 }
2259 }
2260 }
2261 }
2262 }
2263 }
2264
2265 // Must reset this in case it was mutated above!
2266 nlist_data_offset = 0;
2267#endif
2268
2269 // If the sym array was not created while parsing the DSC unmapped
2270 // symbols, create it now.
2271 if (sym == NULL)
2272 {
2273 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2274 num_syms = symtab->GetNumSymbols();
2275 }
2276
2277 if (unmapped_local_symbols_found)
2278 {
2279 assert(m_dysymtab.ilocalsym == 0);
2280 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2281 nlist_idx = m_dysymtab.nlocalsym;
2282 }
2283 else
2284 {
2285 nlist_idx = 0;
2286 }
2287
2288 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002289 {
2290 struct nlist_64 nlist;
2291 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2292 break;
2293
2294 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2295 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2296 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2297 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2298 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2299
2300 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002301 const char *symbol_name = NULL;
2302
Greg Clayton3a5dc012012-05-25 17:04:00 +00002303 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002304 {
2305 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
2306
2307 if (symbol_name == NULL)
2308 {
2309 // No symbol should be NULL, even the symbols with no
2310 // string values should have an offset zero which points
2311 // to an empty C-string
2312 Host::SystemLog (Host::eSystemLogError,
2313 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
2314 nlist_idx,
2315 nlist.n_strx,
2316 module_sp->GetFileSpec().GetDirectory().GetCString(),
2317 module_sp->GetFileSpec().GetFilename().GetCString());
2318 continue;
2319 }
2320 if (symbol_name[0] == '\0')
2321 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002322 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002323 else
2324 {
2325 const addr_t str_addr = strtab_addr + nlist.n_strx;
2326 Error str_error;
2327 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2328 symbol_name = memory_symbol_name.c_str();
2329 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002330 const char *symbol_name_non_abi_mangled = NULL;
2331
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002332 SectionSP symbol_section;
2333 uint32_t symbol_byte_size = 0;
2334 bool add_nlist = true;
2335 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
2336
2337 assert (sym_idx < num_syms);
2338
2339 sym[sym_idx].SetDebug (is_debug);
2340
2341 if (is_debug)
2342 {
2343 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002344 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002345 case StabGlobalSymbol:
2346 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2347 // Sometimes the N_GSYM value contains the address.
2348
2349 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2350 // have the same address, but we want to ensure that we always find only the real symbol,
2351 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2352 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2353 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2354 // same address.
2355
2356 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2357 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2358 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2359 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2360 add_nlist = false;
2361 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002362 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002363 sym[sym_idx].SetExternal(true);
2364 if (nlist.n_value != 0)
2365 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2366 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002367 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002368 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002369
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002370 case StabFunctionName:
2371 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2372 type = eSymbolTypeCompiler;
2373 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002374
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002375 case StabFunction:
2376 // N_FUN -- procedure: name,,n_sect,linenumber,address
2377 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002378 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002379 type = eSymbolTypeCode;
2380 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2381
2382 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2383 // We use the current number of symbols in the symbol table in lieu of
2384 // using nlist_idx in case we ever start trimming entries out
2385 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002386 }
2387 else
2388 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002389 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002390
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002391 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002392 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002393 // Copy the size of the function into the original STAB entry so we don't have
2394 // to hunt for it later
2395 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2396 N_FUN_indexes.pop_back();
2397 // We don't really need the end function STAB as it contains the size which
2398 // we already placed with the original symbol, so don't add it if we want a
2399 // minimal symbol table
2400 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002401 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002402 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002403 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002404 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002405
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002406 case StabStaticSymbol:
2407 // N_STSYM -- static symbol: name,,n_sect,type,address
2408 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2409 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2410 type = eSymbolTypeData;
2411 break;
2412
2413 case StabLocalCommon:
2414 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2415 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2416 type = eSymbolTypeCommonBlock;
2417 break;
2418
2419 case StabBeginSymbol:
2420 // N_BNSYM
2421 // We use the current number of symbols in the symbol table in lieu of
2422 // using nlist_idx in case we ever start trimming entries out
2423 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002424 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002425 // Skip these if we want minimal symbol tables
2426 add_nlist = false;
2427 }
2428 else
2429 {
2430 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2431 N_NSYM_indexes.push_back(sym_idx);
2432 type = eSymbolTypeScopeBegin;
2433 }
2434 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002435
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002436 case StabEndSymbol:
2437 // N_ENSYM
2438 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2439 // so that we can always skip the entire symbol if we need to navigate
2440 // more quickly at the source level when parsing STABS
2441 if (minimize)
2442 {
2443 // Skip these if we want minimal symbol tables
2444 add_nlist = false;
2445 }
2446 else
2447 {
2448 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002449 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002450 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2451 symbol_ptr->SetByteSize(sym_idx + 1);
2452 symbol_ptr->SetSizeIsSibling(true);
2453 N_NSYM_indexes.pop_back();
2454 }
2455 type = eSymbolTypeScopeEnd;
2456 }
2457 break;
2458
2459
2460 case StabSourceFileOptions:
2461 // N_OPT - emitted with gcc2_compiled and in gcc source
2462 type = eSymbolTypeCompiler;
2463 break;
2464
2465 case StabRegisterSymbol:
2466 // N_RSYM - register sym: name,,NO_SECT,type,register
2467 type = eSymbolTypeVariable;
2468 break;
2469
2470 case StabSourceLine:
2471 // N_SLINE - src line: 0,,n_sect,linenumber,address
2472 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2473 type = eSymbolTypeLineEntry;
2474 break;
2475
2476 case StabStructureType:
2477 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2478 type = eSymbolTypeVariableType;
2479 break;
2480
2481 case StabSourceFileName:
2482 // N_SO - source file name
2483 type = eSymbolTypeSourceFile;
2484 if (symbol_name == NULL)
2485 {
2486 if (minimize)
2487 add_nlist = false;
2488 if (N_SO_index != UINT32_MAX)
2489 {
2490 // Set the size of the N_SO to the terminating index of this N_SO
2491 // so that we can always skip the entire N_SO if we need to navigate
2492 // more quickly at the source level when parsing STABS
2493 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2494 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2495 symbol_ptr->SetSizeIsSibling(true);
2496 }
2497 N_NSYM_indexes.clear();
2498 N_INCL_indexes.clear();
2499 N_BRAC_indexes.clear();
2500 N_COMM_indexes.clear();
2501 N_FUN_indexes.clear();
2502 N_SO_index = UINT32_MAX;
2503 }
2504 else
2505 {
2506 // We use the current number of symbols in the symbol table in lieu of
2507 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002508 const bool N_SO_has_full_path = symbol_name[0] == '/';
2509 if (N_SO_has_full_path)
2510 {
2511 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2512 {
2513 // We have two consecutive N_SO entries where the first contains a directory
2514 // and the second contains a full path.
2515 sym[sym_idx - 1].GetMangled().SetValue(symbol_name, false);
2516 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2517 add_nlist = false;
2518 }
2519 else
2520 {
2521 // This is the first entry in a N_SO that contains a directory or
2522 // a full path to the source file
2523 N_SO_index = sym_idx;
2524 }
2525 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002526 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2527 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002528 // This is usually the second N_SO entry that contains just the filename,
2529 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002530 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2531 if (so_path && so_path[0])
2532 {
2533 std::string full_so_path (so_path);
2534 if (*full_so_path.rbegin() != '/')
2535 full_so_path += '/';
2536 full_so_path += symbol_name;
2537 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
2538 add_nlist = false;
2539 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2540 }
2541 }
2542 }
2543
2544 break;
2545
2546 case StabObjectFileName:
2547 // N_OSO - object file name: name,,0,0,st_mtime
2548 type = eSymbolTypeObjectFile;
2549 break;
2550
2551 case StabLocalSymbol:
2552 // N_LSYM - local sym: name,,NO_SECT,type,offset
2553 type = eSymbolTypeLocal;
2554 break;
2555
2556 //----------------------------------------------------------------------
2557 // INCL scopes
2558 //----------------------------------------------------------------------
2559 case StabBeginIncludeFileName:
2560 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2561 // We use the current number of symbols in the symbol table in lieu of
2562 // using nlist_idx in case we ever start trimming entries out
2563 N_INCL_indexes.push_back(sym_idx);
2564 type = eSymbolTypeScopeBegin;
2565 break;
2566
2567 case StabEndIncludeFile:
2568 // N_EINCL - include file end: name,,NO_SECT,0,0
2569 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2570 // so that we can always skip the entire symbol if we need to navigate
2571 // more quickly at the source level when parsing STABS
2572 if ( !N_INCL_indexes.empty() )
2573 {
2574 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2575 symbol_ptr->SetByteSize(sym_idx + 1);
2576 symbol_ptr->SetSizeIsSibling(true);
2577 N_INCL_indexes.pop_back();
2578 }
2579 type = eSymbolTypeScopeEnd;
2580 break;
2581
2582 case StabIncludeFileName:
2583 // N_SOL - #included file name: name,,n_sect,0,address
2584 type = eSymbolTypeHeaderFile;
2585
2586 // We currently don't use the header files on darwin
2587 if (minimize)
2588 add_nlist = false;
2589 break;
2590
2591 case StabCompilerParameters:
2592 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2593 type = eSymbolTypeCompiler;
2594 break;
2595
2596 case StabCompilerVersion:
2597 // N_VERSION - compiler version: name,,NO_SECT,0,0
2598 type = eSymbolTypeCompiler;
2599 break;
2600
2601 case StabCompilerOptLevel:
2602 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2603 type = eSymbolTypeCompiler;
2604 break;
2605
2606 case StabParameter:
2607 // N_PSYM - parameter: name,,NO_SECT,type,offset
2608 type = eSymbolTypeVariable;
2609 break;
2610
2611 case StabAlternateEntry:
2612 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2613 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2614 type = eSymbolTypeLineEntry;
2615 break;
2616
2617 //----------------------------------------------------------------------
2618 // Left and Right Braces
2619 //----------------------------------------------------------------------
2620 case StabLeftBracket:
2621 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2622 // We use the current number of symbols in the symbol table in lieu of
2623 // using nlist_idx in case we ever start trimming entries out
2624 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2625 N_BRAC_indexes.push_back(sym_idx);
2626 type = eSymbolTypeScopeBegin;
2627 break;
2628
2629 case StabRightBracket:
2630 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2631 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2632 // so that we can always skip the entire symbol if we need to navigate
2633 // more quickly at the source level when parsing STABS
2634 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2635 if ( !N_BRAC_indexes.empty() )
2636 {
2637 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2638 symbol_ptr->SetByteSize(sym_idx + 1);
2639 symbol_ptr->SetSizeIsSibling(true);
2640 N_BRAC_indexes.pop_back();
2641 }
2642 type = eSymbolTypeScopeEnd;
2643 break;
2644
2645 case StabDeletedIncludeFile:
2646 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2647 type = eSymbolTypeHeaderFile;
2648 break;
2649
2650 //----------------------------------------------------------------------
2651 // COMM scopes
2652 //----------------------------------------------------------------------
2653 case StabBeginCommon:
2654 // N_BCOMM - begin common: name,,NO_SECT,0,0
2655 // We use the current number of symbols in the symbol table in lieu of
2656 // using nlist_idx in case we ever start trimming entries out
2657 type = eSymbolTypeScopeBegin;
2658 N_COMM_indexes.push_back(sym_idx);
2659 break;
2660
2661 case StabEndCommonLocal:
2662 // N_ECOML - end common (local name): 0,,n_sect,0,address
2663 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2664 // Fall through
2665
2666 case StabEndCommon:
2667 // N_ECOMM - end common: name,,n_sect,0,0
2668 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2669 // so that we can always skip the entire symbol if we need to navigate
2670 // more quickly at the source level when parsing STABS
2671 if ( !N_COMM_indexes.empty() )
2672 {
2673 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2674 symbol_ptr->SetByteSize(sym_idx + 1);
2675 symbol_ptr->SetSizeIsSibling(true);
2676 N_COMM_indexes.pop_back();
2677 }
2678 type = eSymbolTypeScopeEnd;
2679 break;
2680
2681 case StabLength:
2682 // N_LENG - second stab entry with length information
2683 type = eSymbolTypeAdditional;
2684 break;
2685
2686 default: break;
2687 }
2688 }
2689 else
2690 {
2691 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2692 uint8_t n_type = NlistMaskType & nlist.n_type;
2693 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2694
2695 switch (n_type)
2696 {
2697 case NListTypeIndirect: // N_INDR - Fall through
2698 case NListTypePreboundUndefined:// N_PBUD - Fall through
2699 case NListTypeUndefined: // N_UNDF
2700 type = eSymbolTypeUndefined;
2701 break;
2702
2703 case NListTypeAbsolute: // N_ABS
2704 type = eSymbolTypeAbsolute;
2705 break;
2706
2707 case NListTypeSection: // N_SECT
2708 {
2709 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2710
2711 if (symbol_section == NULL)
2712 {
2713 // TODO: warn about this?
2714 add_nlist = false;
2715 break;
2716 }
2717
2718 if (TEXT_eh_frame_sectID == nlist.n_sect)
2719 {
2720 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00002721 }
2722 else
2723 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002724 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2725
2726 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00002727 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002728 case SectionTypeRegular: break; // regular section
2729 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2730 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2731 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2732 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2733 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2734 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2735 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2736 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2737 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2738 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2739 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2740 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2741 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2742 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2743 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2744 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2745 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002746 }
Chris Lattner24943d22010-06-08 16:52:24 +00002747
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002748 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002749 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002750 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2751 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002752 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002753 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2754 SectionAttrUserSelfModifyingCode |
2755 SectionAttrSytemSomeInstructions))
2756 type = eSymbolTypeData;
2757 else
2758 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00002759 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002760 else
2761 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002762 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002763 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00002764 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002765 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00002766
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002767 if (symbol_name &&
2768 symbol_name[0] == '_' &&
2769 symbol_name[1] == 'O' &&
2770 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00002771 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002772 llvm::StringRef symbol_name_ref(symbol_name);
2773 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2774 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2775 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2776 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00002777 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002778 symbol_name_non_abi_mangled = symbol_name + 1;
2779 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2780 type = eSymbolTypeObjCClass;
Chris Lattner24943d22010-06-08 16:52:24 +00002781 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002782 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00002783 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002784 symbol_name_non_abi_mangled = symbol_name + 1;
2785 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2786 type = eSymbolTypeObjCMetaClass;
2787 }
2788 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2789 {
2790 symbol_name_non_abi_mangled = symbol_name + 1;
2791 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2792 type = eSymbolTypeObjCIVar;
Chris Lattner24943d22010-06-08 16:52:24 +00002793 }
2794 }
2795 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002796 else
2797 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
2798 {
2799 type = eSymbolTypeException;
2800 }
2801 else
2802 {
2803 type = eSymbolTypeData;
2804 }
2805 }
2806 else
2807 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2808 {
2809 type = eSymbolTypeTrampoline;
2810 }
2811 else
2812 if (symbol_section->IsDescendant(objc_section_sp.get()))
2813 {
2814 type = eSymbolTypeRuntime;
2815 if (symbol_name && symbol_name[0] == '.')
2816 {
2817 llvm::StringRef symbol_name_ref(symbol_name);
2818 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2819 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
2820 {
2821 symbol_name_non_abi_mangled = symbol_name;
2822 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2823 type = eSymbolTypeObjCClass;
2824 }
2825 }
Chris Lattner24943d22010-06-08 16:52:24 +00002826 }
2827 }
2828 }
2829 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002830 break;
2831 }
2832 }
2833
2834 if (add_nlist)
2835 {
2836 uint64_t symbol_value = nlist.n_value;
2837 bool symbol_name_is_mangled = false;
2838
2839 if (symbol_name_non_abi_mangled)
2840 {
2841 sym[sym_idx].GetMangled().SetMangledName (symbol_name_non_abi_mangled);
2842 sym[sym_idx].GetMangled().SetDemangledName (symbol_name);
Chris Lattner24943d22010-06-08 16:52:24 +00002843 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002844 else
2845 {
2846 if (symbol_name && symbol_name[0] == '_')
2847 {
2848 symbol_name_is_mangled = symbol_name[1] == '_';
2849 symbol_name++; // Skip the leading underscore
2850 }
2851
2852 if (symbol_name)
2853 {
2854 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
2855 }
2856 }
2857
2858 if (is_debug == false)
2859 {
2860 if (type == eSymbolTypeCode)
2861 {
2862 // See if we can find a N_FUN entry for any code symbols.
2863 // If we do find a match, and the name matches, then we
2864 // can merge the two into just the function symbol to avoid
2865 // duplicate entries in the symbol table
2866 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2867 if (pos != N_FUN_addr_to_sym_idx.end())
2868 {
2869 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2870 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2871 {
2872 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2873 // We just need the flags from the linker symbol, so put these flags
2874 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2875 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2876 sym[sym_idx].Clear();
2877 continue;
2878 }
2879 }
2880 }
2881 else if (type == eSymbolTypeData)
2882 {
2883 // See if we can find a N_STSYM entry for any data symbols.
2884 // If we do find a match, and the name matches, then we
2885 // can merge the two into just the Static symbol to avoid
2886 // duplicate entries in the symbol table
2887 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2888 if (pos != N_STSYM_addr_to_sym_idx.end())
2889 {
2890 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2891 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2892 {
2893 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2894 // We just need the flags from the linker symbol, so put these flags
2895 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2896 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2897 sym[sym_idx].Clear();
2898 continue;
2899 }
2900 }
2901 }
2902 }
2903 if (symbol_section)
2904 {
2905 const addr_t section_file_addr = symbol_section->GetFileAddress();
2906 if (symbol_byte_size == 0 && function_starts_count > 0)
2907 {
Greg Claytond2653c22012-03-14 01:53:24 +00002908 addr_t symbol_lookup_file_addr = nlist.n_value;
2909 // Do an exact address match for non-ARM addresses, else get the closest since
2910 // the symbol might be a thumb symbol which has an address with bit zero set
2911 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2912 if (is_arm && func_start_entry)
2913 {
2914 // Verify that the function start address is the symbol address (ARM)
2915 // or the symbol address + 1 (thumb)
2916 if (func_start_entry->addr != symbol_lookup_file_addr &&
2917 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2918 {
2919 // Not the right entry, NULL it out...
2920 func_start_entry = NULL;
2921 }
2922 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002923 if (func_start_entry)
2924 {
2925 func_start_entry->data = true;
Greg Claytond2653c22012-03-14 01:53:24 +00002926
2927 addr_t symbol_file_addr = func_start_entry->addr;
2928 uint32_t symbol_flags = 0;
2929 if (is_arm)
2930 {
2931 if (symbol_file_addr & 1)
2932 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2933 symbol_file_addr &= 0xfffffffffffffffeull;
2934 }
2935
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002936 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2937 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2938 if (next_func_start_entry)
2939 {
Greg Claytond2653c22012-03-14 01:53:24 +00002940 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2941 // Be sure the clear the Thumb address bit when we calculate the size
2942 // from the current and next address
2943 if (is_arm)
2944 next_symbol_file_addr &= 0xfffffffffffffffeull;
2945 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 +00002946 }
2947 else
2948 {
Greg Claytond2653c22012-03-14 01:53:24 +00002949 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002950 }
2951 }
2952 }
2953 symbol_value -= section_file_addr;
2954 }
2955
2956 sym[sym_idx].SetID (nlist_idx);
2957 sym[sym_idx].SetType (type);
2958 sym[sym_idx].GetAddress().SetSection (symbol_section);
2959 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2960 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2961
2962 if (symbol_byte_size > 0)
2963 sym[sym_idx].SetByteSize(symbol_byte_size);
2964
2965 ++sym_idx;
2966 }
2967 else
2968 {
2969 sym[sym_idx].Clear();
2970 }
2971
2972 }
2973
2974 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
2975 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
2976 // such entries by figuring out what the address for the global is by looking up this non-STAB
2977 // entry and copying the value into the debug symbol's value to save us the hassle in the
2978 // debug symbol parser.
2979
2980 Symbol *global_symbol = NULL;
2981 for (nlist_idx = 0;
2982 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
2983 nlist_idx++)
2984 {
2985 if (global_symbol->GetAddress().GetFileAddress() == 0)
2986 {
2987 std::vector<uint32_t> indexes;
2988 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
2989 {
2990 std::vector<uint32_t>::const_iterator pos;
2991 std::vector<uint32_t>::const_iterator end = indexes.end();
2992 for (pos = indexes.begin(); pos != end; ++pos)
2993 {
2994 symbol_ptr = symtab->SymbolAtIndex(*pos);
2995 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
2996 {
2997 global_symbol->GetAddress() = symbol_ptr->GetAddress();
2998 break;
2999 }
3000 }
3001 }
Chris Lattner24943d22010-06-08 16:52:24 +00003002 }
3003 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003004
3005 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3006
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003007 if (function_starts_count > 0)
3008 {
3009 char synthetic_function_symbol[PATH_MAX];
3010 uint32_t num_synthetic_function_symbols = 0;
3011 for (i=0; i<function_starts_count; ++i)
3012 {
3013 if (function_starts.GetEntryRef (i).data == false)
3014 ++num_synthetic_function_symbols;
3015 }
3016
3017 if (num_synthetic_function_symbols > 0)
3018 {
3019 if (num_syms < sym_idx + num_synthetic_function_symbols)
3020 {
3021 num_syms = sym_idx + num_synthetic_function_symbols;
3022 sym = symtab->Resize (num_syms);
3023 }
3024 uint32_t synthetic_function_symbol_idx = 0;
3025 for (i=0; i<function_starts_count; ++i)
3026 {
3027 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3028 if (func_start_entry->data == false)
3029 {
Greg Claytond2653c22012-03-14 01:53:24 +00003030 addr_t symbol_file_addr = func_start_entry->addr;
3031 uint32_t symbol_flags = 0;
3032 if (is_arm)
3033 {
3034 if (symbol_file_addr & 1)
3035 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3036 symbol_file_addr &= 0xfffffffffffffffeull;
3037 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003038 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003039 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003040 {
3041 SectionSP symbol_section (symbol_addr.GetSection());
3042 uint32_t symbol_byte_size = 0;
3043 if (symbol_section)
3044 {
3045 const addr_t section_file_addr = symbol_section->GetFileAddress();
3046 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3047 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3048 if (next_func_start_entry)
3049 {
Greg Claytond2653c22012-03-14 01:53:24 +00003050 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3051 if (is_arm)
3052 next_symbol_file_addr &= 0xfffffffffffffffeull;
3053 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 +00003054 }
3055 else
3056 {
Greg Claytond2653c22012-03-14 01:53:24 +00003057 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003058 }
3059 snprintf (synthetic_function_symbol,
3060 sizeof(synthetic_function_symbol),
3061 "___lldb_unnamed_function%u$$%s",
3062 ++synthetic_function_symbol_idx,
3063 module_sp->GetFileSpec().GetFilename().GetCString());
3064 sym[sym_idx].SetID (synthetic_sym_id++);
3065 sym[sym_idx].GetMangled().SetDemangledName(synthetic_function_symbol);
3066 sym[sym_idx].SetType (eSymbolTypeCode);
3067 sym[sym_idx].SetIsSynthetic (true);
3068 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003069 if (symbol_flags)
3070 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003071 if (symbol_byte_size)
3072 sym[sym_idx].SetByteSize (symbol_byte_size);
3073 ++sym_idx;
3074 }
3075 }
3076 }
3077 }
3078 }
3079 }
3080
3081 // Trim our symbols down to just what we ended up with after
3082 // removing any symbols.
3083 if (sym_idx < num_syms)
3084 {
3085 num_syms = sym_idx;
3086 sym = symtab->Resize (num_syms);
3087 }
3088
3089 // Now synthesize indirect symbols
3090 if (m_dysymtab.nindirectsyms != 0)
3091 {
3092 DataExtractor indirect_symbol_index_data (m_data, m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4);
3093
3094 if (indirect_symbol_index_data.GetByteSize())
3095 {
3096 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3097
3098 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3099 {
3100 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3101 {
3102 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3103 if (symbol_stub_byte_size == 0)
3104 continue;
3105
3106 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3107
3108 if (num_symbol_stubs == 0)
3109 continue;
3110
3111 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3112 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3113 {
3114 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3115 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
3116 uint32_t symbol_stub_offset = symbol_stub_index * 4;
3117 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3118 {
3119 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3120 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3121 continue;
3122
3123 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3124 Symbol *stub_symbol = NULL;
3125 if (index_pos != end_index_pos)
3126 {
3127 // We have a remapping from the original nlist index to
3128 // a current symbol index, so just look this up by index
3129 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3130 }
3131 else
3132 {
3133 // We need to lookup a symbol using the original nlist
3134 // symbol index since this index is coming from the
3135 // S_SYMBOL_STUBS
3136 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3137 }
3138
3139 assert (stub_symbol);
3140 if (stub_symbol)
3141 {
3142 Address so_addr(symbol_stub_addr, section_list);
3143
3144 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3145 {
3146 // Change the external symbol into a trampoline that makes sense
3147 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3148 // can re-use them so we don't have to make up a synthetic symbol
3149 // for no good reason.
3150 stub_symbol->SetType (eSymbolTypeTrampoline);
3151 stub_symbol->SetExternal (false);
3152 stub_symbol->GetAddress() = so_addr;
3153 stub_symbol->SetByteSize (symbol_stub_byte_size);
3154 }
3155 else
3156 {
3157 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003158 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003159 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003160 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003161 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003162 stub_symbol = NULL; // this pointer no longer valid
3163 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003164 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003165 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003166 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3167 sym[sym_idx].SetIsSynthetic (true);
3168 sym[sym_idx].GetAddress() = so_addr;
3169 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3170 ++sym_idx;
3171 }
3172 }
3173 }
3174 }
3175 }
3176 }
3177 }
3178 }
3179 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003180 }
3181 return 0;
3182}
3183
3184
3185void
3186ObjectFileMachO::Dump (Stream *s)
3187{
Greg Clayton9482f052012-03-13 23:14:29 +00003188 ModuleSP module_sp(GetModule());
3189 if (module_sp)
3190 {
3191 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3192 s->Printf("%p: ", this);
3193 s->Indent();
3194 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3195 s->PutCString("ObjectFileMachO64");
3196 else
3197 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003198
Greg Clayton9482f052012-03-13 23:14:29 +00003199 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003200
Greg Clayton9482f052012-03-13 23:14:29 +00003201 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003202
Greg Clayton9482f052012-03-13 23:14:29 +00003203 if (m_sections_ap.get())
3204 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003205
Greg Clayton9482f052012-03-13 23:14:29 +00003206 if (m_symtab_ap.get())
3207 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3208 }
Chris Lattner24943d22010-06-08 16:52:24 +00003209}
3210
3211
3212bool
Greg Clayton0467c782011-02-04 18:53:10 +00003213ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003214{
Greg Clayton9482f052012-03-13 23:14:29 +00003215 ModuleSP module_sp(GetModule());
3216 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003217 {
Greg Clayton9482f052012-03-13 23:14:29 +00003218 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3219 struct uuid_command load_cmd;
3220 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3221 uint32_t i;
3222 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003223 {
Greg Clayton9482f052012-03-13 23:14:29 +00003224 const uint32_t cmd_offset = offset;
3225 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3226 break;
3227
3228 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00003229 {
Greg Clayton9482f052012-03-13 23:14:29 +00003230 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
3231 if (uuid_bytes)
3232 {
3233 uuid->SetBytes (uuid_bytes);
3234 return true;
3235 }
3236 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003237 }
Greg Clayton9482f052012-03-13 23:14:29 +00003238 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003239 }
Chris Lattner24943d22010-06-08 16:52:24 +00003240 }
3241 return false;
3242}
3243
3244
3245uint32_t
3246ObjectFileMachO::GetDependentModules (FileSpecList& files)
3247{
Chris Lattner24943d22010-06-08 16:52:24 +00003248 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003249 ModuleSP module_sp(GetModule());
3250 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003251 {
Greg Clayton9482f052012-03-13 23:14:29 +00003252 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3253 struct load_command load_cmd;
3254 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3255 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3256 uint32_t i;
3257 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003258 {
Greg Clayton9482f052012-03-13 23:14:29 +00003259 const uint32_t cmd_offset = offset;
3260 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3261 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003262
Greg Clayton9482f052012-03-13 23:14:29 +00003263 switch (load_cmd.cmd)
3264 {
3265 case LoadCommandDylibLoad:
3266 case LoadCommandDylibLoadWeak:
3267 case LoadCommandDylibReexport:
3268 case LoadCommandDynamicLinkerLoad:
3269 case LoadCommandFixedVMShlibLoad:
3270 case LoadCommandDylibLoadUpward:
3271 {
3272 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3273 const char *path = m_data.PeekCStr(name_offset);
3274 // Skip any path that starts with '@' since these are usually:
3275 // @executable_path/.../file
3276 // @rpath/.../file
3277 if (path && path[0] != '@')
3278 {
3279 FileSpec file_spec(path, resolve_path);
3280 if (files.AppendIfUnique(file_spec))
3281 count++;
3282 }
3283 }
3284 break;
3285
3286 default:
3287 break;
3288 }
3289 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003290 }
Chris Lattner24943d22010-06-08 16:52:24 +00003291 }
3292 return count;
3293}
3294
Jim Ingham28775942011-03-07 23:44:08 +00003295lldb_private::Address
3296ObjectFileMachO::GetEntryPointAddress ()
3297{
3298 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3299 // is initialized to an invalid address, so we can just return that.
3300 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
3301
3302 if (!IsExecutable() || m_entry_point_address.IsValid())
3303 return m_entry_point_address;
3304
3305 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
3306 // /usr/include/mach-o.h, but it is basically:
3307 //
3308 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3309 // uint32_t count - this is the count of longs in the thread state data
3310 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3311 // <repeat this trio>
3312 //
3313 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3314 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3315 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3316 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3317 //
3318 // For now we hard-code the offsets and flavors we need:
3319 //
3320 //
3321
Greg Clayton9482f052012-03-13 23:14:29 +00003322 ModuleSP module_sp(GetModule());
3323 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003324 {
Greg Clayton9482f052012-03-13 23:14:29 +00003325 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3326 struct load_command load_cmd;
3327 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3328 uint32_t i;
3329 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3330 bool done = false;
3331
3332 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003333 {
Greg Clayton9482f052012-03-13 23:14:29 +00003334 const uint32_t cmd_offset = offset;
3335 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3336 break;
3337
3338 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003339 {
Greg Clayton9482f052012-03-13 23:14:29 +00003340 case LoadCommandUnixThread:
3341 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003342 {
Greg Clayton9482f052012-03-13 23:14:29 +00003343 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003344 {
Greg Clayton9482f052012-03-13 23:14:29 +00003345 uint32_t flavor = m_data.GetU32(&offset);
3346 uint32_t count = m_data.GetU32(&offset);
3347 if (count == 0)
3348 {
3349 // We've gotten off somehow, log and exit;
3350 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003351 }
Greg Clayton9482f052012-03-13 23:14:29 +00003352
3353 switch (m_header.cputype)
3354 {
3355 case llvm::MachO::CPUTypeARM:
3356 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3357 {
3358 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3359 start_address = m_data.GetU32(&offset);
3360 done = true;
3361 }
Jim Ingham28775942011-03-07 23:44:08 +00003362 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003363 case llvm::MachO::CPUTypeI386:
3364 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3365 {
3366 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3367 start_address = m_data.GetU32(&offset);
3368 done = true;
3369 }
3370 break;
3371 case llvm::MachO::CPUTypeX86_64:
3372 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3373 {
3374 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3375 start_address = m_data.GetU64(&offset);
3376 done = true;
3377 }
3378 break;
3379 default:
3380 return m_entry_point_address;
3381 }
3382 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3383 if (done)
3384 break;
3385 offset += count * 4;
3386 }
Jim Ingham28775942011-03-07 23:44:08 +00003387 }
Greg Clayton9482f052012-03-13 23:14:29 +00003388 break;
3389 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003390 {
Greg Clayton9482f052012-03-13 23:14:29 +00003391 ConstString text_segment_name ("__TEXT");
3392 uint64_t entryoffset = m_data.GetU64(&offset);
3393 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3394 if (text_segment_sp)
3395 {
3396 done = true;
3397 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3398 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003399 }
Greg Clayton9482f052012-03-13 23:14:29 +00003400
3401 default:
3402 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003403 }
Greg Clayton9482f052012-03-13 23:14:29 +00003404 if (done)
3405 break;
Jim Ingham28775942011-03-07 23:44:08 +00003406
Greg Clayton9482f052012-03-13 23:14:29 +00003407 // Go to the next load command:
3408 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003409 }
Jim Ingham28775942011-03-07 23:44:08 +00003410
Greg Clayton9482f052012-03-13 23:14:29 +00003411 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003412 {
Greg Clayton9482f052012-03-13 23:14:29 +00003413 // We got the start address from the load commands, so now resolve that address in the sections
3414 // of this ObjectFile:
3415 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003416 {
Greg Clayton9482f052012-03-13 23:14:29 +00003417 m_entry_point_address.Clear();
3418 }
3419 }
3420 else
3421 {
3422 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3423 // "start" symbol in the main executable.
3424
3425 ModuleSP module_sp (GetModule());
3426
3427 if (module_sp)
3428 {
3429 SymbolContextList contexts;
3430 SymbolContext context;
3431 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3432 {
3433 if (contexts.GetContextAtIndex(0, context))
3434 m_entry_point_address = context.symbol->GetAddress();
3435 }
Greg Clayton3508c382012-02-24 01:59:29 +00003436 }
3437 }
Jim Ingham28775942011-03-07 23:44:08 +00003438 }
3439
3440 return m_entry_point_address;
3441
3442}
3443
Greg Claytonb5a8f142012-02-05 02:38:54 +00003444lldb_private::Address
3445ObjectFileMachO::GetHeaderAddress ()
3446{
3447 lldb_private::Address header_addr;
3448 SectionList *section_list = GetSectionList();
3449 if (section_list)
3450 {
3451 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3452 if (text_segment_sp)
3453 {
Greg Clayton3508c382012-02-24 01:59:29 +00003454 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003455 header_addr.SetOffset (0);
3456 }
3457 }
3458 return header_addr;
3459}
3460
Greg Clayton46c9a352012-02-09 06:16:32 +00003461uint32_t
3462ObjectFileMachO::GetNumThreadContexts ()
3463{
Greg Clayton9482f052012-03-13 23:14:29 +00003464 ModuleSP module_sp(GetModule());
3465 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003466 {
Greg Clayton9482f052012-03-13 23:14:29 +00003467 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3468 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003469 {
Greg Clayton9482f052012-03-13 23:14:29 +00003470 m_thread_context_offsets_valid = true;
3471 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3472 FileRangeArray::Entry file_range;
3473 thread_command thread_cmd;
3474 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003475 {
Greg Clayton9482f052012-03-13 23:14:29 +00003476 const uint32_t cmd_offset = offset;
3477 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3478 break;
3479
3480 if (thread_cmd.cmd == LoadCommandThread)
3481 {
3482 file_range.SetRangeBase (offset);
3483 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3484 m_thread_context_offsets.Append (file_range);
3485 }
3486 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003487 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003488 }
3489 }
3490 return m_thread_context_offsets.GetSize();
3491}
3492
3493lldb::RegisterContextSP
3494ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3495{
Greg Clayton46c9a352012-02-09 06:16:32 +00003496 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003497
Greg Clayton9482f052012-03-13 23:14:29 +00003498 ModuleSP module_sp(GetModule());
3499 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003500 {
Greg Clayton9482f052012-03-13 23:14:29 +00003501 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3502 if (!m_thread_context_offsets_valid)
3503 GetNumThreadContexts ();
3504
3505 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
3506
3507 DataExtractor data (m_data,
3508 thread_context_file_range->GetRangeBase(),
3509 thread_context_file_range->GetByteSize());
3510
3511 switch (m_header.cputype)
3512 {
3513 case llvm::MachO::CPUTypeARM:
3514 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3515 break;
3516
3517 case llvm::MachO::CPUTypeI386:
3518 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3519 break;
3520
3521 case llvm::MachO::CPUTypeX86_64:
3522 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3523 break;
3524 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003525 }
3526 return reg_ctx_sp;
3527}
3528
Greg Claytonb5a8f142012-02-05 02:38:54 +00003529
Greg Claytonca319972011-07-09 00:41:34 +00003530ObjectFile::Type
3531ObjectFileMachO::CalculateType()
3532{
3533 switch (m_header.filetype)
3534 {
3535 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3536 if (GetAddressByteSize () == 4)
3537 {
3538 // 32 bit kexts are just object files, but they do have a valid
3539 // UUID load command.
3540 UUID uuid;
3541 if (GetUUID(&uuid))
3542 {
3543 // this checking for the UUID load command is not enough
3544 // we could eventually look for the symbol named
3545 // "OSKextGetCurrentIdentifier" as this is required of kexts
3546 if (m_strata == eStrataInvalid)
3547 m_strata = eStrataKernel;
3548 return eTypeSharedLibrary;
3549 }
3550 }
3551 return eTypeObjectFile;
3552
3553 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3554 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3555 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3556 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3557 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3558 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3559 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3560 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3561 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3562 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3563 default:
3564 break;
3565 }
3566 return eTypeUnknown;
3567}
3568
3569ObjectFile::Strata
3570ObjectFileMachO::CalculateStrata()
3571{
3572 switch (m_header.filetype)
3573 {
3574 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3575 {
3576 // 32 bit kexts are just object files, but they do have a valid
3577 // UUID load command.
3578 UUID uuid;
3579 if (GetUUID(&uuid))
3580 {
3581 // this checking for the UUID load command is not enough
3582 // we could eventually look for the symbol named
3583 // "OSKextGetCurrentIdentifier" as this is required of kexts
3584 if (m_type == eTypeInvalid)
3585 m_type = eTypeSharedLibrary;
3586
3587 return eStrataKernel;
3588 }
3589 }
3590 return eStrataUnknown;
3591
3592 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3593 // Check for the MH_DYLDLINK bit in the flags
3594 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003595 {
Greg Claytonca319972011-07-09 00:41:34 +00003596 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003597 }
3598 else
3599 {
3600 SectionList *section_list = GetSectionList();
3601 if (section_list)
3602 {
3603 static ConstString g_kld_section_name ("__KLD");
3604 if (section_list->FindSectionByName(g_kld_section_name))
3605 return eStrataKernel;
3606 }
3607 }
3608 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00003609
3610 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
3611 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00003612 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00003613 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
3614 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
3615 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
3616 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
3617 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
3618 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
3619 default:
3620 break;
3621 }
3622 return eStrataUnknown;
3623}
3624
3625
Greg Clayton49f4bf22012-02-22 19:41:02 +00003626uint32_t
3627ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
3628{
Greg Clayton9482f052012-03-13 23:14:29 +00003629 ModuleSP module_sp(GetModule());
3630 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003631 {
Greg Clayton9482f052012-03-13 23:14:29 +00003632 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3633 struct dylib_command load_cmd;
3634 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3635 uint32_t version_cmd = 0;
3636 uint64_t version = 0;
3637 uint32_t i;
3638 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003639 {
Greg Clayton9482f052012-03-13 23:14:29 +00003640 const uint32_t cmd_offset = offset;
3641 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3642 break;
3643
3644 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003645 {
Greg Clayton9482f052012-03-13 23:14:29 +00003646 if (version_cmd == 0)
3647 {
3648 version_cmd = load_cmd.cmd;
3649 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
3650 break;
3651 version = load_cmd.dylib.current_version;
3652 }
3653 break; // Break for now unless there is another more complete version
3654 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00003655 }
Greg Clayton9482f052012-03-13 23:14:29 +00003656 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003657 }
Greg Clayton9482f052012-03-13 23:14:29 +00003658
3659 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003660 {
Greg Clayton9482f052012-03-13 23:14:29 +00003661 if (versions != NULL && num_versions > 0)
3662 {
3663 if (num_versions > 0)
3664 versions[0] = (version & 0xFFFF0000ull) >> 16;
3665 if (num_versions > 1)
3666 versions[1] = (version & 0x0000FF00ull) >> 8;
3667 if (num_versions > 2)
3668 versions[2] = (version & 0x000000FFull);
3669 // Fill in an remaining version numbers with invalid values
3670 for (i=3; i<num_versions; ++i)
3671 versions[i] = UINT32_MAX;
3672 }
3673 // The LC_ID_DYLIB load command has a version with 3 version numbers
3674 // in it, so always return 3
3675 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003676 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00003677 }
3678 return false;
3679}
3680
Chris Lattner24943d22010-06-08 16:52:24 +00003681bool
Greg Clayton395fc332011-02-15 21:59:32 +00003682ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00003683{
Greg Clayton9482f052012-03-13 23:14:29 +00003684 ModuleSP module_sp(GetModule());
3685 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003686 {
Greg Clayton9482f052012-03-13 23:14:29 +00003687 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3688 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
3689
3690 // Files with type MH_PRELOAD are currently used in cases where the image
3691 // debugs at the addresses in the file itself. Below we set the OS to
3692 // unknown to make sure we use the DynamicLoaderStatic()...
3693 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
3694 {
3695 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
3696 }
3697 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003698 }
Greg Clayton9482f052012-03-13 23:14:29 +00003699 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003700}
3701
3702
3703//------------------------------------------------------------------
3704// PluginInterface protocol
3705//------------------------------------------------------------------
3706const char *
3707ObjectFileMachO::GetPluginName()
3708{
3709 return "ObjectFileMachO";
3710}
3711
3712const char *
3713ObjectFileMachO::GetShortPluginName()
3714{
3715 return GetPluginNameStatic();
3716}
3717
3718uint32_t
3719ObjectFileMachO::GetPluginVersion()
3720{
3721 return 1;
3722}
3723