blob: b3a81a195e925437010b0484bc8088075540f2b9 [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"
Jason Molendaadf9e3d2013-04-10 05:58:57 +000018#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/FileSpecList.h"
Greg Claytond4330e62012-09-05 01:38:55 +000020#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Module.h"
22#include "lldb/Core/PluginManager.h"
Greg Clayton6f7f8da2012-04-24 03:06:13 +000023#include "lldb/Core/RangeMap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Core/Section.h"
25#include "lldb/Core/StreamFile.h"
26#include "lldb/Core/StreamString.h"
27#include "lldb/Core/Timer.h"
28#include "lldb/Core/UUID.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000029#include "lldb/Host/Host.h"
30#include "lldb/Host/FileSpec.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000031#include "lldb/Symbol/ClangNamespaceDecl.h"
Jason Molendad7938392013-03-21 03:36:01 +000032#include "lldb/Symbol/DWARFCallFrameInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/Symbol/ObjectFile.h"
Greg Clayton29021d32012-04-18 05:19:20 +000034#include "lldb/Target/Platform.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000035#include "lldb/Target/Process.h"
Greg Clayton29021d32012-04-18 05:19:20 +000036#include "lldb/Target/Target.h"
Greg Clayton9ce95382012-02-13 23:10:39 +000037#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
38#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Clayton46c9a352012-02-09 06:16:32 +000039#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner24943d22010-06-08 16:52:24 +000040
Chris Lattner24943d22010-06-08 16:52:24 +000041using namespace lldb;
42using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000043using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000044
Jason Molenda9badb6c2013-03-06 23:19:17 +000045class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
Greg Clayton46c9a352012-02-09 06:16:32 +000046{
47public:
48 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
49 RegisterContextDarwin_x86_64 (thread, 0)
50 {
51 SetRegisterDataFrom_LC_THREAD (data);
52 }
53
54 virtual void
55 InvalidateAllRegisters ()
56 {
57 // Do nothing... registers are always valid...
58 }
59
60 void
61 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
62 {
Greg Clayton36da2aa2013-01-25 18:06:21 +000063 lldb::offset_t offset = 0;
Greg Clayton46c9a352012-02-09 06:16:32 +000064 SetError (GPRRegSet, Read, -1);
65 SetError (FPURegSet, Read, -1);
66 SetError (EXCRegSet, Read, -1);
Greg Clayton9ce95382012-02-13 23:10:39 +000067 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +000068
Greg Clayton9ce95382012-02-13 23:10:39 +000069 while (!done)
Greg Clayton46c9a352012-02-09 06:16:32 +000070 {
Greg Clayton9ce95382012-02-13 23:10:39 +000071 int flavor = data.GetU32 (&offset);
72 if (flavor == 0)
73 done = true;
74 else
Greg Clayton46c9a352012-02-09 06:16:32 +000075 {
Greg Clayton9ce95382012-02-13 23:10:39 +000076 uint32_t i;
77 uint32_t count = data.GetU32 (&offset);
78 switch (flavor)
79 {
80 case GPRRegSet:
81 for (i=0; i<count; ++i)
82 (&gpr.rax)[i] = data.GetU64(&offset);
83 SetError (GPRRegSet, Read, 0);
84 done = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +000085
Greg Clayton9ce95382012-02-13 23:10:39 +000086 break;
87 case FPURegSet:
88 // TODO: fill in FPU regs....
89 //SetError (FPURegSet, Read, -1);
90 done = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +000091
Greg Clayton9ce95382012-02-13 23:10:39 +000092 break;
93 case EXCRegSet:
94 exc.trapno = data.GetU32(&offset);
95 exc.err = data.GetU32(&offset);
96 exc.faultvaddr = data.GetU64(&offset);
97 SetError (EXCRegSet, Read, 0);
98 done = true;
99 break;
100 case 7:
101 case 8:
102 case 9:
103 // fancy flavors that encapsulate of the the above
104 // falvors...
105 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000106
Greg Clayton9ce95382012-02-13 23:10:39 +0000107 default:
108 done = true;
109 break;
110 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000111 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000112 }
113 }
114protected:
115 virtual int
116 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
117 {
118 return 0;
119 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000120
Greg Clayton9ce95382012-02-13 23:10:39 +0000121 virtual int
122 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
123 {
124 return 0;
125 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000126
Greg Clayton9ce95382012-02-13 23:10:39 +0000127 virtual int
128 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
129 {
130 return 0;
131 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000132
Greg Clayton9ce95382012-02-13 23:10:39 +0000133 virtual int
134 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
135 {
136 return 0;
137 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000138
Greg Clayton9ce95382012-02-13 23:10:39 +0000139 virtual int
140 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
141 {
142 return 0;
143 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000144
Greg Clayton9ce95382012-02-13 23:10:39 +0000145 virtual int
146 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
147 {
148 return 0;
149 }
150};
Greg Clayton46c9a352012-02-09 06:16:32 +0000151
Greg Clayton9ce95382012-02-13 23:10:39 +0000152
Jason Molenda9badb6c2013-03-06 23:19:17 +0000153class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
Greg Clayton9ce95382012-02-13 23:10:39 +0000154{
155public:
156 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
157 RegisterContextDarwin_i386 (thread, 0)
158 {
159 SetRegisterDataFrom_LC_THREAD (data);
160 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000161
Greg Clayton9ce95382012-02-13 23:10:39 +0000162 virtual void
163 InvalidateAllRegisters ()
164 {
165 // Do nothing... registers are always valid...
166 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000167
Greg Clayton9ce95382012-02-13 23:10:39 +0000168 void
169 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
170 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000171 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000172 SetError (GPRRegSet, Read, -1);
173 SetError (FPURegSet, Read, -1);
174 SetError (EXCRegSet, Read, -1);
175 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000176
Greg Clayton9ce95382012-02-13 23:10:39 +0000177 while (!done)
178 {
179 int flavor = data.GetU32 (&offset);
180 if (flavor == 0)
181 done = true;
182 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000183 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000184 uint32_t i;
185 uint32_t count = data.GetU32 (&offset);
186 switch (flavor)
187 {
188 case GPRRegSet:
189 for (i=0; i<count; ++i)
190 (&gpr.eax)[i] = data.GetU32(&offset);
191 SetError (GPRRegSet, Read, 0);
192 done = true;
193
194 break;
195 case FPURegSet:
196 // TODO: fill in FPU regs....
197 //SetError (FPURegSet, Read, -1);
198 done = true;
199
200 break;
201 case EXCRegSet:
202 exc.trapno = data.GetU32(&offset);
203 exc.err = data.GetU32(&offset);
204 exc.faultvaddr = data.GetU32(&offset);
205 SetError (EXCRegSet, Read, 0);
206 done = true;
207 break;
208 case 7:
209 case 8:
210 case 9:
211 // fancy flavors that encapsulate of the the above
212 // falvors...
213 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000214
Greg Clayton9ce95382012-02-13 23:10:39 +0000215 default:
216 done = true;
217 break;
218 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000219 }
220 }
221 }
222protected:
223 virtual int
224 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
225 {
226 return 0;
227 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000228
Greg Clayton46c9a352012-02-09 06:16:32 +0000229 virtual int
230 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
231 {
232 return 0;
233 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000234
Greg Clayton46c9a352012-02-09 06:16:32 +0000235 virtual int
236 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
237 {
238 return 0;
239 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000240
Greg Clayton46c9a352012-02-09 06:16:32 +0000241 virtual int
242 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
243 {
244 return 0;
245 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000246
Greg Clayton46c9a352012-02-09 06:16:32 +0000247 virtual int
248 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
249 {
250 return 0;
251 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000252
Greg Clayton46c9a352012-02-09 06:16:32 +0000253 virtual int
254 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
255 {
256 return 0;
257 }
258};
259
Jason Molenda9badb6c2013-03-06 23:19:17 +0000260class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Clayton9ce95382012-02-13 23:10:39 +0000261{
262public:
263 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonb5431d02012-10-30 23:57:32 +0000264 RegisterContextDarwin_arm (thread, 0)
Greg Clayton9ce95382012-02-13 23:10:39 +0000265 {
266 SetRegisterDataFrom_LC_THREAD (data);
267 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000268
Greg Clayton9ce95382012-02-13 23:10:39 +0000269 virtual void
270 InvalidateAllRegisters ()
271 {
272 // Do nothing... registers are always valid...
273 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000274
Greg Clayton9ce95382012-02-13 23:10:39 +0000275 void
276 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
277 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000278 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000279 SetError (GPRRegSet, Read, -1);
280 SetError (FPURegSet, Read, -1);
281 SetError (EXCRegSet, Read, -1);
282 int flavor = data.GetU32 (&offset);
283 uint32_t count = data.GetU32 (&offset);
284 switch (flavor)
285 {
286 case GPRRegSet:
287 for (uint32_t i=0; i<count; ++i)
288 gpr.r[i] = data.GetU32(&offset);
289 SetError (GPRRegSet, Read, 0);
290 break;
291 case FPURegSet:
292 // TODO: fill in FPU regs....
293 //SetError (FPURegSet, Read, -1);
294 break;
295 case EXCRegSet:
296 exc.exception = data.GetU32(&offset);
297 exc.fsr = data.GetU32(&offset);
298 exc.far = data.GetU32(&offset);
299 SetError (EXCRegSet, Read, 0);
300 break;
301 }
302 }
303protected:
304 virtual int
305 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
306 {
307 return 0;
308 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000309
Greg Clayton9ce95382012-02-13 23:10:39 +0000310 virtual int
311 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
312 {
313 return 0;
314 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000315
Greg Clayton9ce95382012-02-13 23:10:39 +0000316 virtual int
317 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
318 {
319 return 0;
320 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000321
322 virtual int
323 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
324 {
325 return -1;
326 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000327
Greg Clayton9ce95382012-02-13 23:10:39 +0000328 virtual int
329 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
330 {
331 return 0;
332 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000333
Greg Clayton9ce95382012-02-13 23:10:39 +0000334 virtual int
335 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
336 {
337 return 0;
338 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000339
Greg Clayton9ce95382012-02-13 23:10:39 +0000340 virtual int
341 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
342 {
343 return 0;
344 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000345
Greg Claytonb5431d02012-10-30 23:57:32 +0000346 virtual int
347 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
348 {
349 return -1;
350 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000351};
352
Greg Claytonb1888f22011-03-19 01:12:21 +0000353#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000354
355void
356ObjectFileMachO::Initialize()
357{
358 PluginManager::RegisterPlugin (GetPluginNameStatic(),
359 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000360 CreateInstance,
361 CreateMemoryInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000362}
363
364void
365ObjectFileMachO::Terminate()
366{
367 PluginManager::UnregisterPlugin (CreateInstance);
368}
369
370
371const char *
372ObjectFileMachO::GetPluginNameStatic()
373{
374 return "object-file.mach-o";
375}
376
377const char *
378ObjectFileMachO::GetPluginDescriptionStatic()
379{
380 return "Mach-o object file reader (32 and 64 bit)";
381}
382
Chris Lattner24943d22010-06-08 16:52:24 +0000383ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000384ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
385 DataBufferSP& data_sp,
386 lldb::offset_t data_offset,
387 const FileSpec* file,
388 lldb::offset_t file_offset,
389 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000390{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000391 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000392 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000393 data_sp = file->MemoryMapFileContents(file_offset, length);
394 data_offset = 0;
395 }
396
397 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
398 {
399 // Update the data to contain the entire file if it doesn't already
400 if (data_sp->GetByteSize() < length)
401 {
402 data_sp = file->MemoryMapFileContents(file_offset, length);
403 data_offset = 0;
404 }
405 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 +0000406 if (objfile_ap.get() && objfile_ap->ParseHeader())
407 return objfile_ap.release();
408 }
409 return NULL;
410}
411
Greg Claytonb5a8f142012-02-05 02:38:54 +0000412ObjectFile *
Jason Molenda9badb6c2013-03-06 23:19:17 +0000413ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
414 DataBufferSP& data_sp,
415 const ProcessSP &process_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000416 lldb::addr_t header_addr)
417{
418 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
419 {
Greg Clayton3508c382012-02-24 01:59:29 +0000420 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000421 if (objfile_ap.get() && objfile_ap->ParseHeader())
422 return objfile_ap.release();
423 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000424 return NULL;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000425}
426
427
428const ConstString &
429ObjectFileMachO::GetSegmentNameTEXT()
430{
431 static ConstString g_segment_name_TEXT ("__TEXT");
432 return g_segment_name_TEXT;
433}
434
435const ConstString &
436ObjectFileMachO::GetSegmentNameDATA()
437{
438 static ConstString g_segment_name_DATA ("__DATA");
439 return g_segment_name_DATA;
440}
441
442const ConstString &
443ObjectFileMachO::GetSegmentNameOBJC()
444{
445 static ConstString g_segment_name_OBJC ("__OBJC");
446 return g_segment_name_OBJC;
447}
448
449const ConstString &
450ObjectFileMachO::GetSegmentNameLINKEDIT()
451{
452 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
453 return g_section_name_LINKEDIT;
454}
455
456const ConstString &
457ObjectFileMachO::GetSectionNameEHFrame()
458{
459 static ConstString g_section_name_eh_frame ("__eh_frame");
460 return g_section_name_eh_frame;
461}
462
463
Chris Lattner24943d22010-06-08 16:52:24 +0000464
465static uint32_t
466MachHeaderSizeFromMagic(uint32_t magic)
467{
468 switch (magic)
469 {
Greg Clayton1674b122010-07-21 22:12:05 +0000470 case HeaderMagic32:
471 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000472 return sizeof(struct mach_header);
473
Greg Clayton1674b122010-07-21 22:12:05 +0000474 case HeaderMagic64:
475 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000476 return sizeof(struct mach_header_64);
477 break;
478
479 default:
480 break;
481 }
482 return 0;
483}
484
485
486bool
Jason Molenda9badb6c2013-03-06 23:19:17 +0000487ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
488 lldb::addr_t data_offset,
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000489 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000490{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000491 DataExtractor data;
492 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000493 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000494 uint32_t magic = data.GetU32(&offset);
495 return MachHeaderSizeFromMagic(magic) != 0;
496}
497
498
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000499ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
500 DataBufferSP& data_sp,
501 lldb::offset_t data_offset,
502 const FileSpec* file,
503 lldb::offset_t file_offset,
504 lldb::offset_t length) :
505 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Clayton46c9a352012-02-09 06:16:32 +0000506 m_mach_segments(),
507 m_mach_sections(),
508 m_entry_point_address(),
509 m_thread_context_offsets(),
510 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000511{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000512 ::memset (&m_header, 0, sizeof(m_header));
513 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000514}
515
Greg Clayton3508c382012-02-24 01:59:29 +0000516ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000517 lldb::DataBufferSP& header_data_sp,
518 const lldb::ProcessSP &process_sp,
519 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000520 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Clayton46c9a352012-02-09 06:16:32 +0000521 m_mach_segments(),
522 m_mach_sections(),
523 m_entry_point_address(),
524 m_thread_context_offsets(),
525 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000526{
527 ::memset (&m_header, 0, sizeof(m_header));
528 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
529}
Chris Lattner24943d22010-06-08 16:52:24 +0000530
531ObjectFileMachO::~ObjectFileMachO()
532{
533}
534
535
536bool
537ObjectFileMachO::ParseHeader ()
538{
Greg Clayton9482f052012-03-13 23:14:29 +0000539 ModuleSP module_sp(GetModule());
540 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000541 {
Greg Clayton9482f052012-03-13 23:14:29 +0000542 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
543 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000544 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000545 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000546 // Leave magic in the original byte order
547 m_header.magic = m_data.GetU32(&offset);
548 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000549 {
Greg Clayton9482f052012-03-13 23:14:29 +0000550 case HeaderMagic32:
551 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
552 m_data.SetAddressByteSize(4);
553 can_parse = true;
554 break;
555
556 case HeaderMagic64:
557 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
558 m_data.SetAddressByteSize(8);
559 can_parse = true;
560 break;
561
562 case HeaderMagic32Swapped:
563 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
564 m_data.SetAddressByteSize(4);
565 can_parse = true;
566 break;
567
568 case HeaderMagic64Swapped:
569 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
570 m_data.SetAddressByteSize(8);
571 can_parse = true;
572 break;
573
574 default:
575 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000576 }
Greg Clayton9482f052012-03-13 23:14:29 +0000577
578 if (can_parse)
579 {
580 m_data.GetU32(&offset, &m_header.cputype, 6);
581
582 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +0000583
Greg Clayton21a25432012-11-16 21:36:10 +0000584 // Check if the module has a required architecture
585 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000586 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000587 return false;
588
Greg Clayton9482f052012-03-13 23:14:29 +0000589 if (SetModulesArchitecture (mach_arch))
590 {
591 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
592 if (m_data.GetByteSize() < header_and_lc_size)
593 {
594 DataBufferSP data_sp;
595 ProcessSP process_sp (m_process_wp.lock());
596 if (process_sp)
597 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000598 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000599 }
600 else
601 {
602 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000603 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000604 if (data_sp->GetByteSize() != header_and_lc_size)
605 return false;
606 }
607 if (data_sp)
608 m_data.SetData (data_sp);
609 }
610 }
611 return true;
612 }
613 else
614 {
615 memset(&m_header, 0, sizeof(struct mach_header));
616 }
Chris Lattner24943d22010-06-08 16:52:24 +0000617 }
618 return false;
619}
620
621
622ByteOrder
623ObjectFileMachO::GetByteOrder () const
624{
Chris Lattner24943d22010-06-08 16:52:24 +0000625 return m_data.GetByteOrder ();
626}
627
Jim Ingham7508e732010-08-09 23:31:02 +0000628bool
629ObjectFileMachO::IsExecutable() const
630{
631 return m_header.filetype == HeaderFileTypeExecutable;
632}
Chris Lattner24943d22010-06-08 16:52:24 +0000633
Greg Clayton36da2aa2013-01-25 18:06:21 +0000634uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000635ObjectFileMachO::GetAddressByteSize () const
636{
Chris Lattner24943d22010-06-08 16:52:24 +0000637 return m_data.GetAddressByteSize ();
638}
639
Greg Claytonb3448432011-03-24 21:19:54 +0000640AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000641ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
642{
643 Symtab *symtab = GetSymtab();
644 if (symtab)
645 {
646 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
647 if (symbol)
648 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000649 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000650 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000651 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000652 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000653 {
Greg Clayton3508c382012-02-24 01:59:29 +0000654 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000655 switch (section_type)
656 {
657 case eSectionTypeInvalid: return eAddressClassUnknown;
658 case eSectionTypeCode:
659 if (m_header.cputype == llvm::MachO::CPUTypeARM)
660 {
661 // For ARM we have a bit in the n_desc field of the symbol
662 // that tells us ARM/Thumb which is bit 0x0008.
663 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
664 return eAddressClassCodeAlternateISA;
665 }
666 return eAddressClassCode;
667
668 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000669 case eSectionTypeData:
670 case eSectionTypeDataCString:
671 case eSectionTypeDataCStringPointers:
672 case eSectionTypeDataSymbolAddress:
673 case eSectionTypeData4:
674 case eSectionTypeData8:
675 case eSectionTypeData16:
676 case eSectionTypeDataPointers:
677 case eSectionTypeZeroFill:
678 case eSectionTypeDataObjCMessageRefs:
679 case eSectionTypeDataObjCCFStrings:
680 return eAddressClassData;
681 case eSectionTypeDebug:
682 case eSectionTypeDWARFDebugAbbrev:
683 case eSectionTypeDWARFDebugAranges:
684 case eSectionTypeDWARFDebugFrame:
685 case eSectionTypeDWARFDebugInfo:
686 case eSectionTypeDWARFDebugLine:
687 case eSectionTypeDWARFDebugLoc:
688 case eSectionTypeDWARFDebugMacInfo:
689 case eSectionTypeDWARFDebugPubNames:
690 case eSectionTypeDWARFDebugPubTypes:
691 case eSectionTypeDWARFDebugRanges:
692 case eSectionTypeDWARFDebugStr:
693 case eSectionTypeDWARFAppleNames:
694 case eSectionTypeDWARFAppleTypes:
695 case eSectionTypeDWARFAppleNamespaces:
696 case eSectionTypeDWARFAppleObjC:
697 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000698 case eSectionTypeEHFrame: return eAddressClassRuntime;
699 case eSectionTypeOther: return eAddressClassUnknown;
700 }
701 }
702 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000703
Greg Claytonb3448432011-03-24 21:19:54 +0000704 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000705 switch (symbol_type)
706 {
707 case eSymbolTypeAny: return eAddressClassUnknown;
708 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000709
Greg Claytonb1888f22011-03-19 01:12:21 +0000710 case eSymbolTypeCode:
711 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000712 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000713 if (m_header.cputype == llvm::MachO::CPUTypeARM)
714 {
715 // For ARM we have a bit in the n_desc field of the symbol
716 // that tells us ARM/Thumb which is bit 0x0008.
717 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
718 return eAddressClassCodeAlternateISA;
719 }
720 return eAddressClassCode;
721
722 case eSymbolTypeData: return eAddressClassData;
723 case eSymbolTypeRuntime: return eAddressClassRuntime;
724 case eSymbolTypeException: return eAddressClassRuntime;
725 case eSymbolTypeSourceFile: return eAddressClassDebug;
726 case eSymbolTypeHeaderFile: return eAddressClassDebug;
727 case eSymbolTypeObjectFile: return eAddressClassDebug;
728 case eSymbolTypeCommonBlock: return eAddressClassDebug;
729 case eSymbolTypeBlock: return eAddressClassDebug;
730 case eSymbolTypeLocal: return eAddressClassData;
731 case eSymbolTypeParam: return eAddressClassData;
732 case eSymbolTypeVariable: return eAddressClassData;
733 case eSymbolTypeVariableType: return eAddressClassDebug;
734 case eSymbolTypeLineEntry: return eAddressClassDebug;
735 case eSymbolTypeLineHeader: return eAddressClassDebug;
736 case eSymbolTypeScopeBegin: return eAddressClassDebug;
737 case eSymbolTypeScopeEnd: return eAddressClassDebug;
738 case eSymbolTypeAdditional: return eAddressClassUnknown;
739 case eSymbolTypeCompiler: return eAddressClassDebug;
740 case eSymbolTypeInstrumentation:return eAddressClassDebug;
741 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000742 case eSymbolTypeObjCClass: return eAddressClassRuntime;
743 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
744 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000745 }
746 }
747 }
748 return eAddressClassUnknown;
749}
Chris Lattner24943d22010-06-08 16:52:24 +0000750
751Symtab *
752ObjectFileMachO::GetSymtab()
753{
Greg Clayton9482f052012-03-13 23:14:29 +0000754 ModuleSP module_sp(GetModule());
755 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000756 {
Greg Clayton9482f052012-03-13 23:14:29 +0000757 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
758 if (m_symtab_ap.get() == NULL)
759 {
760 m_symtab_ap.reset(new Symtab(this));
761 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
762 ParseSymtab (true);
763 m_symtab_ap->Finalize ();
764 }
Chris Lattner24943d22010-06-08 16:52:24 +0000765 }
766 return m_symtab_ap.get();
767}
768
769
770SectionList *
771ObjectFileMachO::GetSectionList()
772{
Greg Clayton9482f052012-03-13 23:14:29 +0000773 ModuleSP module_sp(GetModule());
774 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000775 {
Greg Clayton9482f052012-03-13 23:14:29 +0000776 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
777 if (m_sections_ap.get() == NULL)
778 {
779 m_sections_ap.reset(new SectionList());
780 ParseSections();
781 }
Chris Lattner24943d22010-06-08 16:52:24 +0000782 }
783 return m_sections_ap.get();
784}
785
786
787size_t
788ObjectFileMachO::ParseSections ()
789{
790 lldb::user_id_t segID = 0;
791 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000792 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000793 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000794 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000795 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000796 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000797 // First look up any LC_ENCRYPTION_INFO load commands
798 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
799 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000800 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000801 for (i=0; i<m_header.ncmds; ++i)
802 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000803 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000804 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000805 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000806
Greg Clayton54e33712012-05-25 18:09:55 +0000807 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000808 {
Greg Clayton54e33712012-05-25 18:09:55 +0000809 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
810 {
811 if (encryption_cmd.cryptid != 0)
812 {
813 EncryptedFileRanges::Entry entry;
814 entry.SetRangeBase(encryption_cmd.cryptoff);
815 entry.SetByteSize(encryption_cmd.cryptsize);
816 encrypted_file_ranges.Append(entry);
817 }
818 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000819 }
Greg Clayton54e33712012-05-25 18:09:55 +0000820 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000821 }
822
823 offset = MachHeaderSizeFromMagic(m_header.magic);
824
Greg Clayton54e33712012-05-25 18:09:55 +0000825 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000826 for (i=0; i<m_header.ncmds; ++i)
827 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000828 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000829 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
830 break;
831
Greg Clayton1674b122010-07-21 22:12:05 +0000832 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000833 {
834 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
835 {
836 load_cmd.vmaddr = m_data.GetAddress(&offset);
837 load_cmd.vmsize = m_data.GetAddress(&offset);
838 load_cmd.fileoff = m_data.GetAddress(&offset);
839 load_cmd.filesize = m_data.GetAddress(&offset);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000840 if (m_length != 0 && load_cmd.filesize != 0)
841 {
842 if (load_cmd.fileoff + load_cmd.filesize > m_length)
843 {
844 // We have a load command that says it extends past the end of hte file. This is likely
845 // a corrupt file. We don't have any way to return an error condition here (this method
846 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
847 // is null out the SectionList vector and if a process has been set up, dump a message
848 // to stdout. The most common case here is core file debugging with a truncated file - and
849 // in that case we don't have a Process yet so nothing will be printed. Not really ideal;
850 // the ObjectFile needs some way of reporting an error message for methods like GetSectionList
851 // which fail.
852 ProcessSP process_sp (m_process_wp.lock());
853 if (process_sp)
854 {
855 Stream *s = &process_sp->GetTarget().GetDebugger().GetOutputStream();
856 if (s)
857 {
858 s->Printf ("Corrupt/invalid Mach-O object file -- a load command extends past the end of the file.\n");
859 }
860 }
861 m_sections_ap->Clear();
862 return 0;
863 }
864 }
Chris Lattner24943d22010-06-08 16:52:24 +0000865 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
866 {
Jason Molenda9badb6c2013-03-06 23:19:17 +0000867
Greg Clayton68ca8232011-01-25 02:58:48 +0000868 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
869
Chris Lattner24943d22010-06-08 16:52:24 +0000870 // Keep a list of mach segments around in case we need to
871 // get at data that isn't stored in the abstracted Sections.
872 m_mach_segments.push_back (load_cmd);
873
Greg Clayton36da2aa2013-01-25 18:06:21 +0000874 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 +0000875 // Use a segment ID of the segment index shifted left by 8 so they
876 // never conflict with any of the sections.
877 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000878 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000879 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000880 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000881 ++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
882 segment_name, // Name of this section
883 eSectionTypeContainer, // This section is a container of other sections.
884 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
885 load_cmd.vmsize, // VM size in bytes of this section
886 load_cmd.fileoff, // Offset to the data for this section in the file
887 load_cmd.filesize, // Size in bytes of this section as found in the the file
888 load_cmd.flags)); // Flags for this section
889
Greg Clayton68ca8232011-01-25 02:58:48 +0000890 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000891 m_sections_ap->AddSection(segment_sp);
892 }
893
894 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000895 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000896 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +0000897 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +0000898 // mach sections yet...
899 if (m_mach_sections.empty())
900 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000901 uint32_t segment_sect_idx;
902 const lldb::user_id_t first_segment_sectID = sectID + 1;
903
904
Greg Clayton1674b122010-07-21 22:12:05 +0000905 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000906 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
907 {
908 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
909 break;
910 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
911 break;
912 sect64.addr = m_data.GetAddress(&offset);
913 sect64.size = m_data.GetAddress(&offset);
914
915 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
916 break;
917
918 // Keep a list of mach sections around in case we need to
919 // get at data that isn't stored in the abstracted Sections.
920 m_mach_sections.push_back (sect64);
921
922 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
923 if (!segment_name)
924 {
925 // We have a segment with no name so we need to conjure up
926 // segments that correspond to the section's segname if there
927 // isn't already such a section. If there is such a section,
928 // we resize the section so that it spans all sections.
929 // We also mark these sections as fake so address matches don't
930 // hit if they land in the gaps between the child sections.
931 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
932 segment_sp = m_sections_ap->FindSectionByName (segment_name);
933 if (segment_sp.get())
934 {
935 Section *segment = segment_sp.get();
936 // Grow the section size as needed.
937 const lldb::addr_t sect64_min_addr = sect64.addr;
938 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
939 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
940 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
941 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
942 if (sect64_min_addr >= curr_seg_min_addr)
943 {
944 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
945 // Only grow the section size if needed
946 if (new_seg_byte_size > curr_seg_byte_size)
947 segment->SetByteSize (new_seg_byte_size);
948 }
949 else
950 {
951 // We need to change the base address of the segment and
952 // adjust the child section offsets for all existing children.
953 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
954 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +0000955 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000956 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
957 }
Greg Clayton661825b2010-06-28 23:51:11 +0000958
959 // Grow the section size as needed.
960 if (sect64.offset)
961 {
962 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
963 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
964
965 const lldb::addr_t section_min_file_offset = sect64.offset;
966 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
967 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
968 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
969 segment->SetFileOffset (new_file_offset);
970 segment->SetFileSize (new_file_size);
971 }
Chris Lattner24943d22010-06-08 16:52:24 +0000972 }
973 else
974 {
975 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +0000976 segment_sp.reset(new Section (segment_sp, // Parent section
977 module_sp, // Module to which this section belongs
978 ++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
979 segment_name, // Name of this section
980 eSectionTypeContainer, // This section is a container of other sections.
981 sect64.addr, // File VM address == addresses as they are found in the object file
982 sect64.size, // VM size in bytes of this section
983 sect64.offset, // Offset to the data for this section in the file
984 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
985 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +0000986 segment_sp->SetIsFake(true);
987 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000988 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000989 }
990 }
991 assert (segment_sp.get());
992
Greg Clayton1674b122010-07-21 22:12:05 +0000993 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000994 static ConstString g_sect_name_objc_data ("__objc_data");
995 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
996 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
997 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
998 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
999 static ConstString g_sect_name_objc_const ("__objc_const");
1000 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1001 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001002
1003 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1004 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1005 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1006 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1007 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1008 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1009 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1010 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1011 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1012 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1013 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +00001014 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1015 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +00001016 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +00001017 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001018 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +00001019 static ConstString g_sect_name_DATA ("__DATA");
1020 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001021
Chris Lattner24943d22010-06-08 16:52:24 +00001022 SectionType sect_type = eSectionTypeOther;
1023
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001024 if (section_name == g_sect_name_dwarf_debug_abbrev)
1025 sect_type = eSectionTypeDWARFDebugAbbrev;
1026 else if (section_name == g_sect_name_dwarf_debug_aranges)
1027 sect_type = eSectionTypeDWARFDebugAranges;
1028 else if (section_name == g_sect_name_dwarf_debug_frame)
1029 sect_type = eSectionTypeDWARFDebugFrame;
1030 else if (section_name == g_sect_name_dwarf_debug_info)
1031 sect_type = eSectionTypeDWARFDebugInfo;
1032 else if (section_name == g_sect_name_dwarf_debug_line)
1033 sect_type = eSectionTypeDWARFDebugLine;
1034 else if (section_name == g_sect_name_dwarf_debug_loc)
1035 sect_type = eSectionTypeDWARFDebugLoc;
1036 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1037 sect_type = eSectionTypeDWARFDebugMacInfo;
1038 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1039 sect_type = eSectionTypeDWARFDebugPubNames;
1040 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1041 sect_type = eSectionTypeDWARFDebugPubTypes;
1042 else if (section_name == g_sect_name_dwarf_debug_ranges)
1043 sect_type = eSectionTypeDWARFDebugRanges;
1044 else if (section_name == g_sect_name_dwarf_debug_str)
1045 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001046 else if (section_name == g_sect_name_dwarf_apple_names)
1047 sect_type = eSectionTypeDWARFAppleNames;
1048 else if (section_name == g_sect_name_dwarf_apple_types)
1049 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001050 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1051 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001052 else if (section_name == g_sect_name_dwarf_apple_objc)
1053 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001054 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001055 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001056 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001057 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001058 else if (section_name == g_sect_name_eh_frame)
1059 sect_type = eSectionTypeEHFrame;
1060 else if (section_name == g_sect_name_cfstring)
1061 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001062 else if (section_name == g_sect_name_objc_data ||
1063 section_name == g_sect_name_objc_classrefs ||
1064 section_name == g_sect_name_objc_superrefs ||
1065 section_name == g_sect_name_objc_const ||
1066 section_name == g_sect_name_objc_classlist)
1067 {
1068 sect_type = eSectionTypeDataPointers;
1069 }
Chris Lattner24943d22010-06-08 16:52:24 +00001070
1071 if (sect_type == eSectionTypeOther)
1072 {
1073 switch (mach_sect_type)
1074 {
1075 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001076 case SectionTypeRegular:
1077 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001078 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001079 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001080 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001081 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001082 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001083 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001084 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1085 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1086 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1087 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1088 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1089 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1090 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1091 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1092 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1093 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1094 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1095 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1096 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1097 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1098 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1099 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001100 default: break;
1101 }
1102 }
1103
Greg Clayton3508c382012-02-24 01:59:29 +00001104 SectionSP section_sp(new Section (segment_sp,
1105 module_sp,
1106 ++sectID,
1107 section_name,
1108 sect_type,
1109 sect64.addr - segment_sp->GetFileAddress(),
1110 sect64.size,
1111 sect64.offset,
1112 sect64.offset == 0 ? 0 : sect64.size,
1113 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001114 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001115
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001116 bool section_is_encrypted = false;
1117 if (!segment_is_encrypted && load_cmd.filesize != 0)
1118 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001119
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001120 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001121 segment_sp->GetChildren().AddSection(section_sp);
1122
1123 if (segment_sp->IsFake())
1124 {
1125 segment_sp.reset();
1126 segment_name.Clear();
1127 }
1128 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001129 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001130 {
1131 if (first_segment_sectID <= sectID)
1132 {
1133 lldb::user_id_t sect_uid;
1134 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1135 {
1136 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1137 SectionSP next_section_sp;
1138 if (sect_uid + 1 <= sectID)
1139 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1140
1141 if (curr_section_sp.get())
1142 {
1143 if (curr_section_sp->GetByteSize() == 0)
1144 {
1145 if (next_section_sp.get() != NULL)
1146 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1147 else
1148 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1149 }
1150 }
1151 }
1152 }
1153 }
1154 }
1155 }
1156 }
Greg Clayton1674b122010-07-21 22:12:05 +00001157 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001158 {
1159 m_dysymtab.cmd = load_cmd.cmd;
1160 m_dysymtab.cmdsize = load_cmd.cmdsize;
1161 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1162 }
1163
1164 offset = load_cmd_offset + load_cmd.cmdsize;
1165 }
1166// if (dump_sections)
1167// {
1168// StreamFile s(stdout);
1169// m_sections_ap->Dump(&s, true);
1170// }
1171 return sectID; // Return the number of sections we registered with the module
1172}
1173
1174class MachSymtabSectionInfo
1175{
1176public:
1177
1178 MachSymtabSectionInfo (SectionList *section_list) :
1179 m_section_list (section_list),
1180 m_section_infos()
1181 {
1182 // Get the number of sections down to a depth of 1 to include
1183 // all segments and their sections, but no other sections that
1184 // may be added for debug map or
1185 m_section_infos.resize(section_list->GetNumSections(1));
1186 }
1187
1188
Greg Clayton3508c382012-02-24 01:59:29 +00001189 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001190 GetSection (uint8_t n_sect, addr_t file_addr)
1191 {
1192 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001193 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001194 if (n_sect < m_section_infos.size())
1195 {
Greg Clayton3508c382012-02-24 01:59:29 +00001196 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001197 {
Greg Clayton3508c382012-02-24 01:59:29 +00001198 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1199 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001200 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001201 {
Greg Clayton3508c382012-02-24 01:59:29 +00001202 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1203 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001204 }
1205 else
1206 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001207 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001208 }
Chris Lattner24943d22010-06-08 16:52:24 +00001209 }
1210 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001211 {
1212 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001213 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001214 }
1215 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1216 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1217 {
1218 // Symbol is in section with zero size, but has the same start
1219 // address as the section. This can happen with linker symbols
1220 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001221 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001222 }
Chris Lattner24943d22010-06-08 16:52:24 +00001223 }
Greg Clayton3508c382012-02-24 01:59:29 +00001224 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001225 }
1226
1227protected:
1228 struct SectionInfo
1229 {
1230 SectionInfo () :
1231 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001232 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001233 {
1234 }
1235
1236 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001237 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001238 };
1239 SectionList *m_section_list;
1240 std::vector<SectionInfo> m_section_infos;
1241};
1242
Chris Lattner24943d22010-06-08 16:52:24 +00001243size_t
1244ObjectFileMachO::ParseSymtab (bool minimize)
1245{
1246 Timer scoped_timer(__PRETTY_FUNCTION__,
1247 "ObjectFileMachO::ParseSymtab () module = %s",
1248 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001249 ModuleSP module_sp (GetModule());
1250 if (!module_sp)
1251 return 0;
1252
1253 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1254 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1255 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1256 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001257 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001258 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001259
Greg Clayton952e9dc2013-03-27 23:08:40 +00001260 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001261
Chris Lattner24943d22010-06-08 16:52:24 +00001262 for (i=0; i<m_header.ncmds; ++i)
1263 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001264 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001265 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001266 struct load_command lc;
1267 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001268 break;
1269 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001270 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001271 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001272 case LoadCommandSymtab:
1273 symtab_load_command.cmd = lc.cmd;
1274 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001275 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001276 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1277 return 0;
1278 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001279 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001280 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001281 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001282 return 0;
1283 }
1284
1285 if (symtab_load_command.stroff == 0)
1286 {
1287 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001288 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001289 return 0;
1290 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001291
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001292 if (symtab_load_command.nsyms == 0)
1293 {
1294 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001295 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001296 return 0;
1297 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001298
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001299 if (symtab_load_command.strsize == 0)
1300 {
1301 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001302 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001303 return 0;
1304 }
1305 break;
1306
1307 case LoadCommandFunctionStarts:
1308 function_starts_load_command.cmd = lc.cmd;
1309 function_starts_load_command.cmdsize = lc.cmdsize;
1310 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1311 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1312 break;
1313
1314 default:
1315 break;
1316 }
1317 offset = cmd_offset + lc.cmdsize;
1318 }
1319
1320 if (symtab_load_command.cmd)
1321 {
1322 Symtab *symtab = m_symtab_ap.get();
1323 SectionList *section_list = GetSectionList();
1324 if (section_list == NULL)
1325 return 0;
1326
1327 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001328 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001329
Greg Clayton36da2aa2013-01-25 18:06:21 +00001330 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1331 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001332 bool bit_width_32 = addr_byte_size == 4;
1333 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1334
Greg Clayton36da2aa2013-01-25 18:06:21 +00001335 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1336 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1337 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001338 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001339
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001340 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1341 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001342 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1343 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001344 {
Greg Claytondd29b972012-05-18 23:20:01 +00001345 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001346 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1347 // Reading mach file from memory in a process or core file...
1348
1349 if (linkedit_section_sp)
1350 {
1351 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1352 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1353 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001354 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001355
1356 bool data_was_read = false;
1357
1358#if defined (__APPLE__) && defined (__arm__)
1359 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001360 {
Greg Clayton29021d32012-04-18 05:19:20 +00001361 // This mach-o memory file is in the dyld shared cache. If this
1362 // program is not remote and this is iOS, then this process will
1363 // share the same shared cache as the process we are debugging and
1364 // we can read the entire __LINKEDIT from the address space in this
1365 // process. This is a needed optimization that is used for local iOS
1366 // debugging only since all shared libraries in the shared cache do
1367 // not have corresponding files that exist in the file system of the
1368 // device. They have been combined into a single file. This means we
1369 // always have to load these files from memory. All of the symbol and
1370 // string tables from all of the __LINKEDIT sections from the shared
1371 // libraries in the shared cache have been merged into a single large
1372 // symbol and string table. Reading all of this symbol and string table
1373 // data across can slow down debug launch times, so we optimize this by
1374 // reading the memory for the __LINKEDIT section from this process.
1375 PlatformSP platform_sp (target.GetPlatform());
Jason Molendab76f77e2012-12-13 01:13:00 +00001376 if (platform_sp && platform_sp->IsHost())
Greg Clayton29021d32012-04-18 05:19:20 +00001377 {
1378 data_was_read = true;
1379 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001380 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001381 if (function_starts_load_command.cmd)
1382 {
1383 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1384 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1385 }
1386 }
1387 }
1388#endif
1389
1390 if (!data_was_read)
1391 {
1392 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1393 if (nlist_data_sp)
1394 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001395 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1396 //if (strtab_data_sp)
1397 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001398 if (m_dysymtab.nindirectsyms != 0)
1399 {
1400 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1401 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1402 if (indirect_syms_data_sp)
1403 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1404 }
Greg Clayton29021d32012-04-18 05:19:20 +00001405 if (function_starts_load_command.cmd)
1406 {
1407 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1408 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1409 if (func_start_data_sp)
1410 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1411 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001412 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001413 }
1414 }
1415 else
1416 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001417 nlist_data.SetData (m_data,
1418 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001419 nlist_data_byte_size);
1420 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001421 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001422 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001423 if (m_dysymtab.nindirectsyms != 0)
1424 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001425 indirect_symbol_index_data.SetData (m_data,
1426 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001427 m_dysymtab.nindirectsyms * 4);
1428 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001429 if (function_starts_load_command.cmd)
1430 {
1431 function_starts_data.SetData (m_data,
1432 function_starts_load_command.dataoff,
1433 function_starts_load_command.datasize);
1434 }
1435 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001436
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001437 if (nlist_data.GetByteSize() == 0)
1438 {
1439 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001440 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001441 return 0;
1442 }
1443
1444
Greg Clayton3a5dc012012-05-25 17:04:00 +00001445 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1446 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001447 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001448 if (process)
1449 {
1450 if (strtab_addr == LLDB_INVALID_ADDRESS)
1451 {
1452 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001453 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001454 return 0;
1455 }
1456 }
1457 else
Greg Claytondd29b972012-05-18 23:20:01 +00001458 {
1459 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001460 module_sp->LogMessage(log, "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001461 return 0;
1462 }
1463 }
Greg Claytondd29b972012-05-18 23:20:01 +00001464
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001465 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1466 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1467 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1468 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1469 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1470 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1471 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1472 SectionSP eh_frame_section_sp;
1473 if (text_section_sp.get())
1474 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1475 else
1476 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1477
Greg Claytond2653c22012-03-14 01:53:24 +00001478 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001479
1480 // lldb works best if it knows the start addresss of all functions in a module.
1481 // Linker symbols or debug info are normally the best source of information for start addr / size but
1482 // they may be stripped in a released binary.
1483 // Two additional sources of information exist in Mach-O binaries:
1484 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1485 // binary, relative to the text section.
1486 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1487 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1488 // Binaries built to run on older releases may need to use eh_frame information.
1489
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001490 if (text_section_sp && function_starts_data.GetByteSize())
1491 {
1492 FunctionStarts::Entry function_start_entry;
1493 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001494 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001495 function_start_entry.addr = text_section_sp->GetFileAddress();
1496 uint64_t delta;
1497 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1498 {
1499 // Now append the current entry
1500 function_start_entry.addr += delta;
1501 function_starts.Append(function_start_entry);
1502 }
Jason Molendad7938392013-03-21 03:36:01 +00001503 }
1504 else
1505 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001506 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1507 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1508 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1509 // the module.
1510 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001511 {
1512 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1513 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1514 eh_frame.GetFunctionAddressAndSizeVector (functions);
1515 addr_t text_base_addr = text_section_sp->GetFileAddress();
1516 size_t count = functions.GetSize();
1517 for (size_t i = 0; i < count; ++i)
1518 {
1519 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1520 if (func)
1521 {
1522 FunctionStarts::Entry function_start_entry;
1523 function_start_entry.addr = func->base - text_base_addr;
1524 function_starts.Append(function_start_entry);
1525 }
1526 }
1527 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001528 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001529
Greg Clayton36da2aa2013-01-25 18:06:21 +00001530 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001531
Greg Clayton36da2aa2013-01-25 18:06:21 +00001532 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 +00001533
Greg Clayton36da2aa2013-01-25 18:06:21 +00001534 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001535
1536 uint32_t N_SO_index = UINT32_MAX;
1537
1538 MachSymtabSectionInfo section_info (section_list);
1539 std::vector<uint32_t> N_FUN_indexes;
1540 std::vector<uint32_t> N_NSYM_indexes;
1541 std::vector<uint32_t> N_INCL_indexes;
1542 std::vector<uint32_t> N_BRAC_indexes;
1543 std::vector<uint32_t> N_COMM_indexes;
1544 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1545 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1546 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1547 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1548 // Any symbols that get merged into another will get an entry
1549 // in this map so we know
1550 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1551 uint32_t nlist_idx = 0;
1552 Symbol *symbol_ptr = NULL;
1553
1554 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001555 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001556 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001557 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001558 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001559
Jason Molendab62abd52012-06-21 01:51:02 +00001560#if defined (__APPLE__) && defined (__arm__)
1561
1562 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1563 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1564 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1565 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1566 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1567 // nlist parser to ignore all LOCAL symbols.
1568
1569 if (m_header.flags & 0x80000000u)
1570 {
1571 // Before we can start mapping the DSC, we need to make certain the target process is actually
1572 // using the cache we can find.
1573
1574 /*
1575 * TODO (FIXME!)
1576 *
1577 * Consider the case of testing with a separate DSC file.
1578 * If we go through the normal code paths, we will give symbols for the wrong DSC, and
1579 * that is bad. We need to read the target process' all_image_infos struct, and look
1580 * at the values of the processDetachedFromSharedRegion field. If that is set, we should skip
1581 * this code section.
1582 */
1583
1584 // Next we need to determine the correct path for the dyld shared cache.
1585
1586 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1587 char dsc_path[PATH_MAX];
1588
1589 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001590 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1591 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001592 header_arch.GetArchitectureName());
1593
1594 FileSpec dsc_filespec(dsc_path, false);
1595
1596 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda6bcabae2013-03-06 23:17:36 +00001597 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001598 {
Jason Molenda6bcabae2013-03-06 23:17:36 +00001599 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1600 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1601 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001602 uint32_t imagesOffset;
1603 uint32_t imagesCount;
1604 uint64_t dyldBaseAddress;
1605 uint64_t codeSignatureOffset;
1606 uint64_t codeSignatureSize;
1607 uint64_t slideInfoOffset;
1608 uint64_t slideInfoSize;
Jason Molenda6bcabae2013-03-06 23:17:36 +00001609 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1610 uint64_t localSymbolsSize; // size of local symbols information
1611 };
1612 struct lldb_copy_dyld_cache_header_v1
1613 {
1614 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1615 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1616 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1617 uint32_t imagesOffset;
1618 uint32_t imagesCount;
1619 uint64_t dyldBaseAddress;
1620 uint64_t codeSignatureOffset;
1621 uint64_t codeSignatureSize;
1622 uint64_t slideInfoOffset;
1623 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001624 uint64_t localSymbolsOffset;
1625 uint64_t localSymbolsSize;
Jason Molenda6bcabae2013-03-06 23:17:36 +00001626 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001627 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001628
1629 struct lldb_copy_dyld_cache_mapping_info
1630 {
1631 uint64_t address;
1632 uint64_t size;
1633 uint64_t fileOffset;
1634 uint32_t maxProt;
1635 uint32_t initProt;
1636 };
1637
Greg Claytonab77dcb2012-09-05 22:30:51 +00001638 struct lldb_copy_dyld_cache_local_symbols_info
1639 {
1640 uint32_t nlistOffset;
1641 uint32_t nlistCount;
1642 uint32_t stringsOffset;
1643 uint32_t stringsSize;
1644 uint32_t entriesOffset;
1645 uint32_t entriesCount;
1646 };
1647 struct lldb_copy_dyld_cache_local_symbols_entry
1648 {
1649 uint32_t dylibOffset;
1650 uint32_t nlistStartIndex;
1651 uint32_t nlistCount;
1652 };
Jason Molendab62abd52012-06-21 01:51:02 +00001653
Jason Molendafd3b35d2012-06-22 03:28:35 +00001654 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1655 The dyld_cache_local_symbols_info structure gives us three things:
1656 1. The start and count of the nlist records in the dyld_shared_cache file
1657 2. The start and size of the strings for these nlist records
1658 3. The start and count of dyld_cache_local_symbols_entry entries
1659
1660 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1661 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001662 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001663 and the count of how many nlist records there are for this dylib/framework.
1664 */
1665
Jason Molendab62abd52012-06-21 01:51:02 +00001666 // Process the dsc header to find the unmapped symbols
1667 //
1668 // Save some VM space, do not map the entire cache in one shot.
1669
Jason Molenda6bcabae2013-03-06 23:17:36 +00001670 DataBufferSP dsc_data_sp;
1671 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1672
1673 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001674 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001675 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001676
Jason Molenda6bcabae2013-03-06 23:17:36 +00001677 char version_str[17];
1678 int version = -1;
1679 lldb::offset_t offset = 0;
1680 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1681 version_str[16] = '\0';
1682 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1683 {
1684 int v;
1685 if (::sscanf (version_str + 6, "%d", &v) == 1)
1686 {
1687 version = v;
1688 }
1689 }
1690
Jason Molenda9badb6c2013-03-06 23:19:17 +00001691 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001692
Jason Molendab62abd52012-06-21 01:51:02 +00001693 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1694
1695 // If the mappingOffset points to a location inside the header, we've
1696 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda6bcabae2013-03-06 23:17:36 +00001697 if ((version == 0 && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
1698 || (version >= 1 && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v1)))
Jason Molendab62abd52012-06-21 01:51:02 +00001699 {
1700
Jason Molenda6bcabae2013-03-06 23:17:36 +00001701 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1702 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1703 offset = 0;
1704
1705 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1706 // in the shared library cache need to be adjusted by an offset to match up with the
1707 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1708 // recorded in mapping_offset_value.
1709 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1710
1711 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001712 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1713 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1714
Jason Molenda9badb6c2013-03-06 23:19:17 +00001715 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001716 {
1717 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001718 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001719 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001720 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001721
1722 offset = 0;
1723
1724 // Read the local_symbols_infos struct in one shot
1725 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1726 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1727
Jason Molendab62abd52012-06-21 01:51:02 +00001728 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1729
Jason Molenda6bcabae2013-03-06 23:17:36 +00001730 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001731
1732 offset = local_symbols_info.entriesOffset;
1733 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1734 {
1735 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1736 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1737 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1738 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1739
Jason Molenda9badb6c2013-03-06 23:19:17 +00001740 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001741 {
1742 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1743
1744 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1745 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1746 num_syms = symtab->GetNumSymbols();
1747
1748 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1749 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1750
Jason Molenda9badb6c2013-03-06 23:19:17 +00001751 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001752 {
1753 /////////////////////////////
1754 {
1755 struct nlist_64 nlist;
1756 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1757 break;
1758
1759 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1760 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1761 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1762 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1763 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1764
1765 SymbolType type = eSymbolTypeInvalid;
1766 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1767
1768 if (symbol_name == NULL)
1769 {
1770 // No symbol should be NULL, even the symbols with no
1771 // string values should have an offset zero which points
1772 // to an empty C-string
1773 Host::SystemLog (Host::eSystemLogError,
1774 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1775 entry_index,
1776 nlist.n_strx,
1777 module_sp->GetFileSpec().GetDirectory().GetCString(),
1778 module_sp->GetFileSpec().GetFilename().GetCString());
1779 continue;
1780 }
1781 if (symbol_name[0] == '\0')
1782 symbol_name = NULL;
1783
1784 const char *symbol_name_non_abi_mangled = NULL;
1785
1786 SectionSP symbol_section;
1787 uint32_t symbol_byte_size = 0;
1788 bool add_nlist = true;
1789 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001790 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001791
1792 assert (sym_idx < num_syms);
1793
1794 sym[sym_idx].SetDebug (is_debug);
1795
1796 if (is_debug)
1797 {
1798 switch (nlist.n_type)
1799 {
1800 case StabGlobalSymbol:
1801 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1802 // Sometimes the N_GSYM value contains the address.
1803
1804 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1805 // have the same address, but we want to ensure that we always find only the real symbol,
1806 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1807 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1808 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1809 // same address.
1810
1811 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1812 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1813 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1814 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1815 add_nlist = false;
1816 else
1817 {
1818 sym[sym_idx].SetExternal(true);
1819 if (nlist.n_value != 0)
1820 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1821 type = eSymbolTypeData;
1822 }
1823 break;
1824
1825 case StabFunctionName:
1826 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1827 type = eSymbolTypeCompiler;
1828 break;
1829
1830 case StabFunction:
1831 // N_FUN -- procedure: name,,n_sect,linenumber,address
1832 if (symbol_name)
1833 {
1834 type = eSymbolTypeCode;
1835 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1836
1837 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1838 // We use the current number of symbols in the symbol table in lieu of
1839 // using nlist_idx in case we ever start trimming entries out
1840 N_FUN_indexes.push_back(sym_idx);
1841 }
1842 else
1843 {
1844 type = eSymbolTypeCompiler;
1845
1846 if ( !N_FUN_indexes.empty() )
1847 {
1848 // Copy the size of the function into the original STAB entry so we don't have
1849 // to hunt for it later
1850 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1851 N_FUN_indexes.pop_back();
1852 // We don't really need the end function STAB as it contains the size which
1853 // we already placed with the original symbol, so don't add it if we want a
1854 // minimal symbol table
1855 if (minimize)
1856 add_nlist = false;
1857 }
1858 }
1859 break;
1860
1861 case StabStaticSymbol:
1862 // N_STSYM -- static symbol: name,,n_sect,type,address
1863 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1864 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1865 type = eSymbolTypeData;
1866 break;
1867
1868 case StabLocalCommon:
1869 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1870 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1871 type = eSymbolTypeCommonBlock;
1872 break;
1873
1874 case StabBeginSymbol:
1875 // N_BNSYM
1876 // We use the current number of symbols in the symbol table in lieu of
1877 // using nlist_idx in case we ever start trimming entries out
1878 if (minimize)
1879 {
1880 // Skip these if we want minimal symbol tables
1881 add_nlist = false;
1882 }
1883 else
1884 {
1885 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1886 N_NSYM_indexes.push_back(sym_idx);
1887 type = eSymbolTypeScopeBegin;
1888 }
1889 break;
1890
1891 case StabEndSymbol:
1892 // N_ENSYM
1893 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1894 // so that we can always skip the entire symbol if we need to navigate
1895 // more quickly at the source level when parsing STABS
1896 if (minimize)
1897 {
1898 // Skip these if we want minimal symbol tables
1899 add_nlist = false;
1900 }
1901 else
1902 {
1903 if ( !N_NSYM_indexes.empty() )
1904 {
1905 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1906 symbol_ptr->SetByteSize(sym_idx + 1);
1907 symbol_ptr->SetSizeIsSibling(true);
1908 N_NSYM_indexes.pop_back();
1909 }
1910 type = eSymbolTypeScopeEnd;
1911 }
1912 break;
1913
1914
1915 case StabSourceFileOptions:
1916 // N_OPT - emitted with gcc2_compiled and in gcc source
1917 type = eSymbolTypeCompiler;
1918 break;
1919
1920 case StabRegisterSymbol:
1921 // N_RSYM - register sym: name,,NO_SECT,type,register
1922 type = eSymbolTypeVariable;
1923 break;
1924
1925 case StabSourceLine:
1926 // N_SLINE - src line: 0,,n_sect,linenumber,address
1927 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1928 type = eSymbolTypeLineEntry;
1929 break;
1930
1931 case StabStructureType:
1932 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1933 type = eSymbolTypeVariableType;
1934 break;
1935
1936 case StabSourceFileName:
1937 // N_SO - source file name
1938 type = eSymbolTypeSourceFile;
1939 if (symbol_name == NULL)
1940 {
1941 if (minimize)
1942 add_nlist = false;
1943 if (N_SO_index != UINT32_MAX)
1944 {
1945 // Set the size of the N_SO to the terminating index of this N_SO
1946 // so that we can always skip the entire N_SO if we need to navigate
1947 // more quickly at the source level when parsing STABS
1948 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1949 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1950 symbol_ptr->SetSizeIsSibling(true);
1951 }
1952 N_NSYM_indexes.clear();
1953 N_INCL_indexes.clear();
1954 N_BRAC_indexes.clear();
1955 N_COMM_indexes.clear();
1956 N_FUN_indexes.clear();
1957 N_SO_index = UINT32_MAX;
1958 }
1959 else
1960 {
1961 // We use the current number of symbols in the symbol table in lieu of
1962 // using nlist_idx in case we ever start trimming entries out
1963 const bool N_SO_has_full_path = symbol_name[0] == '/';
1964 if (N_SO_has_full_path)
1965 {
1966 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1967 {
1968 // We have two consecutive N_SO entries where the first contains a directory
1969 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00001970 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001971 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1972 add_nlist = false;
1973 }
1974 else
1975 {
1976 // This is the first entry in a N_SO that contains a directory or
1977 // a full path to the source file
1978 N_SO_index = sym_idx;
1979 }
1980 }
1981 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1982 {
1983 // This is usually the second N_SO entry that contains just the filename,
1984 // so here we combine it with the first one if we are minimizing the symbol table
1985 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1986 if (so_path && so_path[0])
1987 {
1988 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00001989 const size_t double_slash_pos = full_so_path.find("//");
1990 if (double_slash_pos != std::string::npos)
1991 {
1992 // The linker has been generating bad N_SO entries with doubled up paths
1993 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
1994 // and the second is the directory for the source file so you end up with
1995 // a path that looks like "/tmp/src//tmp/src/"
1996 FileSpec so_dir(so_path, false);
1997 if (!so_dir.Exists())
1998 {
1999 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2000 if (so_dir.Exists())
2001 {
2002 // Trim off the incorrect path
2003 full_so_path.erase(0, double_slash_pos + 1);
2004 }
2005 }
2006 }
Jason Molendab62abd52012-06-21 01:51:02 +00002007 if (*full_so_path.rbegin() != '/')
2008 full_so_path += '/';
2009 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00002010 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002011 add_nlist = false;
2012 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2013 }
2014 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002015 else
2016 {
2017 // This could be a relative path to a N_SO
2018 N_SO_index = sym_idx;
2019 }
Jason Molendab62abd52012-06-21 01:51:02 +00002020 }
Jason Molendab62abd52012-06-21 01:51:02 +00002021 break;
2022
2023 case StabObjectFileName:
2024 // N_OSO - object file name: name,,0,0,st_mtime
2025 type = eSymbolTypeObjectFile;
2026 break;
2027
2028 case StabLocalSymbol:
2029 // N_LSYM - local sym: name,,NO_SECT,type,offset
2030 type = eSymbolTypeLocal;
2031 break;
2032
2033 //----------------------------------------------------------------------
2034 // INCL scopes
2035 //----------------------------------------------------------------------
2036 case StabBeginIncludeFileName:
2037 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2038 // We use the current number of symbols in the symbol table in lieu of
2039 // using nlist_idx in case we ever start trimming entries out
2040 N_INCL_indexes.push_back(sym_idx);
2041 type = eSymbolTypeScopeBegin;
2042 break;
2043
2044 case StabEndIncludeFile:
2045 // N_EINCL - include file end: name,,NO_SECT,0,0
2046 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2047 // so that we can always skip the entire symbol if we need to navigate
2048 // more quickly at the source level when parsing STABS
2049 if ( !N_INCL_indexes.empty() )
2050 {
2051 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2052 symbol_ptr->SetByteSize(sym_idx + 1);
2053 symbol_ptr->SetSizeIsSibling(true);
2054 N_INCL_indexes.pop_back();
2055 }
2056 type = eSymbolTypeScopeEnd;
2057 break;
2058
2059 case StabIncludeFileName:
2060 // N_SOL - #included file name: name,,n_sect,0,address
2061 type = eSymbolTypeHeaderFile;
2062
2063 // We currently don't use the header files on darwin
2064 if (minimize)
2065 add_nlist = false;
2066 break;
2067
2068 case StabCompilerParameters:
2069 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2070 type = eSymbolTypeCompiler;
2071 break;
2072
2073 case StabCompilerVersion:
2074 // N_VERSION - compiler version: name,,NO_SECT,0,0
2075 type = eSymbolTypeCompiler;
2076 break;
2077
2078 case StabCompilerOptLevel:
2079 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2080 type = eSymbolTypeCompiler;
2081 break;
2082
2083 case StabParameter:
2084 // N_PSYM - parameter: name,,NO_SECT,type,offset
2085 type = eSymbolTypeVariable;
2086 break;
2087
2088 case StabAlternateEntry:
2089 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2090 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2091 type = eSymbolTypeLineEntry;
2092 break;
2093
2094 //----------------------------------------------------------------------
2095 // Left and Right Braces
2096 //----------------------------------------------------------------------
2097 case StabLeftBracket:
2098 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2099 // We use the current number of symbols in the symbol table in lieu of
2100 // using nlist_idx in case we ever start trimming entries out
2101 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2102 N_BRAC_indexes.push_back(sym_idx);
2103 type = eSymbolTypeScopeBegin;
2104 break;
2105
2106 case StabRightBracket:
2107 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2108 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2109 // so that we can always skip the entire symbol if we need to navigate
2110 // more quickly at the source level when parsing STABS
2111 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2112 if ( !N_BRAC_indexes.empty() )
2113 {
2114 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2115 symbol_ptr->SetByteSize(sym_idx + 1);
2116 symbol_ptr->SetSizeIsSibling(true);
2117 N_BRAC_indexes.pop_back();
2118 }
2119 type = eSymbolTypeScopeEnd;
2120 break;
2121
2122 case StabDeletedIncludeFile:
2123 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2124 type = eSymbolTypeHeaderFile;
2125 break;
2126
2127 //----------------------------------------------------------------------
2128 // COMM scopes
2129 //----------------------------------------------------------------------
2130 case StabBeginCommon:
2131 // N_BCOMM - begin common: name,,NO_SECT,0,0
2132 // We use the current number of symbols in the symbol table in lieu of
2133 // using nlist_idx in case we ever start trimming entries out
2134 type = eSymbolTypeScopeBegin;
2135 N_COMM_indexes.push_back(sym_idx);
2136 break;
2137
2138 case StabEndCommonLocal:
2139 // N_ECOML - end common (local name): 0,,n_sect,0,address
2140 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2141 // Fall through
2142
2143 case StabEndCommon:
2144 // N_ECOMM - end common: name,,n_sect,0,0
2145 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2146 // so that we can always skip the entire symbol if we need to navigate
2147 // more quickly at the source level when parsing STABS
2148 if ( !N_COMM_indexes.empty() )
2149 {
2150 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2151 symbol_ptr->SetByteSize(sym_idx + 1);
2152 symbol_ptr->SetSizeIsSibling(true);
2153 N_COMM_indexes.pop_back();
2154 }
2155 type = eSymbolTypeScopeEnd;
2156 break;
2157
2158 case StabLength:
2159 // N_LENG - second stab entry with length information
2160 type = eSymbolTypeAdditional;
2161 break;
2162
2163 default: break;
2164 }
2165 }
2166 else
2167 {
2168 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2169 uint8_t n_type = NlistMaskType & nlist.n_type;
2170 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2171
2172 switch (n_type)
2173 {
2174 case NListTypeIndirect: // N_INDR - Fall through
2175 case NListTypePreboundUndefined:// N_PBUD - Fall through
2176 case NListTypeUndefined: // N_UNDF
2177 type = eSymbolTypeUndefined;
2178 break;
2179
2180 case NListTypeAbsolute: // N_ABS
2181 type = eSymbolTypeAbsolute;
2182 break;
2183
2184 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002185 {
2186 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002187
Greg Clayton01e6a582012-11-27 01:52:16 +00002188 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002189 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002190 // TODO: warn about this?
2191 add_nlist = false;
2192 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002193 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002194
2195 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002196 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002197 type = eSymbolTypeException;
2198 }
2199 else
2200 {
2201 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002202
Greg Clayton01e6a582012-11-27 01:52:16 +00002203 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002204 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002205 case SectionTypeRegular: break; // regular section
2206 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2207 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2208 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2209 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2210 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2211 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2212 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2213 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2214 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2215 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2216 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2217 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2218 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2219 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2220 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2221 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2222 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002223 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002224
Greg Clayton01e6a582012-11-27 01:52:16 +00002225 if (type == eSymbolTypeInvalid)
2226 {
2227 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2228 if (symbol_section->IsDescendant (text_section_sp.get()))
2229 {
2230 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2231 SectionAttrUserSelfModifyingCode |
2232 SectionAttrSytemSomeInstructions))
2233 type = eSymbolTypeData;
2234 else
2235 type = eSymbolTypeCode;
2236 }
2237 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002238 {
2239 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2240 {
2241 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002242
Greg Clayton01e6a582012-11-27 01:52:16 +00002243 if (symbol_name &&
2244 symbol_name[0] == '_' &&
2245 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002246 symbol_name[2] == 'B')
2247 {
2248 llvm::StringRef symbol_name_ref(symbol_name);
2249 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2250 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2251 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2252 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2253 {
2254 symbol_name_non_abi_mangled = symbol_name + 1;
2255 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2256 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002257 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002258 }
2259 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2260 {
2261 symbol_name_non_abi_mangled = symbol_name + 1;
2262 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2263 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002264 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002265 }
2266 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2267 {
2268 symbol_name_non_abi_mangled = symbol_name + 1;
2269 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2270 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002271 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002272 }
2273 }
2274 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002275 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002276 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002277 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002278 }
2279 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002280 {
2281 type = eSymbolTypeData;
2282 }
2283 }
2284 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2285 {
2286 type = eSymbolTypeTrampoline;
2287 }
2288 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2289 {
2290 type = eSymbolTypeRuntime;
2291 if (symbol_name && symbol_name[0] == '.')
2292 {
2293 llvm::StringRef symbol_name_ref(symbol_name);
2294 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2295 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002296 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002297 symbol_name_non_abi_mangled = symbol_name;
2298 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2299 type = eSymbolTypeObjCClass;
2300 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002301 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002302 }
2303 }
2304 }
Jason Molendab62abd52012-06-21 01:51:02 +00002305 }
2306 }
Jason Molendab62abd52012-06-21 01:51:02 +00002307 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002308 }
Jason Molendab62abd52012-06-21 01:51:02 +00002309 }
2310
2311 if (add_nlist)
2312 {
2313 uint64_t symbol_value = nlist.n_value;
2314 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002315
Jason Molendab62abd52012-06-21 01:51:02 +00002316 if (symbol_name_non_abi_mangled)
2317 {
Jason Molenda292cca82012-07-20 03:35:44 +00002318 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2319 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002320 }
2321 else
2322 {
2323 if (symbol_name && symbol_name[0] == '_')
2324 {
2325 symbol_name_is_mangled = symbol_name[1] == '_';
2326 symbol_name++; // Skip the leading underscore
2327 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002328
Jason Molendab62abd52012-06-21 01:51:02 +00002329 if (symbol_name)
2330 {
Jason Molenda292cca82012-07-20 03:35:44 +00002331 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002332 }
2333 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002334
Jason Molendab62abd52012-06-21 01:51:02 +00002335 if (is_debug == false)
2336 {
2337 if (type == eSymbolTypeCode)
2338 {
2339 // See if we can find a N_FUN entry for any code symbols.
2340 // If we do find a match, and the name matches, then we
2341 // can merge the two into just the function symbol to avoid
2342 // duplicate entries in the symbol table
2343 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2344 if (pos != N_FUN_addr_to_sym_idx.end())
2345 {
2346 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2347 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2348 {
2349 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2350 // We just need the flags from the linker symbol, so put these flags
2351 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2352 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2353 sym[sym_idx].Clear();
2354 continue;
2355 }
2356 }
2357 }
2358 else if (type == eSymbolTypeData)
2359 {
2360 // See if we can find a N_STSYM entry for any data symbols.
2361 // If we do find a match, and the name matches, then we
2362 // can merge the two into just the Static symbol to avoid
2363 // duplicate entries in the symbol table
2364 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2365 if (pos != N_STSYM_addr_to_sym_idx.end())
2366 {
2367 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2368 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2369 {
2370 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2371 // We just need the flags from the linker symbol, so put these flags
2372 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2373 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2374 sym[sym_idx].Clear();
2375 continue;
2376 }
2377 }
2378 }
2379 }
2380 if (symbol_section)
2381 {
2382 const addr_t section_file_addr = symbol_section->GetFileAddress();
2383 if (symbol_byte_size == 0 && function_starts_count > 0)
2384 {
2385 addr_t symbol_lookup_file_addr = nlist.n_value;
2386 // Do an exact address match for non-ARM addresses, else get the closest since
2387 // the symbol might be a thumb symbol which has an address with bit zero set
2388 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2389 if (is_arm && func_start_entry)
2390 {
2391 // Verify that the function start address is the symbol address (ARM)
2392 // or the symbol address + 1 (thumb)
2393 if (func_start_entry->addr != symbol_lookup_file_addr &&
2394 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2395 {
2396 // Not the right entry, NULL it out...
2397 func_start_entry = NULL;
2398 }
2399 }
2400 if (func_start_entry)
2401 {
2402 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002403
Jason Molendab62abd52012-06-21 01:51:02 +00002404 addr_t symbol_file_addr = func_start_entry->addr;
2405 uint32_t symbol_flags = 0;
2406 if (is_arm)
2407 {
2408 if (symbol_file_addr & 1)
2409 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2410 symbol_file_addr &= 0xfffffffffffffffeull;
2411 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002412
Jason Molendab62abd52012-06-21 01:51:02 +00002413 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2414 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2415 if (next_func_start_entry)
2416 {
2417 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2418 // Be sure the clear the Thumb address bit when we calculate the size
2419 // from the current and next address
2420 if (is_arm)
2421 next_symbol_file_addr &= 0xfffffffffffffffeull;
2422 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2423 }
2424 else
2425 {
2426 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2427 }
2428 }
2429 }
2430 symbol_value -= section_file_addr;
2431 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002432
Jason Molendab62abd52012-06-21 01:51:02 +00002433 sym[sym_idx].SetID (nlist_idx);
2434 sym[sym_idx].SetType (type);
2435 sym[sym_idx].GetAddress().SetSection (symbol_section);
2436 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2437 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002438
Jason Molendab62abd52012-06-21 01:51:02 +00002439 if (symbol_byte_size > 0)
2440 sym[sym_idx].SetByteSize(symbol_byte_size);
2441
Greg Clayton01e6a582012-11-27 01:52:16 +00002442 if (demangled_is_synthesized)
2443 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002444 ++sym_idx;
2445 }
2446 else
2447 {
2448 sym[sym_idx].Clear();
2449 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002450
Jason Molendab62abd52012-06-21 01:51:02 +00002451 }
2452 /////////////////////////////
2453 }
2454 break; // No more entries to consider
2455 }
2456 }
2457 }
2458 }
2459 }
2460 }
2461 }
2462
2463 // Must reset this in case it was mutated above!
2464 nlist_data_offset = 0;
2465#endif
2466
2467 // If the sym array was not created while parsing the DSC unmapped
2468 // symbols, create it now.
2469 if (sym == NULL)
2470 {
2471 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2472 num_syms = symtab->GetNumSymbols();
2473 }
2474
2475 if (unmapped_local_symbols_found)
2476 {
2477 assert(m_dysymtab.ilocalsym == 0);
2478 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2479 nlist_idx = m_dysymtab.nlocalsym;
2480 }
2481 else
2482 {
2483 nlist_idx = 0;
2484 }
2485
2486 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002487 {
2488 struct nlist_64 nlist;
2489 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2490 break;
2491
2492 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2493 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2494 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2495 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2496 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2497
2498 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002499 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002500
Greg Clayton3a5dc012012-05-25 17:04:00 +00002501 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002502 {
2503 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002504
Greg Claytondd29b972012-05-18 23:20:01 +00002505 if (symbol_name == NULL)
2506 {
2507 // No symbol should be NULL, even the symbols with no
2508 // string values should have an offset zero which points
2509 // to an empty C-string
2510 Host::SystemLog (Host::eSystemLogError,
Jason Molenda9badb6c2013-03-06 23:19:17 +00002511 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002512 nlist_idx,
2513 nlist.n_strx,
2514 module_sp->GetFileSpec().GetDirectory().GetCString(),
2515 module_sp->GetFileSpec().GetFilename().GetCString());
2516 continue;
2517 }
2518 if (symbol_name[0] == '\0')
2519 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002520 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002521 else
2522 {
2523 const addr_t str_addr = strtab_addr + nlist.n_strx;
2524 Error str_error;
2525 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2526 symbol_name = memory_symbol_name.c_str();
2527 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002528 const char *symbol_name_non_abi_mangled = NULL;
2529
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002530 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002531 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002532 bool add_nlist = true;
2533 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002534 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002535
2536 assert (sym_idx < num_syms);
2537
2538 sym[sym_idx].SetDebug (is_debug);
2539
2540 if (is_debug)
2541 {
2542 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002543 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002544 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002545 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2546 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002547
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002548 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2549 // have the same address, but we want to ensure that we always find only the real symbol,
2550 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2551 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2552 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2553 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002554
2555 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002556 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2557 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2558 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2559 add_nlist = false;
2560 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002561 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002562 sym[sym_idx].SetExternal(true);
2563 if (nlist.n_value != 0)
2564 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2565 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002566 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002567 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002568
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002569 case StabFunctionName:
2570 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2571 type = eSymbolTypeCompiler;
2572 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002573
Jason Molenda9badb6c2013-03-06 23:19:17 +00002574 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002575 // N_FUN -- procedure: name,,n_sect,linenumber,address
2576 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002577 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002578 type = eSymbolTypeCode;
2579 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002580
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002581 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2582 // We use the current number of symbols in the symbol table in lieu of
2583 // using nlist_idx in case we ever start trimming entries out
2584 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002585 }
2586 else
2587 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002588 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002589
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002590 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002591 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002592 // Copy the size of the function into the original STAB entry so we don't have
2593 // to hunt for it later
2594 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2595 N_FUN_indexes.pop_back();
2596 // We don't really need the end function STAB as it contains the size which
2597 // we already placed with the original symbol, so don't add it if we want a
2598 // minimal symbol table
2599 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002600 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002601 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002602 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002603 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002604
Jason Molenda9badb6c2013-03-06 23:19:17 +00002605 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002606 // N_STSYM -- static symbol: name,,n_sect,type,address
2607 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2608 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2609 type = eSymbolTypeData;
2610 break;
2611
2612 case StabLocalCommon:
2613 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2614 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2615 type = eSymbolTypeCommonBlock;
2616 break;
2617
2618 case StabBeginSymbol:
2619 // N_BNSYM
2620 // We use the current number of symbols in the symbol table in lieu of
2621 // using nlist_idx in case we ever start trimming entries out
2622 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002623 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002624 // Skip these if we want minimal symbol tables
2625 add_nlist = false;
2626 }
2627 else
2628 {
2629 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2630 N_NSYM_indexes.push_back(sym_idx);
2631 type = eSymbolTypeScopeBegin;
2632 }
2633 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002634
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002635 case StabEndSymbol:
2636 // N_ENSYM
2637 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2638 // so that we can always skip the entire symbol if we need to navigate
2639 // more quickly at the source level when parsing STABS
2640 if (minimize)
2641 {
2642 // Skip these if we want minimal symbol tables
2643 add_nlist = false;
2644 }
2645 else
2646 {
2647 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002648 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002649 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2650 symbol_ptr->SetByteSize(sym_idx + 1);
2651 symbol_ptr->SetSizeIsSibling(true);
2652 N_NSYM_indexes.pop_back();
2653 }
2654 type = eSymbolTypeScopeEnd;
2655 }
2656 break;
2657
2658
2659 case StabSourceFileOptions:
2660 // N_OPT - emitted with gcc2_compiled and in gcc source
2661 type = eSymbolTypeCompiler;
2662 break;
2663
2664 case StabRegisterSymbol:
2665 // N_RSYM - register sym: name,,NO_SECT,type,register
2666 type = eSymbolTypeVariable;
2667 break;
2668
2669 case StabSourceLine:
2670 // N_SLINE - src line: 0,,n_sect,linenumber,address
2671 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2672 type = eSymbolTypeLineEntry;
2673 break;
2674
2675 case StabStructureType:
2676 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2677 type = eSymbolTypeVariableType;
2678 break;
2679
2680 case StabSourceFileName:
2681 // N_SO - source file name
2682 type = eSymbolTypeSourceFile;
2683 if (symbol_name == NULL)
2684 {
2685 if (minimize)
2686 add_nlist = false;
2687 if (N_SO_index != UINT32_MAX)
2688 {
2689 // Set the size of the N_SO to the terminating index of this N_SO
2690 // so that we can always skip the entire N_SO if we need to navigate
2691 // more quickly at the source level when parsing STABS
2692 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2693 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2694 symbol_ptr->SetSizeIsSibling(true);
2695 }
2696 N_NSYM_indexes.clear();
2697 N_INCL_indexes.clear();
2698 N_BRAC_indexes.clear();
2699 N_COMM_indexes.clear();
2700 N_FUN_indexes.clear();
2701 N_SO_index = UINT32_MAX;
2702 }
2703 else
2704 {
2705 // We use the current number of symbols in the symbol table in lieu of
2706 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002707 const bool N_SO_has_full_path = symbol_name[0] == '/';
2708 if (N_SO_has_full_path)
2709 {
2710 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2711 {
2712 // We have two consecutive N_SO entries where the first contains a directory
2713 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002714 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002715 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2716 add_nlist = false;
2717 }
2718 else
2719 {
2720 // This is the first entry in a N_SO that contains a directory or
2721 // a full path to the source file
2722 N_SO_index = sym_idx;
2723 }
2724 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002725 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2726 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002727 // This is usually the second N_SO entry that contains just the filename,
2728 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002729 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2730 if (so_path && so_path[0])
2731 {
2732 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002733 const size_t double_slash_pos = full_so_path.find("//");
2734 if (double_slash_pos != std::string::npos)
2735 {
2736 // The linker has been generating bad N_SO entries with doubled up paths
2737 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2738 // and the second is the directory for the source file so you end up with
2739 // a path that looks like "/tmp/src//tmp/src/"
2740 FileSpec so_dir(so_path, false);
2741 if (!so_dir.Exists())
2742 {
2743 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2744 if (so_dir.Exists())
2745 {
2746 // Trim off the incorrect path
2747 full_so_path.erase(0, double_slash_pos + 1);
2748 }
2749 }
2750 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002751 if (*full_so_path.rbegin() != '/')
2752 full_so_path += '/';
2753 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002754 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002755 add_nlist = false;
2756 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2757 }
2758 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002759 else
2760 {
2761 // This could be a relative path to a N_SO
2762 N_SO_index = sym_idx;
2763 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002764 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002765
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002766 break;
2767
2768 case StabObjectFileName:
2769 // N_OSO - object file name: name,,0,0,st_mtime
2770 type = eSymbolTypeObjectFile;
2771 break;
2772
2773 case StabLocalSymbol:
2774 // N_LSYM - local sym: name,,NO_SECT,type,offset
2775 type = eSymbolTypeLocal;
2776 break;
2777
2778 //----------------------------------------------------------------------
2779 // INCL scopes
2780 //----------------------------------------------------------------------
2781 case StabBeginIncludeFileName:
2782 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2783 // We use the current number of symbols in the symbol table in lieu of
2784 // using nlist_idx in case we ever start trimming entries out
2785 N_INCL_indexes.push_back(sym_idx);
2786 type = eSymbolTypeScopeBegin;
2787 break;
2788
2789 case StabEndIncludeFile:
2790 // N_EINCL - include file end: name,,NO_SECT,0,0
2791 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2792 // so that we can always skip the entire symbol if we need to navigate
2793 // more quickly at the source level when parsing STABS
2794 if ( !N_INCL_indexes.empty() )
2795 {
2796 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2797 symbol_ptr->SetByteSize(sym_idx + 1);
2798 symbol_ptr->SetSizeIsSibling(true);
2799 N_INCL_indexes.pop_back();
2800 }
2801 type = eSymbolTypeScopeEnd;
2802 break;
2803
2804 case StabIncludeFileName:
2805 // N_SOL - #included file name: name,,n_sect,0,address
2806 type = eSymbolTypeHeaderFile;
2807
2808 // We currently don't use the header files on darwin
2809 if (minimize)
2810 add_nlist = false;
2811 break;
2812
Jason Molenda9badb6c2013-03-06 23:19:17 +00002813 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002814 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2815 type = eSymbolTypeCompiler;
2816 break;
2817
2818 case StabCompilerVersion:
2819 // N_VERSION - compiler version: name,,NO_SECT,0,0
2820 type = eSymbolTypeCompiler;
2821 break;
2822
2823 case StabCompilerOptLevel:
2824 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2825 type = eSymbolTypeCompiler;
2826 break;
2827
2828 case StabParameter:
2829 // N_PSYM - parameter: name,,NO_SECT,type,offset
2830 type = eSymbolTypeVariable;
2831 break;
2832
2833 case StabAlternateEntry:
2834 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2835 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2836 type = eSymbolTypeLineEntry;
2837 break;
2838
2839 //----------------------------------------------------------------------
2840 // Left and Right Braces
2841 //----------------------------------------------------------------------
2842 case StabLeftBracket:
2843 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2844 // We use the current number of symbols in the symbol table in lieu of
2845 // using nlist_idx in case we ever start trimming entries out
2846 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2847 N_BRAC_indexes.push_back(sym_idx);
2848 type = eSymbolTypeScopeBegin;
2849 break;
2850
2851 case StabRightBracket:
2852 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2853 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2854 // so that we can always skip the entire symbol if we need to navigate
2855 // more quickly at the source level when parsing STABS
2856 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2857 if ( !N_BRAC_indexes.empty() )
2858 {
2859 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2860 symbol_ptr->SetByteSize(sym_idx + 1);
2861 symbol_ptr->SetSizeIsSibling(true);
2862 N_BRAC_indexes.pop_back();
2863 }
2864 type = eSymbolTypeScopeEnd;
2865 break;
2866
2867 case StabDeletedIncludeFile:
2868 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2869 type = eSymbolTypeHeaderFile;
2870 break;
2871
2872 //----------------------------------------------------------------------
2873 // COMM scopes
2874 //----------------------------------------------------------------------
2875 case StabBeginCommon:
2876 // N_BCOMM - begin common: name,,NO_SECT,0,0
2877 // We use the current number of symbols in the symbol table in lieu of
2878 // using nlist_idx in case we ever start trimming entries out
2879 type = eSymbolTypeScopeBegin;
2880 N_COMM_indexes.push_back(sym_idx);
2881 break;
2882
2883 case StabEndCommonLocal:
2884 // N_ECOML - end common (local name): 0,,n_sect,0,address
2885 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2886 // Fall through
2887
2888 case StabEndCommon:
2889 // N_ECOMM - end common: name,,n_sect,0,0
2890 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2891 // so that we can always skip the entire symbol if we need to navigate
2892 // more quickly at the source level when parsing STABS
2893 if ( !N_COMM_indexes.empty() )
2894 {
2895 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2896 symbol_ptr->SetByteSize(sym_idx + 1);
2897 symbol_ptr->SetSizeIsSibling(true);
2898 N_COMM_indexes.pop_back();
2899 }
2900 type = eSymbolTypeScopeEnd;
2901 break;
2902
2903 case StabLength:
2904 // N_LENG - second stab entry with length information
2905 type = eSymbolTypeAdditional;
2906 break;
2907
2908 default: break;
2909 }
2910 }
2911 else
2912 {
2913 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2914 uint8_t n_type = NlistMaskType & nlist.n_type;
2915 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2916
2917 switch (n_type)
2918 {
2919 case NListTypeIndirect: // N_INDR - Fall through
2920 case NListTypePreboundUndefined:// N_PBUD - Fall through
2921 case NListTypeUndefined: // N_UNDF
2922 type = eSymbolTypeUndefined;
2923 break;
2924
2925 case NListTypeAbsolute: // N_ABS
2926 type = eSymbolTypeAbsolute;
2927 break;
2928
2929 case NListTypeSection: // N_SECT
2930 {
2931 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2932
Sean Callananb386d822012-08-09 00:50:26 +00002933 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002934 {
2935 // TODO: warn about this?
2936 add_nlist = false;
2937 break;
2938 }
2939
2940 if (TEXT_eh_frame_sectID == nlist.n_sect)
2941 {
2942 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00002943 }
2944 else
2945 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002946 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2947
2948 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00002949 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002950 case SectionTypeRegular: break; // regular section
2951 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2952 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2953 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2954 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2955 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2956 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2957 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2958 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2959 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2960 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2961 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2962 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2963 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2964 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2965 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2966 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2967 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002968 }
Chris Lattner24943d22010-06-08 16:52:24 +00002969
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002970 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002971 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002972 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2973 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002974 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002975 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2976 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002977 SectionAttrSytemSomeInstructions))
2978 type = eSymbolTypeData;
2979 else
2980 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00002981 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002982 else
2983 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002984 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002985 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00002986 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002987 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00002988
Jason Molenda9badb6c2013-03-06 23:19:17 +00002989 if (symbol_name &&
2990 symbol_name[0] == '_' &&
2991 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002992 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00002993 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002994 llvm::StringRef symbol_name_ref(symbol_name);
2995 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2996 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2997 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2998 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00002999 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003000 symbol_name_non_abi_mangled = symbol_name + 1;
3001 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3002 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003003 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003004 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003005 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003006 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003007 symbol_name_non_abi_mangled = symbol_name + 1;
3008 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3009 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003010 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003011 }
3012 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3013 {
3014 symbol_name_non_abi_mangled = symbol_name + 1;
3015 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3016 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003017 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003018 }
3019 }
3020 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003021 else
3022 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3023 {
3024 type = eSymbolTypeException;
3025 }
3026 else
3027 {
3028 type = eSymbolTypeData;
3029 }
3030 }
3031 else
3032 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3033 {
3034 type = eSymbolTypeTrampoline;
3035 }
3036 else
3037 if (symbol_section->IsDescendant(objc_section_sp.get()))
3038 {
3039 type = eSymbolTypeRuntime;
3040 if (symbol_name && symbol_name[0] == '.')
3041 {
3042 llvm::StringRef symbol_name_ref(symbol_name);
3043 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3044 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3045 {
3046 symbol_name_non_abi_mangled = symbol_name;
3047 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3048 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003049 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003050 }
3051 }
Chris Lattner24943d22010-06-08 16:52:24 +00003052 }
3053 }
3054 }
3055 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003056 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003057 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003058 }
3059
3060 if (add_nlist)
3061 {
3062 uint64_t symbol_value = nlist.n_value;
3063 bool symbol_name_is_mangled = false;
3064
3065 if (symbol_name_non_abi_mangled)
3066 {
Greg Claytonc0240042012-07-18 23:18:10 +00003067 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3068 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003069 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003070 else
3071 {
3072 if (symbol_name && symbol_name[0] == '_')
3073 {
3074 symbol_name_is_mangled = symbol_name[1] == '_';
3075 symbol_name++; // Skip the leading underscore
3076 }
3077
3078 if (symbol_name)
3079 {
Greg Claytonc0240042012-07-18 23:18:10 +00003080 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003081 }
3082 }
3083
3084 if (is_debug == false)
3085 {
3086 if (type == eSymbolTypeCode)
3087 {
3088 // See if we can find a N_FUN entry for any code symbols.
3089 // If we do find a match, and the name matches, then we
3090 // can merge the two into just the function symbol to avoid
3091 // duplicate entries in the symbol table
3092 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3093 if (pos != N_FUN_addr_to_sym_idx.end())
3094 {
3095 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3096 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3097 {
3098 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3099 // We just need the flags from the linker symbol, so put these flags
3100 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3101 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3102 sym[sym_idx].Clear();
3103 continue;
3104 }
3105 }
3106 }
3107 else if (type == eSymbolTypeData)
3108 {
3109 // See if we can find a N_STSYM entry for any data symbols.
3110 // If we do find a match, and the name matches, then we
3111 // can merge the two into just the Static symbol to avoid
3112 // duplicate entries in the symbol table
3113 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3114 if (pos != N_STSYM_addr_to_sym_idx.end())
3115 {
3116 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3117 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3118 {
3119 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3120 // We just need the flags from the linker symbol, so put these flags
3121 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3122 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3123 sym[sym_idx].Clear();
3124 continue;
3125 }
3126 }
3127 }
3128 }
3129 if (symbol_section)
3130 {
3131 const addr_t section_file_addr = symbol_section->GetFileAddress();
3132 if (symbol_byte_size == 0 && function_starts_count > 0)
3133 {
Greg Claytond2653c22012-03-14 01:53:24 +00003134 addr_t symbol_lookup_file_addr = nlist.n_value;
3135 // Do an exact address match for non-ARM addresses, else get the closest since
3136 // the symbol might be a thumb symbol which has an address with bit zero set
3137 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3138 if (is_arm && func_start_entry)
3139 {
3140 // Verify that the function start address is the symbol address (ARM)
3141 // or the symbol address + 1 (thumb)
3142 if (func_start_entry->addr != symbol_lookup_file_addr &&
3143 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3144 {
3145 // Not the right entry, NULL it out...
3146 func_start_entry = NULL;
3147 }
3148 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003149 if (func_start_entry)
3150 {
3151 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003152
Greg Claytond2653c22012-03-14 01:53:24 +00003153 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003154 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003155 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003156
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003157 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3158 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3159 if (next_func_start_entry)
3160 {
Greg Claytond2653c22012-03-14 01:53:24 +00003161 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3162 // Be sure the clear the Thumb address bit when we calculate the size
3163 // from the current and next address
3164 if (is_arm)
3165 next_symbol_file_addr &= 0xfffffffffffffffeull;
3166 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 +00003167 }
3168 else
3169 {
Greg Claytond2653c22012-03-14 01:53:24 +00003170 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003171 }
3172 }
3173 }
3174 symbol_value -= section_file_addr;
3175 }
3176
3177 sym[sym_idx].SetID (nlist_idx);
3178 sym[sym_idx].SetType (type);
3179 sym[sym_idx].GetAddress().SetSection (symbol_section);
3180 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3181 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3182
3183 if (symbol_byte_size > 0)
3184 sym[sym_idx].SetByteSize(symbol_byte_size);
3185
Greg Clayton01e6a582012-11-27 01:52:16 +00003186 if (demangled_is_synthesized)
3187 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3188
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003189 ++sym_idx;
3190 }
3191 else
3192 {
3193 sym[sym_idx].Clear();
3194 }
3195
3196 }
3197
3198 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3199 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3200 // such entries by figuring out what the address for the global is by looking up this non-STAB
3201 // entry and copying the value into the debug symbol's value to save us the hassle in the
3202 // debug symbol parser.
3203
3204 Symbol *global_symbol = NULL;
3205 for (nlist_idx = 0;
3206 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3207 nlist_idx++)
3208 {
3209 if (global_symbol->GetAddress().GetFileAddress() == 0)
3210 {
3211 std::vector<uint32_t> indexes;
3212 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3213 {
3214 std::vector<uint32_t>::const_iterator pos;
3215 std::vector<uint32_t>::const_iterator end = indexes.end();
3216 for (pos = indexes.begin(); pos != end; ++pos)
3217 {
3218 symbol_ptr = symtab->SymbolAtIndex(*pos);
3219 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3220 {
3221 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3222 break;
3223 }
3224 }
3225 }
Chris Lattner24943d22010-06-08 16:52:24 +00003226 }
3227 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003228
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003229 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3230
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003231 if (function_starts_count > 0)
3232 {
3233 char synthetic_function_symbol[PATH_MAX];
3234 uint32_t num_synthetic_function_symbols = 0;
3235 for (i=0; i<function_starts_count; ++i)
3236 {
3237 if (function_starts.GetEntryRef (i).data == false)
3238 ++num_synthetic_function_symbols;
3239 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003240
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003241 if (num_synthetic_function_symbols > 0)
3242 {
3243 if (num_syms < sym_idx + num_synthetic_function_symbols)
3244 {
3245 num_syms = sym_idx + num_synthetic_function_symbols;
3246 sym = symtab->Resize (num_syms);
3247 }
3248 uint32_t synthetic_function_symbol_idx = 0;
3249 for (i=0; i<function_starts_count; ++i)
3250 {
3251 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3252 if (func_start_entry->data == false)
3253 {
Greg Claytond2653c22012-03-14 01:53:24 +00003254 addr_t symbol_file_addr = func_start_entry->addr;
3255 uint32_t symbol_flags = 0;
3256 if (is_arm)
3257 {
3258 if (symbol_file_addr & 1)
3259 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3260 symbol_file_addr &= 0xfffffffffffffffeull;
3261 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003262 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003263 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003264 {
3265 SectionSP symbol_section (symbol_addr.GetSection());
3266 uint32_t symbol_byte_size = 0;
3267 if (symbol_section)
3268 {
3269 const addr_t section_file_addr = symbol_section->GetFileAddress();
3270 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3271 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3272 if (next_func_start_entry)
3273 {
Greg Claytond2653c22012-03-14 01:53:24 +00003274 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3275 if (is_arm)
3276 next_symbol_file_addr &= 0xfffffffffffffffeull;
3277 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 +00003278 }
3279 else
3280 {
Greg Claytond2653c22012-03-14 01:53:24 +00003281 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003282 }
3283 snprintf (synthetic_function_symbol,
3284 sizeof(synthetic_function_symbol),
3285 "___lldb_unnamed_function%u$$%s",
3286 ++synthetic_function_symbol_idx,
3287 module_sp->GetFileSpec().GetFilename().GetCString());
3288 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003289 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003290 sym[sym_idx].SetType (eSymbolTypeCode);
3291 sym[sym_idx].SetIsSynthetic (true);
3292 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003293 if (symbol_flags)
3294 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003295 if (symbol_byte_size)
3296 sym[sym_idx].SetByteSize (symbol_byte_size);
3297 ++sym_idx;
3298 }
3299 }
3300 }
3301 }
3302 }
3303 }
3304
3305 // Trim our symbols down to just what we ended up with after
3306 // removing any symbols.
3307 if (sym_idx < num_syms)
3308 {
3309 num_syms = sym_idx;
3310 sym = symtab->Resize (num_syms);
3311 }
3312
3313 // Now synthesize indirect symbols
3314 if (m_dysymtab.nindirectsyms != 0)
3315 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003316 if (indirect_symbol_index_data.GetByteSize())
3317 {
3318 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3319
3320 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3321 {
3322 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3323 {
3324 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3325 if (symbol_stub_byte_size == 0)
3326 continue;
3327
3328 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3329
3330 if (num_symbol_stubs == 0)
3331 continue;
3332
3333 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3334 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3335 {
3336 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3337 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 +00003338 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003339 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3340 {
3341 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3342 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3343 continue;
3344
3345 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3346 Symbol *stub_symbol = NULL;
3347 if (index_pos != end_index_pos)
3348 {
3349 // We have a remapping from the original nlist index to
3350 // a current symbol index, so just look this up by index
3351 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3352 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003353 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003354 {
3355 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003356 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003357 // S_SYMBOL_STUBS
3358 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3359 }
3360
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003361 if (stub_symbol)
3362 {
3363 Address so_addr(symbol_stub_addr, section_list);
3364
3365 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3366 {
3367 // Change the external symbol into a trampoline that makes sense
3368 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3369 // can re-use them so we don't have to make up a synthetic symbol
3370 // for no good reason.
3371 stub_symbol->SetType (eSymbolTypeTrampoline);
3372 stub_symbol->SetExternal (false);
3373 stub_symbol->GetAddress() = so_addr;
3374 stub_symbol->SetByteSize (symbol_stub_byte_size);
3375 }
3376 else
3377 {
3378 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003379 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003380 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003381 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003382 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003383 stub_symbol = NULL; // this pointer no longer valid
3384 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003385 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003386 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003387 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3388 sym[sym_idx].SetIsSynthetic (true);
3389 sym[sym_idx].GetAddress() = so_addr;
3390 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3391 ++sym_idx;
3392 }
3393 }
Greg Claytond4330e62012-09-05 01:38:55 +00003394 else
3395 {
3396 if (log)
3397 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3398 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003399 }
3400 }
3401 }
3402 }
3403 }
3404 }
3405 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003406 }
3407 return 0;
3408}
3409
3410
3411void
3412ObjectFileMachO::Dump (Stream *s)
3413{
Greg Clayton9482f052012-03-13 23:14:29 +00003414 ModuleSP module_sp(GetModule());
3415 if (module_sp)
3416 {
3417 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3418 s->Printf("%p: ", this);
3419 s->Indent();
3420 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3421 s->PutCString("ObjectFileMachO64");
3422 else
3423 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003424
Greg Clayton9482f052012-03-13 23:14:29 +00003425 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003426
Greg Clayton9482f052012-03-13 23:14:29 +00003427 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003428
Greg Clayton9482f052012-03-13 23:14:29 +00003429 if (m_sections_ap.get())
3430 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003431
Greg Clayton9482f052012-03-13 23:14:29 +00003432 if (m_symtab_ap.get())
3433 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3434 }
Chris Lattner24943d22010-06-08 16:52:24 +00003435}
3436
3437
3438bool
Greg Clayton0467c782011-02-04 18:53:10 +00003439ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003440{
Greg Clayton9482f052012-03-13 23:14:29 +00003441 ModuleSP module_sp(GetModule());
3442 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003443 {
Greg Clayton9482f052012-03-13 23:14:29 +00003444 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3445 struct uuid_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003446 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003447 uint32_t i;
3448 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003449 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003450 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003451 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3452 break;
3453
3454 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00003455 {
Greg Clayton9482f052012-03-13 23:14:29 +00003456 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003457
Greg Clayton9482f052012-03-13 23:14:29 +00003458 if (uuid_bytes)
3459 {
Sean Callanana5689d52012-07-12 18:04:03 +00003460 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3461 // We pretend these object files have no UUID to prevent crashing.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003462
Sean Callanana5689d52012-07-12 18:04:03 +00003463 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3464 0x3b, 0xa8,
3465 0x4b, 0x16,
3466 0xb6, 0xa4,
3467 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
Jason Molenda9badb6c2013-03-06 23:19:17 +00003468
Sean Callanana5689d52012-07-12 18:04:03 +00003469 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3470 return false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003471
Greg Clayton9482f052012-03-13 23:14:29 +00003472 uuid->SetBytes (uuid_bytes);
3473 return true;
3474 }
3475 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003476 }
Greg Clayton9482f052012-03-13 23:14:29 +00003477 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003478 }
Chris Lattner24943d22010-06-08 16:52:24 +00003479 }
3480 return false;
3481}
3482
3483
3484uint32_t
3485ObjectFileMachO::GetDependentModules (FileSpecList& files)
3486{
Chris Lattner24943d22010-06-08 16:52:24 +00003487 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003488 ModuleSP module_sp(GetModule());
3489 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003490 {
Greg Clayton9482f052012-03-13 23:14:29 +00003491 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3492 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003493 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003494 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3495 uint32_t i;
3496 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003497 {
Greg Clayton9482f052012-03-13 23:14:29 +00003498 const uint32_t cmd_offset = offset;
3499 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3500 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003501
Greg Clayton9482f052012-03-13 23:14:29 +00003502 switch (load_cmd.cmd)
3503 {
3504 case LoadCommandDylibLoad:
3505 case LoadCommandDylibLoadWeak:
3506 case LoadCommandDylibReexport:
3507 case LoadCommandDynamicLinkerLoad:
3508 case LoadCommandFixedVMShlibLoad:
3509 case LoadCommandDylibLoadUpward:
3510 {
3511 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3512 const char *path = m_data.PeekCStr(name_offset);
3513 // Skip any path that starts with '@' since these are usually:
3514 // @executable_path/.../file
3515 // @rpath/.../file
3516 if (path && path[0] != '@')
3517 {
3518 FileSpec file_spec(path, resolve_path);
3519 if (files.AppendIfUnique(file_spec))
3520 count++;
3521 }
3522 }
3523 break;
3524
3525 default:
3526 break;
3527 }
3528 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003529 }
Chris Lattner24943d22010-06-08 16:52:24 +00003530 }
3531 return count;
3532}
3533
Jim Ingham28775942011-03-07 23:44:08 +00003534lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003535ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003536{
3537 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3538 // is initialized to an invalid address, so we can just return that.
3539 // 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 +00003540
Jim Ingham28775942011-03-07 23:44:08 +00003541 if (!IsExecutable() || m_entry_point_address.IsValid())
3542 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003543
3544 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003545 // /usr/include/mach-o.h, but it is basically:
3546 //
3547 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3548 // uint32_t count - this is the count of longs in the thread state data
3549 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3550 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003551 //
Jim Ingham28775942011-03-07 23:44:08 +00003552 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3553 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3554 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3555 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3556 //
3557 // For now we hard-code the offsets and flavors we need:
3558 //
3559 //
3560
Greg Clayton9482f052012-03-13 23:14:29 +00003561 ModuleSP module_sp(GetModule());
3562 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003563 {
Greg Clayton9482f052012-03-13 23:14:29 +00003564 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3565 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003566 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003567 uint32_t i;
3568 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3569 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003570
Greg Clayton9482f052012-03-13 23:14:29 +00003571 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003572 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003573 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003574 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3575 break;
3576
3577 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003578 {
Greg Clayton9482f052012-03-13 23:14:29 +00003579 case LoadCommandUnixThread:
3580 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003581 {
Greg Clayton9482f052012-03-13 23:14:29 +00003582 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003583 {
Greg Clayton9482f052012-03-13 23:14:29 +00003584 uint32_t flavor = m_data.GetU32(&offset);
3585 uint32_t count = m_data.GetU32(&offset);
3586 if (count == 0)
3587 {
3588 // We've gotten off somehow, log and exit;
3589 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003590 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003591
Greg Clayton9482f052012-03-13 23:14:29 +00003592 switch (m_header.cputype)
3593 {
3594 case llvm::MachO::CPUTypeARM:
3595 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3596 {
3597 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3598 start_address = m_data.GetU32(&offset);
3599 done = true;
3600 }
Jim Ingham28775942011-03-07 23:44:08 +00003601 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003602 case llvm::MachO::CPUTypeI386:
3603 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3604 {
3605 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3606 start_address = m_data.GetU32(&offset);
3607 done = true;
3608 }
3609 break;
3610 case llvm::MachO::CPUTypeX86_64:
3611 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3612 {
3613 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3614 start_address = m_data.GetU64(&offset);
3615 done = true;
3616 }
3617 break;
3618 default:
3619 return m_entry_point_address;
3620 }
3621 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3622 if (done)
3623 break;
3624 offset += count * 4;
3625 }
Jim Ingham28775942011-03-07 23:44:08 +00003626 }
Greg Clayton9482f052012-03-13 23:14:29 +00003627 break;
3628 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003629 {
Greg Clayton9482f052012-03-13 23:14:29 +00003630 ConstString text_segment_name ("__TEXT");
3631 uint64_t entryoffset = m_data.GetU64(&offset);
3632 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3633 if (text_segment_sp)
3634 {
3635 done = true;
3636 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3637 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003638 }
Greg Clayton9482f052012-03-13 23:14:29 +00003639
3640 default:
3641 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003642 }
Greg Clayton9482f052012-03-13 23:14:29 +00003643 if (done)
3644 break;
Jim Ingham28775942011-03-07 23:44:08 +00003645
Greg Clayton9482f052012-03-13 23:14:29 +00003646 // Go to the next load command:
3647 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003648 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003649
Greg Clayton9482f052012-03-13 23:14:29 +00003650 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003651 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003652 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003653 // of this ObjectFile:
3654 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003655 {
Greg Clayton9482f052012-03-13 23:14:29 +00003656 m_entry_point_address.Clear();
3657 }
3658 }
3659 else
3660 {
3661 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3662 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003663
Greg Clayton9482f052012-03-13 23:14:29 +00003664 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003665
Greg Clayton9482f052012-03-13 23:14:29 +00003666 if (module_sp)
3667 {
3668 SymbolContextList contexts;
3669 SymbolContext context;
3670 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3671 {
3672 if (contexts.GetContextAtIndex(0, context))
3673 m_entry_point_address = context.symbol->GetAddress();
3674 }
Greg Clayton3508c382012-02-24 01:59:29 +00003675 }
3676 }
Jim Ingham28775942011-03-07 23:44:08 +00003677 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003678
Jim Ingham28775942011-03-07 23:44:08 +00003679 return m_entry_point_address;
3680
3681}
3682
Greg Claytonb5a8f142012-02-05 02:38:54 +00003683lldb_private::Address
3684ObjectFileMachO::GetHeaderAddress ()
3685{
3686 lldb_private::Address header_addr;
3687 SectionList *section_list = GetSectionList();
3688 if (section_list)
3689 {
3690 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3691 if (text_segment_sp)
3692 {
Greg Clayton3508c382012-02-24 01:59:29 +00003693 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003694 header_addr.SetOffset (0);
3695 }
3696 }
3697 return header_addr;
3698}
3699
Greg Clayton46c9a352012-02-09 06:16:32 +00003700uint32_t
3701ObjectFileMachO::GetNumThreadContexts ()
3702{
Greg Clayton9482f052012-03-13 23:14:29 +00003703 ModuleSP module_sp(GetModule());
3704 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003705 {
Greg Clayton9482f052012-03-13 23:14:29 +00003706 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3707 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003708 {
Greg Clayton9482f052012-03-13 23:14:29 +00003709 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003710 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003711 FileRangeArray::Entry file_range;
3712 thread_command thread_cmd;
3713 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003714 {
Greg Clayton9482f052012-03-13 23:14:29 +00003715 const uint32_t cmd_offset = offset;
3716 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3717 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003718
Greg Clayton9482f052012-03-13 23:14:29 +00003719 if (thread_cmd.cmd == LoadCommandThread)
3720 {
3721 file_range.SetRangeBase (offset);
3722 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3723 m_thread_context_offsets.Append (file_range);
3724 }
3725 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003726 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003727 }
3728 }
3729 return m_thread_context_offsets.GetSize();
3730}
3731
3732lldb::RegisterContextSP
3733ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3734{
Greg Clayton46c9a352012-02-09 06:16:32 +00003735 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003736
Greg Clayton9482f052012-03-13 23:14:29 +00003737 ModuleSP module_sp(GetModule());
3738 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003739 {
Greg Clayton9482f052012-03-13 23:14:29 +00003740 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3741 if (!m_thread_context_offsets_valid)
3742 GetNumThreadContexts ();
3743
3744 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003745 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003746 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003747
3748 DataExtractor data (m_data,
3749 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003750 thread_context_file_range->GetByteSize());
3751
3752 switch (m_header.cputype)
3753 {
3754 case llvm::MachO::CPUTypeARM:
3755 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3756 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003757
Jim Ingham6f01c932012-10-12 17:34:26 +00003758 case llvm::MachO::CPUTypeI386:
3759 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3760 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003761
Jim Ingham6f01c932012-10-12 17:34:26 +00003762 case llvm::MachO::CPUTypeX86_64:
3763 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3764 break;
3765 }
Greg Clayton9482f052012-03-13 23:14:29 +00003766 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003767 }
3768 return reg_ctx_sp;
3769}
3770
Greg Claytonb5a8f142012-02-05 02:38:54 +00003771
Greg Claytonca319972011-07-09 00:41:34 +00003772ObjectFile::Type
3773ObjectFileMachO::CalculateType()
3774{
3775 switch (m_header.filetype)
3776 {
3777 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3778 if (GetAddressByteSize () == 4)
3779 {
3780 // 32 bit kexts are just object files, but they do have a valid
3781 // UUID load command.
3782 UUID uuid;
3783 if (GetUUID(&uuid))
3784 {
3785 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003786 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003787 // "OSKextGetCurrentIdentifier" as this is required of kexts
3788 if (m_strata == eStrataInvalid)
3789 m_strata = eStrataKernel;
3790 return eTypeSharedLibrary;
3791 }
3792 }
3793 return eTypeObjectFile;
3794
3795 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3796 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3797 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3798 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3799 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3800 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3801 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3802 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3803 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3804 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3805 default:
3806 break;
3807 }
3808 return eTypeUnknown;
3809}
3810
3811ObjectFile::Strata
3812ObjectFileMachO::CalculateStrata()
3813{
3814 switch (m_header.filetype)
3815 {
3816 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3817 {
3818 // 32 bit kexts are just object files, but they do have a valid
3819 // UUID load command.
3820 UUID uuid;
3821 if (GetUUID(&uuid))
3822 {
3823 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003824 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003825 // "OSKextGetCurrentIdentifier" as this is required of kexts
3826 if (m_type == eTypeInvalid)
3827 m_type = eTypeSharedLibrary;
3828
3829 return eStrataKernel;
3830 }
3831 }
3832 return eStrataUnknown;
3833
3834 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3835 // Check for the MH_DYLDLINK bit in the flags
3836 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003837 {
Greg Claytonca319972011-07-09 00:41:34 +00003838 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003839 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003840 else
Sean Callananac725af2012-02-10 20:22:35 +00003841 {
3842 SectionList *section_list = GetSectionList();
3843 if (section_list)
3844 {
3845 static ConstString g_kld_section_name ("__KLD");
3846 if (section_list->FindSectionByName(g_kld_section_name))
3847 return eStrataKernel;
3848 }
3849 }
3850 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00003851
3852 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
3853 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00003854 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00003855 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
3856 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
3857 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
3858 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
3859 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
3860 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
3861 default:
3862 break;
3863 }
3864 return eStrataUnknown;
3865}
3866
3867
Greg Clayton49f4bf22012-02-22 19:41:02 +00003868uint32_t
3869ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
3870{
Greg Clayton9482f052012-03-13 23:14:29 +00003871 ModuleSP module_sp(GetModule());
3872 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003873 {
Greg Clayton9482f052012-03-13 23:14:29 +00003874 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3875 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003876 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003877 uint32_t version_cmd = 0;
3878 uint64_t version = 0;
3879 uint32_t i;
3880 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003881 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003882 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003883 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3884 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003885
Greg Clayton9482f052012-03-13 23:14:29 +00003886 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003887 {
Greg Clayton9482f052012-03-13 23:14:29 +00003888 if (version_cmd == 0)
3889 {
3890 version_cmd = load_cmd.cmd;
3891 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
3892 break;
3893 version = load_cmd.dylib.current_version;
3894 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003895 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00003896 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00003897 }
Greg Clayton9482f052012-03-13 23:14:29 +00003898 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003899 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003900
Greg Clayton9482f052012-03-13 23:14:29 +00003901 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003902 {
Greg Clayton9482f052012-03-13 23:14:29 +00003903 if (versions != NULL && num_versions > 0)
3904 {
3905 if (num_versions > 0)
3906 versions[0] = (version & 0xFFFF0000ull) >> 16;
3907 if (num_versions > 1)
3908 versions[1] = (version & 0x0000FF00ull) >> 8;
3909 if (num_versions > 2)
3910 versions[2] = (version & 0x000000FFull);
3911 // Fill in an remaining version numbers with invalid values
3912 for (i=3; i<num_versions; ++i)
3913 versions[i] = UINT32_MAX;
3914 }
3915 // The LC_ID_DYLIB load command has a version with 3 version numbers
3916 // in it, so always return 3
3917 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003918 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00003919 }
3920 return false;
3921}
3922
Chris Lattner24943d22010-06-08 16:52:24 +00003923bool
Greg Clayton395fc332011-02-15 21:59:32 +00003924ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00003925{
Greg Clayton9482f052012-03-13 23:14:29 +00003926 ModuleSP module_sp(GetModule());
3927 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003928 {
Greg Clayton9482f052012-03-13 23:14:29 +00003929 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3930 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003931
Greg Clayton9482f052012-03-13 23:14:29 +00003932 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00003933 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00003934 // unknown to make sure we use the DynamicLoaderStatic()...
3935 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
3936 {
3937 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
3938 }
3939 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003940 }
Greg Clayton9482f052012-03-13 23:14:29 +00003941 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003942}
3943
3944
3945//------------------------------------------------------------------
3946// PluginInterface protocol
3947//------------------------------------------------------------------
3948const char *
3949ObjectFileMachO::GetPluginName()
3950{
3951 return "ObjectFileMachO";
3952}
3953
3954const char *
3955ObjectFileMachO::GetShortPluginName()
3956{
3957 return GetPluginNameStatic();
3958}
3959
3960uint32_t
3961ObjectFileMachO::GetPluginVersion()
3962{
3963 return 1;
3964}
3965