blob: e2b75ad8e16f910a50292f2bea47aac26748a322 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ObjectFileMachO.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Greg Clayton3f69eac2011-12-03 02:30:59 +000010#include "llvm/ADT/StringRef.h"
Jim Ingham28775942011-03-07 23:44:08 +000011#include "llvm/Support/MachO.h"
12
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "ObjectFileMachO.h"
14
Greg Claytond4330e62012-09-05 01:38:55 +000015#include "lldb/lldb-private-log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Core/ArchSpec.h"
17#include "lldb/Core/DataBuffer.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/FileSpecList.h"
Greg Claytond4330e62012-09-05 01:38:55 +000019#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Core/Module.h"
21#include "lldb/Core/PluginManager.h"
Greg Clayton6f7f8da2012-04-24 03:06:13 +000022#include "lldb/Core/RangeMap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/Section.h"
24#include "lldb/Core/StreamFile.h"
25#include "lldb/Core/StreamString.h"
26#include "lldb/Core/Timer.h"
27#include "lldb/Core/UUID.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000028#include "lldb/Host/Host.h"
29#include "lldb/Host/FileSpec.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000030#include "lldb/Symbol/ClangNamespaceDecl.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Symbol/ObjectFile.h"
Greg Clayton29021d32012-04-18 05:19:20 +000032#include "lldb/Target/Platform.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000033#include "lldb/Target/Process.h"
Greg Clayton29021d32012-04-18 05:19:20 +000034#include "lldb/Target/Target.h"
Greg Clayton9ce95382012-02-13 23:10:39 +000035#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
36#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Clayton46c9a352012-02-09 06:16:32 +000037#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner24943d22010-06-08 16:52:24 +000038
Chris Lattner24943d22010-06-08 16:52:24 +000039using namespace lldb;
40using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000041using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000042
Jason Molenda9badb6c2013-03-06 23:19:17 +000043class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
Greg Clayton46c9a352012-02-09 06:16:32 +000044{
45public:
46 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
47 RegisterContextDarwin_x86_64 (thread, 0)
48 {
49 SetRegisterDataFrom_LC_THREAD (data);
50 }
51
52 virtual void
53 InvalidateAllRegisters ()
54 {
55 // Do nothing... registers are always valid...
56 }
57
58 void
59 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
60 {
Greg Clayton36da2aa2013-01-25 18:06:21 +000061 lldb::offset_t offset = 0;
Greg Clayton46c9a352012-02-09 06:16:32 +000062 SetError (GPRRegSet, Read, -1);
63 SetError (FPURegSet, Read, -1);
64 SetError (EXCRegSet, Read, -1);
Greg Clayton9ce95382012-02-13 23:10:39 +000065 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +000066
Greg Clayton9ce95382012-02-13 23:10:39 +000067 while (!done)
Greg Clayton46c9a352012-02-09 06:16:32 +000068 {
Greg Clayton9ce95382012-02-13 23:10:39 +000069 int flavor = data.GetU32 (&offset);
70 if (flavor == 0)
71 done = true;
72 else
Greg Clayton46c9a352012-02-09 06:16:32 +000073 {
Greg Clayton9ce95382012-02-13 23:10:39 +000074 uint32_t i;
75 uint32_t count = data.GetU32 (&offset);
76 switch (flavor)
77 {
78 case GPRRegSet:
79 for (i=0; i<count; ++i)
80 (&gpr.rax)[i] = data.GetU64(&offset);
81 SetError (GPRRegSet, Read, 0);
82 done = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +000083
Greg Clayton9ce95382012-02-13 23:10:39 +000084 break;
85 case FPURegSet:
86 // TODO: fill in FPU regs....
87 //SetError (FPURegSet, Read, -1);
88 done = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +000089
Greg Clayton9ce95382012-02-13 23:10:39 +000090 break;
91 case EXCRegSet:
92 exc.trapno = data.GetU32(&offset);
93 exc.err = data.GetU32(&offset);
94 exc.faultvaddr = data.GetU64(&offset);
95 SetError (EXCRegSet, Read, 0);
96 done = true;
97 break;
98 case 7:
99 case 8:
100 case 9:
101 // fancy flavors that encapsulate of the the above
102 // falvors...
103 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000104
Greg Clayton9ce95382012-02-13 23:10:39 +0000105 default:
106 done = true;
107 break;
108 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000109 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000110 }
111 }
112protected:
113 virtual int
114 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
115 {
116 return 0;
117 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000118
Greg Clayton9ce95382012-02-13 23:10:39 +0000119 virtual int
120 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
121 {
122 return 0;
123 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000124
Greg Clayton9ce95382012-02-13 23:10:39 +0000125 virtual int
126 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
127 {
128 return 0;
129 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000130
Greg Clayton9ce95382012-02-13 23:10:39 +0000131 virtual int
132 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
133 {
134 return 0;
135 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000136
Greg Clayton9ce95382012-02-13 23:10:39 +0000137 virtual int
138 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
139 {
140 return 0;
141 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000142
Greg Clayton9ce95382012-02-13 23:10:39 +0000143 virtual int
144 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
145 {
146 return 0;
147 }
148};
Greg Clayton46c9a352012-02-09 06:16:32 +0000149
Greg Clayton9ce95382012-02-13 23:10:39 +0000150
Jason Molenda9badb6c2013-03-06 23:19:17 +0000151class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
Greg Clayton9ce95382012-02-13 23:10:39 +0000152{
153public:
154 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
155 RegisterContextDarwin_i386 (thread, 0)
156 {
157 SetRegisterDataFrom_LC_THREAD (data);
158 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000159
Greg Clayton9ce95382012-02-13 23:10:39 +0000160 virtual void
161 InvalidateAllRegisters ()
162 {
163 // Do nothing... registers are always valid...
164 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000165
Greg Clayton9ce95382012-02-13 23:10:39 +0000166 void
167 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
168 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000169 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000170 SetError (GPRRegSet, Read, -1);
171 SetError (FPURegSet, Read, -1);
172 SetError (EXCRegSet, Read, -1);
173 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000174
Greg Clayton9ce95382012-02-13 23:10:39 +0000175 while (!done)
176 {
177 int flavor = data.GetU32 (&offset);
178 if (flavor == 0)
179 done = true;
180 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000181 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000182 uint32_t i;
183 uint32_t count = data.GetU32 (&offset);
184 switch (flavor)
185 {
186 case GPRRegSet:
187 for (i=0; i<count; ++i)
188 (&gpr.eax)[i] = data.GetU32(&offset);
189 SetError (GPRRegSet, Read, 0);
190 done = true;
191
192 break;
193 case FPURegSet:
194 // TODO: fill in FPU regs....
195 //SetError (FPURegSet, Read, -1);
196 done = true;
197
198 break;
199 case EXCRegSet:
200 exc.trapno = data.GetU32(&offset);
201 exc.err = data.GetU32(&offset);
202 exc.faultvaddr = data.GetU32(&offset);
203 SetError (EXCRegSet, Read, 0);
204 done = true;
205 break;
206 case 7:
207 case 8:
208 case 9:
209 // fancy flavors that encapsulate of the the above
210 // falvors...
211 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000212
Greg Clayton9ce95382012-02-13 23:10:39 +0000213 default:
214 done = true;
215 break;
216 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000217 }
218 }
219 }
220protected:
221 virtual int
222 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
223 {
224 return 0;
225 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000226
Greg Clayton46c9a352012-02-09 06:16:32 +0000227 virtual int
228 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
229 {
230 return 0;
231 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000232
Greg Clayton46c9a352012-02-09 06:16:32 +0000233 virtual int
234 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
235 {
236 return 0;
237 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000238
Greg Clayton46c9a352012-02-09 06:16:32 +0000239 virtual int
240 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
241 {
242 return 0;
243 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000244
Greg Clayton46c9a352012-02-09 06:16:32 +0000245 virtual int
246 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
247 {
248 return 0;
249 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000250
Greg Clayton46c9a352012-02-09 06:16:32 +0000251 virtual int
252 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
253 {
254 return 0;
255 }
256};
257
Jason Molenda9badb6c2013-03-06 23:19:17 +0000258class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Clayton9ce95382012-02-13 23:10:39 +0000259{
260public:
261 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonb5431d02012-10-30 23:57:32 +0000262 RegisterContextDarwin_arm (thread, 0)
Greg Clayton9ce95382012-02-13 23:10:39 +0000263 {
264 SetRegisterDataFrom_LC_THREAD (data);
265 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000266
Greg Clayton9ce95382012-02-13 23:10:39 +0000267 virtual void
268 InvalidateAllRegisters ()
269 {
270 // Do nothing... registers are always valid...
271 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000272
Greg Clayton9ce95382012-02-13 23:10:39 +0000273 void
274 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
275 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000276 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000277 SetError (GPRRegSet, Read, -1);
278 SetError (FPURegSet, Read, -1);
279 SetError (EXCRegSet, Read, -1);
280 int flavor = data.GetU32 (&offset);
281 uint32_t count = data.GetU32 (&offset);
282 switch (flavor)
283 {
284 case GPRRegSet:
285 for (uint32_t i=0; i<count; ++i)
286 gpr.r[i] = data.GetU32(&offset);
287 SetError (GPRRegSet, Read, 0);
288 break;
289 case FPURegSet:
290 // TODO: fill in FPU regs....
291 //SetError (FPURegSet, Read, -1);
292 break;
293 case EXCRegSet:
294 exc.exception = data.GetU32(&offset);
295 exc.fsr = data.GetU32(&offset);
296 exc.far = data.GetU32(&offset);
297 SetError (EXCRegSet, Read, 0);
298 break;
299 }
300 }
301protected:
302 virtual int
303 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
304 {
305 return 0;
306 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000307
Greg Clayton9ce95382012-02-13 23:10:39 +0000308 virtual int
309 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
310 {
311 return 0;
312 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000313
Greg Clayton9ce95382012-02-13 23:10:39 +0000314 virtual int
315 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
316 {
317 return 0;
318 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000319
320 virtual int
321 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
322 {
323 return -1;
324 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000325
Greg Clayton9ce95382012-02-13 23:10:39 +0000326 virtual int
327 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
328 {
329 return 0;
330 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000331
Greg Clayton9ce95382012-02-13 23:10:39 +0000332 virtual int
333 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
334 {
335 return 0;
336 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000337
Greg Clayton9ce95382012-02-13 23:10:39 +0000338 virtual int
339 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
340 {
341 return 0;
342 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000343
Greg Claytonb5431d02012-10-30 23:57:32 +0000344 virtual int
345 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
346 {
347 return -1;
348 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000349};
350
Greg Claytonb1888f22011-03-19 01:12:21 +0000351#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000352
353void
354ObjectFileMachO::Initialize()
355{
356 PluginManager::RegisterPlugin (GetPluginNameStatic(),
357 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000358 CreateInstance,
359 CreateMemoryInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000360}
361
362void
363ObjectFileMachO::Terminate()
364{
365 PluginManager::UnregisterPlugin (CreateInstance);
366}
367
368
369const char *
370ObjectFileMachO::GetPluginNameStatic()
371{
372 return "object-file.mach-o";
373}
374
375const char *
376ObjectFileMachO::GetPluginDescriptionStatic()
377{
378 return "Mach-o object file reader (32 and 64 bit)";
379}
380
Chris Lattner24943d22010-06-08 16:52:24 +0000381ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000382ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
383 DataBufferSP& data_sp,
384 lldb::offset_t data_offset,
385 const FileSpec* file,
386 lldb::offset_t file_offset,
387 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000388{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000389 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000390 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000391 data_sp = file->MemoryMapFileContents(file_offset, length);
392 data_offset = 0;
393 }
394
395 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
396 {
397 // Update the data to contain the entire file if it doesn't already
398 if (data_sp->GetByteSize() < length)
399 {
400 data_sp = file->MemoryMapFileContents(file_offset, length);
401 data_offset = 0;
402 }
403 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 +0000404 if (objfile_ap.get() && objfile_ap->ParseHeader())
405 return objfile_ap.release();
406 }
407 return NULL;
408}
409
Greg Claytonb5a8f142012-02-05 02:38:54 +0000410ObjectFile *
Jason Molenda9badb6c2013-03-06 23:19:17 +0000411ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
412 DataBufferSP& data_sp,
413 const ProcessSP &process_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000414 lldb::addr_t header_addr)
415{
416 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
417 {
Greg Clayton3508c382012-02-24 01:59:29 +0000418 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000419 if (objfile_ap.get() && objfile_ap->ParseHeader())
420 return objfile_ap.release();
421 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000422 return NULL;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000423}
424
425
426const ConstString &
427ObjectFileMachO::GetSegmentNameTEXT()
428{
429 static ConstString g_segment_name_TEXT ("__TEXT");
430 return g_segment_name_TEXT;
431}
432
433const ConstString &
434ObjectFileMachO::GetSegmentNameDATA()
435{
436 static ConstString g_segment_name_DATA ("__DATA");
437 return g_segment_name_DATA;
438}
439
440const ConstString &
441ObjectFileMachO::GetSegmentNameOBJC()
442{
443 static ConstString g_segment_name_OBJC ("__OBJC");
444 return g_segment_name_OBJC;
445}
446
447const ConstString &
448ObjectFileMachO::GetSegmentNameLINKEDIT()
449{
450 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
451 return g_section_name_LINKEDIT;
452}
453
454const ConstString &
455ObjectFileMachO::GetSectionNameEHFrame()
456{
457 static ConstString g_section_name_eh_frame ("__eh_frame");
458 return g_section_name_eh_frame;
459}
460
461
Chris Lattner24943d22010-06-08 16:52:24 +0000462
463static uint32_t
464MachHeaderSizeFromMagic(uint32_t magic)
465{
466 switch (magic)
467 {
Greg Clayton1674b122010-07-21 22:12:05 +0000468 case HeaderMagic32:
469 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000470 return sizeof(struct mach_header);
471
Greg Clayton1674b122010-07-21 22:12:05 +0000472 case HeaderMagic64:
473 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000474 return sizeof(struct mach_header_64);
475 break;
476
477 default:
478 break;
479 }
480 return 0;
481}
482
483
484bool
Jason Molenda9badb6c2013-03-06 23:19:17 +0000485ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
486 lldb::addr_t data_offset,
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000487 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000488{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000489 DataExtractor data;
490 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000491 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000492 uint32_t magic = data.GetU32(&offset);
493 return MachHeaderSizeFromMagic(magic) != 0;
494}
495
496
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000497ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
498 DataBufferSP& data_sp,
499 lldb::offset_t data_offset,
500 const FileSpec* file,
501 lldb::offset_t file_offset,
502 lldb::offset_t length) :
503 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Clayton46c9a352012-02-09 06:16:32 +0000504 m_mach_segments(),
505 m_mach_sections(),
506 m_entry_point_address(),
507 m_thread_context_offsets(),
508 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000509{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000510 ::memset (&m_header, 0, sizeof(m_header));
511 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000512}
513
Greg Clayton3508c382012-02-24 01:59:29 +0000514ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000515 lldb::DataBufferSP& header_data_sp,
516 const lldb::ProcessSP &process_sp,
517 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000518 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Clayton46c9a352012-02-09 06:16:32 +0000519 m_mach_segments(),
520 m_mach_sections(),
521 m_entry_point_address(),
522 m_thread_context_offsets(),
523 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000524{
525 ::memset (&m_header, 0, sizeof(m_header));
526 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
527}
Chris Lattner24943d22010-06-08 16:52:24 +0000528
529ObjectFileMachO::~ObjectFileMachO()
530{
531}
532
533
534bool
535ObjectFileMachO::ParseHeader ()
536{
Greg Clayton9482f052012-03-13 23:14:29 +0000537 ModuleSP module_sp(GetModule());
538 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000539 {
Greg Clayton9482f052012-03-13 23:14:29 +0000540 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
541 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000542 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000543 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000544 // Leave magic in the original byte order
545 m_header.magic = m_data.GetU32(&offset);
546 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000547 {
Greg Clayton9482f052012-03-13 23:14:29 +0000548 case HeaderMagic32:
549 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
550 m_data.SetAddressByteSize(4);
551 can_parse = true;
552 break;
553
554 case HeaderMagic64:
555 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
556 m_data.SetAddressByteSize(8);
557 can_parse = true;
558 break;
559
560 case HeaderMagic32Swapped:
561 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
562 m_data.SetAddressByteSize(4);
563 can_parse = true;
564 break;
565
566 case HeaderMagic64Swapped:
567 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
568 m_data.SetAddressByteSize(8);
569 can_parse = true;
570 break;
571
572 default:
573 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000574 }
Greg Clayton9482f052012-03-13 23:14:29 +0000575
576 if (can_parse)
577 {
578 m_data.GetU32(&offset, &m_header.cputype, 6);
579
580 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +0000581
Greg Clayton21a25432012-11-16 21:36:10 +0000582 // Check if the module has a required architecture
583 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000584 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000585 return false;
586
Greg Clayton9482f052012-03-13 23:14:29 +0000587 if (SetModulesArchitecture (mach_arch))
588 {
589 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
590 if (m_data.GetByteSize() < header_and_lc_size)
591 {
592 DataBufferSP data_sp;
593 ProcessSP process_sp (m_process_wp.lock());
594 if (process_sp)
595 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000596 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000597 }
598 else
599 {
600 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000601 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000602 if (data_sp->GetByteSize() != header_and_lc_size)
603 return false;
604 }
605 if (data_sp)
606 m_data.SetData (data_sp);
607 }
608 }
609 return true;
610 }
611 else
612 {
613 memset(&m_header, 0, sizeof(struct mach_header));
614 }
Chris Lattner24943d22010-06-08 16:52:24 +0000615 }
616 return false;
617}
618
619
620ByteOrder
621ObjectFileMachO::GetByteOrder () const
622{
Chris Lattner24943d22010-06-08 16:52:24 +0000623 return m_data.GetByteOrder ();
624}
625
Jim Ingham7508e732010-08-09 23:31:02 +0000626bool
627ObjectFileMachO::IsExecutable() const
628{
629 return m_header.filetype == HeaderFileTypeExecutable;
630}
Chris Lattner24943d22010-06-08 16:52:24 +0000631
Greg Clayton36da2aa2013-01-25 18:06:21 +0000632uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000633ObjectFileMachO::GetAddressByteSize () const
634{
Chris Lattner24943d22010-06-08 16:52:24 +0000635 return m_data.GetAddressByteSize ();
636}
637
Greg Claytonb3448432011-03-24 21:19:54 +0000638AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000639ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
640{
641 Symtab *symtab = GetSymtab();
642 if (symtab)
643 {
644 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
645 if (symbol)
646 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000647 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000648 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000649 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000650 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000651 {
Greg Clayton3508c382012-02-24 01:59:29 +0000652 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000653 switch (section_type)
654 {
655 case eSectionTypeInvalid: return eAddressClassUnknown;
656 case eSectionTypeCode:
657 if (m_header.cputype == llvm::MachO::CPUTypeARM)
658 {
659 // For ARM we have a bit in the n_desc field of the symbol
660 // that tells us ARM/Thumb which is bit 0x0008.
661 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
662 return eAddressClassCodeAlternateISA;
663 }
664 return eAddressClassCode;
665
666 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000667 case eSectionTypeData:
668 case eSectionTypeDataCString:
669 case eSectionTypeDataCStringPointers:
670 case eSectionTypeDataSymbolAddress:
671 case eSectionTypeData4:
672 case eSectionTypeData8:
673 case eSectionTypeData16:
674 case eSectionTypeDataPointers:
675 case eSectionTypeZeroFill:
676 case eSectionTypeDataObjCMessageRefs:
677 case eSectionTypeDataObjCCFStrings:
678 return eAddressClassData;
679 case eSectionTypeDebug:
680 case eSectionTypeDWARFDebugAbbrev:
681 case eSectionTypeDWARFDebugAranges:
682 case eSectionTypeDWARFDebugFrame:
683 case eSectionTypeDWARFDebugInfo:
684 case eSectionTypeDWARFDebugLine:
685 case eSectionTypeDWARFDebugLoc:
686 case eSectionTypeDWARFDebugMacInfo:
687 case eSectionTypeDWARFDebugPubNames:
688 case eSectionTypeDWARFDebugPubTypes:
689 case eSectionTypeDWARFDebugRanges:
690 case eSectionTypeDWARFDebugStr:
691 case eSectionTypeDWARFAppleNames:
692 case eSectionTypeDWARFAppleTypes:
693 case eSectionTypeDWARFAppleNamespaces:
694 case eSectionTypeDWARFAppleObjC:
695 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000696 case eSectionTypeEHFrame: return eAddressClassRuntime;
697 case eSectionTypeOther: return eAddressClassUnknown;
698 }
699 }
700 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000701
Greg Claytonb3448432011-03-24 21:19:54 +0000702 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000703 switch (symbol_type)
704 {
705 case eSymbolTypeAny: return eAddressClassUnknown;
706 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000707
Greg Claytonb1888f22011-03-19 01:12:21 +0000708 case eSymbolTypeCode:
709 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000710 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000711 if (m_header.cputype == llvm::MachO::CPUTypeARM)
712 {
713 // For ARM we have a bit in the n_desc field of the symbol
714 // that tells us ARM/Thumb which is bit 0x0008.
715 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
716 return eAddressClassCodeAlternateISA;
717 }
718 return eAddressClassCode;
719
720 case eSymbolTypeData: return eAddressClassData;
721 case eSymbolTypeRuntime: return eAddressClassRuntime;
722 case eSymbolTypeException: return eAddressClassRuntime;
723 case eSymbolTypeSourceFile: return eAddressClassDebug;
724 case eSymbolTypeHeaderFile: return eAddressClassDebug;
725 case eSymbolTypeObjectFile: return eAddressClassDebug;
726 case eSymbolTypeCommonBlock: return eAddressClassDebug;
727 case eSymbolTypeBlock: return eAddressClassDebug;
728 case eSymbolTypeLocal: return eAddressClassData;
729 case eSymbolTypeParam: return eAddressClassData;
730 case eSymbolTypeVariable: return eAddressClassData;
731 case eSymbolTypeVariableType: return eAddressClassDebug;
732 case eSymbolTypeLineEntry: return eAddressClassDebug;
733 case eSymbolTypeLineHeader: return eAddressClassDebug;
734 case eSymbolTypeScopeBegin: return eAddressClassDebug;
735 case eSymbolTypeScopeEnd: return eAddressClassDebug;
736 case eSymbolTypeAdditional: return eAddressClassUnknown;
737 case eSymbolTypeCompiler: return eAddressClassDebug;
738 case eSymbolTypeInstrumentation:return eAddressClassDebug;
739 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000740 case eSymbolTypeObjCClass: return eAddressClassRuntime;
741 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
742 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000743 }
744 }
745 }
746 return eAddressClassUnknown;
747}
Chris Lattner24943d22010-06-08 16:52:24 +0000748
749Symtab *
750ObjectFileMachO::GetSymtab()
751{
Greg Clayton9482f052012-03-13 23:14:29 +0000752 ModuleSP module_sp(GetModule());
753 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000754 {
Greg Clayton9482f052012-03-13 23:14:29 +0000755 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
756 if (m_symtab_ap.get() == NULL)
757 {
758 m_symtab_ap.reset(new Symtab(this));
759 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
760 ParseSymtab (true);
761 m_symtab_ap->Finalize ();
762 }
Chris Lattner24943d22010-06-08 16:52:24 +0000763 }
764 return m_symtab_ap.get();
765}
766
767
768SectionList *
769ObjectFileMachO::GetSectionList()
770{
Greg Clayton9482f052012-03-13 23:14:29 +0000771 ModuleSP module_sp(GetModule());
772 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000773 {
Greg Clayton9482f052012-03-13 23:14:29 +0000774 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
775 if (m_sections_ap.get() == NULL)
776 {
777 m_sections_ap.reset(new SectionList());
778 ParseSections();
779 }
Chris Lattner24943d22010-06-08 16:52:24 +0000780 }
781 return m_sections_ap.get();
782}
783
784
785size_t
786ObjectFileMachO::ParseSections ()
787{
788 lldb::user_id_t segID = 0;
789 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000790 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000791 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000792 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000793 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000794 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000795 // First look up any LC_ENCRYPTION_INFO load commands
796 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
797 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000798 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000799 for (i=0; i<m_header.ncmds; ++i)
800 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000801 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000802 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000803 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000804
Greg Clayton54e33712012-05-25 18:09:55 +0000805 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000806 {
Greg Clayton54e33712012-05-25 18:09:55 +0000807 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
808 {
809 if (encryption_cmd.cryptid != 0)
810 {
811 EncryptedFileRanges::Entry entry;
812 entry.SetRangeBase(encryption_cmd.cryptoff);
813 entry.SetByteSize(encryption_cmd.cryptsize);
814 encrypted_file_ranges.Append(entry);
815 }
816 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000817 }
Greg Clayton54e33712012-05-25 18:09:55 +0000818 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000819 }
820
821 offset = MachHeaderSizeFromMagic(m_header.magic);
822
Greg Clayton54e33712012-05-25 18:09:55 +0000823 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000824 for (i=0; i<m_header.ncmds; ++i)
825 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000826 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000827 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
828 break;
829
Greg Clayton1674b122010-07-21 22:12:05 +0000830 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000831 {
832 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
833 {
834 load_cmd.vmaddr = m_data.GetAddress(&offset);
835 load_cmd.vmsize = m_data.GetAddress(&offset);
836 load_cmd.fileoff = m_data.GetAddress(&offset);
837 load_cmd.filesize = m_data.GetAddress(&offset);
838 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
839 {
Jason Molenda9badb6c2013-03-06 23:19:17 +0000840
Greg Clayton68ca8232011-01-25 02:58:48 +0000841 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
842
Chris Lattner24943d22010-06-08 16:52:24 +0000843 // Keep a list of mach segments around in case we need to
844 // get at data that isn't stored in the abstracted Sections.
845 m_mach_segments.push_back (load_cmd);
846
Greg Clayton36da2aa2013-01-25 18:06:21 +0000847 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 +0000848 // Use a segment ID of the segment index shifted left by 8 so they
849 // never conflict with any of the sections.
850 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000851 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000852 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000853 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000854 ++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
855 segment_name, // Name of this section
856 eSectionTypeContainer, // This section is a container of other sections.
857 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
858 load_cmd.vmsize, // VM size in bytes of this section
859 load_cmd.fileoff, // Offset to the data for this section in the file
860 load_cmd.filesize, // Size in bytes of this section as found in the the file
861 load_cmd.flags)); // Flags for this section
862
Greg Clayton68ca8232011-01-25 02:58:48 +0000863 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000864 m_sections_ap->AddSection(segment_sp);
865 }
866
867 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000868 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000869 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +0000870 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +0000871 // mach sections yet...
872 if (m_mach_sections.empty())
873 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000874 uint32_t segment_sect_idx;
875 const lldb::user_id_t first_segment_sectID = sectID + 1;
876
877
Greg Clayton1674b122010-07-21 22:12:05 +0000878 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000879 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
880 {
881 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
882 break;
883 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
884 break;
885 sect64.addr = m_data.GetAddress(&offset);
886 sect64.size = m_data.GetAddress(&offset);
887
888 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
889 break;
890
891 // Keep a list of mach sections around in case we need to
892 // get at data that isn't stored in the abstracted Sections.
893 m_mach_sections.push_back (sect64);
894
895 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
896 if (!segment_name)
897 {
898 // We have a segment with no name so we need to conjure up
899 // segments that correspond to the section's segname if there
900 // isn't already such a section. If there is such a section,
901 // we resize the section so that it spans all sections.
902 // We also mark these sections as fake so address matches don't
903 // hit if they land in the gaps between the child sections.
904 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
905 segment_sp = m_sections_ap->FindSectionByName (segment_name);
906 if (segment_sp.get())
907 {
908 Section *segment = segment_sp.get();
909 // Grow the section size as needed.
910 const lldb::addr_t sect64_min_addr = sect64.addr;
911 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
912 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
913 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
914 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
915 if (sect64_min_addr >= curr_seg_min_addr)
916 {
917 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
918 // Only grow the section size if needed
919 if (new_seg_byte_size > curr_seg_byte_size)
920 segment->SetByteSize (new_seg_byte_size);
921 }
922 else
923 {
924 // We need to change the base address of the segment and
925 // adjust the child section offsets for all existing children.
926 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
927 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +0000928 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000929 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
930 }
Greg Clayton661825b2010-06-28 23:51:11 +0000931
932 // Grow the section size as needed.
933 if (sect64.offset)
934 {
935 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
936 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
937
938 const lldb::addr_t section_min_file_offset = sect64.offset;
939 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
940 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
941 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
942 segment->SetFileOffset (new_file_offset);
943 segment->SetFileSize (new_file_size);
944 }
Chris Lattner24943d22010-06-08 16:52:24 +0000945 }
946 else
947 {
948 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +0000949 segment_sp.reset(new Section (segment_sp, // Parent section
950 module_sp, // Module to which this section belongs
951 ++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
952 segment_name, // Name of this section
953 eSectionTypeContainer, // This section is a container of other sections.
954 sect64.addr, // File VM address == addresses as they are found in the object file
955 sect64.size, // VM size in bytes of this section
956 sect64.offset, // Offset to the data for this section in the file
957 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
958 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +0000959 segment_sp->SetIsFake(true);
960 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000961 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000962 }
963 }
964 assert (segment_sp.get());
965
Greg Clayton1674b122010-07-21 22:12:05 +0000966 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000967 static ConstString g_sect_name_objc_data ("__objc_data");
968 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
969 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
970 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
971 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
972 static ConstString g_sect_name_objc_const ("__objc_const");
973 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
974 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000975
976 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
977 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
978 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
979 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
980 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
981 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
982 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
983 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
984 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
985 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
986 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +0000987 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
988 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +0000989 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +0000990 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000991 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +0000992 static ConstString g_sect_name_DATA ("__DATA");
993 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000994
Chris Lattner24943d22010-06-08 16:52:24 +0000995 SectionType sect_type = eSectionTypeOther;
996
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000997 if (section_name == g_sect_name_dwarf_debug_abbrev)
998 sect_type = eSectionTypeDWARFDebugAbbrev;
999 else if (section_name == g_sect_name_dwarf_debug_aranges)
1000 sect_type = eSectionTypeDWARFDebugAranges;
1001 else if (section_name == g_sect_name_dwarf_debug_frame)
1002 sect_type = eSectionTypeDWARFDebugFrame;
1003 else if (section_name == g_sect_name_dwarf_debug_info)
1004 sect_type = eSectionTypeDWARFDebugInfo;
1005 else if (section_name == g_sect_name_dwarf_debug_line)
1006 sect_type = eSectionTypeDWARFDebugLine;
1007 else if (section_name == g_sect_name_dwarf_debug_loc)
1008 sect_type = eSectionTypeDWARFDebugLoc;
1009 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1010 sect_type = eSectionTypeDWARFDebugMacInfo;
1011 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1012 sect_type = eSectionTypeDWARFDebugPubNames;
1013 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1014 sect_type = eSectionTypeDWARFDebugPubTypes;
1015 else if (section_name == g_sect_name_dwarf_debug_ranges)
1016 sect_type = eSectionTypeDWARFDebugRanges;
1017 else if (section_name == g_sect_name_dwarf_debug_str)
1018 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001019 else if (section_name == g_sect_name_dwarf_apple_names)
1020 sect_type = eSectionTypeDWARFAppleNames;
1021 else if (section_name == g_sect_name_dwarf_apple_types)
1022 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001023 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1024 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001025 else if (section_name == g_sect_name_dwarf_apple_objc)
1026 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001027 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001028 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001029 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001030 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001031 else if (section_name == g_sect_name_eh_frame)
1032 sect_type = eSectionTypeEHFrame;
1033 else if (section_name == g_sect_name_cfstring)
1034 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001035 else if (section_name == g_sect_name_objc_data ||
1036 section_name == g_sect_name_objc_classrefs ||
1037 section_name == g_sect_name_objc_superrefs ||
1038 section_name == g_sect_name_objc_const ||
1039 section_name == g_sect_name_objc_classlist)
1040 {
1041 sect_type = eSectionTypeDataPointers;
1042 }
Chris Lattner24943d22010-06-08 16:52:24 +00001043
1044 if (sect_type == eSectionTypeOther)
1045 {
1046 switch (mach_sect_type)
1047 {
1048 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001049 case SectionTypeRegular:
1050 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001051 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001052 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001053 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001054 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001055 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001056 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001057 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1058 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1059 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1060 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1061 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1062 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1063 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1064 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1065 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1066 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1067 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1068 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1069 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1070 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1071 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1072 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001073 default: break;
1074 }
1075 }
1076
Greg Clayton3508c382012-02-24 01:59:29 +00001077 SectionSP section_sp(new Section (segment_sp,
1078 module_sp,
1079 ++sectID,
1080 section_name,
1081 sect_type,
1082 sect64.addr - segment_sp->GetFileAddress(),
1083 sect64.size,
1084 sect64.offset,
1085 sect64.offset == 0 ? 0 : sect64.size,
1086 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001087 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001088
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001089 bool section_is_encrypted = false;
1090 if (!segment_is_encrypted && load_cmd.filesize != 0)
1091 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001092
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001093 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001094 segment_sp->GetChildren().AddSection(section_sp);
1095
1096 if (segment_sp->IsFake())
1097 {
1098 segment_sp.reset();
1099 segment_name.Clear();
1100 }
1101 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001102 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001103 {
1104 if (first_segment_sectID <= sectID)
1105 {
1106 lldb::user_id_t sect_uid;
1107 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1108 {
1109 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1110 SectionSP next_section_sp;
1111 if (sect_uid + 1 <= sectID)
1112 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1113
1114 if (curr_section_sp.get())
1115 {
1116 if (curr_section_sp->GetByteSize() == 0)
1117 {
1118 if (next_section_sp.get() != NULL)
1119 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1120 else
1121 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1122 }
1123 }
1124 }
1125 }
1126 }
1127 }
1128 }
1129 }
Greg Clayton1674b122010-07-21 22:12:05 +00001130 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001131 {
1132 m_dysymtab.cmd = load_cmd.cmd;
1133 m_dysymtab.cmdsize = load_cmd.cmdsize;
1134 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1135 }
1136
1137 offset = load_cmd_offset + load_cmd.cmdsize;
1138 }
1139// if (dump_sections)
1140// {
1141// StreamFile s(stdout);
1142// m_sections_ap->Dump(&s, true);
1143// }
1144 return sectID; // Return the number of sections we registered with the module
1145}
1146
1147class MachSymtabSectionInfo
1148{
1149public:
1150
1151 MachSymtabSectionInfo (SectionList *section_list) :
1152 m_section_list (section_list),
1153 m_section_infos()
1154 {
1155 // Get the number of sections down to a depth of 1 to include
1156 // all segments and their sections, but no other sections that
1157 // may be added for debug map or
1158 m_section_infos.resize(section_list->GetNumSections(1));
1159 }
1160
1161
Greg Clayton3508c382012-02-24 01:59:29 +00001162 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001163 GetSection (uint8_t n_sect, addr_t file_addr)
1164 {
1165 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001166 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001167 if (n_sect < m_section_infos.size())
1168 {
Greg Clayton3508c382012-02-24 01:59:29 +00001169 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001170 {
Greg Clayton3508c382012-02-24 01:59:29 +00001171 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1172 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001173 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001174 {
Greg Clayton3508c382012-02-24 01:59:29 +00001175 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1176 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001177 }
1178 else
1179 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001180 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001181 }
Chris Lattner24943d22010-06-08 16:52:24 +00001182 }
1183 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001184 {
1185 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001186 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001187 }
1188 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1189 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1190 {
1191 // Symbol is in section with zero size, but has the same start
1192 // address as the section. This can happen with linker symbols
1193 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001194 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001195 }
Chris Lattner24943d22010-06-08 16:52:24 +00001196 }
Greg Clayton3508c382012-02-24 01:59:29 +00001197 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001198 }
1199
1200protected:
1201 struct SectionInfo
1202 {
1203 SectionInfo () :
1204 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001205 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001206 {
1207 }
1208
1209 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001210 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001211 };
1212 SectionList *m_section_list;
1213 std::vector<SectionInfo> m_section_infos;
1214};
1215
Chris Lattner24943d22010-06-08 16:52:24 +00001216size_t
1217ObjectFileMachO::ParseSymtab (bool minimize)
1218{
1219 Timer scoped_timer(__PRETTY_FUNCTION__,
1220 "ObjectFileMachO::ParseSymtab () module = %s",
1221 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001222 ModuleSP module_sp (GetModule());
1223 if (!module_sp)
1224 return 0;
1225
1226 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1227 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1228 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1229 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001230 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001231 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001232
Greg Clayton0fea0512011-12-30 00:32:24 +00001233 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
1234
Chris Lattner24943d22010-06-08 16:52:24 +00001235 for (i=0; i<m_header.ncmds; ++i)
1236 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001237 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001238 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001239 struct load_command lc;
1240 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001241 break;
1242 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001243 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001244 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001245 case LoadCommandSymtab:
1246 symtab_load_command.cmd = lc.cmd;
1247 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001248 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001249 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1250 return 0;
1251 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001252 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001253 if (log)
1254 module_sp->LogMessage(log.get(), "LC_SYMTAB.symoff == 0");
1255 return 0;
1256 }
1257
1258 if (symtab_load_command.stroff == 0)
1259 {
1260 if (log)
1261 module_sp->LogMessage(log.get(), "LC_SYMTAB.stroff == 0");
1262 return 0;
1263 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001264
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001265 if (symtab_load_command.nsyms == 0)
1266 {
1267 if (log)
1268 module_sp->LogMessage(log.get(), "LC_SYMTAB.nsyms == 0");
1269 return 0;
1270 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001271
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001272 if (symtab_load_command.strsize == 0)
1273 {
1274 if (log)
1275 module_sp->LogMessage(log.get(), "LC_SYMTAB.strsize == 0");
1276 return 0;
1277 }
1278 break;
1279
1280 case LoadCommandFunctionStarts:
1281 function_starts_load_command.cmd = lc.cmd;
1282 function_starts_load_command.cmdsize = lc.cmdsize;
1283 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1284 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1285 break;
1286
1287 default:
1288 break;
1289 }
1290 offset = cmd_offset + lc.cmdsize;
1291 }
1292
1293 if (symtab_load_command.cmd)
1294 {
1295 Symtab *symtab = m_symtab_ap.get();
1296 SectionList *section_list = GetSectionList();
1297 if (section_list == NULL)
1298 return 0;
1299
1300 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001301 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001302
Greg Clayton36da2aa2013-01-25 18:06:21 +00001303 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1304 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001305 bool bit_width_32 = addr_byte_size == 4;
1306 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1307
Greg Clayton36da2aa2013-01-25 18:06:21 +00001308 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1309 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1310 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001311 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001312
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001313 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1314 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001315 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1316 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001317 {
Greg Claytondd29b972012-05-18 23:20:01 +00001318 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001319 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1320 // Reading mach file from memory in a process or core file...
1321
1322 if (linkedit_section_sp)
1323 {
1324 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1325 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1326 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001327 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001328
1329 bool data_was_read = false;
1330
1331#if defined (__APPLE__) && defined (__arm__)
1332 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001333 {
Greg Clayton29021d32012-04-18 05:19:20 +00001334 // This mach-o memory file is in the dyld shared cache. If this
1335 // program is not remote and this is iOS, then this process will
1336 // share the same shared cache as the process we are debugging and
1337 // we can read the entire __LINKEDIT from the address space in this
1338 // process. This is a needed optimization that is used for local iOS
1339 // debugging only since all shared libraries in the shared cache do
1340 // not have corresponding files that exist in the file system of the
1341 // device. They have been combined into a single file. This means we
1342 // always have to load these files from memory. All of the symbol and
1343 // string tables from all of the __LINKEDIT sections from the shared
1344 // libraries in the shared cache have been merged into a single large
1345 // symbol and string table. Reading all of this symbol and string table
1346 // data across can slow down debug launch times, so we optimize this by
1347 // reading the memory for the __LINKEDIT section from this process.
1348 PlatformSP platform_sp (target.GetPlatform());
Jason Molendab76f77e2012-12-13 01:13:00 +00001349 if (platform_sp && platform_sp->IsHost())
Greg Clayton29021d32012-04-18 05:19:20 +00001350 {
1351 data_was_read = true;
1352 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001353 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001354 if (function_starts_load_command.cmd)
1355 {
1356 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1357 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1358 }
1359 }
1360 }
1361#endif
1362
1363 if (!data_was_read)
1364 {
1365 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1366 if (nlist_data_sp)
1367 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001368 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1369 //if (strtab_data_sp)
1370 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001371 if (m_dysymtab.nindirectsyms != 0)
1372 {
1373 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1374 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1375 if (indirect_syms_data_sp)
1376 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1377 }
Greg Clayton29021d32012-04-18 05:19:20 +00001378 if (function_starts_load_command.cmd)
1379 {
1380 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1381 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1382 if (func_start_data_sp)
1383 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1384 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001385 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001386 }
1387 }
1388 else
1389 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001390 nlist_data.SetData (m_data,
1391 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001392 nlist_data_byte_size);
1393 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001394 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001395 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001396 if (m_dysymtab.nindirectsyms != 0)
1397 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001398 indirect_symbol_index_data.SetData (m_data,
1399 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001400 m_dysymtab.nindirectsyms * 4);
1401 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001402 if (function_starts_load_command.cmd)
1403 {
1404 function_starts_data.SetData (m_data,
1405 function_starts_load_command.dataoff,
1406 function_starts_load_command.datasize);
1407 }
1408 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001409
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001410 if (nlist_data.GetByteSize() == 0)
1411 {
1412 if (log)
1413 module_sp->LogMessage(log.get(), "failed to read nlist data");
1414 return 0;
1415 }
1416
1417
Greg Clayton3a5dc012012-05-25 17:04:00 +00001418 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1419 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001420 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001421 if (process)
1422 {
1423 if (strtab_addr == LLDB_INVALID_ADDRESS)
1424 {
1425 if (log)
1426 module_sp->LogMessage(log.get(), "failed to locate the strtab in memory");
1427 return 0;
1428 }
1429 }
1430 else
Greg Claytondd29b972012-05-18 23:20:01 +00001431 {
1432 if (log)
Greg Clayton3a5dc012012-05-25 17:04:00 +00001433 module_sp->LogMessage(log.get(), "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001434 return 0;
1435 }
1436 }
Greg Claytondd29b972012-05-18 23:20:01 +00001437
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001438 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1439 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1440 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1441 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1442 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1443 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1444 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1445 SectionSP eh_frame_section_sp;
1446 if (text_section_sp.get())
1447 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1448 else
1449 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1450
Greg Claytond2653c22012-03-14 01:53:24 +00001451 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001452 if (text_section_sp && function_starts_data.GetByteSize())
1453 {
1454 FunctionStarts::Entry function_start_entry;
1455 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001456 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001457 function_start_entry.addr = text_section_sp->GetFileAddress();
1458 uint64_t delta;
1459 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1460 {
1461 // Now append the current entry
1462 function_start_entry.addr += delta;
1463 function_starts.Append(function_start_entry);
1464 }
1465 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001466
Greg Clayton36da2aa2013-01-25 18:06:21 +00001467 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001468
Greg Clayton36da2aa2013-01-25 18:06:21 +00001469 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 +00001470
Greg Clayton36da2aa2013-01-25 18:06:21 +00001471 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001472
1473 uint32_t N_SO_index = UINT32_MAX;
1474
1475 MachSymtabSectionInfo section_info (section_list);
1476 std::vector<uint32_t> N_FUN_indexes;
1477 std::vector<uint32_t> N_NSYM_indexes;
1478 std::vector<uint32_t> N_INCL_indexes;
1479 std::vector<uint32_t> N_BRAC_indexes;
1480 std::vector<uint32_t> N_COMM_indexes;
1481 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1482 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1483 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1484 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1485 // Any symbols that get merged into another will get an entry
1486 // in this map so we know
1487 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1488 uint32_t nlist_idx = 0;
1489 Symbol *symbol_ptr = NULL;
1490
1491 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001492 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001493 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001494 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001495 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001496
Jason Molendab62abd52012-06-21 01:51:02 +00001497#if defined (__APPLE__) && defined (__arm__)
1498
1499 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1500 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1501 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1502 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1503 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1504 // nlist parser to ignore all LOCAL symbols.
1505
1506 if (m_header.flags & 0x80000000u)
1507 {
1508 // Before we can start mapping the DSC, we need to make certain the target process is actually
1509 // using the cache we can find.
1510
1511 /*
1512 * TODO (FIXME!)
1513 *
1514 * Consider the case of testing with a separate DSC file.
1515 * If we go through the normal code paths, we will give symbols for the wrong DSC, and
1516 * that is bad. We need to read the target process' all_image_infos struct, and look
1517 * at the values of the processDetachedFromSharedRegion field. If that is set, we should skip
1518 * this code section.
1519 */
1520
1521 // Next we need to determine the correct path for the dyld shared cache.
1522
1523 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1524 char dsc_path[PATH_MAX];
1525
1526 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001527 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1528 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001529 header_arch.GetArchitectureName());
1530
1531 FileSpec dsc_filespec(dsc_path, false);
1532
1533 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda6bcabae2013-03-06 23:17:36 +00001534 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001535 {
Jason Molenda6bcabae2013-03-06 23:17:36 +00001536 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1537 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1538 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001539 uint32_t imagesOffset;
1540 uint32_t imagesCount;
1541 uint64_t dyldBaseAddress;
1542 uint64_t codeSignatureOffset;
1543 uint64_t codeSignatureSize;
1544 uint64_t slideInfoOffset;
1545 uint64_t slideInfoSize;
Jason Molenda6bcabae2013-03-06 23:17:36 +00001546 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1547 uint64_t localSymbolsSize; // size of local symbols information
1548 };
1549 struct lldb_copy_dyld_cache_header_v1
1550 {
1551 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1552 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1553 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1554 uint32_t imagesOffset;
1555 uint32_t imagesCount;
1556 uint64_t dyldBaseAddress;
1557 uint64_t codeSignatureOffset;
1558 uint64_t codeSignatureSize;
1559 uint64_t slideInfoOffset;
1560 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001561 uint64_t localSymbolsOffset;
1562 uint64_t localSymbolsSize;
Jason Molenda6bcabae2013-03-06 23:17:36 +00001563 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001564 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001565
1566 struct lldb_copy_dyld_cache_mapping_info
1567 {
1568 uint64_t address;
1569 uint64_t size;
1570 uint64_t fileOffset;
1571 uint32_t maxProt;
1572 uint32_t initProt;
1573 };
1574
Greg Claytonab77dcb2012-09-05 22:30:51 +00001575 struct lldb_copy_dyld_cache_local_symbols_info
1576 {
1577 uint32_t nlistOffset;
1578 uint32_t nlistCount;
1579 uint32_t stringsOffset;
1580 uint32_t stringsSize;
1581 uint32_t entriesOffset;
1582 uint32_t entriesCount;
1583 };
1584 struct lldb_copy_dyld_cache_local_symbols_entry
1585 {
1586 uint32_t dylibOffset;
1587 uint32_t nlistStartIndex;
1588 uint32_t nlistCount;
1589 };
Jason Molendab62abd52012-06-21 01:51:02 +00001590
Jason Molendafd3b35d2012-06-22 03:28:35 +00001591 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1592 The dyld_cache_local_symbols_info structure gives us three things:
1593 1. The start and count of the nlist records in the dyld_shared_cache file
1594 2. The start and size of the strings for these nlist records
1595 3. The start and count of dyld_cache_local_symbols_entry entries
1596
1597 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1598 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001599 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001600 and the count of how many nlist records there are for this dylib/framework.
1601 */
1602
Jason Molendab62abd52012-06-21 01:51:02 +00001603 // Process the dsc header to find the unmapped symbols
1604 //
1605 // Save some VM space, do not map the entire cache in one shot.
1606
Jason Molenda6bcabae2013-03-06 23:17:36 +00001607 DataBufferSP dsc_data_sp;
1608 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1609
1610 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001611 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001612 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001613
Jason Molenda6bcabae2013-03-06 23:17:36 +00001614 char version_str[17];
1615 int version = -1;
1616 lldb::offset_t offset = 0;
1617 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1618 version_str[16] = '\0';
1619 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1620 {
1621 int v;
1622 if (::sscanf (version_str + 6, "%d", &v) == 1)
1623 {
1624 version = v;
1625 }
1626 }
1627
Jason Molenda9badb6c2013-03-06 23:19:17 +00001628 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001629
Jason Molendab62abd52012-06-21 01:51:02 +00001630 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1631
1632 // If the mappingOffset points to a location inside the header, we've
1633 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda6bcabae2013-03-06 23:17:36 +00001634 if ((version == 0 && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
1635 || (version >= 1 && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v1)))
Jason Molendab62abd52012-06-21 01:51:02 +00001636 {
1637
Jason Molenda6bcabae2013-03-06 23:17:36 +00001638 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1639 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1640 offset = 0;
1641
1642 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1643 // in the shared library cache need to be adjusted by an offset to match up with the
1644 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1645 // recorded in mapping_offset_value.
1646 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1647
1648 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001649 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1650 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1651
Jason Molenda9badb6c2013-03-06 23:19:17 +00001652 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001653 {
1654 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001655 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001656 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001657 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001658
1659 offset = 0;
1660
1661 // Read the local_symbols_infos struct in one shot
1662 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1663 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1664
Jason Molendab62abd52012-06-21 01:51:02 +00001665 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1666
Jason Molenda6bcabae2013-03-06 23:17:36 +00001667 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001668
1669 offset = local_symbols_info.entriesOffset;
1670 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1671 {
1672 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1673 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1674 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1675 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1676
Jason Molenda9badb6c2013-03-06 23:19:17 +00001677 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001678 {
1679 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1680
1681 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1682 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1683 num_syms = symtab->GetNumSymbols();
1684
1685 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1686 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1687
Jason Molenda9badb6c2013-03-06 23:19:17 +00001688 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001689 {
1690 /////////////////////////////
1691 {
1692 struct nlist_64 nlist;
1693 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1694 break;
1695
1696 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1697 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1698 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1699 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1700 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1701
1702 SymbolType type = eSymbolTypeInvalid;
1703 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1704
1705 if (symbol_name == NULL)
1706 {
1707 // No symbol should be NULL, even the symbols with no
1708 // string values should have an offset zero which points
1709 // to an empty C-string
1710 Host::SystemLog (Host::eSystemLogError,
1711 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1712 entry_index,
1713 nlist.n_strx,
1714 module_sp->GetFileSpec().GetDirectory().GetCString(),
1715 module_sp->GetFileSpec().GetFilename().GetCString());
1716 continue;
1717 }
1718 if (symbol_name[0] == '\0')
1719 symbol_name = NULL;
1720
1721 const char *symbol_name_non_abi_mangled = NULL;
1722
1723 SectionSP symbol_section;
1724 uint32_t symbol_byte_size = 0;
1725 bool add_nlist = true;
1726 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001727 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001728
1729 assert (sym_idx < num_syms);
1730
1731 sym[sym_idx].SetDebug (is_debug);
1732
1733 if (is_debug)
1734 {
1735 switch (nlist.n_type)
1736 {
1737 case StabGlobalSymbol:
1738 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1739 // Sometimes the N_GSYM value contains the address.
1740
1741 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1742 // have the same address, but we want to ensure that we always find only the real symbol,
1743 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1744 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1745 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1746 // same address.
1747
1748 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1749 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1750 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1751 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1752 add_nlist = false;
1753 else
1754 {
1755 sym[sym_idx].SetExternal(true);
1756 if (nlist.n_value != 0)
1757 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1758 type = eSymbolTypeData;
1759 }
1760 break;
1761
1762 case StabFunctionName:
1763 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1764 type = eSymbolTypeCompiler;
1765 break;
1766
1767 case StabFunction:
1768 // N_FUN -- procedure: name,,n_sect,linenumber,address
1769 if (symbol_name)
1770 {
1771 type = eSymbolTypeCode;
1772 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1773
1774 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1775 // We use the current number of symbols in the symbol table in lieu of
1776 // using nlist_idx in case we ever start trimming entries out
1777 N_FUN_indexes.push_back(sym_idx);
1778 }
1779 else
1780 {
1781 type = eSymbolTypeCompiler;
1782
1783 if ( !N_FUN_indexes.empty() )
1784 {
1785 // Copy the size of the function into the original STAB entry so we don't have
1786 // to hunt for it later
1787 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1788 N_FUN_indexes.pop_back();
1789 // We don't really need the end function STAB as it contains the size which
1790 // we already placed with the original symbol, so don't add it if we want a
1791 // minimal symbol table
1792 if (minimize)
1793 add_nlist = false;
1794 }
1795 }
1796 break;
1797
1798 case StabStaticSymbol:
1799 // N_STSYM -- static symbol: name,,n_sect,type,address
1800 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1801 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1802 type = eSymbolTypeData;
1803 break;
1804
1805 case StabLocalCommon:
1806 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1807 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1808 type = eSymbolTypeCommonBlock;
1809 break;
1810
1811 case StabBeginSymbol:
1812 // N_BNSYM
1813 // We use the current number of symbols in the symbol table in lieu of
1814 // using nlist_idx in case we ever start trimming entries out
1815 if (minimize)
1816 {
1817 // Skip these if we want minimal symbol tables
1818 add_nlist = false;
1819 }
1820 else
1821 {
1822 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1823 N_NSYM_indexes.push_back(sym_idx);
1824 type = eSymbolTypeScopeBegin;
1825 }
1826 break;
1827
1828 case StabEndSymbol:
1829 // N_ENSYM
1830 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1831 // so that we can always skip the entire symbol if we need to navigate
1832 // more quickly at the source level when parsing STABS
1833 if (minimize)
1834 {
1835 // Skip these if we want minimal symbol tables
1836 add_nlist = false;
1837 }
1838 else
1839 {
1840 if ( !N_NSYM_indexes.empty() )
1841 {
1842 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1843 symbol_ptr->SetByteSize(sym_idx + 1);
1844 symbol_ptr->SetSizeIsSibling(true);
1845 N_NSYM_indexes.pop_back();
1846 }
1847 type = eSymbolTypeScopeEnd;
1848 }
1849 break;
1850
1851
1852 case StabSourceFileOptions:
1853 // N_OPT - emitted with gcc2_compiled and in gcc source
1854 type = eSymbolTypeCompiler;
1855 break;
1856
1857 case StabRegisterSymbol:
1858 // N_RSYM - register sym: name,,NO_SECT,type,register
1859 type = eSymbolTypeVariable;
1860 break;
1861
1862 case StabSourceLine:
1863 // N_SLINE - src line: 0,,n_sect,linenumber,address
1864 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1865 type = eSymbolTypeLineEntry;
1866 break;
1867
1868 case StabStructureType:
1869 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1870 type = eSymbolTypeVariableType;
1871 break;
1872
1873 case StabSourceFileName:
1874 // N_SO - source file name
1875 type = eSymbolTypeSourceFile;
1876 if (symbol_name == NULL)
1877 {
1878 if (minimize)
1879 add_nlist = false;
1880 if (N_SO_index != UINT32_MAX)
1881 {
1882 // Set the size of the N_SO to the terminating index of this N_SO
1883 // so that we can always skip the entire N_SO if we need to navigate
1884 // more quickly at the source level when parsing STABS
1885 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1886 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1887 symbol_ptr->SetSizeIsSibling(true);
1888 }
1889 N_NSYM_indexes.clear();
1890 N_INCL_indexes.clear();
1891 N_BRAC_indexes.clear();
1892 N_COMM_indexes.clear();
1893 N_FUN_indexes.clear();
1894 N_SO_index = UINT32_MAX;
1895 }
1896 else
1897 {
1898 // We use the current number of symbols in the symbol table in lieu of
1899 // using nlist_idx in case we ever start trimming entries out
1900 const bool N_SO_has_full_path = symbol_name[0] == '/';
1901 if (N_SO_has_full_path)
1902 {
1903 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1904 {
1905 // We have two consecutive N_SO entries where the first contains a directory
1906 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00001907 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001908 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1909 add_nlist = false;
1910 }
1911 else
1912 {
1913 // This is the first entry in a N_SO that contains a directory or
1914 // a full path to the source file
1915 N_SO_index = sym_idx;
1916 }
1917 }
1918 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1919 {
1920 // This is usually the second N_SO entry that contains just the filename,
1921 // so here we combine it with the first one if we are minimizing the symbol table
1922 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1923 if (so_path && so_path[0])
1924 {
1925 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00001926 const size_t double_slash_pos = full_so_path.find("//");
1927 if (double_slash_pos != std::string::npos)
1928 {
1929 // The linker has been generating bad N_SO entries with doubled up paths
1930 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
1931 // and the second is the directory for the source file so you end up with
1932 // a path that looks like "/tmp/src//tmp/src/"
1933 FileSpec so_dir(so_path, false);
1934 if (!so_dir.Exists())
1935 {
1936 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
1937 if (so_dir.Exists())
1938 {
1939 // Trim off the incorrect path
1940 full_so_path.erase(0, double_slash_pos + 1);
1941 }
1942 }
1943 }
Jason Molendab62abd52012-06-21 01:51:02 +00001944 if (*full_so_path.rbegin() != '/')
1945 full_so_path += '/';
1946 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00001947 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001948 add_nlist = false;
1949 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1950 }
1951 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00001952 else
1953 {
1954 // This could be a relative path to a N_SO
1955 N_SO_index = sym_idx;
1956 }
Jason Molendab62abd52012-06-21 01:51:02 +00001957 }
Jason Molendab62abd52012-06-21 01:51:02 +00001958 break;
1959
1960 case StabObjectFileName:
1961 // N_OSO - object file name: name,,0,0,st_mtime
1962 type = eSymbolTypeObjectFile;
1963 break;
1964
1965 case StabLocalSymbol:
1966 // N_LSYM - local sym: name,,NO_SECT,type,offset
1967 type = eSymbolTypeLocal;
1968 break;
1969
1970 //----------------------------------------------------------------------
1971 // INCL scopes
1972 //----------------------------------------------------------------------
1973 case StabBeginIncludeFileName:
1974 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
1975 // We use the current number of symbols in the symbol table in lieu of
1976 // using nlist_idx in case we ever start trimming entries out
1977 N_INCL_indexes.push_back(sym_idx);
1978 type = eSymbolTypeScopeBegin;
1979 break;
1980
1981 case StabEndIncludeFile:
1982 // N_EINCL - include file end: name,,NO_SECT,0,0
1983 // Set the size of the N_BINCL to the terminating index of this N_EINCL
1984 // so that we can always skip the entire symbol if we need to navigate
1985 // more quickly at the source level when parsing STABS
1986 if ( !N_INCL_indexes.empty() )
1987 {
1988 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1989 symbol_ptr->SetByteSize(sym_idx + 1);
1990 symbol_ptr->SetSizeIsSibling(true);
1991 N_INCL_indexes.pop_back();
1992 }
1993 type = eSymbolTypeScopeEnd;
1994 break;
1995
1996 case StabIncludeFileName:
1997 // N_SOL - #included file name: name,,n_sect,0,address
1998 type = eSymbolTypeHeaderFile;
1999
2000 // We currently don't use the header files on darwin
2001 if (minimize)
2002 add_nlist = false;
2003 break;
2004
2005 case StabCompilerParameters:
2006 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2007 type = eSymbolTypeCompiler;
2008 break;
2009
2010 case StabCompilerVersion:
2011 // N_VERSION - compiler version: name,,NO_SECT,0,0
2012 type = eSymbolTypeCompiler;
2013 break;
2014
2015 case StabCompilerOptLevel:
2016 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2017 type = eSymbolTypeCompiler;
2018 break;
2019
2020 case StabParameter:
2021 // N_PSYM - parameter: name,,NO_SECT,type,offset
2022 type = eSymbolTypeVariable;
2023 break;
2024
2025 case StabAlternateEntry:
2026 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2027 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2028 type = eSymbolTypeLineEntry;
2029 break;
2030
2031 //----------------------------------------------------------------------
2032 // Left and Right Braces
2033 //----------------------------------------------------------------------
2034 case StabLeftBracket:
2035 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2036 // We use the current number of symbols in the symbol table in lieu of
2037 // using nlist_idx in case we ever start trimming entries out
2038 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2039 N_BRAC_indexes.push_back(sym_idx);
2040 type = eSymbolTypeScopeBegin;
2041 break;
2042
2043 case StabRightBracket:
2044 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2045 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2046 // so that we can always skip the entire symbol if we need to navigate
2047 // more quickly at the source level when parsing STABS
2048 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2049 if ( !N_BRAC_indexes.empty() )
2050 {
2051 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2052 symbol_ptr->SetByteSize(sym_idx + 1);
2053 symbol_ptr->SetSizeIsSibling(true);
2054 N_BRAC_indexes.pop_back();
2055 }
2056 type = eSymbolTypeScopeEnd;
2057 break;
2058
2059 case StabDeletedIncludeFile:
2060 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2061 type = eSymbolTypeHeaderFile;
2062 break;
2063
2064 //----------------------------------------------------------------------
2065 // COMM scopes
2066 //----------------------------------------------------------------------
2067 case StabBeginCommon:
2068 // N_BCOMM - begin common: name,,NO_SECT,0,0
2069 // We use the current number of symbols in the symbol table in lieu of
2070 // using nlist_idx in case we ever start trimming entries out
2071 type = eSymbolTypeScopeBegin;
2072 N_COMM_indexes.push_back(sym_idx);
2073 break;
2074
2075 case StabEndCommonLocal:
2076 // N_ECOML - end common (local name): 0,,n_sect,0,address
2077 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2078 // Fall through
2079
2080 case StabEndCommon:
2081 // N_ECOMM - end common: name,,n_sect,0,0
2082 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2083 // so that we can always skip the entire symbol if we need to navigate
2084 // more quickly at the source level when parsing STABS
2085 if ( !N_COMM_indexes.empty() )
2086 {
2087 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2088 symbol_ptr->SetByteSize(sym_idx + 1);
2089 symbol_ptr->SetSizeIsSibling(true);
2090 N_COMM_indexes.pop_back();
2091 }
2092 type = eSymbolTypeScopeEnd;
2093 break;
2094
2095 case StabLength:
2096 // N_LENG - second stab entry with length information
2097 type = eSymbolTypeAdditional;
2098 break;
2099
2100 default: break;
2101 }
2102 }
2103 else
2104 {
2105 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2106 uint8_t n_type = NlistMaskType & nlist.n_type;
2107 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2108
2109 switch (n_type)
2110 {
2111 case NListTypeIndirect: // N_INDR - Fall through
2112 case NListTypePreboundUndefined:// N_PBUD - Fall through
2113 case NListTypeUndefined: // N_UNDF
2114 type = eSymbolTypeUndefined;
2115 break;
2116
2117 case NListTypeAbsolute: // N_ABS
2118 type = eSymbolTypeAbsolute;
2119 break;
2120
2121 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002122 {
2123 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002124
Greg Clayton01e6a582012-11-27 01:52:16 +00002125 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002126 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002127 // TODO: warn about this?
2128 add_nlist = false;
2129 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002130 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002131
2132 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002133 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002134 type = eSymbolTypeException;
2135 }
2136 else
2137 {
2138 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002139
Greg Clayton01e6a582012-11-27 01:52:16 +00002140 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002141 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002142 case SectionTypeRegular: break; // regular section
2143 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2144 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2145 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2146 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2147 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2148 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2149 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2150 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2151 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2152 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2153 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2154 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2155 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2156 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2157 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2158 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2159 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002160 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002161
Greg Clayton01e6a582012-11-27 01:52:16 +00002162 if (type == eSymbolTypeInvalid)
2163 {
2164 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2165 if (symbol_section->IsDescendant (text_section_sp.get()))
2166 {
2167 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2168 SectionAttrUserSelfModifyingCode |
2169 SectionAttrSytemSomeInstructions))
2170 type = eSymbolTypeData;
2171 else
2172 type = eSymbolTypeCode;
2173 }
2174 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002175 {
2176 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2177 {
2178 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002179
Greg Clayton01e6a582012-11-27 01:52:16 +00002180 if (symbol_name &&
2181 symbol_name[0] == '_' &&
2182 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002183 symbol_name[2] == 'B')
2184 {
2185 llvm::StringRef symbol_name_ref(symbol_name);
2186 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2187 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2188 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2189 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2190 {
2191 symbol_name_non_abi_mangled = symbol_name + 1;
2192 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2193 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002194 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002195 }
2196 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2197 {
2198 symbol_name_non_abi_mangled = symbol_name + 1;
2199 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2200 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002201 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002202 }
2203 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2204 {
2205 symbol_name_non_abi_mangled = symbol_name + 1;
2206 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2207 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002208 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002209 }
2210 }
2211 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002212 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002213 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002214 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002215 }
2216 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002217 {
2218 type = eSymbolTypeData;
2219 }
2220 }
2221 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2222 {
2223 type = eSymbolTypeTrampoline;
2224 }
2225 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2226 {
2227 type = eSymbolTypeRuntime;
2228 if (symbol_name && symbol_name[0] == '.')
2229 {
2230 llvm::StringRef symbol_name_ref(symbol_name);
2231 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2232 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002233 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002234 symbol_name_non_abi_mangled = symbol_name;
2235 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2236 type = eSymbolTypeObjCClass;
2237 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002238 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002239 }
2240 }
2241 }
Jason Molendab62abd52012-06-21 01:51:02 +00002242 }
2243 }
Jason Molendab62abd52012-06-21 01:51:02 +00002244 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002245 }
Jason Molendab62abd52012-06-21 01:51:02 +00002246 }
2247
2248 if (add_nlist)
2249 {
2250 uint64_t symbol_value = nlist.n_value;
2251 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002252
Jason Molendab62abd52012-06-21 01:51:02 +00002253 if (symbol_name_non_abi_mangled)
2254 {
Jason Molenda292cca82012-07-20 03:35:44 +00002255 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2256 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002257 }
2258 else
2259 {
2260 if (symbol_name && symbol_name[0] == '_')
2261 {
2262 symbol_name_is_mangled = symbol_name[1] == '_';
2263 symbol_name++; // Skip the leading underscore
2264 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002265
Jason Molendab62abd52012-06-21 01:51:02 +00002266 if (symbol_name)
2267 {
Jason Molenda292cca82012-07-20 03:35:44 +00002268 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002269 }
2270 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002271
Jason Molendab62abd52012-06-21 01:51:02 +00002272 if (is_debug == false)
2273 {
2274 if (type == eSymbolTypeCode)
2275 {
2276 // See if we can find a N_FUN entry for any code symbols.
2277 // If we do find a match, and the name matches, then we
2278 // can merge the two into just the function symbol to avoid
2279 // duplicate entries in the symbol table
2280 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2281 if (pos != N_FUN_addr_to_sym_idx.end())
2282 {
2283 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2284 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2285 {
2286 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2287 // We just need the flags from the linker symbol, so put these flags
2288 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2289 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2290 sym[sym_idx].Clear();
2291 continue;
2292 }
2293 }
2294 }
2295 else if (type == eSymbolTypeData)
2296 {
2297 // See if we can find a N_STSYM entry for any data symbols.
2298 // If we do find a match, and the name matches, then we
2299 // can merge the two into just the Static symbol to avoid
2300 // duplicate entries in the symbol table
2301 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2302 if (pos != N_STSYM_addr_to_sym_idx.end())
2303 {
2304 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2305 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2306 {
2307 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2308 // We just need the flags from the linker symbol, so put these flags
2309 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2310 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2311 sym[sym_idx].Clear();
2312 continue;
2313 }
2314 }
2315 }
2316 }
2317 if (symbol_section)
2318 {
2319 const addr_t section_file_addr = symbol_section->GetFileAddress();
2320 if (symbol_byte_size == 0 && function_starts_count > 0)
2321 {
2322 addr_t symbol_lookup_file_addr = nlist.n_value;
2323 // Do an exact address match for non-ARM addresses, else get the closest since
2324 // the symbol might be a thumb symbol which has an address with bit zero set
2325 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2326 if (is_arm && func_start_entry)
2327 {
2328 // Verify that the function start address is the symbol address (ARM)
2329 // or the symbol address + 1 (thumb)
2330 if (func_start_entry->addr != symbol_lookup_file_addr &&
2331 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2332 {
2333 // Not the right entry, NULL it out...
2334 func_start_entry = NULL;
2335 }
2336 }
2337 if (func_start_entry)
2338 {
2339 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002340
Jason Molendab62abd52012-06-21 01:51:02 +00002341 addr_t symbol_file_addr = func_start_entry->addr;
2342 uint32_t symbol_flags = 0;
2343 if (is_arm)
2344 {
2345 if (symbol_file_addr & 1)
2346 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2347 symbol_file_addr &= 0xfffffffffffffffeull;
2348 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002349
Jason Molendab62abd52012-06-21 01:51:02 +00002350 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2351 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2352 if (next_func_start_entry)
2353 {
2354 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2355 // Be sure the clear the Thumb address bit when we calculate the size
2356 // from the current and next address
2357 if (is_arm)
2358 next_symbol_file_addr &= 0xfffffffffffffffeull;
2359 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2360 }
2361 else
2362 {
2363 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2364 }
2365 }
2366 }
2367 symbol_value -= section_file_addr;
2368 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002369
Jason Molendab62abd52012-06-21 01:51:02 +00002370 sym[sym_idx].SetID (nlist_idx);
2371 sym[sym_idx].SetType (type);
2372 sym[sym_idx].GetAddress().SetSection (symbol_section);
2373 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2374 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002375
Jason Molendab62abd52012-06-21 01:51:02 +00002376 if (symbol_byte_size > 0)
2377 sym[sym_idx].SetByteSize(symbol_byte_size);
2378
Greg Clayton01e6a582012-11-27 01:52:16 +00002379 if (demangled_is_synthesized)
2380 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002381 ++sym_idx;
2382 }
2383 else
2384 {
2385 sym[sym_idx].Clear();
2386 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002387
Jason Molendab62abd52012-06-21 01:51:02 +00002388 }
2389 /////////////////////////////
2390 }
2391 break; // No more entries to consider
2392 }
2393 }
2394 }
2395 }
2396 }
2397 }
2398 }
2399
2400 // Must reset this in case it was mutated above!
2401 nlist_data_offset = 0;
2402#endif
2403
2404 // If the sym array was not created while parsing the DSC unmapped
2405 // symbols, create it now.
2406 if (sym == NULL)
2407 {
2408 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2409 num_syms = symtab->GetNumSymbols();
2410 }
2411
2412 if (unmapped_local_symbols_found)
2413 {
2414 assert(m_dysymtab.ilocalsym == 0);
2415 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2416 nlist_idx = m_dysymtab.nlocalsym;
2417 }
2418 else
2419 {
2420 nlist_idx = 0;
2421 }
2422
2423 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002424 {
2425 struct nlist_64 nlist;
2426 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2427 break;
2428
2429 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2430 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2431 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2432 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2433 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2434
2435 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002436 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002437
Greg Clayton3a5dc012012-05-25 17:04:00 +00002438 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002439 {
2440 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002441
Greg Claytondd29b972012-05-18 23:20:01 +00002442 if (symbol_name == NULL)
2443 {
2444 // No symbol should be NULL, even the symbols with no
2445 // string values should have an offset zero which points
2446 // to an empty C-string
2447 Host::SystemLog (Host::eSystemLogError,
Jason Molenda9badb6c2013-03-06 23:19:17 +00002448 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002449 nlist_idx,
2450 nlist.n_strx,
2451 module_sp->GetFileSpec().GetDirectory().GetCString(),
2452 module_sp->GetFileSpec().GetFilename().GetCString());
2453 continue;
2454 }
2455 if (symbol_name[0] == '\0')
2456 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002457 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002458 else
2459 {
2460 const addr_t str_addr = strtab_addr + nlist.n_strx;
2461 Error str_error;
2462 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2463 symbol_name = memory_symbol_name.c_str();
2464 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002465 const char *symbol_name_non_abi_mangled = NULL;
2466
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002467 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002468 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002469 bool add_nlist = true;
2470 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002471 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002472
2473 assert (sym_idx < num_syms);
2474
2475 sym[sym_idx].SetDebug (is_debug);
2476
2477 if (is_debug)
2478 {
2479 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002480 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002481 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002482 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2483 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002484
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002485 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2486 // have the same address, but we want to ensure that we always find only the real symbol,
2487 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2488 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2489 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2490 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002491
2492 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002493 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2494 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2495 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2496 add_nlist = false;
2497 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002498 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002499 sym[sym_idx].SetExternal(true);
2500 if (nlist.n_value != 0)
2501 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2502 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002503 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002504 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002505
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002506 case StabFunctionName:
2507 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2508 type = eSymbolTypeCompiler;
2509 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002510
Jason Molenda9badb6c2013-03-06 23:19:17 +00002511 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002512 // N_FUN -- procedure: name,,n_sect,linenumber,address
2513 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002514 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002515 type = eSymbolTypeCode;
2516 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002517
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002518 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2519 // We use the current number of symbols in the symbol table in lieu of
2520 // using nlist_idx in case we ever start trimming entries out
2521 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002522 }
2523 else
2524 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002525 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002526
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002527 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002528 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002529 // Copy the size of the function into the original STAB entry so we don't have
2530 // to hunt for it later
2531 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2532 N_FUN_indexes.pop_back();
2533 // We don't really need the end function STAB as it contains the size which
2534 // we already placed with the original symbol, so don't add it if we want a
2535 // minimal symbol table
2536 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002537 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002538 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002539 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002540 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002541
Jason Molenda9badb6c2013-03-06 23:19:17 +00002542 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002543 // N_STSYM -- static symbol: name,,n_sect,type,address
2544 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2545 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2546 type = eSymbolTypeData;
2547 break;
2548
2549 case StabLocalCommon:
2550 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2551 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2552 type = eSymbolTypeCommonBlock;
2553 break;
2554
2555 case StabBeginSymbol:
2556 // N_BNSYM
2557 // We use the current number of symbols in the symbol table in lieu of
2558 // using nlist_idx in case we ever start trimming entries out
2559 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002560 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002561 // Skip these if we want minimal symbol tables
2562 add_nlist = false;
2563 }
2564 else
2565 {
2566 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2567 N_NSYM_indexes.push_back(sym_idx);
2568 type = eSymbolTypeScopeBegin;
2569 }
2570 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002571
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002572 case StabEndSymbol:
2573 // N_ENSYM
2574 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2575 // so that we can always skip the entire symbol if we need to navigate
2576 // more quickly at the source level when parsing STABS
2577 if (minimize)
2578 {
2579 // Skip these if we want minimal symbol tables
2580 add_nlist = false;
2581 }
2582 else
2583 {
2584 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002585 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002586 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2587 symbol_ptr->SetByteSize(sym_idx + 1);
2588 symbol_ptr->SetSizeIsSibling(true);
2589 N_NSYM_indexes.pop_back();
2590 }
2591 type = eSymbolTypeScopeEnd;
2592 }
2593 break;
2594
2595
2596 case StabSourceFileOptions:
2597 // N_OPT - emitted with gcc2_compiled and in gcc source
2598 type = eSymbolTypeCompiler;
2599 break;
2600
2601 case StabRegisterSymbol:
2602 // N_RSYM - register sym: name,,NO_SECT,type,register
2603 type = eSymbolTypeVariable;
2604 break;
2605
2606 case StabSourceLine:
2607 // N_SLINE - src line: 0,,n_sect,linenumber,address
2608 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2609 type = eSymbolTypeLineEntry;
2610 break;
2611
2612 case StabStructureType:
2613 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2614 type = eSymbolTypeVariableType;
2615 break;
2616
2617 case StabSourceFileName:
2618 // N_SO - source file name
2619 type = eSymbolTypeSourceFile;
2620 if (symbol_name == NULL)
2621 {
2622 if (minimize)
2623 add_nlist = false;
2624 if (N_SO_index != UINT32_MAX)
2625 {
2626 // Set the size of the N_SO to the terminating index of this N_SO
2627 // so that we can always skip the entire N_SO if we need to navigate
2628 // more quickly at the source level when parsing STABS
2629 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2630 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2631 symbol_ptr->SetSizeIsSibling(true);
2632 }
2633 N_NSYM_indexes.clear();
2634 N_INCL_indexes.clear();
2635 N_BRAC_indexes.clear();
2636 N_COMM_indexes.clear();
2637 N_FUN_indexes.clear();
2638 N_SO_index = UINT32_MAX;
2639 }
2640 else
2641 {
2642 // We use the current number of symbols in the symbol table in lieu of
2643 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002644 const bool N_SO_has_full_path = symbol_name[0] == '/';
2645 if (N_SO_has_full_path)
2646 {
2647 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2648 {
2649 // We have two consecutive N_SO entries where the first contains a directory
2650 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002651 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002652 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2653 add_nlist = false;
2654 }
2655 else
2656 {
2657 // This is the first entry in a N_SO that contains a directory or
2658 // a full path to the source file
2659 N_SO_index = sym_idx;
2660 }
2661 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002662 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2663 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002664 // This is usually the second N_SO entry that contains just the filename,
2665 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002666 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2667 if (so_path && so_path[0])
2668 {
2669 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002670 const size_t double_slash_pos = full_so_path.find("//");
2671 if (double_slash_pos != std::string::npos)
2672 {
2673 // The linker has been generating bad N_SO entries with doubled up paths
2674 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2675 // and the second is the directory for the source file so you end up with
2676 // a path that looks like "/tmp/src//tmp/src/"
2677 FileSpec so_dir(so_path, false);
2678 if (!so_dir.Exists())
2679 {
2680 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2681 if (so_dir.Exists())
2682 {
2683 // Trim off the incorrect path
2684 full_so_path.erase(0, double_slash_pos + 1);
2685 }
2686 }
2687 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002688 if (*full_so_path.rbegin() != '/')
2689 full_so_path += '/';
2690 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002691 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002692 add_nlist = false;
2693 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2694 }
2695 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002696 else
2697 {
2698 // This could be a relative path to a N_SO
2699 N_SO_index = sym_idx;
2700 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002701 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002702
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002703 break;
2704
2705 case StabObjectFileName:
2706 // N_OSO - object file name: name,,0,0,st_mtime
2707 type = eSymbolTypeObjectFile;
2708 break;
2709
2710 case StabLocalSymbol:
2711 // N_LSYM - local sym: name,,NO_SECT,type,offset
2712 type = eSymbolTypeLocal;
2713 break;
2714
2715 //----------------------------------------------------------------------
2716 // INCL scopes
2717 //----------------------------------------------------------------------
2718 case StabBeginIncludeFileName:
2719 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2720 // We use the current number of symbols in the symbol table in lieu of
2721 // using nlist_idx in case we ever start trimming entries out
2722 N_INCL_indexes.push_back(sym_idx);
2723 type = eSymbolTypeScopeBegin;
2724 break;
2725
2726 case StabEndIncludeFile:
2727 // N_EINCL - include file end: name,,NO_SECT,0,0
2728 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2729 // so that we can always skip the entire symbol if we need to navigate
2730 // more quickly at the source level when parsing STABS
2731 if ( !N_INCL_indexes.empty() )
2732 {
2733 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2734 symbol_ptr->SetByteSize(sym_idx + 1);
2735 symbol_ptr->SetSizeIsSibling(true);
2736 N_INCL_indexes.pop_back();
2737 }
2738 type = eSymbolTypeScopeEnd;
2739 break;
2740
2741 case StabIncludeFileName:
2742 // N_SOL - #included file name: name,,n_sect,0,address
2743 type = eSymbolTypeHeaderFile;
2744
2745 // We currently don't use the header files on darwin
2746 if (minimize)
2747 add_nlist = false;
2748 break;
2749
Jason Molenda9badb6c2013-03-06 23:19:17 +00002750 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002751 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2752 type = eSymbolTypeCompiler;
2753 break;
2754
2755 case StabCompilerVersion:
2756 // N_VERSION - compiler version: name,,NO_SECT,0,0
2757 type = eSymbolTypeCompiler;
2758 break;
2759
2760 case StabCompilerOptLevel:
2761 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2762 type = eSymbolTypeCompiler;
2763 break;
2764
2765 case StabParameter:
2766 // N_PSYM - parameter: name,,NO_SECT,type,offset
2767 type = eSymbolTypeVariable;
2768 break;
2769
2770 case StabAlternateEntry:
2771 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2772 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2773 type = eSymbolTypeLineEntry;
2774 break;
2775
2776 //----------------------------------------------------------------------
2777 // Left and Right Braces
2778 //----------------------------------------------------------------------
2779 case StabLeftBracket:
2780 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2781 // We use the current number of symbols in the symbol table in lieu of
2782 // using nlist_idx in case we ever start trimming entries out
2783 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2784 N_BRAC_indexes.push_back(sym_idx);
2785 type = eSymbolTypeScopeBegin;
2786 break;
2787
2788 case StabRightBracket:
2789 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2790 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2791 // so that we can always skip the entire symbol if we need to navigate
2792 // more quickly at the source level when parsing STABS
2793 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2794 if ( !N_BRAC_indexes.empty() )
2795 {
2796 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2797 symbol_ptr->SetByteSize(sym_idx + 1);
2798 symbol_ptr->SetSizeIsSibling(true);
2799 N_BRAC_indexes.pop_back();
2800 }
2801 type = eSymbolTypeScopeEnd;
2802 break;
2803
2804 case StabDeletedIncludeFile:
2805 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2806 type = eSymbolTypeHeaderFile;
2807 break;
2808
2809 //----------------------------------------------------------------------
2810 // COMM scopes
2811 //----------------------------------------------------------------------
2812 case StabBeginCommon:
2813 // N_BCOMM - begin common: name,,NO_SECT,0,0
2814 // We use the current number of symbols in the symbol table in lieu of
2815 // using nlist_idx in case we ever start trimming entries out
2816 type = eSymbolTypeScopeBegin;
2817 N_COMM_indexes.push_back(sym_idx);
2818 break;
2819
2820 case StabEndCommonLocal:
2821 // N_ECOML - end common (local name): 0,,n_sect,0,address
2822 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2823 // Fall through
2824
2825 case StabEndCommon:
2826 // N_ECOMM - end common: name,,n_sect,0,0
2827 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2828 // so that we can always skip the entire symbol if we need to navigate
2829 // more quickly at the source level when parsing STABS
2830 if ( !N_COMM_indexes.empty() )
2831 {
2832 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2833 symbol_ptr->SetByteSize(sym_idx + 1);
2834 symbol_ptr->SetSizeIsSibling(true);
2835 N_COMM_indexes.pop_back();
2836 }
2837 type = eSymbolTypeScopeEnd;
2838 break;
2839
2840 case StabLength:
2841 // N_LENG - second stab entry with length information
2842 type = eSymbolTypeAdditional;
2843 break;
2844
2845 default: break;
2846 }
2847 }
2848 else
2849 {
2850 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2851 uint8_t n_type = NlistMaskType & nlist.n_type;
2852 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2853
2854 switch (n_type)
2855 {
2856 case NListTypeIndirect: // N_INDR - Fall through
2857 case NListTypePreboundUndefined:// N_PBUD - Fall through
2858 case NListTypeUndefined: // N_UNDF
2859 type = eSymbolTypeUndefined;
2860 break;
2861
2862 case NListTypeAbsolute: // N_ABS
2863 type = eSymbolTypeAbsolute;
2864 break;
2865
2866 case NListTypeSection: // N_SECT
2867 {
2868 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2869
Sean Callananb386d822012-08-09 00:50:26 +00002870 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002871 {
2872 // TODO: warn about this?
2873 add_nlist = false;
2874 break;
2875 }
2876
2877 if (TEXT_eh_frame_sectID == nlist.n_sect)
2878 {
2879 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00002880 }
2881 else
2882 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002883 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2884
2885 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00002886 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002887 case SectionTypeRegular: break; // regular section
2888 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2889 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2890 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2891 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2892 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2893 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2894 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2895 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2896 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2897 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2898 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2899 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2900 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2901 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2902 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2903 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2904 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002905 }
Chris Lattner24943d22010-06-08 16:52:24 +00002906
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002907 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002908 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002909 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2910 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002911 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002912 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2913 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002914 SectionAttrSytemSomeInstructions))
2915 type = eSymbolTypeData;
2916 else
2917 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00002918 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002919 else
2920 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002921 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002922 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00002923 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002924 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00002925
Jason Molenda9badb6c2013-03-06 23:19:17 +00002926 if (symbol_name &&
2927 symbol_name[0] == '_' &&
2928 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002929 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00002930 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002931 llvm::StringRef symbol_name_ref(symbol_name);
2932 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2933 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2934 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2935 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00002936 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002937 symbol_name_non_abi_mangled = symbol_name + 1;
2938 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2939 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002940 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00002941 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002942 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00002943 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002944 symbol_name_non_abi_mangled = symbol_name + 1;
2945 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2946 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002947 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002948 }
2949 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2950 {
2951 symbol_name_non_abi_mangled = symbol_name + 1;
2952 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2953 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002954 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00002955 }
2956 }
2957 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002958 else
2959 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
2960 {
2961 type = eSymbolTypeException;
2962 }
2963 else
2964 {
2965 type = eSymbolTypeData;
2966 }
2967 }
2968 else
2969 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2970 {
2971 type = eSymbolTypeTrampoline;
2972 }
2973 else
2974 if (symbol_section->IsDescendant(objc_section_sp.get()))
2975 {
2976 type = eSymbolTypeRuntime;
2977 if (symbol_name && symbol_name[0] == '.')
2978 {
2979 llvm::StringRef symbol_name_ref(symbol_name);
2980 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2981 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
2982 {
2983 symbol_name_non_abi_mangled = symbol_name;
2984 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2985 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002986 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002987 }
2988 }
Chris Lattner24943d22010-06-08 16:52:24 +00002989 }
2990 }
2991 }
2992 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002993 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002994 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002995 }
2996
2997 if (add_nlist)
2998 {
2999 uint64_t symbol_value = nlist.n_value;
3000 bool symbol_name_is_mangled = false;
3001
3002 if (symbol_name_non_abi_mangled)
3003 {
Greg Claytonc0240042012-07-18 23:18:10 +00003004 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3005 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003006 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003007 else
3008 {
3009 if (symbol_name && symbol_name[0] == '_')
3010 {
3011 symbol_name_is_mangled = symbol_name[1] == '_';
3012 symbol_name++; // Skip the leading underscore
3013 }
3014
3015 if (symbol_name)
3016 {
Greg Claytonc0240042012-07-18 23:18:10 +00003017 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003018 }
3019 }
3020
3021 if (is_debug == false)
3022 {
3023 if (type == eSymbolTypeCode)
3024 {
3025 // See if we can find a N_FUN entry for any code symbols.
3026 // If we do find a match, and the name matches, then we
3027 // can merge the two into just the function symbol to avoid
3028 // duplicate entries in the symbol table
3029 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3030 if (pos != N_FUN_addr_to_sym_idx.end())
3031 {
3032 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3033 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3034 {
3035 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3036 // We just need the flags from the linker symbol, so put these flags
3037 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3038 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3039 sym[sym_idx].Clear();
3040 continue;
3041 }
3042 }
3043 }
3044 else if (type == eSymbolTypeData)
3045 {
3046 // See if we can find a N_STSYM entry for any data symbols.
3047 // If we do find a match, and the name matches, then we
3048 // can merge the two into just the Static symbol to avoid
3049 // duplicate entries in the symbol table
3050 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3051 if (pos != N_STSYM_addr_to_sym_idx.end())
3052 {
3053 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3054 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3055 {
3056 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3057 // We just need the flags from the linker symbol, so put these flags
3058 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3059 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3060 sym[sym_idx].Clear();
3061 continue;
3062 }
3063 }
3064 }
3065 }
3066 if (symbol_section)
3067 {
3068 const addr_t section_file_addr = symbol_section->GetFileAddress();
3069 if (symbol_byte_size == 0 && function_starts_count > 0)
3070 {
Greg Claytond2653c22012-03-14 01:53:24 +00003071 addr_t symbol_lookup_file_addr = nlist.n_value;
3072 // Do an exact address match for non-ARM addresses, else get the closest since
3073 // the symbol might be a thumb symbol which has an address with bit zero set
3074 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3075 if (is_arm && func_start_entry)
3076 {
3077 // Verify that the function start address is the symbol address (ARM)
3078 // or the symbol address + 1 (thumb)
3079 if (func_start_entry->addr != symbol_lookup_file_addr &&
3080 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3081 {
3082 // Not the right entry, NULL it out...
3083 func_start_entry = NULL;
3084 }
3085 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003086 if (func_start_entry)
3087 {
3088 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003089
Greg Claytond2653c22012-03-14 01:53:24 +00003090 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003091 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003092 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003093
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003094 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3095 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3096 if (next_func_start_entry)
3097 {
Greg Claytond2653c22012-03-14 01:53:24 +00003098 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3099 // Be sure the clear the Thumb address bit when we calculate the size
3100 // from the current and next address
3101 if (is_arm)
3102 next_symbol_file_addr &= 0xfffffffffffffffeull;
3103 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 +00003104 }
3105 else
3106 {
Greg Claytond2653c22012-03-14 01:53:24 +00003107 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003108 }
3109 }
3110 }
3111 symbol_value -= section_file_addr;
3112 }
3113
3114 sym[sym_idx].SetID (nlist_idx);
3115 sym[sym_idx].SetType (type);
3116 sym[sym_idx].GetAddress().SetSection (symbol_section);
3117 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3118 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3119
3120 if (symbol_byte_size > 0)
3121 sym[sym_idx].SetByteSize(symbol_byte_size);
3122
Greg Clayton01e6a582012-11-27 01:52:16 +00003123 if (demangled_is_synthesized)
3124 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3125
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003126 ++sym_idx;
3127 }
3128 else
3129 {
3130 sym[sym_idx].Clear();
3131 }
3132
3133 }
3134
3135 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3136 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3137 // such entries by figuring out what the address for the global is by looking up this non-STAB
3138 // entry and copying the value into the debug symbol's value to save us the hassle in the
3139 // debug symbol parser.
3140
3141 Symbol *global_symbol = NULL;
3142 for (nlist_idx = 0;
3143 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3144 nlist_idx++)
3145 {
3146 if (global_symbol->GetAddress().GetFileAddress() == 0)
3147 {
3148 std::vector<uint32_t> indexes;
3149 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3150 {
3151 std::vector<uint32_t>::const_iterator pos;
3152 std::vector<uint32_t>::const_iterator end = indexes.end();
3153 for (pos = indexes.begin(); pos != end; ++pos)
3154 {
3155 symbol_ptr = symtab->SymbolAtIndex(*pos);
3156 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3157 {
3158 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3159 break;
3160 }
3161 }
3162 }
Chris Lattner24943d22010-06-08 16:52:24 +00003163 }
3164 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003165
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003166 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3167
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003168 if (function_starts_count > 0)
3169 {
3170 char synthetic_function_symbol[PATH_MAX];
3171 uint32_t num_synthetic_function_symbols = 0;
3172 for (i=0; i<function_starts_count; ++i)
3173 {
3174 if (function_starts.GetEntryRef (i).data == false)
3175 ++num_synthetic_function_symbols;
3176 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003177
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003178 if (num_synthetic_function_symbols > 0)
3179 {
3180 if (num_syms < sym_idx + num_synthetic_function_symbols)
3181 {
3182 num_syms = sym_idx + num_synthetic_function_symbols;
3183 sym = symtab->Resize (num_syms);
3184 }
3185 uint32_t synthetic_function_symbol_idx = 0;
3186 for (i=0; i<function_starts_count; ++i)
3187 {
3188 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3189 if (func_start_entry->data == false)
3190 {
Greg Claytond2653c22012-03-14 01:53:24 +00003191 addr_t symbol_file_addr = func_start_entry->addr;
3192 uint32_t symbol_flags = 0;
3193 if (is_arm)
3194 {
3195 if (symbol_file_addr & 1)
3196 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3197 symbol_file_addr &= 0xfffffffffffffffeull;
3198 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003199 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003200 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003201 {
3202 SectionSP symbol_section (symbol_addr.GetSection());
3203 uint32_t symbol_byte_size = 0;
3204 if (symbol_section)
3205 {
3206 const addr_t section_file_addr = symbol_section->GetFileAddress();
3207 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3208 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3209 if (next_func_start_entry)
3210 {
Greg Claytond2653c22012-03-14 01:53:24 +00003211 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3212 if (is_arm)
3213 next_symbol_file_addr &= 0xfffffffffffffffeull;
3214 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 +00003215 }
3216 else
3217 {
Greg Claytond2653c22012-03-14 01:53:24 +00003218 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003219 }
3220 snprintf (synthetic_function_symbol,
3221 sizeof(synthetic_function_symbol),
3222 "___lldb_unnamed_function%u$$%s",
3223 ++synthetic_function_symbol_idx,
3224 module_sp->GetFileSpec().GetFilename().GetCString());
3225 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003226 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003227 sym[sym_idx].SetType (eSymbolTypeCode);
3228 sym[sym_idx].SetIsSynthetic (true);
3229 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003230 if (symbol_flags)
3231 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003232 if (symbol_byte_size)
3233 sym[sym_idx].SetByteSize (symbol_byte_size);
3234 ++sym_idx;
3235 }
3236 }
3237 }
3238 }
3239 }
3240 }
3241
3242 // Trim our symbols down to just what we ended up with after
3243 // removing any symbols.
3244 if (sym_idx < num_syms)
3245 {
3246 num_syms = sym_idx;
3247 sym = symtab->Resize (num_syms);
3248 }
3249
3250 // Now synthesize indirect symbols
3251 if (m_dysymtab.nindirectsyms != 0)
3252 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003253 if (indirect_symbol_index_data.GetByteSize())
3254 {
3255 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3256
3257 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3258 {
3259 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3260 {
3261 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3262 if (symbol_stub_byte_size == 0)
3263 continue;
3264
3265 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3266
3267 if (num_symbol_stubs == 0)
3268 continue;
3269
3270 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3271 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3272 {
3273 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3274 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 +00003275 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003276 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3277 {
3278 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3279 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3280 continue;
3281
3282 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3283 Symbol *stub_symbol = NULL;
3284 if (index_pos != end_index_pos)
3285 {
3286 // We have a remapping from the original nlist index to
3287 // a current symbol index, so just look this up by index
3288 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3289 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003290 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003291 {
3292 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003293 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003294 // S_SYMBOL_STUBS
3295 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3296 }
3297
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003298 if (stub_symbol)
3299 {
3300 Address so_addr(symbol_stub_addr, section_list);
3301
3302 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3303 {
3304 // Change the external symbol into a trampoline that makes sense
3305 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3306 // can re-use them so we don't have to make up a synthetic symbol
3307 // for no good reason.
3308 stub_symbol->SetType (eSymbolTypeTrampoline);
3309 stub_symbol->SetExternal (false);
3310 stub_symbol->GetAddress() = so_addr;
3311 stub_symbol->SetByteSize (symbol_stub_byte_size);
3312 }
3313 else
3314 {
3315 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003316 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003317 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003318 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003319 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003320 stub_symbol = NULL; // this pointer no longer valid
3321 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003322 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003323 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003324 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3325 sym[sym_idx].SetIsSynthetic (true);
3326 sym[sym_idx].GetAddress() = so_addr;
3327 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3328 ++sym_idx;
3329 }
3330 }
Greg Claytond4330e62012-09-05 01:38:55 +00003331 else
3332 {
3333 if (log)
3334 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3335 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003336 }
3337 }
3338 }
3339 }
3340 }
3341 }
3342 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003343 }
3344 return 0;
3345}
3346
3347
3348void
3349ObjectFileMachO::Dump (Stream *s)
3350{
Greg Clayton9482f052012-03-13 23:14:29 +00003351 ModuleSP module_sp(GetModule());
3352 if (module_sp)
3353 {
3354 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3355 s->Printf("%p: ", this);
3356 s->Indent();
3357 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3358 s->PutCString("ObjectFileMachO64");
3359 else
3360 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003361
Greg Clayton9482f052012-03-13 23:14:29 +00003362 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003363
Greg Clayton9482f052012-03-13 23:14:29 +00003364 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003365
Greg Clayton9482f052012-03-13 23:14:29 +00003366 if (m_sections_ap.get())
3367 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003368
Greg Clayton9482f052012-03-13 23:14:29 +00003369 if (m_symtab_ap.get())
3370 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3371 }
Chris Lattner24943d22010-06-08 16:52:24 +00003372}
3373
3374
3375bool
Greg Clayton0467c782011-02-04 18:53:10 +00003376ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003377{
Greg Clayton9482f052012-03-13 23:14:29 +00003378 ModuleSP module_sp(GetModule());
3379 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003380 {
Greg Clayton9482f052012-03-13 23:14:29 +00003381 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3382 struct uuid_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003383 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003384 uint32_t i;
3385 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003386 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003387 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003388 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3389 break;
3390
3391 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00003392 {
Greg Clayton9482f052012-03-13 23:14:29 +00003393 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003394
Greg Clayton9482f052012-03-13 23:14:29 +00003395 if (uuid_bytes)
3396 {
Sean Callanana5689d52012-07-12 18:04:03 +00003397 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3398 // We pretend these object files have no UUID to prevent crashing.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003399
Sean Callanana5689d52012-07-12 18:04:03 +00003400 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3401 0x3b, 0xa8,
3402 0x4b, 0x16,
3403 0xb6, 0xa4,
3404 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
Jason Molenda9badb6c2013-03-06 23:19:17 +00003405
Sean Callanana5689d52012-07-12 18:04:03 +00003406 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3407 return false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003408
Greg Clayton9482f052012-03-13 23:14:29 +00003409 uuid->SetBytes (uuid_bytes);
3410 return true;
3411 }
3412 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003413 }
Greg Clayton9482f052012-03-13 23:14:29 +00003414 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003415 }
Chris Lattner24943d22010-06-08 16:52:24 +00003416 }
3417 return false;
3418}
3419
3420
3421uint32_t
3422ObjectFileMachO::GetDependentModules (FileSpecList& files)
3423{
Chris Lattner24943d22010-06-08 16:52:24 +00003424 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003425 ModuleSP module_sp(GetModule());
3426 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003427 {
Greg Clayton9482f052012-03-13 23:14:29 +00003428 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3429 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003430 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003431 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3432 uint32_t i;
3433 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003434 {
Greg Clayton9482f052012-03-13 23:14:29 +00003435 const uint32_t cmd_offset = offset;
3436 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3437 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003438
Greg Clayton9482f052012-03-13 23:14:29 +00003439 switch (load_cmd.cmd)
3440 {
3441 case LoadCommandDylibLoad:
3442 case LoadCommandDylibLoadWeak:
3443 case LoadCommandDylibReexport:
3444 case LoadCommandDynamicLinkerLoad:
3445 case LoadCommandFixedVMShlibLoad:
3446 case LoadCommandDylibLoadUpward:
3447 {
3448 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3449 const char *path = m_data.PeekCStr(name_offset);
3450 // Skip any path that starts with '@' since these are usually:
3451 // @executable_path/.../file
3452 // @rpath/.../file
3453 if (path && path[0] != '@')
3454 {
3455 FileSpec file_spec(path, resolve_path);
3456 if (files.AppendIfUnique(file_spec))
3457 count++;
3458 }
3459 }
3460 break;
3461
3462 default:
3463 break;
3464 }
3465 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003466 }
Chris Lattner24943d22010-06-08 16:52:24 +00003467 }
3468 return count;
3469}
3470
Jim Ingham28775942011-03-07 23:44:08 +00003471lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003472ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003473{
3474 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3475 // is initialized to an invalid address, so we can just return that.
3476 // 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 +00003477
Jim Ingham28775942011-03-07 23:44:08 +00003478 if (!IsExecutable() || m_entry_point_address.IsValid())
3479 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003480
3481 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003482 // /usr/include/mach-o.h, but it is basically:
3483 //
3484 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3485 // uint32_t count - this is the count of longs in the thread state data
3486 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3487 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003488 //
Jim Ingham28775942011-03-07 23:44:08 +00003489 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3490 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3491 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3492 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3493 //
3494 // For now we hard-code the offsets and flavors we need:
3495 //
3496 //
3497
Greg Clayton9482f052012-03-13 23:14:29 +00003498 ModuleSP module_sp(GetModule());
3499 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003500 {
Greg Clayton9482f052012-03-13 23:14:29 +00003501 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3502 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003503 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003504 uint32_t i;
3505 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3506 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003507
Greg Clayton9482f052012-03-13 23:14:29 +00003508 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003509 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003510 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003511 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3512 break;
3513
3514 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003515 {
Greg Clayton9482f052012-03-13 23:14:29 +00003516 case LoadCommandUnixThread:
3517 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003518 {
Greg Clayton9482f052012-03-13 23:14:29 +00003519 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003520 {
Greg Clayton9482f052012-03-13 23:14:29 +00003521 uint32_t flavor = m_data.GetU32(&offset);
3522 uint32_t count = m_data.GetU32(&offset);
3523 if (count == 0)
3524 {
3525 // We've gotten off somehow, log and exit;
3526 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003527 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003528
Greg Clayton9482f052012-03-13 23:14:29 +00003529 switch (m_header.cputype)
3530 {
3531 case llvm::MachO::CPUTypeARM:
3532 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3533 {
3534 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3535 start_address = m_data.GetU32(&offset);
3536 done = true;
3537 }
Jim Ingham28775942011-03-07 23:44:08 +00003538 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003539 case llvm::MachO::CPUTypeI386:
3540 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3541 {
3542 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3543 start_address = m_data.GetU32(&offset);
3544 done = true;
3545 }
3546 break;
3547 case llvm::MachO::CPUTypeX86_64:
3548 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3549 {
3550 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3551 start_address = m_data.GetU64(&offset);
3552 done = true;
3553 }
3554 break;
3555 default:
3556 return m_entry_point_address;
3557 }
3558 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3559 if (done)
3560 break;
3561 offset += count * 4;
3562 }
Jim Ingham28775942011-03-07 23:44:08 +00003563 }
Greg Clayton9482f052012-03-13 23:14:29 +00003564 break;
3565 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003566 {
Greg Clayton9482f052012-03-13 23:14:29 +00003567 ConstString text_segment_name ("__TEXT");
3568 uint64_t entryoffset = m_data.GetU64(&offset);
3569 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3570 if (text_segment_sp)
3571 {
3572 done = true;
3573 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3574 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003575 }
Greg Clayton9482f052012-03-13 23:14:29 +00003576
3577 default:
3578 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003579 }
Greg Clayton9482f052012-03-13 23:14:29 +00003580 if (done)
3581 break;
Jim Ingham28775942011-03-07 23:44:08 +00003582
Greg Clayton9482f052012-03-13 23:14:29 +00003583 // Go to the next load command:
3584 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003585 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003586
Greg Clayton9482f052012-03-13 23:14:29 +00003587 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003588 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003589 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003590 // of this ObjectFile:
3591 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003592 {
Greg Clayton9482f052012-03-13 23:14:29 +00003593 m_entry_point_address.Clear();
3594 }
3595 }
3596 else
3597 {
3598 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3599 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003600
Greg Clayton9482f052012-03-13 23:14:29 +00003601 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003602
Greg Clayton9482f052012-03-13 23:14:29 +00003603 if (module_sp)
3604 {
3605 SymbolContextList contexts;
3606 SymbolContext context;
3607 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3608 {
3609 if (contexts.GetContextAtIndex(0, context))
3610 m_entry_point_address = context.symbol->GetAddress();
3611 }
Greg Clayton3508c382012-02-24 01:59:29 +00003612 }
3613 }
Jim Ingham28775942011-03-07 23:44:08 +00003614 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003615
Jim Ingham28775942011-03-07 23:44:08 +00003616 return m_entry_point_address;
3617
3618}
3619
Greg Claytonb5a8f142012-02-05 02:38:54 +00003620lldb_private::Address
3621ObjectFileMachO::GetHeaderAddress ()
3622{
3623 lldb_private::Address header_addr;
3624 SectionList *section_list = GetSectionList();
3625 if (section_list)
3626 {
3627 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3628 if (text_segment_sp)
3629 {
Greg Clayton3508c382012-02-24 01:59:29 +00003630 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003631 header_addr.SetOffset (0);
3632 }
3633 }
3634 return header_addr;
3635}
3636
Greg Clayton46c9a352012-02-09 06:16:32 +00003637uint32_t
3638ObjectFileMachO::GetNumThreadContexts ()
3639{
Greg Clayton9482f052012-03-13 23:14:29 +00003640 ModuleSP module_sp(GetModule());
3641 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003642 {
Greg Clayton9482f052012-03-13 23:14:29 +00003643 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3644 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003645 {
Greg Clayton9482f052012-03-13 23:14:29 +00003646 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003647 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003648 FileRangeArray::Entry file_range;
3649 thread_command thread_cmd;
3650 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003651 {
Greg Clayton9482f052012-03-13 23:14:29 +00003652 const uint32_t cmd_offset = offset;
3653 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3654 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003655
Greg Clayton9482f052012-03-13 23:14:29 +00003656 if (thread_cmd.cmd == LoadCommandThread)
3657 {
3658 file_range.SetRangeBase (offset);
3659 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3660 m_thread_context_offsets.Append (file_range);
3661 }
3662 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003663 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003664 }
3665 }
3666 return m_thread_context_offsets.GetSize();
3667}
3668
3669lldb::RegisterContextSP
3670ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3671{
Greg Clayton46c9a352012-02-09 06:16:32 +00003672 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003673
Greg Clayton9482f052012-03-13 23:14:29 +00003674 ModuleSP module_sp(GetModule());
3675 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003676 {
Greg Clayton9482f052012-03-13 23:14:29 +00003677 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3678 if (!m_thread_context_offsets_valid)
3679 GetNumThreadContexts ();
3680
3681 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003682 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003683 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003684
3685 DataExtractor data (m_data,
3686 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003687 thread_context_file_range->GetByteSize());
3688
3689 switch (m_header.cputype)
3690 {
3691 case llvm::MachO::CPUTypeARM:
3692 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3693 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003694
Jim Ingham6f01c932012-10-12 17:34:26 +00003695 case llvm::MachO::CPUTypeI386:
3696 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3697 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003698
Jim Ingham6f01c932012-10-12 17:34:26 +00003699 case llvm::MachO::CPUTypeX86_64:
3700 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3701 break;
3702 }
Greg Clayton9482f052012-03-13 23:14:29 +00003703 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003704 }
3705 return reg_ctx_sp;
3706}
3707
Greg Claytonb5a8f142012-02-05 02:38:54 +00003708
Greg Claytonca319972011-07-09 00:41:34 +00003709ObjectFile::Type
3710ObjectFileMachO::CalculateType()
3711{
3712 switch (m_header.filetype)
3713 {
3714 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3715 if (GetAddressByteSize () == 4)
3716 {
3717 // 32 bit kexts are just object files, but they do have a valid
3718 // UUID load command.
3719 UUID uuid;
3720 if (GetUUID(&uuid))
3721 {
3722 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003723 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003724 // "OSKextGetCurrentIdentifier" as this is required of kexts
3725 if (m_strata == eStrataInvalid)
3726 m_strata = eStrataKernel;
3727 return eTypeSharedLibrary;
3728 }
3729 }
3730 return eTypeObjectFile;
3731
3732 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3733 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3734 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3735 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3736 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3737 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3738 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3739 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3740 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3741 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3742 default:
3743 break;
3744 }
3745 return eTypeUnknown;
3746}
3747
3748ObjectFile::Strata
3749ObjectFileMachO::CalculateStrata()
3750{
3751 switch (m_header.filetype)
3752 {
3753 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3754 {
3755 // 32 bit kexts are just object files, but they do have a valid
3756 // UUID load command.
3757 UUID uuid;
3758 if (GetUUID(&uuid))
3759 {
3760 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003761 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003762 // "OSKextGetCurrentIdentifier" as this is required of kexts
3763 if (m_type == eTypeInvalid)
3764 m_type = eTypeSharedLibrary;
3765
3766 return eStrataKernel;
3767 }
3768 }
3769 return eStrataUnknown;
3770
3771 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3772 // Check for the MH_DYLDLINK bit in the flags
3773 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003774 {
Greg Claytonca319972011-07-09 00:41:34 +00003775 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003776 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003777 else
Sean Callananac725af2012-02-10 20:22:35 +00003778 {
3779 SectionList *section_list = GetSectionList();
3780 if (section_list)
3781 {
3782 static ConstString g_kld_section_name ("__KLD");
3783 if (section_list->FindSectionByName(g_kld_section_name))
3784 return eStrataKernel;
3785 }
3786 }
3787 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00003788
3789 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
3790 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00003791 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00003792 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
3793 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
3794 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
3795 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
3796 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
3797 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
3798 default:
3799 break;
3800 }
3801 return eStrataUnknown;
3802}
3803
3804
Greg Clayton49f4bf22012-02-22 19:41:02 +00003805uint32_t
3806ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
3807{
Greg Clayton9482f052012-03-13 23:14:29 +00003808 ModuleSP module_sp(GetModule());
3809 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003810 {
Greg Clayton9482f052012-03-13 23:14:29 +00003811 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3812 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003813 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003814 uint32_t version_cmd = 0;
3815 uint64_t version = 0;
3816 uint32_t i;
3817 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003818 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003819 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003820 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3821 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003822
Greg Clayton9482f052012-03-13 23:14:29 +00003823 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003824 {
Greg Clayton9482f052012-03-13 23:14:29 +00003825 if (version_cmd == 0)
3826 {
3827 version_cmd = load_cmd.cmd;
3828 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
3829 break;
3830 version = load_cmd.dylib.current_version;
3831 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003832 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00003833 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00003834 }
Greg Clayton9482f052012-03-13 23:14:29 +00003835 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003836 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003837
Greg Clayton9482f052012-03-13 23:14:29 +00003838 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003839 {
Greg Clayton9482f052012-03-13 23:14:29 +00003840 if (versions != NULL && num_versions > 0)
3841 {
3842 if (num_versions > 0)
3843 versions[0] = (version & 0xFFFF0000ull) >> 16;
3844 if (num_versions > 1)
3845 versions[1] = (version & 0x0000FF00ull) >> 8;
3846 if (num_versions > 2)
3847 versions[2] = (version & 0x000000FFull);
3848 // Fill in an remaining version numbers with invalid values
3849 for (i=3; i<num_versions; ++i)
3850 versions[i] = UINT32_MAX;
3851 }
3852 // The LC_ID_DYLIB load command has a version with 3 version numbers
3853 // in it, so always return 3
3854 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003855 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00003856 }
3857 return false;
3858}
3859
Chris Lattner24943d22010-06-08 16:52:24 +00003860bool
Greg Clayton395fc332011-02-15 21:59:32 +00003861ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00003862{
Greg Clayton9482f052012-03-13 23:14:29 +00003863 ModuleSP module_sp(GetModule());
3864 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003865 {
Greg Clayton9482f052012-03-13 23:14:29 +00003866 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3867 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003868
Greg Clayton9482f052012-03-13 23:14:29 +00003869 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00003870 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00003871 // unknown to make sure we use the DynamicLoaderStatic()...
3872 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
3873 {
3874 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
3875 }
3876 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003877 }
Greg Clayton9482f052012-03-13 23:14:29 +00003878 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003879}
3880
3881
3882//------------------------------------------------------------------
3883// PluginInterface protocol
3884//------------------------------------------------------------------
3885const char *
3886ObjectFileMachO::GetPluginName()
3887{
3888 return "ObjectFileMachO";
3889}
3890
3891const char *
3892ObjectFileMachO::GetShortPluginName()
3893{
3894 return GetPluginNameStatic();
3895}
3896
3897uint32_t
3898ObjectFileMachO::GetPluginVersion()
3899{
3900 return 1;
3901}
3902