blob: 4f205e9ec634fd0f37b8160b5a68f7ab6842f568 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ObjectFileMachO.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Greg Clayton3f69eac2011-12-03 02:30:59 +000010#include "llvm/ADT/StringRef.h"
Jim Ingham28775942011-03-07 23:44:08 +000011#include "llvm/Support/MachO.h"
12
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "ObjectFileMachO.h"
14
Greg Claytond4330e62012-09-05 01:38:55 +000015#include "lldb/lldb-private-log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Core/ArchSpec.h"
17#include "lldb/Core/DataBuffer.h"
Jason Molendaadf9e3d2013-04-10 05:58:57 +000018#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/FileSpecList.h"
Greg Claytond4330e62012-09-05 01:38:55 +000020#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Module.h"
22#include "lldb/Core/PluginManager.h"
Greg Clayton6f7f8da2012-04-24 03:06:13 +000023#include "lldb/Core/RangeMap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Core/Section.h"
25#include "lldb/Core/StreamFile.h"
26#include "lldb/Core/StreamString.h"
27#include "lldb/Core/Timer.h"
28#include "lldb/Core/UUID.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000029#include "lldb/Host/Host.h"
30#include "lldb/Host/FileSpec.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000031#include "lldb/Symbol/ClangNamespaceDecl.h"
Jason Molendad7938392013-03-21 03:36:01 +000032#include "lldb/Symbol/DWARFCallFrameInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/Symbol/ObjectFile.h"
Greg Clayton29021d32012-04-18 05:19:20 +000034#include "lldb/Target/Platform.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000035#include "lldb/Target/Process.h"
Greg Clayton29021d32012-04-18 05:19:20 +000036#include "lldb/Target/Target.h"
Greg Clayton9ce95382012-02-13 23:10:39 +000037#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
38#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Clayton46c9a352012-02-09 06:16:32 +000039#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner24943d22010-06-08 16:52:24 +000040
Jason Molenda45c75502013-04-16 06:24:42 +000041#if defined (__APPLE__) && defined (__arm__)
42// GetLLDBSharedCacheUUID() needs to call dlsym()
43#include <dlfcn.h>
44#endif
45
Chris Lattner24943d22010-06-08 16:52:24 +000046using namespace lldb;
47using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000048using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000049
Jason Molenda9badb6c2013-03-06 23:19:17 +000050class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
Greg Clayton46c9a352012-02-09 06:16:32 +000051{
52public:
53 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
54 RegisterContextDarwin_x86_64 (thread, 0)
55 {
56 SetRegisterDataFrom_LC_THREAD (data);
57 }
58
59 virtual void
60 InvalidateAllRegisters ()
61 {
62 // Do nothing... registers are always valid...
63 }
64
65 void
66 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
67 {
Greg Clayton36da2aa2013-01-25 18:06:21 +000068 lldb::offset_t offset = 0;
Greg Clayton46c9a352012-02-09 06:16:32 +000069 SetError (GPRRegSet, Read, -1);
70 SetError (FPURegSet, Read, -1);
71 SetError (EXCRegSet, Read, -1);
Greg Clayton9ce95382012-02-13 23:10:39 +000072 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +000073
Greg Clayton9ce95382012-02-13 23:10:39 +000074 while (!done)
Greg Clayton46c9a352012-02-09 06:16:32 +000075 {
Greg Clayton9ce95382012-02-13 23:10:39 +000076 int flavor = data.GetU32 (&offset);
77 if (flavor == 0)
78 done = true;
79 else
Greg Clayton46c9a352012-02-09 06:16:32 +000080 {
Greg Clayton9ce95382012-02-13 23:10:39 +000081 uint32_t i;
82 uint32_t count = data.GetU32 (&offset);
83 switch (flavor)
84 {
85 case GPRRegSet:
86 for (i=0; i<count; ++i)
87 (&gpr.rax)[i] = data.GetU64(&offset);
88 SetError (GPRRegSet, Read, 0);
89 done = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +000090
Greg Clayton9ce95382012-02-13 23:10:39 +000091 break;
92 case FPURegSet:
93 // TODO: fill in FPU regs....
94 //SetError (FPURegSet, Read, -1);
95 done = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +000096
Greg Clayton9ce95382012-02-13 23:10:39 +000097 break;
98 case EXCRegSet:
99 exc.trapno = data.GetU32(&offset);
100 exc.err = data.GetU32(&offset);
101 exc.faultvaddr = data.GetU64(&offset);
102 SetError (EXCRegSet, Read, 0);
103 done = true;
104 break;
105 case 7:
106 case 8:
107 case 9:
108 // fancy flavors that encapsulate of the the above
109 // falvors...
110 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000111
Greg Clayton9ce95382012-02-13 23:10:39 +0000112 default:
113 done = true;
114 break;
115 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000116 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000117 }
118 }
119protected:
120 virtual int
121 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
122 {
123 return 0;
124 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000125
Greg Clayton9ce95382012-02-13 23:10:39 +0000126 virtual int
127 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
128 {
129 return 0;
130 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000131
Greg Clayton9ce95382012-02-13 23:10:39 +0000132 virtual int
133 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
134 {
135 return 0;
136 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000137
Greg Clayton9ce95382012-02-13 23:10:39 +0000138 virtual int
139 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
140 {
141 return 0;
142 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000143
Greg Clayton9ce95382012-02-13 23:10:39 +0000144 virtual int
145 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
146 {
147 return 0;
148 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000149
Greg Clayton9ce95382012-02-13 23:10:39 +0000150 virtual int
151 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
152 {
153 return 0;
154 }
155};
Greg Clayton46c9a352012-02-09 06:16:32 +0000156
Greg Clayton9ce95382012-02-13 23:10:39 +0000157
Jason Molenda9badb6c2013-03-06 23:19:17 +0000158class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
Greg Clayton9ce95382012-02-13 23:10:39 +0000159{
160public:
161 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
162 RegisterContextDarwin_i386 (thread, 0)
163 {
164 SetRegisterDataFrom_LC_THREAD (data);
165 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000166
Greg Clayton9ce95382012-02-13 23:10:39 +0000167 virtual void
168 InvalidateAllRegisters ()
169 {
170 // Do nothing... registers are always valid...
171 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000172
Greg Clayton9ce95382012-02-13 23:10:39 +0000173 void
174 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
175 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000176 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000177 SetError (GPRRegSet, Read, -1);
178 SetError (FPURegSet, Read, -1);
179 SetError (EXCRegSet, Read, -1);
180 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000181
Greg Clayton9ce95382012-02-13 23:10:39 +0000182 while (!done)
183 {
184 int flavor = data.GetU32 (&offset);
185 if (flavor == 0)
186 done = true;
187 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000188 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000189 uint32_t i;
190 uint32_t count = data.GetU32 (&offset);
191 switch (flavor)
192 {
193 case GPRRegSet:
194 for (i=0; i<count; ++i)
195 (&gpr.eax)[i] = data.GetU32(&offset);
196 SetError (GPRRegSet, Read, 0);
197 done = true;
198
199 break;
200 case FPURegSet:
201 // TODO: fill in FPU regs....
202 //SetError (FPURegSet, Read, -1);
203 done = true;
204
205 break;
206 case EXCRegSet:
207 exc.trapno = data.GetU32(&offset);
208 exc.err = data.GetU32(&offset);
209 exc.faultvaddr = data.GetU32(&offset);
210 SetError (EXCRegSet, Read, 0);
211 done = true;
212 break;
213 case 7:
214 case 8:
215 case 9:
216 // fancy flavors that encapsulate of the the above
217 // falvors...
218 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000219
Greg Clayton9ce95382012-02-13 23:10:39 +0000220 default:
221 done = true;
222 break;
223 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000224 }
225 }
226 }
227protected:
228 virtual int
229 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
230 {
231 return 0;
232 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000233
Greg Clayton46c9a352012-02-09 06:16:32 +0000234 virtual int
235 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
236 {
237 return 0;
238 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000239
Greg Clayton46c9a352012-02-09 06:16:32 +0000240 virtual int
241 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
242 {
243 return 0;
244 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000245
Greg Clayton46c9a352012-02-09 06:16:32 +0000246 virtual int
247 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
248 {
249 return 0;
250 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000251
Greg Clayton46c9a352012-02-09 06:16:32 +0000252 virtual int
253 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
254 {
255 return 0;
256 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000257
Greg Clayton46c9a352012-02-09 06:16:32 +0000258 virtual int
259 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
260 {
261 return 0;
262 }
263};
264
Jason Molenda9badb6c2013-03-06 23:19:17 +0000265class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Clayton9ce95382012-02-13 23:10:39 +0000266{
267public:
268 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonb5431d02012-10-30 23:57:32 +0000269 RegisterContextDarwin_arm (thread, 0)
Greg Clayton9ce95382012-02-13 23:10:39 +0000270 {
271 SetRegisterDataFrom_LC_THREAD (data);
272 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000273
Greg Clayton9ce95382012-02-13 23:10:39 +0000274 virtual void
275 InvalidateAllRegisters ()
276 {
277 // Do nothing... registers are always valid...
278 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000279
Greg Clayton9ce95382012-02-13 23:10:39 +0000280 void
281 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
282 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000283 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000284 SetError (GPRRegSet, Read, -1);
285 SetError (FPURegSet, Read, -1);
286 SetError (EXCRegSet, Read, -1);
287 int flavor = data.GetU32 (&offset);
288 uint32_t count = data.GetU32 (&offset);
289 switch (flavor)
290 {
291 case GPRRegSet:
292 for (uint32_t i=0; i<count; ++i)
293 gpr.r[i] = data.GetU32(&offset);
294 SetError (GPRRegSet, Read, 0);
295 break;
296 case FPURegSet:
297 // TODO: fill in FPU regs....
298 //SetError (FPURegSet, Read, -1);
299 break;
300 case EXCRegSet:
301 exc.exception = data.GetU32(&offset);
302 exc.fsr = data.GetU32(&offset);
303 exc.far = data.GetU32(&offset);
304 SetError (EXCRegSet, Read, 0);
305 break;
306 }
307 }
308protected:
309 virtual int
310 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
311 {
312 return 0;
313 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000314
Greg Clayton9ce95382012-02-13 23:10:39 +0000315 virtual int
316 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
317 {
318 return 0;
319 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000320
Greg Clayton9ce95382012-02-13 23:10:39 +0000321 virtual int
322 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
323 {
324 return 0;
325 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000326
327 virtual int
328 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
329 {
330 return -1;
331 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000332
Greg Clayton9ce95382012-02-13 23:10:39 +0000333 virtual int
334 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
335 {
336 return 0;
337 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000338
Greg Clayton9ce95382012-02-13 23:10:39 +0000339 virtual int
340 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
341 {
342 return 0;
343 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000344
Greg Clayton9ce95382012-02-13 23:10:39 +0000345 virtual int
346 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
347 {
348 return 0;
349 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000350
Greg Claytonb5431d02012-10-30 23:57:32 +0000351 virtual int
352 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
353 {
354 return -1;
355 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000356};
357
Greg Claytonb1888f22011-03-19 01:12:21 +0000358#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000359
360void
361ObjectFileMachO::Initialize()
362{
363 PluginManager::RegisterPlugin (GetPluginNameStatic(),
364 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000365 CreateInstance,
366 CreateMemoryInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000367}
368
369void
370ObjectFileMachO::Terminate()
371{
372 PluginManager::UnregisterPlugin (CreateInstance);
373}
374
375
376const char *
377ObjectFileMachO::GetPluginNameStatic()
378{
379 return "object-file.mach-o";
380}
381
382const char *
383ObjectFileMachO::GetPluginDescriptionStatic()
384{
385 return "Mach-o object file reader (32 and 64 bit)";
386}
387
Chris Lattner24943d22010-06-08 16:52:24 +0000388ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000389ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
390 DataBufferSP& data_sp,
391 lldb::offset_t data_offset,
392 const FileSpec* file,
393 lldb::offset_t file_offset,
394 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000395{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000396 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000397 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000398 data_sp = file->MemoryMapFileContents(file_offset, length);
399 data_offset = 0;
400 }
401
402 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
403 {
404 // Update the data to contain the entire file if it doesn't already
405 if (data_sp->GetByteSize() < length)
406 {
407 data_sp = file->MemoryMapFileContents(file_offset, length);
408 data_offset = 0;
409 }
410 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 +0000411 if (objfile_ap.get() && objfile_ap->ParseHeader())
412 return objfile_ap.release();
413 }
414 return NULL;
415}
416
Greg Claytonb5a8f142012-02-05 02:38:54 +0000417ObjectFile *
Jason Molenda9badb6c2013-03-06 23:19:17 +0000418ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
419 DataBufferSP& data_sp,
420 const ProcessSP &process_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000421 lldb::addr_t header_addr)
422{
423 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
424 {
Greg Clayton3508c382012-02-24 01:59:29 +0000425 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000426 if (objfile_ap.get() && objfile_ap->ParseHeader())
427 return objfile_ap.release();
428 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000429 return NULL;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000430}
431
432
433const ConstString &
434ObjectFileMachO::GetSegmentNameTEXT()
435{
436 static ConstString g_segment_name_TEXT ("__TEXT");
437 return g_segment_name_TEXT;
438}
439
440const ConstString &
441ObjectFileMachO::GetSegmentNameDATA()
442{
443 static ConstString g_segment_name_DATA ("__DATA");
444 return g_segment_name_DATA;
445}
446
447const ConstString &
448ObjectFileMachO::GetSegmentNameOBJC()
449{
450 static ConstString g_segment_name_OBJC ("__OBJC");
451 return g_segment_name_OBJC;
452}
453
454const ConstString &
455ObjectFileMachO::GetSegmentNameLINKEDIT()
456{
457 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
458 return g_section_name_LINKEDIT;
459}
460
461const ConstString &
462ObjectFileMachO::GetSectionNameEHFrame()
463{
464 static ConstString g_section_name_eh_frame ("__eh_frame");
465 return g_section_name_eh_frame;
466}
467
468
Chris Lattner24943d22010-06-08 16:52:24 +0000469
470static uint32_t
471MachHeaderSizeFromMagic(uint32_t magic)
472{
473 switch (magic)
474 {
Greg Clayton1674b122010-07-21 22:12:05 +0000475 case HeaderMagic32:
476 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000477 return sizeof(struct mach_header);
478
Greg Clayton1674b122010-07-21 22:12:05 +0000479 case HeaderMagic64:
480 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000481 return sizeof(struct mach_header_64);
482 break;
483
484 default:
485 break;
486 }
487 return 0;
488}
489
490
491bool
Jason Molenda9badb6c2013-03-06 23:19:17 +0000492ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
493 lldb::addr_t data_offset,
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000494 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000495{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000496 DataExtractor data;
497 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000498 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000499 uint32_t magic = data.GetU32(&offset);
500 return MachHeaderSizeFromMagic(magic) != 0;
501}
502
503
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000504ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
505 DataBufferSP& data_sp,
506 lldb::offset_t data_offset,
507 const FileSpec* file,
508 lldb::offset_t file_offset,
509 lldb::offset_t length) :
510 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Clayton46c9a352012-02-09 06:16:32 +0000511 m_mach_segments(),
512 m_mach_sections(),
513 m_entry_point_address(),
514 m_thread_context_offsets(),
515 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000516{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000517 ::memset (&m_header, 0, sizeof(m_header));
518 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000519}
520
Greg Clayton3508c382012-02-24 01:59:29 +0000521ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000522 lldb::DataBufferSP& header_data_sp,
523 const lldb::ProcessSP &process_sp,
524 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000525 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Clayton46c9a352012-02-09 06:16:32 +0000526 m_mach_segments(),
527 m_mach_sections(),
528 m_entry_point_address(),
529 m_thread_context_offsets(),
530 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000531{
532 ::memset (&m_header, 0, sizeof(m_header));
533 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
534}
Chris Lattner24943d22010-06-08 16:52:24 +0000535
536ObjectFileMachO::~ObjectFileMachO()
537{
538}
539
540
541bool
542ObjectFileMachO::ParseHeader ()
543{
Greg Clayton9482f052012-03-13 23:14:29 +0000544 ModuleSP module_sp(GetModule());
545 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000546 {
Greg Clayton9482f052012-03-13 23:14:29 +0000547 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
548 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000549 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000550 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000551 // Leave magic in the original byte order
552 m_header.magic = m_data.GetU32(&offset);
553 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000554 {
Greg Clayton9482f052012-03-13 23:14:29 +0000555 case HeaderMagic32:
556 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
557 m_data.SetAddressByteSize(4);
558 can_parse = true;
559 break;
560
561 case HeaderMagic64:
562 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
563 m_data.SetAddressByteSize(8);
564 can_parse = true;
565 break;
566
567 case HeaderMagic32Swapped:
568 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
569 m_data.SetAddressByteSize(4);
570 can_parse = true;
571 break;
572
573 case HeaderMagic64Swapped:
574 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
575 m_data.SetAddressByteSize(8);
576 can_parse = true;
577 break;
578
579 default:
580 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000581 }
Greg Clayton9482f052012-03-13 23:14:29 +0000582
583 if (can_parse)
584 {
585 m_data.GetU32(&offset, &m_header.cputype, 6);
586
587 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +0000588
Greg Clayton21a25432012-11-16 21:36:10 +0000589 // Check if the module has a required architecture
590 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000591 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000592 return false;
593
Greg Clayton9482f052012-03-13 23:14:29 +0000594 if (SetModulesArchitecture (mach_arch))
595 {
596 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
597 if (m_data.GetByteSize() < header_and_lc_size)
598 {
599 DataBufferSP data_sp;
600 ProcessSP process_sp (m_process_wp.lock());
601 if (process_sp)
602 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000603 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000604 }
605 else
606 {
607 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000608 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000609 if (data_sp->GetByteSize() != header_and_lc_size)
610 return false;
611 }
612 if (data_sp)
613 m_data.SetData (data_sp);
614 }
615 }
616 return true;
617 }
618 else
619 {
620 memset(&m_header, 0, sizeof(struct mach_header));
621 }
Chris Lattner24943d22010-06-08 16:52:24 +0000622 }
623 return false;
624}
625
626
627ByteOrder
628ObjectFileMachO::GetByteOrder () const
629{
Chris Lattner24943d22010-06-08 16:52:24 +0000630 return m_data.GetByteOrder ();
631}
632
Jim Ingham7508e732010-08-09 23:31:02 +0000633bool
634ObjectFileMachO::IsExecutable() const
635{
636 return m_header.filetype == HeaderFileTypeExecutable;
637}
Chris Lattner24943d22010-06-08 16:52:24 +0000638
Greg Clayton36da2aa2013-01-25 18:06:21 +0000639uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000640ObjectFileMachO::GetAddressByteSize () const
641{
Chris Lattner24943d22010-06-08 16:52:24 +0000642 return m_data.GetAddressByteSize ();
643}
644
Greg Claytonb3448432011-03-24 21:19:54 +0000645AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000646ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
647{
648 Symtab *symtab = GetSymtab();
649 if (symtab)
650 {
651 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
652 if (symbol)
653 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000654 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000655 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000656 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000657 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000658 {
Greg Clayton3508c382012-02-24 01:59:29 +0000659 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000660 switch (section_type)
661 {
662 case eSectionTypeInvalid: return eAddressClassUnknown;
663 case eSectionTypeCode:
664 if (m_header.cputype == llvm::MachO::CPUTypeARM)
665 {
666 // For ARM we have a bit in the n_desc field of the symbol
667 // that tells us ARM/Thumb which is bit 0x0008.
668 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
669 return eAddressClassCodeAlternateISA;
670 }
671 return eAddressClassCode;
672
673 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000674 case eSectionTypeData:
675 case eSectionTypeDataCString:
676 case eSectionTypeDataCStringPointers:
677 case eSectionTypeDataSymbolAddress:
678 case eSectionTypeData4:
679 case eSectionTypeData8:
680 case eSectionTypeData16:
681 case eSectionTypeDataPointers:
682 case eSectionTypeZeroFill:
683 case eSectionTypeDataObjCMessageRefs:
684 case eSectionTypeDataObjCCFStrings:
685 return eAddressClassData;
686 case eSectionTypeDebug:
687 case eSectionTypeDWARFDebugAbbrev:
688 case eSectionTypeDWARFDebugAranges:
689 case eSectionTypeDWARFDebugFrame:
690 case eSectionTypeDWARFDebugInfo:
691 case eSectionTypeDWARFDebugLine:
692 case eSectionTypeDWARFDebugLoc:
693 case eSectionTypeDWARFDebugMacInfo:
694 case eSectionTypeDWARFDebugPubNames:
695 case eSectionTypeDWARFDebugPubTypes:
696 case eSectionTypeDWARFDebugRanges:
697 case eSectionTypeDWARFDebugStr:
698 case eSectionTypeDWARFAppleNames:
699 case eSectionTypeDWARFAppleTypes:
700 case eSectionTypeDWARFAppleNamespaces:
701 case eSectionTypeDWARFAppleObjC:
702 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000703 case eSectionTypeEHFrame: return eAddressClassRuntime;
704 case eSectionTypeOther: return eAddressClassUnknown;
705 }
706 }
707 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000708
Greg Claytonb3448432011-03-24 21:19:54 +0000709 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000710 switch (symbol_type)
711 {
712 case eSymbolTypeAny: return eAddressClassUnknown;
713 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000714
Greg Claytonb1888f22011-03-19 01:12:21 +0000715 case eSymbolTypeCode:
716 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000717 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000718 if (m_header.cputype == llvm::MachO::CPUTypeARM)
719 {
720 // For ARM we have a bit in the n_desc field of the symbol
721 // that tells us ARM/Thumb which is bit 0x0008.
722 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
723 return eAddressClassCodeAlternateISA;
724 }
725 return eAddressClassCode;
726
727 case eSymbolTypeData: return eAddressClassData;
728 case eSymbolTypeRuntime: return eAddressClassRuntime;
729 case eSymbolTypeException: return eAddressClassRuntime;
730 case eSymbolTypeSourceFile: return eAddressClassDebug;
731 case eSymbolTypeHeaderFile: return eAddressClassDebug;
732 case eSymbolTypeObjectFile: return eAddressClassDebug;
733 case eSymbolTypeCommonBlock: return eAddressClassDebug;
734 case eSymbolTypeBlock: return eAddressClassDebug;
735 case eSymbolTypeLocal: return eAddressClassData;
736 case eSymbolTypeParam: return eAddressClassData;
737 case eSymbolTypeVariable: return eAddressClassData;
738 case eSymbolTypeVariableType: return eAddressClassDebug;
739 case eSymbolTypeLineEntry: return eAddressClassDebug;
740 case eSymbolTypeLineHeader: return eAddressClassDebug;
741 case eSymbolTypeScopeBegin: return eAddressClassDebug;
742 case eSymbolTypeScopeEnd: return eAddressClassDebug;
743 case eSymbolTypeAdditional: return eAddressClassUnknown;
744 case eSymbolTypeCompiler: return eAddressClassDebug;
745 case eSymbolTypeInstrumentation:return eAddressClassDebug;
746 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000747 case eSymbolTypeObjCClass: return eAddressClassRuntime;
748 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
749 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000750 }
751 }
752 }
753 return eAddressClassUnknown;
754}
Chris Lattner24943d22010-06-08 16:52:24 +0000755
756Symtab *
757ObjectFileMachO::GetSymtab()
758{
Greg Clayton9482f052012-03-13 23:14:29 +0000759 ModuleSP module_sp(GetModule());
760 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000761 {
Greg Clayton9482f052012-03-13 23:14:29 +0000762 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
763 if (m_symtab_ap.get() == NULL)
764 {
765 m_symtab_ap.reset(new Symtab(this));
766 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
767 ParseSymtab (true);
768 m_symtab_ap->Finalize ();
769 }
Chris Lattner24943d22010-06-08 16:52:24 +0000770 }
771 return m_symtab_ap.get();
772}
773
774
775SectionList *
776ObjectFileMachO::GetSectionList()
777{
Greg Clayton9482f052012-03-13 23:14:29 +0000778 ModuleSP module_sp(GetModule());
779 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000780 {
Greg Clayton9482f052012-03-13 23:14:29 +0000781 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
782 if (m_sections_ap.get() == NULL)
783 {
784 m_sections_ap.reset(new SectionList());
785 ParseSections();
786 }
Chris Lattner24943d22010-06-08 16:52:24 +0000787 }
788 return m_sections_ap.get();
789}
790
791
792size_t
793ObjectFileMachO::ParseSections ()
794{
795 lldb::user_id_t segID = 0;
796 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000797 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000798 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000799 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000800 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000801 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000802 // First look up any LC_ENCRYPTION_INFO load commands
803 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
804 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000805 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000806 for (i=0; i<m_header.ncmds; ++i)
807 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000808 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000809 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000810 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000811
Greg Clayton54e33712012-05-25 18:09:55 +0000812 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000813 {
Greg Clayton54e33712012-05-25 18:09:55 +0000814 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
815 {
816 if (encryption_cmd.cryptid != 0)
817 {
818 EncryptedFileRanges::Entry entry;
819 entry.SetRangeBase(encryption_cmd.cryptoff);
820 entry.SetByteSize(encryption_cmd.cryptsize);
821 encrypted_file_ranges.Append(entry);
822 }
823 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000824 }
Greg Clayton54e33712012-05-25 18:09:55 +0000825 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000826 }
827
828 offset = MachHeaderSizeFromMagic(m_header.magic);
829
Greg Clayton54e33712012-05-25 18:09:55 +0000830 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000831 for (i=0; i<m_header.ncmds; ++i)
832 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000833 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000834 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
835 break;
836
Greg Clayton1674b122010-07-21 22:12:05 +0000837 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000838 {
839 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
840 {
841 load_cmd.vmaddr = m_data.GetAddress(&offset);
842 load_cmd.vmsize = m_data.GetAddress(&offset);
843 load_cmd.fileoff = m_data.GetAddress(&offset);
844 load_cmd.filesize = m_data.GetAddress(&offset);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000845 if (m_length != 0 && load_cmd.filesize != 0)
846 {
Greg Claytonbb759862013-04-16 16:51:19 +0000847 if (load_cmd.fileoff > m_length)
848 {
849 // We have a load command that says it extends past the end of hte file. This is likely
850 // a corrupt file. We don't have any way to return an error condition here (this method
851 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
852 // is null out the SectionList vector and if a process has been set up, dump a message
853 // to stdout. The most common case here is core file debugging with a truncated file.
854 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
855 GetModule()->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 ")",
856 i,
857 lc_segment_name,
858 load_cmd.fileoff,
859 m_length);
860 m_sections_ap->Clear();
861 return 0;
862 }
863
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000864 if (load_cmd.fileoff + load_cmd.filesize > m_length)
865 {
866 // We have a load command that says it extends past the end of hte file. This is likely
867 // a corrupt file. We don't have any way to return an error condition here (this method
868 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
869 // is null out the SectionList vector and if a process has been set up, dump a message
Greg Claytonbb759862013-04-16 16:51:19 +0000870 // to stdout. The most common case here is core file debugging with a truncated file.
871 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
872 GetModule()->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff + filesize (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 ")",
873 i,
874 lc_segment_name,
875 load_cmd.fileoff + load_cmd.filesize,
876 m_length);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000877 m_sections_ap->Clear();
878 return 0;
879 }
880 }
Chris Lattner24943d22010-06-08 16:52:24 +0000881 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
882 {
Jason Molenda9badb6c2013-03-06 23:19:17 +0000883
Greg Clayton68ca8232011-01-25 02:58:48 +0000884 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
885
Chris Lattner24943d22010-06-08 16:52:24 +0000886 // Keep a list of mach segments around in case we need to
887 // get at data that isn't stored in the abstracted Sections.
888 m_mach_segments.push_back (load_cmd);
889
Greg Clayton36da2aa2013-01-25 18:06:21 +0000890 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 +0000891 // Use a segment ID of the segment index shifted left by 8 so they
892 // never conflict with any of the sections.
893 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000894 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000895 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000896 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000897 ++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
898 segment_name, // Name of this section
899 eSectionTypeContainer, // This section is a container of other sections.
900 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
901 load_cmd.vmsize, // VM size in bytes of this section
902 load_cmd.fileoff, // Offset to the data for this section in the file
903 load_cmd.filesize, // Size in bytes of this section as found in the the file
904 load_cmd.flags)); // Flags for this section
905
Greg Clayton68ca8232011-01-25 02:58:48 +0000906 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000907 m_sections_ap->AddSection(segment_sp);
908 }
909
910 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000911 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000912 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +0000913 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +0000914 // mach sections yet...
915 if (m_mach_sections.empty())
916 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000917 uint32_t segment_sect_idx;
918 const lldb::user_id_t first_segment_sectID = sectID + 1;
919
920
Greg Clayton1674b122010-07-21 22:12:05 +0000921 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000922 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
923 {
924 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
925 break;
926 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
927 break;
928 sect64.addr = m_data.GetAddress(&offset);
929 sect64.size = m_data.GetAddress(&offset);
930
931 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
932 break;
933
934 // Keep a list of mach sections around in case we need to
935 // get at data that isn't stored in the abstracted Sections.
936 m_mach_sections.push_back (sect64);
937
938 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
939 if (!segment_name)
940 {
941 // We have a segment with no name so we need to conjure up
942 // segments that correspond to the section's segname if there
943 // isn't already such a section. If there is such a section,
944 // we resize the section so that it spans all sections.
945 // We also mark these sections as fake so address matches don't
946 // hit if they land in the gaps between the child sections.
947 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
948 segment_sp = m_sections_ap->FindSectionByName (segment_name);
949 if (segment_sp.get())
950 {
951 Section *segment = segment_sp.get();
952 // Grow the section size as needed.
953 const lldb::addr_t sect64_min_addr = sect64.addr;
954 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
955 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
956 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
957 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
958 if (sect64_min_addr >= curr_seg_min_addr)
959 {
960 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
961 // Only grow the section size if needed
962 if (new_seg_byte_size > curr_seg_byte_size)
963 segment->SetByteSize (new_seg_byte_size);
964 }
965 else
966 {
967 // We need to change the base address of the segment and
968 // adjust the child section offsets for all existing children.
969 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
970 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +0000971 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000972 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
973 }
Greg Clayton661825b2010-06-28 23:51:11 +0000974
975 // Grow the section size as needed.
976 if (sect64.offset)
977 {
978 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
979 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
980
981 const lldb::addr_t section_min_file_offset = sect64.offset;
982 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
983 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
984 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
985 segment->SetFileOffset (new_file_offset);
986 segment->SetFileSize (new_file_size);
987 }
Chris Lattner24943d22010-06-08 16:52:24 +0000988 }
989 else
990 {
991 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +0000992 segment_sp.reset(new Section (segment_sp, // Parent section
993 module_sp, // Module to which this section belongs
994 ++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
995 segment_name, // Name of this section
996 eSectionTypeContainer, // This section is a container of other sections.
997 sect64.addr, // File VM address == addresses as they are found in the object file
998 sect64.size, // VM size in bytes of this section
999 sect64.offset, // Offset to the data for this section in the file
1000 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1001 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +00001002 segment_sp->SetIsFake(true);
1003 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +00001004 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001005 }
1006 }
1007 assert (segment_sp.get());
1008
Greg Clayton1674b122010-07-21 22:12:05 +00001009 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +00001010 static ConstString g_sect_name_objc_data ("__objc_data");
1011 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1012 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1013 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1014 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1015 static ConstString g_sect_name_objc_const ("__objc_const");
1016 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1017 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001018
1019 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1020 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1021 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1022 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1023 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1024 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1025 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1026 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1027 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1028 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1029 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +00001030 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1031 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +00001032 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +00001033 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001034 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +00001035 static ConstString g_sect_name_DATA ("__DATA");
1036 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001037
Chris Lattner24943d22010-06-08 16:52:24 +00001038 SectionType sect_type = eSectionTypeOther;
1039
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001040 if (section_name == g_sect_name_dwarf_debug_abbrev)
1041 sect_type = eSectionTypeDWARFDebugAbbrev;
1042 else if (section_name == g_sect_name_dwarf_debug_aranges)
1043 sect_type = eSectionTypeDWARFDebugAranges;
1044 else if (section_name == g_sect_name_dwarf_debug_frame)
1045 sect_type = eSectionTypeDWARFDebugFrame;
1046 else if (section_name == g_sect_name_dwarf_debug_info)
1047 sect_type = eSectionTypeDWARFDebugInfo;
1048 else if (section_name == g_sect_name_dwarf_debug_line)
1049 sect_type = eSectionTypeDWARFDebugLine;
1050 else if (section_name == g_sect_name_dwarf_debug_loc)
1051 sect_type = eSectionTypeDWARFDebugLoc;
1052 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1053 sect_type = eSectionTypeDWARFDebugMacInfo;
1054 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1055 sect_type = eSectionTypeDWARFDebugPubNames;
1056 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1057 sect_type = eSectionTypeDWARFDebugPubTypes;
1058 else if (section_name == g_sect_name_dwarf_debug_ranges)
1059 sect_type = eSectionTypeDWARFDebugRanges;
1060 else if (section_name == g_sect_name_dwarf_debug_str)
1061 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001062 else if (section_name == g_sect_name_dwarf_apple_names)
1063 sect_type = eSectionTypeDWARFAppleNames;
1064 else if (section_name == g_sect_name_dwarf_apple_types)
1065 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001066 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1067 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001068 else if (section_name == g_sect_name_dwarf_apple_objc)
1069 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001070 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001071 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001072 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001073 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001074 else if (section_name == g_sect_name_eh_frame)
1075 sect_type = eSectionTypeEHFrame;
1076 else if (section_name == g_sect_name_cfstring)
1077 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001078 else if (section_name == g_sect_name_objc_data ||
1079 section_name == g_sect_name_objc_classrefs ||
1080 section_name == g_sect_name_objc_superrefs ||
1081 section_name == g_sect_name_objc_const ||
1082 section_name == g_sect_name_objc_classlist)
1083 {
1084 sect_type = eSectionTypeDataPointers;
1085 }
Chris Lattner24943d22010-06-08 16:52:24 +00001086
1087 if (sect_type == eSectionTypeOther)
1088 {
1089 switch (mach_sect_type)
1090 {
1091 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001092 case SectionTypeRegular:
1093 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001094 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001095 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001096 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001097 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001098 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001099 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001100 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1101 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1102 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1103 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1104 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1105 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1106 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1107 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1108 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1109 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1110 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1111 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1112 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1113 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1114 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1115 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001116 default: break;
1117 }
1118 }
1119
Greg Clayton3508c382012-02-24 01:59:29 +00001120 SectionSP section_sp(new Section (segment_sp,
1121 module_sp,
1122 ++sectID,
1123 section_name,
1124 sect_type,
1125 sect64.addr - segment_sp->GetFileAddress(),
1126 sect64.size,
1127 sect64.offset,
1128 sect64.offset == 0 ? 0 : sect64.size,
1129 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001130 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001131
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001132 bool section_is_encrypted = false;
1133 if (!segment_is_encrypted && load_cmd.filesize != 0)
1134 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001135
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001136 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001137 segment_sp->GetChildren().AddSection(section_sp);
1138
1139 if (segment_sp->IsFake())
1140 {
1141 segment_sp.reset();
1142 segment_name.Clear();
1143 }
1144 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001145 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001146 {
1147 if (first_segment_sectID <= sectID)
1148 {
1149 lldb::user_id_t sect_uid;
1150 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1151 {
1152 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1153 SectionSP next_section_sp;
1154 if (sect_uid + 1 <= sectID)
1155 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1156
1157 if (curr_section_sp.get())
1158 {
1159 if (curr_section_sp->GetByteSize() == 0)
1160 {
1161 if (next_section_sp.get() != NULL)
1162 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1163 else
1164 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1165 }
1166 }
1167 }
1168 }
1169 }
1170 }
1171 }
1172 }
Greg Clayton1674b122010-07-21 22:12:05 +00001173 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001174 {
1175 m_dysymtab.cmd = load_cmd.cmd;
1176 m_dysymtab.cmdsize = load_cmd.cmdsize;
1177 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1178 }
1179
1180 offset = load_cmd_offset + load_cmd.cmdsize;
1181 }
1182// if (dump_sections)
1183// {
1184// StreamFile s(stdout);
1185// m_sections_ap->Dump(&s, true);
1186// }
1187 return sectID; // Return the number of sections we registered with the module
1188}
1189
1190class MachSymtabSectionInfo
1191{
1192public:
1193
1194 MachSymtabSectionInfo (SectionList *section_list) :
1195 m_section_list (section_list),
1196 m_section_infos()
1197 {
1198 // Get the number of sections down to a depth of 1 to include
1199 // all segments and their sections, but no other sections that
1200 // may be added for debug map or
1201 m_section_infos.resize(section_list->GetNumSections(1));
1202 }
1203
1204
Greg Clayton3508c382012-02-24 01:59:29 +00001205 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001206 GetSection (uint8_t n_sect, addr_t file_addr)
1207 {
1208 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001209 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001210 if (n_sect < m_section_infos.size())
1211 {
Greg Clayton3508c382012-02-24 01:59:29 +00001212 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001213 {
Greg Clayton3508c382012-02-24 01:59:29 +00001214 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1215 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001216 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001217 {
Greg Clayton3508c382012-02-24 01:59:29 +00001218 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1219 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001220 }
1221 else
1222 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001223 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001224 }
Chris Lattner24943d22010-06-08 16:52:24 +00001225 }
1226 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001227 {
1228 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001229 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001230 }
1231 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1232 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1233 {
1234 // Symbol is in section with zero size, but has the same start
1235 // address as the section. This can happen with linker symbols
1236 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001237 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001238 }
Chris Lattner24943d22010-06-08 16:52:24 +00001239 }
Greg Clayton3508c382012-02-24 01:59:29 +00001240 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001241 }
1242
1243protected:
1244 struct SectionInfo
1245 {
1246 SectionInfo () :
1247 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001248 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001249 {
1250 }
1251
1252 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001253 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001254 };
1255 SectionList *m_section_list;
1256 std::vector<SectionInfo> m_section_infos;
1257};
1258
Chris Lattner24943d22010-06-08 16:52:24 +00001259size_t
1260ObjectFileMachO::ParseSymtab (bool minimize)
1261{
1262 Timer scoped_timer(__PRETTY_FUNCTION__,
1263 "ObjectFileMachO::ParseSymtab () module = %s",
1264 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001265 ModuleSP module_sp (GetModule());
1266 if (!module_sp)
1267 return 0;
1268
1269 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1270 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1271 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1272 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001273 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001274 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001275
Greg Clayton952e9dc2013-03-27 23:08:40 +00001276 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001277
Chris Lattner24943d22010-06-08 16:52:24 +00001278 for (i=0; i<m_header.ncmds; ++i)
1279 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001280 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001281 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001282 struct load_command lc;
1283 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001284 break;
1285 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001286 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001287 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001288 case LoadCommandSymtab:
1289 symtab_load_command.cmd = lc.cmd;
1290 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001291 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001292 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1293 return 0;
1294 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001295 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001296 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001297 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001298 return 0;
1299 }
1300
1301 if (symtab_load_command.stroff == 0)
1302 {
1303 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001304 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001305 return 0;
1306 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001307
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001308 if (symtab_load_command.nsyms == 0)
1309 {
1310 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001311 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001312 return 0;
1313 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001314
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001315 if (symtab_load_command.strsize == 0)
1316 {
1317 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001318 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001319 return 0;
1320 }
1321 break;
1322
1323 case LoadCommandFunctionStarts:
1324 function_starts_load_command.cmd = lc.cmd;
1325 function_starts_load_command.cmdsize = lc.cmdsize;
1326 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1327 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1328 break;
1329
1330 default:
1331 break;
1332 }
1333 offset = cmd_offset + lc.cmdsize;
1334 }
1335
1336 if (symtab_load_command.cmd)
1337 {
1338 Symtab *symtab = m_symtab_ap.get();
1339 SectionList *section_list = GetSectionList();
1340 if (section_list == NULL)
1341 return 0;
1342
1343 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001344 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001345
Greg Clayton36da2aa2013-01-25 18:06:21 +00001346 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1347 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001348 bool bit_width_32 = addr_byte_size == 4;
1349 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1350
Greg Clayton36da2aa2013-01-25 18:06:21 +00001351 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1352 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1353 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001354 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001355
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001356 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1357 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001358 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1359 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001360 {
Greg Claytondd29b972012-05-18 23:20:01 +00001361 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001362 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1363 // Reading mach file from memory in a process or core file...
1364
1365 if (linkedit_section_sp)
1366 {
1367 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1368 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1369 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001370 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001371
1372 bool data_was_read = false;
1373
1374#if defined (__APPLE__) && defined (__arm__)
1375 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001376 {
Greg Clayton29021d32012-04-18 05:19:20 +00001377 // This mach-o memory file is in the dyld shared cache. If this
1378 // program is not remote and this is iOS, then this process will
1379 // share the same shared cache as the process we are debugging and
1380 // we can read the entire __LINKEDIT from the address space in this
1381 // process. This is a needed optimization that is used for local iOS
1382 // debugging only since all shared libraries in the shared cache do
1383 // not have corresponding files that exist in the file system of the
1384 // device. They have been combined into a single file. This means we
1385 // always have to load these files from memory. All of the symbol and
1386 // string tables from all of the __LINKEDIT sections from the shared
1387 // libraries in the shared cache have been merged into a single large
1388 // symbol and string table. Reading all of this symbol and string table
1389 // data across can slow down debug launch times, so we optimize this by
1390 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda45c75502013-04-16 06:24:42 +00001391
1392 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1393 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1394 bool use_lldb_cache = true;
1395 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1396 {
1397 use_lldb_cache = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001398 ModuleSP module_sp (GetModule());
1399 if (module_sp)
1400 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1401
Jason Molenda45c75502013-04-16 06:24:42 +00001402 }
1403
Greg Clayton29021d32012-04-18 05:19:20 +00001404 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda45c75502013-04-16 06:24:42 +00001405 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton29021d32012-04-18 05:19:20 +00001406 {
1407 data_was_read = true;
1408 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001409 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001410 if (function_starts_load_command.cmd)
1411 {
1412 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1413 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1414 }
1415 }
1416 }
1417#endif
1418
1419 if (!data_was_read)
1420 {
1421 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1422 if (nlist_data_sp)
1423 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001424 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1425 //if (strtab_data_sp)
1426 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001427 if (m_dysymtab.nindirectsyms != 0)
1428 {
1429 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1430 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1431 if (indirect_syms_data_sp)
1432 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1433 }
Greg Clayton29021d32012-04-18 05:19:20 +00001434 if (function_starts_load_command.cmd)
1435 {
1436 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1437 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1438 if (func_start_data_sp)
1439 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1440 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001441 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001442 }
1443 }
1444 else
1445 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001446 nlist_data.SetData (m_data,
1447 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001448 nlist_data_byte_size);
1449 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001450 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001451 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001452 if (m_dysymtab.nindirectsyms != 0)
1453 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001454 indirect_symbol_index_data.SetData (m_data,
1455 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001456 m_dysymtab.nindirectsyms * 4);
1457 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001458 if (function_starts_load_command.cmd)
1459 {
1460 function_starts_data.SetData (m_data,
1461 function_starts_load_command.dataoff,
1462 function_starts_load_command.datasize);
1463 }
1464 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001465
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001466 if (nlist_data.GetByteSize() == 0)
1467 {
1468 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001469 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001470 return 0;
1471 }
1472
1473
Greg Clayton3a5dc012012-05-25 17:04:00 +00001474 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1475 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001476 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001477 if (process)
1478 {
1479 if (strtab_addr == LLDB_INVALID_ADDRESS)
1480 {
1481 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001482 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001483 return 0;
1484 }
1485 }
1486 else
Greg Claytondd29b972012-05-18 23:20:01 +00001487 {
1488 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001489 module_sp->LogMessage(log, "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001490 return 0;
1491 }
1492 }
Greg Claytondd29b972012-05-18 23:20:01 +00001493
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001494 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1495 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1496 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1497 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1498 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1499 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1500 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1501 SectionSP eh_frame_section_sp;
1502 if (text_section_sp.get())
1503 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1504 else
1505 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1506
Greg Claytond2653c22012-03-14 01:53:24 +00001507 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001508
1509 // lldb works best if it knows the start addresss of all functions in a module.
1510 // Linker symbols or debug info are normally the best source of information for start addr / size but
1511 // they may be stripped in a released binary.
Jason Molenda2120aef2013-04-16 00:18:44 +00001512 // Two additional sources of information exist in Mach-O binaries:
Jason Molendad7938392013-03-21 03:36:01 +00001513 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1514 // binary, relative to the text section.
1515 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1516 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1517 // Binaries built to run on older releases may need to use eh_frame information.
1518
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001519 if (text_section_sp && function_starts_data.GetByteSize())
1520 {
1521 FunctionStarts::Entry function_start_entry;
1522 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001523 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001524 function_start_entry.addr = text_section_sp->GetFileAddress();
1525 uint64_t delta;
1526 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1527 {
1528 // Now append the current entry
1529 function_start_entry.addr += delta;
1530 function_starts.Append(function_start_entry);
1531 }
Jason Molenda2120aef2013-04-16 00:18:44 +00001532 }
Jason Molendad7938392013-03-21 03:36:01 +00001533 else
1534 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001535 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1536 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1537 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1538 // the module.
1539 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001540 {
1541 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1542 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1543 eh_frame.GetFunctionAddressAndSizeVector (functions);
1544 addr_t text_base_addr = text_section_sp->GetFileAddress();
1545 size_t count = functions.GetSize();
1546 for (size_t i = 0; i < count; ++i)
1547 {
1548 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1549 if (func)
1550 {
1551 FunctionStarts::Entry function_start_entry;
1552 function_start_entry.addr = func->base - text_base_addr;
1553 function_starts.Append(function_start_entry);
1554 }
1555 }
1556 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001557 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001558
Greg Clayton36da2aa2013-01-25 18:06:21 +00001559 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001560
Greg Clayton36da2aa2013-01-25 18:06:21 +00001561 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 +00001562
Greg Clayton36da2aa2013-01-25 18:06:21 +00001563 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001564
1565 uint32_t N_SO_index = UINT32_MAX;
1566
1567 MachSymtabSectionInfo section_info (section_list);
1568 std::vector<uint32_t> N_FUN_indexes;
1569 std::vector<uint32_t> N_NSYM_indexes;
1570 std::vector<uint32_t> N_INCL_indexes;
1571 std::vector<uint32_t> N_BRAC_indexes;
1572 std::vector<uint32_t> N_COMM_indexes;
1573 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1574 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1575 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1576 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1577 // Any symbols that get merged into another will get an entry
1578 // in this map so we know
1579 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1580 uint32_t nlist_idx = 0;
1581 Symbol *symbol_ptr = NULL;
1582
1583 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001584 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001585 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001586 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001587 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001588
Jason Molendab62abd52012-06-21 01:51:02 +00001589#if defined (__APPLE__) && defined (__arm__)
1590
1591 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1592 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1593 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1594 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1595 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1596 // nlist parser to ignore all LOCAL symbols.
1597
1598 if (m_header.flags & 0x80000000u)
1599 {
1600 // Before we can start mapping the DSC, we need to make certain the target process is actually
1601 // using the cache we can find.
1602
Jason Molendab62abd52012-06-21 01:51:02 +00001603 // Next we need to determine the correct path for the dyld shared cache.
1604
1605 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1606 char dsc_path[PATH_MAX];
1607
1608 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001609 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1610 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001611 header_arch.GetArchitectureName());
1612
1613 FileSpec dsc_filespec(dsc_path, false);
1614
1615 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda2120aef2013-04-16 00:18:44 +00001616 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001617 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001618 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1619 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1620 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001621 uint32_t imagesOffset;
1622 uint32_t imagesCount;
1623 uint64_t dyldBaseAddress;
1624 uint64_t codeSignatureOffset;
1625 uint64_t codeSignatureSize;
1626 uint64_t slideInfoOffset;
1627 uint64_t slideInfoSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001628 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1629 uint64_t localSymbolsSize; // size of local symbols information
1630 };
1631 struct lldb_copy_dyld_cache_header_v1
1632 {
1633 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1634 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1635 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1636 uint32_t imagesOffset;
1637 uint32_t imagesCount;
1638 uint64_t dyldBaseAddress;
1639 uint64_t codeSignatureOffset;
1640 uint64_t codeSignatureSize;
1641 uint64_t slideInfoOffset;
1642 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001643 uint64_t localSymbolsOffset;
1644 uint64_t localSymbolsSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001645 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001646 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001647
Jason Molenda2120aef2013-04-16 00:18:44 +00001648 struct lldb_copy_dyld_cache_mapping_info
1649 {
1650 uint64_t address;
1651 uint64_t size;
1652 uint64_t fileOffset;
1653 uint32_t maxProt;
1654 uint32_t initProt;
1655 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001656
Greg Claytonab77dcb2012-09-05 22:30:51 +00001657 struct lldb_copy_dyld_cache_local_symbols_info
1658 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001659 uint32_t nlistOffset;
1660 uint32_t nlistCount;
1661 uint32_t stringsOffset;
1662 uint32_t stringsSize;
1663 uint32_t entriesOffset;
1664 uint32_t entriesCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001665 };
1666 struct lldb_copy_dyld_cache_local_symbols_entry
1667 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001668 uint32_t dylibOffset;
1669 uint32_t nlistStartIndex;
1670 uint32_t nlistCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001671 };
Jason Molendab62abd52012-06-21 01:51:02 +00001672
Jason Molendafd3b35d2012-06-22 03:28:35 +00001673 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1674 The dyld_cache_local_symbols_info structure gives us three things:
1675 1. The start and count of the nlist records in the dyld_shared_cache file
1676 2. The start and size of the strings for these nlist records
1677 3. The start and count of dyld_cache_local_symbols_entry entries
1678
1679 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1680 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001681 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001682 and the count of how many nlist records there are for this dylib/framework.
1683 */
1684
Jason Molendab62abd52012-06-21 01:51:02 +00001685 // Process the dsc header to find the unmapped symbols
1686 //
1687 // Save some VM space, do not map the entire cache in one shot.
1688
Jason Molenda6bcabae2013-03-06 23:17:36 +00001689 DataBufferSP dsc_data_sp;
1690 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1691
1692 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001693 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001694 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001695
Jason Molenda6bcabae2013-03-06 23:17:36 +00001696 char version_str[17];
1697 int version = -1;
1698 lldb::offset_t offset = 0;
1699 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1700 version_str[16] = '\0';
1701 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1702 {
1703 int v;
1704 if (::sscanf (version_str + 6, "%d", &v) == 1)
1705 {
1706 version = v;
1707 }
1708 }
1709
Jason Molenda45c75502013-04-16 06:24:42 +00001710 UUID dsc_uuid;
1711 if (version >= 1)
1712 {
1713 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1714 uint8_t uuid_bytes[sizeof (uuid_t)];
1715 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1716 dsc_uuid.SetBytes (uuid_bytes);
1717 }
1718
1719 bool uuid_match = true;
1720 if (dsc_uuid.IsValid() && process)
1721 {
1722 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1723
1724 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1725 {
1726 // The on-disk dyld_shared_cache file is not the same as the one in this
1727 // process' memory, don't use it.
1728 uuid_match = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001729 ModuleSP module_sp (GetModule());
1730 if (module_sp)
1731 module_sp->ReportWarning ("process shared cache does not match on-disk dyld_shared_cache file, some symbol names will be missing.");
Jason Molenda45c75502013-04-16 06:24:42 +00001732 }
1733 }
1734
Jason Molenda9badb6c2013-03-06 23:19:17 +00001735 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001736
Jason Molendab62abd52012-06-21 01:51:02 +00001737 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1738
1739 // If the mappingOffset points to a location inside the header, we've
1740 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda45c75502013-04-16 06:24:42 +00001741 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendab62abd52012-06-21 01:51:02 +00001742 {
1743
Jason Molenda6bcabae2013-03-06 23:17:36 +00001744 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1745 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1746 offset = 0;
1747
1748 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1749 // in the shared library cache need to be adjusted by an offset to match up with the
1750 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1751 // recorded in mapping_offset_value.
1752 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1753
1754 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001755 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1756 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1757
Jason Molenda9badb6c2013-03-06 23:19:17 +00001758 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001759 {
1760 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001761 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001762 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001763 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001764
1765 offset = 0;
1766
1767 // Read the local_symbols_infos struct in one shot
1768 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1769 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1770
Jason Molendab62abd52012-06-21 01:51:02 +00001771 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1772
Jason Molenda6bcabae2013-03-06 23:17:36 +00001773 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001774
1775 offset = local_symbols_info.entriesOffset;
1776 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1777 {
1778 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1779 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1780 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1781 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1782
Jason Molenda9badb6c2013-03-06 23:19:17 +00001783 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001784 {
1785 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1786
1787 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1788 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1789 num_syms = symtab->GetNumSymbols();
1790
1791 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1792 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1793
Jason Molenda9badb6c2013-03-06 23:19:17 +00001794 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001795 {
1796 /////////////////////////////
1797 {
1798 struct nlist_64 nlist;
1799 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1800 break;
1801
1802 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1803 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1804 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1805 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1806 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1807
1808 SymbolType type = eSymbolTypeInvalid;
1809 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1810
1811 if (symbol_name == NULL)
1812 {
1813 // No symbol should be NULL, even the symbols with no
1814 // string values should have an offset zero which points
1815 // to an empty C-string
1816 Host::SystemLog (Host::eSystemLogError,
1817 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1818 entry_index,
1819 nlist.n_strx,
1820 module_sp->GetFileSpec().GetDirectory().GetCString(),
1821 module_sp->GetFileSpec().GetFilename().GetCString());
1822 continue;
1823 }
1824 if (symbol_name[0] == '\0')
1825 symbol_name = NULL;
1826
1827 const char *symbol_name_non_abi_mangled = NULL;
1828
1829 SectionSP symbol_section;
1830 uint32_t symbol_byte_size = 0;
1831 bool add_nlist = true;
1832 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001833 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001834
1835 assert (sym_idx < num_syms);
1836
1837 sym[sym_idx].SetDebug (is_debug);
1838
1839 if (is_debug)
1840 {
1841 switch (nlist.n_type)
1842 {
1843 case StabGlobalSymbol:
1844 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1845 // Sometimes the N_GSYM value contains the address.
1846
1847 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1848 // have the same address, but we want to ensure that we always find only the real symbol,
1849 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1850 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1851 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1852 // same address.
1853
1854 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1855 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1856 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1857 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1858 add_nlist = false;
1859 else
1860 {
1861 sym[sym_idx].SetExternal(true);
1862 if (nlist.n_value != 0)
1863 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1864 type = eSymbolTypeData;
1865 }
1866 break;
1867
1868 case StabFunctionName:
1869 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1870 type = eSymbolTypeCompiler;
1871 break;
1872
1873 case StabFunction:
1874 // N_FUN -- procedure: name,,n_sect,linenumber,address
1875 if (symbol_name)
1876 {
1877 type = eSymbolTypeCode;
1878 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1879
1880 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1881 // We use the current number of symbols in the symbol table in lieu of
1882 // using nlist_idx in case we ever start trimming entries out
1883 N_FUN_indexes.push_back(sym_idx);
1884 }
1885 else
1886 {
1887 type = eSymbolTypeCompiler;
1888
1889 if ( !N_FUN_indexes.empty() )
1890 {
1891 // Copy the size of the function into the original STAB entry so we don't have
1892 // to hunt for it later
1893 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1894 N_FUN_indexes.pop_back();
1895 // We don't really need the end function STAB as it contains the size which
1896 // we already placed with the original symbol, so don't add it if we want a
1897 // minimal symbol table
1898 if (minimize)
1899 add_nlist = false;
1900 }
1901 }
1902 break;
1903
1904 case StabStaticSymbol:
1905 // N_STSYM -- static symbol: name,,n_sect,type,address
1906 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1907 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1908 type = eSymbolTypeData;
1909 break;
1910
1911 case StabLocalCommon:
1912 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1913 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1914 type = eSymbolTypeCommonBlock;
1915 break;
1916
1917 case StabBeginSymbol:
1918 // N_BNSYM
1919 // We use the current number of symbols in the symbol table in lieu of
1920 // using nlist_idx in case we ever start trimming entries out
1921 if (minimize)
1922 {
1923 // Skip these if we want minimal symbol tables
1924 add_nlist = false;
1925 }
1926 else
1927 {
1928 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1929 N_NSYM_indexes.push_back(sym_idx);
1930 type = eSymbolTypeScopeBegin;
1931 }
1932 break;
1933
1934 case StabEndSymbol:
1935 // N_ENSYM
1936 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1937 // so that we can always skip the entire symbol if we need to navigate
1938 // more quickly at the source level when parsing STABS
1939 if (minimize)
1940 {
1941 // Skip these if we want minimal symbol tables
1942 add_nlist = false;
1943 }
1944 else
1945 {
1946 if ( !N_NSYM_indexes.empty() )
1947 {
1948 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1949 symbol_ptr->SetByteSize(sym_idx + 1);
1950 symbol_ptr->SetSizeIsSibling(true);
1951 N_NSYM_indexes.pop_back();
1952 }
1953 type = eSymbolTypeScopeEnd;
1954 }
1955 break;
1956
1957
1958 case StabSourceFileOptions:
1959 // N_OPT - emitted with gcc2_compiled and in gcc source
1960 type = eSymbolTypeCompiler;
1961 break;
1962
1963 case StabRegisterSymbol:
1964 // N_RSYM - register sym: name,,NO_SECT,type,register
1965 type = eSymbolTypeVariable;
1966 break;
1967
1968 case StabSourceLine:
1969 // N_SLINE - src line: 0,,n_sect,linenumber,address
1970 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1971 type = eSymbolTypeLineEntry;
1972 break;
1973
1974 case StabStructureType:
1975 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1976 type = eSymbolTypeVariableType;
1977 break;
1978
1979 case StabSourceFileName:
1980 // N_SO - source file name
1981 type = eSymbolTypeSourceFile;
1982 if (symbol_name == NULL)
1983 {
1984 if (minimize)
1985 add_nlist = false;
1986 if (N_SO_index != UINT32_MAX)
1987 {
1988 // Set the size of the N_SO to the terminating index of this N_SO
1989 // so that we can always skip the entire N_SO if we need to navigate
1990 // more quickly at the source level when parsing STABS
1991 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1992 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1993 symbol_ptr->SetSizeIsSibling(true);
1994 }
1995 N_NSYM_indexes.clear();
1996 N_INCL_indexes.clear();
1997 N_BRAC_indexes.clear();
1998 N_COMM_indexes.clear();
1999 N_FUN_indexes.clear();
2000 N_SO_index = UINT32_MAX;
2001 }
2002 else
2003 {
2004 // We use the current number of symbols in the symbol table in lieu of
2005 // using nlist_idx in case we ever start trimming entries out
2006 const bool N_SO_has_full_path = symbol_name[0] == '/';
2007 if (N_SO_has_full_path)
2008 {
2009 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2010 {
2011 // We have two consecutive N_SO entries where the first contains a directory
2012 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00002013 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002014 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2015 add_nlist = false;
2016 }
2017 else
2018 {
2019 // This is the first entry in a N_SO that contains a directory or
2020 // a full path to the source file
2021 N_SO_index = sym_idx;
2022 }
2023 }
2024 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2025 {
2026 // This is usually the second N_SO entry that contains just the filename,
2027 // so here we combine it with the first one if we are minimizing the symbol table
2028 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2029 if (so_path && so_path[0])
2030 {
2031 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002032 const size_t double_slash_pos = full_so_path.find("//");
2033 if (double_slash_pos != std::string::npos)
2034 {
2035 // The linker has been generating bad N_SO entries with doubled up paths
2036 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2037 // and the second is the directory for the source file so you end up with
2038 // a path that looks like "/tmp/src//tmp/src/"
2039 FileSpec so_dir(so_path, false);
2040 if (!so_dir.Exists())
2041 {
2042 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2043 if (so_dir.Exists())
2044 {
2045 // Trim off the incorrect path
2046 full_so_path.erase(0, double_slash_pos + 1);
2047 }
2048 }
2049 }
Jason Molendab62abd52012-06-21 01:51:02 +00002050 if (*full_so_path.rbegin() != '/')
2051 full_so_path += '/';
2052 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00002053 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002054 add_nlist = false;
2055 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2056 }
2057 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002058 else
2059 {
2060 // This could be a relative path to a N_SO
2061 N_SO_index = sym_idx;
2062 }
Jason Molendab62abd52012-06-21 01:51:02 +00002063 }
Jason Molendab62abd52012-06-21 01:51:02 +00002064 break;
2065
2066 case StabObjectFileName:
2067 // N_OSO - object file name: name,,0,0,st_mtime
2068 type = eSymbolTypeObjectFile;
2069 break;
2070
2071 case StabLocalSymbol:
2072 // N_LSYM - local sym: name,,NO_SECT,type,offset
2073 type = eSymbolTypeLocal;
2074 break;
2075
2076 //----------------------------------------------------------------------
2077 // INCL scopes
2078 //----------------------------------------------------------------------
2079 case StabBeginIncludeFileName:
2080 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2081 // We use the current number of symbols in the symbol table in lieu of
2082 // using nlist_idx in case we ever start trimming entries out
2083 N_INCL_indexes.push_back(sym_idx);
2084 type = eSymbolTypeScopeBegin;
2085 break;
2086
2087 case StabEndIncludeFile:
2088 // N_EINCL - include file end: name,,NO_SECT,0,0
2089 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2090 // so that we can always skip the entire symbol if we need to navigate
2091 // more quickly at the source level when parsing STABS
2092 if ( !N_INCL_indexes.empty() )
2093 {
2094 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2095 symbol_ptr->SetByteSize(sym_idx + 1);
2096 symbol_ptr->SetSizeIsSibling(true);
2097 N_INCL_indexes.pop_back();
2098 }
2099 type = eSymbolTypeScopeEnd;
2100 break;
2101
2102 case StabIncludeFileName:
2103 // N_SOL - #included file name: name,,n_sect,0,address
2104 type = eSymbolTypeHeaderFile;
2105
2106 // We currently don't use the header files on darwin
2107 if (minimize)
2108 add_nlist = false;
2109 break;
2110
2111 case StabCompilerParameters:
2112 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2113 type = eSymbolTypeCompiler;
2114 break;
2115
2116 case StabCompilerVersion:
2117 // N_VERSION - compiler version: name,,NO_SECT,0,0
2118 type = eSymbolTypeCompiler;
2119 break;
2120
2121 case StabCompilerOptLevel:
2122 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2123 type = eSymbolTypeCompiler;
2124 break;
2125
2126 case StabParameter:
2127 // N_PSYM - parameter: name,,NO_SECT,type,offset
2128 type = eSymbolTypeVariable;
2129 break;
2130
2131 case StabAlternateEntry:
2132 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2133 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2134 type = eSymbolTypeLineEntry;
2135 break;
2136
2137 //----------------------------------------------------------------------
2138 // Left and Right Braces
2139 //----------------------------------------------------------------------
2140 case StabLeftBracket:
2141 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2142 // We use the current number of symbols in the symbol table in lieu of
2143 // using nlist_idx in case we ever start trimming entries out
2144 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2145 N_BRAC_indexes.push_back(sym_idx);
2146 type = eSymbolTypeScopeBegin;
2147 break;
2148
2149 case StabRightBracket:
2150 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2151 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2152 // so that we can always skip the entire symbol if we need to navigate
2153 // more quickly at the source level when parsing STABS
2154 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2155 if ( !N_BRAC_indexes.empty() )
2156 {
2157 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2158 symbol_ptr->SetByteSize(sym_idx + 1);
2159 symbol_ptr->SetSizeIsSibling(true);
2160 N_BRAC_indexes.pop_back();
2161 }
2162 type = eSymbolTypeScopeEnd;
2163 break;
2164
2165 case StabDeletedIncludeFile:
2166 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2167 type = eSymbolTypeHeaderFile;
2168 break;
2169
2170 //----------------------------------------------------------------------
2171 // COMM scopes
2172 //----------------------------------------------------------------------
2173 case StabBeginCommon:
2174 // N_BCOMM - begin common: name,,NO_SECT,0,0
2175 // We use the current number of symbols in the symbol table in lieu of
2176 // using nlist_idx in case we ever start trimming entries out
2177 type = eSymbolTypeScopeBegin;
2178 N_COMM_indexes.push_back(sym_idx);
2179 break;
2180
2181 case StabEndCommonLocal:
2182 // N_ECOML - end common (local name): 0,,n_sect,0,address
2183 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2184 // Fall through
2185
2186 case StabEndCommon:
2187 // N_ECOMM - end common: name,,n_sect,0,0
2188 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2189 // so that we can always skip the entire symbol if we need to navigate
2190 // more quickly at the source level when parsing STABS
2191 if ( !N_COMM_indexes.empty() )
2192 {
2193 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2194 symbol_ptr->SetByteSize(sym_idx + 1);
2195 symbol_ptr->SetSizeIsSibling(true);
2196 N_COMM_indexes.pop_back();
2197 }
2198 type = eSymbolTypeScopeEnd;
2199 break;
2200
2201 case StabLength:
2202 // N_LENG - second stab entry with length information
2203 type = eSymbolTypeAdditional;
2204 break;
2205
2206 default: break;
2207 }
2208 }
2209 else
2210 {
2211 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2212 uint8_t n_type = NlistMaskType & nlist.n_type;
2213 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2214
2215 switch (n_type)
2216 {
2217 case NListTypeIndirect: // N_INDR - Fall through
2218 case NListTypePreboundUndefined:// N_PBUD - Fall through
2219 case NListTypeUndefined: // N_UNDF
2220 type = eSymbolTypeUndefined;
2221 break;
2222
2223 case NListTypeAbsolute: // N_ABS
2224 type = eSymbolTypeAbsolute;
2225 break;
2226
2227 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002228 {
2229 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002230
Greg Clayton01e6a582012-11-27 01:52:16 +00002231 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002232 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002233 // TODO: warn about this?
2234 add_nlist = false;
2235 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002236 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002237
2238 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002239 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002240 type = eSymbolTypeException;
2241 }
2242 else
2243 {
2244 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002245
Greg Clayton01e6a582012-11-27 01:52:16 +00002246 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002247 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002248 case SectionTypeRegular: break; // regular section
2249 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2250 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2251 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2252 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2253 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2254 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2255 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2256 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2257 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2258 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2259 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2260 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2261 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2262 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2263 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2264 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2265 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002266 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002267
Greg Clayton01e6a582012-11-27 01:52:16 +00002268 if (type == eSymbolTypeInvalid)
2269 {
2270 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2271 if (symbol_section->IsDescendant (text_section_sp.get()))
2272 {
2273 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2274 SectionAttrUserSelfModifyingCode |
2275 SectionAttrSytemSomeInstructions))
2276 type = eSymbolTypeData;
2277 else
2278 type = eSymbolTypeCode;
2279 }
2280 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002281 {
2282 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2283 {
2284 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002285
Greg Clayton01e6a582012-11-27 01:52:16 +00002286 if (symbol_name &&
2287 symbol_name[0] == '_' &&
2288 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002289 symbol_name[2] == 'B')
2290 {
2291 llvm::StringRef symbol_name_ref(symbol_name);
2292 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2293 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2294 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2295 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2296 {
2297 symbol_name_non_abi_mangled = symbol_name + 1;
2298 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2299 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002300 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002301 }
2302 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2303 {
2304 symbol_name_non_abi_mangled = symbol_name + 1;
2305 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2306 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002307 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002308 }
2309 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2310 {
2311 symbol_name_non_abi_mangled = symbol_name + 1;
2312 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2313 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002314 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002315 }
2316 }
2317 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002318 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002319 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002320 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002321 }
2322 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002323 {
2324 type = eSymbolTypeData;
2325 }
2326 }
2327 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2328 {
2329 type = eSymbolTypeTrampoline;
2330 }
2331 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2332 {
2333 type = eSymbolTypeRuntime;
2334 if (symbol_name && symbol_name[0] == '.')
2335 {
2336 llvm::StringRef symbol_name_ref(symbol_name);
2337 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2338 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002339 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002340 symbol_name_non_abi_mangled = symbol_name;
2341 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2342 type = eSymbolTypeObjCClass;
2343 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002344 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002345 }
2346 }
2347 }
Jason Molendab62abd52012-06-21 01:51:02 +00002348 }
2349 }
Jason Molendab62abd52012-06-21 01:51:02 +00002350 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002351 }
Jason Molendab62abd52012-06-21 01:51:02 +00002352 }
2353
2354 if (add_nlist)
2355 {
2356 uint64_t symbol_value = nlist.n_value;
2357 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002358
Jason Molendab62abd52012-06-21 01:51:02 +00002359 if (symbol_name_non_abi_mangled)
2360 {
Jason Molenda292cca82012-07-20 03:35:44 +00002361 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2362 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002363 }
2364 else
2365 {
2366 if (symbol_name && symbol_name[0] == '_')
2367 {
2368 symbol_name_is_mangled = symbol_name[1] == '_';
2369 symbol_name++; // Skip the leading underscore
2370 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002371
Jason Molendab62abd52012-06-21 01:51:02 +00002372 if (symbol_name)
2373 {
Jason Molenda292cca82012-07-20 03:35:44 +00002374 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002375 }
2376 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002377
Jason Molendab62abd52012-06-21 01:51:02 +00002378 if (is_debug == false)
2379 {
2380 if (type == eSymbolTypeCode)
2381 {
2382 // See if we can find a N_FUN entry for any code symbols.
2383 // If we do find a match, and the name matches, then we
2384 // can merge the two into just the function symbol to avoid
2385 // duplicate entries in the symbol table
2386 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2387 if (pos != N_FUN_addr_to_sym_idx.end())
2388 {
2389 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2390 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2391 {
2392 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2393 // We just need the flags from the linker symbol, so put these flags
2394 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2395 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2396 sym[sym_idx].Clear();
2397 continue;
2398 }
2399 }
2400 }
2401 else if (type == eSymbolTypeData)
2402 {
2403 // See if we can find a N_STSYM entry for any data symbols.
2404 // If we do find a match, and the name matches, then we
2405 // can merge the two into just the Static symbol to avoid
2406 // duplicate entries in the symbol table
2407 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2408 if (pos != N_STSYM_addr_to_sym_idx.end())
2409 {
2410 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2411 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2412 {
2413 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2414 // We just need the flags from the linker symbol, so put these flags
2415 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2416 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2417 sym[sym_idx].Clear();
2418 continue;
2419 }
2420 }
2421 }
2422 }
2423 if (symbol_section)
2424 {
2425 const addr_t section_file_addr = symbol_section->GetFileAddress();
2426 if (symbol_byte_size == 0 && function_starts_count > 0)
2427 {
2428 addr_t symbol_lookup_file_addr = nlist.n_value;
2429 // Do an exact address match for non-ARM addresses, else get the closest since
2430 // the symbol might be a thumb symbol which has an address with bit zero set
2431 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2432 if (is_arm && func_start_entry)
2433 {
2434 // Verify that the function start address is the symbol address (ARM)
2435 // or the symbol address + 1 (thumb)
2436 if (func_start_entry->addr != symbol_lookup_file_addr &&
2437 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2438 {
2439 // Not the right entry, NULL it out...
2440 func_start_entry = NULL;
2441 }
2442 }
2443 if (func_start_entry)
2444 {
2445 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002446
Jason Molendab62abd52012-06-21 01:51:02 +00002447 addr_t symbol_file_addr = func_start_entry->addr;
2448 uint32_t symbol_flags = 0;
2449 if (is_arm)
2450 {
2451 if (symbol_file_addr & 1)
2452 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2453 symbol_file_addr &= 0xfffffffffffffffeull;
2454 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002455
Jason Molendab62abd52012-06-21 01:51:02 +00002456 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2457 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2458 if (next_func_start_entry)
2459 {
2460 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2461 // Be sure the clear the Thumb address bit when we calculate the size
2462 // from the current and next address
2463 if (is_arm)
2464 next_symbol_file_addr &= 0xfffffffffffffffeull;
2465 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2466 }
2467 else
2468 {
2469 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2470 }
2471 }
2472 }
2473 symbol_value -= section_file_addr;
2474 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002475
Jason Molendab62abd52012-06-21 01:51:02 +00002476 sym[sym_idx].SetID (nlist_idx);
2477 sym[sym_idx].SetType (type);
2478 sym[sym_idx].GetAddress().SetSection (symbol_section);
2479 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2480 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002481
Jason Molendab62abd52012-06-21 01:51:02 +00002482 if (symbol_byte_size > 0)
2483 sym[sym_idx].SetByteSize(symbol_byte_size);
2484
Greg Clayton01e6a582012-11-27 01:52:16 +00002485 if (demangled_is_synthesized)
2486 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002487 ++sym_idx;
2488 }
2489 else
2490 {
2491 sym[sym_idx].Clear();
2492 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002493
Jason Molendab62abd52012-06-21 01:51:02 +00002494 }
2495 /////////////////////////////
2496 }
2497 break; // No more entries to consider
2498 }
2499 }
2500 }
2501 }
2502 }
2503 }
2504 }
2505
2506 // Must reset this in case it was mutated above!
2507 nlist_data_offset = 0;
2508#endif
2509
2510 // If the sym array was not created while parsing the DSC unmapped
2511 // symbols, create it now.
2512 if (sym == NULL)
2513 {
2514 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2515 num_syms = symtab->GetNumSymbols();
2516 }
2517
2518 if (unmapped_local_symbols_found)
2519 {
2520 assert(m_dysymtab.ilocalsym == 0);
2521 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2522 nlist_idx = m_dysymtab.nlocalsym;
2523 }
2524 else
2525 {
2526 nlist_idx = 0;
2527 }
2528
2529 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002530 {
2531 struct nlist_64 nlist;
2532 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2533 break;
2534
2535 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2536 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2537 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2538 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2539 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2540
2541 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002542 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002543
Greg Clayton3a5dc012012-05-25 17:04:00 +00002544 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002545 {
2546 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002547
Greg Claytondd29b972012-05-18 23:20:01 +00002548 if (symbol_name == NULL)
2549 {
2550 // No symbol should be NULL, even the symbols with no
2551 // string values should have an offset zero which points
2552 // to an empty C-string
2553 Host::SystemLog (Host::eSystemLogError,
Jason Molenda9badb6c2013-03-06 23:19:17 +00002554 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002555 nlist_idx,
2556 nlist.n_strx,
2557 module_sp->GetFileSpec().GetDirectory().GetCString(),
2558 module_sp->GetFileSpec().GetFilename().GetCString());
2559 continue;
2560 }
2561 if (symbol_name[0] == '\0')
2562 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002563 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002564 else
2565 {
2566 const addr_t str_addr = strtab_addr + nlist.n_strx;
2567 Error str_error;
2568 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2569 symbol_name = memory_symbol_name.c_str();
2570 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002571 const char *symbol_name_non_abi_mangled = NULL;
2572
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002573 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002574 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002575 bool add_nlist = true;
2576 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002577 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002578
2579 assert (sym_idx < num_syms);
2580
2581 sym[sym_idx].SetDebug (is_debug);
2582
2583 if (is_debug)
2584 {
2585 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002586 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002587 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002588 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2589 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002590
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002591 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2592 // have the same address, but we want to ensure that we always find only the real symbol,
2593 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2594 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2595 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2596 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002597
2598 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002599 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2600 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2601 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2602 add_nlist = false;
2603 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002604 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002605 sym[sym_idx].SetExternal(true);
2606 if (nlist.n_value != 0)
2607 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2608 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002609 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002610 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002611
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002612 case StabFunctionName:
2613 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2614 type = eSymbolTypeCompiler;
2615 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002616
Jason Molenda9badb6c2013-03-06 23:19:17 +00002617 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002618 // N_FUN -- procedure: name,,n_sect,linenumber,address
2619 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002620 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002621 type = eSymbolTypeCode;
2622 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002623
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002624 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2625 // We use the current number of symbols in the symbol table in lieu of
2626 // using nlist_idx in case we ever start trimming entries out
2627 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002628 }
2629 else
2630 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002631 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002632
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002633 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002634 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002635 // Copy the size of the function into the original STAB entry so we don't have
2636 // to hunt for it later
2637 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2638 N_FUN_indexes.pop_back();
2639 // We don't really need the end function STAB as it contains the size which
2640 // we already placed with the original symbol, so don't add it if we want a
2641 // minimal symbol table
2642 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002643 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002644 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002645 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002646 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002647
Jason Molenda9badb6c2013-03-06 23:19:17 +00002648 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002649 // N_STSYM -- static symbol: name,,n_sect,type,address
2650 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2651 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2652 type = eSymbolTypeData;
2653 break;
2654
2655 case StabLocalCommon:
2656 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2657 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2658 type = eSymbolTypeCommonBlock;
2659 break;
2660
2661 case StabBeginSymbol:
2662 // N_BNSYM
2663 // We use the current number of symbols in the symbol table in lieu of
2664 // using nlist_idx in case we ever start trimming entries out
2665 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002666 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002667 // Skip these if we want minimal symbol tables
2668 add_nlist = false;
2669 }
2670 else
2671 {
2672 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2673 N_NSYM_indexes.push_back(sym_idx);
2674 type = eSymbolTypeScopeBegin;
2675 }
2676 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002677
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002678 case StabEndSymbol:
2679 // N_ENSYM
2680 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2681 // so that we can always skip the entire symbol if we need to navigate
2682 // more quickly at the source level when parsing STABS
2683 if (minimize)
2684 {
2685 // Skip these if we want minimal symbol tables
2686 add_nlist = false;
2687 }
2688 else
2689 {
2690 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002691 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002692 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2693 symbol_ptr->SetByteSize(sym_idx + 1);
2694 symbol_ptr->SetSizeIsSibling(true);
2695 N_NSYM_indexes.pop_back();
2696 }
2697 type = eSymbolTypeScopeEnd;
2698 }
2699 break;
2700
2701
2702 case StabSourceFileOptions:
2703 // N_OPT - emitted with gcc2_compiled and in gcc source
2704 type = eSymbolTypeCompiler;
2705 break;
2706
2707 case StabRegisterSymbol:
2708 // N_RSYM - register sym: name,,NO_SECT,type,register
2709 type = eSymbolTypeVariable;
2710 break;
2711
2712 case StabSourceLine:
2713 // N_SLINE - src line: 0,,n_sect,linenumber,address
2714 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2715 type = eSymbolTypeLineEntry;
2716 break;
2717
2718 case StabStructureType:
2719 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2720 type = eSymbolTypeVariableType;
2721 break;
2722
2723 case StabSourceFileName:
2724 // N_SO - source file name
2725 type = eSymbolTypeSourceFile;
2726 if (symbol_name == NULL)
2727 {
2728 if (minimize)
2729 add_nlist = false;
2730 if (N_SO_index != UINT32_MAX)
2731 {
2732 // Set the size of the N_SO to the terminating index of this N_SO
2733 // so that we can always skip the entire N_SO if we need to navigate
2734 // more quickly at the source level when parsing STABS
2735 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2736 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2737 symbol_ptr->SetSizeIsSibling(true);
2738 }
2739 N_NSYM_indexes.clear();
2740 N_INCL_indexes.clear();
2741 N_BRAC_indexes.clear();
2742 N_COMM_indexes.clear();
2743 N_FUN_indexes.clear();
2744 N_SO_index = UINT32_MAX;
2745 }
2746 else
2747 {
2748 // We use the current number of symbols in the symbol table in lieu of
2749 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002750 const bool N_SO_has_full_path = symbol_name[0] == '/';
2751 if (N_SO_has_full_path)
2752 {
2753 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2754 {
2755 // We have two consecutive N_SO entries where the first contains a directory
2756 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002757 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002758 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2759 add_nlist = false;
2760 }
2761 else
2762 {
2763 // This is the first entry in a N_SO that contains a directory or
2764 // a full path to the source file
2765 N_SO_index = sym_idx;
2766 }
2767 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002768 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2769 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002770 // This is usually the second N_SO entry that contains just the filename,
2771 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002772 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2773 if (so_path && so_path[0])
2774 {
2775 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002776 const size_t double_slash_pos = full_so_path.find("//");
2777 if (double_slash_pos != std::string::npos)
2778 {
2779 // The linker has been generating bad N_SO entries with doubled up paths
2780 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2781 // and the second is the directory for the source file so you end up with
2782 // a path that looks like "/tmp/src//tmp/src/"
2783 FileSpec so_dir(so_path, false);
2784 if (!so_dir.Exists())
2785 {
2786 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2787 if (so_dir.Exists())
2788 {
2789 // Trim off the incorrect path
2790 full_so_path.erase(0, double_slash_pos + 1);
2791 }
2792 }
2793 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002794 if (*full_so_path.rbegin() != '/')
2795 full_so_path += '/';
2796 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002797 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002798 add_nlist = false;
2799 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2800 }
2801 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002802 else
2803 {
2804 // This could be a relative path to a N_SO
2805 N_SO_index = sym_idx;
2806 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002807 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002808
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002809 break;
2810
2811 case StabObjectFileName:
2812 // N_OSO - object file name: name,,0,0,st_mtime
2813 type = eSymbolTypeObjectFile;
2814 break;
2815
2816 case StabLocalSymbol:
2817 // N_LSYM - local sym: name,,NO_SECT,type,offset
2818 type = eSymbolTypeLocal;
2819 break;
2820
2821 //----------------------------------------------------------------------
2822 // INCL scopes
2823 //----------------------------------------------------------------------
2824 case StabBeginIncludeFileName:
2825 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2826 // We use the current number of symbols in the symbol table in lieu of
2827 // using nlist_idx in case we ever start trimming entries out
2828 N_INCL_indexes.push_back(sym_idx);
2829 type = eSymbolTypeScopeBegin;
2830 break;
2831
2832 case StabEndIncludeFile:
2833 // N_EINCL - include file end: name,,NO_SECT,0,0
2834 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2835 // so that we can always skip the entire symbol if we need to navigate
2836 // more quickly at the source level when parsing STABS
2837 if ( !N_INCL_indexes.empty() )
2838 {
2839 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2840 symbol_ptr->SetByteSize(sym_idx + 1);
2841 symbol_ptr->SetSizeIsSibling(true);
2842 N_INCL_indexes.pop_back();
2843 }
2844 type = eSymbolTypeScopeEnd;
2845 break;
2846
2847 case StabIncludeFileName:
2848 // N_SOL - #included file name: name,,n_sect,0,address
2849 type = eSymbolTypeHeaderFile;
2850
2851 // We currently don't use the header files on darwin
2852 if (minimize)
2853 add_nlist = false;
2854 break;
2855
Jason Molenda9badb6c2013-03-06 23:19:17 +00002856 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002857 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2858 type = eSymbolTypeCompiler;
2859 break;
2860
2861 case StabCompilerVersion:
2862 // N_VERSION - compiler version: name,,NO_SECT,0,0
2863 type = eSymbolTypeCompiler;
2864 break;
2865
2866 case StabCompilerOptLevel:
2867 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2868 type = eSymbolTypeCompiler;
2869 break;
2870
2871 case StabParameter:
2872 // N_PSYM - parameter: name,,NO_SECT,type,offset
2873 type = eSymbolTypeVariable;
2874 break;
2875
2876 case StabAlternateEntry:
2877 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2878 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2879 type = eSymbolTypeLineEntry;
2880 break;
2881
2882 //----------------------------------------------------------------------
2883 // Left and Right Braces
2884 //----------------------------------------------------------------------
2885 case StabLeftBracket:
2886 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2887 // We use the current number of symbols in the symbol table in lieu of
2888 // using nlist_idx in case we ever start trimming entries out
2889 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2890 N_BRAC_indexes.push_back(sym_idx);
2891 type = eSymbolTypeScopeBegin;
2892 break;
2893
2894 case StabRightBracket:
2895 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2896 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2897 // so that we can always skip the entire symbol if we need to navigate
2898 // more quickly at the source level when parsing STABS
2899 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2900 if ( !N_BRAC_indexes.empty() )
2901 {
2902 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2903 symbol_ptr->SetByteSize(sym_idx + 1);
2904 symbol_ptr->SetSizeIsSibling(true);
2905 N_BRAC_indexes.pop_back();
2906 }
2907 type = eSymbolTypeScopeEnd;
2908 break;
2909
2910 case StabDeletedIncludeFile:
2911 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2912 type = eSymbolTypeHeaderFile;
2913 break;
2914
2915 //----------------------------------------------------------------------
2916 // COMM scopes
2917 //----------------------------------------------------------------------
2918 case StabBeginCommon:
2919 // N_BCOMM - begin common: name,,NO_SECT,0,0
2920 // We use the current number of symbols in the symbol table in lieu of
2921 // using nlist_idx in case we ever start trimming entries out
2922 type = eSymbolTypeScopeBegin;
2923 N_COMM_indexes.push_back(sym_idx);
2924 break;
2925
2926 case StabEndCommonLocal:
2927 // N_ECOML - end common (local name): 0,,n_sect,0,address
2928 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2929 // Fall through
2930
2931 case StabEndCommon:
2932 // N_ECOMM - end common: name,,n_sect,0,0
2933 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2934 // so that we can always skip the entire symbol if we need to navigate
2935 // more quickly at the source level when parsing STABS
2936 if ( !N_COMM_indexes.empty() )
2937 {
2938 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2939 symbol_ptr->SetByteSize(sym_idx + 1);
2940 symbol_ptr->SetSizeIsSibling(true);
2941 N_COMM_indexes.pop_back();
2942 }
2943 type = eSymbolTypeScopeEnd;
2944 break;
2945
2946 case StabLength:
2947 // N_LENG - second stab entry with length information
2948 type = eSymbolTypeAdditional;
2949 break;
2950
2951 default: break;
2952 }
2953 }
2954 else
2955 {
2956 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2957 uint8_t n_type = NlistMaskType & nlist.n_type;
2958 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2959
2960 switch (n_type)
2961 {
2962 case NListTypeIndirect: // N_INDR - Fall through
2963 case NListTypePreboundUndefined:// N_PBUD - Fall through
2964 case NListTypeUndefined: // N_UNDF
2965 type = eSymbolTypeUndefined;
2966 break;
2967
2968 case NListTypeAbsolute: // N_ABS
2969 type = eSymbolTypeAbsolute;
2970 break;
2971
2972 case NListTypeSection: // N_SECT
2973 {
2974 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2975
Sean Callananb386d822012-08-09 00:50:26 +00002976 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002977 {
2978 // TODO: warn about this?
2979 add_nlist = false;
2980 break;
2981 }
2982
2983 if (TEXT_eh_frame_sectID == nlist.n_sect)
2984 {
2985 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00002986 }
2987 else
2988 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002989 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2990
2991 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00002992 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002993 case SectionTypeRegular: break; // regular section
2994 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2995 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2996 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2997 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2998 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2999 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3000 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3001 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3002 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3003 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3004 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3005 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3006 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3007 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3008 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3009 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3010 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00003011 }
Chris Lattner24943d22010-06-08 16:52:24 +00003012
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003013 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00003014 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003015 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3016 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003017 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003018 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3019 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003020 SectionAttrSytemSomeInstructions))
3021 type = eSymbolTypeData;
3022 else
3023 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00003024 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003025 else
3026 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003027 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003028 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00003029 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003030 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00003031
Jason Molenda9badb6c2013-03-06 23:19:17 +00003032 if (symbol_name &&
3033 symbol_name[0] == '_' &&
3034 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003035 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00003036 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003037 llvm::StringRef symbol_name_ref(symbol_name);
3038 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3039 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3040 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3041 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00003042 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003043 symbol_name_non_abi_mangled = symbol_name + 1;
3044 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3045 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003046 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003047 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003048 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003049 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003050 symbol_name_non_abi_mangled = symbol_name + 1;
3051 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3052 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003053 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003054 }
3055 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3056 {
3057 symbol_name_non_abi_mangled = symbol_name + 1;
3058 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3059 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003060 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003061 }
3062 }
3063 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003064 else
3065 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3066 {
3067 type = eSymbolTypeException;
3068 }
3069 else
3070 {
3071 type = eSymbolTypeData;
3072 }
3073 }
3074 else
3075 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3076 {
3077 type = eSymbolTypeTrampoline;
3078 }
3079 else
3080 if (symbol_section->IsDescendant(objc_section_sp.get()))
3081 {
3082 type = eSymbolTypeRuntime;
3083 if (symbol_name && symbol_name[0] == '.')
3084 {
3085 llvm::StringRef symbol_name_ref(symbol_name);
3086 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3087 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3088 {
3089 symbol_name_non_abi_mangled = symbol_name;
3090 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3091 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003092 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003093 }
3094 }
Chris Lattner24943d22010-06-08 16:52:24 +00003095 }
3096 }
3097 }
3098 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003099 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003100 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003101 }
3102
3103 if (add_nlist)
3104 {
3105 uint64_t symbol_value = nlist.n_value;
3106 bool symbol_name_is_mangled = false;
3107
3108 if (symbol_name_non_abi_mangled)
3109 {
Greg Claytonc0240042012-07-18 23:18:10 +00003110 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3111 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003112 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003113 else
3114 {
3115 if (symbol_name && symbol_name[0] == '_')
3116 {
3117 symbol_name_is_mangled = symbol_name[1] == '_';
3118 symbol_name++; // Skip the leading underscore
3119 }
3120
3121 if (symbol_name)
3122 {
Greg Claytonc0240042012-07-18 23:18:10 +00003123 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003124 }
3125 }
3126
3127 if (is_debug == false)
3128 {
3129 if (type == eSymbolTypeCode)
3130 {
3131 // See if we can find a N_FUN entry for any code symbols.
3132 // If we do find a match, and the name matches, then we
3133 // can merge the two into just the function symbol to avoid
3134 // duplicate entries in the symbol table
3135 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3136 if (pos != N_FUN_addr_to_sym_idx.end())
3137 {
3138 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3139 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3140 {
3141 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3142 // We just need the flags from the linker symbol, so put these flags
3143 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3144 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3145 sym[sym_idx].Clear();
3146 continue;
3147 }
3148 }
3149 }
3150 else if (type == eSymbolTypeData)
3151 {
3152 // See if we can find a N_STSYM entry for any data symbols.
3153 // If we do find a match, and the name matches, then we
3154 // can merge the two into just the Static symbol to avoid
3155 // duplicate entries in the symbol table
3156 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3157 if (pos != N_STSYM_addr_to_sym_idx.end())
3158 {
3159 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3160 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3161 {
3162 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3163 // We just need the flags from the linker symbol, so put these flags
3164 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3165 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3166 sym[sym_idx].Clear();
3167 continue;
3168 }
3169 }
3170 }
3171 }
3172 if (symbol_section)
3173 {
3174 const addr_t section_file_addr = symbol_section->GetFileAddress();
3175 if (symbol_byte_size == 0 && function_starts_count > 0)
3176 {
Greg Claytond2653c22012-03-14 01:53:24 +00003177 addr_t symbol_lookup_file_addr = nlist.n_value;
3178 // Do an exact address match for non-ARM addresses, else get the closest since
3179 // the symbol might be a thumb symbol which has an address with bit zero set
3180 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3181 if (is_arm && func_start_entry)
3182 {
3183 // Verify that the function start address is the symbol address (ARM)
3184 // or the symbol address + 1 (thumb)
3185 if (func_start_entry->addr != symbol_lookup_file_addr &&
3186 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3187 {
3188 // Not the right entry, NULL it out...
3189 func_start_entry = NULL;
3190 }
3191 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003192 if (func_start_entry)
3193 {
3194 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003195
Greg Claytond2653c22012-03-14 01:53:24 +00003196 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003197 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003198 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003199
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003200 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3201 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3202 if (next_func_start_entry)
3203 {
Greg Claytond2653c22012-03-14 01:53:24 +00003204 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3205 // Be sure the clear the Thumb address bit when we calculate the size
3206 // from the current and next address
3207 if (is_arm)
3208 next_symbol_file_addr &= 0xfffffffffffffffeull;
3209 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 +00003210 }
3211 else
3212 {
Greg Claytond2653c22012-03-14 01:53:24 +00003213 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003214 }
3215 }
3216 }
3217 symbol_value -= section_file_addr;
3218 }
3219
3220 sym[sym_idx].SetID (nlist_idx);
3221 sym[sym_idx].SetType (type);
3222 sym[sym_idx].GetAddress().SetSection (symbol_section);
3223 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3224 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3225
3226 if (symbol_byte_size > 0)
3227 sym[sym_idx].SetByteSize(symbol_byte_size);
3228
Greg Clayton01e6a582012-11-27 01:52:16 +00003229 if (demangled_is_synthesized)
3230 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3231
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003232 ++sym_idx;
3233 }
3234 else
3235 {
3236 sym[sym_idx].Clear();
3237 }
3238
3239 }
3240
3241 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3242 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3243 // such entries by figuring out what the address for the global is by looking up this non-STAB
3244 // entry and copying the value into the debug symbol's value to save us the hassle in the
3245 // debug symbol parser.
3246
3247 Symbol *global_symbol = NULL;
3248 for (nlist_idx = 0;
3249 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3250 nlist_idx++)
3251 {
3252 if (global_symbol->GetAddress().GetFileAddress() == 0)
3253 {
3254 std::vector<uint32_t> indexes;
3255 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3256 {
3257 std::vector<uint32_t>::const_iterator pos;
3258 std::vector<uint32_t>::const_iterator end = indexes.end();
3259 for (pos = indexes.begin(); pos != end; ++pos)
3260 {
3261 symbol_ptr = symtab->SymbolAtIndex(*pos);
3262 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3263 {
3264 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3265 break;
3266 }
3267 }
3268 }
Chris Lattner24943d22010-06-08 16:52:24 +00003269 }
3270 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003271
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003272 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3273
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003274 if (function_starts_count > 0)
3275 {
3276 char synthetic_function_symbol[PATH_MAX];
3277 uint32_t num_synthetic_function_symbols = 0;
3278 for (i=0; i<function_starts_count; ++i)
3279 {
3280 if (function_starts.GetEntryRef (i).data == false)
3281 ++num_synthetic_function_symbols;
3282 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003283
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003284 if (num_synthetic_function_symbols > 0)
3285 {
3286 if (num_syms < sym_idx + num_synthetic_function_symbols)
3287 {
3288 num_syms = sym_idx + num_synthetic_function_symbols;
3289 sym = symtab->Resize (num_syms);
3290 }
3291 uint32_t synthetic_function_symbol_idx = 0;
3292 for (i=0; i<function_starts_count; ++i)
3293 {
3294 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3295 if (func_start_entry->data == false)
3296 {
Greg Claytond2653c22012-03-14 01:53:24 +00003297 addr_t symbol_file_addr = func_start_entry->addr;
3298 uint32_t symbol_flags = 0;
3299 if (is_arm)
3300 {
3301 if (symbol_file_addr & 1)
3302 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3303 symbol_file_addr &= 0xfffffffffffffffeull;
3304 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003305 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003306 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003307 {
3308 SectionSP symbol_section (symbol_addr.GetSection());
3309 uint32_t symbol_byte_size = 0;
3310 if (symbol_section)
3311 {
3312 const addr_t section_file_addr = symbol_section->GetFileAddress();
3313 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3314 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3315 if (next_func_start_entry)
3316 {
Greg Claytond2653c22012-03-14 01:53:24 +00003317 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3318 if (is_arm)
3319 next_symbol_file_addr &= 0xfffffffffffffffeull;
3320 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 +00003321 }
3322 else
3323 {
Greg Claytond2653c22012-03-14 01:53:24 +00003324 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003325 }
3326 snprintf (synthetic_function_symbol,
3327 sizeof(synthetic_function_symbol),
3328 "___lldb_unnamed_function%u$$%s",
3329 ++synthetic_function_symbol_idx,
3330 module_sp->GetFileSpec().GetFilename().GetCString());
3331 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003332 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003333 sym[sym_idx].SetType (eSymbolTypeCode);
3334 sym[sym_idx].SetIsSynthetic (true);
3335 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003336 if (symbol_flags)
3337 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003338 if (symbol_byte_size)
3339 sym[sym_idx].SetByteSize (symbol_byte_size);
3340 ++sym_idx;
3341 }
3342 }
3343 }
3344 }
3345 }
3346 }
3347
3348 // Trim our symbols down to just what we ended up with after
3349 // removing any symbols.
3350 if (sym_idx < num_syms)
3351 {
3352 num_syms = sym_idx;
3353 sym = symtab->Resize (num_syms);
3354 }
3355
3356 // Now synthesize indirect symbols
3357 if (m_dysymtab.nindirectsyms != 0)
3358 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003359 if (indirect_symbol_index_data.GetByteSize())
3360 {
3361 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3362
3363 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3364 {
3365 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3366 {
3367 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3368 if (symbol_stub_byte_size == 0)
3369 continue;
3370
3371 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3372
3373 if (num_symbol_stubs == 0)
3374 continue;
3375
3376 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3377 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3378 {
3379 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3380 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 +00003381 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003382 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3383 {
3384 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3385 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3386 continue;
3387
3388 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3389 Symbol *stub_symbol = NULL;
3390 if (index_pos != end_index_pos)
3391 {
3392 // We have a remapping from the original nlist index to
3393 // a current symbol index, so just look this up by index
3394 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3395 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003396 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003397 {
3398 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003399 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003400 // S_SYMBOL_STUBS
3401 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3402 }
3403
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003404 if (stub_symbol)
3405 {
3406 Address so_addr(symbol_stub_addr, section_list);
3407
3408 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3409 {
3410 // Change the external symbol into a trampoline that makes sense
3411 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3412 // can re-use them so we don't have to make up a synthetic symbol
3413 // for no good reason.
3414 stub_symbol->SetType (eSymbolTypeTrampoline);
3415 stub_symbol->SetExternal (false);
3416 stub_symbol->GetAddress() = so_addr;
3417 stub_symbol->SetByteSize (symbol_stub_byte_size);
3418 }
3419 else
3420 {
3421 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003422 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003423 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003424 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003425 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003426 stub_symbol = NULL; // this pointer no longer valid
3427 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003428 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003429 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003430 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3431 sym[sym_idx].SetIsSynthetic (true);
3432 sym[sym_idx].GetAddress() = so_addr;
3433 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3434 ++sym_idx;
3435 }
3436 }
Greg Claytond4330e62012-09-05 01:38:55 +00003437 else
3438 {
3439 if (log)
3440 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3441 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003442 }
3443 }
3444 }
3445 }
3446 }
3447 }
3448 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003449 }
3450 return 0;
3451}
3452
3453
3454void
3455ObjectFileMachO::Dump (Stream *s)
3456{
Greg Clayton9482f052012-03-13 23:14:29 +00003457 ModuleSP module_sp(GetModule());
3458 if (module_sp)
3459 {
3460 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3461 s->Printf("%p: ", this);
3462 s->Indent();
3463 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3464 s->PutCString("ObjectFileMachO64");
3465 else
3466 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003467
Greg Clayton9482f052012-03-13 23:14:29 +00003468 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003469
Greg Clayton9482f052012-03-13 23:14:29 +00003470 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003471
Greg Clayton9482f052012-03-13 23:14:29 +00003472 if (m_sections_ap.get())
3473 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003474
Greg Clayton9482f052012-03-13 23:14:29 +00003475 if (m_symtab_ap.get())
3476 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3477 }
Chris Lattner24943d22010-06-08 16:52:24 +00003478}
3479
3480
3481bool
Greg Clayton0467c782011-02-04 18:53:10 +00003482ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003483{
Greg Clayton9482f052012-03-13 23:14:29 +00003484 ModuleSP module_sp(GetModule());
3485 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003486 {
Greg Clayton9482f052012-03-13 23:14:29 +00003487 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3488 struct uuid_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003489 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003490 uint32_t i;
3491 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003492 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003493 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003494 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3495 break;
3496
3497 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00003498 {
Greg Clayton9482f052012-03-13 23:14:29 +00003499 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003500
Greg Clayton9482f052012-03-13 23:14:29 +00003501 if (uuid_bytes)
3502 {
Sean Callanana5689d52012-07-12 18:04:03 +00003503 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3504 // We pretend these object files have no UUID to prevent crashing.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003505
Sean Callanana5689d52012-07-12 18:04:03 +00003506 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3507 0x3b, 0xa8,
3508 0x4b, 0x16,
3509 0xb6, 0xa4,
3510 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
Jason Molenda9badb6c2013-03-06 23:19:17 +00003511
Sean Callanana5689d52012-07-12 18:04:03 +00003512 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3513 return false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003514
Greg Clayton9482f052012-03-13 23:14:29 +00003515 uuid->SetBytes (uuid_bytes);
3516 return true;
3517 }
3518 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003519 }
Greg Clayton9482f052012-03-13 23:14:29 +00003520 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003521 }
Chris Lattner24943d22010-06-08 16:52:24 +00003522 }
3523 return false;
3524}
3525
3526
3527uint32_t
3528ObjectFileMachO::GetDependentModules (FileSpecList& files)
3529{
Chris Lattner24943d22010-06-08 16:52:24 +00003530 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003531 ModuleSP module_sp(GetModule());
3532 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003533 {
Greg Clayton9482f052012-03-13 23:14:29 +00003534 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3535 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003536 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003537 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3538 uint32_t i;
3539 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003540 {
Greg Clayton9482f052012-03-13 23:14:29 +00003541 const uint32_t cmd_offset = offset;
3542 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3543 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003544
Greg Clayton9482f052012-03-13 23:14:29 +00003545 switch (load_cmd.cmd)
3546 {
3547 case LoadCommandDylibLoad:
3548 case LoadCommandDylibLoadWeak:
3549 case LoadCommandDylibReexport:
3550 case LoadCommandDynamicLinkerLoad:
3551 case LoadCommandFixedVMShlibLoad:
3552 case LoadCommandDylibLoadUpward:
3553 {
3554 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3555 const char *path = m_data.PeekCStr(name_offset);
3556 // Skip any path that starts with '@' since these are usually:
3557 // @executable_path/.../file
3558 // @rpath/.../file
3559 if (path && path[0] != '@')
3560 {
3561 FileSpec file_spec(path, resolve_path);
3562 if (files.AppendIfUnique(file_spec))
3563 count++;
3564 }
3565 }
3566 break;
3567
3568 default:
3569 break;
3570 }
3571 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003572 }
Chris Lattner24943d22010-06-08 16:52:24 +00003573 }
3574 return count;
3575}
3576
Jim Ingham28775942011-03-07 23:44:08 +00003577lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003578ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003579{
3580 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3581 // is initialized to an invalid address, so we can just return that.
3582 // 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 +00003583
Jim Ingham28775942011-03-07 23:44:08 +00003584 if (!IsExecutable() || m_entry_point_address.IsValid())
3585 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003586
3587 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003588 // /usr/include/mach-o.h, but it is basically:
3589 //
3590 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3591 // uint32_t count - this is the count of longs in the thread state data
3592 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3593 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003594 //
Jim Ingham28775942011-03-07 23:44:08 +00003595 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3596 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3597 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3598 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3599 //
3600 // For now we hard-code the offsets and flavors we need:
3601 //
3602 //
3603
Greg Clayton9482f052012-03-13 23:14:29 +00003604 ModuleSP module_sp(GetModule());
3605 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003606 {
Greg Clayton9482f052012-03-13 23:14:29 +00003607 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3608 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003609 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003610 uint32_t i;
3611 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3612 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003613
Greg Clayton9482f052012-03-13 23:14:29 +00003614 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003615 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003616 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003617 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3618 break;
3619
3620 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003621 {
Greg Clayton9482f052012-03-13 23:14:29 +00003622 case LoadCommandUnixThread:
3623 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003624 {
Greg Clayton9482f052012-03-13 23:14:29 +00003625 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003626 {
Greg Clayton9482f052012-03-13 23:14:29 +00003627 uint32_t flavor = m_data.GetU32(&offset);
3628 uint32_t count = m_data.GetU32(&offset);
3629 if (count == 0)
3630 {
3631 // We've gotten off somehow, log and exit;
3632 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003633 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003634
Greg Clayton9482f052012-03-13 23:14:29 +00003635 switch (m_header.cputype)
3636 {
3637 case llvm::MachO::CPUTypeARM:
3638 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3639 {
3640 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3641 start_address = m_data.GetU32(&offset);
3642 done = true;
3643 }
Jim Ingham28775942011-03-07 23:44:08 +00003644 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003645 case llvm::MachO::CPUTypeI386:
3646 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3647 {
3648 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3649 start_address = m_data.GetU32(&offset);
3650 done = true;
3651 }
3652 break;
3653 case llvm::MachO::CPUTypeX86_64:
3654 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3655 {
3656 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3657 start_address = m_data.GetU64(&offset);
3658 done = true;
3659 }
3660 break;
3661 default:
3662 return m_entry_point_address;
3663 }
3664 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3665 if (done)
3666 break;
3667 offset += count * 4;
3668 }
Jim Ingham28775942011-03-07 23:44:08 +00003669 }
Greg Clayton9482f052012-03-13 23:14:29 +00003670 break;
3671 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003672 {
Greg Clayton9482f052012-03-13 23:14:29 +00003673 ConstString text_segment_name ("__TEXT");
3674 uint64_t entryoffset = m_data.GetU64(&offset);
3675 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3676 if (text_segment_sp)
3677 {
3678 done = true;
3679 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3680 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003681 }
Greg Clayton9482f052012-03-13 23:14:29 +00003682
3683 default:
3684 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003685 }
Greg Clayton9482f052012-03-13 23:14:29 +00003686 if (done)
3687 break;
Jim Ingham28775942011-03-07 23:44:08 +00003688
Greg Clayton9482f052012-03-13 23:14:29 +00003689 // Go to the next load command:
3690 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003691 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003692
Greg Clayton9482f052012-03-13 23:14:29 +00003693 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003694 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003695 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003696 // of this ObjectFile:
3697 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003698 {
Greg Clayton9482f052012-03-13 23:14:29 +00003699 m_entry_point_address.Clear();
3700 }
3701 }
3702 else
3703 {
3704 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3705 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003706
Greg Clayton9482f052012-03-13 23:14:29 +00003707 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003708
Greg Clayton9482f052012-03-13 23:14:29 +00003709 if (module_sp)
3710 {
3711 SymbolContextList contexts;
3712 SymbolContext context;
3713 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3714 {
3715 if (contexts.GetContextAtIndex(0, context))
3716 m_entry_point_address = context.symbol->GetAddress();
3717 }
Greg Clayton3508c382012-02-24 01:59:29 +00003718 }
3719 }
Jim Ingham28775942011-03-07 23:44:08 +00003720 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003721
Jim Ingham28775942011-03-07 23:44:08 +00003722 return m_entry_point_address;
3723
3724}
3725
Greg Claytonb5a8f142012-02-05 02:38:54 +00003726lldb_private::Address
3727ObjectFileMachO::GetHeaderAddress ()
3728{
3729 lldb_private::Address header_addr;
3730 SectionList *section_list = GetSectionList();
3731 if (section_list)
3732 {
3733 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3734 if (text_segment_sp)
3735 {
Greg Clayton3508c382012-02-24 01:59:29 +00003736 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003737 header_addr.SetOffset (0);
3738 }
3739 }
3740 return header_addr;
3741}
3742
Greg Clayton46c9a352012-02-09 06:16:32 +00003743uint32_t
3744ObjectFileMachO::GetNumThreadContexts ()
3745{
Greg Clayton9482f052012-03-13 23:14:29 +00003746 ModuleSP module_sp(GetModule());
3747 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003748 {
Greg Clayton9482f052012-03-13 23:14:29 +00003749 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3750 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003751 {
Greg Clayton9482f052012-03-13 23:14:29 +00003752 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003753 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003754 FileRangeArray::Entry file_range;
3755 thread_command thread_cmd;
3756 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003757 {
Greg Clayton9482f052012-03-13 23:14:29 +00003758 const uint32_t cmd_offset = offset;
3759 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3760 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003761
Greg Clayton9482f052012-03-13 23:14:29 +00003762 if (thread_cmd.cmd == LoadCommandThread)
3763 {
3764 file_range.SetRangeBase (offset);
3765 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3766 m_thread_context_offsets.Append (file_range);
3767 }
3768 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003769 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003770 }
3771 }
3772 return m_thread_context_offsets.GetSize();
3773}
3774
3775lldb::RegisterContextSP
3776ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3777{
Greg Clayton46c9a352012-02-09 06:16:32 +00003778 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003779
Greg Clayton9482f052012-03-13 23:14:29 +00003780 ModuleSP module_sp(GetModule());
3781 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003782 {
Greg Clayton9482f052012-03-13 23:14:29 +00003783 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3784 if (!m_thread_context_offsets_valid)
3785 GetNumThreadContexts ();
3786
3787 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003788 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003789 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003790
3791 DataExtractor data (m_data,
3792 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003793 thread_context_file_range->GetByteSize());
3794
3795 switch (m_header.cputype)
3796 {
3797 case llvm::MachO::CPUTypeARM:
3798 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3799 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003800
Jim Ingham6f01c932012-10-12 17:34:26 +00003801 case llvm::MachO::CPUTypeI386:
3802 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3803 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003804
Jim Ingham6f01c932012-10-12 17:34:26 +00003805 case llvm::MachO::CPUTypeX86_64:
3806 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3807 break;
3808 }
Greg Clayton9482f052012-03-13 23:14:29 +00003809 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003810 }
3811 return reg_ctx_sp;
3812}
3813
Greg Claytonb5a8f142012-02-05 02:38:54 +00003814
Greg Claytonca319972011-07-09 00:41:34 +00003815ObjectFile::Type
3816ObjectFileMachO::CalculateType()
3817{
3818 switch (m_header.filetype)
3819 {
3820 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3821 if (GetAddressByteSize () == 4)
3822 {
3823 // 32 bit kexts are just object files, but they do have a valid
3824 // UUID load command.
3825 UUID uuid;
3826 if (GetUUID(&uuid))
3827 {
3828 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003829 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003830 // "OSKextGetCurrentIdentifier" as this is required of kexts
3831 if (m_strata == eStrataInvalid)
3832 m_strata = eStrataKernel;
3833 return eTypeSharedLibrary;
3834 }
3835 }
3836 return eTypeObjectFile;
3837
3838 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3839 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3840 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3841 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3842 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3843 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3844 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3845 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3846 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3847 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3848 default:
3849 break;
3850 }
3851 return eTypeUnknown;
3852}
3853
3854ObjectFile::Strata
3855ObjectFileMachO::CalculateStrata()
3856{
3857 switch (m_header.filetype)
3858 {
3859 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3860 {
3861 // 32 bit kexts are just object files, but they do have a valid
3862 // UUID load command.
3863 UUID uuid;
3864 if (GetUUID(&uuid))
3865 {
3866 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003867 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003868 // "OSKextGetCurrentIdentifier" as this is required of kexts
3869 if (m_type == eTypeInvalid)
3870 m_type = eTypeSharedLibrary;
3871
3872 return eStrataKernel;
3873 }
3874 }
3875 return eStrataUnknown;
3876
3877 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3878 // Check for the MH_DYLDLINK bit in the flags
3879 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003880 {
Greg Claytonca319972011-07-09 00:41:34 +00003881 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003882 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003883 else
Sean Callananac725af2012-02-10 20:22:35 +00003884 {
3885 SectionList *section_list = GetSectionList();
3886 if (section_list)
3887 {
3888 static ConstString g_kld_section_name ("__KLD");
3889 if (section_list->FindSectionByName(g_kld_section_name))
3890 return eStrataKernel;
3891 }
3892 }
3893 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00003894
3895 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
3896 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00003897 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00003898 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
3899 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
3900 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
3901 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
3902 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
3903 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
3904 default:
3905 break;
3906 }
3907 return eStrataUnknown;
3908}
3909
3910
Greg Clayton49f4bf22012-02-22 19:41:02 +00003911uint32_t
3912ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
3913{
Greg Clayton9482f052012-03-13 23:14:29 +00003914 ModuleSP module_sp(GetModule());
3915 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003916 {
Greg Clayton9482f052012-03-13 23:14:29 +00003917 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3918 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003919 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003920 uint32_t version_cmd = 0;
3921 uint64_t version = 0;
3922 uint32_t i;
3923 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003924 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003925 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003926 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3927 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003928
Greg Clayton9482f052012-03-13 23:14:29 +00003929 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003930 {
Greg Clayton9482f052012-03-13 23:14:29 +00003931 if (version_cmd == 0)
3932 {
3933 version_cmd = load_cmd.cmd;
3934 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
3935 break;
3936 version = load_cmd.dylib.current_version;
3937 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003938 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00003939 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00003940 }
Greg Clayton9482f052012-03-13 23:14:29 +00003941 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003942 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003943
Greg Clayton9482f052012-03-13 23:14:29 +00003944 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003945 {
Greg Clayton9482f052012-03-13 23:14:29 +00003946 if (versions != NULL && num_versions > 0)
3947 {
3948 if (num_versions > 0)
3949 versions[0] = (version & 0xFFFF0000ull) >> 16;
3950 if (num_versions > 1)
3951 versions[1] = (version & 0x0000FF00ull) >> 8;
3952 if (num_versions > 2)
3953 versions[2] = (version & 0x000000FFull);
3954 // Fill in an remaining version numbers with invalid values
3955 for (i=3; i<num_versions; ++i)
3956 versions[i] = UINT32_MAX;
3957 }
3958 // The LC_ID_DYLIB load command has a version with 3 version numbers
3959 // in it, so always return 3
3960 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003961 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00003962 }
3963 return false;
3964}
3965
Chris Lattner24943d22010-06-08 16:52:24 +00003966bool
Greg Clayton395fc332011-02-15 21:59:32 +00003967ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00003968{
Greg Clayton9482f052012-03-13 23:14:29 +00003969 ModuleSP module_sp(GetModule());
3970 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003971 {
Greg Clayton9482f052012-03-13 23:14:29 +00003972 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3973 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003974
Greg Clayton9482f052012-03-13 23:14:29 +00003975 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00003976 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00003977 // unknown to make sure we use the DynamicLoaderStatic()...
3978 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
3979 {
3980 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
3981 }
3982 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003983 }
Greg Clayton9482f052012-03-13 23:14:29 +00003984 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003985}
3986
3987
Jason Molenda45c75502013-04-16 06:24:42 +00003988UUID
3989ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
3990{
3991 UUID uuid;
3992 if (process)
3993 {
3994 addr_t all_image_infos = process->GetImageInfoAddress();
3995
3996 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
3997 // or it may be the address of the dyld_all_image_infos structure (want). The first four
3998 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
3999 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4000
4001 Error err;
4002 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4003 if (version_or_magic != -1
4004 && version_or_magic != HeaderMagic32
4005 && version_or_magic != HeaderMagic32Swapped
4006 && version_or_magic != HeaderMagic64
4007 && version_or_magic != HeaderMagic64Swapped
4008 && version_or_magic >= 13)
4009 {
4010 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4011 int wordsize = process->GetAddressByteSize();
4012 if (wordsize == 8)
4013 {
4014 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4015 }
4016 if (wordsize == 4)
4017 {
4018 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4019 }
4020 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4021 {
4022 uuid_t shared_cache_uuid;
4023 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4024 {
4025 uuid.SetBytes (shared_cache_uuid);
4026 }
4027 }
4028 }
4029 }
4030 return uuid;
4031}
4032
4033UUID
4034ObjectFileMachO::GetLLDBSharedCacheUUID ()
4035{
4036 UUID uuid;
4037#if defined (__APPLE__) && defined (__arm__)
4038 uint8_t *(*dyld_get_all_image_infos)(void);
4039 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4040 if (dyld_get_all_image_infos)
4041 {
4042 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4043 if (dyld_all_image_infos_address)
4044 {
Jason Molenda6ff916f2013-04-16 21:42:58 +00004045 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4046 if (*version >= 13)
Jason Molenda45c75502013-04-16 06:24:42 +00004047 {
Jason Molenda2ceae992013-04-16 22:56:17 +00004048 uuid_t *sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84); // sharedCacheUUID <mach-o/dyld_images.h>
Jason Molenda45c75502013-04-16 06:24:42 +00004049 uuid.SetBytes (sharedCacheUUID_address);
4050 }
4051 }
4052 }
4053#endif
4054 return uuid;
4055}
4056
4057
Chris Lattner24943d22010-06-08 16:52:24 +00004058//------------------------------------------------------------------
4059// PluginInterface protocol
4060//------------------------------------------------------------------
4061const char *
4062ObjectFileMachO::GetPluginName()
4063{
4064 return "ObjectFileMachO";
4065}
4066
4067const char *
4068ObjectFileMachO::GetShortPluginName()
4069{
4070 return GetPluginNameStatic();
4071}
4072
4073uint32_t
4074ObjectFileMachO::GetPluginVersion()
4075{
4076 return 1;
4077}
4078