blob: 42f21f7bb368eb56e9de00e085dcbb844931e67e [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 {
847 if (load_cmd.fileoff + load_cmd.filesize > 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 - and
854 // in that case we don't have a Process yet so nothing will be printed. Not really ideal;
855 // the ObjectFile needs some way of reporting an error message for methods like GetSectionList
856 // which fail.
857 ProcessSP process_sp (m_process_wp.lock());
858 if (process_sp)
859 {
860 Stream *s = &process_sp->GetTarget().GetDebugger().GetOutputStream();
861 if (s)
862 {
863 s->Printf ("Corrupt/invalid Mach-O object file -- a load command extends past the end of the file.\n");
864 }
865 }
866 m_sections_ap->Clear();
867 return 0;
868 }
869 }
Chris Lattner24943d22010-06-08 16:52:24 +0000870 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
871 {
Jason Molenda9badb6c2013-03-06 23:19:17 +0000872
Greg Clayton68ca8232011-01-25 02:58:48 +0000873 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
874
Chris Lattner24943d22010-06-08 16:52:24 +0000875 // Keep a list of mach segments around in case we need to
876 // get at data that isn't stored in the abstracted Sections.
877 m_mach_segments.push_back (load_cmd);
878
Greg Clayton36da2aa2013-01-25 18:06:21 +0000879 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 +0000880 // Use a segment ID of the segment index shifted left by 8 so they
881 // never conflict with any of the sections.
882 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000883 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000884 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000885 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000886 ++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
887 segment_name, // Name of this section
888 eSectionTypeContainer, // This section is a container of other sections.
889 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
890 load_cmd.vmsize, // VM size in bytes of this section
891 load_cmd.fileoff, // Offset to the data for this section in the file
892 load_cmd.filesize, // Size in bytes of this section as found in the the file
893 load_cmd.flags)); // Flags for this section
894
Greg Clayton68ca8232011-01-25 02:58:48 +0000895 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000896 m_sections_ap->AddSection(segment_sp);
897 }
898
899 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000900 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000901 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +0000902 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +0000903 // mach sections yet...
904 if (m_mach_sections.empty())
905 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000906 uint32_t segment_sect_idx;
907 const lldb::user_id_t first_segment_sectID = sectID + 1;
908
909
Greg Clayton1674b122010-07-21 22:12:05 +0000910 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000911 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
912 {
913 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
914 break;
915 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
916 break;
917 sect64.addr = m_data.GetAddress(&offset);
918 sect64.size = m_data.GetAddress(&offset);
919
920 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
921 break;
922
923 // Keep a list of mach sections around in case we need to
924 // get at data that isn't stored in the abstracted Sections.
925 m_mach_sections.push_back (sect64);
926
927 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
928 if (!segment_name)
929 {
930 // We have a segment with no name so we need to conjure up
931 // segments that correspond to the section's segname if there
932 // isn't already such a section. If there is such a section,
933 // we resize the section so that it spans all sections.
934 // We also mark these sections as fake so address matches don't
935 // hit if they land in the gaps between the child sections.
936 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
937 segment_sp = m_sections_ap->FindSectionByName (segment_name);
938 if (segment_sp.get())
939 {
940 Section *segment = segment_sp.get();
941 // Grow the section size as needed.
942 const lldb::addr_t sect64_min_addr = sect64.addr;
943 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
944 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
945 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
946 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
947 if (sect64_min_addr >= curr_seg_min_addr)
948 {
949 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
950 // Only grow the section size if needed
951 if (new_seg_byte_size > curr_seg_byte_size)
952 segment->SetByteSize (new_seg_byte_size);
953 }
954 else
955 {
956 // We need to change the base address of the segment and
957 // adjust the child section offsets for all existing children.
958 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
959 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +0000960 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000961 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
962 }
Greg Clayton661825b2010-06-28 23:51:11 +0000963
964 // Grow the section size as needed.
965 if (sect64.offset)
966 {
967 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
968 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
969
970 const lldb::addr_t section_min_file_offset = sect64.offset;
971 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
972 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
973 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
974 segment->SetFileOffset (new_file_offset);
975 segment->SetFileSize (new_file_size);
976 }
Chris Lattner24943d22010-06-08 16:52:24 +0000977 }
978 else
979 {
980 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +0000981 segment_sp.reset(new Section (segment_sp, // Parent section
982 module_sp, // Module to which this section belongs
983 ++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
984 segment_name, // Name of this section
985 eSectionTypeContainer, // This section is a container of other sections.
986 sect64.addr, // File VM address == addresses as they are found in the object file
987 sect64.size, // VM size in bytes of this section
988 sect64.offset, // Offset to the data for this section in the file
989 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
990 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +0000991 segment_sp->SetIsFake(true);
992 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000993 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000994 }
995 }
996 assert (segment_sp.get());
997
Greg Clayton1674b122010-07-21 22:12:05 +0000998 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000999 static ConstString g_sect_name_objc_data ("__objc_data");
1000 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1001 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1002 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1003 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1004 static ConstString g_sect_name_objc_const ("__objc_const");
1005 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1006 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001007
1008 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1009 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1010 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1011 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1012 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1013 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1014 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1015 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1016 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1017 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1018 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +00001019 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1020 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +00001021 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +00001022 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001023 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +00001024 static ConstString g_sect_name_DATA ("__DATA");
1025 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001026
Chris Lattner24943d22010-06-08 16:52:24 +00001027 SectionType sect_type = eSectionTypeOther;
1028
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001029 if (section_name == g_sect_name_dwarf_debug_abbrev)
1030 sect_type = eSectionTypeDWARFDebugAbbrev;
1031 else if (section_name == g_sect_name_dwarf_debug_aranges)
1032 sect_type = eSectionTypeDWARFDebugAranges;
1033 else if (section_name == g_sect_name_dwarf_debug_frame)
1034 sect_type = eSectionTypeDWARFDebugFrame;
1035 else if (section_name == g_sect_name_dwarf_debug_info)
1036 sect_type = eSectionTypeDWARFDebugInfo;
1037 else if (section_name == g_sect_name_dwarf_debug_line)
1038 sect_type = eSectionTypeDWARFDebugLine;
1039 else if (section_name == g_sect_name_dwarf_debug_loc)
1040 sect_type = eSectionTypeDWARFDebugLoc;
1041 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1042 sect_type = eSectionTypeDWARFDebugMacInfo;
1043 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1044 sect_type = eSectionTypeDWARFDebugPubNames;
1045 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1046 sect_type = eSectionTypeDWARFDebugPubTypes;
1047 else if (section_name == g_sect_name_dwarf_debug_ranges)
1048 sect_type = eSectionTypeDWARFDebugRanges;
1049 else if (section_name == g_sect_name_dwarf_debug_str)
1050 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001051 else if (section_name == g_sect_name_dwarf_apple_names)
1052 sect_type = eSectionTypeDWARFAppleNames;
1053 else if (section_name == g_sect_name_dwarf_apple_types)
1054 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001055 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1056 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001057 else if (section_name == g_sect_name_dwarf_apple_objc)
1058 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001059 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001060 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001061 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001062 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001063 else if (section_name == g_sect_name_eh_frame)
1064 sect_type = eSectionTypeEHFrame;
1065 else if (section_name == g_sect_name_cfstring)
1066 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001067 else if (section_name == g_sect_name_objc_data ||
1068 section_name == g_sect_name_objc_classrefs ||
1069 section_name == g_sect_name_objc_superrefs ||
1070 section_name == g_sect_name_objc_const ||
1071 section_name == g_sect_name_objc_classlist)
1072 {
1073 sect_type = eSectionTypeDataPointers;
1074 }
Chris Lattner24943d22010-06-08 16:52:24 +00001075
1076 if (sect_type == eSectionTypeOther)
1077 {
1078 switch (mach_sect_type)
1079 {
1080 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001081 case SectionTypeRegular:
1082 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001083 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001084 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001085 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001086 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001087 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001088 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001089 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1090 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1091 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1092 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1093 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1094 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1095 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1096 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1097 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1098 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1099 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1100 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1101 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1102 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1103 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1104 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001105 default: break;
1106 }
1107 }
1108
Greg Clayton3508c382012-02-24 01:59:29 +00001109 SectionSP section_sp(new Section (segment_sp,
1110 module_sp,
1111 ++sectID,
1112 section_name,
1113 sect_type,
1114 sect64.addr - segment_sp->GetFileAddress(),
1115 sect64.size,
1116 sect64.offset,
1117 sect64.offset == 0 ? 0 : sect64.size,
1118 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001119 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001120
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001121 bool section_is_encrypted = false;
1122 if (!segment_is_encrypted && load_cmd.filesize != 0)
1123 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001124
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001125 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001126 segment_sp->GetChildren().AddSection(section_sp);
1127
1128 if (segment_sp->IsFake())
1129 {
1130 segment_sp.reset();
1131 segment_name.Clear();
1132 }
1133 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001134 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001135 {
1136 if (first_segment_sectID <= sectID)
1137 {
1138 lldb::user_id_t sect_uid;
1139 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1140 {
1141 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1142 SectionSP next_section_sp;
1143 if (sect_uid + 1 <= sectID)
1144 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1145
1146 if (curr_section_sp.get())
1147 {
1148 if (curr_section_sp->GetByteSize() == 0)
1149 {
1150 if (next_section_sp.get() != NULL)
1151 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1152 else
1153 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1154 }
1155 }
1156 }
1157 }
1158 }
1159 }
1160 }
1161 }
Greg Clayton1674b122010-07-21 22:12:05 +00001162 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001163 {
1164 m_dysymtab.cmd = load_cmd.cmd;
1165 m_dysymtab.cmdsize = load_cmd.cmdsize;
1166 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1167 }
1168
1169 offset = load_cmd_offset + load_cmd.cmdsize;
1170 }
1171// if (dump_sections)
1172// {
1173// StreamFile s(stdout);
1174// m_sections_ap->Dump(&s, true);
1175// }
1176 return sectID; // Return the number of sections we registered with the module
1177}
1178
1179class MachSymtabSectionInfo
1180{
1181public:
1182
1183 MachSymtabSectionInfo (SectionList *section_list) :
1184 m_section_list (section_list),
1185 m_section_infos()
1186 {
1187 // Get the number of sections down to a depth of 1 to include
1188 // all segments and their sections, but no other sections that
1189 // may be added for debug map or
1190 m_section_infos.resize(section_list->GetNumSections(1));
1191 }
1192
1193
Greg Clayton3508c382012-02-24 01:59:29 +00001194 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001195 GetSection (uint8_t n_sect, addr_t file_addr)
1196 {
1197 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001198 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001199 if (n_sect < m_section_infos.size())
1200 {
Greg Clayton3508c382012-02-24 01:59:29 +00001201 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001202 {
Greg Clayton3508c382012-02-24 01:59:29 +00001203 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1204 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001205 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001206 {
Greg Clayton3508c382012-02-24 01:59:29 +00001207 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1208 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001209 }
1210 else
1211 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001212 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001213 }
Chris Lattner24943d22010-06-08 16:52:24 +00001214 }
1215 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001216 {
1217 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001218 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001219 }
1220 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1221 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1222 {
1223 // Symbol is in section with zero size, but has the same start
1224 // address as the section. This can happen with linker symbols
1225 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001226 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001227 }
Chris Lattner24943d22010-06-08 16:52:24 +00001228 }
Greg Clayton3508c382012-02-24 01:59:29 +00001229 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001230 }
1231
1232protected:
1233 struct SectionInfo
1234 {
1235 SectionInfo () :
1236 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001237 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001238 {
1239 }
1240
1241 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001242 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001243 };
1244 SectionList *m_section_list;
1245 std::vector<SectionInfo> m_section_infos;
1246};
1247
Chris Lattner24943d22010-06-08 16:52:24 +00001248size_t
1249ObjectFileMachO::ParseSymtab (bool minimize)
1250{
1251 Timer scoped_timer(__PRETTY_FUNCTION__,
1252 "ObjectFileMachO::ParseSymtab () module = %s",
1253 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001254 ModuleSP module_sp (GetModule());
1255 if (!module_sp)
1256 return 0;
1257
1258 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1259 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1260 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1261 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001262 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001263 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001264
Greg Clayton952e9dc2013-03-27 23:08:40 +00001265 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001266
Chris Lattner24943d22010-06-08 16:52:24 +00001267 for (i=0; i<m_header.ncmds; ++i)
1268 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001269 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001270 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001271 struct load_command lc;
1272 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001273 break;
1274 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001275 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001276 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001277 case LoadCommandSymtab:
1278 symtab_load_command.cmd = lc.cmd;
1279 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001280 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001281 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1282 return 0;
1283 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001284 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001285 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001286 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001287 return 0;
1288 }
1289
1290 if (symtab_load_command.stroff == 0)
1291 {
1292 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001293 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001294 return 0;
1295 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001296
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001297 if (symtab_load_command.nsyms == 0)
1298 {
1299 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001300 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001301 return 0;
1302 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001303
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001304 if (symtab_load_command.strsize == 0)
1305 {
1306 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001307 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001308 return 0;
1309 }
1310 break;
1311
1312 case LoadCommandFunctionStarts:
1313 function_starts_load_command.cmd = lc.cmd;
1314 function_starts_load_command.cmdsize = lc.cmdsize;
1315 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1316 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1317 break;
1318
1319 default:
1320 break;
1321 }
1322 offset = cmd_offset + lc.cmdsize;
1323 }
1324
1325 if (symtab_load_command.cmd)
1326 {
1327 Symtab *symtab = m_symtab_ap.get();
1328 SectionList *section_list = GetSectionList();
1329 if (section_list == NULL)
1330 return 0;
1331
1332 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001333 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001334
Greg Clayton36da2aa2013-01-25 18:06:21 +00001335 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1336 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001337 bool bit_width_32 = addr_byte_size == 4;
1338 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1339
Greg Clayton36da2aa2013-01-25 18:06:21 +00001340 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1341 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1342 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001343 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001344
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001345 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1346 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001347 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1348 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001349 {
Greg Claytondd29b972012-05-18 23:20:01 +00001350 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001351 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1352 // Reading mach file from memory in a process or core file...
1353
1354 if (linkedit_section_sp)
1355 {
1356 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1357 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1358 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001359 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001360
1361 bool data_was_read = false;
1362
1363#if defined (__APPLE__) && defined (__arm__)
1364 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001365 {
Greg Clayton29021d32012-04-18 05:19:20 +00001366 // This mach-o memory file is in the dyld shared cache. If this
1367 // program is not remote and this is iOS, then this process will
1368 // share the same shared cache as the process we are debugging and
1369 // we can read the entire __LINKEDIT from the address space in this
1370 // process. This is a needed optimization that is used for local iOS
1371 // debugging only since all shared libraries in the shared cache do
1372 // not have corresponding files that exist in the file system of the
1373 // device. They have been combined into a single file. This means we
1374 // always have to load these files from memory. All of the symbol and
1375 // string tables from all of the __LINKEDIT sections from the shared
1376 // libraries in the shared cache have been merged into a single large
1377 // symbol and string table. Reading all of this symbol and string table
1378 // data across can slow down debug launch times, so we optimize this by
1379 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda45c75502013-04-16 06:24:42 +00001380
1381 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1382 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1383 bool use_lldb_cache = true;
1384 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1385 {
1386 use_lldb_cache = false;
1387 }
1388
Greg Clayton29021d32012-04-18 05:19:20 +00001389 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda45c75502013-04-16 06:24:42 +00001390 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton29021d32012-04-18 05:19:20 +00001391 {
1392 data_was_read = true;
1393 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001394 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001395 if (function_starts_load_command.cmd)
1396 {
1397 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1398 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1399 }
1400 }
1401 }
1402#endif
1403
1404 if (!data_was_read)
1405 {
1406 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1407 if (nlist_data_sp)
1408 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001409 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1410 //if (strtab_data_sp)
1411 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001412 if (m_dysymtab.nindirectsyms != 0)
1413 {
1414 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1415 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1416 if (indirect_syms_data_sp)
1417 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1418 }
Greg Clayton29021d32012-04-18 05:19:20 +00001419 if (function_starts_load_command.cmd)
1420 {
1421 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1422 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1423 if (func_start_data_sp)
1424 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1425 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001426 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001427 }
1428 }
1429 else
1430 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001431 nlist_data.SetData (m_data,
1432 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001433 nlist_data_byte_size);
1434 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001435 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001436 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001437 if (m_dysymtab.nindirectsyms != 0)
1438 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001439 indirect_symbol_index_data.SetData (m_data,
1440 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001441 m_dysymtab.nindirectsyms * 4);
1442 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001443 if (function_starts_load_command.cmd)
1444 {
1445 function_starts_data.SetData (m_data,
1446 function_starts_load_command.dataoff,
1447 function_starts_load_command.datasize);
1448 }
1449 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001450
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001451 if (nlist_data.GetByteSize() == 0)
1452 {
1453 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001454 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001455 return 0;
1456 }
1457
1458
Greg Clayton3a5dc012012-05-25 17:04:00 +00001459 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1460 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001461 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001462 if (process)
1463 {
1464 if (strtab_addr == LLDB_INVALID_ADDRESS)
1465 {
1466 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001467 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001468 return 0;
1469 }
1470 }
1471 else
Greg Claytondd29b972012-05-18 23:20:01 +00001472 {
1473 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001474 module_sp->LogMessage(log, "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001475 return 0;
1476 }
1477 }
Greg Claytondd29b972012-05-18 23:20:01 +00001478
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001479 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1480 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1481 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1482 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1483 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1484 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1485 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1486 SectionSP eh_frame_section_sp;
1487 if (text_section_sp.get())
1488 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1489 else
1490 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1491
Greg Claytond2653c22012-03-14 01:53:24 +00001492 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001493
1494 // lldb works best if it knows the start addresss of all functions in a module.
1495 // Linker symbols or debug info are normally the best source of information for start addr / size but
1496 // they may be stripped in a released binary.
Jason Molenda2120aef2013-04-16 00:18:44 +00001497 // Two additional sources of information exist in Mach-O binaries:
Jason Molendad7938392013-03-21 03:36:01 +00001498 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1499 // binary, relative to the text section.
1500 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1501 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1502 // Binaries built to run on older releases may need to use eh_frame information.
1503
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001504 if (text_section_sp && function_starts_data.GetByteSize())
1505 {
1506 FunctionStarts::Entry function_start_entry;
1507 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001508 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001509 function_start_entry.addr = text_section_sp->GetFileAddress();
1510 uint64_t delta;
1511 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1512 {
1513 // Now append the current entry
1514 function_start_entry.addr += delta;
1515 function_starts.Append(function_start_entry);
1516 }
Jason Molenda2120aef2013-04-16 00:18:44 +00001517 }
Jason Molendad7938392013-03-21 03:36:01 +00001518 else
1519 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001520 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1521 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1522 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1523 // the module.
1524 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001525 {
1526 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1527 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1528 eh_frame.GetFunctionAddressAndSizeVector (functions);
1529 addr_t text_base_addr = text_section_sp->GetFileAddress();
1530 size_t count = functions.GetSize();
1531 for (size_t i = 0; i < count; ++i)
1532 {
1533 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1534 if (func)
1535 {
1536 FunctionStarts::Entry function_start_entry;
1537 function_start_entry.addr = func->base - text_base_addr;
1538 function_starts.Append(function_start_entry);
1539 }
1540 }
1541 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001542 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001543
Greg Clayton36da2aa2013-01-25 18:06:21 +00001544 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001545
Greg Clayton36da2aa2013-01-25 18:06:21 +00001546 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 +00001547
Greg Clayton36da2aa2013-01-25 18:06:21 +00001548 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001549
1550 uint32_t N_SO_index = UINT32_MAX;
1551
1552 MachSymtabSectionInfo section_info (section_list);
1553 std::vector<uint32_t> N_FUN_indexes;
1554 std::vector<uint32_t> N_NSYM_indexes;
1555 std::vector<uint32_t> N_INCL_indexes;
1556 std::vector<uint32_t> N_BRAC_indexes;
1557 std::vector<uint32_t> N_COMM_indexes;
1558 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1559 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1560 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1561 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1562 // Any symbols that get merged into another will get an entry
1563 // in this map so we know
1564 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1565 uint32_t nlist_idx = 0;
1566 Symbol *symbol_ptr = NULL;
1567
1568 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001569 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001570 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001571 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001572 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001573
Jason Molendab62abd52012-06-21 01:51:02 +00001574#if defined (__APPLE__) && defined (__arm__)
1575
1576 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1577 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1578 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1579 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1580 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1581 // nlist parser to ignore all LOCAL symbols.
1582
1583 if (m_header.flags & 0x80000000u)
1584 {
1585 // Before we can start mapping the DSC, we need to make certain the target process is actually
1586 // using the cache we can find.
1587
Jason Molendab62abd52012-06-21 01:51:02 +00001588 // Next we need to determine the correct path for the dyld shared cache.
1589
1590 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1591 char dsc_path[PATH_MAX];
1592
1593 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001594 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1595 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001596 header_arch.GetArchitectureName());
1597
1598 FileSpec dsc_filespec(dsc_path, false);
1599
1600 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda2120aef2013-04-16 00:18:44 +00001601 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001602 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001603 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1604 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1605 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001606 uint32_t imagesOffset;
1607 uint32_t imagesCount;
1608 uint64_t dyldBaseAddress;
1609 uint64_t codeSignatureOffset;
1610 uint64_t codeSignatureSize;
1611 uint64_t slideInfoOffset;
1612 uint64_t slideInfoSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001613 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1614 uint64_t localSymbolsSize; // size of local symbols information
1615 };
1616 struct lldb_copy_dyld_cache_header_v1
1617 {
1618 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
1621 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 Molenda9badb6c2013-03-06 23:19:17 +00001628 uint64_t localSymbolsOffset;
1629 uint64_t localSymbolsSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001630 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001631 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001632
Jason Molenda2120aef2013-04-16 00:18:44 +00001633 struct lldb_copy_dyld_cache_mapping_info
1634 {
1635 uint64_t address;
1636 uint64_t size;
1637 uint64_t fileOffset;
1638 uint32_t maxProt;
1639 uint32_t initProt;
1640 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001641
Greg Claytonab77dcb2012-09-05 22:30:51 +00001642 struct lldb_copy_dyld_cache_local_symbols_info
1643 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001644 uint32_t nlistOffset;
1645 uint32_t nlistCount;
1646 uint32_t stringsOffset;
1647 uint32_t stringsSize;
1648 uint32_t entriesOffset;
1649 uint32_t entriesCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001650 };
1651 struct lldb_copy_dyld_cache_local_symbols_entry
1652 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001653 uint32_t dylibOffset;
1654 uint32_t nlistStartIndex;
1655 uint32_t nlistCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001656 };
Jason Molendab62abd52012-06-21 01:51:02 +00001657
Jason Molendafd3b35d2012-06-22 03:28:35 +00001658 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1659 The dyld_cache_local_symbols_info structure gives us three things:
1660 1. The start and count of the nlist records in the dyld_shared_cache file
1661 2. The start and size of the strings for these nlist records
1662 3. The start and count of dyld_cache_local_symbols_entry entries
1663
1664 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1665 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001666 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001667 and the count of how many nlist records there are for this dylib/framework.
1668 */
1669
Jason Molendab62abd52012-06-21 01:51:02 +00001670 // Process the dsc header to find the unmapped symbols
1671 //
1672 // Save some VM space, do not map the entire cache in one shot.
1673
Jason Molenda6bcabae2013-03-06 23:17:36 +00001674 DataBufferSP dsc_data_sp;
1675 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1676
1677 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001678 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001679 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001680
Jason Molenda6bcabae2013-03-06 23:17:36 +00001681 char version_str[17];
1682 int version = -1;
1683 lldb::offset_t offset = 0;
1684 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1685 version_str[16] = '\0';
1686 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1687 {
1688 int v;
1689 if (::sscanf (version_str + 6, "%d", &v) == 1)
1690 {
1691 version = v;
1692 }
1693 }
1694
Jason Molenda45c75502013-04-16 06:24:42 +00001695 UUID dsc_uuid;
1696 if (version >= 1)
1697 {
1698 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1699 uint8_t uuid_bytes[sizeof (uuid_t)];
1700 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1701 dsc_uuid.SetBytes (uuid_bytes);
1702 }
1703
1704 bool uuid_match = true;
1705 if (dsc_uuid.IsValid() && process)
1706 {
1707 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1708
1709 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1710 {
1711 // The on-disk dyld_shared_cache file is not the same as the one in this
1712 // process' memory, don't use it.
1713 uuid_match = false;
1714 }
1715 }
1716
Jason Molenda9badb6c2013-03-06 23:19:17 +00001717 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001718
Jason Molendab62abd52012-06-21 01:51:02 +00001719 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1720
1721 // If the mappingOffset points to a location inside the header, we've
1722 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda45c75502013-04-16 06:24:42 +00001723 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendab62abd52012-06-21 01:51:02 +00001724 {
1725
Jason Molenda6bcabae2013-03-06 23:17:36 +00001726 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1727 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1728 offset = 0;
1729
1730 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1731 // in the shared library cache need to be adjusted by an offset to match up with the
1732 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1733 // recorded in mapping_offset_value.
1734 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1735
1736 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001737 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1738 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1739
Jason Molenda9badb6c2013-03-06 23:19:17 +00001740 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001741 {
1742 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001743 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001744 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001745 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001746
1747 offset = 0;
1748
1749 // Read the local_symbols_infos struct in one shot
1750 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1751 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1752
Jason Molendab62abd52012-06-21 01:51:02 +00001753 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1754
Jason Molenda6bcabae2013-03-06 23:17:36 +00001755 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001756
1757 offset = local_symbols_info.entriesOffset;
1758 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1759 {
1760 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1761 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1762 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1763 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1764
Jason Molenda9badb6c2013-03-06 23:19:17 +00001765 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001766 {
1767 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1768
1769 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1770 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1771 num_syms = symtab->GetNumSymbols();
1772
1773 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1774 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1775
Jason Molenda9badb6c2013-03-06 23:19:17 +00001776 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001777 {
1778 /////////////////////////////
1779 {
1780 struct nlist_64 nlist;
1781 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1782 break;
1783
1784 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1785 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1786 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1787 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1788 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1789
1790 SymbolType type = eSymbolTypeInvalid;
1791 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1792
1793 if (symbol_name == NULL)
1794 {
1795 // No symbol should be NULL, even the symbols with no
1796 // string values should have an offset zero which points
1797 // to an empty C-string
1798 Host::SystemLog (Host::eSystemLogError,
1799 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1800 entry_index,
1801 nlist.n_strx,
1802 module_sp->GetFileSpec().GetDirectory().GetCString(),
1803 module_sp->GetFileSpec().GetFilename().GetCString());
1804 continue;
1805 }
1806 if (symbol_name[0] == '\0')
1807 symbol_name = NULL;
1808
1809 const char *symbol_name_non_abi_mangled = NULL;
1810
1811 SectionSP symbol_section;
1812 uint32_t symbol_byte_size = 0;
1813 bool add_nlist = true;
1814 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001815 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001816
1817 assert (sym_idx < num_syms);
1818
1819 sym[sym_idx].SetDebug (is_debug);
1820
1821 if (is_debug)
1822 {
1823 switch (nlist.n_type)
1824 {
1825 case StabGlobalSymbol:
1826 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1827 // Sometimes the N_GSYM value contains the address.
1828
1829 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1830 // have the same address, but we want to ensure that we always find only the real symbol,
1831 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1832 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1833 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1834 // same address.
1835
1836 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1837 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1838 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1839 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1840 add_nlist = false;
1841 else
1842 {
1843 sym[sym_idx].SetExternal(true);
1844 if (nlist.n_value != 0)
1845 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1846 type = eSymbolTypeData;
1847 }
1848 break;
1849
1850 case StabFunctionName:
1851 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1852 type = eSymbolTypeCompiler;
1853 break;
1854
1855 case StabFunction:
1856 // N_FUN -- procedure: name,,n_sect,linenumber,address
1857 if (symbol_name)
1858 {
1859 type = eSymbolTypeCode;
1860 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1861
1862 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1863 // We use the current number of symbols in the symbol table in lieu of
1864 // using nlist_idx in case we ever start trimming entries out
1865 N_FUN_indexes.push_back(sym_idx);
1866 }
1867 else
1868 {
1869 type = eSymbolTypeCompiler;
1870
1871 if ( !N_FUN_indexes.empty() )
1872 {
1873 // Copy the size of the function into the original STAB entry so we don't have
1874 // to hunt for it later
1875 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1876 N_FUN_indexes.pop_back();
1877 // We don't really need the end function STAB as it contains the size which
1878 // we already placed with the original symbol, so don't add it if we want a
1879 // minimal symbol table
1880 if (minimize)
1881 add_nlist = false;
1882 }
1883 }
1884 break;
1885
1886 case StabStaticSymbol:
1887 // N_STSYM -- static symbol: name,,n_sect,type,address
1888 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1889 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1890 type = eSymbolTypeData;
1891 break;
1892
1893 case StabLocalCommon:
1894 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1895 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1896 type = eSymbolTypeCommonBlock;
1897 break;
1898
1899 case StabBeginSymbol:
1900 // N_BNSYM
1901 // We use the current number of symbols in the symbol table in lieu of
1902 // using nlist_idx in case we ever start trimming entries out
1903 if (minimize)
1904 {
1905 // Skip these if we want minimal symbol tables
1906 add_nlist = false;
1907 }
1908 else
1909 {
1910 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1911 N_NSYM_indexes.push_back(sym_idx);
1912 type = eSymbolTypeScopeBegin;
1913 }
1914 break;
1915
1916 case StabEndSymbol:
1917 // N_ENSYM
1918 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1919 // so that we can always skip the entire symbol if we need to navigate
1920 // more quickly at the source level when parsing STABS
1921 if (minimize)
1922 {
1923 // Skip these if we want minimal symbol tables
1924 add_nlist = false;
1925 }
1926 else
1927 {
1928 if ( !N_NSYM_indexes.empty() )
1929 {
1930 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1931 symbol_ptr->SetByteSize(sym_idx + 1);
1932 symbol_ptr->SetSizeIsSibling(true);
1933 N_NSYM_indexes.pop_back();
1934 }
1935 type = eSymbolTypeScopeEnd;
1936 }
1937 break;
1938
1939
1940 case StabSourceFileOptions:
1941 // N_OPT - emitted with gcc2_compiled and in gcc source
1942 type = eSymbolTypeCompiler;
1943 break;
1944
1945 case StabRegisterSymbol:
1946 // N_RSYM - register sym: name,,NO_SECT,type,register
1947 type = eSymbolTypeVariable;
1948 break;
1949
1950 case StabSourceLine:
1951 // N_SLINE - src line: 0,,n_sect,linenumber,address
1952 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1953 type = eSymbolTypeLineEntry;
1954 break;
1955
1956 case StabStructureType:
1957 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1958 type = eSymbolTypeVariableType;
1959 break;
1960
1961 case StabSourceFileName:
1962 // N_SO - source file name
1963 type = eSymbolTypeSourceFile;
1964 if (symbol_name == NULL)
1965 {
1966 if (minimize)
1967 add_nlist = false;
1968 if (N_SO_index != UINT32_MAX)
1969 {
1970 // Set the size of the N_SO to the terminating index of this N_SO
1971 // so that we can always skip the entire N_SO if we need to navigate
1972 // more quickly at the source level when parsing STABS
1973 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1974 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1975 symbol_ptr->SetSizeIsSibling(true);
1976 }
1977 N_NSYM_indexes.clear();
1978 N_INCL_indexes.clear();
1979 N_BRAC_indexes.clear();
1980 N_COMM_indexes.clear();
1981 N_FUN_indexes.clear();
1982 N_SO_index = UINT32_MAX;
1983 }
1984 else
1985 {
1986 // We use the current number of symbols in the symbol table in lieu of
1987 // using nlist_idx in case we ever start trimming entries out
1988 const bool N_SO_has_full_path = symbol_name[0] == '/';
1989 if (N_SO_has_full_path)
1990 {
1991 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
1992 {
1993 // We have two consecutive N_SO entries where the first contains a directory
1994 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00001995 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00001996 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
1997 add_nlist = false;
1998 }
1999 else
2000 {
2001 // This is the first entry in a N_SO that contains a directory or
2002 // a full path to the source file
2003 N_SO_index = sym_idx;
2004 }
2005 }
2006 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2007 {
2008 // This is usually the second N_SO entry that contains just the filename,
2009 // so here we combine it with the first one if we are minimizing the symbol table
2010 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2011 if (so_path && so_path[0])
2012 {
2013 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002014 const size_t double_slash_pos = full_so_path.find("//");
2015 if (double_slash_pos != std::string::npos)
2016 {
2017 // The linker has been generating bad N_SO entries with doubled up paths
2018 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2019 // and the second is the directory for the source file so you end up with
2020 // a path that looks like "/tmp/src//tmp/src/"
2021 FileSpec so_dir(so_path, false);
2022 if (!so_dir.Exists())
2023 {
2024 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2025 if (so_dir.Exists())
2026 {
2027 // Trim off the incorrect path
2028 full_so_path.erase(0, double_slash_pos + 1);
2029 }
2030 }
2031 }
Jason Molendab62abd52012-06-21 01:51:02 +00002032 if (*full_so_path.rbegin() != '/')
2033 full_so_path += '/';
2034 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00002035 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002036 add_nlist = false;
2037 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2038 }
2039 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002040 else
2041 {
2042 // This could be a relative path to a N_SO
2043 N_SO_index = sym_idx;
2044 }
Jason Molendab62abd52012-06-21 01:51:02 +00002045 }
Jason Molendab62abd52012-06-21 01:51:02 +00002046 break;
2047
2048 case StabObjectFileName:
2049 // N_OSO - object file name: name,,0,0,st_mtime
2050 type = eSymbolTypeObjectFile;
2051 break;
2052
2053 case StabLocalSymbol:
2054 // N_LSYM - local sym: name,,NO_SECT,type,offset
2055 type = eSymbolTypeLocal;
2056 break;
2057
2058 //----------------------------------------------------------------------
2059 // INCL scopes
2060 //----------------------------------------------------------------------
2061 case StabBeginIncludeFileName:
2062 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2063 // We use the current number of symbols in the symbol table in lieu of
2064 // using nlist_idx in case we ever start trimming entries out
2065 N_INCL_indexes.push_back(sym_idx);
2066 type = eSymbolTypeScopeBegin;
2067 break;
2068
2069 case StabEndIncludeFile:
2070 // N_EINCL - include file end: name,,NO_SECT,0,0
2071 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2072 // so that we can always skip the entire symbol if we need to navigate
2073 // more quickly at the source level when parsing STABS
2074 if ( !N_INCL_indexes.empty() )
2075 {
2076 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2077 symbol_ptr->SetByteSize(sym_idx + 1);
2078 symbol_ptr->SetSizeIsSibling(true);
2079 N_INCL_indexes.pop_back();
2080 }
2081 type = eSymbolTypeScopeEnd;
2082 break;
2083
2084 case StabIncludeFileName:
2085 // N_SOL - #included file name: name,,n_sect,0,address
2086 type = eSymbolTypeHeaderFile;
2087
2088 // We currently don't use the header files on darwin
2089 if (minimize)
2090 add_nlist = false;
2091 break;
2092
2093 case StabCompilerParameters:
2094 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2095 type = eSymbolTypeCompiler;
2096 break;
2097
2098 case StabCompilerVersion:
2099 // N_VERSION - compiler version: name,,NO_SECT,0,0
2100 type = eSymbolTypeCompiler;
2101 break;
2102
2103 case StabCompilerOptLevel:
2104 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2105 type = eSymbolTypeCompiler;
2106 break;
2107
2108 case StabParameter:
2109 // N_PSYM - parameter: name,,NO_SECT,type,offset
2110 type = eSymbolTypeVariable;
2111 break;
2112
2113 case StabAlternateEntry:
2114 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2115 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2116 type = eSymbolTypeLineEntry;
2117 break;
2118
2119 //----------------------------------------------------------------------
2120 // Left and Right Braces
2121 //----------------------------------------------------------------------
2122 case StabLeftBracket:
2123 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2124 // We use the current number of symbols in the symbol table in lieu of
2125 // using nlist_idx in case we ever start trimming entries out
2126 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2127 N_BRAC_indexes.push_back(sym_idx);
2128 type = eSymbolTypeScopeBegin;
2129 break;
2130
2131 case StabRightBracket:
2132 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2133 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2134 // so that we can always skip the entire symbol if we need to navigate
2135 // more quickly at the source level when parsing STABS
2136 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2137 if ( !N_BRAC_indexes.empty() )
2138 {
2139 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2140 symbol_ptr->SetByteSize(sym_idx + 1);
2141 symbol_ptr->SetSizeIsSibling(true);
2142 N_BRAC_indexes.pop_back();
2143 }
2144 type = eSymbolTypeScopeEnd;
2145 break;
2146
2147 case StabDeletedIncludeFile:
2148 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2149 type = eSymbolTypeHeaderFile;
2150 break;
2151
2152 //----------------------------------------------------------------------
2153 // COMM scopes
2154 //----------------------------------------------------------------------
2155 case StabBeginCommon:
2156 // N_BCOMM - begin common: name,,NO_SECT,0,0
2157 // We use the current number of symbols in the symbol table in lieu of
2158 // using nlist_idx in case we ever start trimming entries out
2159 type = eSymbolTypeScopeBegin;
2160 N_COMM_indexes.push_back(sym_idx);
2161 break;
2162
2163 case StabEndCommonLocal:
2164 // N_ECOML - end common (local name): 0,,n_sect,0,address
2165 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2166 // Fall through
2167
2168 case StabEndCommon:
2169 // N_ECOMM - end common: name,,n_sect,0,0
2170 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2171 // so that we can always skip the entire symbol if we need to navigate
2172 // more quickly at the source level when parsing STABS
2173 if ( !N_COMM_indexes.empty() )
2174 {
2175 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2176 symbol_ptr->SetByteSize(sym_idx + 1);
2177 symbol_ptr->SetSizeIsSibling(true);
2178 N_COMM_indexes.pop_back();
2179 }
2180 type = eSymbolTypeScopeEnd;
2181 break;
2182
2183 case StabLength:
2184 // N_LENG - second stab entry with length information
2185 type = eSymbolTypeAdditional;
2186 break;
2187
2188 default: break;
2189 }
2190 }
2191 else
2192 {
2193 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2194 uint8_t n_type = NlistMaskType & nlist.n_type;
2195 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2196
2197 switch (n_type)
2198 {
2199 case NListTypeIndirect: // N_INDR - Fall through
2200 case NListTypePreboundUndefined:// N_PBUD - Fall through
2201 case NListTypeUndefined: // N_UNDF
2202 type = eSymbolTypeUndefined;
2203 break;
2204
2205 case NListTypeAbsolute: // N_ABS
2206 type = eSymbolTypeAbsolute;
2207 break;
2208
2209 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002210 {
2211 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002212
Greg Clayton01e6a582012-11-27 01:52:16 +00002213 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002214 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002215 // TODO: warn about this?
2216 add_nlist = false;
2217 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002218 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002219
2220 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002221 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002222 type = eSymbolTypeException;
2223 }
2224 else
2225 {
2226 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002227
Greg Clayton01e6a582012-11-27 01:52:16 +00002228 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002229 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002230 case SectionTypeRegular: break; // regular section
2231 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2232 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2233 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2234 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2235 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2236 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2237 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2238 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2239 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2240 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2241 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2242 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2243 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2244 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2245 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2246 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2247 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002248 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002249
Greg Clayton01e6a582012-11-27 01:52:16 +00002250 if (type == eSymbolTypeInvalid)
2251 {
2252 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2253 if (symbol_section->IsDescendant (text_section_sp.get()))
2254 {
2255 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2256 SectionAttrUserSelfModifyingCode |
2257 SectionAttrSytemSomeInstructions))
2258 type = eSymbolTypeData;
2259 else
2260 type = eSymbolTypeCode;
2261 }
2262 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002263 {
2264 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2265 {
2266 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002267
Greg Clayton01e6a582012-11-27 01:52:16 +00002268 if (symbol_name &&
2269 symbol_name[0] == '_' &&
2270 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002271 symbol_name[2] == 'B')
2272 {
2273 llvm::StringRef symbol_name_ref(symbol_name);
2274 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2275 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2276 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2277 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2278 {
2279 symbol_name_non_abi_mangled = symbol_name + 1;
2280 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2281 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002282 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002283 }
2284 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2285 {
2286 symbol_name_non_abi_mangled = symbol_name + 1;
2287 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2288 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002289 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002290 }
2291 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2292 {
2293 symbol_name_non_abi_mangled = symbol_name + 1;
2294 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2295 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002296 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002297 }
2298 }
2299 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002300 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002301 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002302 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002303 }
2304 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002305 {
2306 type = eSymbolTypeData;
2307 }
2308 }
2309 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2310 {
2311 type = eSymbolTypeTrampoline;
2312 }
2313 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2314 {
2315 type = eSymbolTypeRuntime;
2316 if (symbol_name && symbol_name[0] == '.')
2317 {
2318 llvm::StringRef symbol_name_ref(symbol_name);
2319 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2320 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002321 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002322 symbol_name_non_abi_mangled = symbol_name;
2323 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2324 type = eSymbolTypeObjCClass;
2325 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002326 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002327 }
2328 }
2329 }
Jason Molendab62abd52012-06-21 01:51:02 +00002330 }
2331 }
Jason Molendab62abd52012-06-21 01:51:02 +00002332 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002333 }
Jason Molendab62abd52012-06-21 01:51:02 +00002334 }
2335
2336 if (add_nlist)
2337 {
2338 uint64_t symbol_value = nlist.n_value;
2339 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002340
Jason Molendab62abd52012-06-21 01:51:02 +00002341 if (symbol_name_non_abi_mangled)
2342 {
Jason Molenda292cca82012-07-20 03:35:44 +00002343 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2344 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002345 }
2346 else
2347 {
2348 if (symbol_name && symbol_name[0] == '_')
2349 {
2350 symbol_name_is_mangled = symbol_name[1] == '_';
2351 symbol_name++; // Skip the leading underscore
2352 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002353
Jason Molendab62abd52012-06-21 01:51:02 +00002354 if (symbol_name)
2355 {
Jason Molenda292cca82012-07-20 03:35:44 +00002356 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002357 }
2358 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002359
Jason Molendab62abd52012-06-21 01:51:02 +00002360 if (is_debug == false)
2361 {
2362 if (type == eSymbolTypeCode)
2363 {
2364 // See if we can find a N_FUN entry for any code symbols.
2365 // If we do find a match, and the name matches, then we
2366 // can merge the two into just the function symbol to avoid
2367 // duplicate entries in the symbol table
2368 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2369 if (pos != N_FUN_addr_to_sym_idx.end())
2370 {
2371 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2372 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2373 {
2374 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2375 // We just need the flags from the linker symbol, so put these flags
2376 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2377 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2378 sym[sym_idx].Clear();
2379 continue;
2380 }
2381 }
2382 }
2383 else if (type == eSymbolTypeData)
2384 {
2385 // See if we can find a N_STSYM entry for any data symbols.
2386 // If we do find a match, and the name matches, then we
2387 // can merge the two into just the Static symbol to avoid
2388 // duplicate entries in the symbol table
2389 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2390 if (pos != N_STSYM_addr_to_sym_idx.end())
2391 {
2392 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2393 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2394 {
2395 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2396 // We just need the flags from the linker symbol, so put these flags
2397 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2398 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2399 sym[sym_idx].Clear();
2400 continue;
2401 }
2402 }
2403 }
2404 }
2405 if (symbol_section)
2406 {
2407 const addr_t section_file_addr = symbol_section->GetFileAddress();
2408 if (symbol_byte_size == 0 && function_starts_count > 0)
2409 {
2410 addr_t symbol_lookup_file_addr = nlist.n_value;
2411 // Do an exact address match for non-ARM addresses, else get the closest since
2412 // the symbol might be a thumb symbol which has an address with bit zero set
2413 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2414 if (is_arm && func_start_entry)
2415 {
2416 // Verify that the function start address is the symbol address (ARM)
2417 // or the symbol address + 1 (thumb)
2418 if (func_start_entry->addr != symbol_lookup_file_addr &&
2419 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2420 {
2421 // Not the right entry, NULL it out...
2422 func_start_entry = NULL;
2423 }
2424 }
2425 if (func_start_entry)
2426 {
2427 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002428
Jason Molendab62abd52012-06-21 01:51:02 +00002429 addr_t symbol_file_addr = func_start_entry->addr;
2430 uint32_t symbol_flags = 0;
2431 if (is_arm)
2432 {
2433 if (symbol_file_addr & 1)
2434 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2435 symbol_file_addr &= 0xfffffffffffffffeull;
2436 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002437
Jason Molendab62abd52012-06-21 01:51:02 +00002438 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2439 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2440 if (next_func_start_entry)
2441 {
2442 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2443 // Be sure the clear the Thumb address bit when we calculate the size
2444 // from the current and next address
2445 if (is_arm)
2446 next_symbol_file_addr &= 0xfffffffffffffffeull;
2447 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2448 }
2449 else
2450 {
2451 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2452 }
2453 }
2454 }
2455 symbol_value -= section_file_addr;
2456 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002457
Jason Molendab62abd52012-06-21 01:51:02 +00002458 sym[sym_idx].SetID (nlist_idx);
2459 sym[sym_idx].SetType (type);
2460 sym[sym_idx].GetAddress().SetSection (symbol_section);
2461 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2462 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002463
Jason Molendab62abd52012-06-21 01:51:02 +00002464 if (symbol_byte_size > 0)
2465 sym[sym_idx].SetByteSize(symbol_byte_size);
2466
Greg Clayton01e6a582012-11-27 01:52:16 +00002467 if (demangled_is_synthesized)
2468 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002469 ++sym_idx;
2470 }
2471 else
2472 {
2473 sym[sym_idx].Clear();
2474 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002475
Jason Molendab62abd52012-06-21 01:51:02 +00002476 }
2477 /////////////////////////////
2478 }
2479 break; // No more entries to consider
2480 }
2481 }
2482 }
2483 }
2484 }
2485 }
2486 }
2487
2488 // Must reset this in case it was mutated above!
2489 nlist_data_offset = 0;
2490#endif
2491
2492 // If the sym array was not created while parsing the DSC unmapped
2493 // symbols, create it now.
2494 if (sym == NULL)
2495 {
2496 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2497 num_syms = symtab->GetNumSymbols();
2498 }
2499
2500 if (unmapped_local_symbols_found)
2501 {
2502 assert(m_dysymtab.ilocalsym == 0);
2503 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2504 nlist_idx = m_dysymtab.nlocalsym;
2505 }
2506 else
2507 {
2508 nlist_idx = 0;
2509 }
2510
2511 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002512 {
2513 struct nlist_64 nlist;
2514 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2515 break;
2516
2517 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2518 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2519 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2520 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2521 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2522
2523 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002524 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002525
Greg Clayton3a5dc012012-05-25 17:04:00 +00002526 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002527 {
2528 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002529
Greg Claytondd29b972012-05-18 23:20:01 +00002530 if (symbol_name == NULL)
2531 {
2532 // No symbol should be NULL, even the symbols with no
2533 // string values should have an offset zero which points
2534 // to an empty C-string
2535 Host::SystemLog (Host::eSystemLogError,
Jason Molenda9badb6c2013-03-06 23:19:17 +00002536 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002537 nlist_idx,
2538 nlist.n_strx,
2539 module_sp->GetFileSpec().GetDirectory().GetCString(),
2540 module_sp->GetFileSpec().GetFilename().GetCString());
2541 continue;
2542 }
2543 if (symbol_name[0] == '\0')
2544 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002545 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002546 else
2547 {
2548 const addr_t str_addr = strtab_addr + nlist.n_strx;
2549 Error str_error;
2550 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2551 symbol_name = memory_symbol_name.c_str();
2552 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002553 const char *symbol_name_non_abi_mangled = NULL;
2554
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002555 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002556 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002557 bool add_nlist = true;
2558 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002559 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002560
2561 assert (sym_idx < num_syms);
2562
2563 sym[sym_idx].SetDebug (is_debug);
2564
2565 if (is_debug)
2566 {
2567 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002568 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002569 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002570 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2571 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002572
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002573 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2574 // have the same address, but we want to ensure that we always find only the real symbol,
2575 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2576 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2577 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2578 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002579
2580 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002581 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2582 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2583 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2584 add_nlist = false;
2585 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002586 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002587 sym[sym_idx].SetExternal(true);
2588 if (nlist.n_value != 0)
2589 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2590 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002591 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002592 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002593
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002594 case StabFunctionName:
2595 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2596 type = eSymbolTypeCompiler;
2597 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002598
Jason Molenda9badb6c2013-03-06 23:19:17 +00002599 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002600 // N_FUN -- procedure: name,,n_sect,linenumber,address
2601 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002602 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002603 type = eSymbolTypeCode;
2604 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002605
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002606 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2607 // We use the current number of symbols in the symbol table in lieu of
2608 // using nlist_idx in case we ever start trimming entries out
2609 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002610 }
2611 else
2612 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002613 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002614
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002615 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002616 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002617 // Copy the size of the function into the original STAB entry so we don't have
2618 // to hunt for it later
2619 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2620 N_FUN_indexes.pop_back();
2621 // We don't really need the end function STAB as it contains the size which
2622 // we already placed with the original symbol, so don't add it if we want a
2623 // minimal symbol table
2624 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002625 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002626 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002627 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002628 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002629
Jason Molenda9badb6c2013-03-06 23:19:17 +00002630 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002631 // N_STSYM -- static symbol: name,,n_sect,type,address
2632 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2633 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2634 type = eSymbolTypeData;
2635 break;
2636
2637 case StabLocalCommon:
2638 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2639 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2640 type = eSymbolTypeCommonBlock;
2641 break;
2642
2643 case StabBeginSymbol:
2644 // N_BNSYM
2645 // We use the current number of symbols in the symbol table in lieu of
2646 // using nlist_idx in case we ever start trimming entries out
2647 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002648 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002649 // Skip these if we want minimal symbol tables
2650 add_nlist = false;
2651 }
2652 else
2653 {
2654 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2655 N_NSYM_indexes.push_back(sym_idx);
2656 type = eSymbolTypeScopeBegin;
2657 }
2658 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002659
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002660 case StabEndSymbol:
2661 // N_ENSYM
2662 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2663 // so that we can always skip the entire symbol if we need to navigate
2664 // more quickly at the source level when parsing STABS
2665 if (minimize)
2666 {
2667 // Skip these if we want minimal symbol tables
2668 add_nlist = false;
2669 }
2670 else
2671 {
2672 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002673 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002674 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2675 symbol_ptr->SetByteSize(sym_idx + 1);
2676 symbol_ptr->SetSizeIsSibling(true);
2677 N_NSYM_indexes.pop_back();
2678 }
2679 type = eSymbolTypeScopeEnd;
2680 }
2681 break;
2682
2683
2684 case StabSourceFileOptions:
2685 // N_OPT - emitted with gcc2_compiled and in gcc source
2686 type = eSymbolTypeCompiler;
2687 break;
2688
2689 case StabRegisterSymbol:
2690 // N_RSYM - register sym: name,,NO_SECT,type,register
2691 type = eSymbolTypeVariable;
2692 break;
2693
2694 case StabSourceLine:
2695 // N_SLINE - src line: 0,,n_sect,linenumber,address
2696 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2697 type = eSymbolTypeLineEntry;
2698 break;
2699
2700 case StabStructureType:
2701 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2702 type = eSymbolTypeVariableType;
2703 break;
2704
2705 case StabSourceFileName:
2706 // N_SO - source file name
2707 type = eSymbolTypeSourceFile;
2708 if (symbol_name == NULL)
2709 {
2710 if (minimize)
2711 add_nlist = false;
2712 if (N_SO_index != UINT32_MAX)
2713 {
2714 // Set the size of the N_SO to the terminating index of this N_SO
2715 // so that we can always skip the entire N_SO if we need to navigate
2716 // more quickly at the source level when parsing STABS
2717 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2718 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2719 symbol_ptr->SetSizeIsSibling(true);
2720 }
2721 N_NSYM_indexes.clear();
2722 N_INCL_indexes.clear();
2723 N_BRAC_indexes.clear();
2724 N_COMM_indexes.clear();
2725 N_FUN_indexes.clear();
2726 N_SO_index = UINT32_MAX;
2727 }
2728 else
2729 {
2730 // We use the current number of symbols in the symbol table in lieu of
2731 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002732 const bool N_SO_has_full_path = symbol_name[0] == '/';
2733 if (N_SO_has_full_path)
2734 {
2735 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2736 {
2737 // We have two consecutive N_SO entries where the first contains a directory
2738 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002739 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002740 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2741 add_nlist = false;
2742 }
2743 else
2744 {
2745 // This is the first entry in a N_SO that contains a directory or
2746 // a full path to the source file
2747 N_SO_index = sym_idx;
2748 }
2749 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002750 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2751 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002752 // This is usually the second N_SO entry that contains just the filename,
2753 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002754 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2755 if (so_path && so_path[0])
2756 {
2757 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002758 const size_t double_slash_pos = full_so_path.find("//");
2759 if (double_slash_pos != std::string::npos)
2760 {
2761 // The linker has been generating bad N_SO entries with doubled up paths
2762 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2763 // and the second is the directory for the source file so you end up with
2764 // a path that looks like "/tmp/src//tmp/src/"
2765 FileSpec so_dir(so_path, false);
2766 if (!so_dir.Exists())
2767 {
2768 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2769 if (so_dir.Exists())
2770 {
2771 // Trim off the incorrect path
2772 full_so_path.erase(0, double_slash_pos + 1);
2773 }
2774 }
2775 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002776 if (*full_so_path.rbegin() != '/')
2777 full_so_path += '/';
2778 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002779 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002780 add_nlist = false;
2781 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2782 }
2783 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002784 else
2785 {
2786 // This could be a relative path to a N_SO
2787 N_SO_index = sym_idx;
2788 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002789 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002790
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002791 break;
2792
2793 case StabObjectFileName:
2794 // N_OSO - object file name: name,,0,0,st_mtime
2795 type = eSymbolTypeObjectFile;
2796 break;
2797
2798 case StabLocalSymbol:
2799 // N_LSYM - local sym: name,,NO_SECT,type,offset
2800 type = eSymbolTypeLocal;
2801 break;
2802
2803 //----------------------------------------------------------------------
2804 // INCL scopes
2805 //----------------------------------------------------------------------
2806 case StabBeginIncludeFileName:
2807 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2808 // We use the current number of symbols in the symbol table in lieu of
2809 // using nlist_idx in case we ever start trimming entries out
2810 N_INCL_indexes.push_back(sym_idx);
2811 type = eSymbolTypeScopeBegin;
2812 break;
2813
2814 case StabEndIncludeFile:
2815 // N_EINCL - include file end: name,,NO_SECT,0,0
2816 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2817 // so that we can always skip the entire symbol if we need to navigate
2818 // more quickly at the source level when parsing STABS
2819 if ( !N_INCL_indexes.empty() )
2820 {
2821 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2822 symbol_ptr->SetByteSize(sym_idx + 1);
2823 symbol_ptr->SetSizeIsSibling(true);
2824 N_INCL_indexes.pop_back();
2825 }
2826 type = eSymbolTypeScopeEnd;
2827 break;
2828
2829 case StabIncludeFileName:
2830 // N_SOL - #included file name: name,,n_sect,0,address
2831 type = eSymbolTypeHeaderFile;
2832
2833 // We currently don't use the header files on darwin
2834 if (minimize)
2835 add_nlist = false;
2836 break;
2837
Jason Molenda9badb6c2013-03-06 23:19:17 +00002838 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002839 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2840 type = eSymbolTypeCompiler;
2841 break;
2842
2843 case StabCompilerVersion:
2844 // N_VERSION - compiler version: name,,NO_SECT,0,0
2845 type = eSymbolTypeCompiler;
2846 break;
2847
2848 case StabCompilerOptLevel:
2849 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2850 type = eSymbolTypeCompiler;
2851 break;
2852
2853 case StabParameter:
2854 // N_PSYM - parameter: name,,NO_SECT,type,offset
2855 type = eSymbolTypeVariable;
2856 break;
2857
2858 case StabAlternateEntry:
2859 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2860 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2861 type = eSymbolTypeLineEntry;
2862 break;
2863
2864 //----------------------------------------------------------------------
2865 // Left and Right Braces
2866 //----------------------------------------------------------------------
2867 case StabLeftBracket:
2868 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2869 // We use the current number of symbols in the symbol table in lieu of
2870 // using nlist_idx in case we ever start trimming entries out
2871 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2872 N_BRAC_indexes.push_back(sym_idx);
2873 type = eSymbolTypeScopeBegin;
2874 break;
2875
2876 case StabRightBracket:
2877 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2878 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2879 // so that we can always skip the entire symbol if we need to navigate
2880 // more quickly at the source level when parsing STABS
2881 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2882 if ( !N_BRAC_indexes.empty() )
2883 {
2884 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2885 symbol_ptr->SetByteSize(sym_idx + 1);
2886 symbol_ptr->SetSizeIsSibling(true);
2887 N_BRAC_indexes.pop_back();
2888 }
2889 type = eSymbolTypeScopeEnd;
2890 break;
2891
2892 case StabDeletedIncludeFile:
2893 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2894 type = eSymbolTypeHeaderFile;
2895 break;
2896
2897 //----------------------------------------------------------------------
2898 // COMM scopes
2899 //----------------------------------------------------------------------
2900 case StabBeginCommon:
2901 // N_BCOMM - begin common: name,,NO_SECT,0,0
2902 // We use the current number of symbols in the symbol table in lieu of
2903 // using nlist_idx in case we ever start trimming entries out
2904 type = eSymbolTypeScopeBegin;
2905 N_COMM_indexes.push_back(sym_idx);
2906 break;
2907
2908 case StabEndCommonLocal:
2909 // N_ECOML - end common (local name): 0,,n_sect,0,address
2910 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2911 // Fall through
2912
2913 case StabEndCommon:
2914 // N_ECOMM - end common: name,,n_sect,0,0
2915 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2916 // so that we can always skip the entire symbol if we need to navigate
2917 // more quickly at the source level when parsing STABS
2918 if ( !N_COMM_indexes.empty() )
2919 {
2920 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2921 symbol_ptr->SetByteSize(sym_idx + 1);
2922 symbol_ptr->SetSizeIsSibling(true);
2923 N_COMM_indexes.pop_back();
2924 }
2925 type = eSymbolTypeScopeEnd;
2926 break;
2927
2928 case StabLength:
2929 // N_LENG - second stab entry with length information
2930 type = eSymbolTypeAdditional;
2931 break;
2932
2933 default: break;
2934 }
2935 }
2936 else
2937 {
2938 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2939 uint8_t n_type = NlistMaskType & nlist.n_type;
2940 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2941
2942 switch (n_type)
2943 {
2944 case NListTypeIndirect: // N_INDR - Fall through
2945 case NListTypePreboundUndefined:// N_PBUD - Fall through
2946 case NListTypeUndefined: // N_UNDF
2947 type = eSymbolTypeUndefined;
2948 break;
2949
2950 case NListTypeAbsolute: // N_ABS
2951 type = eSymbolTypeAbsolute;
2952 break;
2953
2954 case NListTypeSection: // N_SECT
2955 {
2956 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2957
Sean Callananb386d822012-08-09 00:50:26 +00002958 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002959 {
2960 // TODO: warn about this?
2961 add_nlist = false;
2962 break;
2963 }
2964
2965 if (TEXT_eh_frame_sectID == nlist.n_sect)
2966 {
2967 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00002968 }
2969 else
2970 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002971 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2972
2973 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00002974 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002975 case SectionTypeRegular: break; // regular section
2976 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2977 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2978 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2979 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2980 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2981 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2982 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2983 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2984 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2985 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2986 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2987 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2988 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2989 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2990 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2991 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2992 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002993 }
Chris Lattner24943d22010-06-08 16:52:24 +00002994
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002995 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002996 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002997 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2998 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00002999 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003000 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3001 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003002 SectionAttrSytemSomeInstructions))
3003 type = eSymbolTypeData;
3004 else
3005 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00003006 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003007 else
3008 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003009 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003010 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00003011 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003012 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00003013
Jason Molenda9badb6c2013-03-06 23:19:17 +00003014 if (symbol_name &&
3015 symbol_name[0] == '_' &&
3016 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003017 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00003018 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003019 llvm::StringRef symbol_name_ref(symbol_name);
3020 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3021 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3022 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3023 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00003024 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003025 symbol_name_non_abi_mangled = symbol_name + 1;
3026 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3027 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003028 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003029 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003030 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003031 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003032 symbol_name_non_abi_mangled = symbol_name + 1;
3033 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3034 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003035 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003036 }
3037 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3038 {
3039 symbol_name_non_abi_mangled = symbol_name + 1;
3040 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3041 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003042 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003043 }
3044 }
3045 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003046 else
3047 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3048 {
3049 type = eSymbolTypeException;
3050 }
3051 else
3052 {
3053 type = eSymbolTypeData;
3054 }
3055 }
3056 else
3057 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3058 {
3059 type = eSymbolTypeTrampoline;
3060 }
3061 else
3062 if (symbol_section->IsDescendant(objc_section_sp.get()))
3063 {
3064 type = eSymbolTypeRuntime;
3065 if (symbol_name && symbol_name[0] == '.')
3066 {
3067 llvm::StringRef symbol_name_ref(symbol_name);
3068 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3069 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3070 {
3071 symbol_name_non_abi_mangled = symbol_name;
3072 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3073 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003074 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003075 }
3076 }
Chris Lattner24943d22010-06-08 16:52:24 +00003077 }
3078 }
3079 }
3080 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003081 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003082 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003083 }
3084
3085 if (add_nlist)
3086 {
3087 uint64_t symbol_value = nlist.n_value;
3088 bool symbol_name_is_mangled = false;
3089
3090 if (symbol_name_non_abi_mangled)
3091 {
Greg Claytonc0240042012-07-18 23:18:10 +00003092 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3093 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003094 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003095 else
3096 {
3097 if (symbol_name && symbol_name[0] == '_')
3098 {
3099 symbol_name_is_mangled = symbol_name[1] == '_';
3100 symbol_name++; // Skip the leading underscore
3101 }
3102
3103 if (symbol_name)
3104 {
Greg Claytonc0240042012-07-18 23:18:10 +00003105 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003106 }
3107 }
3108
3109 if (is_debug == false)
3110 {
3111 if (type == eSymbolTypeCode)
3112 {
3113 // See if we can find a N_FUN entry for any code symbols.
3114 // If we do find a match, and the name matches, then we
3115 // can merge the two into just the function symbol to avoid
3116 // duplicate entries in the symbol table
3117 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3118 if (pos != N_FUN_addr_to_sym_idx.end())
3119 {
3120 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3121 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3122 {
3123 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3124 // We just need the flags from the linker symbol, so put these flags
3125 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3126 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3127 sym[sym_idx].Clear();
3128 continue;
3129 }
3130 }
3131 }
3132 else if (type == eSymbolTypeData)
3133 {
3134 // See if we can find a N_STSYM entry for any data symbols.
3135 // If we do find a match, and the name matches, then we
3136 // can merge the two into just the Static symbol to avoid
3137 // duplicate entries in the symbol table
3138 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3139 if (pos != N_STSYM_addr_to_sym_idx.end())
3140 {
3141 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3142 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3143 {
3144 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3145 // We just need the flags from the linker symbol, so put these flags
3146 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3147 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3148 sym[sym_idx].Clear();
3149 continue;
3150 }
3151 }
3152 }
3153 }
3154 if (symbol_section)
3155 {
3156 const addr_t section_file_addr = symbol_section->GetFileAddress();
3157 if (symbol_byte_size == 0 && function_starts_count > 0)
3158 {
Greg Claytond2653c22012-03-14 01:53:24 +00003159 addr_t symbol_lookup_file_addr = nlist.n_value;
3160 // Do an exact address match for non-ARM addresses, else get the closest since
3161 // the symbol might be a thumb symbol which has an address with bit zero set
3162 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3163 if (is_arm && func_start_entry)
3164 {
3165 // Verify that the function start address is the symbol address (ARM)
3166 // or the symbol address + 1 (thumb)
3167 if (func_start_entry->addr != symbol_lookup_file_addr &&
3168 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3169 {
3170 // Not the right entry, NULL it out...
3171 func_start_entry = NULL;
3172 }
3173 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003174 if (func_start_entry)
3175 {
3176 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003177
Greg Claytond2653c22012-03-14 01:53:24 +00003178 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003179 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003180 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003181
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003182 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3183 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3184 if (next_func_start_entry)
3185 {
Greg Claytond2653c22012-03-14 01:53:24 +00003186 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3187 // Be sure the clear the Thumb address bit when we calculate the size
3188 // from the current and next address
3189 if (is_arm)
3190 next_symbol_file_addr &= 0xfffffffffffffffeull;
3191 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 +00003192 }
3193 else
3194 {
Greg Claytond2653c22012-03-14 01:53:24 +00003195 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003196 }
3197 }
3198 }
3199 symbol_value -= section_file_addr;
3200 }
3201
3202 sym[sym_idx].SetID (nlist_idx);
3203 sym[sym_idx].SetType (type);
3204 sym[sym_idx].GetAddress().SetSection (symbol_section);
3205 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3206 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3207
3208 if (symbol_byte_size > 0)
3209 sym[sym_idx].SetByteSize(symbol_byte_size);
3210
Greg Clayton01e6a582012-11-27 01:52:16 +00003211 if (demangled_is_synthesized)
3212 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3213
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003214 ++sym_idx;
3215 }
3216 else
3217 {
3218 sym[sym_idx].Clear();
3219 }
3220
3221 }
3222
3223 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3224 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3225 // such entries by figuring out what the address for the global is by looking up this non-STAB
3226 // entry and copying the value into the debug symbol's value to save us the hassle in the
3227 // debug symbol parser.
3228
3229 Symbol *global_symbol = NULL;
3230 for (nlist_idx = 0;
3231 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3232 nlist_idx++)
3233 {
3234 if (global_symbol->GetAddress().GetFileAddress() == 0)
3235 {
3236 std::vector<uint32_t> indexes;
3237 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3238 {
3239 std::vector<uint32_t>::const_iterator pos;
3240 std::vector<uint32_t>::const_iterator end = indexes.end();
3241 for (pos = indexes.begin(); pos != end; ++pos)
3242 {
3243 symbol_ptr = symtab->SymbolAtIndex(*pos);
3244 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3245 {
3246 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3247 break;
3248 }
3249 }
3250 }
Chris Lattner24943d22010-06-08 16:52:24 +00003251 }
3252 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003253
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003254 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3255
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003256 if (function_starts_count > 0)
3257 {
3258 char synthetic_function_symbol[PATH_MAX];
3259 uint32_t num_synthetic_function_symbols = 0;
3260 for (i=0; i<function_starts_count; ++i)
3261 {
3262 if (function_starts.GetEntryRef (i).data == false)
3263 ++num_synthetic_function_symbols;
3264 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003265
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003266 if (num_synthetic_function_symbols > 0)
3267 {
3268 if (num_syms < sym_idx + num_synthetic_function_symbols)
3269 {
3270 num_syms = sym_idx + num_synthetic_function_symbols;
3271 sym = symtab->Resize (num_syms);
3272 }
3273 uint32_t synthetic_function_symbol_idx = 0;
3274 for (i=0; i<function_starts_count; ++i)
3275 {
3276 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3277 if (func_start_entry->data == false)
3278 {
Greg Claytond2653c22012-03-14 01:53:24 +00003279 addr_t symbol_file_addr = func_start_entry->addr;
3280 uint32_t symbol_flags = 0;
3281 if (is_arm)
3282 {
3283 if (symbol_file_addr & 1)
3284 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3285 symbol_file_addr &= 0xfffffffffffffffeull;
3286 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003287 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003288 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003289 {
3290 SectionSP symbol_section (symbol_addr.GetSection());
3291 uint32_t symbol_byte_size = 0;
3292 if (symbol_section)
3293 {
3294 const addr_t section_file_addr = symbol_section->GetFileAddress();
3295 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3296 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3297 if (next_func_start_entry)
3298 {
Greg Claytond2653c22012-03-14 01:53:24 +00003299 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3300 if (is_arm)
3301 next_symbol_file_addr &= 0xfffffffffffffffeull;
3302 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 +00003303 }
3304 else
3305 {
Greg Claytond2653c22012-03-14 01:53:24 +00003306 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003307 }
3308 snprintf (synthetic_function_symbol,
3309 sizeof(synthetic_function_symbol),
3310 "___lldb_unnamed_function%u$$%s",
3311 ++synthetic_function_symbol_idx,
3312 module_sp->GetFileSpec().GetFilename().GetCString());
3313 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003314 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003315 sym[sym_idx].SetType (eSymbolTypeCode);
3316 sym[sym_idx].SetIsSynthetic (true);
3317 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003318 if (symbol_flags)
3319 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003320 if (symbol_byte_size)
3321 sym[sym_idx].SetByteSize (symbol_byte_size);
3322 ++sym_idx;
3323 }
3324 }
3325 }
3326 }
3327 }
3328 }
3329
3330 // Trim our symbols down to just what we ended up with after
3331 // removing any symbols.
3332 if (sym_idx < num_syms)
3333 {
3334 num_syms = sym_idx;
3335 sym = symtab->Resize (num_syms);
3336 }
3337
3338 // Now synthesize indirect symbols
3339 if (m_dysymtab.nindirectsyms != 0)
3340 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003341 if (indirect_symbol_index_data.GetByteSize())
3342 {
3343 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3344
3345 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3346 {
3347 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3348 {
3349 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3350 if (symbol_stub_byte_size == 0)
3351 continue;
3352
3353 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3354
3355 if (num_symbol_stubs == 0)
3356 continue;
3357
3358 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3359 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3360 {
3361 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3362 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 +00003363 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003364 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3365 {
3366 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3367 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3368 continue;
3369
3370 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3371 Symbol *stub_symbol = NULL;
3372 if (index_pos != end_index_pos)
3373 {
3374 // We have a remapping from the original nlist index to
3375 // a current symbol index, so just look this up by index
3376 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3377 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003378 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003379 {
3380 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003381 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003382 // S_SYMBOL_STUBS
3383 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3384 }
3385
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003386 if (stub_symbol)
3387 {
3388 Address so_addr(symbol_stub_addr, section_list);
3389
3390 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3391 {
3392 // Change the external symbol into a trampoline that makes sense
3393 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3394 // can re-use them so we don't have to make up a synthetic symbol
3395 // for no good reason.
3396 stub_symbol->SetType (eSymbolTypeTrampoline);
3397 stub_symbol->SetExternal (false);
3398 stub_symbol->GetAddress() = so_addr;
3399 stub_symbol->SetByteSize (symbol_stub_byte_size);
3400 }
3401 else
3402 {
3403 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003404 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003405 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003406 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003407 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003408 stub_symbol = NULL; // this pointer no longer valid
3409 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003410 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003411 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003412 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3413 sym[sym_idx].SetIsSynthetic (true);
3414 sym[sym_idx].GetAddress() = so_addr;
3415 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3416 ++sym_idx;
3417 }
3418 }
Greg Claytond4330e62012-09-05 01:38:55 +00003419 else
3420 {
3421 if (log)
3422 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3423 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003424 }
3425 }
3426 }
3427 }
3428 }
3429 }
3430 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003431 }
3432 return 0;
3433}
3434
3435
3436void
3437ObjectFileMachO::Dump (Stream *s)
3438{
Greg Clayton9482f052012-03-13 23:14:29 +00003439 ModuleSP module_sp(GetModule());
3440 if (module_sp)
3441 {
3442 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3443 s->Printf("%p: ", this);
3444 s->Indent();
3445 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3446 s->PutCString("ObjectFileMachO64");
3447 else
3448 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003449
Greg Clayton9482f052012-03-13 23:14:29 +00003450 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003451
Greg Clayton9482f052012-03-13 23:14:29 +00003452 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003453
Greg Clayton9482f052012-03-13 23:14:29 +00003454 if (m_sections_ap.get())
3455 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003456
Greg Clayton9482f052012-03-13 23:14:29 +00003457 if (m_symtab_ap.get())
3458 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3459 }
Chris Lattner24943d22010-06-08 16:52:24 +00003460}
3461
3462
3463bool
Greg Clayton0467c782011-02-04 18:53:10 +00003464ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003465{
Greg Clayton9482f052012-03-13 23:14:29 +00003466 ModuleSP module_sp(GetModule());
3467 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003468 {
Greg Clayton9482f052012-03-13 23:14:29 +00003469 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3470 struct uuid_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003471 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003472 uint32_t i;
3473 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003474 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003475 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003476 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3477 break;
3478
3479 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00003480 {
Greg Clayton9482f052012-03-13 23:14:29 +00003481 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003482
Greg Clayton9482f052012-03-13 23:14:29 +00003483 if (uuid_bytes)
3484 {
Sean Callanana5689d52012-07-12 18:04:03 +00003485 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3486 // We pretend these object files have no UUID to prevent crashing.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003487
Sean Callanana5689d52012-07-12 18:04:03 +00003488 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3489 0x3b, 0xa8,
3490 0x4b, 0x16,
3491 0xb6, 0xa4,
3492 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
Jason Molenda9badb6c2013-03-06 23:19:17 +00003493
Sean Callanana5689d52012-07-12 18:04:03 +00003494 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3495 return false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003496
Greg Clayton9482f052012-03-13 23:14:29 +00003497 uuid->SetBytes (uuid_bytes);
3498 return true;
3499 }
3500 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003501 }
Greg Clayton9482f052012-03-13 23:14:29 +00003502 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003503 }
Chris Lattner24943d22010-06-08 16:52:24 +00003504 }
3505 return false;
3506}
3507
3508
3509uint32_t
3510ObjectFileMachO::GetDependentModules (FileSpecList& files)
3511{
Chris Lattner24943d22010-06-08 16:52:24 +00003512 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003513 ModuleSP module_sp(GetModule());
3514 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003515 {
Greg Clayton9482f052012-03-13 23:14:29 +00003516 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3517 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003518 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003519 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3520 uint32_t i;
3521 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003522 {
Greg Clayton9482f052012-03-13 23:14:29 +00003523 const uint32_t cmd_offset = offset;
3524 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3525 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003526
Greg Clayton9482f052012-03-13 23:14:29 +00003527 switch (load_cmd.cmd)
3528 {
3529 case LoadCommandDylibLoad:
3530 case LoadCommandDylibLoadWeak:
3531 case LoadCommandDylibReexport:
3532 case LoadCommandDynamicLinkerLoad:
3533 case LoadCommandFixedVMShlibLoad:
3534 case LoadCommandDylibLoadUpward:
3535 {
3536 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3537 const char *path = m_data.PeekCStr(name_offset);
3538 // Skip any path that starts with '@' since these are usually:
3539 // @executable_path/.../file
3540 // @rpath/.../file
3541 if (path && path[0] != '@')
3542 {
3543 FileSpec file_spec(path, resolve_path);
3544 if (files.AppendIfUnique(file_spec))
3545 count++;
3546 }
3547 }
3548 break;
3549
3550 default:
3551 break;
3552 }
3553 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003554 }
Chris Lattner24943d22010-06-08 16:52:24 +00003555 }
3556 return count;
3557}
3558
Jim Ingham28775942011-03-07 23:44:08 +00003559lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003560ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003561{
3562 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3563 // is initialized to an invalid address, so we can just return that.
3564 // 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 +00003565
Jim Ingham28775942011-03-07 23:44:08 +00003566 if (!IsExecutable() || m_entry_point_address.IsValid())
3567 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003568
3569 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003570 // /usr/include/mach-o.h, but it is basically:
3571 //
3572 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3573 // uint32_t count - this is the count of longs in the thread state data
3574 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3575 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003576 //
Jim Ingham28775942011-03-07 23:44:08 +00003577 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3578 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3579 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3580 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3581 //
3582 // For now we hard-code the offsets and flavors we need:
3583 //
3584 //
3585
Greg Clayton9482f052012-03-13 23:14:29 +00003586 ModuleSP module_sp(GetModule());
3587 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003588 {
Greg Clayton9482f052012-03-13 23:14:29 +00003589 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3590 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003591 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003592 uint32_t i;
3593 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3594 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003595
Greg Clayton9482f052012-03-13 23:14:29 +00003596 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003597 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003598 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003599 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3600 break;
3601
3602 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003603 {
Greg Clayton9482f052012-03-13 23:14:29 +00003604 case LoadCommandUnixThread:
3605 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003606 {
Greg Clayton9482f052012-03-13 23:14:29 +00003607 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003608 {
Greg Clayton9482f052012-03-13 23:14:29 +00003609 uint32_t flavor = m_data.GetU32(&offset);
3610 uint32_t count = m_data.GetU32(&offset);
3611 if (count == 0)
3612 {
3613 // We've gotten off somehow, log and exit;
3614 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003615 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003616
Greg Clayton9482f052012-03-13 23:14:29 +00003617 switch (m_header.cputype)
3618 {
3619 case llvm::MachO::CPUTypeARM:
3620 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3621 {
3622 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3623 start_address = m_data.GetU32(&offset);
3624 done = true;
3625 }
Jim Ingham28775942011-03-07 23:44:08 +00003626 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003627 case llvm::MachO::CPUTypeI386:
3628 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3629 {
3630 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3631 start_address = m_data.GetU32(&offset);
3632 done = true;
3633 }
3634 break;
3635 case llvm::MachO::CPUTypeX86_64:
3636 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3637 {
3638 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3639 start_address = m_data.GetU64(&offset);
3640 done = true;
3641 }
3642 break;
3643 default:
3644 return m_entry_point_address;
3645 }
3646 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3647 if (done)
3648 break;
3649 offset += count * 4;
3650 }
Jim Ingham28775942011-03-07 23:44:08 +00003651 }
Greg Clayton9482f052012-03-13 23:14:29 +00003652 break;
3653 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003654 {
Greg Clayton9482f052012-03-13 23:14:29 +00003655 ConstString text_segment_name ("__TEXT");
3656 uint64_t entryoffset = m_data.GetU64(&offset);
3657 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3658 if (text_segment_sp)
3659 {
3660 done = true;
3661 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3662 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003663 }
Greg Clayton9482f052012-03-13 23:14:29 +00003664
3665 default:
3666 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003667 }
Greg Clayton9482f052012-03-13 23:14:29 +00003668 if (done)
3669 break;
Jim Ingham28775942011-03-07 23:44:08 +00003670
Greg Clayton9482f052012-03-13 23:14:29 +00003671 // Go to the next load command:
3672 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003673 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003674
Greg Clayton9482f052012-03-13 23:14:29 +00003675 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003676 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003677 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003678 // of this ObjectFile:
3679 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003680 {
Greg Clayton9482f052012-03-13 23:14:29 +00003681 m_entry_point_address.Clear();
3682 }
3683 }
3684 else
3685 {
3686 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3687 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003688
Greg Clayton9482f052012-03-13 23:14:29 +00003689 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003690
Greg Clayton9482f052012-03-13 23:14:29 +00003691 if (module_sp)
3692 {
3693 SymbolContextList contexts;
3694 SymbolContext context;
3695 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3696 {
3697 if (contexts.GetContextAtIndex(0, context))
3698 m_entry_point_address = context.symbol->GetAddress();
3699 }
Greg Clayton3508c382012-02-24 01:59:29 +00003700 }
3701 }
Jim Ingham28775942011-03-07 23:44:08 +00003702 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003703
Jim Ingham28775942011-03-07 23:44:08 +00003704 return m_entry_point_address;
3705
3706}
3707
Greg Claytonb5a8f142012-02-05 02:38:54 +00003708lldb_private::Address
3709ObjectFileMachO::GetHeaderAddress ()
3710{
3711 lldb_private::Address header_addr;
3712 SectionList *section_list = GetSectionList();
3713 if (section_list)
3714 {
3715 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3716 if (text_segment_sp)
3717 {
Greg Clayton3508c382012-02-24 01:59:29 +00003718 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003719 header_addr.SetOffset (0);
3720 }
3721 }
3722 return header_addr;
3723}
3724
Greg Clayton46c9a352012-02-09 06:16:32 +00003725uint32_t
3726ObjectFileMachO::GetNumThreadContexts ()
3727{
Greg Clayton9482f052012-03-13 23:14:29 +00003728 ModuleSP module_sp(GetModule());
3729 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003730 {
Greg Clayton9482f052012-03-13 23:14:29 +00003731 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3732 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003733 {
Greg Clayton9482f052012-03-13 23:14:29 +00003734 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003735 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003736 FileRangeArray::Entry file_range;
3737 thread_command thread_cmd;
3738 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003739 {
Greg Clayton9482f052012-03-13 23:14:29 +00003740 const uint32_t cmd_offset = offset;
3741 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3742 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003743
Greg Clayton9482f052012-03-13 23:14:29 +00003744 if (thread_cmd.cmd == LoadCommandThread)
3745 {
3746 file_range.SetRangeBase (offset);
3747 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3748 m_thread_context_offsets.Append (file_range);
3749 }
3750 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003751 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003752 }
3753 }
3754 return m_thread_context_offsets.GetSize();
3755}
3756
3757lldb::RegisterContextSP
3758ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3759{
Greg Clayton46c9a352012-02-09 06:16:32 +00003760 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003761
Greg Clayton9482f052012-03-13 23:14:29 +00003762 ModuleSP module_sp(GetModule());
3763 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003764 {
Greg Clayton9482f052012-03-13 23:14:29 +00003765 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3766 if (!m_thread_context_offsets_valid)
3767 GetNumThreadContexts ();
3768
3769 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003770 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003771 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003772
3773 DataExtractor data (m_data,
3774 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003775 thread_context_file_range->GetByteSize());
3776
3777 switch (m_header.cputype)
3778 {
3779 case llvm::MachO::CPUTypeARM:
3780 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3781 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003782
Jim Ingham6f01c932012-10-12 17:34:26 +00003783 case llvm::MachO::CPUTypeI386:
3784 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3785 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003786
Jim Ingham6f01c932012-10-12 17:34:26 +00003787 case llvm::MachO::CPUTypeX86_64:
3788 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3789 break;
3790 }
Greg Clayton9482f052012-03-13 23:14:29 +00003791 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003792 }
3793 return reg_ctx_sp;
3794}
3795
Greg Claytonb5a8f142012-02-05 02:38:54 +00003796
Greg Claytonca319972011-07-09 00:41:34 +00003797ObjectFile::Type
3798ObjectFileMachO::CalculateType()
3799{
3800 switch (m_header.filetype)
3801 {
3802 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3803 if (GetAddressByteSize () == 4)
3804 {
3805 // 32 bit kexts are just object files, but they do have a valid
3806 // UUID load command.
3807 UUID uuid;
3808 if (GetUUID(&uuid))
3809 {
3810 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003811 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003812 // "OSKextGetCurrentIdentifier" as this is required of kexts
3813 if (m_strata == eStrataInvalid)
3814 m_strata = eStrataKernel;
3815 return eTypeSharedLibrary;
3816 }
3817 }
3818 return eTypeObjectFile;
3819
3820 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3821 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3822 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3823 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3824 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3825 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3826 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3827 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3828 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3829 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3830 default:
3831 break;
3832 }
3833 return eTypeUnknown;
3834}
3835
3836ObjectFile::Strata
3837ObjectFileMachO::CalculateStrata()
3838{
3839 switch (m_header.filetype)
3840 {
3841 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3842 {
3843 // 32 bit kexts are just object files, but they do have a valid
3844 // UUID load command.
3845 UUID uuid;
3846 if (GetUUID(&uuid))
3847 {
3848 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003849 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003850 // "OSKextGetCurrentIdentifier" as this is required of kexts
3851 if (m_type == eTypeInvalid)
3852 m_type = eTypeSharedLibrary;
3853
3854 return eStrataKernel;
3855 }
3856 }
3857 return eStrataUnknown;
3858
3859 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3860 // Check for the MH_DYLDLINK bit in the flags
3861 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003862 {
Greg Claytonca319972011-07-09 00:41:34 +00003863 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003864 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003865 else
Sean Callananac725af2012-02-10 20:22:35 +00003866 {
3867 SectionList *section_list = GetSectionList();
3868 if (section_list)
3869 {
3870 static ConstString g_kld_section_name ("__KLD");
3871 if (section_list->FindSectionByName(g_kld_section_name))
3872 return eStrataKernel;
3873 }
3874 }
3875 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00003876
3877 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
3878 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00003879 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00003880 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
3881 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
3882 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
3883 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
3884 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
3885 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
3886 default:
3887 break;
3888 }
3889 return eStrataUnknown;
3890}
3891
3892
Greg Clayton49f4bf22012-02-22 19:41:02 +00003893uint32_t
3894ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
3895{
Greg Clayton9482f052012-03-13 23:14:29 +00003896 ModuleSP module_sp(GetModule());
3897 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003898 {
Greg Clayton9482f052012-03-13 23:14:29 +00003899 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3900 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003901 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003902 uint32_t version_cmd = 0;
3903 uint64_t version = 0;
3904 uint32_t i;
3905 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003906 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003907 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003908 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3909 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003910
Greg Clayton9482f052012-03-13 23:14:29 +00003911 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003912 {
Greg Clayton9482f052012-03-13 23:14:29 +00003913 if (version_cmd == 0)
3914 {
3915 version_cmd = load_cmd.cmd;
3916 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
3917 break;
3918 version = load_cmd.dylib.current_version;
3919 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003920 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00003921 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00003922 }
Greg Clayton9482f052012-03-13 23:14:29 +00003923 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003924 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003925
Greg Clayton9482f052012-03-13 23:14:29 +00003926 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003927 {
Greg Clayton9482f052012-03-13 23:14:29 +00003928 if (versions != NULL && num_versions > 0)
3929 {
3930 if (num_versions > 0)
3931 versions[0] = (version & 0xFFFF0000ull) >> 16;
3932 if (num_versions > 1)
3933 versions[1] = (version & 0x0000FF00ull) >> 8;
3934 if (num_versions > 2)
3935 versions[2] = (version & 0x000000FFull);
3936 // Fill in an remaining version numbers with invalid values
3937 for (i=3; i<num_versions; ++i)
3938 versions[i] = UINT32_MAX;
3939 }
3940 // The LC_ID_DYLIB load command has a version with 3 version numbers
3941 // in it, so always return 3
3942 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003943 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00003944 }
3945 return false;
3946}
3947
Chris Lattner24943d22010-06-08 16:52:24 +00003948bool
Greg Clayton395fc332011-02-15 21:59:32 +00003949ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00003950{
Greg Clayton9482f052012-03-13 23:14:29 +00003951 ModuleSP module_sp(GetModule());
3952 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003953 {
Greg Clayton9482f052012-03-13 23:14:29 +00003954 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3955 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003956
Greg Clayton9482f052012-03-13 23:14:29 +00003957 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00003958 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00003959 // unknown to make sure we use the DynamicLoaderStatic()...
3960 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
3961 {
3962 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
3963 }
3964 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003965 }
Greg Clayton9482f052012-03-13 23:14:29 +00003966 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003967}
3968
3969
Jason Molenda45c75502013-04-16 06:24:42 +00003970UUID
3971ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
3972{
3973 UUID uuid;
3974 if (process)
3975 {
3976 addr_t all_image_infos = process->GetImageInfoAddress();
3977
3978 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
3979 // or it may be the address of the dyld_all_image_infos structure (want). The first four
3980 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
3981 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
3982
3983 Error err;
3984 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
3985 if (version_or_magic != -1
3986 && version_or_magic != HeaderMagic32
3987 && version_or_magic != HeaderMagic32Swapped
3988 && version_or_magic != HeaderMagic64
3989 && version_or_magic != HeaderMagic64Swapped
3990 && version_or_magic >= 13)
3991 {
3992 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
3993 int wordsize = process->GetAddressByteSize();
3994 if (wordsize == 8)
3995 {
3996 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
3997 }
3998 if (wordsize == 4)
3999 {
4000 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4001 }
4002 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4003 {
4004 uuid_t shared_cache_uuid;
4005 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4006 {
4007 uuid.SetBytes (shared_cache_uuid);
4008 }
4009 }
4010 }
4011 }
4012 return uuid;
4013}
4014
4015UUID
4016ObjectFileMachO::GetLLDBSharedCacheUUID ()
4017{
4018 UUID uuid;
4019#if defined (__APPLE__) && defined (__arm__)
4020 uint8_t *(*dyld_get_all_image_infos)(void);
4021 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4022 if (dyld_get_all_image_infos)
4023 {
4024 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4025 if (dyld_all_image_infos_address)
4026 {
4027 uint32_t version;
4028 memcpy (&version, dyld_all_image_infos_address, 4);
4029 if (version >= 13)
4030 {
4031 uint8_t *sharedCacheUUID_address = 0;
4032 sharedCacheUUID_address = dyld_all_image_infos_address + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4033 uuid.SetBytes (sharedCacheUUID_address);
4034 }
4035 }
4036 }
4037#endif
4038 return uuid;
4039}
4040
4041
Chris Lattner24943d22010-06-08 16:52:24 +00004042//------------------------------------------------------------------
4043// PluginInterface protocol
4044//------------------------------------------------------------------
4045const char *
4046ObjectFileMachO::GetPluginName()
4047{
4048 return "ObjectFileMachO";
4049}
4050
4051const char *
4052ObjectFileMachO::GetShortPluginName()
4053{
4054 return GetPluginNameStatic();
4055}
4056
4057uint32_t
4058ObjectFileMachO::GetPluginVersion()
4059{
4060 return 1;
4061}
4062