blob: 92afb424f649dfc8a4f960d556805a0725157321 [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
565 if (SetModulesArchitecture (mach_arch))
566 {
567 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
568 if (m_data.GetByteSize() < header_and_lc_size)
569 {
570 DataBufferSP data_sp;
571 ProcessSP process_sp (m_process_wp.lock());
572 if (process_sp)
573 {
574 data_sp = ReadMemory (process_sp, m_offset, header_and_lc_size);
575 }
576 else
577 {
578 // Read in all only the load command data from the file on disk
579 data_sp = m_file.ReadFileContents(m_offset, header_and_lc_size);
580 if (data_sp->GetByteSize() != header_and_lc_size)
581 return false;
582 }
583 if (data_sp)
584 m_data.SetData (data_sp);
585 }
586 }
587 return true;
588 }
589 else
590 {
591 memset(&m_header, 0, sizeof(struct mach_header));
592 }
Chris Lattner24943d22010-06-08 16:52:24 +0000593 }
594 return false;
595}
596
597
598ByteOrder
599ObjectFileMachO::GetByteOrder () const
600{
Chris Lattner24943d22010-06-08 16:52:24 +0000601 return m_data.GetByteOrder ();
602}
603
Jim Ingham7508e732010-08-09 23:31:02 +0000604bool
605ObjectFileMachO::IsExecutable() const
606{
607 return m_header.filetype == HeaderFileTypeExecutable;
608}
Chris Lattner24943d22010-06-08 16:52:24 +0000609
610size_t
611ObjectFileMachO::GetAddressByteSize () const
612{
Chris Lattner24943d22010-06-08 16:52:24 +0000613 return m_data.GetAddressByteSize ();
614}
615
Greg Claytonb3448432011-03-24 21:19:54 +0000616AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000617ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
618{
619 Symtab *symtab = GetSymtab();
620 if (symtab)
621 {
622 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
623 if (symbol)
624 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000625 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000626 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000627 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000628 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000629 {
Greg Clayton3508c382012-02-24 01:59:29 +0000630 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000631 switch (section_type)
632 {
633 case eSectionTypeInvalid: return eAddressClassUnknown;
634 case eSectionTypeCode:
635 if (m_header.cputype == llvm::MachO::CPUTypeARM)
636 {
637 // For ARM we have a bit in the n_desc field of the symbol
638 // that tells us ARM/Thumb which is bit 0x0008.
639 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
640 return eAddressClassCodeAlternateISA;
641 }
642 return eAddressClassCode;
643
644 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000645 case eSectionTypeData:
646 case eSectionTypeDataCString:
647 case eSectionTypeDataCStringPointers:
648 case eSectionTypeDataSymbolAddress:
649 case eSectionTypeData4:
650 case eSectionTypeData8:
651 case eSectionTypeData16:
652 case eSectionTypeDataPointers:
653 case eSectionTypeZeroFill:
654 case eSectionTypeDataObjCMessageRefs:
655 case eSectionTypeDataObjCCFStrings:
656 return eAddressClassData;
657 case eSectionTypeDebug:
658 case eSectionTypeDWARFDebugAbbrev:
659 case eSectionTypeDWARFDebugAranges:
660 case eSectionTypeDWARFDebugFrame:
661 case eSectionTypeDWARFDebugInfo:
662 case eSectionTypeDWARFDebugLine:
663 case eSectionTypeDWARFDebugLoc:
664 case eSectionTypeDWARFDebugMacInfo:
665 case eSectionTypeDWARFDebugPubNames:
666 case eSectionTypeDWARFDebugPubTypes:
667 case eSectionTypeDWARFDebugRanges:
668 case eSectionTypeDWARFDebugStr:
669 case eSectionTypeDWARFAppleNames:
670 case eSectionTypeDWARFAppleTypes:
671 case eSectionTypeDWARFAppleNamespaces:
672 case eSectionTypeDWARFAppleObjC:
673 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000674 case eSectionTypeEHFrame: return eAddressClassRuntime;
675 case eSectionTypeOther: return eAddressClassUnknown;
676 }
677 }
678 }
679
Greg Claytonb3448432011-03-24 21:19:54 +0000680 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000681 switch (symbol_type)
682 {
683 case eSymbolTypeAny: return eAddressClassUnknown;
684 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Greg Claytonb1888f22011-03-19 01:12:21 +0000685
686 case eSymbolTypeCode:
687 case eSymbolTypeTrampoline:
688 if (m_header.cputype == llvm::MachO::CPUTypeARM)
689 {
690 // For ARM we have a bit in the n_desc field of the symbol
691 // that tells us ARM/Thumb which is bit 0x0008.
692 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
693 return eAddressClassCodeAlternateISA;
694 }
695 return eAddressClassCode;
696
697 case eSymbolTypeData: return eAddressClassData;
698 case eSymbolTypeRuntime: return eAddressClassRuntime;
699 case eSymbolTypeException: return eAddressClassRuntime;
700 case eSymbolTypeSourceFile: return eAddressClassDebug;
701 case eSymbolTypeHeaderFile: return eAddressClassDebug;
702 case eSymbolTypeObjectFile: return eAddressClassDebug;
703 case eSymbolTypeCommonBlock: return eAddressClassDebug;
704 case eSymbolTypeBlock: return eAddressClassDebug;
705 case eSymbolTypeLocal: return eAddressClassData;
706 case eSymbolTypeParam: return eAddressClassData;
707 case eSymbolTypeVariable: return eAddressClassData;
708 case eSymbolTypeVariableType: return eAddressClassDebug;
709 case eSymbolTypeLineEntry: return eAddressClassDebug;
710 case eSymbolTypeLineHeader: return eAddressClassDebug;
711 case eSymbolTypeScopeBegin: return eAddressClassDebug;
712 case eSymbolTypeScopeEnd: return eAddressClassDebug;
713 case eSymbolTypeAdditional: return eAddressClassUnknown;
714 case eSymbolTypeCompiler: return eAddressClassDebug;
715 case eSymbolTypeInstrumentation:return eAddressClassDebug;
716 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000717 case eSymbolTypeObjCClass: return eAddressClassRuntime;
718 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
719 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000720 }
721 }
722 }
723 return eAddressClassUnknown;
724}
Chris Lattner24943d22010-06-08 16:52:24 +0000725
726Symtab *
727ObjectFileMachO::GetSymtab()
728{
Greg Clayton9482f052012-03-13 23:14:29 +0000729 ModuleSP module_sp(GetModule());
730 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000731 {
Greg Clayton9482f052012-03-13 23:14:29 +0000732 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
733 if (m_symtab_ap.get() == NULL)
734 {
735 m_symtab_ap.reset(new Symtab(this));
736 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
737 ParseSymtab (true);
738 m_symtab_ap->Finalize ();
739 }
Chris Lattner24943d22010-06-08 16:52:24 +0000740 }
741 return m_symtab_ap.get();
742}
743
744
745SectionList *
746ObjectFileMachO::GetSectionList()
747{
Greg Clayton9482f052012-03-13 23:14:29 +0000748 ModuleSP module_sp(GetModule());
749 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000750 {
Greg Clayton9482f052012-03-13 23:14:29 +0000751 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
752 if (m_sections_ap.get() == NULL)
753 {
754 m_sections_ap.reset(new SectionList());
755 ParseSections();
756 }
Chris Lattner24943d22010-06-08 16:52:24 +0000757 }
758 return m_sections_ap.get();
759}
760
761
762size_t
763ObjectFileMachO::ParseSections ()
764{
765 lldb::user_id_t segID = 0;
766 lldb::user_id_t sectID = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000767 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
768 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000769 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000770 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000771 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000772 // First look up any LC_ENCRYPTION_INFO load commands
773 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
774 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000775 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000776 for (i=0; i<m_header.ncmds; ++i)
777 {
778 const uint32_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000779 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000780 break;
781
Greg Clayton54e33712012-05-25 18:09:55 +0000782 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000783 {
Greg Clayton54e33712012-05-25 18:09:55 +0000784 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
785 {
786 if (encryption_cmd.cryptid != 0)
787 {
788 EncryptedFileRanges::Entry entry;
789 entry.SetRangeBase(encryption_cmd.cryptoff);
790 entry.SetByteSize(encryption_cmd.cryptsize);
791 encrypted_file_ranges.Append(entry);
792 }
793 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000794 }
Greg Clayton54e33712012-05-25 18:09:55 +0000795 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000796 }
797
798 offset = MachHeaderSizeFromMagic(m_header.magic);
799
Greg Clayton54e33712012-05-25 18:09:55 +0000800 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000801 for (i=0; i<m_header.ncmds; ++i)
802 {
803 const uint32_t load_cmd_offset = offset;
804 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
805 break;
806
Greg Clayton1674b122010-07-21 22:12:05 +0000807 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000808 {
809 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
810 {
811 load_cmd.vmaddr = m_data.GetAddress(&offset);
812 load_cmd.vmsize = m_data.GetAddress(&offset);
813 load_cmd.fileoff = m_data.GetAddress(&offset);
814 load_cmd.filesize = m_data.GetAddress(&offset);
815 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
816 {
Greg Clayton68ca8232011-01-25 02:58:48 +0000817
818 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
819
Chris Lattner24943d22010-06-08 16:52:24 +0000820 // Keep a list of mach segments around in case we need to
821 // get at data that isn't stored in the abstracted Sections.
822 m_mach_segments.push_back (load_cmd);
823
824 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
825 // Use a segment ID of the segment index shifted left by 8 so they
826 // never conflict with any of the sections.
827 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000828 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000829 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000830 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000831 ++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
832 segment_name, // Name of this section
833 eSectionTypeContainer, // This section is a container of other sections.
834 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
835 load_cmd.vmsize, // VM size in bytes of this section
836 load_cmd.fileoff, // Offset to the data for this section in the file
837 load_cmd.filesize, // Size in bytes of this section as found in the the file
838 load_cmd.flags)); // Flags for this section
839
Greg Clayton68ca8232011-01-25 02:58:48 +0000840 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000841 m_sections_ap->AddSection(segment_sp);
842 }
843
844 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000845 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000846 // Push a section into our mach sections for the section at
Greg Clayton6af4fad2010-10-06 01:26:32 +0000847 // index zero (NListSectionNoSection) if we don't have any
848 // mach sections yet...
849 if (m_mach_sections.empty())
850 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000851 uint32_t segment_sect_idx;
852 const lldb::user_id_t first_segment_sectID = sectID + 1;
853
854
Greg Clayton1674b122010-07-21 22:12:05 +0000855 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000856 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
857 {
858 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
859 break;
860 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
861 break;
862 sect64.addr = m_data.GetAddress(&offset);
863 sect64.size = m_data.GetAddress(&offset);
864
865 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
866 break;
867
868 // Keep a list of mach sections around in case we need to
869 // get at data that isn't stored in the abstracted Sections.
870 m_mach_sections.push_back (sect64);
871
872 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
873 if (!segment_name)
874 {
875 // We have a segment with no name so we need to conjure up
876 // segments that correspond to the section's segname if there
877 // isn't already such a section. If there is such a section,
878 // we resize the section so that it spans all sections.
879 // We also mark these sections as fake so address matches don't
880 // hit if they land in the gaps between the child sections.
881 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
882 segment_sp = m_sections_ap->FindSectionByName (segment_name);
883 if (segment_sp.get())
884 {
885 Section *segment = segment_sp.get();
886 // Grow the section size as needed.
887 const lldb::addr_t sect64_min_addr = sect64.addr;
888 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
889 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
890 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
891 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
892 if (sect64_min_addr >= curr_seg_min_addr)
893 {
894 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
895 // Only grow the section size if needed
896 if (new_seg_byte_size > curr_seg_byte_size)
897 segment->SetByteSize (new_seg_byte_size);
898 }
899 else
900 {
901 // We need to change the base address of the segment and
902 // adjust the child section offsets for all existing children.
903 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
904 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +0000905 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000906 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
907 }
Greg Clayton661825b2010-06-28 23:51:11 +0000908
909 // Grow the section size as needed.
910 if (sect64.offset)
911 {
912 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
913 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
914
915 const lldb::addr_t section_min_file_offset = sect64.offset;
916 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
917 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
918 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
919 segment->SetFileOffset (new_file_offset);
920 segment->SetFileSize (new_file_size);
921 }
Chris Lattner24943d22010-06-08 16:52:24 +0000922 }
923 else
924 {
925 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +0000926 segment_sp.reset(new Section (segment_sp, // Parent section
927 module_sp, // Module to which this section belongs
928 ++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
929 segment_name, // Name of this section
930 eSectionTypeContainer, // This section is a container of other sections.
931 sect64.addr, // File VM address == addresses as they are found in the object file
932 sect64.size, // VM size in bytes of this section
933 sect64.offset, // Offset to the data for this section in the file
934 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
935 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +0000936 segment_sp->SetIsFake(true);
937 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000938 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000939 }
940 }
941 assert (segment_sp.get());
942
Greg Clayton1674b122010-07-21 22:12:05 +0000943 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000944 static ConstString g_sect_name_objc_data ("__objc_data");
945 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
946 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
947 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
948 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
949 static ConstString g_sect_name_objc_const ("__objc_const");
950 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
951 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000952
953 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
954 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
955 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
956 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
957 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
958 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
959 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
960 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
961 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
962 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
963 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +0000964 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
965 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +0000966 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +0000967 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000968 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +0000969 static ConstString g_sect_name_DATA ("__DATA");
970 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000971
Chris Lattner24943d22010-06-08 16:52:24 +0000972 SectionType sect_type = eSectionTypeOther;
973
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000974 if (section_name == g_sect_name_dwarf_debug_abbrev)
975 sect_type = eSectionTypeDWARFDebugAbbrev;
976 else if (section_name == g_sect_name_dwarf_debug_aranges)
977 sect_type = eSectionTypeDWARFDebugAranges;
978 else if (section_name == g_sect_name_dwarf_debug_frame)
979 sect_type = eSectionTypeDWARFDebugFrame;
980 else if (section_name == g_sect_name_dwarf_debug_info)
981 sect_type = eSectionTypeDWARFDebugInfo;
982 else if (section_name == g_sect_name_dwarf_debug_line)
983 sect_type = eSectionTypeDWARFDebugLine;
984 else if (section_name == g_sect_name_dwarf_debug_loc)
985 sect_type = eSectionTypeDWARFDebugLoc;
986 else if (section_name == g_sect_name_dwarf_debug_macinfo)
987 sect_type = eSectionTypeDWARFDebugMacInfo;
988 else if (section_name == g_sect_name_dwarf_debug_pubnames)
989 sect_type = eSectionTypeDWARFDebugPubNames;
990 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
991 sect_type = eSectionTypeDWARFDebugPubTypes;
992 else if (section_name == g_sect_name_dwarf_debug_ranges)
993 sect_type = eSectionTypeDWARFDebugRanges;
994 else if (section_name == g_sect_name_dwarf_debug_str)
995 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +0000996 else if (section_name == g_sect_name_dwarf_apple_names)
997 sect_type = eSectionTypeDWARFAppleNames;
998 else if (section_name == g_sect_name_dwarf_apple_types)
999 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001000 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1001 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001002 else if (section_name == g_sect_name_dwarf_apple_objc)
1003 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001004 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001005 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001006 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001007 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001008 else if (section_name == g_sect_name_eh_frame)
1009 sect_type = eSectionTypeEHFrame;
1010 else if (section_name == g_sect_name_cfstring)
1011 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001012 else if (section_name == g_sect_name_objc_data ||
1013 section_name == g_sect_name_objc_classrefs ||
1014 section_name == g_sect_name_objc_superrefs ||
1015 section_name == g_sect_name_objc_const ||
1016 section_name == g_sect_name_objc_classlist)
1017 {
1018 sect_type = eSectionTypeDataPointers;
1019 }
Chris Lattner24943d22010-06-08 16:52:24 +00001020
1021 if (sect_type == eSectionTypeOther)
1022 {
1023 switch (mach_sect_type)
1024 {
1025 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001026 case SectionTypeRegular:
1027 if (segment_sp->GetName() == g_sect_name_TEXT)
1028 sect_type = eSectionTypeCode;
1029 else if (segment_sp->GetName() == g_sect_name_DATA)
1030 sect_type = eSectionTypeData;
1031 else
1032 sect_type = eSectionTypeOther;
1033 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001034 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1035 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1036 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1037 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1038 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1039 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1040 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1041 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1042 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1043 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1044 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1045 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1046 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1047 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1048 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1049 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001050 default: break;
1051 }
1052 }
1053
Greg Clayton3508c382012-02-24 01:59:29 +00001054 SectionSP section_sp(new Section (segment_sp,
1055 module_sp,
1056 ++sectID,
1057 section_name,
1058 sect_type,
1059 sect64.addr - segment_sp->GetFileAddress(),
1060 sect64.size,
1061 sect64.offset,
1062 sect64.offset == 0 ? 0 : sect64.size,
1063 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001064 // Set the section to be encrypted to match the segment
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001065
1066 bool section_is_encrypted = false;
1067 if (!segment_is_encrypted && load_cmd.filesize != 0)
1068 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001069
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001070 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001071 segment_sp->GetChildren().AddSection(section_sp);
1072
1073 if (segment_sp->IsFake())
1074 {
1075 segment_sp.reset();
1076 segment_name.Clear();
1077 }
1078 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001079 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001080 {
1081 if (first_segment_sectID <= sectID)
1082 {
1083 lldb::user_id_t sect_uid;
1084 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1085 {
1086 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1087 SectionSP next_section_sp;
1088 if (sect_uid + 1 <= sectID)
1089 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1090
1091 if (curr_section_sp.get())
1092 {
1093 if (curr_section_sp->GetByteSize() == 0)
1094 {
1095 if (next_section_sp.get() != NULL)
1096 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1097 else
1098 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1099 }
1100 }
1101 }
1102 }
1103 }
1104 }
1105 }
1106 }
Greg Clayton1674b122010-07-21 22:12:05 +00001107 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001108 {
1109 m_dysymtab.cmd = load_cmd.cmd;
1110 m_dysymtab.cmdsize = load_cmd.cmdsize;
1111 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1112 }
1113
1114 offset = load_cmd_offset + load_cmd.cmdsize;
1115 }
1116// if (dump_sections)
1117// {
1118// StreamFile s(stdout);
1119// m_sections_ap->Dump(&s, true);
1120// }
1121 return sectID; // Return the number of sections we registered with the module
1122}
1123
1124class MachSymtabSectionInfo
1125{
1126public:
1127
1128 MachSymtabSectionInfo (SectionList *section_list) :
1129 m_section_list (section_list),
1130 m_section_infos()
1131 {
1132 // Get the number of sections down to a depth of 1 to include
1133 // all segments and their sections, but no other sections that
1134 // may be added for debug map or
1135 m_section_infos.resize(section_list->GetNumSections(1));
1136 }
1137
1138
Greg Clayton3508c382012-02-24 01:59:29 +00001139 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001140 GetSection (uint8_t n_sect, addr_t file_addr)
1141 {
1142 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001143 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001144 if (n_sect < m_section_infos.size())
1145 {
Greg Clayton3508c382012-02-24 01:59:29 +00001146 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001147 {
Greg Clayton3508c382012-02-24 01:59:29 +00001148 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1149 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001150 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001151 {
Greg Clayton3508c382012-02-24 01:59:29 +00001152 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1153 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001154 }
1155 else
1156 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001157 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001158 }
Chris Lattner24943d22010-06-08 16:52:24 +00001159 }
1160 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001161 {
1162 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001163 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001164 }
1165 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1166 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1167 {
1168 // Symbol is in section with zero size, but has the same start
1169 // address as the section. This can happen with linker symbols
1170 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001171 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001172 }
Chris Lattner24943d22010-06-08 16:52:24 +00001173 }
Greg Clayton3508c382012-02-24 01:59:29 +00001174 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001175 }
1176
1177protected:
1178 struct SectionInfo
1179 {
1180 SectionInfo () :
1181 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001182 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001183 {
1184 }
1185
1186 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001187 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001188 };
1189 SectionList *m_section_list;
1190 std::vector<SectionInfo> m_section_infos;
1191};
1192
Chris Lattner24943d22010-06-08 16:52:24 +00001193size_t
1194ObjectFileMachO::ParseSymtab (bool minimize)
1195{
1196 Timer scoped_timer(__PRETTY_FUNCTION__,
1197 "ObjectFileMachO::ParseSymtab () module = %s",
1198 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001199 ModuleSP module_sp (GetModule());
1200 if (!module_sp)
1201 return 0;
1202
1203 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1204 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1205 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1206 FunctionStarts function_starts;
Chris Lattner24943d22010-06-08 16:52:24 +00001207 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1208 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001209
Greg Clayton0fea0512011-12-30 00:32:24 +00001210 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
1211
Chris Lattner24943d22010-06-08 16:52:24 +00001212 for (i=0; i<m_header.ncmds; ++i)
1213 {
1214 const uint32_t cmd_offset = offset;
1215 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001216 struct load_command lc;
1217 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001218 break;
1219 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001220 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001221 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001222 case LoadCommandSymtab:
1223 symtab_load_command.cmd = lc.cmd;
1224 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001225 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001226 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1227 return 0;
1228 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001229 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001230 if (log)
1231 module_sp->LogMessage(log.get(), "LC_SYMTAB.symoff == 0");
1232 return 0;
1233 }
1234
1235 if (symtab_load_command.stroff == 0)
1236 {
1237 if (log)
1238 module_sp->LogMessage(log.get(), "LC_SYMTAB.stroff == 0");
1239 return 0;
1240 }
1241
1242 if (symtab_load_command.nsyms == 0)
1243 {
1244 if (log)
1245 module_sp->LogMessage(log.get(), "LC_SYMTAB.nsyms == 0");
1246 return 0;
1247 }
1248
1249 if (symtab_load_command.strsize == 0)
1250 {
1251 if (log)
1252 module_sp->LogMessage(log.get(), "LC_SYMTAB.strsize == 0");
1253 return 0;
1254 }
1255 break;
1256
1257 case LoadCommandFunctionStarts:
1258 function_starts_load_command.cmd = lc.cmd;
1259 function_starts_load_command.cmdsize = lc.cmdsize;
1260 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1261 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1262 break;
1263
1264 default:
1265 break;
1266 }
1267 offset = cmd_offset + lc.cmdsize;
1268 }
1269
1270 if (symtab_load_command.cmd)
1271 {
1272 Symtab *symtab = m_symtab_ap.get();
1273 SectionList *section_list = GetSectionList();
1274 if (section_list == NULL)
1275 return 0;
1276
1277 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001278 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001279
1280 const size_t addr_byte_size = m_data.GetAddressByteSize();
1281 bool bit_width_32 = addr_byte_size == 4;
1282 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1283
1284 DataExtractor nlist_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1285 DataExtractor strtab_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1286 DataExtractor function_starts_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1287
1288 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1289 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001290 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1291 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001292 {
Greg Claytondd29b972012-05-18 23:20:01 +00001293 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001294 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1295 // Reading mach file from memory in a process or core file...
1296
1297 if (linkedit_section_sp)
1298 {
1299 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1300 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1301 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001302 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001303
1304 bool data_was_read = false;
1305
1306#if defined (__APPLE__) && defined (__arm__)
1307 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001308 {
Greg Clayton29021d32012-04-18 05:19:20 +00001309 // This mach-o memory file is in the dyld shared cache. If this
1310 // program is not remote and this is iOS, then this process will
1311 // share the same shared cache as the process we are debugging and
1312 // we can read the entire __LINKEDIT from the address space in this
1313 // process. This is a needed optimization that is used for local iOS
1314 // debugging only since all shared libraries in the shared cache do
1315 // not have corresponding files that exist in the file system of the
1316 // device. They have been combined into a single file. This means we
1317 // always have to load these files from memory. All of the symbol and
1318 // string tables from all of the __LINKEDIT sections from the shared
1319 // libraries in the shared cache have been merged into a single large
1320 // symbol and string table. Reading all of this symbol and string table
1321 // data across can slow down debug launch times, so we optimize this by
1322 // reading the memory for the __LINKEDIT section from this process.
1323 PlatformSP platform_sp (target.GetPlatform());
1324 if (platform_sp && platform_sp->IsHost())
1325 {
1326 data_was_read = true;
1327 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001328 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001329 if (function_starts_load_command.cmd)
1330 {
1331 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1332 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1333 }
1334 }
1335 }
1336#endif
1337
1338 if (!data_was_read)
1339 {
1340 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1341 if (nlist_data_sp)
1342 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001343 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1344 //if (strtab_data_sp)
1345 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Greg Clayton29021d32012-04-18 05:19:20 +00001346 if (function_starts_load_command.cmd)
1347 {
1348 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1349 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1350 if (func_start_data_sp)
1351 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1352 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001353 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001354 }
1355 }
1356 else
1357 {
1358 nlist_data.SetData (m_data,
1359 symtab_load_command.symoff,
1360 nlist_data_byte_size);
1361 strtab_data.SetData (m_data,
1362 symtab_load_command.stroff,
1363 strtab_data_byte_size);
1364 if (function_starts_load_command.cmd)
1365 {
1366 function_starts_data.SetData (m_data,
1367 function_starts_load_command.dataoff,
1368 function_starts_load_command.datasize);
1369 }
1370 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001371
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001372 if (nlist_data.GetByteSize() == 0)
1373 {
1374 if (log)
1375 module_sp->LogMessage(log.get(), "failed to read nlist data");
1376 return 0;
1377 }
1378
1379
Greg Clayton3a5dc012012-05-25 17:04:00 +00001380 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1381 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001382 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001383 if (process)
1384 {
1385 if (strtab_addr == LLDB_INVALID_ADDRESS)
1386 {
1387 if (log)
1388 module_sp->LogMessage(log.get(), "failed to locate the strtab in memory");
1389 return 0;
1390 }
1391 }
1392 else
Greg Claytondd29b972012-05-18 23:20:01 +00001393 {
1394 if (log)
Greg Clayton3a5dc012012-05-25 17:04:00 +00001395 module_sp->LogMessage(log.get(), "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001396 return 0;
1397 }
1398 }
Greg Claytondd29b972012-05-18 23:20:01 +00001399
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001400 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1401 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1402 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1403 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1404 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1405 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1406 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1407 SectionSP eh_frame_section_sp;
1408 if (text_section_sp.get())
1409 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1410 else
1411 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1412
Greg Claytond2653c22012-03-14 01:53:24 +00001413 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001414 if (text_section_sp && function_starts_data.GetByteSize())
1415 {
1416 FunctionStarts::Entry function_start_entry;
1417 function_start_entry.data = false;
1418 uint32_t function_start_offset = 0;
1419 function_start_entry.addr = text_section_sp->GetFileAddress();
1420 uint64_t delta;
1421 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1422 {
1423 // Now append the current entry
1424 function_start_entry.addr += delta;
1425 function_starts.Append(function_start_entry);
1426 }
1427 }
1428
1429 const uint32_t function_starts_count = function_starts.GetSize();
1430
1431 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
1432
1433 uint32_t nlist_data_offset = 0;
1434
1435 uint32_t N_SO_index = UINT32_MAX;
1436
1437 MachSymtabSectionInfo section_info (section_list);
1438 std::vector<uint32_t> N_FUN_indexes;
1439 std::vector<uint32_t> N_NSYM_indexes;
1440 std::vector<uint32_t> N_INCL_indexes;
1441 std::vector<uint32_t> N_BRAC_indexes;
1442 std::vector<uint32_t> N_COMM_indexes;
1443 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1444 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1445 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1446 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1447 // Any symbols that get merged into another will get an entry
1448 // in this map so we know
1449 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1450 uint32_t nlist_idx = 0;
1451 Symbol *symbol_ptr = NULL;
1452
1453 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001454 Symbol *sym = NULL;
1455 uint32_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001456 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001457 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001458
Jason Molendab62abd52012-06-21 01:51:02 +00001459#if defined (__APPLE__) && defined (__arm__)
1460
1461 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1462 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1463 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1464 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1465 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1466 // nlist parser to ignore all LOCAL symbols.
1467
1468 if (m_header.flags & 0x80000000u)
1469 {
1470 // Before we can start mapping the DSC, we need to make certain the target process is actually
1471 // using the cache we can find.
1472
1473 /*
1474 * TODO (FIXME!)
1475 *
1476 * Consider the case of testing with a separate DSC file.
1477 * If we go through the normal code paths, we will give symbols for the wrong DSC, and
1478 * that is bad. We need to read the target process' all_image_infos struct, and look
1479 * at the values of the processDetachedFromSharedRegion field. If that is set, we should skip
1480 * this code section.
1481 */
1482
1483 // Next we need to determine the correct path for the dyld shared cache.
1484
1485 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1486 char dsc_path[PATH_MAX];
1487
1488 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
1489 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1490 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
1491 header_arch.GetArchitectureName());
1492
1493 FileSpec dsc_filespec(dsc_path, false);
1494
1495 // We need definitions of two structures in the on-disk DSC, copy them here manually
Greg Claytonab77dcb2012-09-05 22:30:51 +00001496 struct lldb_copy_dyld_cache_header
1497 {
1498 char magic[16];
1499 uint32_t mappingOffset;
1500 uint32_t mappingCount;
1501 uint32_t imagesOffset;
1502 uint32_t imagesCount;
1503 uint64_t dyldBaseAddress;
1504 uint64_t codeSignatureOffset;
1505 uint64_t codeSignatureSize;
1506 uint64_t slideInfoOffset;
1507 uint64_t slideInfoSize;
1508 uint64_t localSymbolsOffset;
1509 uint64_t localSymbolsSize;
1510 };
1511 struct lldb_copy_dyld_cache_local_symbols_info
1512 {
1513 uint32_t nlistOffset;
1514 uint32_t nlistCount;
1515 uint32_t stringsOffset;
1516 uint32_t stringsSize;
1517 uint32_t entriesOffset;
1518 uint32_t entriesCount;
1519 };
1520 struct lldb_copy_dyld_cache_local_symbols_entry
1521 {
1522 uint32_t dylibOffset;
1523 uint32_t nlistStartIndex;
1524 uint32_t nlistCount;
1525 };
Jason Molendab62abd52012-06-21 01:51:02 +00001526
Jason Molendafd3b35d2012-06-22 03:28:35 +00001527 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1528 The dyld_cache_local_symbols_info structure gives us three things:
1529 1. The start and count of the nlist records in the dyld_shared_cache file
1530 2. The start and size of the strings for these nlist records
1531 3. The start and count of dyld_cache_local_symbols_entry entries
1532
1533 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1534 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
1535 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
1536 and the count of how many nlist records there are for this dylib/framework.
1537 */
1538
Jason Molendab62abd52012-06-21 01:51:02 +00001539 // Process the dsc header to find the unmapped symbols
1540 //
1541 // Save some VM space, do not map the entire cache in one shot.
1542
1543 if (DataBufferSP dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header)))
1544 {
1545 DataExtractor dsc_header_data(dsc_data_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1546
Jason Molendafd3b35d2012-06-22 03:28:35 +00001547 uint32_t offset = offsetof (struct lldb_copy_dyld_cache_header, mappingOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001548 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1549
1550 // If the mappingOffset points to a location inside the header, we've
1551 // opened an old dyld shared cache, and should not proceed further.
1552 if (mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header))
1553 {
1554
Jason Molendafd3b35d2012-06-22 03:28:35 +00001555 offset = offsetof (struct lldb_copy_dyld_cache_header, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001556 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1557 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1558
1559 if (localSymbolsOffset && localSymbolsSize)
1560 {
1561 // Map the local symbols
1562 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
1563 {
1564 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1565
1566 offset = 0;
1567
1568 // Read the local_symbols_infos struct in one shot
1569 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1570 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1571
1572 // The local_symbols_infos offsets are offsets into local symbols memory, NOT file offsets!
1573 // We first need to identify the local "entry" that matches the current header.
1574 // The "entry" is stored as a file offset in the dyld_shared_cache, so we need to
1575 // adjust the raw m_header value by slide and 0x30000000.
1576
1577 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1578
1579 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - 0x30000000);
1580
1581 offset = local_symbols_info.entriesOffset;
1582 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1583 {
1584 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1585 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1586 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1587 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1588
1589 if (header_file_offset == local_symbols_entry.dylibOffset)
1590 {
1591 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1592
1593 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1594 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1595 num_syms = symtab->GetNumSymbols();
1596
1597 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1598 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1599
1600 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
1601 {
1602 /////////////////////////////
1603 {
1604 struct nlist_64 nlist;
1605 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1606 break;
1607
1608 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1609 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1610 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1611 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1612 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1613
1614 SymbolType type = eSymbolTypeInvalid;
1615 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1616
1617 if (symbol_name == NULL)
1618 {
1619 // No symbol should be NULL, even the symbols with no
1620 // string values should have an offset zero which points
1621 // to an empty C-string
1622 Host::SystemLog (Host::eSystemLogError,
1623 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1624 entry_index,
1625 nlist.n_strx,
1626 module_sp->GetFileSpec().GetDirectory().GetCString(),
1627 module_sp->GetFileSpec().GetFilename().GetCString());
1628 continue;
1629 }
1630 if (symbol_name[0] == '\0')
1631 symbol_name = NULL;
1632
1633 const char *symbol_name_non_abi_mangled = NULL;
1634
1635 SectionSP symbol_section;
1636 uint32_t symbol_byte_size = 0;
1637 bool add_nlist = true;
1638 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
1639
1640 assert (sym_idx < num_syms);
1641
1642 sym[sym_idx].SetDebug (is_debug);
1643
1644 if (is_debug)
1645 {
1646 switch (nlist.n_type)
1647 {
1648 case StabGlobalSymbol:
1649 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1650 // Sometimes the N_GSYM value contains the address.
1651
1652 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1653 // have the same address, but we want to ensure that we always find only the real symbol,
1654 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1655 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1656 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1657 // same address.
1658
1659 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1660 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1661 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1662 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1663 add_nlist = false;
1664 else
1665 {
1666 sym[sym_idx].SetExternal(true);
1667 if (nlist.n_value != 0)
1668 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1669 type = eSymbolTypeData;
1670 }
1671 break;
1672
1673 case StabFunctionName:
1674 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1675 type = eSymbolTypeCompiler;
1676 break;
1677
1678 case StabFunction:
1679 // N_FUN -- procedure: name,,n_sect,linenumber,address
1680 if (symbol_name)
1681 {
1682 type = eSymbolTypeCode;
1683 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1684
1685 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1686 // We use the current number of symbols in the symbol table in lieu of
1687 // using nlist_idx in case we ever start trimming entries out
1688 N_FUN_indexes.push_back(sym_idx);
1689 }
1690 else
1691 {
1692 type = eSymbolTypeCompiler;
1693
1694 if ( !N_FUN_indexes.empty() )
1695 {
1696 // Copy the size of the function into the original STAB entry so we don't have
1697 // to hunt for it later
1698 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1699 N_FUN_indexes.pop_back();
1700 // We don't really need the end function STAB as it contains the size which
1701 // we already placed with the original symbol, so don't add it if we want a
1702 // minimal symbol table
1703 if (minimize)
1704 add_nlist = false;
1705 }
1706 }
1707 break;
1708
1709 case StabStaticSymbol:
1710 // N_STSYM -- static symbol: name,,n_sect,type,address
1711 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1712 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1713 type = eSymbolTypeData;
1714 break;
1715
1716 case StabLocalCommon:
1717 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1718 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1719 type = eSymbolTypeCommonBlock;
1720 break;
1721
1722 case StabBeginSymbol:
1723 // N_BNSYM
1724 // We use the current number of symbols in the symbol table in lieu of
1725 // using nlist_idx in case we ever start trimming entries out
1726 if (minimize)
1727 {
1728 // Skip these if we want minimal symbol tables
1729 add_nlist = false;
1730 }
1731 else
1732 {
1733 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1734 N_NSYM_indexes.push_back(sym_idx);
1735 type = eSymbolTypeScopeBegin;
1736 }
1737 break;
1738
1739 case StabEndSymbol:
1740 // N_ENSYM
1741 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1742 // so that we can always skip the entire symbol if we need to navigate
1743 // more quickly at the source level when parsing STABS
1744 if (minimize)
1745 {
1746 // Skip these if we want minimal symbol tables
1747 add_nlist = false;
1748 }
1749 else
1750 {
1751 if ( !N_NSYM_indexes.empty() )
1752 {
1753 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1754 symbol_ptr->SetByteSize(sym_idx + 1);
1755 symbol_ptr->SetSizeIsSibling(true);
1756 N_NSYM_indexes.pop_back();
1757 }
1758 type = eSymbolTypeScopeEnd;
1759 }
1760 break;
1761
1762
1763 case StabSourceFileOptions:
1764 // N_OPT - emitted with gcc2_compiled and in gcc source
1765 type = eSymbolTypeCompiler;
1766 break;
1767
1768 case StabRegisterSymbol:
1769 // N_RSYM - register sym: name,,NO_SECT,type,register
1770 type = eSymbolTypeVariable;
1771 break;
1772
1773 case StabSourceLine:
1774 // N_SLINE - src line: 0,,n_sect,linenumber,address
1775 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1776 type = eSymbolTypeLineEntry;
1777 break;
1778
1779 case StabStructureType:
1780 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1781 type = eSymbolTypeVariableType;
1782 break;
1783
1784 case StabSourceFileName:
1785 // N_SO - source file name
1786 type = eSymbolTypeSourceFile;
1787 if (symbol_name == NULL)
1788 {
1789 if (minimize)
1790 add_nlist = false;
1791 if (N_SO_index != UINT32_MAX)
1792 {
1793 // Set the size of the N_SO to the terminating index of this N_SO
1794 // so that we can always skip the entire N_SO if we need to navigate
1795 // more quickly at the source level when parsing STABS
1796 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1797 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1798 symbol_ptr->SetSizeIsSibling(true);
1799 }
1800 N_NSYM_indexes.clear();
1801 N_INCL_indexes.clear();
1802 N_BRAC_indexes.clear();
1803 N_COMM_indexes.clear();
1804 N_FUN_indexes.clear();
1805 N_SO_index = UINT32_MAX;
1806 }
1807 else
1808 {
1809 // We use the current number of symbols in the symbol table in lieu of
1810 // using nlist_idx in case we ever start trimming entries out
1811 const bool N_SO_has_full_path = symbol_name[0] == '/';
1812 if (N_SO_has_full_path)
1813 {
1814 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1815 {
1816 // We have two consecutive N_SO entries where the first contains a directory
1817 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00001818 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001819 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1820 add_nlist = false;
1821 }
1822 else
1823 {
1824 // This is the first entry in a N_SO that contains a directory or
1825 // a full path to the source file
1826 N_SO_index = sym_idx;
1827 }
1828 }
1829 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1830 {
1831 // This is usually the second N_SO entry that contains just the filename,
1832 // so here we combine it with the first one if we are minimizing the symbol table
1833 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1834 if (so_path && so_path[0])
1835 {
1836 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00001837 const size_t double_slash_pos = full_so_path.find("//");
1838 if (double_slash_pos != std::string::npos)
1839 {
1840 // The linker has been generating bad N_SO entries with doubled up paths
1841 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
1842 // and the second is the directory for the source file so you end up with
1843 // a path that looks like "/tmp/src//tmp/src/"
1844 FileSpec so_dir(so_path, false);
1845 if (!so_dir.Exists())
1846 {
1847 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
1848 if (so_dir.Exists())
1849 {
1850 // Trim off the incorrect path
1851 full_so_path.erase(0, double_slash_pos + 1);
1852 }
1853 }
1854 }
Jason Molendab62abd52012-06-21 01:51:02 +00001855 if (*full_so_path.rbegin() != '/')
1856 full_so_path += '/';
1857 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00001858 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001859 add_nlist = false;
1860 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1861 }
1862 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00001863 else
1864 {
1865 // This could be a relative path to a N_SO
1866 N_SO_index = sym_idx;
1867 }
Jason Molendab62abd52012-06-21 01:51:02 +00001868 }
Jason Molendab62abd52012-06-21 01:51:02 +00001869 break;
1870
1871 case StabObjectFileName:
1872 // N_OSO - object file name: name,,0,0,st_mtime
1873 type = eSymbolTypeObjectFile;
1874 break;
1875
1876 case StabLocalSymbol:
1877 // N_LSYM - local sym: name,,NO_SECT,type,offset
1878 type = eSymbolTypeLocal;
1879 break;
1880
1881 //----------------------------------------------------------------------
1882 // INCL scopes
1883 //----------------------------------------------------------------------
1884 case StabBeginIncludeFileName:
1885 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
1886 // We use the current number of symbols in the symbol table in lieu of
1887 // using nlist_idx in case we ever start trimming entries out
1888 N_INCL_indexes.push_back(sym_idx);
1889 type = eSymbolTypeScopeBegin;
1890 break;
1891
1892 case StabEndIncludeFile:
1893 // N_EINCL - include file end: name,,NO_SECT,0,0
1894 // Set the size of the N_BINCL to the terminating index of this N_EINCL
1895 // so that we can always skip the entire symbol if we need to navigate
1896 // more quickly at the source level when parsing STABS
1897 if ( !N_INCL_indexes.empty() )
1898 {
1899 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1900 symbol_ptr->SetByteSize(sym_idx + 1);
1901 symbol_ptr->SetSizeIsSibling(true);
1902 N_INCL_indexes.pop_back();
1903 }
1904 type = eSymbolTypeScopeEnd;
1905 break;
1906
1907 case StabIncludeFileName:
1908 // N_SOL - #included file name: name,,n_sect,0,address
1909 type = eSymbolTypeHeaderFile;
1910
1911 // We currently don't use the header files on darwin
1912 if (minimize)
1913 add_nlist = false;
1914 break;
1915
1916 case StabCompilerParameters:
1917 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
1918 type = eSymbolTypeCompiler;
1919 break;
1920
1921 case StabCompilerVersion:
1922 // N_VERSION - compiler version: name,,NO_SECT,0,0
1923 type = eSymbolTypeCompiler;
1924 break;
1925
1926 case StabCompilerOptLevel:
1927 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
1928 type = eSymbolTypeCompiler;
1929 break;
1930
1931 case StabParameter:
1932 // N_PSYM - parameter: name,,NO_SECT,type,offset
1933 type = eSymbolTypeVariable;
1934 break;
1935
1936 case StabAlternateEntry:
1937 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
1938 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1939 type = eSymbolTypeLineEntry;
1940 break;
1941
1942 //----------------------------------------------------------------------
1943 // Left and Right Braces
1944 //----------------------------------------------------------------------
1945 case StabLeftBracket:
1946 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
1947 // We use the current number of symbols in the symbol table in lieu of
1948 // using nlist_idx in case we ever start trimming entries out
1949 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1950 N_BRAC_indexes.push_back(sym_idx);
1951 type = eSymbolTypeScopeBegin;
1952 break;
1953
1954 case StabRightBracket:
1955 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
1956 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
1957 // so that we can always skip the entire symbol if we need to navigate
1958 // more quickly at the source level when parsing STABS
1959 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1960 if ( !N_BRAC_indexes.empty() )
1961 {
1962 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1963 symbol_ptr->SetByteSize(sym_idx + 1);
1964 symbol_ptr->SetSizeIsSibling(true);
1965 N_BRAC_indexes.pop_back();
1966 }
1967 type = eSymbolTypeScopeEnd;
1968 break;
1969
1970 case StabDeletedIncludeFile:
1971 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
1972 type = eSymbolTypeHeaderFile;
1973 break;
1974
1975 //----------------------------------------------------------------------
1976 // COMM scopes
1977 //----------------------------------------------------------------------
1978 case StabBeginCommon:
1979 // N_BCOMM - begin common: name,,NO_SECT,0,0
1980 // We use the current number of symbols in the symbol table in lieu of
1981 // using nlist_idx in case we ever start trimming entries out
1982 type = eSymbolTypeScopeBegin;
1983 N_COMM_indexes.push_back(sym_idx);
1984 break;
1985
1986 case StabEndCommonLocal:
1987 // N_ECOML - end common (local name): 0,,n_sect,0,address
1988 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1989 // Fall through
1990
1991 case StabEndCommon:
1992 // N_ECOMM - end common: name,,n_sect,0,0
1993 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1994 // so that we can always skip the entire symbol if we need to navigate
1995 // more quickly at the source level when parsing STABS
1996 if ( !N_COMM_indexes.empty() )
1997 {
1998 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1999 symbol_ptr->SetByteSize(sym_idx + 1);
2000 symbol_ptr->SetSizeIsSibling(true);
2001 N_COMM_indexes.pop_back();
2002 }
2003 type = eSymbolTypeScopeEnd;
2004 break;
2005
2006 case StabLength:
2007 // N_LENG - second stab entry with length information
2008 type = eSymbolTypeAdditional;
2009 break;
2010
2011 default: break;
2012 }
2013 }
2014 else
2015 {
2016 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2017 uint8_t n_type = NlistMaskType & nlist.n_type;
2018 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2019
2020 switch (n_type)
2021 {
2022 case NListTypeIndirect: // N_INDR - Fall through
2023 case NListTypePreboundUndefined:// N_PBUD - Fall through
2024 case NListTypeUndefined: // N_UNDF
2025 type = eSymbolTypeUndefined;
2026 break;
2027
2028 case NListTypeAbsolute: // N_ABS
2029 type = eSymbolTypeAbsolute;
2030 break;
2031
2032 case NListTypeSection: // N_SECT
2033 {
2034 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2035
2036 if (symbol_section == NULL)
2037 {
2038 // TODO: warn about this?
2039 add_nlist = false;
2040 break;
2041 }
2042
2043 if (TEXT_eh_frame_sectID == nlist.n_sect)
2044 {
2045 type = eSymbolTypeException;
2046 }
2047 else
2048 {
2049 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2050
2051 switch (section_type)
2052 {
2053 case SectionTypeRegular: break; // regular section
2054 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2055 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2056 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2057 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2058 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2059 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2060 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2061 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2062 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2063 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2064 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2065 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2066 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2067 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2068 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2069 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2070 default: break;
2071 }
2072
2073 if (type == eSymbolTypeInvalid)
2074 {
2075 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2076 if (symbol_section->IsDescendant (text_section_sp.get()))
2077 {
2078 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2079 SectionAttrUserSelfModifyingCode |
2080 SectionAttrSytemSomeInstructions))
2081 type = eSymbolTypeData;
2082 else
2083 type = eSymbolTypeCode;
2084 }
2085 else
2086 if (symbol_section->IsDescendant(data_section_sp.get()))
2087 {
2088 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2089 {
2090 type = eSymbolTypeRuntime;
2091
2092 if (symbol_name &&
2093 symbol_name[0] == '_' &&
2094 symbol_name[1] == 'O' &&
2095 symbol_name[2] == 'B')
2096 {
2097 llvm::StringRef symbol_name_ref(symbol_name);
2098 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2099 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2100 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2101 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2102 {
2103 symbol_name_non_abi_mangled = symbol_name + 1;
2104 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2105 type = eSymbolTypeObjCClass;
2106 }
2107 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2108 {
2109 symbol_name_non_abi_mangled = symbol_name + 1;
2110 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2111 type = eSymbolTypeObjCMetaClass;
2112 }
2113 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2114 {
2115 symbol_name_non_abi_mangled = symbol_name + 1;
2116 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2117 type = eSymbolTypeObjCIVar;
2118 }
2119 }
2120 }
2121 else
2122 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
2123 {
2124 type = eSymbolTypeException;
2125 }
2126 else
2127 {
2128 type = eSymbolTypeData;
2129 }
2130 }
2131 else
2132 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2133 {
2134 type = eSymbolTypeTrampoline;
2135 }
2136 else
2137 if (symbol_section->IsDescendant(objc_section_sp.get()))
2138 {
2139 type = eSymbolTypeRuntime;
2140 if (symbol_name && symbol_name[0] == '.')
2141 {
2142 llvm::StringRef symbol_name_ref(symbol_name);
2143 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2144 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
2145 {
2146 symbol_name_non_abi_mangled = symbol_name;
2147 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2148 type = eSymbolTypeObjCClass;
2149 }
2150 }
2151 }
2152 }
2153 }
2154 }
2155 break;
2156 }
2157 }
2158
2159 if (add_nlist)
2160 {
2161 uint64_t symbol_value = nlist.n_value;
2162 bool symbol_name_is_mangled = false;
2163
2164 if (symbol_name_non_abi_mangled)
2165 {
Jason Molenda292cca82012-07-20 03:35:44 +00002166 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2167 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002168 }
2169 else
2170 {
2171 if (symbol_name && symbol_name[0] == '_')
2172 {
2173 symbol_name_is_mangled = symbol_name[1] == '_';
2174 symbol_name++; // Skip the leading underscore
2175 }
2176
2177 if (symbol_name)
2178 {
Jason Molenda292cca82012-07-20 03:35:44 +00002179 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002180 }
2181 }
2182
2183 if (is_debug == false)
2184 {
2185 if (type == eSymbolTypeCode)
2186 {
2187 // See if we can find a N_FUN entry for any code symbols.
2188 // If we do find a match, and the name matches, then we
2189 // can merge the two into just the function symbol to avoid
2190 // duplicate entries in the symbol table
2191 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2192 if (pos != N_FUN_addr_to_sym_idx.end())
2193 {
2194 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2195 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2196 {
2197 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2198 // We just need the flags from the linker symbol, so put these flags
2199 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2200 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2201 sym[sym_idx].Clear();
2202 continue;
2203 }
2204 }
2205 }
2206 else if (type == eSymbolTypeData)
2207 {
2208 // See if we can find a N_STSYM entry for any data symbols.
2209 // If we do find a match, and the name matches, then we
2210 // can merge the two into just the Static symbol to avoid
2211 // duplicate entries in the symbol table
2212 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2213 if (pos != N_STSYM_addr_to_sym_idx.end())
2214 {
2215 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2216 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2217 {
2218 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2219 // We just need the flags from the linker symbol, so put these flags
2220 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2221 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2222 sym[sym_idx].Clear();
2223 continue;
2224 }
2225 }
2226 }
2227 }
2228 if (symbol_section)
2229 {
2230 const addr_t section_file_addr = symbol_section->GetFileAddress();
2231 if (symbol_byte_size == 0 && function_starts_count > 0)
2232 {
2233 addr_t symbol_lookup_file_addr = nlist.n_value;
2234 // Do an exact address match for non-ARM addresses, else get the closest since
2235 // the symbol might be a thumb symbol which has an address with bit zero set
2236 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2237 if (is_arm && func_start_entry)
2238 {
2239 // Verify that the function start address is the symbol address (ARM)
2240 // or the symbol address + 1 (thumb)
2241 if (func_start_entry->addr != symbol_lookup_file_addr &&
2242 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2243 {
2244 // Not the right entry, NULL it out...
2245 func_start_entry = NULL;
2246 }
2247 }
2248 if (func_start_entry)
2249 {
2250 func_start_entry->data = true;
2251
2252 addr_t symbol_file_addr = func_start_entry->addr;
2253 uint32_t symbol_flags = 0;
2254 if (is_arm)
2255 {
2256 if (symbol_file_addr & 1)
2257 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2258 symbol_file_addr &= 0xfffffffffffffffeull;
2259 }
2260
2261 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2262 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2263 if (next_func_start_entry)
2264 {
2265 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2266 // Be sure the clear the Thumb address bit when we calculate the size
2267 // from the current and next address
2268 if (is_arm)
2269 next_symbol_file_addr &= 0xfffffffffffffffeull;
2270 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2271 }
2272 else
2273 {
2274 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2275 }
2276 }
2277 }
2278 symbol_value -= section_file_addr;
2279 }
2280
2281 sym[sym_idx].SetID (nlist_idx);
2282 sym[sym_idx].SetType (type);
2283 sym[sym_idx].GetAddress().SetSection (symbol_section);
2284 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2285 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2286
2287 if (symbol_byte_size > 0)
2288 sym[sym_idx].SetByteSize(symbol_byte_size);
2289
2290 ++sym_idx;
2291 }
2292 else
2293 {
2294 sym[sym_idx].Clear();
2295 }
2296
2297 }
2298 /////////////////////////////
2299 }
2300 break; // No more entries to consider
2301 }
2302 }
2303 }
2304 }
2305 }
2306 }
2307 }
2308
2309 // Must reset this in case it was mutated above!
2310 nlist_data_offset = 0;
2311#endif
2312
2313 // If the sym array was not created while parsing the DSC unmapped
2314 // symbols, create it now.
2315 if (sym == NULL)
2316 {
2317 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2318 num_syms = symtab->GetNumSymbols();
2319 }
2320
2321 if (unmapped_local_symbols_found)
2322 {
2323 assert(m_dysymtab.ilocalsym == 0);
2324 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2325 nlist_idx = m_dysymtab.nlocalsym;
2326 }
2327 else
2328 {
2329 nlist_idx = 0;
2330 }
2331
2332 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002333 {
2334 struct nlist_64 nlist;
2335 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2336 break;
2337
2338 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2339 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2340 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2341 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2342 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2343
2344 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002345 const char *symbol_name = NULL;
2346
Greg Clayton3a5dc012012-05-25 17:04:00 +00002347 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002348 {
2349 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
2350
2351 if (symbol_name == NULL)
2352 {
2353 // No symbol should be NULL, even the symbols with no
2354 // string values should have an offset zero which points
2355 // to an empty C-string
2356 Host::SystemLog (Host::eSystemLogError,
2357 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
2358 nlist_idx,
2359 nlist.n_strx,
2360 module_sp->GetFileSpec().GetDirectory().GetCString(),
2361 module_sp->GetFileSpec().GetFilename().GetCString());
2362 continue;
2363 }
2364 if (symbol_name[0] == '\0')
2365 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002366 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002367 else
2368 {
2369 const addr_t str_addr = strtab_addr + nlist.n_strx;
2370 Error str_error;
2371 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2372 symbol_name = memory_symbol_name.c_str();
2373 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002374 const char *symbol_name_non_abi_mangled = NULL;
2375
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002376 SectionSP symbol_section;
2377 uint32_t symbol_byte_size = 0;
2378 bool add_nlist = true;
2379 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
2380
2381 assert (sym_idx < num_syms);
2382
2383 sym[sym_idx].SetDebug (is_debug);
2384
2385 if (is_debug)
2386 {
2387 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002388 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002389 case StabGlobalSymbol:
2390 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2391 // Sometimes the N_GSYM value contains the address.
2392
2393 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2394 // have the same address, but we want to ensure that we always find only the real symbol,
2395 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2396 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2397 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2398 // same address.
2399
2400 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
2401 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2402 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2403 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2404 add_nlist = false;
2405 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002406 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002407 sym[sym_idx].SetExternal(true);
2408 if (nlist.n_value != 0)
2409 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2410 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002411 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002412 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002413
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002414 case StabFunctionName:
2415 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2416 type = eSymbolTypeCompiler;
2417 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002418
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002419 case StabFunction:
2420 // N_FUN -- procedure: name,,n_sect,linenumber,address
2421 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002422 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002423 type = eSymbolTypeCode;
2424 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2425
2426 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2427 // We use the current number of symbols in the symbol table in lieu of
2428 // using nlist_idx in case we ever start trimming entries out
2429 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002430 }
2431 else
2432 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002433 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002434
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002435 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002436 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002437 // Copy the size of the function into the original STAB entry so we don't have
2438 // to hunt for it later
2439 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2440 N_FUN_indexes.pop_back();
2441 // We don't really need the end function STAB as it contains the size which
2442 // we already placed with the original symbol, so don't add it if we want a
2443 // minimal symbol table
2444 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002445 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002446 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002447 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002448 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002449
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002450 case StabStaticSymbol:
2451 // N_STSYM -- static symbol: name,,n_sect,type,address
2452 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2453 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2454 type = eSymbolTypeData;
2455 break;
2456
2457 case StabLocalCommon:
2458 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2459 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2460 type = eSymbolTypeCommonBlock;
2461 break;
2462
2463 case StabBeginSymbol:
2464 // N_BNSYM
2465 // We use the current number of symbols in the symbol table in lieu of
2466 // using nlist_idx in case we ever start trimming entries out
2467 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002468 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002469 // Skip these if we want minimal symbol tables
2470 add_nlist = false;
2471 }
2472 else
2473 {
2474 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2475 N_NSYM_indexes.push_back(sym_idx);
2476 type = eSymbolTypeScopeBegin;
2477 }
2478 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002479
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002480 case StabEndSymbol:
2481 // N_ENSYM
2482 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2483 // so that we can always skip the entire symbol if we need to navigate
2484 // more quickly at the source level when parsing STABS
2485 if (minimize)
2486 {
2487 // Skip these if we want minimal symbol tables
2488 add_nlist = false;
2489 }
2490 else
2491 {
2492 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002493 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002494 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2495 symbol_ptr->SetByteSize(sym_idx + 1);
2496 symbol_ptr->SetSizeIsSibling(true);
2497 N_NSYM_indexes.pop_back();
2498 }
2499 type = eSymbolTypeScopeEnd;
2500 }
2501 break;
2502
2503
2504 case StabSourceFileOptions:
2505 // N_OPT - emitted with gcc2_compiled and in gcc source
2506 type = eSymbolTypeCompiler;
2507 break;
2508
2509 case StabRegisterSymbol:
2510 // N_RSYM - register sym: name,,NO_SECT,type,register
2511 type = eSymbolTypeVariable;
2512 break;
2513
2514 case StabSourceLine:
2515 // N_SLINE - src line: 0,,n_sect,linenumber,address
2516 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2517 type = eSymbolTypeLineEntry;
2518 break;
2519
2520 case StabStructureType:
2521 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2522 type = eSymbolTypeVariableType;
2523 break;
2524
2525 case StabSourceFileName:
2526 // N_SO - source file name
2527 type = eSymbolTypeSourceFile;
2528 if (symbol_name == NULL)
2529 {
2530 if (minimize)
2531 add_nlist = false;
2532 if (N_SO_index != UINT32_MAX)
2533 {
2534 // Set the size of the N_SO to the terminating index of this N_SO
2535 // so that we can always skip the entire N_SO if we need to navigate
2536 // more quickly at the source level when parsing STABS
2537 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2538 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2539 symbol_ptr->SetSizeIsSibling(true);
2540 }
2541 N_NSYM_indexes.clear();
2542 N_INCL_indexes.clear();
2543 N_BRAC_indexes.clear();
2544 N_COMM_indexes.clear();
2545 N_FUN_indexes.clear();
2546 N_SO_index = UINT32_MAX;
2547 }
2548 else
2549 {
2550 // We use the current number of symbols in the symbol table in lieu of
2551 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002552 const bool N_SO_has_full_path = symbol_name[0] == '/';
2553 if (N_SO_has_full_path)
2554 {
2555 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2556 {
2557 // We have two consecutive N_SO entries where the first contains a directory
2558 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002559 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002560 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2561 add_nlist = false;
2562 }
2563 else
2564 {
2565 // This is the first entry in a N_SO that contains a directory or
2566 // a full path to the source file
2567 N_SO_index = sym_idx;
2568 }
2569 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002570 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2571 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002572 // This is usually the second N_SO entry that contains just the filename,
2573 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002574 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2575 if (so_path && so_path[0])
2576 {
2577 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002578 const size_t double_slash_pos = full_so_path.find("//");
2579 if (double_slash_pos != std::string::npos)
2580 {
2581 // The linker has been generating bad N_SO entries with doubled up paths
2582 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2583 // and the second is the directory for the source file so you end up with
2584 // a path that looks like "/tmp/src//tmp/src/"
2585 FileSpec so_dir(so_path, false);
2586 if (!so_dir.Exists())
2587 {
2588 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2589 if (so_dir.Exists())
2590 {
2591 // Trim off the incorrect path
2592 full_so_path.erase(0, double_slash_pos + 1);
2593 }
2594 }
2595 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002596 if (*full_so_path.rbegin() != '/')
2597 full_so_path += '/';
2598 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002599 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002600 add_nlist = false;
2601 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2602 }
2603 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002604 else
2605 {
2606 // This could be a relative path to a N_SO
2607 N_SO_index = sym_idx;
2608 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002609 }
2610
2611 break;
2612
2613 case StabObjectFileName:
2614 // N_OSO - object file name: name,,0,0,st_mtime
2615 type = eSymbolTypeObjectFile;
2616 break;
2617
2618 case StabLocalSymbol:
2619 // N_LSYM - local sym: name,,NO_SECT,type,offset
2620 type = eSymbolTypeLocal;
2621 break;
2622
2623 //----------------------------------------------------------------------
2624 // INCL scopes
2625 //----------------------------------------------------------------------
2626 case StabBeginIncludeFileName:
2627 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2628 // We use the current number of symbols in the symbol table in lieu of
2629 // using nlist_idx in case we ever start trimming entries out
2630 N_INCL_indexes.push_back(sym_idx);
2631 type = eSymbolTypeScopeBegin;
2632 break;
2633
2634 case StabEndIncludeFile:
2635 // N_EINCL - include file end: name,,NO_SECT,0,0
2636 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2637 // so that we can always skip the entire symbol if we need to navigate
2638 // more quickly at the source level when parsing STABS
2639 if ( !N_INCL_indexes.empty() )
2640 {
2641 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2642 symbol_ptr->SetByteSize(sym_idx + 1);
2643 symbol_ptr->SetSizeIsSibling(true);
2644 N_INCL_indexes.pop_back();
2645 }
2646 type = eSymbolTypeScopeEnd;
2647 break;
2648
2649 case StabIncludeFileName:
2650 // N_SOL - #included file name: name,,n_sect,0,address
2651 type = eSymbolTypeHeaderFile;
2652
2653 // We currently don't use the header files on darwin
2654 if (minimize)
2655 add_nlist = false;
2656 break;
2657
2658 case StabCompilerParameters:
2659 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2660 type = eSymbolTypeCompiler;
2661 break;
2662
2663 case StabCompilerVersion:
2664 // N_VERSION - compiler version: name,,NO_SECT,0,0
2665 type = eSymbolTypeCompiler;
2666 break;
2667
2668 case StabCompilerOptLevel:
2669 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2670 type = eSymbolTypeCompiler;
2671 break;
2672
2673 case StabParameter:
2674 // N_PSYM - parameter: name,,NO_SECT,type,offset
2675 type = eSymbolTypeVariable;
2676 break;
2677
2678 case StabAlternateEntry:
2679 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2680 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2681 type = eSymbolTypeLineEntry;
2682 break;
2683
2684 //----------------------------------------------------------------------
2685 // Left and Right Braces
2686 //----------------------------------------------------------------------
2687 case StabLeftBracket:
2688 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2689 // We use the current number of symbols in the symbol table in lieu of
2690 // using nlist_idx in case we ever start trimming entries out
2691 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2692 N_BRAC_indexes.push_back(sym_idx);
2693 type = eSymbolTypeScopeBegin;
2694 break;
2695
2696 case StabRightBracket:
2697 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2698 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2699 // so that we can always skip the entire symbol if we need to navigate
2700 // more quickly at the source level when parsing STABS
2701 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2702 if ( !N_BRAC_indexes.empty() )
2703 {
2704 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2705 symbol_ptr->SetByteSize(sym_idx + 1);
2706 symbol_ptr->SetSizeIsSibling(true);
2707 N_BRAC_indexes.pop_back();
2708 }
2709 type = eSymbolTypeScopeEnd;
2710 break;
2711
2712 case StabDeletedIncludeFile:
2713 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2714 type = eSymbolTypeHeaderFile;
2715 break;
2716
2717 //----------------------------------------------------------------------
2718 // COMM scopes
2719 //----------------------------------------------------------------------
2720 case StabBeginCommon:
2721 // N_BCOMM - begin common: name,,NO_SECT,0,0
2722 // We use the current number of symbols in the symbol table in lieu of
2723 // using nlist_idx in case we ever start trimming entries out
2724 type = eSymbolTypeScopeBegin;
2725 N_COMM_indexes.push_back(sym_idx);
2726 break;
2727
2728 case StabEndCommonLocal:
2729 // N_ECOML - end common (local name): 0,,n_sect,0,address
2730 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2731 // Fall through
2732
2733 case StabEndCommon:
2734 // N_ECOMM - end common: name,,n_sect,0,0
2735 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2736 // so that we can always skip the entire symbol if we need to navigate
2737 // more quickly at the source level when parsing STABS
2738 if ( !N_COMM_indexes.empty() )
2739 {
2740 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2741 symbol_ptr->SetByteSize(sym_idx + 1);
2742 symbol_ptr->SetSizeIsSibling(true);
2743 N_COMM_indexes.pop_back();
2744 }
2745 type = eSymbolTypeScopeEnd;
2746 break;
2747
2748 case StabLength:
2749 // N_LENG - second stab entry with length information
2750 type = eSymbolTypeAdditional;
2751 break;
2752
2753 default: break;
2754 }
2755 }
2756 else
2757 {
2758 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2759 uint8_t n_type = NlistMaskType & nlist.n_type;
2760 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2761
2762 switch (n_type)
2763 {
2764 case NListTypeIndirect: // N_INDR - Fall through
2765 case NListTypePreboundUndefined:// N_PBUD - Fall through
2766 case NListTypeUndefined: // N_UNDF
2767 type = eSymbolTypeUndefined;
2768 break;
2769
2770 case NListTypeAbsolute: // N_ABS
2771 type = eSymbolTypeAbsolute;
2772 break;
2773
2774 case NListTypeSection: // N_SECT
2775 {
2776 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2777
Sean Callananb386d822012-08-09 00:50:26 +00002778 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002779 {
2780 // TODO: warn about this?
2781 add_nlist = false;
2782 break;
2783 }
2784
2785 if (TEXT_eh_frame_sectID == nlist.n_sect)
2786 {
2787 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00002788 }
2789 else
2790 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002791 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2792
2793 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00002794 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002795 case SectionTypeRegular: break; // regular section
2796 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2797 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2798 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2799 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2800 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2801 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2802 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2803 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2804 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2805 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2806 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2807 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2808 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2809 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2810 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2811 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2812 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002813 }
Chris Lattner24943d22010-06-08 16:52:24 +00002814
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002815 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002816 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002817 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2818 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002819 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002820 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2821 SectionAttrUserSelfModifyingCode |
2822 SectionAttrSytemSomeInstructions))
2823 type = eSymbolTypeData;
2824 else
2825 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00002826 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002827 else
2828 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002829 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002830 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00002831 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002832 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00002833
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002834 if (symbol_name &&
2835 symbol_name[0] == '_' &&
2836 symbol_name[1] == 'O' &&
2837 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00002838 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002839 llvm::StringRef symbol_name_ref(symbol_name);
2840 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2841 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2842 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2843 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00002844 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002845 symbol_name_non_abi_mangled = symbol_name + 1;
2846 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2847 type = eSymbolTypeObjCClass;
Chris Lattner24943d22010-06-08 16:52:24 +00002848 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002849 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00002850 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002851 symbol_name_non_abi_mangled = symbol_name + 1;
2852 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2853 type = eSymbolTypeObjCMetaClass;
2854 }
2855 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2856 {
2857 symbol_name_non_abi_mangled = symbol_name + 1;
2858 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2859 type = eSymbolTypeObjCIVar;
Chris Lattner24943d22010-06-08 16:52:24 +00002860 }
2861 }
2862 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002863 else
2864 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
2865 {
2866 type = eSymbolTypeException;
2867 }
2868 else
2869 {
2870 type = eSymbolTypeData;
2871 }
2872 }
2873 else
2874 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2875 {
2876 type = eSymbolTypeTrampoline;
2877 }
2878 else
2879 if (symbol_section->IsDescendant(objc_section_sp.get()))
2880 {
2881 type = eSymbolTypeRuntime;
2882 if (symbol_name && symbol_name[0] == '.')
2883 {
2884 llvm::StringRef symbol_name_ref(symbol_name);
2885 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2886 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
2887 {
2888 symbol_name_non_abi_mangled = symbol_name;
2889 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2890 type = eSymbolTypeObjCClass;
2891 }
2892 }
Chris Lattner24943d22010-06-08 16:52:24 +00002893 }
2894 }
2895 }
2896 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002897 break;
2898 }
2899 }
2900
2901 if (add_nlist)
2902 {
2903 uint64_t symbol_value = nlist.n_value;
2904 bool symbol_name_is_mangled = false;
2905
2906 if (symbol_name_non_abi_mangled)
2907 {
Greg Claytonc0240042012-07-18 23:18:10 +00002908 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2909 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00002910 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002911 else
2912 {
2913 if (symbol_name && symbol_name[0] == '_')
2914 {
2915 symbol_name_is_mangled = symbol_name[1] == '_';
2916 symbol_name++; // Skip the leading underscore
2917 }
2918
2919 if (symbol_name)
2920 {
Greg Claytonc0240042012-07-18 23:18:10 +00002921 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002922 }
2923 }
2924
2925 if (is_debug == false)
2926 {
2927 if (type == eSymbolTypeCode)
2928 {
2929 // See if we can find a N_FUN entry for any code symbols.
2930 // If we do find a match, and the name matches, then we
2931 // can merge the two into just the function symbol to avoid
2932 // duplicate entries in the symbol table
2933 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2934 if (pos != N_FUN_addr_to_sym_idx.end())
2935 {
2936 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2937 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2938 {
2939 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2940 // We just need the flags from the linker symbol, so put these flags
2941 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2942 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2943 sym[sym_idx].Clear();
2944 continue;
2945 }
2946 }
2947 }
2948 else if (type == eSymbolTypeData)
2949 {
2950 // See if we can find a N_STSYM entry for any data symbols.
2951 // If we do find a match, and the name matches, then we
2952 // can merge the two into just the Static symbol to avoid
2953 // duplicate entries in the symbol table
2954 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2955 if (pos != N_STSYM_addr_to_sym_idx.end())
2956 {
2957 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2958 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2959 {
2960 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2961 // We just need the flags from the linker symbol, so put these flags
2962 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2963 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2964 sym[sym_idx].Clear();
2965 continue;
2966 }
2967 }
2968 }
2969 }
2970 if (symbol_section)
2971 {
2972 const addr_t section_file_addr = symbol_section->GetFileAddress();
2973 if (symbol_byte_size == 0 && function_starts_count > 0)
2974 {
Greg Claytond2653c22012-03-14 01:53:24 +00002975 addr_t symbol_lookup_file_addr = nlist.n_value;
2976 // Do an exact address match for non-ARM addresses, else get the closest since
2977 // the symbol might be a thumb symbol which has an address with bit zero set
2978 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2979 if (is_arm && func_start_entry)
2980 {
2981 // Verify that the function start address is the symbol address (ARM)
2982 // or the symbol address + 1 (thumb)
2983 if (func_start_entry->addr != symbol_lookup_file_addr &&
2984 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2985 {
2986 // Not the right entry, NULL it out...
2987 func_start_entry = NULL;
2988 }
2989 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002990 if (func_start_entry)
2991 {
2992 func_start_entry->data = true;
Greg Claytond2653c22012-03-14 01:53:24 +00002993
2994 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00002995 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00002996 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00002997
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002998 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2999 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3000 if (next_func_start_entry)
3001 {
Greg Claytond2653c22012-03-14 01:53:24 +00003002 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3003 // Be sure the clear the Thumb address bit when we calculate the size
3004 // from the current and next address
3005 if (is_arm)
3006 next_symbol_file_addr &= 0xfffffffffffffffeull;
3007 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 +00003008 }
3009 else
3010 {
Greg Claytond2653c22012-03-14 01:53:24 +00003011 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003012 }
3013 }
3014 }
3015 symbol_value -= section_file_addr;
3016 }
3017
3018 sym[sym_idx].SetID (nlist_idx);
3019 sym[sym_idx].SetType (type);
3020 sym[sym_idx].GetAddress().SetSection (symbol_section);
3021 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3022 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3023
3024 if (symbol_byte_size > 0)
3025 sym[sym_idx].SetByteSize(symbol_byte_size);
3026
3027 ++sym_idx;
3028 }
3029 else
3030 {
3031 sym[sym_idx].Clear();
3032 }
3033
3034 }
3035
3036 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3037 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3038 // such entries by figuring out what the address for the global is by looking up this non-STAB
3039 // entry and copying the value into the debug symbol's value to save us the hassle in the
3040 // debug symbol parser.
3041
3042 Symbol *global_symbol = NULL;
3043 for (nlist_idx = 0;
3044 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3045 nlist_idx++)
3046 {
3047 if (global_symbol->GetAddress().GetFileAddress() == 0)
3048 {
3049 std::vector<uint32_t> indexes;
3050 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3051 {
3052 std::vector<uint32_t>::const_iterator pos;
3053 std::vector<uint32_t>::const_iterator end = indexes.end();
3054 for (pos = indexes.begin(); pos != end; ++pos)
3055 {
3056 symbol_ptr = symtab->SymbolAtIndex(*pos);
3057 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3058 {
3059 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3060 break;
3061 }
3062 }
3063 }
Chris Lattner24943d22010-06-08 16:52:24 +00003064 }
3065 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003066
3067 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3068
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003069 if (function_starts_count > 0)
3070 {
3071 char synthetic_function_symbol[PATH_MAX];
3072 uint32_t num_synthetic_function_symbols = 0;
3073 for (i=0; i<function_starts_count; ++i)
3074 {
3075 if (function_starts.GetEntryRef (i).data == false)
3076 ++num_synthetic_function_symbols;
3077 }
3078
3079 if (num_synthetic_function_symbols > 0)
3080 {
3081 if (num_syms < sym_idx + num_synthetic_function_symbols)
3082 {
3083 num_syms = sym_idx + num_synthetic_function_symbols;
3084 sym = symtab->Resize (num_syms);
3085 }
3086 uint32_t synthetic_function_symbol_idx = 0;
3087 for (i=0; i<function_starts_count; ++i)
3088 {
3089 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3090 if (func_start_entry->data == false)
3091 {
Greg Claytond2653c22012-03-14 01:53:24 +00003092 addr_t symbol_file_addr = func_start_entry->addr;
3093 uint32_t symbol_flags = 0;
3094 if (is_arm)
3095 {
3096 if (symbol_file_addr & 1)
3097 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3098 symbol_file_addr &= 0xfffffffffffffffeull;
3099 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003100 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003101 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003102 {
3103 SectionSP symbol_section (symbol_addr.GetSection());
3104 uint32_t symbol_byte_size = 0;
3105 if (symbol_section)
3106 {
3107 const addr_t section_file_addr = symbol_section->GetFileAddress();
3108 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3109 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3110 if (next_func_start_entry)
3111 {
Greg Claytond2653c22012-03-14 01:53:24 +00003112 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3113 if (is_arm)
3114 next_symbol_file_addr &= 0xfffffffffffffffeull;
3115 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 +00003116 }
3117 else
3118 {
Greg Claytond2653c22012-03-14 01:53:24 +00003119 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003120 }
3121 snprintf (synthetic_function_symbol,
3122 sizeof(synthetic_function_symbol),
3123 "___lldb_unnamed_function%u$$%s",
3124 ++synthetic_function_symbol_idx,
3125 module_sp->GetFileSpec().GetFilename().GetCString());
3126 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003127 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003128 sym[sym_idx].SetType (eSymbolTypeCode);
3129 sym[sym_idx].SetIsSynthetic (true);
3130 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003131 if (symbol_flags)
3132 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003133 if (symbol_byte_size)
3134 sym[sym_idx].SetByteSize (symbol_byte_size);
3135 ++sym_idx;
3136 }
3137 }
3138 }
3139 }
3140 }
3141 }
3142
3143 // Trim our symbols down to just what we ended up with after
3144 // removing any symbols.
3145 if (sym_idx < num_syms)
3146 {
3147 num_syms = sym_idx;
3148 sym = symtab->Resize (num_syms);
3149 }
3150
3151 // Now synthesize indirect symbols
3152 if (m_dysymtab.nindirectsyms != 0)
3153 {
3154 DataExtractor indirect_symbol_index_data (m_data, m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4);
3155
3156 if (indirect_symbol_index_data.GetByteSize())
3157 {
3158 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3159
3160 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3161 {
3162 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3163 {
3164 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3165 if (symbol_stub_byte_size == 0)
3166 continue;
3167
3168 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3169
3170 if (num_symbol_stubs == 0)
3171 continue;
3172
3173 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3174 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3175 {
3176 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3177 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
3178 uint32_t symbol_stub_offset = symbol_stub_index * 4;
3179 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3180 {
3181 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3182 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3183 continue;
3184
3185 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3186 Symbol *stub_symbol = NULL;
3187 if (index_pos != end_index_pos)
3188 {
3189 // We have a remapping from the original nlist index to
3190 // a current symbol index, so just look this up by index
3191 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3192 }
3193 else
3194 {
3195 // We need to lookup a symbol using the original nlist
3196 // symbol index since this index is coming from the
3197 // S_SYMBOL_STUBS
3198 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3199 }
3200
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003201 if (stub_symbol)
3202 {
3203 Address so_addr(symbol_stub_addr, section_list);
3204
3205 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3206 {
3207 // Change the external symbol into a trampoline that makes sense
3208 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3209 // can re-use them so we don't have to make up a synthetic symbol
3210 // for no good reason.
3211 stub_symbol->SetType (eSymbolTypeTrampoline);
3212 stub_symbol->SetExternal (false);
3213 stub_symbol->GetAddress() = so_addr;
3214 stub_symbol->SetByteSize (symbol_stub_byte_size);
3215 }
3216 else
3217 {
3218 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003219 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003220 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003221 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003222 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003223 stub_symbol = NULL; // this pointer no longer valid
3224 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003225 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003226 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003227 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3228 sym[sym_idx].SetIsSynthetic (true);
3229 sym[sym_idx].GetAddress() = so_addr;
3230 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3231 ++sym_idx;
3232 }
3233 }
Greg Claytond4330e62012-09-05 01:38:55 +00003234 else
3235 {
3236 if (log)
3237 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3238 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003239 }
3240 }
3241 }
3242 }
3243 }
3244 }
3245 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003246 }
3247 return 0;
3248}
3249
3250
3251void
3252ObjectFileMachO::Dump (Stream *s)
3253{
Greg Clayton9482f052012-03-13 23:14:29 +00003254 ModuleSP module_sp(GetModule());
3255 if (module_sp)
3256 {
3257 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3258 s->Printf("%p: ", this);
3259 s->Indent();
3260 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3261 s->PutCString("ObjectFileMachO64");
3262 else
3263 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003264
Greg Clayton9482f052012-03-13 23:14:29 +00003265 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003266
Greg Clayton9482f052012-03-13 23:14:29 +00003267 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003268
Greg Clayton9482f052012-03-13 23:14:29 +00003269 if (m_sections_ap.get())
3270 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003271
Greg Clayton9482f052012-03-13 23:14:29 +00003272 if (m_symtab_ap.get())
3273 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3274 }
Chris Lattner24943d22010-06-08 16:52:24 +00003275}
3276
3277
3278bool
Greg Clayton0467c782011-02-04 18:53:10 +00003279ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003280{
Greg Clayton9482f052012-03-13 23:14:29 +00003281 ModuleSP module_sp(GetModule());
3282 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003283 {
Greg Clayton9482f052012-03-13 23:14:29 +00003284 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3285 struct uuid_command load_cmd;
3286 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3287 uint32_t i;
3288 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003289 {
Greg Clayton9482f052012-03-13 23:14:29 +00003290 const uint32_t cmd_offset = offset;
3291 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3292 break;
3293
3294 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00003295 {
Greg Clayton9482f052012-03-13 23:14:29 +00003296 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
Sean Callanana5689d52012-07-12 18:04:03 +00003297
Greg Clayton9482f052012-03-13 23:14:29 +00003298 if (uuid_bytes)
3299 {
Sean Callanana5689d52012-07-12 18:04:03 +00003300 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3301 // We pretend these object files have no UUID to prevent crashing.
3302
3303 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3304 0x3b, 0xa8,
3305 0x4b, 0x16,
3306 0xb6, 0xa4,
3307 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3308
3309 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3310 return false;
3311
Greg Clayton9482f052012-03-13 23:14:29 +00003312 uuid->SetBytes (uuid_bytes);
3313 return true;
3314 }
3315 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003316 }
Greg Clayton9482f052012-03-13 23:14:29 +00003317 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003318 }
Chris Lattner24943d22010-06-08 16:52:24 +00003319 }
3320 return false;
3321}
3322
3323
3324uint32_t
3325ObjectFileMachO::GetDependentModules (FileSpecList& files)
3326{
Chris Lattner24943d22010-06-08 16:52:24 +00003327 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003328 ModuleSP module_sp(GetModule());
3329 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003330 {
Greg Clayton9482f052012-03-13 23:14:29 +00003331 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3332 struct load_command load_cmd;
3333 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3334 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3335 uint32_t i;
3336 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003337 {
Greg Clayton9482f052012-03-13 23:14:29 +00003338 const uint32_t cmd_offset = offset;
3339 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3340 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003341
Greg Clayton9482f052012-03-13 23:14:29 +00003342 switch (load_cmd.cmd)
3343 {
3344 case LoadCommandDylibLoad:
3345 case LoadCommandDylibLoadWeak:
3346 case LoadCommandDylibReexport:
3347 case LoadCommandDynamicLinkerLoad:
3348 case LoadCommandFixedVMShlibLoad:
3349 case LoadCommandDylibLoadUpward:
3350 {
3351 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3352 const char *path = m_data.PeekCStr(name_offset);
3353 // Skip any path that starts with '@' since these are usually:
3354 // @executable_path/.../file
3355 // @rpath/.../file
3356 if (path && path[0] != '@')
3357 {
3358 FileSpec file_spec(path, resolve_path);
3359 if (files.AppendIfUnique(file_spec))
3360 count++;
3361 }
3362 }
3363 break;
3364
3365 default:
3366 break;
3367 }
3368 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003369 }
Chris Lattner24943d22010-06-08 16:52:24 +00003370 }
3371 return count;
3372}
3373
Jim Ingham28775942011-03-07 23:44:08 +00003374lldb_private::Address
3375ObjectFileMachO::GetEntryPointAddress ()
3376{
3377 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3378 // is initialized to an invalid address, so we can just return that.
3379 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
3380
3381 if (!IsExecutable() || m_entry_point_address.IsValid())
3382 return m_entry_point_address;
3383
3384 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
3385 // /usr/include/mach-o.h, but it is basically:
3386 //
3387 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3388 // uint32_t count - this is the count of longs in the thread state data
3389 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3390 // <repeat this trio>
3391 //
3392 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3393 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3394 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3395 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3396 //
3397 // For now we hard-code the offsets and flavors we need:
3398 //
3399 //
3400
Greg Clayton9482f052012-03-13 23:14:29 +00003401 ModuleSP module_sp(GetModule());
3402 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003403 {
Greg Clayton9482f052012-03-13 23:14:29 +00003404 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3405 struct load_command load_cmd;
3406 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3407 uint32_t i;
3408 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3409 bool done = false;
3410
3411 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003412 {
Greg Clayton9482f052012-03-13 23:14:29 +00003413 const uint32_t cmd_offset = offset;
3414 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3415 break;
3416
3417 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003418 {
Greg Clayton9482f052012-03-13 23:14:29 +00003419 case LoadCommandUnixThread:
3420 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003421 {
Greg Clayton9482f052012-03-13 23:14:29 +00003422 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003423 {
Greg Clayton9482f052012-03-13 23:14:29 +00003424 uint32_t flavor = m_data.GetU32(&offset);
3425 uint32_t count = m_data.GetU32(&offset);
3426 if (count == 0)
3427 {
3428 // We've gotten off somehow, log and exit;
3429 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003430 }
Greg Clayton9482f052012-03-13 23:14:29 +00003431
3432 switch (m_header.cputype)
3433 {
3434 case llvm::MachO::CPUTypeARM:
3435 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3436 {
3437 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3438 start_address = m_data.GetU32(&offset);
3439 done = true;
3440 }
Jim Ingham28775942011-03-07 23:44:08 +00003441 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003442 case llvm::MachO::CPUTypeI386:
3443 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3444 {
3445 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3446 start_address = m_data.GetU32(&offset);
3447 done = true;
3448 }
3449 break;
3450 case llvm::MachO::CPUTypeX86_64:
3451 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3452 {
3453 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3454 start_address = m_data.GetU64(&offset);
3455 done = true;
3456 }
3457 break;
3458 default:
3459 return m_entry_point_address;
3460 }
3461 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3462 if (done)
3463 break;
3464 offset += count * 4;
3465 }
Jim Ingham28775942011-03-07 23:44:08 +00003466 }
Greg Clayton9482f052012-03-13 23:14:29 +00003467 break;
3468 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003469 {
Greg Clayton9482f052012-03-13 23:14:29 +00003470 ConstString text_segment_name ("__TEXT");
3471 uint64_t entryoffset = m_data.GetU64(&offset);
3472 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3473 if (text_segment_sp)
3474 {
3475 done = true;
3476 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3477 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003478 }
Greg Clayton9482f052012-03-13 23:14:29 +00003479
3480 default:
3481 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003482 }
Greg Clayton9482f052012-03-13 23:14:29 +00003483 if (done)
3484 break;
Jim Ingham28775942011-03-07 23:44:08 +00003485
Greg Clayton9482f052012-03-13 23:14:29 +00003486 // Go to the next load command:
3487 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003488 }
Jim Ingham28775942011-03-07 23:44:08 +00003489
Greg Clayton9482f052012-03-13 23:14:29 +00003490 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003491 {
Greg Clayton9482f052012-03-13 23:14:29 +00003492 // We got the start address from the load commands, so now resolve that address in the sections
3493 // of this ObjectFile:
3494 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003495 {
Greg Clayton9482f052012-03-13 23:14:29 +00003496 m_entry_point_address.Clear();
3497 }
3498 }
3499 else
3500 {
3501 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3502 // "start" symbol in the main executable.
3503
3504 ModuleSP module_sp (GetModule());
3505
3506 if (module_sp)
3507 {
3508 SymbolContextList contexts;
3509 SymbolContext context;
3510 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3511 {
3512 if (contexts.GetContextAtIndex(0, context))
3513 m_entry_point_address = context.symbol->GetAddress();
3514 }
Greg Clayton3508c382012-02-24 01:59:29 +00003515 }
3516 }
Jim Ingham28775942011-03-07 23:44:08 +00003517 }
3518
3519 return m_entry_point_address;
3520
3521}
3522
Greg Claytonb5a8f142012-02-05 02:38:54 +00003523lldb_private::Address
3524ObjectFileMachO::GetHeaderAddress ()
3525{
3526 lldb_private::Address header_addr;
3527 SectionList *section_list = GetSectionList();
3528 if (section_list)
3529 {
3530 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3531 if (text_segment_sp)
3532 {
Greg Clayton3508c382012-02-24 01:59:29 +00003533 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003534 header_addr.SetOffset (0);
3535 }
3536 }
3537 return header_addr;
3538}
3539
Greg Clayton46c9a352012-02-09 06:16:32 +00003540uint32_t
3541ObjectFileMachO::GetNumThreadContexts ()
3542{
Greg Clayton9482f052012-03-13 23:14:29 +00003543 ModuleSP module_sp(GetModule());
3544 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003545 {
Greg Clayton9482f052012-03-13 23:14:29 +00003546 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3547 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003548 {
Greg Clayton9482f052012-03-13 23:14:29 +00003549 m_thread_context_offsets_valid = true;
3550 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3551 FileRangeArray::Entry file_range;
3552 thread_command thread_cmd;
3553 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003554 {
Greg Clayton9482f052012-03-13 23:14:29 +00003555 const uint32_t cmd_offset = offset;
3556 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3557 break;
3558
3559 if (thread_cmd.cmd == LoadCommandThread)
3560 {
3561 file_range.SetRangeBase (offset);
3562 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3563 m_thread_context_offsets.Append (file_range);
3564 }
3565 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003566 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003567 }
3568 }
3569 return m_thread_context_offsets.GetSize();
3570}
3571
3572lldb::RegisterContextSP
3573ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3574{
Greg Clayton46c9a352012-02-09 06:16:32 +00003575 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003576
Greg Clayton9482f052012-03-13 23:14:29 +00003577 ModuleSP module_sp(GetModule());
3578 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003579 {
Greg Clayton9482f052012-03-13 23:14:29 +00003580 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3581 if (!m_thread_context_offsets_valid)
3582 GetNumThreadContexts ();
3583
3584 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003585 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003586 {
Jim Ingham6f01c932012-10-12 17:34:26 +00003587
3588 DataExtractor data (m_data,
3589 thread_context_file_range->GetRangeBase(),
3590 thread_context_file_range->GetByteSize());
3591
3592 switch (m_header.cputype)
3593 {
3594 case llvm::MachO::CPUTypeARM:
3595 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3596 break;
3597
3598 case llvm::MachO::CPUTypeI386:
3599 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3600 break;
3601
3602 case llvm::MachO::CPUTypeX86_64:
3603 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3604 break;
3605 }
Greg Clayton9482f052012-03-13 23:14:29 +00003606 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003607 }
3608 return reg_ctx_sp;
3609}
3610
Greg Claytonb5a8f142012-02-05 02:38:54 +00003611
Greg Claytonca319972011-07-09 00:41:34 +00003612ObjectFile::Type
3613ObjectFileMachO::CalculateType()
3614{
3615 switch (m_header.filetype)
3616 {
3617 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3618 if (GetAddressByteSize () == 4)
3619 {
3620 // 32 bit kexts are just object files, but they do have a valid
3621 // UUID load command.
3622 UUID uuid;
3623 if (GetUUID(&uuid))
3624 {
3625 // this checking for the UUID load command is not enough
3626 // we could eventually look for the symbol named
3627 // "OSKextGetCurrentIdentifier" as this is required of kexts
3628 if (m_strata == eStrataInvalid)
3629 m_strata = eStrataKernel;
3630 return eTypeSharedLibrary;
3631 }
3632 }
3633 return eTypeObjectFile;
3634
3635 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3636 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3637 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3638 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3639 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3640 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3641 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3642 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3643 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3644 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3645 default:
3646 break;
3647 }
3648 return eTypeUnknown;
3649}
3650
3651ObjectFile::Strata
3652ObjectFileMachO::CalculateStrata()
3653{
3654 switch (m_header.filetype)
3655 {
3656 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3657 {
3658 // 32 bit kexts are just object files, but they do have a valid
3659 // UUID load command.
3660 UUID uuid;
3661 if (GetUUID(&uuid))
3662 {
3663 // this checking for the UUID load command is not enough
3664 // we could eventually look for the symbol named
3665 // "OSKextGetCurrentIdentifier" as this is required of kexts
3666 if (m_type == eTypeInvalid)
3667 m_type = eTypeSharedLibrary;
3668
3669 return eStrataKernel;
3670 }
3671 }
3672 return eStrataUnknown;
3673
3674 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3675 // Check for the MH_DYLDLINK bit in the flags
3676 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003677 {
Greg Claytonca319972011-07-09 00:41:34 +00003678 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003679 }
3680 else
3681 {
3682 SectionList *section_list = GetSectionList();
3683 if (section_list)
3684 {
3685 static ConstString g_kld_section_name ("__KLD");
3686 if (section_list->FindSectionByName(g_kld_section_name))
3687 return eStrataKernel;
3688 }
3689 }
3690 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00003691
3692 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
3693 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00003694 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00003695 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
3696 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
3697 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
3698 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
3699 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
3700 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
3701 default:
3702 break;
3703 }
3704 return eStrataUnknown;
3705}
3706
3707
Greg Clayton49f4bf22012-02-22 19:41:02 +00003708uint32_t
3709ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
3710{
Greg Clayton9482f052012-03-13 23:14:29 +00003711 ModuleSP module_sp(GetModule());
3712 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003713 {
Greg Clayton9482f052012-03-13 23:14:29 +00003714 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3715 struct dylib_command load_cmd;
3716 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
3717 uint32_t version_cmd = 0;
3718 uint64_t version = 0;
3719 uint32_t i;
3720 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003721 {
Greg Clayton9482f052012-03-13 23:14:29 +00003722 const uint32_t cmd_offset = offset;
3723 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3724 break;
3725
3726 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003727 {
Greg Clayton9482f052012-03-13 23:14:29 +00003728 if (version_cmd == 0)
3729 {
3730 version_cmd = load_cmd.cmd;
3731 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
3732 break;
3733 version = load_cmd.dylib.current_version;
3734 }
3735 break; // Break for now unless there is another more complete version
3736 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00003737 }
Greg Clayton9482f052012-03-13 23:14:29 +00003738 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003739 }
Greg Clayton9482f052012-03-13 23:14:29 +00003740
3741 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003742 {
Greg Clayton9482f052012-03-13 23:14:29 +00003743 if (versions != NULL && num_versions > 0)
3744 {
3745 if (num_versions > 0)
3746 versions[0] = (version & 0xFFFF0000ull) >> 16;
3747 if (num_versions > 1)
3748 versions[1] = (version & 0x0000FF00ull) >> 8;
3749 if (num_versions > 2)
3750 versions[2] = (version & 0x000000FFull);
3751 // Fill in an remaining version numbers with invalid values
3752 for (i=3; i<num_versions; ++i)
3753 versions[i] = UINT32_MAX;
3754 }
3755 // The LC_ID_DYLIB load command has a version with 3 version numbers
3756 // in it, so always return 3
3757 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003758 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00003759 }
3760 return false;
3761}
3762
Chris Lattner24943d22010-06-08 16:52:24 +00003763bool
Greg Clayton395fc332011-02-15 21:59:32 +00003764ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00003765{
Greg Clayton9482f052012-03-13 23:14:29 +00003766 ModuleSP module_sp(GetModule());
3767 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003768 {
Greg Clayton9482f052012-03-13 23:14:29 +00003769 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3770 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
3771
3772 // Files with type MH_PRELOAD are currently used in cases where the image
3773 // debugs at the addresses in the file itself. Below we set the OS to
3774 // unknown to make sure we use the DynamicLoaderStatic()...
3775 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
3776 {
3777 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
3778 }
3779 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003780 }
Greg Clayton9482f052012-03-13 23:14:29 +00003781 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003782}
3783
3784
3785//------------------------------------------------------------------
3786// PluginInterface protocol
3787//------------------------------------------------------------------
3788const char *
3789ObjectFileMachO::GetPluginName()
3790{
3791 return "ObjectFileMachO";
3792}
3793
3794const char *
3795ObjectFileMachO::GetShortPluginName()
3796{
3797 return GetPluginNameStatic();
3798}
3799
3800uint32_t
3801ObjectFileMachO::GetPluginVersion()
3802{
3803 return 1;
3804}
3805