blob: dc30be6877a032e888d7c3b82d1866418d671fa8 [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);
Jason Molenda0c7c90a2013-05-14 04:50:47 +0000298 lldb::offset_t next_thread_state = offset + (count * 4);
Jason Molenda3b244b72013-05-14 03:25:58 +0000299 switch (flavor)
300 {
301 case GPRRegSet:
302 for (uint32_t i=0; i<count; ++i)
Jason Molenda0c7c90a2013-05-14 04:50:47 +0000303 {
Jason Molenda3b244b72013-05-14 03:25:58 +0000304 gpr.r[i] = data.GetU32(&offset);
Jason Molenda0c7c90a2013-05-14 04:50:47 +0000305 }
306
307 // Note that gpr.cpsr is also copied by the above loop; this loop technically extends
308 // one element past the end of the gpr.r[] array.
309
Jason Molenda3b244b72013-05-14 03:25:58 +0000310 SetError (GPRRegSet, Read, 0);
Jason Molenda0c7c90a2013-05-14 04:50:47 +0000311 offset = next_thread_state;
Jason Molenda3b244b72013-05-14 03:25:58 +0000312 break;
313
314 case FPURegSet:
315 {
Jason Molenda2f038762013-05-14 03:52:22 +0000316 uint8_t *fpu_reg_buf = (uint8_t*) &fpu.floats.s[0];
317 const int fpu_reg_buf_size = sizeof (fpu.floats);
318 if (data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
Jason Molenda3b244b72013-05-14 03:25:58 +0000319 {
Jason Molenda2f038762013-05-14 03:52:22 +0000320 offset += fpu_reg_buf_size;
321 fpu.fpscr = data.GetU32(&offset);
322 SetError (FPURegSet, Read, 0);
Jason Molenda3b244b72013-05-14 03:25:58 +0000323 }
Jason Molenda2f038762013-05-14 03:52:22 +0000324 else
325 {
326 done = true;
327 }
Jason Molenda3b244b72013-05-14 03:25:58 +0000328 }
Jason Molenda0c7c90a2013-05-14 04:50:47 +0000329 offset = next_thread_state;
Jason Molenda3b244b72013-05-14 03:25:58 +0000330 break;
331
332 case EXCRegSet:
Jason Molenda0c7c90a2013-05-14 04:50:47 +0000333 if (count == 3)
334 {
335 exc.exception = data.GetU32(&offset);
336 exc.fsr = data.GetU32(&offset);
337 exc.far = data.GetU32(&offset);
338 SetError (EXCRegSet, Read, 0);
339 }
Jason Molenda3b244b72013-05-14 03:25:58 +0000340 done = true;
Jason Molenda0c7c90a2013-05-14 04:50:47 +0000341 offset = next_thread_state;
Jason Molenda3b244b72013-05-14 03:25:58 +0000342 break;
343
344 // Unknown register set flavor, stop trying to parse.
345 default:
346 done = true;
347 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000348 }
349 }
350protected:
351 virtual int
352 DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr)
353 {
Jason Molenda0c7c90a2013-05-14 04:50:47 +0000354 return -1;
Greg Clayton9ce95382012-02-13 23:10:39 +0000355 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000356
Greg Clayton9ce95382012-02-13 23:10:39 +0000357 virtual int
358 DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu)
359 {
Jason Molenda0c7c90a2013-05-14 04:50:47 +0000360 return -1;
Greg Clayton9ce95382012-02-13 23:10:39 +0000361 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000362
Greg Clayton9ce95382012-02-13 23:10:39 +0000363 virtual int
364 DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc)
365 {
Jason Molenda0c7c90a2013-05-14 04:50:47 +0000366 return -1;
Greg Clayton9ce95382012-02-13 23:10:39 +0000367 }
Greg Claytonb5431d02012-10-30 23:57:32 +0000368
369 virtual int
370 DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg)
371 {
372 return -1;
373 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000374
Greg Clayton9ce95382012-02-13 23:10:39 +0000375 virtual int
376 DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr)
377 {
378 return 0;
379 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000380
Greg Clayton9ce95382012-02-13 23:10:39 +0000381 virtual int
382 DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu)
383 {
384 return 0;
385 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000386
Greg Clayton9ce95382012-02-13 23:10:39 +0000387 virtual int
388 DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc)
389 {
390 return 0;
391 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000392
Greg Claytonb5431d02012-10-30 23:57:32 +0000393 virtual int
394 DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg)
395 {
396 return -1;
397 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000398};
399
Greg Claytonb1888f22011-03-19 01:12:21 +0000400#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000401
402void
403ObjectFileMachO::Initialize()
404{
405 PluginManager::RegisterPlugin (GetPluginNameStatic(),
406 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000407 CreateInstance,
Greg Clayton36b877d2013-04-24 22:29:28 +0000408 CreateMemoryInstance,
409 GetModuleSpecifications);
Chris Lattner24943d22010-06-08 16:52:24 +0000410}
411
412void
413ObjectFileMachO::Terminate()
414{
415 PluginManager::UnregisterPlugin (CreateInstance);
416}
417
418
Greg Clayton0e191602013-05-10 21:47:16 +0000419lldb_private::ConstString
Chris Lattner24943d22010-06-08 16:52:24 +0000420ObjectFileMachO::GetPluginNameStatic()
421{
Greg Clayton0e191602013-05-10 21:47:16 +0000422 static ConstString g_name("mach-o");
423 return g_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000424}
425
426const char *
427ObjectFileMachO::GetPluginDescriptionStatic()
428{
429 return "Mach-o object file reader (32 and 64 bit)";
430}
431
Chris Lattner24943d22010-06-08 16:52:24 +0000432ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000433ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
434 DataBufferSP& data_sp,
435 lldb::offset_t data_offset,
436 const FileSpec* file,
437 lldb::offset_t file_offset,
438 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000439{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000440 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000441 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000442 data_sp = file->MemoryMapFileContents(file_offset, length);
443 data_offset = 0;
444 }
445
446 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
447 {
448 // Update the data to contain the entire file if it doesn't already
449 if (data_sp->GetByteSize() < length)
450 {
451 data_sp = file->MemoryMapFileContents(file_offset, length);
452 data_offset = 0;
453 }
Greg Clayton102b2c22013-04-18 22:45:39 +0000454 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 +0000455 if (objfile_ap.get() && objfile_ap->ParseHeader())
456 return objfile_ap.release();
457 }
458 return NULL;
459}
460
Greg Claytonb5a8f142012-02-05 02:38:54 +0000461ObjectFile *
Jason Molenda9badb6c2013-03-06 23:19:17 +0000462ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
463 DataBufferSP& data_sp,
464 const ProcessSP &process_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000465 lldb::addr_t header_addr)
466{
467 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
468 {
Greg Clayton102b2c22013-04-18 22:45:39 +0000469 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000470 if (objfile_ap.get() && objfile_ap->ParseHeader())
471 return objfile_ap.release();
472 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000473 return NULL;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000474}
475
Greg Clayton36b877d2013-04-24 22:29:28 +0000476size_t
477ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
478 lldb::DataBufferSP& data_sp,
479 lldb::offset_t data_offset,
480 lldb::offset_t file_offset,
481 lldb::offset_t length,
482 lldb_private::ModuleSpecList &specs)
483{
484 const size_t initial_count = specs.GetSize();
485
486 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
487 {
488 DataExtractor data;
489 data.SetData(data_sp);
490 llvm::MachO::mach_header header;
491 if (ParseHeader (data, &data_offset, header))
492 {
493 if (header.sizeofcmds >= data_sp->GetByteSize())
494 {
495 data_sp = file.ReadFileContents(file_offset, header.sizeofcmds);
496 data_offset = 0;
497 }
498 if (data_sp)
499 {
500 ModuleSpec spec;
501 spec.GetFileSpec() = file;
502 spec.GetArchitecture().SetArchitecture(eArchTypeMachO,
503 header.cputype,
504 header.cpusubtype);
505 if (spec.GetArchitecture().IsValid())
506 {
507 GetUUID (header, data, data_offset, spec.GetUUID());
508 specs.Append(spec);
509 }
510 }
511 }
512 }
513 return specs.GetSize() - initial_count;
514}
515
516
Greg Claytonb5a8f142012-02-05 02:38:54 +0000517
518const ConstString &
519ObjectFileMachO::GetSegmentNameTEXT()
520{
521 static ConstString g_segment_name_TEXT ("__TEXT");
522 return g_segment_name_TEXT;
523}
524
525const ConstString &
526ObjectFileMachO::GetSegmentNameDATA()
527{
528 static ConstString g_segment_name_DATA ("__DATA");
529 return g_segment_name_DATA;
530}
531
532const ConstString &
533ObjectFileMachO::GetSegmentNameOBJC()
534{
535 static ConstString g_segment_name_OBJC ("__OBJC");
536 return g_segment_name_OBJC;
537}
538
539const ConstString &
540ObjectFileMachO::GetSegmentNameLINKEDIT()
541{
542 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
543 return g_section_name_LINKEDIT;
544}
545
546const ConstString &
547ObjectFileMachO::GetSectionNameEHFrame()
548{
549 static ConstString g_section_name_eh_frame ("__eh_frame");
550 return g_section_name_eh_frame;
551}
552
553
Chris Lattner24943d22010-06-08 16:52:24 +0000554
555static uint32_t
556MachHeaderSizeFromMagic(uint32_t magic)
557{
558 switch (magic)
559 {
Greg Clayton1674b122010-07-21 22:12:05 +0000560 case HeaderMagic32:
561 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000562 return sizeof(struct mach_header);
563
Greg Clayton1674b122010-07-21 22:12:05 +0000564 case HeaderMagic64:
565 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000566 return sizeof(struct mach_header_64);
567 break;
568
569 default:
570 break;
571 }
572 return 0;
573}
574
575
576bool
Jason Molenda9badb6c2013-03-06 23:19:17 +0000577ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
578 lldb::addr_t data_offset,
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000579 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000580{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000581 DataExtractor data;
582 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000583 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000584 uint32_t magic = data.GetU32(&offset);
585 return MachHeaderSizeFromMagic(magic) != 0;
586}
587
588
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000589ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
590 DataBufferSP& data_sp,
591 lldb::offset_t data_offset,
592 const FileSpec* file,
593 lldb::offset_t file_offset,
594 lldb::offset_t length) :
595 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Clayton46c9a352012-02-09 06:16:32 +0000596 m_mach_segments(),
597 m_mach_sections(),
598 m_entry_point_address(),
599 m_thread_context_offsets(),
600 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000601{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000602 ::memset (&m_header, 0, sizeof(m_header));
603 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000604}
605
Greg Clayton3508c382012-02-24 01:59:29 +0000606ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000607 lldb::DataBufferSP& header_data_sp,
608 const lldb::ProcessSP &process_sp,
609 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000610 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Clayton46c9a352012-02-09 06:16:32 +0000611 m_mach_segments(),
612 m_mach_sections(),
613 m_entry_point_address(),
614 m_thread_context_offsets(),
615 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000616{
617 ::memset (&m_header, 0, sizeof(m_header));
618 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
619}
Chris Lattner24943d22010-06-08 16:52:24 +0000620
621ObjectFileMachO::~ObjectFileMachO()
622{
623}
624
Greg Clayton36b877d2013-04-24 22:29:28 +0000625bool
626ObjectFileMachO::ParseHeader (DataExtractor &data,
627 lldb::offset_t *data_offset_ptr,
628 llvm::MachO::mach_header &header)
629{
630 data.SetByteOrder (lldb::endian::InlHostByteOrder());
631 // Leave magic in the original byte order
632 header.magic = data.GetU32(data_offset_ptr);
633 bool can_parse = false;
634 bool is_64_bit = false;
635 switch (header.magic)
636 {
637 case HeaderMagic32:
638 data.SetByteOrder (lldb::endian::InlHostByteOrder());
639 data.SetAddressByteSize(4);
640 can_parse = true;
641 break;
642
643 case HeaderMagic64:
644 data.SetByteOrder (lldb::endian::InlHostByteOrder());
645 data.SetAddressByteSize(8);
646 can_parse = true;
647 is_64_bit = true;
648 break;
649
650 case HeaderMagic32Swapped:
651 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
652 data.SetAddressByteSize(4);
653 can_parse = true;
654 break;
655
656 case HeaderMagic64Swapped:
657 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
658 data.SetAddressByteSize(8);
659 is_64_bit = true;
660 can_parse = true;
661 break;
662
663 default:
664 break;
665 }
666
667 if (can_parse)
668 {
669 data.GetU32(data_offset_ptr, &header.cputype, 6);
670 if (is_64_bit)
671 *data_offset_ptr += 4;
672 return true;
673 }
674 else
675 {
676 memset(&header, 0, sizeof(header));
677 }
678 return false;
679}
Chris Lattner24943d22010-06-08 16:52:24 +0000680
681bool
682ObjectFileMachO::ParseHeader ()
683{
Greg Clayton9482f052012-03-13 23:14:29 +0000684 ModuleSP module_sp(GetModule());
685 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000686 {
Greg Clayton9482f052012-03-13 23:14:29 +0000687 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
688 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000689 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000690 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000691 // Leave magic in the original byte order
692 m_header.magic = m_data.GetU32(&offset);
693 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000694 {
Greg Clayton9482f052012-03-13 23:14:29 +0000695 case HeaderMagic32:
696 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
697 m_data.SetAddressByteSize(4);
698 can_parse = true;
699 break;
700
701 case HeaderMagic64:
702 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
703 m_data.SetAddressByteSize(8);
704 can_parse = true;
705 break;
706
707 case HeaderMagic32Swapped:
708 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
709 m_data.SetAddressByteSize(4);
710 can_parse = true;
711 break;
712
713 case HeaderMagic64Swapped:
714 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
715 m_data.SetAddressByteSize(8);
716 can_parse = true;
717 break;
718
719 default:
720 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000721 }
Greg Clayton9482f052012-03-13 23:14:29 +0000722
723 if (can_parse)
724 {
725 m_data.GetU32(&offset, &m_header.cputype, 6);
726
727 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +0000728
Greg Clayton21a25432012-11-16 21:36:10 +0000729 // Check if the module has a required architecture
730 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000731 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000732 return false;
733
Greg Clayton9482f052012-03-13 23:14:29 +0000734 if (SetModulesArchitecture (mach_arch))
735 {
736 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
737 if (m_data.GetByteSize() < header_and_lc_size)
738 {
739 DataBufferSP data_sp;
740 ProcessSP process_sp (m_process_wp.lock());
741 if (process_sp)
742 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000743 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000744 }
745 else
746 {
747 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000748 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000749 if (data_sp->GetByteSize() != header_and_lc_size)
750 return false;
751 }
752 if (data_sp)
753 m_data.SetData (data_sp);
754 }
755 }
756 return true;
757 }
758 else
759 {
760 memset(&m_header, 0, sizeof(struct mach_header));
761 }
Chris Lattner24943d22010-06-08 16:52:24 +0000762 }
763 return false;
764}
765
766
767ByteOrder
768ObjectFileMachO::GetByteOrder () const
769{
Chris Lattner24943d22010-06-08 16:52:24 +0000770 return m_data.GetByteOrder ();
771}
772
Jim Ingham7508e732010-08-09 23:31:02 +0000773bool
774ObjectFileMachO::IsExecutable() const
775{
776 return m_header.filetype == HeaderFileTypeExecutable;
777}
Chris Lattner24943d22010-06-08 16:52:24 +0000778
Greg Clayton36da2aa2013-01-25 18:06:21 +0000779uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000780ObjectFileMachO::GetAddressByteSize () const
781{
Chris Lattner24943d22010-06-08 16:52:24 +0000782 return m_data.GetAddressByteSize ();
783}
784
Greg Claytonb3448432011-03-24 21:19:54 +0000785AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000786ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
787{
788 Symtab *symtab = GetSymtab();
789 if (symtab)
790 {
791 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
792 if (symbol)
793 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000794 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000795 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000796 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000797 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000798 {
Greg Clayton3508c382012-02-24 01:59:29 +0000799 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000800 switch (section_type)
801 {
802 case eSectionTypeInvalid: return eAddressClassUnknown;
803 case eSectionTypeCode:
804 if (m_header.cputype == llvm::MachO::CPUTypeARM)
805 {
806 // For ARM we have a bit in the n_desc field of the symbol
807 // that tells us ARM/Thumb which is bit 0x0008.
808 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
809 return eAddressClassCodeAlternateISA;
810 }
811 return eAddressClassCode;
812
813 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000814 case eSectionTypeData:
815 case eSectionTypeDataCString:
816 case eSectionTypeDataCStringPointers:
817 case eSectionTypeDataSymbolAddress:
818 case eSectionTypeData4:
819 case eSectionTypeData8:
820 case eSectionTypeData16:
821 case eSectionTypeDataPointers:
822 case eSectionTypeZeroFill:
823 case eSectionTypeDataObjCMessageRefs:
824 case eSectionTypeDataObjCCFStrings:
825 return eAddressClassData;
826 case eSectionTypeDebug:
827 case eSectionTypeDWARFDebugAbbrev:
828 case eSectionTypeDWARFDebugAranges:
829 case eSectionTypeDWARFDebugFrame:
830 case eSectionTypeDWARFDebugInfo:
831 case eSectionTypeDWARFDebugLine:
832 case eSectionTypeDWARFDebugLoc:
833 case eSectionTypeDWARFDebugMacInfo:
834 case eSectionTypeDWARFDebugPubNames:
835 case eSectionTypeDWARFDebugPubTypes:
836 case eSectionTypeDWARFDebugRanges:
837 case eSectionTypeDWARFDebugStr:
838 case eSectionTypeDWARFAppleNames:
839 case eSectionTypeDWARFAppleTypes:
840 case eSectionTypeDWARFAppleNamespaces:
841 case eSectionTypeDWARFAppleObjC:
842 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000843 case eSectionTypeEHFrame: return eAddressClassRuntime;
844 case eSectionTypeOther: return eAddressClassUnknown;
845 }
846 }
847 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000848
Greg Claytonb3448432011-03-24 21:19:54 +0000849 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000850 switch (symbol_type)
851 {
852 case eSymbolTypeAny: return eAddressClassUnknown;
853 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000854
Greg Claytonb1888f22011-03-19 01:12:21 +0000855 case eSymbolTypeCode:
856 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000857 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000858 if (m_header.cputype == llvm::MachO::CPUTypeARM)
859 {
860 // For ARM we have a bit in the n_desc field of the symbol
861 // that tells us ARM/Thumb which is bit 0x0008.
862 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
863 return eAddressClassCodeAlternateISA;
864 }
865 return eAddressClassCode;
866
867 case eSymbolTypeData: return eAddressClassData;
868 case eSymbolTypeRuntime: return eAddressClassRuntime;
869 case eSymbolTypeException: return eAddressClassRuntime;
870 case eSymbolTypeSourceFile: return eAddressClassDebug;
871 case eSymbolTypeHeaderFile: return eAddressClassDebug;
872 case eSymbolTypeObjectFile: return eAddressClassDebug;
873 case eSymbolTypeCommonBlock: return eAddressClassDebug;
874 case eSymbolTypeBlock: return eAddressClassDebug;
875 case eSymbolTypeLocal: return eAddressClassData;
876 case eSymbolTypeParam: return eAddressClassData;
877 case eSymbolTypeVariable: return eAddressClassData;
878 case eSymbolTypeVariableType: return eAddressClassDebug;
879 case eSymbolTypeLineEntry: return eAddressClassDebug;
880 case eSymbolTypeLineHeader: return eAddressClassDebug;
881 case eSymbolTypeScopeBegin: return eAddressClassDebug;
882 case eSymbolTypeScopeEnd: return eAddressClassDebug;
883 case eSymbolTypeAdditional: return eAddressClassUnknown;
884 case eSymbolTypeCompiler: return eAddressClassDebug;
885 case eSymbolTypeInstrumentation:return eAddressClassDebug;
886 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000887 case eSymbolTypeObjCClass: return eAddressClassRuntime;
888 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
889 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000890 }
891 }
892 }
893 return eAddressClassUnknown;
894}
Chris Lattner24943d22010-06-08 16:52:24 +0000895
896Symtab *
897ObjectFileMachO::GetSymtab()
898{
Greg Clayton9482f052012-03-13 23:14:29 +0000899 ModuleSP module_sp(GetModule());
900 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000901 {
Greg Clayton9482f052012-03-13 23:14:29 +0000902 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
903 if (m_symtab_ap.get() == NULL)
904 {
905 m_symtab_ap.reset(new Symtab(this));
906 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
907 ParseSymtab (true);
908 m_symtab_ap->Finalize ();
909 }
Chris Lattner24943d22010-06-08 16:52:24 +0000910 }
911 return m_symtab_ap.get();
912}
913
914
915SectionList *
916ObjectFileMachO::GetSectionList()
917{
Greg Clayton9482f052012-03-13 23:14:29 +0000918 ModuleSP module_sp(GetModule());
919 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000920 {
Greg Clayton9482f052012-03-13 23:14:29 +0000921 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
922 if (m_sections_ap.get() == NULL)
923 {
924 m_sections_ap.reset(new SectionList());
925 ParseSections();
926 }
Chris Lattner24943d22010-06-08 16:52:24 +0000927 }
928 return m_sections_ap.get();
929}
930
931
932size_t
933ObjectFileMachO::ParseSections ()
934{
935 lldb::user_id_t segID = 0;
936 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000937 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000938 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000939 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000940 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000941 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000942 // First look up any LC_ENCRYPTION_INFO load commands
943 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
944 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000945 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000946 for (i=0; i<m_header.ncmds; ++i)
947 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000948 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000949 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000950 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000951
Greg Clayton54e33712012-05-25 18:09:55 +0000952 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000953 {
Greg Clayton54e33712012-05-25 18:09:55 +0000954 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
955 {
956 if (encryption_cmd.cryptid != 0)
957 {
958 EncryptedFileRanges::Entry entry;
959 entry.SetRangeBase(encryption_cmd.cryptoff);
960 entry.SetByteSize(encryption_cmd.cryptsize);
961 encrypted_file_ranges.Append(entry);
962 }
963 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000964 }
Greg Clayton54e33712012-05-25 18:09:55 +0000965 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000966 }
967
968 offset = MachHeaderSizeFromMagic(m_header.magic);
969
Greg Clayton54e33712012-05-25 18:09:55 +0000970 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000971 for (i=0; i<m_header.ncmds; ++i)
972 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000973 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000974 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
975 break;
976
Greg Clayton1674b122010-07-21 22:12:05 +0000977 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000978 {
979 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
980 {
981 load_cmd.vmaddr = m_data.GetAddress(&offset);
982 load_cmd.vmsize = m_data.GetAddress(&offset);
983 load_cmd.fileoff = m_data.GetAddress(&offset);
984 load_cmd.filesize = m_data.GetAddress(&offset);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000985 if (m_length != 0 && load_cmd.filesize != 0)
986 {
Greg Claytonbb759862013-04-16 16:51:19 +0000987 if (load_cmd.fileoff > m_length)
988 {
989 // We have a load command that says it extends past the end of hte file. This is likely
990 // a corrupt file. We don't have any way to return an error condition here (this method
991 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
992 // is null out the SectionList vector and if a process has been set up, dump a message
993 // to stdout. The most common case here is core file debugging with a truncated file.
994 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
995 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 ")",
996 i,
997 lc_segment_name,
998 load_cmd.fileoff,
999 m_length);
1000 m_sections_ap->Clear();
1001 return 0;
1002 }
1003
Jason Molendaadf9e3d2013-04-10 05:58:57 +00001004 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1005 {
1006 // We have a load command that says it extends past the end of hte file. This is likely
1007 // a corrupt file. We don't have any way to return an error condition here (this method
1008 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1009 // is null out the SectionList vector and if a process has been set up, dump a message
Greg Claytonbb759862013-04-16 16:51:19 +00001010 // to stdout. The most common case here is core file debugging with a truncated file.
1011 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
1012 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 ")",
1013 i,
1014 lc_segment_name,
1015 load_cmd.fileoff + load_cmd.filesize,
1016 m_length);
Jason Molendaadf9e3d2013-04-10 05:58:57 +00001017 m_sections_ap->Clear();
1018 return 0;
1019 }
1020 }
Chris Lattner24943d22010-06-08 16:52:24 +00001021 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1022 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001023
Greg Clayton68ca8232011-01-25 02:58:48 +00001024 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
1025
Chris Lattner24943d22010-06-08 16:52:24 +00001026 // Keep a list of mach segments around in case we need to
1027 // get at data that isn't stored in the abstracted Sections.
1028 m_mach_segments.push_back (load_cmd);
1029
Greg Clayton36da2aa2013-01-25 18:06:21 +00001030 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 +00001031 // Use a segment ID of the segment index shifted left by 8 so they
1032 // never conflict with any of the sections.
1033 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +00001034 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +00001035 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001036 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +00001037 ++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
1038 segment_name, // Name of this section
1039 eSectionTypeContainer, // This section is a container of other sections.
1040 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1041 load_cmd.vmsize, // VM size in bytes of this section
1042 load_cmd.fileoff, // Offset to the data for this section in the file
1043 load_cmd.filesize, // Size in bytes of this section as found in the the file
1044 load_cmd.flags)); // Flags for this section
1045
Greg Clayton68ca8232011-01-25 02:58:48 +00001046 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001047 m_sections_ap->AddSection(segment_sp);
1048 }
1049
1050 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +00001051 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +00001052 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +00001053 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +00001054 // mach sections yet...
1055 if (m_mach_sections.empty())
1056 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +00001057 uint32_t segment_sect_idx;
1058 const lldb::user_id_t first_segment_sectID = sectID + 1;
1059
1060
Greg Clayton1674b122010-07-21 22:12:05 +00001061 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +00001062 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
1063 {
1064 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
1065 break;
1066 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1067 break;
1068 sect64.addr = m_data.GetAddress(&offset);
1069 sect64.size = m_data.GetAddress(&offset);
1070
1071 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1072 break;
1073
1074 // Keep a list of mach sections around in case we need to
1075 // get at data that isn't stored in the abstracted Sections.
1076 m_mach_sections.push_back (sect64);
1077
1078 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1079 if (!segment_name)
1080 {
1081 // We have a segment with no name so we need to conjure up
1082 // segments that correspond to the section's segname if there
1083 // isn't already such a section. If there is such a section,
1084 // we resize the section so that it spans all sections.
1085 // We also mark these sections as fake so address matches don't
1086 // hit if they land in the gaps between the child sections.
1087 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1088 segment_sp = m_sections_ap->FindSectionByName (segment_name);
1089 if (segment_sp.get())
1090 {
1091 Section *segment = segment_sp.get();
1092 // Grow the section size as needed.
1093 const lldb::addr_t sect64_min_addr = sect64.addr;
1094 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1095 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1096 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1097 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1098 if (sect64_min_addr >= curr_seg_min_addr)
1099 {
1100 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1101 // Only grow the section size if needed
1102 if (new_seg_byte_size > curr_seg_byte_size)
1103 segment->SetByteSize (new_seg_byte_size);
1104 }
1105 else
1106 {
1107 // We need to change the base address of the segment and
1108 // adjust the child section offsets for all existing children.
1109 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1110 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +00001111 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +00001112 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1113 }
Greg Clayton661825b2010-06-28 23:51:11 +00001114
1115 // Grow the section size as needed.
1116 if (sect64.offset)
1117 {
1118 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1119 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1120
1121 const lldb::addr_t section_min_file_offset = sect64.offset;
1122 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1123 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1124 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1125 segment->SetFileOffset (new_file_offset);
1126 segment->SetFileSize (new_file_size);
1127 }
Chris Lattner24943d22010-06-08 16:52:24 +00001128 }
1129 else
1130 {
1131 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +00001132 segment_sp.reset(new Section (segment_sp, // Parent section
1133 module_sp, // Module to which this section belongs
1134 ++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
1135 segment_name, // Name of this section
1136 eSectionTypeContainer, // This section is a container of other sections.
1137 sect64.addr, // File VM address == addresses as they are found in the object file
1138 sect64.size, // VM size in bytes of this section
1139 sect64.offset, // Offset to the data for this section in the file
1140 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1141 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +00001142 segment_sp->SetIsFake(true);
1143 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +00001144 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001145 }
1146 }
1147 assert (segment_sp.get());
1148
Greg Clayton1674b122010-07-21 22:12:05 +00001149 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +00001150 static ConstString g_sect_name_objc_data ("__objc_data");
1151 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1152 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1153 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1154 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1155 static ConstString g_sect_name_objc_const ("__objc_const");
1156 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1157 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001158
1159 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1160 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1161 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1162 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1163 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1164 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1165 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1166 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1167 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1168 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1169 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +00001170 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1171 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +00001172 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +00001173 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001174 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +00001175 static ConstString g_sect_name_DATA ("__DATA");
1176 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001177
Chris Lattner24943d22010-06-08 16:52:24 +00001178 SectionType sect_type = eSectionTypeOther;
1179
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001180 if (section_name == g_sect_name_dwarf_debug_abbrev)
1181 sect_type = eSectionTypeDWARFDebugAbbrev;
1182 else if (section_name == g_sect_name_dwarf_debug_aranges)
1183 sect_type = eSectionTypeDWARFDebugAranges;
1184 else if (section_name == g_sect_name_dwarf_debug_frame)
1185 sect_type = eSectionTypeDWARFDebugFrame;
1186 else if (section_name == g_sect_name_dwarf_debug_info)
1187 sect_type = eSectionTypeDWARFDebugInfo;
1188 else if (section_name == g_sect_name_dwarf_debug_line)
1189 sect_type = eSectionTypeDWARFDebugLine;
1190 else if (section_name == g_sect_name_dwarf_debug_loc)
1191 sect_type = eSectionTypeDWARFDebugLoc;
1192 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1193 sect_type = eSectionTypeDWARFDebugMacInfo;
1194 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1195 sect_type = eSectionTypeDWARFDebugPubNames;
1196 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1197 sect_type = eSectionTypeDWARFDebugPubTypes;
1198 else if (section_name == g_sect_name_dwarf_debug_ranges)
1199 sect_type = eSectionTypeDWARFDebugRanges;
1200 else if (section_name == g_sect_name_dwarf_debug_str)
1201 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001202 else if (section_name == g_sect_name_dwarf_apple_names)
1203 sect_type = eSectionTypeDWARFAppleNames;
1204 else if (section_name == g_sect_name_dwarf_apple_types)
1205 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001206 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1207 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001208 else if (section_name == g_sect_name_dwarf_apple_objc)
1209 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001210 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001211 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001212 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001213 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001214 else if (section_name == g_sect_name_eh_frame)
1215 sect_type = eSectionTypeEHFrame;
1216 else if (section_name == g_sect_name_cfstring)
1217 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001218 else if (section_name == g_sect_name_objc_data ||
1219 section_name == g_sect_name_objc_classrefs ||
1220 section_name == g_sect_name_objc_superrefs ||
1221 section_name == g_sect_name_objc_const ||
1222 section_name == g_sect_name_objc_classlist)
1223 {
1224 sect_type = eSectionTypeDataPointers;
1225 }
Chris Lattner24943d22010-06-08 16:52:24 +00001226
1227 if (sect_type == eSectionTypeOther)
1228 {
1229 switch (mach_sect_type)
1230 {
1231 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001232 case SectionTypeRegular:
1233 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001234 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001235 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001236 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001237 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001238 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001239 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001240 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1241 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1242 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1243 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1244 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1245 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1246 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1247 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1248 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1249 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1250 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1251 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1252 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1253 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1254 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1255 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001256 default: break;
1257 }
1258 }
1259
Greg Clayton3508c382012-02-24 01:59:29 +00001260 SectionSP section_sp(new Section (segment_sp,
1261 module_sp,
1262 ++sectID,
1263 section_name,
1264 sect_type,
1265 sect64.addr - segment_sp->GetFileAddress(),
1266 sect64.size,
1267 sect64.offset,
1268 sect64.offset == 0 ? 0 : sect64.size,
1269 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001270 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001271
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001272 bool section_is_encrypted = false;
1273 if (!segment_is_encrypted && load_cmd.filesize != 0)
1274 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001275
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001276 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001277 segment_sp->GetChildren().AddSection(section_sp);
1278
1279 if (segment_sp->IsFake())
1280 {
1281 segment_sp.reset();
1282 segment_name.Clear();
1283 }
1284 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001285 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001286 {
1287 if (first_segment_sectID <= sectID)
1288 {
1289 lldb::user_id_t sect_uid;
1290 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1291 {
1292 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1293 SectionSP next_section_sp;
1294 if (sect_uid + 1 <= sectID)
1295 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1296
1297 if (curr_section_sp.get())
1298 {
1299 if (curr_section_sp->GetByteSize() == 0)
1300 {
1301 if (next_section_sp.get() != NULL)
1302 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1303 else
1304 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1305 }
1306 }
1307 }
1308 }
1309 }
1310 }
1311 }
1312 }
Greg Clayton1674b122010-07-21 22:12:05 +00001313 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001314 {
1315 m_dysymtab.cmd = load_cmd.cmd;
1316 m_dysymtab.cmdsize = load_cmd.cmdsize;
1317 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1318 }
1319
1320 offset = load_cmd_offset + load_cmd.cmdsize;
1321 }
1322// if (dump_sections)
1323// {
1324// StreamFile s(stdout);
1325// m_sections_ap->Dump(&s, true);
1326// }
1327 return sectID; // Return the number of sections we registered with the module
1328}
1329
1330class MachSymtabSectionInfo
1331{
1332public:
1333
1334 MachSymtabSectionInfo (SectionList *section_list) :
1335 m_section_list (section_list),
1336 m_section_infos()
1337 {
1338 // Get the number of sections down to a depth of 1 to include
1339 // all segments and their sections, but no other sections that
1340 // may be added for debug map or
1341 m_section_infos.resize(section_list->GetNumSections(1));
1342 }
1343
1344
Greg Clayton3508c382012-02-24 01:59:29 +00001345 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001346 GetSection (uint8_t n_sect, addr_t file_addr)
1347 {
1348 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001349 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001350 if (n_sect < m_section_infos.size())
1351 {
Greg Clayton3508c382012-02-24 01:59:29 +00001352 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001353 {
Greg Clayton3508c382012-02-24 01:59:29 +00001354 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1355 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001356 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001357 {
Greg Clayton3508c382012-02-24 01:59:29 +00001358 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1359 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001360 }
1361 else
1362 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001363 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001364 }
Chris Lattner24943d22010-06-08 16:52:24 +00001365 }
1366 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001367 {
1368 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001369 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001370 }
1371 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1372 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1373 {
1374 // Symbol is in section with zero size, but has the same start
1375 // address as the section. This can happen with linker symbols
1376 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001377 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001378 }
Chris Lattner24943d22010-06-08 16:52:24 +00001379 }
Greg Clayton3508c382012-02-24 01:59:29 +00001380 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001381 }
1382
1383protected:
1384 struct SectionInfo
1385 {
1386 SectionInfo () :
1387 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001388 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001389 {
1390 }
1391
1392 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001393 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001394 };
1395 SectionList *m_section_list;
1396 std::vector<SectionInfo> m_section_infos;
1397};
1398
Chris Lattner24943d22010-06-08 16:52:24 +00001399size_t
1400ObjectFileMachO::ParseSymtab (bool minimize)
1401{
1402 Timer scoped_timer(__PRETTY_FUNCTION__,
1403 "ObjectFileMachO::ParseSymtab () module = %s",
1404 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001405 ModuleSP module_sp (GetModule());
1406 if (!module_sp)
1407 return 0;
1408
1409 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1410 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1411 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1412 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001413 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001414 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001415
Greg Clayton952e9dc2013-03-27 23:08:40 +00001416 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001417
Chris Lattner24943d22010-06-08 16:52:24 +00001418 for (i=0; i<m_header.ncmds; ++i)
1419 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001420 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001421 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001422 struct load_command lc;
1423 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001424 break;
1425 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001426 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001427 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001428 case LoadCommandSymtab:
1429 symtab_load_command.cmd = lc.cmd;
1430 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001431 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001432 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1433 return 0;
1434 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001435 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001436 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001437 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001438 return 0;
1439 }
1440
1441 if (symtab_load_command.stroff == 0)
1442 {
1443 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001444 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001445 return 0;
1446 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001447
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001448 if (symtab_load_command.nsyms == 0)
1449 {
1450 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001451 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001452 return 0;
1453 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001454
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001455 if (symtab_load_command.strsize == 0)
1456 {
1457 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001458 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001459 return 0;
1460 }
1461 break;
1462
1463 case LoadCommandFunctionStarts:
1464 function_starts_load_command.cmd = lc.cmd;
1465 function_starts_load_command.cmdsize = lc.cmdsize;
1466 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1467 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1468 break;
1469
1470 default:
1471 break;
1472 }
1473 offset = cmd_offset + lc.cmdsize;
1474 }
1475
1476 if (symtab_load_command.cmd)
1477 {
1478 Symtab *symtab = m_symtab_ap.get();
1479 SectionList *section_list = GetSectionList();
1480 if (section_list == NULL)
1481 return 0;
1482
1483 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001484 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001485
Greg Clayton36da2aa2013-01-25 18:06:21 +00001486 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1487 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001488 bool bit_width_32 = addr_byte_size == 4;
1489 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1490
Greg Clayton36da2aa2013-01-25 18:06:21 +00001491 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1492 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1493 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001494 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001495
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001496 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1497 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001498 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1499 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001500 {
Greg Claytondd29b972012-05-18 23:20:01 +00001501 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001502 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1503 // Reading mach file from memory in a process or core file...
1504
1505 if (linkedit_section_sp)
1506 {
1507 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1508 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1509 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001510 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001511
1512 bool data_was_read = false;
1513
1514#if defined (__APPLE__) && defined (__arm__)
1515 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001516 {
Greg Clayton29021d32012-04-18 05:19:20 +00001517 // This mach-o memory file is in the dyld shared cache. If this
1518 // program is not remote and this is iOS, then this process will
1519 // share the same shared cache as the process we are debugging and
1520 // we can read the entire __LINKEDIT from the address space in this
1521 // process. This is a needed optimization that is used for local iOS
1522 // debugging only since all shared libraries in the shared cache do
1523 // not have corresponding files that exist in the file system of the
1524 // device. They have been combined into a single file. This means we
1525 // always have to load these files from memory. All of the symbol and
1526 // string tables from all of the __LINKEDIT sections from the shared
1527 // libraries in the shared cache have been merged into a single large
1528 // symbol and string table. Reading all of this symbol and string table
1529 // data across can slow down debug launch times, so we optimize this by
1530 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda45c75502013-04-16 06:24:42 +00001531
1532 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1533 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1534 bool use_lldb_cache = true;
1535 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1536 {
1537 use_lldb_cache = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001538 ModuleSP module_sp (GetModule());
1539 if (module_sp)
1540 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1541
Jason Molenda45c75502013-04-16 06:24:42 +00001542 }
1543
Greg Clayton29021d32012-04-18 05:19:20 +00001544 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda45c75502013-04-16 06:24:42 +00001545 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton29021d32012-04-18 05:19:20 +00001546 {
1547 data_was_read = true;
1548 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001549 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001550 if (function_starts_load_command.cmd)
1551 {
1552 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1553 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1554 }
1555 }
1556 }
1557#endif
1558
1559 if (!data_was_read)
1560 {
1561 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1562 if (nlist_data_sp)
1563 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001564 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1565 //if (strtab_data_sp)
1566 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001567 if (m_dysymtab.nindirectsyms != 0)
1568 {
1569 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1570 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1571 if (indirect_syms_data_sp)
1572 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1573 }
Greg Clayton29021d32012-04-18 05:19:20 +00001574 if (function_starts_load_command.cmd)
1575 {
1576 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1577 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1578 if (func_start_data_sp)
1579 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1580 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001581 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001582 }
1583 }
1584 else
1585 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001586 nlist_data.SetData (m_data,
1587 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001588 nlist_data_byte_size);
1589 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001590 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001591 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001592 if (m_dysymtab.nindirectsyms != 0)
1593 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001594 indirect_symbol_index_data.SetData (m_data,
1595 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001596 m_dysymtab.nindirectsyms * 4);
1597 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001598 if (function_starts_load_command.cmd)
1599 {
1600 function_starts_data.SetData (m_data,
1601 function_starts_load_command.dataoff,
1602 function_starts_load_command.datasize);
1603 }
1604 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001605
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001606 if (nlist_data.GetByteSize() == 0)
1607 {
1608 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001609 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001610 return 0;
1611 }
1612
1613
Greg Clayton3a5dc012012-05-25 17:04:00 +00001614 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1615 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001616 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001617 if (process)
1618 {
1619 if (strtab_addr == LLDB_INVALID_ADDRESS)
1620 {
1621 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001622 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001623 return 0;
1624 }
1625 }
1626 else
Greg Claytondd29b972012-05-18 23:20:01 +00001627 {
1628 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001629 module_sp->LogMessage(log, "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001630 return 0;
1631 }
1632 }
Greg Claytondd29b972012-05-18 23:20:01 +00001633
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001634 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1635 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1636 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1637 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1638 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1639 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1640 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1641 SectionSP eh_frame_section_sp;
1642 if (text_section_sp.get())
1643 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1644 else
1645 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1646
Greg Claytond2653c22012-03-14 01:53:24 +00001647 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001648
1649 // lldb works best if it knows the start addresss of all functions in a module.
1650 // Linker symbols or debug info are normally the best source of information for start addr / size but
1651 // they may be stripped in a released binary.
Jason Molenda2120aef2013-04-16 00:18:44 +00001652 // Two additional sources of information exist in Mach-O binaries:
Jason Molendad7938392013-03-21 03:36:01 +00001653 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1654 // binary, relative to the text section.
1655 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1656 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1657 // Binaries built to run on older releases may need to use eh_frame information.
1658
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001659 if (text_section_sp && function_starts_data.GetByteSize())
1660 {
1661 FunctionStarts::Entry function_start_entry;
1662 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001663 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001664 function_start_entry.addr = text_section_sp->GetFileAddress();
1665 uint64_t delta;
1666 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1667 {
1668 // Now append the current entry
1669 function_start_entry.addr += delta;
1670 function_starts.Append(function_start_entry);
1671 }
Jason Molenda2120aef2013-04-16 00:18:44 +00001672 }
Jason Molendad7938392013-03-21 03:36:01 +00001673 else
1674 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001675 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1676 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1677 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1678 // the module.
1679 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001680 {
1681 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1682 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1683 eh_frame.GetFunctionAddressAndSizeVector (functions);
1684 addr_t text_base_addr = text_section_sp->GetFileAddress();
1685 size_t count = functions.GetSize();
1686 for (size_t i = 0; i < count; ++i)
1687 {
1688 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1689 if (func)
1690 {
1691 FunctionStarts::Entry function_start_entry;
1692 function_start_entry.addr = func->base - text_base_addr;
1693 function_starts.Append(function_start_entry);
1694 }
1695 }
1696 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001697 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001698
Greg Clayton36da2aa2013-01-25 18:06:21 +00001699 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001700
Greg Clayton36da2aa2013-01-25 18:06:21 +00001701 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 +00001702
Greg Clayton36da2aa2013-01-25 18:06:21 +00001703 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001704
1705 uint32_t N_SO_index = UINT32_MAX;
1706
1707 MachSymtabSectionInfo section_info (section_list);
1708 std::vector<uint32_t> N_FUN_indexes;
1709 std::vector<uint32_t> N_NSYM_indexes;
1710 std::vector<uint32_t> N_INCL_indexes;
1711 std::vector<uint32_t> N_BRAC_indexes;
1712 std::vector<uint32_t> N_COMM_indexes;
1713 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1714 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
1715 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1716 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
1717 // Any symbols that get merged into another will get an entry
1718 // in this map so we know
1719 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1720 uint32_t nlist_idx = 0;
1721 Symbol *symbol_ptr = NULL;
1722
1723 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001724 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001725 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001726 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001727 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001728
Jason Molendab62abd52012-06-21 01:51:02 +00001729#if defined (__APPLE__) && defined (__arm__)
1730
1731 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1732 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1733 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1734 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1735 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1736 // nlist parser to ignore all LOCAL symbols.
1737
1738 if (m_header.flags & 0x80000000u)
1739 {
1740 // Before we can start mapping the DSC, we need to make certain the target process is actually
1741 // using the cache we can find.
1742
Jason Molendab62abd52012-06-21 01:51:02 +00001743 // Next we need to determine the correct path for the dyld shared cache.
1744
1745 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1746 char dsc_path[PATH_MAX];
1747
1748 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001749 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1750 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001751 header_arch.GetArchitectureName());
1752
1753 FileSpec dsc_filespec(dsc_path, false);
1754
1755 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda2120aef2013-04-16 00:18:44 +00001756 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001757 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001758 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1759 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1760 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001761 uint32_t imagesOffset;
1762 uint32_t imagesCount;
1763 uint64_t dyldBaseAddress;
1764 uint64_t codeSignatureOffset;
1765 uint64_t codeSignatureSize;
1766 uint64_t slideInfoOffset;
1767 uint64_t slideInfoSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001768 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1769 uint64_t localSymbolsSize; // size of local symbols information
1770 };
1771 struct lldb_copy_dyld_cache_header_v1
1772 {
1773 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1774 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1775 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1776 uint32_t imagesOffset;
1777 uint32_t imagesCount;
1778 uint64_t dyldBaseAddress;
1779 uint64_t codeSignatureOffset;
1780 uint64_t codeSignatureSize;
1781 uint64_t slideInfoOffset;
1782 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001783 uint64_t localSymbolsOffset;
1784 uint64_t localSymbolsSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001785 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001786 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001787
Jason Molenda2120aef2013-04-16 00:18:44 +00001788 struct lldb_copy_dyld_cache_mapping_info
1789 {
1790 uint64_t address;
1791 uint64_t size;
1792 uint64_t fileOffset;
1793 uint32_t maxProt;
1794 uint32_t initProt;
1795 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001796
Greg Claytonab77dcb2012-09-05 22:30:51 +00001797 struct lldb_copy_dyld_cache_local_symbols_info
1798 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001799 uint32_t nlistOffset;
1800 uint32_t nlistCount;
1801 uint32_t stringsOffset;
1802 uint32_t stringsSize;
1803 uint32_t entriesOffset;
1804 uint32_t entriesCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001805 };
1806 struct lldb_copy_dyld_cache_local_symbols_entry
1807 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001808 uint32_t dylibOffset;
1809 uint32_t nlistStartIndex;
1810 uint32_t nlistCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001811 };
Jason Molendab62abd52012-06-21 01:51:02 +00001812
Jason Molendafd3b35d2012-06-22 03:28:35 +00001813 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1814 The dyld_cache_local_symbols_info structure gives us three things:
1815 1. The start and count of the nlist records in the dyld_shared_cache file
1816 2. The start and size of the strings for these nlist records
1817 3. The start and count of dyld_cache_local_symbols_entry entries
1818
1819 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1820 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001821 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001822 and the count of how many nlist records there are for this dylib/framework.
1823 */
1824
Jason Molendab62abd52012-06-21 01:51:02 +00001825 // Process the dsc header to find the unmapped symbols
1826 //
1827 // Save some VM space, do not map the entire cache in one shot.
1828
Jason Molenda6bcabae2013-03-06 23:17:36 +00001829 DataBufferSP dsc_data_sp;
1830 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1831
1832 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001833 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001834 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001835
Jason Molenda6bcabae2013-03-06 23:17:36 +00001836 char version_str[17];
1837 int version = -1;
1838 lldb::offset_t offset = 0;
1839 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1840 version_str[16] = '\0';
1841 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1842 {
1843 int v;
1844 if (::sscanf (version_str + 6, "%d", &v) == 1)
1845 {
1846 version = v;
1847 }
1848 }
1849
Jason Molenda45c75502013-04-16 06:24:42 +00001850 UUID dsc_uuid;
1851 if (version >= 1)
1852 {
1853 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1854 uint8_t uuid_bytes[sizeof (uuid_t)];
1855 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1856 dsc_uuid.SetBytes (uuid_bytes);
1857 }
1858
1859 bool uuid_match = true;
1860 if (dsc_uuid.IsValid() && process)
1861 {
1862 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1863
1864 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1865 {
1866 // The on-disk dyld_shared_cache file is not the same as the one in this
1867 // process' memory, don't use it.
1868 uuid_match = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001869 ModuleSP module_sp (GetModule());
1870 if (module_sp)
1871 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 +00001872 }
1873 }
1874
Jason Molenda9badb6c2013-03-06 23:19:17 +00001875 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001876
Jason Molendab62abd52012-06-21 01:51:02 +00001877 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1878
1879 // If the mappingOffset points to a location inside the header, we've
1880 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda45c75502013-04-16 06:24:42 +00001881 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendab62abd52012-06-21 01:51:02 +00001882 {
1883
Jason Molenda6bcabae2013-03-06 23:17:36 +00001884 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1885 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1886 offset = 0;
1887
1888 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1889 // in the shared library cache need to be adjusted by an offset to match up with the
1890 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1891 // recorded in mapping_offset_value.
1892 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1893
1894 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001895 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1896 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1897
Jason Molenda9badb6c2013-03-06 23:19:17 +00001898 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001899 {
1900 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001901 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001902 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001903 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001904
1905 offset = 0;
1906
1907 // Read the local_symbols_infos struct in one shot
1908 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1909 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1910
Jason Molendab62abd52012-06-21 01:51:02 +00001911 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1912
Jason Molenda6bcabae2013-03-06 23:17:36 +00001913 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001914
1915 offset = local_symbols_info.entriesOffset;
1916 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1917 {
1918 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1919 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1920 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1921 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1922
Jason Molenda9badb6c2013-03-06 23:19:17 +00001923 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001924 {
1925 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1926
1927 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1928 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1929 num_syms = symtab->GetNumSymbols();
1930
1931 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1932 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1933
Jason Molenda9badb6c2013-03-06 23:19:17 +00001934 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001935 {
1936 /////////////////////////////
1937 {
1938 struct nlist_64 nlist;
1939 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1940 break;
1941
1942 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1943 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1944 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1945 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1946 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1947
1948 SymbolType type = eSymbolTypeInvalid;
1949 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1950
1951 if (symbol_name == NULL)
1952 {
1953 // No symbol should be NULL, even the symbols with no
1954 // string values should have an offset zero which points
1955 // to an empty C-string
1956 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00001957 "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 +00001958 entry_index,
1959 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00001960 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendab62abd52012-06-21 01:51:02 +00001961 continue;
1962 }
1963 if (symbol_name[0] == '\0')
1964 symbol_name = NULL;
1965
1966 const char *symbol_name_non_abi_mangled = NULL;
1967
1968 SectionSP symbol_section;
1969 uint32_t symbol_byte_size = 0;
1970 bool add_nlist = true;
1971 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001972 bool demangled_is_synthesized = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001973
1974 assert (sym_idx < num_syms);
1975
1976 sym[sym_idx].SetDebug (is_debug);
1977
1978 if (is_debug)
1979 {
1980 switch (nlist.n_type)
1981 {
1982 case StabGlobalSymbol:
1983 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1984 // Sometimes the N_GSYM value contains the address.
1985
1986 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1987 // have the same address, but we want to ensure that we always find only the real symbol,
1988 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1989 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1990 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1991 // same address.
1992
1993 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1994 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1995 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1996 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1997 add_nlist = false;
1998 else
1999 {
2000 sym[sym_idx].SetExternal(true);
2001 if (nlist.n_value != 0)
2002 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2003 type = eSymbolTypeData;
2004 }
2005 break;
2006
2007 case StabFunctionName:
2008 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2009 type = eSymbolTypeCompiler;
2010 break;
2011
2012 case StabFunction:
2013 // N_FUN -- procedure: name,,n_sect,linenumber,address
2014 if (symbol_name)
2015 {
2016 type = eSymbolTypeCode;
2017 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2018
2019 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2020 // We use the current number of symbols in the symbol table in lieu of
2021 // using nlist_idx in case we ever start trimming entries out
2022 N_FUN_indexes.push_back(sym_idx);
2023 }
2024 else
2025 {
2026 type = eSymbolTypeCompiler;
2027
2028 if ( !N_FUN_indexes.empty() )
2029 {
2030 // Copy the size of the function into the original STAB entry so we don't have
2031 // to hunt for it later
2032 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2033 N_FUN_indexes.pop_back();
2034 // We don't really need the end function STAB as it contains the size which
2035 // we already placed with the original symbol, so don't add it if we want a
2036 // minimal symbol table
2037 if (minimize)
2038 add_nlist = false;
2039 }
2040 }
2041 break;
2042
2043 case StabStaticSymbol:
2044 // N_STSYM -- static symbol: name,,n_sect,type,address
2045 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2046 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2047 type = eSymbolTypeData;
2048 break;
2049
2050 case StabLocalCommon:
2051 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2052 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2053 type = eSymbolTypeCommonBlock;
2054 break;
2055
2056 case StabBeginSymbol:
2057 // N_BNSYM
2058 // We use the current number of symbols in the symbol table in lieu of
2059 // using nlist_idx in case we ever start trimming entries out
2060 if (minimize)
2061 {
2062 // Skip these if we want minimal symbol tables
2063 add_nlist = false;
2064 }
2065 else
2066 {
2067 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2068 N_NSYM_indexes.push_back(sym_idx);
2069 type = eSymbolTypeScopeBegin;
2070 }
2071 break;
2072
2073 case StabEndSymbol:
2074 // N_ENSYM
2075 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2076 // so that we can always skip the entire symbol if we need to navigate
2077 // more quickly at the source level when parsing STABS
2078 if (minimize)
2079 {
2080 // Skip these if we want minimal symbol tables
2081 add_nlist = false;
2082 }
2083 else
2084 {
2085 if ( !N_NSYM_indexes.empty() )
2086 {
2087 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2088 symbol_ptr->SetByteSize(sym_idx + 1);
2089 symbol_ptr->SetSizeIsSibling(true);
2090 N_NSYM_indexes.pop_back();
2091 }
2092 type = eSymbolTypeScopeEnd;
2093 }
2094 break;
2095
2096
2097 case StabSourceFileOptions:
2098 // N_OPT - emitted with gcc2_compiled and in gcc source
2099 type = eSymbolTypeCompiler;
2100 break;
2101
2102 case StabRegisterSymbol:
2103 // N_RSYM - register sym: name,,NO_SECT,type,register
2104 type = eSymbolTypeVariable;
2105 break;
2106
2107 case StabSourceLine:
2108 // N_SLINE - src line: 0,,n_sect,linenumber,address
2109 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2110 type = eSymbolTypeLineEntry;
2111 break;
2112
2113 case StabStructureType:
2114 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2115 type = eSymbolTypeVariableType;
2116 break;
2117
2118 case StabSourceFileName:
2119 // N_SO - source file name
2120 type = eSymbolTypeSourceFile;
2121 if (symbol_name == NULL)
2122 {
2123 if (minimize)
2124 add_nlist = false;
2125 if (N_SO_index != UINT32_MAX)
2126 {
2127 // Set the size of the N_SO to the terminating index of this N_SO
2128 // so that we can always skip the entire N_SO if we need to navigate
2129 // more quickly at the source level when parsing STABS
2130 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2131 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2132 symbol_ptr->SetSizeIsSibling(true);
2133 }
2134 N_NSYM_indexes.clear();
2135 N_INCL_indexes.clear();
2136 N_BRAC_indexes.clear();
2137 N_COMM_indexes.clear();
2138 N_FUN_indexes.clear();
2139 N_SO_index = UINT32_MAX;
2140 }
2141 else
2142 {
2143 // We use the current number of symbols in the symbol table in lieu of
2144 // using nlist_idx in case we ever start trimming entries out
2145 const bool N_SO_has_full_path = symbol_name[0] == '/';
2146 if (N_SO_has_full_path)
2147 {
2148 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2149 {
2150 // We have two consecutive N_SO entries where the first contains a directory
2151 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00002152 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002153 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2154 add_nlist = false;
2155 }
2156 else
2157 {
2158 // This is the first entry in a N_SO that contains a directory or
2159 // a full path to the source file
2160 N_SO_index = sym_idx;
2161 }
2162 }
2163 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2164 {
2165 // This is usually the second N_SO entry that contains just the filename,
2166 // so here we combine it with the first one if we are minimizing the symbol table
2167 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2168 if (so_path && so_path[0])
2169 {
2170 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002171 const size_t double_slash_pos = full_so_path.find("//");
2172 if (double_slash_pos != std::string::npos)
2173 {
2174 // The linker has been generating bad N_SO entries with doubled up paths
2175 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2176 // and the second is the directory for the source file so you end up with
2177 // a path that looks like "/tmp/src//tmp/src/"
2178 FileSpec so_dir(so_path, false);
2179 if (!so_dir.Exists())
2180 {
2181 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2182 if (so_dir.Exists())
2183 {
2184 // Trim off the incorrect path
2185 full_so_path.erase(0, double_slash_pos + 1);
2186 }
2187 }
2188 }
Jason Molendab62abd52012-06-21 01:51:02 +00002189 if (*full_so_path.rbegin() != '/')
2190 full_so_path += '/';
2191 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00002192 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002193 add_nlist = false;
2194 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2195 }
2196 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002197 else
2198 {
2199 // This could be a relative path to a N_SO
2200 N_SO_index = sym_idx;
2201 }
Jason Molendab62abd52012-06-21 01:51:02 +00002202 }
Jason Molendab62abd52012-06-21 01:51:02 +00002203 break;
2204
2205 case StabObjectFileName:
2206 // N_OSO - object file name: name,,0,0,st_mtime
2207 type = eSymbolTypeObjectFile;
2208 break;
2209
2210 case StabLocalSymbol:
2211 // N_LSYM - local sym: name,,NO_SECT,type,offset
2212 type = eSymbolTypeLocal;
2213 break;
2214
2215 //----------------------------------------------------------------------
2216 // INCL scopes
2217 //----------------------------------------------------------------------
2218 case StabBeginIncludeFileName:
2219 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2220 // We use the current number of symbols in the symbol table in lieu of
2221 // using nlist_idx in case we ever start trimming entries out
2222 N_INCL_indexes.push_back(sym_idx);
2223 type = eSymbolTypeScopeBegin;
2224 break;
2225
2226 case StabEndIncludeFile:
2227 // N_EINCL - include file end: name,,NO_SECT,0,0
2228 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2229 // so that we can always skip the entire symbol if we need to navigate
2230 // more quickly at the source level when parsing STABS
2231 if ( !N_INCL_indexes.empty() )
2232 {
2233 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2234 symbol_ptr->SetByteSize(sym_idx + 1);
2235 symbol_ptr->SetSizeIsSibling(true);
2236 N_INCL_indexes.pop_back();
2237 }
2238 type = eSymbolTypeScopeEnd;
2239 break;
2240
2241 case StabIncludeFileName:
2242 // N_SOL - #included file name: name,,n_sect,0,address
2243 type = eSymbolTypeHeaderFile;
2244
2245 // We currently don't use the header files on darwin
2246 if (minimize)
2247 add_nlist = false;
2248 break;
2249
2250 case StabCompilerParameters:
2251 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2252 type = eSymbolTypeCompiler;
2253 break;
2254
2255 case StabCompilerVersion:
2256 // N_VERSION - compiler version: name,,NO_SECT,0,0
2257 type = eSymbolTypeCompiler;
2258 break;
2259
2260 case StabCompilerOptLevel:
2261 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2262 type = eSymbolTypeCompiler;
2263 break;
2264
2265 case StabParameter:
2266 // N_PSYM - parameter: name,,NO_SECT,type,offset
2267 type = eSymbolTypeVariable;
2268 break;
2269
2270 case StabAlternateEntry:
2271 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2272 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2273 type = eSymbolTypeLineEntry;
2274 break;
2275
2276 //----------------------------------------------------------------------
2277 // Left and Right Braces
2278 //----------------------------------------------------------------------
2279 case StabLeftBracket:
2280 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2281 // We use the current number of symbols in the symbol table in lieu of
2282 // using nlist_idx in case we ever start trimming entries out
2283 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2284 N_BRAC_indexes.push_back(sym_idx);
2285 type = eSymbolTypeScopeBegin;
2286 break;
2287
2288 case StabRightBracket:
2289 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2290 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2291 // so that we can always skip the entire symbol if we need to navigate
2292 // more quickly at the source level when parsing STABS
2293 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2294 if ( !N_BRAC_indexes.empty() )
2295 {
2296 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2297 symbol_ptr->SetByteSize(sym_idx + 1);
2298 symbol_ptr->SetSizeIsSibling(true);
2299 N_BRAC_indexes.pop_back();
2300 }
2301 type = eSymbolTypeScopeEnd;
2302 break;
2303
2304 case StabDeletedIncludeFile:
2305 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2306 type = eSymbolTypeHeaderFile;
2307 break;
2308
2309 //----------------------------------------------------------------------
2310 // COMM scopes
2311 //----------------------------------------------------------------------
2312 case StabBeginCommon:
2313 // N_BCOMM - begin common: name,,NO_SECT,0,0
2314 // We use the current number of symbols in the symbol table in lieu of
2315 // using nlist_idx in case we ever start trimming entries out
2316 type = eSymbolTypeScopeBegin;
2317 N_COMM_indexes.push_back(sym_idx);
2318 break;
2319
2320 case StabEndCommonLocal:
2321 // N_ECOML - end common (local name): 0,,n_sect,0,address
2322 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2323 // Fall through
2324
2325 case StabEndCommon:
2326 // N_ECOMM - end common: name,,n_sect,0,0
2327 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2328 // so that we can always skip the entire symbol if we need to navigate
2329 // more quickly at the source level when parsing STABS
2330 if ( !N_COMM_indexes.empty() )
2331 {
2332 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2333 symbol_ptr->SetByteSize(sym_idx + 1);
2334 symbol_ptr->SetSizeIsSibling(true);
2335 N_COMM_indexes.pop_back();
2336 }
2337 type = eSymbolTypeScopeEnd;
2338 break;
2339
2340 case StabLength:
2341 // N_LENG - second stab entry with length information
2342 type = eSymbolTypeAdditional;
2343 break;
2344
2345 default: break;
2346 }
2347 }
2348 else
2349 {
2350 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2351 uint8_t n_type = NlistMaskType & nlist.n_type;
2352 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2353
2354 switch (n_type)
2355 {
2356 case NListTypeIndirect: // N_INDR - Fall through
2357 case NListTypePreboundUndefined:// N_PBUD - Fall through
2358 case NListTypeUndefined: // N_UNDF
2359 type = eSymbolTypeUndefined;
2360 break;
2361
2362 case NListTypeAbsolute: // N_ABS
2363 type = eSymbolTypeAbsolute;
2364 break;
2365
2366 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002367 {
2368 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002369
Greg Clayton01e6a582012-11-27 01:52:16 +00002370 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002371 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002372 // TODO: warn about this?
2373 add_nlist = false;
2374 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002375 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002376
2377 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002378 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002379 type = eSymbolTypeException;
2380 }
2381 else
2382 {
2383 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002384
Greg Clayton01e6a582012-11-27 01:52:16 +00002385 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002386 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002387 case SectionTypeRegular: break; // regular section
2388 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2389 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2390 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2391 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2392 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2393 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2394 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2395 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2396 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2397 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2398 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2399 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2400 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2401 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2402 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2403 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2404 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002405 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002406
Greg Clayton01e6a582012-11-27 01:52:16 +00002407 if (type == eSymbolTypeInvalid)
2408 {
2409 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2410 if (symbol_section->IsDescendant (text_section_sp.get()))
2411 {
2412 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2413 SectionAttrUserSelfModifyingCode |
2414 SectionAttrSytemSomeInstructions))
2415 type = eSymbolTypeData;
2416 else
2417 type = eSymbolTypeCode;
2418 }
2419 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002420 {
2421 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2422 {
2423 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002424
Greg Clayton01e6a582012-11-27 01:52:16 +00002425 if (symbol_name &&
2426 symbol_name[0] == '_' &&
2427 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002428 symbol_name[2] == 'B')
2429 {
2430 llvm::StringRef symbol_name_ref(symbol_name);
2431 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2432 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2433 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2434 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2435 {
2436 symbol_name_non_abi_mangled = symbol_name + 1;
2437 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2438 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002439 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002440 }
2441 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2442 {
2443 symbol_name_non_abi_mangled = symbol_name + 1;
2444 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2445 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002446 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002447 }
2448 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2449 {
2450 symbol_name_non_abi_mangled = symbol_name + 1;
2451 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2452 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002453 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002454 }
2455 }
2456 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002457 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002458 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002459 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002460 }
2461 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002462 {
2463 type = eSymbolTypeData;
2464 }
2465 }
2466 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2467 {
2468 type = eSymbolTypeTrampoline;
2469 }
2470 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2471 {
2472 type = eSymbolTypeRuntime;
2473 if (symbol_name && symbol_name[0] == '.')
2474 {
2475 llvm::StringRef symbol_name_ref(symbol_name);
2476 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2477 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002478 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002479 symbol_name_non_abi_mangled = symbol_name;
2480 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2481 type = eSymbolTypeObjCClass;
2482 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002483 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002484 }
2485 }
2486 }
Jason Molendab62abd52012-06-21 01:51:02 +00002487 }
2488 }
Jason Molendab62abd52012-06-21 01:51:02 +00002489 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002490 }
Jason Molendab62abd52012-06-21 01:51:02 +00002491 }
2492
2493 if (add_nlist)
2494 {
2495 uint64_t symbol_value = nlist.n_value;
2496 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002497
Jason Molendab62abd52012-06-21 01:51:02 +00002498 if (symbol_name_non_abi_mangled)
2499 {
Jason Molenda292cca82012-07-20 03:35:44 +00002500 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2501 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002502 }
2503 else
2504 {
2505 if (symbol_name && symbol_name[0] == '_')
2506 {
2507 symbol_name_is_mangled = symbol_name[1] == '_';
2508 symbol_name++; // Skip the leading underscore
2509 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002510
Jason Molendab62abd52012-06-21 01:51:02 +00002511 if (symbol_name)
2512 {
Jason Molenda292cca82012-07-20 03:35:44 +00002513 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002514 }
2515 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002516
Jason Molendab62abd52012-06-21 01:51:02 +00002517 if (is_debug == false)
2518 {
2519 if (type == eSymbolTypeCode)
2520 {
2521 // See if we can find a N_FUN entry for any code symbols.
2522 // If we do find a match, and the name matches, then we
2523 // can merge the two into just the function symbol to avoid
2524 // duplicate entries in the symbol table
2525 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2526 if (pos != N_FUN_addr_to_sym_idx.end())
2527 {
2528 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2529 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2530 {
2531 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2532 // We just need the flags from the linker symbol, so put these flags
2533 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2534 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2535 sym[sym_idx].Clear();
2536 continue;
2537 }
2538 }
2539 }
2540 else if (type == eSymbolTypeData)
2541 {
2542 // See if we can find a N_STSYM entry for any data symbols.
2543 // If we do find a match, and the name matches, then we
2544 // can merge the two into just the Static symbol to avoid
2545 // duplicate entries in the symbol table
2546 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2547 if (pos != N_STSYM_addr_to_sym_idx.end())
2548 {
2549 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2550 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2551 {
2552 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2553 // We just need the flags from the linker symbol, so put these flags
2554 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2555 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2556 sym[sym_idx].Clear();
2557 continue;
2558 }
2559 }
2560 }
2561 }
2562 if (symbol_section)
2563 {
2564 const addr_t section_file_addr = symbol_section->GetFileAddress();
2565 if (symbol_byte_size == 0 && function_starts_count > 0)
2566 {
2567 addr_t symbol_lookup_file_addr = nlist.n_value;
2568 // Do an exact address match for non-ARM addresses, else get the closest since
2569 // the symbol might be a thumb symbol which has an address with bit zero set
2570 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2571 if (is_arm && func_start_entry)
2572 {
2573 // Verify that the function start address is the symbol address (ARM)
2574 // or the symbol address + 1 (thumb)
2575 if (func_start_entry->addr != symbol_lookup_file_addr &&
2576 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2577 {
2578 // Not the right entry, NULL it out...
2579 func_start_entry = NULL;
2580 }
2581 }
2582 if (func_start_entry)
2583 {
2584 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002585
Jason Molendab62abd52012-06-21 01:51:02 +00002586 addr_t symbol_file_addr = func_start_entry->addr;
2587 uint32_t symbol_flags = 0;
2588 if (is_arm)
2589 {
2590 if (symbol_file_addr & 1)
2591 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2592 symbol_file_addr &= 0xfffffffffffffffeull;
2593 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002594
Jason Molendab62abd52012-06-21 01:51:02 +00002595 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2596 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2597 if (next_func_start_entry)
2598 {
2599 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2600 // Be sure the clear the Thumb address bit when we calculate the size
2601 // from the current and next address
2602 if (is_arm)
2603 next_symbol_file_addr &= 0xfffffffffffffffeull;
2604 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2605 }
2606 else
2607 {
2608 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2609 }
2610 }
2611 }
2612 symbol_value -= section_file_addr;
2613 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002614
Jason Molendab62abd52012-06-21 01:51:02 +00002615 sym[sym_idx].SetID (nlist_idx);
2616 sym[sym_idx].SetType (type);
2617 sym[sym_idx].GetAddress().SetSection (symbol_section);
2618 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2619 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002620
Jason Molendab62abd52012-06-21 01:51:02 +00002621 if (symbol_byte_size > 0)
2622 sym[sym_idx].SetByteSize(symbol_byte_size);
2623
Greg Clayton01e6a582012-11-27 01:52:16 +00002624 if (demangled_is_synthesized)
2625 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002626 ++sym_idx;
2627 }
2628 else
2629 {
2630 sym[sym_idx].Clear();
2631 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002632
Jason Molendab62abd52012-06-21 01:51:02 +00002633 }
2634 /////////////////////////////
2635 }
2636 break; // No more entries to consider
2637 }
2638 }
2639 }
2640 }
2641 }
2642 }
2643 }
2644
2645 // Must reset this in case it was mutated above!
2646 nlist_data_offset = 0;
2647#endif
2648
2649 // If the sym array was not created while parsing the DSC unmapped
2650 // symbols, create it now.
2651 if (sym == NULL)
2652 {
2653 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2654 num_syms = symtab->GetNumSymbols();
2655 }
2656
2657 if (unmapped_local_symbols_found)
2658 {
2659 assert(m_dysymtab.ilocalsym == 0);
2660 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2661 nlist_idx = m_dysymtab.nlocalsym;
2662 }
2663 else
2664 {
2665 nlist_idx = 0;
2666 }
2667
2668 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002669 {
2670 struct nlist_64 nlist;
2671 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2672 break;
2673
2674 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2675 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2676 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2677 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2678 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2679
2680 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002681 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002682
Greg Clayton3a5dc012012-05-25 17:04:00 +00002683 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002684 {
2685 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002686
Greg Claytondd29b972012-05-18 23:20:01 +00002687 if (symbol_name == NULL)
2688 {
2689 // No symbol should be NULL, even the symbols with no
2690 // string values should have an offset zero which points
2691 // to an empty C-string
2692 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00002693 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002694 nlist_idx,
2695 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00002696 module_sp->GetFileSpec().GetPath().c_str());
Greg Claytondd29b972012-05-18 23:20:01 +00002697 continue;
2698 }
2699 if (symbol_name[0] == '\0')
2700 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002701 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002702 else
2703 {
2704 const addr_t str_addr = strtab_addr + nlist.n_strx;
2705 Error str_error;
2706 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2707 symbol_name = memory_symbol_name.c_str();
2708 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002709 const char *symbol_name_non_abi_mangled = NULL;
2710
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002711 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002712 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002713 bool add_nlist = true;
2714 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002715 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002716
2717 assert (sym_idx < num_syms);
2718
2719 sym[sym_idx].SetDebug (is_debug);
2720
2721 if (is_debug)
2722 {
2723 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002724 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002725 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002726 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2727 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002728
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002729 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2730 // have the same address, but we want to ensure that we always find only the real symbol,
2731 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2732 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2733 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2734 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002735
2736 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002737 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2738 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2739 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2740 add_nlist = false;
2741 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002742 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002743 sym[sym_idx].SetExternal(true);
2744 if (nlist.n_value != 0)
2745 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2746 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002747 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002748 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002749
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002750 case StabFunctionName:
2751 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2752 type = eSymbolTypeCompiler;
2753 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002754
Jason Molenda9badb6c2013-03-06 23:19:17 +00002755 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002756 // N_FUN -- procedure: name,,n_sect,linenumber,address
2757 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002758 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002759 type = eSymbolTypeCode;
2760 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002761
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002762 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2763 // We use the current number of symbols in the symbol table in lieu of
2764 // using nlist_idx in case we ever start trimming entries out
2765 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002766 }
2767 else
2768 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002769 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002770
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002771 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002772 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002773 // Copy the size of the function into the original STAB entry so we don't have
2774 // to hunt for it later
2775 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2776 N_FUN_indexes.pop_back();
2777 // We don't really need the end function STAB as it contains the size which
2778 // we already placed with the original symbol, so don't add it if we want a
2779 // minimal symbol table
2780 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002781 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002782 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002783 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002784 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002785
Jason Molenda9badb6c2013-03-06 23:19:17 +00002786 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002787 // N_STSYM -- static symbol: name,,n_sect,type,address
2788 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2789 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2790 type = eSymbolTypeData;
2791 break;
2792
2793 case StabLocalCommon:
2794 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2795 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2796 type = eSymbolTypeCommonBlock;
2797 break;
2798
2799 case StabBeginSymbol:
2800 // N_BNSYM
2801 // We use the current number of symbols in the symbol table in lieu of
2802 // using nlist_idx in case we ever start trimming entries out
2803 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002804 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002805 // Skip these if we want minimal symbol tables
2806 add_nlist = false;
2807 }
2808 else
2809 {
2810 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2811 N_NSYM_indexes.push_back(sym_idx);
2812 type = eSymbolTypeScopeBegin;
2813 }
2814 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002815
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002816 case StabEndSymbol:
2817 // N_ENSYM
2818 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2819 // so that we can always skip the entire symbol if we need to navigate
2820 // more quickly at the source level when parsing STABS
2821 if (minimize)
2822 {
2823 // Skip these if we want minimal symbol tables
2824 add_nlist = false;
2825 }
2826 else
2827 {
2828 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002829 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002830 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2831 symbol_ptr->SetByteSize(sym_idx + 1);
2832 symbol_ptr->SetSizeIsSibling(true);
2833 N_NSYM_indexes.pop_back();
2834 }
2835 type = eSymbolTypeScopeEnd;
2836 }
2837 break;
2838
2839
2840 case StabSourceFileOptions:
2841 // N_OPT - emitted with gcc2_compiled and in gcc source
2842 type = eSymbolTypeCompiler;
2843 break;
2844
2845 case StabRegisterSymbol:
2846 // N_RSYM - register sym: name,,NO_SECT,type,register
2847 type = eSymbolTypeVariable;
2848 break;
2849
2850 case StabSourceLine:
2851 // N_SLINE - src line: 0,,n_sect,linenumber,address
2852 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2853 type = eSymbolTypeLineEntry;
2854 break;
2855
2856 case StabStructureType:
2857 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2858 type = eSymbolTypeVariableType;
2859 break;
2860
2861 case StabSourceFileName:
2862 // N_SO - source file name
2863 type = eSymbolTypeSourceFile;
2864 if (symbol_name == NULL)
2865 {
2866 if (minimize)
2867 add_nlist = false;
2868 if (N_SO_index != UINT32_MAX)
2869 {
2870 // Set the size of the N_SO to the terminating index of this N_SO
2871 // so that we can always skip the entire N_SO if we need to navigate
2872 // more quickly at the source level when parsing STABS
2873 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2874 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2875 symbol_ptr->SetSizeIsSibling(true);
2876 }
2877 N_NSYM_indexes.clear();
2878 N_INCL_indexes.clear();
2879 N_BRAC_indexes.clear();
2880 N_COMM_indexes.clear();
2881 N_FUN_indexes.clear();
2882 N_SO_index = UINT32_MAX;
2883 }
2884 else
2885 {
2886 // We use the current number of symbols in the symbol table in lieu of
2887 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002888 const bool N_SO_has_full_path = symbol_name[0] == '/';
2889 if (N_SO_has_full_path)
2890 {
2891 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2892 {
2893 // We have two consecutive N_SO entries where the first contains a directory
2894 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002895 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002896 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2897 add_nlist = false;
2898 }
2899 else
2900 {
2901 // This is the first entry in a N_SO that contains a directory or
2902 // a full path to the source file
2903 N_SO_index = sym_idx;
2904 }
2905 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002906 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2907 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002908 // This is usually the second N_SO entry that contains just the filename,
2909 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002910 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2911 if (so_path && so_path[0])
2912 {
2913 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002914 const size_t double_slash_pos = full_so_path.find("//");
2915 if (double_slash_pos != std::string::npos)
2916 {
2917 // The linker has been generating bad N_SO entries with doubled up paths
2918 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2919 // and the second is the directory for the source file so you end up with
2920 // a path that looks like "/tmp/src//tmp/src/"
2921 FileSpec so_dir(so_path, false);
2922 if (!so_dir.Exists())
2923 {
2924 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2925 if (so_dir.Exists())
2926 {
2927 // Trim off the incorrect path
2928 full_so_path.erase(0, double_slash_pos + 1);
2929 }
2930 }
2931 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002932 if (*full_so_path.rbegin() != '/')
2933 full_so_path += '/';
2934 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002935 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002936 add_nlist = false;
2937 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2938 }
2939 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002940 else
2941 {
2942 // This could be a relative path to a N_SO
2943 N_SO_index = sym_idx;
2944 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002945 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002946
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002947 break;
2948
2949 case StabObjectFileName:
2950 // N_OSO - object file name: name,,0,0,st_mtime
2951 type = eSymbolTypeObjectFile;
2952 break;
2953
2954 case StabLocalSymbol:
2955 // N_LSYM - local sym: name,,NO_SECT,type,offset
2956 type = eSymbolTypeLocal;
2957 break;
2958
2959 //----------------------------------------------------------------------
2960 // INCL scopes
2961 //----------------------------------------------------------------------
2962 case StabBeginIncludeFileName:
2963 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2964 // We use the current number of symbols in the symbol table in lieu of
2965 // using nlist_idx in case we ever start trimming entries out
2966 N_INCL_indexes.push_back(sym_idx);
2967 type = eSymbolTypeScopeBegin;
2968 break;
2969
2970 case StabEndIncludeFile:
2971 // N_EINCL - include file end: name,,NO_SECT,0,0
2972 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2973 // so that we can always skip the entire symbol if we need to navigate
2974 // more quickly at the source level when parsing STABS
2975 if ( !N_INCL_indexes.empty() )
2976 {
2977 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2978 symbol_ptr->SetByteSize(sym_idx + 1);
2979 symbol_ptr->SetSizeIsSibling(true);
2980 N_INCL_indexes.pop_back();
2981 }
2982 type = eSymbolTypeScopeEnd;
2983 break;
2984
2985 case StabIncludeFileName:
2986 // N_SOL - #included file name: name,,n_sect,0,address
2987 type = eSymbolTypeHeaderFile;
2988
2989 // We currently don't use the header files on darwin
2990 if (minimize)
2991 add_nlist = false;
2992 break;
2993
Jason Molenda9badb6c2013-03-06 23:19:17 +00002994 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002995 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2996 type = eSymbolTypeCompiler;
2997 break;
2998
2999 case StabCompilerVersion:
3000 // N_VERSION - compiler version: name,,NO_SECT,0,0
3001 type = eSymbolTypeCompiler;
3002 break;
3003
3004 case StabCompilerOptLevel:
3005 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
3006 type = eSymbolTypeCompiler;
3007 break;
3008
3009 case StabParameter:
3010 // N_PSYM - parameter: name,,NO_SECT,type,offset
3011 type = eSymbolTypeVariable;
3012 break;
3013
3014 case StabAlternateEntry:
3015 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
3016 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3017 type = eSymbolTypeLineEntry;
3018 break;
3019
3020 //----------------------------------------------------------------------
3021 // Left and Right Braces
3022 //----------------------------------------------------------------------
3023 case StabLeftBracket:
3024 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
3025 // We use the current number of symbols in the symbol table in lieu of
3026 // using nlist_idx in case we ever start trimming entries out
3027 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3028 N_BRAC_indexes.push_back(sym_idx);
3029 type = eSymbolTypeScopeBegin;
3030 break;
3031
3032 case StabRightBracket:
3033 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
3034 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3035 // so that we can always skip the entire symbol if we need to navigate
3036 // more quickly at the source level when parsing STABS
3037 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3038 if ( !N_BRAC_indexes.empty() )
3039 {
3040 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3041 symbol_ptr->SetByteSize(sym_idx + 1);
3042 symbol_ptr->SetSizeIsSibling(true);
3043 N_BRAC_indexes.pop_back();
3044 }
3045 type = eSymbolTypeScopeEnd;
3046 break;
3047
3048 case StabDeletedIncludeFile:
3049 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3050 type = eSymbolTypeHeaderFile;
3051 break;
3052
3053 //----------------------------------------------------------------------
3054 // COMM scopes
3055 //----------------------------------------------------------------------
3056 case StabBeginCommon:
3057 // N_BCOMM - begin common: name,,NO_SECT,0,0
3058 // We use the current number of symbols in the symbol table in lieu of
3059 // using nlist_idx in case we ever start trimming entries out
3060 type = eSymbolTypeScopeBegin;
3061 N_COMM_indexes.push_back(sym_idx);
3062 break;
3063
3064 case StabEndCommonLocal:
3065 // N_ECOML - end common (local name): 0,,n_sect,0,address
3066 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3067 // Fall through
3068
3069 case StabEndCommon:
3070 // N_ECOMM - end common: name,,n_sect,0,0
3071 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3072 // so that we can always skip the entire symbol if we need to navigate
3073 // more quickly at the source level when parsing STABS
3074 if ( !N_COMM_indexes.empty() )
3075 {
3076 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3077 symbol_ptr->SetByteSize(sym_idx + 1);
3078 symbol_ptr->SetSizeIsSibling(true);
3079 N_COMM_indexes.pop_back();
3080 }
3081 type = eSymbolTypeScopeEnd;
3082 break;
3083
3084 case StabLength:
3085 // N_LENG - second stab entry with length information
3086 type = eSymbolTypeAdditional;
3087 break;
3088
3089 default: break;
3090 }
3091 }
3092 else
3093 {
3094 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3095 uint8_t n_type = NlistMaskType & nlist.n_type;
3096 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3097
3098 switch (n_type)
3099 {
3100 case NListTypeIndirect: // N_INDR - Fall through
3101 case NListTypePreboundUndefined:// N_PBUD - Fall through
3102 case NListTypeUndefined: // N_UNDF
3103 type = eSymbolTypeUndefined;
3104 break;
3105
3106 case NListTypeAbsolute: // N_ABS
3107 type = eSymbolTypeAbsolute;
3108 break;
3109
3110 case NListTypeSection: // N_SECT
3111 {
3112 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3113
Sean Callananb386d822012-08-09 00:50:26 +00003114 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003115 {
3116 // TODO: warn about this?
3117 add_nlist = false;
3118 break;
3119 }
3120
3121 if (TEXT_eh_frame_sectID == nlist.n_sect)
3122 {
3123 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00003124 }
3125 else
3126 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003127 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3128
3129 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00003130 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003131 case SectionTypeRegular: break; // regular section
3132 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3133 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3134 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3135 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3136 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3137 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3138 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3139 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3140 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3141 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3142 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3143 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3144 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3145 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3146 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3147 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3148 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00003149 }
Chris Lattner24943d22010-06-08 16:52:24 +00003150
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003151 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00003152 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003153 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3154 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003155 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003156 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3157 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003158 SectionAttrSytemSomeInstructions))
3159 type = eSymbolTypeData;
3160 else
3161 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00003162 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003163 else
3164 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003165 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003166 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00003167 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003168 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00003169
Jason Molenda9badb6c2013-03-06 23:19:17 +00003170 if (symbol_name &&
3171 symbol_name[0] == '_' &&
3172 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003173 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00003174 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003175 llvm::StringRef symbol_name_ref(symbol_name);
3176 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3177 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3178 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3179 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00003180 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003181 symbol_name_non_abi_mangled = symbol_name + 1;
3182 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3183 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003184 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003185 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003186 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003187 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003188 symbol_name_non_abi_mangled = symbol_name + 1;
3189 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3190 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003191 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003192 }
3193 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3194 {
3195 symbol_name_non_abi_mangled = symbol_name + 1;
3196 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3197 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003198 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003199 }
3200 }
3201 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003202 else
3203 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3204 {
3205 type = eSymbolTypeException;
3206 }
3207 else
3208 {
3209 type = eSymbolTypeData;
3210 }
3211 }
3212 else
3213 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3214 {
3215 type = eSymbolTypeTrampoline;
3216 }
3217 else
3218 if (symbol_section->IsDescendant(objc_section_sp.get()))
3219 {
3220 type = eSymbolTypeRuntime;
3221 if (symbol_name && symbol_name[0] == '.')
3222 {
3223 llvm::StringRef symbol_name_ref(symbol_name);
3224 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3225 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3226 {
3227 symbol_name_non_abi_mangled = symbol_name;
3228 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3229 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003230 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003231 }
3232 }
Chris Lattner24943d22010-06-08 16:52:24 +00003233 }
3234 }
3235 }
3236 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003237 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003238 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003239 }
3240
3241 if (add_nlist)
3242 {
3243 uint64_t symbol_value = nlist.n_value;
3244 bool symbol_name_is_mangled = false;
3245
3246 if (symbol_name_non_abi_mangled)
3247 {
Greg Claytonc0240042012-07-18 23:18:10 +00003248 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3249 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003250 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003251 else
3252 {
3253 if (symbol_name && symbol_name[0] == '_')
3254 {
3255 symbol_name_is_mangled = symbol_name[1] == '_';
3256 symbol_name++; // Skip the leading underscore
3257 }
3258
3259 if (symbol_name)
3260 {
Greg Claytonc0240042012-07-18 23:18:10 +00003261 sym[sym_idx].GetMangled().SetValue(ConstString(symbol_name), symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003262 }
3263 }
3264
3265 if (is_debug == false)
3266 {
3267 if (type == eSymbolTypeCode)
3268 {
3269 // See if we can find a N_FUN entry for any code symbols.
3270 // If we do find a match, and the name matches, then we
3271 // can merge the two into just the function symbol to avoid
3272 // duplicate entries in the symbol table
3273 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3274 if (pos != N_FUN_addr_to_sym_idx.end())
3275 {
3276 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3277 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3278 {
3279 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3280 // We just need the flags from the linker symbol, so put these flags
3281 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3282 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3283 sym[sym_idx].Clear();
3284 continue;
3285 }
3286 }
3287 }
3288 else if (type == eSymbolTypeData)
3289 {
3290 // See if we can find a N_STSYM entry for any data symbols.
3291 // If we do find a match, and the name matches, then we
3292 // can merge the two into just the Static symbol to avoid
3293 // duplicate entries in the symbol table
3294 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3295 if (pos != N_STSYM_addr_to_sym_idx.end())
3296 {
3297 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3298 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3299 {
3300 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3301 // We just need the flags from the linker symbol, so put these flags
3302 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3303 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3304 sym[sym_idx].Clear();
3305 continue;
3306 }
3307 }
3308 }
3309 }
3310 if (symbol_section)
3311 {
3312 const addr_t section_file_addr = symbol_section->GetFileAddress();
3313 if (symbol_byte_size == 0 && function_starts_count > 0)
3314 {
Greg Claytond2653c22012-03-14 01:53:24 +00003315 addr_t symbol_lookup_file_addr = nlist.n_value;
3316 // Do an exact address match for non-ARM addresses, else get the closest since
3317 // the symbol might be a thumb symbol which has an address with bit zero set
3318 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3319 if (is_arm && func_start_entry)
3320 {
3321 // Verify that the function start address is the symbol address (ARM)
3322 // or the symbol address + 1 (thumb)
3323 if (func_start_entry->addr != symbol_lookup_file_addr &&
3324 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3325 {
3326 // Not the right entry, NULL it out...
3327 func_start_entry = NULL;
3328 }
3329 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003330 if (func_start_entry)
3331 {
3332 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003333
Greg Claytond2653c22012-03-14 01:53:24 +00003334 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003335 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003336 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003337
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003338 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3339 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3340 if (next_func_start_entry)
3341 {
Greg Claytond2653c22012-03-14 01:53:24 +00003342 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3343 // Be sure the clear the Thumb address bit when we calculate the size
3344 // from the current and next address
3345 if (is_arm)
3346 next_symbol_file_addr &= 0xfffffffffffffffeull;
3347 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 +00003348 }
3349 else
3350 {
Greg Claytond2653c22012-03-14 01:53:24 +00003351 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003352 }
3353 }
3354 }
3355 symbol_value -= section_file_addr;
3356 }
3357
3358 sym[sym_idx].SetID (nlist_idx);
3359 sym[sym_idx].SetType (type);
3360 sym[sym_idx].GetAddress().SetSection (symbol_section);
3361 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3362 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3363
3364 if (symbol_byte_size > 0)
3365 sym[sym_idx].SetByteSize(symbol_byte_size);
3366
Greg Clayton01e6a582012-11-27 01:52:16 +00003367 if (demangled_is_synthesized)
3368 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3369
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003370 ++sym_idx;
3371 }
3372 else
3373 {
3374 sym[sym_idx].Clear();
3375 }
3376
3377 }
3378
3379 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3380 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3381 // such entries by figuring out what the address for the global is by looking up this non-STAB
3382 // entry and copying the value into the debug symbol's value to save us the hassle in the
3383 // debug symbol parser.
3384
3385 Symbol *global_symbol = NULL;
3386 for (nlist_idx = 0;
3387 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3388 nlist_idx++)
3389 {
3390 if (global_symbol->GetAddress().GetFileAddress() == 0)
3391 {
3392 std::vector<uint32_t> indexes;
3393 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3394 {
3395 std::vector<uint32_t>::const_iterator pos;
3396 std::vector<uint32_t>::const_iterator end = indexes.end();
3397 for (pos = indexes.begin(); pos != end; ++pos)
3398 {
3399 symbol_ptr = symtab->SymbolAtIndex(*pos);
3400 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3401 {
3402 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3403 break;
3404 }
3405 }
3406 }
Chris Lattner24943d22010-06-08 16:52:24 +00003407 }
3408 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003409
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003410 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3411
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003412 if (function_starts_count > 0)
3413 {
3414 char synthetic_function_symbol[PATH_MAX];
3415 uint32_t num_synthetic_function_symbols = 0;
3416 for (i=0; i<function_starts_count; ++i)
3417 {
3418 if (function_starts.GetEntryRef (i).data == false)
3419 ++num_synthetic_function_symbols;
3420 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003421
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003422 if (num_synthetic_function_symbols > 0)
3423 {
3424 if (num_syms < sym_idx + num_synthetic_function_symbols)
3425 {
3426 num_syms = sym_idx + num_synthetic_function_symbols;
3427 sym = symtab->Resize (num_syms);
3428 }
3429 uint32_t synthetic_function_symbol_idx = 0;
3430 for (i=0; i<function_starts_count; ++i)
3431 {
3432 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3433 if (func_start_entry->data == false)
3434 {
Greg Claytond2653c22012-03-14 01:53:24 +00003435 addr_t symbol_file_addr = func_start_entry->addr;
3436 uint32_t symbol_flags = 0;
3437 if (is_arm)
3438 {
3439 if (symbol_file_addr & 1)
3440 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3441 symbol_file_addr &= 0xfffffffffffffffeull;
3442 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003443 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003444 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003445 {
3446 SectionSP symbol_section (symbol_addr.GetSection());
3447 uint32_t symbol_byte_size = 0;
3448 if (symbol_section)
3449 {
3450 const addr_t section_file_addr = symbol_section->GetFileAddress();
3451 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3452 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3453 if (next_func_start_entry)
3454 {
Greg Claytond2653c22012-03-14 01:53:24 +00003455 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3456 if (is_arm)
3457 next_symbol_file_addr &= 0xfffffffffffffffeull;
3458 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 +00003459 }
3460 else
3461 {
Greg Claytond2653c22012-03-14 01:53:24 +00003462 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003463 }
3464 snprintf (synthetic_function_symbol,
3465 sizeof(synthetic_function_symbol),
3466 "___lldb_unnamed_function%u$$%s",
3467 ++synthetic_function_symbol_idx,
3468 module_sp->GetFileSpec().GetFilename().GetCString());
3469 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003470 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003471 sym[sym_idx].SetType (eSymbolTypeCode);
3472 sym[sym_idx].SetIsSynthetic (true);
3473 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003474 if (symbol_flags)
3475 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003476 if (symbol_byte_size)
3477 sym[sym_idx].SetByteSize (symbol_byte_size);
3478 ++sym_idx;
3479 }
3480 }
3481 }
3482 }
3483 }
3484 }
3485
3486 // Trim our symbols down to just what we ended up with after
3487 // removing any symbols.
3488 if (sym_idx < num_syms)
3489 {
3490 num_syms = sym_idx;
3491 sym = symtab->Resize (num_syms);
3492 }
3493
3494 // Now synthesize indirect symbols
3495 if (m_dysymtab.nindirectsyms != 0)
3496 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003497 if (indirect_symbol_index_data.GetByteSize())
3498 {
3499 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3500
3501 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3502 {
3503 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3504 {
3505 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3506 if (symbol_stub_byte_size == 0)
3507 continue;
3508
3509 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3510
3511 if (num_symbol_stubs == 0)
3512 continue;
3513
3514 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3515 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3516 {
3517 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3518 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 +00003519 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003520 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3521 {
3522 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3523 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3524 continue;
3525
3526 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3527 Symbol *stub_symbol = NULL;
3528 if (index_pos != end_index_pos)
3529 {
3530 // We have a remapping from the original nlist index to
3531 // a current symbol index, so just look this up by index
3532 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3533 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003534 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003535 {
3536 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003537 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003538 // S_SYMBOL_STUBS
3539 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3540 }
3541
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003542 if (stub_symbol)
3543 {
3544 Address so_addr(symbol_stub_addr, section_list);
3545
3546 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3547 {
3548 // Change the external symbol into a trampoline that makes sense
3549 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3550 // can re-use them so we don't have to make up a synthetic symbol
3551 // for no good reason.
3552 stub_symbol->SetType (eSymbolTypeTrampoline);
3553 stub_symbol->SetExternal (false);
3554 stub_symbol->GetAddress() = so_addr;
3555 stub_symbol->SetByteSize (symbol_stub_byte_size);
3556 }
3557 else
3558 {
3559 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003560 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003561 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003562 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003563 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003564 stub_symbol = NULL; // this pointer no longer valid
3565 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003566 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003567 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003568 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3569 sym[sym_idx].SetIsSynthetic (true);
3570 sym[sym_idx].GetAddress() = so_addr;
3571 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3572 ++sym_idx;
3573 }
3574 }
Greg Claytond4330e62012-09-05 01:38:55 +00003575 else
3576 {
3577 if (log)
3578 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3579 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003580 }
3581 }
3582 }
3583 }
3584 }
3585 }
3586 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003587 }
3588 return 0;
3589}
3590
3591
3592void
3593ObjectFileMachO::Dump (Stream *s)
3594{
Greg Clayton9482f052012-03-13 23:14:29 +00003595 ModuleSP module_sp(GetModule());
3596 if (module_sp)
3597 {
3598 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3599 s->Printf("%p: ", this);
3600 s->Indent();
3601 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3602 s->PutCString("ObjectFileMachO64");
3603 else
3604 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003605
Greg Clayton9482f052012-03-13 23:14:29 +00003606 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003607
Greg Clayton9482f052012-03-13 23:14:29 +00003608 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003609
Greg Clayton9482f052012-03-13 23:14:29 +00003610 if (m_sections_ap.get())
3611 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003612
Greg Clayton9482f052012-03-13 23:14:29 +00003613 if (m_symtab_ap.get())
3614 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3615 }
Chris Lattner24943d22010-06-08 16:52:24 +00003616}
3617
Greg Clayton36b877d2013-04-24 22:29:28 +00003618bool
3619ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3620 const lldb_private::DataExtractor &data,
3621 lldb::offset_t lc_offset,
3622 lldb_private::UUID& uuid)
3623{
3624 uint32_t i;
3625 struct uuid_command load_cmd;
3626
3627 lldb::offset_t offset = lc_offset;
3628 for (i=0; i<header.ncmds; ++i)
3629 {
3630 const lldb::offset_t cmd_offset = offset;
3631 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3632 break;
3633
3634 if (load_cmd.cmd == LoadCommandUUID)
3635 {
3636 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3637
3638 if (uuid_bytes)
3639 {
3640 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3641 // We pretend these object files have no UUID to prevent crashing.
3642
3643 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3644 0x3b, 0xa8,
3645 0x4b, 0x16,
3646 0xb6, 0xa4,
3647 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3648
3649 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3650 return false;
3651
3652 uuid.SetBytes (uuid_bytes);
3653 return true;
3654 }
3655 return false;
3656 }
3657 offset = cmd_offset + load_cmd.cmdsize;
3658 }
3659 return false;
3660}
Chris Lattner24943d22010-06-08 16:52:24 +00003661
3662bool
Greg Clayton0467c782011-02-04 18:53:10 +00003663ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003664{
Greg Clayton9482f052012-03-13 23:14:29 +00003665 ModuleSP module_sp(GetModule());
3666 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003667 {
Greg Clayton9482f052012-03-13 23:14:29 +00003668 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00003669 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton36b877d2013-04-24 22:29:28 +00003670 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner24943d22010-06-08 16:52:24 +00003671 }
3672 return false;
3673}
3674
3675
3676uint32_t
3677ObjectFileMachO::GetDependentModules (FileSpecList& files)
3678{
Chris Lattner24943d22010-06-08 16:52:24 +00003679 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003680 ModuleSP module_sp(GetModule());
3681 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003682 {
Greg Clayton9482f052012-03-13 23:14:29 +00003683 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3684 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003685 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003686 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3687 uint32_t i;
3688 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003689 {
Greg Clayton9482f052012-03-13 23:14:29 +00003690 const uint32_t cmd_offset = offset;
3691 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3692 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003693
Greg Clayton9482f052012-03-13 23:14:29 +00003694 switch (load_cmd.cmd)
3695 {
3696 case LoadCommandDylibLoad:
3697 case LoadCommandDylibLoadWeak:
3698 case LoadCommandDylibReexport:
3699 case LoadCommandDynamicLinkerLoad:
3700 case LoadCommandFixedVMShlibLoad:
3701 case LoadCommandDylibLoadUpward:
3702 {
3703 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3704 const char *path = m_data.PeekCStr(name_offset);
3705 // Skip any path that starts with '@' since these are usually:
3706 // @executable_path/.../file
3707 // @rpath/.../file
3708 if (path && path[0] != '@')
3709 {
3710 FileSpec file_spec(path, resolve_path);
3711 if (files.AppendIfUnique(file_spec))
3712 count++;
3713 }
3714 }
3715 break;
3716
3717 default:
3718 break;
3719 }
3720 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003721 }
Chris Lattner24943d22010-06-08 16:52:24 +00003722 }
3723 return count;
3724}
3725
Jim Ingham28775942011-03-07 23:44:08 +00003726lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003727ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003728{
3729 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3730 // is initialized to an invalid address, so we can just return that.
3731 // 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 +00003732
Jim Ingham28775942011-03-07 23:44:08 +00003733 if (!IsExecutable() || m_entry_point_address.IsValid())
3734 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003735
3736 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003737 // /usr/include/mach-o.h, but it is basically:
3738 //
3739 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3740 // uint32_t count - this is the count of longs in the thread state data
3741 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3742 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003743 //
Jim Ingham28775942011-03-07 23:44:08 +00003744 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3745 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3746 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3747 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3748 //
3749 // For now we hard-code the offsets and flavors we need:
3750 //
3751 //
3752
Greg Clayton9482f052012-03-13 23:14:29 +00003753 ModuleSP module_sp(GetModule());
3754 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003755 {
Greg Clayton9482f052012-03-13 23:14:29 +00003756 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3757 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003758 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003759 uint32_t i;
3760 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3761 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003762
Greg Clayton9482f052012-03-13 23:14:29 +00003763 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003764 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003765 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003766 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3767 break;
3768
3769 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003770 {
Greg Clayton9482f052012-03-13 23:14:29 +00003771 case LoadCommandUnixThread:
3772 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003773 {
Greg Clayton9482f052012-03-13 23:14:29 +00003774 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003775 {
Greg Clayton9482f052012-03-13 23:14:29 +00003776 uint32_t flavor = m_data.GetU32(&offset);
3777 uint32_t count = m_data.GetU32(&offset);
3778 if (count == 0)
3779 {
3780 // We've gotten off somehow, log and exit;
3781 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003782 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003783
Greg Clayton9482f052012-03-13 23:14:29 +00003784 switch (m_header.cputype)
3785 {
3786 case llvm::MachO::CPUTypeARM:
3787 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3788 {
3789 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3790 start_address = m_data.GetU32(&offset);
3791 done = true;
3792 }
Jim Ingham28775942011-03-07 23:44:08 +00003793 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003794 case llvm::MachO::CPUTypeI386:
3795 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3796 {
3797 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3798 start_address = m_data.GetU32(&offset);
3799 done = true;
3800 }
3801 break;
3802 case llvm::MachO::CPUTypeX86_64:
3803 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3804 {
3805 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3806 start_address = m_data.GetU64(&offset);
3807 done = true;
3808 }
3809 break;
3810 default:
3811 return m_entry_point_address;
3812 }
3813 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3814 if (done)
3815 break;
3816 offset += count * 4;
3817 }
Jim Ingham28775942011-03-07 23:44:08 +00003818 }
Greg Clayton9482f052012-03-13 23:14:29 +00003819 break;
3820 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003821 {
Greg Clayton9482f052012-03-13 23:14:29 +00003822 ConstString text_segment_name ("__TEXT");
3823 uint64_t entryoffset = m_data.GetU64(&offset);
3824 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3825 if (text_segment_sp)
3826 {
3827 done = true;
3828 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3829 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003830 }
Greg Clayton9482f052012-03-13 23:14:29 +00003831
3832 default:
3833 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003834 }
Greg Clayton9482f052012-03-13 23:14:29 +00003835 if (done)
3836 break;
Jim Ingham28775942011-03-07 23:44:08 +00003837
Greg Clayton9482f052012-03-13 23:14:29 +00003838 // Go to the next load command:
3839 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003840 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003841
Greg Clayton9482f052012-03-13 23:14:29 +00003842 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003843 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003844 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003845 // of this ObjectFile:
3846 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003847 {
Greg Clayton9482f052012-03-13 23:14:29 +00003848 m_entry_point_address.Clear();
3849 }
3850 }
3851 else
3852 {
3853 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3854 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003855
Greg Clayton9482f052012-03-13 23:14:29 +00003856 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003857
Greg Clayton9482f052012-03-13 23:14:29 +00003858 if (module_sp)
3859 {
3860 SymbolContextList contexts;
3861 SymbolContext context;
3862 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3863 {
3864 if (contexts.GetContextAtIndex(0, context))
3865 m_entry_point_address = context.symbol->GetAddress();
3866 }
Greg Clayton3508c382012-02-24 01:59:29 +00003867 }
3868 }
Jim Ingham28775942011-03-07 23:44:08 +00003869 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003870
Jim Ingham28775942011-03-07 23:44:08 +00003871 return m_entry_point_address;
3872
3873}
3874
Greg Claytonb5a8f142012-02-05 02:38:54 +00003875lldb_private::Address
3876ObjectFileMachO::GetHeaderAddress ()
3877{
3878 lldb_private::Address header_addr;
3879 SectionList *section_list = GetSectionList();
3880 if (section_list)
3881 {
3882 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3883 if (text_segment_sp)
3884 {
Greg Clayton3508c382012-02-24 01:59:29 +00003885 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003886 header_addr.SetOffset (0);
3887 }
3888 }
3889 return header_addr;
3890}
3891
Greg Clayton46c9a352012-02-09 06:16:32 +00003892uint32_t
3893ObjectFileMachO::GetNumThreadContexts ()
3894{
Greg Clayton9482f052012-03-13 23:14:29 +00003895 ModuleSP module_sp(GetModule());
3896 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003897 {
Greg Clayton9482f052012-03-13 23:14:29 +00003898 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3899 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003900 {
Greg Clayton9482f052012-03-13 23:14:29 +00003901 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003902 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003903 FileRangeArray::Entry file_range;
3904 thread_command thread_cmd;
3905 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003906 {
Greg Clayton9482f052012-03-13 23:14:29 +00003907 const uint32_t cmd_offset = offset;
3908 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3909 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003910
Greg Clayton9482f052012-03-13 23:14:29 +00003911 if (thread_cmd.cmd == LoadCommandThread)
3912 {
3913 file_range.SetRangeBase (offset);
3914 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3915 m_thread_context_offsets.Append (file_range);
3916 }
3917 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003918 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003919 }
3920 }
3921 return m_thread_context_offsets.GetSize();
3922}
3923
3924lldb::RegisterContextSP
3925ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3926{
Greg Clayton46c9a352012-02-09 06:16:32 +00003927 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003928
Greg Clayton9482f052012-03-13 23:14:29 +00003929 ModuleSP module_sp(GetModule());
3930 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003931 {
Greg Clayton9482f052012-03-13 23:14:29 +00003932 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3933 if (!m_thread_context_offsets_valid)
3934 GetNumThreadContexts ();
3935
3936 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003937 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003938 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003939
3940 DataExtractor data (m_data,
3941 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003942 thread_context_file_range->GetByteSize());
3943
3944 switch (m_header.cputype)
3945 {
3946 case llvm::MachO::CPUTypeARM:
3947 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3948 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003949
Jim Ingham6f01c932012-10-12 17:34:26 +00003950 case llvm::MachO::CPUTypeI386:
3951 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3952 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003953
Jim Ingham6f01c932012-10-12 17:34:26 +00003954 case llvm::MachO::CPUTypeX86_64:
3955 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
3956 break;
3957 }
Greg Clayton9482f052012-03-13 23:14:29 +00003958 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003959 }
3960 return reg_ctx_sp;
3961}
3962
Greg Claytonb5a8f142012-02-05 02:38:54 +00003963
Greg Claytonca319972011-07-09 00:41:34 +00003964ObjectFile::Type
3965ObjectFileMachO::CalculateType()
3966{
3967 switch (m_header.filetype)
3968 {
3969 case HeaderFileTypeObject: // 0x1u MH_OBJECT
3970 if (GetAddressByteSize () == 4)
3971 {
3972 // 32 bit kexts are just object files, but they do have a valid
3973 // UUID load command.
3974 UUID uuid;
3975 if (GetUUID(&uuid))
3976 {
3977 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00003978 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00003979 // "OSKextGetCurrentIdentifier" as this is required of kexts
3980 if (m_strata == eStrataInvalid)
3981 m_strata = eStrataKernel;
3982 return eTypeSharedLibrary;
3983 }
3984 }
3985 return eTypeObjectFile;
3986
3987 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
3988 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
3989 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
3990 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
3991 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
3992 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
3993 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
3994 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
3995 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
3996 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
3997 default:
3998 break;
3999 }
4000 return eTypeUnknown;
4001}
4002
4003ObjectFile::Strata
4004ObjectFileMachO::CalculateStrata()
4005{
4006 switch (m_header.filetype)
4007 {
4008 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4009 {
4010 // 32 bit kexts are just object files, but they do have a valid
4011 // UUID load command.
4012 UUID uuid;
4013 if (GetUUID(&uuid))
4014 {
4015 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00004016 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00004017 // "OSKextGetCurrentIdentifier" as this is required of kexts
4018 if (m_type == eTypeInvalid)
4019 m_type = eTypeSharedLibrary;
4020
4021 return eStrataKernel;
4022 }
4023 }
4024 return eStrataUnknown;
4025
4026 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
4027 // Check for the MH_DYLDLINK bit in the flags
4028 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00004029 {
Greg Claytonca319972011-07-09 00:41:34 +00004030 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00004031 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004032 else
Sean Callananac725af2012-02-10 20:22:35 +00004033 {
4034 SectionList *section_list = GetSectionList();
4035 if (section_list)
4036 {
4037 static ConstString g_kld_section_name ("__KLD");
4038 if (section_list->FindSectionByName(g_kld_section_name))
4039 return eStrataKernel;
4040 }
4041 }
4042 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00004043
4044 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4045 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00004046 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00004047 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4048 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4049 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4050 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4051 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4052 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4053 default:
4054 break;
4055 }
4056 return eStrataUnknown;
4057}
4058
4059
Greg Clayton49f4bf22012-02-22 19:41:02 +00004060uint32_t
4061ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4062{
Greg Clayton9482f052012-03-13 23:14:29 +00004063 ModuleSP module_sp(GetModule());
4064 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004065 {
Greg Clayton9482f052012-03-13 23:14:29 +00004066 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4067 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00004068 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00004069 uint32_t version_cmd = 0;
4070 uint64_t version = 0;
4071 uint32_t i;
4072 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004073 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00004074 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00004075 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4076 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00004077
Greg Clayton9482f052012-03-13 23:14:29 +00004078 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004079 {
Greg Clayton9482f052012-03-13 23:14:29 +00004080 if (version_cmd == 0)
4081 {
4082 version_cmd = load_cmd.cmd;
4083 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4084 break;
4085 version = load_cmd.dylib.current_version;
4086 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004087 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00004088 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00004089 }
Greg Clayton9482f052012-03-13 23:14:29 +00004090 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004091 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004092
Greg Clayton9482f052012-03-13 23:14:29 +00004093 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004094 {
Greg Clayton9482f052012-03-13 23:14:29 +00004095 if (versions != NULL && num_versions > 0)
4096 {
4097 if (num_versions > 0)
4098 versions[0] = (version & 0xFFFF0000ull) >> 16;
4099 if (num_versions > 1)
4100 versions[1] = (version & 0x0000FF00ull) >> 8;
4101 if (num_versions > 2)
4102 versions[2] = (version & 0x000000FFull);
4103 // Fill in an remaining version numbers with invalid values
4104 for (i=3; i<num_versions; ++i)
4105 versions[i] = UINT32_MAX;
4106 }
4107 // The LC_ID_DYLIB load command has a version with 3 version numbers
4108 // in it, so always return 3
4109 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004110 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00004111 }
4112 return false;
4113}
4114
Chris Lattner24943d22010-06-08 16:52:24 +00004115bool
Greg Clayton395fc332011-02-15 21:59:32 +00004116ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00004117{
Greg Clayton9482f052012-03-13 23:14:29 +00004118 ModuleSP module_sp(GetModule());
4119 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004120 {
Greg Clayton9482f052012-03-13 23:14:29 +00004121 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4122 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00004123
Greg Clayton9482f052012-03-13 23:14:29 +00004124 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00004125 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00004126 // unknown to make sure we use the DynamicLoaderStatic()...
4127 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4128 {
4129 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4130 }
4131 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004132 }
Greg Clayton9482f052012-03-13 23:14:29 +00004133 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00004134}
4135
4136
Jason Molenda45c75502013-04-16 06:24:42 +00004137UUID
4138ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4139{
4140 UUID uuid;
4141 if (process)
4142 {
4143 addr_t all_image_infos = process->GetImageInfoAddress();
4144
4145 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4146 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4147 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4148 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4149
4150 Error err;
4151 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4152 if (version_or_magic != -1
4153 && version_or_magic != HeaderMagic32
4154 && version_or_magic != HeaderMagic32Swapped
4155 && version_or_magic != HeaderMagic64
4156 && version_or_magic != HeaderMagic64Swapped
4157 && version_or_magic >= 13)
4158 {
4159 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4160 int wordsize = process->GetAddressByteSize();
4161 if (wordsize == 8)
4162 {
4163 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4164 }
4165 if (wordsize == 4)
4166 {
4167 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4168 }
4169 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4170 {
4171 uuid_t shared_cache_uuid;
4172 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4173 {
4174 uuid.SetBytes (shared_cache_uuid);
4175 }
4176 }
4177 }
4178 }
4179 return uuid;
4180}
4181
4182UUID
4183ObjectFileMachO::GetLLDBSharedCacheUUID ()
4184{
4185 UUID uuid;
4186#if defined (__APPLE__) && defined (__arm__)
4187 uint8_t *(*dyld_get_all_image_infos)(void);
4188 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4189 if (dyld_get_all_image_infos)
4190 {
4191 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4192 if (dyld_all_image_infos_address)
4193 {
Jason Molenda6ff916f2013-04-16 21:42:58 +00004194 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4195 if (*version >= 13)
Jason Molenda45c75502013-04-16 06:24:42 +00004196 {
Jason Molenda2ceae992013-04-16 22:56:17 +00004197 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 +00004198 uuid.SetBytes (sharedCacheUUID_address);
4199 }
4200 }
4201 }
4202#endif
4203 return uuid;
4204}
4205
4206
Chris Lattner24943d22010-06-08 16:52:24 +00004207//------------------------------------------------------------------
4208// PluginInterface protocol
4209//------------------------------------------------------------------
Greg Clayton0e191602013-05-10 21:47:16 +00004210lldb_private::ConstString
Chris Lattner24943d22010-06-08 16:52:24 +00004211ObjectFileMachO::GetPluginName()
4212{
Chris Lattner24943d22010-06-08 16:52:24 +00004213 return GetPluginNameStatic();
4214}
4215
4216uint32_t
4217ObjectFileMachO::GetPluginVersion()
4218{
4219 return 1;
4220}
4221