blob: 49441f45ce98187e481afad99ecb439d15e9c9c5 [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
Daniel Malea3e649052013-04-17 19:24:22 +000046#ifndef __APPLE__
47#include "Utility/UuidCompatibility.h"
48#endif
49
Chris Lattner24943d22010-06-08 16:52:24 +000050using namespace lldb;
51using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000052using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000053
Jason Molenda9badb6c2013-03-06 23:19:17 +000054class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
Greg Clayton46c9a352012-02-09 06:16:32 +000055{
56public:
57 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
58 RegisterContextDarwin_x86_64 (thread, 0)
59 {
60 SetRegisterDataFrom_LC_THREAD (data);
61 }
62
63 virtual void
64 InvalidateAllRegisters ()
65 {
66 // Do nothing... registers are always valid...
67 }
68
69 void
70 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
71 {
Greg Clayton36da2aa2013-01-25 18:06:21 +000072 lldb::offset_t offset = 0;
Greg Clayton46c9a352012-02-09 06:16:32 +000073 SetError (GPRRegSet, Read, -1);
74 SetError (FPURegSet, Read, -1);
75 SetError (EXCRegSet, Read, -1);
Greg Clayton9ce95382012-02-13 23:10:39 +000076 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +000077
Greg Clayton9ce95382012-02-13 23:10:39 +000078 while (!done)
Greg Clayton46c9a352012-02-09 06:16:32 +000079 {
Greg Clayton9ce95382012-02-13 23:10:39 +000080 int flavor = data.GetU32 (&offset);
81 if (flavor == 0)
82 done = true;
83 else
Greg Clayton46c9a352012-02-09 06:16:32 +000084 {
Greg Clayton9ce95382012-02-13 23:10:39 +000085 uint32_t i;
86 uint32_t count = data.GetU32 (&offset);
87 switch (flavor)
88 {
89 case GPRRegSet:
90 for (i=0; i<count; ++i)
91 (&gpr.rax)[i] = data.GetU64(&offset);
92 SetError (GPRRegSet, Read, 0);
93 done = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +000094
Greg Clayton9ce95382012-02-13 23:10:39 +000095 break;
96 case FPURegSet:
97 // TODO: fill in FPU regs....
98 //SetError (FPURegSet, Read, -1);
99 done = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000100
Greg Clayton9ce95382012-02-13 23:10:39 +0000101 break;
102 case EXCRegSet:
103 exc.trapno = data.GetU32(&offset);
104 exc.err = data.GetU32(&offset);
105 exc.faultvaddr = data.GetU64(&offset);
106 SetError (EXCRegSet, Read, 0);
107 done = true;
108 break;
109 case 7:
110 case 8:
111 case 9:
112 // fancy flavors that encapsulate of the the above
113 // falvors...
114 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000115
Greg Clayton9ce95382012-02-13 23:10:39 +0000116 default:
117 done = true;
118 break;
119 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000120 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000121 }
122 }
123protected:
124 virtual int
125 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
126 {
127 return 0;
128 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000129
Greg Clayton9ce95382012-02-13 23:10:39 +0000130 virtual int
131 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
132 {
133 return 0;
134 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000135
Greg Clayton9ce95382012-02-13 23:10:39 +0000136 virtual int
137 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
138 {
139 return 0;
140 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000141
Greg Clayton9ce95382012-02-13 23:10:39 +0000142 virtual int
143 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
144 {
145 return 0;
146 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000147
Greg Clayton9ce95382012-02-13 23:10:39 +0000148 virtual int
149 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
150 {
151 return 0;
152 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000153
Greg Clayton9ce95382012-02-13 23:10:39 +0000154 virtual int
155 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
156 {
157 return 0;
158 }
159};
Greg Clayton46c9a352012-02-09 06:16:32 +0000160
Greg Clayton9ce95382012-02-13 23:10:39 +0000161
Jason Molenda9badb6c2013-03-06 23:19:17 +0000162class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
Greg Clayton9ce95382012-02-13 23:10:39 +0000163{
164public:
165 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
166 RegisterContextDarwin_i386 (thread, 0)
167 {
168 SetRegisterDataFrom_LC_THREAD (data);
169 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000170
Greg Clayton9ce95382012-02-13 23:10:39 +0000171 virtual void
172 InvalidateAllRegisters ()
173 {
174 // Do nothing... registers are always valid...
175 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000176
Greg Clayton9ce95382012-02-13 23:10:39 +0000177 void
178 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
179 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000180 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000181 SetError (GPRRegSet, Read, -1);
182 SetError (FPURegSet, Read, -1);
183 SetError (EXCRegSet, Read, -1);
184 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000185
Greg Clayton9ce95382012-02-13 23:10:39 +0000186 while (!done)
187 {
188 int flavor = data.GetU32 (&offset);
189 if (flavor == 0)
190 done = true;
191 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000192 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000193 uint32_t i;
194 uint32_t count = data.GetU32 (&offset);
195 switch (flavor)
196 {
197 case GPRRegSet:
198 for (i=0; i<count; ++i)
199 (&gpr.eax)[i] = data.GetU32(&offset);
200 SetError (GPRRegSet, Read, 0);
201 done = true;
202
203 break;
204 case FPURegSet:
205 // TODO: fill in FPU regs....
206 //SetError (FPURegSet, Read, -1);
207 done = true;
208
209 break;
210 case EXCRegSet:
211 exc.trapno = data.GetU32(&offset);
212 exc.err = data.GetU32(&offset);
213 exc.faultvaddr = data.GetU32(&offset);
214 SetError (EXCRegSet, Read, 0);
215 done = true;
216 break;
217 case 7:
218 case 8:
219 case 9:
220 // fancy flavors that encapsulate of the the above
221 // falvors...
222 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000223
Greg Clayton9ce95382012-02-13 23:10:39 +0000224 default:
225 done = true;
226 break;
227 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000228 }
229 }
230 }
231protected:
232 virtual int
233 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
234 {
235 return 0;
236 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000237
Greg Clayton46c9a352012-02-09 06:16:32 +0000238 virtual int
239 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
240 {
241 return 0;
242 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000243
Greg Clayton46c9a352012-02-09 06:16:32 +0000244 virtual int
245 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
246 {
247 return 0;
248 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000249
Greg Clayton46c9a352012-02-09 06:16:32 +0000250 virtual int
251 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
252 {
253 return 0;
254 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000255
Greg Clayton46c9a352012-02-09 06:16:32 +0000256 virtual int
257 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
258 {
259 return 0;
260 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000261
Greg Clayton46c9a352012-02-09 06:16:32 +0000262 virtual int
263 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
264 {
265 return 0;
266 }
267};
268
Jason Molenda9badb6c2013-03-06 23:19:17 +0000269class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Clayton9ce95382012-02-13 23:10:39 +0000270{
271public:
272 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonb5431d02012-10-30 23:57:32 +0000273 RegisterContextDarwin_arm (thread, 0)
Greg Clayton9ce95382012-02-13 23:10:39 +0000274 {
275 SetRegisterDataFrom_LC_THREAD (data);
276 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000277
Greg Clayton9ce95382012-02-13 23:10:39 +0000278 virtual void
279 InvalidateAllRegisters ()
280 {
281 // Do nothing... registers are always valid...
282 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000283
Greg Clayton9ce95382012-02-13 23:10:39 +0000284 void
285 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
286 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000287 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000288 SetError (GPRRegSet, Read, -1);
289 SetError (FPURegSet, Read, -1);
290 SetError (EXCRegSet, Read, -1);
291 int flavor = data.GetU32 (&offset);
292 uint32_t count = data.GetU32 (&offset);
293 switch (flavor)
294 {
295 case GPRRegSet:
296 for (uint32_t i=0; i<count; ++i)
297 gpr.r[i] = data.GetU32(&offset);
298 SetError (GPRRegSet, Read, 0);
299 break;
300 case FPURegSet:
301 // TODO: fill in FPU regs....
302 //SetError (FPURegSet, Read, -1);
303 break;
304 case EXCRegSet:
305 exc.exception = data.GetU32(&offset);
306 exc.fsr = data.GetU32(&offset);
307 exc.far = data.GetU32(&offset);
308 SetError (EXCRegSet, Read, 0);
309 break;
310 }
311 }
312protected:
313 virtual int
314 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
315 {
316 return 0;
317 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000318
Greg Clayton9ce95382012-02-13 23:10:39 +0000319 virtual int
320 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
321 {
322 return 0;
323 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000324
Greg Clayton9ce95382012-02-13 23:10:39 +0000325 virtual int
326 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
327 {
328 return 0;
329 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000330
331 virtual int
332 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
333 {
334 return -1;
335 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000336
Greg Clayton9ce95382012-02-13 23:10:39 +0000337 virtual int
338 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
339 {
340 return 0;
341 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000342
Greg Clayton9ce95382012-02-13 23:10:39 +0000343 virtual int
344 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
345 {
346 return 0;
347 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000348
Greg Clayton9ce95382012-02-13 23:10:39 +0000349 virtual int
350 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
351 {
352 return 0;
353 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000354
Greg Claytonb5431d02012-10-30 23:57:32 +0000355 virtual int
356 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
357 {
358 return -1;
359 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000360};
361
Greg Claytonb1888f22011-03-19 01:12:21 +0000362#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000363
364void
365ObjectFileMachO::Initialize()
366{
367 PluginManager::RegisterPlugin (GetPluginNameStatic(),
368 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000369 CreateInstance,
370 CreateMemoryInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000371}
372
373void
374ObjectFileMachO::Terminate()
375{
376 PluginManager::UnregisterPlugin (CreateInstance);
377}
378
379
380const char *
381ObjectFileMachO::GetPluginNameStatic()
382{
383 return "object-file.mach-o";
384}
385
386const char *
387ObjectFileMachO::GetPluginDescriptionStatic()
388{
389 return "Mach-o object file reader (32 and 64 bit)";
390}
391
Chris Lattner24943d22010-06-08 16:52:24 +0000392ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000393ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
394 DataBufferSP& data_sp,
395 lldb::offset_t data_offset,
396 const FileSpec* file,
397 lldb::offset_t file_offset,
398 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000399{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000400 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000401 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000402 data_sp = file->MemoryMapFileContents(file_offset, length);
403 data_offset = 0;
404 }
405
406 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
407 {
408 // Update the data to contain the entire file if it doesn't already
409 if (data_sp->GetByteSize() < length)
410 {
411 data_sp = file->MemoryMapFileContents(file_offset, length);
412 data_offset = 0;
413 }
414 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 +0000415 if (objfile_ap.get() && objfile_ap->ParseHeader())
416 return objfile_ap.release();
417 }
418 return NULL;
419}
420
Greg Claytonb5a8f142012-02-05 02:38:54 +0000421ObjectFile *
Jason Molenda9badb6c2013-03-06 23:19:17 +0000422ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
423 DataBufferSP& data_sp,
424 const ProcessSP &process_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000425 lldb::addr_t header_addr)
426{
427 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
428 {
Greg Clayton3508c382012-02-24 01:59:29 +0000429 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000430 if (objfile_ap.get() && objfile_ap->ParseHeader())
431 return objfile_ap.release();
432 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000433 return NULL;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000434}
435
436
437const ConstString &
438ObjectFileMachO::GetSegmentNameTEXT()
439{
440 static ConstString g_segment_name_TEXT ("__TEXT");
441 return g_segment_name_TEXT;
442}
443
444const ConstString &
445ObjectFileMachO::GetSegmentNameDATA()
446{
447 static ConstString g_segment_name_DATA ("__DATA");
448 return g_segment_name_DATA;
449}
450
451const ConstString &
452ObjectFileMachO::GetSegmentNameOBJC()
453{
454 static ConstString g_segment_name_OBJC ("__OBJC");
455 return g_segment_name_OBJC;
456}
457
458const ConstString &
459ObjectFileMachO::GetSegmentNameLINKEDIT()
460{
461 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
462 return g_section_name_LINKEDIT;
463}
464
465const ConstString &
466ObjectFileMachO::GetSectionNameEHFrame()
467{
468 static ConstString g_section_name_eh_frame ("__eh_frame");
469 return g_section_name_eh_frame;
470}
471
472
Chris Lattner24943d22010-06-08 16:52:24 +0000473
474static uint32_t
475MachHeaderSizeFromMagic(uint32_t magic)
476{
477 switch (magic)
478 {
Greg Clayton1674b122010-07-21 22:12:05 +0000479 case HeaderMagic32:
480 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000481 return sizeof(struct mach_header);
482
Greg Clayton1674b122010-07-21 22:12:05 +0000483 case HeaderMagic64:
484 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000485 return sizeof(struct mach_header_64);
486 break;
487
488 default:
489 break;
490 }
491 return 0;
492}
493
494
495bool
Jason Molenda9badb6c2013-03-06 23:19:17 +0000496ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
497 lldb::addr_t data_offset,
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000498 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000499{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000500 DataExtractor data;
501 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000502 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000503 uint32_t magic = data.GetU32(&offset);
504 return MachHeaderSizeFromMagic(magic) != 0;
505}
506
507
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000508ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
509 DataBufferSP& data_sp,
510 lldb::offset_t data_offset,
511 const FileSpec* file,
512 lldb::offset_t file_offset,
513 lldb::offset_t length) :
514 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Clayton46c9a352012-02-09 06:16:32 +0000515 m_mach_segments(),
516 m_mach_sections(),
517 m_entry_point_address(),
518 m_thread_context_offsets(),
519 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000520{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000521 ::memset (&m_header, 0, sizeof(m_header));
522 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000523}
524
Greg Clayton3508c382012-02-24 01:59:29 +0000525ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000526 lldb::DataBufferSP& header_data_sp,
527 const lldb::ProcessSP &process_sp,
528 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000529 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Clayton46c9a352012-02-09 06:16:32 +0000530 m_mach_segments(),
531 m_mach_sections(),
532 m_entry_point_address(),
533 m_thread_context_offsets(),
534 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000535{
536 ::memset (&m_header, 0, sizeof(m_header));
537 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
538}
Chris Lattner24943d22010-06-08 16:52:24 +0000539
540ObjectFileMachO::~ObjectFileMachO()
541{
542}
543
544
545bool
546ObjectFileMachO::ParseHeader ()
547{
Greg Clayton9482f052012-03-13 23:14:29 +0000548 ModuleSP module_sp(GetModule());
549 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000550 {
Greg Clayton9482f052012-03-13 23:14:29 +0000551 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
552 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000553 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000554 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000555 // Leave magic in the original byte order
556 m_header.magic = m_data.GetU32(&offset);
557 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000558 {
Greg Clayton9482f052012-03-13 23:14:29 +0000559 case HeaderMagic32:
560 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
561 m_data.SetAddressByteSize(4);
562 can_parse = true;
563 break;
564
565 case HeaderMagic64:
566 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
567 m_data.SetAddressByteSize(8);
568 can_parse = true;
569 break;
570
571 case HeaderMagic32Swapped:
572 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
573 m_data.SetAddressByteSize(4);
574 can_parse = true;
575 break;
576
577 case HeaderMagic64Swapped:
578 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
579 m_data.SetAddressByteSize(8);
580 can_parse = true;
581 break;
582
583 default:
584 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000585 }
Greg Clayton9482f052012-03-13 23:14:29 +0000586
587 if (can_parse)
588 {
589 m_data.GetU32(&offset, &m_header.cputype, 6);
590
591 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +0000592
Greg Clayton21a25432012-11-16 21:36:10 +0000593 // Check if the module has a required architecture
594 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000595 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000596 return false;
597
Greg Clayton9482f052012-03-13 23:14:29 +0000598 if (SetModulesArchitecture (mach_arch))
599 {
600 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
601 if (m_data.GetByteSize() < header_and_lc_size)
602 {
603 DataBufferSP data_sp;
604 ProcessSP process_sp (m_process_wp.lock());
605 if (process_sp)
606 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000607 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000608 }
609 else
610 {
611 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000612 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000613 if (data_sp->GetByteSize() != header_and_lc_size)
614 return false;
615 }
616 if (data_sp)
617 m_data.SetData (data_sp);
618 }
619 }
620 return true;
621 }
622 else
623 {
624 memset(&m_header, 0, sizeof(struct mach_header));
625 }
Chris Lattner24943d22010-06-08 16:52:24 +0000626 }
627 return false;
628}
629
630
631ByteOrder
632ObjectFileMachO::GetByteOrder () const
633{
Chris Lattner24943d22010-06-08 16:52:24 +0000634 return m_data.GetByteOrder ();
635}
636
Jim Ingham7508e732010-08-09 23:31:02 +0000637bool
638ObjectFileMachO::IsExecutable() const
639{
640 return m_header.filetype == HeaderFileTypeExecutable;
641}
Chris Lattner24943d22010-06-08 16:52:24 +0000642
Greg Clayton36da2aa2013-01-25 18:06:21 +0000643uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000644ObjectFileMachO::GetAddressByteSize () const
645{
Chris Lattner24943d22010-06-08 16:52:24 +0000646 return m_data.GetAddressByteSize ();
647}
648
Greg Claytonb3448432011-03-24 21:19:54 +0000649AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000650ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
651{
652 Symtab *symtab = GetSymtab();
653 if (symtab)
654 {
655 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
656 if (symbol)
657 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000658 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000659 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000660 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000661 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000662 {
Greg Clayton3508c382012-02-24 01:59:29 +0000663 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000664 switch (section_type)
665 {
666 case eSectionTypeInvalid: return eAddressClassUnknown;
667 case eSectionTypeCode:
668 if (m_header.cputype == llvm::MachO::CPUTypeARM)
669 {
670 // For ARM we have a bit in the n_desc field of the symbol
671 // that tells us ARM/Thumb which is bit 0x0008.
672 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
673 return eAddressClassCodeAlternateISA;
674 }
675 return eAddressClassCode;
676
677 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000678 case eSectionTypeData:
679 case eSectionTypeDataCString:
680 case eSectionTypeDataCStringPointers:
681 case eSectionTypeDataSymbolAddress:
682 case eSectionTypeData4:
683 case eSectionTypeData8:
684 case eSectionTypeData16:
685 case eSectionTypeDataPointers:
686 case eSectionTypeZeroFill:
687 case eSectionTypeDataObjCMessageRefs:
688 case eSectionTypeDataObjCCFStrings:
689 return eAddressClassData;
690 case eSectionTypeDebug:
691 case eSectionTypeDWARFDebugAbbrev:
692 case eSectionTypeDWARFDebugAranges:
693 case eSectionTypeDWARFDebugFrame:
694 case eSectionTypeDWARFDebugInfo:
695 case eSectionTypeDWARFDebugLine:
696 case eSectionTypeDWARFDebugLoc:
697 case eSectionTypeDWARFDebugMacInfo:
698 case eSectionTypeDWARFDebugPubNames:
699 case eSectionTypeDWARFDebugPubTypes:
700 case eSectionTypeDWARFDebugRanges:
701 case eSectionTypeDWARFDebugStr:
702 case eSectionTypeDWARFAppleNames:
703 case eSectionTypeDWARFAppleTypes:
704 case eSectionTypeDWARFAppleNamespaces:
705 case eSectionTypeDWARFAppleObjC:
706 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000707 case eSectionTypeEHFrame: return eAddressClassRuntime;
708 case eSectionTypeOther: return eAddressClassUnknown;
709 }
710 }
711 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000712
Greg Claytonb3448432011-03-24 21:19:54 +0000713 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000714 switch (symbol_type)
715 {
716 case eSymbolTypeAny: return eAddressClassUnknown;
717 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000718
Greg Claytonb1888f22011-03-19 01:12:21 +0000719 case eSymbolTypeCode:
720 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000721 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000722 if (m_header.cputype == llvm::MachO::CPUTypeARM)
723 {
724 // For ARM we have a bit in the n_desc field of the symbol
725 // that tells us ARM/Thumb which is bit 0x0008.
726 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
727 return eAddressClassCodeAlternateISA;
728 }
729 return eAddressClassCode;
730
731 case eSymbolTypeData: return eAddressClassData;
732 case eSymbolTypeRuntime: return eAddressClassRuntime;
733 case eSymbolTypeException: return eAddressClassRuntime;
734 case eSymbolTypeSourceFile: return eAddressClassDebug;
735 case eSymbolTypeHeaderFile: return eAddressClassDebug;
736 case eSymbolTypeObjectFile: return eAddressClassDebug;
737 case eSymbolTypeCommonBlock: return eAddressClassDebug;
738 case eSymbolTypeBlock: return eAddressClassDebug;
739 case eSymbolTypeLocal: return eAddressClassData;
740 case eSymbolTypeParam: return eAddressClassData;
741 case eSymbolTypeVariable: return eAddressClassData;
742 case eSymbolTypeVariableType: return eAddressClassDebug;
743 case eSymbolTypeLineEntry: return eAddressClassDebug;
744 case eSymbolTypeLineHeader: return eAddressClassDebug;
745 case eSymbolTypeScopeBegin: return eAddressClassDebug;
746 case eSymbolTypeScopeEnd: return eAddressClassDebug;
747 case eSymbolTypeAdditional: return eAddressClassUnknown;
748 case eSymbolTypeCompiler: return eAddressClassDebug;
749 case eSymbolTypeInstrumentation:return eAddressClassDebug;
750 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000751 case eSymbolTypeObjCClass: return eAddressClassRuntime;
752 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
753 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000754 }
755 }
756 }
757 return eAddressClassUnknown;
758}
Chris Lattner24943d22010-06-08 16:52:24 +0000759
760Symtab *
761ObjectFileMachO::GetSymtab()
762{
Greg Clayton9482f052012-03-13 23:14:29 +0000763 ModuleSP module_sp(GetModule());
764 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000765 {
Greg Clayton9482f052012-03-13 23:14:29 +0000766 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
767 if (m_symtab_ap.get() == NULL)
768 {
769 m_symtab_ap.reset(new Symtab(this));
770 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
771 ParseSymtab (true);
772 m_symtab_ap->Finalize ();
773 }
Chris Lattner24943d22010-06-08 16:52:24 +0000774 }
775 return m_symtab_ap.get();
776}
777
778
779SectionList *
780ObjectFileMachO::GetSectionList()
781{
Greg Clayton9482f052012-03-13 23:14:29 +0000782 ModuleSP module_sp(GetModule());
783 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000784 {
Greg Clayton9482f052012-03-13 23:14:29 +0000785 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
786 if (m_sections_ap.get() == NULL)
787 {
788 m_sections_ap.reset(new SectionList());
789 ParseSections();
790 }
Chris Lattner24943d22010-06-08 16:52:24 +0000791 }
792 return m_sections_ap.get();
793}
794
795
796size_t
797ObjectFileMachO::ParseSections ()
798{
799 lldb::user_id_t segID = 0;
800 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000801 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000802 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000803 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000804 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000805 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000806 // First look up any LC_ENCRYPTION_INFO load commands
807 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
808 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000809 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000810 for (i=0; i<m_header.ncmds; ++i)
811 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000812 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000813 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000814 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000815
Greg Clayton54e33712012-05-25 18:09:55 +0000816 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000817 {
Greg Clayton54e33712012-05-25 18:09:55 +0000818 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
819 {
820 if (encryption_cmd.cryptid != 0)
821 {
822 EncryptedFileRanges::Entry entry;
823 entry.SetRangeBase(encryption_cmd.cryptoff);
824 entry.SetByteSize(encryption_cmd.cryptsize);
825 encrypted_file_ranges.Append(entry);
826 }
827 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000828 }
Greg Clayton54e33712012-05-25 18:09:55 +0000829 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000830 }
831
832 offset = MachHeaderSizeFromMagic(m_header.magic);
833
Greg Clayton54e33712012-05-25 18:09:55 +0000834 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000835 for (i=0; i<m_header.ncmds; ++i)
836 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000837 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000838 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
839 break;
840
Greg Clayton1674b122010-07-21 22:12:05 +0000841 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000842 {
843 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
844 {
845 load_cmd.vmaddr = m_data.GetAddress(&offset);
846 load_cmd.vmsize = m_data.GetAddress(&offset);
847 load_cmd.fileoff = m_data.GetAddress(&offset);
848 load_cmd.filesize = m_data.GetAddress(&offset);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000849 if (m_length != 0 && load_cmd.filesize != 0)
850 {
Greg Claytonbb759862013-04-16 16:51:19 +0000851 if (load_cmd.fileoff > m_length)
852 {
853 // We have a load command that says it extends past the end of hte file. This is likely
854 // a corrupt file. We don't have any way to return an error condition here (this method
855 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
856 // is null out the SectionList vector and if a process has been set up, dump a message
857 // to stdout. The most common case here is core file debugging with a truncated file.
858 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
859 GetModule()->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 ")",
860 i,
861 lc_segment_name,
862 load_cmd.fileoff,
863 m_length);
864 m_sections_ap->Clear();
865 return 0;
866 }
867
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000868 if (load_cmd.fileoff + load_cmd.filesize > m_length)
869 {
870 // We have a load command that says it extends past the end of hte file. This is likely
871 // a corrupt file. We don't have any way to return an error condition here (this method
872 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
873 // is null out the SectionList vector and if a process has been set up, dump a message
Greg Claytonbb759862013-04-16 16:51:19 +0000874 // to stdout. The most common case here is core file debugging with a truncated file.
875 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
876 GetModule()->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff + filesize (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 ")",
877 i,
878 lc_segment_name,
879 load_cmd.fileoff + load_cmd.filesize,
880 m_length);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000881 m_sections_ap->Clear();
882 return 0;
883 }
884 }
Chris Lattner24943d22010-06-08 16:52:24 +0000885 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
886 {
Jason Molenda9badb6c2013-03-06 23:19:17 +0000887
Greg Clayton68ca8232011-01-25 02:58:48 +0000888 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
889
Chris Lattner24943d22010-06-08 16:52:24 +0000890 // Keep a list of mach segments around in case we need to
891 // get at data that isn't stored in the abstracted Sections.
892 m_mach_segments.push_back (load_cmd);
893
Greg Clayton36da2aa2013-01-25 18:06:21 +0000894 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 +0000895 // Use a segment ID of the segment index shifted left by 8 so they
896 // never conflict with any of the sections.
897 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000898 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000899 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000900 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000901 ++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
902 segment_name, // Name of this section
903 eSectionTypeContainer, // This section is a container of other sections.
904 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
905 load_cmd.vmsize, // VM size in bytes of this section
906 load_cmd.fileoff, // Offset to the data for this section in the file
907 load_cmd.filesize, // Size in bytes of this section as found in the the file
908 load_cmd.flags)); // Flags for this section
909
Greg Clayton68ca8232011-01-25 02:58:48 +0000910 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000911 m_sections_ap->AddSection(segment_sp);
912 }
913
914 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000915 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000916 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +0000917 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +0000918 // mach sections yet...
919 if (m_mach_sections.empty())
920 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000921 uint32_t segment_sect_idx;
922 const lldb::user_id_t first_segment_sectID = sectID + 1;
923
924
Greg Clayton1674b122010-07-21 22:12:05 +0000925 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000926 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
927 {
928 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
929 break;
930 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
931 break;
932 sect64.addr = m_data.GetAddress(&offset);
933 sect64.size = m_data.GetAddress(&offset);
934
935 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
936 break;
937
938 // Keep a list of mach sections around in case we need to
939 // get at data that isn't stored in the abstracted Sections.
940 m_mach_sections.push_back (sect64);
941
942 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
943 if (!segment_name)
944 {
945 // We have a segment with no name so we need to conjure up
946 // segments that correspond to the section's segname if there
947 // isn't already such a section. If there is such a section,
948 // we resize the section so that it spans all sections.
949 // We also mark these sections as fake so address matches don't
950 // hit if they land in the gaps between the child sections.
951 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
952 segment_sp = m_sections_ap->FindSectionByName (segment_name);
953 if (segment_sp.get())
954 {
955 Section *segment = segment_sp.get();
956 // Grow the section size as needed.
957 const lldb::addr_t sect64_min_addr = sect64.addr;
958 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
959 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
960 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
961 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
962 if (sect64_min_addr >= curr_seg_min_addr)
963 {
964 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
965 // Only grow the section size if needed
966 if (new_seg_byte_size > curr_seg_byte_size)
967 segment->SetByteSize (new_seg_byte_size);
968 }
969 else
970 {
971 // We need to change the base address of the segment and
972 // adjust the child section offsets for all existing children.
973 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
974 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +0000975 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000976 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
977 }
Greg Clayton661825b2010-06-28 23:51:11 +0000978
979 // Grow the section size as needed.
980 if (sect64.offset)
981 {
982 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
983 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
984
985 const lldb::addr_t section_min_file_offset = sect64.offset;
986 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
987 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
988 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
989 segment->SetFileOffset (new_file_offset);
990 segment->SetFileSize (new_file_size);
991 }
Chris Lattner24943d22010-06-08 16:52:24 +0000992 }
993 else
994 {
995 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +0000996 segment_sp.reset(new Section (segment_sp, // Parent section
997 module_sp, // Module to which this section belongs
998 ++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
999 segment_name, // Name of this section
1000 eSectionTypeContainer, // This section is a container of other sections.
1001 sect64.addr, // File VM address == addresses as they are found in the object file
1002 sect64.size, // VM size in bytes of this section
1003 sect64.offset, // Offset to the data for this section in the file
1004 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1005 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +00001006 segment_sp->SetIsFake(true);
1007 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +00001008 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001009 }
1010 }
1011 assert (segment_sp.get());
1012
Greg Clayton1674b122010-07-21 22:12:05 +00001013 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +00001014 static ConstString g_sect_name_objc_data ("__objc_data");
1015 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1016 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1017 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1018 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1019 static ConstString g_sect_name_objc_const ("__objc_const");
1020 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1021 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001022
1023 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1024 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1025 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1026 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1027 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1028 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1029 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1030 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1031 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1032 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1033 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +00001034 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1035 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +00001036 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +00001037 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001038 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +00001039 static ConstString g_sect_name_DATA ("__DATA");
1040 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001041
Chris Lattner24943d22010-06-08 16:52:24 +00001042 SectionType sect_type = eSectionTypeOther;
1043
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001044 if (section_name == g_sect_name_dwarf_debug_abbrev)
1045 sect_type = eSectionTypeDWARFDebugAbbrev;
1046 else if (section_name == g_sect_name_dwarf_debug_aranges)
1047 sect_type = eSectionTypeDWARFDebugAranges;
1048 else if (section_name == g_sect_name_dwarf_debug_frame)
1049 sect_type = eSectionTypeDWARFDebugFrame;
1050 else if (section_name == g_sect_name_dwarf_debug_info)
1051 sect_type = eSectionTypeDWARFDebugInfo;
1052 else if (section_name == g_sect_name_dwarf_debug_line)
1053 sect_type = eSectionTypeDWARFDebugLine;
1054 else if (section_name == g_sect_name_dwarf_debug_loc)
1055 sect_type = eSectionTypeDWARFDebugLoc;
1056 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1057 sect_type = eSectionTypeDWARFDebugMacInfo;
1058 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1059 sect_type = eSectionTypeDWARFDebugPubNames;
1060 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1061 sect_type = eSectionTypeDWARFDebugPubTypes;
1062 else if (section_name == g_sect_name_dwarf_debug_ranges)
1063 sect_type = eSectionTypeDWARFDebugRanges;
1064 else if (section_name == g_sect_name_dwarf_debug_str)
1065 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001066 else if (section_name == g_sect_name_dwarf_apple_names)
1067 sect_type = eSectionTypeDWARFAppleNames;
1068 else if (section_name == g_sect_name_dwarf_apple_types)
1069 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001070 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1071 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001072 else if (section_name == g_sect_name_dwarf_apple_objc)
1073 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001074 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001075 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001076 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001077 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001078 else if (section_name == g_sect_name_eh_frame)
1079 sect_type = eSectionTypeEHFrame;
1080 else if (section_name == g_sect_name_cfstring)
1081 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001082 else if (section_name == g_sect_name_objc_data ||
1083 section_name == g_sect_name_objc_classrefs ||
1084 section_name == g_sect_name_objc_superrefs ||
1085 section_name == g_sect_name_objc_const ||
1086 section_name == g_sect_name_objc_classlist)
1087 {
1088 sect_type = eSectionTypeDataPointers;
1089 }
Chris Lattner24943d22010-06-08 16:52:24 +00001090
1091 if (sect_type == eSectionTypeOther)
1092 {
1093 switch (mach_sect_type)
1094 {
1095 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001096 case SectionTypeRegular:
1097 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001098 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001099 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001100 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001101 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001102 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001103 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001104 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1105 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1106 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1107 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1108 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1109 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1110 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1111 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1112 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1113 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1114 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1115 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1116 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1117 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1118 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1119 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001120 default: break;
1121 }
1122 }
1123
Greg Clayton3508c382012-02-24 01:59:29 +00001124 SectionSP section_sp(new Section (segment_sp,
1125 module_sp,
1126 ++sectID,
1127 section_name,
1128 sect_type,
1129 sect64.addr - segment_sp->GetFileAddress(),
1130 sect64.size,
1131 sect64.offset,
1132 sect64.offset == 0 ? 0 : sect64.size,
1133 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001134 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001135
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001136 bool section_is_encrypted = false;
1137 if (!segment_is_encrypted && load_cmd.filesize != 0)
1138 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001139
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001140 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001141 segment_sp->GetChildren().AddSection(section_sp);
1142
1143 if (segment_sp->IsFake())
1144 {
1145 segment_sp.reset();
1146 segment_name.Clear();
1147 }
1148 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001149 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001150 {
1151 if (first_segment_sectID <= sectID)
1152 {
1153 lldb::user_id_t sect_uid;
1154 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1155 {
1156 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1157 SectionSP next_section_sp;
1158 if (sect_uid + 1 <= sectID)
1159 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1160
1161 if (curr_section_sp.get())
1162 {
1163 if (curr_section_sp->GetByteSize() == 0)
1164 {
1165 if (next_section_sp.get() != NULL)
1166 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1167 else
1168 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1169 }
1170 }
1171 }
1172 }
1173 }
1174 }
1175 }
1176 }
Greg Clayton1674b122010-07-21 22:12:05 +00001177 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001178 {
1179 m_dysymtab.cmd = load_cmd.cmd;
1180 m_dysymtab.cmdsize = load_cmd.cmdsize;
1181 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1182 }
1183
1184 offset = load_cmd_offset + load_cmd.cmdsize;
1185 }
1186// if (dump_sections)
1187// {
1188// StreamFile s(stdout);
1189// m_sections_ap->Dump(&s, true);
1190// }
1191 return sectID; // Return the number of sections we registered with the module
1192}
1193
1194class MachSymtabSectionInfo
1195{
1196public:
1197
1198 MachSymtabSectionInfo (SectionList *section_list) :
1199 m_section_list (section_list),
1200 m_section_infos()
1201 {
1202 // Get the number of sections down to a depth of 1 to include
1203 // all segments and their sections, but no other sections that
1204 // may be added for debug map or
1205 m_section_infos.resize(section_list->GetNumSections(1));
1206 }
1207
1208
Greg Clayton3508c382012-02-24 01:59:29 +00001209 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001210 GetSection (uint8_t n_sect, addr_t file_addr)
1211 {
1212 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001213 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001214 if (n_sect < m_section_infos.size())
1215 {
Greg Clayton3508c382012-02-24 01:59:29 +00001216 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001217 {
Greg Clayton3508c382012-02-24 01:59:29 +00001218 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1219 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001220 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001221 {
Greg Clayton3508c382012-02-24 01:59:29 +00001222 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1223 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001224 }
1225 else
1226 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001227 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001228 }
Chris Lattner24943d22010-06-08 16:52:24 +00001229 }
1230 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001231 {
1232 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001233 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001234 }
1235 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1236 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1237 {
1238 // Symbol is in section with zero size, but has the same start
1239 // address as the section. This can happen with linker symbols
1240 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001241 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001242 }
Chris Lattner24943d22010-06-08 16:52:24 +00001243 }
Greg Clayton3508c382012-02-24 01:59:29 +00001244 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001245 }
1246
1247protected:
1248 struct SectionInfo
1249 {
1250 SectionInfo () :
1251 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001252 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001253 {
1254 }
1255
1256 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001257 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001258 };
1259 SectionList *m_section_list;
1260 std::vector<SectionInfo> m_section_infos;
1261};
1262
Chris Lattner24943d22010-06-08 16:52:24 +00001263size_t
1264ObjectFileMachO::ParseSymtab (bool minimize)
1265{
1266 Timer scoped_timer(__PRETTY_FUNCTION__,
1267 "ObjectFileMachO::ParseSymtab () module = %s",
1268 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001269 ModuleSP module_sp (GetModule());
1270 if (!module_sp)
1271 return 0;
1272
1273 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1274 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1275 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1276 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001277 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001278 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001279
Greg Clayton952e9dc2013-03-27 23:08:40 +00001280 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001281
Chris Lattner24943d22010-06-08 16:52:24 +00001282 for (i=0; i<m_header.ncmds; ++i)
1283 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001284 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001285 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001286 struct load_command lc;
1287 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001288 break;
1289 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001290 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001291 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001292 case LoadCommandSymtab:
1293 symtab_load_command.cmd = lc.cmd;
1294 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001295 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001296 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1297 return 0;
1298 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001299 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001300 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001301 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001302 return 0;
1303 }
1304
1305 if (symtab_load_command.stroff == 0)
1306 {
1307 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001308 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001309 return 0;
1310 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001311
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001312 if (symtab_load_command.nsyms == 0)
1313 {
1314 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001315 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001316 return 0;
1317 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001318
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001319 if (symtab_load_command.strsize == 0)
1320 {
1321 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001322 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001323 return 0;
1324 }
1325 break;
1326
1327 case LoadCommandFunctionStarts:
1328 function_starts_load_command.cmd = lc.cmd;
1329 function_starts_load_command.cmdsize = lc.cmdsize;
1330 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1331 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1332 break;
1333
1334 default:
1335 break;
1336 }
1337 offset = cmd_offset + lc.cmdsize;
1338 }
1339
1340 if (symtab_load_command.cmd)
1341 {
1342 Symtab *symtab = m_symtab_ap.get();
1343 SectionList *section_list = GetSectionList();
1344 if (section_list == NULL)
1345 return 0;
1346
1347 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001348 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001349
Greg Clayton36da2aa2013-01-25 18:06:21 +00001350 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1351 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001352 bool bit_width_32 = addr_byte_size == 4;
1353 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1354
Greg Clayton36da2aa2013-01-25 18:06:21 +00001355 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1356 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1357 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001358 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001359
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001360 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1361 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001362 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1363 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001364 {
Greg Claytondd29b972012-05-18 23:20:01 +00001365 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001366 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1367 // Reading mach file from memory in a process or core file...
1368
1369 if (linkedit_section_sp)
1370 {
1371 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1372 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1373 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001374 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001375
1376 bool data_was_read = false;
1377
1378#if defined (__APPLE__) && defined (__arm__)
1379 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001380 {
Greg Clayton29021d32012-04-18 05:19:20 +00001381 // This mach-o memory file is in the dyld shared cache. If this
1382 // program is not remote and this is iOS, then this process will
1383 // share the same shared cache as the process we are debugging and
1384 // we can read the entire __LINKEDIT from the address space in this
1385 // process. This is a needed optimization that is used for local iOS
1386 // debugging only since all shared libraries in the shared cache do
1387 // not have corresponding files that exist in the file system of the
1388 // device. They have been combined into a single file. This means we
1389 // always have to load these files from memory. All of the symbol and
1390 // string tables from all of the __LINKEDIT sections from the shared
1391 // libraries in the shared cache have been merged into a single large
1392 // symbol and string table. Reading all of this symbol and string table
1393 // data across can slow down debug launch times, so we optimize this by
1394 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda45c75502013-04-16 06:24:42 +00001395
1396 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1397 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1398 bool use_lldb_cache = true;
1399 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1400 {
1401 use_lldb_cache = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001402 ModuleSP module_sp (GetModule());
1403 if (module_sp)
1404 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1405
Jason Molenda45c75502013-04-16 06:24:42 +00001406 }
1407
Greg Clayton29021d32012-04-18 05:19:20 +00001408 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda45c75502013-04-16 06:24:42 +00001409 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton29021d32012-04-18 05:19:20 +00001410 {
1411 data_was_read = true;
1412 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001413 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001414 if (function_starts_load_command.cmd)
1415 {
1416 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1417 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1418 }
1419 }
1420 }
1421#endif
1422
1423 if (!data_was_read)
1424 {
1425 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1426 if (nlist_data_sp)
1427 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001428 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1429 //if (strtab_data_sp)
1430 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001431 if (m_dysymtab.nindirectsyms != 0)
1432 {
1433 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1434 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1435 if (indirect_syms_data_sp)
1436 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1437 }
Greg Clayton29021d32012-04-18 05:19:20 +00001438 if (function_starts_load_command.cmd)
1439 {
1440 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1441 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1442 if (func_start_data_sp)
1443 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1444 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001445 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001446 }
1447 }
1448 else
1449 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001450 nlist_data.SetData (m_data,
1451 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001452 nlist_data_byte_size);
1453 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001454 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001455 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001456 if (m_dysymtab.nindirectsyms != 0)
1457 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001458 indirect_symbol_index_data.SetData (m_data,
1459 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001460 m_dysymtab.nindirectsyms * 4);
1461 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001462 if (function_starts_load_command.cmd)
1463 {
1464 function_starts_data.SetData (m_data,
1465 function_starts_load_command.dataoff,
1466 function_starts_load_command.datasize);
1467 }
1468 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001469
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001470 if (nlist_data.GetByteSize() == 0)
1471 {
1472 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001473 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001474 return 0;
1475 }
1476
1477
Greg Clayton3a5dc012012-05-25 17:04:00 +00001478 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1479 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001480 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001481 if (process)
1482 {
1483 if (strtab_addr == LLDB_INVALID_ADDRESS)
1484 {
1485 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001486 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001487 return 0;
1488 }
1489 }
1490 else
Greg Claytondd29b972012-05-18 23:20:01 +00001491 {
1492 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001493 module_sp->LogMessage(log, "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001494 return 0;
1495 }
1496 }
Greg Claytondd29b972012-05-18 23:20:01 +00001497
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001498 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1499 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1500 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1501 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1502 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1503 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1504 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1505 SectionSP eh_frame_section_sp;
1506 if (text_section_sp.get())
1507 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1508 else
1509 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1510
Greg Claytond2653c22012-03-14 01:53:24 +00001511 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001512
1513 // lldb works best if it knows the start addresss of all functions in a module.
1514 // Linker symbols or debug info are normally the best source of information for start addr / size but
1515 // they may be stripped in a released binary.
Jason Molenda2120aef2013-04-16 00:18:44 +00001516 // Two additional sources of information exist in Mach-O binaries:
Jason Molendad7938392013-03-21 03:36:01 +00001517 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1518 // binary, relative to the text section.
1519 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1520 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1521 // Binaries built to run on older releases may need to use eh_frame information.
1522
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001523 if (text_section_sp && function_starts_data.GetByteSize())
1524 {
1525 FunctionStarts::Entry function_start_entry;
1526 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001527 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001528 function_start_entry.addr = text_section_sp->GetFileAddress();
1529 uint64_t delta;
1530 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1531 {
1532 // Now append the current entry
1533 function_start_entry.addr += delta;
1534 function_starts.Append(function_start_entry);
1535 }
Jason Molenda2120aef2013-04-16 00:18:44 +00001536 }
Jason Molendad7938392013-03-21 03:36:01 +00001537 else
1538 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001539 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1540 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1541 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1542 // the module.
1543 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001544 {
1545 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1546 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1547 eh_frame.GetFunctionAddressAndSizeVector (functions);
1548 addr_t text_base_addr = text_section_sp->GetFileAddress();
1549 size_t count = functions.GetSize();
1550 for (size_t i = 0; i < count; ++i)
1551 {
1552 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1553 if (func)
1554 {
1555 FunctionStarts::Entry function_start_entry;
1556 function_start_entry.addr = func->base - text_base_addr;
1557 function_starts.Append(function_start_entry);
1558 }
1559 }
1560 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001561 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001562
Greg Clayton36da2aa2013-01-25 18:06:21 +00001563 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001564
Greg Clayton36da2aa2013-01-25 18:06:21 +00001565 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 +00001566
Greg Clayton36da2aa2013-01-25 18:06:21 +00001567 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001568
1569 uint32_t N_SO_index = UINT32_MAX;
1570
1571 MachSymtabSectionInfo section_info (section_list);
1572 std::vector<uint32_t> N_FUN_indexes;
1573 std::vector<uint32_t> N_NSYM_indexes;
1574 std::vector<uint32_t> N_INCL_indexes;
1575 std::vector<uint32_t> N_BRAC_indexes;
1576 std::vector<uint32_t> N_COMM_indexes;
1577 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1578 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1579 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1580 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1581 // Any symbols that get merged into another will get an entry
1582 // in this map so we know
1583 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1584 uint32_t nlist_idx = 0;
1585 Symbol *symbol_ptr = NULL;
1586
1587 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001588 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001589 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001590 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001591 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001592
Jason Molendab62abd52012-06-21 01:51:02 +00001593#if defined (__APPLE__) && defined (__arm__)
1594
1595 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1596 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1597 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1598 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1599 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1600 // nlist parser to ignore all LOCAL symbols.
1601
1602 if (m_header.flags & 0x80000000u)
1603 {
1604 // Before we can start mapping the DSC, we need to make certain the target process is actually
1605 // using the cache we can find.
1606
Jason Molendab62abd52012-06-21 01:51:02 +00001607 // Next we need to determine the correct path for the dyld shared cache.
1608
1609 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1610 char dsc_path[PATH_MAX];
1611
1612 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001613 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1614 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001615 header_arch.GetArchitectureName());
1616
1617 FileSpec dsc_filespec(dsc_path, false);
1618
1619 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda2120aef2013-04-16 00:18:44 +00001620 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001621 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001622 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1623 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1624 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001625 uint32_t imagesOffset;
1626 uint32_t imagesCount;
1627 uint64_t dyldBaseAddress;
1628 uint64_t codeSignatureOffset;
1629 uint64_t codeSignatureSize;
1630 uint64_t slideInfoOffset;
1631 uint64_t slideInfoSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001632 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1633 uint64_t localSymbolsSize; // size of local symbols information
1634 };
1635 struct lldb_copy_dyld_cache_header_v1
1636 {
1637 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1638 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1639 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1640 uint32_t imagesOffset;
1641 uint32_t imagesCount;
1642 uint64_t dyldBaseAddress;
1643 uint64_t codeSignatureOffset;
1644 uint64_t codeSignatureSize;
1645 uint64_t slideInfoOffset;
1646 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001647 uint64_t localSymbolsOffset;
1648 uint64_t localSymbolsSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001649 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001650 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001651
Jason Molenda2120aef2013-04-16 00:18:44 +00001652 struct lldb_copy_dyld_cache_mapping_info
1653 {
1654 uint64_t address;
1655 uint64_t size;
1656 uint64_t fileOffset;
1657 uint32_t maxProt;
1658 uint32_t initProt;
1659 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001660
Greg Claytonab77dcb2012-09-05 22:30:51 +00001661 struct lldb_copy_dyld_cache_local_symbols_info
1662 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001663 uint32_t nlistOffset;
1664 uint32_t nlistCount;
1665 uint32_t stringsOffset;
1666 uint32_t stringsSize;
1667 uint32_t entriesOffset;
1668 uint32_t entriesCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001669 };
1670 struct lldb_copy_dyld_cache_local_symbols_entry
1671 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001672 uint32_t dylibOffset;
1673 uint32_t nlistStartIndex;
1674 uint32_t nlistCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001675 };
Jason Molendab62abd52012-06-21 01:51:02 +00001676
Jason Molendafd3b35d2012-06-22 03:28:35 +00001677 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1678 The dyld_cache_local_symbols_info structure gives us three things:
1679 1. The start and count of the nlist records in the dyld_shared_cache file
1680 2. The start and size of the strings for these nlist records
1681 3. The start and count of dyld_cache_local_symbols_entry entries
1682
1683 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1684 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001685 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001686 and the count of how many nlist records there are for this dylib/framework.
1687 */
1688
Jason Molendab62abd52012-06-21 01:51:02 +00001689 // Process the dsc header to find the unmapped symbols
1690 //
1691 // Save some VM space, do not map the entire cache in one shot.
1692
Jason Molenda6bcabae2013-03-06 23:17:36 +00001693 DataBufferSP dsc_data_sp;
1694 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1695
1696 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001697 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001698 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001699
Jason Molenda6bcabae2013-03-06 23:17:36 +00001700 char version_str[17];
1701 int version = -1;
1702 lldb::offset_t offset = 0;
1703 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1704 version_str[16] = '\0';
1705 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1706 {
1707 int v;
1708 if (::sscanf (version_str + 6, "%d", &v) == 1)
1709 {
1710 version = v;
1711 }
1712 }
1713
Jason Molenda45c75502013-04-16 06:24:42 +00001714 UUID dsc_uuid;
1715 if (version >= 1)
1716 {
1717 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1718 uint8_t uuid_bytes[sizeof (uuid_t)];
1719 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1720 dsc_uuid.SetBytes (uuid_bytes);
1721 }
1722
1723 bool uuid_match = true;
1724 if (dsc_uuid.IsValid() && process)
1725 {
1726 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1727
1728 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1729 {
1730 // The on-disk dyld_shared_cache file is not the same as the one in this
1731 // process' memory, don't use it.
1732 uuid_match = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001733 ModuleSP module_sp (GetModule());
1734 if (module_sp)
1735 module_sp->ReportWarning ("process shared cache does not match on-disk dyld_shared_cache file, some symbol names will be missing.");
Jason Molenda45c75502013-04-16 06:24:42 +00001736 }
1737 }
1738
Jason Molenda9badb6c2013-03-06 23:19:17 +00001739 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001740
Jason Molendab62abd52012-06-21 01:51:02 +00001741 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1742
1743 // If the mappingOffset points to a location inside the header, we've
1744 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda45c75502013-04-16 06:24:42 +00001745 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendab62abd52012-06-21 01:51:02 +00001746 {
1747
Jason Molenda6bcabae2013-03-06 23:17:36 +00001748 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1749 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1750 offset = 0;
1751
1752 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1753 // in the shared library cache need to be adjusted by an offset to match up with the
1754 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1755 // recorded in mapping_offset_value.
1756 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1757
1758 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001759 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1760 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1761
Jason Molenda9badb6c2013-03-06 23:19:17 +00001762 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001763 {
1764 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001765 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001766 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001767 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001768
1769 offset = 0;
1770
1771 // Read the local_symbols_infos struct in one shot
1772 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1773 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1774
Jason Molendab62abd52012-06-21 01:51:02 +00001775 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1776
Jason Molenda6bcabae2013-03-06 23:17:36 +00001777 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001778
1779 offset = local_symbols_info.entriesOffset;
1780 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1781 {
1782 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1783 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1784 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1785 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1786
Jason Molenda9badb6c2013-03-06 23:19:17 +00001787 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001788 {
1789 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1790
1791 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1792 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1793 num_syms = symtab->GetNumSymbols();
1794
1795 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1796 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1797
Jason Molenda9badb6c2013-03-06 23:19:17 +00001798 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001799 {
1800 /////////////////////////////
1801 {
1802 struct nlist_64 nlist;
1803 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1804 break;
1805
1806 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1807 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1808 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1809 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1810 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1811
1812 SymbolType type = eSymbolTypeInvalid;
1813 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1814
1815 if (symbol_name == NULL)
1816 {
1817 // No symbol should be NULL, even the symbols with no
1818 // string values should have an offset zero which points
1819 // to an empty C-string
1820 Host::SystemLog (Host::eSystemLogError,
1821 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1822 entry_index,
1823 nlist.n_strx,
1824 module_sp->GetFileSpec().GetDirectory().GetCString(),
1825 module_sp->GetFileSpec().GetFilename().GetCString());
1826 continue;
1827 }
1828 if (symbol_name[0] == '\0')
1829 symbol_name = NULL;
1830
1831 const char *symbol_name_non_abi_mangled = NULL;
1832
1833 SectionSP symbol_section;
1834 uint32_t symbol_byte_size = 0;
1835 bool add_nlist = true;
1836 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001837 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001838
1839 assert (sym_idx < num_syms);
1840
1841 sym[sym_idx].SetDebug (is_debug);
1842
1843 if (is_debug)
1844 {
1845 switch (nlist.n_type)
1846 {
1847 case StabGlobalSymbol:
1848 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1849 // Sometimes the N_GSYM value contains the address.
1850
1851 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1852 // have the same address, but we want to ensure that we always find only the real symbol,
1853 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1854 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1855 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1856 // same address.
1857
1858 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1859 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1860 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1861 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1862 add_nlist = false;
1863 else
1864 {
1865 sym[sym_idx].SetExternal(true);
1866 if (nlist.n_value != 0)
1867 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1868 type = eSymbolTypeData;
1869 }
1870 break;
1871
1872 case StabFunctionName:
1873 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1874 type = eSymbolTypeCompiler;
1875 break;
1876
1877 case StabFunction:
1878 // N_FUN -- procedure: name,,n_sect,linenumber,address
1879 if (symbol_name)
1880 {
1881 type = eSymbolTypeCode;
1882 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1883
1884 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1885 // We use the current number of symbols in the symbol table in lieu of
1886 // using nlist_idx in case we ever start trimming entries out
1887 N_FUN_indexes.push_back(sym_idx);
1888 }
1889 else
1890 {
1891 type = eSymbolTypeCompiler;
1892
1893 if ( !N_FUN_indexes.empty() )
1894 {
1895 // Copy the size of the function into the original STAB entry so we don't have
1896 // to hunt for it later
1897 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1898 N_FUN_indexes.pop_back();
1899 // We don't really need the end function STAB as it contains the size which
1900 // we already placed with the original symbol, so don't add it if we want a
1901 // minimal symbol table
1902 if (minimize)
1903 add_nlist = false;
1904 }
1905 }
1906 break;
1907
1908 case StabStaticSymbol:
1909 // N_STSYM -- static symbol: name,,n_sect,type,address
1910 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
1911 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1912 type = eSymbolTypeData;
1913 break;
1914
1915 case StabLocalCommon:
1916 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
1917 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1918 type = eSymbolTypeCommonBlock;
1919 break;
1920
1921 case StabBeginSymbol:
1922 // N_BNSYM
1923 // We use the current number of symbols in the symbol table in lieu of
1924 // using nlist_idx in case we ever start trimming entries out
1925 if (minimize)
1926 {
1927 // Skip these if we want minimal symbol tables
1928 add_nlist = false;
1929 }
1930 else
1931 {
1932 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1933 N_NSYM_indexes.push_back(sym_idx);
1934 type = eSymbolTypeScopeBegin;
1935 }
1936 break;
1937
1938 case StabEndSymbol:
1939 // N_ENSYM
1940 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1941 // so that we can always skip the entire symbol if we need to navigate
1942 // more quickly at the source level when parsing STABS
1943 if (minimize)
1944 {
1945 // Skip these if we want minimal symbol tables
1946 add_nlist = false;
1947 }
1948 else
1949 {
1950 if ( !N_NSYM_indexes.empty() )
1951 {
1952 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1953 symbol_ptr->SetByteSize(sym_idx + 1);
1954 symbol_ptr->SetSizeIsSibling(true);
1955 N_NSYM_indexes.pop_back();
1956 }
1957 type = eSymbolTypeScopeEnd;
1958 }
1959 break;
1960
1961
1962 case StabSourceFileOptions:
1963 // N_OPT - emitted with gcc2_compiled and in gcc source
1964 type = eSymbolTypeCompiler;
1965 break;
1966
1967 case StabRegisterSymbol:
1968 // N_RSYM - register sym: name,,NO_SECT,type,register
1969 type = eSymbolTypeVariable;
1970 break;
1971
1972 case StabSourceLine:
1973 // N_SLINE - src line: 0,,n_sect,linenumber,address
1974 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1975 type = eSymbolTypeLineEntry;
1976 break;
1977
1978 case StabStructureType:
1979 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
1980 type = eSymbolTypeVariableType;
1981 break;
1982
1983 case StabSourceFileName:
1984 // N_SO - source file name
1985 type = eSymbolTypeSourceFile;
1986 if (symbol_name == NULL)
1987 {
1988 if (minimize)
1989 add_nlist = false;
1990 if (N_SO_index != UINT32_MAX)
1991 {
1992 // Set the size of the N_SO to the terminating index of this N_SO
1993 // so that we can always skip the entire N_SO if we need to navigate
1994 // more quickly at the source level when parsing STABS
1995 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
1996 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
1997 symbol_ptr->SetSizeIsSibling(true);
1998 }
1999 N_NSYM_indexes.clear();
2000 N_INCL_indexes.clear();
2001 N_BRAC_indexes.clear();
2002 N_COMM_indexes.clear();
2003 N_FUN_indexes.clear();
2004 N_SO_index = UINT32_MAX;
2005 }
2006 else
2007 {
2008 // We use the current number of symbols in the symbol table in lieu of
2009 // using nlist_idx in case we ever start trimming entries out
2010 const bool N_SO_has_full_path = symbol_name[0] == '/';
2011 if (N_SO_has_full_path)
2012 {
2013 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2014 {
2015 // We have two consecutive N_SO entries where the first contains a directory
2016 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00002017 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002018 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2019 add_nlist = false;
2020 }
2021 else
2022 {
2023 // This is the first entry in a N_SO that contains a directory or
2024 // a full path to the source file
2025 N_SO_index = sym_idx;
2026 }
2027 }
2028 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2029 {
2030 // This is usually the second N_SO entry that contains just the filename,
2031 // so here we combine it with the first one if we are minimizing the symbol table
2032 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2033 if (so_path && so_path[0])
2034 {
2035 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002036 const size_t double_slash_pos = full_so_path.find("//");
2037 if (double_slash_pos != std::string::npos)
2038 {
2039 // The linker has been generating bad N_SO entries with doubled up paths
2040 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2041 // and the second is the directory for the source file so you end up with
2042 // a path that looks like "/tmp/src//tmp/src/"
2043 FileSpec so_dir(so_path, false);
2044 if (!so_dir.Exists())
2045 {
2046 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2047 if (so_dir.Exists())
2048 {
2049 // Trim off the incorrect path
2050 full_so_path.erase(0, double_slash_pos + 1);
2051 }
2052 }
2053 }
Jason Molendab62abd52012-06-21 01:51:02 +00002054 if (*full_so_path.rbegin() != '/')
2055 full_so_path += '/';
2056 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00002057 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002058 add_nlist = false;
2059 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2060 }
2061 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002062 else
2063 {
2064 // This could be a relative path to a N_SO
2065 N_SO_index = sym_idx;
2066 }
Jason Molendab62abd52012-06-21 01:51:02 +00002067 }
Jason Molendab62abd52012-06-21 01:51:02 +00002068 break;
2069
2070 case StabObjectFileName:
2071 // N_OSO - object file name: name,,0,0,st_mtime
2072 type = eSymbolTypeObjectFile;
2073 break;
2074
2075 case StabLocalSymbol:
2076 // N_LSYM - local sym: name,,NO_SECT,type,offset
2077 type = eSymbolTypeLocal;
2078 break;
2079
2080 //----------------------------------------------------------------------
2081 // INCL scopes
2082 //----------------------------------------------------------------------
2083 case StabBeginIncludeFileName:
2084 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2085 // We use the current number of symbols in the symbol table in lieu of
2086 // using nlist_idx in case we ever start trimming entries out
2087 N_INCL_indexes.push_back(sym_idx);
2088 type = eSymbolTypeScopeBegin;
2089 break;
2090
2091 case StabEndIncludeFile:
2092 // N_EINCL - include file end: name,,NO_SECT,0,0
2093 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2094 // so that we can always skip the entire symbol if we need to navigate
2095 // more quickly at the source level when parsing STABS
2096 if ( !N_INCL_indexes.empty() )
2097 {
2098 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2099 symbol_ptr->SetByteSize(sym_idx + 1);
2100 symbol_ptr->SetSizeIsSibling(true);
2101 N_INCL_indexes.pop_back();
2102 }
2103 type = eSymbolTypeScopeEnd;
2104 break;
2105
2106 case StabIncludeFileName:
2107 // N_SOL - #included file name: name,,n_sect,0,address
2108 type = eSymbolTypeHeaderFile;
2109
2110 // We currently don't use the header files on darwin
2111 if (minimize)
2112 add_nlist = false;
2113 break;
2114
2115 case StabCompilerParameters:
2116 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2117 type = eSymbolTypeCompiler;
2118 break;
2119
2120 case StabCompilerVersion:
2121 // N_VERSION - compiler version: name,,NO_SECT,0,0
2122 type = eSymbolTypeCompiler;
2123 break;
2124
2125 case StabCompilerOptLevel:
2126 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2127 type = eSymbolTypeCompiler;
2128 break;
2129
2130 case StabParameter:
2131 // N_PSYM - parameter: name,,NO_SECT,type,offset
2132 type = eSymbolTypeVariable;
2133 break;
2134
2135 case StabAlternateEntry:
2136 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2137 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2138 type = eSymbolTypeLineEntry;
2139 break;
2140
2141 //----------------------------------------------------------------------
2142 // Left and Right Braces
2143 //----------------------------------------------------------------------
2144 case StabLeftBracket:
2145 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2146 // We use the current number of symbols in the symbol table in lieu of
2147 // using nlist_idx in case we ever start trimming entries out
2148 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2149 N_BRAC_indexes.push_back(sym_idx);
2150 type = eSymbolTypeScopeBegin;
2151 break;
2152
2153 case StabRightBracket:
2154 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2155 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2156 // so that we can always skip the entire symbol if we need to navigate
2157 // more quickly at the source level when parsing STABS
2158 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2159 if ( !N_BRAC_indexes.empty() )
2160 {
2161 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2162 symbol_ptr->SetByteSize(sym_idx + 1);
2163 symbol_ptr->SetSizeIsSibling(true);
2164 N_BRAC_indexes.pop_back();
2165 }
2166 type = eSymbolTypeScopeEnd;
2167 break;
2168
2169 case StabDeletedIncludeFile:
2170 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2171 type = eSymbolTypeHeaderFile;
2172 break;
2173
2174 //----------------------------------------------------------------------
2175 // COMM scopes
2176 //----------------------------------------------------------------------
2177 case StabBeginCommon:
2178 // N_BCOMM - begin common: name,,NO_SECT,0,0
2179 // We use the current number of symbols in the symbol table in lieu of
2180 // using nlist_idx in case we ever start trimming entries out
2181 type = eSymbolTypeScopeBegin;
2182 N_COMM_indexes.push_back(sym_idx);
2183 break;
2184
2185 case StabEndCommonLocal:
2186 // N_ECOML - end common (local name): 0,,n_sect,0,address
2187 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2188 // Fall through
2189
2190 case StabEndCommon:
2191 // N_ECOMM - end common: name,,n_sect,0,0
2192 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2193 // so that we can always skip the entire symbol if we need to navigate
2194 // more quickly at the source level when parsing STABS
2195 if ( !N_COMM_indexes.empty() )
2196 {
2197 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2198 symbol_ptr->SetByteSize(sym_idx + 1);
2199 symbol_ptr->SetSizeIsSibling(true);
2200 N_COMM_indexes.pop_back();
2201 }
2202 type = eSymbolTypeScopeEnd;
2203 break;
2204
2205 case StabLength:
2206 // N_LENG - second stab entry with length information
2207 type = eSymbolTypeAdditional;
2208 break;
2209
2210 default: break;
2211 }
2212 }
2213 else
2214 {
2215 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2216 uint8_t n_type = NlistMaskType & nlist.n_type;
2217 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2218
2219 switch (n_type)
2220 {
2221 case NListTypeIndirect: // N_INDR - Fall through
2222 case NListTypePreboundUndefined:// N_PBUD - Fall through
2223 case NListTypeUndefined: // N_UNDF
2224 type = eSymbolTypeUndefined;
2225 break;
2226
2227 case NListTypeAbsolute: // N_ABS
2228 type = eSymbolTypeAbsolute;
2229 break;
2230
2231 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002232 {
2233 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002234
Greg Clayton01e6a582012-11-27 01:52:16 +00002235 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002236 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002237 // TODO: warn about this?
2238 add_nlist = false;
2239 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002240 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002241
2242 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002243 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002244 type = eSymbolTypeException;
2245 }
2246 else
2247 {
2248 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002249
Greg Clayton01e6a582012-11-27 01:52:16 +00002250 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002251 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002252 case SectionTypeRegular: break; // regular section
2253 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2254 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2255 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2256 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2257 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2258 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2259 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2260 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2261 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2262 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2263 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2264 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2265 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2266 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2267 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2268 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2269 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002270 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002271
Greg Clayton01e6a582012-11-27 01:52:16 +00002272 if (type == eSymbolTypeInvalid)
2273 {
2274 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2275 if (symbol_section->IsDescendant (text_section_sp.get()))
2276 {
2277 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2278 SectionAttrUserSelfModifyingCode |
2279 SectionAttrSytemSomeInstructions))
2280 type = eSymbolTypeData;
2281 else
2282 type = eSymbolTypeCode;
2283 }
2284 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002285 {
2286 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2287 {
2288 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002289
Greg Clayton01e6a582012-11-27 01:52:16 +00002290 if (symbol_name &&
2291 symbol_name[0] == '_' &&
2292 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002293 symbol_name[2] == 'B')
2294 {
2295 llvm::StringRef symbol_name_ref(symbol_name);
2296 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2297 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2298 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2299 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2300 {
2301 symbol_name_non_abi_mangled = symbol_name + 1;
2302 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2303 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002304 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002305 }
2306 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2307 {
2308 symbol_name_non_abi_mangled = symbol_name + 1;
2309 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2310 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002311 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002312 }
2313 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2314 {
2315 symbol_name_non_abi_mangled = symbol_name + 1;
2316 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2317 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002318 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002319 }
2320 }
2321 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002322 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002323 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002324 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002325 }
2326 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002327 {
2328 type = eSymbolTypeData;
2329 }
2330 }
2331 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2332 {
2333 type = eSymbolTypeTrampoline;
2334 }
2335 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2336 {
2337 type = eSymbolTypeRuntime;
2338 if (symbol_name && symbol_name[0] == '.')
2339 {
2340 llvm::StringRef symbol_name_ref(symbol_name);
2341 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2342 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002343 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002344 symbol_name_non_abi_mangled = symbol_name;
2345 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2346 type = eSymbolTypeObjCClass;
2347 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002348 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002349 }
2350 }
2351 }
Jason Molendab62abd52012-06-21 01:51:02 +00002352 }
2353 }
Jason Molendab62abd52012-06-21 01:51:02 +00002354 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002355 }
Jason Molendab62abd52012-06-21 01:51:02 +00002356 }
2357
2358 if (add_nlist)
2359 {
2360 uint64_t symbol_value = nlist.n_value;
2361 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002362
Jason Molendab62abd52012-06-21 01:51:02 +00002363 if (symbol_name_non_abi_mangled)
2364 {
Jason Molenda292cca82012-07-20 03:35:44 +00002365 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2366 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002367 }
2368 else
2369 {
2370 if (symbol_name && symbol_name[0] == '_')
2371 {
2372 symbol_name_is_mangled = symbol_name[1] == '_';
2373 symbol_name++; // Skip the leading underscore
2374 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002375
Jason Molendab62abd52012-06-21 01:51:02 +00002376 if (symbol_name)
2377 {
Jason Molenda292cca82012-07-20 03:35:44 +00002378 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002379 }
2380 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002381
Jason Molendab62abd52012-06-21 01:51:02 +00002382 if (is_debug == false)
2383 {
2384 if (type == eSymbolTypeCode)
2385 {
2386 // See if we can find a N_FUN entry for any code symbols.
2387 // If we do find a match, and the name matches, then we
2388 // can merge the two into just the function symbol to avoid
2389 // duplicate entries in the symbol table
2390 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2391 if (pos != N_FUN_addr_to_sym_idx.end())
2392 {
2393 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2394 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2395 {
2396 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2397 // We just need the flags from the linker symbol, so put these flags
2398 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2399 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2400 sym[sym_idx].Clear();
2401 continue;
2402 }
2403 }
2404 }
2405 else if (type == eSymbolTypeData)
2406 {
2407 // See if we can find a N_STSYM entry for any data symbols.
2408 // If we do find a match, and the name matches, then we
2409 // can merge the two into just the Static symbol to avoid
2410 // duplicate entries in the symbol table
2411 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2412 if (pos != N_STSYM_addr_to_sym_idx.end())
2413 {
2414 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2415 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2416 {
2417 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2418 // We just need the flags from the linker symbol, so put these flags
2419 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2420 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2421 sym[sym_idx].Clear();
2422 continue;
2423 }
2424 }
2425 }
2426 }
2427 if (symbol_section)
2428 {
2429 const addr_t section_file_addr = symbol_section->GetFileAddress();
2430 if (symbol_byte_size == 0 && function_starts_count > 0)
2431 {
2432 addr_t symbol_lookup_file_addr = nlist.n_value;
2433 // Do an exact address match for non-ARM addresses, else get the closest since
2434 // the symbol might be a thumb symbol which has an address with bit zero set
2435 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2436 if (is_arm && func_start_entry)
2437 {
2438 // Verify that the function start address is the symbol address (ARM)
2439 // or the symbol address + 1 (thumb)
2440 if (func_start_entry->addr != symbol_lookup_file_addr &&
2441 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2442 {
2443 // Not the right entry, NULL it out...
2444 func_start_entry = NULL;
2445 }
2446 }
2447 if (func_start_entry)
2448 {
2449 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002450
Jason Molendab62abd52012-06-21 01:51:02 +00002451 addr_t symbol_file_addr = func_start_entry->addr;
2452 uint32_t symbol_flags = 0;
2453 if (is_arm)
2454 {
2455 if (symbol_file_addr & 1)
2456 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2457 symbol_file_addr &= 0xfffffffffffffffeull;
2458 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002459
Jason Molendab62abd52012-06-21 01:51:02 +00002460 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2461 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2462 if (next_func_start_entry)
2463 {
2464 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2465 // Be sure the clear the Thumb address bit when we calculate the size
2466 // from the current and next address
2467 if (is_arm)
2468 next_symbol_file_addr &= 0xfffffffffffffffeull;
2469 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2470 }
2471 else
2472 {
2473 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2474 }
2475 }
2476 }
2477 symbol_value -= section_file_addr;
2478 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002479
Jason Molendab62abd52012-06-21 01:51:02 +00002480 sym[sym_idx].SetID (nlist_idx);
2481 sym[sym_idx].SetType (type);
2482 sym[sym_idx].GetAddress().SetSection (symbol_section);
2483 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2484 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002485
Jason Molendab62abd52012-06-21 01:51:02 +00002486 if (symbol_byte_size > 0)
2487 sym[sym_idx].SetByteSize(symbol_byte_size);
2488
Greg Clayton01e6a582012-11-27 01:52:16 +00002489 if (demangled_is_synthesized)
2490 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002491 ++sym_idx;
2492 }
2493 else
2494 {
2495 sym[sym_idx].Clear();
2496 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002497
Jason Molendab62abd52012-06-21 01:51:02 +00002498 }
2499 /////////////////////////////
2500 }
2501 break; // No more entries to consider
2502 }
2503 }
2504 }
2505 }
2506 }
2507 }
2508 }
2509
2510 // Must reset this in case it was mutated above!
2511 nlist_data_offset = 0;
2512#endif
2513
2514 // If the sym array was not created while parsing the DSC unmapped
2515 // symbols, create it now.
2516 if (sym == NULL)
2517 {
2518 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2519 num_syms = symtab->GetNumSymbols();
2520 }
2521
2522 if (unmapped_local_symbols_found)
2523 {
2524 assert(m_dysymtab.ilocalsym == 0);
2525 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2526 nlist_idx = m_dysymtab.nlocalsym;
2527 }
2528 else
2529 {
2530 nlist_idx = 0;
2531 }
2532
2533 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002534 {
2535 struct nlist_64 nlist;
2536 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2537 break;
2538
2539 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2540 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2541 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2542 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2543 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2544
2545 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002546 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002547
Greg Clayton3a5dc012012-05-25 17:04:00 +00002548 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002549 {
2550 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002551
Greg Claytondd29b972012-05-18 23:20:01 +00002552 if (symbol_name == NULL)
2553 {
2554 // No symbol should be NULL, even the symbols with no
2555 // string values should have an offset zero which points
2556 // to an empty C-string
2557 Host::SystemLog (Host::eSystemLogError,
Jason Molenda9badb6c2013-03-06 23:19:17 +00002558 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002559 nlist_idx,
2560 nlist.n_strx,
2561 module_sp->GetFileSpec().GetDirectory().GetCString(),
2562 module_sp->GetFileSpec().GetFilename().GetCString());
2563 continue;
2564 }
2565 if (symbol_name[0] == '\0')
2566 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002567 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002568 else
2569 {
2570 const addr_t str_addr = strtab_addr + nlist.n_strx;
2571 Error str_error;
2572 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2573 symbol_name = memory_symbol_name.c_str();
2574 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002575 const char *symbol_name_non_abi_mangled = NULL;
2576
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002577 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002578 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002579 bool add_nlist = true;
2580 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002581 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002582
2583 assert (sym_idx < num_syms);
2584
2585 sym[sym_idx].SetDebug (is_debug);
2586
2587 if (is_debug)
2588 {
2589 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002590 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002591 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002592 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2593 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002594
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002595 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2596 // have the same address, but we want to ensure that we always find only the real symbol,
2597 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2598 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2599 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2600 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002601
2602 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002603 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2604 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2605 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2606 add_nlist = false;
2607 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002608 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002609 sym[sym_idx].SetExternal(true);
2610 if (nlist.n_value != 0)
2611 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2612 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002613 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002614 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002615
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002616 case StabFunctionName:
2617 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2618 type = eSymbolTypeCompiler;
2619 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002620
Jason Molenda9badb6c2013-03-06 23:19:17 +00002621 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002622 // N_FUN -- procedure: name,,n_sect,linenumber,address
2623 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002624 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002625 type = eSymbolTypeCode;
2626 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002627
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002628 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2629 // We use the current number of symbols in the symbol table in lieu of
2630 // using nlist_idx in case we ever start trimming entries out
2631 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002632 }
2633 else
2634 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002635 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002636
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002637 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002638 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002639 // Copy the size of the function into the original STAB entry so we don't have
2640 // to hunt for it later
2641 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2642 N_FUN_indexes.pop_back();
2643 // We don't really need the end function STAB as it contains the size which
2644 // we already placed with the original symbol, so don't add it if we want a
2645 // minimal symbol table
2646 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002647 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002648 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002649 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002650 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002651
Jason Molenda9badb6c2013-03-06 23:19:17 +00002652 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002653 // N_STSYM -- static symbol: name,,n_sect,type,address
2654 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2655 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2656 type = eSymbolTypeData;
2657 break;
2658
2659 case StabLocalCommon:
2660 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2661 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2662 type = eSymbolTypeCommonBlock;
2663 break;
2664
2665 case StabBeginSymbol:
2666 // N_BNSYM
2667 // We use the current number of symbols in the symbol table in lieu of
2668 // using nlist_idx in case we ever start trimming entries out
2669 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002670 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002671 // Skip these if we want minimal symbol tables
2672 add_nlist = false;
2673 }
2674 else
2675 {
2676 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2677 N_NSYM_indexes.push_back(sym_idx);
2678 type = eSymbolTypeScopeBegin;
2679 }
2680 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002681
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002682 case StabEndSymbol:
2683 // N_ENSYM
2684 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2685 // so that we can always skip the entire symbol if we need to navigate
2686 // more quickly at the source level when parsing STABS
2687 if (minimize)
2688 {
2689 // Skip these if we want minimal symbol tables
2690 add_nlist = false;
2691 }
2692 else
2693 {
2694 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002695 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002696 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2697 symbol_ptr->SetByteSize(sym_idx + 1);
2698 symbol_ptr->SetSizeIsSibling(true);
2699 N_NSYM_indexes.pop_back();
2700 }
2701 type = eSymbolTypeScopeEnd;
2702 }
2703 break;
2704
2705
2706 case StabSourceFileOptions:
2707 // N_OPT - emitted with gcc2_compiled and in gcc source
2708 type = eSymbolTypeCompiler;
2709 break;
2710
2711 case StabRegisterSymbol:
2712 // N_RSYM - register sym: name,,NO_SECT,type,register
2713 type = eSymbolTypeVariable;
2714 break;
2715
2716 case StabSourceLine:
2717 // N_SLINE - src line: 0,,n_sect,linenumber,address
2718 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2719 type = eSymbolTypeLineEntry;
2720 break;
2721
2722 case StabStructureType:
2723 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2724 type = eSymbolTypeVariableType;
2725 break;
2726
2727 case StabSourceFileName:
2728 // N_SO - source file name
2729 type = eSymbolTypeSourceFile;
2730 if (symbol_name == NULL)
2731 {
2732 if (minimize)
2733 add_nlist = false;
2734 if (N_SO_index != UINT32_MAX)
2735 {
2736 // Set the size of the N_SO to the terminating index of this N_SO
2737 // so that we can always skip the entire N_SO if we need to navigate
2738 // more quickly at the source level when parsing STABS
2739 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2740 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2741 symbol_ptr->SetSizeIsSibling(true);
2742 }
2743 N_NSYM_indexes.clear();
2744 N_INCL_indexes.clear();
2745 N_BRAC_indexes.clear();
2746 N_COMM_indexes.clear();
2747 N_FUN_indexes.clear();
2748 N_SO_index = UINT32_MAX;
2749 }
2750 else
2751 {
2752 // We use the current number of symbols in the symbol table in lieu of
2753 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002754 const bool N_SO_has_full_path = symbol_name[0] == '/';
2755 if (N_SO_has_full_path)
2756 {
2757 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2758 {
2759 // We have two consecutive N_SO entries where the first contains a directory
2760 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002761 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002762 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2763 add_nlist = false;
2764 }
2765 else
2766 {
2767 // This is the first entry in a N_SO that contains a directory or
2768 // a full path to the source file
2769 N_SO_index = sym_idx;
2770 }
2771 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002772 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2773 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002774 // This is usually the second N_SO entry that contains just the filename,
2775 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002776 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2777 if (so_path && so_path[0])
2778 {
2779 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002780 const size_t double_slash_pos = full_so_path.find("//");
2781 if (double_slash_pos != std::string::npos)
2782 {
2783 // The linker has been generating bad N_SO entries with doubled up paths
2784 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2785 // and the second is the directory for the source file so you end up with
2786 // a path that looks like "/tmp/src//tmp/src/"
2787 FileSpec so_dir(so_path, false);
2788 if (!so_dir.Exists())
2789 {
2790 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2791 if (so_dir.Exists())
2792 {
2793 // Trim off the incorrect path
2794 full_so_path.erase(0, double_slash_pos + 1);
2795 }
2796 }
2797 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002798 if (*full_so_path.rbegin() != '/')
2799 full_so_path += '/';
2800 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002801 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002802 add_nlist = false;
2803 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2804 }
2805 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002806 else
2807 {
2808 // This could be a relative path to a N_SO
2809 N_SO_index = sym_idx;
2810 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002811 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002812
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002813 break;
2814
2815 case StabObjectFileName:
2816 // N_OSO - object file name: name,,0,0,st_mtime
2817 type = eSymbolTypeObjectFile;
2818 break;
2819
2820 case StabLocalSymbol:
2821 // N_LSYM - local sym: name,,NO_SECT,type,offset
2822 type = eSymbolTypeLocal;
2823 break;
2824
2825 //----------------------------------------------------------------------
2826 // INCL scopes
2827 //----------------------------------------------------------------------
2828 case StabBeginIncludeFileName:
2829 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2830 // We use the current number of symbols in the symbol table in lieu of
2831 // using nlist_idx in case we ever start trimming entries out
2832 N_INCL_indexes.push_back(sym_idx);
2833 type = eSymbolTypeScopeBegin;
2834 break;
2835
2836 case StabEndIncludeFile:
2837 // N_EINCL - include file end: name,,NO_SECT,0,0
2838 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2839 // so that we can always skip the entire symbol if we need to navigate
2840 // more quickly at the source level when parsing STABS
2841 if ( !N_INCL_indexes.empty() )
2842 {
2843 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2844 symbol_ptr->SetByteSize(sym_idx + 1);
2845 symbol_ptr->SetSizeIsSibling(true);
2846 N_INCL_indexes.pop_back();
2847 }
2848 type = eSymbolTypeScopeEnd;
2849 break;
2850
2851 case StabIncludeFileName:
2852 // N_SOL - #included file name: name,,n_sect,0,address
2853 type = eSymbolTypeHeaderFile;
2854
2855 // We currently don't use the header files on darwin
2856 if (minimize)
2857 add_nlist = false;
2858 break;
2859
Jason Molenda9badb6c2013-03-06 23:19:17 +00002860 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002861 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2862 type = eSymbolTypeCompiler;
2863 break;
2864
2865 case StabCompilerVersion:
2866 // N_VERSION - compiler version: name,,NO_SECT,0,0
2867 type = eSymbolTypeCompiler;
2868 break;
2869
2870 case StabCompilerOptLevel:
2871 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2872 type = eSymbolTypeCompiler;
2873 break;
2874
2875 case StabParameter:
2876 // N_PSYM - parameter: name,,NO_SECT,type,offset
2877 type = eSymbolTypeVariable;
2878 break;
2879
2880 case StabAlternateEntry:
2881 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2882 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2883 type = eSymbolTypeLineEntry;
2884 break;
2885
2886 //----------------------------------------------------------------------
2887 // Left and Right Braces
2888 //----------------------------------------------------------------------
2889 case StabLeftBracket:
2890 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2891 // We use the current number of symbols in the symbol table in lieu of
2892 // using nlist_idx in case we ever start trimming entries out
2893 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2894 N_BRAC_indexes.push_back(sym_idx);
2895 type = eSymbolTypeScopeBegin;
2896 break;
2897
2898 case StabRightBracket:
2899 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2900 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2901 // so that we can always skip the entire symbol if we need to navigate
2902 // more quickly at the source level when parsing STABS
2903 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2904 if ( !N_BRAC_indexes.empty() )
2905 {
2906 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2907 symbol_ptr->SetByteSize(sym_idx + 1);
2908 symbol_ptr->SetSizeIsSibling(true);
2909 N_BRAC_indexes.pop_back();
2910 }
2911 type = eSymbolTypeScopeEnd;
2912 break;
2913
2914 case StabDeletedIncludeFile:
2915 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2916 type = eSymbolTypeHeaderFile;
2917 break;
2918
2919 //----------------------------------------------------------------------
2920 // COMM scopes
2921 //----------------------------------------------------------------------
2922 case StabBeginCommon:
2923 // N_BCOMM - begin common: name,,NO_SECT,0,0
2924 // We use the current number of symbols in the symbol table in lieu of
2925 // using nlist_idx in case we ever start trimming entries out
2926 type = eSymbolTypeScopeBegin;
2927 N_COMM_indexes.push_back(sym_idx);
2928 break;
2929
2930 case StabEndCommonLocal:
2931 // N_ECOML - end common (local name): 0,,n_sect,0,address
2932 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2933 // Fall through
2934
2935 case StabEndCommon:
2936 // N_ECOMM - end common: name,,n_sect,0,0
2937 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2938 // so that we can always skip the entire symbol if we need to navigate
2939 // more quickly at the source level when parsing STABS
2940 if ( !N_COMM_indexes.empty() )
2941 {
2942 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2943 symbol_ptr->SetByteSize(sym_idx + 1);
2944 symbol_ptr->SetSizeIsSibling(true);
2945 N_COMM_indexes.pop_back();
2946 }
2947 type = eSymbolTypeScopeEnd;
2948 break;
2949
2950 case StabLength:
2951 // N_LENG - second stab entry with length information
2952 type = eSymbolTypeAdditional;
2953 break;
2954
2955 default: break;
2956 }
2957 }
2958 else
2959 {
2960 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2961 uint8_t n_type = NlistMaskType & nlist.n_type;
2962 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2963
2964 switch (n_type)
2965 {
2966 case NListTypeIndirect: // N_INDR - Fall through
2967 case NListTypePreboundUndefined:// N_PBUD - Fall through
2968 case NListTypeUndefined: // N_UNDF
2969 type = eSymbolTypeUndefined;
2970 break;
2971
2972 case NListTypeAbsolute: // N_ABS
2973 type = eSymbolTypeAbsolute;
2974 break;
2975
2976 case NListTypeSection: // N_SECT
2977 {
2978 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2979
Sean Callananb386d822012-08-09 00:50:26 +00002980 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002981 {
2982 // TODO: warn about this?
2983 add_nlist = false;
2984 break;
2985 }
2986
2987 if (TEXT_eh_frame_sectID == nlist.n_sect)
2988 {
2989 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00002990 }
2991 else
2992 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002993 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
2994
2995 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00002996 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002997 case SectionTypeRegular: break; // regular section
2998 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2999 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3000 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3001 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3002 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3003 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3004 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3005 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3006 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3007 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3008 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3009 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3010 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3011 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3012 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3013 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3014 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00003015 }
Chris Lattner24943d22010-06-08 16:52:24 +00003016
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003017 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00003018 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003019 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3020 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003021 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003022 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3023 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003024 SectionAttrSytemSomeInstructions))
3025 type = eSymbolTypeData;
3026 else
3027 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00003028 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003029 else
3030 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003031 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003032 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00003033 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003034 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00003035
Jason Molenda9badb6c2013-03-06 23:19:17 +00003036 if (symbol_name &&
3037 symbol_name[0] == '_' &&
3038 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003039 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00003040 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003041 llvm::StringRef symbol_name_ref(symbol_name);
3042 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3043 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3044 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3045 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00003046 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003047 symbol_name_non_abi_mangled = symbol_name + 1;
3048 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3049 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003050 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003051 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003052 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003053 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003054 symbol_name_non_abi_mangled = symbol_name + 1;
3055 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3056 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003057 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003058 }
3059 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3060 {
3061 symbol_name_non_abi_mangled = symbol_name + 1;
3062 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3063 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003064 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003065 }
3066 }
3067 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003068 else
3069 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3070 {
3071 type = eSymbolTypeException;
3072 }
3073 else
3074 {
3075 type = eSymbolTypeData;
3076 }
3077 }
3078 else
3079 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3080 {
3081 type = eSymbolTypeTrampoline;
3082 }
3083 else
3084 if (symbol_section->IsDescendant(objc_section_sp.get()))
3085 {
3086 type = eSymbolTypeRuntime;
3087 if (symbol_name && symbol_name[0] == '.')
3088 {
3089 llvm::StringRef symbol_name_ref(symbol_name);
3090 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3091 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3092 {
3093 symbol_name_non_abi_mangled = symbol_name;
3094 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3095 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003096 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003097 }
3098 }
Chris Lattner24943d22010-06-08 16:52:24 +00003099 }
3100 }
3101 }
3102 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003103 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003104 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003105 }
3106
3107 if (add_nlist)
3108 {
3109 uint64_t symbol_value = nlist.n_value;
3110 bool symbol_name_is_mangled = false;
3111
3112 if (symbol_name_non_abi_mangled)
3113 {
Greg Claytonc0240042012-07-18 23:18:10 +00003114 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3115 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003116 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003117 else
3118 {
3119 if (symbol_name && symbol_name[0] == '_')
3120 {
3121 symbol_name_is_mangled = symbol_name[1] == '_';
3122 symbol_name++; // Skip the leading underscore
3123 }
3124
3125 if (symbol_name)
3126 {
Greg Claytonc0240042012-07-18 23:18:10 +00003127 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003128 }
3129 }
3130
3131 if (is_debug == false)
3132 {
3133 if (type == eSymbolTypeCode)
3134 {
3135 // See if we can find a N_FUN entry for any code symbols.
3136 // If we do find a match, and the name matches, then we
3137 // can merge the two into just the function symbol to avoid
3138 // duplicate entries in the symbol table
3139 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3140 if (pos != N_FUN_addr_to_sym_idx.end())
3141 {
3142 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3143 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3144 {
3145 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3146 // We just need the flags from the linker symbol, so put these flags
3147 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3148 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3149 sym[sym_idx].Clear();
3150 continue;
3151 }
3152 }
3153 }
3154 else if (type == eSymbolTypeData)
3155 {
3156 // See if we can find a N_STSYM entry for any data symbols.
3157 // If we do find a match, and the name matches, then we
3158 // can merge the two into just the Static symbol to avoid
3159 // duplicate entries in the symbol table
3160 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3161 if (pos != N_STSYM_addr_to_sym_idx.end())
3162 {
3163 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3164 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3165 {
3166 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3167 // We just need the flags from the linker symbol, so put these flags
3168 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3169 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3170 sym[sym_idx].Clear();
3171 continue;
3172 }
3173 }
3174 }
3175 }
3176 if (symbol_section)
3177 {
3178 const addr_t section_file_addr = symbol_section->GetFileAddress();
3179 if (symbol_byte_size == 0 && function_starts_count > 0)
3180 {
Greg Claytond2653c22012-03-14 01:53:24 +00003181 addr_t symbol_lookup_file_addr = nlist.n_value;
3182 // Do an exact address match for non-ARM addresses, else get the closest since
3183 // the symbol might be a thumb symbol which has an address with bit zero set
3184 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3185 if (is_arm && func_start_entry)
3186 {
3187 // Verify that the function start address is the symbol address (ARM)
3188 // or the symbol address + 1 (thumb)
3189 if (func_start_entry->addr != symbol_lookup_file_addr &&
3190 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3191 {
3192 // Not the right entry, NULL it out...
3193 func_start_entry = NULL;
3194 }
3195 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003196 if (func_start_entry)
3197 {
3198 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003199
Greg Claytond2653c22012-03-14 01:53:24 +00003200 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003201 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003202 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003203
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003204 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3205 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3206 if (next_func_start_entry)
3207 {
Greg Claytond2653c22012-03-14 01:53:24 +00003208 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3209 // Be sure the clear the Thumb address bit when we calculate the size
3210 // from the current and next address
3211 if (is_arm)
3212 next_symbol_file_addr &= 0xfffffffffffffffeull;
3213 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 +00003214 }
3215 else
3216 {
Greg Claytond2653c22012-03-14 01:53:24 +00003217 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003218 }
3219 }
3220 }
3221 symbol_value -= section_file_addr;
3222 }
3223
3224 sym[sym_idx].SetID (nlist_idx);
3225 sym[sym_idx].SetType (type);
3226 sym[sym_idx].GetAddress().SetSection (symbol_section);
3227 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3228 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3229
3230 if (symbol_byte_size > 0)
3231 sym[sym_idx].SetByteSize(symbol_byte_size);
3232
Greg Clayton01e6a582012-11-27 01:52:16 +00003233 if (demangled_is_synthesized)
3234 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3235
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003236 ++sym_idx;
3237 }
3238 else
3239 {
3240 sym[sym_idx].Clear();
3241 }
3242
3243 }
3244
3245 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3246 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3247 // such entries by figuring out what the address for the global is by looking up this non-STAB
3248 // entry and copying the value into the debug symbol's value to save us the hassle in the
3249 // debug symbol parser.
3250
3251 Symbol *global_symbol = NULL;
3252 for (nlist_idx = 0;
3253 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3254 nlist_idx++)
3255 {
3256 if (global_symbol->GetAddress().GetFileAddress() == 0)
3257 {
3258 std::vector<uint32_t> indexes;
3259 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3260 {
3261 std::vector<uint32_t>::const_iterator pos;
3262 std::vector<uint32_t>::const_iterator end = indexes.end();
3263 for (pos = indexes.begin(); pos != end; ++pos)
3264 {
3265 symbol_ptr = symtab->SymbolAtIndex(*pos);
3266 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3267 {
3268 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3269 break;
3270 }
3271 }
3272 }
Chris Lattner24943d22010-06-08 16:52:24 +00003273 }
3274 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003275
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003276 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3277
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003278 if (function_starts_count > 0)
3279 {
3280 char synthetic_function_symbol[PATH_MAX];
3281 uint32_t num_synthetic_function_symbols = 0;
3282 for (i=0; i<function_starts_count; ++i)
3283 {
3284 if (function_starts.GetEntryRef (i).data == false)
3285 ++num_synthetic_function_symbols;
3286 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003287
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003288 if (num_synthetic_function_symbols > 0)
3289 {
3290 if (num_syms < sym_idx + num_synthetic_function_symbols)
3291 {
3292 num_syms = sym_idx + num_synthetic_function_symbols;
3293 sym = symtab->Resize (num_syms);
3294 }
3295 uint32_t synthetic_function_symbol_idx = 0;
3296 for (i=0; i<function_starts_count; ++i)
3297 {
3298 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3299 if (func_start_entry->data == false)
3300 {
Greg Claytond2653c22012-03-14 01:53:24 +00003301 addr_t symbol_file_addr = func_start_entry->addr;
3302 uint32_t symbol_flags = 0;
3303 if (is_arm)
3304 {
3305 if (symbol_file_addr & 1)
3306 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3307 symbol_file_addr &= 0xfffffffffffffffeull;
3308 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003309 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003310 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003311 {
3312 SectionSP symbol_section (symbol_addr.GetSection());
3313 uint32_t symbol_byte_size = 0;
3314 if (symbol_section)
3315 {
3316 const addr_t section_file_addr = symbol_section->GetFileAddress();
3317 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3318 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3319 if (next_func_start_entry)
3320 {
Greg Claytond2653c22012-03-14 01:53:24 +00003321 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3322 if (is_arm)
3323 next_symbol_file_addr &= 0xfffffffffffffffeull;
3324 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 +00003325 }
3326 else
3327 {
Greg Claytond2653c22012-03-14 01:53:24 +00003328 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003329 }
3330 snprintf (synthetic_function_symbol,
3331 sizeof(synthetic_function_symbol),
3332 "___lldb_unnamed_function%u$$%s",
3333 ++synthetic_function_symbol_idx,
3334 module_sp->GetFileSpec().GetFilename().GetCString());
3335 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003336 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003337 sym[sym_idx].SetType (eSymbolTypeCode);
3338 sym[sym_idx].SetIsSynthetic (true);
3339 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003340 if (symbol_flags)
3341 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003342 if (symbol_byte_size)
3343 sym[sym_idx].SetByteSize (symbol_byte_size);
3344 ++sym_idx;
3345 }
3346 }
3347 }
3348 }
3349 }
3350 }
3351
3352 // Trim our symbols down to just what we ended up with after
3353 // removing any symbols.
3354 if (sym_idx < num_syms)
3355 {
3356 num_syms = sym_idx;
3357 sym = symtab->Resize (num_syms);
3358 }
3359
3360 // Now synthesize indirect symbols
3361 if (m_dysymtab.nindirectsyms != 0)
3362 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003363 if (indirect_symbol_index_data.GetByteSize())
3364 {
3365 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3366
3367 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3368 {
3369 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3370 {
3371 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3372 if (symbol_stub_byte_size == 0)
3373 continue;
3374
3375 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3376
3377 if (num_symbol_stubs == 0)
3378 continue;
3379
3380 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3381 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3382 {
3383 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3384 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 +00003385 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003386 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3387 {
3388 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3389 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3390 continue;
3391
3392 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3393 Symbol *stub_symbol = NULL;
3394 if (index_pos != end_index_pos)
3395 {
3396 // We have a remapping from the original nlist index to
3397 // a current symbol index, so just look this up by index
3398 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3399 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003400 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003401 {
3402 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003403 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003404 // S_SYMBOL_STUBS
3405 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3406 }
3407
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003408 if (stub_symbol)
3409 {
3410 Address so_addr(symbol_stub_addr, section_list);
3411
3412 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3413 {
3414 // Change the external symbol into a trampoline that makes sense
3415 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3416 // can re-use them so we don't have to make up a synthetic symbol
3417 // for no good reason.
3418 stub_symbol->SetType (eSymbolTypeTrampoline);
3419 stub_symbol->SetExternal (false);
3420 stub_symbol->GetAddress() = so_addr;
3421 stub_symbol->SetByteSize (symbol_stub_byte_size);
3422 }
3423 else
3424 {
3425 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003426 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003427 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003428 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003429 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003430 stub_symbol = NULL; // this pointer no longer valid
3431 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003432 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003433 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003434 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3435 sym[sym_idx].SetIsSynthetic (true);
3436 sym[sym_idx].GetAddress() = so_addr;
3437 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3438 ++sym_idx;
3439 }
3440 }
Greg Claytond4330e62012-09-05 01:38:55 +00003441 else
3442 {
3443 if (log)
3444 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3445 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003446 }
3447 }
3448 }
3449 }
3450 }
3451 }
3452 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003453 }
3454 return 0;
3455}
3456
3457
3458void
3459ObjectFileMachO::Dump (Stream *s)
3460{
Greg Clayton9482f052012-03-13 23:14:29 +00003461 ModuleSP module_sp(GetModule());
3462 if (module_sp)
3463 {
3464 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3465 s->Printf("%p: ", this);
3466 s->Indent();
3467 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3468 s->PutCString("ObjectFileMachO64");
3469 else
3470 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003471
Greg Clayton9482f052012-03-13 23:14:29 +00003472 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003473
Greg Clayton9482f052012-03-13 23:14:29 +00003474 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003475
Greg Clayton9482f052012-03-13 23:14:29 +00003476 if (m_sections_ap.get())
3477 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003478
Greg Clayton9482f052012-03-13 23:14:29 +00003479 if (m_symtab_ap.get())
3480 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3481 }
Chris Lattner24943d22010-06-08 16:52:24 +00003482}
3483
3484
3485bool
Greg Clayton0467c782011-02-04 18:53:10 +00003486ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003487{
Greg Clayton9482f052012-03-13 23:14:29 +00003488 ModuleSP module_sp(GetModule());
3489 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003490 {
Greg Clayton9482f052012-03-13 23:14:29 +00003491 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3492 struct uuid_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003493 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003494 uint32_t i;
3495 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003496 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003497 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003498 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3499 break;
3500
3501 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00003502 {
Greg Clayton9482f052012-03-13 23:14:29 +00003503 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003504
Greg Clayton9482f052012-03-13 23:14:29 +00003505 if (uuid_bytes)
3506 {
Sean Callanana5689d52012-07-12 18:04:03 +00003507 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3508 // We pretend these object files have no UUID to prevent crashing.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003509
Sean Callanana5689d52012-07-12 18:04:03 +00003510 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3511 0x3b, 0xa8,
3512 0x4b, 0x16,
3513 0xb6, 0xa4,
3514 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
Jason Molenda9badb6c2013-03-06 23:19:17 +00003515
Sean Callanana5689d52012-07-12 18:04:03 +00003516 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3517 return false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003518
Greg Clayton9482f052012-03-13 23:14:29 +00003519 uuid->SetBytes (uuid_bytes);
3520 return true;
3521 }
3522 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003523 }
Greg Clayton9482f052012-03-13 23:14:29 +00003524 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003525 }
Chris Lattner24943d22010-06-08 16:52:24 +00003526 }
3527 return false;
3528}
3529
3530
3531uint32_t
3532ObjectFileMachO::GetDependentModules (FileSpecList& files)
3533{
Chris Lattner24943d22010-06-08 16:52:24 +00003534 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003535 ModuleSP module_sp(GetModule());
3536 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003537 {
Greg Clayton9482f052012-03-13 23:14:29 +00003538 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3539 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003540 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003541 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3542 uint32_t i;
3543 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003544 {
Greg Clayton9482f052012-03-13 23:14:29 +00003545 const uint32_t cmd_offset = offset;
3546 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3547 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003548
Greg Clayton9482f052012-03-13 23:14:29 +00003549 switch (load_cmd.cmd)
3550 {
3551 case LoadCommandDylibLoad:
3552 case LoadCommandDylibLoadWeak:
3553 case LoadCommandDylibReexport:
3554 case LoadCommandDynamicLinkerLoad:
3555 case LoadCommandFixedVMShlibLoad:
3556 case LoadCommandDylibLoadUpward:
3557 {
3558 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3559 const char *path = m_data.PeekCStr(name_offset);
3560 // Skip any path that starts with '@' since these are usually:
3561 // @executable_path/.../file
3562 // @rpath/.../file
3563 if (path && path[0] != '@')
3564 {
3565 FileSpec file_spec(path, resolve_path);
3566 if (files.AppendIfUnique(file_spec))
3567 count++;
3568 }
3569 }
3570 break;
3571
3572 default:
3573 break;
3574 }
3575 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003576 }
Chris Lattner24943d22010-06-08 16:52:24 +00003577 }
3578 return count;
3579}
3580
Jim Ingham28775942011-03-07 23:44:08 +00003581lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003582ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003583{
3584 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3585 // is initialized to an invalid address, so we can just return that.
3586 // 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 +00003587
Jim Ingham28775942011-03-07 23:44:08 +00003588 if (!IsExecutable() || m_entry_point_address.IsValid())
3589 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003590
3591 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003592 // /usr/include/mach-o.h, but it is basically:
3593 //
3594 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3595 // uint32_t count - this is the count of longs in the thread state data
3596 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3597 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003598 //
Jim Ingham28775942011-03-07 23:44:08 +00003599 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3600 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3601 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3602 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3603 //
3604 // For now we hard-code the offsets and flavors we need:
3605 //
3606 //
3607
Greg Clayton9482f052012-03-13 23:14:29 +00003608 ModuleSP module_sp(GetModule());
3609 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003610 {
Greg Clayton9482f052012-03-13 23:14:29 +00003611 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3612 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003613 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003614 uint32_t i;
3615 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3616 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003617
Greg Clayton9482f052012-03-13 23:14:29 +00003618 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003619 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003620 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003621 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3622 break;
3623
3624 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003625 {
Greg Clayton9482f052012-03-13 23:14:29 +00003626 case LoadCommandUnixThread:
3627 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003628 {
Greg Clayton9482f052012-03-13 23:14:29 +00003629 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003630 {
Greg Clayton9482f052012-03-13 23:14:29 +00003631 uint32_t flavor = m_data.GetU32(&offset);
3632 uint32_t count = m_data.GetU32(&offset);
3633 if (count == 0)
3634 {
3635 // We've gotten off somehow, log and exit;
3636 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003637 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003638
Greg Clayton9482f052012-03-13 23:14:29 +00003639 switch (m_header.cputype)
3640 {
3641 case llvm::MachO::CPUTypeARM:
3642 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3643 {
3644 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3645 start_address = m_data.GetU32(&offset);
3646 done = true;
3647 }
Jim Ingham28775942011-03-07 23:44:08 +00003648 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003649 case llvm::MachO::CPUTypeI386:
3650 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3651 {
3652 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3653 start_address = m_data.GetU32(&offset);
3654 done = true;
3655 }
3656 break;
3657 case llvm::MachO::CPUTypeX86_64:
3658 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3659 {
3660 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3661 start_address = m_data.GetU64(&offset);
3662 done = true;
3663 }
3664 break;
3665 default:
3666 return m_entry_point_address;
3667 }
3668 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3669 if (done)
3670 break;
3671 offset += count * 4;
3672 }
Jim Ingham28775942011-03-07 23:44:08 +00003673 }
Greg Clayton9482f052012-03-13 23:14:29 +00003674 break;
3675 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003676 {
Greg Clayton9482f052012-03-13 23:14:29 +00003677 ConstString text_segment_name ("__TEXT");
3678 uint64_t entryoffset = m_data.GetU64(&offset);
3679 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3680 if (text_segment_sp)
3681 {
3682 done = true;
3683 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3684 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003685 }
Greg Clayton9482f052012-03-13 23:14:29 +00003686
3687 default:
3688 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003689 }
Greg Clayton9482f052012-03-13 23:14:29 +00003690 if (done)
3691 break;
Jim Ingham28775942011-03-07 23:44:08 +00003692
Greg Clayton9482f052012-03-13 23:14:29 +00003693 // Go to the next load command:
3694 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003695 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003696
Greg Clayton9482f052012-03-13 23:14:29 +00003697 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003698 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003699 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003700 // of this ObjectFile:
3701 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003702 {
Greg Clayton9482f052012-03-13 23:14:29 +00003703 m_entry_point_address.Clear();
3704 }
3705 }
3706 else
3707 {
3708 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3709 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003710
Greg Clayton9482f052012-03-13 23:14:29 +00003711 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003712
Greg Clayton9482f052012-03-13 23:14:29 +00003713 if (module_sp)
3714 {
3715 SymbolContextList contexts;
3716 SymbolContext context;
3717 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3718 {
3719 if (contexts.GetContextAtIndex(0, context))
3720 m_entry_point_address = context.symbol->GetAddress();
3721 }
Greg Clayton3508c382012-02-24 01:59:29 +00003722 }
3723 }
Jim Ingham28775942011-03-07 23:44:08 +00003724 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003725
Jim Ingham28775942011-03-07 23:44:08 +00003726 return m_entry_point_address;
3727
3728}
3729
Greg Claytonb5a8f142012-02-05 02:38:54 +00003730lldb_private::Address
3731ObjectFileMachO::GetHeaderAddress ()
3732{
3733 lldb_private::Address header_addr;
3734 SectionList *section_list = GetSectionList();
3735 if (section_list)
3736 {
3737 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3738 if (text_segment_sp)
3739 {
Greg Clayton3508c382012-02-24 01:59:29 +00003740 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003741 header_addr.SetOffset (0);
3742 }
3743 }
3744 return header_addr;
3745}
3746
Greg Clayton46c9a352012-02-09 06:16:32 +00003747uint32_t
3748ObjectFileMachO::GetNumThreadContexts ()
3749{
Greg Clayton9482f052012-03-13 23:14:29 +00003750 ModuleSP module_sp(GetModule());
3751 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003752 {
Greg Clayton9482f052012-03-13 23:14:29 +00003753 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3754 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003755 {
Greg Clayton9482f052012-03-13 23:14:29 +00003756 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003757 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003758 FileRangeArray::Entry file_range;
3759 thread_command thread_cmd;
3760 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003761 {
Greg Clayton9482f052012-03-13 23:14:29 +00003762 const uint32_t cmd_offset = offset;
3763 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3764 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003765
Greg Clayton9482f052012-03-13 23:14:29 +00003766 if (thread_cmd.cmd == LoadCommandThread)
3767 {
3768 file_range.SetRangeBase (offset);
3769 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3770 m_thread_context_offsets.Append (file_range);
3771 }
3772 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003773 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003774 }
3775 }
3776 return m_thread_context_offsets.GetSize();
3777}
3778
3779lldb::RegisterContextSP
3780ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3781{
Greg Clayton46c9a352012-02-09 06:16:32 +00003782 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003783
Greg Clayton9482f052012-03-13 23:14:29 +00003784 ModuleSP module_sp(GetModule());
3785 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003786 {
Greg Clayton9482f052012-03-13 23:14:29 +00003787 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3788 if (!m_thread_context_offsets_valid)
3789 GetNumThreadContexts ();
3790
3791 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003792 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003793 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003794
3795 DataExtractor data (m_data,
3796 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003797 thread_context_file_range->GetByteSize());
3798
3799 switch (m_header.cputype)
3800 {
3801 case llvm::MachO::CPUTypeARM:
3802 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3803 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003804
Jim Ingham6f01c932012-10-12 17:34:26 +00003805 case llvm::MachO::CPUTypeI386:
3806 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3807 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003808
Jim Ingham6f01c932012-10-12 17:34:26 +00003809 case llvm::MachO::CPUTypeX86_64:
3810 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3811 break;
3812 }
Greg Clayton9482f052012-03-13 23:14:29 +00003813 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003814 }
3815 return reg_ctx_sp;
3816}
3817
Greg Claytonb5a8f142012-02-05 02:38:54 +00003818
Greg Claytonca319972011-07-09 00:41:34 +00003819ObjectFile::Type
3820ObjectFileMachO::CalculateType()
3821{
3822 switch (m_header.filetype)
3823 {
3824 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3825 if (GetAddressByteSize () == 4)
3826 {
3827 // 32 bit kexts are just object files, but they do have a valid
3828 // UUID load command.
3829 UUID uuid;
3830 if (GetUUID(&uuid))
3831 {
3832 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003833 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003834 // "OSKextGetCurrentIdentifier" as this is required of kexts
3835 if (m_strata == eStrataInvalid)
3836 m_strata = eStrataKernel;
3837 return eTypeSharedLibrary;
3838 }
3839 }
3840 return eTypeObjectFile;
3841
3842 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3843 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3844 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3845 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3846 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3847 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3848 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3849 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3850 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3851 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3852 default:
3853 break;
3854 }
3855 return eTypeUnknown;
3856}
3857
3858ObjectFile::Strata
3859ObjectFileMachO::CalculateStrata()
3860{
3861 switch (m_header.filetype)
3862 {
3863 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3864 {
3865 // 32 bit kexts are just object files, but they do have a valid
3866 // UUID load command.
3867 UUID uuid;
3868 if (GetUUID(&uuid))
3869 {
3870 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003871 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003872 // "OSKextGetCurrentIdentifier" as this is required of kexts
3873 if (m_type == eTypeInvalid)
3874 m_type = eTypeSharedLibrary;
3875
3876 return eStrataKernel;
3877 }
3878 }
3879 return eStrataUnknown;
3880
3881 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3882 // Check for the MH_DYLDLINK bit in the flags
3883 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003884 {
Greg Claytonca319972011-07-09 00:41:34 +00003885 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003886 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003887 else
Sean Callananac725af2012-02-10 20:22:35 +00003888 {
3889 SectionList *section_list = GetSectionList();
3890 if (section_list)
3891 {
3892 static ConstString g_kld_section_name ("__KLD");
3893 if (section_list->FindSectionByName(g_kld_section_name))
3894 return eStrataKernel;
3895 }
3896 }
3897 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00003898
3899 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
3900 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00003901 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00003902 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
3903 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
3904 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
3905 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
3906 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
3907 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
3908 default:
3909 break;
3910 }
3911 return eStrataUnknown;
3912}
3913
3914
Greg Clayton49f4bf22012-02-22 19:41:02 +00003915uint32_t
3916ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
3917{
Greg Clayton9482f052012-03-13 23:14:29 +00003918 ModuleSP module_sp(GetModule());
3919 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003920 {
Greg Clayton9482f052012-03-13 23:14:29 +00003921 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3922 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003923 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003924 uint32_t version_cmd = 0;
3925 uint64_t version = 0;
3926 uint32_t i;
3927 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003928 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003929 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003930 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3931 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003932
Greg Clayton9482f052012-03-13 23:14:29 +00003933 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003934 {
Greg Clayton9482f052012-03-13 23:14:29 +00003935 if (version_cmd == 0)
3936 {
3937 version_cmd = load_cmd.cmd;
3938 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
3939 break;
3940 version = load_cmd.dylib.current_version;
3941 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003942 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00003943 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00003944 }
Greg Clayton9482f052012-03-13 23:14:29 +00003945 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003946 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003947
Greg Clayton9482f052012-03-13 23:14:29 +00003948 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00003949 {
Greg Clayton9482f052012-03-13 23:14:29 +00003950 if (versions != NULL && num_versions > 0)
3951 {
3952 if (num_versions > 0)
3953 versions[0] = (version & 0xFFFF0000ull) >> 16;
3954 if (num_versions > 1)
3955 versions[1] = (version & 0x0000FF00ull) >> 8;
3956 if (num_versions > 2)
3957 versions[2] = (version & 0x000000FFull);
3958 // Fill in an remaining version numbers with invalid values
3959 for (i=3; i<num_versions; ++i)
3960 versions[i] = UINT32_MAX;
3961 }
3962 // The LC_ID_DYLIB load command has a version with 3 version numbers
3963 // in it, so always return 3
3964 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00003965 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00003966 }
3967 return false;
3968}
3969
Chris Lattner24943d22010-06-08 16:52:24 +00003970bool
Greg Clayton395fc332011-02-15 21:59:32 +00003971ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00003972{
Greg Clayton9482f052012-03-13 23:14:29 +00003973 ModuleSP module_sp(GetModule());
3974 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003975 {
Greg Clayton9482f052012-03-13 23:14:29 +00003976 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3977 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00003978
Greg Clayton9482f052012-03-13 23:14:29 +00003979 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00003980 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00003981 // unknown to make sure we use the DynamicLoaderStatic()...
3982 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
3983 {
3984 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
3985 }
3986 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00003987 }
Greg Clayton9482f052012-03-13 23:14:29 +00003988 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00003989}
3990
3991
Jason Molenda45c75502013-04-16 06:24:42 +00003992UUID
3993ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
3994{
3995 UUID uuid;
3996 if (process)
3997 {
3998 addr_t all_image_infos = process->GetImageInfoAddress();
3999
4000 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4001 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4002 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4003 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4004
4005 Error err;
4006 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4007 if (version_or_magic != -1
4008 && version_or_magic != HeaderMagic32
4009 && version_or_magic != HeaderMagic32Swapped
4010 && version_or_magic != HeaderMagic64
4011 && version_or_magic != HeaderMagic64Swapped
4012 && version_or_magic >= 13)
4013 {
4014 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4015 int wordsize = process->GetAddressByteSize();
4016 if (wordsize == 8)
4017 {
4018 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4019 }
4020 if (wordsize == 4)
4021 {
4022 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4023 }
4024 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4025 {
4026 uuid_t shared_cache_uuid;
4027 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4028 {
4029 uuid.SetBytes (shared_cache_uuid);
4030 }
4031 }
4032 }
4033 }
4034 return uuid;
4035}
4036
4037UUID
4038ObjectFileMachO::GetLLDBSharedCacheUUID ()
4039{
4040 UUID uuid;
4041#if defined (__APPLE__) && defined (__arm__)
4042 uint8_t *(*dyld_get_all_image_infos)(void);
4043 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4044 if (dyld_get_all_image_infos)
4045 {
4046 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4047 if (dyld_all_image_infos_address)
4048 {
Jason Molenda6ff916f2013-04-16 21:42:58 +00004049 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4050 if (*version >= 13)
Jason Molenda45c75502013-04-16 06:24:42 +00004051 {
Jason Molenda2ceae992013-04-16 22:56:17 +00004052 uuid_t *sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84); // sharedCacheUUID <mach-o/dyld_images.h>
Jason Molenda45c75502013-04-16 06:24:42 +00004053 uuid.SetBytes (sharedCacheUUID_address);
4054 }
4055 }
4056 }
4057#endif
4058 return uuid;
4059}
4060
4061
Chris Lattner24943d22010-06-08 16:52:24 +00004062//------------------------------------------------------------------
4063// PluginInterface protocol
4064//------------------------------------------------------------------
4065const char *
4066ObjectFileMachO::GetPluginName()
4067{
4068 return "ObjectFileMachO";
4069}
4070
4071const char *
4072ObjectFileMachO::GetShortPluginName()
4073{
4074 return GetPluginNameStatic();
4075}
4076
4077uint32_t
4078ObjectFileMachO::GetPluginVersion()
4079{
4080 return 1;
4081}
4082