blob: 523acd8da73afad966a24becd665ff8bdfd87c68 [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"
20#include "lldb/Core/Section.h"
21#include "lldb/Core/StreamFile.h"
22#include "lldb/Core/StreamString.h"
23#include "lldb/Core/Timer.h"
24#include "lldb/Core/UUID.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000025#include "lldb/Host/Host.h"
26#include "lldb/Host/FileSpec.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000027#include "lldb/Symbol/ClangNamespaceDecl.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Symbol/ObjectFile.h"
Greg Clayton29021d32012-04-18 05:19:20 +000029#include "lldb/Target/Platform.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000030#include "lldb/Target/Process.h"
Greg Clayton29021d32012-04-18 05:19:20 +000031#include "lldb/Target/Target.h"
Greg Clayton9ce95382012-02-13 23:10:39 +000032#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
33#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Clayton46c9a352012-02-09 06:16:32 +000034#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner24943d22010-06-08 16:52:24 +000035
Chris Lattner24943d22010-06-08 16:52:24 +000036
37using 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;
753 struct segment_command_64 load_cmd;
754 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
755 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000756 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000757 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000758 ModuleSP module_sp (GetModule());
Chris Lattner24943d22010-06-08 16:52:24 +0000759 for (i=0; i<m_header.ncmds; ++i)
760 {
761 const uint32_t load_cmd_offset = offset;
762 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
763 break;
764
Greg Clayton1674b122010-07-21 22:12:05 +0000765 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000766 {
767 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
768 {
769 load_cmd.vmaddr = m_data.GetAddress(&offset);
770 load_cmd.vmsize = m_data.GetAddress(&offset);
771 load_cmd.fileoff = m_data.GetAddress(&offset);
772 load_cmd.filesize = m_data.GetAddress(&offset);
773 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
774 {
Greg Clayton68ca8232011-01-25 02:58:48 +0000775
776 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
777
Chris Lattner24943d22010-06-08 16:52:24 +0000778 // Keep a list of mach segments around in case we need to
779 // get at data that isn't stored in the abstracted Sections.
780 m_mach_segments.push_back (load_cmd);
781
782 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
783 // Use a segment ID of the segment index shifted left by 8 so they
784 // never conflict with any of the sections.
785 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000786 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000787 {
Greg Clayton3508c382012-02-24 01:59:29 +0000788 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000789 ++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
790 segment_name, // Name of this section
791 eSectionTypeContainer, // This section is a container of other sections.
792 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
793 load_cmd.vmsize, // VM size in bytes of this section
794 load_cmd.fileoff, // Offset to the data for this section in the file
795 load_cmd.filesize, // Size in bytes of this section as found in the the file
796 load_cmd.flags)); // Flags for this section
797
Greg Clayton68ca8232011-01-25 02:58:48 +0000798 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000799 m_sections_ap->AddSection(segment_sp);
800 }
801
802 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000803 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000804 // Push a section into our mach sections for the section at
Greg Clayton6af4fad2010-10-06 01:26:32 +0000805 // index zero (NListSectionNoSection) if we don't have any
806 // mach sections yet...
807 if (m_mach_sections.empty())
808 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000809 uint32_t segment_sect_idx;
810 const lldb::user_id_t first_segment_sectID = sectID + 1;
811
812
Greg Clayton1674b122010-07-21 22:12:05 +0000813 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000814 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
815 {
816 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
817 break;
818 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
819 break;
820 sect64.addr = m_data.GetAddress(&offset);
821 sect64.size = m_data.GetAddress(&offset);
822
823 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
824 break;
825
826 // Keep a list of mach sections around in case we need to
827 // get at data that isn't stored in the abstracted Sections.
828 m_mach_sections.push_back (sect64);
829
830 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
831 if (!segment_name)
832 {
833 // We have a segment with no name so we need to conjure up
834 // segments that correspond to the section's segname if there
835 // isn't already such a section. If there is such a section,
836 // we resize the section so that it spans all sections.
837 // We also mark these sections as fake so address matches don't
838 // hit if they land in the gaps between the child sections.
839 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
840 segment_sp = m_sections_ap->FindSectionByName (segment_name);
841 if (segment_sp.get())
842 {
843 Section *segment = segment_sp.get();
844 // Grow the section size as needed.
845 const lldb::addr_t sect64_min_addr = sect64.addr;
846 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
847 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
848 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
849 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
850 if (sect64_min_addr >= curr_seg_min_addr)
851 {
852 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
853 // Only grow the section size if needed
854 if (new_seg_byte_size > curr_seg_byte_size)
855 segment->SetByteSize (new_seg_byte_size);
856 }
857 else
858 {
859 // We need to change the base address of the segment and
860 // adjust the child section offsets for all existing children.
861 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
862 segment->Slide(slide_amount, false);
863 segment->GetChildren().Slide (-slide_amount, false);
864 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
865 }
Greg Clayton661825b2010-06-28 23:51:11 +0000866
867 // Grow the section size as needed.
868 if (sect64.offset)
869 {
870 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
871 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
872
873 const lldb::addr_t section_min_file_offset = sect64.offset;
874 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
875 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
876 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
877 segment->SetFileOffset (new_file_offset);
878 segment->SetFileSize (new_file_size);
879 }
Chris Lattner24943d22010-06-08 16:52:24 +0000880 }
881 else
882 {
883 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +0000884 segment_sp.reset(new Section (segment_sp, // Parent section
885 module_sp, // Module to which this section belongs
886 ++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
887 segment_name, // Name of this section
888 eSectionTypeContainer, // This section is a container of other sections.
889 sect64.addr, // File VM address == addresses as they are found in the object file
890 sect64.size, // VM size in bytes of this section
891 sect64.offset, // Offset to the data for this section in the file
892 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
893 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +0000894 segment_sp->SetIsFake(true);
895 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000896 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000897 }
898 }
899 assert (segment_sp.get());
900
Greg Clayton1674b122010-07-21 22:12:05 +0000901 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000902 static ConstString g_sect_name_objc_data ("__objc_data");
903 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
904 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
905 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
906 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
907 static ConstString g_sect_name_objc_const ("__objc_const");
908 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
909 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000910
911 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
912 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
913 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
914 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
915 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
916 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
917 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
918 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
919 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
920 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
921 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +0000922 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
923 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +0000924 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +0000925 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000926 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +0000927 static ConstString g_sect_name_DATA ("__DATA");
928 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000929
Chris Lattner24943d22010-06-08 16:52:24 +0000930 SectionType sect_type = eSectionTypeOther;
931
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000932 if (section_name == g_sect_name_dwarf_debug_abbrev)
933 sect_type = eSectionTypeDWARFDebugAbbrev;
934 else if (section_name == g_sect_name_dwarf_debug_aranges)
935 sect_type = eSectionTypeDWARFDebugAranges;
936 else if (section_name == g_sect_name_dwarf_debug_frame)
937 sect_type = eSectionTypeDWARFDebugFrame;
938 else if (section_name == g_sect_name_dwarf_debug_info)
939 sect_type = eSectionTypeDWARFDebugInfo;
940 else if (section_name == g_sect_name_dwarf_debug_line)
941 sect_type = eSectionTypeDWARFDebugLine;
942 else if (section_name == g_sect_name_dwarf_debug_loc)
943 sect_type = eSectionTypeDWARFDebugLoc;
944 else if (section_name == g_sect_name_dwarf_debug_macinfo)
945 sect_type = eSectionTypeDWARFDebugMacInfo;
946 else if (section_name == g_sect_name_dwarf_debug_pubnames)
947 sect_type = eSectionTypeDWARFDebugPubNames;
948 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
949 sect_type = eSectionTypeDWARFDebugPubTypes;
950 else if (section_name == g_sect_name_dwarf_debug_ranges)
951 sect_type = eSectionTypeDWARFDebugRanges;
952 else if (section_name == g_sect_name_dwarf_debug_str)
953 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +0000954 else if (section_name == g_sect_name_dwarf_apple_names)
955 sect_type = eSectionTypeDWARFAppleNames;
956 else if (section_name == g_sect_name_dwarf_apple_types)
957 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +0000958 else if (section_name == g_sect_name_dwarf_apple_namespaces)
959 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000960 else if (section_name == g_sect_name_dwarf_apple_objc)
961 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000962 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +0000963 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +0000964 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +0000965 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000966 else if (section_name == g_sect_name_eh_frame)
967 sect_type = eSectionTypeEHFrame;
968 else if (section_name == g_sect_name_cfstring)
969 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +0000970 else if (section_name == g_sect_name_objc_data ||
971 section_name == g_sect_name_objc_classrefs ||
972 section_name == g_sect_name_objc_superrefs ||
973 section_name == g_sect_name_objc_const ||
974 section_name == g_sect_name_objc_classlist)
975 {
976 sect_type = eSectionTypeDataPointers;
977 }
Chris Lattner24943d22010-06-08 16:52:24 +0000978
979 if (sect_type == eSectionTypeOther)
980 {
981 switch (mach_sect_type)
982 {
983 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +0000984 case SectionTypeRegular:
985 if (segment_sp->GetName() == g_sect_name_TEXT)
986 sect_type = eSectionTypeCode;
987 else if (segment_sp->GetName() == g_sect_name_DATA)
988 sect_type = eSectionTypeData;
989 else
990 sect_type = eSectionTypeOther;
991 break;
Greg Clayton1674b122010-07-21 22:12:05 +0000992 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
993 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
994 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
995 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
996 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
997 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
998 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
999 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1000 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1001 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1002 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1003 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1004 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1005 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1006 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1007 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001008 default: break;
1009 }
1010 }
1011
Greg Clayton3508c382012-02-24 01:59:29 +00001012 SectionSP section_sp(new Section (segment_sp,
1013 module_sp,
1014 ++sectID,
1015 section_name,
1016 sect_type,
1017 sect64.addr - segment_sp->GetFileAddress(),
1018 sect64.size,
1019 sect64.offset,
1020 sect64.offset == 0 ? 0 : sect64.size,
1021 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001022 // Set the section to be encrypted to match the segment
1023 section_sp->SetIsEncrypted (segment_is_encrypted);
1024
Chris Lattner24943d22010-06-08 16:52:24 +00001025 segment_sp->GetChildren().AddSection(section_sp);
1026
1027 if (segment_sp->IsFake())
1028 {
1029 segment_sp.reset();
1030 segment_name.Clear();
1031 }
1032 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001033 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001034 {
1035 if (first_segment_sectID <= sectID)
1036 {
1037 lldb::user_id_t sect_uid;
1038 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1039 {
1040 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1041 SectionSP next_section_sp;
1042 if (sect_uid + 1 <= sectID)
1043 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1044
1045 if (curr_section_sp.get())
1046 {
1047 if (curr_section_sp->GetByteSize() == 0)
1048 {
1049 if (next_section_sp.get() != NULL)
1050 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1051 else
1052 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1053 }
1054 }
1055 }
1056 }
1057 }
1058 }
1059 }
1060 }
Greg Clayton1674b122010-07-21 22:12:05 +00001061 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001062 {
1063 m_dysymtab.cmd = load_cmd.cmd;
1064 m_dysymtab.cmdsize = load_cmd.cmdsize;
1065 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1066 }
1067
1068 offset = load_cmd_offset + load_cmd.cmdsize;
1069 }
1070// if (dump_sections)
1071// {
1072// StreamFile s(stdout);
1073// m_sections_ap->Dump(&s, true);
1074// }
1075 return sectID; // Return the number of sections we registered with the module
1076}
1077
1078class MachSymtabSectionInfo
1079{
1080public:
1081
1082 MachSymtabSectionInfo (SectionList *section_list) :
1083 m_section_list (section_list),
1084 m_section_infos()
1085 {
1086 // Get the number of sections down to a depth of 1 to include
1087 // all segments and their sections, but no other sections that
1088 // may be added for debug map or
1089 m_section_infos.resize(section_list->GetNumSections(1));
1090 }
1091
1092
Greg Clayton3508c382012-02-24 01:59:29 +00001093 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001094 GetSection (uint8_t n_sect, addr_t file_addr)
1095 {
1096 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001097 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001098 if (n_sect < m_section_infos.size())
1099 {
Greg Clayton3508c382012-02-24 01:59:29 +00001100 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001101 {
Greg Clayton3508c382012-02-24 01:59:29 +00001102 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1103 m_section_infos[n_sect].section_sp = section_sp;
1104 if (section_sp != NULL)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001105 {
Greg Clayton3508c382012-02-24 01:59:29 +00001106 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1107 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001108 }
1109 else
1110 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001111 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001112 }
Chris Lattner24943d22010-06-08 16:52:24 +00001113 }
1114 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001115 {
1116 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001117 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001118 }
1119 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1120 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1121 {
1122 // Symbol is in section with zero size, but has the same start
1123 // address as the section. This can happen with linker symbols
1124 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001125 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001126 }
Chris Lattner24943d22010-06-08 16:52:24 +00001127 }
Greg Clayton3508c382012-02-24 01:59:29 +00001128 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001129 }
1130
1131protected:
1132 struct SectionInfo
1133 {
1134 SectionInfo () :
1135 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001136 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001137 {
1138 }
1139
1140 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001141 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001142 };
1143 SectionList *m_section_list;
1144 std::vector<SectionInfo> m_section_infos;
1145};
1146
1147
1148
1149size_t
1150ObjectFileMachO::ParseSymtab (bool minimize)
1151{
1152 Timer scoped_timer(__PRETTY_FUNCTION__,
1153 "ObjectFileMachO::ParseSymtab () module = %s",
1154 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001155 ModuleSP module_sp (GetModule());
1156 if (!module_sp)
1157 return 0;
1158
1159 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1160 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1161 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1162 FunctionStarts function_starts;
Chris Lattner24943d22010-06-08 16:52:24 +00001163 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1164 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001165
Greg Clayton0fea0512011-12-30 00:32:24 +00001166 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
1167
Chris Lattner24943d22010-06-08 16:52:24 +00001168 for (i=0; i<m_header.ncmds; ++i)
1169 {
1170 const uint32_t cmd_offset = offset;
1171 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001172 struct load_command lc;
1173 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001174 break;
1175 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001176 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001177 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001178 case LoadCommandSymtab:
1179 symtab_load_command.cmd = lc.cmd;
1180 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001181 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001182 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1183 return 0;
1184 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001185 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001186 if (log)
1187 module_sp->LogMessage(log.get(), "LC_SYMTAB.symoff == 0");
1188 return 0;
1189 }
1190
1191 if (symtab_load_command.stroff == 0)
1192 {
1193 if (log)
1194 module_sp->LogMessage(log.get(), "LC_SYMTAB.stroff == 0");
1195 return 0;
1196 }
1197
1198 if (symtab_load_command.nsyms == 0)
1199 {
1200 if (log)
1201 module_sp->LogMessage(log.get(), "LC_SYMTAB.nsyms == 0");
1202 return 0;
1203 }
1204
1205 if (symtab_load_command.strsize == 0)
1206 {
1207 if (log)
1208 module_sp->LogMessage(log.get(), "LC_SYMTAB.strsize == 0");
1209 return 0;
1210 }
1211 break;
1212
1213 case LoadCommandFunctionStarts:
1214 function_starts_load_command.cmd = lc.cmd;
1215 function_starts_load_command.cmdsize = lc.cmdsize;
1216 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1217 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1218 break;
1219
1220 default:
1221 break;
1222 }
1223 offset = cmd_offset + lc.cmdsize;
1224 }
1225
1226 if (symtab_load_command.cmd)
1227 {
1228 Symtab *symtab = m_symtab_ap.get();
1229 SectionList *section_list = GetSectionList();
1230 if (section_list == NULL)
1231 return 0;
1232
1233 ProcessSP process_sp (m_process_wp.lock());
1234
1235 const size_t addr_byte_size = m_data.GetAddressByteSize();
1236 bool bit_width_32 = addr_byte_size == 4;
1237 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1238
1239 DataExtractor nlist_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1240 DataExtractor strtab_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1241 DataExtractor function_starts_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1242
1243 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1244 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
1245 if (process_sp)
1246 {
1247 Target &target = process_sp->GetTarget();
1248 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1249 // Reading mach file from memory in a process or core file...
1250
1251 if (linkedit_section_sp)
1252 {
1253 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1254 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1255 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
1256 const addr_t stroff_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001257
1258 bool data_was_read = false;
1259
1260#if defined (__APPLE__) && defined (__arm__)
1261 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001262 {
Greg Clayton29021d32012-04-18 05:19:20 +00001263 // This mach-o memory file is in the dyld shared cache. If this
1264 // program is not remote and this is iOS, then this process will
1265 // share the same shared cache as the process we are debugging and
1266 // we can read the entire __LINKEDIT from the address space in this
1267 // process. This is a needed optimization that is used for local iOS
1268 // debugging only since all shared libraries in the shared cache do
1269 // not have corresponding files that exist in the file system of the
1270 // device. They have been combined into a single file. This means we
1271 // always have to load these files from memory. All of the symbol and
1272 // string tables from all of the __LINKEDIT sections from the shared
1273 // libraries in the shared cache have been merged into a single large
1274 // symbol and string table. Reading all of this symbol and string table
1275 // data across can slow down debug launch times, so we optimize this by
1276 // reading the memory for the __LINKEDIT section from this process.
1277 PlatformSP platform_sp (target.GetPlatform());
1278 if (platform_sp && platform_sp->IsHost())
1279 {
1280 data_was_read = true;
1281 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
1282 strtab_data.SetData((void *)stroff_addr, strtab_data_byte_size, eByteOrderLittle);
1283 if (function_starts_load_command.cmd)
1284 {
1285 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1286 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1287 }
1288 }
1289 }
1290#endif
1291
1292 if (!data_was_read)
1293 {
1294 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1295 if (nlist_data_sp)
1296 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
1297 DataBufferSP strtab_data_sp (ReadMemory (process_sp, stroff_addr, strtab_data_byte_size));
1298 if (strtab_data_sp)
1299 strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
1300 if (function_starts_load_command.cmd)
1301 {
1302 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1303 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1304 if (func_start_data_sp)
1305 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1306 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001307 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001308 }
1309 }
1310 else
1311 {
1312 nlist_data.SetData (m_data,
1313 symtab_load_command.symoff,
1314 nlist_data_byte_size);
1315 strtab_data.SetData (m_data,
1316 symtab_load_command.stroff,
1317 strtab_data_byte_size);
1318 if (function_starts_load_command.cmd)
1319 {
1320 function_starts_data.SetData (m_data,
1321 function_starts_load_command.dataoff,
1322 function_starts_load_command.datasize);
1323 }
1324 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001325
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001326 if (nlist_data.GetByteSize() == 0)
1327 {
1328 if (log)
1329 module_sp->LogMessage(log.get(), "failed to read nlist data");
1330 return 0;
1331 }
1332
1333
1334 if (strtab_data.GetByteSize() == 0)
1335 {
1336 if (log)
1337 module_sp->LogMessage(log.get(), "failed to read strtab data");
1338 return 0;
1339 }
1340
1341 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1342 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1343 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1344 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1345 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1346 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1347 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1348 SectionSP eh_frame_section_sp;
1349 if (text_section_sp.get())
1350 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1351 else
1352 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1353
Greg Claytond2653c22012-03-14 01:53:24 +00001354 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001355 if (text_section_sp && function_starts_data.GetByteSize())
1356 {
1357 FunctionStarts::Entry function_start_entry;
1358 function_start_entry.data = false;
1359 uint32_t function_start_offset = 0;
1360 function_start_entry.addr = text_section_sp->GetFileAddress();
1361 uint64_t delta;
1362 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1363 {
1364 // Now append the current entry
1365 function_start_entry.addr += delta;
1366 function_starts.Append(function_start_entry);
1367 }
1368 }
1369
1370 const uint32_t function_starts_count = function_starts.GetSize();
1371
1372 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
1373
1374 uint32_t nlist_data_offset = 0;
1375
1376 uint32_t N_SO_index = UINT32_MAX;
1377
1378 MachSymtabSectionInfo section_info (section_list);
1379 std::vector<uint32_t> N_FUN_indexes;
1380 std::vector<uint32_t> N_NSYM_indexes;
1381 std::vector<uint32_t> N_INCL_indexes;
1382 std::vector<uint32_t> N_BRAC_indexes;
1383 std::vector<uint32_t> N_COMM_indexes;
1384 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1385 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1386 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1387 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1388 // Any symbols that get merged into another will get an entry
1389 // in this map so we know
1390 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1391 uint32_t nlist_idx = 0;
1392 Symbol *symbol_ptr = NULL;
1393
1394 uint32_t sym_idx = 0;
1395 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
1396 uint32_t num_syms = symtab->GetNumSymbols();
1397
1398 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
1399 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
1400 {
1401 struct nlist_64 nlist;
1402 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1403 break;
1404
1405 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
1406 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
1407 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
1408 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
1409 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
1410
1411 SymbolType type = eSymbolTypeInvalid;
1412 const char *symbol_name = strtab_data.PeekCStr(nlist.n_strx);
1413 if (symbol_name == NULL)
1414 {
1415 // No symbol should be NULL, even the symbols with no
1416 // string values should have an offset zero which points
1417 // to an empty C-string
1418 Host::SystemLog (Host::eSystemLogError,
1419 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1420 nlist_idx,
1421 nlist.n_strx,
1422 module_sp->GetFileSpec().GetDirectory().GetCString(),
1423 module_sp->GetFileSpec().GetFilename().GetCString());
1424 continue;
1425 }
1426 const char *symbol_name_non_abi_mangled = NULL;
1427
1428 if (symbol_name[0] == '\0')
1429 symbol_name = NULL;
1430 SectionSP symbol_section;
1431 uint32_t symbol_byte_size = 0;
1432 bool add_nlist = true;
1433 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
1434
1435 assert (sym_idx < num_syms);
1436
1437 sym[sym_idx].SetDebug (is_debug);
1438
1439 if (is_debug)
1440 {
1441 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00001442 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001443 case StabGlobalSymbol:
1444 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1445 // Sometimes the N_GSYM value contains the address.
1446
1447 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1448 // have the same address, but we want to ensure that we always find only the real symbol,
1449 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1450 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1451 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1452 // same address.
1453
1454 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1455 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1456 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1457 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1458 add_nlist = false;
1459 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00001460 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001461 sym[sym_idx].SetExternal(true);
1462 if (nlist.n_value != 0)
1463 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1464 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00001465 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001466 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00001467
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001468 case StabFunctionName:
1469 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1470 type = eSymbolTypeCompiler;
1471 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00001472
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001473 case StabFunction:
1474 // N_FUN -- procedure: name,,n_sect,linenumber,address
1475 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00001476 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001477 type = eSymbolTypeCode;
1478 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1479
1480 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1481 // We use the current number of symbols in the symbol table in lieu of
1482 // using nlist_idx in case we ever start trimming entries out
1483 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001484 }
1485 else
1486 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001487 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00001488
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001489 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00001490 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001491 // Copy the size of the function into the original STAB entry so we don't have
1492 // to hunt for it later
1493 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1494 N_FUN_indexes.pop_back();
1495 // We don't really need the end function STAB as it contains the size which
1496 // we already placed with the original symbol, so don't add it if we want a
1497 // minimal symbol table
1498 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00001499 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001500 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00001501 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001502 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00001503
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001504 case StabStaticSymbol:
1505 // N_STSYM -- static symbol: name,,n_sect,type,address
1506 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1507 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1508 type = eSymbolTypeData;
1509 break;
1510
1511 case StabLocalCommon:
1512 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1513 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1514 type = eSymbolTypeCommonBlock;
1515 break;
1516
1517 case StabBeginSymbol:
1518 // N_BNSYM
1519 // We use the current number of symbols in the symbol table in lieu of
1520 // using nlist_idx in case we ever start trimming entries out
1521 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00001522 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001523 // Skip these if we want minimal symbol tables
1524 add_nlist = false;
1525 }
1526 else
1527 {
1528 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1529 N_NSYM_indexes.push_back(sym_idx);
1530 type = eSymbolTypeScopeBegin;
1531 }
1532 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00001533
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001534 case StabEndSymbol:
1535 // N_ENSYM
1536 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1537 // so that we can always skip the entire symbol if we need to navigate
1538 // more quickly at the source level when parsing STABS
1539 if (minimize)
1540 {
1541 // Skip these if we want minimal symbol tables
1542 add_nlist = false;
1543 }
1544 else
1545 {
1546 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00001547 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001548 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1549 symbol_ptr->SetByteSize(sym_idx + 1);
1550 symbol_ptr->SetSizeIsSibling(true);
1551 N_NSYM_indexes.pop_back();
1552 }
1553 type = eSymbolTypeScopeEnd;
1554 }
1555 break;
1556
1557
1558 case StabSourceFileOptions:
1559 // N_OPT - emitted with gcc2_compiled and in gcc source
1560 type = eSymbolTypeCompiler;
1561 break;
1562
1563 case StabRegisterSymbol:
1564 // N_RSYM - register sym: name,,NO_SECT,type,register
1565 type = eSymbolTypeVariable;
1566 break;
1567
1568 case StabSourceLine:
1569 // N_SLINE - src line: 0,,n_sect,linenumber,address
1570 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1571 type = eSymbolTypeLineEntry;
1572 break;
1573
1574 case StabStructureType:
1575 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1576 type = eSymbolTypeVariableType;
1577 break;
1578
1579 case StabSourceFileName:
1580 // N_SO - source file name
1581 type = eSymbolTypeSourceFile;
1582 if (symbol_name == NULL)
1583 {
1584 if (minimize)
1585 add_nlist = false;
1586 if (N_SO_index != UINT32_MAX)
1587 {
1588 // Set the size of the N_SO to the terminating index of this N_SO
1589 // so that we can always skip the entire N_SO if we need to navigate
1590 // more quickly at the source level when parsing STABS
1591 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1592 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1593 symbol_ptr->SetSizeIsSibling(true);
1594 }
1595 N_NSYM_indexes.clear();
1596 N_INCL_indexes.clear();
1597 N_BRAC_indexes.clear();
1598 N_COMM_indexes.clear();
1599 N_FUN_indexes.clear();
1600 N_SO_index = UINT32_MAX;
1601 }
1602 else
1603 {
1604 // We use the current number of symbols in the symbol table in lieu of
1605 // using nlist_idx in case we ever start trimming entries out
1606 if (symbol_name[0] == '/')
1607 N_SO_index = sym_idx;
1608 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1609 {
1610 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1611 if (so_path && so_path[0])
1612 {
1613 std::string full_so_path (so_path);
1614 if (*full_so_path.rbegin() != '/')
1615 full_so_path += '/';
1616 full_so_path += symbol_name;
1617 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
1618 add_nlist = false;
1619 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1620 }
1621 }
1622 }
1623
1624 break;
1625
1626 case StabObjectFileName:
1627 // N_OSO - object file name: name,,0,0,st_mtime
1628 type = eSymbolTypeObjectFile;
1629 break;
1630
1631 case StabLocalSymbol:
1632 // N_LSYM - local sym: name,,NO_SECT,type,offset
1633 type = eSymbolTypeLocal;
1634 break;
1635
1636 //----------------------------------------------------------------------
1637 // INCL scopes
1638 //----------------------------------------------------------------------
1639 case StabBeginIncludeFileName:
1640 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
1641 // We use the current number of symbols in the symbol table in lieu of
1642 // using nlist_idx in case we ever start trimming entries out
1643 N_INCL_indexes.push_back(sym_idx);
1644 type = eSymbolTypeScopeBegin;
1645 break;
1646
1647 case StabEndIncludeFile:
1648 // N_EINCL - include file end: name,,NO_SECT,0,0
1649 // Set the size of the N_BINCL to the terminating index of this N_EINCL
1650 // so that we can always skip the entire symbol if we need to navigate
1651 // more quickly at the source level when parsing STABS
1652 if ( !N_INCL_indexes.empty() )
1653 {
1654 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1655 symbol_ptr->SetByteSize(sym_idx + 1);
1656 symbol_ptr->SetSizeIsSibling(true);
1657 N_INCL_indexes.pop_back();
1658 }
1659 type = eSymbolTypeScopeEnd;
1660 break;
1661
1662 case StabIncludeFileName:
1663 // N_SOL - #included file name: name,,n_sect,0,address
1664 type = eSymbolTypeHeaderFile;
1665
1666 // We currently don't use the header files on darwin
1667 if (minimize)
1668 add_nlist = false;
1669 break;
1670
1671 case StabCompilerParameters:
1672 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
1673 type = eSymbolTypeCompiler;
1674 break;
1675
1676 case StabCompilerVersion:
1677 // N_VERSION - compiler version: name,,NO_SECT,0,0
1678 type = eSymbolTypeCompiler;
1679 break;
1680
1681 case StabCompilerOptLevel:
1682 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
1683 type = eSymbolTypeCompiler;
1684 break;
1685
1686 case StabParameter:
1687 // N_PSYM - parameter: name,,NO_SECT,type,offset
1688 type = eSymbolTypeVariable;
1689 break;
1690
1691 case StabAlternateEntry:
1692 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
1693 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1694 type = eSymbolTypeLineEntry;
1695 break;
1696
1697 //----------------------------------------------------------------------
1698 // Left and Right Braces
1699 //----------------------------------------------------------------------
1700 case StabLeftBracket:
1701 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
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 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1705 N_BRAC_indexes.push_back(sym_idx);
1706 type = eSymbolTypeScopeBegin;
1707 break;
1708
1709 case StabRightBracket:
1710 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
1711 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
1712 // so that we can always skip the entire symbol if we need to navigate
1713 // more quickly at the source level when parsing STABS
1714 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1715 if ( !N_BRAC_indexes.empty() )
1716 {
1717 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1718 symbol_ptr->SetByteSize(sym_idx + 1);
1719 symbol_ptr->SetSizeIsSibling(true);
1720 N_BRAC_indexes.pop_back();
1721 }
1722 type = eSymbolTypeScopeEnd;
1723 break;
1724
1725 case StabDeletedIncludeFile:
1726 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
1727 type = eSymbolTypeHeaderFile;
1728 break;
1729
1730 //----------------------------------------------------------------------
1731 // COMM scopes
1732 //----------------------------------------------------------------------
1733 case StabBeginCommon:
1734 // N_BCOMM - begin common: name,,NO_SECT,0,0
1735 // We use the current number of symbols in the symbol table in lieu of
1736 // using nlist_idx in case we ever start trimming entries out
1737 type = eSymbolTypeScopeBegin;
1738 N_COMM_indexes.push_back(sym_idx);
1739 break;
1740
1741 case StabEndCommonLocal:
1742 // N_ECOML - end common (local name): 0,,n_sect,0,address
1743 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1744 // Fall through
1745
1746 case StabEndCommon:
1747 // N_ECOMM - end common: name,,n_sect,0,0
1748 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1749 // so that we can always skip the entire symbol if we need to navigate
1750 // more quickly at the source level when parsing STABS
1751 if ( !N_COMM_indexes.empty() )
1752 {
1753 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1754 symbol_ptr->SetByteSize(sym_idx + 1);
1755 symbol_ptr->SetSizeIsSibling(true);
1756 N_COMM_indexes.pop_back();
1757 }
1758 type = eSymbolTypeScopeEnd;
1759 break;
1760
1761 case StabLength:
1762 // N_LENG - second stab entry with length information
1763 type = eSymbolTypeAdditional;
1764 break;
1765
1766 default: break;
1767 }
1768 }
1769 else
1770 {
1771 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
1772 uint8_t n_type = NlistMaskType & nlist.n_type;
1773 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
1774
1775 switch (n_type)
1776 {
1777 case NListTypeIndirect: // N_INDR - Fall through
1778 case NListTypePreboundUndefined:// N_PBUD - Fall through
1779 case NListTypeUndefined: // N_UNDF
1780 type = eSymbolTypeUndefined;
1781 break;
1782
1783 case NListTypeAbsolute: // N_ABS
1784 type = eSymbolTypeAbsolute;
1785 break;
1786
1787 case NListTypeSection: // N_SECT
1788 {
1789 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1790
1791 if (symbol_section == NULL)
1792 {
1793 // TODO: warn about this?
1794 add_nlist = false;
1795 break;
1796 }
1797
1798 if (TEXT_eh_frame_sectID == nlist.n_sect)
1799 {
1800 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00001801 }
1802 else
1803 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001804 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
1805
1806 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001807 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001808 case SectionTypeRegular: break; // regular section
1809 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
1810 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
1811 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
1812 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
1813 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1814 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1815 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1816 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1817 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
1818 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
1819 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
1820 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
1821 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
1822 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
1823 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
1824 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
1825 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00001826 }
Chris Lattner24943d22010-06-08 16:52:24 +00001827
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001828 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00001829 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001830 const char *symbol_sect_name = symbol_section->GetName().AsCString();
1831 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00001832 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001833 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1834 SectionAttrUserSelfModifyingCode |
1835 SectionAttrSytemSomeInstructions))
1836 type = eSymbolTypeData;
1837 else
1838 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00001839 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001840 else
1841 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00001842 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001843 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00001844 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001845 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00001846
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001847 if (symbol_name &&
1848 symbol_name[0] == '_' &&
1849 symbol_name[1] == 'O' &&
1850 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00001851 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001852 llvm::StringRef symbol_name_ref(symbol_name);
1853 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
1854 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
1855 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
1856 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00001857 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001858 symbol_name_non_abi_mangled = symbol_name + 1;
1859 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
1860 type = eSymbolTypeObjCClass;
Chris Lattner24943d22010-06-08 16:52:24 +00001861 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001862 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00001863 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001864 symbol_name_non_abi_mangled = symbol_name + 1;
1865 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
1866 type = eSymbolTypeObjCMetaClass;
1867 }
1868 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
1869 {
1870 symbol_name_non_abi_mangled = symbol_name + 1;
1871 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
1872 type = eSymbolTypeObjCIVar;
Chris Lattner24943d22010-06-08 16:52:24 +00001873 }
1874 }
1875 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001876 else
1877 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1878 {
1879 type = eSymbolTypeException;
1880 }
1881 else
1882 {
1883 type = eSymbolTypeData;
1884 }
1885 }
1886 else
1887 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1888 {
1889 type = eSymbolTypeTrampoline;
1890 }
1891 else
1892 if (symbol_section->IsDescendant(objc_section_sp.get()))
1893 {
1894 type = eSymbolTypeRuntime;
1895 if (symbol_name && symbol_name[0] == '.')
1896 {
1897 llvm::StringRef symbol_name_ref(symbol_name);
1898 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
1899 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
1900 {
1901 symbol_name_non_abi_mangled = symbol_name;
1902 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
1903 type = eSymbolTypeObjCClass;
1904 }
1905 }
Chris Lattner24943d22010-06-08 16:52:24 +00001906 }
1907 }
1908 }
1909 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001910 break;
1911 }
1912 }
1913
1914 if (add_nlist)
1915 {
1916 uint64_t symbol_value = nlist.n_value;
1917 bool symbol_name_is_mangled = false;
1918
1919 if (symbol_name_non_abi_mangled)
1920 {
1921 sym[sym_idx].GetMangled().SetMangledName (symbol_name_non_abi_mangled);
1922 sym[sym_idx].GetMangled().SetDemangledName (symbol_name);
Chris Lattner24943d22010-06-08 16:52:24 +00001923 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001924 else
1925 {
1926 if (symbol_name && symbol_name[0] == '_')
1927 {
1928 symbol_name_is_mangled = symbol_name[1] == '_';
1929 symbol_name++; // Skip the leading underscore
1930 }
1931
1932 if (symbol_name)
1933 {
1934 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
1935 }
1936 }
1937
1938 if (is_debug == false)
1939 {
1940 if (type == eSymbolTypeCode)
1941 {
1942 // See if we can find a N_FUN entry for any code symbols.
1943 // If we do find a match, and the name matches, then we
1944 // can merge the two into just the function symbol to avoid
1945 // duplicate entries in the symbol table
1946 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1947 if (pos != N_FUN_addr_to_sym_idx.end())
1948 {
1949 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1950 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1951 {
1952 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
1953 // We just need the flags from the linker symbol, so put these flags
1954 // into the N_FUN flags to avoid duplicate symbols in the symbol table
1955 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1956 sym[sym_idx].Clear();
1957 continue;
1958 }
1959 }
1960 }
1961 else if (type == eSymbolTypeData)
1962 {
1963 // See if we can find a N_STSYM entry for any data symbols.
1964 // If we do find a match, and the name matches, then we
1965 // can merge the two into just the Static symbol to avoid
1966 // duplicate entries in the symbol table
1967 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
1968 if (pos != N_STSYM_addr_to_sym_idx.end())
1969 {
1970 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1971 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1972 {
1973 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
1974 // We just need the flags from the linker symbol, so put these flags
1975 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
1976 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1977 sym[sym_idx].Clear();
1978 continue;
1979 }
1980 }
1981 }
1982 }
1983 if (symbol_section)
1984 {
1985 const addr_t section_file_addr = symbol_section->GetFileAddress();
1986 if (symbol_byte_size == 0 && function_starts_count > 0)
1987 {
Greg Claytond2653c22012-03-14 01:53:24 +00001988 addr_t symbol_lookup_file_addr = nlist.n_value;
1989 // Do an exact address match for non-ARM addresses, else get the closest since
1990 // the symbol might be a thumb symbol which has an address with bit zero set
1991 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
1992 if (is_arm && func_start_entry)
1993 {
1994 // Verify that the function start address is the symbol address (ARM)
1995 // or the symbol address + 1 (thumb)
1996 if (func_start_entry->addr != symbol_lookup_file_addr &&
1997 func_start_entry->addr != (symbol_lookup_file_addr + 1))
1998 {
1999 // Not the right entry, NULL it out...
2000 func_start_entry = NULL;
2001 }
2002 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002003 if (func_start_entry)
2004 {
2005 func_start_entry->data = true;
Greg Claytond2653c22012-03-14 01:53:24 +00002006
2007 addr_t symbol_file_addr = func_start_entry->addr;
2008 uint32_t symbol_flags = 0;
2009 if (is_arm)
2010 {
2011 if (symbol_file_addr & 1)
2012 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2013 symbol_file_addr &= 0xfffffffffffffffeull;
2014 }
2015
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002016 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2017 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2018 if (next_func_start_entry)
2019 {
Greg Claytond2653c22012-03-14 01:53:24 +00002020 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2021 // Be sure the clear the Thumb address bit when we calculate the size
2022 // from the current and next address
2023 if (is_arm)
2024 next_symbol_file_addr &= 0xfffffffffffffffeull;
2025 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 +00002026 }
2027 else
2028 {
Greg Claytond2653c22012-03-14 01:53:24 +00002029 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002030 }
2031 }
2032 }
2033 symbol_value -= section_file_addr;
2034 }
2035
2036 sym[sym_idx].SetID (nlist_idx);
2037 sym[sym_idx].SetType (type);
2038 sym[sym_idx].GetAddress().SetSection (symbol_section);
2039 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2040 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2041
2042 if (symbol_byte_size > 0)
2043 sym[sym_idx].SetByteSize(symbol_byte_size);
2044
2045 ++sym_idx;
2046 }
2047 else
2048 {
2049 sym[sym_idx].Clear();
2050 }
2051
2052 }
2053
2054 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
2055 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
2056 // such entries by figuring out what the address for the global is by looking up this non-STAB
2057 // entry and copying the value into the debug symbol's value to save us the hassle in the
2058 // debug symbol parser.
2059
2060 Symbol *global_symbol = NULL;
2061 for (nlist_idx = 0;
2062 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
2063 nlist_idx++)
2064 {
2065 if (global_symbol->GetAddress().GetFileAddress() == 0)
2066 {
2067 std::vector<uint32_t> indexes;
2068 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
2069 {
2070 std::vector<uint32_t>::const_iterator pos;
2071 std::vector<uint32_t>::const_iterator end = indexes.end();
2072 for (pos = indexes.begin(); pos != end; ++pos)
2073 {
2074 symbol_ptr = symtab->SymbolAtIndex(*pos);
2075 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
2076 {
2077 global_symbol->GetAddress() = symbol_ptr->GetAddress();
2078 break;
2079 }
2080 }
2081 }
Chris Lattner24943d22010-06-08 16:52:24 +00002082 }
2083 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002084
2085 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
2086
2087
2088 if (function_starts_count > 0)
2089 {
2090 char synthetic_function_symbol[PATH_MAX];
2091 uint32_t num_synthetic_function_symbols = 0;
2092 for (i=0; i<function_starts_count; ++i)
2093 {
2094 if (function_starts.GetEntryRef (i).data == false)
2095 ++num_synthetic_function_symbols;
2096 }
2097
2098 if (num_synthetic_function_symbols > 0)
2099 {
2100 if (num_syms < sym_idx + num_synthetic_function_symbols)
2101 {
2102 num_syms = sym_idx + num_synthetic_function_symbols;
2103 sym = symtab->Resize (num_syms);
2104 }
2105 uint32_t synthetic_function_symbol_idx = 0;
2106 for (i=0; i<function_starts_count; ++i)
2107 {
2108 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
2109 if (func_start_entry->data == false)
2110 {
Greg Claytond2653c22012-03-14 01:53:24 +00002111 addr_t symbol_file_addr = func_start_entry->addr;
2112 uint32_t symbol_flags = 0;
2113 if (is_arm)
2114 {
2115 if (symbol_file_addr & 1)
2116 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2117 symbol_file_addr &= 0xfffffffffffffffeull;
2118 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002119 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00002120 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002121 {
2122 SectionSP symbol_section (symbol_addr.GetSection());
2123 uint32_t symbol_byte_size = 0;
2124 if (symbol_section)
2125 {
2126 const addr_t section_file_addr = symbol_section->GetFileAddress();
2127 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2128 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2129 if (next_func_start_entry)
2130 {
Greg Claytond2653c22012-03-14 01:53:24 +00002131 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2132 if (is_arm)
2133 next_symbol_file_addr &= 0xfffffffffffffffeull;
2134 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 +00002135 }
2136 else
2137 {
Greg Claytond2653c22012-03-14 01:53:24 +00002138 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002139 }
2140 snprintf (synthetic_function_symbol,
2141 sizeof(synthetic_function_symbol),
2142 "___lldb_unnamed_function%u$$%s",
2143 ++synthetic_function_symbol_idx,
2144 module_sp->GetFileSpec().GetFilename().GetCString());
2145 sym[sym_idx].SetID (synthetic_sym_id++);
2146 sym[sym_idx].GetMangled().SetDemangledName(synthetic_function_symbol);
2147 sym[sym_idx].SetType (eSymbolTypeCode);
2148 sym[sym_idx].SetIsSynthetic (true);
2149 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00002150 if (symbol_flags)
2151 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002152 if (symbol_byte_size)
2153 sym[sym_idx].SetByteSize (symbol_byte_size);
2154 ++sym_idx;
2155 }
2156 }
2157 }
2158 }
2159 }
2160 }
2161
2162 // Trim our symbols down to just what we ended up with after
2163 // removing any symbols.
2164 if (sym_idx < num_syms)
2165 {
2166 num_syms = sym_idx;
2167 sym = symtab->Resize (num_syms);
2168 }
2169
2170 // Now synthesize indirect symbols
2171 if (m_dysymtab.nindirectsyms != 0)
2172 {
2173 DataExtractor indirect_symbol_index_data (m_data, m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4);
2174
2175 if (indirect_symbol_index_data.GetByteSize())
2176 {
2177 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
2178
2179 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
2180 {
2181 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
2182 {
2183 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
2184 if (symbol_stub_byte_size == 0)
2185 continue;
2186
2187 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
2188
2189 if (num_symbol_stubs == 0)
2190 continue;
2191
2192 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
2193 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
2194 {
2195 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
2196 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
2197 uint32_t symbol_stub_offset = symbol_stub_index * 4;
2198 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
2199 {
2200 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
2201 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
2202 continue;
2203
2204 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
2205 Symbol *stub_symbol = NULL;
2206 if (index_pos != end_index_pos)
2207 {
2208 // We have a remapping from the original nlist index to
2209 // a current symbol index, so just look this up by index
2210 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
2211 }
2212 else
2213 {
2214 // We need to lookup a symbol using the original nlist
2215 // symbol index since this index is coming from the
2216 // S_SYMBOL_STUBS
2217 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
2218 }
2219
2220 assert (stub_symbol);
2221 if (stub_symbol)
2222 {
2223 Address so_addr(symbol_stub_addr, section_list);
2224
2225 if (stub_symbol->GetType() == eSymbolTypeUndefined)
2226 {
2227 // Change the external symbol into a trampoline that makes sense
2228 // These symbols were N_UNDF N_EXT, and are useless to us, so we
2229 // can re-use them so we don't have to make up a synthetic symbol
2230 // for no good reason.
2231 stub_symbol->SetType (eSymbolTypeTrampoline);
2232 stub_symbol->SetExternal (false);
2233 stub_symbol->GetAddress() = so_addr;
2234 stub_symbol->SetByteSize (symbol_stub_byte_size);
2235 }
2236 else
2237 {
2238 // Make a synthetic symbol to describe the trampoline stub
2239 if (sym_idx >= num_syms)
2240 sym = symtab->Resize (++num_syms);
2241 sym[sym_idx].SetID (synthetic_sym_id++);
2242 sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
2243 sym[sym_idx].SetType (eSymbolTypeTrampoline);
2244 sym[sym_idx].SetIsSynthetic (true);
2245 sym[sym_idx].GetAddress() = so_addr;
2246 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
2247 ++sym_idx;
2248 }
2249 }
2250 }
2251 }
2252 }
2253 }
2254 }
2255 }
2256 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00002257 }
2258 return 0;
2259}
2260
2261
2262void
2263ObjectFileMachO::Dump (Stream *s)
2264{
Greg Clayton9482f052012-03-13 23:14:29 +00002265 ModuleSP module_sp(GetModule());
2266 if (module_sp)
2267 {
2268 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2269 s->Printf("%p: ", this);
2270 s->Indent();
2271 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
2272 s->PutCString("ObjectFileMachO64");
2273 else
2274 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00002275
Greg Clayton9482f052012-03-13 23:14:29 +00002276 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00002277
Greg Clayton9482f052012-03-13 23:14:29 +00002278 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00002279
Greg Clayton9482f052012-03-13 23:14:29 +00002280 if (m_sections_ap.get())
2281 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00002282
Greg Clayton9482f052012-03-13 23:14:29 +00002283 if (m_symtab_ap.get())
2284 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
2285 }
Chris Lattner24943d22010-06-08 16:52:24 +00002286}
2287
2288
2289bool
Greg Clayton0467c782011-02-04 18:53:10 +00002290ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00002291{
Greg Clayton9482f052012-03-13 23:14:29 +00002292 ModuleSP module_sp(GetModule());
2293 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002294 {
Greg Clayton9482f052012-03-13 23:14:29 +00002295 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2296 struct uuid_command load_cmd;
2297 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2298 uint32_t i;
2299 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00002300 {
Greg Clayton9482f052012-03-13 23:14:29 +00002301 const uint32_t cmd_offset = offset;
2302 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
2303 break;
2304
2305 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00002306 {
Greg Clayton9482f052012-03-13 23:14:29 +00002307 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
2308 if (uuid_bytes)
2309 {
2310 uuid->SetBytes (uuid_bytes);
2311 return true;
2312 }
2313 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00002314 }
Greg Clayton9482f052012-03-13 23:14:29 +00002315 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00002316 }
Chris Lattner24943d22010-06-08 16:52:24 +00002317 }
2318 return false;
2319}
2320
2321
2322uint32_t
2323ObjectFileMachO::GetDependentModules (FileSpecList& files)
2324{
Chris Lattner24943d22010-06-08 16:52:24 +00002325 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00002326 ModuleSP module_sp(GetModule());
2327 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002328 {
Greg Clayton9482f052012-03-13 23:14:29 +00002329 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2330 struct load_command load_cmd;
2331 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2332 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
2333 uint32_t i;
2334 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00002335 {
Greg Clayton9482f052012-03-13 23:14:29 +00002336 const uint32_t cmd_offset = offset;
2337 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
2338 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002339
Greg Clayton9482f052012-03-13 23:14:29 +00002340 switch (load_cmd.cmd)
2341 {
2342 case LoadCommandDylibLoad:
2343 case LoadCommandDylibLoadWeak:
2344 case LoadCommandDylibReexport:
2345 case LoadCommandDynamicLinkerLoad:
2346 case LoadCommandFixedVMShlibLoad:
2347 case LoadCommandDylibLoadUpward:
2348 {
2349 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
2350 const char *path = m_data.PeekCStr(name_offset);
2351 // Skip any path that starts with '@' since these are usually:
2352 // @executable_path/.../file
2353 // @rpath/.../file
2354 if (path && path[0] != '@')
2355 {
2356 FileSpec file_spec(path, resolve_path);
2357 if (files.AppendIfUnique(file_spec))
2358 count++;
2359 }
2360 }
2361 break;
2362
2363 default:
2364 break;
2365 }
2366 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00002367 }
Chris Lattner24943d22010-06-08 16:52:24 +00002368 }
2369 return count;
2370}
2371
Jim Ingham28775942011-03-07 23:44:08 +00002372lldb_private::Address
2373ObjectFileMachO::GetEntryPointAddress ()
2374{
2375 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
2376 // is initialized to an invalid address, so we can just return that.
2377 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
2378
2379 if (!IsExecutable() || m_entry_point_address.IsValid())
2380 return m_entry_point_address;
2381
2382 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
2383 // /usr/include/mach-o.h, but it is basically:
2384 //
2385 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
2386 // uint32_t count - this is the count of longs in the thread state data
2387 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
2388 // <repeat this trio>
2389 //
2390 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
2391 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
2392 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
2393 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
2394 //
2395 // For now we hard-code the offsets and flavors we need:
2396 //
2397 //
2398
Greg Clayton9482f052012-03-13 23:14:29 +00002399 ModuleSP module_sp(GetModule());
2400 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00002401 {
Greg Clayton9482f052012-03-13 23:14:29 +00002402 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2403 struct load_command load_cmd;
2404 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2405 uint32_t i;
2406 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
2407 bool done = false;
2408
2409 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00002410 {
Greg Clayton9482f052012-03-13 23:14:29 +00002411 const uint32_t cmd_offset = offset;
2412 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
2413 break;
2414
2415 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00002416 {
Greg Clayton9482f052012-03-13 23:14:29 +00002417 case LoadCommandUnixThread:
2418 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00002419 {
Greg Clayton9482f052012-03-13 23:14:29 +00002420 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00002421 {
Greg Clayton9482f052012-03-13 23:14:29 +00002422 uint32_t flavor = m_data.GetU32(&offset);
2423 uint32_t count = m_data.GetU32(&offset);
2424 if (count == 0)
2425 {
2426 // We've gotten off somehow, log and exit;
2427 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00002428 }
Greg Clayton9482f052012-03-13 23:14:29 +00002429
2430 switch (m_header.cputype)
2431 {
2432 case llvm::MachO::CPUTypeARM:
2433 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
2434 {
2435 offset += 60; // This is the offset of pc in the GPR thread state data structure.
2436 start_address = m_data.GetU32(&offset);
2437 done = true;
2438 }
Jim Ingham28775942011-03-07 23:44:08 +00002439 break;
Greg Clayton9482f052012-03-13 23:14:29 +00002440 case llvm::MachO::CPUTypeI386:
2441 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
2442 {
2443 offset += 40; // This is the offset of eip in the GPR thread state data structure.
2444 start_address = m_data.GetU32(&offset);
2445 done = true;
2446 }
2447 break;
2448 case llvm::MachO::CPUTypeX86_64:
2449 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
2450 {
2451 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
2452 start_address = m_data.GetU64(&offset);
2453 done = true;
2454 }
2455 break;
2456 default:
2457 return m_entry_point_address;
2458 }
2459 // Haven't found the GPR flavor yet, skip over the data for this flavor:
2460 if (done)
2461 break;
2462 offset += count * 4;
2463 }
Jim Ingham28775942011-03-07 23:44:08 +00002464 }
Greg Clayton9482f052012-03-13 23:14:29 +00002465 break;
2466 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00002467 {
Greg Clayton9482f052012-03-13 23:14:29 +00002468 ConstString text_segment_name ("__TEXT");
2469 uint64_t entryoffset = m_data.GetU64(&offset);
2470 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
2471 if (text_segment_sp)
2472 {
2473 done = true;
2474 start_address = text_segment_sp->GetFileAddress() + entryoffset;
2475 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00002476 }
Greg Clayton9482f052012-03-13 23:14:29 +00002477
2478 default:
2479 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00002480 }
Greg Clayton9482f052012-03-13 23:14:29 +00002481 if (done)
2482 break;
Jim Ingham28775942011-03-07 23:44:08 +00002483
Greg Clayton9482f052012-03-13 23:14:29 +00002484 // Go to the next load command:
2485 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00002486 }
Jim Ingham28775942011-03-07 23:44:08 +00002487
Greg Clayton9482f052012-03-13 23:14:29 +00002488 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00002489 {
Greg Clayton9482f052012-03-13 23:14:29 +00002490 // We got the start address from the load commands, so now resolve that address in the sections
2491 // of this ObjectFile:
2492 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00002493 {
Greg Clayton9482f052012-03-13 23:14:29 +00002494 m_entry_point_address.Clear();
2495 }
2496 }
2497 else
2498 {
2499 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
2500 // "start" symbol in the main executable.
2501
2502 ModuleSP module_sp (GetModule());
2503
2504 if (module_sp)
2505 {
2506 SymbolContextList contexts;
2507 SymbolContext context;
2508 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
2509 {
2510 if (contexts.GetContextAtIndex(0, context))
2511 m_entry_point_address = context.symbol->GetAddress();
2512 }
Greg Clayton3508c382012-02-24 01:59:29 +00002513 }
2514 }
Jim Ingham28775942011-03-07 23:44:08 +00002515 }
2516
2517 return m_entry_point_address;
2518
2519}
2520
Greg Claytonb5a8f142012-02-05 02:38:54 +00002521lldb_private::Address
2522ObjectFileMachO::GetHeaderAddress ()
2523{
2524 lldb_private::Address header_addr;
2525 SectionList *section_list = GetSectionList();
2526 if (section_list)
2527 {
2528 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
2529 if (text_segment_sp)
2530 {
Greg Clayton3508c382012-02-24 01:59:29 +00002531 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00002532 header_addr.SetOffset (0);
2533 }
2534 }
2535 return header_addr;
2536}
2537
Greg Clayton46c9a352012-02-09 06:16:32 +00002538uint32_t
2539ObjectFileMachO::GetNumThreadContexts ()
2540{
Greg Clayton9482f052012-03-13 23:14:29 +00002541 ModuleSP module_sp(GetModule());
2542 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00002543 {
Greg Clayton9482f052012-03-13 23:14:29 +00002544 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2545 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00002546 {
Greg Clayton9482f052012-03-13 23:14:29 +00002547 m_thread_context_offsets_valid = true;
2548 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2549 FileRangeArray::Entry file_range;
2550 thread_command thread_cmd;
2551 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00002552 {
Greg Clayton9482f052012-03-13 23:14:29 +00002553 const uint32_t cmd_offset = offset;
2554 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
2555 break;
2556
2557 if (thread_cmd.cmd == LoadCommandThread)
2558 {
2559 file_range.SetRangeBase (offset);
2560 file_range.SetByteSize (thread_cmd.cmdsize - 8);
2561 m_thread_context_offsets.Append (file_range);
2562 }
2563 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00002564 }
Greg Clayton46c9a352012-02-09 06:16:32 +00002565 }
2566 }
2567 return m_thread_context_offsets.GetSize();
2568}
2569
2570lldb::RegisterContextSP
2571ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
2572{
Greg Clayton46c9a352012-02-09 06:16:32 +00002573 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00002574
Greg Clayton9482f052012-03-13 23:14:29 +00002575 ModuleSP module_sp(GetModule());
2576 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00002577 {
Greg Clayton9482f052012-03-13 23:14:29 +00002578 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2579 if (!m_thread_context_offsets_valid)
2580 GetNumThreadContexts ();
2581
2582 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
2583
2584 DataExtractor data (m_data,
2585 thread_context_file_range->GetRangeBase(),
2586 thread_context_file_range->GetByteSize());
2587
2588 switch (m_header.cputype)
2589 {
2590 case llvm::MachO::CPUTypeARM:
2591 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
2592 break;
2593
2594 case llvm::MachO::CPUTypeI386:
2595 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
2596 break;
2597
2598 case llvm::MachO::CPUTypeX86_64:
2599 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
2600 break;
2601 }
Greg Clayton46c9a352012-02-09 06:16:32 +00002602 }
2603 return reg_ctx_sp;
2604}
2605
Greg Claytonb5a8f142012-02-05 02:38:54 +00002606
Greg Claytonca319972011-07-09 00:41:34 +00002607ObjectFile::Type
2608ObjectFileMachO::CalculateType()
2609{
2610 switch (m_header.filetype)
2611 {
2612 case HeaderFileTypeObject: // 0x1u MH_OBJECT
2613 if (GetAddressByteSize () == 4)
2614 {
2615 // 32 bit kexts are just object files, but they do have a valid
2616 // UUID load command.
2617 UUID uuid;
2618 if (GetUUID(&uuid))
2619 {
2620 // this checking for the UUID load command is not enough
2621 // we could eventually look for the symbol named
2622 // "OSKextGetCurrentIdentifier" as this is required of kexts
2623 if (m_strata == eStrataInvalid)
2624 m_strata = eStrataKernel;
2625 return eTypeSharedLibrary;
2626 }
2627 }
2628 return eTypeObjectFile;
2629
2630 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
2631 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
2632 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
2633 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
2634 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
2635 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
2636 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
2637 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
2638 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
2639 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
2640 default:
2641 break;
2642 }
2643 return eTypeUnknown;
2644}
2645
2646ObjectFile::Strata
2647ObjectFileMachO::CalculateStrata()
2648{
2649 switch (m_header.filetype)
2650 {
2651 case HeaderFileTypeObject: // 0x1u MH_OBJECT
2652 {
2653 // 32 bit kexts are just object files, but they do have a valid
2654 // UUID load command.
2655 UUID uuid;
2656 if (GetUUID(&uuid))
2657 {
2658 // this checking for the UUID load command is not enough
2659 // we could eventually look for the symbol named
2660 // "OSKextGetCurrentIdentifier" as this is required of kexts
2661 if (m_type == eTypeInvalid)
2662 m_type = eTypeSharedLibrary;
2663
2664 return eStrataKernel;
2665 }
2666 }
2667 return eStrataUnknown;
2668
2669 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
2670 // Check for the MH_DYLDLINK bit in the flags
2671 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00002672 {
Greg Claytonca319972011-07-09 00:41:34 +00002673 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00002674 }
2675 else
2676 {
2677 SectionList *section_list = GetSectionList();
2678 if (section_list)
2679 {
2680 static ConstString g_kld_section_name ("__KLD");
2681 if (section_list->FindSectionByName(g_kld_section_name))
2682 return eStrataKernel;
2683 }
2684 }
2685 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00002686
2687 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
2688 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00002689 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00002690 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
2691 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
2692 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
2693 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
2694 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
2695 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
2696 default:
2697 break;
2698 }
2699 return eStrataUnknown;
2700}
2701
2702
Greg Clayton49f4bf22012-02-22 19:41:02 +00002703uint32_t
2704ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
2705{
Greg Clayton9482f052012-03-13 23:14:29 +00002706 ModuleSP module_sp(GetModule());
2707 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00002708 {
Greg Clayton9482f052012-03-13 23:14:29 +00002709 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2710 struct dylib_command load_cmd;
2711 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2712 uint32_t version_cmd = 0;
2713 uint64_t version = 0;
2714 uint32_t i;
2715 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00002716 {
Greg Clayton9482f052012-03-13 23:14:29 +00002717 const uint32_t cmd_offset = offset;
2718 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
2719 break;
2720
2721 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00002722 {
Greg Clayton9482f052012-03-13 23:14:29 +00002723 if (version_cmd == 0)
2724 {
2725 version_cmd = load_cmd.cmd;
2726 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
2727 break;
2728 version = load_cmd.dylib.current_version;
2729 }
2730 break; // Break for now unless there is another more complete version
2731 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00002732 }
Greg Clayton9482f052012-03-13 23:14:29 +00002733 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00002734 }
Greg Clayton9482f052012-03-13 23:14:29 +00002735
2736 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00002737 {
Greg Clayton9482f052012-03-13 23:14:29 +00002738 if (versions != NULL && num_versions > 0)
2739 {
2740 if (num_versions > 0)
2741 versions[0] = (version & 0xFFFF0000ull) >> 16;
2742 if (num_versions > 1)
2743 versions[1] = (version & 0x0000FF00ull) >> 8;
2744 if (num_versions > 2)
2745 versions[2] = (version & 0x000000FFull);
2746 // Fill in an remaining version numbers with invalid values
2747 for (i=3; i<num_versions; ++i)
2748 versions[i] = UINT32_MAX;
2749 }
2750 // The LC_ID_DYLIB load command has a version with 3 version numbers
2751 // in it, so always return 3
2752 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00002753 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00002754 }
2755 return false;
2756}
2757
Chris Lattner24943d22010-06-08 16:52:24 +00002758bool
Greg Clayton395fc332011-02-15 21:59:32 +00002759ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00002760{
Greg Clayton9482f052012-03-13 23:14:29 +00002761 ModuleSP module_sp(GetModule());
2762 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00002763 {
Greg Clayton9482f052012-03-13 23:14:29 +00002764 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2765 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
2766
2767 // Files with type MH_PRELOAD are currently used in cases where the image
2768 // debugs at the addresses in the file itself. Below we set the OS to
2769 // unknown to make sure we use the DynamicLoaderStatic()...
2770 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
2771 {
2772 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
2773 }
2774 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00002775 }
Greg Clayton9482f052012-03-13 23:14:29 +00002776 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00002777}
2778
2779
2780//------------------------------------------------------------------
2781// PluginInterface protocol
2782//------------------------------------------------------------------
2783const char *
2784ObjectFileMachO::GetPluginName()
2785{
2786 return "ObjectFileMachO";
2787}
2788
2789const char *
2790ObjectFileMachO::GetShortPluginName()
2791{
2792 return GetPluginNameStatic();
2793}
2794
2795uint32_t
2796ObjectFileMachO::GetPluginVersion()
2797{
2798 return 1;
2799}
2800