blob: 6f3093236897e9de7c3cc141a338995b07830857 [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"
Jason Molendad7938392013-03-21 03:36:01 +000031#include "lldb/Symbol/DWARFCallFrameInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032#include "lldb/Symbol/ObjectFile.h"
Greg Clayton29021d32012-04-18 05:19:20 +000033#include "lldb/Target/Platform.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000034#include "lldb/Target/Process.h"
Greg Clayton29021d32012-04-18 05:19:20 +000035#include "lldb/Target/Target.h"
Greg Clayton9ce95382012-02-13 23:10:39 +000036#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
37#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Clayton46c9a352012-02-09 06:16:32 +000038#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039
Chris Lattner24943d22010-06-08 16:52:24 +000040using namespace lldb;
41using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000042using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000043
Jason Molenda9badb6c2013-03-06 23:19:17 +000044class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
Greg Clayton46c9a352012-02-09 06:16:32 +000045{
46public:
47 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
48 RegisterContextDarwin_x86_64 (thread, 0)
49 {
50 SetRegisterDataFrom_LC_THREAD (data);
51 }
52
53 virtual void
54 InvalidateAllRegisters ()
55 {
56 // Do nothing... registers are always valid...
57 }
58
59 void
60 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
61 {
Greg Clayton36da2aa2013-01-25 18:06:21 +000062 lldb::offset_t offset = 0;
Greg Clayton46c9a352012-02-09 06:16:32 +000063 SetError (GPRRegSet, Read, -1);
64 SetError (FPURegSet, Read, -1);
65 SetError (EXCRegSet, Read, -1);
Greg Clayton9ce95382012-02-13 23:10:39 +000066 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +000067
Greg Clayton9ce95382012-02-13 23:10:39 +000068 while (!done)
Greg Clayton46c9a352012-02-09 06:16:32 +000069 {
Greg Clayton9ce95382012-02-13 23:10:39 +000070 int flavor = data.GetU32 (&offset);
71 if (flavor == 0)
72 done = true;
73 else
Greg Clayton46c9a352012-02-09 06:16:32 +000074 {
Greg Clayton9ce95382012-02-13 23:10:39 +000075 uint32_t i;
76 uint32_t count = data.GetU32 (&offset);
77 switch (flavor)
78 {
79 case GPRRegSet:
80 for (i=0; i<count; ++i)
81 (&gpr.rax)[i] = data.GetU64(&offset);
82 SetError (GPRRegSet, Read, 0);
83 done = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +000084
Greg Clayton9ce95382012-02-13 23:10:39 +000085 break;
86 case FPURegSet:
87 // TODO: fill in FPU regs....
88 //SetError (FPURegSet, Read, -1);
89 done = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +000090
Greg Clayton9ce95382012-02-13 23:10:39 +000091 break;
92 case EXCRegSet:
93 exc.trapno = data.GetU32(&offset);
94 exc.err = data.GetU32(&offset);
95 exc.faultvaddr = data.GetU64(&offset);
96 SetError (EXCRegSet, Read, 0);
97 done = true;
98 break;
99 case 7:
100 case 8:
101 case 9:
102 // fancy flavors that encapsulate of the the above
103 // falvors...
104 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000105
Greg Clayton9ce95382012-02-13 23:10:39 +0000106 default:
107 done = true;
108 break;
109 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000110 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000111 }
112 }
113protected:
114 virtual int
115 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
116 {
117 return 0;
118 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000119
Greg Clayton9ce95382012-02-13 23:10:39 +0000120 virtual int
121 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
122 {
123 return 0;
124 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000125
Greg Clayton9ce95382012-02-13 23:10:39 +0000126 virtual int
127 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
128 {
129 return 0;
130 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000131
Greg Clayton9ce95382012-02-13 23:10:39 +0000132 virtual int
133 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
134 {
135 return 0;
136 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000137
Greg Clayton9ce95382012-02-13 23:10:39 +0000138 virtual int
139 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
140 {
141 return 0;
142 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000143
Greg Clayton9ce95382012-02-13 23:10:39 +0000144 virtual int
145 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
146 {
147 return 0;
148 }
149};
Greg Clayton46c9a352012-02-09 06:16:32 +0000150
Greg Clayton9ce95382012-02-13 23:10:39 +0000151
Jason Molenda9badb6c2013-03-06 23:19:17 +0000152class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
Greg Clayton9ce95382012-02-13 23:10:39 +0000153{
154public:
155 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
156 RegisterContextDarwin_i386 (thread, 0)
157 {
158 SetRegisterDataFrom_LC_THREAD (data);
159 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000160
Greg Clayton9ce95382012-02-13 23:10:39 +0000161 virtual void
162 InvalidateAllRegisters ()
163 {
164 // Do nothing... registers are always valid...
165 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000166
Greg Clayton9ce95382012-02-13 23:10:39 +0000167 void
168 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
169 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000170 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000171 SetError (GPRRegSet, Read, -1);
172 SetError (FPURegSet, Read, -1);
173 SetError (EXCRegSet, Read, -1);
174 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000175
Greg Clayton9ce95382012-02-13 23:10:39 +0000176 while (!done)
177 {
178 int flavor = data.GetU32 (&offset);
179 if (flavor == 0)
180 done = true;
181 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000182 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000183 uint32_t i;
184 uint32_t count = data.GetU32 (&offset);
185 switch (flavor)
186 {
187 case GPRRegSet:
188 for (i=0; i<count; ++i)
189 (&gpr.eax)[i] = data.GetU32(&offset);
190 SetError (GPRRegSet, Read, 0);
191 done = true;
192
193 break;
194 case FPURegSet:
195 // TODO: fill in FPU regs....
196 //SetError (FPURegSet, Read, -1);
197 done = true;
198
199 break;
200 case EXCRegSet:
201 exc.trapno = data.GetU32(&offset);
202 exc.err = data.GetU32(&offset);
203 exc.faultvaddr = data.GetU32(&offset);
204 SetError (EXCRegSet, Read, 0);
205 done = true;
206 break;
207 case 7:
208 case 8:
209 case 9:
210 // fancy flavors that encapsulate of the the above
211 // falvors...
212 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000213
Greg Clayton9ce95382012-02-13 23:10:39 +0000214 default:
215 done = true;
216 break;
217 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000218 }
219 }
220 }
221protected:
222 virtual int
223 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
224 {
225 return 0;
226 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000227
Greg Clayton46c9a352012-02-09 06:16:32 +0000228 virtual int
229 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
230 {
231 return 0;
232 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000233
Greg Clayton46c9a352012-02-09 06:16:32 +0000234 virtual int
235 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
236 {
237 return 0;
238 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000239
Greg Clayton46c9a352012-02-09 06:16:32 +0000240 virtual int
241 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
242 {
243 return 0;
244 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000245
Greg Clayton46c9a352012-02-09 06:16:32 +0000246 virtual int
247 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
248 {
249 return 0;
250 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000251
Greg Clayton46c9a352012-02-09 06:16:32 +0000252 virtual int
253 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
254 {
255 return 0;
256 }
257};
258
Jason Molenda9badb6c2013-03-06 23:19:17 +0000259class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Clayton9ce95382012-02-13 23:10:39 +0000260{
261public:
262 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonb5431d02012-10-30 23:57:32 +0000263 RegisterContextDarwin_arm (thread, 0)
Greg Clayton9ce95382012-02-13 23:10:39 +0000264 {
265 SetRegisterDataFrom_LC_THREAD (data);
266 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000267
Greg Clayton9ce95382012-02-13 23:10:39 +0000268 virtual void
269 InvalidateAllRegisters ()
270 {
271 // Do nothing... registers are always valid...
272 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000273
Greg Clayton9ce95382012-02-13 23:10:39 +0000274 void
275 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
276 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000277 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000278 SetError (GPRRegSet, Read, -1);
279 SetError (FPURegSet, Read, -1);
280 SetError (EXCRegSet, Read, -1);
281 int flavor = data.GetU32 (&offset);
282 uint32_t count = data.GetU32 (&offset);
283 switch (flavor)
284 {
285 case GPRRegSet:
286 for (uint32_t i=0; i<count; ++i)
287 gpr.r[i] = data.GetU32(&offset);
288 SetError (GPRRegSet, Read, 0);
289 break;
290 case FPURegSet:
291 // TODO: fill in FPU regs....
292 //SetError (FPURegSet, Read, -1);
293 break;
294 case EXCRegSet:
295 exc.exception = data.GetU32(&offset);
296 exc.fsr = data.GetU32(&offset);
297 exc.far = data.GetU32(&offset);
298 SetError (EXCRegSet, Read, 0);
299 break;
300 }
301 }
302protected:
303 virtual int
304 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
305 {
306 return 0;
307 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000308
Greg Clayton9ce95382012-02-13 23:10:39 +0000309 virtual int
310 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
311 {
312 return 0;
313 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000314
Greg Clayton9ce95382012-02-13 23:10:39 +0000315 virtual int
316 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
317 {
318 return 0;
319 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000320
321 virtual int
322 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
323 {
324 return -1;
325 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000326
Greg Clayton9ce95382012-02-13 23:10:39 +0000327 virtual int
328 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
329 {
330 return 0;
331 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000332
Greg Clayton9ce95382012-02-13 23:10:39 +0000333 virtual int
334 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
335 {
336 return 0;
337 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000338
Greg Clayton9ce95382012-02-13 23:10:39 +0000339 virtual int
340 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
341 {
342 return 0;
343 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000344
Greg Claytonb5431d02012-10-30 23:57:32 +0000345 virtual int
346 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
347 {
348 return -1;
349 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000350};
351
Greg Claytonb1888f22011-03-19 01:12:21 +0000352#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000353
354void
355ObjectFileMachO::Initialize()
356{
357 PluginManager::RegisterPlugin (GetPluginNameStatic(),
358 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000359 CreateInstance,
360 CreateMemoryInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000361}
362
363void
364ObjectFileMachO::Terminate()
365{
366 PluginManager::UnregisterPlugin (CreateInstance);
367}
368
369
370const char *
371ObjectFileMachO::GetPluginNameStatic()
372{
373 return "object-file.mach-o";
374}
375
376const char *
377ObjectFileMachO::GetPluginDescriptionStatic()
378{
379 return "Mach-o object file reader (32 and 64 bit)";
380}
381
Chris Lattner24943d22010-06-08 16:52:24 +0000382ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000383ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
384 DataBufferSP& data_sp,
385 lldb::offset_t data_offset,
386 const FileSpec* file,
387 lldb::offset_t file_offset,
388 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000389{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000390 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000391 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000392 data_sp = file->MemoryMapFileContents(file_offset, length);
393 data_offset = 0;
394 }
395
396 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
397 {
398 // Update the data to contain the entire file if it doesn't already
399 if (data_sp->GetByteSize() < length)
400 {
401 data_sp = file->MemoryMapFileContents(file_offset, length);
402 data_offset = 0;
403 }
404 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, data_offset, file, file_offset, length));
Chris Lattner24943d22010-06-08 16:52:24 +0000405 if (objfile_ap.get() && objfile_ap->ParseHeader())
406 return objfile_ap.release();
407 }
408 return NULL;
409}
410
Greg Claytonb5a8f142012-02-05 02:38:54 +0000411ObjectFile *
Jason Molenda9badb6c2013-03-06 23:19:17 +0000412ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
413 DataBufferSP& data_sp,
414 const ProcessSP &process_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000415 lldb::addr_t header_addr)
416{
417 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
418 {
Greg Clayton3508c382012-02-24 01:59:29 +0000419 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000420 if (objfile_ap.get() && objfile_ap->ParseHeader())
421 return objfile_ap.release();
422 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000423 return NULL;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000424}
425
426
427const ConstString &
428ObjectFileMachO::GetSegmentNameTEXT()
429{
430 static ConstString g_segment_name_TEXT ("__TEXT");
431 return g_segment_name_TEXT;
432}
433
434const ConstString &
435ObjectFileMachO::GetSegmentNameDATA()
436{
437 static ConstString g_segment_name_DATA ("__DATA");
438 return g_segment_name_DATA;
439}
440
441const ConstString &
442ObjectFileMachO::GetSegmentNameOBJC()
443{
444 static ConstString g_segment_name_OBJC ("__OBJC");
445 return g_segment_name_OBJC;
446}
447
448const ConstString &
449ObjectFileMachO::GetSegmentNameLINKEDIT()
450{
451 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
452 return g_section_name_LINKEDIT;
453}
454
455const ConstString &
456ObjectFileMachO::GetSectionNameEHFrame()
457{
458 static ConstString g_section_name_eh_frame ("__eh_frame");
459 return g_section_name_eh_frame;
460}
461
462
Chris Lattner24943d22010-06-08 16:52:24 +0000463
464static uint32_t
465MachHeaderSizeFromMagic(uint32_t magic)
466{
467 switch (magic)
468 {
Greg Clayton1674b122010-07-21 22:12:05 +0000469 case HeaderMagic32:
470 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000471 return sizeof(struct mach_header);
472
Greg Clayton1674b122010-07-21 22:12:05 +0000473 case HeaderMagic64:
474 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000475 return sizeof(struct mach_header_64);
476 break;
477
478 default:
479 break;
480 }
481 return 0;
482}
483
484
485bool
Jason Molenda9badb6c2013-03-06 23:19:17 +0000486ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
487 lldb::addr_t data_offset,
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000488 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000489{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000490 DataExtractor data;
491 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000492 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000493 uint32_t magic = data.GetU32(&offset);
494 return MachHeaderSizeFromMagic(magic) != 0;
495}
496
497
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000498ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
499 DataBufferSP& data_sp,
500 lldb::offset_t data_offset,
501 const FileSpec* file,
502 lldb::offset_t file_offset,
503 lldb::offset_t length) :
504 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Clayton46c9a352012-02-09 06:16:32 +0000505 m_mach_segments(),
506 m_mach_sections(),
507 m_entry_point_address(),
508 m_thread_context_offsets(),
509 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000510{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000511 ::memset (&m_header, 0, sizeof(m_header));
512 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000513}
514
Greg Clayton3508c382012-02-24 01:59:29 +0000515ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000516 lldb::DataBufferSP& header_data_sp,
517 const lldb::ProcessSP &process_sp,
518 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000519 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Clayton46c9a352012-02-09 06:16:32 +0000520 m_mach_segments(),
521 m_mach_sections(),
522 m_entry_point_address(),
523 m_thread_context_offsets(),
524 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000525{
526 ::memset (&m_header, 0, sizeof(m_header));
527 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
528}
Chris Lattner24943d22010-06-08 16:52:24 +0000529
530ObjectFileMachO::~ObjectFileMachO()
531{
532}
533
534
535bool
536ObjectFileMachO::ParseHeader ()
537{
Greg Clayton9482f052012-03-13 23:14:29 +0000538 ModuleSP module_sp(GetModule());
539 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000540 {
Greg Clayton9482f052012-03-13 23:14:29 +0000541 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
542 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000543 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000544 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000545 // Leave magic in the original byte order
546 m_header.magic = m_data.GetU32(&offset);
547 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000548 {
Greg Clayton9482f052012-03-13 23:14:29 +0000549 case HeaderMagic32:
550 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
551 m_data.SetAddressByteSize(4);
552 can_parse = true;
553 break;
554
555 case HeaderMagic64:
556 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
557 m_data.SetAddressByteSize(8);
558 can_parse = true;
559 break;
560
561 case HeaderMagic32Swapped:
562 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
563 m_data.SetAddressByteSize(4);
564 can_parse = true;
565 break;
566
567 case HeaderMagic64Swapped:
568 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
569 m_data.SetAddressByteSize(8);
570 can_parse = true;
571 break;
572
573 default:
574 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000575 }
Greg Clayton9482f052012-03-13 23:14:29 +0000576
577 if (can_parse)
578 {
579 m_data.GetU32(&offset, &m_header.cputype, 6);
580
581 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +0000582
Greg Clayton21a25432012-11-16 21:36:10 +0000583 // Check if the module has a required architecture
584 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000585 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000586 return false;
587
Greg Clayton9482f052012-03-13 23:14:29 +0000588 if (SetModulesArchitecture (mach_arch))
589 {
590 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
591 if (m_data.GetByteSize() < header_and_lc_size)
592 {
593 DataBufferSP data_sp;
594 ProcessSP process_sp (m_process_wp.lock());
595 if (process_sp)
596 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000597 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000598 }
599 else
600 {
601 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000602 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000603 if (data_sp->GetByteSize() != header_and_lc_size)
604 return false;
605 }
606 if (data_sp)
607 m_data.SetData (data_sp);
608 }
609 }
610 return true;
611 }
612 else
613 {
614 memset(&m_header, 0, sizeof(struct mach_header));
615 }
Chris Lattner24943d22010-06-08 16:52:24 +0000616 }
617 return false;
618}
619
620
621ByteOrder
622ObjectFileMachO::GetByteOrder () const
623{
Chris Lattner24943d22010-06-08 16:52:24 +0000624 return m_data.GetByteOrder ();
625}
626
Jim Ingham7508e732010-08-09 23:31:02 +0000627bool
628ObjectFileMachO::IsExecutable() const
629{
630 return m_header.filetype == HeaderFileTypeExecutable;
631}
Chris Lattner24943d22010-06-08 16:52:24 +0000632
Greg Clayton36da2aa2013-01-25 18:06:21 +0000633uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000634ObjectFileMachO::GetAddressByteSize () const
635{
Chris Lattner24943d22010-06-08 16:52:24 +0000636 return m_data.GetAddressByteSize ();
637}
638
Greg Claytonb3448432011-03-24 21:19:54 +0000639AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000640ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
641{
642 Symtab *symtab = GetSymtab();
643 if (symtab)
644 {
645 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
646 if (symbol)
647 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000648 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000649 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000650 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000651 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000652 {
Greg Clayton3508c382012-02-24 01:59:29 +0000653 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000654 switch (section_type)
655 {
656 case eSectionTypeInvalid: return eAddressClassUnknown;
657 case eSectionTypeCode:
658 if (m_header.cputype == llvm::MachO::CPUTypeARM)
659 {
660 // For ARM we have a bit in the n_desc field of the symbol
661 // that tells us ARM/Thumb which is bit 0x0008.
662 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
663 return eAddressClassCodeAlternateISA;
664 }
665 return eAddressClassCode;
666
667 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000668 case eSectionTypeData:
669 case eSectionTypeDataCString:
670 case eSectionTypeDataCStringPointers:
671 case eSectionTypeDataSymbolAddress:
672 case eSectionTypeData4:
673 case eSectionTypeData8:
674 case eSectionTypeData16:
675 case eSectionTypeDataPointers:
676 case eSectionTypeZeroFill:
677 case eSectionTypeDataObjCMessageRefs:
678 case eSectionTypeDataObjCCFStrings:
679 return eAddressClassData;
680 case eSectionTypeDebug:
681 case eSectionTypeDWARFDebugAbbrev:
682 case eSectionTypeDWARFDebugAranges:
683 case eSectionTypeDWARFDebugFrame:
684 case eSectionTypeDWARFDebugInfo:
685 case eSectionTypeDWARFDebugLine:
686 case eSectionTypeDWARFDebugLoc:
687 case eSectionTypeDWARFDebugMacInfo:
688 case eSectionTypeDWARFDebugPubNames:
689 case eSectionTypeDWARFDebugPubTypes:
690 case eSectionTypeDWARFDebugRanges:
691 case eSectionTypeDWARFDebugStr:
692 case eSectionTypeDWARFAppleNames:
693 case eSectionTypeDWARFAppleTypes:
694 case eSectionTypeDWARFAppleNamespaces:
695 case eSectionTypeDWARFAppleObjC:
696 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000697 case eSectionTypeEHFrame: return eAddressClassRuntime;
698 case eSectionTypeOther: return eAddressClassUnknown;
699 }
700 }
701 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000702
Greg Claytonb3448432011-03-24 21:19:54 +0000703 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000704 switch (symbol_type)
705 {
706 case eSymbolTypeAny: return eAddressClassUnknown;
707 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000708
Greg Claytonb1888f22011-03-19 01:12:21 +0000709 case eSymbolTypeCode:
710 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000711 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000712 if (m_header.cputype == llvm::MachO::CPUTypeARM)
713 {
714 // For ARM we have a bit in the n_desc field of the symbol
715 // that tells us ARM/Thumb which is bit 0x0008.
716 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
717 return eAddressClassCodeAlternateISA;
718 }
719 return eAddressClassCode;
720
721 case eSymbolTypeData: return eAddressClassData;
722 case eSymbolTypeRuntime: return eAddressClassRuntime;
723 case eSymbolTypeException: return eAddressClassRuntime;
724 case eSymbolTypeSourceFile: return eAddressClassDebug;
725 case eSymbolTypeHeaderFile: return eAddressClassDebug;
726 case eSymbolTypeObjectFile: return eAddressClassDebug;
727 case eSymbolTypeCommonBlock: return eAddressClassDebug;
728 case eSymbolTypeBlock: return eAddressClassDebug;
729 case eSymbolTypeLocal: return eAddressClassData;
730 case eSymbolTypeParam: return eAddressClassData;
731 case eSymbolTypeVariable: return eAddressClassData;
732 case eSymbolTypeVariableType: return eAddressClassDebug;
733 case eSymbolTypeLineEntry: return eAddressClassDebug;
734 case eSymbolTypeLineHeader: return eAddressClassDebug;
735 case eSymbolTypeScopeBegin: return eAddressClassDebug;
736 case eSymbolTypeScopeEnd: return eAddressClassDebug;
737 case eSymbolTypeAdditional: return eAddressClassUnknown;
738 case eSymbolTypeCompiler: return eAddressClassDebug;
739 case eSymbolTypeInstrumentation:return eAddressClassDebug;
740 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000741 case eSymbolTypeObjCClass: return eAddressClassRuntime;
742 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
743 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000744 }
745 }
746 }
747 return eAddressClassUnknown;
748}
Chris Lattner24943d22010-06-08 16:52:24 +0000749
750Symtab *
751ObjectFileMachO::GetSymtab()
752{
Greg Clayton9482f052012-03-13 23:14:29 +0000753 ModuleSP module_sp(GetModule());
754 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000755 {
Greg Clayton9482f052012-03-13 23:14:29 +0000756 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
757 if (m_symtab_ap.get() == NULL)
758 {
759 m_symtab_ap.reset(new Symtab(this));
760 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
761 ParseSymtab (true);
762 m_symtab_ap->Finalize ();
763 }
Chris Lattner24943d22010-06-08 16:52:24 +0000764 }
765 return m_symtab_ap.get();
766}
767
768
769SectionList *
770ObjectFileMachO::GetSectionList()
771{
Greg Clayton9482f052012-03-13 23:14:29 +0000772 ModuleSP module_sp(GetModule());
773 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000774 {
Greg Clayton9482f052012-03-13 23:14:29 +0000775 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
776 if (m_sections_ap.get() == NULL)
777 {
778 m_sections_ap.reset(new SectionList());
779 ParseSections();
780 }
Chris Lattner24943d22010-06-08 16:52:24 +0000781 }
782 return m_sections_ap.get();
783}
784
785
786size_t
787ObjectFileMachO::ParseSections ()
788{
789 lldb::user_id_t segID = 0;
790 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000791 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000792 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000793 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000794 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000795 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000796 // First look up any LC_ENCRYPTION_INFO load commands
797 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
798 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000799 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000800 for (i=0; i<m_header.ncmds; ++i)
801 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000802 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000803 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000804 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000805
Greg Clayton54e33712012-05-25 18:09:55 +0000806 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000807 {
Greg Clayton54e33712012-05-25 18:09:55 +0000808 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
809 {
810 if (encryption_cmd.cryptid != 0)
811 {
812 EncryptedFileRanges::Entry entry;
813 entry.SetRangeBase(encryption_cmd.cryptoff);
814 entry.SetByteSize(encryption_cmd.cryptsize);
815 encrypted_file_ranges.Append(entry);
816 }
817 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000818 }
Greg Clayton54e33712012-05-25 18:09:55 +0000819 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000820 }
821
822 offset = MachHeaderSizeFromMagic(m_header.magic);
823
Greg Clayton54e33712012-05-25 18:09:55 +0000824 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000825 for (i=0; i<m_header.ncmds; ++i)
826 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000827 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000828 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
829 break;
830
Greg Clayton1674b122010-07-21 22:12:05 +0000831 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000832 {
833 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
834 {
835 load_cmd.vmaddr = m_data.GetAddress(&offset);
836 load_cmd.vmsize = m_data.GetAddress(&offset);
837 load_cmd.fileoff = m_data.GetAddress(&offset);
838 load_cmd.filesize = m_data.GetAddress(&offset);
839 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
840 {
Jason Molenda9badb6c2013-03-06 23:19:17 +0000841
Greg Clayton68ca8232011-01-25 02:58:48 +0000842 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
843
Chris Lattner24943d22010-06-08 16:52:24 +0000844 // Keep a list of mach segments around in case we need to
845 // get at data that isn't stored in the abstracted Sections.
846 m_mach_segments.push_back (load_cmd);
847
Greg Clayton36da2aa2013-01-25 18:06:21 +0000848 ConstString segment_name (load_cmd.segname, std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
Chris Lattner24943d22010-06-08 16:52:24 +0000849 // Use a segment ID of the segment index shifted left by 8 so they
850 // never conflict with any of the sections.
851 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000852 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000853 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000854 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000855 ++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
856 segment_name, // Name of this section
857 eSectionTypeContainer, // This section is a container of other sections.
858 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
859 load_cmd.vmsize, // VM size in bytes of this section
860 load_cmd.fileoff, // Offset to the data for this section in the file
861 load_cmd.filesize, // Size in bytes of this section as found in the the file
862 load_cmd.flags)); // Flags for this section
863
Greg Clayton68ca8232011-01-25 02:58:48 +0000864 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000865 m_sections_ap->AddSection(segment_sp);
866 }
867
868 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000869 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000870 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +0000871 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +0000872 // mach sections yet...
873 if (m_mach_sections.empty())
874 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000875 uint32_t segment_sect_idx;
876 const lldb::user_id_t first_segment_sectID = sectID + 1;
877
878
Greg Clayton1674b122010-07-21 22:12:05 +0000879 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000880 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
881 {
882 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
883 break;
884 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
885 break;
886 sect64.addr = m_data.GetAddress(&offset);
887 sect64.size = m_data.GetAddress(&offset);
888
889 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
890 break;
891
892 // Keep a list of mach sections around in case we need to
893 // get at data that isn't stored in the abstracted Sections.
894 m_mach_sections.push_back (sect64);
895
896 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
897 if (!segment_name)
898 {
899 // We have a segment with no name so we need to conjure up
900 // segments that correspond to the section's segname if there
901 // isn't already such a section. If there is such a section,
902 // we resize the section so that it spans all sections.
903 // We also mark these sections as fake so address matches don't
904 // hit if they land in the gaps between the child sections.
905 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
906 segment_sp = m_sections_ap->FindSectionByName (segment_name);
907 if (segment_sp.get())
908 {
909 Section *segment = segment_sp.get();
910 // Grow the section size as needed.
911 const lldb::addr_t sect64_min_addr = sect64.addr;
912 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
913 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
914 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
915 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
916 if (sect64_min_addr >= curr_seg_min_addr)
917 {
918 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
919 // Only grow the section size if needed
920 if (new_seg_byte_size > curr_seg_byte_size)
921 segment->SetByteSize (new_seg_byte_size);
922 }
923 else
924 {
925 // We need to change the base address of the segment and
926 // adjust the child section offsets for all existing children.
927 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
928 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +0000929 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000930 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
931 }
Greg Clayton661825b2010-06-28 23:51:11 +0000932
933 // Grow the section size as needed.
934 if (sect64.offset)
935 {
936 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
937 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
938
939 const lldb::addr_t section_min_file_offset = sect64.offset;
940 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
941 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
942 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
943 segment->SetFileOffset (new_file_offset);
944 segment->SetFileSize (new_file_size);
945 }
Chris Lattner24943d22010-06-08 16:52:24 +0000946 }
947 else
948 {
949 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +0000950 segment_sp.reset(new Section (segment_sp, // Parent section
951 module_sp, // Module to which this section belongs
952 ++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
953 segment_name, // Name of this section
954 eSectionTypeContainer, // This section is a container of other sections.
955 sect64.addr, // File VM address == addresses as they are found in the object file
956 sect64.size, // VM size in bytes of this section
957 sect64.offset, // Offset to the data for this section in the file
958 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
959 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +0000960 segment_sp->SetIsFake(true);
961 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000962 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000963 }
964 }
965 assert (segment_sp.get());
966
Greg Clayton1674b122010-07-21 22:12:05 +0000967 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000968 static ConstString g_sect_name_objc_data ("__objc_data");
969 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
970 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
971 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
972 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
973 static ConstString g_sect_name_objc_const ("__objc_const");
974 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
975 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000976
977 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
978 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
979 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
980 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
981 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
982 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
983 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
984 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
985 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
986 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
987 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +0000988 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
989 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +0000990 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +0000991 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000992 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +0000993 static ConstString g_sect_name_DATA ("__DATA");
994 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000995
Chris Lattner24943d22010-06-08 16:52:24 +0000996 SectionType sect_type = eSectionTypeOther;
997
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000998 if (section_name == g_sect_name_dwarf_debug_abbrev)
999 sect_type = eSectionTypeDWARFDebugAbbrev;
1000 else if (section_name == g_sect_name_dwarf_debug_aranges)
1001 sect_type = eSectionTypeDWARFDebugAranges;
1002 else if (section_name == g_sect_name_dwarf_debug_frame)
1003 sect_type = eSectionTypeDWARFDebugFrame;
1004 else if (section_name == g_sect_name_dwarf_debug_info)
1005 sect_type = eSectionTypeDWARFDebugInfo;
1006 else if (section_name == g_sect_name_dwarf_debug_line)
1007 sect_type = eSectionTypeDWARFDebugLine;
1008 else if (section_name == g_sect_name_dwarf_debug_loc)
1009 sect_type = eSectionTypeDWARFDebugLoc;
1010 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1011 sect_type = eSectionTypeDWARFDebugMacInfo;
1012 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1013 sect_type = eSectionTypeDWARFDebugPubNames;
1014 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1015 sect_type = eSectionTypeDWARFDebugPubTypes;
1016 else if (section_name == g_sect_name_dwarf_debug_ranges)
1017 sect_type = eSectionTypeDWARFDebugRanges;
1018 else if (section_name == g_sect_name_dwarf_debug_str)
1019 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001020 else if (section_name == g_sect_name_dwarf_apple_names)
1021 sect_type = eSectionTypeDWARFAppleNames;
1022 else if (section_name == g_sect_name_dwarf_apple_types)
1023 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001024 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1025 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001026 else if (section_name == g_sect_name_dwarf_apple_objc)
1027 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001028 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001029 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001030 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001031 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001032 else if (section_name == g_sect_name_eh_frame)
1033 sect_type = eSectionTypeEHFrame;
1034 else if (section_name == g_sect_name_cfstring)
1035 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001036 else if (section_name == g_sect_name_objc_data ||
1037 section_name == g_sect_name_objc_classrefs ||
1038 section_name == g_sect_name_objc_superrefs ||
1039 section_name == g_sect_name_objc_const ||
1040 section_name == g_sect_name_objc_classlist)
1041 {
1042 sect_type = eSectionTypeDataPointers;
1043 }
Chris Lattner24943d22010-06-08 16:52:24 +00001044
1045 if (sect_type == eSectionTypeOther)
1046 {
1047 switch (mach_sect_type)
1048 {
1049 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001050 case SectionTypeRegular:
1051 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001052 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001053 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001054 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001055 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001056 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001057 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001058 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1059 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1060 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1061 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1062 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1063 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1064 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1065 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1066 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1067 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1068 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1069 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1070 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1071 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1072 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1073 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001074 default: break;
1075 }
1076 }
1077
Greg Clayton3508c382012-02-24 01:59:29 +00001078 SectionSP section_sp(new Section (segment_sp,
1079 module_sp,
1080 ++sectID,
1081 section_name,
1082 sect_type,
1083 sect64.addr - segment_sp->GetFileAddress(),
1084 sect64.size,
1085 sect64.offset,
1086 sect64.offset == 0 ? 0 : sect64.size,
1087 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001088 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001089
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001090 bool section_is_encrypted = false;
1091 if (!segment_is_encrypted && load_cmd.filesize != 0)
1092 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001093
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001094 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001095 segment_sp->GetChildren().AddSection(section_sp);
1096
1097 if (segment_sp->IsFake())
1098 {
1099 segment_sp.reset();
1100 segment_name.Clear();
1101 }
1102 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001103 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001104 {
1105 if (first_segment_sectID <= sectID)
1106 {
1107 lldb::user_id_t sect_uid;
1108 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1109 {
1110 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1111 SectionSP next_section_sp;
1112 if (sect_uid + 1 <= sectID)
1113 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1114
1115 if (curr_section_sp.get())
1116 {
1117 if (curr_section_sp->GetByteSize() == 0)
1118 {
1119 if (next_section_sp.get() != NULL)
1120 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1121 else
1122 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1123 }
1124 }
1125 }
1126 }
1127 }
1128 }
1129 }
1130 }
Greg Clayton1674b122010-07-21 22:12:05 +00001131 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001132 {
1133 m_dysymtab.cmd = load_cmd.cmd;
1134 m_dysymtab.cmdsize = load_cmd.cmdsize;
1135 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1136 }
1137
1138 offset = load_cmd_offset + load_cmd.cmdsize;
1139 }
1140// if (dump_sections)
1141// {
1142// StreamFile s(stdout);
1143// m_sections_ap->Dump(&s, true);
1144// }
1145 return sectID; // Return the number of sections we registered with the module
1146}
1147
1148class MachSymtabSectionInfo
1149{
1150public:
1151
1152 MachSymtabSectionInfo (SectionList *section_list) :
1153 m_section_list (section_list),
1154 m_section_infos()
1155 {
1156 // Get the number of sections down to a depth of 1 to include
1157 // all segments and their sections, but no other sections that
1158 // may be added for debug map or
1159 m_section_infos.resize(section_list->GetNumSections(1));
1160 }
1161
1162
Greg Clayton3508c382012-02-24 01:59:29 +00001163 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001164 GetSection (uint8_t n_sect, addr_t file_addr)
1165 {
1166 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001167 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001168 if (n_sect < m_section_infos.size())
1169 {
Greg Clayton3508c382012-02-24 01:59:29 +00001170 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001171 {
Greg Clayton3508c382012-02-24 01:59:29 +00001172 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1173 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001174 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001175 {
Greg Clayton3508c382012-02-24 01:59:29 +00001176 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1177 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001178 }
1179 else
1180 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001181 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001182 }
Chris Lattner24943d22010-06-08 16:52:24 +00001183 }
1184 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001185 {
1186 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001187 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001188 }
1189 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1190 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1191 {
1192 // Symbol is in section with zero size, but has the same start
1193 // address as the section. This can happen with linker symbols
1194 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001195 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001196 }
Chris Lattner24943d22010-06-08 16:52:24 +00001197 }
Greg Clayton3508c382012-02-24 01:59:29 +00001198 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001199 }
1200
1201protected:
1202 struct SectionInfo
1203 {
1204 SectionInfo () :
1205 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001206 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001207 {
1208 }
1209
1210 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001211 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001212 };
1213 SectionList *m_section_list;
1214 std::vector<SectionInfo> m_section_infos;
1215};
1216
Chris Lattner24943d22010-06-08 16:52:24 +00001217size_t
1218ObjectFileMachO::ParseSymtab (bool minimize)
1219{
1220 Timer scoped_timer(__PRETTY_FUNCTION__,
1221 "ObjectFileMachO::ParseSymtab () module = %s",
1222 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001223 ModuleSP module_sp (GetModule());
1224 if (!module_sp)
1225 return 0;
1226
1227 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1228 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1229 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1230 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001231 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001232 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001233
Greg Clayton0fea0512011-12-30 00:32:24 +00001234 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
1235
Chris Lattner24943d22010-06-08 16:52:24 +00001236 for (i=0; i<m_header.ncmds; ++i)
1237 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001238 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001239 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001240 struct load_command lc;
1241 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001242 break;
1243 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001244 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001245 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001246 case LoadCommandSymtab:
1247 symtab_load_command.cmd = lc.cmd;
1248 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001249 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001250 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1251 return 0;
1252 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001253 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001254 if (log)
1255 module_sp->LogMessage(log.get(), "LC_SYMTAB.symoff == 0");
1256 return 0;
1257 }
1258
1259 if (symtab_load_command.stroff == 0)
1260 {
1261 if (log)
1262 module_sp->LogMessage(log.get(), "LC_SYMTAB.stroff == 0");
1263 return 0;
1264 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001265
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001266 if (symtab_load_command.nsyms == 0)
1267 {
1268 if (log)
1269 module_sp->LogMessage(log.get(), "LC_SYMTAB.nsyms == 0");
1270 return 0;
1271 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001272
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001273 if (symtab_load_command.strsize == 0)
1274 {
1275 if (log)
1276 module_sp->LogMessage(log.get(), "LC_SYMTAB.strsize == 0");
1277 return 0;
1278 }
1279 break;
1280
1281 case LoadCommandFunctionStarts:
1282 function_starts_load_command.cmd = lc.cmd;
1283 function_starts_load_command.cmdsize = lc.cmdsize;
1284 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1285 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1286 break;
1287
1288 default:
1289 break;
1290 }
1291 offset = cmd_offset + lc.cmdsize;
1292 }
1293
1294 if (symtab_load_command.cmd)
1295 {
1296 Symtab *symtab = m_symtab_ap.get();
1297 SectionList *section_list = GetSectionList();
1298 if (section_list == NULL)
1299 return 0;
1300
1301 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001302 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001303
Greg Clayton36da2aa2013-01-25 18:06:21 +00001304 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1305 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001306 bool bit_width_32 = addr_byte_size == 4;
1307 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1308
Greg Clayton36da2aa2013-01-25 18:06:21 +00001309 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1310 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1311 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001312 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001313
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001314 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1315 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001316 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1317 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001318 {
Greg Claytondd29b972012-05-18 23:20:01 +00001319 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001320 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1321 // Reading mach file from memory in a process or core file...
1322
1323 if (linkedit_section_sp)
1324 {
1325 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1326 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1327 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001328 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001329
1330 bool data_was_read = false;
1331
1332#if defined (__APPLE__) && defined (__arm__)
1333 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001334 {
Greg Clayton29021d32012-04-18 05:19:20 +00001335 // This mach-o memory file is in the dyld shared cache. If this
1336 // program is not remote and this is iOS, then this process will
1337 // share the same shared cache as the process we are debugging and
1338 // we can read the entire __LINKEDIT from the address space in this
1339 // process. This is a needed optimization that is used for local iOS
1340 // debugging only since all shared libraries in the shared cache do
1341 // not have corresponding files that exist in the file system of the
1342 // device. They have been combined into a single file. This means we
1343 // always have to load these files from memory. All of the symbol and
1344 // string tables from all of the __LINKEDIT sections from the shared
1345 // libraries in the shared cache have been merged into a single large
1346 // symbol and string table. Reading all of this symbol and string table
1347 // data across can slow down debug launch times, so we optimize this by
1348 // reading the memory for the __LINKEDIT section from this process.
1349 PlatformSP platform_sp (target.GetPlatform());
Jason Molendab76f77e2012-12-13 01:13:00 +00001350 if (platform_sp && platform_sp->IsHost())
Greg Clayton29021d32012-04-18 05:19:20 +00001351 {
1352 data_was_read = true;
1353 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001354 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001355 if (function_starts_load_command.cmd)
1356 {
1357 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1358 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1359 }
1360 }
1361 }
1362#endif
1363
1364 if (!data_was_read)
1365 {
1366 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1367 if (nlist_data_sp)
1368 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001369 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1370 //if (strtab_data_sp)
1371 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001372 if (m_dysymtab.nindirectsyms != 0)
1373 {
1374 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1375 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1376 if (indirect_syms_data_sp)
1377 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1378 }
Greg Clayton29021d32012-04-18 05:19:20 +00001379 if (function_starts_load_command.cmd)
1380 {
1381 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1382 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1383 if (func_start_data_sp)
1384 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1385 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001386 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001387 }
1388 }
1389 else
1390 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001391 nlist_data.SetData (m_data,
1392 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001393 nlist_data_byte_size);
1394 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001395 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001396 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001397 if (m_dysymtab.nindirectsyms != 0)
1398 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001399 indirect_symbol_index_data.SetData (m_data,
1400 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001401 m_dysymtab.nindirectsyms * 4);
1402 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001403 if (function_starts_load_command.cmd)
1404 {
1405 function_starts_data.SetData (m_data,
1406 function_starts_load_command.dataoff,
1407 function_starts_load_command.datasize);
1408 }
1409 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001410
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001411 if (nlist_data.GetByteSize() == 0)
1412 {
1413 if (log)
1414 module_sp->LogMessage(log.get(), "failed to read nlist data");
1415 return 0;
1416 }
1417
1418
Greg Clayton3a5dc012012-05-25 17:04:00 +00001419 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1420 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001421 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001422 if (process)
1423 {
1424 if (strtab_addr == LLDB_INVALID_ADDRESS)
1425 {
1426 if (log)
1427 module_sp->LogMessage(log.get(), "failed to locate the strtab in memory");
1428 return 0;
1429 }
1430 }
1431 else
Greg Claytondd29b972012-05-18 23:20:01 +00001432 {
1433 if (log)
Greg Clayton3a5dc012012-05-25 17:04:00 +00001434 module_sp->LogMessage(log.get(), "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001435 return 0;
1436 }
1437 }
Greg Claytondd29b972012-05-18 23:20:01 +00001438
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001439 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1440 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1441 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1442 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1443 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1444 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1445 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1446 SectionSP eh_frame_section_sp;
1447 if (text_section_sp.get())
1448 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1449 else
1450 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1451
Greg Claytond2653c22012-03-14 01:53:24 +00001452 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001453
1454 // lldb works best if it knows the start addresss of all functions in a module.
1455 // Linker symbols or debug info are normally the best source of information for start addr / size but
1456 // they may be stripped in a released binary.
1457 // Two additional sources of information exist in Mach-O binaries:
1458 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1459 // binary, relative to the text section.
1460 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1461 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1462 // Binaries built to run on older releases may need to use eh_frame information.
1463
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001464 if (text_section_sp && function_starts_data.GetByteSize())
1465 {
1466 FunctionStarts::Entry function_start_entry;
1467 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001468 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001469 function_start_entry.addr = text_section_sp->GetFileAddress();
1470 uint64_t delta;
1471 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1472 {
1473 // Now append the current entry
1474 function_start_entry.addr += delta;
1475 function_starts.Append(function_start_entry);
1476 }
Jason Molendad7938392013-03-21 03:36:01 +00001477 }
1478 else
1479 {
1480 if (text_section_sp.get() && eh_frame_section_sp.get())
1481 {
1482 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1483 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1484 eh_frame.GetFunctionAddressAndSizeVector (functions);
1485 addr_t text_base_addr = text_section_sp->GetFileAddress();
1486 size_t count = functions.GetSize();
1487 for (size_t i = 0; i < count; ++i)
1488 {
1489 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1490 if (func)
1491 {
1492 FunctionStarts::Entry function_start_entry;
1493 function_start_entry.addr = func->base - text_base_addr;
1494 function_starts.Append(function_start_entry);
1495 }
1496 }
1497 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001498 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001499
Greg Clayton36da2aa2013-01-25 18:06:21 +00001500 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001501
Greg Clayton36da2aa2013-01-25 18:06:21 +00001502 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001503
Greg Clayton36da2aa2013-01-25 18:06:21 +00001504 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001505
1506 uint32_t N_SO_index = UINT32_MAX;
1507
1508 MachSymtabSectionInfo section_info (section_list);
1509 std::vector<uint32_t> N_FUN_indexes;
1510 std::vector<uint32_t> N_NSYM_indexes;
1511 std::vector<uint32_t> N_INCL_indexes;
1512 std::vector<uint32_t> N_BRAC_indexes;
1513 std::vector<uint32_t> N_COMM_indexes;
1514 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1515 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1516 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1517 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1518 // Any symbols that get merged into another will get an entry
1519 // in this map so we know
1520 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1521 uint32_t nlist_idx = 0;
1522 Symbol *symbol_ptr = NULL;
1523
1524 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001525 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001526 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001527 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001528 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001529
Jason Molendab62abd52012-06-21 01:51:02 +00001530#if defined (__APPLE__) && defined (__arm__)
1531
1532 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1533 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1534 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1535 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1536 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1537 // nlist parser to ignore all LOCAL symbols.
1538
1539 if (m_header.flags & 0x80000000u)
1540 {
1541 // Before we can start mapping the DSC, we need to make certain the target process is actually
1542 // using the cache we can find.
1543
1544 /*
1545 * TODO (FIXME!)
1546 *
1547 * Consider the case of testing with a separate DSC file.
1548 * If we go through the normal code paths, we will give symbols for the wrong DSC, and
1549 * that is bad. We need to read the target process' all_image_infos struct, and look
1550 * at the values of the processDetachedFromSharedRegion field. If that is set, we should skip
1551 * this code section.
1552 */
1553
1554 // Next we need to determine the correct path for the dyld shared cache.
1555
1556 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1557 char dsc_path[PATH_MAX];
1558
1559 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001560 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1561 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001562 header_arch.GetArchitectureName());
1563
1564 FileSpec dsc_filespec(dsc_path, false);
1565
1566 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda6bcabae2013-03-06 23:17:36 +00001567 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001568 {
Jason Molenda6bcabae2013-03-06 23:17:36 +00001569 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1570 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1571 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001572 uint32_t imagesOffset;
1573 uint32_t imagesCount;
1574 uint64_t dyldBaseAddress;
1575 uint64_t codeSignatureOffset;
1576 uint64_t codeSignatureSize;
1577 uint64_t slideInfoOffset;
1578 uint64_t slideInfoSize;
Jason Molenda6bcabae2013-03-06 23:17:36 +00001579 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1580 uint64_t localSymbolsSize; // size of local symbols information
1581 };
1582 struct lldb_copy_dyld_cache_header_v1
1583 {
1584 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1585 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1586 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1587 uint32_t imagesOffset;
1588 uint32_t imagesCount;
1589 uint64_t dyldBaseAddress;
1590 uint64_t codeSignatureOffset;
1591 uint64_t codeSignatureSize;
1592 uint64_t slideInfoOffset;
1593 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001594 uint64_t localSymbolsOffset;
1595 uint64_t localSymbolsSize;
Jason Molenda6bcabae2013-03-06 23:17:36 +00001596 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001597 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001598
1599 struct lldb_copy_dyld_cache_mapping_info
1600 {
1601 uint64_t address;
1602 uint64_t size;
1603 uint64_t fileOffset;
1604 uint32_t maxProt;
1605 uint32_t initProt;
1606 };
1607
Greg Claytonab77dcb2012-09-05 22:30:51 +00001608 struct lldb_copy_dyld_cache_local_symbols_info
1609 {
1610 uint32_t nlistOffset;
1611 uint32_t nlistCount;
1612 uint32_t stringsOffset;
1613 uint32_t stringsSize;
1614 uint32_t entriesOffset;
1615 uint32_t entriesCount;
1616 };
1617 struct lldb_copy_dyld_cache_local_symbols_entry
1618 {
1619 uint32_t dylibOffset;
1620 uint32_t nlistStartIndex;
1621 uint32_t nlistCount;
1622 };
Jason Molendab62abd52012-06-21 01:51:02 +00001623
Jason Molendafd3b35d2012-06-22 03:28:35 +00001624 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1625 The dyld_cache_local_symbols_info structure gives us three things:
1626 1. The start and count of the nlist records in the dyld_shared_cache file
1627 2. The start and size of the strings for these nlist records
1628 3. The start and count of dyld_cache_local_symbols_entry entries
1629
1630 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1631 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001632 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001633 and the count of how many nlist records there are for this dylib/framework.
1634 */
1635
Jason Molendab62abd52012-06-21 01:51:02 +00001636 // Process the dsc header to find the unmapped symbols
1637 //
1638 // Save some VM space, do not map the entire cache in one shot.
1639
Jason Molenda6bcabae2013-03-06 23:17:36 +00001640 DataBufferSP dsc_data_sp;
1641 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1642
1643 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001644 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001645 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001646
Jason Molenda6bcabae2013-03-06 23:17:36 +00001647 char version_str[17];
1648 int version = -1;
1649 lldb::offset_t offset = 0;
1650 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1651 version_str[16] = '\0';
1652 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1653 {
1654 int v;
1655 if (::sscanf (version_str + 6, "%d", &v) == 1)
1656 {
1657 version = v;
1658 }
1659 }
1660
Jason Molenda9badb6c2013-03-06 23:19:17 +00001661 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001662
Jason Molendab62abd52012-06-21 01:51:02 +00001663 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1664
1665 // If the mappingOffset points to a location inside the header, we've
1666 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda6bcabae2013-03-06 23:17:36 +00001667 if ((version == 0 && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
1668 || (version >= 1 && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v1)))
Jason Molendab62abd52012-06-21 01:51:02 +00001669 {
1670
Jason Molenda6bcabae2013-03-06 23:17:36 +00001671 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1672 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1673 offset = 0;
1674
1675 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1676 // in the shared library cache need to be adjusted by an offset to match up with the
1677 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1678 // recorded in mapping_offset_value.
1679 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1680
1681 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001682 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1683 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1684
Jason Molenda9badb6c2013-03-06 23:19:17 +00001685 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001686 {
1687 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001688 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001689 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001690 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001691
1692 offset = 0;
1693
1694 // Read the local_symbols_infos struct in one shot
1695 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1696 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1697
Jason Molendab62abd52012-06-21 01:51:02 +00001698 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1699
Jason Molenda6bcabae2013-03-06 23:17:36 +00001700 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001701
1702 offset = local_symbols_info.entriesOffset;
1703 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1704 {
1705 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1706 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1707 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1708 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1709
Jason Molenda9badb6c2013-03-06 23:19:17 +00001710 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001711 {
1712 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1713
1714 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1715 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1716 num_syms = symtab->GetNumSymbols();
1717
1718 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1719 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1720
Jason Molenda9badb6c2013-03-06 23:19:17 +00001721 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001722 {
1723 /////////////////////////////
1724 {
1725 struct nlist_64 nlist;
1726 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1727 break;
1728
1729 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1730 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1731 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1732 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1733 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1734
1735 SymbolType type = eSymbolTypeInvalid;
1736 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1737
1738 if (symbol_name == NULL)
1739 {
1740 // No symbol should be NULL, even the symbols with no
1741 // string values should have an offset zero which points
1742 // to an empty C-string
1743 Host::SystemLog (Host::eSystemLogError,
1744 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1745 entry_index,
1746 nlist.n_strx,
1747 module_sp->GetFileSpec().GetDirectory().GetCString(),
1748 module_sp->GetFileSpec().GetFilename().GetCString());
1749 continue;
1750 }
1751 if (symbol_name[0] == '\0')
1752 symbol_name = NULL;
1753
1754 const char *symbol_name_non_abi_mangled = NULL;
1755
1756 SectionSP symbol_section;
1757 uint32_t symbol_byte_size = 0;
1758 bool add_nlist = true;
1759 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001760 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001761
1762 assert (sym_idx < num_syms);
1763
1764 sym[sym_idx].SetDebug (is_debug);
1765
1766 if (is_debug)
1767 {
1768 switch (nlist.n_type)
1769 {
1770 case StabGlobalSymbol:
1771 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1772 // Sometimes the N_GSYM value contains the address.
1773
1774 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1775 // have the same address, but we want to ensure that we always find only the real symbol,
1776 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1777 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1778 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1779 // same address.
1780
1781 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1782 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1783 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1784 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1785 add_nlist = false;
1786 else
1787 {
1788 sym[sym_idx].SetExternal(true);
1789 if (nlist.n_value != 0)
1790 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1791 type = eSymbolTypeData;
1792 }
1793 break;
1794
1795 case StabFunctionName:
1796 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1797 type = eSymbolTypeCompiler;
1798 break;
1799
1800 case StabFunction:
1801 // N_FUN -- procedure: name,,n_sect,linenumber,address
1802 if (symbol_name)
1803 {
1804 type = eSymbolTypeCode;
1805 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1806
1807 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1808 // We use the current number of symbols in the symbol table in lieu of
1809 // using nlist_idx in case we ever start trimming entries out
1810 N_FUN_indexes.push_back(sym_idx);
1811 }
1812 else
1813 {
1814 type = eSymbolTypeCompiler;
1815
1816 if ( !N_FUN_indexes.empty() )
1817 {
1818 // Copy the size of the function into the original STAB entry so we don't have
1819 // to hunt for it later
1820 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1821 N_FUN_indexes.pop_back();
1822 // We don't really need the end function STAB as it contains the size which
1823 // we already placed with the original symbol, so don't add it if we want a
1824 // minimal symbol table
1825 if (minimize)
1826 add_nlist = false;
1827 }
1828 }
1829 break;
1830
1831 case StabStaticSymbol:
1832 // N_STSYM -- static symbol: name,,n_sect,type,address
1833 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1834 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1835 type = eSymbolTypeData;
1836 break;
1837
1838 case StabLocalCommon:
1839 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1840 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1841 type = eSymbolTypeCommonBlock;
1842 break;
1843
1844 case StabBeginSymbol:
1845 // N_BNSYM
1846 // We use the current number of symbols in the symbol table in lieu of
1847 // using nlist_idx in case we ever start trimming entries out
1848 if (minimize)
1849 {
1850 // Skip these if we want minimal symbol tables
1851 add_nlist = false;
1852 }
1853 else
1854 {
1855 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1856 N_NSYM_indexes.push_back(sym_idx);
1857 type = eSymbolTypeScopeBegin;
1858 }
1859 break;
1860
1861 case StabEndSymbol:
1862 // N_ENSYM
1863 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1864 // so that we can always skip the entire symbol if we need to navigate
1865 // more quickly at the source level when parsing STABS
1866 if (minimize)
1867 {
1868 // Skip these if we want minimal symbol tables
1869 add_nlist = false;
1870 }
1871 else
1872 {
1873 if ( !N_NSYM_indexes.empty() )
1874 {
1875 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1876 symbol_ptr->SetByteSize(sym_idx + 1);
1877 symbol_ptr->SetSizeIsSibling(true);
1878 N_NSYM_indexes.pop_back();
1879 }
1880 type = eSymbolTypeScopeEnd;
1881 }
1882 break;
1883
1884
1885 case StabSourceFileOptions:
1886 // N_OPT - emitted with gcc2_compiled and in gcc source
1887 type = eSymbolTypeCompiler;
1888 break;
1889
1890 case StabRegisterSymbol:
1891 // N_RSYM - register sym: name,,NO_SECT,type,register
1892 type = eSymbolTypeVariable;
1893 break;
1894
1895 case StabSourceLine:
1896 // N_SLINE - src line: 0,,n_sect,linenumber,address
1897 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1898 type = eSymbolTypeLineEntry;
1899 break;
1900
1901 case StabStructureType:
1902 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1903 type = eSymbolTypeVariableType;
1904 break;
1905
1906 case StabSourceFileName:
1907 // N_SO - source file name
1908 type = eSymbolTypeSourceFile;
1909 if (symbol_name == NULL)
1910 {
1911 if (minimize)
1912 add_nlist = false;
1913 if (N_SO_index != UINT32_MAX)
1914 {
1915 // Set the size of the N_SO to the terminating index of this N_SO
1916 // so that we can always skip the entire N_SO if we need to navigate
1917 // more quickly at the source level when parsing STABS
1918 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1919 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1920 symbol_ptr->SetSizeIsSibling(true);
1921 }
1922 N_NSYM_indexes.clear();
1923 N_INCL_indexes.clear();
1924 N_BRAC_indexes.clear();
1925 N_COMM_indexes.clear();
1926 N_FUN_indexes.clear();
1927 N_SO_index = UINT32_MAX;
1928 }
1929 else
1930 {
1931 // We use the current number of symbols in the symbol table in lieu of
1932 // using nlist_idx in case we ever start trimming entries out
1933 const bool N_SO_has_full_path = symbol_name[0] == '/';
1934 if (N_SO_has_full_path)
1935 {
1936 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1937 {
1938 // We have two consecutive N_SO entries where the first contains a directory
1939 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00001940 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001941 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1942 add_nlist = false;
1943 }
1944 else
1945 {
1946 // This is the first entry in a N_SO that contains a directory or
1947 // a full path to the source file
1948 N_SO_index = sym_idx;
1949 }
1950 }
1951 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1952 {
1953 // This is usually the second N_SO entry that contains just the filename,
1954 // so here we combine it with the first one if we are minimizing the symbol table
1955 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1956 if (so_path && so_path[0])
1957 {
1958 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00001959 const size_t double_slash_pos = full_so_path.find("//");
1960 if (double_slash_pos != std::string::npos)
1961 {
1962 // The linker has been generating bad N_SO entries with doubled up paths
1963 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
1964 // and the second is the directory for the source file so you end up with
1965 // a path that looks like "/tmp/src//tmp/src/"
1966 FileSpec so_dir(so_path, false);
1967 if (!so_dir.Exists())
1968 {
1969 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
1970 if (so_dir.Exists())
1971 {
1972 // Trim off the incorrect path
1973 full_so_path.erase(0, double_slash_pos + 1);
1974 }
1975 }
1976 }
Jason Molendab62abd52012-06-21 01:51:02 +00001977 if (*full_so_path.rbegin() != '/')
1978 full_so_path += '/';
1979 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00001980 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001981 add_nlist = false;
1982 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1983 }
1984 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00001985 else
1986 {
1987 // This could be a relative path to a N_SO
1988 N_SO_index = sym_idx;
1989 }
Jason Molendab62abd52012-06-21 01:51:02 +00001990 }
Jason Molendab62abd52012-06-21 01:51:02 +00001991 break;
1992
1993 case StabObjectFileName:
1994 // N_OSO - object file name: name,,0,0,st_mtime
1995 type = eSymbolTypeObjectFile;
1996 break;
1997
1998 case StabLocalSymbol:
1999 // N_LSYM - local sym: name,,NO_SECT,type,offset
2000 type = eSymbolTypeLocal;
2001 break;
2002
2003 //----------------------------------------------------------------------
2004 // INCL scopes
2005 //----------------------------------------------------------------------
2006 case StabBeginIncludeFileName:
2007 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2008 // We use the current number of symbols in the symbol table in lieu of
2009 // using nlist_idx in case we ever start trimming entries out
2010 N_INCL_indexes.push_back(sym_idx);
2011 type = eSymbolTypeScopeBegin;
2012 break;
2013
2014 case StabEndIncludeFile:
2015 // N_EINCL - include file end: name,,NO_SECT,0,0
2016 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2017 // so that we can always skip the entire symbol if we need to navigate
2018 // more quickly at the source level when parsing STABS
2019 if ( !N_INCL_indexes.empty() )
2020 {
2021 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2022 symbol_ptr->SetByteSize(sym_idx + 1);
2023 symbol_ptr->SetSizeIsSibling(true);
2024 N_INCL_indexes.pop_back();
2025 }
2026 type = eSymbolTypeScopeEnd;
2027 break;
2028
2029 case StabIncludeFileName:
2030 // N_SOL - #included file name: name,,n_sect,0,address
2031 type = eSymbolTypeHeaderFile;
2032
2033 // We currently don't use the header files on darwin
2034 if (minimize)
2035 add_nlist = false;
2036 break;
2037
2038 case StabCompilerParameters:
2039 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2040 type = eSymbolTypeCompiler;
2041 break;
2042
2043 case StabCompilerVersion:
2044 // N_VERSION - compiler version: name,,NO_SECT,0,0
2045 type = eSymbolTypeCompiler;
2046 break;
2047
2048 case StabCompilerOptLevel:
2049 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2050 type = eSymbolTypeCompiler;
2051 break;
2052
2053 case StabParameter:
2054 // N_PSYM - parameter: name,,NO_SECT,type,offset
2055 type = eSymbolTypeVariable;
2056 break;
2057
2058 case StabAlternateEntry:
2059 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2060 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2061 type = eSymbolTypeLineEntry;
2062 break;
2063
2064 //----------------------------------------------------------------------
2065 // Left and Right Braces
2066 //----------------------------------------------------------------------
2067 case StabLeftBracket:
2068 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2069 // We use the current number of symbols in the symbol table in lieu of
2070 // using nlist_idx in case we ever start trimming entries out
2071 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2072 N_BRAC_indexes.push_back(sym_idx);
2073 type = eSymbolTypeScopeBegin;
2074 break;
2075
2076 case StabRightBracket:
2077 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2078 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2079 // so that we can always skip the entire symbol if we need to navigate
2080 // more quickly at the source level when parsing STABS
2081 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2082 if ( !N_BRAC_indexes.empty() )
2083 {
2084 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2085 symbol_ptr->SetByteSize(sym_idx + 1);
2086 symbol_ptr->SetSizeIsSibling(true);
2087 N_BRAC_indexes.pop_back();
2088 }
2089 type = eSymbolTypeScopeEnd;
2090 break;
2091
2092 case StabDeletedIncludeFile:
2093 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2094 type = eSymbolTypeHeaderFile;
2095 break;
2096
2097 //----------------------------------------------------------------------
2098 // COMM scopes
2099 //----------------------------------------------------------------------
2100 case StabBeginCommon:
2101 // N_BCOMM - begin common: name,,NO_SECT,0,0
2102 // We use the current number of symbols in the symbol table in lieu of
2103 // using nlist_idx in case we ever start trimming entries out
2104 type = eSymbolTypeScopeBegin;
2105 N_COMM_indexes.push_back(sym_idx);
2106 break;
2107
2108 case StabEndCommonLocal:
2109 // N_ECOML - end common (local name): 0,,n_sect,0,address
2110 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2111 // Fall through
2112
2113 case StabEndCommon:
2114 // N_ECOMM - end common: name,,n_sect,0,0
2115 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2116 // so that we can always skip the entire symbol if we need to navigate
2117 // more quickly at the source level when parsing STABS
2118 if ( !N_COMM_indexes.empty() )
2119 {
2120 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2121 symbol_ptr->SetByteSize(sym_idx + 1);
2122 symbol_ptr->SetSizeIsSibling(true);
2123 N_COMM_indexes.pop_back();
2124 }
2125 type = eSymbolTypeScopeEnd;
2126 break;
2127
2128 case StabLength:
2129 // N_LENG - second stab entry with length information
2130 type = eSymbolTypeAdditional;
2131 break;
2132
2133 default: break;
2134 }
2135 }
2136 else
2137 {
2138 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2139 uint8_t n_type = NlistMaskType & nlist.n_type;
2140 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2141
2142 switch (n_type)
2143 {
2144 case NListTypeIndirect: // N_INDR - Fall through
2145 case NListTypePreboundUndefined:// N_PBUD - Fall through
2146 case NListTypeUndefined: // N_UNDF
2147 type = eSymbolTypeUndefined;
2148 break;
2149
2150 case NListTypeAbsolute: // N_ABS
2151 type = eSymbolTypeAbsolute;
2152 break;
2153
2154 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002155 {
2156 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002157
Greg Clayton01e6a582012-11-27 01:52:16 +00002158 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002159 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002160 // TODO: warn about this?
2161 add_nlist = false;
2162 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002163 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002164
2165 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002166 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002167 type = eSymbolTypeException;
2168 }
2169 else
2170 {
2171 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002172
Greg Clayton01e6a582012-11-27 01:52:16 +00002173 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002174 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002175 case SectionTypeRegular: break; // regular section
2176 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2177 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2178 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2179 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2180 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2181 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2182 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2183 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2184 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2185 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2186 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2187 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2188 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2189 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2190 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2191 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2192 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002193 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002194
Greg Clayton01e6a582012-11-27 01:52:16 +00002195 if (type == eSymbolTypeInvalid)
2196 {
2197 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2198 if (symbol_section->IsDescendant (text_section_sp.get()))
2199 {
2200 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2201 SectionAttrUserSelfModifyingCode |
2202 SectionAttrSytemSomeInstructions))
2203 type = eSymbolTypeData;
2204 else
2205 type = eSymbolTypeCode;
2206 }
2207 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002208 {
2209 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2210 {
2211 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002212
Greg Clayton01e6a582012-11-27 01:52:16 +00002213 if (symbol_name &&
2214 symbol_name[0] == '_' &&
2215 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002216 symbol_name[2] == 'B')
2217 {
2218 llvm::StringRef symbol_name_ref(symbol_name);
2219 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2220 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2221 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2222 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2223 {
2224 symbol_name_non_abi_mangled = symbol_name + 1;
2225 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2226 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002227 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002228 }
2229 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2230 {
2231 symbol_name_non_abi_mangled = symbol_name + 1;
2232 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2233 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002234 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002235 }
2236 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2237 {
2238 symbol_name_non_abi_mangled = symbol_name + 1;
2239 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2240 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002241 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002242 }
2243 }
2244 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002245 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002246 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002247 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002248 }
2249 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002250 {
2251 type = eSymbolTypeData;
2252 }
2253 }
2254 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2255 {
2256 type = eSymbolTypeTrampoline;
2257 }
2258 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2259 {
2260 type = eSymbolTypeRuntime;
2261 if (symbol_name && symbol_name[0] == '.')
2262 {
2263 llvm::StringRef symbol_name_ref(symbol_name);
2264 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2265 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002266 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002267 symbol_name_non_abi_mangled = symbol_name;
2268 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2269 type = eSymbolTypeObjCClass;
2270 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002271 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002272 }
2273 }
2274 }
Jason Molendab62abd52012-06-21 01:51:02 +00002275 }
2276 }
Jason Molendab62abd52012-06-21 01:51:02 +00002277 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002278 }
Jason Molendab62abd52012-06-21 01:51:02 +00002279 }
2280
2281 if (add_nlist)
2282 {
2283 uint64_t symbol_value = nlist.n_value;
2284 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002285
Jason Molendab62abd52012-06-21 01:51:02 +00002286 if (symbol_name_non_abi_mangled)
2287 {
Jason Molenda292cca82012-07-20 03:35:44 +00002288 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2289 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002290 }
2291 else
2292 {
2293 if (symbol_name && symbol_name[0] == '_')
2294 {
2295 symbol_name_is_mangled = symbol_name[1] == '_';
2296 symbol_name++; // Skip the leading underscore
2297 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002298
Jason Molendab62abd52012-06-21 01:51:02 +00002299 if (symbol_name)
2300 {
Jason Molenda292cca82012-07-20 03:35:44 +00002301 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002302 }
2303 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002304
Jason Molendab62abd52012-06-21 01:51:02 +00002305 if (is_debug == false)
2306 {
2307 if (type == eSymbolTypeCode)
2308 {
2309 // See if we can find a N_FUN entry for any code symbols.
2310 // If we do find a match, and the name matches, then we
2311 // can merge the two into just the function symbol to avoid
2312 // duplicate entries in the symbol table
2313 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2314 if (pos != N_FUN_addr_to_sym_idx.end())
2315 {
2316 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2317 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2318 {
2319 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2320 // We just need the flags from the linker symbol, so put these flags
2321 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2322 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2323 sym[sym_idx].Clear();
2324 continue;
2325 }
2326 }
2327 }
2328 else if (type == eSymbolTypeData)
2329 {
2330 // See if we can find a N_STSYM entry for any data symbols.
2331 // If we do find a match, and the name matches, then we
2332 // can merge the two into just the Static symbol to avoid
2333 // duplicate entries in the symbol table
2334 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2335 if (pos != N_STSYM_addr_to_sym_idx.end())
2336 {
2337 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2338 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2339 {
2340 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2341 // We just need the flags from the linker symbol, so put these flags
2342 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2343 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2344 sym[sym_idx].Clear();
2345 continue;
2346 }
2347 }
2348 }
2349 }
2350 if (symbol_section)
2351 {
2352 const addr_t section_file_addr = symbol_section->GetFileAddress();
2353 if (symbol_byte_size == 0 && function_starts_count > 0)
2354 {
2355 addr_t symbol_lookup_file_addr = nlist.n_value;
2356 // Do an exact address match for non-ARM addresses, else get the closest since
2357 // the symbol might be a thumb symbol which has an address with bit zero set
2358 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2359 if (is_arm && func_start_entry)
2360 {
2361 // Verify that the function start address is the symbol address (ARM)
2362 // or the symbol address + 1 (thumb)
2363 if (func_start_entry->addr != symbol_lookup_file_addr &&
2364 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2365 {
2366 // Not the right entry, NULL it out...
2367 func_start_entry = NULL;
2368 }
2369 }
2370 if (func_start_entry)
2371 {
2372 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002373
Jason Molendab62abd52012-06-21 01:51:02 +00002374 addr_t symbol_file_addr = func_start_entry->addr;
2375 uint32_t symbol_flags = 0;
2376 if (is_arm)
2377 {
2378 if (symbol_file_addr & 1)
2379 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2380 symbol_file_addr &= 0xfffffffffffffffeull;
2381 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002382
Jason Molendab62abd52012-06-21 01:51:02 +00002383 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2384 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2385 if (next_func_start_entry)
2386 {
2387 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2388 // Be sure the clear the Thumb address bit when we calculate the size
2389 // from the current and next address
2390 if (is_arm)
2391 next_symbol_file_addr &= 0xfffffffffffffffeull;
2392 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2393 }
2394 else
2395 {
2396 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2397 }
2398 }
2399 }
2400 symbol_value -= section_file_addr;
2401 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002402
Jason Molendab62abd52012-06-21 01:51:02 +00002403 sym[sym_idx].SetID (nlist_idx);
2404 sym[sym_idx].SetType (type);
2405 sym[sym_idx].GetAddress().SetSection (symbol_section);
2406 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2407 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002408
Jason Molendab62abd52012-06-21 01:51:02 +00002409 if (symbol_byte_size > 0)
2410 sym[sym_idx].SetByteSize(symbol_byte_size);
2411
Greg Clayton01e6a582012-11-27 01:52:16 +00002412 if (demangled_is_synthesized)
2413 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002414 ++sym_idx;
2415 }
2416 else
2417 {
2418 sym[sym_idx].Clear();
2419 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002420
Jason Molendab62abd52012-06-21 01:51:02 +00002421 }
2422 /////////////////////////////
2423 }
2424 break; // No more entries to consider
2425 }
2426 }
2427 }
2428 }
2429 }
2430 }
2431 }
2432
2433 // Must reset this in case it was mutated above!
2434 nlist_data_offset = 0;
2435#endif
2436
2437 // If the sym array was not created while parsing the DSC unmapped
2438 // symbols, create it now.
2439 if (sym == NULL)
2440 {
2441 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2442 num_syms = symtab->GetNumSymbols();
2443 }
2444
2445 if (unmapped_local_symbols_found)
2446 {
2447 assert(m_dysymtab.ilocalsym == 0);
2448 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2449 nlist_idx = m_dysymtab.nlocalsym;
2450 }
2451 else
2452 {
2453 nlist_idx = 0;
2454 }
2455
2456 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002457 {
2458 struct nlist_64 nlist;
2459 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2460 break;
2461
2462 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2463 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2464 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2465 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2466 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2467
2468 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002469 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002470
Greg Clayton3a5dc012012-05-25 17:04:00 +00002471 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002472 {
2473 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002474
Greg Claytondd29b972012-05-18 23:20:01 +00002475 if (symbol_name == NULL)
2476 {
2477 // No symbol should be NULL, even the symbols with no
2478 // string values should have an offset zero which points
2479 // to an empty C-string
2480 Host::SystemLog (Host::eSystemLogError,
Jason Molenda9badb6c2013-03-06 23:19:17 +00002481 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002482 nlist_idx,
2483 nlist.n_strx,
2484 module_sp->GetFileSpec().GetDirectory().GetCString(),
2485 module_sp->GetFileSpec().GetFilename().GetCString());
2486 continue;
2487 }
2488 if (symbol_name[0] == '\0')
2489 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002490 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002491 else
2492 {
2493 const addr_t str_addr = strtab_addr + nlist.n_strx;
2494 Error str_error;
2495 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2496 symbol_name = memory_symbol_name.c_str();
2497 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002498 const char *symbol_name_non_abi_mangled = NULL;
2499
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002500 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002501 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002502 bool add_nlist = true;
2503 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002504 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002505
2506 assert (sym_idx < num_syms);
2507
2508 sym[sym_idx].SetDebug (is_debug);
2509
2510 if (is_debug)
2511 {
2512 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002513 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002514 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002515 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2516 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002517
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002518 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2519 // have the same address, but we want to ensure that we always find only the real symbol,
2520 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2521 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2522 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2523 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002524
2525 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002526 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2527 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2528 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2529 add_nlist = false;
2530 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002531 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002532 sym[sym_idx].SetExternal(true);
2533 if (nlist.n_value != 0)
2534 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2535 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002536 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002537 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002538
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002539 case StabFunctionName:
2540 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2541 type = eSymbolTypeCompiler;
2542 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002543
Jason Molenda9badb6c2013-03-06 23:19:17 +00002544 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002545 // N_FUN -- procedure: name,,n_sect,linenumber,address
2546 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002547 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002548 type = eSymbolTypeCode;
2549 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002550
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002551 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2552 // We use the current number of symbols in the symbol table in lieu of
2553 // using nlist_idx in case we ever start trimming entries out
2554 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002555 }
2556 else
2557 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002558 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002559
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002560 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002561 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002562 // Copy the size of the function into the original STAB entry so we don't have
2563 // to hunt for it later
2564 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2565 N_FUN_indexes.pop_back();
2566 // We don't really need the end function STAB as it contains the size which
2567 // we already placed with the original symbol, so don't add it if we want a
2568 // minimal symbol table
2569 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002570 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002571 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002572 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002573 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002574
Jason Molenda9badb6c2013-03-06 23:19:17 +00002575 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002576 // N_STSYM -- static symbol: name,,n_sect,type,address
2577 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2578 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2579 type = eSymbolTypeData;
2580 break;
2581
2582 case StabLocalCommon:
2583 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2584 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2585 type = eSymbolTypeCommonBlock;
2586 break;
2587
2588 case StabBeginSymbol:
2589 // N_BNSYM
2590 // We use the current number of symbols in the symbol table in lieu of
2591 // using nlist_idx in case we ever start trimming entries out
2592 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002593 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002594 // Skip these if we want minimal symbol tables
2595 add_nlist = false;
2596 }
2597 else
2598 {
2599 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2600 N_NSYM_indexes.push_back(sym_idx);
2601 type = eSymbolTypeScopeBegin;
2602 }
2603 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002604
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002605 case StabEndSymbol:
2606 // N_ENSYM
2607 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2608 // so that we can always skip the entire symbol if we need to navigate
2609 // more quickly at the source level when parsing STABS
2610 if (minimize)
2611 {
2612 // Skip these if we want minimal symbol tables
2613 add_nlist = false;
2614 }
2615 else
2616 {
2617 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002618 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002619 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2620 symbol_ptr->SetByteSize(sym_idx + 1);
2621 symbol_ptr->SetSizeIsSibling(true);
2622 N_NSYM_indexes.pop_back();
2623 }
2624 type = eSymbolTypeScopeEnd;
2625 }
2626 break;
2627
2628
2629 case StabSourceFileOptions:
2630 // N_OPT - emitted with gcc2_compiled and in gcc source
2631 type = eSymbolTypeCompiler;
2632 break;
2633
2634 case StabRegisterSymbol:
2635 // N_RSYM - register sym: name,,NO_SECT,type,register
2636 type = eSymbolTypeVariable;
2637 break;
2638
2639 case StabSourceLine:
2640 // N_SLINE - src line: 0,,n_sect,linenumber,address
2641 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2642 type = eSymbolTypeLineEntry;
2643 break;
2644
2645 case StabStructureType:
2646 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2647 type = eSymbolTypeVariableType;
2648 break;
2649
2650 case StabSourceFileName:
2651 // N_SO - source file name
2652 type = eSymbolTypeSourceFile;
2653 if (symbol_name == NULL)
2654 {
2655 if (minimize)
2656 add_nlist = false;
2657 if (N_SO_index != UINT32_MAX)
2658 {
2659 // Set the size of the N_SO to the terminating index of this N_SO
2660 // so that we can always skip the entire N_SO if we need to navigate
2661 // more quickly at the source level when parsing STABS
2662 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2663 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2664 symbol_ptr->SetSizeIsSibling(true);
2665 }
2666 N_NSYM_indexes.clear();
2667 N_INCL_indexes.clear();
2668 N_BRAC_indexes.clear();
2669 N_COMM_indexes.clear();
2670 N_FUN_indexes.clear();
2671 N_SO_index = UINT32_MAX;
2672 }
2673 else
2674 {
2675 // We use the current number of symbols in the symbol table in lieu of
2676 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002677 const bool N_SO_has_full_path = symbol_name[0] == '/';
2678 if (N_SO_has_full_path)
2679 {
2680 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2681 {
2682 // We have two consecutive N_SO entries where the first contains a directory
2683 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002684 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002685 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2686 add_nlist = false;
2687 }
2688 else
2689 {
2690 // This is the first entry in a N_SO that contains a directory or
2691 // a full path to the source file
2692 N_SO_index = sym_idx;
2693 }
2694 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002695 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2696 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002697 // This is usually the second N_SO entry that contains just the filename,
2698 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002699 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2700 if (so_path && so_path[0])
2701 {
2702 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002703 const size_t double_slash_pos = full_so_path.find("//");
2704 if (double_slash_pos != std::string::npos)
2705 {
2706 // The linker has been generating bad N_SO entries with doubled up paths
2707 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2708 // and the second is the directory for the source file so you end up with
2709 // a path that looks like "/tmp/src//tmp/src/"
2710 FileSpec so_dir(so_path, false);
2711 if (!so_dir.Exists())
2712 {
2713 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2714 if (so_dir.Exists())
2715 {
2716 // Trim off the incorrect path
2717 full_so_path.erase(0, double_slash_pos + 1);
2718 }
2719 }
2720 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002721 if (*full_so_path.rbegin() != '/')
2722 full_so_path += '/';
2723 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002724 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002725 add_nlist = false;
2726 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2727 }
2728 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002729 else
2730 {
2731 // This could be a relative path to a N_SO
2732 N_SO_index = sym_idx;
2733 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002734 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002735
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002736 break;
2737
2738 case StabObjectFileName:
2739 // N_OSO - object file name: name,,0,0,st_mtime
2740 type = eSymbolTypeObjectFile;
2741 break;
2742
2743 case StabLocalSymbol:
2744 // N_LSYM - local sym: name,,NO_SECT,type,offset
2745 type = eSymbolTypeLocal;
2746 break;
2747
2748 //----------------------------------------------------------------------
2749 // INCL scopes
2750 //----------------------------------------------------------------------
2751 case StabBeginIncludeFileName:
2752 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2753 // We use the current number of symbols in the symbol table in lieu of
2754 // using nlist_idx in case we ever start trimming entries out
2755 N_INCL_indexes.push_back(sym_idx);
2756 type = eSymbolTypeScopeBegin;
2757 break;
2758
2759 case StabEndIncludeFile:
2760 // N_EINCL - include file end: name,,NO_SECT,0,0
2761 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2762 // so that we can always skip the entire symbol if we need to navigate
2763 // more quickly at the source level when parsing STABS
2764 if ( !N_INCL_indexes.empty() )
2765 {
2766 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2767 symbol_ptr->SetByteSize(sym_idx + 1);
2768 symbol_ptr->SetSizeIsSibling(true);
2769 N_INCL_indexes.pop_back();
2770 }
2771 type = eSymbolTypeScopeEnd;
2772 break;
2773
2774 case StabIncludeFileName:
2775 // N_SOL - #included file name: name,,n_sect,0,address
2776 type = eSymbolTypeHeaderFile;
2777
2778 // We currently don't use the header files on darwin
2779 if (minimize)
2780 add_nlist = false;
2781 break;
2782
Jason Molenda9badb6c2013-03-06 23:19:17 +00002783 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002784 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2785 type = eSymbolTypeCompiler;
2786 break;
2787
2788 case StabCompilerVersion:
2789 // N_VERSION - compiler version: name,,NO_SECT,0,0
2790 type = eSymbolTypeCompiler;
2791 break;
2792
2793 case StabCompilerOptLevel:
2794 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2795 type = eSymbolTypeCompiler;
2796 break;
2797
2798 case StabParameter:
2799 // N_PSYM - parameter: name,,NO_SECT,type,offset
2800 type = eSymbolTypeVariable;
2801 break;
2802
2803 case StabAlternateEntry:
2804 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2805 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2806 type = eSymbolTypeLineEntry;
2807 break;
2808
2809 //----------------------------------------------------------------------
2810 // Left and Right Braces
2811 //----------------------------------------------------------------------
2812 case StabLeftBracket:
2813 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2814 // We use the current number of symbols in the symbol table in lieu of
2815 // using nlist_idx in case we ever start trimming entries out
2816 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2817 N_BRAC_indexes.push_back(sym_idx);
2818 type = eSymbolTypeScopeBegin;
2819 break;
2820
2821 case StabRightBracket:
2822 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2823 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2824 // so that we can always skip the entire symbol if we need to navigate
2825 // more quickly at the source level when parsing STABS
2826 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2827 if ( !N_BRAC_indexes.empty() )
2828 {
2829 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2830 symbol_ptr->SetByteSize(sym_idx + 1);
2831 symbol_ptr->SetSizeIsSibling(true);
2832 N_BRAC_indexes.pop_back();
2833 }
2834 type = eSymbolTypeScopeEnd;
2835 break;
2836
2837 case StabDeletedIncludeFile:
2838 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2839 type = eSymbolTypeHeaderFile;
2840 break;
2841
2842 //----------------------------------------------------------------------
2843 // COMM scopes
2844 //----------------------------------------------------------------------
2845 case StabBeginCommon:
2846 // N_BCOMM - begin common: name,,NO_SECT,0,0
2847 // We use the current number of symbols in the symbol table in lieu of
2848 // using nlist_idx in case we ever start trimming entries out
2849 type = eSymbolTypeScopeBegin;
2850 N_COMM_indexes.push_back(sym_idx);
2851 break;
2852
2853 case StabEndCommonLocal:
2854 // N_ECOML - end common (local name): 0,,n_sect,0,address
2855 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2856 // Fall through
2857
2858 case StabEndCommon:
2859 // N_ECOMM - end common: name,,n_sect,0,0
2860 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2861 // so that we can always skip the entire symbol if we need to navigate
2862 // more quickly at the source level when parsing STABS
2863 if ( !N_COMM_indexes.empty() )
2864 {
2865 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2866 symbol_ptr->SetByteSize(sym_idx + 1);
2867 symbol_ptr->SetSizeIsSibling(true);
2868 N_COMM_indexes.pop_back();
2869 }
2870 type = eSymbolTypeScopeEnd;
2871 break;
2872
2873 case StabLength:
2874 // N_LENG - second stab entry with length information
2875 type = eSymbolTypeAdditional;
2876 break;
2877
2878 default: break;
2879 }
2880 }
2881 else
2882 {
2883 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2884 uint8_t n_type = NlistMaskType & nlist.n_type;
2885 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2886
2887 switch (n_type)
2888 {
2889 case NListTypeIndirect: // N_INDR - Fall through
2890 case NListTypePreboundUndefined:// N_PBUD - Fall through
2891 case NListTypeUndefined: // N_UNDF
2892 type = eSymbolTypeUndefined;
2893 break;
2894
2895 case NListTypeAbsolute: // N_ABS
2896 type = eSymbolTypeAbsolute;
2897 break;
2898
2899 case NListTypeSection: // N_SECT
2900 {
2901 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2902
Sean Callananb386d822012-08-09 00:50:26 +00002903 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002904 {
2905 // TODO: warn about this?
2906 add_nlist = false;
2907 break;
2908 }
2909
2910 if (TEXT_eh_frame_sectID == nlist.n_sect)
2911 {
2912 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00002913 }
2914 else
2915 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002916 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2917
2918 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00002919 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002920 case SectionTypeRegular: break; // regular section
2921 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2922 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2923 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2924 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2925 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2926 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2927 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2928 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2929 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2930 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2931 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2932 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2933 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2934 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2935 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2936 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2937 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002938 }
Chris Lattner24943d22010-06-08 16:52:24 +00002939
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002940 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002941 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002942 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2943 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002944 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002945 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2946 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002947 SectionAttrSytemSomeInstructions))
2948 type = eSymbolTypeData;
2949 else
2950 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00002951 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002952 else
2953 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002954 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002955 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00002956 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002957 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00002958
Jason Molenda9badb6c2013-03-06 23:19:17 +00002959 if (symbol_name &&
2960 symbol_name[0] == '_' &&
2961 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002962 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00002963 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002964 llvm::StringRef symbol_name_ref(symbol_name);
2965 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2966 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2967 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2968 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00002969 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002970 symbol_name_non_abi_mangled = symbol_name + 1;
2971 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2972 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002973 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00002974 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002975 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00002976 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002977 symbol_name_non_abi_mangled = symbol_name + 1;
2978 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2979 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002980 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002981 }
2982 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2983 {
2984 symbol_name_non_abi_mangled = symbol_name + 1;
2985 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2986 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002987 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00002988 }
2989 }
2990 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002991 else
2992 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
2993 {
2994 type = eSymbolTypeException;
2995 }
2996 else
2997 {
2998 type = eSymbolTypeData;
2999 }
3000 }
3001 else
3002 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3003 {
3004 type = eSymbolTypeTrampoline;
3005 }
3006 else
3007 if (symbol_section->IsDescendant(objc_section_sp.get()))
3008 {
3009 type = eSymbolTypeRuntime;
3010 if (symbol_name && symbol_name[0] == '.')
3011 {
3012 llvm::StringRef symbol_name_ref(symbol_name);
3013 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3014 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3015 {
3016 symbol_name_non_abi_mangled = symbol_name;
3017 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3018 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003019 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003020 }
3021 }
Chris Lattner24943d22010-06-08 16:52:24 +00003022 }
3023 }
3024 }
3025 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003026 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003027 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003028 }
3029
3030 if (add_nlist)
3031 {
3032 uint64_t symbol_value = nlist.n_value;
3033 bool symbol_name_is_mangled = false;
3034
3035 if (symbol_name_non_abi_mangled)
3036 {
Greg Claytonc0240042012-07-18 23:18:10 +00003037 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3038 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003039 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003040 else
3041 {
3042 if (symbol_name && symbol_name[0] == '_')
3043 {
3044 symbol_name_is_mangled = symbol_name[1] == '_';
3045 symbol_name++; // Skip the leading underscore
3046 }
3047
3048 if (symbol_name)
3049 {
Greg Claytonc0240042012-07-18 23:18:10 +00003050 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003051 }
3052 }
3053
3054 if (is_debug == false)
3055 {
3056 if (type == eSymbolTypeCode)
3057 {
3058 // See if we can find a N_FUN entry for any code symbols.
3059 // If we do find a match, and the name matches, then we
3060 // can merge the two into just the function symbol to avoid
3061 // duplicate entries in the symbol table
3062 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3063 if (pos != N_FUN_addr_to_sym_idx.end())
3064 {
3065 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3066 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3067 {
3068 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3069 // We just need the flags from the linker symbol, so put these flags
3070 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3071 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3072 sym[sym_idx].Clear();
3073 continue;
3074 }
3075 }
3076 }
3077 else if (type == eSymbolTypeData)
3078 {
3079 // See if we can find a N_STSYM entry for any data symbols.
3080 // If we do find a match, and the name matches, then we
3081 // can merge the two into just the Static symbol to avoid
3082 // duplicate entries in the symbol table
3083 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3084 if (pos != N_STSYM_addr_to_sym_idx.end())
3085 {
3086 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3087 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3088 {
3089 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3090 // We just need the flags from the linker symbol, so put these flags
3091 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3092 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3093 sym[sym_idx].Clear();
3094 continue;
3095 }
3096 }
3097 }
3098 }
3099 if (symbol_section)
3100 {
3101 const addr_t section_file_addr = symbol_section->GetFileAddress();
3102 if (symbol_byte_size == 0 && function_starts_count > 0)
3103 {
Greg Claytond2653c22012-03-14 01:53:24 +00003104 addr_t symbol_lookup_file_addr = nlist.n_value;
3105 // Do an exact address match for non-ARM addresses, else get the closest since
3106 // the symbol might be a thumb symbol which has an address with bit zero set
3107 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3108 if (is_arm && func_start_entry)
3109 {
3110 // Verify that the function start address is the symbol address (ARM)
3111 // or the symbol address + 1 (thumb)
3112 if (func_start_entry->addr != symbol_lookup_file_addr &&
3113 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3114 {
3115 // Not the right entry, NULL it out...
3116 func_start_entry = NULL;
3117 }
3118 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003119 if (func_start_entry)
3120 {
3121 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003122
Greg Claytond2653c22012-03-14 01:53:24 +00003123 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003124 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003125 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003126
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003127 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3128 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3129 if (next_func_start_entry)
3130 {
Greg Claytond2653c22012-03-14 01:53:24 +00003131 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3132 // Be sure the clear the Thumb address bit when we calculate the size
3133 // from the current and next address
3134 if (is_arm)
3135 next_symbol_file_addr &= 0xfffffffffffffffeull;
3136 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 +00003137 }
3138 else
3139 {
Greg Claytond2653c22012-03-14 01:53:24 +00003140 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003141 }
3142 }
3143 }
3144 symbol_value -= section_file_addr;
3145 }
3146
3147 sym[sym_idx].SetID (nlist_idx);
3148 sym[sym_idx].SetType (type);
3149 sym[sym_idx].GetAddress().SetSection (symbol_section);
3150 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3151 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3152
3153 if (symbol_byte_size > 0)
3154 sym[sym_idx].SetByteSize(symbol_byte_size);
3155
Greg Clayton01e6a582012-11-27 01:52:16 +00003156 if (demangled_is_synthesized)
3157 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3158
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003159 ++sym_idx;
3160 }
3161 else
3162 {
3163 sym[sym_idx].Clear();
3164 }
3165
3166 }
3167
3168 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3169 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3170 // such entries by figuring out what the address for the global is by looking up this non-STAB
3171 // entry and copying the value into the debug symbol's value to save us the hassle in the
3172 // debug symbol parser.
3173
3174 Symbol *global_symbol = NULL;
3175 for (nlist_idx = 0;
3176 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3177 nlist_idx++)
3178 {
3179 if (global_symbol->GetAddress().GetFileAddress() == 0)
3180 {
3181 std::vector<uint32_t> indexes;
3182 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3183 {
3184 std::vector<uint32_t>::const_iterator pos;
3185 std::vector<uint32_t>::const_iterator end = indexes.end();
3186 for (pos = indexes.begin(); pos != end; ++pos)
3187 {
3188 symbol_ptr = symtab->SymbolAtIndex(*pos);
3189 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3190 {
3191 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3192 break;
3193 }
3194 }
3195 }
Chris Lattner24943d22010-06-08 16:52:24 +00003196 }
3197 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003198
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003199 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3200
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003201 if (function_starts_count > 0)
3202 {
3203 char synthetic_function_symbol[PATH_MAX];
3204 uint32_t num_synthetic_function_symbols = 0;
3205 for (i=0; i<function_starts_count; ++i)
3206 {
3207 if (function_starts.GetEntryRef (i).data == false)
3208 ++num_synthetic_function_symbols;
3209 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003210
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003211 if (num_synthetic_function_symbols > 0)
3212 {
3213 if (num_syms < sym_idx + num_synthetic_function_symbols)
3214 {
3215 num_syms = sym_idx + num_synthetic_function_symbols;
3216 sym = symtab->Resize (num_syms);
3217 }
3218 uint32_t synthetic_function_symbol_idx = 0;
3219 for (i=0; i<function_starts_count; ++i)
3220 {
3221 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3222 if (func_start_entry->data == false)
3223 {
Greg Claytond2653c22012-03-14 01:53:24 +00003224 addr_t symbol_file_addr = func_start_entry->addr;
3225 uint32_t symbol_flags = 0;
3226 if (is_arm)
3227 {
3228 if (symbol_file_addr & 1)
3229 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3230 symbol_file_addr &= 0xfffffffffffffffeull;
3231 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003232 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003233 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003234 {
3235 SectionSP symbol_section (symbol_addr.GetSection());
3236 uint32_t symbol_byte_size = 0;
3237 if (symbol_section)
3238 {
3239 const addr_t section_file_addr = symbol_section->GetFileAddress();
3240 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3241 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3242 if (next_func_start_entry)
3243 {
Greg Claytond2653c22012-03-14 01:53:24 +00003244 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3245 if (is_arm)
3246 next_symbol_file_addr &= 0xfffffffffffffffeull;
3247 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 +00003248 }
3249 else
3250 {
Greg Claytond2653c22012-03-14 01:53:24 +00003251 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003252 }
3253 snprintf (synthetic_function_symbol,
3254 sizeof(synthetic_function_symbol),
3255 "___lldb_unnamed_function%u$$%s",
3256 ++synthetic_function_symbol_idx,
3257 module_sp->GetFileSpec().GetFilename().GetCString());
3258 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003259 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003260 sym[sym_idx].SetType (eSymbolTypeCode);
3261 sym[sym_idx].SetIsSynthetic (true);
3262 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003263 if (symbol_flags)
3264 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003265 if (symbol_byte_size)
3266 sym[sym_idx].SetByteSize (symbol_byte_size);
3267 ++sym_idx;
3268 }
3269 }
3270 }
3271 }
3272 }
3273 }
3274
3275 // Trim our symbols down to just what we ended up with after
3276 // removing any symbols.
3277 if (sym_idx < num_syms)
3278 {
3279 num_syms = sym_idx;
3280 sym = symtab->Resize (num_syms);
3281 }
3282
3283 // Now synthesize indirect symbols
3284 if (m_dysymtab.nindirectsyms != 0)
3285 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003286 if (indirect_symbol_index_data.GetByteSize())
3287 {
3288 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3289
3290 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3291 {
3292 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3293 {
3294 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3295 if (symbol_stub_byte_size == 0)
3296 continue;
3297
3298 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3299
3300 if (num_symbol_stubs == 0)
3301 continue;
3302
3303 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3304 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3305 {
3306 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3307 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
Greg Clayton36da2aa2013-01-25 18:06:21 +00003308 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003309 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3310 {
3311 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3312 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3313 continue;
3314
3315 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3316 Symbol *stub_symbol = NULL;
3317 if (index_pos != end_index_pos)
3318 {
3319 // We have a remapping from the original nlist index to
3320 // a current symbol index, so just look this up by index
3321 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3322 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003323 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003324 {
3325 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003326 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003327 // S_SYMBOL_STUBS
3328 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3329 }
3330
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003331 if (stub_symbol)
3332 {
3333 Address so_addr(symbol_stub_addr, section_list);
3334
3335 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3336 {
3337 // Change the external symbol into a trampoline that makes sense
3338 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3339 // can re-use them so we don't have to make up a synthetic symbol
3340 // for no good reason.
3341 stub_symbol->SetType (eSymbolTypeTrampoline);
3342 stub_symbol->SetExternal (false);
3343 stub_symbol->GetAddress() = so_addr;
3344 stub_symbol->SetByteSize (symbol_stub_byte_size);
3345 }
3346 else
3347 {
3348 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003349 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003350 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003351 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003352 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003353 stub_symbol = NULL; // this pointer no longer valid
3354 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003355 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003356 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003357 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3358 sym[sym_idx].SetIsSynthetic (true);
3359 sym[sym_idx].GetAddress() = so_addr;
3360 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3361 ++sym_idx;
3362 }
3363 }
Greg Claytond4330e62012-09-05 01:38:55 +00003364 else
3365 {
3366 if (log)
3367 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3368 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003369 }
3370 }
3371 }
3372 }
3373 }
3374 }
3375 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003376 }
3377 return 0;
3378}
3379
3380
3381void
3382ObjectFileMachO::Dump (Stream *s)
3383{
Greg Clayton9482f052012-03-13 23:14:29 +00003384 ModuleSP module_sp(GetModule());
3385 if (module_sp)
3386 {
3387 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3388 s->Printf("%p: ", this);
3389 s->Indent();
3390 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3391 s->PutCString("ObjectFileMachO64");
3392 else
3393 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003394
Greg Clayton9482f052012-03-13 23:14:29 +00003395 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003396
Greg Clayton9482f052012-03-13 23:14:29 +00003397 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003398
Greg Clayton9482f052012-03-13 23:14:29 +00003399 if (m_sections_ap.get())
3400 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003401
Greg Clayton9482f052012-03-13 23:14:29 +00003402 if (m_symtab_ap.get())
3403 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3404 }
Chris Lattner24943d22010-06-08 16:52:24 +00003405}
3406
3407
3408bool
Greg Clayton0467c782011-02-04 18:53:10 +00003409ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003410{
Greg Clayton9482f052012-03-13 23:14:29 +00003411 ModuleSP module_sp(GetModule());
3412 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003413 {
Greg Clayton9482f052012-03-13 23:14:29 +00003414 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3415 struct uuid_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003416 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003417 uint32_t i;
3418 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003419 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003420 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003421 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3422 break;
3423
3424 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00003425 {
Greg Clayton9482f052012-03-13 23:14:29 +00003426 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003427
Greg Clayton9482f052012-03-13 23:14:29 +00003428 if (uuid_bytes)
3429 {
Sean Callanana5689d52012-07-12 18:04:03 +00003430 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3431 // We pretend these object files have no UUID to prevent crashing.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003432
Sean Callanana5689d52012-07-12 18:04:03 +00003433 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3434 0x3b, 0xa8,
3435 0x4b, 0x16,
3436 0xb6, 0xa4,
3437 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
Jason Molenda9badb6c2013-03-06 23:19:17 +00003438
Sean Callanana5689d52012-07-12 18:04:03 +00003439 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3440 return false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003441
Greg Clayton9482f052012-03-13 23:14:29 +00003442 uuid->SetBytes (uuid_bytes);
3443 return true;
3444 }
3445 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003446 }
Greg Clayton9482f052012-03-13 23:14:29 +00003447 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003448 }
Chris Lattner24943d22010-06-08 16:52:24 +00003449 }
3450 return false;
3451}
3452
3453
3454uint32_t
3455ObjectFileMachO::GetDependentModules (FileSpecList& files)
3456{
Chris Lattner24943d22010-06-08 16:52:24 +00003457 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003458 ModuleSP module_sp(GetModule());
3459 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003460 {
Greg Clayton9482f052012-03-13 23:14:29 +00003461 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3462 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003463 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003464 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3465 uint32_t i;
3466 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003467 {
Greg Clayton9482f052012-03-13 23:14:29 +00003468 const uint32_t cmd_offset = offset;
3469 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3470 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003471
Greg Clayton9482f052012-03-13 23:14:29 +00003472 switch (load_cmd.cmd)
3473 {
3474 case LoadCommandDylibLoad:
3475 case LoadCommandDylibLoadWeak:
3476 case LoadCommandDylibReexport:
3477 case LoadCommandDynamicLinkerLoad:
3478 case LoadCommandFixedVMShlibLoad:
3479 case LoadCommandDylibLoadUpward:
3480 {
3481 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3482 const char *path = m_data.PeekCStr(name_offset);
3483 // Skip any path that starts with '@' since these are usually:
3484 // @executable_path/.../file
3485 // @rpath/.../file
3486 if (path && path[0] != '@')
3487 {
3488 FileSpec file_spec(path, resolve_path);
3489 if (files.AppendIfUnique(file_spec))
3490 count++;
3491 }
3492 }
3493 break;
3494
3495 default:
3496 break;
3497 }
3498 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003499 }
Chris Lattner24943d22010-06-08 16:52:24 +00003500 }
3501 return count;
3502}
3503
Jim Ingham28775942011-03-07 23:44:08 +00003504lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003505ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003506{
3507 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3508 // is initialized to an invalid address, so we can just return that.
3509 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003510
Jim Ingham28775942011-03-07 23:44:08 +00003511 if (!IsExecutable() || m_entry_point_address.IsValid())
3512 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003513
3514 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003515 // /usr/include/mach-o.h, but it is basically:
3516 //
3517 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3518 // uint32_t count - this is the count of longs in the thread state data
3519 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3520 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003521 //
Jim Ingham28775942011-03-07 23:44:08 +00003522 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3523 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3524 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3525 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3526 //
3527 // For now we hard-code the offsets and flavors we need:
3528 //
3529 //
3530
Greg Clayton9482f052012-03-13 23:14:29 +00003531 ModuleSP module_sp(GetModule());
3532 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003533 {
Greg Clayton9482f052012-03-13 23:14:29 +00003534 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3535 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003536 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003537 uint32_t i;
3538 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3539 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003540
Greg Clayton9482f052012-03-13 23:14:29 +00003541 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003542 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003543 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003544 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3545 break;
3546
3547 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003548 {
Greg Clayton9482f052012-03-13 23:14:29 +00003549 case LoadCommandUnixThread:
3550 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003551 {
Greg Clayton9482f052012-03-13 23:14:29 +00003552 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003553 {
Greg Clayton9482f052012-03-13 23:14:29 +00003554 uint32_t flavor = m_data.GetU32(&offset);
3555 uint32_t count = m_data.GetU32(&offset);
3556 if (count == 0)
3557 {
3558 // We've gotten off somehow, log and exit;
3559 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003560 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003561
Greg Clayton9482f052012-03-13 23:14:29 +00003562 switch (m_header.cputype)
3563 {
3564 case llvm::MachO::CPUTypeARM:
3565 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3566 {
3567 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3568 start_address = m_data.GetU32(&offset);
3569 done = true;
3570 }
Jim Ingham28775942011-03-07 23:44:08 +00003571 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003572 case llvm::MachO::CPUTypeI386:
3573 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3574 {
3575 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3576 start_address = m_data.GetU32(&offset);
3577 done = true;
3578 }
3579 break;
3580 case llvm::MachO::CPUTypeX86_64:
3581 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3582 {
3583 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3584 start_address = m_data.GetU64(&offset);
3585 done = true;
3586 }
3587 break;
3588 default:
3589 return m_entry_point_address;
3590 }
3591 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3592 if (done)
3593 break;
3594 offset += count * 4;
3595 }
Jim Ingham28775942011-03-07 23:44:08 +00003596 }
Greg Clayton9482f052012-03-13 23:14:29 +00003597 break;
3598 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003599 {
Greg Clayton9482f052012-03-13 23:14:29 +00003600 ConstString text_segment_name ("__TEXT");
3601 uint64_t entryoffset = m_data.GetU64(&offset);
3602 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3603 if (text_segment_sp)
3604 {
3605 done = true;
3606 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3607 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003608 }
Greg Clayton9482f052012-03-13 23:14:29 +00003609
3610 default:
3611 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003612 }
Greg Clayton9482f052012-03-13 23:14:29 +00003613 if (done)
3614 break;
Jim Ingham28775942011-03-07 23:44:08 +00003615
Greg Clayton9482f052012-03-13 23:14:29 +00003616 // Go to the next load command:
3617 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003618 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003619
Greg Clayton9482f052012-03-13 23:14:29 +00003620 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003621 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003622 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003623 // of this ObjectFile:
3624 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003625 {
Greg Clayton9482f052012-03-13 23:14:29 +00003626 m_entry_point_address.Clear();
3627 }
3628 }
3629 else
3630 {
3631 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3632 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003633
Greg Clayton9482f052012-03-13 23:14:29 +00003634 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003635
Greg Clayton9482f052012-03-13 23:14:29 +00003636 if (module_sp)
3637 {
3638 SymbolContextList contexts;
3639 SymbolContext context;
3640 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3641 {
3642 if (contexts.GetContextAtIndex(0, context))
3643 m_entry_point_address = context.symbol->GetAddress();
3644 }
Greg Clayton3508c382012-02-24 01:59:29 +00003645 }
3646 }
Jim Ingham28775942011-03-07 23:44:08 +00003647 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003648
Jim Ingham28775942011-03-07 23:44:08 +00003649 return m_entry_point_address;
3650
3651}
3652
Greg Claytonb5a8f142012-02-05 02:38:54 +00003653lldb_private::Address
3654ObjectFileMachO::GetHeaderAddress ()
3655{
3656 lldb_private::Address header_addr;
3657 SectionList *section_list = GetSectionList();
3658 if (section_list)
3659 {
3660 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3661 if (text_segment_sp)
3662 {
Greg Clayton3508c382012-02-24 01:59:29 +00003663 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003664 header_addr.SetOffset (0);
3665 }
3666 }
3667 return header_addr;
3668}
3669
Greg Clayton46c9a352012-02-09 06:16:32 +00003670uint32_t
3671ObjectFileMachO::GetNumThreadContexts ()
3672{
Greg Clayton9482f052012-03-13 23:14:29 +00003673 ModuleSP module_sp(GetModule());
3674 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003675 {
Greg Clayton9482f052012-03-13 23:14:29 +00003676 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3677 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003678 {
Greg Clayton9482f052012-03-13 23:14:29 +00003679 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003680 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003681 FileRangeArray::Entry file_range;
3682 thread_command thread_cmd;
3683 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003684 {
Greg Clayton9482f052012-03-13 23:14:29 +00003685 const uint32_t cmd_offset = offset;
3686 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3687 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003688
Greg Clayton9482f052012-03-13 23:14:29 +00003689 if (thread_cmd.cmd == LoadCommandThread)
3690 {
3691 file_range.SetRangeBase (offset);
3692 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3693 m_thread_context_offsets.Append (file_range);
3694 }
3695 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003696 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003697 }
3698 }
3699 return m_thread_context_offsets.GetSize();
3700}
3701
3702lldb::RegisterContextSP
3703ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3704{
Greg Clayton46c9a352012-02-09 06:16:32 +00003705 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003706
Greg Clayton9482f052012-03-13 23:14:29 +00003707 ModuleSP module_sp(GetModule());
3708 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003709 {
Greg Clayton9482f052012-03-13 23:14:29 +00003710 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3711 if (!m_thread_context_offsets_valid)
3712 GetNumThreadContexts ();
3713
3714 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003715 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003716 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003717
3718 DataExtractor data (m_data,
3719 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003720 thread_context_file_range->GetByteSize());
3721
3722 switch (m_header.cputype)
3723 {
3724 case llvm::MachO::CPUTypeARM:
3725 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3726 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003727
Jim Ingham6f01c932012-10-12 17:34:26 +00003728 case llvm::MachO::CPUTypeI386:
3729 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3730 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003731
Jim Ingham6f01c932012-10-12 17:34:26 +00003732 case llvm::MachO::CPUTypeX86_64:
3733 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3734 break;
3735 }
Greg Clayton9482f052012-03-13 23:14:29 +00003736 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003737 }
3738 return reg_ctx_sp;
3739}
3740
Greg Claytonb5a8f142012-02-05 02:38:54 +00003741
Greg Claytonca319972011-07-09 00:41:34 +00003742ObjectFile::Type
3743ObjectFileMachO::CalculateType()
3744{
3745 switch (m_header.filetype)
3746 {
3747 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3748 if (GetAddressByteSize () == 4)
3749 {
3750 // 32 bit kexts are just object files, but they do have a valid
3751 // UUID load command.
3752 UUID uuid;
3753 if (GetUUID(&uuid))
3754 {
3755 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003756 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003757 // "OSKextGetCurrentIdentifier" as this is required of kexts
3758 if (m_strata == eStrataInvalid)
3759 m_strata = eStrataKernel;
3760 return eTypeSharedLibrary;
3761 }
3762 }
3763 return eTypeObjectFile;
3764
3765 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3766 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3767 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3768 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3769 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3770 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3771 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3772 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3773 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3774 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3775 default:
3776 break;
3777 }
3778 return eTypeUnknown;
3779}
3780
3781ObjectFile::Strata
3782ObjectFileMachO::CalculateStrata()
3783{
3784 switch (m_header.filetype)
3785 {
3786 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3787 {
3788 // 32 bit kexts are just object files, but they do have a valid
3789 // UUID load command.
3790 UUID uuid;
3791 if (GetUUID(&uuid))
3792 {
3793 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003794 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003795 // "OSKextGetCurrentIdentifier" as this is required of kexts
3796 if (m_type == eTypeInvalid)
3797 m_type = eTypeSharedLibrary;
3798
3799 return eStrataKernel;
3800 }
3801 }
3802 return eStrataUnknown;
3803
3804 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3805 // Check for the MH_DYLDLINK bit in the flags
3806 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003807 {
Greg Claytonca319972011-07-09 00:41:34 +00003808 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003809 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003810 else
Sean Callananac725af2012-02-10 20:22:35 +00003811 {
3812 SectionList *section_list = GetSectionList();
3813 if (section_list)
3814 {
3815 static ConstString g_kld_section_name ("__KLD");
3816 if (section_list->FindSectionByName(g_kld_section_name))
3817 return eStrataKernel;
3818 }
3819 }
3820 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00003821
3822 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
3823 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00003824 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00003825 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
3826 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
3827 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
3828 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
3829 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
3830 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
3831 default:
3832 break;
3833 }
3834 return eStrataUnknown;
3835}
3836
3837
Greg Clayton49f4bf22012-02-22 19:41:02 +00003838uint32_t
3839ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
3840{
Greg Clayton9482f052012-03-13 23:14:29 +00003841 ModuleSP module_sp(GetModule());
3842 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003843 {
Greg Clayton9482f052012-03-13 23:14:29 +00003844 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3845 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003846 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003847 uint32_t version_cmd = 0;
3848 uint64_t version = 0;
3849 uint32_t i;
3850 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003851 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003852 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003853 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3854 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003855
Greg Clayton9482f052012-03-13 23:14:29 +00003856 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003857 {
Greg Clayton9482f052012-03-13 23:14:29 +00003858 if (version_cmd == 0)
3859 {
3860 version_cmd = load_cmd.cmd;
3861 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
3862 break;
3863 version = load_cmd.dylib.current_version;
3864 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003865 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00003866 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00003867 }
Greg Clayton9482f052012-03-13 23:14:29 +00003868 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003869 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003870
Greg Clayton9482f052012-03-13 23:14:29 +00003871 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003872 {
Greg Clayton9482f052012-03-13 23:14:29 +00003873 if (versions != NULL && num_versions > 0)
3874 {
3875 if (num_versions > 0)
3876 versions[0] = (version & 0xFFFF0000ull) >> 16;
3877 if (num_versions > 1)
3878 versions[1] = (version & 0x0000FF00ull) >> 8;
3879 if (num_versions > 2)
3880 versions[2] = (version & 0x000000FFull);
3881 // Fill in an remaining version numbers with invalid values
3882 for (i=3; i<num_versions; ++i)
3883 versions[i] = UINT32_MAX;
3884 }
3885 // The LC_ID_DYLIB load command has a version with 3 version numbers
3886 // in it, so always return 3
3887 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003888 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00003889 }
3890 return false;
3891}
3892
Chris Lattner24943d22010-06-08 16:52:24 +00003893bool
Greg Clayton395fc332011-02-15 21:59:32 +00003894ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00003895{
Greg Clayton9482f052012-03-13 23:14:29 +00003896 ModuleSP module_sp(GetModule());
3897 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003898 {
Greg Clayton9482f052012-03-13 23:14:29 +00003899 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3900 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003901
Greg Clayton9482f052012-03-13 23:14:29 +00003902 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00003903 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00003904 // unknown to make sure we use the DynamicLoaderStatic()...
3905 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
3906 {
3907 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
3908 }
3909 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003910 }
Greg Clayton9482f052012-03-13 23:14:29 +00003911 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003912}
3913
3914
3915//------------------------------------------------------------------
3916// PluginInterface protocol
3917//------------------------------------------------------------------
3918const char *
3919ObjectFileMachO::GetPluginName()
3920{
3921 return "ObjectFileMachO";
3922}
3923
3924const char *
3925ObjectFileMachO::GetShortPluginName()
3926{
3927 return GetPluginNameStatic();
3928}
3929
3930uint32_t
3931ObjectFileMachO::GetPluginVersion()
3932{
3933 return 1;
3934}
3935