blob: 7047395bdc79177bf2bbdf933ed014f35a268270 [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
Greg Clayton0e191602013-05-10 21:47:16 +0000382lldb_private::ConstString
Chris Lattner24943d22010-06-08 16:52:24 +0000383ObjectFileMachO::GetPluginNameStatic()
384{
Greg Clayton0e191602013-05-10 21:47:16 +0000385 static ConstString g_name("mach-o");
386 return g_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000387}
388
389const char *
390ObjectFileMachO::GetPluginDescriptionStatic()
391{
392 return "Mach-o object file reader (32 and 64 bit)";
393}
394
Chris Lattner24943d22010-06-08 16:52:24 +0000395ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000396ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
397 DataBufferSP& data_sp,
398 lldb::offset_t data_offset,
399 const FileSpec* file,
400 lldb::offset_t file_offset,
401 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000402{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000403 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000404 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000405 data_sp = file->MemoryMapFileContents(file_offset, length);
406 data_offset = 0;
407 }
408
409 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
410 {
411 // Update the data to contain the entire file if it doesn't already
412 if (data_sp->GetByteSize() < length)
413 {
414 data_sp = file->MemoryMapFileContents(file_offset, length);
415 data_offset = 0;
416 }
Greg Clayton102b2c22013-04-18 22:45:39 +0000417 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 +0000418 if (objfile_ap.get() && objfile_ap->ParseHeader())
419 return objfile_ap.release();
420 }
421 return NULL;
422}
423
Greg Claytonb5a8f142012-02-05 02:38:54 +0000424ObjectFile *
Jason Molenda9badb6c2013-03-06 23:19:17 +0000425ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
426 DataBufferSP& data_sp,
427 const ProcessSP &process_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000428 lldb::addr_t header_addr)
429{
430 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
431 {
Greg Clayton102b2c22013-04-18 22:45:39 +0000432 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000433 if (objfile_ap.get() && objfile_ap->ParseHeader())
434 return objfile_ap.release();
435 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000436 return NULL;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000437}
438
Greg Clayton36b877d2013-04-24 22:29:28 +0000439size_t
440ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
441 lldb::DataBufferSP& data_sp,
442 lldb::offset_t data_offset,
443 lldb::offset_t file_offset,
444 lldb::offset_t length,
445 lldb_private::ModuleSpecList &specs)
446{
447 const size_t initial_count = specs.GetSize();
448
449 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
450 {
451 DataExtractor data;
452 data.SetData(data_sp);
453 llvm::MachO::mach_header header;
454 if (ParseHeader (data, &data_offset, header))
455 {
456 if (header.sizeofcmds >= data_sp->GetByteSize())
457 {
458 data_sp = file.ReadFileContents(file_offset, header.sizeofcmds);
459 data_offset = 0;
460 }
461 if (data_sp)
462 {
463 ModuleSpec spec;
464 spec.GetFileSpec() = file;
465 spec.GetArchitecture().SetArchitecture(eArchTypeMachO,
466 header.cputype,
467 header.cpusubtype);
468 if (spec.GetArchitecture().IsValid())
469 {
470 GetUUID (header, data, data_offset, spec.GetUUID());
471 specs.Append(spec);
472 }
473 }
474 }
475 }
476 return specs.GetSize() - initial_count;
477}
478
479
Greg Claytonb5a8f142012-02-05 02:38:54 +0000480
481const ConstString &
482ObjectFileMachO::GetSegmentNameTEXT()
483{
484 static ConstString g_segment_name_TEXT ("__TEXT");
485 return g_segment_name_TEXT;
486}
487
488const ConstString &
489ObjectFileMachO::GetSegmentNameDATA()
490{
491 static ConstString g_segment_name_DATA ("__DATA");
492 return g_segment_name_DATA;
493}
494
495const ConstString &
496ObjectFileMachO::GetSegmentNameOBJC()
497{
498 static ConstString g_segment_name_OBJC ("__OBJC");
499 return g_segment_name_OBJC;
500}
501
502const ConstString &
503ObjectFileMachO::GetSegmentNameLINKEDIT()
504{
505 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
506 return g_section_name_LINKEDIT;
507}
508
509const ConstString &
510ObjectFileMachO::GetSectionNameEHFrame()
511{
512 static ConstString g_section_name_eh_frame ("__eh_frame");
513 return g_section_name_eh_frame;
514}
515
516
Chris Lattner24943d22010-06-08 16:52:24 +0000517
518static uint32_t
519MachHeaderSizeFromMagic(uint32_t magic)
520{
521 switch (magic)
522 {
Greg Clayton1674b122010-07-21 22:12:05 +0000523 case HeaderMagic32:
524 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000525 return sizeof(struct mach_header);
526
Greg Clayton1674b122010-07-21 22:12:05 +0000527 case HeaderMagic64:
528 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000529 return sizeof(struct mach_header_64);
530 break;
531
532 default:
533 break;
534 }
535 return 0;
536}
537
538
539bool
Jason Molenda9badb6c2013-03-06 23:19:17 +0000540ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
541 lldb::addr_t data_offset,
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000542 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000543{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000544 DataExtractor data;
545 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000546 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000547 uint32_t magic = data.GetU32(&offset);
548 return MachHeaderSizeFromMagic(magic) != 0;
549}
550
551
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000552ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
553 DataBufferSP& data_sp,
554 lldb::offset_t data_offset,
555 const FileSpec* file,
556 lldb::offset_t file_offset,
557 lldb::offset_t length) :
558 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Clayton46c9a352012-02-09 06:16:32 +0000559 m_mach_segments(),
560 m_mach_sections(),
561 m_entry_point_address(),
562 m_thread_context_offsets(),
563 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000564{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000565 ::memset (&m_header, 0, sizeof(m_header));
566 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000567}
568
Greg Clayton3508c382012-02-24 01:59:29 +0000569ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000570 lldb::DataBufferSP& header_data_sp,
571 const lldb::ProcessSP &process_sp,
572 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000573 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Clayton46c9a352012-02-09 06:16:32 +0000574 m_mach_segments(),
575 m_mach_sections(),
576 m_entry_point_address(),
577 m_thread_context_offsets(),
578 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000579{
580 ::memset (&m_header, 0, sizeof(m_header));
581 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
582}
Chris Lattner24943d22010-06-08 16:52:24 +0000583
584ObjectFileMachO::~ObjectFileMachO()
585{
586}
587
Greg Clayton36b877d2013-04-24 22:29:28 +0000588bool
589ObjectFileMachO::ParseHeader (DataExtractor &data,
590 lldb::offset_t *data_offset_ptr,
591 llvm::MachO::mach_header &header)
592{
593 data.SetByteOrder (lldb::endian::InlHostByteOrder());
594 // Leave magic in the original byte order
595 header.magic = data.GetU32(data_offset_ptr);
596 bool can_parse = false;
597 bool is_64_bit = false;
598 switch (header.magic)
599 {
600 case HeaderMagic32:
601 data.SetByteOrder (lldb::endian::InlHostByteOrder());
602 data.SetAddressByteSize(4);
603 can_parse = true;
604 break;
605
606 case HeaderMagic64:
607 data.SetByteOrder (lldb::endian::InlHostByteOrder());
608 data.SetAddressByteSize(8);
609 can_parse = true;
610 is_64_bit = true;
611 break;
612
613 case HeaderMagic32Swapped:
614 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
615 data.SetAddressByteSize(4);
616 can_parse = true;
617 break;
618
619 case HeaderMagic64Swapped:
620 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
621 data.SetAddressByteSize(8);
622 is_64_bit = true;
623 can_parse = true;
624 break;
625
626 default:
627 break;
628 }
629
630 if (can_parse)
631 {
632 data.GetU32(data_offset_ptr, &header.cputype, 6);
633 if (is_64_bit)
634 *data_offset_ptr += 4;
635 return true;
636 }
637 else
638 {
639 memset(&header, 0, sizeof(header));
640 }
641 return false;
642}
Chris Lattner24943d22010-06-08 16:52:24 +0000643
644bool
645ObjectFileMachO::ParseHeader ()
646{
Greg Clayton9482f052012-03-13 23:14:29 +0000647 ModuleSP module_sp(GetModule());
648 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000649 {
Greg Clayton9482f052012-03-13 23:14:29 +0000650 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
651 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000652 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000653 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000654 // Leave magic in the original byte order
655 m_header.magic = m_data.GetU32(&offset);
656 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000657 {
Greg Clayton9482f052012-03-13 23:14:29 +0000658 case HeaderMagic32:
659 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
660 m_data.SetAddressByteSize(4);
661 can_parse = true;
662 break;
663
664 case HeaderMagic64:
665 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
666 m_data.SetAddressByteSize(8);
667 can_parse = true;
668 break;
669
670 case HeaderMagic32Swapped:
671 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
672 m_data.SetAddressByteSize(4);
673 can_parse = true;
674 break;
675
676 case HeaderMagic64Swapped:
677 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
678 m_data.SetAddressByteSize(8);
679 can_parse = true;
680 break;
681
682 default:
683 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000684 }
Greg Clayton9482f052012-03-13 23:14:29 +0000685
686 if (can_parse)
687 {
688 m_data.GetU32(&offset, &m_header.cputype, 6);
689
690 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +0000691
Greg Clayton21a25432012-11-16 21:36:10 +0000692 // Check if the module has a required architecture
693 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000694 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000695 return false;
696
Greg Clayton9482f052012-03-13 23:14:29 +0000697 if (SetModulesArchitecture (mach_arch))
698 {
699 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
700 if (m_data.GetByteSize() < header_and_lc_size)
701 {
702 DataBufferSP data_sp;
703 ProcessSP process_sp (m_process_wp.lock());
704 if (process_sp)
705 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000706 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000707 }
708 else
709 {
710 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000711 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000712 if (data_sp->GetByteSize() != header_and_lc_size)
713 return false;
714 }
715 if (data_sp)
716 m_data.SetData (data_sp);
717 }
718 }
719 return true;
720 }
721 else
722 {
723 memset(&m_header, 0, sizeof(struct mach_header));
724 }
Chris Lattner24943d22010-06-08 16:52:24 +0000725 }
726 return false;
727}
728
729
730ByteOrder
731ObjectFileMachO::GetByteOrder () const
732{
Chris Lattner24943d22010-06-08 16:52:24 +0000733 return m_data.GetByteOrder ();
734}
735
Jim Ingham7508e732010-08-09 23:31:02 +0000736bool
737ObjectFileMachO::IsExecutable() const
738{
739 return m_header.filetype == HeaderFileTypeExecutable;
740}
Chris Lattner24943d22010-06-08 16:52:24 +0000741
Greg Clayton36da2aa2013-01-25 18:06:21 +0000742uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000743ObjectFileMachO::GetAddressByteSize () const
744{
Chris Lattner24943d22010-06-08 16:52:24 +0000745 return m_data.GetAddressByteSize ();
746}
747
Greg Claytonb3448432011-03-24 21:19:54 +0000748AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000749ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
750{
751 Symtab *symtab = GetSymtab();
752 if (symtab)
753 {
754 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
755 if (symbol)
756 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000757 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000758 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000759 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000760 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000761 {
Greg Clayton3508c382012-02-24 01:59:29 +0000762 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000763 switch (section_type)
764 {
765 case eSectionTypeInvalid: return eAddressClassUnknown;
766 case eSectionTypeCode:
767 if (m_header.cputype == llvm::MachO::CPUTypeARM)
768 {
769 // For ARM we have a bit in the n_desc field of the symbol
770 // that tells us ARM/Thumb which is bit 0x0008.
771 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
772 return eAddressClassCodeAlternateISA;
773 }
774 return eAddressClassCode;
775
776 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000777 case eSectionTypeData:
778 case eSectionTypeDataCString:
779 case eSectionTypeDataCStringPointers:
780 case eSectionTypeDataSymbolAddress:
781 case eSectionTypeData4:
782 case eSectionTypeData8:
783 case eSectionTypeData16:
784 case eSectionTypeDataPointers:
785 case eSectionTypeZeroFill:
786 case eSectionTypeDataObjCMessageRefs:
787 case eSectionTypeDataObjCCFStrings:
788 return eAddressClassData;
789 case eSectionTypeDebug:
790 case eSectionTypeDWARFDebugAbbrev:
791 case eSectionTypeDWARFDebugAranges:
792 case eSectionTypeDWARFDebugFrame:
793 case eSectionTypeDWARFDebugInfo:
794 case eSectionTypeDWARFDebugLine:
795 case eSectionTypeDWARFDebugLoc:
796 case eSectionTypeDWARFDebugMacInfo:
797 case eSectionTypeDWARFDebugPubNames:
798 case eSectionTypeDWARFDebugPubTypes:
799 case eSectionTypeDWARFDebugRanges:
800 case eSectionTypeDWARFDebugStr:
801 case eSectionTypeDWARFAppleNames:
802 case eSectionTypeDWARFAppleTypes:
803 case eSectionTypeDWARFAppleNamespaces:
804 case eSectionTypeDWARFAppleObjC:
805 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000806 case eSectionTypeEHFrame: return eAddressClassRuntime;
807 case eSectionTypeOther: return eAddressClassUnknown;
808 }
809 }
810 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000811
Greg Claytonb3448432011-03-24 21:19:54 +0000812 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000813 switch (symbol_type)
814 {
815 case eSymbolTypeAny: return eAddressClassUnknown;
816 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000817
Greg Claytonb1888f22011-03-19 01:12:21 +0000818 case eSymbolTypeCode:
819 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000820 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000821 if (m_header.cputype == llvm::MachO::CPUTypeARM)
822 {
823 // For ARM we have a bit in the n_desc field of the symbol
824 // that tells us ARM/Thumb which is bit 0x0008.
825 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
826 return eAddressClassCodeAlternateISA;
827 }
828 return eAddressClassCode;
829
830 case eSymbolTypeData: return eAddressClassData;
831 case eSymbolTypeRuntime: return eAddressClassRuntime;
832 case eSymbolTypeException: return eAddressClassRuntime;
833 case eSymbolTypeSourceFile: return eAddressClassDebug;
834 case eSymbolTypeHeaderFile: return eAddressClassDebug;
835 case eSymbolTypeObjectFile: return eAddressClassDebug;
836 case eSymbolTypeCommonBlock: return eAddressClassDebug;
837 case eSymbolTypeBlock: return eAddressClassDebug;
838 case eSymbolTypeLocal: return eAddressClassData;
839 case eSymbolTypeParam: return eAddressClassData;
840 case eSymbolTypeVariable: return eAddressClassData;
841 case eSymbolTypeVariableType: return eAddressClassDebug;
842 case eSymbolTypeLineEntry: return eAddressClassDebug;
843 case eSymbolTypeLineHeader: return eAddressClassDebug;
844 case eSymbolTypeScopeBegin: return eAddressClassDebug;
845 case eSymbolTypeScopeEnd: return eAddressClassDebug;
846 case eSymbolTypeAdditional: return eAddressClassUnknown;
847 case eSymbolTypeCompiler: return eAddressClassDebug;
848 case eSymbolTypeInstrumentation:return eAddressClassDebug;
849 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000850 case eSymbolTypeObjCClass: return eAddressClassRuntime;
851 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
852 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000853 }
854 }
855 }
856 return eAddressClassUnknown;
857}
Chris Lattner24943d22010-06-08 16:52:24 +0000858
859Symtab *
860ObjectFileMachO::GetSymtab()
861{
Greg Clayton9482f052012-03-13 23:14:29 +0000862 ModuleSP module_sp(GetModule());
863 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000864 {
Greg Clayton9482f052012-03-13 23:14:29 +0000865 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
866 if (m_symtab_ap.get() == NULL)
867 {
868 m_symtab_ap.reset(new Symtab(this));
869 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
870 ParseSymtab (true);
871 m_symtab_ap->Finalize ();
872 }
Chris Lattner24943d22010-06-08 16:52:24 +0000873 }
874 return m_symtab_ap.get();
875}
876
877
878SectionList *
879ObjectFileMachO::GetSectionList()
880{
Greg Clayton9482f052012-03-13 23:14:29 +0000881 ModuleSP module_sp(GetModule());
882 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000883 {
Greg Clayton9482f052012-03-13 23:14:29 +0000884 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
885 if (m_sections_ap.get() == NULL)
886 {
887 m_sections_ap.reset(new SectionList());
888 ParseSections();
889 }
Chris Lattner24943d22010-06-08 16:52:24 +0000890 }
891 return m_sections_ap.get();
892}
893
894
895size_t
896ObjectFileMachO::ParseSections ()
897{
898 lldb::user_id_t segID = 0;
899 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000900 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000901 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000902 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000903 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000904 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000905 // First look up any LC_ENCRYPTION_INFO load commands
906 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
907 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000908 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000909 for (i=0; i<m_header.ncmds; ++i)
910 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000911 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000912 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000913 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000914
Greg Clayton54e33712012-05-25 18:09:55 +0000915 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000916 {
Greg Clayton54e33712012-05-25 18:09:55 +0000917 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
918 {
919 if (encryption_cmd.cryptid != 0)
920 {
921 EncryptedFileRanges::Entry entry;
922 entry.SetRangeBase(encryption_cmd.cryptoff);
923 entry.SetByteSize(encryption_cmd.cryptsize);
924 encrypted_file_ranges.Append(entry);
925 }
926 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000927 }
Greg Clayton54e33712012-05-25 18:09:55 +0000928 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000929 }
930
931 offset = MachHeaderSizeFromMagic(m_header.magic);
932
Greg Clayton54e33712012-05-25 18:09:55 +0000933 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000934 for (i=0; i<m_header.ncmds; ++i)
935 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000936 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000937 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
938 break;
939
Greg Clayton1674b122010-07-21 22:12:05 +0000940 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000941 {
942 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
943 {
944 load_cmd.vmaddr = m_data.GetAddress(&offset);
945 load_cmd.vmsize = m_data.GetAddress(&offset);
946 load_cmd.fileoff = m_data.GetAddress(&offset);
947 load_cmd.filesize = m_data.GetAddress(&offset);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000948 if (m_length != 0 && load_cmd.filesize != 0)
949 {
Greg Claytonbb759862013-04-16 16:51:19 +0000950 if (load_cmd.fileoff > m_length)
951 {
952 // We have a load command that says it extends past the end of hte file. This is likely
953 // a corrupt file. We don't have any way to return an error condition here (this method
954 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
955 // is null out the SectionList vector and if a process has been set up, dump a message
956 // to stdout. The most common case here is core file debugging with a truncated file.
957 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
958 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 ")",
959 i,
960 lc_segment_name,
961 load_cmd.fileoff,
962 m_length);
963 m_sections_ap->Clear();
964 return 0;
965 }
966
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000967 if (load_cmd.fileoff + load_cmd.filesize > m_length)
968 {
969 // We have a load command that says it extends past the end of hte file. This is likely
970 // a corrupt file. We don't have any way to return an error condition here (this method
971 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
972 // is null out the SectionList vector and if a process has been set up, dump a message
Greg Claytonbb759862013-04-16 16:51:19 +0000973 // to stdout. The most common case here is core file debugging with a truncated file.
974 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
975 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 ")",
976 i,
977 lc_segment_name,
978 load_cmd.fileoff + load_cmd.filesize,
979 m_length);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000980 m_sections_ap->Clear();
981 return 0;
982 }
983 }
Chris Lattner24943d22010-06-08 16:52:24 +0000984 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
985 {
Jason Molenda9badb6c2013-03-06 23:19:17 +0000986
Greg Clayton68ca8232011-01-25 02:58:48 +0000987 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
988
Chris Lattner24943d22010-06-08 16:52:24 +0000989 // Keep a list of mach segments around in case we need to
990 // get at data that isn't stored in the abstracted Sections.
991 m_mach_segments.push_back (load_cmd);
992
Greg Clayton36da2aa2013-01-25 18:06:21 +0000993 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 +0000994 // Use a segment ID of the segment index shifted left by 8 so they
995 // never conflict with any of the sections.
996 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +0000997 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +0000998 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000999 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +00001000 ++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
1001 segment_name, // Name of this section
1002 eSectionTypeContainer, // This section is a container of other sections.
1003 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1004 load_cmd.vmsize, // VM size in bytes of this section
1005 load_cmd.fileoff, // Offset to the data for this section in the file
1006 load_cmd.filesize, // Size in bytes of this section as found in the the file
1007 load_cmd.flags)); // Flags for this section
1008
Greg Clayton68ca8232011-01-25 02:58:48 +00001009 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001010 m_sections_ap->AddSection(segment_sp);
1011 }
1012
1013 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +00001014 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +00001015 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +00001016 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +00001017 // mach sections yet...
1018 if (m_mach_sections.empty())
1019 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +00001020 uint32_t segment_sect_idx;
1021 const lldb::user_id_t first_segment_sectID = sectID + 1;
1022
1023
Greg Clayton1674b122010-07-21 22:12:05 +00001024 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +00001025 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
1026 {
1027 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
1028 break;
1029 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1030 break;
1031 sect64.addr = m_data.GetAddress(&offset);
1032 sect64.size = m_data.GetAddress(&offset);
1033
1034 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1035 break;
1036
1037 // Keep a list of mach sections around in case we need to
1038 // get at data that isn't stored in the abstracted Sections.
1039 m_mach_sections.push_back (sect64);
1040
1041 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1042 if (!segment_name)
1043 {
1044 // We have a segment with no name so we need to conjure up
1045 // segments that correspond to the section's segname if there
1046 // isn't already such a section. If there is such a section,
1047 // we resize the section so that it spans all sections.
1048 // We also mark these sections as fake so address matches don't
1049 // hit if they land in the gaps between the child sections.
1050 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1051 segment_sp = m_sections_ap->FindSectionByName (segment_name);
1052 if (segment_sp.get())
1053 {
1054 Section *segment = segment_sp.get();
1055 // Grow the section size as needed.
1056 const lldb::addr_t sect64_min_addr = sect64.addr;
1057 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1058 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1059 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1060 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1061 if (sect64_min_addr >= curr_seg_min_addr)
1062 {
1063 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1064 // Only grow the section size if needed
1065 if (new_seg_byte_size > curr_seg_byte_size)
1066 segment->SetByteSize (new_seg_byte_size);
1067 }
1068 else
1069 {
1070 // We need to change the base address of the segment and
1071 // adjust the child section offsets for all existing children.
1072 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1073 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +00001074 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +00001075 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1076 }
Greg Clayton661825b2010-06-28 23:51:11 +00001077
1078 // Grow the section size as needed.
1079 if (sect64.offset)
1080 {
1081 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1082 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1083
1084 const lldb::addr_t section_min_file_offset = sect64.offset;
1085 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1086 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1087 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1088 segment->SetFileOffset (new_file_offset);
1089 segment->SetFileSize (new_file_size);
1090 }
Chris Lattner24943d22010-06-08 16:52:24 +00001091 }
1092 else
1093 {
1094 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +00001095 segment_sp.reset(new Section (segment_sp, // Parent section
1096 module_sp, // Module to which this section belongs
1097 ++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
1098 segment_name, // Name of this section
1099 eSectionTypeContainer, // This section is a container of other sections.
1100 sect64.addr, // File VM address == addresses as they are found in the object file
1101 sect64.size, // VM size in bytes of this section
1102 sect64.offset, // Offset to the data for this section in the file
1103 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1104 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +00001105 segment_sp->SetIsFake(true);
1106 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +00001107 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001108 }
1109 }
1110 assert (segment_sp.get());
1111
Greg Clayton1674b122010-07-21 22:12:05 +00001112 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +00001113 static ConstString g_sect_name_objc_data ("__objc_data");
1114 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1115 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1116 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1117 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1118 static ConstString g_sect_name_objc_const ("__objc_const");
1119 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1120 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001121
1122 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1123 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1124 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1125 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1126 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1127 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1128 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1129 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1130 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1131 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1132 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +00001133 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1134 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +00001135 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +00001136 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001137 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +00001138 static ConstString g_sect_name_DATA ("__DATA");
1139 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001140
Chris Lattner24943d22010-06-08 16:52:24 +00001141 SectionType sect_type = eSectionTypeOther;
1142
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001143 if (section_name == g_sect_name_dwarf_debug_abbrev)
1144 sect_type = eSectionTypeDWARFDebugAbbrev;
1145 else if (section_name == g_sect_name_dwarf_debug_aranges)
1146 sect_type = eSectionTypeDWARFDebugAranges;
1147 else if (section_name == g_sect_name_dwarf_debug_frame)
1148 sect_type = eSectionTypeDWARFDebugFrame;
1149 else if (section_name == g_sect_name_dwarf_debug_info)
1150 sect_type = eSectionTypeDWARFDebugInfo;
1151 else if (section_name == g_sect_name_dwarf_debug_line)
1152 sect_type = eSectionTypeDWARFDebugLine;
1153 else if (section_name == g_sect_name_dwarf_debug_loc)
1154 sect_type = eSectionTypeDWARFDebugLoc;
1155 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1156 sect_type = eSectionTypeDWARFDebugMacInfo;
1157 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1158 sect_type = eSectionTypeDWARFDebugPubNames;
1159 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1160 sect_type = eSectionTypeDWARFDebugPubTypes;
1161 else if (section_name == g_sect_name_dwarf_debug_ranges)
1162 sect_type = eSectionTypeDWARFDebugRanges;
1163 else if (section_name == g_sect_name_dwarf_debug_str)
1164 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001165 else if (section_name == g_sect_name_dwarf_apple_names)
1166 sect_type = eSectionTypeDWARFAppleNames;
1167 else if (section_name == g_sect_name_dwarf_apple_types)
1168 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001169 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1170 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001171 else if (section_name == g_sect_name_dwarf_apple_objc)
1172 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001173 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001174 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001175 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001176 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001177 else if (section_name == g_sect_name_eh_frame)
1178 sect_type = eSectionTypeEHFrame;
1179 else if (section_name == g_sect_name_cfstring)
1180 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001181 else if (section_name == g_sect_name_objc_data ||
1182 section_name == g_sect_name_objc_classrefs ||
1183 section_name == g_sect_name_objc_superrefs ||
1184 section_name == g_sect_name_objc_const ||
1185 section_name == g_sect_name_objc_classlist)
1186 {
1187 sect_type = eSectionTypeDataPointers;
1188 }
Chris Lattner24943d22010-06-08 16:52:24 +00001189
1190 if (sect_type == eSectionTypeOther)
1191 {
1192 switch (mach_sect_type)
1193 {
1194 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001195 case SectionTypeRegular:
1196 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001197 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001198 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001199 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001200 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001201 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001202 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001203 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1204 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1205 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1206 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1207 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1208 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1209 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1210 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1211 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1212 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1213 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1214 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1215 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1216 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1217 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1218 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001219 default: break;
1220 }
1221 }
1222
Greg Clayton3508c382012-02-24 01:59:29 +00001223 SectionSP section_sp(new Section (segment_sp,
1224 module_sp,
1225 ++sectID,
1226 section_name,
1227 sect_type,
1228 sect64.addr - segment_sp->GetFileAddress(),
1229 sect64.size,
1230 sect64.offset,
1231 sect64.offset == 0 ? 0 : sect64.size,
1232 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001233 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001234
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001235 bool section_is_encrypted = false;
1236 if (!segment_is_encrypted && load_cmd.filesize != 0)
1237 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001238
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001239 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001240 segment_sp->GetChildren().AddSection(section_sp);
1241
1242 if (segment_sp->IsFake())
1243 {
1244 segment_sp.reset();
1245 segment_name.Clear();
1246 }
1247 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001248 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001249 {
1250 if (first_segment_sectID <= sectID)
1251 {
1252 lldb::user_id_t sect_uid;
1253 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1254 {
1255 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1256 SectionSP next_section_sp;
1257 if (sect_uid + 1 <= sectID)
1258 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1259
1260 if (curr_section_sp.get())
1261 {
1262 if (curr_section_sp->GetByteSize() == 0)
1263 {
1264 if (next_section_sp.get() != NULL)
1265 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1266 else
1267 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1268 }
1269 }
1270 }
1271 }
1272 }
1273 }
1274 }
1275 }
Greg Clayton1674b122010-07-21 22:12:05 +00001276 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001277 {
1278 m_dysymtab.cmd = load_cmd.cmd;
1279 m_dysymtab.cmdsize = load_cmd.cmdsize;
1280 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1281 }
1282
1283 offset = load_cmd_offset + load_cmd.cmdsize;
1284 }
1285// if (dump_sections)
1286// {
1287// StreamFile s(stdout);
1288// m_sections_ap->Dump(&s, true);
1289// }
1290 return sectID; // Return the number of sections we registered with the module
1291}
1292
1293class MachSymtabSectionInfo
1294{
1295public:
1296
1297 MachSymtabSectionInfo (SectionList *section_list) :
1298 m_section_list (section_list),
1299 m_section_infos()
1300 {
1301 // Get the number of sections down to a depth of 1 to include
1302 // all segments and their sections, but no other sections that
1303 // may be added for debug map or
1304 m_section_infos.resize(section_list->GetNumSections(1));
1305 }
1306
1307
Greg Clayton3508c382012-02-24 01:59:29 +00001308 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001309 GetSection (uint8_t n_sect, addr_t file_addr)
1310 {
1311 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001312 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001313 if (n_sect < m_section_infos.size())
1314 {
Greg Clayton3508c382012-02-24 01:59:29 +00001315 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001316 {
Greg Clayton3508c382012-02-24 01:59:29 +00001317 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1318 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001319 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001320 {
Greg Clayton3508c382012-02-24 01:59:29 +00001321 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1322 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001323 }
1324 else
1325 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001326 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001327 }
Chris Lattner24943d22010-06-08 16:52:24 +00001328 }
1329 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001330 {
1331 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001332 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001333 }
1334 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1335 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1336 {
1337 // Symbol is in section with zero size, but has the same start
1338 // address as the section. This can happen with linker symbols
1339 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001340 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001341 }
Chris Lattner24943d22010-06-08 16:52:24 +00001342 }
Greg Clayton3508c382012-02-24 01:59:29 +00001343 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001344 }
1345
1346protected:
1347 struct SectionInfo
1348 {
1349 SectionInfo () :
1350 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001351 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001352 {
1353 }
1354
1355 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001356 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001357 };
1358 SectionList *m_section_list;
1359 std::vector<SectionInfo> m_section_infos;
1360};
1361
Chris Lattner24943d22010-06-08 16:52:24 +00001362size_t
1363ObjectFileMachO::ParseSymtab (bool minimize)
1364{
1365 Timer scoped_timer(__PRETTY_FUNCTION__,
1366 "ObjectFileMachO::ParseSymtab () module = %s",
1367 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001368 ModuleSP module_sp (GetModule());
1369 if (!module_sp)
1370 return 0;
1371
1372 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1373 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1374 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1375 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001376 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001377 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001378
Greg Clayton952e9dc2013-03-27 23:08:40 +00001379 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001380
Chris Lattner24943d22010-06-08 16:52:24 +00001381 for (i=0; i<m_header.ncmds; ++i)
1382 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001383 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001384 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001385 struct load_command lc;
1386 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001387 break;
1388 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001389 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001390 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001391 case LoadCommandSymtab:
1392 symtab_load_command.cmd = lc.cmd;
1393 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001394 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001395 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1396 return 0;
1397 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001398 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001399 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001400 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001401 return 0;
1402 }
1403
1404 if (symtab_load_command.stroff == 0)
1405 {
1406 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001407 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001408 return 0;
1409 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001410
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001411 if (symtab_load_command.nsyms == 0)
1412 {
1413 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001414 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001415 return 0;
1416 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001417
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001418 if (symtab_load_command.strsize == 0)
1419 {
1420 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001421 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001422 return 0;
1423 }
1424 break;
1425
1426 case LoadCommandFunctionStarts:
1427 function_starts_load_command.cmd = lc.cmd;
1428 function_starts_load_command.cmdsize = lc.cmdsize;
1429 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1430 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1431 break;
1432
1433 default:
1434 break;
1435 }
1436 offset = cmd_offset + lc.cmdsize;
1437 }
1438
1439 if (symtab_load_command.cmd)
1440 {
1441 Symtab *symtab = m_symtab_ap.get();
1442 SectionList *section_list = GetSectionList();
1443 if (section_list == NULL)
1444 return 0;
1445
1446 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001447 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001448
Greg Clayton36da2aa2013-01-25 18:06:21 +00001449 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1450 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001451 bool bit_width_32 = addr_byte_size == 4;
1452 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1453
Greg Clayton36da2aa2013-01-25 18:06:21 +00001454 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1455 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1456 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001457 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001458
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001459 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1460 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001461 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1462 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001463 {
Greg Claytondd29b972012-05-18 23:20:01 +00001464 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001465 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1466 // Reading mach file from memory in a process or core file...
1467
1468 if (linkedit_section_sp)
1469 {
1470 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1471 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1472 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001473 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001474
1475 bool data_was_read = false;
1476
1477#if defined (__APPLE__) && defined (__arm__)
1478 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001479 {
Greg Clayton29021d32012-04-18 05:19:20 +00001480 // This mach-o memory file is in the dyld shared cache. If this
1481 // program is not remote and this is iOS, then this process will
1482 // share the same shared cache as the process we are debugging and
1483 // we can read the entire __LINKEDIT from the address space in this
1484 // process. This is a needed optimization that is used for local iOS
1485 // debugging only since all shared libraries in the shared cache do
1486 // not have corresponding files that exist in the file system of the
1487 // device. They have been combined into a single file. This means we
1488 // always have to load these files from memory. All of the symbol and
1489 // string tables from all of the __LINKEDIT sections from the shared
1490 // libraries in the shared cache have been merged into a single large
1491 // symbol and string table. Reading all of this symbol and string table
1492 // data across can slow down debug launch times, so we optimize this by
1493 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda45c75502013-04-16 06:24:42 +00001494
1495 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1496 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1497 bool use_lldb_cache = true;
1498 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1499 {
1500 use_lldb_cache = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001501 ModuleSP module_sp (GetModule());
1502 if (module_sp)
1503 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1504
Jason Molenda45c75502013-04-16 06:24:42 +00001505 }
1506
Greg Clayton29021d32012-04-18 05:19:20 +00001507 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda45c75502013-04-16 06:24:42 +00001508 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton29021d32012-04-18 05:19:20 +00001509 {
1510 data_was_read = true;
1511 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001512 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001513 if (function_starts_load_command.cmd)
1514 {
1515 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1516 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1517 }
1518 }
1519 }
1520#endif
1521
1522 if (!data_was_read)
1523 {
1524 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1525 if (nlist_data_sp)
1526 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001527 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1528 //if (strtab_data_sp)
1529 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001530 if (m_dysymtab.nindirectsyms != 0)
1531 {
1532 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1533 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1534 if (indirect_syms_data_sp)
1535 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1536 }
Greg Clayton29021d32012-04-18 05:19:20 +00001537 if (function_starts_load_command.cmd)
1538 {
1539 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1540 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1541 if (func_start_data_sp)
1542 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1543 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001544 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001545 }
1546 }
1547 else
1548 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001549 nlist_data.SetData (m_data,
1550 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001551 nlist_data_byte_size);
1552 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001553 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001554 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001555 if (m_dysymtab.nindirectsyms != 0)
1556 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001557 indirect_symbol_index_data.SetData (m_data,
1558 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001559 m_dysymtab.nindirectsyms * 4);
1560 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001561 if (function_starts_load_command.cmd)
1562 {
1563 function_starts_data.SetData (m_data,
1564 function_starts_load_command.dataoff,
1565 function_starts_load_command.datasize);
1566 }
1567 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001568
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001569 if (nlist_data.GetByteSize() == 0)
1570 {
1571 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001572 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001573 return 0;
1574 }
1575
1576
Greg Clayton3a5dc012012-05-25 17:04:00 +00001577 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1578 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001579 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001580 if (process)
1581 {
1582 if (strtab_addr == LLDB_INVALID_ADDRESS)
1583 {
1584 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001585 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001586 return 0;
1587 }
1588 }
1589 else
Greg Claytondd29b972012-05-18 23:20:01 +00001590 {
1591 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001592 module_sp->LogMessage(log, "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001593 return 0;
1594 }
1595 }
Greg Claytondd29b972012-05-18 23:20:01 +00001596
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001597 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1598 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1599 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1600 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1601 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1602 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1603 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1604 SectionSP eh_frame_section_sp;
1605 if (text_section_sp.get())
1606 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1607 else
1608 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1609
Greg Claytond2653c22012-03-14 01:53:24 +00001610 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001611
1612 // lldb works best if it knows the start addresss of all functions in a module.
1613 // Linker symbols or debug info are normally the best source of information for start addr / size but
1614 // they may be stripped in a released binary.
Jason Molenda2120aef2013-04-16 00:18:44 +00001615 // Two additional sources of information exist in Mach-O binaries:
Jason Molendad7938392013-03-21 03:36:01 +00001616 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1617 // binary, relative to the text section.
1618 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1619 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1620 // Binaries built to run on older releases may need to use eh_frame information.
1621
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001622 if (text_section_sp && function_starts_data.GetByteSize())
1623 {
1624 FunctionStarts::Entry function_start_entry;
1625 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001626 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001627 function_start_entry.addr = text_section_sp->GetFileAddress();
1628 uint64_t delta;
1629 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1630 {
1631 // Now append the current entry
1632 function_start_entry.addr += delta;
1633 function_starts.Append(function_start_entry);
1634 }
Jason Molenda2120aef2013-04-16 00:18:44 +00001635 }
Jason Molendad7938392013-03-21 03:36:01 +00001636 else
1637 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001638 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1639 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1640 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1641 // the module.
1642 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001643 {
1644 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1645 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1646 eh_frame.GetFunctionAddressAndSizeVector (functions);
1647 addr_t text_base_addr = text_section_sp->GetFileAddress();
1648 size_t count = functions.GetSize();
1649 for (size_t i = 0; i < count; ++i)
1650 {
1651 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1652 if (func)
1653 {
1654 FunctionStarts::Entry function_start_entry;
1655 function_start_entry.addr = func->base - text_base_addr;
1656 function_starts.Append(function_start_entry);
1657 }
1658 }
1659 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001660 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001661
Greg Clayton36da2aa2013-01-25 18:06:21 +00001662 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001663
Greg Clayton36da2aa2013-01-25 18:06:21 +00001664 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 +00001665
Greg Clayton36da2aa2013-01-25 18:06:21 +00001666 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001667
1668 uint32_t N_SO_index = UINT32_MAX;
1669
1670 MachSymtabSectionInfo section_info (section_list);
1671 std::vector<uint32_t> N_FUN_indexes;
1672 std::vector<uint32_t> N_NSYM_indexes;
1673 std::vector<uint32_t> N_INCL_indexes;
1674 std::vector<uint32_t> N_BRAC_indexes;
1675 std::vector<uint32_t> N_COMM_indexes;
1676 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1677 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1678 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1679 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1680 // Any symbols that get merged into another will get an entry
1681 // in this map so we know
1682 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1683 uint32_t nlist_idx = 0;
1684 Symbol *symbol_ptr = NULL;
1685
1686 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001687 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001688 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001689 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001690 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001691
Jason Molendab62abd52012-06-21 01:51:02 +00001692#if defined (__APPLE__) && defined (__arm__)
1693
1694 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1695 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1696 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1697 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1698 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1699 // nlist parser to ignore all LOCAL symbols.
1700
1701 if (m_header.flags & 0x80000000u)
1702 {
1703 // Before we can start mapping the DSC, we need to make certain the target process is actually
1704 // using the cache we can find.
1705
Jason Molendab62abd52012-06-21 01:51:02 +00001706 // Next we need to determine the correct path for the dyld shared cache.
1707
1708 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1709 char dsc_path[PATH_MAX];
1710
1711 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001712 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1713 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001714 header_arch.GetArchitectureName());
1715
1716 FileSpec dsc_filespec(dsc_path, false);
1717
1718 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda2120aef2013-04-16 00:18:44 +00001719 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001720 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001721 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1722 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1723 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001724 uint32_t imagesOffset;
1725 uint32_t imagesCount;
1726 uint64_t dyldBaseAddress;
1727 uint64_t codeSignatureOffset;
1728 uint64_t codeSignatureSize;
1729 uint64_t slideInfoOffset;
1730 uint64_t slideInfoSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001731 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1732 uint64_t localSymbolsSize; // size of local symbols information
1733 };
1734 struct lldb_copy_dyld_cache_header_v1
1735 {
1736 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1737 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1738 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1739 uint32_t imagesOffset;
1740 uint32_t imagesCount;
1741 uint64_t dyldBaseAddress;
1742 uint64_t codeSignatureOffset;
1743 uint64_t codeSignatureSize;
1744 uint64_t slideInfoOffset;
1745 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001746 uint64_t localSymbolsOffset;
1747 uint64_t localSymbolsSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001748 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001749 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001750
Jason Molenda2120aef2013-04-16 00:18:44 +00001751 struct lldb_copy_dyld_cache_mapping_info
1752 {
1753 uint64_t address;
1754 uint64_t size;
1755 uint64_t fileOffset;
1756 uint32_t maxProt;
1757 uint32_t initProt;
1758 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001759
Greg Claytonab77dcb2012-09-05 22:30:51 +00001760 struct lldb_copy_dyld_cache_local_symbols_info
1761 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001762 uint32_t nlistOffset;
1763 uint32_t nlistCount;
1764 uint32_t stringsOffset;
1765 uint32_t stringsSize;
1766 uint32_t entriesOffset;
1767 uint32_t entriesCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001768 };
1769 struct lldb_copy_dyld_cache_local_symbols_entry
1770 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001771 uint32_t dylibOffset;
1772 uint32_t nlistStartIndex;
1773 uint32_t nlistCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001774 };
Jason Molendab62abd52012-06-21 01:51:02 +00001775
Jason Molendafd3b35d2012-06-22 03:28:35 +00001776 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1777 The dyld_cache_local_symbols_info structure gives us three things:
1778 1. The start and count of the nlist records in the dyld_shared_cache file
1779 2. The start and size of the strings for these nlist records
1780 3. The start and count of dyld_cache_local_symbols_entry entries
1781
1782 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1783 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001784 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001785 and the count of how many nlist records there are for this dylib/framework.
1786 */
1787
Jason Molendab62abd52012-06-21 01:51:02 +00001788 // Process the dsc header to find the unmapped symbols
1789 //
1790 // Save some VM space, do not map the entire cache in one shot.
1791
Jason Molenda6bcabae2013-03-06 23:17:36 +00001792 DataBufferSP dsc_data_sp;
1793 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1794
1795 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001796 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001797 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001798
Jason Molenda6bcabae2013-03-06 23:17:36 +00001799 char version_str[17];
1800 int version = -1;
1801 lldb::offset_t offset = 0;
1802 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1803 version_str[16] = '\0';
1804 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1805 {
1806 int v;
1807 if (::sscanf (version_str + 6, "%d", &v) == 1)
1808 {
1809 version = v;
1810 }
1811 }
1812
Jason Molenda45c75502013-04-16 06:24:42 +00001813 UUID dsc_uuid;
1814 if (version >= 1)
1815 {
1816 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1817 uint8_t uuid_bytes[sizeof (uuid_t)];
1818 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1819 dsc_uuid.SetBytes (uuid_bytes);
1820 }
1821
1822 bool uuid_match = true;
1823 if (dsc_uuid.IsValid() && process)
1824 {
1825 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1826
1827 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1828 {
1829 // The on-disk dyld_shared_cache file is not the same as the one in this
1830 // process' memory, don't use it.
1831 uuid_match = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001832 ModuleSP module_sp (GetModule());
1833 if (module_sp)
1834 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 +00001835 }
1836 }
1837
Jason Molenda9badb6c2013-03-06 23:19:17 +00001838 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001839
Jason Molendab62abd52012-06-21 01:51:02 +00001840 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1841
1842 // If the mappingOffset points to a location inside the header, we've
1843 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda45c75502013-04-16 06:24:42 +00001844 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendab62abd52012-06-21 01:51:02 +00001845 {
1846
Jason Molenda6bcabae2013-03-06 23:17:36 +00001847 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1848 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1849 offset = 0;
1850
1851 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1852 // in the shared library cache need to be adjusted by an offset to match up with the
1853 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1854 // recorded in mapping_offset_value.
1855 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1856
1857 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001858 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1859 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1860
Jason Molenda9badb6c2013-03-06 23:19:17 +00001861 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001862 {
1863 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001864 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001865 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001866 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001867
1868 offset = 0;
1869
1870 // Read the local_symbols_infos struct in one shot
1871 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1872 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1873
Jason Molendab62abd52012-06-21 01:51:02 +00001874 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1875
Jason Molenda6bcabae2013-03-06 23:17:36 +00001876 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001877
1878 offset = local_symbols_info.entriesOffset;
1879 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1880 {
1881 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1882 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1883 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1884 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1885
Jason Molenda9badb6c2013-03-06 23:19:17 +00001886 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001887 {
1888 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1889
1890 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1891 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1892 num_syms = symtab->GetNumSymbols();
1893
1894 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1895 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1896
Jason Molenda9badb6c2013-03-06 23:19:17 +00001897 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001898 {
1899 /////////////////////////////
1900 {
1901 struct nlist_64 nlist;
1902 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1903 break;
1904
1905 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1906 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1907 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1908 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1909 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1910
1911 SymbolType type = eSymbolTypeInvalid;
1912 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1913
1914 if (symbol_name == NULL)
1915 {
1916 // No symbol should be NULL, even the symbols with no
1917 // string values should have an offset zero which points
1918 // to an empty C-string
1919 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00001920 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Jason Molendab62abd52012-06-21 01:51:02 +00001921 entry_index,
1922 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00001923 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendab62abd52012-06-21 01:51:02 +00001924 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,
Greg Clayton97a19b22013-04-29 17:25:54 +00002656 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002657 nlist_idx,
2658 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00002659 module_sp->GetFileSpec().GetPath().c_str());
Greg Claytondd29b972012-05-18 23:20:01 +00002660 continue;
2661 }
2662 if (symbol_name[0] == '\0')
2663 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002664 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002665 else
2666 {
2667 const addr_t str_addr = strtab_addr + nlist.n_strx;
2668 Error str_error;
2669 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2670 symbol_name = memory_symbol_name.c_str();
2671 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002672 const char *symbol_name_non_abi_mangled = NULL;
2673
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002674 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002675 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002676 bool add_nlist = true;
2677 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002678 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002679
2680 assert (sym_idx < num_syms);
2681
2682 sym[sym_idx].SetDebug (is_debug);
2683
2684 if (is_debug)
2685 {
2686 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002687 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002688 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002689 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2690 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002691
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002692 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2693 // have the same address, but we want to ensure that we always find only the real symbol,
2694 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2695 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2696 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2697 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002698
2699 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002700 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2701 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2702 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2703 add_nlist = false;
2704 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002705 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002706 sym[sym_idx].SetExternal(true);
2707 if (nlist.n_value != 0)
2708 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2709 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002710 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002711 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002712
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002713 case StabFunctionName:
2714 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2715 type = eSymbolTypeCompiler;
2716 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002717
Jason Molenda9badb6c2013-03-06 23:19:17 +00002718 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002719 // N_FUN -- procedure: name,,n_sect,linenumber,address
2720 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002721 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002722 type = eSymbolTypeCode;
2723 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002724
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002725 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2726 // We use the current number of symbols in the symbol table in lieu of
2727 // using nlist_idx in case we ever start trimming entries out
2728 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002729 }
2730 else
2731 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002732 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002733
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002734 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002735 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002736 // Copy the size of the function into the original STAB entry so we don't have
2737 // to hunt for it later
2738 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2739 N_FUN_indexes.pop_back();
2740 // We don't really need the end function STAB as it contains the size which
2741 // we already placed with the original symbol, so don't add it if we want a
2742 // minimal symbol table
2743 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002744 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002745 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002746 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002747 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002748
Jason Molenda9badb6c2013-03-06 23:19:17 +00002749 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002750 // N_STSYM -- static symbol: name,,n_sect,type,address
2751 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2752 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2753 type = eSymbolTypeData;
2754 break;
2755
2756 case StabLocalCommon:
2757 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2758 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2759 type = eSymbolTypeCommonBlock;
2760 break;
2761
2762 case StabBeginSymbol:
2763 // N_BNSYM
2764 // We use the current number of symbols in the symbol table in lieu of
2765 // using nlist_idx in case we ever start trimming entries out
2766 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002767 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002768 // Skip these if we want minimal symbol tables
2769 add_nlist = false;
2770 }
2771 else
2772 {
2773 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2774 N_NSYM_indexes.push_back(sym_idx);
2775 type = eSymbolTypeScopeBegin;
2776 }
2777 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002778
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002779 case StabEndSymbol:
2780 // N_ENSYM
2781 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2782 // so that we can always skip the entire symbol if we need to navigate
2783 // more quickly at the source level when parsing STABS
2784 if (minimize)
2785 {
2786 // Skip these if we want minimal symbol tables
2787 add_nlist = false;
2788 }
2789 else
2790 {
2791 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002792 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002793 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2794 symbol_ptr->SetByteSize(sym_idx + 1);
2795 symbol_ptr->SetSizeIsSibling(true);
2796 N_NSYM_indexes.pop_back();
2797 }
2798 type = eSymbolTypeScopeEnd;
2799 }
2800 break;
2801
2802
2803 case StabSourceFileOptions:
2804 // N_OPT - emitted with gcc2_compiled and in gcc source
2805 type = eSymbolTypeCompiler;
2806 break;
2807
2808 case StabRegisterSymbol:
2809 // N_RSYM - register sym: name,,NO_SECT,type,register
2810 type = eSymbolTypeVariable;
2811 break;
2812
2813 case StabSourceLine:
2814 // N_SLINE - src line: 0,,n_sect,linenumber,address
2815 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2816 type = eSymbolTypeLineEntry;
2817 break;
2818
2819 case StabStructureType:
2820 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2821 type = eSymbolTypeVariableType;
2822 break;
2823
2824 case StabSourceFileName:
2825 // N_SO - source file name
2826 type = eSymbolTypeSourceFile;
2827 if (symbol_name == NULL)
2828 {
2829 if (minimize)
2830 add_nlist = false;
2831 if (N_SO_index != UINT32_MAX)
2832 {
2833 // Set the size of the N_SO to the terminating index of this N_SO
2834 // so that we can always skip the entire N_SO if we need to navigate
2835 // more quickly at the source level when parsing STABS
2836 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2837 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2838 symbol_ptr->SetSizeIsSibling(true);
2839 }
2840 N_NSYM_indexes.clear();
2841 N_INCL_indexes.clear();
2842 N_BRAC_indexes.clear();
2843 N_COMM_indexes.clear();
2844 N_FUN_indexes.clear();
2845 N_SO_index = UINT32_MAX;
2846 }
2847 else
2848 {
2849 // We use the current number of symbols in the symbol table in lieu of
2850 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002851 const bool N_SO_has_full_path = symbol_name[0] == '/';
2852 if (N_SO_has_full_path)
2853 {
2854 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2855 {
2856 // We have two consecutive N_SO entries where the first contains a directory
2857 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002858 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002859 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2860 add_nlist = false;
2861 }
2862 else
2863 {
2864 // This is the first entry in a N_SO that contains a directory or
2865 // a full path to the source file
2866 N_SO_index = sym_idx;
2867 }
2868 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002869 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2870 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002871 // This is usually the second N_SO entry that contains just the filename,
2872 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002873 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2874 if (so_path && so_path[0])
2875 {
2876 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002877 const size_t double_slash_pos = full_so_path.find("//");
2878 if (double_slash_pos != std::string::npos)
2879 {
2880 // The linker has been generating bad N_SO entries with doubled up paths
2881 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2882 // and the second is the directory for the source file so you end up with
2883 // a path that looks like "/tmp/src//tmp/src/"
2884 FileSpec so_dir(so_path, false);
2885 if (!so_dir.Exists())
2886 {
2887 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2888 if (so_dir.Exists())
2889 {
2890 // Trim off the incorrect path
2891 full_so_path.erase(0, double_slash_pos + 1);
2892 }
2893 }
2894 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002895 if (*full_so_path.rbegin() != '/')
2896 full_so_path += '/';
2897 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002898 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002899 add_nlist = false;
2900 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2901 }
2902 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002903 else
2904 {
2905 // This could be a relative path to a N_SO
2906 N_SO_index = sym_idx;
2907 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002908 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002909
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002910 break;
2911
2912 case StabObjectFileName:
2913 // N_OSO - object file name: name,,0,0,st_mtime
2914 type = eSymbolTypeObjectFile;
2915 break;
2916
2917 case StabLocalSymbol:
2918 // N_LSYM - local sym: name,,NO_SECT,type,offset
2919 type = eSymbolTypeLocal;
2920 break;
2921
2922 //----------------------------------------------------------------------
2923 // INCL scopes
2924 //----------------------------------------------------------------------
2925 case StabBeginIncludeFileName:
2926 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2927 // We use the current number of symbols in the symbol table in lieu of
2928 // using nlist_idx in case we ever start trimming entries out
2929 N_INCL_indexes.push_back(sym_idx);
2930 type = eSymbolTypeScopeBegin;
2931 break;
2932
2933 case StabEndIncludeFile:
2934 // N_EINCL - include file end: name,,NO_SECT,0,0
2935 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2936 // so that we can always skip the entire symbol if we need to navigate
2937 // more quickly at the source level when parsing STABS
2938 if ( !N_INCL_indexes.empty() )
2939 {
2940 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2941 symbol_ptr->SetByteSize(sym_idx + 1);
2942 symbol_ptr->SetSizeIsSibling(true);
2943 N_INCL_indexes.pop_back();
2944 }
2945 type = eSymbolTypeScopeEnd;
2946 break;
2947
2948 case StabIncludeFileName:
2949 // N_SOL - #included file name: name,,n_sect,0,address
2950 type = eSymbolTypeHeaderFile;
2951
2952 // We currently don't use the header files on darwin
2953 if (minimize)
2954 add_nlist = false;
2955 break;
2956
Jason Molenda9badb6c2013-03-06 23:19:17 +00002957 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002958 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2959 type = eSymbolTypeCompiler;
2960 break;
2961
2962 case StabCompilerVersion:
2963 // N_VERSION - compiler version: name,,NO_SECT,0,0
2964 type = eSymbolTypeCompiler;
2965 break;
2966
2967 case StabCompilerOptLevel:
2968 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2969 type = eSymbolTypeCompiler;
2970 break;
2971
2972 case StabParameter:
2973 // N_PSYM - parameter: name,,NO_SECT,type,offset
2974 type = eSymbolTypeVariable;
2975 break;
2976
2977 case StabAlternateEntry:
2978 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2979 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2980 type = eSymbolTypeLineEntry;
2981 break;
2982
2983 //----------------------------------------------------------------------
2984 // Left and Right Braces
2985 //----------------------------------------------------------------------
2986 case StabLeftBracket:
2987 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2988 // We use the current number of symbols in the symbol table in lieu of
2989 // using nlist_idx in case we ever start trimming entries out
2990 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2991 N_BRAC_indexes.push_back(sym_idx);
2992 type = eSymbolTypeScopeBegin;
2993 break;
2994
2995 case StabRightBracket:
2996 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2997 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2998 // so that we can always skip the entire symbol if we need to navigate
2999 // more quickly at the source level when parsing STABS
3000 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3001 if ( !N_BRAC_indexes.empty() )
3002 {
3003 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3004 symbol_ptr->SetByteSize(sym_idx + 1);
3005 symbol_ptr->SetSizeIsSibling(true);
3006 N_BRAC_indexes.pop_back();
3007 }
3008 type = eSymbolTypeScopeEnd;
3009 break;
3010
3011 case StabDeletedIncludeFile:
3012 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3013 type = eSymbolTypeHeaderFile;
3014 break;
3015
3016 //----------------------------------------------------------------------
3017 // COMM scopes
3018 //----------------------------------------------------------------------
3019 case StabBeginCommon:
3020 // N_BCOMM - begin common: name,,NO_SECT,0,0
3021 // We use the current number of symbols in the symbol table in lieu of
3022 // using nlist_idx in case we ever start trimming entries out
3023 type = eSymbolTypeScopeBegin;
3024 N_COMM_indexes.push_back(sym_idx);
3025 break;
3026
3027 case StabEndCommonLocal:
3028 // N_ECOML - end common (local name): 0,,n_sect,0,address
3029 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3030 // Fall through
3031
3032 case StabEndCommon:
3033 // N_ECOMM - end common: name,,n_sect,0,0
3034 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3035 // so that we can always skip the entire symbol if we need to navigate
3036 // more quickly at the source level when parsing STABS
3037 if ( !N_COMM_indexes.empty() )
3038 {
3039 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3040 symbol_ptr->SetByteSize(sym_idx + 1);
3041 symbol_ptr->SetSizeIsSibling(true);
3042 N_COMM_indexes.pop_back();
3043 }
3044 type = eSymbolTypeScopeEnd;
3045 break;
3046
3047 case StabLength:
3048 // N_LENG - second stab entry with length information
3049 type = eSymbolTypeAdditional;
3050 break;
3051
3052 default: break;
3053 }
3054 }
3055 else
3056 {
3057 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3058 uint8_t n_type = NlistMaskType & nlist.n_type;
3059 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3060
3061 switch (n_type)
3062 {
3063 case NListTypeIndirect: // N_INDR - Fall through
3064 case NListTypePreboundUndefined:// N_PBUD - Fall through
3065 case NListTypeUndefined: // N_UNDF
3066 type = eSymbolTypeUndefined;
3067 break;
3068
3069 case NListTypeAbsolute: // N_ABS
3070 type = eSymbolTypeAbsolute;
3071 break;
3072
3073 case NListTypeSection: // N_SECT
3074 {
3075 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3076
Sean Callananb386d822012-08-09 00:50:26 +00003077 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003078 {
3079 // TODO: warn about this?
3080 add_nlist = false;
3081 break;
3082 }
3083
3084 if (TEXT_eh_frame_sectID == nlist.n_sect)
3085 {
3086 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00003087 }
3088 else
3089 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003090 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3091
3092 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00003093 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003094 case SectionTypeRegular: break; // regular section
3095 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3096 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3097 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3098 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3099 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3100 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3101 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3102 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3103 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3104 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3105 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3106 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3107 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3108 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3109 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3110 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3111 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00003112 }
Chris Lattner24943d22010-06-08 16:52:24 +00003113
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003114 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00003115 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003116 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3117 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003118 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003119 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3120 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003121 SectionAttrSytemSomeInstructions))
3122 type = eSymbolTypeData;
3123 else
3124 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00003125 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003126 else
3127 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003128 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003129 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00003130 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003131 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00003132
Jason Molenda9badb6c2013-03-06 23:19:17 +00003133 if (symbol_name &&
3134 symbol_name[0] == '_' &&
3135 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003136 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00003137 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003138 llvm::StringRef symbol_name_ref(symbol_name);
3139 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3140 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3141 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3142 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00003143 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003144 symbol_name_non_abi_mangled = symbol_name + 1;
3145 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3146 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003147 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003148 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003149 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003150 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003151 symbol_name_non_abi_mangled = symbol_name + 1;
3152 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3153 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003154 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003155 }
3156 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3157 {
3158 symbol_name_non_abi_mangled = symbol_name + 1;
3159 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3160 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003161 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003162 }
3163 }
3164 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003165 else
3166 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3167 {
3168 type = eSymbolTypeException;
3169 }
3170 else
3171 {
3172 type = eSymbolTypeData;
3173 }
3174 }
3175 else
3176 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3177 {
3178 type = eSymbolTypeTrampoline;
3179 }
3180 else
3181 if (symbol_section->IsDescendant(objc_section_sp.get()))
3182 {
3183 type = eSymbolTypeRuntime;
3184 if (symbol_name && symbol_name[0] == '.')
3185 {
3186 llvm::StringRef symbol_name_ref(symbol_name);
3187 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3188 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3189 {
3190 symbol_name_non_abi_mangled = symbol_name;
3191 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3192 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003193 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003194 }
3195 }
Chris Lattner24943d22010-06-08 16:52:24 +00003196 }
3197 }
3198 }
3199 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003200 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003201 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003202 }
3203
3204 if (add_nlist)
3205 {
3206 uint64_t symbol_value = nlist.n_value;
3207 bool symbol_name_is_mangled = false;
3208
3209 if (symbol_name_non_abi_mangled)
3210 {
Greg Claytonc0240042012-07-18 23:18:10 +00003211 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3212 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003213 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003214 else
3215 {
3216 if (symbol_name && symbol_name[0] == '_')
3217 {
3218 symbol_name_is_mangled = symbol_name[1] == '_';
3219 symbol_name++; // Skip the leading underscore
3220 }
3221
3222 if (symbol_name)
3223 {
Greg Claytonc0240042012-07-18 23:18:10 +00003224 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003225 }
3226 }
3227
3228 if (is_debug == false)
3229 {
3230 if (type == eSymbolTypeCode)
3231 {
3232 // See if we can find a N_FUN entry for any code symbols.
3233 // If we do find a match, and the name matches, then we
3234 // can merge the two into just the function symbol to avoid
3235 // duplicate entries in the symbol table
3236 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3237 if (pos != N_FUN_addr_to_sym_idx.end())
3238 {
3239 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3240 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3241 {
3242 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3243 // We just need the flags from the linker symbol, so put these flags
3244 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3245 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3246 sym[sym_idx].Clear();
3247 continue;
3248 }
3249 }
3250 }
3251 else if (type == eSymbolTypeData)
3252 {
3253 // See if we can find a N_STSYM entry for any data symbols.
3254 // If we do find a match, and the name matches, then we
3255 // can merge the two into just the Static symbol to avoid
3256 // duplicate entries in the symbol table
3257 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3258 if (pos != N_STSYM_addr_to_sym_idx.end())
3259 {
3260 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3261 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3262 {
3263 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3264 // We just need the flags from the linker symbol, so put these flags
3265 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3266 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3267 sym[sym_idx].Clear();
3268 continue;
3269 }
3270 }
3271 }
3272 }
3273 if (symbol_section)
3274 {
3275 const addr_t section_file_addr = symbol_section->GetFileAddress();
3276 if (symbol_byte_size == 0 && function_starts_count > 0)
3277 {
Greg Claytond2653c22012-03-14 01:53:24 +00003278 addr_t symbol_lookup_file_addr = nlist.n_value;
3279 // Do an exact address match for non-ARM addresses, else get the closest since
3280 // the symbol might be a thumb symbol which has an address with bit zero set
3281 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3282 if (is_arm && func_start_entry)
3283 {
3284 // Verify that the function start address is the symbol address (ARM)
3285 // or the symbol address + 1 (thumb)
3286 if (func_start_entry->addr != symbol_lookup_file_addr &&
3287 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3288 {
3289 // Not the right entry, NULL it out...
3290 func_start_entry = NULL;
3291 }
3292 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003293 if (func_start_entry)
3294 {
3295 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003296
Greg Claytond2653c22012-03-14 01:53:24 +00003297 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003298 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003299 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003300
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003301 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3302 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3303 if (next_func_start_entry)
3304 {
Greg Claytond2653c22012-03-14 01:53:24 +00003305 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3306 // Be sure the clear the Thumb address bit when we calculate the size
3307 // from the current and next address
3308 if (is_arm)
3309 next_symbol_file_addr &= 0xfffffffffffffffeull;
3310 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 +00003311 }
3312 else
3313 {
Greg Claytond2653c22012-03-14 01:53:24 +00003314 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003315 }
3316 }
3317 }
3318 symbol_value -= section_file_addr;
3319 }
3320
3321 sym[sym_idx].SetID (nlist_idx);
3322 sym[sym_idx].SetType (type);
3323 sym[sym_idx].GetAddress().SetSection (symbol_section);
3324 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3325 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3326
3327 if (symbol_byte_size > 0)
3328 sym[sym_idx].SetByteSize(symbol_byte_size);
3329
Greg Clayton01e6a582012-11-27 01:52:16 +00003330 if (demangled_is_synthesized)
3331 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3332
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003333 ++sym_idx;
3334 }
3335 else
3336 {
3337 sym[sym_idx].Clear();
3338 }
3339
3340 }
3341
3342 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3343 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3344 // such entries by figuring out what the address for the global is by looking up this non-STAB
3345 // entry and copying the value into the debug symbol's value to save us the hassle in the
3346 // debug symbol parser.
3347
3348 Symbol *global_symbol = NULL;
3349 for (nlist_idx = 0;
3350 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3351 nlist_idx++)
3352 {
3353 if (global_symbol->GetAddress().GetFileAddress() == 0)
3354 {
3355 std::vector<uint32_t> indexes;
3356 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3357 {
3358 std::vector<uint32_t>::const_iterator pos;
3359 std::vector<uint32_t>::const_iterator end = indexes.end();
3360 for (pos = indexes.begin(); pos != end; ++pos)
3361 {
3362 symbol_ptr = symtab->SymbolAtIndex(*pos);
3363 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3364 {
3365 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3366 break;
3367 }
3368 }
3369 }
Chris Lattner24943d22010-06-08 16:52:24 +00003370 }
3371 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003372
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003373 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3374
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003375 if (function_starts_count > 0)
3376 {
3377 char synthetic_function_symbol[PATH_MAX];
3378 uint32_t num_synthetic_function_symbols = 0;
3379 for (i=0; i<function_starts_count; ++i)
3380 {
3381 if (function_starts.GetEntryRef (i).data == false)
3382 ++num_synthetic_function_symbols;
3383 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003384
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003385 if (num_synthetic_function_symbols > 0)
3386 {
3387 if (num_syms < sym_idx + num_synthetic_function_symbols)
3388 {
3389 num_syms = sym_idx + num_synthetic_function_symbols;
3390 sym = symtab->Resize (num_syms);
3391 }
3392 uint32_t synthetic_function_symbol_idx = 0;
3393 for (i=0; i<function_starts_count; ++i)
3394 {
3395 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3396 if (func_start_entry->data == false)
3397 {
Greg Claytond2653c22012-03-14 01:53:24 +00003398 addr_t symbol_file_addr = func_start_entry->addr;
3399 uint32_t symbol_flags = 0;
3400 if (is_arm)
3401 {
3402 if (symbol_file_addr & 1)
3403 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3404 symbol_file_addr &= 0xfffffffffffffffeull;
3405 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003406 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003407 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003408 {
3409 SectionSP symbol_section (symbol_addr.GetSection());
3410 uint32_t symbol_byte_size = 0;
3411 if (symbol_section)
3412 {
3413 const addr_t section_file_addr = symbol_section->GetFileAddress();
3414 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3415 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3416 if (next_func_start_entry)
3417 {
Greg Claytond2653c22012-03-14 01:53:24 +00003418 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3419 if (is_arm)
3420 next_symbol_file_addr &= 0xfffffffffffffffeull;
3421 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 +00003422 }
3423 else
3424 {
Greg Claytond2653c22012-03-14 01:53:24 +00003425 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003426 }
3427 snprintf (synthetic_function_symbol,
3428 sizeof(synthetic_function_symbol),
3429 "___lldb_unnamed_function%u$$%s",
3430 ++synthetic_function_symbol_idx,
3431 module_sp->GetFileSpec().GetFilename().GetCString());
3432 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003433 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003434 sym[sym_idx].SetType (eSymbolTypeCode);
3435 sym[sym_idx].SetIsSynthetic (true);
3436 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003437 if (symbol_flags)
3438 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003439 if (symbol_byte_size)
3440 sym[sym_idx].SetByteSize (symbol_byte_size);
3441 ++sym_idx;
3442 }
3443 }
3444 }
3445 }
3446 }
3447 }
3448
3449 // Trim our symbols down to just what we ended up with after
3450 // removing any symbols.
3451 if (sym_idx < num_syms)
3452 {
3453 num_syms = sym_idx;
3454 sym = symtab->Resize (num_syms);
3455 }
3456
3457 // Now synthesize indirect symbols
3458 if (m_dysymtab.nindirectsyms != 0)
3459 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003460 if (indirect_symbol_index_data.GetByteSize())
3461 {
3462 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3463
3464 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3465 {
3466 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3467 {
3468 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3469 if (symbol_stub_byte_size == 0)
3470 continue;
3471
3472 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3473
3474 if (num_symbol_stubs == 0)
3475 continue;
3476
3477 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3478 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3479 {
3480 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3481 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 +00003482 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003483 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3484 {
3485 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3486 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3487 continue;
3488
3489 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3490 Symbol *stub_symbol = NULL;
3491 if (index_pos != end_index_pos)
3492 {
3493 // We have a remapping from the original nlist index to
3494 // a current symbol index, so just look this up by index
3495 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3496 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003497 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003498 {
3499 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003500 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003501 // S_SYMBOL_STUBS
3502 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3503 }
3504
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003505 if (stub_symbol)
3506 {
3507 Address so_addr(symbol_stub_addr, section_list);
3508
3509 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3510 {
3511 // Change the external symbol into a trampoline that makes sense
3512 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3513 // can re-use them so we don't have to make up a synthetic symbol
3514 // for no good reason.
3515 stub_symbol->SetType (eSymbolTypeTrampoline);
3516 stub_symbol->SetExternal (false);
3517 stub_symbol->GetAddress() = so_addr;
3518 stub_symbol->SetByteSize (symbol_stub_byte_size);
3519 }
3520 else
3521 {
3522 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003523 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003524 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003525 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003526 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003527 stub_symbol = NULL; // this pointer no longer valid
3528 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003529 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003530 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003531 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3532 sym[sym_idx].SetIsSynthetic (true);
3533 sym[sym_idx].GetAddress() = so_addr;
3534 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3535 ++sym_idx;
3536 }
3537 }
Greg Claytond4330e62012-09-05 01:38:55 +00003538 else
3539 {
3540 if (log)
3541 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3542 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003543 }
3544 }
3545 }
3546 }
3547 }
3548 }
3549 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003550 }
3551 return 0;
3552}
3553
3554
3555void
3556ObjectFileMachO::Dump (Stream *s)
3557{
Greg Clayton9482f052012-03-13 23:14:29 +00003558 ModuleSP module_sp(GetModule());
3559 if (module_sp)
3560 {
3561 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3562 s->Printf("%p: ", this);
3563 s->Indent();
3564 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3565 s->PutCString("ObjectFileMachO64");
3566 else
3567 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003568
Greg Clayton9482f052012-03-13 23:14:29 +00003569 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003570
Greg Clayton9482f052012-03-13 23:14:29 +00003571 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003572
Greg Clayton9482f052012-03-13 23:14:29 +00003573 if (m_sections_ap.get())
3574 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003575
Greg Clayton9482f052012-03-13 23:14:29 +00003576 if (m_symtab_ap.get())
3577 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3578 }
Chris Lattner24943d22010-06-08 16:52:24 +00003579}
3580
Greg Clayton36b877d2013-04-24 22:29:28 +00003581bool
3582ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3583 const lldb_private::DataExtractor &data,
3584 lldb::offset_t lc_offset,
3585 lldb_private::UUID& uuid)
3586{
3587 uint32_t i;
3588 struct uuid_command load_cmd;
3589
3590 lldb::offset_t offset = lc_offset;
3591 for (i=0; i<header.ncmds; ++i)
3592 {
3593 const lldb::offset_t cmd_offset = offset;
3594 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3595 break;
3596
3597 if (load_cmd.cmd == LoadCommandUUID)
3598 {
3599 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3600
3601 if (uuid_bytes)
3602 {
3603 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3604 // We pretend these object files have no UUID to prevent crashing.
3605
3606 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3607 0x3b, 0xa8,
3608 0x4b, 0x16,
3609 0xb6, 0xa4,
3610 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3611
3612 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3613 return false;
3614
3615 uuid.SetBytes (uuid_bytes);
3616 return true;
3617 }
3618 return false;
3619 }
3620 offset = cmd_offset + load_cmd.cmdsize;
3621 }
3622 return false;
3623}
Chris Lattner24943d22010-06-08 16:52:24 +00003624
3625bool
Greg Clayton0467c782011-02-04 18:53:10 +00003626ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003627{
Greg Clayton9482f052012-03-13 23:14:29 +00003628 ModuleSP module_sp(GetModule());
3629 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003630 {
Greg Clayton9482f052012-03-13 23:14:29 +00003631 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00003632 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton36b877d2013-04-24 22:29:28 +00003633 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner24943d22010-06-08 16:52:24 +00003634 }
3635 return false;
3636}
3637
3638
3639uint32_t
3640ObjectFileMachO::GetDependentModules (FileSpecList& files)
3641{
Chris Lattner24943d22010-06-08 16:52:24 +00003642 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003643 ModuleSP module_sp(GetModule());
3644 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003645 {
Greg Clayton9482f052012-03-13 23:14:29 +00003646 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3647 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003648 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003649 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3650 uint32_t i;
3651 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003652 {
Greg Clayton9482f052012-03-13 23:14:29 +00003653 const uint32_t cmd_offset = offset;
3654 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3655 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003656
Greg Clayton9482f052012-03-13 23:14:29 +00003657 switch (load_cmd.cmd)
3658 {
3659 case LoadCommandDylibLoad:
3660 case LoadCommandDylibLoadWeak:
3661 case LoadCommandDylibReexport:
3662 case LoadCommandDynamicLinkerLoad:
3663 case LoadCommandFixedVMShlibLoad:
3664 case LoadCommandDylibLoadUpward:
3665 {
3666 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3667 const char *path = m_data.PeekCStr(name_offset);
3668 // Skip any path that starts with '@' since these are usually:
3669 // @executable_path/.../file
3670 // @rpath/.../file
3671 if (path && path[0] != '@')
3672 {
3673 FileSpec file_spec(path, resolve_path);
3674 if (files.AppendIfUnique(file_spec))
3675 count++;
3676 }
3677 }
3678 break;
3679
3680 default:
3681 break;
3682 }
3683 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003684 }
Chris Lattner24943d22010-06-08 16:52:24 +00003685 }
3686 return count;
3687}
3688
Jim Ingham28775942011-03-07 23:44:08 +00003689lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003690ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003691{
3692 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3693 // is initialized to an invalid address, so we can just return that.
3694 // 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 +00003695
Jim Ingham28775942011-03-07 23:44:08 +00003696 if (!IsExecutable() || m_entry_point_address.IsValid())
3697 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003698
3699 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003700 // /usr/include/mach-o.h, but it is basically:
3701 //
3702 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3703 // uint32_t count - this is the count of longs in the thread state data
3704 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3705 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003706 //
Jim Ingham28775942011-03-07 23:44:08 +00003707 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3708 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3709 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3710 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3711 //
3712 // For now we hard-code the offsets and flavors we need:
3713 //
3714 //
3715
Greg Clayton9482f052012-03-13 23:14:29 +00003716 ModuleSP module_sp(GetModule());
3717 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003718 {
Greg Clayton9482f052012-03-13 23:14:29 +00003719 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3720 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003721 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003722 uint32_t i;
3723 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3724 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003725
Greg Clayton9482f052012-03-13 23:14:29 +00003726 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003727 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003728 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003729 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3730 break;
3731
3732 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003733 {
Greg Clayton9482f052012-03-13 23:14:29 +00003734 case LoadCommandUnixThread:
3735 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003736 {
Greg Clayton9482f052012-03-13 23:14:29 +00003737 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003738 {
Greg Clayton9482f052012-03-13 23:14:29 +00003739 uint32_t flavor = m_data.GetU32(&offset);
3740 uint32_t count = m_data.GetU32(&offset);
3741 if (count == 0)
3742 {
3743 // We've gotten off somehow, log and exit;
3744 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003745 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003746
Greg Clayton9482f052012-03-13 23:14:29 +00003747 switch (m_header.cputype)
3748 {
3749 case llvm::MachO::CPUTypeARM:
3750 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3751 {
3752 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3753 start_address = m_data.GetU32(&offset);
3754 done = true;
3755 }
Jim Ingham28775942011-03-07 23:44:08 +00003756 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003757 case llvm::MachO::CPUTypeI386:
3758 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3759 {
3760 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3761 start_address = m_data.GetU32(&offset);
3762 done = true;
3763 }
3764 break;
3765 case llvm::MachO::CPUTypeX86_64:
3766 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3767 {
3768 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3769 start_address = m_data.GetU64(&offset);
3770 done = true;
3771 }
3772 break;
3773 default:
3774 return m_entry_point_address;
3775 }
3776 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3777 if (done)
3778 break;
3779 offset += count * 4;
3780 }
Jim Ingham28775942011-03-07 23:44:08 +00003781 }
Greg Clayton9482f052012-03-13 23:14:29 +00003782 break;
3783 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003784 {
Greg Clayton9482f052012-03-13 23:14:29 +00003785 ConstString text_segment_name ("__TEXT");
3786 uint64_t entryoffset = m_data.GetU64(&offset);
3787 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3788 if (text_segment_sp)
3789 {
3790 done = true;
3791 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3792 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003793 }
Greg Clayton9482f052012-03-13 23:14:29 +00003794
3795 default:
3796 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003797 }
Greg Clayton9482f052012-03-13 23:14:29 +00003798 if (done)
3799 break;
Jim Ingham28775942011-03-07 23:44:08 +00003800
Greg Clayton9482f052012-03-13 23:14:29 +00003801 // Go to the next load command:
3802 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003803 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003804
Greg Clayton9482f052012-03-13 23:14:29 +00003805 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003806 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003807 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003808 // of this ObjectFile:
3809 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003810 {
Greg Clayton9482f052012-03-13 23:14:29 +00003811 m_entry_point_address.Clear();
3812 }
3813 }
3814 else
3815 {
3816 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3817 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003818
Greg Clayton9482f052012-03-13 23:14:29 +00003819 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003820
Greg Clayton9482f052012-03-13 23:14:29 +00003821 if (module_sp)
3822 {
3823 SymbolContextList contexts;
3824 SymbolContext context;
3825 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3826 {
3827 if (contexts.GetContextAtIndex(0, context))
3828 m_entry_point_address = context.symbol->GetAddress();
3829 }
Greg Clayton3508c382012-02-24 01:59:29 +00003830 }
3831 }
Jim Ingham28775942011-03-07 23:44:08 +00003832 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003833
Jim Ingham28775942011-03-07 23:44:08 +00003834 return m_entry_point_address;
3835
3836}
3837
Greg Claytonb5a8f142012-02-05 02:38:54 +00003838lldb_private::Address
3839ObjectFileMachO::GetHeaderAddress ()
3840{
3841 lldb_private::Address header_addr;
3842 SectionList *section_list = GetSectionList();
3843 if (section_list)
3844 {
3845 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3846 if (text_segment_sp)
3847 {
Greg Clayton3508c382012-02-24 01:59:29 +00003848 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003849 header_addr.SetOffset (0);
3850 }
3851 }
3852 return header_addr;
3853}
3854
Greg Clayton46c9a352012-02-09 06:16:32 +00003855uint32_t
3856ObjectFileMachO::GetNumThreadContexts ()
3857{
Greg Clayton9482f052012-03-13 23:14:29 +00003858 ModuleSP module_sp(GetModule());
3859 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003860 {
Greg Clayton9482f052012-03-13 23:14:29 +00003861 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3862 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003863 {
Greg Clayton9482f052012-03-13 23:14:29 +00003864 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003865 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003866 FileRangeArray::Entry file_range;
3867 thread_command thread_cmd;
3868 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003869 {
Greg Clayton9482f052012-03-13 23:14:29 +00003870 const uint32_t cmd_offset = offset;
3871 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3872 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003873
Greg Clayton9482f052012-03-13 23:14:29 +00003874 if (thread_cmd.cmd == LoadCommandThread)
3875 {
3876 file_range.SetRangeBase (offset);
3877 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3878 m_thread_context_offsets.Append (file_range);
3879 }
3880 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003881 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003882 }
3883 }
3884 return m_thread_context_offsets.GetSize();
3885}
3886
3887lldb::RegisterContextSP
3888ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3889{
Greg Clayton46c9a352012-02-09 06:16:32 +00003890 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003891
Greg Clayton9482f052012-03-13 23:14:29 +00003892 ModuleSP module_sp(GetModule());
3893 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003894 {
Greg Clayton9482f052012-03-13 23:14:29 +00003895 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3896 if (!m_thread_context_offsets_valid)
3897 GetNumThreadContexts ();
3898
3899 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003900 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003901 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003902
3903 DataExtractor data (m_data,
3904 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003905 thread_context_file_range->GetByteSize());
3906
3907 switch (m_header.cputype)
3908 {
3909 case llvm::MachO::CPUTypeARM:
3910 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3911 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003912
Jim Ingham6f01c932012-10-12 17:34:26 +00003913 case llvm::MachO::CPUTypeI386:
3914 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3915 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003916
Jim Ingham6f01c932012-10-12 17:34:26 +00003917 case llvm::MachO::CPUTypeX86_64:
3918 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3919 break;
3920 }
Greg Clayton9482f052012-03-13 23:14:29 +00003921 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003922 }
3923 return reg_ctx_sp;
3924}
3925
Greg Claytonb5a8f142012-02-05 02:38:54 +00003926
Greg Claytonca319972011-07-09 00:41:34 +00003927ObjectFile::Type
3928ObjectFileMachO::CalculateType()
3929{
3930 switch (m_header.filetype)
3931 {
3932 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3933 if (GetAddressByteSize () == 4)
3934 {
3935 // 32 bit kexts are just object files, but they do have a valid
3936 // UUID load command.
3937 UUID uuid;
3938 if (GetUUID(&uuid))
3939 {
3940 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003941 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003942 // "OSKextGetCurrentIdentifier" as this is required of kexts
3943 if (m_strata == eStrataInvalid)
3944 m_strata = eStrataKernel;
3945 return eTypeSharedLibrary;
3946 }
3947 }
3948 return eTypeObjectFile;
3949
3950 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3951 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3952 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3953 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3954 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3955 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3956 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3957 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3958 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3959 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3960 default:
3961 break;
3962 }
3963 return eTypeUnknown;
3964}
3965
3966ObjectFile::Strata
3967ObjectFileMachO::CalculateStrata()
3968{
3969 switch (m_header.filetype)
3970 {
3971 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3972 {
3973 // 32 bit kexts are just object files, but they do have a valid
3974 // UUID load command.
3975 UUID uuid;
3976 if (GetUUID(&uuid))
3977 {
3978 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003979 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003980 // "OSKextGetCurrentIdentifier" as this is required of kexts
3981 if (m_type == eTypeInvalid)
3982 m_type = eTypeSharedLibrary;
3983
3984 return eStrataKernel;
3985 }
3986 }
3987 return eStrataUnknown;
3988
3989 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
3990 // Check for the MH_DYLDLINK bit in the flags
3991 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00003992 {
Greg Claytonca319972011-07-09 00:41:34 +00003993 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00003994 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003995 else
Sean Callananac725af2012-02-10 20:22:35 +00003996 {
3997 SectionList *section_list = GetSectionList();
3998 if (section_list)
3999 {
4000 static ConstString g_kld_section_name ("__KLD");
4001 if (section_list->FindSectionByName(g_kld_section_name))
4002 return eStrataKernel;
4003 }
4004 }
4005 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00004006
4007 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4008 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00004009 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00004010 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4011 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4012 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4013 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4014 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4015 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4016 default:
4017 break;
4018 }
4019 return eStrataUnknown;
4020}
4021
4022
Greg Clayton49f4bf22012-02-22 19:41:02 +00004023uint32_t
4024ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4025{
Greg Clayton9482f052012-03-13 23:14:29 +00004026 ModuleSP module_sp(GetModule());
4027 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004028 {
Greg Clayton9482f052012-03-13 23:14:29 +00004029 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4030 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00004031 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00004032 uint32_t version_cmd = 0;
4033 uint64_t version = 0;
4034 uint32_t i;
4035 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004036 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00004037 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00004038 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4039 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00004040
Greg Clayton9482f052012-03-13 23:14:29 +00004041 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004042 {
Greg Clayton9482f052012-03-13 23:14:29 +00004043 if (version_cmd == 0)
4044 {
4045 version_cmd = load_cmd.cmd;
4046 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4047 break;
4048 version = load_cmd.dylib.current_version;
4049 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004050 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00004051 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00004052 }
Greg Clayton9482f052012-03-13 23:14:29 +00004053 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004054 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004055
Greg Clayton9482f052012-03-13 23:14:29 +00004056 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004057 {
Greg Clayton9482f052012-03-13 23:14:29 +00004058 if (versions != NULL && num_versions > 0)
4059 {
4060 if (num_versions > 0)
4061 versions[0] = (version & 0xFFFF0000ull) >> 16;
4062 if (num_versions > 1)
4063 versions[1] = (version & 0x0000FF00ull) >> 8;
4064 if (num_versions > 2)
4065 versions[2] = (version & 0x000000FFull);
4066 // Fill in an remaining version numbers with invalid values
4067 for (i=3; i<num_versions; ++i)
4068 versions[i] = UINT32_MAX;
4069 }
4070 // The LC_ID_DYLIB load command has a version with 3 version numbers
4071 // in it, so always return 3
4072 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004073 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00004074 }
4075 return false;
4076}
4077
Chris Lattner24943d22010-06-08 16:52:24 +00004078bool
Greg Clayton395fc332011-02-15 21:59:32 +00004079ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00004080{
Greg Clayton9482f052012-03-13 23:14:29 +00004081 ModuleSP module_sp(GetModule());
4082 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004083 {
Greg Clayton9482f052012-03-13 23:14:29 +00004084 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4085 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00004086
Greg Clayton9482f052012-03-13 23:14:29 +00004087 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00004088 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00004089 // unknown to make sure we use the DynamicLoaderStatic()...
4090 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4091 {
4092 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4093 }
4094 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004095 }
Greg Clayton9482f052012-03-13 23:14:29 +00004096 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00004097}
4098
4099
Jason Molenda45c75502013-04-16 06:24:42 +00004100UUID
4101ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4102{
4103 UUID uuid;
4104 if (process)
4105 {
4106 addr_t all_image_infos = process->GetImageInfoAddress();
4107
4108 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4109 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4110 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4111 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4112
4113 Error err;
4114 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4115 if (version_or_magic != -1
4116 && version_or_magic != HeaderMagic32
4117 && version_or_magic != HeaderMagic32Swapped
4118 && version_or_magic != HeaderMagic64
4119 && version_or_magic != HeaderMagic64Swapped
4120 && version_or_magic >= 13)
4121 {
4122 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4123 int wordsize = process->GetAddressByteSize();
4124 if (wordsize == 8)
4125 {
4126 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4127 }
4128 if (wordsize == 4)
4129 {
4130 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4131 }
4132 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4133 {
4134 uuid_t shared_cache_uuid;
4135 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4136 {
4137 uuid.SetBytes (shared_cache_uuid);
4138 }
4139 }
4140 }
4141 }
4142 return uuid;
4143}
4144
4145UUID
4146ObjectFileMachO::GetLLDBSharedCacheUUID ()
4147{
4148 UUID uuid;
4149#if defined (__APPLE__) && defined (__arm__)
4150 uint8_t *(*dyld_get_all_image_infos)(void);
4151 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4152 if (dyld_get_all_image_infos)
4153 {
4154 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4155 if (dyld_all_image_infos_address)
4156 {
Jason Molenda6ff916f2013-04-16 21:42:58 +00004157 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4158 if (*version >= 13)
Jason Molenda45c75502013-04-16 06:24:42 +00004159 {
Jason Molenda2ceae992013-04-16 22:56:17 +00004160 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 +00004161 uuid.SetBytes (sharedCacheUUID_address);
4162 }
4163 }
4164 }
4165#endif
4166 return uuid;
4167}
4168
4169
Chris Lattner24943d22010-06-08 16:52:24 +00004170//------------------------------------------------------------------
4171// PluginInterface protocol
4172//------------------------------------------------------------------
Greg Clayton0e191602013-05-10 21:47:16 +00004173lldb_private::ConstString
Chris Lattner24943d22010-06-08 16:52:24 +00004174ObjectFileMachO::GetPluginName()
4175{
Chris Lattner24943d22010-06-08 16:52:24 +00004176 return GetPluginNameStatic();
4177}
4178
4179uint32_t
4180ObjectFileMachO::GetPluginVersion()
4181{
4182 return 1;
4183}
4184