blob: cfa54387b37f0b1c17a6c1e271badee6b23d1b6f [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;
Greg Claytona8364e92013-05-14 22:19:37 +00001715 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001716 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1717 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytona8364e92013-05-14 22:19:37 +00001718 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001719 // Any symbols that get merged into another will get an entry
1720 // in this map so we know
1721 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1722 uint32_t nlist_idx = 0;
1723 Symbol *symbol_ptr = NULL;
1724
1725 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001726 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001727 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001728 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001729 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001730
Jason Molendab62abd52012-06-21 01:51:02 +00001731#if defined (__APPLE__) && defined (__arm__)
1732
1733 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1734 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1735 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1736 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1737 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1738 // nlist parser to ignore all LOCAL symbols.
1739
1740 if (m_header.flags & 0x80000000u)
1741 {
1742 // Before we can start mapping the DSC, we need to make certain the target process is actually
1743 // using the cache we can find.
1744
Jason Molendab62abd52012-06-21 01:51:02 +00001745 // Next we need to determine the correct path for the dyld shared cache.
1746
1747 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1748 char dsc_path[PATH_MAX];
1749
1750 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001751 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1752 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001753 header_arch.GetArchitectureName());
1754
1755 FileSpec dsc_filespec(dsc_path, false);
1756
1757 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda2120aef2013-04-16 00:18:44 +00001758 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001759 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001760 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1761 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1762 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001763 uint32_t imagesOffset;
1764 uint32_t imagesCount;
1765 uint64_t dyldBaseAddress;
1766 uint64_t codeSignatureOffset;
1767 uint64_t codeSignatureSize;
1768 uint64_t slideInfoOffset;
1769 uint64_t slideInfoSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001770 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1771 uint64_t localSymbolsSize; // size of local symbols information
1772 };
1773 struct lldb_copy_dyld_cache_header_v1
1774 {
1775 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1776 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1777 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1778 uint32_t imagesOffset;
1779 uint32_t imagesCount;
1780 uint64_t dyldBaseAddress;
1781 uint64_t codeSignatureOffset;
1782 uint64_t codeSignatureSize;
1783 uint64_t slideInfoOffset;
1784 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001785 uint64_t localSymbolsOffset;
1786 uint64_t localSymbolsSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001787 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001788 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001789
Jason Molenda2120aef2013-04-16 00:18:44 +00001790 struct lldb_copy_dyld_cache_mapping_info
1791 {
1792 uint64_t address;
1793 uint64_t size;
1794 uint64_t fileOffset;
1795 uint32_t maxProt;
1796 uint32_t initProt;
1797 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001798
Greg Claytonab77dcb2012-09-05 22:30:51 +00001799 struct lldb_copy_dyld_cache_local_symbols_info
1800 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001801 uint32_t nlistOffset;
1802 uint32_t nlistCount;
1803 uint32_t stringsOffset;
1804 uint32_t stringsSize;
1805 uint32_t entriesOffset;
1806 uint32_t entriesCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001807 };
1808 struct lldb_copy_dyld_cache_local_symbols_entry
1809 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001810 uint32_t dylibOffset;
1811 uint32_t nlistStartIndex;
1812 uint32_t nlistCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001813 };
Jason Molendab62abd52012-06-21 01:51:02 +00001814
Jason Molendafd3b35d2012-06-22 03:28:35 +00001815 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1816 The dyld_cache_local_symbols_info structure gives us three things:
1817 1. The start and count of the nlist records in the dyld_shared_cache file
1818 2. The start and size of the strings for these nlist records
1819 3. The start and count of dyld_cache_local_symbols_entry entries
1820
1821 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1822 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001823 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001824 and the count of how many nlist records there are for this dylib/framework.
1825 */
1826
Jason Molendab62abd52012-06-21 01:51:02 +00001827 // Process the dsc header to find the unmapped symbols
1828 //
1829 // Save some VM space, do not map the entire cache in one shot.
1830
Jason Molenda6bcabae2013-03-06 23:17:36 +00001831 DataBufferSP dsc_data_sp;
1832 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1833
1834 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001835 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001836 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001837
Jason Molenda6bcabae2013-03-06 23:17:36 +00001838 char version_str[17];
1839 int version = -1;
1840 lldb::offset_t offset = 0;
1841 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1842 version_str[16] = '\0';
1843 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1844 {
1845 int v;
1846 if (::sscanf (version_str + 6, "%d", &v) == 1)
1847 {
1848 version = v;
1849 }
1850 }
1851
Jason Molenda45c75502013-04-16 06:24:42 +00001852 UUID dsc_uuid;
1853 if (version >= 1)
1854 {
1855 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1856 uint8_t uuid_bytes[sizeof (uuid_t)];
1857 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1858 dsc_uuid.SetBytes (uuid_bytes);
1859 }
1860
1861 bool uuid_match = true;
1862 if (dsc_uuid.IsValid() && process)
1863 {
1864 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1865
1866 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1867 {
1868 // The on-disk dyld_shared_cache file is not the same as the one in this
1869 // process' memory, don't use it.
1870 uuid_match = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001871 ModuleSP module_sp (GetModule());
1872 if (module_sp)
1873 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 +00001874 }
1875 }
1876
Jason Molenda9badb6c2013-03-06 23:19:17 +00001877 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001878
Jason Molendab62abd52012-06-21 01:51:02 +00001879 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1880
1881 // If the mappingOffset points to a location inside the header, we've
1882 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda45c75502013-04-16 06:24:42 +00001883 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendab62abd52012-06-21 01:51:02 +00001884 {
1885
Jason Molenda6bcabae2013-03-06 23:17:36 +00001886 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1887 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1888 offset = 0;
1889
1890 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1891 // in the shared library cache need to be adjusted by an offset to match up with the
1892 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1893 // recorded in mapping_offset_value.
1894 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1895
1896 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001897 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1898 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1899
Jason Molenda9badb6c2013-03-06 23:19:17 +00001900 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001901 {
1902 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001903 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001904 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001905 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001906
1907 offset = 0;
1908
1909 // Read the local_symbols_infos struct in one shot
1910 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1911 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1912
Jason Molendab62abd52012-06-21 01:51:02 +00001913 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1914
Jason Molenda6bcabae2013-03-06 23:17:36 +00001915 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001916
1917 offset = local_symbols_info.entriesOffset;
1918 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1919 {
1920 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1921 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1922 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1923 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1924
Jason Molenda9badb6c2013-03-06 23:19:17 +00001925 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001926 {
1927 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1928
1929 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1930 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1931 num_syms = symtab->GetNumSymbols();
1932
1933 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1934 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1935
Jason Molenda9badb6c2013-03-06 23:19:17 +00001936 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001937 {
1938 /////////////////////////////
1939 {
1940 struct nlist_64 nlist;
1941 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1942 break;
1943
1944 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1945 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1946 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1947 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1948 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1949
1950 SymbolType type = eSymbolTypeInvalid;
1951 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1952
1953 if (symbol_name == NULL)
1954 {
1955 // No symbol should be NULL, even the symbols with no
1956 // string values should have an offset zero which points
1957 // to an empty C-string
1958 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00001959 "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 +00001960 entry_index,
1961 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00001962 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendab62abd52012-06-21 01:51:02 +00001963 continue;
1964 }
1965 if (symbol_name[0] == '\0')
1966 symbol_name = NULL;
1967
1968 const char *symbol_name_non_abi_mangled = NULL;
1969
1970 SectionSP symbol_section;
1971 uint32_t symbol_byte_size = 0;
1972 bool add_nlist = true;
1973 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001974 bool demangled_is_synthesized = false;
Greg Claytona8364e92013-05-14 22:19:37 +00001975 bool is_gsym = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001976
1977 assert (sym_idx < num_syms);
1978
1979 sym[sym_idx].SetDebug (is_debug);
1980
1981 if (is_debug)
1982 {
1983 switch (nlist.n_type)
1984 {
1985 case StabGlobalSymbol:
1986 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1987 // Sometimes the N_GSYM value contains the address.
1988
1989 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1990 // have the same address, but we want to ensure that we always find only the real symbol,
1991 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1992 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1993 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1994 // same address.
1995
1996 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1997 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1998 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1999 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2000 add_nlist = false;
2001 else
2002 {
Greg Claytona8364e92013-05-14 22:19:37 +00002003 is_gsym = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002004 sym[sym_idx].SetExternal(true);
2005 if (nlist.n_value != 0)
2006 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2007 type = eSymbolTypeData;
2008 }
2009 break;
2010
2011 case StabFunctionName:
2012 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2013 type = eSymbolTypeCompiler;
2014 break;
2015
2016 case StabFunction:
2017 // N_FUN -- procedure: name,,n_sect,linenumber,address
2018 if (symbol_name)
2019 {
2020 type = eSymbolTypeCode;
2021 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2022
2023 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2024 // We use the current number of symbols in the symbol table in lieu of
2025 // using nlist_idx in case we ever start trimming entries out
2026 N_FUN_indexes.push_back(sym_idx);
2027 }
2028 else
2029 {
2030 type = eSymbolTypeCompiler;
2031
2032 if ( !N_FUN_indexes.empty() )
2033 {
2034 // Copy the size of the function into the original STAB entry so we don't have
2035 // to hunt for it later
2036 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2037 N_FUN_indexes.pop_back();
2038 // We don't really need the end function STAB as it contains the size which
2039 // we already placed with the original symbol, so don't add it if we want a
2040 // minimal symbol table
2041 if (minimize)
2042 add_nlist = false;
2043 }
2044 }
2045 break;
2046
2047 case StabStaticSymbol:
2048 // N_STSYM -- static symbol: name,,n_sect,type,address
2049 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2050 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2051 type = eSymbolTypeData;
2052 break;
2053
2054 case StabLocalCommon:
2055 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2056 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2057 type = eSymbolTypeCommonBlock;
2058 break;
2059
2060 case StabBeginSymbol:
2061 // N_BNSYM
2062 // We use the current number of symbols in the symbol table in lieu of
2063 // using nlist_idx in case we ever start trimming entries out
2064 if (minimize)
2065 {
2066 // Skip these if we want minimal symbol tables
2067 add_nlist = false;
2068 }
2069 else
2070 {
2071 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2072 N_NSYM_indexes.push_back(sym_idx);
2073 type = eSymbolTypeScopeBegin;
2074 }
2075 break;
2076
2077 case StabEndSymbol:
2078 // N_ENSYM
2079 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2080 // so that we can always skip the entire symbol if we need to navigate
2081 // more quickly at the source level when parsing STABS
2082 if (minimize)
2083 {
2084 // Skip these if we want minimal symbol tables
2085 add_nlist = false;
2086 }
2087 else
2088 {
2089 if ( !N_NSYM_indexes.empty() )
2090 {
2091 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2092 symbol_ptr->SetByteSize(sym_idx + 1);
2093 symbol_ptr->SetSizeIsSibling(true);
2094 N_NSYM_indexes.pop_back();
2095 }
2096 type = eSymbolTypeScopeEnd;
2097 }
2098 break;
2099
2100
2101 case StabSourceFileOptions:
2102 // N_OPT - emitted with gcc2_compiled and in gcc source
2103 type = eSymbolTypeCompiler;
2104 break;
2105
2106 case StabRegisterSymbol:
2107 // N_RSYM - register sym: name,,NO_SECT,type,register
2108 type = eSymbolTypeVariable;
2109 break;
2110
2111 case StabSourceLine:
2112 // N_SLINE - src line: 0,,n_sect,linenumber,address
2113 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2114 type = eSymbolTypeLineEntry;
2115 break;
2116
2117 case StabStructureType:
2118 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2119 type = eSymbolTypeVariableType;
2120 break;
2121
2122 case StabSourceFileName:
2123 // N_SO - source file name
2124 type = eSymbolTypeSourceFile;
2125 if (symbol_name == NULL)
2126 {
2127 if (minimize)
2128 add_nlist = false;
2129 if (N_SO_index != UINT32_MAX)
2130 {
2131 // Set the size of the N_SO to the terminating index of this N_SO
2132 // so that we can always skip the entire N_SO if we need to navigate
2133 // more quickly at the source level when parsing STABS
2134 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2135 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2136 symbol_ptr->SetSizeIsSibling(true);
2137 }
2138 N_NSYM_indexes.clear();
2139 N_INCL_indexes.clear();
2140 N_BRAC_indexes.clear();
2141 N_COMM_indexes.clear();
2142 N_FUN_indexes.clear();
2143 N_SO_index = UINT32_MAX;
2144 }
2145 else
2146 {
2147 // We use the current number of symbols in the symbol table in lieu of
2148 // using nlist_idx in case we ever start trimming entries out
2149 const bool N_SO_has_full_path = symbol_name[0] == '/';
2150 if (N_SO_has_full_path)
2151 {
2152 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2153 {
2154 // We have two consecutive N_SO entries where the first contains a directory
2155 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00002156 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002157 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2158 add_nlist = false;
2159 }
2160 else
2161 {
2162 // This is the first entry in a N_SO that contains a directory or
2163 // a full path to the source file
2164 N_SO_index = sym_idx;
2165 }
2166 }
2167 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2168 {
2169 // This is usually the second N_SO entry that contains just the filename,
2170 // so here we combine it with the first one if we are minimizing the symbol table
2171 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2172 if (so_path && so_path[0])
2173 {
2174 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002175 const size_t double_slash_pos = full_so_path.find("//");
2176 if (double_slash_pos != std::string::npos)
2177 {
2178 // The linker has been generating bad N_SO entries with doubled up paths
2179 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2180 // and the second is the directory for the source file so you end up with
2181 // a path that looks like "/tmp/src//tmp/src/"
2182 FileSpec so_dir(so_path, false);
2183 if (!so_dir.Exists())
2184 {
2185 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2186 if (so_dir.Exists())
2187 {
2188 // Trim off the incorrect path
2189 full_so_path.erase(0, double_slash_pos + 1);
2190 }
2191 }
2192 }
Jason Molendab62abd52012-06-21 01:51:02 +00002193 if (*full_so_path.rbegin() != '/')
2194 full_so_path += '/';
2195 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00002196 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002197 add_nlist = false;
2198 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2199 }
2200 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002201 else
2202 {
2203 // This could be a relative path to a N_SO
2204 N_SO_index = sym_idx;
2205 }
Jason Molendab62abd52012-06-21 01:51:02 +00002206 }
Jason Molendab62abd52012-06-21 01:51:02 +00002207 break;
2208
2209 case StabObjectFileName:
2210 // N_OSO - object file name: name,,0,0,st_mtime
2211 type = eSymbolTypeObjectFile;
2212 break;
2213
2214 case StabLocalSymbol:
2215 // N_LSYM - local sym: name,,NO_SECT,type,offset
2216 type = eSymbolTypeLocal;
2217 break;
2218
2219 //----------------------------------------------------------------------
2220 // INCL scopes
2221 //----------------------------------------------------------------------
2222 case StabBeginIncludeFileName:
2223 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2224 // We use the current number of symbols in the symbol table in lieu of
2225 // using nlist_idx in case we ever start trimming entries out
2226 N_INCL_indexes.push_back(sym_idx);
2227 type = eSymbolTypeScopeBegin;
2228 break;
2229
2230 case StabEndIncludeFile:
2231 // N_EINCL - include file end: name,,NO_SECT,0,0
2232 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2233 // so that we can always skip the entire symbol if we need to navigate
2234 // more quickly at the source level when parsing STABS
2235 if ( !N_INCL_indexes.empty() )
2236 {
2237 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2238 symbol_ptr->SetByteSize(sym_idx + 1);
2239 symbol_ptr->SetSizeIsSibling(true);
2240 N_INCL_indexes.pop_back();
2241 }
2242 type = eSymbolTypeScopeEnd;
2243 break;
2244
2245 case StabIncludeFileName:
2246 // N_SOL - #included file name: name,,n_sect,0,address
2247 type = eSymbolTypeHeaderFile;
2248
2249 // We currently don't use the header files on darwin
2250 if (minimize)
2251 add_nlist = false;
2252 break;
2253
2254 case StabCompilerParameters:
2255 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2256 type = eSymbolTypeCompiler;
2257 break;
2258
2259 case StabCompilerVersion:
2260 // N_VERSION - compiler version: name,,NO_SECT,0,0
2261 type = eSymbolTypeCompiler;
2262 break;
2263
2264 case StabCompilerOptLevel:
2265 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2266 type = eSymbolTypeCompiler;
2267 break;
2268
2269 case StabParameter:
2270 // N_PSYM - parameter: name,,NO_SECT,type,offset
2271 type = eSymbolTypeVariable;
2272 break;
2273
2274 case StabAlternateEntry:
2275 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2276 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2277 type = eSymbolTypeLineEntry;
2278 break;
2279
2280 //----------------------------------------------------------------------
2281 // Left and Right Braces
2282 //----------------------------------------------------------------------
2283 case StabLeftBracket:
2284 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2285 // We use the current number of symbols in the symbol table in lieu of
2286 // using nlist_idx in case we ever start trimming entries out
2287 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2288 N_BRAC_indexes.push_back(sym_idx);
2289 type = eSymbolTypeScopeBegin;
2290 break;
2291
2292 case StabRightBracket:
2293 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2294 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2295 // so that we can always skip the entire symbol if we need to navigate
2296 // more quickly at the source level when parsing STABS
2297 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2298 if ( !N_BRAC_indexes.empty() )
2299 {
2300 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2301 symbol_ptr->SetByteSize(sym_idx + 1);
2302 symbol_ptr->SetSizeIsSibling(true);
2303 N_BRAC_indexes.pop_back();
2304 }
2305 type = eSymbolTypeScopeEnd;
2306 break;
2307
2308 case StabDeletedIncludeFile:
2309 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2310 type = eSymbolTypeHeaderFile;
2311 break;
2312
2313 //----------------------------------------------------------------------
2314 // COMM scopes
2315 //----------------------------------------------------------------------
2316 case StabBeginCommon:
2317 // N_BCOMM - begin common: name,,NO_SECT,0,0
2318 // We use the current number of symbols in the symbol table in lieu of
2319 // using nlist_idx in case we ever start trimming entries out
2320 type = eSymbolTypeScopeBegin;
2321 N_COMM_indexes.push_back(sym_idx);
2322 break;
2323
2324 case StabEndCommonLocal:
2325 // N_ECOML - end common (local name): 0,,n_sect,0,address
2326 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2327 // Fall through
2328
2329 case StabEndCommon:
2330 // N_ECOMM - end common: name,,n_sect,0,0
2331 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2332 // so that we can always skip the entire symbol if we need to navigate
2333 // more quickly at the source level when parsing STABS
2334 if ( !N_COMM_indexes.empty() )
2335 {
2336 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2337 symbol_ptr->SetByteSize(sym_idx + 1);
2338 symbol_ptr->SetSizeIsSibling(true);
2339 N_COMM_indexes.pop_back();
2340 }
2341 type = eSymbolTypeScopeEnd;
2342 break;
2343
2344 case StabLength:
2345 // N_LENG - second stab entry with length information
2346 type = eSymbolTypeAdditional;
2347 break;
2348
2349 default: break;
2350 }
2351 }
2352 else
2353 {
2354 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2355 uint8_t n_type = NlistMaskType & nlist.n_type;
2356 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2357
2358 switch (n_type)
2359 {
2360 case NListTypeIndirect: // N_INDR - Fall through
2361 case NListTypePreboundUndefined:// N_PBUD - Fall through
2362 case NListTypeUndefined: // N_UNDF
2363 type = eSymbolTypeUndefined;
2364 break;
2365
2366 case NListTypeAbsolute: // N_ABS
2367 type = eSymbolTypeAbsolute;
2368 break;
2369
2370 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002371 {
2372 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002373
Greg Clayton01e6a582012-11-27 01:52:16 +00002374 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002375 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002376 // TODO: warn about this?
2377 add_nlist = false;
2378 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002379 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002380
2381 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002382 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002383 type = eSymbolTypeException;
2384 }
2385 else
2386 {
2387 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002388
Greg Clayton01e6a582012-11-27 01:52:16 +00002389 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002390 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002391 case SectionTypeRegular: break; // regular section
2392 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2393 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2394 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2395 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2396 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2397 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2398 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2399 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2400 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2401 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2402 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2403 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2404 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2405 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2406 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2407 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2408 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002409 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002410
Greg Clayton01e6a582012-11-27 01:52:16 +00002411 if (type == eSymbolTypeInvalid)
2412 {
2413 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2414 if (symbol_section->IsDescendant (text_section_sp.get()))
2415 {
2416 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2417 SectionAttrUserSelfModifyingCode |
2418 SectionAttrSytemSomeInstructions))
2419 type = eSymbolTypeData;
2420 else
2421 type = eSymbolTypeCode;
2422 }
2423 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002424 {
2425 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2426 {
2427 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002428
Greg Clayton01e6a582012-11-27 01:52:16 +00002429 if (symbol_name &&
2430 symbol_name[0] == '_' &&
2431 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002432 symbol_name[2] == 'B')
2433 {
2434 llvm::StringRef symbol_name_ref(symbol_name);
2435 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2436 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2437 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2438 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2439 {
2440 symbol_name_non_abi_mangled = symbol_name + 1;
2441 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2442 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002443 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002444 }
2445 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2446 {
2447 symbol_name_non_abi_mangled = symbol_name + 1;
2448 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2449 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002450 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002451 }
2452 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2453 {
2454 symbol_name_non_abi_mangled = symbol_name + 1;
2455 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2456 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002457 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002458 }
2459 }
2460 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002461 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002462 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002463 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002464 }
2465 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002466 {
2467 type = eSymbolTypeData;
2468 }
2469 }
2470 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2471 {
2472 type = eSymbolTypeTrampoline;
2473 }
2474 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2475 {
2476 type = eSymbolTypeRuntime;
2477 if (symbol_name && symbol_name[0] == '.')
2478 {
2479 llvm::StringRef symbol_name_ref(symbol_name);
2480 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2481 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002482 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002483 symbol_name_non_abi_mangled = symbol_name;
2484 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2485 type = eSymbolTypeObjCClass;
2486 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002487 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002488 }
2489 }
2490 }
Jason Molendab62abd52012-06-21 01:51:02 +00002491 }
2492 }
Jason Molendab62abd52012-06-21 01:51:02 +00002493 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002494 }
Jason Molendab62abd52012-06-21 01:51:02 +00002495 }
2496
2497 if (add_nlist)
2498 {
2499 uint64_t symbol_value = nlist.n_value;
2500 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002501
Jason Molendab62abd52012-06-21 01:51:02 +00002502 if (symbol_name_non_abi_mangled)
2503 {
Jason Molenda292cca82012-07-20 03:35:44 +00002504 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2505 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002506 }
2507 else
2508 {
2509 if (symbol_name && symbol_name[0] == '_')
2510 {
2511 symbol_name_is_mangled = symbol_name[1] == '_';
2512 symbol_name++; // Skip the leading underscore
2513 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002514
Jason Molendab62abd52012-06-21 01:51:02 +00002515 if (symbol_name)
2516 {
Greg Claytona8364e92013-05-14 22:19:37 +00002517 ConstString const_symbol_name(symbol_name);
2518 if (is_gsym)
2519 N_GSYM_name_to_sym_idx[const_symbol_name.GetCString()] = sym_idx;
2520 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002521 }
2522 }
2523 if (symbol_section)
2524 {
2525 const addr_t section_file_addr = symbol_section->GetFileAddress();
2526 if (symbol_byte_size == 0 && function_starts_count > 0)
2527 {
2528 addr_t symbol_lookup_file_addr = nlist.n_value;
2529 // Do an exact address match for non-ARM addresses, else get the closest since
2530 // the symbol might be a thumb symbol which has an address with bit zero set
2531 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2532 if (is_arm && func_start_entry)
2533 {
2534 // Verify that the function start address is the symbol address (ARM)
2535 // or the symbol address + 1 (thumb)
2536 if (func_start_entry->addr != symbol_lookup_file_addr &&
2537 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2538 {
2539 // Not the right entry, NULL it out...
2540 func_start_entry = NULL;
2541 }
2542 }
2543 if (func_start_entry)
2544 {
2545 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002546
Jason Molendab62abd52012-06-21 01:51:02 +00002547 addr_t symbol_file_addr = func_start_entry->addr;
2548 uint32_t symbol_flags = 0;
2549 if (is_arm)
2550 {
2551 if (symbol_file_addr & 1)
2552 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2553 symbol_file_addr &= 0xfffffffffffffffeull;
2554 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002555
Jason Molendab62abd52012-06-21 01:51:02 +00002556 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2557 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2558 if (next_func_start_entry)
2559 {
2560 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2561 // Be sure the clear the Thumb address bit when we calculate the size
2562 // from the current and next address
2563 if (is_arm)
2564 next_symbol_file_addr &= 0xfffffffffffffffeull;
2565 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2566 }
2567 else
2568 {
2569 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2570 }
2571 }
2572 }
2573 symbol_value -= section_file_addr;
2574 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002575
Greg Claytona8364e92013-05-14 22:19:37 +00002576 if (is_debug == false)
2577 {
2578 if (type == eSymbolTypeCode)
2579 {
2580 // See if we can find a N_FUN entry for any code symbols.
2581 // If we do find a match, and the name matches, then we
2582 // can merge the two into just the function symbol to avoid
2583 // duplicate entries in the symbol table
2584 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2585 if (pos != N_FUN_addr_to_sym_idx.end())
2586 {
2587 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2588 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2589 {
2590 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2591 // We just need the flags from the linker symbol, so put these flags
2592 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2593 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2594 sym[sym_idx].Clear();
2595 continue;
2596 }
2597 }
2598 }
2599 else if (type == eSymbolTypeData)
2600 {
2601 // See if we can find a N_STSYM entry for any data symbols.
2602 // If we do find a match, and the name matches, then we
2603 // can merge the two into just the Static symbol to avoid
2604 // duplicate entries in the symbol table
2605 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2606 if (pos != N_STSYM_addr_to_sym_idx.end())
2607 {
2608 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2609 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2610 {
2611 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2612 // We just need the flags from the linker symbol, so put these flags
2613 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2614 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2615 sym[sym_idx].Clear();
2616 continue;
2617 }
2618 }
2619 else
2620 {
2621 // Combine N_GSYM stab entries with the non stab symbol
2622 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetMangledName().GetCString());
2623 if (pos != N_GSYM_name_to_sym_idx.end())
2624 {
2625 const uint32_t GSYM_sym_idx = pos->second;
2626 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
2627 // Copy the address, because often the N_GSYM address has an invalid address of zero
2628 // when the global is a common symbol
2629 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
2630 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
2631 // We just need the flags from the linker symbol, so put these flags
2632 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2633 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2634 sym[sym_idx].Clear();
2635 continue;
2636 }
2637 }
2638 }
2639 }
2640
Jason Molendab62abd52012-06-21 01:51:02 +00002641 sym[sym_idx].SetID (nlist_idx);
2642 sym[sym_idx].SetType (type);
2643 sym[sym_idx].GetAddress().SetSection (symbol_section);
2644 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2645 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002646
Jason Molendab62abd52012-06-21 01:51:02 +00002647 if (symbol_byte_size > 0)
2648 sym[sym_idx].SetByteSize(symbol_byte_size);
2649
Greg Clayton01e6a582012-11-27 01:52:16 +00002650 if (demangled_is_synthesized)
2651 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002652 ++sym_idx;
2653 }
2654 else
2655 {
2656 sym[sym_idx].Clear();
2657 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002658
Jason Molendab62abd52012-06-21 01:51:02 +00002659 }
2660 /////////////////////////////
2661 }
2662 break; // No more entries to consider
2663 }
2664 }
2665 }
2666 }
2667 }
2668 }
2669 }
2670
2671 // Must reset this in case it was mutated above!
2672 nlist_data_offset = 0;
2673#endif
2674
2675 // If the sym array was not created while parsing the DSC unmapped
2676 // symbols, create it now.
2677 if (sym == NULL)
2678 {
2679 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2680 num_syms = symtab->GetNumSymbols();
2681 }
2682
2683 if (unmapped_local_symbols_found)
2684 {
2685 assert(m_dysymtab.ilocalsym == 0);
2686 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2687 nlist_idx = m_dysymtab.nlocalsym;
2688 }
2689 else
2690 {
2691 nlist_idx = 0;
2692 }
2693
2694 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002695 {
2696 struct nlist_64 nlist;
2697 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2698 break;
2699
2700 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2701 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2702 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2703 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2704 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2705
2706 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002707 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002708
Greg Clayton3a5dc012012-05-25 17:04:00 +00002709 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002710 {
2711 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002712
Greg Claytondd29b972012-05-18 23:20:01 +00002713 if (symbol_name == NULL)
2714 {
2715 // No symbol should be NULL, even the symbols with no
2716 // string values should have an offset zero which points
2717 // to an empty C-string
2718 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00002719 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002720 nlist_idx,
2721 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00002722 module_sp->GetFileSpec().GetPath().c_str());
Greg Claytondd29b972012-05-18 23:20:01 +00002723 continue;
2724 }
2725 if (symbol_name[0] == '\0')
2726 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002727 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002728 else
2729 {
2730 const addr_t str_addr = strtab_addr + nlist.n_strx;
2731 Error str_error;
2732 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2733 symbol_name = memory_symbol_name.c_str();
2734 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002735 const char *symbol_name_non_abi_mangled = NULL;
2736
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002737 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002738 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002739 bool add_nlist = true;
Greg Claytona8364e92013-05-14 22:19:37 +00002740 bool is_gsym = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002741 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002742 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002743
2744 assert (sym_idx < num_syms);
2745
2746 sym[sym_idx].SetDebug (is_debug);
2747
2748 if (is_debug)
2749 {
2750 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002751 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002752 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002753 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2754 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002755
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002756 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2757 // have the same address, but we want to ensure that we always find only the real symbol,
2758 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2759 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2760 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2761 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002762
2763 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002764 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2765 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2766 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2767 add_nlist = false;
2768 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002769 {
Greg Claytona8364e92013-05-14 22:19:37 +00002770 is_gsym = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002771 sym[sym_idx].SetExternal(true);
2772 if (nlist.n_value != 0)
2773 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2774 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002775 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002776 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002777
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002778 case StabFunctionName:
2779 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2780 type = eSymbolTypeCompiler;
2781 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002782
Jason Molenda9badb6c2013-03-06 23:19:17 +00002783 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002784 // N_FUN -- procedure: name,,n_sect,linenumber,address
2785 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002786 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002787 type = eSymbolTypeCode;
2788 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002789
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002790 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2791 // We use the current number of symbols in the symbol table in lieu of
2792 // using nlist_idx in case we ever start trimming entries out
2793 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002794 }
2795 else
2796 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002797 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002798
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002799 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002800 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002801 // Copy the size of the function into the original STAB entry so we don't have
2802 // to hunt for it later
2803 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2804 N_FUN_indexes.pop_back();
2805 // We don't really need the end function STAB as it contains the size which
2806 // we already placed with the original symbol, so don't add it if we want a
2807 // minimal symbol table
2808 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002809 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002810 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002811 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002812 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002813
Jason Molenda9badb6c2013-03-06 23:19:17 +00002814 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002815 // N_STSYM -- static symbol: name,,n_sect,type,address
2816 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2817 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2818 type = eSymbolTypeData;
2819 break;
2820
2821 case StabLocalCommon:
2822 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2823 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2824 type = eSymbolTypeCommonBlock;
2825 break;
2826
2827 case StabBeginSymbol:
2828 // N_BNSYM
2829 // We use the current number of symbols in the symbol table in lieu of
2830 // using nlist_idx in case we ever start trimming entries out
2831 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002832 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002833 // Skip these if we want minimal symbol tables
2834 add_nlist = false;
2835 }
2836 else
2837 {
2838 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2839 N_NSYM_indexes.push_back(sym_idx);
2840 type = eSymbolTypeScopeBegin;
2841 }
2842 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002843
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002844 case StabEndSymbol:
2845 // N_ENSYM
2846 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2847 // so that we can always skip the entire symbol if we need to navigate
2848 // more quickly at the source level when parsing STABS
2849 if (minimize)
2850 {
2851 // Skip these if we want minimal symbol tables
2852 add_nlist = false;
2853 }
2854 else
2855 {
2856 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002857 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002858 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2859 symbol_ptr->SetByteSize(sym_idx + 1);
2860 symbol_ptr->SetSizeIsSibling(true);
2861 N_NSYM_indexes.pop_back();
2862 }
2863 type = eSymbolTypeScopeEnd;
2864 }
2865 break;
2866
2867
2868 case StabSourceFileOptions:
2869 // N_OPT - emitted with gcc2_compiled and in gcc source
2870 type = eSymbolTypeCompiler;
2871 break;
2872
2873 case StabRegisterSymbol:
2874 // N_RSYM - register sym: name,,NO_SECT,type,register
2875 type = eSymbolTypeVariable;
2876 break;
2877
2878 case StabSourceLine:
2879 // N_SLINE - src line: 0,,n_sect,linenumber,address
2880 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2881 type = eSymbolTypeLineEntry;
2882 break;
2883
2884 case StabStructureType:
2885 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2886 type = eSymbolTypeVariableType;
2887 break;
2888
2889 case StabSourceFileName:
2890 // N_SO - source file name
2891 type = eSymbolTypeSourceFile;
2892 if (symbol_name == NULL)
2893 {
2894 if (minimize)
2895 add_nlist = false;
2896 if (N_SO_index != UINT32_MAX)
2897 {
2898 // Set the size of the N_SO to the terminating index of this N_SO
2899 // so that we can always skip the entire N_SO if we need to navigate
2900 // more quickly at the source level when parsing STABS
2901 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2902 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2903 symbol_ptr->SetSizeIsSibling(true);
2904 }
2905 N_NSYM_indexes.clear();
2906 N_INCL_indexes.clear();
2907 N_BRAC_indexes.clear();
2908 N_COMM_indexes.clear();
2909 N_FUN_indexes.clear();
2910 N_SO_index = UINT32_MAX;
2911 }
2912 else
2913 {
2914 // We use the current number of symbols in the symbol table in lieu of
2915 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002916 const bool N_SO_has_full_path = symbol_name[0] == '/';
2917 if (N_SO_has_full_path)
2918 {
2919 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2920 {
2921 // We have two consecutive N_SO entries where the first contains a directory
2922 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002923 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002924 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2925 add_nlist = false;
2926 }
2927 else
2928 {
2929 // This is the first entry in a N_SO that contains a directory or
2930 // a full path to the source file
2931 N_SO_index = sym_idx;
2932 }
2933 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002934 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2935 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002936 // This is usually the second N_SO entry that contains just the filename,
2937 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002938 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2939 if (so_path && so_path[0])
2940 {
2941 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002942 const size_t double_slash_pos = full_so_path.find("//");
2943 if (double_slash_pos != std::string::npos)
2944 {
2945 // The linker has been generating bad N_SO entries with doubled up paths
2946 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2947 // and the second is the directory for the source file so you end up with
2948 // a path that looks like "/tmp/src//tmp/src/"
2949 FileSpec so_dir(so_path, false);
2950 if (!so_dir.Exists())
2951 {
2952 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2953 if (so_dir.Exists())
2954 {
2955 // Trim off the incorrect path
2956 full_so_path.erase(0, double_slash_pos + 1);
2957 }
2958 }
2959 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002960 if (*full_so_path.rbegin() != '/')
2961 full_so_path += '/';
2962 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002963 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002964 add_nlist = false;
2965 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2966 }
2967 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002968 else
2969 {
2970 // This could be a relative path to a N_SO
2971 N_SO_index = sym_idx;
2972 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002973 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002974
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002975 break;
2976
2977 case StabObjectFileName:
2978 // N_OSO - object file name: name,,0,0,st_mtime
2979 type = eSymbolTypeObjectFile;
2980 break;
2981
2982 case StabLocalSymbol:
2983 // N_LSYM - local sym: name,,NO_SECT,type,offset
2984 type = eSymbolTypeLocal;
2985 break;
2986
2987 //----------------------------------------------------------------------
2988 // INCL scopes
2989 //----------------------------------------------------------------------
2990 case StabBeginIncludeFileName:
2991 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2992 // We use the current number of symbols in the symbol table in lieu of
2993 // using nlist_idx in case we ever start trimming entries out
2994 N_INCL_indexes.push_back(sym_idx);
2995 type = eSymbolTypeScopeBegin;
2996 break;
2997
2998 case StabEndIncludeFile:
2999 // N_EINCL - include file end: name,,NO_SECT,0,0
3000 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3001 // so that we can always skip the entire symbol if we need to navigate
3002 // more quickly at the source level when parsing STABS
3003 if ( !N_INCL_indexes.empty() )
3004 {
3005 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3006 symbol_ptr->SetByteSize(sym_idx + 1);
3007 symbol_ptr->SetSizeIsSibling(true);
3008 N_INCL_indexes.pop_back();
3009 }
3010 type = eSymbolTypeScopeEnd;
3011 break;
3012
3013 case StabIncludeFileName:
3014 // N_SOL - #included file name: name,,n_sect,0,address
3015 type = eSymbolTypeHeaderFile;
3016
3017 // We currently don't use the header files on darwin
3018 if (minimize)
3019 add_nlist = false;
3020 break;
3021
Jason Molenda9badb6c2013-03-06 23:19:17 +00003022 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003023 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
3024 type = eSymbolTypeCompiler;
3025 break;
3026
3027 case StabCompilerVersion:
3028 // N_VERSION - compiler version: name,,NO_SECT,0,0
3029 type = eSymbolTypeCompiler;
3030 break;
3031
3032 case StabCompilerOptLevel:
3033 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
3034 type = eSymbolTypeCompiler;
3035 break;
3036
3037 case StabParameter:
3038 // N_PSYM - parameter: name,,NO_SECT,type,offset
3039 type = eSymbolTypeVariable;
3040 break;
3041
3042 case StabAlternateEntry:
3043 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
3044 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3045 type = eSymbolTypeLineEntry;
3046 break;
3047
3048 //----------------------------------------------------------------------
3049 // Left and Right Braces
3050 //----------------------------------------------------------------------
3051 case StabLeftBracket:
3052 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
3053 // We use the current number of symbols in the symbol table in lieu of
3054 // using nlist_idx in case we ever start trimming entries out
3055 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3056 N_BRAC_indexes.push_back(sym_idx);
3057 type = eSymbolTypeScopeBegin;
3058 break;
3059
3060 case StabRightBracket:
3061 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
3062 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3063 // so that we can always skip the entire symbol if we need to navigate
3064 // more quickly at the source level when parsing STABS
3065 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3066 if ( !N_BRAC_indexes.empty() )
3067 {
3068 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3069 symbol_ptr->SetByteSize(sym_idx + 1);
3070 symbol_ptr->SetSizeIsSibling(true);
3071 N_BRAC_indexes.pop_back();
3072 }
3073 type = eSymbolTypeScopeEnd;
3074 break;
3075
3076 case StabDeletedIncludeFile:
3077 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3078 type = eSymbolTypeHeaderFile;
3079 break;
3080
3081 //----------------------------------------------------------------------
3082 // COMM scopes
3083 //----------------------------------------------------------------------
3084 case StabBeginCommon:
3085 // N_BCOMM - begin common: name,,NO_SECT,0,0
3086 // We use the current number of symbols in the symbol table in lieu of
3087 // using nlist_idx in case we ever start trimming entries out
3088 type = eSymbolTypeScopeBegin;
3089 N_COMM_indexes.push_back(sym_idx);
3090 break;
3091
3092 case StabEndCommonLocal:
3093 // N_ECOML - end common (local name): 0,,n_sect,0,address
3094 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3095 // Fall through
3096
3097 case StabEndCommon:
3098 // N_ECOMM - end common: name,,n_sect,0,0
3099 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3100 // so that we can always skip the entire symbol if we need to navigate
3101 // more quickly at the source level when parsing STABS
3102 if ( !N_COMM_indexes.empty() )
3103 {
3104 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3105 symbol_ptr->SetByteSize(sym_idx + 1);
3106 symbol_ptr->SetSizeIsSibling(true);
3107 N_COMM_indexes.pop_back();
3108 }
3109 type = eSymbolTypeScopeEnd;
3110 break;
3111
3112 case StabLength:
3113 // N_LENG - second stab entry with length information
3114 type = eSymbolTypeAdditional;
3115 break;
3116
3117 default: break;
3118 }
3119 }
3120 else
3121 {
3122 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3123 uint8_t n_type = NlistMaskType & nlist.n_type;
3124 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3125
3126 switch (n_type)
3127 {
3128 case NListTypeIndirect: // N_INDR - Fall through
3129 case NListTypePreboundUndefined:// N_PBUD - Fall through
3130 case NListTypeUndefined: // N_UNDF
3131 type = eSymbolTypeUndefined;
3132 break;
3133
3134 case NListTypeAbsolute: // N_ABS
3135 type = eSymbolTypeAbsolute;
3136 break;
3137
3138 case NListTypeSection: // N_SECT
3139 {
3140 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3141
Sean Callananb386d822012-08-09 00:50:26 +00003142 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003143 {
3144 // TODO: warn about this?
3145 add_nlist = false;
3146 break;
3147 }
3148
3149 if (TEXT_eh_frame_sectID == nlist.n_sect)
3150 {
3151 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00003152 }
3153 else
3154 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003155 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3156
3157 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00003158 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003159 case SectionTypeRegular: break; // regular section
3160 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3161 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3162 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3163 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3164 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3165 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3166 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3167 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3168 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3169 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3170 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3171 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3172 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3173 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3174 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3175 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3176 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00003177 }
Chris Lattner24943d22010-06-08 16:52:24 +00003178
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003179 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00003180 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003181 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3182 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003183 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003184 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3185 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003186 SectionAttrSytemSomeInstructions))
3187 type = eSymbolTypeData;
3188 else
3189 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00003190 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003191 else
3192 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003193 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003194 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00003195 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003196 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00003197
Jason Molenda9badb6c2013-03-06 23:19:17 +00003198 if (symbol_name &&
3199 symbol_name[0] == '_' &&
3200 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003201 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00003202 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003203 llvm::StringRef symbol_name_ref(symbol_name);
3204 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3205 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3206 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3207 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00003208 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003209 symbol_name_non_abi_mangled = symbol_name + 1;
3210 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3211 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003212 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003213 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003214 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003215 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003216 symbol_name_non_abi_mangled = symbol_name + 1;
3217 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3218 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003219 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003220 }
3221 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3222 {
3223 symbol_name_non_abi_mangled = symbol_name + 1;
3224 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3225 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003226 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003227 }
3228 }
3229 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003230 else
3231 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3232 {
3233 type = eSymbolTypeException;
3234 }
3235 else
3236 {
3237 type = eSymbolTypeData;
3238 }
3239 }
3240 else
3241 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3242 {
3243 type = eSymbolTypeTrampoline;
3244 }
3245 else
3246 if (symbol_section->IsDescendant(objc_section_sp.get()))
3247 {
3248 type = eSymbolTypeRuntime;
3249 if (symbol_name && symbol_name[0] == '.')
3250 {
3251 llvm::StringRef symbol_name_ref(symbol_name);
3252 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3253 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3254 {
3255 symbol_name_non_abi_mangled = symbol_name;
3256 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3257 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003258 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003259 }
3260 }
Chris Lattner24943d22010-06-08 16:52:24 +00003261 }
3262 }
3263 }
3264 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003265 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003266 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003267 }
3268
3269 if (add_nlist)
3270 {
3271 uint64_t symbol_value = nlist.n_value;
3272 bool symbol_name_is_mangled = false;
3273
3274 if (symbol_name_non_abi_mangled)
3275 {
Greg Claytonc0240042012-07-18 23:18:10 +00003276 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3277 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003278 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003279 else
3280 {
3281 if (symbol_name && symbol_name[0] == '_')
3282 {
3283 symbol_name_is_mangled = symbol_name[1] == '_';
3284 symbol_name++; // Skip the leading underscore
3285 }
3286
3287 if (symbol_name)
3288 {
Greg Claytona8364e92013-05-14 22:19:37 +00003289 ConstString const_symbol_name(symbol_name);
3290 if (is_gsym)
3291 N_GSYM_name_to_sym_idx[const_symbol_name.GetCString()] = sym_idx;
3292 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003293 }
3294 }
3295 if (symbol_section)
3296 {
3297 const addr_t section_file_addr = symbol_section->GetFileAddress();
3298 if (symbol_byte_size == 0 && function_starts_count > 0)
3299 {
Greg Claytond2653c22012-03-14 01:53:24 +00003300 addr_t symbol_lookup_file_addr = nlist.n_value;
3301 // Do an exact address match for non-ARM addresses, else get the closest since
3302 // the symbol might be a thumb symbol which has an address with bit zero set
3303 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3304 if (is_arm && func_start_entry)
3305 {
3306 // Verify that the function start address is the symbol address (ARM)
3307 // or the symbol address + 1 (thumb)
3308 if (func_start_entry->addr != symbol_lookup_file_addr &&
3309 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3310 {
3311 // Not the right entry, NULL it out...
3312 func_start_entry = NULL;
3313 }
3314 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003315 if (func_start_entry)
3316 {
3317 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003318
Greg Claytond2653c22012-03-14 01:53:24 +00003319 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003320 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003321 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003322
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003323 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3324 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3325 if (next_func_start_entry)
3326 {
Greg Claytond2653c22012-03-14 01:53:24 +00003327 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3328 // Be sure the clear the Thumb address bit when we calculate the size
3329 // from the current and next address
3330 if (is_arm)
3331 next_symbol_file_addr &= 0xfffffffffffffffeull;
3332 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 +00003333 }
3334 else
3335 {
Greg Claytond2653c22012-03-14 01:53:24 +00003336 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003337 }
3338 }
3339 }
3340 symbol_value -= section_file_addr;
3341 }
3342
Greg Claytona8364e92013-05-14 22:19:37 +00003343 if (is_debug == false)
3344 {
3345 if (type == eSymbolTypeCode)
3346 {
3347 // See if we can find a N_FUN entry for any code symbols.
3348 // If we do find a match, and the name matches, then we
3349 // can merge the two into just the function symbol to avoid
3350 // duplicate entries in the symbol table
3351 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3352 if (pos != N_FUN_addr_to_sym_idx.end())
3353 {
3354 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3355 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3356 {
3357 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3358 // We just need the flags from the linker symbol, so put these flags
3359 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3360 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3361 sym[sym_idx].Clear();
3362 continue;
3363 }
3364 }
3365 }
3366 else if (type == eSymbolTypeData)
3367 {
3368 // See if we can find a N_STSYM entry for any data symbols.
3369 // If we do find a match, and the name matches, then we
3370 // can merge the two into just the Static symbol to avoid
3371 // duplicate entries in the symbol table
3372 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3373 if (pos != N_STSYM_addr_to_sym_idx.end())
3374 {
3375 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3376 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3377 {
3378 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3379 // We just need the flags from the linker symbol, so put these flags
3380 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3381 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3382 sym[sym_idx].Clear();
3383 continue;
3384 }
3385 }
3386 else
3387 {
3388 // Combine N_GSYM stab entries with the non stab symbol
3389 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetMangledName().GetCString());
3390 if (pos != N_GSYM_name_to_sym_idx.end())
3391 {
3392 const uint32_t GSYM_sym_idx = pos->second;
3393 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3394 // Copy the address, because often the N_GSYM address has an invalid address of zero
3395 // when the global is a common symbol
3396 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3397 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3398 // We just need the flags from the linker symbol, so put these flags
3399 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3400 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3401 sym[sym_idx].Clear();
3402 continue;
3403 }
3404 }
3405 }
3406 }
3407
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003408 sym[sym_idx].SetID (nlist_idx);
3409 sym[sym_idx].SetType (type);
3410 sym[sym_idx].GetAddress().SetSection (symbol_section);
3411 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3412 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3413
3414 if (symbol_byte_size > 0)
3415 sym[sym_idx].SetByteSize(symbol_byte_size);
3416
Greg Clayton01e6a582012-11-27 01:52:16 +00003417 if (demangled_is_synthesized)
3418 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3419
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003420 ++sym_idx;
3421 }
3422 else
3423 {
3424 sym[sym_idx].Clear();
3425 }
3426
3427 }
3428
3429 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3430 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3431 // such entries by figuring out what the address for the global is by looking up this non-STAB
3432 // entry and copying the value into the debug symbol's value to save us the hassle in the
3433 // debug symbol parser.
3434
3435 Symbol *global_symbol = NULL;
3436 for (nlist_idx = 0;
3437 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3438 nlist_idx++)
3439 {
3440 if (global_symbol->GetAddress().GetFileAddress() == 0)
3441 {
3442 std::vector<uint32_t> indexes;
3443 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3444 {
3445 std::vector<uint32_t>::const_iterator pos;
3446 std::vector<uint32_t>::const_iterator end = indexes.end();
3447 for (pos = indexes.begin(); pos != end; ++pos)
3448 {
3449 symbol_ptr = symtab->SymbolAtIndex(*pos);
3450 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3451 {
3452 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3453 break;
3454 }
3455 }
3456 }
Chris Lattner24943d22010-06-08 16:52:24 +00003457 }
3458 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003459
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003460 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3461
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003462 if (function_starts_count > 0)
3463 {
3464 char synthetic_function_symbol[PATH_MAX];
3465 uint32_t num_synthetic_function_symbols = 0;
3466 for (i=0; i<function_starts_count; ++i)
3467 {
3468 if (function_starts.GetEntryRef (i).data == false)
3469 ++num_synthetic_function_symbols;
3470 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003471
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003472 if (num_synthetic_function_symbols > 0)
3473 {
3474 if (num_syms < sym_idx + num_synthetic_function_symbols)
3475 {
3476 num_syms = sym_idx + num_synthetic_function_symbols;
3477 sym = symtab->Resize (num_syms);
3478 }
3479 uint32_t synthetic_function_symbol_idx = 0;
3480 for (i=0; i<function_starts_count; ++i)
3481 {
3482 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3483 if (func_start_entry->data == false)
3484 {
Greg Claytond2653c22012-03-14 01:53:24 +00003485 addr_t symbol_file_addr = func_start_entry->addr;
3486 uint32_t symbol_flags = 0;
3487 if (is_arm)
3488 {
3489 if (symbol_file_addr & 1)
3490 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3491 symbol_file_addr &= 0xfffffffffffffffeull;
3492 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003493 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003494 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003495 {
3496 SectionSP symbol_section (symbol_addr.GetSection());
3497 uint32_t symbol_byte_size = 0;
3498 if (symbol_section)
3499 {
3500 const addr_t section_file_addr = symbol_section->GetFileAddress();
3501 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3502 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3503 if (next_func_start_entry)
3504 {
Greg Claytond2653c22012-03-14 01:53:24 +00003505 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3506 if (is_arm)
3507 next_symbol_file_addr &= 0xfffffffffffffffeull;
3508 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 +00003509 }
3510 else
3511 {
Greg Claytond2653c22012-03-14 01:53:24 +00003512 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003513 }
3514 snprintf (synthetic_function_symbol,
3515 sizeof(synthetic_function_symbol),
3516 "___lldb_unnamed_function%u$$%s",
3517 ++synthetic_function_symbol_idx,
3518 module_sp->GetFileSpec().GetFilename().GetCString());
3519 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003520 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003521 sym[sym_idx].SetType (eSymbolTypeCode);
3522 sym[sym_idx].SetIsSynthetic (true);
3523 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003524 if (symbol_flags)
3525 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003526 if (symbol_byte_size)
3527 sym[sym_idx].SetByteSize (symbol_byte_size);
3528 ++sym_idx;
3529 }
3530 }
3531 }
3532 }
3533 }
3534 }
3535
3536 // Trim our symbols down to just what we ended up with after
3537 // removing any symbols.
3538 if (sym_idx < num_syms)
3539 {
3540 num_syms = sym_idx;
3541 sym = symtab->Resize (num_syms);
3542 }
3543
3544 // Now synthesize indirect symbols
3545 if (m_dysymtab.nindirectsyms != 0)
3546 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003547 if (indirect_symbol_index_data.GetByteSize())
3548 {
3549 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3550
3551 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3552 {
3553 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3554 {
3555 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3556 if (symbol_stub_byte_size == 0)
3557 continue;
3558
3559 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3560
3561 if (num_symbol_stubs == 0)
3562 continue;
3563
3564 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3565 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3566 {
3567 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3568 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 +00003569 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003570 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3571 {
3572 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3573 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3574 continue;
3575
3576 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3577 Symbol *stub_symbol = NULL;
3578 if (index_pos != end_index_pos)
3579 {
3580 // We have a remapping from the original nlist index to
3581 // a current symbol index, so just look this up by index
3582 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3583 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003584 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003585 {
3586 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003587 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003588 // S_SYMBOL_STUBS
3589 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3590 }
3591
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003592 if (stub_symbol)
3593 {
3594 Address so_addr(symbol_stub_addr, section_list);
3595
3596 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3597 {
3598 // Change the external symbol into a trampoline that makes sense
3599 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3600 // can re-use them so we don't have to make up a synthetic symbol
3601 // for no good reason.
3602 stub_symbol->SetType (eSymbolTypeTrampoline);
3603 stub_symbol->SetExternal (false);
3604 stub_symbol->GetAddress() = so_addr;
3605 stub_symbol->SetByteSize (symbol_stub_byte_size);
3606 }
3607 else
3608 {
3609 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003610 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003611 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003612 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003613 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003614 stub_symbol = NULL; // this pointer no longer valid
3615 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003616 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003617 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003618 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3619 sym[sym_idx].SetIsSynthetic (true);
3620 sym[sym_idx].GetAddress() = so_addr;
3621 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3622 ++sym_idx;
3623 }
3624 }
Greg Claytond4330e62012-09-05 01:38:55 +00003625 else
3626 {
3627 if (log)
3628 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3629 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003630 }
3631 }
3632 }
3633 }
3634 }
3635 }
3636 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003637 }
3638 return 0;
3639}
3640
3641
3642void
3643ObjectFileMachO::Dump (Stream *s)
3644{
Greg Clayton9482f052012-03-13 23:14:29 +00003645 ModuleSP module_sp(GetModule());
3646 if (module_sp)
3647 {
3648 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3649 s->Printf("%p: ", this);
3650 s->Indent();
3651 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3652 s->PutCString("ObjectFileMachO64");
3653 else
3654 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003655
Greg Clayton9482f052012-03-13 23:14:29 +00003656 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003657
Greg Clayton9482f052012-03-13 23:14:29 +00003658 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003659
Greg Clayton9482f052012-03-13 23:14:29 +00003660 if (m_sections_ap.get())
3661 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003662
Greg Clayton9482f052012-03-13 23:14:29 +00003663 if (m_symtab_ap.get())
3664 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3665 }
Chris Lattner24943d22010-06-08 16:52:24 +00003666}
3667
Greg Clayton36b877d2013-04-24 22:29:28 +00003668bool
3669ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3670 const lldb_private::DataExtractor &data,
3671 lldb::offset_t lc_offset,
3672 lldb_private::UUID& uuid)
3673{
3674 uint32_t i;
3675 struct uuid_command load_cmd;
3676
3677 lldb::offset_t offset = lc_offset;
3678 for (i=0; i<header.ncmds; ++i)
3679 {
3680 const lldb::offset_t cmd_offset = offset;
3681 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3682 break;
3683
3684 if (load_cmd.cmd == LoadCommandUUID)
3685 {
3686 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3687
3688 if (uuid_bytes)
3689 {
3690 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3691 // We pretend these object files have no UUID to prevent crashing.
3692
3693 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3694 0x3b, 0xa8,
3695 0x4b, 0x16,
3696 0xb6, 0xa4,
3697 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3698
3699 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3700 return false;
3701
3702 uuid.SetBytes (uuid_bytes);
3703 return true;
3704 }
3705 return false;
3706 }
3707 offset = cmd_offset + load_cmd.cmdsize;
3708 }
3709 return false;
3710}
Chris Lattner24943d22010-06-08 16:52:24 +00003711
3712bool
Greg Clayton0467c782011-02-04 18:53:10 +00003713ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003714{
Greg Clayton9482f052012-03-13 23:14:29 +00003715 ModuleSP module_sp(GetModule());
3716 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003717 {
Greg Clayton9482f052012-03-13 23:14:29 +00003718 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00003719 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton36b877d2013-04-24 22:29:28 +00003720 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner24943d22010-06-08 16:52:24 +00003721 }
3722 return false;
3723}
3724
3725
3726uint32_t
3727ObjectFileMachO::GetDependentModules (FileSpecList& files)
3728{
Chris Lattner24943d22010-06-08 16:52:24 +00003729 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003730 ModuleSP module_sp(GetModule());
3731 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003732 {
Greg Clayton9482f052012-03-13 23:14:29 +00003733 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3734 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003735 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003736 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3737 uint32_t i;
3738 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003739 {
Greg Clayton9482f052012-03-13 23:14:29 +00003740 const uint32_t cmd_offset = offset;
3741 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3742 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003743
Greg Clayton9482f052012-03-13 23:14:29 +00003744 switch (load_cmd.cmd)
3745 {
3746 case LoadCommandDylibLoad:
3747 case LoadCommandDylibLoadWeak:
3748 case LoadCommandDylibReexport:
3749 case LoadCommandDynamicLinkerLoad:
3750 case LoadCommandFixedVMShlibLoad:
3751 case LoadCommandDylibLoadUpward:
3752 {
3753 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3754 const char *path = m_data.PeekCStr(name_offset);
3755 // Skip any path that starts with '@' since these are usually:
3756 // @executable_path/.../file
3757 // @rpath/.../file
3758 if (path && path[0] != '@')
3759 {
3760 FileSpec file_spec(path, resolve_path);
3761 if (files.AppendIfUnique(file_spec))
3762 count++;
3763 }
3764 }
3765 break;
3766
3767 default:
3768 break;
3769 }
3770 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003771 }
Chris Lattner24943d22010-06-08 16:52:24 +00003772 }
3773 return count;
3774}
3775
Jim Ingham28775942011-03-07 23:44:08 +00003776lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003777ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003778{
3779 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3780 // is initialized to an invalid address, so we can just return that.
3781 // 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 +00003782
Jim Ingham28775942011-03-07 23:44:08 +00003783 if (!IsExecutable() || m_entry_point_address.IsValid())
3784 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003785
3786 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003787 // /usr/include/mach-o.h, but it is basically:
3788 //
3789 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3790 // uint32_t count - this is the count of longs in the thread state data
3791 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3792 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003793 //
Jim Ingham28775942011-03-07 23:44:08 +00003794 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3795 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3796 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3797 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3798 //
3799 // For now we hard-code the offsets and flavors we need:
3800 //
3801 //
3802
Greg Clayton9482f052012-03-13 23:14:29 +00003803 ModuleSP module_sp(GetModule());
3804 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003805 {
Greg Clayton9482f052012-03-13 23:14:29 +00003806 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3807 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003808 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003809 uint32_t i;
3810 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3811 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003812
Greg Clayton9482f052012-03-13 23:14:29 +00003813 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003814 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003815 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003816 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3817 break;
3818
3819 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003820 {
Greg Clayton9482f052012-03-13 23:14:29 +00003821 case LoadCommandUnixThread:
3822 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003823 {
Greg Clayton9482f052012-03-13 23:14:29 +00003824 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003825 {
Greg Clayton9482f052012-03-13 23:14:29 +00003826 uint32_t flavor = m_data.GetU32(&offset);
3827 uint32_t count = m_data.GetU32(&offset);
3828 if (count == 0)
3829 {
3830 // We've gotten off somehow, log and exit;
3831 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003832 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003833
Greg Clayton9482f052012-03-13 23:14:29 +00003834 switch (m_header.cputype)
3835 {
3836 case llvm::MachO::CPUTypeARM:
3837 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3838 {
3839 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3840 start_address = m_data.GetU32(&offset);
3841 done = true;
3842 }
Jim Ingham28775942011-03-07 23:44:08 +00003843 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003844 case llvm::MachO::CPUTypeI386:
3845 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3846 {
3847 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3848 start_address = m_data.GetU32(&offset);
3849 done = true;
3850 }
3851 break;
3852 case llvm::MachO::CPUTypeX86_64:
3853 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3854 {
3855 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3856 start_address = m_data.GetU64(&offset);
3857 done = true;
3858 }
3859 break;
3860 default:
3861 return m_entry_point_address;
3862 }
3863 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3864 if (done)
3865 break;
3866 offset += count * 4;
3867 }
Jim Ingham28775942011-03-07 23:44:08 +00003868 }
Greg Clayton9482f052012-03-13 23:14:29 +00003869 break;
3870 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003871 {
Greg Clayton9482f052012-03-13 23:14:29 +00003872 ConstString text_segment_name ("__TEXT");
3873 uint64_t entryoffset = m_data.GetU64(&offset);
3874 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3875 if (text_segment_sp)
3876 {
3877 done = true;
3878 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3879 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003880 }
Greg Clayton9482f052012-03-13 23:14:29 +00003881
3882 default:
3883 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003884 }
Greg Clayton9482f052012-03-13 23:14:29 +00003885 if (done)
3886 break;
Jim Ingham28775942011-03-07 23:44:08 +00003887
Greg Clayton9482f052012-03-13 23:14:29 +00003888 // Go to the next load command:
3889 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003890 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003891
Greg Clayton9482f052012-03-13 23:14:29 +00003892 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003893 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003894 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003895 // of this ObjectFile:
3896 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003897 {
Greg Clayton9482f052012-03-13 23:14:29 +00003898 m_entry_point_address.Clear();
3899 }
3900 }
3901 else
3902 {
3903 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3904 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003905
Greg Clayton9482f052012-03-13 23:14:29 +00003906 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003907
Greg Clayton9482f052012-03-13 23:14:29 +00003908 if (module_sp)
3909 {
3910 SymbolContextList contexts;
3911 SymbolContext context;
3912 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3913 {
3914 if (contexts.GetContextAtIndex(0, context))
3915 m_entry_point_address = context.symbol->GetAddress();
3916 }
Greg Clayton3508c382012-02-24 01:59:29 +00003917 }
3918 }
Jim Ingham28775942011-03-07 23:44:08 +00003919 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003920
Jim Ingham28775942011-03-07 23:44:08 +00003921 return m_entry_point_address;
3922
3923}
3924
Greg Claytonb5a8f142012-02-05 02:38:54 +00003925lldb_private::Address
3926ObjectFileMachO::GetHeaderAddress ()
3927{
3928 lldb_private::Address header_addr;
3929 SectionList *section_list = GetSectionList();
3930 if (section_list)
3931 {
3932 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3933 if (text_segment_sp)
3934 {
Greg Clayton3508c382012-02-24 01:59:29 +00003935 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003936 header_addr.SetOffset (0);
3937 }
3938 }
3939 return header_addr;
3940}
3941
Greg Clayton46c9a352012-02-09 06:16:32 +00003942uint32_t
3943ObjectFileMachO::GetNumThreadContexts ()
3944{
Greg Clayton9482f052012-03-13 23:14:29 +00003945 ModuleSP module_sp(GetModule());
3946 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003947 {
Greg Clayton9482f052012-03-13 23:14:29 +00003948 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3949 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003950 {
Greg Clayton9482f052012-03-13 23:14:29 +00003951 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003952 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003953 FileRangeArray::Entry file_range;
3954 thread_command thread_cmd;
3955 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003956 {
Greg Clayton9482f052012-03-13 23:14:29 +00003957 const uint32_t cmd_offset = offset;
3958 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3959 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003960
Greg Clayton9482f052012-03-13 23:14:29 +00003961 if (thread_cmd.cmd == LoadCommandThread)
3962 {
3963 file_range.SetRangeBase (offset);
3964 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3965 m_thread_context_offsets.Append (file_range);
3966 }
3967 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003968 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003969 }
3970 }
3971 return m_thread_context_offsets.GetSize();
3972}
3973
3974lldb::RegisterContextSP
3975ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3976{
Greg Clayton46c9a352012-02-09 06:16:32 +00003977 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003978
Greg Clayton9482f052012-03-13 23:14:29 +00003979 ModuleSP module_sp(GetModule());
3980 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003981 {
Greg Clayton9482f052012-03-13 23:14:29 +00003982 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3983 if (!m_thread_context_offsets_valid)
3984 GetNumThreadContexts ();
3985
3986 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003987 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003988 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003989
3990 DataExtractor data (m_data,
3991 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003992 thread_context_file_range->GetByteSize());
3993
3994 switch (m_header.cputype)
3995 {
3996 case llvm::MachO::CPUTypeARM:
3997 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3998 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003999
Jim Ingham6f01c932012-10-12 17:34:26 +00004000 case llvm::MachO::CPUTypeI386:
4001 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
4002 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00004003
Jim Ingham6f01c932012-10-12 17:34:26 +00004004 case llvm::MachO::CPUTypeX86_64:
4005 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
4006 break;
4007 }
Greg Clayton9482f052012-03-13 23:14:29 +00004008 }
Greg Clayton46c9a352012-02-09 06:16:32 +00004009 }
4010 return reg_ctx_sp;
4011}
4012
Greg Claytonb5a8f142012-02-05 02:38:54 +00004013
Greg Claytonca319972011-07-09 00:41:34 +00004014ObjectFile::Type
4015ObjectFileMachO::CalculateType()
4016{
4017 switch (m_header.filetype)
4018 {
4019 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4020 if (GetAddressByteSize () == 4)
4021 {
4022 // 32 bit kexts are just object files, but they do have a valid
4023 // UUID load command.
4024 UUID uuid;
4025 if (GetUUID(&uuid))
4026 {
4027 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00004028 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00004029 // "OSKextGetCurrentIdentifier" as this is required of kexts
4030 if (m_strata == eStrataInvalid)
4031 m_strata = eStrataKernel;
4032 return eTypeSharedLibrary;
4033 }
4034 }
4035 return eTypeObjectFile;
4036
4037 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
4038 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
4039 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
4040 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
4041 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
4042 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
4043 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
4044 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
4045 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
4046 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
4047 default:
4048 break;
4049 }
4050 return eTypeUnknown;
4051}
4052
4053ObjectFile::Strata
4054ObjectFileMachO::CalculateStrata()
4055{
4056 switch (m_header.filetype)
4057 {
4058 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4059 {
4060 // 32 bit kexts are just object files, but they do have a valid
4061 // UUID load command.
4062 UUID uuid;
4063 if (GetUUID(&uuid))
4064 {
4065 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00004066 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00004067 // "OSKextGetCurrentIdentifier" as this is required of kexts
4068 if (m_type == eTypeInvalid)
4069 m_type = eTypeSharedLibrary;
4070
4071 return eStrataKernel;
4072 }
4073 }
4074 return eStrataUnknown;
4075
4076 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
4077 // Check for the MH_DYLDLINK bit in the flags
4078 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00004079 {
Greg Claytonca319972011-07-09 00:41:34 +00004080 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00004081 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004082 else
Sean Callananac725af2012-02-10 20:22:35 +00004083 {
4084 SectionList *section_list = GetSectionList();
4085 if (section_list)
4086 {
4087 static ConstString g_kld_section_name ("__KLD");
4088 if (section_list->FindSectionByName(g_kld_section_name))
4089 return eStrataKernel;
4090 }
4091 }
4092 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00004093
4094 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4095 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00004096 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00004097 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4098 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4099 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4100 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4101 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4102 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4103 default:
4104 break;
4105 }
4106 return eStrataUnknown;
4107}
4108
4109
Greg Clayton49f4bf22012-02-22 19:41:02 +00004110uint32_t
4111ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4112{
Greg Clayton9482f052012-03-13 23:14:29 +00004113 ModuleSP module_sp(GetModule());
4114 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004115 {
Greg Clayton9482f052012-03-13 23:14:29 +00004116 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4117 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00004118 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00004119 uint32_t version_cmd = 0;
4120 uint64_t version = 0;
4121 uint32_t i;
4122 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004123 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00004124 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00004125 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4126 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00004127
Greg Clayton9482f052012-03-13 23:14:29 +00004128 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004129 {
Greg Clayton9482f052012-03-13 23:14:29 +00004130 if (version_cmd == 0)
4131 {
4132 version_cmd = load_cmd.cmd;
4133 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4134 break;
4135 version = load_cmd.dylib.current_version;
4136 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004137 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00004138 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00004139 }
Greg Clayton9482f052012-03-13 23:14:29 +00004140 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004141 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004142
Greg Clayton9482f052012-03-13 23:14:29 +00004143 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004144 {
Greg Clayton9482f052012-03-13 23:14:29 +00004145 if (versions != NULL && num_versions > 0)
4146 {
4147 if (num_versions > 0)
4148 versions[0] = (version & 0xFFFF0000ull) >> 16;
4149 if (num_versions > 1)
4150 versions[1] = (version & 0x0000FF00ull) >> 8;
4151 if (num_versions > 2)
4152 versions[2] = (version & 0x000000FFull);
4153 // Fill in an remaining version numbers with invalid values
4154 for (i=3; i<num_versions; ++i)
4155 versions[i] = UINT32_MAX;
4156 }
4157 // The LC_ID_DYLIB load command has a version with 3 version numbers
4158 // in it, so always return 3
4159 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004160 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00004161 }
4162 return false;
4163}
4164
Chris Lattner24943d22010-06-08 16:52:24 +00004165bool
Greg Clayton395fc332011-02-15 21:59:32 +00004166ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00004167{
Greg Clayton9482f052012-03-13 23:14:29 +00004168 ModuleSP module_sp(GetModule());
4169 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004170 {
Greg Clayton9482f052012-03-13 23:14:29 +00004171 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4172 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00004173
Greg Clayton9482f052012-03-13 23:14:29 +00004174 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00004175 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00004176 // unknown to make sure we use the DynamicLoaderStatic()...
4177 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4178 {
4179 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4180 }
4181 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004182 }
Greg Clayton9482f052012-03-13 23:14:29 +00004183 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00004184}
4185
4186
Jason Molenda45c75502013-04-16 06:24:42 +00004187UUID
4188ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4189{
4190 UUID uuid;
4191 if (process)
4192 {
4193 addr_t all_image_infos = process->GetImageInfoAddress();
4194
4195 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4196 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4197 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4198 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4199
4200 Error err;
4201 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4202 if (version_or_magic != -1
4203 && version_or_magic != HeaderMagic32
4204 && version_or_magic != HeaderMagic32Swapped
4205 && version_or_magic != HeaderMagic64
4206 && version_or_magic != HeaderMagic64Swapped
4207 && version_or_magic >= 13)
4208 {
4209 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4210 int wordsize = process->GetAddressByteSize();
4211 if (wordsize == 8)
4212 {
4213 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4214 }
4215 if (wordsize == 4)
4216 {
4217 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4218 }
4219 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4220 {
4221 uuid_t shared_cache_uuid;
4222 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4223 {
4224 uuid.SetBytes (shared_cache_uuid);
4225 }
4226 }
4227 }
4228 }
4229 return uuid;
4230}
4231
4232UUID
4233ObjectFileMachO::GetLLDBSharedCacheUUID ()
4234{
4235 UUID uuid;
4236#if defined (__APPLE__) && defined (__arm__)
4237 uint8_t *(*dyld_get_all_image_infos)(void);
4238 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4239 if (dyld_get_all_image_infos)
4240 {
4241 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4242 if (dyld_all_image_infos_address)
4243 {
Jason Molenda6ff916f2013-04-16 21:42:58 +00004244 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4245 if (*version >= 13)
Jason Molenda45c75502013-04-16 06:24:42 +00004246 {
Jason Molenda2ceae992013-04-16 22:56:17 +00004247 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 +00004248 uuid.SetBytes (sharedCacheUUID_address);
4249 }
4250 }
4251 }
4252#endif
4253 return uuid;
4254}
4255
4256
Chris Lattner24943d22010-06-08 16:52:24 +00004257//------------------------------------------------------------------
4258// PluginInterface protocol
4259//------------------------------------------------------------------
Greg Clayton0e191602013-05-10 21:47:16 +00004260lldb_private::ConstString
Chris Lattner24943d22010-06-08 16:52:24 +00004261ObjectFileMachO::GetPluginName()
4262{
Chris Lattner24943d22010-06-08 16:52:24 +00004263 return GetPluginNameStatic();
4264}
4265
4266uint32_t
4267ObjectFileMachO::GetPluginVersion()
4268{
4269 return 1;
4270}
4271