blob: 36bd9292ba94a87b550c060f2a178ff90736a1f2 [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 Clayton952e9dc2013-03-27 23:08:40 +00001234 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001235
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)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001255 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001256 return 0;
1257 }
1258
1259 if (symtab_load_command.stroff == 0)
1260 {
1261 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001262 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001263 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)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001269 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001270 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)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001276 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001277 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)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001414 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001415 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)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001427 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001428 return 0;
1429 }
1430 }
1431 else
Greg Claytondd29b972012-05-18 23:20:01 +00001432 {
1433 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001434 module_sp->LogMessage(log, "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 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001480 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1481 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1482 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1483 // the module.
1484 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001485 {
1486 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1487 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1488 eh_frame.GetFunctionAddressAndSizeVector (functions);
1489 addr_t text_base_addr = text_section_sp->GetFileAddress();
1490 size_t count = functions.GetSize();
1491 for (size_t i = 0; i < count; ++i)
1492 {
1493 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1494 if (func)
1495 {
1496 FunctionStarts::Entry function_start_entry;
1497 function_start_entry.addr = func->base - text_base_addr;
1498 function_starts.Append(function_start_entry);
1499 }
1500 }
1501 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001502 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001503
Greg Clayton36da2aa2013-01-25 18:06:21 +00001504 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001505
Greg Clayton36da2aa2013-01-25 18:06:21 +00001506 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 +00001507
Greg Clayton36da2aa2013-01-25 18:06:21 +00001508 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001509
1510 uint32_t N_SO_index = UINT32_MAX;
1511
1512 MachSymtabSectionInfo section_info (section_list);
1513 std::vector<uint32_t> N_FUN_indexes;
1514 std::vector<uint32_t> N_NSYM_indexes;
1515 std::vector<uint32_t> N_INCL_indexes;
1516 std::vector<uint32_t> N_BRAC_indexes;
1517 std::vector<uint32_t> N_COMM_indexes;
1518 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1519 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1520 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1521 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1522 // Any symbols that get merged into another will get an entry
1523 // in this map so we know
1524 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1525 uint32_t nlist_idx = 0;
1526 Symbol *symbol_ptr = NULL;
1527
1528 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001529 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001530 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001531 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001532 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001533
Jason Molendab62abd52012-06-21 01:51:02 +00001534#if defined (__APPLE__) && defined (__arm__)
1535
1536 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1537 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1538 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1539 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1540 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1541 // nlist parser to ignore all LOCAL symbols.
1542
1543 if (m_header.flags & 0x80000000u)
1544 {
1545 // Before we can start mapping the DSC, we need to make certain the target process is actually
1546 // using the cache we can find.
1547
1548 /*
1549 * TODO (FIXME!)
1550 *
1551 * Consider the case of testing with a separate DSC file.
1552 * If we go through the normal code paths, we will give symbols for the wrong DSC, and
1553 * that is bad. We need to read the target process' all_image_infos struct, and look
1554 * at the values of the processDetachedFromSharedRegion field. If that is set, we should skip
1555 * this code section.
1556 */
1557
1558 // Next we need to determine the correct path for the dyld shared cache.
1559
1560 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1561 char dsc_path[PATH_MAX];
1562
1563 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001564 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1565 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001566 header_arch.GetArchitectureName());
1567
1568 FileSpec dsc_filespec(dsc_path, false);
1569
1570 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda6bcabae2013-03-06 23:17:36 +00001571 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001572 {
Jason Molenda6bcabae2013-03-06 23:17:36 +00001573 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1574 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1575 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001576 uint32_t imagesOffset;
1577 uint32_t imagesCount;
1578 uint64_t dyldBaseAddress;
1579 uint64_t codeSignatureOffset;
1580 uint64_t codeSignatureSize;
1581 uint64_t slideInfoOffset;
1582 uint64_t slideInfoSize;
Jason Molenda6bcabae2013-03-06 23:17:36 +00001583 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1584 uint64_t localSymbolsSize; // size of local symbols information
1585 };
1586 struct lldb_copy_dyld_cache_header_v1
1587 {
1588 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1589 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1590 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1591 uint32_t imagesOffset;
1592 uint32_t imagesCount;
1593 uint64_t dyldBaseAddress;
1594 uint64_t codeSignatureOffset;
1595 uint64_t codeSignatureSize;
1596 uint64_t slideInfoOffset;
1597 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001598 uint64_t localSymbolsOffset;
1599 uint64_t localSymbolsSize;
Jason Molenda6bcabae2013-03-06 23:17:36 +00001600 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001601 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001602
1603 struct lldb_copy_dyld_cache_mapping_info
1604 {
1605 uint64_t address;
1606 uint64_t size;
1607 uint64_t fileOffset;
1608 uint32_t maxProt;
1609 uint32_t initProt;
1610 };
1611
Greg Claytonab77dcb2012-09-05 22:30:51 +00001612 struct lldb_copy_dyld_cache_local_symbols_info
1613 {
1614 uint32_t nlistOffset;
1615 uint32_t nlistCount;
1616 uint32_t stringsOffset;
1617 uint32_t stringsSize;
1618 uint32_t entriesOffset;
1619 uint32_t entriesCount;
1620 };
1621 struct lldb_copy_dyld_cache_local_symbols_entry
1622 {
1623 uint32_t dylibOffset;
1624 uint32_t nlistStartIndex;
1625 uint32_t nlistCount;
1626 };
Jason Molendab62abd52012-06-21 01:51:02 +00001627
Jason Molendafd3b35d2012-06-22 03:28:35 +00001628 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1629 The dyld_cache_local_symbols_info structure gives us three things:
1630 1. The start and count of the nlist records in the dyld_shared_cache file
1631 2. The start and size of the strings for these nlist records
1632 3. The start and count of dyld_cache_local_symbols_entry entries
1633
1634 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1635 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001636 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001637 and the count of how many nlist records there are for this dylib/framework.
1638 */
1639
Jason Molendab62abd52012-06-21 01:51:02 +00001640 // Process the dsc header to find the unmapped symbols
1641 //
1642 // Save some VM space, do not map the entire cache in one shot.
1643
Jason Molenda6bcabae2013-03-06 23:17:36 +00001644 DataBufferSP dsc_data_sp;
1645 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1646
1647 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001648 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001649 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001650
Jason Molenda6bcabae2013-03-06 23:17:36 +00001651 char version_str[17];
1652 int version = -1;
1653 lldb::offset_t offset = 0;
1654 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1655 version_str[16] = '\0';
1656 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1657 {
1658 int v;
1659 if (::sscanf (version_str + 6, "%d", &v) == 1)
1660 {
1661 version = v;
1662 }
1663 }
1664
Jason Molenda9badb6c2013-03-06 23:19:17 +00001665 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001666
Jason Molendab62abd52012-06-21 01:51:02 +00001667 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1668
1669 // If the mappingOffset points to a location inside the header, we've
1670 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda6bcabae2013-03-06 23:17:36 +00001671 if ((version == 0 && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
1672 || (version >= 1 && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v1)))
Jason Molendab62abd52012-06-21 01:51:02 +00001673 {
1674
Jason Molenda6bcabae2013-03-06 23:17:36 +00001675 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1676 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1677 offset = 0;
1678
1679 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1680 // in the shared library cache need to be adjusted by an offset to match up with the
1681 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1682 // recorded in mapping_offset_value.
1683 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1684
1685 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001686 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1687 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1688
Jason Molenda9badb6c2013-03-06 23:19:17 +00001689 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001690 {
1691 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001692 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001693 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001694 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001695
1696 offset = 0;
1697
1698 // Read the local_symbols_infos struct in one shot
1699 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1700 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1701
Jason Molendab62abd52012-06-21 01:51:02 +00001702 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1703
Jason Molenda6bcabae2013-03-06 23:17:36 +00001704 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001705
1706 offset = local_symbols_info.entriesOffset;
1707 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1708 {
1709 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1710 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1711 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1712 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1713
Jason Molenda9badb6c2013-03-06 23:19:17 +00001714 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001715 {
1716 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1717
1718 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1719 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1720 num_syms = symtab->GetNumSymbols();
1721
1722 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1723 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1724
Jason Molenda9badb6c2013-03-06 23:19:17 +00001725 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001726 {
1727 /////////////////////////////
1728 {
1729 struct nlist_64 nlist;
1730 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1731 break;
1732
1733 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1734 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1735 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1736 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1737 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1738
1739 SymbolType type = eSymbolTypeInvalid;
1740 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1741
1742 if (symbol_name == NULL)
1743 {
1744 // No symbol should be NULL, even the symbols with no
1745 // string values should have an offset zero which points
1746 // to an empty C-string
1747 Host::SystemLog (Host::eSystemLogError,
1748 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1749 entry_index,
1750 nlist.n_strx,
1751 module_sp->GetFileSpec().GetDirectory().GetCString(),
1752 module_sp->GetFileSpec().GetFilename().GetCString());
1753 continue;
1754 }
1755 if (symbol_name[0] == '\0')
1756 symbol_name = NULL;
1757
1758 const char *symbol_name_non_abi_mangled = NULL;
1759
1760 SectionSP symbol_section;
1761 uint32_t symbol_byte_size = 0;
1762 bool add_nlist = true;
1763 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001764 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001765
1766 assert (sym_idx < num_syms);
1767
1768 sym[sym_idx].SetDebug (is_debug);
1769
1770 if (is_debug)
1771 {
1772 switch (nlist.n_type)
1773 {
1774 case StabGlobalSymbol:
1775 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1776 // Sometimes the N_GSYM value contains the address.
1777
1778 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1779 // have the same address, but we want to ensure that we always find only the real symbol,
1780 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1781 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1782 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1783 // same address.
1784
1785 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1786 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1787 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1788 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1789 add_nlist = false;
1790 else
1791 {
1792 sym[sym_idx].SetExternal(true);
1793 if (nlist.n_value != 0)
1794 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1795 type = eSymbolTypeData;
1796 }
1797 break;
1798
1799 case StabFunctionName:
1800 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1801 type = eSymbolTypeCompiler;
1802 break;
1803
1804 case StabFunction:
1805 // N_FUN -- procedure: name,,n_sect,linenumber,address
1806 if (symbol_name)
1807 {
1808 type = eSymbolTypeCode;
1809 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1810
1811 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1812 // We use the current number of symbols in the symbol table in lieu of
1813 // using nlist_idx in case we ever start trimming entries out
1814 N_FUN_indexes.push_back(sym_idx);
1815 }
1816 else
1817 {
1818 type = eSymbolTypeCompiler;
1819
1820 if ( !N_FUN_indexes.empty() )
1821 {
1822 // Copy the size of the function into the original STAB entry so we don't have
1823 // to hunt for it later
1824 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1825 N_FUN_indexes.pop_back();
1826 // We don't really need the end function STAB as it contains the size which
1827 // we already placed with the original symbol, so don't add it if we want a
1828 // minimal symbol table
1829 if (minimize)
1830 add_nlist = false;
1831 }
1832 }
1833 break;
1834
1835 case StabStaticSymbol:
1836 // N_STSYM -- static symbol: name,,n_sect,type,address
1837 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1838 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1839 type = eSymbolTypeData;
1840 break;
1841
1842 case StabLocalCommon:
1843 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1844 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1845 type = eSymbolTypeCommonBlock;
1846 break;
1847
1848 case StabBeginSymbol:
1849 // N_BNSYM
1850 // We use the current number of symbols in the symbol table in lieu of
1851 // using nlist_idx in case we ever start trimming entries out
1852 if (minimize)
1853 {
1854 // Skip these if we want minimal symbol tables
1855 add_nlist = false;
1856 }
1857 else
1858 {
1859 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1860 N_NSYM_indexes.push_back(sym_idx);
1861 type = eSymbolTypeScopeBegin;
1862 }
1863 break;
1864
1865 case StabEndSymbol:
1866 // N_ENSYM
1867 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1868 // so that we can always skip the entire symbol if we need to navigate
1869 // more quickly at the source level when parsing STABS
1870 if (minimize)
1871 {
1872 // Skip these if we want minimal symbol tables
1873 add_nlist = false;
1874 }
1875 else
1876 {
1877 if ( !N_NSYM_indexes.empty() )
1878 {
1879 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1880 symbol_ptr->SetByteSize(sym_idx + 1);
1881 symbol_ptr->SetSizeIsSibling(true);
1882 N_NSYM_indexes.pop_back();
1883 }
1884 type = eSymbolTypeScopeEnd;
1885 }
1886 break;
1887
1888
1889 case StabSourceFileOptions:
1890 // N_OPT - emitted with gcc2_compiled and in gcc source
1891 type = eSymbolTypeCompiler;
1892 break;
1893
1894 case StabRegisterSymbol:
1895 // N_RSYM - register sym: name,,NO_SECT,type,register
1896 type = eSymbolTypeVariable;
1897 break;
1898
1899 case StabSourceLine:
1900 // N_SLINE - src line: 0,,n_sect,linenumber,address
1901 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1902 type = eSymbolTypeLineEntry;
1903 break;
1904
1905 case StabStructureType:
1906 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1907 type = eSymbolTypeVariableType;
1908 break;
1909
1910 case StabSourceFileName:
1911 // N_SO - source file name
1912 type = eSymbolTypeSourceFile;
1913 if (symbol_name == NULL)
1914 {
1915 if (minimize)
1916 add_nlist = false;
1917 if (N_SO_index != UINT32_MAX)
1918 {
1919 // Set the size of the N_SO to the terminating index of this N_SO
1920 // so that we can always skip the entire N_SO if we need to navigate
1921 // more quickly at the source level when parsing STABS
1922 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1923 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1924 symbol_ptr->SetSizeIsSibling(true);
1925 }
1926 N_NSYM_indexes.clear();
1927 N_INCL_indexes.clear();
1928 N_BRAC_indexes.clear();
1929 N_COMM_indexes.clear();
1930 N_FUN_indexes.clear();
1931 N_SO_index = UINT32_MAX;
1932 }
1933 else
1934 {
1935 // We use the current number of symbols in the symbol table in lieu of
1936 // using nlist_idx in case we ever start trimming entries out
1937 const bool N_SO_has_full_path = symbol_name[0] == '/';
1938 if (N_SO_has_full_path)
1939 {
1940 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1941 {
1942 // We have two consecutive N_SO entries where the first contains a directory
1943 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00001944 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001945 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1946 add_nlist = false;
1947 }
1948 else
1949 {
1950 // This is the first entry in a N_SO that contains a directory or
1951 // a full path to the source file
1952 N_SO_index = sym_idx;
1953 }
1954 }
1955 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1956 {
1957 // This is usually the second N_SO entry that contains just the filename,
1958 // so here we combine it with the first one if we are minimizing the symbol table
1959 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1960 if (so_path && so_path[0])
1961 {
1962 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00001963 const size_t double_slash_pos = full_so_path.find("//");
1964 if (double_slash_pos != std::string::npos)
1965 {
1966 // The linker has been generating bad N_SO entries with doubled up paths
1967 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
1968 // and the second is the directory for the source file so you end up with
1969 // a path that looks like "/tmp/src//tmp/src/"
1970 FileSpec so_dir(so_path, false);
1971 if (!so_dir.Exists())
1972 {
1973 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
1974 if (so_dir.Exists())
1975 {
1976 // Trim off the incorrect path
1977 full_so_path.erase(0, double_slash_pos + 1);
1978 }
1979 }
1980 }
Jason Molendab62abd52012-06-21 01:51:02 +00001981 if (*full_so_path.rbegin() != '/')
1982 full_so_path += '/';
1983 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00001984 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001985 add_nlist = false;
1986 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1987 }
1988 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00001989 else
1990 {
1991 // This could be a relative path to a N_SO
1992 N_SO_index = sym_idx;
1993 }
Jason Molendab62abd52012-06-21 01:51:02 +00001994 }
Jason Molendab62abd52012-06-21 01:51:02 +00001995 break;
1996
1997 case StabObjectFileName:
1998 // N_OSO - object file name: name,,0,0,st_mtime
1999 type = eSymbolTypeObjectFile;
2000 break;
2001
2002 case StabLocalSymbol:
2003 // N_LSYM - local sym: name,,NO_SECT,type,offset
2004 type = eSymbolTypeLocal;
2005 break;
2006
2007 //----------------------------------------------------------------------
2008 // INCL scopes
2009 //----------------------------------------------------------------------
2010 case StabBeginIncludeFileName:
2011 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2012 // We use the current number of symbols in the symbol table in lieu of
2013 // using nlist_idx in case we ever start trimming entries out
2014 N_INCL_indexes.push_back(sym_idx);
2015 type = eSymbolTypeScopeBegin;
2016 break;
2017
2018 case StabEndIncludeFile:
2019 // N_EINCL - include file end: name,,NO_SECT,0,0
2020 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2021 // so that we can always skip the entire symbol if we need to navigate
2022 // more quickly at the source level when parsing STABS
2023 if ( !N_INCL_indexes.empty() )
2024 {
2025 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2026 symbol_ptr->SetByteSize(sym_idx + 1);
2027 symbol_ptr->SetSizeIsSibling(true);
2028 N_INCL_indexes.pop_back();
2029 }
2030 type = eSymbolTypeScopeEnd;
2031 break;
2032
2033 case StabIncludeFileName:
2034 // N_SOL - #included file name: name,,n_sect,0,address
2035 type = eSymbolTypeHeaderFile;
2036
2037 // We currently don't use the header files on darwin
2038 if (minimize)
2039 add_nlist = false;
2040 break;
2041
2042 case StabCompilerParameters:
2043 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2044 type = eSymbolTypeCompiler;
2045 break;
2046
2047 case StabCompilerVersion:
2048 // N_VERSION - compiler version: name,,NO_SECT,0,0
2049 type = eSymbolTypeCompiler;
2050 break;
2051
2052 case StabCompilerOptLevel:
2053 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2054 type = eSymbolTypeCompiler;
2055 break;
2056
2057 case StabParameter:
2058 // N_PSYM - parameter: name,,NO_SECT,type,offset
2059 type = eSymbolTypeVariable;
2060 break;
2061
2062 case StabAlternateEntry:
2063 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2064 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2065 type = eSymbolTypeLineEntry;
2066 break;
2067
2068 //----------------------------------------------------------------------
2069 // Left and Right Braces
2070 //----------------------------------------------------------------------
2071 case StabLeftBracket:
2072 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2073 // We use the current number of symbols in the symbol table in lieu of
2074 // using nlist_idx in case we ever start trimming entries out
2075 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2076 N_BRAC_indexes.push_back(sym_idx);
2077 type = eSymbolTypeScopeBegin;
2078 break;
2079
2080 case StabRightBracket:
2081 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2082 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2083 // so that we can always skip the entire symbol if we need to navigate
2084 // more quickly at the source level when parsing STABS
2085 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2086 if ( !N_BRAC_indexes.empty() )
2087 {
2088 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2089 symbol_ptr->SetByteSize(sym_idx + 1);
2090 symbol_ptr->SetSizeIsSibling(true);
2091 N_BRAC_indexes.pop_back();
2092 }
2093 type = eSymbolTypeScopeEnd;
2094 break;
2095
2096 case StabDeletedIncludeFile:
2097 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2098 type = eSymbolTypeHeaderFile;
2099 break;
2100
2101 //----------------------------------------------------------------------
2102 // COMM scopes
2103 //----------------------------------------------------------------------
2104 case StabBeginCommon:
2105 // N_BCOMM - begin common: name,,NO_SECT,0,0
2106 // We use the current number of symbols in the symbol table in lieu of
2107 // using nlist_idx in case we ever start trimming entries out
2108 type = eSymbolTypeScopeBegin;
2109 N_COMM_indexes.push_back(sym_idx);
2110 break;
2111
2112 case StabEndCommonLocal:
2113 // N_ECOML - end common (local name): 0,,n_sect,0,address
2114 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2115 // Fall through
2116
2117 case StabEndCommon:
2118 // N_ECOMM - end common: name,,n_sect,0,0
2119 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2120 // so that we can always skip the entire symbol if we need to navigate
2121 // more quickly at the source level when parsing STABS
2122 if ( !N_COMM_indexes.empty() )
2123 {
2124 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2125 symbol_ptr->SetByteSize(sym_idx + 1);
2126 symbol_ptr->SetSizeIsSibling(true);
2127 N_COMM_indexes.pop_back();
2128 }
2129 type = eSymbolTypeScopeEnd;
2130 break;
2131
2132 case StabLength:
2133 // N_LENG - second stab entry with length information
2134 type = eSymbolTypeAdditional;
2135 break;
2136
2137 default: break;
2138 }
2139 }
2140 else
2141 {
2142 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2143 uint8_t n_type = NlistMaskType & nlist.n_type;
2144 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2145
2146 switch (n_type)
2147 {
2148 case NListTypeIndirect: // N_INDR - Fall through
2149 case NListTypePreboundUndefined:// N_PBUD - Fall through
2150 case NListTypeUndefined: // N_UNDF
2151 type = eSymbolTypeUndefined;
2152 break;
2153
2154 case NListTypeAbsolute: // N_ABS
2155 type = eSymbolTypeAbsolute;
2156 break;
2157
2158 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002159 {
2160 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002161
Greg Clayton01e6a582012-11-27 01:52:16 +00002162 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002163 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002164 // TODO: warn about this?
2165 add_nlist = false;
2166 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002167 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002168
2169 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002170 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002171 type = eSymbolTypeException;
2172 }
2173 else
2174 {
2175 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002176
Greg Clayton01e6a582012-11-27 01:52:16 +00002177 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002178 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002179 case SectionTypeRegular: break; // regular section
2180 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2181 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2182 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2183 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2184 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2185 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2186 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2187 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2188 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2189 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2190 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2191 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2192 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2193 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2194 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2195 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2196 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002197 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002198
Greg Clayton01e6a582012-11-27 01:52:16 +00002199 if (type == eSymbolTypeInvalid)
2200 {
2201 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2202 if (symbol_section->IsDescendant (text_section_sp.get()))
2203 {
2204 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2205 SectionAttrUserSelfModifyingCode |
2206 SectionAttrSytemSomeInstructions))
2207 type = eSymbolTypeData;
2208 else
2209 type = eSymbolTypeCode;
2210 }
2211 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002212 {
2213 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2214 {
2215 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002216
Greg Clayton01e6a582012-11-27 01:52:16 +00002217 if (symbol_name &&
2218 symbol_name[0] == '_' &&
2219 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002220 symbol_name[2] == 'B')
2221 {
2222 llvm::StringRef symbol_name_ref(symbol_name);
2223 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2224 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2225 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2226 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2227 {
2228 symbol_name_non_abi_mangled = symbol_name + 1;
2229 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2230 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002231 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002232 }
2233 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2234 {
2235 symbol_name_non_abi_mangled = symbol_name + 1;
2236 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2237 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002238 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002239 }
2240 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2241 {
2242 symbol_name_non_abi_mangled = symbol_name + 1;
2243 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2244 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002245 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002246 }
2247 }
2248 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002249 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002250 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002251 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002252 }
2253 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002254 {
2255 type = eSymbolTypeData;
2256 }
2257 }
2258 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2259 {
2260 type = eSymbolTypeTrampoline;
2261 }
2262 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2263 {
2264 type = eSymbolTypeRuntime;
2265 if (symbol_name && symbol_name[0] == '.')
2266 {
2267 llvm::StringRef symbol_name_ref(symbol_name);
2268 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2269 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002270 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002271 symbol_name_non_abi_mangled = symbol_name;
2272 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2273 type = eSymbolTypeObjCClass;
2274 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002275 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002276 }
2277 }
2278 }
Jason Molendab62abd52012-06-21 01:51:02 +00002279 }
2280 }
Jason Molendab62abd52012-06-21 01:51:02 +00002281 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002282 }
Jason Molendab62abd52012-06-21 01:51:02 +00002283 }
2284
2285 if (add_nlist)
2286 {
2287 uint64_t symbol_value = nlist.n_value;
2288 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002289
Jason Molendab62abd52012-06-21 01:51:02 +00002290 if (symbol_name_non_abi_mangled)
2291 {
Jason Molenda292cca82012-07-20 03:35:44 +00002292 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2293 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002294 }
2295 else
2296 {
2297 if (symbol_name && symbol_name[0] == '_')
2298 {
2299 symbol_name_is_mangled = symbol_name[1] == '_';
2300 symbol_name++; // Skip the leading underscore
2301 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002302
Jason Molendab62abd52012-06-21 01:51:02 +00002303 if (symbol_name)
2304 {
Jason Molenda292cca82012-07-20 03:35:44 +00002305 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002306 }
2307 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002308
Jason Molendab62abd52012-06-21 01:51:02 +00002309 if (is_debug == false)
2310 {
2311 if (type == eSymbolTypeCode)
2312 {
2313 // See if we can find a N_FUN entry for any code symbols.
2314 // If we do find a match, and the name matches, then we
2315 // can merge the two into just the function symbol to avoid
2316 // duplicate entries in the symbol table
2317 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2318 if (pos != N_FUN_addr_to_sym_idx.end())
2319 {
2320 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2321 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2322 {
2323 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2324 // We just need the flags from the linker symbol, so put these flags
2325 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2326 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2327 sym[sym_idx].Clear();
2328 continue;
2329 }
2330 }
2331 }
2332 else if (type == eSymbolTypeData)
2333 {
2334 // See if we can find a N_STSYM entry for any data symbols.
2335 // If we do find a match, and the name matches, then we
2336 // can merge the two into just the Static symbol to avoid
2337 // duplicate entries in the symbol table
2338 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2339 if (pos != N_STSYM_addr_to_sym_idx.end())
2340 {
2341 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2342 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2343 {
2344 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2345 // We just need the flags from the linker symbol, so put these flags
2346 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2347 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2348 sym[sym_idx].Clear();
2349 continue;
2350 }
2351 }
2352 }
2353 }
2354 if (symbol_section)
2355 {
2356 const addr_t section_file_addr = symbol_section->GetFileAddress();
2357 if (symbol_byte_size == 0 && function_starts_count > 0)
2358 {
2359 addr_t symbol_lookup_file_addr = nlist.n_value;
2360 // Do an exact address match for non-ARM addresses, else get the closest since
2361 // the symbol might be a thumb symbol which has an address with bit zero set
2362 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2363 if (is_arm && func_start_entry)
2364 {
2365 // Verify that the function start address is the symbol address (ARM)
2366 // or the symbol address + 1 (thumb)
2367 if (func_start_entry->addr != symbol_lookup_file_addr &&
2368 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2369 {
2370 // Not the right entry, NULL it out...
2371 func_start_entry = NULL;
2372 }
2373 }
2374 if (func_start_entry)
2375 {
2376 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002377
Jason Molendab62abd52012-06-21 01:51:02 +00002378 addr_t symbol_file_addr = func_start_entry->addr;
2379 uint32_t symbol_flags = 0;
2380 if (is_arm)
2381 {
2382 if (symbol_file_addr & 1)
2383 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2384 symbol_file_addr &= 0xfffffffffffffffeull;
2385 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002386
Jason Molendab62abd52012-06-21 01:51:02 +00002387 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2388 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2389 if (next_func_start_entry)
2390 {
2391 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2392 // Be sure the clear the Thumb address bit when we calculate the size
2393 // from the current and next address
2394 if (is_arm)
2395 next_symbol_file_addr &= 0xfffffffffffffffeull;
2396 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2397 }
2398 else
2399 {
2400 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2401 }
2402 }
2403 }
2404 symbol_value -= section_file_addr;
2405 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002406
Jason Molendab62abd52012-06-21 01:51:02 +00002407 sym[sym_idx].SetID (nlist_idx);
2408 sym[sym_idx].SetType (type);
2409 sym[sym_idx].GetAddress().SetSection (symbol_section);
2410 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2411 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002412
Jason Molendab62abd52012-06-21 01:51:02 +00002413 if (symbol_byte_size > 0)
2414 sym[sym_idx].SetByteSize(symbol_byte_size);
2415
Greg Clayton01e6a582012-11-27 01:52:16 +00002416 if (demangled_is_synthesized)
2417 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002418 ++sym_idx;
2419 }
2420 else
2421 {
2422 sym[sym_idx].Clear();
2423 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002424
Jason Molendab62abd52012-06-21 01:51:02 +00002425 }
2426 /////////////////////////////
2427 }
2428 break; // No more entries to consider
2429 }
2430 }
2431 }
2432 }
2433 }
2434 }
2435 }
2436
2437 // Must reset this in case it was mutated above!
2438 nlist_data_offset = 0;
2439#endif
2440
2441 // If the sym array was not created while parsing the DSC unmapped
2442 // symbols, create it now.
2443 if (sym == NULL)
2444 {
2445 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2446 num_syms = symtab->GetNumSymbols();
2447 }
2448
2449 if (unmapped_local_symbols_found)
2450 {
2451 assert(m_dysymtab.ilocalsym == 0);
2452 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2453 nlist_idx = m_dysymtab.nlocalsym;
2454 }
2455 else
2456 {
2457 nlist_idx = 0;
2458 }
2459
2460 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002461 {
2462 struct nlist_64 nlist;
2463 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2464 break;
2465
2466 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2467 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2468 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2469 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2470 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2471
2472 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002473 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002474
Greg Clayton3a5dc012012-05-25 17:04:00 +00002475 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002476 {
2477 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002478
Greg Claytondd29b972012-05-18 23:20:01 +00002479 if (symbol_name == NULL)
2480 {
2481 // No symbol should be NULL, even the symbols with no
2482 // string values should have an offset zero which points
2483 // to an empty C-string
2484 Host::SystemLog (Host::eSystemLogError,
Jason Molenda9badb6c2013-03-06 23:19:17 +00002485 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002486 nlist_idx,
2487 nlist.n_strx,
2488 module_sp->GetFileSpec().GetDirectory().GetCString(),
2489 module_sp->GetFileSpec().GetFilename().GetCString());
2490 continue;
2491 }
2492 if (symbol_name[0] == '\0')
2493 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002494 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002495 else
2496 {
2497 const addr_t str_addr = strtab_addr + nlist.n_strx;
2498 Error str_error;
2499 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2500 symbol_name = memory_symbol_name.c_str();
2501 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002502 const char *symbol_name_non_abi_mangled = NULL;
2503
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002504 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002505 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002506 bool add_nlist = true;
2507 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002508 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002509
2510 assert (sym_idx < num_syms);
2511
2512 sym[sym_idx].SetDebug (is_debug);
2513
2514 if (is_debug)
2515 {
2516 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002517 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002518 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002519 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2520 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002521
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002522 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2523 // have the same address, but we want to ensure that we always find only the real symbol,
2524 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2525 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2526 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2527 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002528
2529 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002530 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2531 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2532 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2533 add_nlist = false;
2534 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002535 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002536 sym[sym_idx].SetExternal(true);
2537 if (nlist.n_value != 0)
2538 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2539 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002540 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002541 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002542
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002543 case StabFunctionName:
2544 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2545 type = eSymbolTypeCompiler;
2546 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002547
Jason Molenda9badb6c2013-03-06 23:19:17 +00002548 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002549 // N_FUN -- procedure: name,,n_sect,linenumber,address
2550 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002551 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002552 type = eSymbolTypeCode;
2553 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002554
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002555 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2556 // We use the current number of symbols in the symbol table in lieu of
2557 // using nlist_idx in case we ever start trimming entries out
2558 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002559 }
2560 else
2561 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002562 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002563
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002564 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002565 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002566 // Copy the size of the function into the original STAB entry so we don't have
2567 // to hunt for it later
2568 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2569 N_FUN_indexes.pop_back();
2570 // We don't really need the end function STAB as it contains the size which
2571 // we already placed with the original symbol, so don't add it if we want a
2572 // minimal symbol table
2573 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002574 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002575 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002576 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002577 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002578
Jason Molenda9badb6c2013-03-06 23:19:17 +00002579 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002580 // N_STSYM -- static symbol: name,,n_sect,type,address
2581 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2582 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2583 type = eSymbolTypeData;
2584 break;
2585
2586 case StabLocalCommon:
2587 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2588 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2589 type = eSymbolTypeCommonBlock;
2590 break;
2591
2592 case StabBeginSymbol:
2593 // N_BNSYM
2594 // We use the current number of symbols in the symbol table in lieu of
2595 // using nlist_idx in case we ever start trimming entries out
2596 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002597 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002598 // Skip these if we want minimal symbol tables
2599 add_nlist = false;
2600 }
2601 else
2602 {
2603 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2604 N_NSYM_indexes.push_back(sym_idx);
2605 type = eSymbolTypeScopeBegin;
2606 }
2607 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002608
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002609 case StabEndSymbol:
2610 // N_ENSYM
2611 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2612 // so that we can always skip the entire symbol if we need to navigate
2613 // more quickly at the source level when parsing STABS
2614 if (minimize)
2615 {
2616 // Skip these if we want minimal symbol tables
2617 add_nlist = false;
2618 }
2619 else
2620 {
2621 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002622 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002623 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2624 symbol_ptr->SetByteSize(sym_idx + 1);
2625 symbol_ptr->SetSizeIsSibling(true);
2626 N_NSYM_indexes.pop_back();
2627 }
2628 type = eSymbolTypeScopeEnd;
2629 }
2630 break;
2631
2632
2633 case StabSourceFileOptions:
2634 // N_OPT - emitted with gcc2_compiled and in gcc source
2635 type = eSymbolTypeCompiler;
2636 break;
2637
2638 case StabRegisterSymbol:
2639 // N_RSYM - register sym: name,,NO_SECT,type,register
2640 type = eSymbolTypeVariable;
2641 break;
2642
2643 case StabSourceLine:
2644 // N_SLINE - src line: 0,,n_sect,linenumber,address
2645 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2646 type = eSymbolTypeLineEntry;
2647 break;
2648
2649 case StabStructureType:
2650 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2651 type = eSymbolTypeVariableType;
2652 break;
2653
2654 case StabSourceFileName:
2655 // N_SO - source file name
2656 type = eSymbolTypeSourceFile;
2657 if (symbol_name == NULL)
2658 {
2659 if (minimize)
2660 add_nlist = false;
2661 if (N_SO_index != UINT32_MAX)
2662 {
2663 // Set the size of the N_SO to the terminating index of this N_SO
2664 // so that we can always skip the entire N_SO if we need to navigate
2665 // more quickly at the source level when parsing STABS
2666 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2667 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2668 symbol_ptr->SetSizeIsSibling(true);
2669 }
2670 N_NSYM_indexes.clear();
2671 N_INCL_indexes.clear();
2672 N_BRAC_indexes.clear();
2673 N_COMM_indexes.clear();
2674 N_FUN_indexes.clear();
2675 N_SO_index = UINT32_MAX;
2676 }
2677 else
2678 {
2679 // We use the current number of symbols in the symbol table in lieu of
2680 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002681 const bool N_SO_has_full_path = symbol_name[0] == '/';
2682 if (N_SO_has_full_path)
2683 {
2684 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2685 {
2686 // We have two consecutive N_SO entries where the first contains a directory
2687 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002688 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002689 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2690 add_nlist = false;
2691 }
2692 else
2693 {
2694 // This is the first entry in a N_SO that contains a directory or
2695 // a full path to the source file
2696 N_SO_index = sym_idx;
2697 }
2698 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002699 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2700 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002701 // This is usually the second N_SO entry that contains just the filename,
2702 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002703 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2704 if (so_path && so_path[0])
2705 {
2706 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002707 const size_t double_slash_pos = full_so_path.find("//");
2708 if (double_slash_pos != std::string::npos)
2709 {
2710 // The linker has been generating bad N_SO entries with doubled up paths
2711 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2712 // and the second is the directory for the source file so you end up with
2713 // a path that looks like "/tmp/src//tmp/src/"
2714 FileSpec so_dir(so_path, false);
2715 if (!so_dir.Exists())
2716 {
2717 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2718 if (so_dir.Exists())
2719 {
2720 // Trim off the incorrect path
2721 full_so_path.erase(0, double_slash_pos + 1);
2722 }
2723 }
2724 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002725 if (*full_so_path.rbegin() != '/')
2726 full_so_path += '/';
2727 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002728 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002729 add_nlist = false;
2730 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2731 }
2732 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002733 else
2734 {
2735 // This could be a relative path to a N_SO
2736 N_SO_index = sym_idx;
2737 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002738 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002739
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002740 break;
2741
2742 case StabObjectFileName:
2743 // N_OSO - object file name: name,,0,0,st_mtime
2744 type = eSymbolTypeObjectFile;
2745 break;
2746
2747 case StabLocalSymbol:
2748 // N_LSYM - local sym: name,,NO_SECT,type,offset
2749 type = eSymbolTypeLocal;
2750 break;
2751
2752 //----------------------------------------------------------------------
2753 // INCL scopes
2754 //----------------------------------------------------------------------
2755 case StabBeginIncludeFileName:
2756 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2757 // We use the current number of symbols in the symbol table in lieu of
2758 // using nlist_idx in case we ever start trimming entries out
2759 N_INCL_indexes.push_back(sym_idx);
2760 type = eSymbolTypeScopeBegin;
2761 break;
2762
2763 case StabEndIncludeFile:
2764 // N_EINCL - include file end: name,,NO_SECT,0,0
2765 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2766 // so that we can always skip the entire symbol if we need to navigate
2767 // more quickly at the source level when parsing STABS
2768 if ( !N_INCL_indexes.empty() )
2769 {
2770 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2771 symbol_ptr->SetByteSize(sym_idx + 1);
2772 symbol_ptr->SetSizeIsSibling(true);
2773 N_INCL_indexes.pop_back();
2774 }
2775 type = eSymbolTypeScopeEnd;
2776 break;
2777
2778 case StabIncludeFileName:
2779 // N_SOL - #included file name: name,,n_sect,0,address
2780 type = eSymbolTypeHeaderFile;
2781
2782 // We currently don't use the header files on darwin
2783 if (minimize)
2784 add_nlist = false;
2785 break;
2786
Jason Molenda9badb6c2013-03-06 23:19:17 +00002787 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002788 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2789 type = eSymbolTypeCompiler;
2790 break;
2791
2792 case StabCompilerVersion:
2793 // N_VERSION - compiler version: name,,NO_SECT,0,0
2794 type = eSymbolTypeCompiler;
2795 break;
2796
2797 case StabCompilerOptLevel:
2798 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2799 type = eSymbolTypeCompiler;
2800 break;
2801
2802 case StabParameter:
2803 // N_PSYM - parameter: name,,NO_SECT,type,offset
2804 type = eSymbolTypeVariable;
2805 break;
2806
2807 case StabAlternateEntry:
2808 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2809 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2810 type = eSymbolTypeLineEntry;
2811 break;
2812
2813 //----------------------------------------------------------------------
2814 // Left and Right Braces
2815 //----------------------------------------------------------------------
2816 case StabLeftBracket:
2817 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2818 // We use the current number of symbols in the symbol table in lieu of
2819 // using nlist_idx in case we ever start trimming entries out
2820 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2821 N_BRAC_indexes.push_back(sym_idx);
2822 type = eSymbolTypeScopeBegin;
2823 break;
2824
2825 case StabRightBracket:
2826 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2827 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2828 // so that we can always skip the entire symbol if we need to navigate
2829 // more quickly at the source level when parsing STABS
2830 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2831 if ( !N_BRAC_indexes.empty() )
2832 {
2833 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2834 symbol_ptr->SetByteSize(sym_idx + 1);
2835 symbol_ptr->SetSizeIsSibling(true);
2836 N_BRAC_indexes.pop_back();
2837 }
2838 type = eSymbolTypeScopeEnd;
2839 break;
2840
2841 case StabDeletedIncludeFile:
2842 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2843 type = eSymbolTypeHeaderFile;
2844 break;
2845
2846 //----------------------------------------------------------------------
2847 // COMM scopes
2848 //----------------------------------------------------------------------
2849 case StabBeginCommon:
2850 // N_BCOMM - begin common: name,,NO_SECT,0,0
2851 // We use the current number of symbols in the symbol table in lieu of
2852 // using nlist_idx in case we ever start trimming entries out
2853 type = eSymbolTypeScopeBegin;
2854 N_COMM_indexes.push_back(sym_idx);
2855 break;
2856
2857 case StabEndCommonLocal:
2858 // N_ECOML - end common (local name): 0,,n_sect,0,address
2859 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2860 // Fall through
2861
2862 case StabEndCommon:
2863 // N_ECOMM - end common: name,,n_sect,0,0
2864 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2865 // so that we can always skip the entire symbol if we need to navigate
2866 // more quickly at the source level when parsing STABS
2867 if ( !N_COMM_indexes.empty() )
2868 {
2869 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2870 symbol_ptr->SetByteSize(sym_idx + 1);
2871 symbol_ptr->SetSizeIsSibling(true);
2872 N_COMM_indexes.pop_back();
2873 }
2874 type = eSymbolTypeScopeEnd;
2875 break;
2876
2877 case StabLength:
2878 // N_LENG - second stab entry with length information
2879 type = eSymbolTypeAdditional;
2880 break;
2881
2882 default: break;
2883 }
2884 }
2885 else
2886 {
2887 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2888 uint8_t n_type = NlistMaskType & nlist.n_type;
2889 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2890
2891 switch (n_type)
2892 {
2893 case NListTypeIndirect: // N_INDR - Fall through
2894 case NListTypePreboundUndefined:// N_PBUD - Fall through
2895 case NListTypeUndefined: // N_UNDF
2896 type = eSymbolTypeUndefined;
2897 break;
2898
2899 case NListTypeAbsolute: // N_ABS
2900 type = eSymbolTypeAbsolute;
2901 break;
2902
2903 case NListTypeSection: // N_SECT
2904 {
2905 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2906
Sean Callananb386d822012-08-09 00:50:26 +00002907 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002908 {
2909 // TODO: warn about this?
2910 add_nlist = false;
2911 break;
2912 }
2913
2914 if (TEXT_eh_frame_sectID == nlist.n_sect)
2915 {
2916 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00002917 }
2918 else
2919 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002920 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2921
2922 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00002923 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002924 case SectionTypeRegular: break; // regular section
2925 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2926 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2927 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2928 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2929 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2930 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2931 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2932 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2933 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2934 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2935 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2936 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2937 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2938 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2939 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2940 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2941 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002942 }
Chris Lattner24943d22010-06-08 16:52:24 +00002943
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002944 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002945 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002946 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2947 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002948 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002949 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2950 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002951 SectionAttrSytemSomeInstructions))
2952 type = eSymbolTypeData;
2953 else
2954 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00002955 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002956 else
2957 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002958 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002959 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00002960 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002961 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00002962
Jason Molenda9badb6c2013-03-06 23:19:17 +00002963 if (symbol_name &&
2964 symbol_name[0] == '_' &&
2965 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002966 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00002967 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002968 llvm::StringRef symbol_name_ref(symbol_name);
2969 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2970 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2971 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2972 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00002973 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002974 symbol_name_non_abi_mangled = symbol_name + 1;
2975 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2976 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002977 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00002978 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002979 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00002980 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002981 symbol_name_non_abi_mangled = symbol_name + 1;
2982 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2983 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002984 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002985 }
2986 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2987 {
2988 symbol_name_non_abi_mangled = symbol_name + 1;
2989 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2990 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002991 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00002992 }
2993 }
2994 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002995 else
2996 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
2997 {
2998 type = eSymbolTypeException;
2999 }
3000 else
3001 {
3002 type = eSymbolTypeData;
3003 }
3004 }
3005 else
3006 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3007 {
3008 type = eSymbolTypeTrampoline;
3009 }
3010 else
3011 if (symbol_section->IsDescendant(objc_section_sp.get()))
3012 {
3013 type = eSymbolTypeRuntime;
3014 if (symbol_name && symbol_name[0] == '.')
3015 {
3016 llvm::StringRef symbol_name_ref(symbol_name);
3017 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3018 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3019 {
3020 symbol_name_non_abi_mangled = symbol_name;
3021 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3022 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003023 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003024 }
3025 }
Chris Lattner24943d22010-06-08 16:52:24 +00003026 }
3027 }
3028 }
3029 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003030 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003031 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003032 }
3033
3034 if (add_nlist)
3035 {
3036 uint64_t symbol_value = nlist.n_value;
3037 bool symbol_name_is_mangled = false;
3038
3039 if (symbol_name_non_abi_mangled)
3040 {
Greg Claytonc0240042012-07-18 23:18:10 +00003041 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3042 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003043 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003044 else
3045 {
3046 if (symbol_name && symbol_name[0] == '_')
3047 {
3048 symbol_name_is_mangled = symbol_name[1] == '_';
3049 symbol_name++; // Skip the leading underscore
3050 }
3051
3052 if (symbol_name)
3053 {
Greg Claytonc0240042012-07-18 23:18:10 +00003054 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003055 }
3056 }
3057
3058 if (is_debug == false)
3059 {
3060 if (type == eSymbolTypeCode)
3061 {
3062 // See if we can find a N_FUN entry for any code symbols.
3063 // If we do find a match, and the name matches, then we
3064 // can merge the two into just the function symbol to avoid
3065 // duplicate entries in the symbol table
3066 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3067 if (pos != N_FUN_addr_to_sym_idx.end())
3068 {
3069 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3070 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3071 {
3072 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3073 // We just need the flags from the linker symbol, so put these flags
3074 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3075 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3076 sym[sym_idx].Clear();
3077 continue;
3078 }
3079 }
3080 }
3081 else if (type == eSymbolTypeData)
3082 {
3083 // See if we can find a N_STSYM entry for any data symbols.
3084 // If we do find a match, and the name matches, then we
3085 // can merge the two into just the Static symbol to avoid
3086 // duplicate entries in the symbol table
3087 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3088 if (pos != N_STSYM_addr_to_sym_idx.end())
3089 {
3090 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3091 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3092 {
3093 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3094 // We just need the flags from the linker symbol, so put these flags
3095 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3096 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3097 sym[sym_idx].Clear();
3098 continue;
3099 }
3100 }
3101 }
3102 }
3103 if (symbol_section)
3104 {
3105 const addr_t section_file_addr = symbol_section->GetFileAddress();
3106 if (symbol_byte_size == 0 && function_starts_count > 0)
3107 {
Greg Claytond2653c22012-03-14 01:53:24 +00003108 addr_t symbol_lookup_file_addr = nlist.n_value;
3109 // Do an exact address match for non-ARM addresses, else get the closest since
3110 // the symbol might be a thumb symbol which has an address with bit zero set
3111 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3112 if (is_arm && func_start_entry)
3113 {
3114 // Verify that the function start address is the symbol address (ARM)
3115 // or the symbol address + 1 (thumb)
3116 if (func_start_entry->addr != symbol_lookup_file_addr &&
3117 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3118 {
3119 // Not the right entry, NULL it out...
3120 func_start_entry = NULL;
3121 }
3122 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003123 if (func_start_entry)
3124 {
3125 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003126
Greg Claytond2653c22012-03-14 01:53:24 +00003127 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003128 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003129 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003130
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003131 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3132 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3133 if (next_func_start_entry)
3134 {
Greg Claytond2653c22012-03-14 01:53:24 +00003135 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3136 // Be sure the clear the Thumb address bit when we calculate the size
3137 // from the current and next address
3138 if (is_arm)
3139 next_symbol_file_addr &= 0xfffffffffffffffeull;
3140 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 +00003141 }
3142 else
3143 {
Greg Claytond2653c22012-03-14 01:53:24 +00003144 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003145 }
3146 }
3147 }
3148 symbol_value -= section_file_addr;
3149 }
3150
3151 sym[sym_idx].SetID (nlist_idx);
3152 sym[sym_idx].SetType (type);
3153 sym[sym_idx].GetAddress().SetSection (symbol_section);
3154 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3155 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3156
3157 if (symbol_byte_size > 0)
3158 sym[sym_idx].SetByteSize(symbol_byte_size);
3159
Greg Clayton01e6a582012-11-27 01:52:16 +00003160 if (demangled_is_synthesized)
3161 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3162
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003163 ++sym_idx;
3164 }
3165 else
3166 {
3167 sym[sym_idx].Clear();
3168 }
3169
3170 }
3171
3172 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3173 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3174 // such entries by figuring out what the address for the global is by looking up this non-STAB
3175 // entry and copying the value into the debug symbol's value to save us the hassle in the
3176 // debug symbol parser.
3177
3178 Symbol *global_symbol = NULL;
3179 for (nlist_idx = 0;
3180 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3181 nlist_idx++)
3182 {
3183 if (global_symbol->GetAddress().GetFileAddress() == 0)
3184 {
3185 std::vector<uint32_t> indexes;
3186 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3187 {
3188 std::vector<uint32_t>::const_iterator pos;
3189 std::vector<uint32_t>::const_iterator end = indexes.end();
3190 for (pos = indexes.begin(); pos != end; ++pos)
3191 {
3192 symbol_ptr = symtab->SymbolAtIndex(*pos);
3193 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3194 {
3195 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3196 break;
3197 }
3198 }
3199 }
Chris Lattner24943d22010-06-08 16:52:24 +00003200 }
3201 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003202
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003203 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3204
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003205 if (function_starts_count > 0)
3206 {
3207 char synthetic_function_symbol[PATH_MAX];
3208 uint32_t num_synthetic_function_symbols = 0;
3209 for (i=0; i<function_starts_count; ++i)
3210 {
3211 if (function_starts.GetEntryRef (i).data == false)
3212 ++num_synthetic_function_symbols;
3213 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003214
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003215 if (num_synthetic_function_symbols > 0)
3216 {
3217 if (num_syms < sym_idx + num_synthetic_function_symbols)
3218 {
3219 num_syms = sym_idx + num_synthetic_function_symbols;
3220 sym = symtab->Resize (num_syms);
3221 }
3222 uint32_t synthetic_function_symbol_idx = 0;
3223 for (i=0; i<function_starts_count; ++i)
3224 {
3225 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3226 if (func_start_entry->data == false)
3227 {
Greg Claytond2653c22012-03-14 01:53:24 +00003228 addr_t symbol_file_addr = func_start_entry->addr;
3229 uint32_t symbol_flags = 0;
3230 if (is_arm)
3231 {
3232 if (symbol_file_addr & 1)
3233 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3234 symbol_file_addr &= 0xfffffffffffffffeull;
3235 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003236 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003237 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003238 {
3239 SectionSP symbol_section (symbol_addr.GetSection());
3240 uint32_t symbol_byte_size = 0;
3241 if (symbol_section)
3242 {
3243 const addr_t section_file_addr = symbol_section->GetFileAddress();
3244 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3245 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3246 if (next_func_start_entry)
3247 {
Greg Claytond2653c22012-03-14 01:53:24 +00003248 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3249 if (is_arm)
3250 next_symbol_file_addr &= 0xfffffffffffffffeull;
3251 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 +00003252 }
3253 else
3254 {
Greg Claytond2653c22012-03-14 01:53:24 +00003255 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003256 }
3257 snprintf (synthetic_function_symbol,
3258 sizeof(synthetic_function_symbol),
3259 "___lldb_unnamed_function%u$$%s",
3260 ++synthetic_function_symbol_idx,
3261 module_sp->GetFileSpec().GetFilename().GetCString());
3262 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003263 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003264 sym[sym_idx].SetType (eSymbolTypeCode);
3265 sym[sym_idx].SetIsSynthetic (true);
3266 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003267 if (symbol_flags)
3268 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003269 if (symbol_byte_size)
3270 sym[sym_idx].SetByteSize (symbol_byte_size);
3271 ++sym_idx;
3272 }
3273 }
3274 }
3275 }
3276 }
3277 }
3278
3279 // Trim our symbols down to just what we ended up with after
3280 // removing any symbols.
3281 if (sym_idx < num_syms)
3282 {
3283 num_syms = sym_idx;
3284 sym = symtab->Resize (num_syms);
3285 }
3286
3287 // Now synthesize indirect symbols
3288 if (m_dysymtab.nindirectsyms != 0)
3289 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003290 if (indirect_symbol_index_data.GetByteSize())
3291 {
3292 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3293
3294 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3295 {
3296 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3297 {
3298 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3299 if (symbol_stub_byte_size == 0)
3300 continue;
3301
3302 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3303
3304 if (num_symbol_stubs == 0)
3305 continue;
3306
3307 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3308 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3309 {
3310 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3311 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 +00003312 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003313 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3314 {
3315 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3316 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3317 continue;
3318
3319 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3320 Symbol *stub_symbol = NULL;
3321 if (index_pos != end_index_pos)
3322 {
3323 // We have a remapping from the original nlist index to
3324 // a current symbol index, so just look this up by index
3325 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3326 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003327 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003328 {
3329 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003330 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003331 // S_SYMBOL_STUBS
3332 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3333 }
3334
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003335 if (stub_symbol)
3336 {
3337 Address so_addr(symbol_stub_addr, section_list);
3338
3339 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3340 {
3341 // Change the external symbol into a trampoline that makes sense
3342 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3343 // can re-use them so we don't have to make up a synthetic symbol
3344 // for no good reason.
3345 stub_symbol->SetType (eSymbolTypeTrampoline);
3346 stub_symbol->SetExternal (false);
3347 stub_symbol->GetAddress() = so_addr;
3348 stub_symbol->SetByteSize (symbol_stub_byte_size);
3349 }
3350 else
3351 {
3352 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003353 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003354 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003355 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003356 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003357 stub_symbol = NULL; // this pointer no longer valid
3358 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003359 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003360 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003361 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3362 sym[sym_idx].SetIsSynthetic (true);
3363 sym[sym_idx].GetAddress() = so_addr;
3364 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3365 ++sym_idx;
3366 }
3367 }
Greg Claytond4330e62012-09-05 01:38:55 +00003368 else
3369 {
3370 if (log)
3371 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3372 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003373 }
3374 }
3375 }
3376 }
3377 }
3378 }
3379 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003380 }
3381 return 0;
3382}
3383
3384
3385void
3386ObjectFileMachO::Dump (Stream *s)
3387{
Greg Clayton9482f052012-03-13 23:14:29 +00003388 ModuleSP module_sp(GetModule());
3389 if (module_sp)
3390 {
3391 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3392 s->Printf("%p: ", this);
3393 s->Indent();
3394 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3395 s->PutCString("ObjectFileMachO64");
3396 else
3397 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003398
Greg Clayton9482f052012-03-13 23:14:29 +00003399 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003400
Greg Clayton9482f052012-03-13 23:14:29 +00003401 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003402
Greg Clayton9482f052012-03-13 23:14:29 +00003403 if (m_sections_ap.get())
3404 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003405
Greg Clayton9482f052012-03-13 23:14:29 +00003406 if (m_symtab_ap.get())
3407 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3408 }
Chris Lattner24943d22010-06-08 16:52:24 +00003409}
3410
3411
3412bool
Greg Clayton0467c782011-02-04 18:53:10 +00003413ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003414{
Greg Clayton9482f052012-03-13 23:14:29 +00003415 ModuleSP module_sp(GetModule());
3416 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003417 {
Greg Clayton9482f052012-03-13 23:14:29 +00003418 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3419 struct uuid_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003420 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003421 uint32_t i;
3422 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003423 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003424 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003425 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3426 break;
3427
3428 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00003429 {
Greg Clayton9482f052012-03-13 23:14:29 +00003430 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003431
Greg Clayton9482f052012-03-13 23:14:29 +00003432 if (uuid_bytes)
3433 {
Sean Callanana5689d52012-07-12 18:04:03 +00003434 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3435 // We pretend these object files have no UUID to prevent crashing.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003436
Sean Callanana5689d52012-07-12 18:04:03 +00003437 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3438 0x3b, 0xa8,
3439 0x4b, 0x16,
3440 0xb6, 0xa4,
3441 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
Jason Molenda9badb6c2013-03-06 23:19:17 +00003442
Sean Callanana5689d52012-07-12 18:04:03 +00003443 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3444 return false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003445
Greg Clayton9482f052012-03-13 23:14:29 +00003446 uuid->SetBytes (uuid_bytes);
3447 return true;
3448 }
3449 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003450 }
Greg Clayton9482f052012-03-13 23:14:29 +00003451 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003452 }
Chris Lattner24943d22010-06-08 16:52:24 +00003453 }
3454 return false;
3455}
3456
3457
3458uint32_t
3459ObjectFileMachO::GetDependentModules (FileSpecList& files)
3460{
Chris Lattner24943d22010-06-08 16:52:24 +00003461 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003462 ModuleSP module_sp(GetModule());
3463 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003464 {
Greg Clayton9482f052012-03-13 23:14:29 +00003465 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3466 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003467 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003468 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3469 uint32_t i;
3470 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003471 {
Greg Clayton9482f052012-03-13 23:14:29 +00003472 const uint32_t cmd_offset = offset;
3473 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3474 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003475
Greg Clayton9482f052012-03-13 23:14:29 +00003476 switch (load_cmd.cmd)
3477 {
3478 case LoadCommandDylibLoad:
3479 case LoadCommandDylibLoadWeak:
3480 case LoadCommandDylibReexport:
3481 case LoadCommandDynamicLinkerLoad:
3482 case LoadCommandFixedVMShlibLoad:
3483 case LoadCommandDylibLoadUpward:
3484 {
3485 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3486 const char *path = m_data.PeekCStr(name_offset);
3487 // Skip any path that starts with '@' since these are usually:
3488 // @executable_path/.../file
3489 // @rpath/.../file
3490 if (path && path[0] != '@')
3491 {
3492 FileSpec file_spec(path, resolve_path);
3493 if (files.AppendIfUnique(file_spec))
3494 count++;
3495 }
3496 }
3497 break;
3498
3499 default:
3500 break;
3501 }
3502 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003503 }
Chris Lattner24943d22010-06-08 16:52:24 +00003504 }
3505 return count;
3506}
3507
Jim Ingham28775942011-03-07 23:44:08 +00003508lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003509ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003510{
3511 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3512 // is initialized to an invalid address, so we can just return that.
3513 // 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 +00003514
Jim Ingham28775942011-03-07 23:44:08 +00003515 if (!IsExecutable() || m_entry_point_address.IsValid())
3516 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003517
3518 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003519 // /usr/include/mach-o.h, but it is basically:
3520 //
3521 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3522 // uint32_t count - this is the count of longs in the thread state data
3523 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3524 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003525 //
Jim Ingham28775942011-03-07 23:44:08 +00003526 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3527 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3528 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3529 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3530 //
3531 // For now we hard-code the offsets and flavors we need:
3532 //
3533 //
3534
Greg Clayton9482f052012-03-13 23:14:29 +00003535 ModuleSP module_sp(GetModule());
3536 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003537 {
Greg Clayton9482f052012-03-13 23:14:29 +00003538 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3539 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003540 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003541 uint32_t i;
3542 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3543 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003544
Greg Clayton9482f052012-03-13 23:14:29 +00003545 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003546 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003547 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003548 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3549 break;
3550
3551 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003552 {
Greg Clayton9482f052012-03-13 23:14:29 +00003553 case LoadCommandUnixThread:
3554 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003555 {
Greg Clayton9482f052012-03-13 23:14:29 +00003556 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003557 {
Greg Clayton9482f052012-03-13 23:14:29 +00003558 uint32_t flavor = m_data.GetU32(&offset);
3559 uint32_t count = m_data.GetU32(&offset);
3560 if (count == 0)
3561 {
3562 // We've gotten off somehow, log and exit;
3563 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003564 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003565
Greg Clayton9482f052012-03-13 23:14:29 +00003566 switch (m_header.cputype)
3567 {
3568 case llvm::MachO::CPUTypeARM:
3569 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3570 {
3571 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3572 start_address = m_data.GetU32(&offset);
3573 done = true;
3574 }
Jim Ingham28775942011-03-07 23:44:08 +00003575 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003576 case llvm::MachO::CPUTypeI386:
3577 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3578 {
3579 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3580 start_address = m_data.GetU32(&offset);
3581 done = true;
3582 }
3583 break;
3584 case llvm::MachO::CPUTypeX86_64:
3585 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3586 {
3587 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3588 start_address = m_data.GetU64(&offset);
3589 done = true;
3590 }
3591 break;
3592 default:
3593 return m_entry_point_address;
3594 }
3595 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3596 if (done)
3597 break;
3598 offset += count * 4;
3599 }
Jim Ingham28775942011-03-07 23:44:08 +00003600 }
Greg Clayton9482f052012-03-13 23:14:29 +00003601 break;
3602 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003603 {
Greg Clayton9482f052012-03-13 23:14:29 +00003604 ConstString text_segment_name ("__TEXT");
3605 uint64_t entryoffset = m_data.GetU64(&offset);
3606 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3607 if (text_segment_sp)
3608 {
3609 done = true;
3610 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3611 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003612 }
Greg Clayton9482f052012-03-13 23:14:29 +00003613
3614 default:
3615 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003616 }
Greg Clayton9482f052012-03-13 23:14:29 +00003617 if (done)
3618 break;
Jim Ingham28775942011-03-07 23:44:08 +00003619
Greg Clayton9482f052012-03-13 23:14:29 +00003620 // Go to the next load command:
3621 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003622 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003623
Greg Clayton9482f052012-03-13 23:14:29 +00003624 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003625 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003626 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003627 // of this ObjectFile:
3628 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003629 {
Greg Clayton9482f052012-03-13 23:14:29 +00003630 m_entry_point_address.Clear();
3631 }
3632 }
3633 else
3634 {
3635 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3636 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003637
Greg Clayton9482f052012-03-13 23:14:29 +00003638 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003639
Greg Clayton9482f052012-03-13 23:14:29 +00003640 if (module_sp)
3641 {
3642 SymbolContextList contexts;
3643 SymbolContext context;
3644 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3645 {
3646 if (contexts.GetContextAtIndex(0, context))
3647 m_entry_point_address = context.symbol->GetAddress();
3648 }
Greg Clayton3508c382012-02-24 01:59:29 +00003649 }
3650 }
Jim Ingham28775942011-03-07 23:44:08 +00003651 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003652
Jim Ingham28775942011-03-07 23:44:08 +00003653 return m_entry_point_address;
3654
3655}
3656
Greg Claytonb5a8f142012-02-05 02:38:54 +00003657lldb_private::Address
3658ObjectFileMachO::GetHeaderAddress ()
3659{
3660 lldb_private::Address header_addr;
3661 SectionList *section_list = GetSectionList();
3662 if (section_list)
3663 {
3664 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3665 if (text_segment_sp)
3666 {
Greg Clayton3508c382012-02-24 01:59:29 +00003667 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003668 header_addr.SetOffset (0);
3669 }
3670 }
3671 return header_addr;
3672}
3673
Greg Clayton46c9a352012-02-09 06:16:32 +00003674uint32_t
3675ObjectFileMachO::GetNumThreadContexts ()
3676{
Greg Clayton9482f052012-03-13 23:14:29 +00003677 ModuleSP module_sp(GetModule());
3678 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003679 {
Greg Clayton9482f052012-03-13 23:14:29 +00003680 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3681 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003682 {
Greg Clayton9482f052012-03-13 23:14:29 +00003683 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003684 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003685 FileRangeArray::Entry file_range;
3686 thread_command thread_cmd;
3687 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003688 {
Greg Clayton9482f052012-03-13 23:14:29 +00003689 const uint32_t cmd_offset = offset;
3690 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3691 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003692
Greg Clayton9482f052012-03-13 23:14:29 +00003693 if (thread_cmd.cmd == LoadCommandThread)
3694 {
3695 file_range.SetRangeBase (offset);
3696 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3697 m_thread_context_offsets.Append (file_range);
3698 }
3699 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003700 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003701 }
3702 }
3703 return m_thread_context_offsets.GetSize();
3704}
3705
3706lldb::RegisterContextSP
3707ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3708{
Greg Clayton46c9a352012-02-09 06:16:32 +00003709 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003710
Greg Clayton9482f052012-03-13 23:14:29 +00003711 ModuleSP module_sp(GetModule());
3712 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003713 {
Greg Clayton9482f052012-03-13 23:14:29 +00003714 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3715 if (!m_thread_context_offsets_valid)
3716 GetNumThreadContexts ();
3717
3718 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003719 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003720 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003721
3722 DataExtractor data (m_data,
3723 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003724 thread_context_file_range->GetByteSize());
3725
3726 switch (m_header.cputype)
3727 {
3728 case llvm::MachO::CPUTypeARM:
3729 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3730 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003731
Jim Ingham6f01c932012-10-12 17:34:26 +00003732 case llvm::MachO::CPUTypeI386:
3733 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3734 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003735
Jim Ingham6f01c932012-10-12 17:34:26 +00003736 case llvm::MachO::CPUTypeX86_64:
3737 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3738 break;
3739 }
Greg Clayton9482f052012-03-13 23:14:29 +00003740 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003741 }
3742 return reg_ctx_sp;
3743}
3744
Greg Claytonb5a8f142012-02-05 02:38:54 +00003745
Greg Claytonca319972011-07-09 00:41:34 +00003746ObjectFile::Type
3747ObjectFileMachO::CalculateType()
3748{
3749 switch (m_header.filetype)
3750 {
3751 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3752 if (GetAddressByteSize () == 4)
3753 {
3754 // 32 bit kexts are just object files, but they do have a valid
3755 // UUID load command.
3756 UUID uuid;
3757 if (GetUUID(&uuid))
3758 {
3759 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003760 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003761 // "OSKextGetCurrentIdentifier" as this is required of kexts
3762 if (m_strata == eStrataInvalid)
3763 m_strata = eStrataKernel;
3764 return eTypeSharedLibrary;
3765 }
3766 }
3767 return eTypeObjectFile;
3768
3769 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3770 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3771 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3772 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3773 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3774 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3775 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3776 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3777 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3778 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3779 default:
3780 break;
3781 }
3782 return eTypeUnknown;
3783}
3784
3785ObjectFile::Strata
3786ObjectFileMachO::CalculateStrata()
3787{
3788 switch (m_header.filetype)
3789 {
3790 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3791 {
3792 // 32 bit kexts are just object files, but they do have a valid
3793 // UUID load command.
3794 UUID uuid;
3795 if (GetUUID(&uuid))
3796 {
3797 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003798 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003799 // "OSKextGetCurrentIdentifier" as this is required of kexts
3800 if (m_type == eTypeInvalid)
3801 m_type = eTypeSharedLibrary;
3802
3803 return eStrataKernel;
3804 }
3805 }
3806 return eStrataUnknown;
3807
3808 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3809 // Check for the MH_DYLDLINK bit in the flags
3810 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003811 {
Greg Claytonca319972011-07-09 00:41:34 +00003812 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003813 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003814 else
Sean Callananac725af2012-02-10 20:22:35 +00003815 {
3816 SectionList *section_list = GetSectionList();
3817 if (section_list)
3818 {
3819 static ConstString g_kld_section_name ("__KLD");
3820 if (section_list->FindSectionByName(g_kld_section_name))
3821 return eStrataKernel;
3822 }
3823 }
3824 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00003825
3826 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
3827 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00003828 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00003829 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
3830 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
3831 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
3832 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
3833 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
3834 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
3835 default:
3836 break;
3837 }
3838 return eStrataUnknown;
3839}
3840
3841
Greg Clayton49f4bf22012-02-22 19:41:02 +00003842uint32_t
3843ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
3844{
Greg Clayton9482f052012-03-13 23:14:29 +00003845 ModuleSP module_sp(GetModule());
3846 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003847 {
Greg Clayton9482f052012-03-13 23:14:29 +00003848 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3849 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003850 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003851 uint32_t version_cmd = 0;
3852 uint64_t version = 0;
3853 uint32_t i;
3854 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003855 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003856 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003857 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3858 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003859
Greg Clayton9482f052012-03-13 23:14:29 +00003860 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003861 {
Greg Clayton9482f052012-03-13 23:14:29 +00003862 if (version_cmd == 0)
3863 {
3864 version_cmd = load_cmd.cmd;
3865 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
3866 break;
3867 version = load_cmd.dylib.current_version;
3868 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003869 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00003870 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00003871 }
Greg Clayton9482f052012-03-13 23:14:29 +00003872 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003873 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003874
Greg Clayton9482f052012-03-13 23:14:29 +00003875 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003876 {
Greg Clayton9482f052012-03-13 23:14:29 +00003877 if (versions != NULL && num_versions > 0)
3878 {
3879 if (num_versions > 0)
3880 versions[0] = (version & 0xFFFF0000ull) >> 16;
3881 if (num_versions > 1)
3882 versions[1] = (version & 0x0000FF00ull) >> 8;
3883 if (num_versions > 2)
3884 versions[2] = (version & 0x000000FFull);
3885 // Fill in an remaining version numbers with invalid values
3886 for (i=3; i<num_versions; ++i)
3887 versions[i] = UINT32_MAX;
3888 }
3889 // The LC_ID_DYLIB load command has a version with 3 version numbers
3890 // in it, so always return 3
3891 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003892 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00003893 }
3894 return false;
3895}
3896
Chris Lattner24943d22010-06-08 16:52:24 +00003897bool
Greg Clayton395fc332011-02-15 21:59:32 +00003898ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00003899{
Greg Clayton9482f052012-03-13 23:14:29 +00003900 ModuleSP module_sp(GetModule());
3901 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003902 {
Greg Clayton9482f052012-03-13 23:14:29 +00003903 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3904 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003905
Greg Clayton9482f052012-03-13 23:14:29 +00003906 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00003907 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00003908 // unknown to make sure we use the DynamicLoaderStatic()...
3909 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
3910 {
3911 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
3912 }
3913 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003914 }
Greg Clayton9482f052012-03-13 23:14:29 +00003915 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003916}
3917
3918
3919//------------------------------------------------------------------
3920// PluginInterface protocol
3921//------------------------------------------------------------------
3922const char *
3923ObjectFileMachO::GetPluginName()
3924{
3925 return "ObjectFileMachO";
3926}
3927
3928const char *
3929ObjectFileMachO::GetShortPluginName()
3930{
3931 return GetPluginNameStatic();
3932}
3933
3934uint32_t
3935ObjectFileMachO::GetPluginVersion()
3936{
3937 return 1;
3938}
3939