blob: 7ef4fac242231c25a29a0fd7319af00aa93b1e9c [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);
Jason Molenda3b244b72013-05-14 03:25:58 +0000292 bool done = false;
293
294 while (!done)
Greg Clayton9ce95382012-02-13 23:10:39 +0000295 {
Jason Molenda3b244b72013-05-14 03:25:58 +0000296 int flavor = data.GetU32 (&offset);
297 uint32_t count = data.GetU32 (&offset);
298 switch (flavor)
299 {
300 case GPRRegSet:
301 for (uint32_t i=0; i<count; ++i)
302 gpr.r[i] = data.GetU32(&offset);
303 SetError (GPRRegSet, Read, 0);
304 break;
305
306 case FPURegSet:
307 {
Jason Molenda2f038762013-05-14 03:52:22 +0000308 uint8_t *fpu_reg_buf = (uint8_t*) &fpu.floats.s[0];
309 const int fpu_reg_buf_size = sizeof (fpu.floats);
310 if (data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
Jason Molenda3b244b72013-05-14 03:25:58 +0000311 {
Jason Molenda2f038762013-05-14 03:52:22 +0000312 offset += fpu_reg_buf_size;
313 fpu.fpscr = data.GetU32(&offset);
314 SetError (FPURegSet, Read, 0);
Jason Molenda3b244b72013-05-14 03:25:58 +0000315 }
Jason Molenda2f038762013-05-14 03:52:22 +0000316 else
317 {
318 done = true;
319 }
Jason Molenda3b244b72013-05-14 03:25:58 +0000320 }
321 break;
322
323 case EXCRegSet:
324 exc.exception = data.GetU32(&offset);
325 exc.fsr = data.GetU32(&offset);
326 exc.far = data.GetU32(&offset);
327 SetError (EXCRegSet, Read, 0);
328 done = true;
329 break;
330
331 // Unknown register set flavor, stop trying to parse.
332 default:
333 done = true;
334 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000335 }
336 }
337protected:
338 virtual int
339 DoReadGPR (lldb::tid_t tid, int flavor, 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 DoReadFPU (lldb::tid_t tid, int flavor, 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 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
352 {
353 return 0;
354 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000355
356 virtual int
357 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
358 {
359 return -1;
360 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000361
Greg Clayton9ce95382012-02-13 23:10:39 +0000362 virtual int
363 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
364 {
365 return 0;
366 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000367
Greg Clayton9ce95382012-02-13 23:10:39 +0000368 virtual int
369 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
370 {
371 return 0;
372 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000373
Greg Clayton9ce95382012-02-13 23:10:39 +0000374 virtual int
375 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
376 {
377 return 0;
378 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000379
Greg Claytonb5431d02012-10-30 23:57:32 +0000380 virtual int
381 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
382 {
383 return -1;
384 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000385};
386
Greg Claytonb1888f22011-03-19 01:12:21 +0000387#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000388
389void
390ObjectFileMachO::Initialize()
391{
392 PluginManager::RegisterPlugin (GetPluginNameStatic(),
393 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000394 CreateInstance,
Greg Clayton36b877d2013-04-24 22:29:28 +0000395 CreateMemoryInstance,
396 GetModuleSpecifications);
Chris Lattner24943d22010-06-08 16:52:24 +0000397}
398
399void
400ObjectFileMachO::Terminate()
401{
402 PluginManager::UnregisterPlugin (CreateInstance);
403}
404
405
Greg Clayton0e191602013-05-10 21:47:16 +0000406lldb_private::ConstString
Chris Lattner24943d22010-06-08 16:52:24 +0000407ObjectFileMachO::GetPluginNameStatic()
408{
Greg Clayton0e191602013-05-10 21:47:16 +0000409 static ConstString g_name("mach-o");
410 return g_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000411}
412
413const char *
414ObjectFileMachO::GetPluginDescriptionStatic()
415{
416 return "Mach-o object file reader (32 and 64 bit)";
417}
418
Chris Lattner24943d22010-06-08 16:52:24 +0000419ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000420ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
421 DataBufferSP& data_sp,
422 lldb::offset_t data_offset,
423 const FileSpec* file,
424 lldb::offset_t file_offset,
425 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000426{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000427 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000428 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000429 data_sp = file->MemoryMapFileContents(file_offset, length);
430 data_offset = 0;
431 }
432
433 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
434 {
435 // Update the data to contain the entire file if it doesn't already
436 if (data_sp->GetByteSize() < length)
437 {
438 data_sp = file->MemoryMapFileContents(file_offset, length);
439 data_offset = 0;
440 }
Greg Clayton102b2c22013-04-18 22:45:39 +0000441 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 +0000442 if (objfile_ap.get() && objfile_ap->ParseHeader())
443 return objfile_ap.release();
444 }
445 return NULL;
446}
447
Greg Claytonb5a8f142012-02-05 02:38:54 +0000448ObjectFile *
Jason Molenda9badb6c2013-03-06 23:19:17 +0000449ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
450 DataBufferSP& data_sp,
451 const ProcessSP &process_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000452 lldb::addr_t header_addr)
453{
454 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
455 {
Greg Clayton102b2c22013-04-18 22:45:39 +0000456 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000457 if (objfile_ap.get() && objfile_ap->ParseHeader())
458 return objfile_ap.release();
459 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000460 return NULL;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000461}
462
Greg Clayton36b877d2013-04-24 22:29:28 +0000463size_t
464ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
465 lldb::DataBufferSP& data_sp,
466 lldb::offset_t data_offset,
467 lldb::offset_t file_offset,
468 lldb::offset_t length,
469 lldb_private::ModuleSpecList &specs)
470{
471 const size_t initial_count = specs.GetSize();
472
473 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
474 {
475 DataExtractor data;
476 data.SetData(data_sp);
477 llvm::MachO::mach_header header;
478 if (ParseHeader (data, &data_offset, header))
479 {
480 if (header.sizeofcmds >= data_sp->GetByteSize())
481 {
482 data_sp = file.ReadFileContents(file_offset, header.sizeofcmds);
483 data_offset = 0;
484 }
485 if (data_sp)
486 {
487 ModuleSpec spec;
488 spec.GetFileSpec() = file;
489 spec.GetArchitecture().SetArchitecture(eArchTypeMachO,
490 header.cputype,
491 header.cpusubtype);
492 if (spec.GetArchitecture().IsValid())
493 {
494 GetUUID (header, data, data_offset, spec.GetUUID());
495 specs.Append(spec);
496 }
497 }
498 }
499 }
500 return specs.GetSize() - initial_count;
501}
502
503
Greg Claytonb5a8f142012-02-05 02:38:54 +0000504
505const ConstString &
506ObjectFileMachO::GetSegmentNameTEXT()
507{
508 static ConstString g_segment_name_TEXT ("__TEXT");
509 return g_segment_name_TEXT;
510}
511
512const ConstString &
513ObjectFileMachO::GetSegmentNameDATA()
514{
515 static ConstString g_segment_name_DATA ("__DATA");
516 return g_segment_name_DATA;
517}
518
519const ConstString &
520ObjectFileMachO::GetSegmentNameOBJC()
521{
522 static ConstString g_segment_name_OBJC ("__OBJC");
523 return g_segment_name_OBJC;
524}
525
526const ConstString &
527ObjectFileMachO::GetSegmentNameLINKEDIT()
528{
529 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
530 return g_section_name_LINKEDIT;
531}
532
533const ConstString &
534ObjectFileMachO::GetSectionNameEHFrame()
535{
536 static ConstString g_section_name_eh_frame ("__eh_frame");
537 return g_section_name_eh_frame;
538}
539
540
Chris Lattner24943d22010-06-08 16:52:24 +0000541
542static uint32_t
543MachHeaderSizeFromMagic(uint32_t magic)
544{
545 switch (magic)
546 {
Greg Clayton1674b122010-07-21 22:12:05 +0000547 case HeaderMagic32:
548 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000549 return sizeof(struct mach_header);
550
Greg Clayton1674b122010-07-21 22:12:05 +0000551 case HeaderMagic64:
552 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000553 return sizeof(struct mach_header_64);
554 break;
555
556 default:
557 break;
558 }
559 return 0;
560}
561
562
563bool
Jason Molenda9badb6c2013-03-06 23:19:17 +0000564ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
565 lldb::addr_t data_offset,
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000566 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000567{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000568 DataExtractor data;
569 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000570 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000571 uint32_t magic = data.GetU32(&offset);
572 return MachHeaderSizeFromMagic(magic) != 0;
573}
574
575
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000576ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
577 DataBufferSP& data_sp,
578 lldb::offset_t data_offset,
579 const FileSpec* file,
580 lldb::offset_t file_offset,
581 lldb::offset_t length) :
582 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Clayton46c9a352012-02-09 06:16:32 +0000583 m_mach_segments(),
584 m_mach_sections(),
585 m_entry_point_address(),
586 m_thread_context_offsets(),
587 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000588{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000589 ::memset (&m_header, 0, sizeof(m_header));
590 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000591}
592
Greg Clayton3508c382012-02-24 01:59:29 +0000593ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000594 lldb::DataBufferSP& header_data_sp,
595 const lldb::ProcessSP &process_sp,
596 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000597 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Clayton46c9a352012-02-09 06:16:32 +0000598 m_mach_segments(),
599 m_mach_sections(),
600 m_entry_point_address(),
601 m_thread_context_offsets(),
602 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000603{
604 ::memset (&m_header, 0, sizeof(m_header));
605 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
606}
Chris Lattner24943d22010-06-08 16:52:24 +0000607
608ObjectFileMachO::~ObjectFileMachO()
609{
610}
611
Greg Clayton36b877d2013-04-24 22:29:28 +0000612bool
613ObjectFileMachO::ParseHeader (DataExtractor &data,
614 lldb::offset_t *data_offset_ptr,
615 llvm::MachO::mach_header &header)
616{
617 data.SetByteOrder (lldb::endian::InlHostByteOrder());
618 // Leave magic in the original byte order
619 header.magic = data.GetU32(data_offset_ptr);
620 bool can_parse = false;
621 bool is_64_bit = false;
622 switch (header.magic)
623 {
624 case HeaderMagic32:
625 data.SetByteOrder (lldb::endian::InlHostByteOrder());
626 data.SetAddressByteSize(4);
627 can_parse = true;
628 break;
629
630 case HeaderMagic64:
631 data.SetByteOrder (lldb::endian::InlHostByteOrder());
632 data.SetAddressByteSize(8);
633 can_parse = true;
634 is_64_bit = true;
635 break;
636
637 case HeaderMagic32Swapped:
638 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
639 data.SetAddressByteSize(4);
640 can_parse = true;
641 break;
642
643 case HeaderMagic64Swapped:
644 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
645 data.SetAddressByteSize(8);
646 is_64_bit = true;
647 can_parse = true;
648 break;
649
650 default:
651 break;
652 }
653
654 if (can_parse)
655 {
656 data.GetU32(data_offset_ptr, &header.cputype, 6);
657 if (is_64_bit)
658 *data_offset_ptr += 4;
659 return true;
660 }
661 else
662 {
663 memset(&header, 0, sizeof(header));
664 }
665 return false;
666}
Chris Lattner24943d22010-06-08 16:52:24 +0000667
668bool
669ObjectFileMachO::ParseHeader ()
670{
Greg Clayton9482f052012-03-13 23:14:29 +0000671 ModuleSP module_sp(GetModule());
672 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000673 {
Greg Clayton9482f052012-03-13 23:14:29 +0000674 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
675 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000676 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000677 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000678 // Leave magic in the original byte order
679 m_header.magic = m_data.GetU32(&offset);
680 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000681 {
Greg Clayton9482f052012-03-13 23:14:29 +0000682 case HeaderMagic32:
683 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
684 m_data.SetAddressByteSize(4);
685 can_parse = true;
686 break;
687
688 case HeaderMagic64:
689 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
690 m_data.SetAddressByteSize(8);
691 can_parse = true;
692 break;
693
694 case HeaderMagic32Swapped:
695 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
696 m_data.SetAddressByteSize(4);
697 can_parse = true;
698 break;
699
700 case HeaderMagic64Swapped:
701 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
702 m_data.SetAddressByteSize(8);
703 can_parse = true;
704 break;
705
706 default:
707 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000708 }
Greg Clayton9482f052012-03-13 23:14:29 +0000709
710 if (can_parse)
711 {
712 m_data.GetU32(&offset, &m_header.cputype, 6);
713
714 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +0000715
Greg Clayton21a25432012-11-16 21:36:10 +0000716 // Check if the module has a required architecture
717 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000718 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000719 return false;
720
Greg Clayton9482f052012-03-13 23:14:29 +0000721 if (SetModulesArchitecture (mach_arch))
722 {
723 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
724 if (m_data.GetByteSize() < header_and_lc_size)
725 {
726 DataBufferSP data_sp;
727 ProcessSP process_sp (m_process_wp.lock());
728 if (process_sp)
729 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000730 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000731 }
732 else
733 {
734 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000735 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000736 if (data_sp->GetByteSize() != header_and_lc_size)
737 return false;
738 }
739 if (data_sp)
740 m_data.SetData (data_sp);
741 }
742 }
743 return true;
744 }
745 else
746 {
747 memset(&m_header, 0, sizeof(struct mach_header));
748 }
Chris Lattner24943d22010-06-08 16:52:24 +0000749 }
750 return false;
751}
752
753
754ByteOrder
755ObjectFileMachO::GetByteOrder () const
756{
Chris Lattner24943d22010-06-08 16:52:24 +0000757 return m_data.GetByteOrder ();
758}
759
Jim Ingham7508e732010-08-09 23:31:02 +0000760bool
761ObjectFileMachO::IsExecutable() const
762{
763 return m_header.filetype == HeaderFileTypeExecutable;
764}
Chris Lattner24943d22010-06-08 16:52:24 +0000765
Greg Clayton36da2aa2013-01-25 18:06:21 +0000766uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000767ObjectFileMachO::GetAddressByteSize () const
768{
Chris Lattner24943d22010-06-08 16:52:24 +0000769 return m_data.GetAddressByteSize ();
770}
771
Greg Claytonb3448432011-03-24 21:19:54 +0000772AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000773ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
774{
775 Symtab *symtab = GetSymtab();
776 if (symtab)
777 {
778 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
779 if (symbol)
780 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000781 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000782 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000783 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000784 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000785 {
Greg Clayton3508c382012-02-24 01:59:29 +0000786 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000787 switch (section_type)
788 {
789 case eSectionTypeInvalid: return eAddressClassUnknown;
790 case eSectionTypeCode:
791 if (m_header.cputype == llvm::MachO::CPUTypeARM)
792 {
793 // For ARM we have a bit in the n_desc field of the symbol
794 // that tells us ARM/Thumb which is bit 0x0008.
795 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
796 return eAddressClassCodeAlternateISA;
797 }
798 return eAddressClassCode;
799
800 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000801 case eSectionTypeData:
802 case eSectionTypeDataCString:
803 case eSectionTypeDataCStringPointers:
804 case eSectionTypeDataSymbolAddress:
805 case eSectionTypeData4:
806 case eSectionTypeData8:
807 case eSectionTypeData16:
808 case eSectionTypeDataPointers:
809 case eSectionTypeZeroFill:
810 case eSectionTypeDataObjCMessageRefs:
811 case eSectionTypeDataObjCCFStrings:
812 return eAddressClassData;
813 case eSectionTypeDebug:
814 case eSectionTypeDWARFDebugAbbrev:
815 case eSectionTypeDWARFDebugAranges:
816 case eSectionTypeDWARFDebugFrame:
817 case eSectionTypeDWARFDebugInfo:
818 case eSectionTypeDWARFDebugLine:
819 case eSectionTypeDWARFDebugLoc:
820 case eSectionTypeDWARFDebugMacInfo:
821 case eSectionTypeDWARFDebugPubNames:
822 case eSectionTypeDWARFDebugPubTypes:
823 case eSectionTypeDWARFDebugRanges:
824 case eSectionTypeDWARFDebugStr:
825 case eSectionTypeDWARFAppleNames:
826 case eSectionTypeDWARFAppleTypes:
827 case eSectionTypeDWARFAppleNamespaces:
828 case eSectionTypeDWARFAppleObjC:
829 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000830 case eSectionTypeEHFrame: return eAddressClassRuntime;
831 case eSectionTypeOther: return eAddressClassUnknown;
832 }
833 }
834 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000835
Greg Claytonb3448432011-03-24 21:19:54 +0000836 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000837 switch (symbol_type)
838 {
839 case eSymbolTypeAny: return eAddressClassUnknown;
840 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000841
Greg Claytonb1888f22011-03-19 01:12:21 +0000842 case eSymbolTypeCode:
843 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000844 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000845 if (m_header.cputype == llvm::MachO::CPUTypeARM)
846 {
847 // For ARM we have a bit in the n_desc field of the symbol
848 // that tells us ARM/Thumb which is bit 0x0008.
849 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
850 return eAddressClassCodeAlternateISA;
851 }
852 return eAddressClassCode;
853
854 case eSymbolTypeData: return eAddressClassData;
855 case eSymbolTypeRuntime: return eAddressClassRuntime;
856 case eSymbolTypeException: return eAddressClassRuntime;
857 case eSymbolTypeSourceFile: return eAddressClassDebug;
858 case eSymbolTypeHeaderFile: return eAddressClassDebug;
859 case eSymbolTypeObjectFile: return eAddressClassDebug;
860 case eSymbolTypeCommonBlock: return eAddressClassDebug;
861 case eSymbolTypeBlock: return eAddressClassDebug;
862 case eSymbolTypeLocal: return eAddressClassData;
863 case eSymbolTypeParam: return eAddressClassData;
864 case eSymbolTypeVariable: return eAddressClassData;
865 case eSymbolTypeVariableType: return eAddressClassDebug;
866 case eSymbolTypeLineEntry: return eAddressClassDebug;
867 case eSymbolTypeLineHeader: return eAddressClassDebug;
868 case eSymbolTypeScopeBegin: return eAddressClassDebug;
869 case eSymbolTypeScopeEnd: return eAddressClassDebug;
870 case eSymbolTypeAdditional: return eAddressClassUnknown;
871 case eSymbolTypeCompiler: return eAddressClassDebug;
872 case eSymbolTypeInstrumentation:return eAddressClassDebug;
873 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000874 case eSymbolTypeObjCClass: return eAddressClassRuntime;
875 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
876 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000877 }
878 }
879 }
880 return eAddressClassUnknown;
881}
Chris Lattner24943d22010-06-08 16:52:24 +0000882
883Symtab *
884ObjectFileMachO::GetSymtab()
885{
Greg Clayton9482f052012-03-13 23:14:29 +0000886 ModuleSP module_sp(GetModule());
887 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000888 {
Greg Clayton9482f052012-03-13 23:14:29 +0000889 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
890 if (m_symtab_ap.get() == NULL)
891 {
892 m_symtab_ap.reset(new Symtab(this));
893 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
894 ParseSymtab (true);
895 m_symtab_ap->Finalize ();
896 }
Chris Lattner24943d22010-06-08 16:52:24 +0000897 }
898 return m_symtab_ap.get();
899}
900
901
902SectionList *
903ObjectFileMachO::GetSectionList()
904{
Greg Clayton9482f052012-03-13 23:14:29 +0000905 ModuleSP module_sp(GetModule());
906 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000907 {
Greg Clayton9482f052012-03-13 23:14:29 +0000908 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
909 if (m_sections_ap.get() == NULL)
910 {
911 m_sections_ap.reset(new SectionList());
912 ParseSections();
913 }
Chris Lattner24943d22010-06-08 16:52:24 +0000914 }
915 return m_sections_ap.get();
916}
917
918
919size_t
920ObjectFileMachO::ParseSections ()
921{
922 lldb::user_id_t segID = 0;
923 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000924 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000925 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000926 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000927 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000928 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000929 // First look up any LC_ENCRYPTION_INFO load commands
930 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
931 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000932 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000933 for (i=0; i<m_header.ncmds; ++i)
934 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000935 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000936 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000937 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000938
Greg Clayton54e33712012-05-25 18:09:55 +0000939 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000940 {
Greg Clayton54e33712012-05-25 18:09:55 +0000941 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
942 {
943 if (encryption_cmd.cryptid != 0)
944 {
945 EncryptedFileRanges::Entry entry;
946 entry.SetRangeBase(encryption_cmd.cryptoff);
947 entry.SetByteSize(encryption_cmd.cryptsize);
948 encrypted_file_ranges.Append(entry);
949 }
950 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000951 }
Greg Clayton54e33712012-05-25 18:09:55 +0000952 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000953 }
954
955 offset = MachHeaderSizeFromMagic(m_header.magic);
956
Greg Clayton54e33712012-05-25 18:09:55 +0000957 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000958 for (i=0; i<m_header.ncmds; ++i)
959 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000960 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000961 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
962 break;
963
Greg Clayton1674b122010-07-21 22:12:05 +0000964 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000965 {
966 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
967 {
968 load_cmd.vmaddr = m_data.GetAddress(&offset);
969 load_cmd.vmsize = m_data.GetAddress(&offset);
970 load_cmd.fileoff = m_data.GetAddress(&offset);
971 load_cmd.filesize = m_data.GetAddress(&offset);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000972 if (m_length != 0 && load_cmd.filesize != 0)
973 {
Greg Claytonbb759862013-04-16 16:51:19 +0000974 if (load_cmd.fileoff > m_length)
975 {
976 // We have a load command that says it extends past the end of hte file. This is likely
977 // a corrupt file. We don't have any way to return an error condition here (this method
978 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
979 // is null out the SectionList vector and if a process has been set up, dump a message
980 // to stdout. The most common case here is core file debugging with a truncated file.
981 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
982 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 ")",
983 i,
984 lc_segment_name,
985 load_cmd.fileoff,
986 m_length);
987 m_sections_ap->Clear();
988 return 0;
989 }
990
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000991 if (load_cmd.fileoff + load_cmd.filesize > m_length)
992 {
993 // We have a load command that says it extends past the end of hte file. This is likely
994 // a corrupt file. We don't have any way to return an error condition here (this method
995 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
996 // is null out the SectionList vector and if a process has been set up, dump a message
Greg Claytonbb759862013-04-16 16:51:19 +0000997 // to stdout. The most common case here is core file debugging with a truncated file.
998 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
999 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 ")",
1000 i,
1001 lc_segment_name,
1002 load_cmd.fileoff + load_cmd.filesize,
1003 m_length);
Jason Molendaadf9e3d2013-04-10 05:58:57 +00001004 m_sections_ap->Clear();
1005 return 0;
1006 }
1007 }
Chris Lattner24943d22010-06-08 16:52:24 +00001008 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1009 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001010
Greg Clayton68ca8232011-01-25 02:58:48 +00001011 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
1012
Chris Lattner24943d22010-06-08 16:52:24 +00001013 // Keep a list of mach segments around in case we need to
1014 // get at data that isn't stored in the abstracted Sections.
1015 m_mach_segments.push_back (load_cmd);
1016
Greg Clayton36da2aa2013-01-25 18:06:21 +00001017 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 +00001018 // Use a segment ID of the segment index shifted left by 8 so they
1019 // never conflict with any of the sections.
1020 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +00001021 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +00001022 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001023 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +00001024 ++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
1025 segment_name, // Name of this section
1026 eSectionTypeContainer, // This section is a container of other sections.
1027 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1028 load_cmd.vmsize, // VM size in bytes of this section
1029 load_cmd.fileoff, // Offset to the data for this section in the file
1030 load_cmd.filesize, // Size in bytes of this section as found in the the file
1031 load_cmd.flags)); // Flags for this section
1032
Greg Clayton68ca8232011-01-25 02:58:48 +00001033 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001034 m_sections_ap->AddSection(segment_sp);
1035 }
1036
1037 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +00001038 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +00001039 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +00001040 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +00001041 // mach sections yet...
1042 if (m_mach_sections.empty())
1043 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +00001044 uint32_t segment_sect_idx;
1045 const lldb::user_id_t first_segment_sectID = sectID + 1;
1046
1047
Greg Clayton1674b122010-07-21 22:12:05 +00001048 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +00001049 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
1050 {
1051 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
1052 break;
1053 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1054 break;
1055 sect64.addr = m_data.GetAddress(&offset);
1056 sect64.size = m_data.GetAddress(&offset);
1057
1058 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1059 break;
1060
1061 // Keep a list of mach sections around in case we need to
1062 // get at data that isn't stored in the abstracted Sections.
1063 m_mach_sections.push_back (sect64);
1064
1065 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1066 if (!segment_name)
1067 {
1068 // We have a segment with no name so we need to conjure up
1069 // segments that correspond to the section's segname if there
1070 // isn't already such a section. If there is such a section,
1071 // we resize the section so that it spans all sections.
1072 // We also mark these sections as fake so address matches don't
1073 // hit if they land in the gaps between the child sections.
1074 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1075 segment_sp = m_sections_ap->FindSectionByName (segment_name);
1076 if (segment_sp.get())
1077 {
1078 Section *segment = segment_sp.get();
1079 // Grow the section size as needed.
1080 const lldb::addr_t sect64_min_addr = sect64.addr;
1081 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1082 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1083 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1084 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1085 if (sect64_min_addr >= curr_seg_min_addr)
1086 {
1087 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1088 // Only grow the section size if needed
1089 if (new_seg_byte_size > curr_seg_byte_size)
1090 segment->SetByteSize (new_seg_byte_size);
1091 }
1092 else
1093 {
1094 // We need to change the base address of the segment and
1095 // adjust the child section offsets for all existing children.
1096 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1097 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +00001098 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +00001099 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1100 }
Greg Clayton661825b2010-06-28 23:51:11 +00001101
1102 // Grow the section size as needed.
1103 if (sect64.offset)
1104 {
1105 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1106 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1107
1108 const lldb::addr_t section_min_file_offset = sect64.offset;
1109 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1110 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1111 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1112 segment->SetFileOffset (new_file_offset);
1113 segment->SetFileSize (new_file_size);
1114 }
Chris Lattner24943d22010-06-08 16:52:24 +00001115 }
1116 else
1117 {
1118 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +00001119 segment_sp.reset(new Section (segment_sp, // Parent section
1120 module_sp, // Module to which this section belongs
1121 ++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
1122 segment_name, // Name of this section
1123 eSectionTypeContainer, // This section is a container of other sections.
1124 sect64.addr, // File VM address == addresses as they are found in the object file
1125 sect64.size, // VM size in bytes of this section
1126 sect64.offset, // Offset to the data for this section in the file
1127 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1128 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +00001129 segment_sp->SetIsFake(true);
1130 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +00001131 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001132 }
1133 }
1134 assert (segment_sp.get());
1135
Greg Clayton1674b122010-07-21 22:12:05 +00001136 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +00001137 static ConstString g_sect_name_objc_data ("__objc_data");
1138 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1139 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1140 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1141 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1142 static ConstString g_sect_name_objc_const ("__objc_const");
1143 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1144 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001145
1146 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1147 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1148 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1149 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1150 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1151 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1152 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1153 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1154 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1155 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1156 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +00001157 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1158 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +00001159 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +00001160 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001161 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +00001162 static ConstString g_sect_name_DATA ("__DATA");
1163 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001164
Chris Lattner24943d22010-06-08 16:52:24 +00001165 SectionType sect_type = eSectionTypeOther;
1166
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001167 if (section_name == g_sect_name_dwarf_debug_abbrev)
1168 sect_type = eSectionTypeDWARFDebugAbbrev;
1169 else if (section_name == g_sect_name_dwarf_debug_aranges)
1170 sect_type = eSectionTypeDWARFDebugAranges;
1171 else if (section_name == g_sect_name_dwarf_debug_frame)
1172 sect_type = eSectionTypeDWARFDebugFrame;
1173 else if (section_name == g_sect_name_dwarf_debug_info)
1174 sect_type = eSectionTypeDWARFDebugInfo;
1175 else if (section_name == g_sect_name_dwarf_debug_line)
1176 sect_type = eSectionTypeDWARFDebugLine;
1177 else if (section_name == g_sect_name_dwarf_debug_loc)
1178 sect_type = eSectionTypeDWARFDebugLoc;
1179 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1180 sect_type = eSectionTypeDWARFDebugMacInfo;
1181 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1182 sect_type = eSectionTypeDWARFDebugPubNames;
1183 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1184 sect_type = eSectionTypeDWARFDebugPubTypes;
1185 else if (section_name == g_sect_name_dwarf_debug_ranges)
1186 sect_type = eSectionTypeDWARFDebugRanges;
1187 else if (section_name == g_sect_name_dwarf_debug_str)
1188 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001189 else if (section_name == g_sect_name_dwarf_apple_names)
1190 sect_type = eSectionTypeDWARFAppleNames;
1191 else if (section_name == g_sect_name_dwarf_apple_types)
1192 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001193 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1194 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001195 else if (section_name == g_sect_name_dwarf_apple_objc)
1196 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001197 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001198 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001199 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001200 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001201 else if (section_name == g_sect_name_eh_frame)
1202 sect_type = eSectionTypeEHFrame;
1203 else if (section_name == g_sect_name_cfstring)
1204 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001205 else if (section_name == g_sect_name_objc_data ||
1206 section_name == g_sect_name_objc_classrefs ||
1207 section_name == g_sect_name_objc_superrefs ||
1208 section_name == g_sect_name_objc_const ||
1209 section_name == g_sect_name_objc_classlist)
1210 {
1211 sect_type = eSectionTypeDataPointers;
1212 }
Chris Lattner24943d22010-06-08 16:52:24 +00001213
1214 if (sect_type == eSectionTypeOther)
1215 {
1216 switch (mach_sect_type)
1217 {
1218 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001219 case SectionTypeRegular:
1220 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001221 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001222 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001223 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001224 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001225 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001226 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001227 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1228 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1229 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1230 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1231 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1232 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1233 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1234 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1235 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1236 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1237 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1238 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1239 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1240 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1241 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1242 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001243 default: break;
1244 }
1245 }
1246
Greg Clayton3508c382012-02-24 01:59:29 +00001247 SectionSP section_sp(new Section (segment_sp,
1248 module_sp,
1249 ++sectID,
1250 section_name,
1251 sect_type,
1252 sect64.addr - segment_sp->GetFileAddress(),
1253 sect64.size,
1254 sect64.offset,
1255 sect64.offset == 0 ? 0 : sect64.size,
1256 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001257 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001258
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001259 bool section_is_encrypted = false;
1260 if (!segment_is_encrypted && load_cmd.filesize != 0)
1261 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001262
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001263 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001264 segment_sp->GetChildren().AddSection(section_sp);
1265
1266 if (segment_sp->IsFake())
1267 {
1268 segment_sp.reset();
1269 segment_name.Clear();
1270 }
1271 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001272 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001273 {
1274 if (first_segment_sectID <= sectID)
1275 {
1276 lldb::user_id_t sect_uid;
1277 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1278 {
1279 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1280 SectionSP next_section_sp;
1281 if (sect_uid + 1 <= sectID)
1282 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1283
1284 if (curr_section_sp.get())
1285 {
1286 if (curr_section_sp->GetByteSize() == 0)
1287 {
1288 if (next_section_sp.get() != NULL)
1289 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1290 else
1291 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1292 }
1293 }
1294 }
1295 }
1296 }
1297 }
1298 }
1299 }
Greg Clayton1674b122010-07-21 22:12:05 +00001300 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001301 {
1302 m_dysymtab.cmd = load_cmd.cmd;
1303 m_dysymtab.cmdsize = load_cmd.cmdsize;
1304 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1305 }
1306
1307 offset = load_cmd_offset + load_cmd.cmdsize;
1308 }
1309// if (dump_sections)
1310// {
1311// StreamFile s(stdout);
1312// m_sections_ap->Dump(&s, true);
1313// }
1314 return sectID; // Return the number of sections we registered with the module
1315}
1316
1317class MachSymtabSectionInfo
1318{
1319public:
1320
1321 MachSymtabSectionInfo (SectionList *section_list) :
1322 m_section_list (section_list),
1323 m_section_infos()
1324 {
1325 // Get the number of sections down to a depth of 1 to include
1326 // all segments and their sections, but no other sections that
1327 // may be added for debug map or
1328 m_section_infos.resize(section_list->GetNumSections(1));
1329 }
1330
1331
Greg Clayton3508c382012-02-24 01:59:29 +00001332 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001333 GetSection (uint8_t n_sect, addr_t file_addr)
1334 {
1335 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001336 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001337 if (n_sect < m_section_infos.size())
1338 {
Greg Clayton3508c382012-02-24 01:59:29 +00001339 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001340 {
Greg Clayton3508c382012-02-24 01:59:29 +00001341 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1342 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001343 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001344 {
Greg Clayton3508c382012-02-24 01:59:29 +00001345 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1346 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001347 }
1348 else
1349 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001350 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001351 }
Chris Lattner24943d22010-06-08 16:52:24 +00001352 }
1353 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001354 {
1355 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001356 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001357 }
1358 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1359 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1360 {
1361 // Symbol is in section with zero size, but has the same start
1362 // address as the section. This can happen with linker symbols
1363 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001364 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001365 }
Chris Lattner24943d22010-06-08 16:52:24 +00001366 }
Greg Clayton3508c382012-02-24 01:59:29 +00001367 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001368 }
1369
1370protected:
1371 struct SectionInfo
1372 {
1373 SectionInfo () :
1374 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001375 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001376 {
1377 }
1378
1379 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001380 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001381 };
1382 SectionList *m_section_list;
1383 std::vector<SectionInfo> m_section_infos;
1384};
1385
Chris Lattner24943d22010-06-08 16:52:24 +00001386size_t
1387ObjectFileMachO::ParseSymtab (bool minimize)
1388{
1389 Timer scoped_timer(__PRETTY_FUNCTION__,
1390 "ObjectFileMachO::ParseSymtab () module = %s",
1391 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001392 ModuleSP module_sp (GetModule());
1393 if (!module_sp)
1394 return 0;
1395
1396 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1397 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1398 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1399 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001400 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001401 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001402
Greg Clayton952e9dc2013-03-27 23:08:40 +00001403 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001404
Chris Lattner24943d22010-06-08 16:52:24 +00001405 for (i=0; i<m_header.ncmds; ++i)
1406 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001407 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001408 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001409 struct load_command lc;
1410 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001411 break;
1412 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001413 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001414 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001415 case LoadCommandSymtab:
1416 symtab_load_command.cmd = lc.cmd;
1417 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001418 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001419 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1420 return 0;
1421 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001422 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001423 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001424 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001425 return 0;
1426 }
1427
1428 if (symtab_load_command.stroff == 0)
1429 {
1430 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001431 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001432 return 0;
1433 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001434
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001435 if (symtab_load_command.nsyms == 0)
1436 {
1437 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001438 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001439 return 0;
1440 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001441
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001442 if (symtab_load_command.strsize == 0)
1443 {
1444 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001445 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001446 return 0;
1447 }
1448 break;
1449
1450 case LoadCommandFunctionStarts:
1451 function_starts_load_command.cmd = lc.cmd;
1452 function_starts_load_command.cmdsize = lc.cmdsize;
1453 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1454 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1455 break;
1456
1457 default:
1458 break;
1459 }
1460 offset = cmd_offset + lc.cmdsize;
1461 }
1462
1463 if (symtab_load_command.cmd)
1464 {
1465 Symtab *symtab = m_symtab_ap.get();
1466 SectionList *section_list = GetSectionList();
1467 if (section_list == NULL)
1468 return 0;
1469
1470 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001471 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001472
Greg Clayton36da2aa2013-01-25 18:06:21 +00001473 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1474 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001475 bool bit_width_32 = addr_byte_size == 4;
1476 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1477
Greg Clayton36da2aa2013-01-25 18:06:21 +00001478 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1479 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1480 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001481 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001482
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001483 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1484 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001485 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1486 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001487 {
Greg Claytondd29b972012-05-18 23:20:01 +00001488 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001489 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1490 // Reading mach file from memory in a process or core file...
1491
1492 if (linkedit_section_sp)
1493 {
1494 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1495 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1496 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001497 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001498
1499 bool data_was_read = false;
1500
1501#if defined (__APPLE__) && defined (__arm__)
1502 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001503 {
Greg Clayton29021d32012-04-18 05:19:20 +00001504 // This mach-o memory file is in the dyld shared cache. If this
1505 // program is not remote and this is iOS, then this process will
1506 // share the same shared cache as the process we are debugging and
1507 // we can read the entire __LINKEDIT from the address space in this
1508 // process. This is a needed optimization that is used for local iOS
1509 // debugging only since all shared libraries in the shared cache do
1510 // not have corresponding files that exist in the file system of the
1511 // device. They have been combined into a single file. This means we
1512 // always have to load these files from memory. All of the symbol and
1513 // string tables from all of the __LINKEDIT sections from the shared
1514 // libraries in the shared cache have been merged into a single large
1515 // symbol and string table. Reading all of this symbol and string table
1516 // data across can slow down debug launch times, so we optimize this by
1517 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda45c75502013-04-16 06:24:42 +00001518
1519 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1520 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1521 bool use_lldb_cache = true;
1522 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1523 {
1524 use_lldb_cache = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001525 ModuleSP module_sp (GetModule());
1526 if (module_sp)
1527 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1528
Jason Molenda45c75502013-04-16 06:24:42 +00001529 }
1530
Greg Clayton29021d32012-04-18 05:19:20 +00001531 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda45c75502013-04-16 06:24:42 +00001532 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton29021d32012-04-18 05:19:20 +00001533 {
1534 data_was_read = true;
1535 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001536 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
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 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1541 }
1542 }
1543 }
1544#endif
1545
1546 if (!data_was_read)
1547 {
1548 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1549 if (nlist_data_sp)
1550 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001551 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1552 //if (strtab_data_sp)
1553 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001554 if (m_dysymtab.nindirectsyms != 0)
1555 {
1556 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1557 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1558 if (indirect_syms_data_sp)
1559 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1560 }
Greg Clayton29021d32012-04-18 05:19:20 +00001561 if (function_starts_load_command.cmd)
1562 {
1563 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1564 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1565 if (func_start_data_sp)
1566 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1567 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001568 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001569 }
1570 }
1571 else
1572 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001573 nlist_data.SetData (m_data,
1574 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001575 nlist_data_byte_size);
1576 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001577 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001578 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001579 if (m_dysymtab.nindirectsyms != 0)
1580 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001581 indirect_symbol_index_data.SetData (m_data,
1582 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001583 m_dysymtab.nindirectsyms * 4);
1584 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001585 if (function_starts_load_command.cmd)
1586 {
1587 function_starts_data.SetData (m_data,
1588 function_starts_load_command.dataoff,
1589 function_starts_load_command.datasize);
1590 }
1591 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001592
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001593 if (nlist_data.GetByteSize() == 0)
1594 {
1595 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001596 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001597 return 0;
1598 }
1599
1600
Greg Clayton3a5dc012012-05-25 17:04:00 +00001601 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1602 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001603 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001604 if (process)
1605 {
1606 if (strtab_addr == LLDB_INVALID_ADDRESS)
1607 {
1608 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001609 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001610 return 0;
1611 }
1612 }
1613 else
Greg Claytondd29b972012-05-18 23:20:01 +00001614 {
1615 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001616 module_sp->LogMessage(log, "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001617 return 0;
1618 }
1619 }
Greg Claytondd29b972012-05-18 23:20:01 +00001620
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001621 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1622 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1623 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1624 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1625 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1626 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1627 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1628 SectionSP eh_frame_section_sp;
1629 if (text_section_sp.get())
1630 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1631 else
1632 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1633
Greg Claytond2653c22012-03-14 01:53:24 +00001634 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001635
1636 // lldb works best if it knows the start addresss of all functions in a module.
1637 // Linker symbols or debug info are normally the best source of information for start addr / size but
1638 // they may be stripped in a released binary.
Jason Molenda2120aef2013-04-16 00:18:44 +00001639 // Two additional sources of information exist in Mach-O binaries:
Jason Molendad7938392013-03-21 03:36:01 +00001640 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1641 // binary, relative to the text section.
1642 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1643 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1644 // Binaries built to run on older releases may need to use eh_frame information.
1645
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001646 if (text_section_sp && function_starts_data.GetByteSize())
1647 {
1648 FunctionStarts::Entry function_start_entry;
1649 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001650 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001651 function_start_entry.addr = text_section_sp->GetFileAddress();
1652 uint64_t delta;
1653 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1654 {
1655 // Now append the current entry
1656 function_start_entry.addr += delta;
1657 function_starts.Append(function_start_entry);
1658 }
Jason Molenda2120aef2013-04-16 00:18:44 +00001659 }
Jason Molendad7938392013-03-21 03:36:01 +00001660 else
1661 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001662 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1663 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1664 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1665 // the module.
1666 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001667 {
1668 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1669 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1670 eh_frame.GetFunctionAddressAndSizeVector (functions);
1671 addr_t text_base_addr = text_section_sp->GetFileAddress();
1672 size_t count = functions.GetSize();
1673 for (size_t i = 0; i < count; ++i)
1674 {
1675 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1676 if (func)
1677 {
1678 FunctionStarts::Entry function_start_entry;
1679 function_start_entry.addr = func->base - text_base_addr;
1680 function_starts.Append(function_start_entry);
1681 }
1682 }
1683 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001684 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001685
Greg Clayton36da2aa2013-01-25 18:06:21 +00001686 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001687
Greg Clayton36da2aa2013-01-25 18:06:21 +00001688 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 +00001689
Greg Clayton36da2aa2013-01-25 18:06:21 +00001690 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001691
1692 uint32_t N_SO_index = UINT32_MAX;
1693
1694 MachSymtabSectionInfo section_info (section_list);
1695 std::vector<uint32_t> N_FUN_indexes;
1696 std::vector<uint32_t> N_NSYM_indexes;
1697 std::vector<uint32_t> N_INCL_indexes;
1698 std::vector<uint32_t> N_BRAC_indexes;
1699 std::vector<uint32_t> N_COMM_indexes;
1700 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1701 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1702 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1703 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1704 // Any symbols that get merged into another will get an entry
1705 // in this map so we know
1706 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1707 uint32_t nlist_idx = 0;
1708 Symbol *symbol_ptr = NULL;
1709
1710 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001711 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001712 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001713 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001714 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001715
Jason Molendab62abd52012-06-21 01:51:02 +00001716#if defined (__APPLE__) && defined (__arm__)
1717
1718 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1719 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1720 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1721 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1722 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1723 // nlist parser to ignore all LOCAL symbols.
1724
1725 if (m_header.flags & 0x80000000u)
1726 {
1727 // Before we can start mapping the DSC, we need to make certain the target process is actually
1728 // using the cache we can find.
1729
Jason Molendab62abd52012-06-21 01:51:02 +00001730 // Next we need to determine the correct path for the dyld shared cache.
1731
1732 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1733 char dsc_path[PATH_MAX];
1734
1735 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001736 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1737 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001738 header_arch.GetArchitectureName());
1739
1740 FileSpec dsc_filespec(dsc_path, false);
1741
1742 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda2120aef2013-04-16 00:18:44 +00001743 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001744 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001745 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1746 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1747 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001748 uint32_t imagesOffset;
1749 uint32_t imagesCount;
1750 uint64_t dyldBaseAddress;
1751 uint64_t codeSignatureOffset;
1752 uint64_t codeSignatureSize;
1753 uint64_t slideInfoOffset;
1754 uint64_t slideInfoSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001755 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1756 uint64_t localSymbolsSize; // size of local symbols information
1757 };
1758 struct lldb_copy_dyld_cache_header_v1
1759 {
1760 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1761 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1762 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1763 uint32_t imagesOffset;
1764 uint32_t imagesCount;
1765 uint64_t dyldBaseAddress;
1766 uint64_t codeSignatureOffset;
1767 uint64_t codeSignatureSize;
1768 uint64_t slideInfoOffset;
1769 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001770 uint64_t localSymbolsOffset;
1771 uint64_t localSymbolsSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001772 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001773 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001774
Jason Molenda2120aef2013-04-16 00:18:44 +00001775 struct lldb_copy_dyld_cache_mapping_info
1776 {
1777 uint64_t address;
1778 uint64_t size;
1779 uint64_t fileOffset;
1780 uint32_t maxProt;
1781 uint32_t initProt;
1782 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001783
Greg Claytonab77dcb2012-09-05 22:30:51 +00001784 struct lldb_copy_dyld_cache_local_symbols_info
1785 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001786 uint32_t nlistOffset;
1787 uint32_t nlistCount;
1788 uint32_t stringsOffset;
1789 uint32_t stringsSize;
1790 uint32_t entriesOffset;
1791 uint32_t entriesCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001792 };
1793 struct lldb_copy_dyld_cache_local_symbols_entry
1794 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001795 uint32_t dylibOffset;
1796 uint32_t nlistStartIndex;
1797 uint32_t nlistCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001798 };
Jason Molendab62abd52012-06-21 01:51:02 +00001799
Jason Molendafd3b35d2012-06-22 03:28:35 +00001800 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1801 The dyld_cache_local_symbols_info structure gives us three things:
1802 1. The start and count of the nlist records in the dyld_shared_cache file
1803 2. The start and size of the strings for these nlist records
1804 3. The start and count of dyld_cache_local_symbols_entry entries
1805
1806 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1807 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001808 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001809 and the count of how many nlist records there are for this dylib/framework.
1810 */
1811
Jason Molendab62abd52012-06-21 01:51:02 +00001812 // Process the dsc header to find the unmapped symbols
1813 //
1814 // Save some VM space, do not map the entire cache in one shot.
1815
Jason Molenda6bcabae2013-03-06 23:17:36 +00001816 DataBufferSP dsc_data_sp;
1817 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1818
1819 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001820 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001821 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001822
Jason Molenda6bcabae2013-03-06 23:17:36 +00001823 char version_str[17];
1824 int version = -1;
1825 lldb::offset_t offset = 0;
1826 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1827 version_str[16] = '\0';
1828 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1829 {
1830 int v;
1831 if (::sscanf (version_str + 6, "%d", &v) == 1)
1832 {
1833 version = v;
1834 }
1835 }
1836
Jason Molenda45c75502013-04-16 06:24:42 +00001837 UUID dsc_uuid;
1838 if (version >= 1)
1839 {
1840 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1841 uint8_t uuid_bytes[sizeof (uuid_t)];
1842 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1843 dsc_uuid.SetBytes (uuid_bytes);
1844 }
1845
1846 bool uuid_match = true;
1847 if (dsc_uuid.IsValid() && process)
1848 {
1849 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1850
1851 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1852 {
1853 // The on-disk dyld_shared_cache file is not the same as the one in this
1854 // process' memory, don't use it.
1855 uuid_match = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001856 ModuleSP module_sp (GetModule());
1857 if (module_sp)
1858 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 +00001859 }
1860 }
1861
Jason Molenda9badb6c2013-03-06 23:19:17 +00001862 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001863
Jason Molendab62abd52012-06-21 01:51:02 +00001864 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1865
1866 // If the mappingOffset points to a location inside the header, we've
1867 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda45c75502013-04-16 06:24:42 +00001868 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendab62abd52012-06-21 01:51:02 +00001869 {
1870
Jason Molenda6bcabae2013-03-06 23:17:36 +00001871 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1872 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1873 offset = 0;
1874
1875 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1876 // in the shared library cache need to be adjusted by an offset to match up with the
1877 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1878 // recorded in mapping_offset_value.
1879 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1880
1881 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001882 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1883 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1884
Jason Molenda9badb6c2013-03-06 23:19:17 +00001885 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001886 {
1887 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001888 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001889 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001890 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001891
1892 offset = 0;
1893
1894 // Read the local_symbols_infos struct in one shot
1895 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1896 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1897
Jason Molendab62abd52012-06-21 01:51:02 +00001898 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1899
Jason Molenda6bcabae2013-03-06 23:17:36 +00001900 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001901
1902 offset = local_symbols_info.entriesOffset;
1903 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1904 {
1905 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1906 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1907 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1908 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1909
Jason Molenda9badb6c2013-03-06 23:19:17 +00001910 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001911 {
1912 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1913
1914 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1915 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1916 num_syms = symtab->GetNumSymbols();
1917
1918 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1919 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1920
Jason Molenda9badb6c2013-03-06 23:19:17 +00001921 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001922 {
1923 /////////////////////////////
1924 {
1925 struct nlist_64 nlist;
1926 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1927 break;
1928
1929 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1930 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1931 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1932 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1933 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1934
1935 SymbolType type = eSymbolTypeInvalid;
1936 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1937
1938 if (symbol_name == NULL)
1939 {
1940 // No symbol should be NULL, even the symbols with no
1941 // string values should have an offset zero which points
1942 // to an empty C-string
1943 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00001944 "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 +00001945 entry_index,
1946 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00001947 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendab62abd52012-06-21 01:51:02 +00001948 continue;
1949 }
1950 if (symbol_name[0] == '\0')
1951 symbol_name = NULL;
1952
1953 const char *symbol_name_non_abi_mangled = NULL;
1954
1955 SectionSP symbol_section;
1956 uint32_t symbol_byte_size = 0;
1957 bool add_nlist = true;
1958 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001959 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001960
1961 assert (sym_idx < num_syms);
1962
1963 sym[sym_idx].SetDebug (is_debug);
1964
1965 if (is_debug)
1966 {
1967 switch (nlist.n_type)
1968 {
1969 case StabGlobalSymbol:
1970 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1971 // Sometimes the N_GSYM value contains the address.
1972
1973 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1974 // have the same address, but we want to ensure that we always find only the real symbol,
1975 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1976 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1977 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1978 // same address.
1979
1980 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1981 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1982 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1983 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1984 add_nlist = false;
1985 else
1986 {
1987 sym[sym_idx].SetExternal(true);
1988 if (nlist.n_value != 0)
1989 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1990 type = eSymbolTypeData;
1991 }
1992 break;
1993
1994 case StabFunctionName:
1995 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
1996 type = eSymbolTypeCompiler;
1997 break;
1998
1999 case StabFunction:
2000 // N_FUN -- procedure: name,,n_sect,linenumber,address
2001 if (symbol_name)
2002 {
2003 type = eSymbolTypeCode;
2004 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2005
2006 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2007 // We use the current number of symbols in the symbol table in lieu of
2008 // using nlist_idx in case we ever start trimming entries out
2009 N_FUN_indexes.push_back(sym_idx);
2010 }
2011 else
2012 {
2013 type = eSymbolTypeCompiler;
2014
2015 if ( !N_FUN_indexes.empty() )
2016 {
2017 // Copy the size of the function into the original STAB entry so we don't have
2018 // to hunt for it later
2019 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2020 N_FUN_indexes.pop_back();
2021 // We don't really need the end function STAB as it contains the size which
2022 // we already placed with the original symbol, so don't add it if we want a
2023 // minimal symbol table
2024 if (minimize)
2025 add_nlist = false;
2026 }
2027 }
2028 break;
2029
2030 case StabStaticSymbol:
2031 // N_STSYM -- static symbol: name,,n_sect,type,address
2032 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2033 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2034 type = eSymbolTypeData;
2035 break;
2036
2037 case StabLocalCommon:
2038 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2039 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2040 type = eSymbolTypeCommonBlock;
2041 break;
2042
2043 case StabBeginSymbol:
2044 // N_BNSYM
2045 // We use the current number of symbols in the symbol table in lieu of
2046 // using nlist_idx in case we ever start trimming entries out
2047 if (minimize)
2048 {
2049 // Skip these if we want minimal symbol tables
2050 add_nlist = false;
2051 }
2052 else
2053 {
2054 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2055 N_NSYM_indexes.push_back(sym_idx);
2056 type = eSymbolTypeScopeBegin;
2057 }
2058 break;
2059
2060 case StabEndSymbol:
2061 // N_ENSYM
2062 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2063 // so that we can always skip the entire symbol if we need to navigate
2064 // more quickly at the source level when parsing STABS
2065 if (minimize)
2066 {
2067 // Skip these if we want minimal symbol tables
2068 add_nlist = false;
2069 }
2070 else
2071 {
2072 if ( !N_NSYM_indexes.empty() )
2073 {
2074 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2075 symbol_ptr->SetByteSize(sym_idx + 1);
2076 symbol_ptr->SetSizeIsSibling(true);
2077 N_NSYM_indexes.pop_back();
2078 }
2079 type = eSymbolTypeScopeEnd;
2080 }
2081 break;
2082
2083
2084 case StabSourceFileOptions:
2085 // N_OPT - emitted with gcc2_compiled and in gcc source
2086 type = eSymbolTypeCompiler;
2087 break;
2088
2089 case StabRegisterSymbol:
2090 // N_RSYM - register sym: name,,NO_SECT,type,register
2091 type = eSymbolTypeVariable;
2092 break;
2093
2094 case StabSourceLine:
2095 // N_SLINE - src line: 0,,n_sect,linenumber,address
2096 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2097 type = eSymbolTypeLineEntry;
2098 break;
2099
2100 case StabStructureType:
2101 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2102 type = eSymbolTypeVariableType;
2103 break;
2104
2105 case StabSourceFileName:
2106 // N_SO - source file name
2107 type = eSymbolTypeSourceFile;
2108 if (symbol_name == NULL)
2109 {
2110 if (minimize)
2111 add_nlist = false;
2112 if (N_SO_index != UINT32_MAX)
2113 {
2114 // Set the size of the N_SO to the terminating index of this N_SO
2115 // so that we can always skip the entire N_SO if we need to navigate
2116 // more quickly at the source level when parsing STABS
2117 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2118 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2119 symbol_ptr->SetSizeIsSibling(true);
2120 }
2121 N_NSYM_indexes.clear();
2122 N_INCL_indexes.clear();
2123 N_BRAC_indexes.clear();
2124 N_COMM_indexes.clear();
2125 N_FUN_indexes.clear();
2126 N_SO_index = UINT32_MAX;
2127 }
2128 else
2129 {
2130 // We use the current number of symbols in the symbol table in lieu of
2131 // using nlist_idx in case we ever start trimming entries out
2132 const bool N_SO_has_full_path = symbol_name[0] == '/';
2133 if (N_SO_has_full_path)
2134 {
2135 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2136 {
2137 // We have two consecutive N_SO entries where the first contains a directory
2138 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00002139 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002140 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2141 add_nlist = false;
2142 }
2143 else
2144 {
2145 // This is the first entry in a N_SO that contains a directory or
2146 // a full path to the source file
2147 N_SO_index = sym_idx;
2148 }
2149 }
2150 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2151 {
2152 // This is usually the second N_SO entry that contains just the filename,
2153 // so here we combine it with the first one if we are minimizing the symbol table
2154 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2155 if (so_path && so_path[0])
2156 {
2157 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002158 const size_t double_slash_pos = full_so_path.find("//");
2159 if (double_slash_pos != std::string::npos)
2160 {
2161 // The linker has been generating bad N_SO entries with doubled up paths
2162 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2163 // and the second is the directory for the source file so you end up with
2164 // a path that looks like "/tmp/src//tmp/src/"
2165 FileSpec so_dir(so_path, false);
2166 if (!so_dir.Exists())
2167 {
2168 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2169 if (so_dir.Exists())
2170 {
2171 // Trim off the incorrect path
2172 full_so_path.erase(0, double_slash_pos + 1);
2173 }
2174 }
2175 }
Jason Molendab62abd52012-06-21 01:51:02 +00002176 if (*full_so_path.rbegin() != '/')
2177 full_so_path += '/';
2178 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00002179 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002180 add_nlist = false;
2181 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2182 }
2183 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002184 else
2185 {
2186 // This could be a relative path to a N_SO
2187 N_SO_index = sym_idx;
2188 }
Jason Molendab62abd52012-06-21 01:51:02 +00002189 }
Jason Molendab62abd52012-06-21 01:51:02 +00002190 break;
2191
2192 case StabObjectFileName:
2193 // N_OSO - object file name: name,,0,0,st_mtime
2194 type = eSymbolTypeObjectFile;
2195 break;
2196
2197 case StabLocalSymbol:
2198 // N_LSYM - local sym: name,,NO_SECT,type,offset
2199 type = eSymbolTypeLocal;
2200 break;
2201
2202 //----------------------------------------------------------------------
2203 // INCL scopes
2204 //----------------------------------------------------------------------
2205 case StabBeginIncludeFileName:
2206 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2207 // We use the current number of symbols in the symbol table in lieu of
2208 // using nlist_idx in case we ever start trimming entries out
2209 N_INCL_indexes.push_back(sym_idx);
2210 type = eSymbolTypeScopeBegin;
2211 break;
2212
2213 case StabEndIncludeFile:
2214 // N_EINCL - include file end: name,,NO_SECT,0,0
2215 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2216 // so that we can always skip the entire symbol if we need to navigate
2217 // more quickly at the source level when parsing STABS
2218 if ( !N_INCL_indexes.empty() )
2219 {
2220 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2221 symbol_ptr->SetByteSize(sym_idx + 1);
2222 symbol_ptr->SetSizeIsSibling(true);
2223 N_INCL_indexes.pop_back();
2224 }
2225 type = eSymbolTypeScopeEnd;
2226 break;
2227
2228 case StabIncludeFileName:
2229 // N_SOL - #included file name: name,,n_sect,0,address
2230 type = eSymbolTypeHeaderFile;
2231
2232 // We currently don't use the header files on darwin
2233 if (minimize)
2234 add_nlist = false;
2235 break;
2236
2237 case StabCompilerParameters:
2238 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2239 type = eSymbolTypeCompiler;
2240 break;
2241
2242 case StabCompilerVersion:
2243 // N_VERSION - compiler version: name,,NO_SECT,0,0
2244 type = eSymbolTypeCompiler;
2245 break;
2246
2247 case StabCompilerOptLevel:
2248 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2249 type = eSymbolTypeCompiler;
2250 break;
2251
2252 case StabParameter:
2253 // N_PSYM - parameter: name,,NO_SECT,type,offset
2254 type = eSymbolTypeVariable;
2255 break;
2256
2257 case StabAlternateEntry:
2258 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2259 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2260 type = eSymbolTypeLineEntry;
2261 break;
2262
2263 //----------------------------------------------------------------------
2264 // Left and Right Braces
2265 //----------------------------------------------------------------------
2266 case StabLeftBracket:
2267 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2268 // We use the current number of symbols in the symbol table in lieu of
2269 // using nlist_idx in case we ever start trimming entries out
2270 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2271 N_BRAC_indexes.push_back(sym_idx);
2272 type = eSymbolTypeScopeBegin;
2273 break;
2274
2275 case StabRightBracket:
2276 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2277 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2278 // so that we can always skip the entire symbol if we need to navigate
2279 // more quickly at the source level when parsing STABS
2280 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2281 if ( !N_BRAC_indexes.empty() )
2282 {
2283 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2284 symbol_ptr->SetByteSize(sym_idx + 1);
2285 symbol_ptr->SetSizeIsSibling(true);
2286 N_BRAC_indexes.pop_back();
2287 }
2288 type = eSymbolTypeScopeEnd;
2289 break;
2290
2291 case StabDeletedIncludeFile:
2292 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2293 type = eSymbolTypeHeaderFile;
2294 break;
2295
2296 //----------------------------------------------------------------------
2297 // COMM scopes
2298 //----------------------------------------------------------------------
2299 case StabBeginCommon:
2300 // N_BCOMM - begin common: name,,NO_SECT,0,0
2301 // We use the current number of symbols in the symbol table in lieu of
2302 // using nlist_idx in case we ever start trimming entries out
2303 type = eSymbolTypeScopeBegin;
2304 N_COMM_indexes.push_back(sym_idx);
2305 break;
2306
2307 case StabEndCommonLocal:
2308 // N_ECOML - end common (local name): 0,,n_sect,0,address
2309 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2310 // Fall through
2311
2312 case StabEndCommon:
2313 // N_ECOMM - end common: name,,n_sect,0,0
2314 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2315 // so that we can always skip the entire symbol if we need to navigate
2316 // more quickly at the source level when parsing STABS
2317 if ( !N_COMM_indexes.empty() )
2318 {
2319 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2320 symbol_ptr->SetByteSize(sym_idx + 1);
2321 symbol_ptr->SetSizeIsSibling(true);
2322 N_COMM_indexes.pop_back();
2323 }
2324 type = eSymbolTypeScopeEnd;
2325 break;
2326
2327 case StabLength:
2328 // N_LENG - second stab entry with length information
2329 type = eSymbolTypeAdditional;
2330 break;
2331
2332 default: break;
2333 }
2334 }
2335 else
2336 {
2337 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2338 uint8_t n_type = NlistMaskType & nlist.n_type;
2339 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2340
2341 switch (n_type)
2342 {
2343 case NListTypeIndirect: // N_INDR - Fall through
2344 case NListTypePreboundUndefined:// N_PBUD - Fall through
2345 case NListTypeUndefined: // N_UNDF
2346 type = eSymbolTypeUndefined;
2347 break;
2348
2349 case NListTypeAbsolute: // N_ABS
2350 type = eSymbolTypeAbsolute;
2351 break;
2352
2353 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002354 {
2355 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002356
Greg Clayton01e6a582012-11-27 01:52:16 +00002357 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002358 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002359 // TODO: warn about this?
2360 add_nlist = false;
2361 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002362 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002363
2364 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002365 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002366 type = eSymbolTypeException;
2367 }
2368 else
2369 {
2370 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002371
Greg Clayton01e6a582012-11-27 01:52:16 +00002372 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002373 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002374 case SectionTypeRegular: break; // regular section
2375 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2376 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2377 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2378 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2379 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2380 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2381 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2382 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2383 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2384 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2385 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2386 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2387 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2388 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2389 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2390 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2391 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002392 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002393
Greg Clayton01e6a582012-11-27 01:52:16 +00002394 if (type == eSymbolTypeInvalid)
2395 {
2396 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2397 if (symbol_section->IsDescendant (text_section_sp.get()))
2398 {
2399 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2400 SectionAttrUserSelfModifyingCode |
2401 SectionAttrSytemSomeInstructions))
2402 type = eSymbolTypeData;
2403 else
2404 type = eSymbolTypeCode;
2405 }
2406 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002407 {
2408 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2409 {
2410 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002411
Greg Clayton01e6a582012-11-27 01:52:16 +00002412 if (symbol_name &&
2413 symbol_name[0] == '_' &&
2414 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002415 symbol_name[2] == 'B')
2416 {
2417 llvm::StringRef symbol_name_ref(symbol_name);
2418 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2419 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2420 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2421 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2422 {
2423 symbol_name_non_abi_mangled = symbol_name + 1;
2424 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2425 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002426 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002427 }
2428 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2429 {
2430 symbol_name_non_abi_mangled = symbol_name + 1;
2431 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2432 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002433 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002434 }
2435 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2436 {
2437 symbol_name_non_abi_mangled = symbol_name + 1;
2438 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2439 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002440 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002441 }
2442 }
2443 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002444 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002445 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002446 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002447 }
2448 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002449 {
2450 type = eSymbolTypeData;
2451 }
2452 }
2453 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2454 {
2455 type = eSymbolTypeTrampoline;
2456 }
2457 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2458 {
2459 type = eSymbolTypeRuntime;
2460 if (symbol_name && symbol_name[0] == '.')
2461 {
2462 llvm::StringRef symbol_name_ref(symbol_name);
2463 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2464 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002465 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002466 symbol_name_non_abi_mangled = symbol_name;
2467 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2468 type = eSymbolTypeObjCClass;
2469 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002470 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002471 }
2472 }
2473 }
Jason Molendab62abd52012-06-21 01:51:02 +00002474 }
2475 }
Jason Molendab62abd52012-06-21 01:51:02 +00002476 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002477 }
Jason Molendab62abd52012-06-21 01:51:02 +00002478 }
2479
2480 if (add_nlist)
2481 {
2482 uint64_t symbol_value = nlist.n_value;
2483 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002484
Jason Molendab62abd52012-06-21 01:51:02 +00002485 if (symbol_name_non_abi_mangled)
2486 {
Jason Molenda292cca82012-07-20 03:35:44 +00002487 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2488 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002489 }
2490 else
2491 {
2492 if (symbol_name && symbol_name[0] == '_')
2493 {
2494 symbol_name_is_mangled = symbol_name[1] == '_';
2495 symbol_name++; // Skip the leading underscore
2496 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002497
Jason Molendab62abd52012-06-21 01:51:02 +00002498 if (symbol_name)
2499 {
Jason Molenda292cca82012-07-20 03:35:44 +00002500 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002501 }
2502 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002503
Jason Molendab62abd52012-06-21 01:51:02 +00002504 if (is_debug == false)
2505 {
2506 if (type == eSymbolTypeCode)
2507 {
2508 // See if we can find a N_FUN entry for any code symbols.
2509 // If we do find a match, and the name matches, then we
2510 // can merge the two into just the function symbol to avoid
2511 // duplicate entries in the symbol table
2512 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2513 if (pos != N_FUN_addr_to_sym_idx.end())
2514 {
2515 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2516 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2517 {
2518 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2519 // We just need the flags from the linker symbol, so put these flags
2520 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2521 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2522 sym[sym_idx].Clear();
2523 continue;
2524 }
2525 }
2526 }
2527 else if (type == eSymbolTypeData)
2528 {
2529 // See if we can find a N_STSYM entry for any data symbols.
2530 // If we do find a match, and the name matches, then we
2531 // can merge the two into just the Static symbol to avoid
2532 // duplicate entries in the symbol table
2533 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2534 if (pos != N_STSYM_addr_to_sym_idx.end())
2535 {
2536 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2537 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2538 {
2539 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2540 // We just need the flags from the linker symbol, so put these flags
2541 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2542 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2543 sym[sym_idx].Clear();
2544 continue;
2545 }
2546 }
2547 }
2548 }
2549 if (symbol_section)
2550 {
2551 const addr_t section_file_addr = symbol_section->GetFileAddress();
2552 if (symbol_byte_size == 0 && function_starts_count > 0)
2553 {
2554 addr_t symbol_lookup_file_addr = nlist.n_value;
2555 // Do an exact address match for non-ARM addresses, else get the closest since
2556 // the symbol might be a thumb symbol which has an address with bit zero set
2557 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2558 if (is_arm && func_start_entry)
2559 {
2560 // Verify that the function start address is the symbol address (ARM)
2561 // or the symbol address + 1 (thumb)
2562 if (func_start_entry->addr != symbol_lookup_file_addr &&
2563 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2564 {
2565 // Not the right entry, NULL it out...
2566 func_start_entry = NULL;
2567 }
2568 }
2569 if (func_start_entry)
2570 {
2571 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002572
Jason Molendab62abd52012-06-21 01:51:02 +00002573 addr_t symbol_file_addr = func_start_entry->addr;
2574 uint32_t symbol_flags = 0;
2575 if (is_arm)
2576 {
2577 if (symbol_file_addr & 1)
2578 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2579 symbol_file_addr &= 0xfffffffffffffffeull;
2580 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002581
Jason Molendab62abd52012-06-21 01:51:02 +00002582 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2583 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2584 if (next_func_start_entry)
2585 {
2586 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2587 // Be sure the clear the Thumb address bit when we calculate the size
2588 // from the current and next address
2589 if (is_arm)
2590 next_symbol_file_addr &= 0xfffffffffffffffeull;
2591 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2592 }
2593 else
2594 {
2595 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2596 }
2597 }
2598 }
2599 symbol_value -= section_file_addr;
2600 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002601
Jason Molendab62abd52012-06-21 01:51:02 +00002602 sym[sym_idx].SetID (nlist_idx);
2603 sym[sym_idx].SetType (type);
2604 sym[sym_idx].GetAddress().SetSection (symbol_section);
2605 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2606 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002607
Jason Molendab62abd52012-06-21 01:51:02 +00002608 if (symbol_byte_size > 0)
2609 sym[sym_idx].SetByteSize(symbol_byte_size);
2610
Greg Clayton01e6a582012-11-27 01:52:16 +00002611 if (demangled_is_synthesized)
2612 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002613 ++sym_idx;
2614 }
2615 else
2616 {
2617 sym[sym_idx].Clear();
2618 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002619
Jason Molendab62abd52012-06-21 01:51:02 +00002620 }
2621 /////////////////////////////
2622 }
2623 break; // No more entries to consider
2624 }
2625 }
2626 }
2627 }
2628 }
2629 }
2630 }
2631
2632 // Must reset this in case it was mutated above!
2633 nlist_data_offset = 0;
2634#endif
2635
2636 // If the sym array was not created while parsing the DSC unmapped
2637 // symbols, create it now.
2638 if (sym == NULL)
2639 {
2640 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2641 num_syms = symtab->GetNumSymbols();
2642 }
2643
2644 if (unmapped_local_symbols_found)
2645 {
2646 assert(m_dysymtab.ilocalsym == 0);
2647 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2648 nlist_idx = m_dysymtab.nlocalsym;
2649 }
2650 else
2651 {
2652 nlist_idx = 0;
2653 }
2654
2655 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002656 {
2657 struct nlist_64 nlist;
2658 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2659 break;
2660
2661 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2662 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2663 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2664 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2665 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2666
2667 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002668 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002669
Greg Clayton3a5dc012012-05-25 17:04:00 +00002670 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002671 {
2672 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002673
Greg Claytondd29b972012-05-18 23:20:01 +00002674 if (symbol_name == NULL)
2675 {
2676 // No symbol should be NULL, even the symbols with no
2677 // string values should have an offset zero which points
2678 // to an empty C-string
2679 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00002680 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002681 nlist_idx,
2682 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00002683 module_sp->GetFileSpec().GetPath().c_str());
Greg Claytondd29b972012-05-18 23:20:01 +00002684 continue;
2685 }
2686 if (symbol_name[0] == '\0')
2687 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002688 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002689 else
2690 {
2691 const addr_t str_addr = strtab_addr + nlist.n_strx;
2692 Error str_error;
2693 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2694 symbol_name = memory_symbol_name.c_str();
2695 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002696 const char *symbol_name_non_abi_mangled = NULL;
2697
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002698 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002699 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002700 bool add_nlist = true;
2701 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002702 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002703
2704 assert (sym_idx < num_syms);
2705
2706 sym[sym_idx].SetDebug (is_debug);
2707
2708 if (is_debug)
2709 {
2710 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002711 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002712 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002713 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2714 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002715
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002716 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2717 // have the same address, but we want to ensure that we always find only the real symbol,
2718 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2719 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2720 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2721 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002722
2723 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002724 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2725 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2726 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2727 add_nlist = false;
2728 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002729 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002730 sym[sym_idx].SetExternal(true);
2731 if (nlist.n_value != 0)
2732 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2733 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002734 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002735 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002736
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002737 case StabFunctionName:
2738 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2739 type = eSymbolTypeCompiler;
2740 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002741
Jason Molenda9badb6c2013-03-06 23:19:17 +00002742 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002743 // N_FUN -- procedure: name,,n_sect,linenumber,address
2744 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002745 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002746 type = eSymbolTypeCode;
2747 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002748
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002749 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2750 // We use the current number of symbols in the symbol table in lieu of
2751 // using nlist_idx in case we ever start trimming entries out
2752 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002753 }
2754 else
2755 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002756 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002757
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002758 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002759 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002760 // Copy the size of the function into the original STAB entry so we don't have
2761 // to hunt for it later
2762 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2763 N_FUN_indexes.pop_back();
2764 // We don't really need the end function STAB as it contains the size which
2765 // we already placed with the original symbol, so don't add it if we want a
2766 // minimal symbol table
2767 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002768 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002769 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002770 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002771 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002772
Jason Molenda9badb6c2013-03-06 23:19:17 +00002773 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002774 // N_STSYM -- static symbol: name,,n_sect,type,address
2775 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2776 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2777 type = eSymbolTypeData;
2778 break;
2779
2780 case StabLocalCommon:
2781 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2782 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2783 type = eSymbolTypeCommonBlock;
2784 break;
2785
2786 case StabBeginSymbol:
2787 // N_BNSYM
2788 // We use the current number of symbols in the symbol table in lieu of
2789 // using nlist_idx in case we ever start trimming entries out
2790 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002791 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002792 // Skip these if we want minimal symbol tables
2793 add_nlist = false;
2794 }
2795 else
2796 {
2797 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2798 N_NSYM_indexes.push_back(sym_idx);
2799 type = eSymbolTypeScopeBegin;
2800 }
2801 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002802
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002803 case StabEndSymbol:
2804 // N_ENSYM
2805 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2806 // so that we can always skip the entire symbol if we need to navigate
2807 // more quickly at the source level when parsing STABS
2808 if (minimize)
2809 {
2810 // Skip these if we want minimal symbol tables
2811 add_nlist = false;
2812 }
2813 else
2814 {
2815 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002816 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002817 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2818 symbol_ptr->SetByteSize(sym_idx + 1);
2819 symbol_ptr->SetSizeIsSibling(true);
2820 N_NSYM_indexes.pop_back();
2821 }
2822 type = eSymbolTypeScopeEnd;
2823 }
2824 break;
2825
2826
2827 case StabSourceFileOptions:
2828 // N_OPT - emitted with gcc2_compiled and in gcc source
2829 type = eSymbolTypeCompiler;
2830 break;
2831
2832 case StabRegisterSymbol:
2833 // N_RSYM - register sym: name,,NO_SECT,type,register
2834 type = eSymbolTypeVariable;
2835 break;
2836
2837 case StabSourceLine:
2838 // N_SLINE - src line: 0,,n_sect,linenumber,address
2839 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2840 type = eSymbolTypeLineEntry;
2841 break;
2842
2843 case StabStructureType:
2844 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2845 type = eSymbolTypeVariableType;
2846 break;
2847
2848 case StabSourceFileName:
2849 // N_SO - source file name
2850 type = eSymbolTypeSourceFile;
2851 if (symbol_name == NULL)
2852 {
2853 if (minimize)
2854 add_nlist = false;
2855 if (N_SO_index != UINT32_MAX)
2856 {
2857 // Set the size of the N_SO to the terminating index of this N_SO
2858 // so that we can always skip the entire N_SO if we need to navigate
2859 // more quickly at the source level when parsing STABS
2860 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2861 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2862 symbol_ptr->SetSizeIsSibling(true);
2863 }
2864 N_NSYM_indexes.clear();
2865 N_INCL_indexes.clear();
2866 N_BRAC_indexes.clear();
2867 N_COMM_indexes.clear();
2868 N_FUN_indexes.clear();
2869 N_SO_index = UINT32_MAX;
2870 }
2871 else
2872 {
2873 // We use the current number of symbols in the symbol table in lieu of
2874 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002875 const bool N_SO_has_full_path = symbol_name[0] == '/';
2876 if (N_SO_has_full_path)
2877 {
2878 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2879 {
2880 // We have two consecutive N_SO entries where the first contains a directory
2881 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002882 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002883 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2884 add_nlist = false;
2885 }
2886 else
2887 {
2888 // This is the first entry in a N_SO that contains a directory or
2889 // a full path to the source file
2890 N_SO_index = sym_idx;
2891 }
2892 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002893 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2894 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002895 // This is usually the second N_SO entry that contains just the filename,
2896 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002897 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2898 if (so_path && so_path[0])
2899 {
2900 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002901 const size_t double_slash_pos = full_so_path.find("//");
2902 if (double_slash_pos != std::string::npos)
2903 {
2904 // The linker has been generating bad N_SO entries with doubled up paths
2905 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2906 // and the second is the directory for the source file so you end up with
2907 // a path that looks like "/tmp/src//tmp/src/"
2908 FileSpec so_dir(so_path, false);
2909 if (!so_dir.Exists())
2910 {
2911 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2912 if (so_dir.Exists())
2913 {
2914 // Trim off the incorrect path
2915 full_so_path.erase(0, double_slash_pos + 1);
2916 }
2917 }
2918 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002919 if (*full_so_path.rbegin() != '/')
2920 full_so_path += '/';
2921 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002922 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002923 add_nlist = false;
2924 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2925 }
2926 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002927 else
2928 {
2929 // This could be a relative path to a N_SO
2930 N_SO_index = sym_idx;
2931 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002932 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002933
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002934 break;
2935
2936 case StabObjectFileName:
2937 // N_OSO - object file name: name,,0,0,st_mtime
2938 type = eSymbolTypeObjectFile;
2939 break;
2940
2941 case StabLocalSymbol:
2942 // N_LSYM - local sym: name,,NO_SECT,type,offset
2943 type = eSymbolTypeLocal;
2944 break;
2945
2946 //----------------------------------------------------------------------
2947 // INCL scopes
2948 //----------------------------------------------------------------------
2949 case StabBeginIncludeFileName:
2950 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2951 // We use the current number of symbols in the symbol table in lieu of
2952 // using nlist_idx in case we ever start trimming entries out
2953 N_INCL_indexes.push_back(sym_idx);
2954 type = eSymbolTypeScopeBegin;
2955 break;
2956
2957 case StabEndIncludeFile:
2958 // N_EINCL - include file end: name,,NO_SECT,0,0
2959 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2960 // so that we can always skip the entire symbol if we need to navigate
2961 // more quickly at the source level when parsing STABS
2962 if ( !N_INCL_indexes.empty() )
2963 {
2964 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2965 symbol_ptr->SetByteSize(sym_idx + 1);
2966 symbol_ptr->SetSizeIsSibling(true);
2967 N_INCL_indexes.pop_back();
2968 }
2969 type = eSymbolTypeScopeEnd;
2970 break;
2971
2972 case StabIncludeFileName:
2973 // N_SOL - #included file name: name,,n_sect,0,address
2974 type = eSymbolTypeHeaderFile;
2975
2976 // We currently don't use the header files on darwin
2977 if (minimize)
2978 add_nlist = false;
2979 break;
2980
Jason Molenda9badb6c2013-03-06 23:19:17 +00002981 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002982 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2983 type = eSymbolTypeCompiler;
2984 break;
2985
2986 case StabCompilerVersion:
2987 // N_VERSION - compiler version: name,,NO_SECT,0,0
2988 type = eSymbolTypeCompiler;
2989 break;
2990
2991 case StabCompilerOptLevel:
2992 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2993 type = eSymbolTypeCompiler;
2994 break;
2995
2996 case StabParameter:
2997 // N_PSYM - parameter: name,,NO_SECT,type,offset
2998 type = eSymbolTypeVariable;
2999 break;
3000
3001 case StabAlternateEntry:
3002 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
3003 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3004 type = eSymbolTypeLineEntry;
3005 break;
3006
3007 //----------------------------------------------------------------------
3008 // Left and Right Braces
3009 //----------------------------------------------------------------------
3010 case StabLeftBracket:
3011 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
3012 // We use the current number of symbols in the symbol table in lieu of
3013 // using nlist_idx in case we ever start trimming entries out
3014 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3015 N_BRAC_indexes.push_back(sym_idx);
3016 type = eSymbolTypeScopeBegin;
3017 break;
3018
3019 case StabRightBracket:
3020 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
3021 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3022 // so that we can always skip the entire symbol if we need to navigate
3023 // more quickly at the source level when parsing STABS
3024 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3025 if ( !N_BRAC_indexes.empty() )
3026 {
3027 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3028 symbol_ptr->SetByteSize(sym_idx + 1);
3029 symbol_ptr->SetSizeIsSibling(true);
3030 N_BRAC_indexes.pop_back();
3031 }
3032 type = eSymbolTypeScopeEnd;
3033 break;
3034
3035 case StabDeletedIncludeFile:
3036 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3037 type = eSymbolTypeHeaderFile;
3038 break;
3039
3040 //----------------------------------------------------------------------
3041 // COMM scopes
3042 //----------------------------------------------------------------------
3043 case StabBeginCommon:
3044 // N_BCOMM - begin common: name,,NO_SECT,0,0
3045 // We use the current number of symbols in the symbol table in lieu of
3046 // using nlist_idx in case we ever start trimming entries out
3047 type = eSymbolTypeScopeBegin;
3048 N_COMM_indexes.push_back(sym_idx);
3049 break;
3050
3051 case StabEndCommonLocal:
3052 // N_ECOML - end common (local name): 0,,n_sect,0,address
3053 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3054 // Fall through
3055
3056 case StabEndCommon:
3057 // N_ECOMM - end common: name,,n_sect,0,0
3058 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3059 // so that we can always skip the entire symbol if we need to navigate
3060 // more quickly at the source level when parsing STABS
3061 if ( !N_COMM_indexes.empty() )
3062 {
3063 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3064 symbol_ptr->SetByteSize(sym_idx + 1);
3065 symbol_ptr->SetSizeIsSibling(true);
3066 N_COMM_indexes.pop_back();
3067 }
3068 type = eSymbolTypeScopeEnd;
3069 break;
3070
3071 case StabLength:
3072 // N_LENG - second stab entry with length information
3073 type = eSymbolTypeAdditional;
3074 break;
3075
3076 default: break;
3077 }
3078 }
3079 else
3080 {
3081 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3082 uint8_t n_type = NlistMaskType & nlist.n_type;
3083 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3084
3085 switch (n_type)
3086 {
3087 case NListTypeIndirect: // N_INDR - Fall through
3088 case NListTypePreboundUndefined:// N_PBUD - Fall through
3089 case NListTypeUndefined: // N_UNDF
3090 type = eSymbolTypeUndefined;
3091 break;
3092
3093 case NListTypeAbsolute: // N_ABS
3094 type = eSymbolTypeAbsolute;
3095 break;
3096
3097 case NListTypeSection: // N_SECT
3098 {
3099 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3100
Sean Callananb386d822012-08-09 00:50:26 +00003101 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003102 {
3103 // TODO: warn about this?
3104 add_nlist = false;
3105 break;
3106 }
3107
3108 if (TEXT_eh_frame_sectID == nlist.n_sect)
3109 {
3110 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00003111 }
3112 else
3113 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003114 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3115
3116 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00003117 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003118 case SectionTypeRegular: break; // regular section
3119 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3120 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3121 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3122 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3123 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3124 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3125 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3126 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3127 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3128 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3129 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3130 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3131 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3132 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3133 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3134 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3135 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00003136 }
Chris Lattner24943d22010-06-08 16:52:24 +00003137
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003138 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00003139 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003140 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3141 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003142 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003143 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3144 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003145 SectionAttrSytemSomeInstructions))
3146 type = eSymbolTypeData;
3147 else
3148 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00003149 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003150 else
3151 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003152 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003153 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00003154 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003155 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00003156
Jason Molenda9badb6c2013-03-06 23:19:17 +00003157 if (symbol_name &&
3158 symbol_name[0] == '_' &&
3159 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003160 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00003161 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003162 llvm::StringRef symbol_name_ref(symbol_name);
3163 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3164 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3165 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3166 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00003167 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003168 symbol_name_non_abi_mangled = symbol_name + 1;
3169 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3170 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003171 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003172 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003173 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003174 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003175 symbol_name_non_abi_mangled = symbol_name + 1;
3176 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3177 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003178 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003179 }
3180 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3181 {
3182 symbol_name_non_abi_mangled = symbol_name + 1;
3183 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3184 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003185 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003186 }
3187 }
3188 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003189 else
3190 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3191 {
3192 type = eSymbolTypeException;
3193 }
3194 else
3195 {
3196 type = eSymbolTypeData;
3197 }
3198 }
3199 else
3200 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3201 {
3202 type = eSymbolTypeTrampoline;
3203 }
3204 else
3205 if (symbol_section->IsDescendant(objc_section_sp.get()))
3206 {
3207 type = eSymbolTypeRuntime;
3208 if (symbol_name && symbol_name[0] == '.')
3209 {
3210 llvm::StringRef symbol_name_ref(symbol_name);
3211 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3212 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3213 {
3214 symbol_name_non_abi_mangled = symbol_name;
3215 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3216 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003217 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003218 }
3219 }
Chris Lattner24943d22010-06-08 16:52:24 +00003220 }
3221 }
3222 }
3223 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003224 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003225 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003226 }
3227
3228 if (add_nlist)
3229 {
3230 uint64_t symbol_value = nlist.n_value;
3231 bool symbol_name_is_mangled = false;
3232
3233 if (symbol_name_non_abi_mangled)
3234 {
Greg Claytonc0240042012-07-18 23:18:10 +00003235 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3236 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003237 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003238 else
3239 {
3240 if (symbol_name && symbol_name[0] == '_')
3241 {
3242 symbol_name_is_mangled = symbol_name[1] == '_';
3243 symbol_name++; // Skip the leading underscore
3244 }
3245
3246 if (symbol_name)
3247 {
Greg Claytonc0240042012-07-18 23:18:10 +00003248 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003249 }
3250 }
3251
3252 if (is_debug == false)
3253 {
3254 if (type == eSymbolTypeCode)
3255 {
3256 // See if we can find a N_FUN entry for any code symbols.
3257 // If we do find a match, and the name matches, then we
3258 // can merge the two into just the function symbol to avoid
3259 // duplicate entries in the symbol table
3260 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3261 if (pos != N_FUN_addr_to_sym_idx.end())
3262 {
3263 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3264 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3265 {
3266 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3267 // We just need the flags from the linker symbol, so put these flags
3268 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3269 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3270 sym[sym_idx].Clear();
3271 continue;
3272 }
3273 }
3274 }
3275 else if (type == eSymbolTypeData)
3276 {
3277 // See if we can find a N_STSYM entry for any data symbols.
3278 // If we do find a match, and the name matches, then we
3279 // can merge the two into just the Static symbol to avoid
3280 // duplicate entries in the symbol table
3281 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3282 if (pos != N_STSYM_addr_to_sym_idx.end())
3283 {
3284 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3285 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3286 {
3287 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3288 // We just need the flags from the linker symbol, so put these flags
3289 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3290 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3291 sym[sym_idx].Clear();
3292 continue;
3293 }
3294 }
3295 }
3296 }
3297 if (symbol_section)
3298 {
3299 const addr_t section_file_addr = symbol_section->GetFileAddress();
3300 if (symbol_byte_size == 0 && function_starts_count > 0)
3301 {
Greg Claytond2653c22012-03-14 01:53:24 +00003302 addr_t symbol_lookup_file_addr = nlist.n_value;
3303 // Do an exact address match for non-ARM addresses, else get the closest since
3304 // the symbol might be a thumb symbol which has an address with bit zero set
3305 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3306 if (is_arm && func_start_entry)
3307 {
3308 // Verify that the function start address is the symbol address (ARM)
3309 // or the symbol address + 1 (thumb)
3310 if (func_start_entry->addr != symbol_lookup_file_addr &&
3311 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3312 {
3313 // Not the right entry, NULL it out...
3314 func_start_entry = NULL;
3315 }
3316 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003317 if (func_start_entry)
3318 {
3319 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003320
Greg Claytond2653c22012-03-14 01:53:24 +00003321 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003322 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003323 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003324
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003325 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3326 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3327 if (next_func_start_entry)
3328 {
Greg Claytond2653c22012-03-14 01:53:24 +00003329 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3330 // Be sure the clear the Thumb address bit when we calculate the size
3331 // from the current and next address
3332 if (is_arm)
3333 next_symbol_file_addr &= 0xfffffffffffffffeull;
3334 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 +00003335 }
3336 else
3337 {
Greg Claytond2653c22012-03-14 01:53:24 +00003338 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003339 }
3340 }
3341 }
3342 symbol_value -= section_file_addr;
3343 }
3344
3345 sym[sym_idx].SetID (nlist_idx);
3346 sym[sym_idx].SetType (type);
3347 sym[sym_idx].GetAddress().SetSection (symbol_section);
3348 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3349 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3350
3351 if (symbol_byte_size > 0)
3352 sym[sym_idx].SetByteSize(symbol_byte_size);
3353
Greg Clayton01e6a582012-11-27 01:52:16 +00003354 if (demangled_is_synthesized)
3355 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3356
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003357 ++sym_idx;
3358 }
3359 else
3360 {
3361 sym[sym_idx].Clear();
3362 }
3363
3364 }
3365
3366 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3367 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3368 // such entries by figuring out what the address for the global is by looking up this non-STAB
3369 // entry and copying the value into the debug symbol's value to save us the hassle in the
3370 // debug symbol parser.
3371
3372 Symbol *global_symbol = NULL;
3373 for (nlist_idx = 0;
3374 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3375 nlist_idx++)
3376 {
3377 if (global_symbol->GetAddress().GetFileAddress() == 0)
3378 {
3379 std::vector<uint32_t> indexes;
3380 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3381 {
3382 std::vector<uint32_t>::const_iterator pos;
3383 std::vector<uint32_t>::const_iterator end = indexes.end();
3384 for (pos = indexes.begin(); pos != end; ++pos)
3385 {
3386 symbol_ptr = symtab->SymbolAtIndex(*pos);
3387 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3388 {
3389 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3390 break;
3391 }
3392 }
3393 }
Chris Lattner24943d22010-06-08 16:52:24 +00003394 }
3395 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003396
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003397 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3398
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003399 if (function_starts_count > 0)
3400 {
3401 char synthetic_function_symbol[PATH_MAX];
3402 uint32_t num_synthetic_function_symbols = 0;
3403 for (i=0; i<function_starts_count; ++i)
3404 {
3405 if (function_starts.GetEntryRef (i).data == false)
3406 ++num_synthetic_function_symbols;
3407 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003408
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003409 if (num_synthetic_function_symbols > 0)
3410 {
3411 if (num_syms < sym_idx + num_synthetic_function_symbols)
3412 {
3413 num_syms = sym_idx + num_synthetic_function_symbols;
3414 sym = symtab->Resize (num_syms);
3415 }
3416 uint32_t synthetic_function_symbol_idx = 0;
3417 for (i=0; i<function_starts_count; ++i)
3418 {
3419 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3420 if (func_start_entry->data == false)
3421 {
Greg Claytond2653c22012-03-14 01:53:24 +00003422 addr_t symbol_file_addr = func_start_entry->addr;
3423 uint32_t symbol_flags = 0;
3424 if (is_arm)
3425 {
3426 if (symbol_file_addr & 1)
3427 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3428 symbol_file_addr &= 0xfffffffffffffffeull;
3429 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003430 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003431 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003432 {
3433 SectionSP symbol_section (symbol_addr.GetSection());
3434 uint32_t symbol_byte_size = 0;
3435 if (symbol_section)
3436 {
3437 const addr_t section_file_addr = symbol_section->GetFileAddress();
3438 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3439 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3440 if (next_func_start_entry)
3441 {
Greg Claytond2653c22012-03-14 01:53:24 +00003442 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3443 if (is_arm)
3444 next_symbol_file_addr &= 0xfffffffffffffffeull;
3445 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 +00003446 }
3447 else
3448 {
Greg Claytond2653c22012-03-14 01:53:24 +00003449 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003450 }
3451 snprintf (synthetic_function_symbol,
3452 sizeof(synthetic_function_symbol),
3453 "___lldb_unnamed_function%u$$%s",
3454 ++synthetic_function_symbol_idx,
3455 module_sp->GetFileSpec().GetFilename().GetCString());
3456 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003457 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003458 sym[sym_idx].SetType (eSymbolTypeCode);
3459 sym[sym_idx].SetIsSynthetic (true);
3460 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003461 if (symbol_flags)
3462 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003463 if (symbol_byte_size)
3464 sym[sym_idx].SetByteSize (symbol_byte_size);
3465 ++sym_idx;
3466 }
3467 }
3468 }
3469 }
3470 }
3471 }
3472
3473 // Trim our symbols down to just what we ended up with after
3474 // removing any symbols.
3475 if (sym_idx < num_syms)
3476 {
3477 num_syms = sym_idx;
3478 sym = symtab->Resize (num_syms);
3479 }
3480
3481 // Now synthesize indirect symbols
3482 if (m_dysymtab.nindirectsyms != 0)
3483 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003484 if (indirect_symbol_index_data.GetByteSize())
3485 {
3486 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3487
3488 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3489 {
3490 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3491 {
3492 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3493 if (symbol_stub_byte_size == 0)
3494 continue;
3495
3496 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3497
3498 if (num_symbol_stubs == 0)
3499 continue;
3500
3501 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3502 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3503 {
3504 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3505 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 +00003506 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003507 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3508 {
3509 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3510 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3511 continue;
3512
3513 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3514 Symbol *stub_symbol = NULL;
3515 if (index_pos != end_index_pos)
3516 {
3517 // We have a remapping from the original nlist index to
3518 // a current symbol index, so just look this up by index
3519 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3520 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003521 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003522 {
3523 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003524 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003525 // S_SYMBOL_STUBS
3526 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3527 }
3528
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003529 if (stub_symbol)
3530 {
3531 Address so_addr(symbol_stub_addr, section_list);
3532
3533 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3534 {
3535 // Change the external symbol into a trampoline that makes sense
3536 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3537 // can re-use them so we don't have to make up a synthetic symbol
3538 // for no good reason.
3539 stub_symbol->SetType (eSymbolTypeTrampoline);
3540 stub_symbol->SetExternal (false);
3541 stub_symbol->GetAddress() = so_addr;
3542 stub_symbol->SetByteSize (symbol_stub_byte_size);
3543 }
3544 else
3545 {
3546 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003547 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003548 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003549 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003550 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003551 stub_symbol = NULL; // this pointer no longer valid
3552 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003553 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003554 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003555 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3556 sym[sym_idx].SetIsSynthetic (true);
3557 sym[sym_idx].GetAddress() = so_addr;
3558 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3559 ++sym_idx;
3560 }
3561 }
Greg Claytond4330e62012-09-05 01:38:55 +00003562 else
3563 {
3564 if (log)
3565 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3566 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003567 }
3568 }
3569 }
3570 }
3571 }
3572 }
3573 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003574 }
3575 return 0;
3576}
3577
3578
3579void
3580ObjectFileMachO::Dump (Stream *s)
3581{
Greg Clayton9482f052012-03-13 23:14:29 +00003582 ModuleSP module_sp(GetModule());
3583 if (module_sp)
3584 {
3585 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3586 s->Printf("%p: ", this);
3587 s->Indent();
3588 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3589 s->PutCString("ObjectFileMachO64");
3590 else
3591 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003592
Greg Clayton9482f052012-03-13 23:14:29 +00003593 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003594
Greg Clayton9482f052012-03-13 23:14:29 +00003595 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003596
Greg Clayton9482f052012-03-13 23:14:29 +00003597 if (m_sections_ap.get())
3598 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003599
Greg Clayton9482f052012-03-13 23:14:29 +00003600 if (m_symtab_ap.get())
3601 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3602 }
Chris Lattner24943d22010-06-08 16:52:24 +00003603}
3604
Greg Clayton36b877d2013-04-24 22:29:28 +00003605bool
3606ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3607 const lldb_private::DataExtractor &data,
3608 lldb::offset_t lc_offset,
3609 lldb_private::UUID& uuid)
3610{
3611 uint32_t i;
3612 struct uuid_command load_cmd;
3613
3614 lldb::offset_t offset = lc_offset;
3615 for (i=0; i<header.ncmds; ++i)
3616 {
3617 const lldb::offset_t cmd_offset = offset;
3618 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3619 break;
3620
3621 if (load_cmd.cmd == LoadCommandUUID)
3622 {
3623 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3624
3625 if (uuid_bytes)
3626 {
3627 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3628 // We pretend these object files have no UUID to prevent crashing.
3629
3630 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3631 0x3b, 0xa8,
3632 0x4b, 0x16,
3633 0xb6, 0xa4,
3634 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3635
3636 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3637 return false;
3638
3639 uuid.SetBytes (uuid_bytes);
3640 return true;
3641 }
3642 return false;
3643 }
3644 offset = cmd_offset + load_cmd.cmdsize;
3645 }
3646 return false;
3647}
Chris Lattner24943d22010-06-08 16:52:24 +00003648
3649bool
Greg Clayton0467c782011-02-04 18:53:10 +00003650ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003651{
Greg Clayton9482f052012-03-13 23:14:29 +00003652 ModuleSP module_sp(GetModule());
3653 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003654 {
Greg Clayton9482f052012-03-13 23:14:29 +00003655 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00003656 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton36b877d2013-04-24 22:29:28 +00003657 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner24943d22010-06-08 16:52:24 +00003658 }
3659 return false;
3660}
3661
3662
3663uint32_t
3664ObjectFileMachO::GetDependentModules (FileSpecList& files)
3665{
Chris Lattner24943d22010-06-08 16:52:24 +00003666 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003667 ModuleSP module_sp(GetModule());
3668 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003669 {
Greg Clayton9482f052012-03-13 23:14:29 +00003670 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3671 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003672 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003673 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3674 uint32_t i;
3675 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003676 {
Greg Clayton9482f052012-03-13 23:14:29 +00003677 const uint32_t cmd_offset = offset;
3678 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3679 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003680
Greg Clayton9482f052012-03-13 23:14:29 +00003681 switch (load_cmd.cmd)
3682 {
3683 case LoadCommandDylibLoad:
3684 case LoadCommandDylibLoadWeak:
3685 case LoadCommandDylibReexport:
3686 case LoadCommandDynamicLinkerLoad:
3687 case LoadCommandFixedVMShlibLoad:
3688 case LoadCommandDylibLoadUpward:
3689 {
3690 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3691 const char *path = m_data.PeekCStr(name_offset);
3692 // Skip any path that starts with '@' since these are usually:
3693 // @executable_path/.../file
3694 // @rpath/.../file
3695 if (path && path[0] != '@')
3696 {
3697 FileSpec file_spec(path, resolve_path);
3698 if (files.AppendIfUnique(file_spec))
3699 count++;
3700 }
3701 }
3702 break;
3703
3704 default:
3705 break;
3706 }
3707 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003708 }
Chris Lattner24943d22010-06-08 16:52:24 +00003709 }
3710 return count;
3711}
3712
Jim Ingham28775942011-03-07 23:44:08 +00003713lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003714ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003715{
3716 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3717 // is initialized to an invalid address, so we can just return that.
3718 // 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 +00003719
Jim Ingham28775942011-03-07 23:44:08 +00003720 if (!IsExecutable() || m_entry_point_address.IsValid())
3721 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003722
3723 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003724 // /usr/include/mach-o.h, but it is basically:
3725 //
3726 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3727 // uint32_t count - this is the count of longs in the thread state data
3728 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3729 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003730 //
Jim Ingham28775942011-03-07 23:44:08 +00003731 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3732 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3733 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3734 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3735 //
3736 // For now we hard-code the offsets and flavors we need:
3737 //
3738 //
3739
Greg Clayton9482f052012-03-13 23:14:29 +00003740 ModuleSP module_sp(GetModule());
3741 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003742 {
Greg Clayton9482f052012-03-13 23:14:29 +00003743 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3744 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003745 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003746 uint32_t i;
3747 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3748 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003749
Greg Clayton9482f052012-03-13 23:14:29 +00003750 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003751 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003752 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003753 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3754 break;
3755
3756 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003757 {
Greg Clayton9482f052012-03-13 23:14:29 +00003758 case LoadCommandUnixThread:
3759 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003760 {
Greg Clayton9482f052012-03-13 23:14:29 +00003761 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003762 {
Greg Clayton9482f052012-03-13 23:14:29 +00003763 uint32_t flavor = m_data.GetU32(&offset);
3764 uint32_t count = m_data.GetU32(&offset);
3765 if (count == 0)
3766 {
3767 // We've gotten off somehow, log and exit;
3768 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003769 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003770
Greg Clayton9482f052012-03-13 23:14:29 +00003771 switch (m_header.cputype)
3772 {
3773 case llvm::MachO::CPUTypeARM:
3774 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3775 {
3776 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3777 start_address = m_data.GetU32(&offset);
3778 done = true;
3779 }
Jim Ingham28775942011-03-07 23:44:08 +00003780 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003781 case llvm::MachO::CPUTypeI386:
3782 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3783 {
3784 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3785 start_address = m_data.GetU32(&offset);
3786 done = true;
3787 }
3788 break;
3789 case llvm::MachO::CPUTypeX86_64:
3790 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3791 {
3792 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3793 start_address = m_data.GetU64(&offset);
3794 done = true;
3795 }
3796 break;
3797 default:
3798 return m_entry_point_address;
3799 }
3800 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3801 if (done)
3802 break;
3803 offset += count * 4;
3804 }
Jim Ingham28775942011-03-07 23:44:08 +00003805 }
Greg Clayton9482f052012-03-13 23:14:29 +00003806 break;
3807 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003808 {
Greg Clayton9482f052012-03-13 23:14:29 +00003809 ConstString text_segment_name ("__TEXT");
3810 uint64_t entryoffset = m_data.GetU64(&offset);
3811 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3812 if (text_segment_sp)
3813 {
3814 done = true;
3815 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3816 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003817 }
Greg Clayton9482f052012-03-13 23:14:29 +00003818
3819 default:
3820 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003821 }
Greg Clayton9482f052012-03-13 23:14:29 +00003822 if (done)
3823 break;
Jim Ingham28775942011-03-07 23:44:08 +00003824
Greg Clayton9482f052012-03-13 23:14:29 +00003825 // Go to the next load command:
3826 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003827 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003828
Greg Clayton9482f052012-03-13 23:14:29 +00003829 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003830 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003831 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003832 // of this ObjectFile:
3833 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003834 {
Greg Clayton9482f052012-03-13 23:14:29 +00003835 m_entry_point_address.Clear();
3836 }
3837 }
3838 else
3839 {
3840 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3841 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003842
Greg Clayton9482f052012-03-13 23:14:29 +00003843 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003844
Greg Clayton9482f052012-03-13 23:14:29 +00003845 if (module_sp)
3846 {
3847 SymbolContextList contexts;
3848 SymbolContext context;
3849 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3850 {
3851 if (contexts.GetContextAtIndex(0, context))
3852 m_entry_point_address = context.symbol->GetAddress();
3853 }
Greg Clayton3508c382012-02-24 01:59:29 +00003854 }
3855 }
Jim Ingham28775942011-03-07 23:44:08 +00003856 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003857
Jim Ingham28775942011-03-07 23:44:08 +00003858 return m_entry_point_address;
3859
3860}
3861
Greg Claytonb5a8f142012-02-05 02:38:54 +00003862lldb_private::Address
3863ObjectFileMachO::GetHeaderAddress ()
3864{
3865 lldb_private::Address header_addr;
3866 SectionList *section_list = GetSectionList();
3867 if (section_list)
3868 {
3869 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3870 if (text_segment_sp)
3871 {
Greg Clayton3508c382012-02-24 01:59:29 +00003872 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003873 header_addr.SetOffset (0);
3874 }
3875 }
3876 return header_addr;
3877}
3878
Greg Clayton46c9a352012-02-09 06:16:32 +00003879uint32_t
3880ObjectFileMachO::GetNumThreadContexts ()
3881{
Greg Clayton9482f052012-03-13 23:14:29 +00003882 ModuleSP module_sp(GetModule());
3883 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003884 {
Greg Clayton9482f052012-03-13 23:14:29 +00003885 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3886 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003887 {
Greg Clayton9482f052012-03-13 23:14:29 +00003888 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003889 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003890 FileRangeArray::Entry file_range;
3891 thread_command thread_cmd;
3892 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003893 {
Greg Clayton9482f052012-03-13 23:14:29 +00003894 const uint32_t cmd_offset = offset;
3895 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3896 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003897
Greg Clayton9482f052012-03-13 23:14:29 +00003898 if (thread_cmd.cmd == LoadCommandThread)
3899 {
3900 file_range.SetRangeBase (offset);
3901 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3902 m_thread_context_offsets.Append (file_range);
3903 }
3904 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003905 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003906 }
3907 }
3908 return m_thread_context_offsets.GetSize();
3909}
3910
3911lldb::RegisterContextSP
3912ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3913{
Greg Clayton46c9a352012-02-09 06:16:32 +00003914 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003915
Greg Clayton9482f052012-03-13 23:14:29 +00003916 ModuleSP module_sp(GetModule());
3917 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003918 {
Greg Clayton9482f052012-03-13 23:14:29 +00003919 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3920 if (!m_thread_context_offsets_valid)
3921 GetNumThreadContexts ();
3922
3923 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003924 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003925 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003926
3927 DataExtractor data (m_data,
3928 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003929 thread_context_file_range->GetByteSize());
3930
3931 switch (m_header.cputype)
3932 {
3933 case llvm::MachO::CPUTypeARM:
3934 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3935 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003936
Jim Ingham6f01c932012-10-12 17:34:26 +00003937 case llvm::MachO::CPUTypeI386:
3938 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3939 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003940
Jim Ingham6f01c932012-10-12 17:34:26 +00003941 case llvm::MachO::CPUTypeX86_64:
3942 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3943 break;
3944 }
Greg Clayton9482f052012-03-13 23:14:29 +00003945 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003946 }
3947 return reg_ctx_sp;
3948}
3949
Greg Claytonb5a8f142012-02-05 02:38:54 +00003950
Greg Claytonca319972011-07-09 00:41:34 +00003951ObjectFile::Type
3952ObjectFileMachO::CalculateType()
3953{
3954 switch (m_header.filetype)
3955 {
3956 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3957 if (GetAddressByteSize () == 4)
3958 {
3959 // 32 bit kexts are just object files, but they do have a valid
3960 // UUID load command.
3961 UUID uuid;
3962 if (GetUUID(&uuid))
3963 {
3964 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003965 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003966 // "OSKextGetCurrentIdentifier" as this is required of kexts
3967 if (m_strata == eStrataInvalid)
3968 m_strata = eStrataKernel;
3969 return eTypeSharedLibrary;
3970 }
3971 }
3972 return eTypeObjectFile;
3973
3974 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3975 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3976 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3977 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3978 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3979 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3980 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3981 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3982 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3983 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3984 default:
3985 break;
3986 }
3987 return eTypeUnknown;
3988}
3989
3990ObjectFile::Strata
3991ObjectFileMachO::CalculateStrata()
3992{
3993 switch (m_header.filetype)
3994 {
3995 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3996 {
3997 // 32 bit kexts are just object files, but they do have a valid
3998 // UUID load command.
3999 UUID uuid;
4000 if (GetUUID(&uuid))
4001 {
4002 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00004003 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00004004 // "OSKextGetCurrentIdentifier" as this is required of kexts
4005 if (m_type == eTypeInvalid)
4006 m_type = eTypeSharedLibrary;
4007
4008 return eStrataKernel;
4009 }
4010 }
4011 return eStrataUnknown;
4012
4013 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
4014 // Check for the MH_DYLDLINK bit in the flags
4015 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00004016 {
Greg Claytonca319972011-07-09 00:41:34 +00004017 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00004018 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004019 else
Sean Callananac725af2012-02-10 20:22:35 +00004020 {
4021 SectionList *section_list = GetSectionList();
4022 if (section_list)
4023 {
4024 static ConstString g_kld_section_name ("__KLD");
4025 if (section_list->FindSectionByName(g_kld_section_name))
4026 return eStrataKernel;
4027 }
4028 }
4029 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00004030
4031 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4032 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00004033 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00004034 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4035 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4036 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4037 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4038 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4039 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4040 default:
4041 break;
4042 }
4043 return eStrataUnknown;
4044}
4045
4046
Greg Clayton49f4bf22012-02-22 19:41:02 +00004047uint32_t
4048ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4049{
Greg Clayton9482f052012-03-13 23:14:29 +00004050 ModuleSP module_sp(GetModule());
4051 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004052 {
Greg Clayton9482f052012-03-13 23:14:29 +00004053 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4054 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00004055 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00004056 uint32_t version_cmd = 0;
4057 uint64_t version = 0;
4058 uint32_t i;
4059 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004060 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00004061 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00004062 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4063 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00004064
Greg Clayton9482f052012-03-13 23:14:29 +00004065 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004066 {
Greg Clayton9482f052012-03-13 23:14:29 +00004067 if (version_cmd == 0)
4068 {
4069 version_cmd = load_cmd.cmd;
4070 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4071 break;
4072 version = load_cmd.dylib.current_version;
4073 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004074 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00004075 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00004076 }
Greg Clayton9482f052012-03-13 23:14:29 +00004077 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004078 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004079
Greg Clayton9482f052012-03-13 23:14:29 +00004080 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004081 {
Greg Clayton9482f052012-03-13 23:14:29 +00004082 if (versions != NULL && num_versions > 0)
4083 {
4084 if (num_versions > 0)
4085 versions[0] = (version & 0xFFFF0000ull) >> 16;
4086 if (num_versions > 1)
4087 versions[1] = (version & 0x0000FF00ull) >> 8;
4088 if (num_versions > 2)
4089 versions[2] = (version & 0x000000FFull);
4090 // Fill in an remaining version numbers with invalid values
4091 for (i=3; i<num_versions; ++i)
4092 versions[i] = UINT32_MAX;
4093 }
4094 // The LC_ID_DYLIB load command has a version with 3 version numbers
4095 // in it, so always return 3
4096 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004097 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00004098 }
4099 return false;
4100}
4101
Chris Lattner24943d22010-06-08 16:52:24 +00004102bool
Greg Clayton395fc332011-02-15 21:59:32 +00004103ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00004104{
Greg Clayton9482f052012-03-13 23:14:29 +00004105 ModuleSP module_sp(GetModule());
4106 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004107 {
Greg Clayton9482f052012-03-13 23:14:29 +00004108 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4109 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00004110
Greg Clayton9482f052012-03-13 23:14:29 +00004111 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00004112 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00004113 // unknown to make sure we use the DynamicLoaderStatic()...
4114 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4115 {
4116 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4117 }
4118 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004119 }
Greg Clayton9482f052012-03-13 23:14:29 +00004120 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00004121}
4122
4123
Jason Molenda45c75502013-04-16 06:24:42 +00004124UUID
4125ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4126{
4127 UUID uuid;
4128 if (process)
4129 {
4130 addr_t all_image_infos = process->GetImageInfoAddress();
4131
4132 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4133 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4134 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4135 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4136
4137 Error err;
4138 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4139 if (version_or_magic != -1
4140 && version_or_magic != HeaderMagic32
4141 && version_or_magic != HeaderMagic32Swapped
4142 && version_or_magic != HeaderMagic64
4143 && version_or_magic != HeaderMagic64Swapped
4144 && version_or_magic >= 13)
4145 {
4146 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4147 int wordsize = process->GetAddressByteSize();
4148 if (wordsize == 8)
4149 {
4150 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4151 }
4152 if (wordsize == 4)
4153 {
4154 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4155 }
4156 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4157 {
4158 uuid_t shared_cache_uuid;
4159 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4160 {
4161 uuid.SetBytes (shared_cache_uuid);
4162 }
4163 }
4164 }
4165 }
4166 return uuid;
4167}
4168
4169UUID
4170ObjectFileMachO::GetLLDBSharedCacheUUID ()
4171{
4172 UUID uuid;
4173#if defined (__APPLE__) && defined (__arm__)
4174 uint8_t *(*dyld_get_all_image_infos)(void);
4175 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4176 if (dyld_get_all_image_infos)
4177 {
4178 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4179 if (dyld_all_image_infos_address)
4180 {
Jason Molenda6ff916f2013-04-16 21:42:58 +00004181 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4182 if (*version >= 13)
Jason Molenda45c75502013-04-16 06:24:42 +00004183 {
Jason Molenda2ceae992013-04-16 22:56:17 +00004184 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 +00004185 uuid.SetBytes (sharedCacheUUID_address);
4186 }
4187 }
4188 }
4189#endif
4190 return uuid;
4191}
4192
4193
Chris Lattner24943d22010-06-08 16:52:24 +00004194//------------------------------------------------------------------
4195// PluginInterface protocol
4196//------------------------------------------------------------------
Greg Clayton0e191602013-05-10 21:47:16 +00004197lldb_private::ConstString
Chris Lattner24943d22010-06-08 16:52:24 +00004198ObjectFileMachO::GetPluginName()
4199{
Chris Lattner24943d22010-06-08 16:52:24 +00004200 return GetPluginNameStatic();
4201}
4202
4203uint32_t
4204ObjectFileMachO::GetPluginVersion()
4205{
4206 return 1;
4207}
4208