blob: 3e6cd1efbb8b86391e43d8b4901a636096546693 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ObjectFileMachO.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Greg Clayton3f69eac2011-12-03 02:30:59 +000010#include "llvm/ADT/StringRef.h"
Jim Ingham28775942011-03-07 23:44:08 +000011#include "llvm/Support/MachO.h"
12
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "ObjectFileMachO.h"
14
Greg Claytond4330e62012-09-05 01:38:55 +000015#include "lldb/lldb-private-log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Core/ArchSpec.h"
17#include "lldb/Core/DataBuffer.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/FileSpecList.h"
Greg Claytond4330e62012-09-05 01:38:55 +000019#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Core/Module.h"
21#include "lldb/Core/PluginManager.h"
Greg Clayton6f7f8da2012-04-24 03:06:13 +000022#include "lldb/Core/RangeMap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/Section.h"
24#include "lldb/Core/StreamFile.h"
25#include "lldb/Core/StreamString.h"
26#include "lldb/Core/Timer.h"
27#include "lldb/Core/UUID.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000028#include "lldb/Host/Host.h"
29#include "lldb/Host/FileSpec.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000030#include "lldb/Symbol/ClangNamespaceDecl.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Symbol/ObjectFile.h"
Greg Clayton29021d32012-04-18 05:19:20 +000032#include "lldb/Target/Platform.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000033#include "lldb/Target/Process.h"
Greg Clayton29021d32012-04-18 05:19:20 +000034#include "lldb/Target/Target.h"
Greg Clayton9ce95382012-02-13 23:10:39 +000035#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
36#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Clayton46c9a352012-02-09 06:16:32 +000037#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner24943d22010-06-08 16:52:24 +000038
Chris Lattner24943d22010-06-08 16:52:24 +000039using namespace lldb;
40using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000041using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000042
Greg Clayton46c9a352012-02-09 06:16:32 +000043class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
44{
45public:
46 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
47 RegisterContextDarwin_x86_64 (thread, 0)
48 {
49 SetRegisterDataFrom_LC_THREAD (data);
50 }
51
52 virtual void
53 InvalidateAllRegisters ()
54 {
55 // Do nothing... registers are always valid...
56 }
57
58 void
59 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
60 {
Greg Clayton46c9a352012-02-09 06:16:32 +000061 uint32_t offset = 0;
62 SetError (GPRRegSet, Read, -1);
63 SetError (FPURegSet, Read, -1);
64 SetError (EXCRegSet, Read, -1);
Greg Clayton9ce95382012-02-13 23:10:39 +000065 bool done = false;
66
67 while (!done)
Greg Clayton46c9a352012-02-09 06:16:32 +000068 {
Greg Clayton9ce95382012-02-13 23:10:39 +000069 int flavor = data.GetU32 (&offset);
70 if (flavor == 0)
71 done = true;
72 else
Greg Clayton46c9a352012-02-09 06:16:32 +000073 {
Greg Clayton9ce95382012-02-13 23:10:39 +000074 uint32_t i;
75 uint32_t count = data.GetU32 (&offset);
76 switch (flavor)
77 {
78 case GPRRegSet:
79 for (i=0; i<count; ++i)
80 (&gpr.rax)[i] = data.GetU64(&offset);
81 SetError (GPRRegSet, Read, 0);
82 done = true;
83
84 break;
85 case FPURegSet:
86 // TODO: fill in FPU regs....
87 //SetError (FPURegSet, Read, -1);
88 done = true;
89
90 break;
91 case EXCRegSet:
92 exc.trapno = data.GetU32(&offset);
93 exc.err = data.GetU32(&offset);
94 exc.faultvaddr = data.GetU64(&offset);
95 SetError (EXCRegSet, Read, 0);
96 done = true;
97 break;
98 case 7:
99 case 8:
100 case 9:
101 // fancy flavors that encapsulate of the the above
102 // falvors...
103 break;
104
105 default:
106 done = true;
107 break;
108 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000109 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000110 }
111 }
112protected:
113 virtual int
114 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
115 {
116 return 0;
117 }
118
119 virtual int
120 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
121 {
122 return 0;
123 }
124
125 virtual int
126 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
127 {
128 return 0;
129 }
130
131 virtual int
132 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
133 {
134 return 0;
135 }
136
137 virtual int
138 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
139 {
140 return 0;
141 }
142
143 virtual int
144 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
145 {
146 return 0;
147 }
148};
Greg Clayton46c9a352012-02-09 06:16:32 +0000149
Greg Clayton9ce95382012-02-13 23:10:39 +0000150
151class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
152{
153public:
154 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
155 RegisterContextDarwin_i386 (thread, 0)
156 {
157 SetRegisterDataFrom_LC_THREAD (data);
158 }
159
160 virtual void
161 InvalidateAllRegisters ()
162 {
163 // Do nothing... registers are always valid...
164 }
165
166 void
167 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
168 {
169 uint32_t offset = 0;
170 SetError (GPRRegSet, Read, -1);
171 SetError (FPURegSet, Read, -1);
172 SetError (EXCRegSet, Read, -1);
173 bool done = false;
174
175 while (!done)
176 {
177 int flavor = data.GetU32 (&offset);
178 if (flavor == 0)
179 done = true;
180 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000181 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000182 uint32_t i;
183 uint32_t count = data.GetU32 (&offset);
184 switch (flavor)
185 {
186 case GPRRegSet:
187 for (i=0; i<count; ++i)
188 (&gpr.eax)[i] = data.GetU32(&offset);
189 SetError (GPRRegSet, Read, 0);
190 done = true;
191
192 break;
193 case FPURegSet:
194 // TODO: fill in FPU regs....
195 //SetError (FPURegSet, Read, -1);
196 done = true;
197
198 break;
199 case EXCRegSet:
200 exc.trapno = data.GetU32(&offset);
201 exc.err = data.GetU32(&offset);
202 exc.faultvaddr = data.GetU32(&offset);
203 SetError (EXCRegSet, Read, 0);
204 done = true;
205 break;
206 case 7:
207 case 8:
208 case 9:
209 // fancy flavors that encapsulate of the the above
210 // falvors...
211 break;
212
213 default:
214 done = true;
215 break;
216 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000217 }
218 }
219 }
220protected:
221 virtual int
222 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
223 {
224 return 0;
225 }
226
227 virtual int
228 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
229 {
230 return 0;
231 }
232
233 virtual int
234 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
235 {
236 return 0;
237 }
238
239 virtual int
240 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
241 {
242 return 0;
243 }
244
245 virtual int
246 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
247 {
248 return 0;
249 }
250
251 virtual int
252 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
253 {
254 return 0;
255 }
256};
257
Greg Clayton9ce95382012-02-13 23:10:39 +0000258class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
259{
260public:
261 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonb5431d02012-10-30 23:57:32 +0000262 RegisterContextDarwin_arm (thread, 0)
Greg Clayton9ce95382012-02-13 23:10:39 +0000263 {
264 SetRegisterDataFrom_LC_THREAD (data);
265 }
266
267 virtual void
268 InvalidateAllRegisters ()
269 {
270 // Do nothing... registers are always valid...
271 }
272
273 void
274 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
275 {
276 uint32_t offset = 0;
277 SetError (GPRRegSet, Read, -1);
278 SetError (FPURegSet, Read, -1);
279 SetError (EXCRegSet, Read, -1);
280 int flavor = data.GetU32 (&offset);
281 uint32_t count = data.GetU32 (&offset);
282 switch (flavor)
283 {
284 case GPRRegSet:
285 for (uint32_t i=0; i<count; ++i)
286 gpr.r[i] = data.GetU32(&offset);
287 SetError (GPRRegSet, Read, 0);
288 break;
289 case FPURegSet:
290 // TODO: fill in FPU regs....
291 //SetError (FPURegSet, Read, -1);
292 break;
293 case EXCRegSet:
294 exc.exception = data.GetU32(&offset);
295 exc.fsr = data.GetU32(&offset);
296 exc.far = data.GetU32(&offset);
297 SetError (EXCRegSet, Read, 0);
298 break;
299 }
300 }
301protected:
302 virtual int
303 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
304 {
305 return 0;
306 }
307
308 virtual int
309 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
310 {
311 return 0;
312 }
313
314 virtual int
315 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
316 {
317 return 0;
318 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000319
320 virtual int
321 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
322 {
323 return -1;
324 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000325
326 virtual int
327 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
328 {
329 return 0;
330 }
331
332 virtual int
333 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
334 {
335 return 0;
336 }
337
338 virtual int
339 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
340 {
341 return 0;
342 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000343
344 virtual int
345 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
346 {
347 return -1;
348 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000349};
350
Greg Claytonb1888f22011-03-19 01:12:21 +0000351#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000352
353void
354ObjectFileMachO::Initialize()
355{
356 PluginManager::RegisterPlugin (GetPluginNameStatic(),
357 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000358 CreateInstance,
359 CreateMemoryInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000360}
361
362void
363ObjectFileMachO::Terminate()
364{
365 PluginManager::UnregisterPlugin (CreateInstance);
366}
367
368
369const char *
370ObjectFileMachO::GetPluginNameStatic()
371{
372 return "object-file.mach-o";
373}
374
375const char *
376ObjectFileMachO::GetPluginDescriptionStatic()
377{
378 return "Mach-o object file reader (32 and 64 bit)";
379}
380
381
382ObjectFile *
Greg Clayton3508c382012-02-24 01:59:29 +0000383ObjectFileMachO::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 +0000384{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000385 if (ObjectFileMachO::MagicBytesMatch(data_sp, offset, length))
Chris Lattner24943d22010-06-08 16:52:24 +0000386 {
Greg Clayton3508c382012-02-24 01:59:29 +0000387 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, file, offset, length));
Chris Lattner24943d22010-06-08 16:52:24 +0000388 if (objfile_ap.get() && objfile_ap->ParseHeader())
389 return objfile_ap.release();
390 }
391 return NULL;
392}
393
Greg Claytonb5a8f142012-02-05 02:38:54 +0000394ObjectFile *
Greg Clayton3508c382012-02-24 01:59:29 +0000395ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000396 DataBufferSP& data_sp,
397 const ProcessSP &process_sp,
398 lldb::addr_t header_addr)
399{
400 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
401 {
Greg Clayton3508c382012-02-24 01:59:29 +0000402 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000403 if (objfile_ap.get() && objfile_ap->ParseHeader())
404 return objfile_ap.release();
405 }
406 return NULL;
407}
408
409
410const ConstString &
411ObjectFileMachO::GetSegmentNameTEXT()
412{
413 static ConstString g_segment_name_TEXT ("__TEXT");
414 return g_segment_name_TEXT;
415}
416
417const ConstString &
418ObjectFileMachO::GetSegmentNameDATA()
419{
420 static ConstString g_segment_name_DATA ("__DATA");
421 return g_segment_name_DATA;
422}
423
424const ConstString &
425ObjectFileMachO::GetSegmentNameOBJC()
426{
427 static ConstString g_segment_name_OBJC ("__OBJC");
428 return g_segment_name_OBJC;
429}
430
431const ConstString &
432ObjectFileMachO::GetSegmentNameLINKEDIT()
433{
434 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
435 return g_section_name_LINKEDIT;
436}
437
438const ConstString &
439ObjectFileMachO::GetSectionNameEHFrame()
440{
441 static ConstString g_section_name_eh_frame ("__eh_frame");
442 return g_section_name_eh_frame;
443}
444
445
Chris Lattner24943d22010-06-08 16:52:24 +0000446
447static uint32_t
448MachHeaderSizeFromMagic(uint32_t magic)
449{
450 switch (magic)
451 {
Greg Clayton1674b122010-07-21 22:12:05 +0000452 case HeaderMagic32:
453 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000454 return sizeof(struct mach_header);
455
Greg Clayton1674b122010-07-21 22:12:05 +0000456 case HeaderMagic64:
457 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000458 return sizeof(struct mach_header_64);
459 break;
460
461 default:
462 break;
463 }
464 return 0;
465}
466
467
468bool
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000469ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
470 lldb::addr_t data_offset,
471 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000472{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000473 DataExtractor data;
474 data.SetData (data_sp, data_offset, data_length);
Chris Lattner24943d22010-06-08 16:52:24 +0000475 uint32_t offset = 0;
476 uint32_t magic = data.GetU32(&offset);
477 return MachHeaderSizeFromMagic(magic) != 0;
478}
479
480
Greg Clayton3508c382012-02-24 01:59:29 +0000481ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp, DataBufferSP& data_sp, const FileSpec* file, addr_t offset, addr_t length) :
482 ObjectFile(module_sp, file, offset, length, data_sp),
Chris Lattner24943d22010-06-08 16:52:24 +0000483 m_sections_ap(),
Jim Ingham28775942011-03-07 23:44:08 +0000484 m_symtab_ap(),
Greg Clayton46c9a352012-02-09 06:16:32 +0000485 m_mach_segments(),
486 m_mach_sections(),
487 m_entry_point_address(),
488 m_thread_context_offsets(),
489 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000490{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000491 ::memset (&m_header, 0, sizeof(m_header));
492 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000493}
494
Greg Clayton3508c382012-02-24 01:59:29 +0000495ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000496 lldb::DataBufferSP& header_data_sp,
497 const lldb::ProcessSP &process_sp,
498 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000499 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000500 m_sections_ap(),
501 m_symtab_ap(),
Greg Clayton46c9a352012-02-09 06:16:32 +0000502 m_mach_segments(),
503 m_mach_sections(),
504 m_entry_point_address(),
505 m_thread_context_offsets(),
506 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000507{
508 ::memset (&m_header, 0, sizeof(m_header));
509 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
510}
Chris Lattner24943d22010-06-08 16:52:24 +0000511
512ObjectFileMachO::~ObjectFileMachO()
513{
514}
515
516
517bool
518ObjectFileMachO::ParseHeader ()
519{
Greg Clayton9482f052012-03-13 23:14:29 +0000520 ModuleSP module_sp(GetModule());
521 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000522 {
Greg Clayton9482f052012-03-13 23:14:29 +0000523 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
524 bool can_parse = false;
525 uint32_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000526 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000527 // Leave magic in the original byte order
528 m_header.magic = m_data.GetU32(&offset);
529 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000530 {
Greg Clayton9482f052012-03-13 23:14:29 +0000531 case HeaderMagic32:
532 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
533 m_data.SetAddressByteSize(4);
534 can_parse = true;
535 break;
536
537 case HeaderMagic64:
538 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
539 m_data.SetAddressByteSize(8);
540 can_parse = true;
541 break;
542
543 case HeaderMagic32Swapped:
544 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
545 m_data.SetAddressByteSize(4);
546 can_parse = true;
547 break;
548
549 case HeaderMagic64Swapped:
550 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
551 m_data.SetAddressByteSize(8);
552 can_parse = true;
553 break;
554
555 default:
556 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000557 }
Greg Clayton9482f052012-03-13 23:14:29 +0000558
559 if (can_parse)
560 {
561 m_data.GetU32(&offset, &m_header.cputype, 6);
562
563 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
564
Greg Clayton21a25432012-11-16 21:36:10 +0000565 // Check if the module has a required architecture
566 const ArchSpec &module_arch = module_sp->GetArchitecture();
567 if (module_arch.IsValid() && !module_arch.IsExactMatch(mach_arch))
568 return false;
569
Greg Clayton9482f052012-03-13 23:14:29 +0000570 if (SetModulesArchitecture (mach_arch))
571 {
572 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
573 if (m_data.GetByteSize() < header_and_lc_size)
574 {
575 DataBufferSP data_sp;
576 ProcessSP process_sp (m_process_wp.lock());
577 if (process_sp)
578 {
579 data_sp = ReadMemory (process_sp, m_offset, header_and_lc_size);
580 }
581 else
582 {
583 // Read in all only the load command data from the file on disk
584 data_sp = m_file.ReadFileContents(m_offset, header_and_lc_size);
585 if (data_sp->GetByteSize() != header_and_lc_size)
586 return false;
587 }
588 if (data_sp)
589 m_data.SetData (data_sp);
590 }
591 }
592 return true;
593 }
594 else
595 {
596 memset(&m_header, 0, sizeof(struct mach_header));
597 }
Chris Lattner24943d22010-06-08 16:52:24 +0000598 }
599 return false;
600}
601
602
603ByteOrder
604ObjectFileMachO::GetByteOrder () const
605{
Chris Lattner24943d22010-06-08 16:52:24 +0000606 return m_data.GetByteOrder ();
607}
608
Jim Ingham7508e732010-08-09 23:31:02 +0000609bool
610ObjectFileMachO::IsExecutable() const
611{
612 return m_header.filetype == HeaderFileTypeExecutable;
613}
Chris Lattner24943d22010-06-08 16:52:24 +0000614
615size_t
616ObjectFileMachO::GetAddressByteSize () const
617{
Chris Lattner24943d22010-06-08 16:52:24 +0000618 return m_data.GetAddressByteSize ();
619}
620
Greg Claytonb3448432011-03-24 21:19:54 +0000621AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000622ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
623{
624 Symtab *symtab = GetSymtab();
625 if (symtab)
626 {
627 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
628 if (symbol)
629 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000630 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000631 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000632 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000633 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000634 {
Greg Clayton3508c382012-02-24 01:59:29 +0000635 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000636 switch (section_type)
637 {
638 case eSectionTypeInvalid: return eAddressClassUnknown;
639 case eSectionTypeCode:
640 if (m_header.cputype == llvm::MachO::CPUTypeARM)
641 {
642 // For ARM we have a bit in the n_desc field of the symbol
643 // that tells us ARM/Thumb which is bit 0x0008.
644 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
645 return eAddressClassCodeAlternateISA;
646 }
647 return eAddressClassCode;
648
649 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000650 case eSectionTypeData:
651 case eSectionTypeDataCString:
652 case eSectionTypeDataCStringPointers:
653 case eSectionTypeDataSymbolAddress:
654 case eSectionTypeData4:
655 case eSectionTypeData8:
656 case eSectionTypeData16:
657 case eSectionTypeDataPointers:
658 case eSectionTypeZeroFill:
659 case eSectionTypeDataObjCMessageRefs:
660 case eSectionTypeDataObjCCFStrings:
661 return eAddressClassData;
662 case eSectionTypeDebug:
663 case eSectionTypeDWARFDebugAbbrev:
664 case eSectionTypeDWARFDebugAranges:
665 case eSectionTypeDWARFDebugFrame:
666 case eSectionTypeDWARFDebugInfo:
667 case eSectionTypeDWARFDebugLine:
668 case eSectionTypeDWARFDebugLoc:
669 case eSectionTypeDWARFDebugMacInfo:
670 case eSectionTypeDWARFDebugPubNames:
671 case eSectionTypeDWARFDebugPubTypes:
672 case eSectionTypeDWARFDebugRanges:
673 case eSectionTypeDWARFDebugStr:
674 case eSectionTypeDWARFAppleNames:
675 case eSectionTypeDWARFAppleTypes:
676 case eSectionTypeDWARFAppleNamespaces:
677 case eSectionTypeDWARFAppleObjC:
678 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000679 case eSectionTypeEHFrame: return eAddressClassRuntime;
680 case eSectionTypeOther: return eAddressClassUnknown;
681 }
682 }
683 }
684
Greg Claytonb3448432011-03-24 21:19:54 +0000685 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000686 switch (symbol_type)
687 {
688 case eSymbolTypeAny: return eAddressClassUnknown;
689 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Greg Claytonb1888f22011-03-19 01:12:21 +0000690
691 case eSymbolTypeCode:
692 case eSymbolTypeTrampoline:
693 if (m_header.cputype == llvm::MachO::CPUTypeARM)
694 {
695 // For ARM we have a bit in the n_desc field of the symbol
696 // that tells us ARM/Thumb which is bit 0x0008.
697 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
698 return eAddressClassCodeAlternateISA;
699 }
700 return eAddressClassCode;
701
702 case eSymbolTypeData: return eAddressClassData;
703 case eSymbolTypeRuntime: return eAddressClassRuntime;
704 case eSymbolTypeException: return eAddressClassRuntime;
705 case eSymbolTypeSourceFile: return eAddressClassDebug;
706 case eSymbolTypeHeaderFile: return eAddressClassDebug;
707 case eSymbolTypeObjectFile: return eAddressClassDebug;
708 case eSymbolTypeCommonBlock: return eAddressClassDebug;
709 case eSymbolTypeBlock: return eAddressClassDebug;
710 case eSymbolTypeLocal: return eAddressClassData;
711 case eSymbolTypeParam: return eAddressClassData;
712 case eSymbolTypeVariable: return eAddressClassData;
713 case eSymbolTypeVariableType: return eAddressClassDebug;
714 case eSymbolTypeLineEntry: return eAddressClassDebug;
715 case eSymbolTypeLineHeader: return eAddressClassDebug;
716 case eSymbolTypeScopeBegin: return eAddressClassDebug;
717 case eSymbolTypeScopeEnd: return eAddressClassDebug;
718 case eSymbolTypeAdditional: return eAddressClassUnknown;
719 case eSymbolTypeCompiler: return eAddressClassDebug;
720 case eSymbolTypeInstrumentation:return eAddressClassDebug;
721 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000722 case eSymbolTypeObjCClass: return eAddressClassRuntime;
723 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
724 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000725 }
726 }
727 }
728 return eAddressClassUnknown;
729}
Chris Lattner24943d22010-06-08 16:52:24 +0000730
731Symtab *
732ObjectFileMachO::GetSymtab()
733{
Greg Clayton9482f052012-03-13 23:14:29 +0000734 ModuleSP module_sp(GetModule());
735 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000736 {
Greg Clayton9482f052012-03-13 23:14:29 +0000737 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
738 if (m_symtab_ap.get() == NULL)
739 {
740 m_symtab_ap.reset(new Symtab(this));
741 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
742 ParseSymtab (true);
743 m_symtab_ap->Finalize ();
744 }
Chris Lattner24943d22010-06-08 16:52:24 +0000745 }
746 return m_symtab_ap.get();
747}
748
749
750SectionList *
751ObjectFileMachO::GetSectionList()
752{
Greg Clayton9482f052012-03-13 23:14:29 +0000753 ModuleSP module_sp(GetModule());
754 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000755 {
Greg Clayton9482f052012-03-13 23:14:29 +0000756 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
757 if (m_sections_ap.get() == NULL)
758 {
759 m_sections_ap.reset(new SectionList());
760 ParseSections();
761 }
Chris Lattner24943d22010-06-08 16:52:24 +0000762 }
763 return m_sections_ap.get();
764}
765
766
767size_t
768ObjectFileMachO::ParseSections ()
769{
770 lldb::user_id_t segID = 0;
771 lldb::user_id_t sectID = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000772 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
773 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000774 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000775 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000776 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000777 // First look up any LC_ENCRYPTION_INFO load commands
778 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
779 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000780 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000781 for (i=0; i<m_header.ncmds; ++i)
782 {
783 const uint32_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000784 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000785 break;
786
Greg Clayton54e33712012-05-25 18:09:55 +0000787 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000788 {
Greg Clayton54e33712012-05-25 18:09:55 +0000789 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
790 {
791 if (encryption_cmd.cryptid != 0)
792 {
793 EncryptedFileRanges::Entry entry;
794 entry.SetRangeBase(encryption_cmd.cryptoff);
795 entry.SetByteSize(encryption_cmd.cryptsize);
796 encrypted_file_ranges.Append(entry);
797 }
798 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000799 }
Greg Clayton54e33712012-05-25 18:09:55 +0000800 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000801 }
802
803 offset = MachHeaderSizeFromMagic(m_header.magic);
804
Greg Clayton54e33712012-05-25 18:09:55 +0000805 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000806 for (i=0; i<m_header.ncmds; ++i)
807 {
808 const uint32_t load_cmd_offset = offset;
809 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
810 break;
811
Greg Clayton1674b122010-07-21 22:12:05 +0000812 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000813 {
814 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
815 {
816 load_cmd.vmaddr = m_data.GetAddress(&offset);
817 load_cmd.vmsize = m_data.GetAddress(&offset);
818 load_cmd.fileoff = m_data.GetAddress(&offset);
819 load_cmd.filesize = m_data.GetAddress(&offset);
820 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
821 {
Greg Clayton68ca8232011-01-25 02:58:48 +0000822
823 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
824
Chris Lattner24943d22010-06-08 16:52:24 +0000825 // Keep a list of mach segments around in case we need to
826 // get at data that isn't stored in the abstracted Sections.
827 m_mach_segments.push_back (load_cmd);
828
829 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
830 // Use a segment ID of the segment index shifted left by 8 so they
831 // never conflict with any of the sections.
832 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000833 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000834 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000835 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000836 ++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
837 segment_name, // Name of this section
838 eSectionTypeContainer, // This section is a container of other sections.
839 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
840 load_cmd.vmsize, // VM size in bytes of this section
841 load_cmd.fileoff, // Offset to the data for this section in the file
842 load_cmd.filesize, // Size in bytes of this section as found in the the file
843 load_cmd.flags)); // Flags for this section
844
Greg Clayton68ca8232011-01-25 02:58:48 +0000845 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000846 m_sections_ap->AddSection(segment_sp);
847 }
848
849 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000850 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000851 // Push a section into our mach sections for the section at
Greg Clayton6af4fad2010-10-06 01:26:32 +0000852 // index zero (NListSectionNoSection) if we don't have any
853 // mach sections yet...
854 if (m_mach_sections.empty())
855 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000856 uint32_t segment_sect_idx;
857 const lldb::user_id_t first_segment_sectID = sectID + 1;
858
859
Greg Clayton1674b122010-07-21 22:12:05 +0000860 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000861 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
862 {
863 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
864 break;
865 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
866 break;
867 sect64.addr = m_data.GetAddress(&offset);
868 sect64.size = m_data.GetAddress(&offset);
869
870 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
871 break;
872
873 // Keep a list of mach sections around in case we need to
874 // get at data that isn't stored in the abstracted Sections.
875 m_mach_sections.push_back (sect64);
876
877 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
878 if (!segment_name)
879 {
880 // We have a segment with no name so we need to conjure up
881 // segments that correspond to the section's segname if there
882 // isn't already such a section. If there is such a section,
883 // we resize the section so that it spans all sections.
884 // We also mark these sections as fake so address matches don't
885 // hit if they land in the gaps between the child sections.
886 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
887 segment_sp = m_sections_ap->FindSectionByName (segment_name);
888 if (segment_sp.get())
889 {
890 Section *segment = segment_sp.get();
891 // Grow the section size as needed.
892 const lldb::addr_t sect64_min_addr = sect64.addr;
893 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
894 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
895 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
896 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
897 if (sect64_min_addr >= curr_seg_min_addr)
898 {
899 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
900 // Only grow the section size if needed
901 if (new_seg_byte_size > curr_seg_byte_size)
902 segment->SetByteSize (new_seg_byte_size);
903 }
904 else
905 {
906 // We need to change the base address of the segment and
907 // adjust the child section offsets for all existing children.
908 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
909 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +0000910 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000911 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
912 }
Greg Clayton661825b2010-06-28 23:51:11 +0000913
914 // Grow the section size as needed.
915 if (sect64.offset)
916 {
917 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
918 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
919
920 const lldb::addr_t section_min_file_offset = sect64.offset;
921 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
922 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
923 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
924 segment->SetFileOffset (new_file_offset);
925 segment->SetFileSize (new_file_size);
926 }
Chris Lattner24943d22010-06-08 16:52:24 +0000927 }
928 else
929 {
930 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +0000931 segment_sp.reset(new Section (segment_sp, // Parent section
932 module_sp, // Module to which this section belongs
933 ++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
934 segment_name, // Name of this section
935 eSectionTypeContainer, // This section is a container of other sections.
936 sect64.addr, // File VM address == addresses as they are found in the object file
937 sect64.size, // VM size in bytes of this section
938 sect64.offset, // Offset to the data for this section in the file
939 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
940 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +0000941 segment_sp->SetIsFake(true);
942 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000943 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000944 }
945 }
946 assert (segment_sp.get());
947
Greg Clayton1674b122010-07-21 22:12:05 +0000948 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000949 static ConstString g_sect_name_objc_data ("__objc_data");
950 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
951 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
952 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
953 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
954 static ConstString g_sect_name_objc_const ("__objc_const");
955 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
956 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000957
958 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
959 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
960 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
961 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
962 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
963 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
964 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
965 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
966 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
967 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
968 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +0000969 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
970 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +0000971 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +0000972 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000973 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +0000974 static ConstString g_sect_name_DATA ("__DATA");
975 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000976
Chris Lattner24943d22010-06-08 16:52:24 +0000977 SectionType sect_type = eSectionTypeOther;
978
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000979 if (section_name == g_sect_name_dwarf_debug_abbrev)
980 sect_type = eSectionTypeDWARFDebugAbbrev;
981 else if (section_name == g_sect_name_dwarf_debug_aranges)
982 sect_type = eSectionTypeDWARFDebugAranges;
983 else if (section_name == g_sect_name_dwarf_debug_frame)
984 sect_type = eSectionTypeDWARFDebugFrame;
985 else if (section_name == g_sect_name_dwarf_debug_info)
986 sect_type = eSectionTypeDWARFDebugInfo;
987 else if (section_name == g_sect_name_dwarf_debug_line)
988 sect_type = eSectionTypeDWARFDebugLine;
989 else if (section_name == g_sect_name_dwarf_debug_loc)
990 sect_type = eSectionTypeDWARFDebugLoc;
991 else if (section_name == g_sect_name_dwarf_debug_macinfo)
992 sect_type = eSectionTypeDWARFDebugMacInfo;
993 else if (section_name == g_sect_name_dwarf_debug_pubnames)
994 sect_type = eSectionTypeDWARFDebugPubNames;
995 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
996 sect_type = eSectionTypeDWARFDebugPubTypes;
997 else if (section_name == g_sect_name_dwarf_debug_ranges)
998 sect_type = eSectionTypeDWARFDebugRanges;
999 else if (section_name == g_sect_name_dwarf_debug_str)
1000 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001001 else if (section_name == g_sect_name_dwarf_apple_names)
1002 sect_type = eSectionTypeDWARFAppleNames;
1003 else if (section_name == g_sect_name_dwarf_apple_types)
1004 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001005 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1006 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001007 else if (section_name == g_sect_name_dwarf_apple_objc)
1008 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001009 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001010 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001011 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001012 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001013 else if (section_name == g_sect_name_eh_frame)
1014 sect_type = eSectionTypeEHFrame;
1015 else if (section_name == g_sect_name_cfstring)
1016 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001017 else if (section_name == g_sect_name_objc_data ||
1018 section_name == g_sect_name_objc_classrefs ||
1019 section_name == g_sect_name_objc_superrefs ||
1020 section_name == g_sect_name_objc_const ||
1021 section_name == g_sect_name_objc_classlist)
1022 {
1023 sect_type = eSectionTypeDataPointers;
1024 }
Chris Lattner24943d22010-06-08 16:52:24 +00001025
1026 if (sect_type == eSectionTypeOther)
1027 {
1028 switch (mach_sect_type)
1029 {
1030 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001031 case SectionTypeRegular:
1032 if (segment_sp->GetName() == g_sect_name_TEXT)
1033 sect_type = eSectionTypeCode;
1034 else if (segment_sp->GetName() == g_sect_name_DATA)
1035 sect_type = eSectionTypeData;
1036 else
1037 sect_type = eSectionTypeOther;
1038 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001039 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1040 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1041 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1042 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1043 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1044 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1045 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1046 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1047 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1048 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1049 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1050 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1051 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1052 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1053 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1054 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001055 default: break;
1056 }
1057 }
1058
Greg Clayton3508c382012-02-24 01:59:29 +00001059 SectionSP section_sp(new Section (segment_sp,
1060 module_sp,
1061 ++sectID,
1062 section_name,
1063 sect_type,
1064 sect64.addr - segment_sp->GetFileAddress(),
1065 sect64.size,
1066 sect64.offset,
1067 sect64.offset == 0 ? 0 : sect64.size,
1068 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001069 // Set the section to be encrypted to match the segment
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001070
1071 bool section_is_encrypted = false;
1072 if (!segment_is_encrypted && load_cmd.filesize != 0)
1073 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001074
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001075 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001076 segment_sp->GetChildren().AddSection(section_sp);
1077
1078 if (segment_sp->IsFake())
1079 {
1080 segment_sp.reset();
1081 segment_name.Clear();
1082 }
1083 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001084 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001085 {
1086 if (first_segment_sectID <= sectID)
1087 {
1088 lldb::user_id_t sect_uid;
1089 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1090 {
1091 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1092 SectionSP next_section_sp;
1093 if (sect_uid + 1 <= sectID)
1094 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1095
1096 if (curr_section_sp.get())
1097 {
1098 if (curr_section_sp->GetByteSize() == 0)
1099 {
1100 if (next_section_sp.get() != NULL)
1101 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1102 else
1103 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1104 }
1105 }
1106 }
1107 }
1108 }
1109 }
1110 }
1111 }
Greg Clayton1674b122010-07-21 22:12:05 +00001112 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001113 {
1114 m_dysymtab.cmd = load_cmd.cmd;
1115 m_dysymtab.cmdsize = load_cmd.cmdsize;
1116 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1117 }
1118
1119 offset = load_cmd_offset + load_cmd.cmdsize;
1120 }
1121// if (dump_sections)
1122// {
1123// StreamFile s(stdout);
1124// m_sections_ap->Dump(&s, true);
1125// }
1126 return sectID; // Return the number of sections we registered with the module
1127}
1128
1129class MachSymtabSectionInfo
1130{
1131public:
1132
1133 MachSymtabSectionInfo (SectionList *section_list) :
1134 m_section_list (section_list),
1135 m_section_infos()
1136 {
1137 // Get the number of sections down to a depth of 1 to include
1138 // all segments and their sections, but no other sections that
1139 // may be added for debug map or
1140 m_section_infos.resize(section_list->GetNumSections(1));
1141 }
1142
1143
Greg Clayton3508c382012-02-24 01:59:29 +00001144 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001145 GetSection (uint8_t n_sect, addr_t file_addr)
1146 {
1147 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001148 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001149 if (n_sect < m_section_infos.size())
1150 {
Greg Clayton3508c382012-02-24 01:59:29 +00001151 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001152 {
Greg Clayton3508c382012-02-24 01:59:29 +00001153 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1154 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001155 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001156 {
Greg Clayton3508c382012-02-24 01:59:29 +00001157 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1158 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001159 }
1160 else
1161 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001162 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001163 }
Chris Lattner24943d22010-06-08 16:52:24 +00001164 }
1165 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001166 {
1167 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001168 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001169 }
1170 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1171 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1172 {
1173 // Symbol is in section with zero size, but has the same start
1174 // address as the section. This can happen with linker symbols
1175 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001176 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001177 }
Chris Lattner24943d22010-06-08 16:52:24 +00001178 }
Greg Clayton3508c382012-02-24 01:59:29 +00001179 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001180 }
1181
1182protected:
1183 struct SectionInfo
1184 {
1185 SectionInfo () :
1186 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001187 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001188 {
1189 }
1190
1191 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001192 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001193 };
1194 SectionList *m_section_list;
1195 std::vector<SectionInfo> m_section_infos;
1196};
1197
Chris Lattner24943d22010-06-08 16:52:24 +00001198size_t
1199ObjectFileMachO::ParseSymtab (bool minimize)
1200{
1201 Timer scoped_timer(__PRETTY_FUNCTION__,
1202 "ObjectFileMachO::ParseSymtab () module = %s",
1203 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001204 ModuleSP module_sp (GetModule());
1205 if (!module_sp)
1206 return 0;
1207
1208 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1209 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1210 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1211 FunctionStarts function_starts;
Chris Lattner24943d22010-06-08 16:52:24 +00001212 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1213 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001214
Greg Clayton0fea0512011-12-30 00:32:24 +00001215 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
1216
Chris Lattner24943d22010-06-08 16:52:24 +00001217 for (i=0; i<m_header.ncmds; ++i)
1218 {
1219 const uint32_t cmd_offset = offset;
1220 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001221 struct load_command lc;
1222 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001223 break;
1224 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001225 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001226 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001227 case LoadCommandSymtab:
1228 symtab_load_command.cmd = lc.cmd;
1229 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001230 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001231 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1232 return 0;
1233 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001234 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001235 if (log)
1236 module_sp->LogMessage(log.get(), "LC_SYMTAB.symoff == 0");
1237 return 0;
1238 }
1239
1240 if (symtab_load_command.stroff == 0)
1241 {
1242 if (log)
1243 module_sp->LogMessage(log.get(), "LC_SYMTAB.stroff == 0");
1244 return 0;
1245 }
1246
1247 if (symtab_load_command.nsyms == 0)
1248 {
1249 if (log)
1250 module_sp->LogMessage(log.get(), "LC_SYMTAB.nsyms == 0");
1251 return 0;
1252 }
1253
1254 if (symtab_load_command.strsize == 0)
1255 {
1256 if (log)
1257 module_sp->LogMessage(log.get(), "LC_SYMTAB.strsize == 0");
1258 return 0;
1259 }
1260 break;
1261
1262 case LoadCommandFunctionStarts:
1263 function_starts_load_command.cmd = lc.cmd;
1264 function_starts_load_command.cmdsize = lc.cmdsize;
1265 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1266 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1267 break;
1268
1269 default:
1270 break;
1271 }
1272 offset = cmd_offset + lc.cmdsize;
1273 }
1274
1275 if (symtab_load_command.cmd)
1276 {
1277 Symtab *symtab = m_symtab_ap.get();
1278 SectionList *section_list = GetSectionList();
1279 if (section_list == NULL)
1280 return 0;
1281
1282 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001283 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001284
1285 const size_t addr_byte_size = m_data.GetAddressByteSize();
1286 bool bit_width_32 = addr_byte_size == 4;
1287 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1288
1289 DataExtractor nlist_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1290 DataExtractor strtab_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1291 DataExtractor function_starts_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1292
1293 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1294 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001295 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1296 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001297 {
Greg Claytondd29b972012-05-18 23:20:01 +00001298 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001299 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1300 // Reading mach file from memory in a process or core file...
1301
1302 if (linkedit_section_sp)
1303 {
1304 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1305 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1306 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001307 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001308
1309 bool data_was_read = false;
1310
1311#if defined (__APPLE__) && defined (__arm__)
1312 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001313 {
Greg Clayton29021d32012-04-18 05:19:20 +00001314 // This mach-o memory file is in the dyld shared cache. If this
1315 // program is not remote and this is iOS, then this process will
1316 // share the same shared cache as the process we are debugging and
1317 // we can read the entire __LINKEDIT from the address space in this
1318 // process. This is a needed optimization that is used for local iOS
1319 // debugging only since all shared libraries in the shared cache do
1320 // not have corresponding files that exist in the file system of the
1321 // device. They have been combined into a single file. This means we
1322 // always have to load these files from memory. All of the symbol and
1323 // string tables from all of the __LINKEDIT sections from the shared
1324 // libraries in the shared cache have been merged into a single large
1325 // symbol and string table. Reading all of this symbol and string table
1326 // data across can slow down debug launch times, so we optimize this by
1327 // reading the memory for the __LINKEDIT section from this process.
1328 PlatformSP platform_sp (target.GetPlatform());
1329 if (platform_sp && platform_sp->IsHost())
1330 {
1331 data_was_read = true;
1332 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001333 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001334 if (function_starts_load_command.cmd)
1335 {
1336 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1337 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1338 }
1339 }
1340 }
1341#endif
1342
1343 if (!data_was_read)
1344 {
1345 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1346 if (nlist_data_sp)
1347 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001348 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1349 //if (strtab_data_sp)
1350 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Greg Clayton29021d32012-04-18 05:19:20 +00001351 if (function_starts_load_command.cmd)
1352 {
1353 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1354 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1355 if (func_start_data_sp)
1356 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1357 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001358 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001359 }
1360 }
1361 else
1362 {
1363 nlist_data.SetData (m_data,
1364 symtab_load_command.symoff,
1365 nlist_data_byte_size);
1366 strtab_data.SetData (m_data,
1367 symtab_load_command.stroff,
1368 strtab_data_byte_size);
1369 if (function_starts_load_command.cmd)
1370 {
1371 function_starts_data.SetData (m_data,
1372 function_starts_load_command.dataoff,
1373 function_starts_load_command.datasize);
1374 }
1375 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001376
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001377 if (nlist_data.GetByteSize() == 0)
1378 {
1379 if (log)
1380 module_sp->LogMessage(log.get(), "failed to read nlist data");
1381 return 0;
1382 }
1383
1384
Greg Clayton3a5dc012012-05-25 17:04:00 +00001385 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1386 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001387 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001388 if (process)
1389 {
1390 if (strtab_addr == LLDB_INVALID_ADDRESS)
1391 {
1392 if (log)
1393 module_sp->LogMessage(log.get(), "failed to locate the strtab in memory");
1394 return 0;
1395 }
1396 }
1397 else
Greg Claytondd29b972012-05-18 23:20:01 +00001398 {
1399 if (log)
Greg Clayton3a5dc012012-05-25 17:04:00 +00001400 module_sp->LogMessage(log.get(), "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001401 return 0;
1402 }
1403 }
Greg Claytondd29b972012-05-18 23:20:01 +00001404
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001405 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1406 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1407 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1408 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1409 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1410 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1411 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1412 SectionSP eh_frame_section_sp;
1413 if (text_section_sp.get())
1414 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1415 else
1416 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1417
Greg Claytond2653c22012-03-14 01:53:24 +00001418 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001419 if (text_section_sp && function_starts_data.GetByteSize())
1420 {
1421 FunctionStarts::Entry function_start_entry;
1422 function_start_entry.data = false;
1423 uint32_t function_start_offset = 0;
1424 function_start_entry.addr = text_section_sp->GetFileAddress();
1425 uint64_t delta;
1426 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1427 {
1428 // Now append the current entry
1429 function_start_entry.addr += delta;
1430 function_starts.Append(function_start_entry);
1431 }
1432 }
1433
1434 const uint32_t function_starts_count = function_starts.GetSize();
1435
1436 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
1437
1438 uint32_t nlist_data_offset = 0;
1439
1440 uint32_t N_SO_index = UINT32_MAX;
1441
1442 MachSymtabSectionInfo section_info (section_list);
1443 std::vector<uint32_t> N_FUN_indexes;
1444 std::vector<uint32_t> N_NSYM_indexes;
1445 std::vector<uint32_t> N_INCL_indexes;
1446 std::vector<uint32_t> N_BRAC_indexes;
1447 std::vector<uint32_t> N_COMM_indexes;
1448 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1449 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1450 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1451 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1452 // Any symbols that get merged into another will get an entry
1453 // in this map so we know
1454 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1455 uint32_t nlist_idx = 0;
1456 Symbol *symbol_ptr = NULL;
1457
1458 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001459 Symbol *sym = NULL;
1460 uint32_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001461 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001462 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001463
Jason Molendab62abd52012-06-21 01:51:02 +00001464#if defined (__APPLE__) && defined (__arm__)
1465
1466 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1467 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1468 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1469 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1470 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1471 // nlist parser to ignore all LOCAL symbols.
1472
1473 if (m_header.flags & 0x80000000u)
1474 {
1475 // Before we can start mapping the DSC, we need to make certain the target process is actually
1476 // using the cache we can find.
1477
1478 /*
1479 * TODO (FIXME!)
1480 *
1481 * Consider the case of testing with a separate DSC file.
1482 * If we go through the normal code paths, we will give symbols for the wrong DSC, and
1483 * that is bad. We need to read the target process' all_image_infos struct, and look
1484 * at the values of the processDetachedFromSharedRegion field. If that is set, we should skip
1485 * this code section.
1486 */
1487
1488 // Next we need to determine the correct path for the dyld shared cache.
1489
1490 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1491 char dsc_path[PATH_MAX];
1492
1493 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
1494 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1495 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
1496 header_arch.GetArchitectureName());
1497
1498 FileSpec dsc_filespec(dsc_path, false);
1499
1500 // We need definitions of two structures in the on-disk DSC, copy them here manually
Greg Claytonab77dcb2012-09-05 22:30:51 +00001501 struct lldb_copy_dyld_cache_header
1502 {
1503 char magic[16];
1504 uint32_t mappingOffset;
1505 uint32_t mappingCount;
1506 uint32_t imagesOffset;
1507 uint32_t imagesCount;
1508 uint64_t dyldBaseAddress;
1509 uint64_t codeSignatureOffset;
1510 uint64_t codeSignatureSize;
1511 uint64_t slideInfoOffset;
1512 uint64_t slideInfoSize;
1513 uint64_t localSymbolsOffset;
1514 uint64_t localSymbolsSize;
1515 };
1516 struct lldb_copy_dyld_cache_local_symbols_info
1517 {
1518 uint32_t nlistOffset;
1519 uint32_t nlistCount;
1520 uint32_t stringsOffset;
1521 uint32_t stringsSize;
1522 uint32_t entriesOffset;
1523 uint32_t entriesCount;
1524 };
1525 struct lldb_copy_dyld_cache_local_symbols_entry
1526 {
1527 uint32_t dylibOffset;
1528 uint32_t nlistStartIndex;
1529 uint32_t nlistCount;
1530 };
Jason Molendab62abd52012-06-21 01:51:02 +00001531
Jason Molendafd3b35d2012-06-22 03:28:35 +00001532 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1533 The dyld_cache_local_symbols_info structure gives us three things:
1534 1. The start and count of the nlist records in the dyld_shared_cache file
1535 2. The start and size of the strings for these nlist records
1536 3. The start and count of dyld_cache_local_symbols_entry entries
1537
1538 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1539 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
1540 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
1541 and the count of how many nlist records there are for this dylib/framework.
1542 */
1543
Jason Molendab62abd52012-06-21 01:51:02 +00001544 // Process the dsc header to find the unmapped symbols
1545 //
1546 // Save some VM space, do not map the entire cache in one shot.
1547
1548 if (DataBufferSP dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header)))
1549 {
1550 DataExtractor dsc_header_data(dsc_data_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1551
Jason Molendafd3b35d2012-06-22 03:28:35 +00001552 uint32_t offset = offsetof (struct lldb_copy_dyld_cache_header, mappingOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001553 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1554
1555 // If the mappingOffset points to a location inside the header, we've
1556 // opened an old dyld shared cache, and should not proceed further.
1557 if (mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header))
1558 {
1559
Jason Molendafd3b35d2012-06-22 03:28:35 +00001560 offset = offsetof (struct lldb_copy_dyld_cache_header, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001561 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1562 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1563
1564 if (localSymbolsOffset && localSymbolsSize)
1565 {
1566 // Map the local symbols
1567 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
1568 {
1569 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1570
1571 offset = 0;
1572
1573 // Read the local_symbols_infos struct in one shot
1574 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1575 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1576
1577 // The local_symbols_infos offsets are offsets into local symbols memory, NOT file offsets!
1578 // We first need to identify the local "entry" that matches the current header.
1579 // The "entry" is stored as a file offset in the dyld_shared_cache, so we need to
1580 // adjust the raw m_header value by slide and 0x30000000.
1581
1582 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1583
1584 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - 0x30000000);
1585
1586 offset = local_symbols_info.entriesOffset;
1587 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1588 {
1589 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1590 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1591 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1592 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1593
1594 if (header_file_offset == local_symbols_entry.dylibOffset)
1595 {
1596 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1597
1598 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1599 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1600 num_syms = symtab->GetNumSymbols();
1601
1602 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1603 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1604
1605 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
1606 {
1607 /////////////////////////////
1608 {
1609 struct nlist_64 nlist;
1610 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1611 break;
1612
1613 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1614 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1615 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1616 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1617 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1618
1619 SymbolType type = eSymbolTypeInvalid;
1620 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1621
1622 if (symbol_name == NULL)
1623 {
1624 // No symbol should be NULL, even the symbols with no
1625 // string values should have an offset zero which points
1626 // to an empty C-string
1627 Host::SystemLog (Host::eSystemLogError,
1628 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1629 entry_index,
1630 nlist.n_strx,
1631 module_sp->GetFileSpec().GetDirectory().GetCString(),
1632 module_sp->GetFileSpec().GetFilename().GetCString());
1633 continue;
1634 }
1635 if (symbol_name[0] == '\0')
1636 symbol_name = NULL;
1637
1638 const char *symbol_name_non_abi_mangled = NULL;
1639
1640 SectionSP symbol_section;
1641 uint32_t symbol_byte_size = 0;
1642 bool add_nlist = true;
1643 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
1644
1645 assert (sym_idx < num_syms);
1646
1647 sym[sym_idx].SetDebug (is_debug);
1648
1649 if (is_debug)
1650 {
1651 switch (nlist.n_type)
1652 {
1653 case StabGlobalSymbol:
1654 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1655 // Sometimes the N_GSYM value contains the address.
1656
1657 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1658 // have the same address, but we want to ensure that we always find only the real symbol,
1659 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1660 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1661 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1662 // same address.
1663
1664 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1665 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1666 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1667 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1668 add_nlist = false;
1669 else
1670 {
1671 sym[sym_idx].SetExternal(true);
1672 if (nlist.n_value != 0)
1673 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1674 type = eSymbolTypeData;
1675 }
1676 break;
1677
1678 case StabFunctionName:
1679 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1680 type = eSymbolTypeCompiler;
1681 break;
1682
1683 case StabFunction:
1684 // N_FUN -- procedure: name,,n_sect,linenumber,address
1685 if (symbol_name)
1686 {
1687 type = eSymbolTypeCode;
1688 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1689
1690 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1691 // We use the current number of symbols in the symbol table in lieu of
1692 // using nlist_idx in case we ever start trimming entries out
1693 N_FUN_indexes.push_back(sym_idx);
1694 }
1695 else
1696 {
1697 type = eSymbolTypeCompiler;
1698
1699 if ( !N_FUN_indexes.empty() )
1700 {
1701 // Copy the size of the function into the original STAB entry so we don't have
1702 // to hunt for it later
1703 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1704 N_FUN_indexes.pop_back();
1705 // We don't really need the end function STAB as it contains the size which
1706 // we already placed with the original symbol, so don't add it if we want a
1707 // minimal symbol table
1708 if (minimize)
1709 add_nlist = false;
1710 }
1711 }
1712 break;
1713
1714 case StabStaticSymbol:
1715 // N_STSYM -- static symbol: name,,n_sect,type,address
1716 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1717 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1718 type = eSymbolTypeData;
1719 break;
1720
1721 case StabLocalCommon:
1722 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1723 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1724 type = eSymbolTypeCommonBlock;
1725 break;
1726
1727 case StabBeginSymbol:
1728 // N_BNSYM
1729 // We use the current number of symbols in the symbol table in lieu of
1730 // using nlist_idx in case we ever start trimming entries out
1731 if (minimize)
1732 {
1733 // Skip these if we want minimal symbol tables
1734 add_nlist = false;
1735 }
1736 else
1737 {
1738 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1739 N_NSYM_indexes.push_back(sym_idx);
1740 type = eSymbolTypeScopeBegin;
1741 }
1742 break;
1743
1744 case StabEndSymbol:
1745 // N_ENSYM
1746 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1747 // so that we can always skip the entire symbol if we need to navigate
1748 // more quickly at the source level when parsing STABS
1749 if (minimize)
1750 {
1751 // Skip these if we want minimal symbol tables
1752 add_nlist = false;
1753 }
1754 else
1755 {
1756 if ( !N_NSYM_indexes.empty() )
1757 {
1758 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1759 symbol_ptr->SetByteSize(sym_idx + 1);
1760 symbol_ptr->SetSizeIsSibling(true);
1761 N_NSYM_indexes.pop_back();
1762 }
1763 type = eSymbolTypeScopeEnd;
1764 }
1765 break;
1766
1767
1768 case StabSourceFileOptions:
1769 // N_OPT - emitted with gcc2_compiled and in gcc source
1770 type = eSymbolTypeCompiler;
1771 break;
1772
1773 case StabRegisterSymbol:
1774 // N_RSYM - register sym: name,,NO_SECT,type,register
1775 type = eSymbolTypeVariable;
1776 break;
1777
1778 case StabSourceLine:
1779 // N_SLINE - src line: 0,,n_sect,linenumber,address
1780 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1781 type = eSymbolTypeLineEntry;
1782 break;
1783
1784 case StabStructureType:
1785 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1786 type = eSymbolTypeVariableType;
1787 break;
1788
1789 case StabSourceFileName:
1790 // N_SO - source file name
1791 type = eSymbolTypeSourceFile;
1792 if (symbol_name == NULL)
1793 {
1794 if (minimize)
1795 add_nlist = false;
1796 if (N_SO_index != UINT32_MAX)
1797 {
1798 // Set the size of the N_SO to the terminating index of this N_SO
1799 // so that we can always skip the entire N_SO if we need to navigate
1800 // more quickly at the source level when parsing STABS
1801 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1802 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1803 symbol_ptr->SetSizeIsSibling(true);
1804 }
1805 N_NSYM_indexes.clear();
1806 N_INCL_indexes.clear();
1807 N_BRAC_indexes.clear();
1808 N_COMM_indexes.clear();
1809 N_FUN_indexes.clear();
1810 N_SO_index = UINT32_MAX;
1811 }
1812 else
1813 {
1814 // We use the current number of symbols in the symbol table in lieu of
1815 // using nlist_idx in case we ever start trimming entries out
1816 const bool N_SO_has_full_path = symbol_name[0] == '/';
1817 if (N_SO_has_full_path)
1818 {
1819 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1820 {
1821 // We have two consecutive N_SO entries where the first contains a directory
1822 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00001823 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001824 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1825 add_nlist = false;
1826 }
1827 else
1828 {
1829 // This is the first entry in a N_SO that contains a directory or
1830 // a full path to the source file
1831 N_SO_index = sym_idx;
1832 }
1833 }
1834 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1835 {
1836 // This is usually the second N_SO entry that contains just the filename,
1837 // so here we combine it with the first one if we are minimizing the symbol table
1838 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1839 if (so_path && so_path[0])
1840 {
1841 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00001842 const size_t double_slash_pos = full_so_path.find("//");
1843 if (double_slash_pos != std::string::npos)
1844 {
1845 // The linker has been generating bad N_SO entries with doubled up paths
1846 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
1847 // and the second is the directory for the source file so you end up with
1848 // a path that looks like "/tmp/src//tmp/src/"
1849 FileSpec so_dir(so_path, false);
1850 if (!so_dir.Exists())
1851 {
1852 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
1853 if (so_dir.Exists())
1854 {
1855 // Trim off the incorrect path
1856 full_so_path.erase(0, double_slash_pos + 1);
1857 }
1858 }
1859 }
Jason Molendab62abd52012-06-21 01:51:02 +00001860 if (*full_so_path.rbegin() != '/')
1861 full_so_path += '/';
1862 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00001863 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001864 add_nlist = false;
1865 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1866 }
1867 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00001868 else
1869 {
1870 // This could be a relative path to a N_SO
1871 N_SO_index = sym_idx;
1872 }
Jason Molendab62abd52012-06-21 01:51:02 +00001873 }
Jason Molendab62abd52012-06-21 01:51:02 +00001874 break;
1875
1876 case StabObjectFileName:
1877 // N_OSO - object file name: name,,0,0,st_mtime
1878 type = eSymbolTypeObjectFile;
1879 break;
1880
1881 case StabLocalSymbol:
1882 // N_LSYM - local sym: name,,NO_SECT,type,offset
1883 type = eSymbolTypeLocal;
1884 break;
1885
1886 //----------------------------------------------------------------------
1887 // INCL scopes
1888 //----------------------------------------------------------------------
1889 case StabBeginIncludeFileName:
1890 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
1891 // We use the current number of symbols in the symbol table in lieu of
1892 // using nlist_idx in case we ever start trimming entries out
1893 N_INCL_indexes.push_back(sym_idx);
1894 type = eSymbolTypeScopeBegin;
1895 break;
1896
1897 case StabEndIncludeFile:
1898 // N_EINCL - include file end: name,,NO_SECT,0,0
1899 // Set the size of the N_BINCL to the terminating index of this N_EINCL
1900 // so that we can always skip the entire symbol if we need to navigate
1901 // more quickly at the source level when parsing STABS
1902 if ( !N_INCL_indexes.empty() )
1903 {
1904 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1905 symbol_ptr->SetByteSize(sym_idx + 1);
1906 symbol_ptr->SetSizeIsSibling(true);
1907 N_INCL_indexes.pop_back();
1908 }
1909 type = eSymbolTypeScopeEnd;
1910 break;
1911
1912 case StabIncludeFileName:
1913 // N_SOL - #included file name: name,,n_sect,0,address
1914 type = eSymbolTypeHeaderFile;
1915
1916 // We currently don't use the header files on darwin
1917 if (minimize)
1918 add_nlist = false;
1919 break;
1920
1921 case StabCompilerParameters:
1922 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
1923 type = eSymbolTypeCompiler;
1924 break;
1925
1926 case StabCompilerVersion:
1927 // N_VERSION - compiler version: name,,NO_SECT,0,0
1928 type = eSymbolTypeCompiler;
1929 break;
1930
1931 case StabCompilerOptLevel:
1932 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
1933 type = eSymbolTypeCompiler;
1934 break;
1935
1936 case StabParameter:
1937 // N_PSYM - parameter: name,,NO_SECT,type,offset
1938 type = eSymbolTypeVariable;
1939 break;
1940
1941 case StabAlternateEntry:
1942 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
1943 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1944 type = eSymbolTypeLineEntry;
1945 break;
1946
1947 //----------------------------------------------------------------------
1948 // Left and Right Braces
1949 //----------------------------------------------------------------------
1950 case StabLeftBracket:
1951 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
1952 // We use the current number of symbols in the symbol table in lieu of
1953 // using nlist_idx in case we ever start trimming entries out
1954 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1955 N_BRAC_indexes.push_back(sym_idx);
1956 type = eSymbolTypeScopeBegin;
1957 break;
1958
1959 case StabRightBracket:
1960 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
1961 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
1962 // so that we can always skip the entire symbol if we need to navigate
1963 // more quickly at the source level when parsing STABS
1964 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1965 if ( !N_BRAC_indexes.empty() )
1966 {
1967 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1968 symbol_ptr->SetByteSize(sym_idx + 1);
1969 symbol_ptr->SetSizeIsSibling(true);
1970 N_BRAC_indexes.pop_back();
1971 }
1972 type = eSymbolTypeScopeEnd;
1973 break;
1974
1975 case StabDeletedIncludeFile:
1976 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
1977 type = eSymbolTypeHeaderFile;
1978 break;
1979
1980 //----------------------------------------------------------------------
1981 // COMM scopes
1982 //----------------------------------------------------------------------
1983 case StabBeginCommon:
1984 // N_BCOMM - begin common: name,,NO_SECT,0,0
1985 // We use the current number of symbols in the symbol table in lieu of
1986 // using nlist_idx in case we ever start trimming entries out
1987 type = eSymbolTypeScopeBegin;
1988 N_COMM_indexes.push_back(sym_idx);
1989 break;
1990
1991 case StabEndCommonLocal:
1992 // N_ECOML - end common (local name): 0,,n_sect,0,address
1993 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1994 // Fall through
1995
1996 case StabEndCommon:
1997 // N_ECOMM - end common: name,,n_sect,0,0
1998 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1999 // so that we can always skip the entire symbol if we need to navigate
2000 // more quickly at the source level when parsing STABS
2001 if ( !N_COMM_indexes.empty() )
2002 {
2003 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2004 symbol_ptr->SetByteSize(sym_idx + 1);
2005 symbol_ptr->SetSizeIsSibling(true);
2006 N_COMM_indexes.pop_back();
2007 }
2008 type = eSymbolTypeScopeEnd;
2009 break;
2010
2011 case StabLength:
2012 // N_LENG - second stab entry with length information
2013 type = eSymbolTypeAdditional;
2014 break;
2015
2016 default: break;
2017 }
2018 }
2019 else
2020 {
2021 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2022 uint8_t n_type = NlistMaskType & nlist.n_type;
2023 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2024
2025 switch (n_type)
2026 {
2027 case NListTypeIndirect: // N_INDR - Fall through
2028 case NListTypePreboundUndefined:// N_PBUD - Fall through
2029 case NListTypeUndefined: // N_UNDF
2030 type = eSymbolTypeUndefined;
2031 break;
2032
2033 case NListTypeAbsolute: // N_ABS
2034 type = eSymbolTypeAbsolute;
2035 break;
2036
2037 case NListTypeSection: // N_SECT
2038 {
2039 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2040
2041 if (symbol_section == NULL)
2042 {
2043 // TODO: warn about this?
2044 add_nlist = false;
2045 break;
2046 }
2047
2048 if (TEXT_eh_frame_sectID == nlist.n_sect)
2049 {
2050 type = eSymbolTypeException;
2051 }
2052 else
2053 {
2054 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2055
2056 switch (section_type)
2057 {
2058 case SectionTypeRegular: break; // regular section
2059 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2060 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2061 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2062 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2063 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2064 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2065 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2066 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2067 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2068 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2069 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2070 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2071 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2072 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2073 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2074 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2075 default: break;
2076 }
2077
2078 if (type == eSymbolTypeInvalid)
2079 {
2080 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2081 if (symbol_section->IsDescendant (text_section_sp.get()))
2082 {
2083 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2084 SectionAttrUserSelfModifyingCode |
2085 SectionAttrSytemSomeInstructions))
2086 type = eSymbolTypeData;
2087 else
2088 type = eSymbolTypeCode;
2089 }
2090 else
2091 if (symbol_section->IsDescendant(data_section_sp.get()))
2092 {
2093 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2094 {
2095 type = eSymbolTypeRuntime;
2096
2097 if (symbol_name &&
2098 symbol_name[0] == '_' &&
2099 symbol_name[1] == 'O' &&
2100 symbol_name[2] == 'B')
2101 {
2102 llvm::StringRef symbol_name_ref(symbol_name);
2103 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2104 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2105 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2106 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2107 {
2108 symbol_name_non_abi_mangled = symbol_name + 1;
2109 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2110 type = eSymbolTypeObjCClass;
2111 }
2112 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2113 {
2114 symbol_name_non_abi_mangled = symbol_name + 1;
2115 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2116 type = eSymbolTypeObjCMetaClass;
2117 }
2118 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2119 {
2120 symbol_name_non_abi_mangled = symbol_name + 1;
2121 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2122 type = eSymbolTypeObjCIVar;
2123 }
2124 }
2125 }
2126 else
2127 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
2128 {
2129 type = eSymbolTypeException;
2130 }
2131 else
2132 {
2133 type = eSymbolTypeData;
2134 }
2135 }
2136 else
2137 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2138 {
2139 type = eSymbolTypeTrampoline;
2140 }
2141 else
2142 if (symbol_section->IsDescendant(objc_section_sp.get()))
2143 {
2144 type = eSymbolTypeRuntime;
2145 if (symbol_name && symbol_name[0] == '.')
2146 {
2147 llvm::StringRef symbol_name_ref(symbol_name);
2148 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2149 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
2150 {
2151 symbol_name_non_abi_mangled = symbol_name;
2152 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2153 type = eSymbolTypeObjCClass;
2154 }
2155 }
2156 }
2157 }
2158 }
2159 }
2160 break;
2161 }
2162 }
2163
2164 if (add_nlist)
2165 {
2166 uint64_t symbol_value = nlist.n_value;
2167 bool symbol_name_is_mangled = false;
2168
2169 if (symbol_name_non_abi_mangled)
2170 {
Jason Molenda292cca82012-07-20 03:35:44 +00002171 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2172 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002173 }
2174 else
2175 {
2176 if (symbol_name && symbol_name[0] == '_')
2177 {
2178 symbol_name_is_mangled = symbol_name[1] == '_';
2179 symbol_name++; // Skip the leading underscore
2180 }
2181
2182 if (symbol_name)
2183 {
Jason Molenda292cca82012-07-20 03:35:44 +00002184 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002185 }
2186 }
2187
2188 if (is_debug == false)
2189 {
2190 if (type == eSymbolTypeCode)
2191 {
2192 // See if we can find a N_FUN entry for any code symbols.
2193 // If we do find a match, and the name matches, then we
2194 // can merge the two into just the function symbol to avoid
2195 // duplicate entries in the symbol table
2196 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2197 if (pos != N_FUN_addr_to_sym_idx.end())
2198 {
2199 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2200 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2201 {
2202 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2203 // We just need the flags from the linker symbol, so put these flags
2204 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2205 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2206 sym[sym_idx].Clear();
2207 continue;
2208 }
2209 }
2210 }
2211 else if (type == eSymbolTypeData)
2212 {
2213 // See if we can find a N_STSYM entry for any data symbols.
2214 // If we do find a match, and the name matches, then we
2215 // can merge the two into just the Static symbol to avoid
2216 // duplicate entries in the symbol table
2217 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2218 if (pos != N_STSYM_addr_to_sym_idx.end())
2219 {
2220 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2221 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2222 {
2223 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2224 // We just need the flags from the linker symbol, so put these flags
2225 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2226 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2227 sym[sym_idx].Clear();
2228 continue;
2229 }
2230 }
2231 }
2232 }
2233 if (symbol_section)
2234 {
2235 const addr_t section_file_addr = symbol_section->GetFileAddress();
2236 if (symbol_byte_size == 0 && function_starts_count > 0)
2237 {
2238 addr_t symbol_lookup_file_addr = nlist.n_value;
2239 // Do an exact address match for non-ARM addresses, else get the closest since
2240 // the symbol might be a thumb symbol which has an address with bit zero set
2241 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2242 if (is_arm && func_start_entry)
2243 {
2244 // Verify that the function start address is the symbol address (ARM)
2245 // or the symbol address + 1 (thumb)
2246 if (func_start_entry->addr != symbol_lookup_file_addr &&
2247 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2248 {
2249 // Not the right entry, NULL it out...
2250 func_start_entry = NULL;
2251 }
2252 }
2253 if (func_start_entry)
2254 {
2255 func_start_entry->data = true;
2256
2257 addr_t symbol_file_addr = func_start_entry->addr;
2258 uint32_t symbol_flags = 0;
2259 if (is_arm)
2260 {
2261 if (symbol_file_addr & 1)
2262 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2263 symbol_file_addr &= 0xfffffffffffffffeull;
2264 }
2265
2266 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2267 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2268 if (next_func_start_entry)
2269 {
2270 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2271 // Be sure the clear the Thumb address bit when we calculate the size
2272 // from the current and next address
2273 if (is_arm)
2274 next_symbol_file_addr &= 0xfffffffffffffffeull;
2275 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2276 }
2277 else
2278 {
2279 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2280 }
2281 }
2282 }
2283 symbol_value -= section_file_addr;
2284 }
2285
2286 sym[sym_idx].SetID (nlist_idx);
2287 sym[sym_idx].SetType (type);
2288 sym[sym_idx].GetAddress().SetSection (symbol_section);
2289 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2290 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2291
2292 if (symbol_byte_size > 0)
2293 sym[sym_idx].SetByteSize(symbol_byte_size);
2294
2295 ++sym_idx;
2296 }
2297 else
2298 {
2299 sym[sym_idx].Clear();
2300 }
2301
2302 }
2303 /////////////////////////////
2304 }
2305 break; // No more entries to consider
2306 }
2307 }
2308 }
2309 }
2310 }
2311 }
2312 }
2313
2314 // Must reset this in case it was mutated above!
2315 nlist_data_offset = 0;
2316#endif
2317
2318 // If the sym array was not created while parsing the DSC unmapped
2319 // symbols, create it now.
2320 if (sym == NULL)
2321 {
2322 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2323 num_syms = symtab->GetNumSymbols();
2324 }
2325
2326 if (unmapped_local_symbols_found)
2327 {
2328 assert(m_dysymtab.ilocalsym == 0);
2329 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2330 nlist_idx = m_dysymtab.nlocalsym;
2331 }
2332 else
2333 {
2334 nlist_idx = 0;
2335 }
2336
2337 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002338 {
2339 struct nlist_64 nlist;
2340 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2341 break;
2342
2343 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2344 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2345 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2346 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2347 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2348
2349 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002350 const char *symbol_name = NULL;
2351
Greg Clayton3a5dc012012-05-25 17:04:00 +00002352 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002353 {
2354 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
2355
2356 if (symbol_name == NULL)
2357 {
2358 // No symbol should be NULL, even the symbols with no
2359 // string values should have an offset zero which points
2360 // to an empty C-string
2361 Host::SystemLog (Host::eSystemLogError,
2362 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
2363 nlist_idx,
2364 nlist.n_strx,
2365 module_sp->GetFileSpec().GetDirectory().GetCString(),
2366 module_sp->GetFileSpec().GetFilename().GetCString());
2367 continue;
2368 }
2369 if (symbol_name[0] == '\0')
2370 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002371 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002372 else
2373 {
2374 const addr_t str_addr = strtab_addr + nlist.n_strx;
2375 Error str_error;
2376 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2377 symbol_name = memory_symbol_name.c_str();
2378 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002379 const char *symbol_name_non_abi_mangled = NULL;
2380
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002381 SectionSP symbol_section;
2382 uint32_t symbol_byte_size = 0;
2383 bool add_nlist = true;
2384 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
2385
2386 assert (sym_idx < num_syms);
2387
2388 sym[sym_idx].SetDebug (is_debug);
2389
2390 if (is_debug)
2391 {
2392 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002393 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002394 case StabGlobalSymbol:
2395 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2396 // Sometimes the N_GSYM value contains the address.
2397
2398 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2399 // have the same address, but we want to ensure that we always find only the real symbol,
2400 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2401 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2402 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2403 // same address.
2404
2405 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2406 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2407 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2408 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2409 add_nlist = false;
2410 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002411 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002412 sym[sym_idx].SetExternal(true);
2413 if (nlist.n_value != 0)
2414 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2415 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002416 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002417 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002418
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002419 case StabFunctionName:
2420 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2421 type = eSymbolTypeCompiler;
2422 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002423
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002424 case StabFunction:
2425 // N_FUN -- procedure: name,,n_sect,linenumber,address
2426 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002427 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002428 type = eSymbolTypeCode;
2429 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2430
2431 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2432 // We use the current number of symbols in the symbol table in lieu of
2433 // using nlist_idx in case we ever start trimming entries out
2434 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002435 }
2436 else
2437 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002438 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002439
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002440 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002441 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002442 // Copy the size of the function into the original STAB entry so we don't have
2443 // to hunt for it later
2444 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2445 N_FUN_indexes.pop_back();
2446 // We don't really need the end function STAB as it contains the size which
2447 // we already placed with the original symbol, so don't add it if we want a
2448 // minimal symbol table
2449 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002450 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002451 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002452 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002453 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002454
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002455 case StabStaticSymbol:
2456 // N_STSYM -- static symbol: name,,n_sect,type,address
2457 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2458 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2459 type = eSymbolTypeData;
2460 break;
2461
2462 case StabLocalCommon:
2463 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2464 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2465 type = eSymbolTypeCommonBlock;
2466 break;
2467
2468 case StabBeginSymbol:
2469 // N_BNSYM
2470 // We use the current number of symbols in the symbol table in lieu of
2471 // using nlist_idx in case we ever start trimming entries out
2472 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002473 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002474 // Skip these if we want minimal symbol tables
2475 add_nlist = false;
2476 }
2477 else
2478 {
2479 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2480 N_NSYM_indexes.push_back(sym_idx);
2481 type = eSymbolTypeScopeBegin;
2482 }
2483 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002484
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002485 case StabEndSymbol:
2486 // N_ENSYM
2487 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2488 // so that we can always skip the entire symbol if we need to navigate
2489 // more quickly at the source level when parsing STABS
2490 if (minimize)
2491 {
2492 // Skip these if we want minimal symbol tables
2493 add_nlist = false;
2494 }
2495 else
2496 {
2497 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002498 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002499 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2500 symbol_ptr->SetByteSize(sym_idx + 1);
2501 symbol_ptr->SetSizeIsSibling(true);
2502 N_NSYM_indexes.pop_back();
2503 }
2504 type = eSymbolTypeScopeEnd;
2505 }
2506 break;
2507
2508
2509 case StabSourceFileOptions:
2510 // N_OPT - emitted with gcc2_compiled and in gcc source
2511 type = eSymbolTypeCompiler;
2512 break;
2513
2514 case StabRegisterSymbol:
2515 // N_RSYM - register sym: name,,NO_SECT,type,register
2516 type = eSymbolTypeVariable;
2517 break;
2518
2519 case StabSourceLine:
2520 // N_SLINE - src line: 0,,n_sect,linenumber,address
2521 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2522 type = eSymbolTypeLineEntry;
2523 break;
2524
2525 case StabStructureType:
2526 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2527 type = eSymbolTypeVariableType;
2528 break;
2529
2530 case StabSourceFileName:
2531 // N_SO - source file name
2532 type = eSymbolTypeSourceFile;
2533 if (symbol_name == NULL)
2534 {
2535 if (minimize)
2536 add_nlist = false;
2537 if (N_SO_index != UINT32_MAX)
2538 {
2539 // Set the size of the N_SO to the terminating index of this N_SO
2540 // so that we can always skip the entire N_SO if we need to navigate
2541 // more quickly at the source level when parsing STABS
2542 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2543 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2544 symbol_ptr->SetSizeIsSibling(true);
2545 }
2546 N_NSYM_indexes.clear();
2547 N_INCL_indexes.clear();
2548 N_BRAC_indexes.clear();
2549 N_COMM_indexes.clear();
2550 N_FUN_indexes.clear();
2551 N_SO_index = UINT32_MAX;
2552 }
2553 else
2554 {
2555 // We use the current number of symbols in the symbol table in lieu of
2556 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002557 const bool N_SO_has_full_path = symbol_name[0] == '/';
2558 if (N_SO_has_full_path)
2559 {
2560 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2561 {
2562 // We have two consecutive N_SO entries where the first contains a directory
2563 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002564 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002565 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2566 add_nlist = false;
2567 }
2568 else
2569 {
2570 // This is the first entry in a N_SO that contains a directory or
2571 // a full path to the source file
2572 N_SO_index = sym_idx;
2573 }
2574 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002575 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2576 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002577 // This is usually the second N_SO entry that contains just the filename,
2578 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002579 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2580 if (so_path && so_path[0])
2581 {
2582 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002583 const size_t double_slash_pos = full_so_path.find("//");
2584 if (double_slash_pos != std::string::npos)
2585 {
2586 // The linker has been generating bad N_SO entries with doubled up paths
2587 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2588 // and the second is the directory for the source file so you end up with
2589 // a path that looks like "/tmp/src//tmp/src/"
2590 FileSpec so_dir(so_path, false);
2591 if (!so_dir.Exists())
2592 {
2593 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2594 if (so_dir.Exists())
2595 {
2596 // Trim off the incorrect path
2597 full_so_path.erase(0, double_slash_pos + 1);
2598 }
2599 }
2600 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002601 if (*full_so_path.rbegin() != '/')
2602 full_so_path += '/';
2603 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002604 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002605 add_nlist = false;
2606 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2607 }
2608 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002609 else
2610 {
2611 // This could be a relative path to a N_SO
2612 N_SO_index = sym_idx;
2613 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002614 }
2615
2616 break;
2617
2618 case StabObjectFileName:
2619 // N_OSO - object file name: name,,0,0,st_mtime
2620 type = eSymbolTypeObjectFile;
2621 break;
2622
2623 case StabLocalSymbol:
2624 // N_LSYM - local sym: name,,NO_SECT,type,offset
2625 type = eSymbolTypeLocal;
2626 break;
2627
2628 //----------------------------------------------------------------------
2629 // INCL scopes
2630 //----------------------------------------------------------------------
2631 case StabBeginIncludeFileName:
2632 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2633 // We use the current number of symbols in the symbol table in lieu of
2634 // using nlist_idx in case we ever start trimming entries out
2635 N_INCL_indexes.push_back(sym_idx);
2636 type = eSymbolTypeScopeBegin;
2637 break;
2638
2639 case StabEndIncludeFile:
2640 // N_EINCL - include file end: name,,NO_SECT,0,0
2641 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2642 // so that we can always skip the entire symbol if we need to navigate
2643 // more quickly at the source level when parsing STABS
2644 if ( !N_INCL_indexes.empty() )
2645 {
2646 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2647 symbol_ptr->SetByteSize(sym_idx + 1);
2648 symbol_ptr->SetSizeIsSibling(true);
2649 N_INCL_indexes.pop_back();
2650 }
2651 type = eSymbolTypeScopeEnd;
2652 break;
2653
2654 case StabIncludeFileName:
2655 // N_SOL - #included file name: name,,n_sect,0,address
2656 type = eSymbolTypeHeaderFile;
2657
2658 // We currently don't use the header files on darwin
2659 if (minimize)
2660 add_nlist = false;
2661 break;
2662
2663 case StabCompilerParameters:
2664 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2665 type = eSymbolTypeCompiler;
2666 break;
2667
2668 case StabCompilerVersion:
2669 // N_VERSION - compiler version: name,,NO_SECT,0,0
2670 type = eSymbolTypeCompiler;
2671 break;
2672
2673 case StabCompilerOptLevel:
2674 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2675 type = eSymbolTypeCompiler;
2676 break;
2677
2678 case StabParameter:
2679 // N_PSYM - parameter: name,,NO_SECT,type,offset
2680 type = eSymbolTypeVariable;
2681 break;
2682
2683 case StabAlternateEntry:
2684 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2685 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2686 type = eSymbolTypeLineEntry;
2687 break;
2688
2689 //----------------------------------------------------------------------
2690 // Left and Right Braces
2691 //----------------------------------------------------------------------
2692 case StabLeftBracket:
2693 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2694 // We use the current number of symbols in the symbol table in lieu of
2695 // using nlist_idx in case we ever start trimming entries out
2696 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2697 N_BRAC_indexes.push_back(sym_idx);
2698 type = eSymbolTypeScopeBegin;
2699 break;
2700
2701 case StabRightBracket:
2702 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2703 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2704 // so that we can always skip the entire symbol if we need to navigate
2705 // more quickly at the source level when parsing STABS
2706 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2707 if ( !N_BRAC_indexes.empty() )
2708 {
2709 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2710 symbol_ptr->SetByteSize(sym_idx + 1);
2711 symbol_ptr->SetSizeIsSibling(true);
2712 N_BRAC_indexes.pop_back();
2713 }
2714 type = eSymbolTypeScopeEnd;
2715 break;
2716
2717 case StabDeletedIncludeFile:
2718 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2719 type = eSymbolTypeHeaderFile;
2720 break;
2721
2722 //----------------------------------------------------------------------
2723 // COMM scopes
2724 //----------------------------------------------------------------------
2725 case StabBeginCommon:
2726 // N_BCOMM - begin common: name,,NO_SECT,0,0
2727 // We use the current number of symbols in the symbol table in lieu of
2728 // using nlist_idx in case we ever start trimming entries out
2729 type = eSymbolTypeScopeBegin;
2730 N_COMM_indexes.push_back(sym_idx);
2731 break;
2732
2733 case StabEndCommonLocal:
2734 // N_ECOML - end common (local name): 0,,n_sect,0,address
2735 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2736 // Fall through
2737
2738 case StabEndCommon:
2739 // N_ECOMM - end common: name,,n_sect,0,0
2740 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2741 // so that we can always skip the entire symbol if we need to navigate
2742 // more quickly at the source level when parsing STABS
2743 if ( !N_COMM_indexes.empty() )
2744 {
2745 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2746 symbol_ptr->SetByteSize(sym_idx + 1);
2747 symbol_ptr->SetSizeIsSibling(true);
2748 N_COMM_indexes.pop_back();
2749 }
2750 type = eSymbolTypeScopeEnd;
2751 break;
2752
2753 case StabLength:
2754 // N_LENG - second stab entry with length information
2755 type = eSymbolTypeAdditional;
2756 break;
2757
2758 default: break;
2759 }
2760 }
2761 else
2762 {
2763 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2764 uint8_t n_type = NlistMaskType & nlist.n_type;
2765 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2766
2767 switch (n_type)
2768 {
2769 case NListTypeIndirect: // N_INDR - Fall through
2770 case NListTypePreboundUndefined:// N_PBUD - Fall through
2771 case NListTypeUndefined: // N_UNDF
2772 type = eSymbolTypeUndefined;
2773 break;
2774
2775 case NListTypeAbsolute: // N_ABS
2776 type = eSymbolTypeAbsolute;
2777 break;
2778
2779 case NListTypeSection: // N_SECT
2780 {
2781 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2782
Sean Callananb386d822012-08-09 00:50:26 +00002783 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002784 {
2785 // TODO: warn about this?
2786 add_nlist = false;
2787 break;
2788 }
2789
2790 if (TEXT_eh_frame_sectID == nlist.n_sect)
2791 {
2792 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00002793 }
2794 else
2795 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002796 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2797
2798 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00002799 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002800 case SectionTypeRegular: break; // regular section
2801 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2802 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2803 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2804 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2805 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2806 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2807 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2808 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2809 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2810 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2811 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2812 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2813 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2814 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2815 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2816 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2817 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002818 }
Chris Lattner24943d22010-06-08 16:52:24 +00002819
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002820 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002821 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002822 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2823 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002824 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002825 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2826 SectionAttrUserSelfModifyingCode |
2827 SectionAttrSytemSomeInstructions))
2828 type = eSymbolTypeData;
2829 else
2830 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00002831 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002832 else
2833 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002834 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002835 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00002836 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002837 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00002838
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002839 if (symbol_name &&
2840 symbol_name[0] == '_' &&
2841 symbol_name[1] == 'O' &&
2842 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00002843 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002844 llvm::StringRef symbol_name_ref(symbol_name);
2845 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2846 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2847 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2848 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00002849 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002850 symbol_name_non_abi_mangled = symbol_name + 1;
2851 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2852 type = eSymbolTypeObjCClass;
Chris Lattner24943d22010-06-08 16:52:24 +00002853 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002854 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00002855 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002856 symbol_name_non_abi_mangled = symbol_name + 1;
2857 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2858 type = eSymbolTypeObjCMetaClass;
2859 }
2860 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2861 {
2862 symbol_name_non_abi_mangled = symbol_name + 1;
2863 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2864 type = eSymbolTypeObjCIVar;
Chris Lattner24943d22010-06-08 16:52:24 +00002865 }
2866 }
2867 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002868 else
2869 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
2870 {
2871 type = eSymbolTypeException;
2872 }
2873 else
2874 {
2875 type = eSymbolTypeData;
2876 }
2877 }
2878 else
2879 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2880 {
2881 type = eSymbolTypeTrampoline;
2882 }
2883 else
2884 if (symbol_section->IsDescendant(objc_section_sp.get()))
2885 {
2886 type = eSymbolTypeRuntime;
2887 if (symbol_name && symbol_name[0] == '.')
2888 {
2889 llvm::StringRef symbol_name_ref(symbol_name);
2890 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2891 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
2892 {
2893 symbol_name_non_abi_mangled = symbol_name;
2894 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2895 type = eSymbolTypeObjCClass;
2896 }
2897 }
Chris Lattner24943d22010-06-08 16:52:24 +00002898 }
2899 }
2900 }
2901 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002902 break;
2903 }
2904 }
2905
2906 if (add_nlist)
2907 {
2908 uint64_t symbol_value = nlist.n_value;
2909 bool symbol_name_is_mangled = false;
2910
2911 if (symbol_name_non_abi_mangled)
2912 {
Greg Claytonc0240042012-07-18 23:18:10 +00002913 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2914 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00002915 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002916 else
2917 {
2918 if (symbol_name && symbol_name[0] == '_')
2919 {
2920 symbol_name_is_mangled = symbol_name[1] == '_';
2921 symbol_name++; // Skip the leading underscore
2922 }
2923
2924 if (symbol_name)
2925 {
Greg Claytonc0240042012-07-18 23:18:10 +00002926 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002927 }
2928 }
2929
2930 if (is_debug == false)
2931 {
2932 if (type == eSymbolTypeCode)
2933 {
2934 // See if we can find a N_FUN entry for any code symbols.
2935 // If we do find a match, and the name matches, then we
2936 // can merge the two into just the function symbol to avoid
2937 // duplicate entries in the symbol table
2938 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2939 if (pos != N_FUN_addr_to_sym_idx.end())
2940 {
2941 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2942 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2943 {
2944 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2945 // We just need the flags from the linker symbol, so put these flags
2946 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2947 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2948 sym[sym_idx].Clear();
2949 continue;
2950 }
2951 }
2952 }
2953 else if (type == eSymbolTypeData)
2954 {
2955 // See if we can find a N_STSYM entry for any data symbols.
2956 // If we do find a match, and the name matches, then we
2957 // can merge the two into just the Static symbol to avoid
2958 // duplicate entries in the symbol table
2959 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2960 if (pos != N_STSYM_addr_to_sym_idx.end())
2961 {
2962 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2963 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2964 {
2965 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2966 // We just need the flags from the linker symbol, so put these flags
2967 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2968 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2969 sym[sym_idx].Clear();
2970 continue;
2971 }
2972 }
2973 }
2974 }
2975 if (symbol_section)
2976 {
2977 const addr_t section_file_addr = symbol_section->GetFileAddress();
2978 if (symbol_byte_size == 0 && function_starts_count > 0)
2979 {
Greg Claytond2653c22012-03-14 01:53:24 +00002980 addr_t symbol_lookup_file_addr = nlist.n_value;
2981 // Do an exact address match for non-ARM addresses, else get the closest since
2982 // the symbol might be a thumb symbol which has an address with bit zero set
2983 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2984 if (is_arm && func_start_entry)
2985 {
2986 // Verify that the function start address is the symbol address (ARM)
2987 // or the symbol address + 1 (thumb)
2988 if (func_start_entry->addr != symbol_lookup_file_addr &&
2989 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2990 {
2991 // Not the right entry, NULL it out...
2992 func_start_entry = NULL;
2993 }
2994 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002995 if (func_start_entry)
2996 {
2997 func_start_entry->data = true;
Greg Claytond2653c22012-03-14 01:53:24 +00002998
2999 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003000 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003001 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003002
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003003 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3004 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3005 if (next_func_start_entry)
3006 {
Greg Claytond2653c22012-03-14 01:53:24 +00003007 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3008 // Be sure the clear the Thumb address bit when we calculate the size
3009 // from the current and next address
3010 if (is_arm)
3011 next_symbol_file_addr &= 0xfffffffffffffffeull;
3012 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 +00003013 }
3014 else
3015 {
Greg Claytond2653c22012-03-14 01:53:24 +00003016 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003017 }
3018 }
3019 }
3020 symbol_value -= section_file_addr;
3021 }
3022
3023 sym[sym_idx].SetID (nlist_idx);
3024 sym[sym_idx].SetType (type);
3025 sym[sym_idx].GetAddress().SetSection (symbol_section);
3026 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3027 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3028
3029 if (symbol_byte_size > 0)
3030 sym[sym_idx].SetByteSize(symbol_byte_size);
3031
3032 ++sym_idx;
3033 }
3034 else
3035 {
3036 sym[sym_idx].Clear();
3037 }
3038
3039 }
3040
3041 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3042 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3043 // such entries by figuring out what the address for the global is by looking up this non-STAB
3044 // entry and copying the value into the debug symbol's value to save us the hassle in the
3045 // debug symbol parser.
3046
3047 Symbol *global_symbol = NULL;
3048 for (nlist_idx = 0;
3049 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3050 nlist_idx++)
3051 {
3052 if (global_symbol->GetAddress().GetFileAddress() == 0)
3053 {
3054 std::vector<uint32_t> indexes;
3055 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3056 {
3057 std::vector<uint32_t>::const_iterator pos;
3058 std::vector<uint32_t>::const_iterator end = indexes.end();
3059 for (pos = indexes.begin(); pos != end; ++pos)
3060 {
3061 symbol_ptr = symtab->SymbolAtIndex(*pos);
3062 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3063 {
3064 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3065 break;
3066 }
3067 }
3068 }
Chris Lattner24943d22010-06-08 16:52:24 +00003069 }
3070 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003071
3072 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3073
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003074 if (function_starts_count > 0)
3075 {
3076 char synthetic_function_symbol[PATH_MAX];
3077 uint32_t num_synthetic_function_symbols = 0;
3078 for (i=0; i<function_starts_count; ++i)
3079 {
3080 if (function_starts.GetEntryRef (i).data == false)
3081 ++num_synthetic_function_symbols;
3082 }
3083
3084 if (num_synthetic_function_symbols > 0)
3085 {
3086 if (num_syms < sym_idx + num_synthetic_function_symbols)
3087 {
3088 num_syms = sym_idx + num_synthetic_function_symbols;
3089 sym = symtab->Resize (num_syms);
3090 }
3091 uint32_t synthetic_function_symbol_idx = 0;
3092 for (i=0; i<function_starts_count; ++i)
3093 {
3094 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3095 if (func_start_entry->data == false)
3096 {
Greg Claytond2653c22012-03-14 01:53:24 +00003097 addr_t symbol_file_addr = func_start_entry->addr;
3098 uint32_t symbol_flags = 0;
3099 if (is_arm)
3100 {
3101 if (symbol_file_addr & 1)
3102 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3103 symbol_file_addr &= 0xfffffffffffffffeull;
3104 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003105 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003106 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003107 {
3108 SectionSP symbol_section (symbol_addr.GetSection());
3109 uint32_t symbol_byte_size = 0;
3110 if (symbol_section)
3111 {
3112 const addr_t section_file_addr = symbol_section->GetFileAddress();
3113 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3114 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3115 if (next_func_start_entry)
3116 {
Greg Claytond2653c22012-03-14 01:53:24 +00003117 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3118 if (is_arm)
3119 next_symbol_file_addr &= 0xfffffffffffffffeull;
3120 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 +00003121 }
3122 else
3123 {
Greg Claytond2653c22012-03-14 01:53:24 +00003124 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003125 }
3126 snprintf (synthetic_function_symbol,
3127 sizeof(synthetic_function_symbol),
3128 "___lldb_unnamed_function%u$$%s",
3129 ++synthetic_function_symbol_idx,
3130 module_sp->GetFileSpec().GetFilename().GetCString());
3131 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003132 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003133 sym[sym_idx].SetType (eSymbolTypeCode);
3134 sym[sym_idx].SetIsSynthetic (true);
3135 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003136 if (symbol_flags)
3137 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003138 if (symbol_byte_size)
3139 sym[sym_idx].SetByteSize (symbol_byte_size);
3140 ++sym_idx;
3141 }
3142 }
3143 }
3144 }
3145 }
3146 }
3147
3148 // Trim our symbols down to just what we ended up with after
3149 // removing any symbols.
3150 if (sym_idx < num_syms)
3151 {
3152 num_syms = sym_idx;
3153 sym = symtab->Resize (num_syms);
3154 }
3155
3156 // Now synthesize indirect symbols
3157 if (m_dysymtab.nindirectsyms != 0)
3158 {
3159 DataExtractor indirect_symbol_index_data (m_data, m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4);
3160
3161 if (indirect_symbol_index_data.GetByteSize())
3162 {
3163 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3164
3165 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3166 {
3167 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3168 {
3169 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3170 if (symbol_stub_byte_size == 0)
3171 continue;
3172
3173 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3174
3175 if (num_symbol_stubs == 0)
3176 continue;
3177
3178 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3179 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3180 {
3181 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3182 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
3183 uint32_t symbol_stub_offset = symbol_stub_index * 4;
3184 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3185 {
3186 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3187 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3188 continue;
3189
3190 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3191 Symbol *stub_symbol = NULL;
3192 if (index_pos != end_index_pos)
3193 {
3194 // We have a remapping from the original nlist index to
3195 // a current symbol index, so just look this up by index
3196 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3197 }
3198 else
3199 {
3200 // We need to lookup a symbol using the original nlist
3201 // symbol index since this index is coming from the
3202 // S_SYMBOL_STUBS
3203 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3204 }
3205
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003206 if (stub_symbol)
3207 {
3208 Address so_addr(symbol_stub_addr, section_list);
3209
3210 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3211 {
3212 // Change the external symbol into a trampoline that makes sense
3213 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3214 // can re-use them so we don't have to make up a synthetic symbol
3215 // for no good reason.
3216 stub_symbol->SetType (eSymbolTypeTrampoline);
3217 stub_symbol->SetExternal (false);
3218 stub_symbol->GetAddress() = so_addr;
3219 stub_symbol->SetByteSize (symbol_stub_byte_size);
3220 }
3221 else
3222 {
3223 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003224 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003225 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003226 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003227 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003228 stub_symbol = NULL; // this pointer no longer valid
3229 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003230 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003231 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003232 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3233 sym[sym_idx].SetIsSynthetic (true);
3234 sym[sym_idx].GetAddress() = so_addr;
3235 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3236 ++sym_idx;
3237 }
3238 }
Greg Claytond4330e62012-09-05 01:38:55 +00003239 else
3240 {
3241 if (log)
3242 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3243 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003244 }
3245 }
3246 }
3247 }
3248 }
3249 }
3250 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003251 }
3252 return 0;
3253}
3254
3255
3256void
3257ObjectFileMachO::Dump (Stream *s)
3258{
Greg Clayton9482f052012-03-13 23:14:29 +00003259 ModuleSP module_sp(GetModule());
3260 if (module_sp)
3261 {
3262 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3263 s->Printf("%p: ", this);
3264 s->Indent();
3265 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3266 s->PutCString("ObjectFileMachO64");
3267 else
3268 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003269
Greg Clayton9482f052012-03-13 23:14:29 +00003270 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003271
Greg Clayton9482f052012-03-13 23:14:29 +00003272 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003273
Greg Clayton9482f052012-03-13 23:14:29 +00003274 if (m_sections_ap.get())
3275 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003276
Greg Clayton9482f052012-03-13 23:14:29 +00003277 if (m_symtab_ap.get())
3278 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3279 }
Chris Lattner24943d22010-06-08 16:52:24 +00003280}
3281
3282
3283bool
Greg Clayton0467c782011-02-04 18:53:10 +00003284ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003285{
Greg Clayton9482f052012-03-13 23:14:29 +00003286 ModuleSP module_sp(GetModule());
3287 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003288 {
Greg Clayton9482f052012-03-13 23:14:29 +00003289 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3290 struct uuid_command load_cmd;
3291 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3292 uint32_t i;
3293 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003294 {
Greg Clayton9482f052012-03-13 23:14:29 +00003295 const uint32_t cmd_offset = offset;
3296 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3297 break;
3298
3299 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00003300 {
Greg Clayton9482f052012-03-13 23:14:29 +00003301 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
Sean Callanana5689d52012-07-12 18:04:03 +00003302
Greg Clayton9482f052012-03-13 23:14:29 +00003303 if (uuid_bytes)
3304 {
Sean Callanana5689d52012-07-12 18:04:03 +00003305 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3306 // We pretend these object files have no UUID to prevent crashing.
3307
3308 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3309 0x3b, 0xa8,
3310 0x4b, 0x16,
3311 0xb6, 0xa4,
3312 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3313
3314 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3315 return false;
3316
Greg Clayton9482f052012-03-13 23:14:29 +00003317 uuid->SetBytes (uuid_bytes);
3318 return true;
3319 }
3320 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003321 }
Greg Clayton9482f052012-03-13 23:14:29 +00003322 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003323 }
Chris Lattner24943d22010-06-08 16:52:24 +00003324 }
3325 return false;
3326}
3327
3328
3329uint32_t
3330ObjectFileMachO::GetDependentModules (FileSpecList& files)
3331{
Chris Lattner24943d22010-06-08 16:52:24 +00003332 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003333 ModuleSP module_sp(GetModule());
3334 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003335 {
Greg Clayton9482f052012-03-13 23:14:29 +00003336 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3337 struct load_command load_cmd;
3338 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3339 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3340 uint32_t i;
3341 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003342 {
Greg Clayton9482f052012-03-13 23:14:29 +00003343 const uint32_t cmd_offset = offset;
3344 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3345 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003346
Greg Clayton9482f052012-03-13 23:14:29 +00003347 switch (load_cmd.cmd)
3348 {
3349 case LoadCommandDylibLoad:
3350 case LoadCommandDylibLoadWeak:
3351 case LoadCommandDylibReexport:
3352 case LoadCommandDynamicLinkerLoad:
3353 case LoadCommandFixedVMShlibLoad:
3354 case LoadCommandDylibLoadUpward:
3355 {
3356 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3357 const char *path = m_data.PeekCStr(name_offset);
3358 // Skip any path that starts with '@' since these are usually:
3359 // @executable_path/.../file
3360 // @rpath/.../file
3361 if (path && path[0] != '@')
3362 {
3363 FileSpec file_spec(path, resolve_path);
3364 if (files.AppendIfUnique(file_spec))
3365 count++;
3366 }
3367 }
3368 break;
3369
3370 default:
3371 break;
3372 }
3373 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003374 }
Chris Lattner24943d22010-06-08 16:52:24 +00003375 }
3376 return count;
3377}
3378
Jim Ingham28775942011-03-07 23:44:08 +00003379lldb_private::Address
3380ObjectFileMachO::GetEntryPointAddress ()
3381{
3382 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3383 // is initialized to an invalid address, so we can just return that.
3384 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
3385
3386 if (!IsExecutable() || m_entry_point_address.IsValid())
3387 return m_entry_point_address;
3388
3389 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
3390 // /usr/include/mach-o.h, but it is basically:
3391 //
3392 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3393 // uint32_t count - this is the count of longs in the thread state data
3394 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3395 // <repeat this trio>
3396 //
3397 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3398 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3399 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3400 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3401 //
3402 // For now we hard-code the offsets and flavors we need:
3403 //
3404 //
3405
Greg Clayton9482f052012-03-13 23:14:29 +00003406 ModuleSP module_sp(GetModule());
3407 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003408 {
Greg Clayton9482f052012-03-13 23:14:29 +00003409 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3410 struct load_command load_cmd;
3411 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3412 uint32_t i;
3413 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3414 bool done = false;
3415
3416 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003417 {
Greg Clayton9482f052012-03-13 23:14:29 +00003418 const uint32_t cmd_offset = offset;
3419 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3420 break;
3421
3422 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003423 {
Greg Clayton9482f052012-03-13 23:14:29 +00003424 case LoadCommandUnixThread:
3425 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003426 {
Greg Clayton9482f052012-03-13 23:14:29 +00003427 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003428 {
Greg Clayton9482f052012-03-13 23:14:29 +00003429 uint32_t flavor = m_data.GetU32(&offset);
3430 uint32_t count = m_data.GetU32(&offset);
3431 if (count == 0)
3432 {
3433 // We've gotten off somehow, log and exit;
3434 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003435 }
Greg Clayton9482f052012-03-13 23:14:29 +00003436
3437 switch (m_header.cputype)
3438 {
3439 case llvm::MachO::CPUTypeARM:
3440 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3441 {
3442 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3443 start_address = m_data.GetU32(&offset);
3444 done = true;
3445 }
Jim Ingham28775942011-03-07 23:44:08 +00003446 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003447 case llvm::MachO::CPUTypeI386:
3448 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3449 {
3450 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3451 start_address = m_data.GetU32(&offset);
3452 done = true;
3453 }
3454 break;
3455 case llvm::MachO::CPUTypeX86_64:
3456 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3457 {
3458 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3459 start_address = m_data.GetU64(&offset);
3460 done = true;
3461 }
3462 break;
3463 default:
3464 return m_entry_point_address;
3465 }
3466 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3467 if (done)
3468 break;
3469 offset += count * 4;
3470 }
Jim Ingham28775942011-03-07 23:44:08 +00003471 }
Greg Clayton9482f052012-03-13 23:14:29 +00003472 break;
3473 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003474 {
Greg Clayton9482f052012-03-13 23:14:29 +00003475 ConstString text_segment_name ("__TEXT");
3476 uint64_t entryoffset = m_data.GetU64(&offset);
3477 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3478 if (text_segment_sp)
3479 {
3480 done = true;
3481 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3482 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003483 }
Greg Clayton9482f052012-03-13 23:14:29 +00003484
3485 default:
3486 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003487 }
Greg Clayton9482f052012-03-13 23:14:29 +00003488 if (done)
3489 break;
Jim Ingham28775942011-03-07 23:44:08 +00003490
Greg Clayton9482f052012-03-13 23:14:29 +00003491 // Go to the next load command:
3492 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003493 }
Jim Ingham28775942011-03-07 23:44:08 +00003494
Greg Clayton9482f052012-03-13 23:14:29 +00003495 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003496 {
Greg Clayton9482f052012-03-13 23:14:29 +00003497 // We got the start address from the load commands, so now resolve that address in the sections
3498 // of this ObjectFile:
3499 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003500 {
Greg Clayton9482f052012-03-13 23:14:29 +00003501 m_entry_point_address.Clear();
3502 }
3503 }
3504 else
3505 {
3506 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3507 // "start" symbol in the main executable.
3508
3509 ModuleSP module_sp (GetModule());
3510
3511 if (module_sp)
3512 {
3513 SymbolContextList contexts;
3514 SymbolContext context;
3515 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3516 {
3517 if (contexts.GetContextAtIndex(0, context))
3518 m_entry_point_address = context.symbol->GetAddress();
3519 }
Greg Clayton3508c382012-02-24 01:59:29 +00003520 }
3521 }
Jim Ingham28775942011-03-07 23:44:08 +00003522 }
3523
3524 return m_entry_point_address;
3525
3526}
3527
Greg Claytonb5a8f142012-02-05 02:38:54 +00003528lldb_private::Address
3529ObjectFileMachO::GetHeaderAddress ()
3530{
3531 lldb_private::Address header_addr;
3532 SectionList *section_list = GetSectionList();
3533 if (section_list)
3534 {
3535 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3536 if (text_segment_sp)
3537 {
Greg Clayton3508c382012-02-24 01:59:29 +00003538 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003539 header_addr.SetOffset (0);
3540 }
3541 }
3542 return header_addr;
3543}
3544
Greg Clayton46c9a352012-02-09 06:16:32 +00003545uint32_t
3546ObjectFileMachO::GetNumThreadContexts ()
3547{
Greg Clayton9482f052012-03-13 23:14:29 +00003548 ModuleSP module_sp(GetModule());
3549 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003550 {
Greg Clayton9482f052012-03-13 23:14:29 +00003551 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3552 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003553 {
Greg Clayton9482f052012-03-13 23:14:29 +00003554 m_thread_context_offsets_valid = true;
3555 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3556 FileRangeArray::Entry file_range;
3557 thread_command thread_cmd;
3558 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003559 {
Greg Clayton9482f052012-03-13 23:14:29 +00003560 const uint32_t cmd_offset = offset;
3561 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3562 break;
3563
3564 if (thread_cmd.cmd == LoadCommandThread)
3565 {
3566 file_range.SetRangeBase (offset);
3567 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3568 m_thread_context_offsets.Append (file_range);
3569 }
3570 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003571 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003572 }
3573 }
3574 return m_thread_context_offsets.GetSize();
3575}
3576
3577lldb::RegisterContextSP
3578ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3579{
Greg Clayton46c9a352012-02-09 06:16:32 +00003580 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003581
Greg Clayton9482f052012-03-13 23:14:29 +00003582 ModuleSP module_sp(GetModule());
3583 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003584 {
Greg Clayton9482f052012-03-13 23:14:29 +00003585 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3586 if (!m_thread_context_offsets_valid)
3587 GetNumThreadContexts ();
3588
3589 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003590 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003591 {
Jim Ingham6f01c932012-10-12 17:34:26 +00003592
3593 DataExtractor data (m_data,
3594 thread_context_file_range->GetRangeBase(),
3595 thread_context_file_range->GetByteSize());
3596
3597 switch (m_header.cputype)
3598 {
3599 case llvm::MachO::CPUTypeARM:
3600 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3601 break;
3602
3603 case llvm::MachO::CPUTypeI386:
3604 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3605 break;
3606
3607 case llvm::MachO::CPUTypeX86_64:
3608 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3609 break;
3610 }
Greg Clayton9482f052012-03-13 23:14:29 +00003611 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003612 }
3613 return reg_ctx_sp;
3614}
3615
Greg Claytonb5a8f142012-02-05 02:38:54 +00003616
Greg Claytonca319972011-07-09 00:41:34 +00003617ObjectFile::Type
3618ObjectFileMachO::CalculateType()
3619{
3620 switch (m_header.filetype)
3621 {
3622 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3623 if (GetAddressByteSize () == 4)
3624 {
3625 // 32 bit kexts are just object files, but they do have a valid
3626 // UUID load command.
3627 UUID uuid;
3628 if (GetUUID(&uuid))
3629 {
3630 // this checking for the UUID load command is not enough
3631 // we could eventually look for the symbol named
3632 // "OSKextGetCurrentIdentifier" as this is required of kexts
3633 if (m_strata == eStrataInvalid)
3634 m_strata = eStrataKernel;
3635 return eTypeSharedLibrary;
3636 }
3637 }
3638 return eTypeObjectFile;
3639
3640 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3641 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3642 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3643 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3644 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3645 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3646 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3647 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3648 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3649 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3650 default:
3651 break;
3652 }
3653 return eTypeUnknown;
3654}
3655
3656ObjectFile::Strata
3657ObjectFileMachO::CalculateStrata()
3658{
3659 switch (m_header.filetype)
3660 {
3661 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3662 {
3663 // 32 bit kexts are just object files, but they do have a valid
3664 // UUID load command.
3665 UUID uuid;
3666 if (GetUUID(&uuid))
3667 {
3668 // this checking for the UUID load command is not enough
3669 // we could eventually look for the symbol named
3670 // "OSKextGetCurrentIdentifier" as this is required of kexts
3671 if (m_type == eTypeInvalid)
3672 m_type = eTypeSharedLibrary;
3673
3674 return eStrataKernel;
3675 }
3676 }
3677 return eStrataUnknown;
3678
3679 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3680 // Check for the MH_DYLDLINK bit in the flags
3681 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003682 {
Greg Claytonca319972011-07-09 00:41:34 +00003683 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003684 }
3685 else
3686 {
3687 SectionList *section_list = GetSectionList();
3688 if (section_list)
3689 {
3690 static ConstString g_kld_section_name ("__KLD");
3691 if (section_list->FindSectionByName(g_kld_section_name))
3692 return eStrataKernel;
3693 }
3694 }
3695 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00003696
3697 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
3698 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00003699 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00003700 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
3701 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
3702 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
3703 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
3704 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
3705 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
3706 default:
3707 break;
3708 }
3709 return eStrataUnknown;
3710}
3711
3712
Greg Clayton49f4bf22012-02-22 19:41:02 +00003713uint32_t
3714ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
3715{
Greg Clayton9482f052012-03-13 23:14:29 +00003716 ModuleSP module_sp(GetModule());
3717 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003718 {
Greg Clayton9482f052012-03-13 23:14:29 +00003719 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3720 struct dylib_command load_cmd;
3721 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3722 uint32_t version_cmd = 0;
3723 uint64_t version = 0;
3724 uint32_t i;
3725 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003726 {
Greg Clayton9482f052012-03-13 23:14:29 +00003727 const uint32_t cmd_offset = offset;
3728 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3729 break;
3730
3731 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003732 {
Greg Clayton9482f052012-03-13 23:14:29 +00003733 if (version_cmd == 0)
3734 {
3735 version_cmd = load_cmd.cmd;
3736 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
3737 break;
3738 version = load_cmd.dylib.current_version;
3739 }
3740 break; // Break for now unless there is another more complete version
3741 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00003742 }
Greg Clayton9482f052012-03-13 23:14:29 +00003743 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003744 }
Greg Clayton9482f052012-03-13 23:14:29 +00003745
3746 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003747 {
Greg Clayton9482f052012-03-13 23:14:29 +00003748 if (versions != NULL && num_versions > 0)
3749 {
3750 if (num_versions > 0)
3751 versions[0] = (version & 0xFFFF0000ull) >> 16;
3752 if (num_versions > 1)
3753 versions[1] = (version & 0x0000FF00ull) >> 8;
3754 if (num_versions > 2)
3755 versions[2] = (version & 0x000000FFull);
3756 // Fill in an remaining version numbers with invalid values
3757 for (i=3; i<num_versions; ++i)
3758 versions[i] = UINT32_MAX;
3759 }
3760 // The LC_ID_DYLIB load command has a version with 3 version numbers
3761 // in it, so always return 3
3762 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003763 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00003764 }
3765 return false;
3766}
3767
Chris Lattner24943d22010-06-08 16:52:24 +00003768bool
Greg Clayton395fc332011-02-15 21:59:32 +00003769ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00003770{
Greg Clayton9482f052012-03-13 23:14:29 +00003771 ModuleSP module_sp(GetModule());
3772 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003773 {
Greg Clayton9482f052012-03-13 23:14:29 +00003774 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3775 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
3776
3777 // Files with type MH_PRELOAD are currently used in cases where the image
3778 // debugs at the addresses in the file itself. Below we set the OS to
3779 // unknown to make sure we use the DynamicLoaderStatic()...
3780 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
3781 {
3782 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
3783 }
3784 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003785 }
Greg Clayton9482f052012-03-13 23:14:29 +00003786 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003787}
3788
3789
3790//------------------------------------------------------------------
3791// PluginInterface protocol
3792//------------------------------------------------------------------
3793const char *
3794ObjectFileMachO::GetPluginName()
3795{
3796 return "ObjectFileMachO";
3797}
3798
3799const char *
3800ObjectFileMachO::GetShortPluginName()
3801{
3802 return GetPluginNameStatic();
3803}
3804
3805uint32_t
3806ObjectFileMachO::GetPluginVersion()
3807{
3808 return 1;
3809}
3810