blob: 7bd6020f1e63a02fcae4a96f1167d67b2e0cd878 [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"
Greg Clayton36b877d2013-04-24 22:29:28 +000022#include "lldb/Core/ModuleSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/PluginManager.h"
Greg Clayton6f7f8da2012-04-24 03:06:13 +000024#include "lldb/Core/RangeMap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Core/Section.h"
26#include "lldb/Core/StreamFile.h"
27#include "lldb/Core/StreamString.h"
28#include "lldb/Core/Timer.h"
29#include "lldb/Core/UUID.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000030#include "lldb/Host/Host.h"
31#include "lldb/Host/FileSpec.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000032#include "lldb/Symbol/ClangNamespaceDecl.h"
Jason Molendad7938392013-03-21 03:36:01 +000033#include "lldb/Symbol/DWARFCallFrameInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000034#include "lldb/Symbol/ObjectFile.h"
Greg Clayton29021d32012-04-18 05:19:20 +000035#include "lldb/Target/Platform.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000036#include "lldb/Target/Process.h"
Greg Clayton29021d32012-04-18 05:19:20 +000037#include "lldb/Target/Target.h"
Greg Clayton9ce95382012-02-13 23:10:39 +000038#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
39#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Clayton46c9a352012-02-09 06:16:32 +000040#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner24943d22010-06-08 16:52:24 +000041
Jason Molenda45c75502013-04-16 06:24:42 +000042#if defined (__APPLE__) && defined (__arm__)
43// GetLLDBSharedCacheUUID() needs to call dlsym()
44#include <dlfcn.h>
45#endif
46
Daniel Malea3e649052013-04-17 19:24:22 +000047#ifndef __APPLE__
48#include "Utility/UuidCompatibility.h"
49#endif
50
Chris Lattner24943d22010-06-08 16:52:24 +000051using namespace lldb;
52using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000053using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000054
Jason Molenda9badb6c2013-03-06 23:19:17 +000055class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
Greg Clayton46c9a352012-02-09 06:16:32 +000056{
57public:
58 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
59 RegisterContextDarwin_x86_64 (thread, 0)
60 {
61 SetRegisterDataFrom_LC_THREAD (data);
62 }
63
64 virtual void
65 InvalidateAllRegisters ()
66 {
67 // Do nothing... registers are always valid...
68 }
69
70 void
71 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
72 {
Greg Clayton36da2aa2013-01-25 18:06:21 +000073 lldb::offset_t offset = 0;
Greg Clayton46c9a352012-02-09 06:16:32 +000074 SetError (GPRRegSet, Read, -1);
75 SetError (FPURegSet, Read, -1);
76 SetError (EXCRegSet, Read, -1);
Greg Clayton9ce95382012-02-13 23:10:39 +000077 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +000078
Greg Clayton9ce95382012-02-13 23:10:39 +000079 while (!done)
Greg Clayton46c9a352012-02-09 06:16:32 +000080 {
Greg Clayton9ce95382012-02-13 23:10:39 +000081 int flavor = data.GetU32 (&offset);
82 if (flavor == 0)
83 done = true;
84 else
Greg Clayton46c9a352012-02-09 06:16:32 +000085 {
Greg Clayton9ce95382012-02-13 23:10:39 +000086 uint32_t i;
87 uint32_t count = data.GetU32 (&offset);
88 switch (flavor)
89 {
90 case GPRRegSet:
91 for (i=0; i<count; ++i)
92 (&gpr.rax)[i] = data.GetU64(&offset);
93 SetError (GPRRegSet, Read, 0);
94 done = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +000095
Greg Clayton9ce95382012-02-13 23:10:39 +000096 break;
97 case FPURegSet:
98 // TODO: fill in FPU regs....
99 //SetError (FPURegSet, Read, -1);
100 done = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000101
Greg Clayton9ce95382012-02-13 23:10:39 +0000102 break;
103 case EXCRegSet:
104 exc.trapno = data.GetU32(&offset);
105 exc.err = data.GetU32(&offset);
106 exc.faultvaddr = data.GetU64(&offset);
107 SetError (EXCRegSet, Read, 0);
108 done = true;
109 break;
110 case 7:
111 case 8:
112 case 9:
113 // fancy flavors that encapsulate of the the above
114 // falvors...
115 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000116
Greg Clayton9ce95382012-02-13 23:10:39 +0000117 default:
118 done = true;
119 break;
120 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000121 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000122 }
123 }
124protected:
125 virtual int
126 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
127 {
128 return 0;
129 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000130
Greg Clayton9ce95382012-02-13 23:10:39 +0000131 virtual int
132 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
133 {
134 return 0;
135 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000136
Greg Clayton9ce95382012-02-13 23:10:39 +0000137 virtual int
138 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
139 {
140 return 0;
141 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000142
Greg Clayton9ce95382012-02-13 23:10:39 +0000143 virtual int
144 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
145 {
146 return 0;
147 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000148
Greg Clayton9ce95382012-02-13 23:10:39 +0000149 virtual int
150 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
151 {
152 return 0;
153 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000154
Greg Clayton9ce95382012-02-13 23:10:39 +0000155 virtual int
156 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
157 {
158 return 0;
159 }
160};
Greg Clayton46c9a352012-02-09 06:16:32 +0000161
Greg Clayton9ce95382012-02-13 23:10:39 +0000162
Jason Molenda9badb6c2013-03-06 23:19:17 +0000163class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
Greg Clayton9ce95382012-02-13 23:10:39 +0000164{
165public:
166 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
167 RegisterContextDarwin_i386 (thread, 0)
168 {
169 SetRegisterDataFrom_LC_THREAD (data);
170 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000171
Greg Clayton9ce95382012-02-13 23:10:39 +0000172 virtual void
173 InvalidateAllRegisters ()
174 {
175 // Do nothing... registers are always valid...
176 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000177
Greg Clayton9ce95382012-02-13 23:10:39 +0000178 void
179 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
180 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000181 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000182 SetError (GPRRegSet, Read, -1);
183 SetError (FPURegSet, Read, -1);
184 SetError (EXCRegSet, Read, -1);
185 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000186
Greg Clayton9ce95382012-02-13 23:10:39 +0000187 while (!done)
188 {
189 int flavor = data.GetU32 (&offset);
190 if (flavor == 0)
191 done = true;
192 else
Greg Clayton46c9a352012-02-09 06:16:32 +0000193 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000194 uint32_t i;
195 uint32_t count = data.GetU32 (&offset);
196 switch (flavor)
197 {
198 case GPRRegSet:
199 for (i=0; i<count; ++i)
200 (&gpr.eax)[i] = data.GetU32(&offset);
201 SetError (GPRRegSet, Read, 0);
202 done = true;
203
204 break;
205 case FPURegSet:
206 // TODO: fill in FPU regs....
207 //SetError (FPURegSet, Read, -1);
208 done = true;
209
210 break;
211 case EXCRegSet:
212 exc.trapno = data.GetU32(&offset);
213 exc.err = data.GetU32(&offset);
214 exc.faultvaddr = data.GetU32(&offset);
215 SetError (EXCRegSet, Read, 0);
216 done = true;
217 break;
218 case 7:
219 case 8:
220 case 9:
221 // fancy flavors that encapsulate of the the above
222 // falvors...
223 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000224
Greg Clayton9ce95382012-02-13 23:10:39 +0000225 default:
226 done = true;
227 break;
228 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000229 }
230 }
231 }
232protected:
233 virtual int
234 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
235 {
236 return 0;
237 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000238
Greg Clayton46c9a352012-02-09 06:16:32 +0000239 virtual int
240 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
241 {
242 return 0;
243 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000244
Greg Clayton46c9a352012-02-09 06:16:32 +0000245 virtual int
246 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
247 {
248 return 0;
249 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000250
Greg Clayton46c9a352012-02-09 06:16:32 +0000251 virtual int
252 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
253 {
254 return 0;
255 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000256
Greg Clayton46c9a352012-02-09 06:16:32 +0000257 virtual int
258 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
259 {
260 return 0;
261 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000262
Greg Clayton46c9a352012-02-09 06:16:32 +0000263 virtual int
264 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
265 {
266 return 0;
267 }
268};
269
Jason Molenda9badb6c2013-03-06 23:19:17 +0000270class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Clayton9ce95382012-02-13 23:10:39 +0000271{
272public:
273 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonb5431d02012-10-30 23:57:32 +0000274 RegisterContextDarwin_arm (thread, 0)
Greg Clayton9ce95382012-02-13 23:10:39 +0000275 {
276 SetRegisterDataFrom_LC_THREAD (data);
277 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000278
Greg Clayton9ce95382012-02-13 23:10:39 +0000279 virtual void
280 InvalidateAllRegisters ()
281 {
282 // Do nothing... registers are always valid...
283 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000284
Greg Clayton9ce95382012-02-13 23:10:39 +0000285 void
286 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
287 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000288 lldb::offset_t offset = 0;
Greg Clayton9ce95382012-02-13 23:10:39 +0000289 SetError (GPRRegSet, Read, -1);
290 SetError (FPURegSet, Read, -1);
291 SetError (EXCRegSet, Read, -1);
292 int flavor = data.GetU32 (&offset);
293 uint32_t count = data.GetU32 (&offset);
294 switch (flavor)
295 {
296 case GPRRegSet:
297 for (uint32_t i=0; i<count; ++i)
298 gpr.r[i] = data.GetU32(&offset);
299 SetError (GPRRegSet, Read, 0);
300 break;
301 case FPURegSet:
302 // TODO: fill in FPU regs....
303 //SetError (FPURegSet, Read, -1);
304 break;
305 case EXCRegSet:
306 exc.exception = data.GetU32(&offset);
307 exc.fsr = data.GetU32(&offset);
308 exc.far = data.GetU32(&offset);
309 SetError (EXCRegSet, Read, 0);
310 break;
311 }
312 }
313protected:
314 virtual int
315 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
316 {
317 return 0;
318 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000319
Greg Clayton9ce95382012-02-13 23:10:39 +0000320 virtual int
321 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
322 {
323 return 0;
324 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000325
Greg Clayton9ce95382012-02-13 23:10:39 +0000326 virtual int
327 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
328 {
329 return 0;
330 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000331
332 virtual int
333 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
334 {
335 return -1;
336 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000337
Greg Clayton9ce95382012-02-13 23:10:39 +0000338 virtual int
339 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
340 {
341 return 0;
342 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000343
Greg Clayton9ce95382012-02-13 23:10:39 +0000344 virtual int
345 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
346 {
347 return 0;
348 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000349
Greg Clayton9ce95382012-02-13 23:10:39 +0000350 virtual int
351 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
352 {
353 return 0;
354 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000355
Greg Claytonb5431d02012-10-30 23:57:32 +0000356 virtual int
357 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
358 {
359 return -1;
360 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000361};
362
Greg Claytonb1888f22011-03-19 01:12:21 +0000363#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000364
365void
366ObjectFileMachO::Initialize()
367{
368 PluginManager::RegisterPlugin (GetPluginNameStatic(),
369 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000370 CreateInstance,
Greg Clayton36b877d2013-04-24 22:29:28 +0000371 CreateMemoryInstance,
372 GetModuleSpecifications);
Chris Lattner24943d22010-06-08 16:52:24 +0000373}
374
375void
376ObjectFileMachO::Terminate()
377{
378 PluginManager::UnregisterPlugin (CreateInstance);
379}
380
381
382const char *
383ObjectFileMachO::GetPluginNameStatic()
384{
385 return "object-file.mach-o";
386}
387
388const char *
389ObjectFileMachO::GetPluginDescriptionStatic()
390{
391 return "Mach-o object file reader (32 and 64 bit)";
392}
393
Chris Lattner24943d22010-06-08 16:52:24 +0000394ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000395ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
396 DataBufferSP& data_sp,
397 lldb::offset_t data_offset,
398 const FileSpec* file,
399 lldb::offset_t file_offset,
400 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000401{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000402 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000403 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000404 data_sp = file->MemoryMapFileContents(file_offset, length);
405 data_offset = 0;
406 }
407
408 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
409 {
410 // Update the data to contain the entire file if it doesn't already
411 if (data_sp->GetByteSize() < length)
412 {
413 data_sp = file->MemoryMapFileContents(file_offset, length);
414 data_offset = 0;
415 }
Greg Clayton102b2c22013-04-18 22:45:39 +0000416 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, data_offset, file, file_offset, length));
Chris Lattner24943d22010-06-08 16:52:24 +0000417 if (objfile_ap.get() && objfile_ap->ParseHeader())
418 return objfile_ap.release();
419 }
420 return NULL;
421}
422
Greg Claytonb5a8f142012-02-05 02:38:54 +0000423ObjectFile *
Jason Molenda9badb6c2013-03-06 23:19:17 +0000424ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
425 DataBufferSP& data_sp,
426 const ProcessSP &process_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000427 lldb::addr_t header_addr)
428{
429 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
430 {
Greg Clayton102b2c22013-04-18 22:45:39 +0000431 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000432 if (objfile_ap.get() && objfile_ap->ParseHeader())
433 return objfile_ap.release();
434 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000435 return NULL;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000436}
437
Greg Clayton36b877d2013-04-24 22:29:28 +0000438size_t
439ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
440 lldb::DataBufferSP& data_sp,
441 lldb::offset_t data_offset,
442 lldb::offset_t file_offset,
443 lldb::offset_t length,
444 lldb_private::ModuleSpecList &specs)
445{
446 const size_t initial_count = specs.GetSize();
447
448 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
449 {
450 DataExtractor data;
451 data.SetData(data_sp);
452 llvm::MachO::mach_header header;
453 if (ParseHeader (data, &data_offset, header))
454 {
455 if (header.sizeofcmds >= data_sp->GetByteSize())
456 {
457 data_sp = file.ReadFileContents(file_offset, header.sizeofcmds);
458 data_offset = 0;
459 }
460 if (data_sp)
461 {
462 ModuleSpec spec;
463 spec.GetFileSpec() = file;
464 spec.GetArchitecture().SetArchitecture(eArchTypeMachO,
465 header.cputype,
466 header.cpusubtype);
467 if (spec.GetArchitecture().IsValid())
468 {
469 GetUUID (header, data, data_offset, spec.GetUUID());
470 specs.Append(spec);
471 }
472 }
473 }
474 }
475 return specs.GetSize() - initial_count;
476}
477
478
Greg Claytonb5a8f142012-02-05 02:38:54 +0000479
480const ConstString &
481ObjectFileMachO::GetSegmentNameTEXT()
482{
483 static ConstString g_segment_name_TEXT ("__TEXT");
484 return g_segment_name_TEXT;
485}
486
487const ConstString &
488ObjectFileMachO::GetSegmentNameDATA()
489{
490 static ConstString g_segment_name_DATA ("__DATA");
491 return g_segment_name_DATA;
492}
493
494const ConstString &
495ObjectFileMachO::GetSegmentNameOBJC()
496{
497 static ConstString g_segment_name_OBJC ("__OBJC");
498 return g_segment_name_OBJC;
499}
500
501const ConstString &
502ObjectFileMachO::GetSegmentNameLINKEDIT()
503{
504 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
505 return g_section_name_LINKEDIT;
506}
507
508const ConstString &
509ObjectFileMachO::GetSectionNameEHFrame()
510{
511 static ConstString g_section_name_eh_frame ("__eh_frame");
512 return g_section_name_eh_frame;
513}
514
515
Chris Lattner24943d22010-06-08 16:52:24 +0000516
517static uint32_t
518MachHeaderSizeFromMagic(uint32_t magic)
519{
520 switch (magic)
521 {
Greg Clayton1674b122010-07-21 22:12:05 +0000522 case HeaderMagic32:
523 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000524 return sizeof(struct mach_header);
525
Greg Clayton1674b122010-07-21 22:12:05 +0000526 case HeaderMagic64:
527 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000528 return sizeof(struct mach_header_64);
529 break;
530
531 default:
532 break;
533 }
534 return 0;
535}
536
537
538bool
Jason Molenda9badb6c2013-03-06 23:19:17 +0000539ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
540 lldb::addr_t data_offset,
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000541 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000542{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000543 DataExtractor data;
544 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000545 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000546 uint32_t magic = data.GetU32(&offset);
547 return MachHeaderSizeFromMagic(magic) != 0;
548}
549
550
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000551ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
552 DataBufferSP& data_sp,
553 lldb::offset_t data_offset,
554 const FileSpec* file,
555 lldb::offset_t file_offset,
556 lldb::offset_t length) :
557 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Clayton46c9a352012-02-09 06:16:32 +0000558 m_mach_segments(),
559 m_mach_sections(),
560 m_entry_point_address(),
561 m_thread_context_offsets(),
562 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000563{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000564 ::memset (&m_header, 0, sizeof(m_header));
565 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000566}
567
Greg Clayton3508c382012-02-24 01:59:29 +0000568ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000569 lldb::DataBufferSP& header_data_sp,
570 const lldb::ProcessSP &process_sp,
571 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000572 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Clayton46c9a352012-02-09 06:16:32 +0000573 m_mach_segments(),
574 m_mach_sections(),
575 m_entry_point_address(),
576 m_thread_context_offsets(),
577 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000578{
579 ::memset (&m_header, 0, sizeof(m_header));
580 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
581}
Chris Lattner24943d22010-06-08 16:52:24 +0000582
583ObjectFileMachO::~ObjectFileMachO()
584{
585}
586
Greg Clayton36b877d2013-04-24 22:29:28 +0000587bool
588ObjectFileMachO::ParseHeader (DataExtractor &data,
589 lldb::offset_t *data_offset_ptr,
590 llvm::MachO::mach_header &header)
591{
592 data.SetByteOrder (lldb::endian::InlHostByteOrder());
593 // Leave magic in the original byte order
594 header.magic = data.GetU32(data_offset_ptr);
595 bool can_parse = false;
596 bool is_64_bit = false;
597 switch (header.magic)
598 {
599 case HeaderMagic32:
600 data.SetByteOrder (lldb::endian::InlHostByteOrder());
601 data.SetAddressByteSize(4);
602 can_parse = true;
603 break;
604
605 case HeaderMagic64:
606 data.SetByteOrder (lldb::endian::InlHostByteOrder());
607 data.SetAddressByteSize(8);
608 can_parse = true;
609 is_64_bit = true;
610 break;
611
612 case HeaderMagic32Swapped:
613 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
614 data.SetAddressByteSize(4);
615 can_parse = true;
616 break;
617
618 case HeaderMagic64Swapped:
619 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
620 data.SetAddressByteSize(8);
621 is_64_bit = true;
622 can_parse = true;
623 break;
624
625 default:
626 break;
627 }
628
629 if (can_parse)
630 {
631 data.GetU32(data_offset_ptr, &header.cputype, 6);
632 if (is_64_bit)
633 *data_offset_ptr += 4;
634 return true;
635 }
636 else
637 {
638 memset(&header, 0, sizeof(header));
639 }
640 return false;
641}
Chris Lattner24943d22010-06-08 16:52:24 +0000642
643bool
644ObjectFileMachO::ParseHeader ()
645{
Greg Clayton9482f052012-03-13 23:14:29 +0000646 ModuleSP module_sp(GetModule());
647 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000648 {
Greg Clayton9482f052012-03-13 23:14:29 +0000649 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
650 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000651 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000652 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000653 // Leave magic in the original byte order
654 m_header.magic = m_data.GetU32(&offset);
655 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000656 {
Greg Clayton9482f052012-03-13 23:14:29 +0000657 case HeaderMagic32:
658 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
659 m_data.SetAddressByteSize(4);
660 can_parse = true;
661 break;
662
663 case HeaderMagic64:
664 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
665 m_data.SetAddressByteSize(8);
666 can_parse = true;
667 break;
668
669 case HeaderMagic32Swapped:
670 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
671 m_data.SetAddressByteSize(4);
672 can_parse = true;
673 break;
674
675 case HeaderMagic64Swapped:
676 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
677 m_data.SetAddressByteSize(8);
678 can_parse = true;
679 break;
680
681 default:
682 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000683 }
Greg Clayton9482f052012-03-13 23:14:29 +0000684
685 if (can_parse)
686 {
687 m_data.GetU32(&offset, &m_header.cputype, 6);
688
689 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +0000690
Greg Clayton21a25432012-11-16 21:36:10 +0000691 // Check if the module has a required architecture
692 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000693 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000694 return false;
695
Greg Clayton9482f052012-03-13 23:14:29 +0000696 if (SetModulesArchitecture (mach_arch))
697 {
698 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
699 if (m_data.GetByteSize() < header_and_lc_size)
700 {
701 DataBufferSP data_sp;
702 ProcessSP process_sp (m_process_wp.lock());
703 if (process_sp)
704 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000705 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000706 }
707 else
708 {
709 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000710 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000711 if (data_sp->GetByteSize() != header_and_lc_size)
712 return false;
713 }
714 if (data_sp)
715 m_data.SetData (data_sp);
716 }
717 }
718 return true;
719 }
720 else
721 {
722 memset(&m_header, 0, sizeof(struct mach_header));
723 }
Chris Lattner24943d22010-06-08 16:52:24 +0000724 }
725 return false;
726}
727
728
729ByteOrder
730ObjectFileMachO::GetByteOrder () const
731{
Chris Lattner24943d22010-06-08 16:52:24 +0000732 return m_data.GetByteOrder ();
733}
734
Jim Ingham7508e732010-08-09 23:31:02 +0000735bool
736ObjectFileMachO::IsExecutable() const
737{
738 return m_header.filetype == HeaderFileTypeExecutable;
739}
Chris Lattner24943d22010-06-08 16:52:24 +0000740
Greg Clayton36da2aa2013-01-25 18:06:21 +0000741uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000742ObjectFileMachO::GetAddressByteSize () const
743{
Chris Lattner24943d22010-06-08 16:52:24 +0000744 return m_data.GetAddressByteSize ();
745}
746
Greg Claytonb3448432011-03-24 21:19:54 +0000747AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000748ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
749{
750 Symtab *symtab = GetSymtab();
751 if (symtab)
752 {
753 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
754 if (symbol)
755 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000756 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000757 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000758 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000759 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000760 {
Greg Clayton3508c382012-02-24 01:59:29 +0000761 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000762 switch (section_type)
763 {
764 case eSectionTypeInvalid: return eAddressClassUnknown;
765 case eSectionTypeCode:
766 if (m_header.cputype == llvm::MachO::CPUTypeARM)
767 {
768 // For ARM we have a bit in the n_desc field of the symbol
769 // that tells us ARM/Thumb which is bit 0x0008.
770 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
771 return eAddressClassCodeAlternateISA;
772 }
773 return eAddressClassCode;
774
775 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000776 case eSectionTypeData:
777 case eSectionTypeDataCString:
778 case eSectionTypeDataCStringPointers:
779 case eSectionTypeDataSymbolAddress:
780 case eSectionTypeData4:
781 case eSectionTypeData8:
782 case eSectionTypeData16:
783 case eSectionTypeDataPointers:
784 case eSectionTypeZeroFill:
785 case eSectionTypeDataObjCMessageRefs:
786 case eSectionTypeDataObjCCFStrings:
787 return eAddressClassData;
788 case eSectionTypeDebug:
789 case eSectionTypeDWARFDebugAbbrev:
790 case eSectionTypeDWARFDebugAranges:
791 case eSectionTypeDWARFDebugFrame:
792 case eSectionTypeDWARFDebugInfo:
793 case eSectionTypeDWARFDebugLine:
794 case eSectionTypeDWARFDebugLoc:
795 case eSectionTypeDWARFDebugMacInfo:
796 case eSectionTypeDWARFDebugPubNames:
797 case eSectionTypeDWARFDebugPubTypes:
798 case eSectionTypeDWARFDebugRanges:
799 case eSectionTypeDWARFDebugStr:
800 case eSectionTypeDWARFAppleNames:
801 case eSectionTypeDWARFAppleTypes:
802 case eSectionTypeDWARFAppleNamespaces:
803 case eSectionTypeDWARFAppleObjC:
804 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000805 case eSectionTypeEHFrame: return eAddressClassRuntime;
806 case eSectionTypeOther: return eAddressClassUnknown;
807 }
808 }
809 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000810
Greg Claytonb3448432011-03-24 21:19:54 +0000811 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000812 switch (symbol_type)
813 {
814 case eSymbolTypeAny: return eAddressClassUnknown;
815 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000816
Greg Claytonb1888f22011-03-19 01:12:21 +0000817 case eSymbolTypeCode:
818 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000819 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000820 if (m_header.cputype == llvm::MachO::CPUTypeARM)
821 {
822 // For ARM we have a bit in the n_desc field of the symbol
823 // that tells us ARM/Thumb which is bit 0x0008.
824 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
825 return eAddressClassCodeAlternateISA;
826 }
827 return eAddressClassCode;
828
829 case eSymbolTypeData: return eAddressClassData;
830 case eSymbolTypeRuntime: return eAddressClassRuntime;
831 case eSymbolTypeException: return eAddressClassRuntime;
832 case eSymbolTypeSourceFile: return eAddressClassDebug;
833 case eSymbolTypeHeaderFile: return eAddressClassDebug;
834 case eSymbolTypeObjectFile: return eAddressClassDebug;
835 case eSymbolTypeCommonBlock: return eAddressClassDebug;
836 case eSymbolTypeBlock: return eAddressClassDebug;
837 case eSymbolTypeLocal: return eAddressClassData;
838 case eSymbolTypeParam: return eAddressClassData;
839 case eSymbolTypeVariable: return eAddressClassData;
840 case eSymbolTypeVariableType: return eAddressClassDebug;
841 case eSymbolTypeLineEntry: return eAddressClassDebug;
842 case eSymbolTypeLineHeader: return eAddressClassDebug;
843 case eSymbolTypeScopeBegin: return eAddressClassDebug;
844 case eSymbolTypeScopeEnd: return eAddressClassDebug;
845 case eSymbolTypeAdditional: return eAddressClassUnknown;
846 case eSymbolTypeCompiler: return eAddressClassDebug;
847 case eSymbolTypeInstrumentation:return eAddressClassDebug;
848 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000849 case eSymbolTypeObjCClass: return eAddressClassRuntime;
850 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
851 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000852 }
853 }
854 }
855 return eAddressClassUnknown;
856}
Chris Lattner24943d22010-06-08 16:52:24 +0000857
858Symtab *
859ObjectFileMachO::GetSymtab()
860{
Greg Clayton9482f052012-03-13 23:14:29 +0000861 ModuleSP module_sp(GetModule());
862 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000863 {
Greg Clayton9482f052012-03-13 23:14:29 +0000864 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
865 if (m_symtab_ap.get() == NULL)
866 {
867 m_symtab_ap.reset(new Symtab(this));
868 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
869 ParseSymtab (true);
870 m_symtab_ap->Finalize ();
871 }
Chris Lattner24943d22010-06-08 16:52:24 +0000872 }
873 return m_symtab_ap.get();
874}
875
876
877SectionList *
878ObjectFileMachO::GetSectionList()
879{
Greg Clayton9482f052012-03-13 23:14:29 +0000880 ModuleSP module_sp(GetModule());
881 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000882 {
Greg Clayton9482f052012-03-13 23:14:29 +0000883 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
884 if (m_sections_ap.get() == NULL)
885 {
886 m_sections_ap.reset(new SectionList());
887 ParseSections();
888 }
Chris Lattner24943d22010-06-08 16:52:24 +0000889 }
890 return m_sections_ap.get();
891}
892
893
894size_t
895ObjectFileMachO::ParseSections ()
896{
897 lldb::user_id_t segID = 0;
898 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000899 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000900 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000901 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000902 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000903 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000904 // First look up any LC_ENCRYPTION_INFO load commands
905 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
906 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000907 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000908 for (i=0; i<m_header.ncmds; ++i)
909 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000910 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000911 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000912 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000913
Greg Clayton54e33712012-05-25 18:09:55 +0000914 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000915 {
Greg Clayton54e33712012-05-25 18:09:55 +0000916 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
917 {
918 if (encryption_cmd.cryptid != 0)
919 {
920 EncryptedFileRanges::Entry entry;
921 entry.SetRangeBase(encryption_cmd.cryptoff);
922 entry.SetByteSize(encryption_cmd.cryptsize);
923 encrypted_file_ranges.Append(entry);
924 }
925 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000926 }
Greg Clayton54e33712012-05-25 18:09:55 +0000927 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000928 }
929
930 offset = MachHeaderSizeFromMagic(m_header.magic);
931
Greg Clayton54e33712012-05-25 18:09:55 +0000932 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000933 for (i=0; i<m_header.ncmds; ++i)
934 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000935 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000936 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
937 break;
938
Greg Clayton1674b122010-07-21 22:12:05 +0000939 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000940 {
941 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
942 {
943 load_cmd.vmaddr = m_data.GetAddress(&offset);
944 load_cmd.vmsize = m_data.GetAddress(&offset);
945 load_cmd.fileoff = m_data.GetAddress(&offset);
946 load_cmd.filesize = m_data.GetAddress(&offset);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000947 if (m_length != 0 && load_cmd.filesize != 0)
948 {
Greg Claytonbb759862013-04-16 16:51:19 +0000949 if (load_cmd.fileoff > m_length)
950 {
951 // We have a load command that says it extends past the end of hte file. This is likely
952 // a corrupt file. We don't have any way to return an error condition here (this method
953 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
954 // is null out the SectionList vector and if a process has been set up, dump a message
955 // to stdout. The most common case here is core file debugging with a truncated file.
956 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
957 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 ")",
958 i,
959 lc_segment_name,
960 load_cmd.fileoff,
961 m_length);
962 m_sections_ap->Clear();
963 return 0;
964 }
965
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000966 if (load_cmd.fileoff + load_cmd.filesize > m_length)
967 {
968 // We have a load command that says it extends past the end of hte file. This is likely
969 // a corrupt file. We don't have any way to return an error condition here (this method
970 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
971 // is null out the SectionList vector and if a process has been set up, dump a message
Greg Claytonbb759862013-04-16 16:51:19 +0000972 // to stdout. The most common case here is core file debugging with a truncated file.
973 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
974 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 ")",
975 i,
976 lc_segment_name,
977 load_cmd.fileoff + load_cmd.filesize,
978 m_length);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000979 m_sections_ap->Clear();
980 return 0;
981 }
982 }
Chris Lattner24943d22010-06-08 16:52:24 +0000983 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
984 {
Jason Molenda9badb6c2013-03-06 23:19:17 +0000985
Greg Clayton68ca8232011-01-25 02:58:48 +0000986 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
987
Chris Lattner24943d22010-06-08 16:52:24 +0000988 // Keep a list of mach segments around in case we need to
989 // get at data that isn't stored in the abstracted Sections.
990 m_mach_segments.push_back (load_cmd);
991
Greg Clayton36da2aa2013-01-25 18:06:21 +0000992 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 +0000993 // Use a segment ID of the segment index shifted left by 8 so they
994 // never conflict with any of the sections.
995 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000996 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000997 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000998 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +0000999 ++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
1000 segment_name, // Name of this section
1001 eSectionTypeContainer, // This section is a container of other sections.
1002 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1003 load_cmd.vmsize, // VM size in bytes of this section
1004 load_cmd.fileoff, // Offset to the data for this section in the file
1005 load_cmd.filesize, // Size in bytes of this section as found in the the file
1006 load_cmd.flags)); // Flags for this section
1007
Greg Clayton68ca8232011-01-25 02:58:48 +00001008 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001009 m_sections_ap->AddSection(segment_sp);
1010 }
1011
1012 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +00001013 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +00001014 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +00001015 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +00001016 // mach sections yet...
1017 if (m_mach_sections.empty())
1018 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +00001019 uint32_t segment_sect_idx;
1020 const lldb::user_id_t first_segment_sectID = sectID + 1;
1021
1022
Greg Clayton1674b122010-07-21 22:12:05 +00001023 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +00001024 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
1025 {
1026 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
1027 break;
1028 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1029 break;
1030 sect64.addr = m_data.GetAddress(&offset);
1031 sect64.size = m_data.GetAddress(&offset);
1032
1033 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1034 break;
1035
1036 // Keep a list of mach sections around in case we need to
1037 // get at data that isn't stored in the abstracted Sections.
1038 m_mach_sections.push_back (sect64);
1039
1040 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1041 if (!segment_name)
1042 {
1043 // We have a segment with no name so we need to conjure up
1044 // segments that correspond to the section's segname if there
1045 // isn't already such a section. If there is such a section,
1046 // we resize the section so that it spans all sections.
1047 // We also mark these sections as fake so address matches don't
1048 // hit if they land in the gaps between the child sections.
1049 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1050 segment_sp = m_sections_ap->FindSectionByName (segment_name);
1051 if (segment_sp.get())
1052 {
1053 Section *segment = segment_sp.get();
1054 // Grow the section size as needed.
1055 const lldb::addr_t sect64_min_addr = sect64.addr;
1056 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1057 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1058 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1059 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1060 if (sect64_min_addr >= curr_seg_min_addr)
1061 {
1062 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1063 // Only grow the section size if needed
1064 if (new_seg_byte_size > curr_seg_byte_size)
1065 segment->SetByteSize (new_seg_byte_size);
1066 }
1067 else
1068 {
1069 // We need to change the base address of the segment and
1070 // adjust the child section offsets for all existing children.
1071 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1072 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +00001073 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +00001074 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1075 }
Greg Clayton661825b2010-06-28 23:51:11 +00001076
1077 // Grow the section size as needed.
1078 if (sect64.offset)
1079 {
1080 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1081 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1082
1083 const lldb::addr_t section_min_file_offset = sect64.offset;
1084 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1085 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1086 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1087 segment->SetFileOffset (new_file_offset);
1088 segment->SetFileSize (new_file_size);
1089 }
Chris Lattner24943d22010-06-08 16:52:24 +00001090 }
1091 else
1092 {
1093 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +00001094 segment_sp.reset(new Section (segment_sp, // Parent section
1095 module_sp, // Module to which this section belongs
1096 ++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
1097 segment_name, // Name of this section
1098 eSectionTypeContainer, // This section is a container of other sections.
1099 sect64.addr, // File VM address == addresses as they are found in the object file
1100 sect64.size, // VM size in bytes of this section
1101 sect64.offset, // Offset to the data for this section in the file
1102 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1103 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +00001104 segment_sp->SetIsFake(true);
1105 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +00001106 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001107 }
1108 }
1109 assert (segment_sp.get());
1110
Greg Clayton1674b122010-07-21 22:12:05 +00001111 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +00001112 static ConstString g_sect_name_objc_data ("__objc_data");
1113 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1114 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1115 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1116 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1117 static ConstString g_sect_name_objc_const ("__objc_const");
1118 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1119 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001120
1121 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1122 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1123 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1124 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1125 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1126 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1127 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1128 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1129 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1130 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1131 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +00001132 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1133 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +00001134 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +00001135 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001136 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +00001137 static ConstString g_sect_name_DATA ("__DATA");
1138 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001139
Chris Lattner24943d22010-06-08 16:52:24 +00001140 SectionType sect_type = eSectionTypeOther;
1141
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001142 if (section_name == g_sect_name_dwarf_debug_abbrev)
1143 sect_type = eSectionTypeDWARFDebugAbbrev;
1144 else if (section_name == g_sect_name_dwarf_debug_aranges)
1145 sect_type = eSectionTypeDWARFDebugAranges;
1146 else if (section_name == g_sect_name_dwarf_debug_frame)
1147 sect_type = eSectionTypeDWARFDebugFrame;
1148 else if (section_name == g_sect_name_dwarf_debug_info)
1149 sect_type = eSectionTypeDWARFDebugInfo;
1150 else if (section_name == g_sect_name_dwarf_debug_line)
1151 sect_type = eSectionTypeDWARFDebugLine;
1152 else if (section_name == g_sect_name_dwarf_debug_loc)
1153 sect_type = eSectionTypeDWARFDebugLoc;
1154 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1155 sect_type = eSectionTypeDWARFDebugMacInfo;
1156 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1157 sect_type = eSectionTypeDWARFDebugPubNames;
1158 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1159 sect_type = eSectionTypeDWARFDebugPubTypes;
1160 else if (section_name == g_sect_name_dwarf_debug_ranges)
1161 sect_type = eSectionTypeDWARFDebugRanges;
1162 else if (section_name == g_sect_name_dwarf_debug_str)
1163 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001164 else if (section_name == g_sect_name_dwarf_apple_names)
1165 sect_type = eSectionTypeDWARFAppleNames;
1166 else if (section_name == g_sect_name_dwarf_apple_types)
1167 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001168 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1169 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001170 else if (section_name == g_sect_name_dwarf_apple_objc)
1171 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001172 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001173 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001174 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001175 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001176 else if (section_name == g_sect_name_eh_frame)
1177 sect_type = eSectionTypeEHFrame;
1178 else if (section_name == g_sect_name_cfstring)
1179 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001180 else if (section_name == g_sect_name_objc_data ||
1181 section_name == g_sect_name_objc_classrefs ||
1182 section_name == g_sect_name_objc_superrefs ||
1183 section_name == g_sect_name_objc_const ||
1184 section_name == g_sect_name_objc_classlist)
1185 {
1186 sect_type = eSectionTypeDataPointers;
1187 }
Chris Lattner24943d22010-06-08 16:52:24 +00001188
1189 if (sect_type == eSectionTypeOther)
1190 {
1191 switch (mach_sect_type)
1192 {
1193 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001194 case SectionTypeRegular:
1195 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001196 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001197 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001198 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001199 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001200 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001201 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001202 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1203 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1204 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1205 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1206 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1207 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1208 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1209 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1210 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1211 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1212 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1213 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1214 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1215 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1216 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1217 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001218 default: break;
1219 }
1220 }
1221
Greg Clayton3508c382012-02-24 01:59:29 +00001222 SectionSP section_sp(new Section (segment_sp,
1223 module_sp,
1224 ++sectID,
1225 section_name,
1226 sect_type,
1227 sect64.addr - segment_sp->GetFileAddress(),
1228 sect64.size,
1229 sect64.offset,
1230 sect64.offset == 0 ? 0 : sect64.size,
1231 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001232 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001233
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001234 bool section_is_encrypted = false;
1235 if (!segment_is_encrypted && load_cmd.filesize != 0)
1236 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001237
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001238 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001239 segment_sp->GetChildren().AddSection(section_sp);
1240
1241 if (segment_sp->IsFake())
1242 {
1243 segment_sp.reset();
1244 segment_name.Clear();
1245 }
1246 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001247 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001248 {
1249 if (first_segment_sectID <= sectID)
1250 {
1251 lldb::user_id_t sect_uid;
1252 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1253 {
1254 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1255 SectionSP next_section_sp;
1256 if (sect_uid + 1 <= sectID)
1257 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1258
1259 if (curr_section_sp.get())
1260 {
1261 if (curr_section_sp->GetByteSize() == 0)
1262 {
1263 if (next_section_sp.get() != NULL)
1264 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1265 else
1266 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1267 }
1268 }
1269 }
1270 }
1271 }
1272 }
1273 }
1274 }
Greg Clayton1674b122010-07-21 22:12:05 +00001275 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001276 {
1277 m_dysymtab.cmd = load_cmd.cmd;
1278 m_dysymtab.cmdsize = load_cmd.cmdsize;
1279 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1280 }
1281
1282 offset = load_cmd_offset + load_cmd.cmdsize;
1283 }
1284// if (dump_sections)
1285// {
1286// StreamFile s(stdout);
1287// m_sections_ap->Dump(&s, true);
1288// }
1289 return sectID; // Return the number of sections we registered with the module
1290}
1291
1292class MachSymtabSectionInfo
1293{
1294public:
1295
1296 MachSymtabSectionInfo (SectionList *section_list) :
1297 m_section_list (section_list),
1298 m_section_infos()
1299 {
1300 // Get the number of sections down to a depth of 1 to include
1301 // all segments and their sections, but no other sections that
1302 // may be added for debug map or
1303 m_section_infos.resize(section_list->GetNumSections(1));
1304 }
1305
1306
Greg Clayton3508c382012-02-24 01:59:29 +00001307 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001308 GetSection (uint8_t n_sect, addr_t file_addr)
1309 {
1310 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001311 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001312 if (n_sect < m_section_infos.size())
1313 {
Greg Clayton3508c382012-02-24 01:59:29 +00001314 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001315 {
Greg Clayton3508c382012-02-24 01:59:29 +00001316 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1317 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001318 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001319 {
Greg Clayton3508c382012-02-24 01:59:29 +00001320 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1321 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001322 }
1323 else
1324 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001325 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001326 }
Chris Lattner24943d22010-06-08 16:52:24 +00001327 }
1328 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001329 {
1330 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001331 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001332 }
1333 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1334 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1335 {
1336 // Symbol is in section with zero size, but has the same start
1337 // address as the section. This can happen with linker symbols
1338 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001339 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001340 }
Chris Lattner24943d22010-06-08 16:52:24 +00001341 }
Greg Clayton3508c382012-02-24 01:59:29 +00001342 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001343 }
1344
1345protected:
1346 struct SectionInfo
1347 {
1348 SectionInfo () :
1349 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001350 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001351 {
1352 }
1353
1354 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001355 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001356 };
1357 SectionList *m_section_list;
1358 std::vector<SectionInfo> m_section_infos;
1359};
1360
Chris Lattner24943d22010-06-08 16:52:24 +00001361size_t
1362ObjectFileMachO::ParseSymtab (bool minimize)
1363{
1364 Timer scoped_timer(__PRETTY_FUNCTION__,
1365 "ObjectFileMachO::ParseSymtab () module = %s",
1366 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001367 ModuleSP module_sp (GetModule());
1368 if (!module_sp)
1369 return 0;
1370
1371 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1372 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1373 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1374 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001375 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001376 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001377
Greg Clayton952e9dc2013-03-27 23:08:40 +00001378 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001379
Chris Lattner24943d22010-06-08 16:52:24 +00001380 for (i=0; i<m_header.ncmds; ++i)
1381 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001382 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001383 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001384 struct load_command lc;
1385 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001386 break;
1387 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001388 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001389 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001390 case LoadCommandSymtab:
1391 symtab_load_command.cmd = lc.cmd;
1392 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001393 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001394 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1395 return 0;
1396 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001397 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001398 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001399 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001400 return 0;
1401 }
1402
1403 if (symtab_load_command.stroff == 0)
1404 {
1405 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001406 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001407 return 0;
1408 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001409
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001410 if (symtab_load_command.nsyms == 0)
1411 {
1412 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001413 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001414 return 0;
1415 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001416
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001417 if (symtab_load_command.strsize == 0)
1418 {
1419 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001420 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001421 return 0;
1422 }
1423 break;
1424
1425 case LoadCommandFunctionStarts:
1426 function_starts_load_command.cmd = lc.cmd;
1427 function_starts_load_command.cmdsize = lc.cmdsize;
1428 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1429 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1430 break;
1431
1432 default:
1433 break;
1434 }
1435 offset = cmd_offset + lc.cmdsize;
1436 }
1437
1438 if (symtab_load_command.cmd)
1439 {
1440 Symtab *symtab = m_symtab_ap.get();
1441 SectionList *section_list = GetSectionList();
1442 if (section_list == NULL)
1443 return 0;
1444
1445 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001446 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001447
Greg Clayton36da2aa2013-01-25 18:06:21 +00001448 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1449 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001450 bool bit_width_32 = addr_byte_size == 4;
1451 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1452
Greg Clayton36da2aa2013-01-25 18:06:21 +00001453 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1454 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1455 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001456 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001457
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001458 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1459 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001460 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1461 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001462 {
Greg Claytondd29b972012-05-18 23:20:01 +00001463 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001464 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1465 // Reading mach file from memory in a process or core file...
1466
1467 if (linkedit_section_sp)
1468 {
1469 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1470 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1471 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001472 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001473
1474 bool data_was_read = false;
1475
1476#if defined (__APPLE__) && defined (__arm__)
1477 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001478 {
Greg Clayton29021d32012-04-18 05:19:20 +00001479 // This mach-o memory file is in the dyld shared cache. If this
1480 // program is not remote and this is iOS, then this process will
1481 // share the same shared cache as the process we are debugging and
1482 // we can read the entire __LINKEDIT from the address space in this
1483 // process. This is a needed optimization that is used for local iOS
1484 // debugging only since all shared libraries in the shared cache do
1485 // not have corresponding files that exist in the file system of the
1486 // device. They have been combined into a single file. This means we
1487 // always have to load these files from memory. All of the symbol and
1488 // string tables from all of the __LINKEDIT sections from the shared
1489 // libraries in the shared cache have been merged into a single large
1490 // symbol and string table. Reading all of this symbol and string table
1491 // data across can slow down debug launch times, so we optimize this by
1492 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda45c75502013-04-16 06:24:42 +00001493
1494 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1495 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1496 bool use_lldb_cache = true;
1497 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1498 {
1499 use_lldb_cache = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001500 ModuleSP module_sp (GetModule());
1501 if (module_sp)
1502 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1503
Jason Molenda45c75502013-04-16 06:24:42 +00001504 }
1505
Greg Clayton29021d32012-04-18 05:19:20 +00001506 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda45c75502013-04-16 06:24:42 +00001507 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton29021d32012-04-18 05:19:20 +00001508 {
1509 data_was_read = true;
1510 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001511 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001512 if (function_starts_load_command.cmd)
1513 {
1514 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1515 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1516 }
1517 }
1518 }
1519#endif
1520
1521 if (!data_was_read)
1522 {
1523 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1524 if (nlist_data_sp)
1525 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001526 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1527 //if (strtab_data_sp)
1528 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001529 if (m_dysymtab.nindirectsyms != 0)
1530 {
1531 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1532 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1533 if (indirect_syms_data_sp)
1534 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1535 }
Greg Clayton29021d32012-04-18 05:19:20 +00001536 if (function_starts_load_command.cmd)
1537 {
1538 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1539 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1540 if (func_start_data_sp)
1541 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1542 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001543 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001544 }
1545 }
1546 else
1547 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001548 nlist_data.SetData (m_data,
1549 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001550 nlist_data_byte_size);
1551 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001552 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001553 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001554 if (m_dysymtab.nindirectsyms != 0)
1555 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001556 indirect_symbol_index_data.SetData (m_data,
1557 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001558 m_dysymtab.nindirectsyms * 4);
1559 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001560 if (function_starts_load_command.cmd)
1561 {
1562 function_starts_data.SetData (m_data,
1563 function_starts_load_command.dataoff,
1564 function_starts_load_command.datasize);
1565 }
1566 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001567
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001568 if (nlist_data.GetByteSize() == 0)
1569 {
1570 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001571 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001572 return 0;
1573 }
1574
1575
Greg Clayton3a5dc012012-05-25 17:04:00 +00001576 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1577 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001578 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001579 if (process)
1580 {
1581 if (strtab_addr == LLDB_INVALID_ADDRESS)
1582 {
1583 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001584 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001585 return 0;
1586 }
1587 }
1588 else
Greg Claytondd29b972012-05-18 23:20:01 +00001589 {
1590 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001591 module_sp->LogMessage(log, "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001592 return 0;
1593 }
1594 }
Greg Claytondd29b972012-05-18 23:20:01 +00001595
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001596 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1597 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1598 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1599 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1600 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1601 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1602 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1603 SectionSP eh_frame_section_sp;
1604 if (text_section_sp.get())
1605 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1606 else
1607 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1608
Greg Claytond2653c22012-03-14 01:53:24 +00001609 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001610
1611 // lldb works best if it knows the start addresss of all functions in a module.
1612 // Linker symbols or debug info are normally the best source of information for start addr / size but
1613 // they may be stripped in a released binary.
Jason Molenda2120aef2013-04-16 00:18:44 +00001614 // Two additional sources of information exist in Mach-O binaries:
Jason Molendad7938392013-03-21 03:36:01 +00001615 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1616 // binary, relative to the text section.
1617 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1618 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1619 // Binaries built to run on older releases may need to use eh_frame information.
1620
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001621 if (text_section_sp && function_starts_data.GetByteSize())
1622 {
1623 FunctionStarts::Entry function_start_entry;
1624 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001625 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001626 function_start_entry.addr = text_section_sp->GetFileAddress();
1627 uint64_t delta;
1628 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1629 {
1630 // Now append the current entry
1631 function_start_entry.addr += delta;
1632 function_starts.Append(function_start_entry);
1633 }
Jason Molenda2120aef2013-04-16 00:18:44 +00001634 }
Jason Molendad7938392013-03-21 03:36:01 +00001635 else
1636 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001637 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1638 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1639 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1640 // the module.
1641 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001642 {
1643 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1644 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1645 eh_frame.GetFunctionAddressAndSizeVector (functions);
1646 addr_t text_base_addr = text_section_sp->GetFileAddress();
1647 size_t count = functions.GetSize();
1648 for (size_t i = 0; i < count; ++i)
1649 {
1650 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1651 if (func)
1652 {
1653 FunctionStarts::Entry function_start_entry;
1654 function_start_entry.addr = func->base - text_base_addr;
1655 function_starts.Append(function_start_entry);
1656 }
1657 }
1658 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001659 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001660
Greg Clayton36da2aa2013-01-25 18:06:21 +00001661 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001662
Greg Clayton36da2aa2013-01-25 18:06:21 +00001663 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 +00001664
Greg Clayton36da2aa2013-01-25 18:06:21 +00001665 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001666
1667 uint32_t N_SO_index = UINT32_MAX;
1668
1669 MachSymtabSectionInfo section_info (section_list);
1670 std::vector<uint32_t> N_FUN_indexes;
1671 std::vector<uint32_t> N_NSYM_indexes;
1672 std::vector<uint32_t> N_INCL_indexes;
1673 std::vector<uint32_t> N_BRAC_indexes;
1674 std::vector<uint32_t> N_COMM_indexes;
1675 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1676 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1677 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1678 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1679 // Any symbols that get merged into another will get an entry
1680 // in this map so we know
1681 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1682 uint32_t nlist_idx = 0;
1683 Symbol *symbol_ptr = NULL;
1684
1685 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001686 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001687 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001688 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001689 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001690
Jason Molendab62abd52012-06-21 01:51:02 +00001691#if defined (__APPLE__) && defined (__arm__)
1692
1693 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1694 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1695 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1696 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1697 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1698 // nlist parser to ignore all LOCAL symbols.
1699
1700 if (m_header.flags & 0x80000000u)
1701 {
1702 // Before we can start mapping the DSC, we need to make certain the target process is actually
1703 // using the cache we can find.
1704
Jason Molendab62abd52012-06-21 01:51:02 +00001705 // Next we need to determine the correct path for the dyld shared cache.
1706
1707 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1708 char dsc_path[PATH_MAX];
1709
1710 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001711 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1712 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001713 header_arch.GetArchitectureName());
1714
1715 FileSpec dsc_filespec(dsc_path, false);
1716
1717 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda2120aef2013-04-16 00:18:44 +00001718 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001719 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001720 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1721 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1722 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001723 uint32_t imagesOffset;
1724 uint32_t imagesCount;
1725 uint64_t dyldBaseAddress;
1726 uint64_t codeSignatureOffset;
1727 uint64_t codeSignatureSize;
1728 uint64_t slideInfoOffset;
1729 uint64_t slideInfoSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001730 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1731 uint64_t localSymbolsSize; // size of local symbols information
1732 };
1733 struct lldb_copy_dyld_cache_header_v1
1734 {
1735 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1736 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1737 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1738 uint32_t imagesOffset;
1739 uint32_t imagesCount;
1740 uint64_t dyldBaseAddress;
1741 uint64_t codeSignatureOffset;
1742 uint64_t codeSignatureSize;
1743 uint64_t slideInfoOffset;
1744 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001745 uint64_t localSymbolsOffset;
1746 uint64_t localSymbolsSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001747 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001748 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001749
Jason Molenda2120aef2013-04-16 00:18:44 +00001750 struct lldb_copy_dyld_cache_mapping_info
1751 {
1752 uint64_t address;
1753 uint64_t size;
1754 uint64_t fileOffset;
1755 uint32_t maxProt;
1756 uint32_t initProt;
1757 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001758
Greg Claytonab77dcb2012-09-05 22:30:51 +00001759 struct lldb_copy_dyld_cache_local_symbols_info
1760 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001761 uint32_t nlistOffset;
1762 uint32_t nlistCount;
1763 uint32_t stringsOffset;
1764 uint32_t stringsSize;
1765 uint32_t entriesOffset;
1766 uint32_t entriesCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001767 };
1768 struct lldb_copy_dyld_cache_local_symbols_entry
1769 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001770 uint32_t dylibOffset;
1771 uint32_t nlistStartIndex;
1772 uint32_t nlistCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001773 };
Jason Molendab62abd52012-06-21 01:51:02 +00001774
Jason Molendafd3b35d2012-06-22 03:28:35 +00001775 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1776 The dyld_cache_local_symbols_info structure gives us three things:
1777 1. The start and count of the nlist records in the dyld_shared_cache file
1778 2. The start and size of the strings for these nlist records
1779 3. The start and count of dyld_cache_local_symbols_entry entries
1780
1781 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1782 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001783 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001784 and the count of how many nlist records there are for this dylib/framework.
1785 */
1786
Jason Molendab62abd52012-06-21 01:51:02 +00001787 // Process the dsc header to find the unmapped symbols
1788 //
1789 // Save some VM space, do not map the entire cache in one shot.
1790
Jason Molenda6bcabae2013-03-06 23:17:36 +00001791 DataBufferSP dsc_data_sp;
1792 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1793
1794 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001795 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001796 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001797
Jason Molenda6bcabae2013-03-06 23:17:36 +00001798 char version_str[17];
1799 int version = -1;
1800 lldb::offset_t offset = 0;
1801 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1802 version_str[16] = '\0';
1803 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1804 {
1805 int v;
1806 if (::sscanf (version_str + 6, "%d", &v) == 1)
1807 {
1808 version = v;
1809 }
1810 }
1811
Jason Molenda45c75502013-04-16 06:24:42 +00001812 UUID dsc_uuid;
1813 if (version >= 1)
1814 {
1815 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1816 uint8_t uuid_bytes[sizeof (uuid_t)];
1817 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1818 dsc_uuid.SetBytes (uuid_bytes);
1819 }
1820
1821 bool uuid_match = true;
1822 if (dsc_uuid.IsValid() && process)
1823 {
1824 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1825
1826 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1827 {
1828 // The on-disk dyld_shared_cache file is not the same as the one in this
1829 // process' memory, don't use it.
1830 uuid_match = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001831 ModuleSP module_sp (GetModule());
1832 if (module_sp)
1833 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 +00001834 }
1835 }
1836
Jason Molenda9badb6c2013-03-06 23:19:17 +00001837 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001838
Jason Molendab62abd52012-06-21 01:51:02 +00001839 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1840
1841 // If the mappingOffset points to a location inside the header, we've
1842 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda45c75502013-04-16 06:24:42 +00001843 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendab62abd52012-06-21 01:51:02 +00001844 {
1845
Jason Molenda6bcabae2013-03-06 23:17:36 +00001846 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1847 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1848 offset = 0;
1849
1850 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1851 // in the shared library cache need to be adjusted by an offset to match up with the
1852 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1853 // recorded in mapping_offset_value.
1854 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1855
1856 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001857 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1858 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1859
Jason Molenda9badb6c2013-03-06 23:19:17 +00001860 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001861 {
1862 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001863 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001864 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001865 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001866
1867 offset = 0;
1868
1869 // Read the local_symbols_infos struct in one shot
1870 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1871 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1872
Jason Molendab62abd52012-06-21 01:51:02 +00001873 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1874
Jason Molenda6bcabae2013-03-06 23:17:36 +00001875 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001876
1877 offset = local_symbols_info.entriesOffset;
1878 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1879 {
1880 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1881 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1882 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1883 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1884
Jason Molenda9badb6c2013-03-06 23:19:17 +00001885 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001886 {
1887 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1888
1889 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1890 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1891 num_syms = symtab->GetNumSymbols();
1892
1893 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1894 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1895
Jason Molenda9badb6c2013-03-06 23:19:17 +00001896 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001897 {
1898 /////////////////////////////
1899 {
1900 struct nlist_64 nlist;
1901 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1902 break;
1903
1904 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1905 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1906 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1907 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1908 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1909
1910 SymbolType type = eSymbolTypeInvalid;
1911 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1912
1913 if (symbol_name == NULL)
1914 {
1915 // No symbol should be NULL, even the symbols with no
1916 // string values should have an offset zero which points
1917 // to an empty C-string
1918 Host::SystemLog (Host::eSystemLogError,
1919 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1920 entry_index,
1921 nlist.n_strx,
1922 module_sp->GetFileSpec().GetDirectory().GetCString(),
1923 module_sp->GetFileSpec().GetFilename().GetCString());
1924 continue;
1925 }
1926 if (symbol_name[0] == '\0')
1927 symbol_name = NULL;
1928
1929 const char *symbol_name_non_abi_mangled = NULL;
1930
1931 SectionSP symbol_section;
1932 uint32_t symbol_byte_size = 0;
1933 bool add_nlist = true;
1934 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001935 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001936
1937 assert (sym_idx < num_syms);
1938
1939 sym[sym_idx].SetDebug (is_debug);
1940
1941 if (is_debug)
1942 {
1943 switch (nlist.n_type)
1944 {
1945 case StabGlobalSymbol:
1946 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1947 // Sometimes the N_GSYM value contains the address.
1948
1949 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1950 // have the same address, but we want to ensure that we always find only the real symbol,
1951 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1952 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1953 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1954 // same address.
1955
1956 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1957 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1958 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1959 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1960 add_nlist = false;
1961 else
1962 {
1963 sym[sym_idx].SetExternal(true);
1964 if (nlist.n_value != 0)
1965 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1966 type = eSymbolTypeData;
1967 }
1968 break;
1969
1970 case StabFunctionName:
1971 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1972 type = eSymbolTypeCompiler;
1973 break;
1974
1975 case StabFunction:
1976 // N_FUN -- procedure: name,,n_sect,linenumber,address
1977 if (symbol_name)
1978 {
1979 type = eSymbolTypeCode;
1980 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1981
1982 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
1983 // We use the current number of symbols in the symbol table in lieu of
1984 // using nlist_idx in case we ever start trimming entries out
1985 N_FUN_indexes.push_back(sym_idx);
1986 }
1987 else
1988 {
1989 type = eSymbolTypeCompiler;
1990
1991 if ( !N_FUN_indexes.empty() )
1992 {
1993 // Copy the size of the function into the original STAB entry so we don't have
1994 // to hunt for it later
1995 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1996 N_FUN_indexes.pop_back();
1997 // We don't really need the end function STAB as it contains the size which
1998 // we already placed with the original symbol, so don't add it if we want a
1999 // minimal symbol table
2000 if (minimize)
2001 add_nlist = false;
2002 }
2003 }
2004 break;
2005
2006 case StabStaticSymbol:
2007 // N_STSYM -- static symbol: name,,n_sect,type,address
2008 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2009 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2010 type = eSymbolTypeData;
2011 break;
2012
2013 case StabLocalCommon:
2014 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2015 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2016 type = eSymbolTypeCommonBlock;
2017 break;
2018
2019 case StabBeginSymbol:
2020 // N_BNSYM
2021 // We use the current number of symbols in the symbol table in lieu of
2022 // using nlist_idx in case we ever start trimming entries out
2023 if (minimize)
2024 {
2025 // Skip these if we want minimal symbol tables
2026 add_nlist = false;
2027 }
2028 else
2029 {
2030 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2031 N_NSYM_indexes.push_back(sym_idx);
2032 type = eSymbolTypeScopeBegin;
2033 }
2034 break;
2035
2036 case StabEndSymbol:
2037 // N_ENSYM
2038 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2039 // so that we can always skip the entire symbol if we need to navigate
2040 // more quickly at the source level when parsing STABS
2041 if (minimize)
2042 {
2043 // Skip these if we want minimal symbol tables
2044 add_nlist = false;
2045 }
2046 else
2047 {
2048 if ( !N_NSYM_indexes.empty() )
2049 {
2050 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2051 symbol_ptr->SetByteSize(sym_idx + 1);
2052 symbol_ptr->SetSizeIsSibling(true);
2053 N_NSYM_indexes.pop_back();
2054 }
2055 type = eSymbolTypeScopeEnd;
2056 }
2057 break;
2058
2059
2060 case StabSourceFileOptions:
2061 // N_OPT - emitted with gcc2_compiled and in gcc source
2062 type = eSymbolTypeCompiler;
2063 break;
2064
2065 case StabRegisterSymbol:
2066 // N_RSYM - register sym: name,,NO_SECT,type,register
2067 type = eSymbolTypeVariable;
2068 break;
2069
2070 case StabSourceLine:
2071 // N_SLINE - src line: 0,,n_sect,linenumber,address
2072 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2073 type = eSymbolTypeLineEntry;
2074 break;
2075
2076 case StabStructureType:
2077 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2078 type = eSymbolTypeVariableType;
2079 break;
2080
2081 case StabSourceFileName:
2082 // N_SO - source file name
2083 type = eSymbolTypeSourceFile;
2084 if (symbol_name == NULL)
2085 {
2086 if (minimize)
2087 add_nlist = false;
2088 if (N_SO_index != UINT32_MAX)
2089 {
2090 // Set the size of the N_SO to the terminating index of this N_SO
2091 // so that we can always skip the entire N_SO if we need to navigate
2092 // more quickly at the source level when parsing STABS
2093 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2094 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2095 symbol_ptr->SetSizeIsSibling(true);
2096 }
2097 N_NSYM_indexes.clear();
2098 N_INCL_indexes.clear();
2099 N_BRAC_indexes.clear();
2100 N_COMM_indexes.clear();
2101 N_FUN_indexes.clear();
2102 N_SO_index = UINT32_MAX;
2103 }
2104 else
2105 {
2106 // We use the current number of symbols in the symbol table in lieu of
2107 // using nlist_idx in case we ever start trimming entries out
2108 const bool N_SO_has_full_path = symbol_name[0] == '/';
2109 if (N_SO_has_full_path)
2110 {
2111 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2112 {
2113 // We have two consecutive N_SO entries where the first contains a directory
2114 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00002115 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002116 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2117 add_nlist = false;
2118 }
2119 else
2120 {
2121 // This is the first entry in a N_SO that contains a directory or
2122 // a full path to the source file
2123 N_SO_index = sym_idx;
2124 }
2125 }
2126 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2127 {
2128 // This is usually the second N_SO entry that contains just the filename,
2129 // so here we combine it with the first one if we are minimizing the symbol table
2130 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2131 if (so_path && so_path[0])
2132 {
2133 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002134 const size_t double_slash_pos = full_so_path.find("//");
2135 if (double_slash_pos != std::string::npos)
2136 {
2137 // The linker has been generating bad N_SO entries with doubled up paths
2138 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2139 // and the second is the directory for the source file so you end up with
2140 // a path that looks like "/tmp/src//tmp/src/"
2141 FileSpec so_dir(so_path, false);
2142 if (!so_dir.Exists())
2143 {
2144 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2145 if (so_dir.Exists())
2146 {
2147 // Trim off the incorrect path
2148 full_so_path.erase(0, double_slash_pos + 1);
2149 }
2150 }
2151 }
Jason Molendab62abd52012-06-21 01:51:02 +00002152 if (*full_so_path.rbegin() != '/')
2153 full_so_path += '/';
2154 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00002155 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002156 add_nlist = false;
2157 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2158 }
2159 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002160 else
2161 {
2162 // This could be a relative path to a N_SO
2163 N_SO_index = sym_idx;
2164 }
Jason Molendab62abd52012-06-21 01:51:02 +00002165 }
Jason Molendab62abd52012-06-21 01:51:02 +00002166 break;
2167
2168 case StabObjectFileName:
2169 // N_OSO - object file name: name,,0,0,st_mtime
2170 type = eSymbolTypeObjectFile;
2171 break;
2172
2173 case StabLocalSymbol:
2174 // N_LSYM - local sym: name,,NO_SECT,type,offset
2175 type = eSymbolTypeLocal;
2176 break;
2177
2178 //----------------------------------------------------------------------
2179 // INCL scopes
2180 //----------------------------------------------------------------------
2181 case StabBeginIncludeFileName:
2182 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2183 // We use the current number of symbols in the symbol table in lieu of
2184 // using nlist_idx in case we ever start trimming entries out
2185 N_INCL_indexes.push_back(sym_idx);
2186 type = eSymbolTypeScopeBegin;
2187 break;
2188
2189 case StabEndIncludeFile:
2190 // N_EINCL - include file end: name,,NO_SECT,0,0
2191 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2192 // so that we can always skip the entire symbol if we need to navigate
2193 // more quickly at the source level when parsing STABS
2194 if ( !N_INCL_indexes.empty() )
2195 {
2196 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2197 symbol_ptr->SetByteSize(sym_idx + 1);
2198 symbol_ptr->SetSizeIsSibling(true);
2199 N_INCL_indexes.pop_back();
2200 }
2201 type = eSymbolTypeScopeEnd;
2202 break;
2203
2204 case StabIncludeFileName:
2205 // N_SOL - #included file name: name,,n_sect,0,address
2206 type = eSymbolTypeHeaderFile;
2207
2208 // We currently don't use the header files on darwin
2209 if (minimize)
2210 add_nlist = false;
2211 break;
2212
2213 case StabCompilerParameters:
2214 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2215 type = eSymbolTypeCompiler;
2216 break;
2217
2218 case StabCompilerVersion:
2219 // N_VERSION - compiler version: name,,NO_SECT,0,0
2220 type = eSymbolTypeCompiler;
2221 break;
2222
2223 case StabCompilerOptLevel:
2224 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2225 type = eSymbolTypeCompiler;
2226 break;
2227
2228 case StabParameter:
2229 // N_PSYM - parameter: name,,NO_SECT,type,offset
2230 type = eSymbolTypeVariable;
2231 break;
2232
2233 case StabAlternateEntry:
2234 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2235 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2236 type = eSymbolTypeLineEntry;
2237 break;
2238
2239 //----------------------------------------------------------------------
2240 // Left and Right Braces
2241 //----------------------------------------------------------------------
2242 case StabLeftBracket:
2243 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2244 // We use the current number of symbols in the symbol table in lieu of
2245 // using nlist_idx in case we ever start trimming entries out
2246 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2247 N_BRAC_indexes.push_back(sym_idx);
2248 type = eSymbolTypeScopeBegin;
2249 break;
2250
2251 case StabRightBracket:
2252 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2253 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2254 // so that we can always skip the entire symbol if we need to navigate
2255 // more quickly at the source level when parsing STABS
2256 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2257 if ( !N_BRAC_indexes.empty() )
2258 {
2259 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2260 symbol_ptr->SetByteSize(sym_idx + 1);
2261 symbol_ptr->SetSizeIsSibling(true);
2262 N_BRAC_indexes.pop_back();
2263 }
2264 type = eSymbolTypeScopeEnd;
2265 break;
2266
2267 case StabDeletedIncludeFile:
2268 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2269 type = eSymbolTypeHeaderFile;
2270 break;
2271
2272 //----------------------------------------------------------------------
2273 // COMM scopes
2274 //----------------------------------------------------------------------
2275 case StabBeginCommon:
2276 // N_BCOMM - begin common: name,,NO_SECT,0,0
2277 // We use the current number of symbols in the symbol table in lieu of
2278 // using nlist_idx in case we ever start trimming entries out
2279 type = eSymbolTypeScopeBegin;
2280 N_COMM_indexes.push_back(sym_idx);
2281 break;
2282
2283 case StabEndCommonLocal:
2284 // N_ECOML - end common (local name): 0,,n_sect,0,address
2285 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2286 // Fall through
2287
2288 case StabEndCommon:
2289 // N_ECOMM - end common: name,,n_sect,0,0
2290 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2291 // so that we can always skip the entire symbol if we need to navigate
2292 // more quickly at the source level when parsing STABS
2293 if ( !N_COMM_indexes.empty() )
2294 {
2295 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2296 symbol_ptr->SetByteSize(sym_idx + 1);
2297 symbol_ptr->SetSizeIsSibling(true);
2298 N_COMM_indexes.pop_back();
2299 }
2300 type = eSymbolTypeScopeEnd;
2301 break;
2302
2303 case StabLength:
2304 // N_LENG - second stab entry with length information
2305 type = eSymbolTypeAdditional;
2306 break;
2307
2308 default: break;
2309 }
2310 }
2311 else
2312 {
2313 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2314 uint8_t n_type = NlistMaskType & nlist.n_type;
2315 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2316
2317 switch (n_type)
2318 {
2319 case NListTypeIndirect: // N_INDR - Fall through
2320 case NListTypePreboundUndefined:// N_PBUD - Fall through
2321 case NListTypeUndefined: // N_UNDF
2322 type = eSymbolTypeUndefined;
2323 break;
2324
2325 case NListTypeAbsolute: // N_ABS
2326 type = eSymbolTypeAbsolute;
2327 break;
2328
2329 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002330 {
2331 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002332
Greg Clayton01e6a582012-11-27 01:52:16 +00002333 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002334 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002335 // TODO: warn about this?
2336 add_nlist = false;
2337 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002338 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002339
2340 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002341 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002342 type = eSymbolTypeException;
2343 }
2344 else
2345 {
2346 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002347
Greg Clayton01e6a582012-11-27 01:52:16 +00002348 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002349 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002350 case SectionTypeRegular: break; // regular section
2351 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2352 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2353 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2354 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2355 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2356 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2357 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2358 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2359 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2360 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2361 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2362 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2363 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2364 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2365 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2366 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2367 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002368 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002369
Greg Clayton01e6a582012-11-27 01:52:16 +00002370 if (type == eSymbolTypeInvalid)
2371 {
2372 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2373 if (symbol_section->IsDescendant (text_section_sp.get()))
2374 {
2375 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2376 SectionAttrUserSelfModifyingCode |
2377 SectionAttrSytemSomeInstructions))
2378 type = eSymbolTypeData;
2379 else
2380 type = eSymbolTypeCode;
2381 }
2382 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002383 {
2384 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2385 {
2386 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002387
Greg Clayton01e6a582012-11-27 01:52:16 +00002388 if (symbol_name &&
2389 symbol_name[0] == '_' &&
2390 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002391 symbol_name[2] == 'B')
2392 {
2393 llvm::StringRef symbol_name_ref(symbol_name);
2394 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2395 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2396 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2397 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2398 {
2399 symbol_name_non_abi_mangled = symbol_name + 1;
2400 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2401 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002402 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002403 }
2404 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2405 {
2406 symbol_name_non_abi_mangled = symbol_name + 1;
2407 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2408 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002409 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002410 }
2411 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2412 {
2413 symbol_name_non_abi_mangled = symbol_name + 1;
2414 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2415 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002416 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002417 }
2418 }
2419 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002420 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002421 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002422 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002423 }
2424 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002425 {
2426 type = eSymbolTypeData;
2427 }
2428 }
2429 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2430 {
2431 type = eSymbolTypeTrampoline;
2432 }
2433 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2434 {
2435 type = eSymbolTypeRuntime;
2436 if (symbol_name && symbol_name[0] == '.')
2437 {
2438 llvm::StringRef symbol_name_ref(symbol_name);
2439 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2440 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002441 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002442 symbol_name_non_abi_mangled = symbol_name;
2443 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2444 type = eSymbolTypeObjCClass;
2445 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002446 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002447 }
2448 }
2449 }
Jason Molendab62abd52012-06-21 01:51:02 +00002450 }
2451 }
Jason Molendab62abd52012-06-21 01:51:02 +00002452 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002453 }
Jason Molendab62abd52012-06-21 01:51:02 +00002454 }
2455
2456 if (add_nlist)
2457 {
2458 uint64_t symbol_value = nlist.n_value;
2459 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002460
Jason Molendab62abd52012-06-21 01:51:02 +00002461 if (symbol_name_non_abi_mangled)
2462 {
Jason Molenda292cca82012-07-20 03:35:44 +00002463 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2464 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002465 }
2466 else
2467 {
2468 if (symbol_name && symbol_name[0] == '_')
2469 {
2470 symbol_name_is_mangled = symbol_name[1] == '_';
2471 symbol_name++; // Skip the leading underscore
2472 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002473
Jason Molendab62abd52012-06-21 01:51:02 +00002474 if (symbol_name)
2475 {
Jason Molenda292cca82012-07-20 03:35:44 +00002476 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002477 }
2478 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002479
Jason Molendab62abd52012-06-21 01:51:02 +00002480 if (is_debug == false)
2481 {
2482 if (type == eSymbolTypeCode)
2483 {
2484 // See if we can find a N_FUN entry for any code symbols.
2485 // If we do find a match, and the name matches, then we
2486 // can merge the two into just the function symbol to avoid
2487 // duplicate entries in the symbol table
2488 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2489 if (pos != N_FUN_addr_to_sym_idx.end())
2490 {
2491 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2492 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2493 {
2494 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2495 // We just need the flags from the linker symbol, so put these flags
2496 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2497 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2498 sym[sym_idx].Clear();
2499 continue;
2500 }
2501 }
2502 }
2503 else if (type == eSymbolTypeData)
2504 {
2505 // See if we can find a N_STSYM entry for any data symbols.
2506 // If we do find a match, and the name matches, then we
2507 // can merge the two into just the Static symbol to avoid
2508 // duplicate entries in the symbol table
2509 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2510 if (pos != N_STSYM_addr_to_sym_idx.end())
2511 {
2512 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2513 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2514 {
2515 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2516 // We just need the flags from the linker symbol, so put these flags
2517 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2518 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2519 sym[sym_idx].Clear();
2520 continue;
2521 }
2522 }
2523 }
2524 }
2525 if (symbol_section)
2526 {
2527 const addr_t section_file_addr = symbol_section->GetFileAddress();
2528 if (symbol_byte_size == 0 && function_starts_count > 0)
2529 {
2530 addr_t symbol_lookup_file_addr = nlist.n_value;
2531 // Do an exact address match for non-ARM addresses, else get the closest since
2532 // the symbol might be a thumb symbol which has an address with bit zero set
2533 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2534 if (is_arm && func_start_entry)
2535 {
2536 // Verify that the function start address is the symbol address (ARM)
2537 // or the symbol address + 1 (thumb)
2538 if (func_start_entry->addr != symbol_lookup_file_addr &&
2539 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2540 {
2541 // Not the right entry, NULL it out...
2542 func_start_entry = NULL;
2543 }
2544 }
2545 if (func_start_entry)
2546 {
2547 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002548
Jason Molendab62abd52012-06-21 01:51:02 +00002549 addr_t symbol_file_addr = func_start_entry->addr;
2550 uint32_t symbol_flags = 0;
2551 if (is_arm)
2552 {
2553 if (symbol_file_addr & 1)
2554 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2555 symbol_file_addr &= 0xfffffffffffffffeull;
2556 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002557
Jason Molendab62abd52012-06-21 01:51:02 +00002558 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2559 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2560 if (next_func_start_entry)
2561 {
2562 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2563 // Be sure the clear the Thumb address bit when we calculate the size
2564 // from the current and next address
2565 if (is_arm)
2566 next_symbol_file_addr &= 0xfffffffffffffffeull;
2567 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2568 }
2569 else
2570 {
2571 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2572 }
2573 }
2574 }
2575 symbol_value -= section_file_addr;
2576 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002577
Jason Molendab62abd52012-06-21 01:51:02 +00002578 sym[sym_idx].SetID (nlist_idx);
2579 sym[sym_idx].SetType (type);
2580 sym[sym_idx].GetAddress().SetSection (symbol_section);
2581 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2582 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002583
Jason Molendab62abd52012-06-21 01:51:02 +00002584 if (symbol_byte_size > 0)
2585 sym[sym_idx].SetByteSize(symbol_byte_size);
2586
Greg Clayton01e6a582012-11-27 01:52:16 +00002587 if (demangled_is_synthesized)
2588 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002589 ++sym_idx;
2590 }
2591 else
2592 {
2593 sym[sym_idx].Clear();
2594 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002595
Jason Molendab62abd52012-06-21 01:51:02 +00002596 }
2597 /////////////////////////////
2598 }
2599 break; // No more entries to consider
2600 }
2601 }
2602 }
2603 }
2604 }
2605 }
2606 }
2607
2608 // Must reset this in case it was mutated above!
2609 nlist_data_offset = 0;
2610#endif
2611
2612 // If the sym array was not created while parsing the DSC unmapped
2613 // symbols, create it now.
2614 if (sym == NULL)
2615 {
2616 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2617 num_syms = symtab->GetNumSymbols();
2618 }
2619
2620 if (unmapped_local_symbols_found)
2621 {
2622 assert(m_dysymtab.ilocalsym == 0);
2623 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2624 nlist_idx = m_dysymtab.nlocalsym;
2625 }
2626 else
2627 {
2628 nlist_idx = 0;
2629 }
2630
2631 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002632 {
2633 struct nlist_64 nlist;
2634 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2635 break;
2636
2637 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2638 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2639 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2640 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2641 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2642
2643 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002644 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002645
Greg Clayton3a5dc012012-05-25 17:04:00 +00002646 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002647 {
2648 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002649
Greg Claytondd29b972012-05-18 23:20:01 +00002650 if (symbol_name == NULL)
2651 {
2652 // No symbol should be NULL, even the symbols with no
2653 // string values should have an offset zero which points
2654 // to an empty C-string
2655 Host::SystemLog (Host::eSystemLogError,
Jason Molenda9badb6c2013-03-06 23:19:17 +00002656 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002657 nlist_idx,
2658 nlist.n_strx,
2659 module_sp->GetFileSpec().GetDirectory().GetCString(),
2660 module_sp->GetFileSpec().GetFilename().GetCString());
2661 continue;
2662 }
2663 if (symbol_name[0] == '\0')
2664 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002665 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002666 else
2667 {
2668 const addr_t str_addr = strtab_addr + nlist.n_strx;
2669 Error str_error;
2670 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2671 symbol_name = memory_symbol_name.c_str();
2672 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002673 const char *symbol_name_non_abi_mangled = NULL;
2674
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002675 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002676 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002677 bool add_nlist = true;
2678 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002679 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002680
2681 assert (sym_idx < num_syms);
2682
2683 sym[sym_idx].SetDebug (is_debug);
2684
2685 if (is_debug)
2686 {
2687 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002688 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002689 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002690 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2691 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002692
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002693 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2694 // have the same address, but we want to ensure that we always find only the real symbol,
2695 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2696 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2697 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2698 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002699
2700 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002701 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2702 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2703 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2704 add_nlist = false;
2705 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002706 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002707 sym[sym_idx].SetExternal(true);
2708 if (nlist.n_value != 0)
2709 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2710 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002711 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002712 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002713
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002714 case StabFunctionName:
2715 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2716 type = eSymbolTypeCompiler;
2717 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002718
Jason Molenda9badb6c2013-03-06 23:19:17 +00002719 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002720 // N_FUN -- procedure: name,,n_sect,linenumber,address
2721 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002722 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002723 type = eSymbolTypeCode;
2724 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002725
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002726 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2727 // We use the current number of symbols in the symbol table in lieu of
2728 // using nlist_idx in case we ever start trimming entries out
2729 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002730 }
2731 else
2732 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002733 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002734
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002735 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002736 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002737 // Copy the size of the function into the original STAB entry so we don't have
2738 // to hunt for it later
2739 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2740 N_FUN_indexes.pop_back();
2741 // We don't really need the end function STAB as it contains the size which
2742 // we already placed with the original symbol, so don't add it if we want a
2743 // minimal symbol table
2744 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002745 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002746 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002747 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002748 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002749
Jason Molenda9badb6c2013-03-06 23:19:17 +00002750 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002751 // N_STSYM -- static symbol: name,,n_sect,type,address
2752 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2753 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2754 type = eSymbolTypeData;
2755 break;
2756
2757 case StabLocalCommon:
2758 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2759 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2760 type = eSymbolTypeCommonBlock;
2761 break;
2762
2763 case StabBeginSymbol:
2764 // N_BNSYM
2765 // We use the current number of symbols in the symbol table in lieu of
2766 // using nlist_idx in case we ever start trimming entries out
2767 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002768 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002769 // Skip these if we want minimal symbol tables
2770 add_nlist = false;
2771 }
2772 else
2773 {
2774 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2775 N_NSYM_indexes.push_back(sym_idx);
2776 type = eSymbolTypeScopeBegin;
2777 }
2778 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002779
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002780 case StabEndSymbol:
2781 // N_ENSYM
2782 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2783 // so that we can always skip the entire symbol if we need to navigate
2784 // more quickly at the source level when parsing STABS
2785 if (minimize)
2786 {
2787 // Skip these if we want minimal symbol tables
2788 add_nlist = false;
2789 }
2790 else
2791 {
2792 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002793 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002794 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2795 symbol_ptr->SetByteSize(sym_idx + 1);
2796 symbol_ptr->SetSizeIsSibling(true);
2797 N_NSYM_indexes.pop_back();
2798 }
2799 type = eSymbolTypeScopeEnd;
2800 }
2801 break;
2802
2803
2804 case StabSourceFileOptions:
2805 // N_OPT - emitted with gcc2_compiled and in gcc source
2806 type = eSymbolTypeCompiler;
2807 break;
2808
2809 case StabRegisterSymbol:
2810 // N_RSYM - register sym: name,,NO_SECT,type,register
2811 type = eSymbolTypeVariable;
2812 break;
2813
2814 case StabSourceLine:
2815 // N_SLINE - src line: 0,,n_sect,linenumber,address
2816 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2817 type = eSymbolTypeLineEntry;
2818 break;
2819
2820 case StabStructureType:
2821 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2822 type = eSymbolTypeVariableType;
2823 break;
2824
2825 case StabSourceFileName:
2826 // N_SO - source file name
2827 type = eSymbolTypeSourceFile;
2828 if (symbol_name == NULL)
2829 {
2830 if (minimize)
2831 add_nlist = false;
2832 if (N_SO_index != UINT32_MAX)
2833 {
2834 // Set the size of the N_SO to the terminating index of this N_SO
2835 // so that we can always skip the entire N_SO if we need to navigate
2836 // more quickly at the source level when parsing STABS
2837 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2838 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2839 symbol_ptr->SetSizeIsSibling(true);
2840 }
2841 N_NSYM_indexes.clear();
2842 N_INCL_indexes.clear();
2843 N_BRAC_indexes.clear();
2844 N_COMM_indexes.clear();
2845 N_FUN_indexes.clear();
2846 N_SO_index = UINT32_MAX;
2847 }
2848 else
2849 {
2850 // We use the current number of symbols in the symbol table in lieu of
2851 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002852 const bool N_SO_has_full_path = symbol_name[0] == '/';
2853 if (N_SO_has_full_path)
2854 {
2855 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2856 {
2857 // We have two consecutive N_SO entries where the first contains a directory
2858 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002859 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002860 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2861 add_nlist = false;
2862 }
2863 else
2864 {
2865 // This is the first entry in a N_SO that contains a directory or
2866 // a full path to the source file
2867 N_SO_index = sym_idx;
2868 }
2869 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002870 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2871 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002872 // This is usually the second N_SO entry that contains just the filename,
2873 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002874 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2875 if (so_path && so_path[0])
2876 {
2877 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002878 const size_t double_slash_pos = full_so_path.find("//");
2879 if (double_slash_pos != std::string::npos)
2880 {
2881 // The linker has been generating bad N_SO entries with doubled up paths
2882 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2883 // and the second is the directory for the source file so you end up with
2884 // a path that looks like "/tmp/src//tmp/src/"
2885 FileSpec so_dir(so_path, false);
2886 if (!so_dir.Exists())
2887 {
2888 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2889 if (so_dir.Exists())
2890 {
2891 // Trim off the incorrect path
2892 full_so_path.erase(0, double_slash_pos + 1);
2893 }
2894 }
2895 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002896 if (*full_so_path.rbegin() != '/')
2897 full_so_path += '/';
2898 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002899 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002900 add_nlist = false;
2901 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2902 }
2903 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002904 else
2905 {
2906 // This could be a relative path to a N_SO
2907 N_SO_index = sym_idx;
2908 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002909 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002910
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002911 break;
2912
2913 case StabObjectFileName:
2914 // N_OSO - object file name: name,,0,0,st_mtime
2915 type = eSymbolTypeObjectFile;
2916 break;
2917
2918 case StabLocalSymbol:
2919 // N_LSYM - local sym: name,,NO_SECT,type,offset
2920 type = eSymbolTypeLocal;
2921 break;
2922
2923 //----------------------------------------------------------------------
2924 // INCL scopes
2925 //----------------------------------------------------------------------
2926 case StabBeginIncludeFileName:
2927 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2928 // We use the current number of symbols in the symbol table in lieu of
2929 // using nlist_idx in case we ever start trimming entries out
2930 N_INCL_indexes.push_back(sym_idx);
2931 type = eSymbolTypeScopeBegin;
2932 break;
2933
2934 case StabEndIncludeFile:
2935 // N_EINCL - include file end: name,,NO_SECT,0,0
2936 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2937 // so that we can always skip the entire symbol if we need to navigate
2938 // more quickly at the source level when parsing STABS
2939 if ( !N_INCL_indexes.empty() )
2940 {
2941 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2942 symbol_ptr->SetByteSize(sym_idx + 1);
2943 symbol_ptr->SetSizeIsSibling(true);
2944 N_INCL_indexes.pop_back();
2945 }
2946 type = eSymbolTypeScopeEnd;
2947 break;
2948
2949 case StabIncludeFileName:
2950 // N_SOL - #included file name: name,,n_sect,0,address
2951 type = eSymbolTypeHeaderFile;
2952
2953 // We currently don't use the header files on darwin
2954 if (minimize)
2955 add_nlist = false;
2956 break;
2957
Jason Molenda9badb6c2013-03-06 23:19:17 +00002958 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002959 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2960 type = eSymbolTypeCompiler;
2961 break;
2962
2963 case StabCompilerVersion:
2964 // N_VERSION - compiler version: name,,NO_SECT,0,0
2965 type = eSymbolTypeCompiler;
2966 break;
2967
2968 case StabCompilerOptLevel:
2969 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2970 type = eSymbolTypeCompiler;
2971 break;
2972
2973 case StabParameter:
2974 // N_PSYM - parameter: name,,NO_SECT,type,offset
2975 type = eSymbolTypeVariable;
2976 break;
2977
2978 case StabAlternateEntry:
2979 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2980 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2981 type = eSymbolTypeLineEntry;
2982 break;
2983
2984 //----------------------------------------------------------------------
2985 // Left and Right Braces
2986 //----------------------------------------------------------------------
2987 case StabLeftBracket:
2988 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2989 // We use the current number of symbols in the symbol table in lieu of
2990 // using nlist_idx in case we ever start trimming entries out
2991 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2992 N_BRAC_indexes.push_back(sym_idx);
2993 type = eSymbolTypeScopeBegin;
2994 break;
2995
2996 case StabRightBracket:
2997 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2998 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2999 // so that we can always skip the entire symbol if we need to navigate
3000 // more quickly at the source level when parsing STABS
3001 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3002 if ( !N_BRAC_indexes.empty() )
3003 {
3004 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3005 symbol_ptr->SetByteSize(sym_idx + 1);
3006 symbol_ptr->SetSizeIsSibling(true);
3007 N_BRAC_indexes.pop_back();
3008 }
3009 type = eSymbolTypeScopeEnd;
3010 break;
3011
3012 case StabDeletedIncludeFile:
3013 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3014 type = eSymbolTypeHeaderFile;
3015 break;
3016
3017 //----------------------------------------------------------------------
3018 // COMM scopes
3019 //----------------------------------------------------------------------
3020 case StabBeginCommon:
3021 // N_BCOMM - begin common: name,,NO_SECT,0,0
3022 // We use the current number of symbols in the symbol table in lieu of
3023 // using nlist_idx in case we ever start trimming entries out
3024 type = eSymbolTypeScopeBegin;
3025 N_COMM_indexes.push_back(sym_idx);
3026 break;
3027
3028 case StabEndCommonLocal:
3029 // N_ECOML - end common (local name): 0,,n_sect,0,address
3030 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3031 // Fall through
3032
3033 case StabEndCommon:
3034 // N_ECOMM - end common: name,,n_sect,0,0
3035 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3036 // so that we can always skip the entire symbol if we need to navigate
3037 // more quickly at the source level when parsing STABS
3038 if ( !N_COMM_indexes.empty() )
3039 {
3040 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3041 symbol_ptr->SetByteSize(sym_idx + 1);
3042 symbol_ptr->SetSizeIsSibling(true);
3043 N_COMM_indexes.pop_back();
3044 }
3045 type = eSymbolTypeScopeEnd;
3046 break;
3047
3048 case StabLength:
3049 // N_LENG - second stab entry with length information
3050 type = eSymbolTypeAdditional;
3051 break;
3052
3053 default: break;
3054 }
3055 }
3056 else
3057 {
3058 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3059 uint8_t n_type = NlistMaskType & nlist.n_type;
3060 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3061
3062 switch (n_type)
3063 {
3064 case NListTypeIndirect: // N_INDR - Fall through
3065 case NListTypePreboundUndefined:// N_PBUD - Fall through
3066 case NListTypeUndefined: // N_UNDF
3067 type = eSymbolTypeUndefined;
3068 break;
3069
3070 case NListTypeAbsolute: // N_ABS
3071 type = eSymbolTypeAbsolute;
3072 break;
3073
3074 case NListTypeSection: // N_SECT
3075 {
3076 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3077
Sean Callananb386d822012-08-09 00:50:26 +00003078 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003079 {
3080 // TODO: warn about this?
3081 add_nlist = false;
3082 break;
3083 }
3084
3085 if (TEXT_eh_frame_sectID == nlist.n_sect)
3086 {
3087 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00003088 }
3089 else
3090 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003091 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3092
3093 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00003094 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003095 case SectionTypeRegular: break; // regular section
3096 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3097 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3098 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3099 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3100 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3101 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3102 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3103 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3104 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3105 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3106 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3107 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3108 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3109 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3110 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3111 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3112 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00003113 }
Chris Lattner24943d22010-06-08 16:52:24 +00003114
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003115 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00003116 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003117 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3118 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003119 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003120 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3121 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003122 SectionAttrSytemSomeInstructions))
3123 type = eSymbolTypeData;
3124 else
3125 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00003126 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003127 else
3128 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003129 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003130 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00003131 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003132 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00003133
Jason Molenda9badb6c2013-03-06 23:19:17 +00003134 if (symbol_name &&
3135 symbol_name[0] == '_' &&
3136 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003137 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00003138 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003139 llvm::StringRef symbol_name_ref(symbol_name);
3140 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3141 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3142 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3143 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00003144 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003145 symbol_name_non_abi_mangled = symbol_name + 1;
3146 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3147 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003148 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003149 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003150 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003151 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003152 symbol_name_non_abi_mangled = symbol_name + 1;
3153 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3154 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003155 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003156 }
3157 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3158 {
3159 symbol_name_non_abi_mangled = symbol_name + 1;
3160 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3161 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003162 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003163 }
3164 }
3165 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003166 else
3167 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3168 {
3169 type = eSymbolTypeException;
3170 }
3171 else
3172 {
3173 type = eSymbolTypeData;
3174 }
3175 }
3176 else
3177 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3178 {
3179 type = eSymbolTypeTrampoline;
3180 }
3181 else
3182 if (symbol_section->IsDescendant(objc_section_sp.get()))
3183 {
3184 type = eSymbolTypeRuntime;
3185 if (symbol_name && symbol_name[0] == '.')
3186 {
3187 llvm::StringRef symbol_name_ref(symbol_name);
3188 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3189 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3190 {
3191 symbol_name_non_abi_mangled = symbol_name;
3192 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3193 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003194 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003195 }
3196 }
Chris Lattner24943d22010-06-08 16:52:24 +00003197 }
3198 }
3199 }
3200 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003201 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003202 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003203 }
3204
3205 if (add_nlist)
3206 {
3207 uint64_t symbol_value = nlist.n_value;
3208 bool symbol_name_is_mangled = false;
3209
3210 if (symbol_name_non_abi_mangled)
3211 {
Greg Claytonc0240042012-07-18 23:18:10 +00003212 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3213 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003214 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003215 else
3216 {
3217 if (symbol_name && symbol_name[0] == '_')
3218 {
3219 symbol_name_is_mangled = symbol_name[1] == '_';
3220 symbol_name++; // Skip the leading underscore
3221 }
3222
3223 if (symbol_name)
3224 {
Greg Claytonc0240042012-07-18 23:18:10 +00003225 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003226 }
3227 }
3228
3229 if (is_debug == false)
3230 {
3231 if (type == eSymbolTypeCode)
3232 {
3233 // See if we can find a N_FUN entry for any code symbols.
3234 // If we do find a match, and the name matches, then we
3235 // can merge the two into just the function symbol to avoid
3236 // duplicate entries in the symbol table
3237 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3238 if (pos != N_FUN_addr_to_sym_idx.end())
3239 {
3240 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3241 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3242 {
3243 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3244 // We just need the flags from the linker symbol, so put these flags
3245 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3246 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3247 sym[sym_idx].Clear();
3248 continue;
3249 }
3250 }
3251 }
3252 else if (type == eSymbolTypeData)
3253 {
3254 // See if we can find a N_STSYM entry for any data symbols.
3255 // If we do find a match, and the name matches, then we
3256 // can merge the two into just the Static symbol to avoid
3257 // duplicate entries in the symbol table
3258 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3259 if (pos != N_STSYM_addr_to_sym_idx.end())
3260 {
3261 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3262 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3263 {
3264 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3265 // We just need the flags from the linker symbol, so put these flags
3266 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3267 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3268 sym[sym_idx].Clear();
3269 continue;
3270 }
3271 }
3272 }
3273 }
3274 if (symbol_section)
3275 {
3276 const addr_t section_file_addr = symbol_section->GetFileAddress();
3277 if (symbol_byte_size == 0 && function_starts_count > 0)
3278 {
Greg Claytond2653c22012-03-14 01:53:24 +00003279 addr_t symbol_lookup_file_addr = nlist.n_value;
3280 // Do an exact address match for non-ARM addresses, else get the closest since
3281 // the symbol might be a thumb symbol which has an address with bit zero set
3282 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3283 if (is_arm && func_start_entry)
3284 {
3285 // Verify that the function start address is the symbol address (ARM)
3286 // or the symbol address + 1 (thumb)
3287 if (func_start_entry->addr != symbol_lookup_file_addr &&
3288 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3289 {
3290 // Not the right entry, NULL it out...
3291 func_start_entry = NULL;
3292 }
3293 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003294 if (func_start_entry)
3295 {
3296 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003297
Greg Claytond2653c22012-03-14 01:53:24 +00003298 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003299 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003300 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003301
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003302 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3303 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3304 if (next_func_start_entry)
3305 {
Greg Claytond2653c22012-03-14 01:53:24 +00003306 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3307 // Be sure the clear the Thumb address bit when we calculate the size
3308 // from the current and next address
3309 if (is_arm)
3310 next_symbol_file_addr &= 0xfffffffffffffffeull;
3311 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 +00003312 }
3313 else
3314 {
Greg Claytond2653c22012-03-14 01:53:24 +00003315 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003316 }
3317 }
3318 }
3319 symbol_value -= section_file_addr;
3320 }
3321
3322 sym[sym_idx].SetID (nlist_idx);
3323 sym[sym_idx].SetType (type);
3324 sym[sym_idx].GetAddress().SetSection (symbol_section);
3325 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3326 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3327
3328 if (symbol_byte_size > 0)
3329 sym[sym_idx].SetByteSize(symbol_byte_size);
3330
Greg Clayton01e6a582012-11-27 01:52:16 +00003331 if (demangled_is_synthesized)
3332 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3333
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003334 ++sym_idx;
3335 }
3336 else
3337 {
3338 sym[sym_idx].Clear();
3339 }
3340
3341 }
3342
3343 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3344 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3345 // such entries by figuring out what the address for the global is by looking up this non-STAB
3346 // entry and copying the value into the debug symbol's value to save us the hassle in the
3347 // debug symbol parser.
3348
3349 Symbol *global_symbol = NULL;
3350 for (nlist_idx = 0;
3351 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3352 nlist_idx++)
3353 {
3354 if (global_symbol->GetAddress().GetFileAddress() == 0)
3355 {
3356 std::vector<uint32_t> indexes;
3357 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3358 {
3359 std::vector<uint32_t>::const_iterator pos;
3360 std::vector<uint32_t>::const_iterator end = indexes.end();
3361 for (pos = indexes.begin(); pos != end; ++pos)
3362 {
3363 symbol_ptr = symtab->SymbolAtIndex(*pos);
3364 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3365 {
3366 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3367 break;
3368 }
3369 }
3370 }
Chris Lattner24943d22010-06-08 16:52:24 +00003371 }
3372 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003373
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003374 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3375
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003376 if (function_starts_count > 0)
3377 {
3378 char synthetic_function_symbol[PATH_MAX];
3379 uint32_t num_synthetic_function_symbols = 0;
3380 for (i=0; i<function_starts_count; ++i)
3381 {
3382 if (function_starts.GetEntryRef (i).data == false)
3383 ++num_synthetic_function_symbols;
3384 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003385
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003386 if (num_synthetic_function_symbols > 0)
3387 {
3388 if (num_syms < sym_idx + num_synthetic_function_symbols)
3389 {
3390 num_syms = sym_idx + num_synthetic_function_symbols;
3391 sym = symtab->Resize (num_syms);
3392 }
3393 uint32_t synthetic_function_symbol_idx = 0;
3394 for (i=0; i<function_starts_count; ++i)
3395 {
3396 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3397 if (func_start_entry->data == false)
3398 {
Greg Claytond2653c22012-03-14 01:53:24 +00003399 addr_t symbol_file_addr = func_start_entry->addr;
3400 uint32_t symbol_flags = 0;
3401 if (is_arm)
3402 {
3403 if (symbol_file_addr & 1)
3404 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3405 symbol_file_addr &= 0xfffffffffffffffeull;
3406 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003407 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003408 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003409 {
3410 SectionSP symbol_section (symbol_addr.GetSection());
3411 uint32_t symbol_byte_size = 0;
3412 if (symbol_section)
3413 {
3414 const addr_t section_file_addr = symbol_section->GetFileAddress();
3415 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3416 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3417 if (next_func_start_entry)
3418 {
Greg Claytond2653c22012-03-14 01:53:24 +00003419 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3420 if (is_arm)
3421 next_symbol_file_addr &= 0xfffffffffffffffeull;
3422 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 +00003423 }
3424 else
3425 {
Greg Claytond2653c22012-03-14 01:53:24 +00003426 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003427 }
3428 snprintf (synthetic_function_symbol,
3429 sizeof(synthetic_function_symbol),
3430 "___lldb_unnamed_function%u$$%s",
3431 ++synthetic_function_symbol_idx,
3432 module_sp->GetFileSpec().GetFilename().GetCString());
3433 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003434 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003435 sym[sym_idx].SetType (eSymbolTypeCode);
3436 sym[sym_idx].SetIsSynthetic (true);
3437 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003438 if (symbol_flags)
3439 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003440 if (symbol_byte_size)
3441 sym[sym_idx].SetByteSize (symbol_byte_size);
3442 ++sym_idx;
3443 }
3444 }
3445 }
3446 }
3447 }
3448 }
3449
3450 // Trim our symbols down to just what we ended up with after
3451 // removing any symbols.
3452 if (sym_idx < num_syms)
3453 {
3454 num_syms = sym_idx;
3455 sym = symtab->Resize (num_syms);
3456 }
3457
3458 // Now synthesize indirect symbols
3459 if (m_dysymtab.nindirectsyms != 0)
3460 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003461 if (indirect_symbol_index_data.GetByteSize())
3462 {
3463 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3464
3465 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3466 {
3467 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3468 {
3469 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3470 if (symbol_stub_byte_size == 0)
3471 continue;
3472
3473 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3474
3475 if (num_symbol_stubs == 0)
3476 continue;
3477
3478 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3479 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3480 {
3481 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3482 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 +00003483 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003484 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3485 {
3486 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3487 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3488 continue;
3489
3490 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3491 Symbol *stub_symbol = NULL;
3492 if (index_pos != end_index_pos)
3493 {
3494 // We have a remapping from the original nlist index to
3495 // a current symbol index, so just look this up by index
3496 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3497 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003498 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003499 {
3500 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003501 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003502 // S_SYMBOL_STUBS
3503 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3504 }
3505
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003506 if (stub_symbol)
3507 {
3508 Address so_addr(symbol_stub_addr, section_list);
3509
3510 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3511 {
3512 // Change the external symbol into a trampoline that makes sense
3513 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3514 // can re-use them so we don't have to make up a synthetic symbol
3515 // for no good reason.
3516 stub_symbol->SetType (eSymbolTypeTrampoline);
3517 stub_symbol->SetExternal (false);
3518 stub_symbol->GetAddress() = so_addr;
3519 stub_symbol->SetByteSize (symbol_stub_byte_size);
3520 }
3521 else
3522 {
3523 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003524 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003525 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003526 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003527 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003528 stub_symbol = NULL; // this pointer no longer valid
3529 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003530 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003531 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003532 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3533 sym[sym_idx].SetIsSynthetic (true);
3534 sym[sym_idx].GetAddress() = so_addr;
3535 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3536 ++sym_idx;
3537 }
3538 }
Greg Claytond4330e62012-09-05 01:38:55 +00003539 else
3540 {
3541 if (log)
3542 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3543 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003544 }
3545 }
3546 }
3547 }
3548 }
3549 }
3550 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003551 }
3552 return 0;
3553}
3554
3555
3556void
3557ObjectFileMachO::Dump (Stream *s)
3558{
Greg Clayton9482f052012-03-13 23:14:29 +00003559 ModuleSP module_sp(GetModule());
3560 if (module_sp)
3561 {
3562 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3563 s->Printf("%p: ", this);
3564 s->Indent();
3565 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3566 s->PutCString("ObjectFileMachO64");
3567 else
3568 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003569
Greg Clayton9482f052012-03-13 23:14:29 +00003570 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003571
Greg Clayton9482f052012-03-13 23:14:29 +00003572 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003573
Greg Clayton9482f052012-03-13 23:14:29 +00003574 if (m_sections_ap.get())
3575 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003576
Greg Clayton9482f052012-03-13 23:14:29 +00003577 if (m_symtab_ap.get())
3578 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3579 }
Chris Lattner24943d22010-06-08 16:52:24 +00003580}
3581
Greg Clayton36b877d2013-04-24 22:29:28 +00003582bool
3583ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3584 const lldb_private::DataExtractor &data,
3585 lldb::offset_t lc_offset,
3586 lldb_private::UUID& uuid)
3587{
3588 uint32_t i;
3589 struct uuid_command load_cmd;
3590
3591 lldb::offset_t offset = lc_offset;
3592 for (i=0; i<header.ncmds; ++i)
3593 {
3594 const lldb::offset_t cmd_offset = offset;
3595 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3596 break;
3597
3598 if (load_cmd.cmd == LoadCommandUUID)
3599 {
3600 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3601
3602 if (uuid_bytes)
3603 {
3604 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3605 // We pretend these object files have no UUID to prevent crashing.
3606
3607 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3608 0x3b, 0xa8,
3609 0x4b, 0x16,
3610 0xb6, 0xa4,
3611 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3612
3613 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3614 return false;
3615
3616 uuid.SetBytes (uuid_bytes);
3617 return true;
3618 }
3619 return false;
3620 }
3621 offset = cmd_offset + load_cmd.cmdsize;
3622 }
3623 return false;
3624}
Chris Lattner24943d22010-06-08 16:52:24 +00003625
3626bool
Greg Clayton0467c782011-02-04 18:53:10 +00003627ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003628{
Greg Clayton9482f052012-03-13 23:14:29 +00003629 ModuleSP module_sp(GetModule());
3630 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003631 {
Greg Clayton9482f052012-03-13 23:14:29 +00003632 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00003633 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton36b877d2013-04-24 22:29:28 +00003634 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner24943d22010-06-08 16:52:24 +00003635 }
3636 return false;
3637}
3638
3639
3640uint32_t
3641ObjectFileMachO::GetDependentModules (FileSpecList& files)
3642{
Chris Lattner24943d22010-06-08 16:52:24 +00003643 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003644 ModuleSP module_sp(GetModule());
3645 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003646 {
Greg Clayton9482f052012-03-13 23:14:29 +00003647 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3648 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003649 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003650 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3651 uint32_t i;
3652 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003653 {
Greg Clayton9482f052012-03-13 23:14:29 +00003654 const uint32_t cmd_offset = offset;
3655 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3656 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003657
Greg Clayton9482f052012-03-13 23:14:29 +00003658 switch (load_cmd.cmd)
3659 {
3660 case LoadCommandDylibLoad:
3661 case LoadCommandDylibLoadWeak:
3662 case LoadCommandDylibReexport:
3663 case LoadCommandDynamicLinkerLoad:
3664 case LoadCommandFixedVMShlibLoad:
3665 case LoadCommandDylibLoadUpward:
3666 {
3667 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3668 const char *path = m_data.PeekCStr(name_offset);
3669 // Skip any path that starts with '@' since these are usually:
3670 // @executable_path/.../file
3671 // @rpath/.../file
3672 if (path && path[0] != '@')
3673 {
3674 FileSpec file_spec(path, resolve_path);
3675 if (files.AppendIfUnique(file_spec))
3676 count++;
3677 }
3678 }
3679 break;
3680
3681 default:
3682 break;
3683 }
3684 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003685 }
Chris Lattner24943d22010-06-08 16:52:24 +00003686 }
3687 return count;
3688}
3689
Jim Ingham28775942011-03-07 23:44:08 +00003690lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003691ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003692{
3693 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3694 // is initialized to an invalid address, so we can just return that.
3695 // 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 +00003696
Jim Ingham28775942011-03-07 23:44:08 +00003697 if (!IsExecutable() || m_entry_point_address.IsValid())
3698 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003699
3700 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003701 // /usr/include/mach-o.h, but it is basically:
3702 //
3703 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3704 // uint32_t count - this is the count of longs in the thread state data
3705 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3706 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003707 //
Jim Ingham28775942011-03-07 23:44:08 +00003708 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3709 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3710 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3711 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3712 //
3713 // For now we hard-code the offsets and flavors we need:
3714 //
3715 //
3716
Greg Clayton9482f052012-03-13 23:14:29 +00003717 ModuleSP module_sp(GetModule());
3718 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003719 {
Greg Clayton9482f052012-03-13 23:14:29 +00003720 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3721 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003722 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003723 uint32_t i;
3724 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3725 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003726
Greg Clayton9482f052012-03-13 23:14:29 +00003727 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003728 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003729 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003730 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3731 break;
3732
3733 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003734 {
Greg Clayton9482f052012-03-13 23:14:29 +00003735 case LoadCommandUnixThread:
3736 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003737 {
Greg Clayton9482f052012-03-13 23:14:29 +00003738 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003739 {
Greg Clayton9482f052012-03-13 23:14:29 +00003740 uint32_t flavor = m_data.GetU32(&offset);
3741 uint32_t count = m_data.GetU32(&offset);
3742 if (count == 0)
3743 {
3744 // We've gotten off somehow, log and exit;
3745 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003746 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003747
Greg Clayton9482f052012-03-13 23:14:29 +00003748 switch (m_header.cputype)
3749 {
3750 case llvm::MachO::CPUTypeARM:
3751 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3752 {
3753 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3754 start_address = m_data.GetU32(&offset);
3755 done = true;
3756 }
Jim Ingham28775942011-03-07 23:44:08 +00003757 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003758 case llvm::MachO::CPUTypeI386:
3759 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3760 {
3761 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3762 start_address = m_data.GetU32(&offset);
3763 done = true;
3764 }
3765 break;
3766 case llvm::MachO::CPUTypeX86_64:
3767 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3768 {
3769 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3770 start_address = m_data.GetU64(&offset);
3771 done = true;
3772 }
3773 break;
3774 default:
3775 return m_entry_point_address;
3776 }
3777 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3778 if (done)
3779 break;
3780 offset += count * 4;
3781 }
Jim Ingham28775942011-03-07 23:44:08 +00003782 }
Greg Clayton9482f052012-03-13 23:14:29 +00003783 break;
3784 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003785 {
Greg Clayton9482f052012-03-13 23:14:29 +00003786 ConstString text_segment_name ("__TEXT");
3787 uint64_t entryoffset = m_data.GetU64(&offset);
3788 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3789 if (text_segment_sp)
3790 {
3791 done = true;
3792 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3793 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003794 }
Greg Clayton9482f052012-03-13 23:14:29 +00003795
3796 default:
3797 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003798 }
Greg Clayton9482f052012-03-13 23:14:29 +00003799 if (done)
3800 break;
Jim Ingham28775942011-03-07 23:44:08 +00003801
Greg Clayton9482f052012-03-13 23:14:29 +00003802 // Go to the next load command:
3803 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003804 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003805
Greg Clayton9482f052012-03-13 23:14:29 +00003806 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003807 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003808 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003809 // of this ObjectFile:
3810 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003811 {
Greg Clayton9482f052012-03-13 23:14:29 +00003812 m_entry_point_address.Clear();
3813 }
3814 }
3815 else
3816 {
3817 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3818 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003819
Greg Clayton9482f052012-03-13 23:14:29 +00003820 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003821
Greg Clayton9482f052012-03-13 23:14:29 +00003822 if (module_sp)
3823 {
3824 SymbolContextList contexts;
3825 SymbolContext context;
3826 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3827 {
3828 if (contexts.GetContextAtIndex(0, context))
3829 m_entry_point_address = context.symbol->GetAddress();
3830 }
Greg Clayton3508c382012-02-24 01:59:29 +00003831 }
3832 }
Jim Ingham28775942011-03-07 23:44:08 +00003833 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003834
Jim Ingham28775942011-03-07 23:44:08 +00003835 return m_entry_point_address;
3836
3837}
3838
Greg Claytonb5a8f142012-02-05 02:38:54 +00003839lldb_private::Address
3840ObjectFileMachO::GetHeaderAddress ()
3841{
3842 lldb_private::Address header_addr;
3843 SectionList *section_list = GetSectionList();
3844 if (section_list)
3845 {
3846 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3847 if (text_segment_sp)
3848 {
Greg Clayton3508c382012-02-24 01:59:29 +00003849 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003850 header_addr.SetOffset (0);
3851 }
3852 }
3853 return header_addr;
3854}
3855
Greg Clayton46c9a352012-02-09 06:16:32 +00003856uint32_t
3857ObjectFileMachO::GetNumThreadContexts ()
3858{
Greg Clayton9482f052012-03-13 23:14:29 +00003859 ModuleSP module_sp(GetModule());
3860 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003861 {
Greg Clayton9482f052012-03-13 23:14:29 +00003862 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3863 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003864 {
Greg Clayton9482f052012-03-13 23:14:29 +00003865 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003866 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003867 FileRangeArray::Entry file_range;
3868 thread_command thread_cmd;
3869 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003870 {
Greg Clayton9482f052012-03-13 23:14:29 +00003871 const uint32_t cmd_offset = offset;
3872 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3873 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003874
Greg Clayton9482f052012-03-13 23:14:29 +00003875 if (thread_cmd.cmd == LoadCommandThread)
3876 {
3877 file_range.SetRangeBase (offset);
3878 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3879 m_thread_context_offsets.Append (file_range);
3880 }
3881 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003882 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003883 }
3884 }
3885 return m_thread_context_offsets.GetSize();
3886}
3887
3888lldb::RegisterContextSP
3889ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3890{
Greg Clayton46c9a352012-02-09 06:16:32 +00003891 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003892
Greg Clayton9482f052012-03-13 23:14:29 +00003893 ModuleSP module_sp(GetModule());
3894 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003895 {
Greg Clayton9482f052012-03-13 23:14:29 +00003896 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3897 if (!m_thread_context_offsets_valid)
3898 GetNumThreadContexts ();
3899
3900 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003901 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003902 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003903
3904 DataExtractor data (m_data,
3905 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003906 thread_context_file_range->GetByteSize());
3907
3908 switch (m_header.cputype)
3909 {
3910 case llvm::MachO::CPUTypeARM:
3911 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3912 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003913
Jim Ingham6f01c932012-10-12 17:34:26 +00003914 case llvm::MachO::CPUTypeI386:
3915 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3916 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003917
Jim Ingham6f01c932012-10-12 17:34:26 +00003918 case llvm::MachO::CPUTypeX86_64:
3919 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3920 break;
3921 }
Greg Clayton9482f052012-03-13 23:14:29 +00003922 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003923 }
3924 return reg_ctx_sp;
3925}
3926
Greg Claytonb5a8f142012-02-05 02:38:54 +00003927
Greg Claytonca319972011-07-09 00:41:34 +00003928ObjectFile::Type
3929ObjectFileMachO::CalculateType()
3930{
3931 switch (m_header.filetype)
3932 {
3933 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3934 if (GetAddressByteSize () == 4)
3935 {
3936 // 32 bit kexts are just object files, but they do have a valid
3937 // UUID load command.
3938 UUID uuid;
3939 if (GetUUID(&uuid))
3940 {
3941 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003942 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003943 // "OSKextGetCurrentIdentifier" as this is required of kexts
3944 if (m_strata == eStrataInvalid)
3945 m_strata = eStrataKernel;
3946 return eTypeSharedLibrary;
3947 }
3948 }
3949 return eTypeObjectFile;
3950
3951 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3952 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3953 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3954 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3955 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3956 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3957 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3958 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3959 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3960 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3961 default:
3962 break;
3963 }
3964 return eTypeUnknown;
3965}
3966
3967ObjectFile::Strata
3968ObjectFileMachO::CalculateStrata()
3969{
3970 switch (m_header.filetype)
3971 {
3972 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3973 {
3974 // 32 bit kexts are just object files, but they do have a valid
3975 // UUID load command.
3976 UUID uuid;
3977 if (GetUUID(&uuid))
3978 {
3979 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003980 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003981 // "OSKextGetCurrentIdentifier" as this is required of kexts
3982 if (m_type == eTypeInvalid)
3983 m_type = eTypeSharedLibrary;
3984
3985 return eStrataKernel;
3986 }
3987 }
3988 return eStrataUnknown;
3989
3990 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3991 // Check for the MH_DYLDLINK bit in the flags
3992 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003993 {
Greg Claytonca319972011-07-09 00:41:34 +00003994 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003995 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003996 else
Sean Callananac725af2012-02-10 20:22:35 +00003997 {
3998 SectionList *section_list = GetSectionList();
3999 if (section_list)
4000 {
4001 static ConstString g_kld_section_name ("__KLD");
4002 if (section_list->FindSectionByName(g_kld_section_name))
4003 return eStrataKernel;
4004 }
4005 }
4006 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00004007
4008 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4009 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00004010 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00004011 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4012 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4013 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4014 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4015 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4016 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4017 default:
4018 break;
4019 }
4020 return eStrataUnknown;
4021}
4022
4023
Greg Clayton49f4bf22012-02-22 19:41:02 +00004024uint32_t
4025ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4026{
Greg Clayton9482f052012-03-13 23:14:29 +00004027 ModuleSP module_sp(GetModule());
4028 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004029 {
Greg Clayton9482f052012-03-13 23:14:29 +00004030 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4031 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00004032 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00004033 uint32_t version_cmd = 0;
4034 uint64_t version = 0;
4035 uint32_t i;
4036 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004037 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00004038 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00004039 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4040 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00004041
Greg Clayton9482f052012-03-13 23:14:29 +00004042 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004043 {
Greg Clayton9482f052012-03-13 23:14:29 +00004044 if (version_cmd == 0)
4045 {
4046 version_cmd = load_cmd.cmd;
4047 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4048 break;
4049 version = load_cmd.dylib.current_version;
4050 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004051 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00004052 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00004053 }
Greg Clayton9482f052012-03-13 23:14:29 +00004054 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004055 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004056
Greg Clayton9482f052012-03-13 23:14:29 +00004057 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004058 {
Greg Clayton9482f052012-03-13 23:14:29 +00004059 if (versions != NULL && num_versions > 0)
4060 {
4061 if (num_versions > 0)
4062 versions[0] = (version & 0xFFFF0000ull) >> 16;
4063 if (num_versions > 1)
4064 versions[1] = (version & 0x0000FF00ull) >> 8;
4065 if (num_versions > 2)
4066 versions[2] = (version & 0x000000FFull);
4067 // Fill in an remaining version numbers with invalid values
4068 for (i=3; i<num_versions; ++i)
4069 versions[i] = UINT32_MAX;
4070 }
4071 // The LC_ID_DYLIB load command has a version with 3 version numbers
4072 // in it, so always return 3
4073 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004074 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00004075 }
4076 return false;
4077}
4078
Chris Lattner24943d22010-06-08 16:52:24 +00004079bool
Greg Clayton395fc332011-02-15 21:59:32 +00004080ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00004081{
Greg Clayton9482f052012-03-13 23:14:29 +00004082 ModuleSP module_sp(GetModule());
4083 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004084 {
Greg Clayton9482f052012-03-13 23:14:29 +00004085 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4086 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00004087
Greg Clayton9482f052012-03-13 23:14:29 +00004088 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00004089 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00004090 // unknown to make sure we use the DynamicLoaderStatic()...
4091 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4092 {
4093 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4094 }
4095 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004096 }
Greg Clayton9482f052012-03-13 23:14:29 +00004097 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00004098}
4099
4100
Jason Molenda45c75502013-04-16 06:24:42 +00004101UUID
4102ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4103{
4104 UUID uuid;
4105 if (process)
4106 {
4107 addr_t all_image_infos = process->GetImageInfoAddress();
4108
4109 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4110 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4111 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4112 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4113
4114 Error err;
4115 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4116 if (version_or_magic != -1
4117 && version_or_magic != HeaderMagic32
4118 && version_or_magic != HeaderMagic32Swapped
4119 && version_or_magic != HeaderMagic64
4120 && version_or_magic != HeaderMagic64Swapped
4121 && version_or_magic >= 13)
4122 {
4123 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4124 int wordsize = process->GetAddressByteSize();
4125 if (wordsize == 8)
4126 {
4127 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4128 }
4129 if (wordsize == 4)
4130 {
4131 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4132 }
4133 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4134 {
4135 uuid_t shared_cache_uuid;
4136 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4137 {
4138 uuid.SetBytes (shared_cache_uuid);
4139 }
4140 }
4141 }
4142 }
4143 return uuid;
4144}
4145
4146UUID
4147ObjectFileMachO::GetLLDBSharedCacheUUID ()
4148{
4149 UUID uuid;
4150#if defined (__APPLE__) && defined (__arm__)
4151 uint8_t *(*dyld_get_all_image_infos)(void);
4152 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4153 if (dyld_get_all_image_infos)
4154 {
4155 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4156 if (dyld_all_image_infos_address)
4157 {
Jason Molenda6ff916f2013-04-16 21:42:58 +00004158 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4159 if (*version >= 13)
Jason Molenda45c75502013-04-16 06:24:42 +00004160 {
Jason Molenda2ceae992013-04-16 22:56:17 +00004161 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 +00004162 uuid.SetBytes (sharedCacheUUID_address);
4163 }
4164 }
4165 }
4166#endif
4167 return uuid;
4168}
4169
4170
Chris Lattner24943d22010-06-08 16:52:24 +00004171//------------------------------------------------------------------
4172// PluginInterface protocol
4173//------------------------------------------------------------------
4174const char *
4175ObjectFileMachO::GetPluginName()
4176{
4177 return "ObjectFileMachO";
4178}
4179
4180const char *
4181ObjectFileMachO::GetShortPluginName()
4182{
4183 return GetPluginNameStatic();
4184}
4185
4186uint32_t
4187ObjectFileMachO::GetPluginVersion()
4188{
4189 return 1;
4190}
4191