blob: 418a93ccda61e0ec0c88a3c42aff03ac9ed97f07 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ObjectFileMachO.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Greg Clayton3f69eac2011-12-03 02:30:59 +000010#include "llvm/ADT/StringRef.h"
Jim Ingham28775942011-03-07 23:44:08 +000011#include "llvm/Support/MachO.h"
12
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "ObjectFileMachO.h"
14
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Core/ArchSpec.h"
16#include "lldb/Core/DataBuffer.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/FileSpecList.h"
18#include "lldb/Core/Module.h"
19#include "lldb/Core/PluginManager.h"
Greg Clayton6f7f8da2012-04-24 03:06:13 +000020#include "lldb/Core/RangeMap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Section.h"
22#include "lldb/Core/StreamFile.h"
23#include "lldb/Core/StreamString.h"
24#include "lldb/Core/Timer.h"
25#include "lldb/Core/UUID.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000026#include "lldb/Host/Host.h"
27#include "lldb/Host/FileSpec.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000028#include "lldb/Symbol/ClangNamespaceDecl.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Symbol/ObjectFile.h"
Greg Clayton29021d32012-04-18 05:19:20 +000030#include "lldb/Target/Platform.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000031#include "lldb/Target/Process.h"
Greg Clayton29021d32012-04-18 05:19:20 +000032#include "lldb/Target/Target.h"
Greg Clayton9ce95382012-02-13 23:10:39 +000033#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
34#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Clayton46c9a352012-02-09 06:16:32 +000035#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner24943d22010-06-08 16:52:24 +000036
Chris Lattner24943d22010-06-08 16:52:24 +000037
38using namespace lldb;
39using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000040using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000041
Greg Clayton46c9a352012-02-09 06:16:32 +000042class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
43{
44public:
45 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
46 RegisterContextDarwin_x86_64 (thread, 0)
47 {
48 SetRegisterDataFrom_LC_THREAD (data);
49 }
50
51 virtual void
52 InvalidateAllRegisters ()
53 {
54 // Do nothing... registers are always valid...
55 }
56
57 void
58 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
59 {
Greg Clayton46c9a352012-02-09 06:16:32 +000060 uint32_t offset = 0;
61 SetError (GPRRegSet, Read, -1);
62 SetError (FPURegSet, Read, -1);
63 SetError (EXCRegSet, Read, -1);
Greg Clayton9ce95382012-02-13 23:10:39 +000064 bool done = false;
65
66 while (!done)
Greg Clayton46c9a352012-02-09 06:16:32 +000067 {
Greg Clayton9ce95382012-02-13 23:10:39 +000068 int flavor = data.GetU32 (&offset);
69 if (flavor == 0)
70 done = true;
71 else
Greg Clayton46c9a352012-02-09 06:16:32 +000072 {
Greg Clayton9ce95382012-02-13 23:10:39 +000073 uint32_t i;
74 uint32_t count = data.GetU32 (&offset);
75 switch (flavor)
76 {
77 case GPRRegSet:
78 for (i=0; i<count; ++i)
79 (&gpr.rax)[i] = data.GetU64(&offset);
80 SetError (GPRRegSet, Read, 0);
81 done = true;
82
83 break;
84 case FPURegSet:
85 // TODO: fill in FPU regs....
86 //SetError (FPURegSet, Read, -1);
87 done = true;
88
89 break;
90 case EXCRegSet:
91 exc.trapno = data.GetU32(&offset);
92 exc.err = data.GetU32(&offset);
93 exc.faultvaddr = data.GetU64(&offset);
94 SetError (EXCRegSet, Read, 0);
95 done = true;
96 break;
97 case 7:
98 case 8:
99 case 9:
100 // fancy flavors that encapsulate of the the above
101 // falvors...
102 break;
103
104 default:
105 done = true;
106 break;
107 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000108 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000109 }
110 }
111protected:
112 virtual int
113 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
114 {
115 return 0;
116 }
117
118 virtual int
119 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
120 {
121 return 0;
122 }
123
124 virtual int
125 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
126 {
127 return 0;
128 }
129
130 virtual int
131 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
132 {
133 return 0;
134 }
135
136 virtual int
137 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
138 {
139 return 0;
140 }
141
142 virtual int
143 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
144 {
145 return 0;
146 }
147};
Greg Clayton46c9a352012-02-09 06:16:32 +0000148
Greg Clayton9ce95382012-02-13 23:10:39 +0000149
150class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
151{
152public:
153 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
154 RegisterContextDarwin_i386 (thread, 0)
155 {
156 SetRegisterDataFrom_LC_THREAD (data);
157 }
158
159 virtual void
160 InvalidateAllRegisters ()
161 {
162 // Do nothing... registers are always valid...
163 }
164
165 void
166 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
167 {
168 uint32_t offset = 0;
169 SetError (GPRRegSet, Read, -1);
170 SetError (FPURegSet, Read, -1);
171 SetError (EXCRegSet, Read, -1);
172 bool done = false;
173
174 while (!done)
175 {
176 int flavor = data.GetU32 (&offset);
177 if (flavor == 0)
178 done = true;
179 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000180 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000181 uint32_t i;
182 uint32_t count = data.GetU32 (&offset);
183 switch (flavor)
184 {
185 case GPRRegSet:
186 for (i=0; i<count; ++i)
187 (&gpr.eax)[i] = data.GetU32(&offset);
188 SetError (GPRRegSet, Read, 0);
189 done = true;
190
191 break;
192 case FPURegSet:
193 // TODO: fill in FPU regs....
194 //SetError (FPURegSet, Read, -1);
195 done = true;
196
197 break;
198 case EXCRegSet:
199 exc.trapno = data.GetU32(&offset);
200 exc.err = data.GetU32(&offset);
201 exc.faultvaddr = data.GetU32(&offset);
202 SetError (EXCRegSet, Read, 0);
203 done = true;
204 break;
205 case 7:
206 case 8:
207 case 9:
208 // fancy flavors that encapsulate of the the above
209 // falvors...
210 break;
211
212 default:
213 done = true;
214 break;
215 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000216 }
217 }
218 }
219protected:
220 virtual int
221 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
222 {
223 return 0;
224 }
225
226 virtual int
227 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
228 {
229 return 0;
230 }
231
232 virtual int
233 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
234 {
235 return 0;
236 }
237
238 virtual int
239 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
240 {
241 return 0;
242 }
243
244 virtual int
245 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
246 {
247 return 0;
248 }
249
250 virtual int
251 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
252 {
253 return 0;
254 }
255};
256
Greg Clayton9ce95382012-02-13 23:10:39 +0000257class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
258{
259public:
260 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
261 RegisterContextDarwin_arm (thread, 0)
262 {
263 SetRegisterDataFrom_LC_THREAD (data);
264 }
265
266 virtual void
267 InvalidateAllRegisters ()
268 {
269 // Do nothing... registers are always valid...
270 }
271
272 void
273 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
274 {
275 uint32_t offset = 0;
276 SetError (GPRRegSet, Read, -1);
277 SetError (FPURegSet, Read, -1);
278 SetError (EXCRegSet, Read, -1);
279 int flavor = data.GetU32 (&offset);
280 uint32_t count = data.GetU32 (&offset);
281 switch (flavor)
282 {
283 case GPRRegSet:
284 for (uint32_t i=0; i<count; ++i)
285 gpr.r[i] = data.GetU32(&offset);
286 SetError (GPRRegSet, Read, 0);
287 break;
288 case FPURegSet:
289 // TODO: fill in FPU regs....
290 //SetError (FPURegSet, Read, -1);
291 break;
292 case EXCRegSet:
293 exc.exception = data.GetU32(&offset);
294 exc.fsr = data.GetU32(&offset);
295 exc.far = data.GetU32(&offset);
296 SetError (EXCRegSet, Read, 0);
297 break;
298 }
299 }
300protected:
301 virtual int
302 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
303 {
304 return 0;
305 }
306
307 virtual int
308 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
309 {
310 return 0;
311 }
312
313 virtual int
314 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
315 {
316 return 0;
317 }
318
319 virtual int
320 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
321 {
322 return 0;
323 }
324
325 virtual int
326 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
327 {
328 return 0;
329 }
330
331 virtual int
332 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
333 {
334 return 0;
335 }
336};
337
Greg Claytonb1888f22011-03-19 01:12:21 +0000338#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000339
340void
341ObjectFileMachO::Initialize()
342{
343 PluginManager::RegisterPlugin (GetPluginNameStatic(),
344 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000345 CreateInstance,
346 CreateMemoryInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000347}
348
349void
350ObjectFileMachO::Terminate()
351{
352 PluginManager::UnregisterPlugin (CreateInstance);
353}
354
355
356const char *
357ObjectFileMachO::GetPluginNameStatic()
358{
359 return "object-file.mach-o";
360}
361
362const char *
363ObjectFileMachO::GetPluginDescriptionStatic()
364{
365 return "Mach-o object file reader (32 and 64 bit)";
366}
367
368
369ObjectFile *
Greg Clayton3508c382012-02-24 01:59:29 +0000370ObjectFileMachO::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 +0000371{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000372 if (ObjectFileMachO::MagicBytesMatch(data_sp, offset, length))
Chris Lattner24943d22010-06-08 16:52:24 +0000373 {
Greg Clayton3508c382012-02-24 01:59:29 +0000374 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, file, offset, length));
Chris Lattner24943d22010-06-08 16:52:24 +0000375 if (objfile_ap.get() && objfile_ap->ParseHeader())
376 return objfile_ap.release();
377 }
378 return NULL;
379}
380
Greg Claytonb5a8f142012-02-05 02:38:54 +0000381ObjectFile *
Greg Clayton3508c382012-02-24 01:59:29 +0000382ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000383 DataBufferSP& data_sp,
384 const ProcessSP &process_sp,
385 lldb::addr_t header_addr)
386{
387 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
388 {
Greg Clayton3508c382012-02-24 01:59:29 +0000389 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000390 if (objfile_ap.get() && objfile_ap->ParseHeader())
391 return objfile_ap.release();
392 }
393 return NULL;
394}
395
396
397const ConstString &
398ObjectFileMachO::GetSegmentNameTEXT()
399{
400 static ConstString g_segment_name_TEXT ("__TEXT");
401 return g_segment_name_TEXT;
402}
403
404const ConstString &
405ObjectFileMachO::GetSegmentNameDATA()
406{
407 static ConstString g_segment_name_DATA ("__DATA");
408 return g_segment_name_DATA;
409}
410
411const ConstString &
412ObjectFileMachO::GetSegmentNameOBJC()
413{
414 static ConstString g_segment_name_OBJC ("__OBJC");
415 return g_segment_name_OBJC;
416}
417
418const ConstString &
419ObjectFileMachO::GetSegmentNameLINKEDIT()
420{
421 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
422 return g_section_name_LINKEDIT;
423}
424
425const ConstString &
426ObjectFileMachO::GetSectionNameEHFrame()
427{
428 static ConstString g_section_name_eh_frame ("__eh_frame");
429 return g_section_name_eh_frame;
430}
431
432
Chris Lattner24943d22010-06-08 16:52:24 +0000433
434static uint32_t
435MachHeaderSizeFromMagic(uint32_t magic)
436{
437 switch (magic)
438 {
Greg Clayton1674b122010-07-21 22:12:05 +0000439 case HeaderMagic32:
440 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000441 return sizeof(struct mach_header);
442
Greg Clayton1674b122010-07-21 22:12:05 +0000443 case HeaderMagic64:
444 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000445 return sizeof(struct mach_header_64);
446 break;
447
448 default:
449 break;
450 }
451 return 0;
452}
453
454
455bool
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000456ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
457 lldb::addr_t data_offset,
458 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000459{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000460 DataExtractor data;
461 data.SetData (data_sp, data_offset, data_length);
Chris Lattner24943d22010-06-08 16:52:24 +0000462 uint32_t offset = 0;
463 uint32_t magic = data.GetU32(&offset);
464 return MachHeaderSizeFromMagic(magic) != 0;
465}
466
467
Greg Clayton3508c382012-02-24 01:59:29 +0000468ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp, DataBufferSP& data_sp, const FileSpec* file, addr_t offset, addr_t length) :
469 ObjectFile(module_sp, file, offset, length, data_sp),
Chris Lattner24943d22010-06-08 16:52:24 +0000470 m_sections_ap(),
Jim Ingham28775942011-03-07 23:44:08 +0000471 m_symtab_ap(),
Greg Clayton46c9a352012-02-09 06:16:32 +0000472 m_mach_segments(),
473 m_mach_sections(),
474 m_entry_point_address(),
475 m_thread_context_offsets(),
476 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000477{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000478 ::memset (&m_header, 0, sizeof(m_header));
479 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000480}
481
Greg Clayton3508c382012-02-24 01:59:29 +0000482ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000483 lldb::DataBufferSP& header_data_sp,
484 const lldb::ProcessSP &process_sp,
485 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000486 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000487 m_sections_ap(),
488 m_symtab_ap(),
Greg Clayton46c9a352012-02-09 06:16:32 +0000489 m_mach_segments(),
490 m_mach_sections(),
491 m_entry_point_address(),
492 m_thread_context_offsets(),
493 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000494{
495 ::memset (&m_header, 0, sizeof(m_header));
496 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
497}
Chris Lattner24943d22010-06-08 16:52:24 +0000498
499ObjectFileMachO::~ObjectFileMachO()
500{
501}
502
503
504bool
505ObjectFileMachO::ParseHeader ()
506{
Greg Clayton9482f052012-03-13 23:14:29 +0000507 ModuleSP module_sp(GetModule());
508 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000509 {
Greg Clayton9482f052012-03-13 23:14:29 +0000510 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
511 bool can_parse = false;
512 uint32_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000513 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000514 // Leave magic in the original byte order
515 m_header.magic = m_data.GetU32(&offset);
516 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000517 {
Greg Clayton9482f052012-03-13 23:14:29 +0000518 case HeaderMagic32:
519 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
520 m_data.SetAddressByteSize(4);
521 can_parse = true;
522 break;
523
524 case HeaderMagic64:
525 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
526 m_data.SetAddressByteSize(8);
527 can_parse = true;
528 break;
529
530 case HeaderMagic32Swapped:
531 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
532 m_data.SetAddressByteSize(4);
533 can_parse = true;
534 break;
535
536 case HeaderMagic64Swapped:
537 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
538 m_data.SetAddressByteSize(8);
539 can_parse = true;
540 break;
541
542 default:
543 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000544 }
Greg Clayton9482f052012-03-13 23:14:29 +0000545
546 if (can_parse)
547 {
548 m_data.GetU32(&offset, &m_header.cputype, 6);
549
550 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
551
552 if (SetModulesArchitecture (mach_arch))
553 {
554 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
555 if (m_data.GetByteSize() < header_and_lc_size)
556 {
557 DataBufferSP data_sp;
558 ProcessSP process_sp (m_process_wp.lock());
559 if (process_sp)
560 {
561 data_sp = ReadMemory (process_sp, m_offset, header_and_lc_size);
562 }
563 else
564 {
565 // Read in all only the load command data from the file on disk
566 data_sp = m_file.ReadFileContents(m_offset, header_and_lc_size);
567 if (data_sp->GetByteSize() != header_and_lc_size)
568 return false;
569 }
570 if (data_sp)
571 m_data.SetData (data_sp);
572 }
573 }
574 return true;
575 }
576 else
577 {
578 memset(&m_header, 0, sizeof(struct mach_header));
579 }
Chris Lattner24943d22010-06-08 16:52:24 +0000580 }
581 return false;
582}
583
584
585ByteOrder
586ObjectFileMachO::GetByteOrder () const
587{
Chris Lattner24943d22010-06-08 16:52:24 +0000588 return m_data.GetByteOrder ();
589}
590
Jim Ingham7508e732010-08-09 23:31:02 +0000591bool
592ObjectFileMachO::IsExecutable() const
593{
594 return m_header.filetype == HeaderFileTypeExecutable;
595}
Chris Lattner24943d22010-06-08 16:52:24 +0000596
597size_t
598ObjectFileMachO::GetAddressByteSize () const
599{
Chris Lattner24943d22010-06-08 16:52:24 +0000600 return m_data.GetAddressByteSize ();
601}
602
Greg Claytonb3448432011-03-24 21:19:54 +0000603AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000604ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
605{
606 Symtab *symtab = GetSymtab();
607 if (symtab)
608 {
609 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
610 if (symbol)
611 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000612 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000613 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000614 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000615 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000616 {
Greg Clayton3508c382012-02-24 01:59:29 +0000617 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000618 switch (section_type)
619 {
620 case eSectionTypeInvalid: return eAddressClassUnknown;
621 case eSectionTypeCode:
622 if (m_header.cputype == llvm::MachO::CPUTypeARM)
623 {
624 // For ARM we have a bit in the n_desc field of the symbol
625 // that tells us ARM/Thumb which is bit 0x0008.
626 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
627 return eAddressClassCodeAlternateISA;
628 }
629 return eAddressClassCode;
630
631 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000632 case eSectionTypeData:
633 case eSectionTypeDataCString:
634 case eSectionTypeDataCStringPointers:
635 case eSectionTypeDataSymbolAddress:
636 case eSectionTypeData4:
637 case eSectionTypeData8:
638 case eSectionTypeData16:
639 case eSectionTypeDataPointers:
640 case eSectionTypeZeroFill:
641 case eSectionTypeDataObjCMessageRefs:
642 case eSectionTypeDataObjCCFStrings:
643 return eAddressClassData;
644 case eSectionTypeDebug:
645 case eSectionTypeDWARFDebugAbbrev:
646 case eSectionTypeDWARFDebugAranges:
647 case eSectionTypeDWARFDebugFrame:
648 case eSectionTypeDWARFDebugInfo:
649 case eSectionTypeDWARFDebugLine:
650 case eSectionTypeDWARFDebugLoc:
651 case eSectionTypeDWARFDebugMacInfo:
652 case eSectionTypeDWARFDebugPubNames:
653 case eSectionTypeDWARFDebugPubTypes:
654 case eSectionTypeDWARFDebugRanges:
655 case eSectionTypeDWARFDebugStr:
656 case eSectionTypeDWARFAppleNames:
657 case eSectionTypeDWARFAppleTypes:
658 case eSectionTypeDWARFAppleNamespaces:
659 case eSectionTypeDWARFAppleObjC:
660 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000661 case eSectionTypeEHFrame: return eAddressClassRuntime;
662 case eSectionTypeOther: return eAddressClassUnknown;
663 }
664 }
665 }
666
Greg Claytonb3448432011-03-24 21:19:54 +0000667 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000668 switch (symbol_type)
669 {
670 case eSymbolTypeAny: return eAddressClassUnknown;
671 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Greg Claytonb1888f22011-03-19 01:12:21 +0000672
673 case eSymbolTypeCode:
674 case eSymbolTypeTrampoline:
675 if (m_header.cputype == llvm::MachO::CPUTypeARM)
676 {
677 // For ARM we have a bit in the n_desc field of the symbol
678 // that tells us ARM/Thumb which is bit 0x0008.
679 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
680 return eAddressClassCodeAlternateISA;
681 }
682 return eAddressClassCode;
683
684 case eSymbolTypeData: return eAddressClassData;
685 case eSymbolTypeRuntime: return eAddressClassRuntime;
686 case eSymbolTypeException: return eAddressClassRuntime;
687 case eSymbolTypeSourceFile: return eAddressClassDebug;
688 case eSymbolTypeHeaderFile: return eAddressClassDebug;
689 case eSymbolTypeObjectFile: return eAddressClassDebug;
690 case eSymbolTypeCommonBlock: return eAddressClassDebug;
691 case eSymbolTypeBlock: return eAddressClassDebug;
692 case eSymbolTypeLocal: return eAddressClassData;
693 case eSymbolTypeParam: return eAddressClassData;
694 case eSymbolTypeVariable: return eAddressClassData;
695 case eSymbolTypeVariableType: return eAddressClassDebug;
696 case eSymbolTypeLineEntry: return eAddressClassDebug;
697 case eSymbolTypeLineHeader: return eAddressClassDebug;
698 case eSymbolTypeScopeBegin: return eAddressClassDebug;
699 case eSymbolTypeScopeEnd: return eAddressClassDebug;
700 case eSymbolTypeAdditional: return eAddressClassUnknown;
701 case eSymbolTypeCompiler: return eAddressClassDebug;
702 case eSymbolTypeInstrumentation:return eAddressClassDebug;
703 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000704 case eSymbolTypeObjCClass: return eAddressClassRuntime;
705 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
706 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000707 }
708 }
709 }
710 return eAddressClassUnknown;
711}
Chris Lattner24943d22010-06-08 16:52:24 +0000712
713Symtab *
714ObjectFileMachO::GetSymtab()
715{
Greg Clayton9482f052012-03-13 23:14:29 +0000716 ModuleSP module_sp(GetModule());
717 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000718 {
Greg Clayton9482f052012-03-13 23:14:29 +0000719 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
720 if (m_symtab_ap.get() == NULL)
721 {
722 m_symtab_ap.reset(new Symtab(this));
723 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
724 ParseSymtab (true);
725 m_symtab_ap->Finalize ();
726 }
Chris Lattner24943d22010-06-08 16:52:24 +0000727 }
728 return m_symtab_ap.get();
729}
730
731
732SectionList *
733ObjectFileMachO::GetSectionList()
734{
Greg Clayton9482f052012-03-13 23:14:29 +0000735 ModuleSP module_sp(GetModule());
736 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000737 {
Greg Clayton9482f052012-03-13 23:14:29 +0000738 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
739 if (m_sections_ap.get() == NULL)
740 {
741 m_sections_ap.reset(new SectionList());
742 ParseSections();
743 }
Chris Lattner24943d22010-06-08 16:52:24 +0000744 }
745 return m_sections_ap.get();
746}
747
748
749size_t
750ObjectFileMachO::ParseSections ()
751{
752 lldb::user_id_t segID = 0;
753 lldb::user_id_t sectID = 0;
754 struct segment_command_64 load_cmd;
755 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
756 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000757 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000758 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000759 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000760 // First look up any LC_ENCRYPTION_INFO load commands
761 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
762 EncryptedFileRanges encrypted_file_ranges;
763 for (i=0; i<m_header.ncmds; ++i)
764 {
765 const uint32_t load_cmd_offset = offset;
766 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
767 break;
768
769 if (load_cmd.cmd == LoadCommandEncryptionInfo)
770 {
771 EncryptedFileRanges::Entry entry;
772 entry.SetRangeBase(m_data.GetU32(&offset));
773 entry.SetByteSize(m_data.GetU32(&offset));
774 encrypted_file_ranges.Append(entry);
775 }
776 offset = load_cmd_offset + load_cmd.cmdsize;
777 }
778
779 offset = MachHeaderSizeFromMagic(m_header.magic);
780
Chris Lattner24943d22010-06-08 16:52:24 +0000781 for (i=0; i<m_header.ncmds; ++i)
782 {
783 const uint32_t load_cmd_offset = offset;
784 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
785 break;
786
Greg Clayton1674b122010-07-21 22:12:05 +0000787 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000788 {
789 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
790 {
791 load_cmd.vmaddr = m_data.GetAddress(&offset);
792 load_cmd.vmsize = m_data.GetAddress(&offset);
793 load_cmd.fileoff = m_data.GetAddress(&offset);
794 load_cmd.filesize = m_data.GetAddress(&offset);
795 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
796 {
Greg Clayton68ca8232011-01-25 02:58:48 +0000797
798 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
799
Chris Lattner24943d22010-06-08 16:52:24 +0000800 // Keep a list of mach segments around in case we need to
801 // get at data that isn't stored in the abstracted Sections.
802 m_mach_segments.push_back (load_cmd);
803
804 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
805 // Use a segment ID of the segment index shifted left by 8 so they
806 // never conflict with any of the sections.
807 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000808 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000809 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000810 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000811 ++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
812 segment_name, // Name of this section
813 eSectionTypeContainer, // This section is a container of other sections.
814 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
815 load_cmd.vmsize, // VM size in bytes of this section
816 load_cmd.fileoff, // Offset to the data for this section in the file
817 load_cmd.filesize, // Size in bytes of this section as found in the the file
818 load_cmd.flags)); // Flags for this section
819
Greg Clayton68ca8232011-01-25 02:58:48 +0000820 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000821 m_sections_ap->AddSection(segment_sp);
822 }
823
824 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000825 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000826 // Push a section into our mach sections for the section at
Greg Clayton6af4fad2010-10-06 01:26:32 +0000827 // index zero (NListSectionNoSection) if we don't have any
828 // mach sections yet...
829 if (m_mach_sections.empty())
830 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000831 uint32_t segment_sect_idx;
832 const lldb::user_id_t first_segment_sectID = sectID + 1;
833
834
Greg Clayton1674b122010-07-21 22:12:05 +0000835 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000836 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
837 {
838 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
839 break;
840 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
841 break;
842 sect64.addr = m_data.GetAddress(&offset);
843 sect64.size = m_data.GetAddress(&offset);
844
845 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
846 break;
847
848 // Keep a list of mach sections around in case we need to
849 // get at data that isn't stored in the abstracted Sections.
850 m_mach_sections.push_back (sect64);
851
852 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
853 if (!segment_name)
854 {
855 // We have a segment with no name so we need to conjure up
856 // segments that correspond to the section's segname if there
857 // isn't already such a section. If there is such a section,
858 // we resize the section so that it spans all sections.
859 // We also mark these sections as fake so address matches don't
860 // hit if they land in the gaps between the child sections.
861 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
862 segment_sp = m_sections_ap->FindSectionByName (segment_name);
863 if (segment_sp.get())
864 {
865 Section *segment = segment_sp.get();
866 // Grow the section size as needed.
867 const lldb::addr_t sect64_min_addr = sect64.addr;
868 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
869 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
870 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
871 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
872 if (sect64_min_addr >= curr_seg_min_addr)
873 {
874 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
875 // Only grow the section size if needed
876 if (new_seg_byte_size > curr_seg_byte_size)
877 segment->SetByteSize (new_seg_byte_size);
878 }
879 else
880 {
881 // We need to change the base address of the segment and
882 // adjust the child section offsets for all existing children.
883 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
884 segment->Slide(slide_amount, false);
885 segment->GetChildren().Slide (-slide_amount, false);
886 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
887 }
Greg Clayton661825b2010-06-28 23:51:11 +0000888
889 // Grow the section size as needed.
890 if (sect64.offset)
891 {
892 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
893 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
894
895 const lldb::addr_t section_min_file_offset = sect64.offset;
896 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
897 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
898 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
899 segment->SetFileOffset (new_file_offset);
900 segment->SetFileSize (new_file_size);
901 }
Chris Lattner24943d22010-06-08 16:52:24 +0000902 }
903 else
904 {
905 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +0000906 segment_sp.reset(new Section (segment_sp, // Parent section
907 module_sp, // Module to which this section belongs
908 ++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
909 segment_name, // Name of this section
910 eSectionTypeContainer, // This section is a container of other sections.
911 sect64.addr, // File VM address == addresses as they are found in the object file
912 sect64.size, // VM size in bytes of this section
913 sect64.offset, // Offset to the data for this section in the file
914 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
915 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +0000916 segment_sp->SetIsFake(true);
917 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000918 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000919 }
920 }
921 assert (segment_sp.get());
922
Greg Clayton1674b122010-07-21 22:12:05 +0000923 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000924 static ConstString g_sect_name_objc_data ("__objc_data");
925 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
926 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
927 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
928 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
929 static ConstString g_sect_name_objc_const ("__objc_const");
930 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
931 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000932
933 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
934 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
935 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
936 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
937 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
938 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
939 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
940 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
941 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
942 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
943 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +0000944 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
945 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +0000946 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +0000947 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000948 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +0000949 static ConstString g_sect_name_DATA ("__DATA");
950 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000951
Chris Lattner24943d22010-06-08 16:52:24 +0000952 SectionType sect_type = eSectionTypeOther;
953
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000954 if (section_name == g_sect_name_dwarf_debug_abbrev)
955 sect_type = eSectionTypeDWARFDebugAbbrev;
956 else if (section_name == g_sect_name_dwarf_debug_aranges)
957 sect_type = eSectionTypeDWARFDebugAranges;
958 else if (section_name == g_sect_name_dwarf_debug_frame)
959 sect_type = eSectionTypeDWARFDebugFrame;
960 else if (section_name == g_sect_name_dwarf_debug_info)
961 sect_type = eSectionTypeDWARFDebugInfo;
962 else if (section_name == g_sect_name_dwarf_debug_line)
963 sect_type = eSectionTypeDWARFDebugLine;
964 else if (section_name == g_sect_name_dwarf_debug_loc)
965 sect_type = eSectionTypeDWARFDebugLoc;
966 else if (section_name == g_sect_name_dwarf_debug_macinfo)
967 sect_type = eSectionTypeDWARFDebugMacInfo;
968 else if (section_name == g_sect_name_dwarf_debug_pubnames)
969 sect_type = eSectionTypeDWARFDebugPubNames;
970 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
971 sect_type = eSectionTypeDWARFDebugPubTypes;
972 else if (section_name == g_sect_name_dwarf_debug_ranges)
973 sect_type = eSectionTypeDWARFDebugRanges;
974 else if (section_name == g_sect_name_dwarf_debug_str)
975 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +0000976 else if (section_name == g_sect_name_dwarf_apple_names)
977 sect_type = eSectionTypeDWARFAppleNames;
978 else if (section_name == g_sect_name_dwarf_apple_types)
979 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +0000980 else if (section_name == g_sect_name_dwarf_apple_namespaces)
981 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000982 else if (section_name == g_sect_name_dwarf_apple_objc)
983 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000984 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +0000985 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +0000986 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +0000987 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000988 else if (section_name == g_sect_name_eh_frame)
989 sect_type = eSectionTypeEHFrame;
990 else if (section_name == g_sect_name_cfstring)
991 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +0000992 else if (section_name == g_sect_name_objc_data ||
993 section_name == g_sect_name_objc_classrefs ||
994 section_name == g_sect_name_objc_superrefs ||
995 section_name == g_sect_name_objc_const ||
996 section_name == g_sect_name_objc_classlist)
997 {
998 sect_type = eSectionTypeDataPointers;
999 }
Chris Lattner24943d22010-06-08 16:52:24 +00001000
1001 if (sect_type == eSectionTypeOther)
1002 {
1003 switch (mach_sect_type)
1004 {
1005 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001006 case SectionTypeRegular:
1007 if (segment_sp->GetName() == g_sect_name_TEXT)
1008 sect_type = eSectionTypeCode;
1009 else if (segment_sp->GetName() == g_sect_name_DATA)
1010 sect_type = eSectionTypeData;
1011 else
1012 sect_type = eSectionTypeOther;
1013 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001014 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1015 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1016 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1017 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1018 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1019 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1020 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1021 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1022 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1023 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1024 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1025 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1026 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1027 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1028 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1029 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001030 default: break;
1031 }
1032 }
1033
Greg Clayton3508c382012-02-24 01:59:29 +00001034 SectionSP section_sp(new Section (segment_sp,
1035 module_sp,
1036 ++sectID,
1037 section_name,
1038 sect_type,
1039 sect64.addr - segment_sp->GetFileAddress(),
1040 sect64.size,
1041 sect64.offset,
1042 sect64.offset == 0 ? 0 : sect64.size,
1043 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001044 // Set the section to be encrypted to match the segment
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001045
1046 bool section_is_encrypted = false;
1047 if (!segment_is_encrypted && load_cmd.filesize != 0)
1048 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001049
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001050 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001051 segment_sp->GetChildren().AddSection(section_sp);
1052
1053 if (segment_sp->IsFake())
1054 {
1055 segment_sp.reset();
1056 segment_name.Clear();
1057 }
1058 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001059 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001060 {
1061 if (first_segment_sectID <= sectID)
1062 {
1063 lldb::user_id_t sect_uid;
1064 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1065 {
1066 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1067 SectionSP next_section_sp;
1068 if (sect_uid + 1 <= sectID)
1069 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1070
1071 if (curr_section_sp.get())
1072 {
1073 if (curr_section_sp->GetByteSize() == 0)
1074 {
1075 if (next_section_sp.get() != NULL)
1076 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1077 else
1078 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1079 }
1080 }
1081 }
1082 }
1083 }
1084 }
1085 }
1086 }
Greg Clayton1674b122010-07-21 22:12:05 +00001087 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001088 {
1089 m_dysymtab.cmd = load_cmd.cmd;
1090 m_dysymtab.cmdsize = load_cmd.cmdsize;
1091 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1092 }
1093
1094 offset = load_cmd_offset + load_cmd.cmdsize;
1095 }
1096// if (dump_sections)
1097// {
1098// StreamFile s(stdout);
1099// m_sections_ap->Dump(&s, true);
1100// }
1101 return sectID; // Return the number of sections we registered with the module
1102}
1103
1104class MachSymtabSectionInfo
1105{
1106public:
1107
1108 MachSymtabSectionInfo (SectionList *section_list) :
1109 m_section_list (section_list),
1110 m_section_infos()
1111 {
1112 // Get the number of sections down to a depth of 1 to include
1113 // all segments and their sections, but no other sections that
1114 // may be added for debug map or
1115 m_section_infos.resize(section_list->GetNumSections(1));
1116 }
1117
1118
Greg Clayton3508c382012-02-24 01:59:29 +00001119 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001120 GetSection (uint8_t n_sect, addr_t file_addr)
1121 {
1122 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001123 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001124 if (n_sect < m_section_infos.size())
1125 {
Greg Clayton3508c382012-02-24 01:59:29 +00001126 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001127 {
Greg Clayton3508c382012-02-24 01:59:29 +00001128 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1129 m_section_infos[n_sect].section_sp = section_sp;
1130 if (section_sp != NULL)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001131 {
Greg Clayton3508c382012-02-24 01:59:29 +00001132 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1133 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001134 }
1135 else
1136 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001137 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001138 }
Chris Lattner24943d22010-06-08 16:52:24 +00001139 }
1140 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001141 {
1142 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001143 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001144 }
1145 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1146 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1147 {
1148 // Symbol is in section with zero size, but has the same start
1149 // address as the section. This can happen with linker symbols
1150 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001151 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001152 }
Chris Lattner24943d22010-06-08 16:52:24 +00001153 }
Greg Clayton3508c382012-02-24 01:59:29 +00001154 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001155 }
1156
1157protected:
1158 struct SectionInfo
1159 {
1160 SectionInfo () :
1161 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001162 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001163 {
1164 }
1165
1166 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001167 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001168 };
1169 SectionList *m_section_list;
1170 std::vector<SectionInfo> m_section_infos;
1171};
1172
1173
1174
1175size_t
1176ObjectFileMachO::ParseSymtab (bool minimize)
1177{
1178 Timer scoped_timer(__PRETTY_FUNCTION__,
1179 "ObjectFileMachO::ParseSymtab () module = %s",
1180 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001181 ModuleSP module_sp (GetModule());
1182 if (!module_sp)
1183 return 0;
1184
1185 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1186 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1187 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1188 FunctionStarts function_starts;
Chris Lattner24943d22010-06-08 16:52:24 +00001189 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1190 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001191
Greg Clayton0fea0512011-12-30 00:32:24 +00001192 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
1193
Chris Lattner24943d22010-06-08 16:52:24 +00001194 for (i=0; i<m_header.ncmds; ++i)
1195 {
1196 const uint32_t cmd_offset = offset;
1197 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001198 struct load_command lc;
1199 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001200 break;
1201 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001202 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001203 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001204 case LoadCommandSymtab:
1205 symtab_load_command.cmd = lc.cmd;
1206 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001207 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001208 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1209 return 0;
1210 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001211 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001212 if (log)
1213 module_sp->LogMessage(log.get(), "LC_SYMTAB.symoff == 0");
1214 return 0;
1215 }
1216
1217 if (symtab_load_command.stroff == 0)
1218 {
1219 if (log)
1220 module_sp->LogMessage(log.get(), "LC_SYMTAB.stroff == 0");
1221 return 0;
1222 }
1223
1224 if (symtab_load_command.nsyms == 0)
1225 {
1226 if (log)
1227 module_sp->LogMessage(log.get(), "LC_SYMTAB.nsyms == 0");
1228 return 0;
1229 }
1230
1231 if (symtab_load_command.strsize == 0)
1232 {
1233 if (log)
1234 module_sp->LogMessage(log.get(), "LC_SYMTAB.strsize == 0");
1235 return 0;
1236 }
1237 break;
1238
1239 case LoadCommandFunctionStarts:
1240 function_starts_load_command.cmd = lc.cmd;
1241 function_starts_load_command.cmdsize = lc.cmdsize;
1242 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1243 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1244 break;
1245
1246 default:
1247 break;
1248 }
1249 offset = cmd_offset + lc.cmdsize;
1250 }
1251
1252 if (symtab_load_command.cmd)
1253 {
1254 Symtab *symtab = m_symtab_ap.get();
1255 SectionList *section_list = GetSectionList();
1256 if (section_list == NULL)
1257 return 0;
1258
1259 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001260 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001261
1262 const size_t addr_byte_size = m_data.GetAddressByteSize();
1263 bool bit_width_32 = addr_byte_size == 4;
1264 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1265
1266 DataExtractor nlist_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1267 DataExtractor strtab_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1268 DataExtractor function_starts_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1269
1270 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1271 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001272 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1273 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001274 {
Greg Claytondd29b972012-05-18 23:20:01 +00001275 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001276 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1277 // Reading mach file from memory in a process or core file...
1278
1279 if (linkedit_section_sp)
1280 {
1281 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1282 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1283 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001284 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001285
1286 bool data_was_read = false;
1287
1288#if defined (__APPLE__) && defined (__arm__)
1289 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001290 {
Greg Clayton29021d32012-04-18 05:19:20 +00001291 // This mach-o memory file is in the dyld shared cache. If this
1292 // program is not remote and this is iOS, then this process will
1293 // share the same shared cache as the process we are debugging and
1294 // we can read the entire __LINKEDIT from the address space in this
1295 // process. This is a needed optimization that is used for local iOS
1296 // debugging only since all shared libraries in the shared cache do
1297 // not have corresponding files that exist in the file system of the
1298 // device. They have been combined into a single file. This means we
1299 // always have to load these files from memory. All of the symbol and
1300 // string tables from all of the __LINKEDIT sections from the shared
1301 // libraries in the shared cache have been merged into a single large
1302 // symbol and string table. Reading all of this symbol and string table
1303 // data across can slow down debug launch times, so we optimize this by
1304 // reading the memory for the __LINKEDIT section from this process.
1305 PlatformSP platform_sp (target.GetPlatform());
1306 if (platform_sp && platform_sp->IsHost())
1307 {
1308 data_was_read = true;
1309 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001310 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001311 if (function_starts_load_command.cmd)
1312 {
1313 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1314 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1315 }
1316 }
1317 }
1318#endif
1319
1320 if (!data_was_read)
1321 {
1322 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1323 if (nlist_data_sp)
1324 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001325 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1326 //if (strtab_data_sp)
1327 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Greg Clayton29021d32012-04-18 05:19:20 +00001328 if (function_starts_load_command.cmd)
1329 {
1330 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1331 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1332 if (func_start_data_sp)
1333 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1334 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001335 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001336 }
1337 }
1338 else
1339 {
1340 nlist_data.SetData (m_data,
1341 symtab_load_command.symoff,
1342 nlist_data_byte_size);
1343 strtab_data.SetData (m_data,
1344 symtab_load_command.stroff,
1345 strtab_data_byte_size);
1346 if (function_starts_load_command.cmd)
1347 {
1348 function_starts_data.SetData (m_data,
1349 function_starts_load_command.dataoff,
1350 function_starts_load_command.datasize);
1351 }
1352 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001353
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001354 if (nlist_data.GetByteSize() == 0)
1355 {
1356 if (log)
1357 module_sp->LogMessage(log.get(), "failed to read nlist data");
1358 return 0;
1359 }
1360
1361
Greg Claytondd29b972012-05-18 23:20:01 +00001362 if (process)
1363 {
1364 if (strtab_addr == LLDB_INVALID_ADDRESS)
1365 {
1366 if (log)
1367 module_sp->LogMessage(log.get(), "failed to locate the strtab in memory");
1368 return 0;
1369 }
1370 }
1371 else if (strtab_data.GetByteSize() == 0)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001372 {
1373 if (log)
1374 module_sp->LogMessage(log.get(), "failed to read strtab data");
1375 return 0;
1376 }
Greg Claytondd29b972012-05-18 23:20:01 +00001377
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001378 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1379 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1380 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1381 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1382 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1383 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1384 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1385 SectionSP eh_frame_section_sp;
1386 if (text_section_sp.get())
1387 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1388 else
1389 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1390
Greg Claytond2653c22012-03-14 01:53:24 +00001391 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001392 if (text_section_sp && function_starts_data.GetByteSize())
1393 {
1394 FunctionStarts::Entry function_start_entry;
1395 function_start_entry.data = false;
1396 uint32_t function_start_offset = 0;
1397 function_start_entry.addr = text_section_sp->GetFileAddress();
1398 uint64_t delta;
1399 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1400 {
1401 // Now append the current entry
1402 function_start_entry.addr += delta;
1403 function_starts.Append(function_start_entry);
1404 }
1405 }
1406
1407 const uint32_t function_starts_count = function_starts.GetSize();
1408
1409 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
1410
1411 uint32_t nlist_data_offset = 0;
1412
1413 uint32_t N_SO_index = UINT32_MAX;
1414
1415 MachSymtabSectionInfo section_info (section_list);
1416 std::vector<uint32_t> N_FUN_indexes;
1417 std::vector<uint32_t> N_NSYM_indexes;
1418 std::vector<uint32_t> N_INCL_indexes;
1419 std::vector<uint32_t> N_BRAC_indexes;
1420 std::vector<uint32_t> N_COMM_indexes;
1421 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1422 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1423 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1424 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1425 // Any symbols that get merged into another will get an entry
1426 // in this map so we know
1427 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1428 uint32_t nlist_idx = 0;
1429 Symbol *symbol_ptr = NULL;
1430
1431 uint32_t sym_idx = 0;
1432 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
1433 uint32_t num_syms = symtab->GetNumSymbols();
Greg Claytondd29b972012-05-18 23:20:01 +00001434 std::string memory_symbol_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001435
1436 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
1437 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
1438 {
1439 struct nlist_64 nlist;
1440 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1441 break;
1442
1443 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
1444 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
1445 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
1446 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
1447 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
1448
1449 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00001450 const char *symbol_name = NULL;
1451
1452 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001453 {
Greg Claytondd29b972012-05-18 23:20:01 +00001454 const addr_t str_addr = strtab_addr + nlist.n_strx;
1455 Error str_error;
1456 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
1457 symbol_name = memory_symbol_name.c_str();
1458 }
1459 else
1460 {
1461 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
1462
1463 if (symbol_name == NULL)
1464 {
1465 // No symbol should be NULL, even the symbols with no
1466 // string values should have an offset zero which points
1467 // to an empty C-string
1468 Host::SystemLog (Host::eSystemLogError,
1469 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1470 nlist_idx,
1471 nlist.n_strx,
1472 module_sp->GetFileSpec().GetDirectory().GetCString(),
1473 module_sp->GetFileSpec().GetFilename().GetCString());
1474 continue;
1475 }
1476 if (symbol_name[0] == '\0')
1477 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001478 }
1479 const char *symbol_name_non_abi_mangled = NULL;
1480
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001481 SectionSP symbol_section;
1482 uint32_t symbol_byte_size = 0;
1483 bool add_nlist = true;
1484 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
1485
1486 assert (sym_idx < num_syms);
1487
1488 sym[sym_idx].SetDebug (is_debug);
1489
1490 if (is_debug)
1491 {
1492 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00001493 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001494 case StabGlobalSymbol:
1495 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1496 // Sometimes the N_GSYM value contains the address.
1497
1498 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1499 // have the same address, but we want to ensure that we always find only the real symbol,
1500 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1501 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1502 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1503 // same address.
1504
1505 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1506 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1507 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1508 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1509 add_nlist = false;
1510 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00001511 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001512 sym[sym_idx].SetExternal(true);
1513 if (nlist.n_value != 0)
1514 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1515 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00001516 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001517 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00001518
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001519 case StabFunctionName:
1520 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1521 type = eSymbolTypeCompiler;
1522 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00001523
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001524 case StabFunction:
1525 // N_FUN -- procedure: name,,n_sect,linenumber,address
1526 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00001527 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001528 type = eSymbolTypeCode;
1529 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1530
1531 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1532 // We use the current number of symbols in the symbol table in lieu of
1533 // using nlist_idx in case we ever start trimming entries out
1534 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001535 }
1536 else
1537 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001538 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00001539
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001540 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00001541 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001542 // Copy the size of the function into the original STAB entry so we don't have
1543 // to hunt for it later
1544 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1545 N_FUN_indexes.pop_back();
1546 // We don't really need the end function STAB as it contains the size which
1547 // we already placed with the original symbol, so don't add it if we want a
1548 // minimal symbol table
1549 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00001550 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001551 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00001552 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001553 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00001554
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001555 case StabStaticSymbol:
1556 // N_STSYM -- static symbol: name,,n_sect,type,address
1557 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1558 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1559 type = eSymbolTypeData;
1560 break;
1561
1562 case StabLocalCommon:
1563 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1564 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1565 type = eSymbolTypeCommonBlock;
1566 break;
1567
1568 case StabBeginSymbol:
1569 // N_BNSYM
1570 // We use the current number of symbols in the symbol table in lieu of
1571 // using nlist_idx in case we ever start trimming entries out
1572 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00001573 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001574 // Skip these if we want minimal symbol tables
1575 add_nlist = false;
1576 }
1577 else
1578 {
1579 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1580 N_NSYM_indexes.push_back(sym_idx);
1581 type = eSymbolTypeScopeBegin;
1582 }
1583 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00001584
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001585 case StabEndSymbol:
1586 // N_ENSYM
1587 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1588 // so that we can always skip the entire symbol if we need to navigate
1589 // more quickly at the source level when parsing STABS
1590 if (minimize)
1591 {
1592 // Skip these if we want minimal symbol tables
1593 add_nlist = false;
1594 }
1595 else
1596 {
1597 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00001598 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001599 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1600 symbol_ptr->SetByteSize(sym_idx + 1);
1601 symbol_ptr->SetSizeIsSibling(true);
1602 N_NSYM_indexes.pop_back();
1603 }
1604 type = eSymbolTypeScopeEnd;
1605 }
1606 break;
1607
1608
1609 case StabSourceFileOptions:
1610 // N_OPT - emitted with gcc2_compiled and in gcc source
1611 type = eSymbolTypeCompiler;
1612 break;
1613
1614 case StabRegisterSymbol:
1615 // N_RSYM - register sym: name,,NO_SECT,type,register
1616 type = eSymbolTypeVariable;
1617 break;
1618
1619 case StabSourceLine:
1620 // N_SLINE - src line: 0,,n_sect,linenumber,address
1621 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1622 type = eSymbolTypeLineEntry;
1623 break;
1624
1625 case StabStructureType:
1626 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1627 type = eSymbolTypeVariableType;
1628 break;
1629
1630 case StabSourceFileName:
1631 // N_SO - source file name
1632 type = eSymbolTypeSourceFile;
1633 if (symbol_name == NULL)
1634 {
1635 if (minimize)
1636 add_nlist = false;
1637 if (N_SO_index != UINT32_MAX)
1638 {
1639 // Set the size of the N_SO to the terminating index of this N_SO
1640 // so that we can always skip the entire N_SO if we need to navigate
1641 // more quickly at the source level when parsing STABS
1642 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1643 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1644 symbol_ptr->SetSizeIsSibling(true);
1645 }
1646 N_NSYM_indexes.clear();
1647 N_INCL_indexes.clear();
1648 N_BRAC_indexes.clear();
1649 N_COMM_indexes.clear();
1650 N_FUN_indexes.clear();
1651 N_SO_index = UINT32_MAX;
1652 }
1653 else
1654 {
1655 // We use the current number of symbols in the symbol table in lieu of
1656 // using nlist_idx in case we ever start trimming entries out
1657 if (symbol_name[0] == '/')
1658 N_SO_index = sym_idx;
1659 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1660 {
1661 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1662 if (so_path && so_path[0])
1663 {
1664 std::string full_so_path (so_path);
1665 if (*full_so_path.rbegin() != '/')
1666 full_so_path += '/';
1667 full_so_path += symbol_name;
1668 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
1669 add_nlist = false;
1670 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1671 }
1672 }
1673 }
1674
1675 break;
1676
1677 case StabObjectFileName:
1678 // N_OSO - object file name: name,,0,0,st_mtime
1679 type = eSymbolTypeObjectFile;
1680 break;
1681
1682 case StabLocalSymbol:
1683 // N_LSYM - local sym: name,,NO_SECT,type,offset
1684 type = eSymbolTypeLocal;
1685 break;
1686
1687 //----------------------------------------------------------------------
1688 // INCL scopes
1689 //----------------------------------------------------------------------
1690 case StabBeginIncludeFileName:
1691 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
1692 // We use the current number of symbols in the symbol table in lieu of
1693 // using nlist_idx in case we ever start trimming entries out
1694 N_INCL_indexes.push_back(sym_idx);
1695 type = eSymbolTypeScopeBegin;
1696 break;
1697
1698 case StabEndIncludeFile:
1699 // N_EINCL - include file end: name,,NO_SECT,0,0
1700 // Set the size of the N_BINCL to the terminating index of this N_EINCL
1701 // so that we can always skip the entire symbol if we need to navigate
1702 // more quickly at the source level when parsing STABS
1703 if ( !N_INCL_indexes.empty() )
1704 {
1705 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1706 symbol_ptr->SetByteSize(sym_idx + 1);
1707 symbol_ptr->SetSizeIsSibling(true);
1708 N_INCL_indexes.pop_back();
1709 }
1710 type = eSymbolTypeScopeEnd;
1711 break;
1712
1713 case StabIncludeFileName:
1714 // N_SOL - #included file name: name,,n_sect,0,address
1715 type = eSymbolTypeHeaderFile;
1716
1717 // We currently don't use the header files on darwin
1718 if (minimize)
1719 add_nlist = false;
1720 break;
1721
1722 case StabCompilerParameters:
1723 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
1724 type = eSymbolTypeCompiler;
1725 break;
1726
1727 case StabCompilerVersion:
1728 // N_VERSION - compiler version: name,,NO_SECT,0,0
1729 type = eSymbolTypeCompiler;
1730 break;
1731
1732 case StabCompilerOptLevel:
1733 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
1734 type = eSymbolTypeCompiler;
1735 break;
1736
1737 case StabParameter:
1738 // N_PSYM - parameter: name,,NO_SECT,type,offset
1739 type = eSymbolTypeVariable;
1740 break;
1741
1742 case StabAlternateEntry:
1743 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
1744 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1745 type = eSymbolTypeLineEntry;
1746 break;
1747
1748 //----------------------------------------------------------------------
1749 // Left and Right Braces
1750 //----------------------------------------------------------------------
1751 case StabLeftBracket:
1752 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
1753 // We use the current number of symbols in the symbol table in lieu of
1754 // using nlist_idx in case we ever start trimming entries out
1755 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1756 N_BRAC_indexes.push_back(sym_idx);
1757 type = eSymbolTypeScopeBegin;
1758 break;
1759
1760 case StabRightBracket:
1761 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
1762 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
1763 // so that we can always skip the entire symbol if we need to navigate
1764 // more quickly at the source level when parsing STABS
1765 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1766 if ( !N_BRAC_indexes.empty() )
1767 {
1768 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1769 symbol_ptr->SetByteSize(sym_idx + 1);
1770 symbol_ptr->SetSizeIsSibling(true);
1771 N_BRAC_indexes.pop_back();
1772 }
1773 type = eSymbolTypeScopeEnd;
1774 break;
1775
1776 case StabDeletedIncludeFile:
1777 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
1778 type = eSymbolTypeHeaderFile;
1779 break;
1780
1781 //----------------------------------------------------------------------
1782 // COMM scopes
1783 //----------------------------------------------------------------------
1784 case StabBeginCommon:
1785 // N_BCOMM - begin common: name,,NO_SECT,0,0
1786 // We use the current number of symbols in the symbol table in lieu of
1787 // using nlist_idx in case we ever start trimming entries out
1788 type = eSymbolTypeScopeBegin;
1789 N_COMM_indexes.push_back(sym_idx);
1790 break;
1791
1792 case StabEndCommonLocal:
1793 // N_ECOML - end common (local name): 0,,n_sect,0,address
1794 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1795 // Fall through
1796
1797 case StabEndCommon:
1798 // N_ECOMM - end common: name,,n_sect,0,0
1799 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1800 // so that we can always skip the entire symbol if we need to navigate
1801 // more quickly at the source level when parsing STABS
1802 if ( !N_COMM_indexes.empty() )
1803 {
1804 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1805 symbol_ptr->SetByteSize(sym_idx + 1);
1806 symbol_ptr->SetSizeIsSibling(true);
1807 N_COMM_indexes.pop_back();
1808 }
1809 type = eSymbolTypeScopeEnd;
1810 break;
1811
1812 case StabLength:
1813 // N_LENG - second stab entry with length information
1814 type = eSymbolTypeAdditional;
1815 break;
1816
1817 default: break;
1818 }
1819 }
1820 else
1821 {
1822 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
1823 uint8_t n_type = NlistMaskType & nlist.n_type;
1824 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
1825
1826 switch (n_type)
1827 {
1828 case NListTypeIndirect: // N_INDR - Fall through
1829 case NListTypePreboundUndefined:// N_PBUD - Fall through
1830 case NListTypeUndefined: // N_UNDF
1831 type = eSymbolTypeUndefined;
1832 break;
1833
1834 case NListTypeAbsolute: // N_ABS
1835 type = eSymbolTypeAbsolute;
1836 break;
1837
1838 case NListTypeSection: // N_SECT
1839 {
1840 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1841
1842 if (symbol_section == NULL)
1843 {
1844 // TODO: warn about this?
1845 add_nlist = false;
1846 break;
1847 }
1848
1849 if (TEXT_eh_frame_sectID == nlist.n_sect)
1850 {
1851 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00001852 }
1853 else
1854 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001855 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
1856
1857 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001858 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001859 case SectionTypeRegular: break; // regular section
1860 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
1861 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
1862 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
1863 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
1864 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1865 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1866 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1867 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1868 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
1869 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
1870 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
1871 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
1872 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
1873 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
1874 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
1875 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
1876 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00001877 }
Chris Lattner24943d22010-06-08 16:52:24 +00001878
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001879 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00001880 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001881 const char *symbol_sect_name = symbol_section->GetName().AsCString();
1882 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00001883 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001884 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1885 SectionAttrUserSelfModifyingCode |
1886 SectionAttrSytemSomeInstructions))
1887 type = eSymbolTypeData;
1888 else
1889 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00001890 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001891 else
1892 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00001893 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001894 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00001895 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001896 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00001897
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001898 if (symbol_name &&
1899 symbol_name[0] == '_' &&
1900 symbol_name[1] == 'O' &&
1901 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00001902 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001903 llvm::StringRef symbol_name_ref(symbol_name);
1904 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
1905 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
1906 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
1907 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00001908 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001909 symbol_name_non_abi_mangled = symbol_name + 1;
1910 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
1911 type = eSymbolTypeObjCClass;
Chris Lattner24943d22010-06-08 16:52:24 +00001912 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001913 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00001914 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001915 symbol_name_non_abi_mangled = symbol_name + 1;
1916 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
1917 type = eSymbolTypeObjCMetaClass;
1918 }
1919 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
1920 {
1921 symbol_name_non_abi_mangled = symbol_name + 1;
1922 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
1923 type = eSymbolTypeObjCIVar;
Chris Lattner24943d22010-06-08 16:52:24 +00001924 }
1925 }
1926 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001927 else
1928 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1929 {
1930 type = eSymbolTypeException;
1931 }
1932 else
1933 {
1934 type = eSymbolTypeData;
1935 }
1936 }
1937 else
1938 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1939 {
1940 type = eSymbolTypeTrampoline;
1941 }
1942 else
1943 if (symbol_section->IsDescendant(objc_section_sp.get()))
1944 {
1945 type = eSymbolTypeRuntime;
1946 if (symbol_name && symbol_name[0] == '.')
1947 {
1948 llvm::StringRef symbol_name_ref(symbol_name);
1949 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
1950 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
1951 {
1952 symbol_name_non_abi_mangled = symbol_name;
1953 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
1954 type = eSymbolTypeObjCClass;
1955 }
1956 }
Chris Lattner24943d22010-06-08 16:52:24 +00001957 }
1958 }
1959 }
1960 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001961 break;
1962 }
1963 }
1964
1965 if (add_nlist)
1966 {
1967 uint64_t symbol_value = nlist.n_value;
1968 bool symbol_name_is_mangled = false;
1969
1970 if (symbol_name_non_abi_mangled)
1971 {
1972 sym[sym_idx].GetMangled().SetMangledName (symbol_name_non_abi_mangled);
1973 sym[sym_idx].GetMangled().SetDemangledName (symbol_name);
Chris Lattner24943d22010-06-08 16:52:24 +00001974 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001975 else
1976 {
1977 if (symbol_name && symbol_name[0] == '_')
1978 {
1979 symbol_name_is_mangled = symbol_name[1] == '_';
1980 symbol_name++; // Skip the leading underscore
1981 }
1982
1983 if (symbol_name)
1984 {
1985 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
1986 }
1987 }
1988
1989 if (is_debug == false)
1990 {
1991 if (type == eSymbolTypeCode)
1992 {
1993 // See if we can find a N_FUN entry for any code symbols.
1994 // If we do find a match, and the name matches, then we
1995 // can merge the two into just the function symbol to avoid
1996 // duplicate entries in the symbol table
1997 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1998 if (pos != N_FUN_addr_to_sym_idx.end())
1999 {
2000 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2001 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2002 {
2003 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2004 // We just need the flags from the linker symbol, so put these flags
2005 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2006 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2007 sym[sym_idx].Clear();
2008 continue;
2009 }
2010 }
2011 }
2012 else if (type == eSymbolTypeData)
2013 {
2014 // See if we can find a N_STSYM entry for any data symbols.
2015 // If we do find a match, and the name matches, then we
2016 // can merge the two into just the Static symbol to avoid
2017 // duplicate entries in the symbol table
2018 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2019 if (pos != N_STSYM_addr_to_sym_idx.end())
2020 {
2021 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2022 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2023 {
2024 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2025 // We just need the flags from the linker symbol, so put these flags
2026 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2027 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2028 sym[sym_idx].Clear();
2029 continue;
2030 }
2031 }
2032 }
2033 }
2034 if (symbol_section)
2035 {
2036 const addr_t section_file_addr = symbol_section->GetFileAddress();
2037 if (symbol_byte_size == 0 && function_starts_count > 0)
2038 {
Greg Claytond2653c22012-03-14 01:53:24 +00002039 addr_t symbol_lookup_file_addr = nlist.n_value;
2040 // Do an exact address match for non-ARM addresses, else get the closest since
2041 // the symbol might be a thumb symbol which has an address with bit zero set
2042 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2043 if (is_arm && func_start_entry)
2044 {
2045 // Verify that the function start address is the symbol address (ARM)
2046 // or the symbol address + 1 (thumb)
2047 if (func_start_entry->addr != symbol_lookup_file_addr &&
2048 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2049 {
2050 // Not the right entry, NULL it out...
2051 func_start_entry = NULL;
2052 }
2053 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002054 if (func_start_entry)
2055 {
2056 func_start_entry->data = true;
Greg Claytond2653c22012-03-14 01:53:24 +00002057
2058 addr_t symbol_file_addr = func_start_entry->addr;
2059 uint32_t symbol_flags = 0;
2060 if (is_arm)
2061 {
2062 if (symbol_file_addr & 1)
2063 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2064 symbol_file_addr &= 0xfffffffffffffffeull;
2065 }
2066
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002067 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2068 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2069 if (next_func_start_entry)
2070 {
Greg Claytond2653c22012-03-14 01:53:24 +00002071 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2072 // Be sure the clear the Thumb address bit when we calculate the size
2073 // from the current and next address
2074 if (is_arm)
2075 next_symbol_file_addr &= 0xfffffffffffffffeull;
2076 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 +00002077 }
2078 else
2079 {
Greg Claytond2653c22012-03-14 01:53:24 +00002080 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002081 }
2082 }
2083 }
2084 symbol_value -= section_file_addr;
2085 }
2086
2087 sym[sym_idx].SetID (nlist_idx);
2088 sym[sym_idx].SetType (type);
2089 sym[sym_idx].GetAddress().SetSection (symbol_section);
2090 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2091 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2092
2093 if (symbol_byte_size > 0)
2094 sym[sym_idx].SetByteSize(symbol_byte_size);
2095
2096 ++sym_idx;
2097 }
2098 else
2099 {
2100 sym[sym_idx].Clear();
2101 }
2102
2103 }
2104
2105 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
2106 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
2107 // such entries by figuring out what the address for the global is by looking up this non-STAB
2108 // entry and copying the value into the debug symbol's value to save us the hassle in the
2109 // debug symbol parser.
2110
2111 Symbol *global_symbol = NULL;
2112 for (nlist_idx = 0;
2113 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
2114 nlist_idx++)
2115 {
2116 if (global_symbol->GetAddress().GetFileAddress() == 0)
2117 {
2118 std::vector<uint32_t> indexes;
2119 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
2120 {
2121 std::vector<uint32_t>::const_iterator pos;
2122 std::vector<uint32_t>::const_iterator end = indexes.end();
2123 for (pos = indexes.begin(); pos != end; ++pos)
2124 {
2125 symbol_ptr = symtab->SymbolAtIndex(*pos);
2126 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
2127 {
2128 global_symbol->GetAddress() = symbol_ptr->GetAddress();
2129 break;
2130 }
2131 }
2132 }
Chris Lattner24943d22010-06-08 16:52:24 +00002133 }
2134 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002135
2136 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
2137
2138
2139 if (function_starts_count > 0)
2140 {
2141 char synthetic_function_symbol[PATH_MAX];
2142 uint32_t num_synthetic_function_symbols = 0;
2143 for (i=0; i<function_starts_count; ++i)
2144 {
2145 if (function_starts.GetEntryRef (i).data == false)
2146 ++num_synthetic_function_symbols;
2147 }
2148
2149 if (num_synthetic_function_symbols > 0)
2150 {
2151 if (num_syms < sym_idx + num_synthetic_function_symbols)
2152 {
2153 num_syms = sym_idx + num_synthetic_function_symbols;
2154 sym = symtab->Resize (num_syms);
2155 }
2156 uint32_t synthetic_function_symbol_idx = 0;
2157 for (i=0; i<function_starts_count; ++i)
2158 {
2159 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
2160 if (func_start_entry->data == false)
2161 {
Greg Claytond2653c22012-03-14 01:53:24 +00002162 addr_t symbol_file_addr = func_start_entry->addr;
2163 uint32_t symbol_flags = 0;
2164 if (is_arm)
2165 {
2166 if (symbol_file_addr & 1)
2167 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2168 symbol_file_addr &= 0xfffffffffffffffeull;
2169 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002170 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00002171 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002172 {
2173 SectionSP symbol_section (symbol_addr.GetSection());
2174 uint32_t symbol_byte_size = 0;
2175 if (symbol_section)
2176 {
2177 const addr_t section_file_addr = symbol_section->GetFileAddress();
2178 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2179 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2180 if (next_func_start_entry)
2181 {
Greg Claytond2653c22012-03-14 01:53:24 +00002182 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2183 if (is_arm)
2184 next_symbol_file_addr &= 0xfffffffffffffffeull;
2185 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 +00002186 }
2187 else
2188 {
Greg Claytond2653c22012-03-14 01:53:24 +00002189 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002190 }
2191 snprintf (synthetic_function_symbol,
2192 sizeof(synthetic_function_symbol),
2193 "___lldb_unnamed_function%u$$%s",
2194 ++synthetic_function_symbol_idx,
2195 module_sp->GetFileSpec().GetFilename().GetCString());
2196 sym[sym_idx].SetID (synthetic_sym_id++);
2197 sym[sym_idx].GetMangled().SetDemangledName(synthetic_function_symbol);
2198 sym[sym_idx].SetType (eSymbolTypeCode);
2199 sym[sym_idx].SetIsSynthetic (true);
2200 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00002201 if (symbol_flags)
2202 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002203 if (symbol_byte_size)
2204 sym[sym_idx].SetByteSize (symbol_byte_size);
2205 ++sym_idx;
2206 }
2207 }
2208 }
2209 }
2210 }
2211 }
2212
2213 // Trim our symbols down to just what we ended up with after
2214 // removing any symbols.
2215 if (sym_idx < num_syms)
2216 {
2217 num_syms = sym_idx;
2218 sym = symtab->Resize (num_syms);
2219 }
2220
2221 // Now synthesize indirect symbols
2222 if (m_dysymtab.nindirectsyms != 0)
2223 {
2224 DataExtractor indirect_symbol_index_data (m_data, m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4);
2225
2226 if (indirect_symbol_index_data.GetByteSize())
2227 {
2228 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
2229
2230 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
2231 {
2232 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
2233 {
2234 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
2235 if (symbol_stub_byte_size == 0)
2236 continue;
2237
2238 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
2239
2240 if (num_symbol_stubs == 0)
2241 continue;
2242
2243 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
2244 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
2245 {
2246 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
2247 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
2248 uint32_t symbol_stub_offset = symbol_stub_index * 4;
2249 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
2250 {
2251 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
2252 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
2253 continue;
2254
2255 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
2256 Symbol *stub_symbol = NULL;
2257 if (index_pos != end_index_pos)
2258 {
2259 // We have a remapping from the original nlist index to
2260 // a current symbol index, so just look this up by index
2261 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
2262 }
2263 else
2264 {
2265 // We need to lookup a symbol using the original nlist
2266 // symbol index since this index is coming from the
2267 // S_SYMBOL_STUBS
2268 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
2269 }
2270
2271 assert (stub_symbol);
2272 if (stub_symbol)
2273 {
2274 Address so_addr(symbol_stub_addr, section_list);
2275
2276 if (stub_symbol->GetType() == eSymbolTypeUndefined)
2277 {
2278 // Change the external symbol into a trampoline that makes sense
2279 // These symbols were N_UNDF N_EXT, and are useless to us, so we
2280 // can re-use them so we don't have to make up a synthetic symbol
2281 // for no good reason.
2282 stub_symbol->SetType (eSymbolTypeTrampoline);
2283 stub_symbol->SetExternal (false);
2284 stub_symbol->GetAddress() = so_addr;
2285 stub_symbol->SetByteSize (symbol_stub_byte_size);
2286 }
2287 else
2288 {
2289 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00002290 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002291 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00002292 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002293 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00002294 stub_symbol = NULL; // this pointer no longer valid
2295 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002296 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00002297 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002298 sym[sym_idx].SetType (eSymbolTypeTrampoline);
2299 sym[sym_idx].SetIsSynthetic (true);
2300 sym[sym_idx].GetAddress() = so_addr;
2301 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
2302 ++sym_idx;
2303 }
2304 }
2305 }
2306 }
2307 }
2308 }
2309 }
2310 }
2311 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00002312 }
2313 return 0;
2314}
2315
2316
2317void
2318ObjectFileMachO::Dump (Stream *s)
2319{
Greg Clayton9482f052012-03-13 23:14:29 +00002320 ModuleSP module_sp(GetModule());
2321 if (module_sp)
2322 {
2323 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2324 s->Printf("%p: ", this);
2325 s->Indent();
2326 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
2327 s->PutCString("ObjectFileMachO64");
2328 else
2329 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00002330
Greg Clayton9482f052012-03-13 23:14:29 +00002331 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00002332
Greg Clayton9482f052012-03-13 23:14:29 +00002333 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00002334
Greg Clayton9482f052012-03-13 23:14:29 +00002335 if (m_sections_ap.get())
2336 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00002337
Greg Clayton9482f052012-03-13 23:14:29 +00002338 if (m_symtab_ap.get())
2339 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
2340 }
Chris Lattner24943d22010-06-08 16:52:24 +00002341}
2342
2343
2344bool
Greg Clayton0467c782011-02-04 18:53:10 +00002345ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00002346{
Greg Clayton9482f052012-03-13 23:14:29 +00002347 ModuleSP module_sp(GetModule());
2348 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002349 {
Greg Clayton9482f052012-03-13 23:14:29 +00002350 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2351 struct uuid_command load_cmd;
2352 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2353 uint32_t i;
2354 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00002355 {
Greg Clayton9482f052012-03-13 23:14:29 +00002356 const uint32_t cmd_offset = offset;
2357 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
2358 break;
2359
2360 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00002361 {
Greg Clayton9482f052012-03-13 23:14:29 +00002362 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
2363 if (uuid_bytes)
2364 {
2365 uuid->SetBytes (uuid_bytes);
2366 return true;
2367 }
2368 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00002369 }
Greg Clayton9482f052012-03-13 23:14:29 +00002370 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00002371 }
Chris Lattner24943d22010-06-08 16:52:24 +00002372 }
2373 return false;
2374}
2375
2376
2377uint32_t
2378ObjectFileMachO::GetDependentModules (FileSpecList& files)
2379{
Chris Lattner24943d22010-06-08 16:52:24 +00002380 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00002381 ModuleSP module_sp(GetModule());
2382 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00002383 {
Greg Clayton9482f052012-03-13 23:14:29 +00002384 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2385 struct load_command load_cmd;
2386 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2387 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
2388 uint32_t i;
2389 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00002390 {
Greg Clayton9482f052012-03-13 23:14:29 +00002391 const uint32_t cmd_offset = offset;
2392 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
2393 break;
Chris Lattner24943d22010-06-08 16:52:24 +00002394
Greg Clayton9482f052012-03-13 23:14:29 +00002395 switch (load_cmd.cmd)
2396 {
2397 case LoadCommandDylibLoad:
2398 case LoadCommandDylibLoadWeak:
2399 case LoadCommandDylibReexport:
2400 case LoadCommandDynamicLinkerLoad:
2401 case LoadCommandFixedVMShlibLoad:
2402 case LoadCommandDylibLoadUpward:
2403 {
2404 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
2405 const char *path = m_data.PeekCStr(name_offset);
2406 // Skip any path that starts with '@' since these are usually:
2407 // @executable_path/.../file
2408 // @rpath/.../file
2409 if (path && path[0] != '@')
2410 {
2411 FileSpec file_spec(path, resolve_path);
2412 if (files.AppendIfUnique(file_spec))
2413 count++;
2414 }
2415 }
2416 break;
2417
2418 default:
2419 break;
2420 }
2421 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00002422 }
Chris Lattner24943d22010-06-08 16:52:24 +00002423 }
2424 return count;
2425}
2426
Jim Ingham28775942011-03-07 23:44:08 +00002427lldb_private::Address
2428ObjectFileMachO::GetEntryPointAddress ()
2429{
2430 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
2431 // is initialized to an invalid address, so we can just return that.
2432 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
2433
2434 if (!IsExecutable() || m_entry_point_address.IsValid())
2435 return m_entry_point_address;
2436
2437 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
2438 // /usr/include/mach-o.h, but it is basically:
2439 //
2440 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
2441 // uint32_t count - this is the count of longs in the thread state data
2442 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
2443 // <repeat this trio>
2444 //
2445 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
2446 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
2447 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
2448 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
2449 //
2450 // For now we hard-code the offsets and flavors we need:
2451 //
2452 //
2453
Greg Clayton9482f052012-03-13 23:14:29 +00002454 ModuleSP module_sp(GetModule());
2455 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00002456 {
Greg Clayton9482f052012-03-13 23:14:29 +00002457 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2458 struct load_command load_cmd;
2459 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2460 uint32_t i;
2461 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
2462 bool done = false;
2463
2464 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00002465 {
Greg Clayton9482f052012-03-13 23:14:29 +00002466 const uint32_t cmd_offset = offset;
2467 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
2468 break;
2469
2470 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00002471 {
Greg Clayton9482f052012-03-13 23:14:29 +00002472 case LoadCommandUnixThread:
2473 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00002474 {
Greg Clayton9482f052012-03-13 23:14:29 +00002475 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00002476 {
Greg Clayton9482f052012-03-13 23:14:29 +00002477 uint32_t flavor = m_data.GetU32(&offset);
2478 uint32_t count = m_data.GetU32(&offset);
2479 if (count == 0)
2480 {
2481 // We've gotten off somehow, log and exit;
2482 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00002483 }
Greg Clayton9482f052012-03-13 23:14:29 +00002484
2485 switch (m_header.cputype)
2486 {
2487 case llvm::MachO::CPUTypeARM:
2488 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
2489 {
2490 offset += 60; // This is the offset of pc in the GPR thread state data structure.
2491 start_address = m_data.GetU32(&offset);
2492 done = true;
2493 }
Jim Ingham28775942011-03-07 23:44:08 +00002494 break;
Greg Clayton9482f052012-03-13 23:14:29 +00002495 case llvm::MachO::CPUTypeI386:
2496 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
2497 {
2498 offset += 40; // This is the offset of eip in the GPR thread state data structure.
2499 start_address = m_data.GetU32(&offset);
2500 done = true;
2501 }
2502 break;
2503 case llvm::MachO::CPUTypeX86_64:
2504 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
2505 {
2506 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
2507 start_address = m_data.GetU64(&offset);
2508 done = true;
2509 }
2510 break;
2511 default:
2512 return m_entry_point_address;
2513 }
2514 // Haven't found the GPR flavor yet, skip over the data for this flavor:
2515 if (done)
2516 break;
2517 offset += count * 4;
2518 }
Jim Ingham28775942011-03-07 23:44:08 +00002519 }
Greg Clayton9482f052012-03-13 23:14:29 +00002520 break;
2521 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00002522 {
Greg Clayton9482f052012-03-13 23:14:29 +00002523 ConstString text_segment_name ("__TEXT");
2524 uint64_t entryoffset = m_data.GetU64(&offset);
2525 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
2526 if (text_segment_sp)
2527 {
2528 done = true;
2529 start_address = text_segment_sp->GetFileAddress() + entryoffset;
2530 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00002531 }
Greg Clayton9482f052012-03-13 23:14:29 +00002532
2533 default:
2534 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00002535 }
Greg Clayton9482f052012-03-13 23:14:29 +00002536 if (done)
2537 break;
Jim Ingham28775942011-03-07 23:44:08 +00002538
Greg Clayton9482f052012-03-13 23:14:29 +00002539 // Go to the next load command:
2540 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00002541 }
Jim Ingham28775942011-03-07 23:44:08 +00002542
Greg Clayton9482f052012-03-13 23:14:29 +00002543 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00002544 {
Greg Clayton9482f052012-03-13 23:14:29 +00002545 // We got the start address from the load commands, so now resolve that address in the sections
2546 // of this ObjectFile:
2547 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00002548 {
Greg Clayton9482f052012-03-13 23:14:29 +00002549 m_entry_point_address.Clear();
2550 }
2551 }
2552 else
2553 {
2554 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
2555 // "start" symbol in the main executable.
2556
2557 ModuleSP module_sp (GetModule());
2558
2559 if (module_sp)
2560 {
2561 SymbolContextList contexts;
2562 SymbolContext context;
2563 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
2564 {
2565 if (contexts.GetContextAtIndex(0, context))
2566 m_entry_point_address = context.symbol->GetAddress();
2567 }
Greg Clayton3508c382012-02-24 01:59:29 +00002568 }
2569 }
Jim Ingham28775942011-03-07 23:44:08 +00002570 }
2571
2572 return m_entry_point_address;
2573
2574}
2575
Greg Claytonb5a8f142012-02-05 02:38:54 +00002576lldb_private::Address
2577ObjectFileMachO::GetHeaderAddress ()
2578{
2579 lldb_private::Address header_addr;
2580 SectionList *section_list = GetSectionList();
2581 if (section_list)
2582 {
2583 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
2584 if (text_segment_sp)
2585 {
Greg Clayton3508c382012-02-24 01:59:29 +00002586 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00002587 header_addr.SetOffset (0);
2588 }
2589 }
2590 return header_addr;
2591}
2592
Greg Clayton46c9a352012-02-09 06:16:32 +00002593uint32_t
2594ObjectFileMachO::GetNumThreadContexts ()
2595{
Greg Clayton9482f052012-03-13 23:14:29 +00002596 ModuleSP module_sp(GetModule());
2597 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00002598 {
Greg Clayton9482f052012-03-13 23:14:29 +00002599 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2600 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00002601 {
Greg Clayton9482f052012-03-13 23:14:29 +00002602 m_thread_context_offsets_valid = true;
2603 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2604 FileRangeArray::Entry file_range;
2605 thread_command thread_cmd;
2606 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00002607 {
Greg Clayton9482f052012-03-13 23:14:29 +00002608 const uint32_t cmd_offset = offset;
2609 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
2610 break;
2611
2612 if (thread_cmd.cmd == LoadCommandThread)
2613 {
2614 file_range.SetRangeBase (offset);
2615 file_range.SetByteSize (thread_cmd.cmdsize - 8);
2616 m_thread_context_offsets.Append (file_range);
2617 }
2618 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00002619 }
Greg Clayton46c9a352012-02-09 06:16:32 +00002620 }
2621 }
2622 return m_thread_context_offsets.GetSize();
2623}
2624
2625lldb::RegisterContextSP
2626ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
2627{
Greg Clayton46c9a352012-02-09 06:16:32 +00002628 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00002629
Greg Clayton9482f052012-03-13 23:14:29 +00002630 ModuleSP module_sp(GetModule());
2631 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00002632 {
Greg Clayton9482f052012-03-13 23:14:29 +00002633 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2634 if (!m_thread_context_offsets_valid)
2635 GetNumThreadContexts ();
2636
2637 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
2638
2639 DataExtractor data (m_data,
2640 thread_context_file_range->GetRangeBase(),
2641 thread_context_file_range->GetByteSize());
2642
2643 switch (m_header.cputype)
2644 {
2645 case llvm::MachO::CPUTypeARM:
2646 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
2647 break;
2648
2649 case llvm::MachO::CPUTypeI386:
2650 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
2651 break;
2652
2653 case llvm::MachO::CPUTypeX86_64:
2654 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
2655 break;
2656 }
Greg Clayton46c9a352012-02-09 06:16:32 +00002657 }
2658 return reg_ctx_sp;
2659}
2660
Greg Claytonb5a8f142012-02-05 02:38:54 +00002661
Greg Claytonca319972011-07-09 00:41:34 +00002662ObjectFile::Type
2663ObjectFileMachO::CalculateType()
2664{
2665 switch (m_header.filetype)
2666 {
2667 case HeaderFileTypeObject: // 0x1u MH_OBJECT
2668 if (GetAddressByteSize () == 4)
2669 {
2670 // 32 bit kexts are just object files, but they do have a valid
2671 // UUID load command.
2672 UUID uuid;
2673 if (GetUUID(&uuid))
2674 {
2675 // this checking for the UUID load command is not enough
2676 // we could eventually look for the symbol named
2677 // "OSKextGetCurrentIdentifier" as this is required of kexts
2678 if (m_strata == eStrataInvalid)
2679 m_strata = eStrataKernel;
2680 return eTypeSharedLibrary;
2681 }
2682 }
2683 return eTypeObjectFile;
2684
2685 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
2686 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
2687 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
2688 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
2689 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
2690 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
2691 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
2692 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
2693 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
2694 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
2695 default:
2696 break;
2697 }
2698 return eTypeUnknown;
2699}
2700
2701ObjectFile::Strata
2702ObjectFileMachO::CalculateStrata()
2703{
2704 switch (m_header.filetype)
2705 {
2706 case HeaderFileTypeObject: // 0x1u MH_OBJECT
2707 {
2708 // 32 bit kexts are just object files, but they do have a valid
2709 // UUID load command.
2710 UUID uuid;
2711 if (GetUUID(&uuid))
2712 {
2713 // this checking for the UUID load command is not enough
2714 // we could eventually look for the symbol named
2715 // "OSKextGetCurrentIdentifier" as this is required of kexts
2716 if (m_type == eTypeInvalid)
2717 m_type = eTypeSharedLibrary;
2718
2719 return eStrataKernel;
2720 }
2721 }
2722 return eStrataUnknown;
2723
2724 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
2725 // Check for the MH_DYLDLINK bit in the flags
2726 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00002727 {
Greg Claytonca319972011-07-09 00:41:34 +00002728 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00002729 }
2730 else
2731 {
2732 SectionList *section_list = GetSectionList();
2733 if (section_list)
2734 {
2735 static ConstString g_kld_section_name ("__KLD");
2736 if (section_list->FindSectionByName(g_kld_section_name))
2737 return eStrataKernel;
2738 }
2739 }
2740 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00002741
2742 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
2743 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00002744 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00002745 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
2746 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
2747 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
2748 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
2749 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
2750 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
2751 default:
2752 break;
2753 }
2754 return eStrataUnknown;
2755}
2756
2757
Greg Clayton49f4bf22012-02-22 19:41:02 +00002758uint32_t
2759ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
2760{
Greg Clayton9482f052012-03-13 23:14:29 +00002761 ModuleSP module_sp(GetModule());
2762 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00002763 {
Greg Clayton9482f052012-03-13 23:14:29 +00002764 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2765 struct dylib_command load_cmd;
2766 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
2767 uint32_t version_cmd = 0;
2768 uint64_t version = 0;
2769 uint32_t i;
2770 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00002771 {
Greg Clayton9482f052012-03-13 23:14:29 +00002772 const uint32_t cmd_offset = offset;
2773 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
2774 break;
2775
2776 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00002777 {
Greg Clayton9482f052012-03-13 23:14:29 +00002778 if (version_cmd == 0)
2779 {
2780 version_cmd = load_cmd.cmd;
2781 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
2782 break;
2783 version = load_cmd.dylib.current_version;
2784 }
2785 break; // Break for now unless there is another more complete version
2786 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00002787 }
Greg Clayton9482f052012-03-13 23:14:29 +00002788 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00002789 }
Greg Clayton9482f052012-03-13 23:14:29 +00002790
2791 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00002792 {
Greg Clayton9482f052012-03-13 23:14:29 +00002793 if (versions != NULL && num_versions > 0)
2794 {
2795 if (num_versions > 0)
2796 versions[0] = (version & 0xFFFF0000ull) >> 16;
2797 if (num_versions > 1)
2798 versions[1] = (version & 0x0000FF00ull) >> 8;
2799 if (num_versions > 2)
2800 versions[2] = (version & 0x000000FFull);
2801 // Fill in an remaining version numbers with invalid values
2802 for (i=3; i<num_versions; ++i)
2803 versions[i] = UINT32_MAX;
2804 }
2805 // The LC_ID_DYLIB load command has a version with 3 version numbers
2806 // in it, so always return 3
2807 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00002808 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00002809 }
2810 return false;
2811}
2812
Chris Lattner24943d22010-06-08 16:52:24 +00002813bool
Greg Clayton395fc332011-02-15 21:59:32 +00002814ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00002815{
Greg Clayton9482f052012-03-13 23:14:29 +00002816 ModuleSP module_sp(GetModule());
2817 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00002818 {
Greg Clayton9482f052012-03-13 23:14:29 +00002819 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
2820 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
2821
2822 // Files with type MH_PRELOAD are currently used in cases where the image
2823 // debugs at the addresses in the file itself. Below we set the OS to
2824 // unknown to make sure we use the DynamicLoaderStatic()...
2825 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
2826 {
2827 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
2828 }
2829 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00002830 }
Greg Clayton9482f052012-03-13 23:14:29 +00002831 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00002832}
2833
2834
2835//------------------------------------------------------------------
2836// PluginInterface protocol
2837//------------------------------------------------------------------
2838const char *
2839ObjectFileMachO::GetPluginName()
2840{
2841 return "ObjectFileMachO";
2842}
2843
2844const char *
2845ObjectFileMachO::GetShortPluginName()
2846{
2847 return GetPluginNameStatic();
2848}
2849
2850uint32_t
2851ObjectFileMachO::GetPluginVersion()
2852{
2853 return 1;
2854}
2855