blob: 140272e091289038b4be3eae59f2c4deb5e3c19c [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 Clayton95fd2852013-05-15 19:52:08 +0000400static uint32_t
401MachHeaderSizeFromMagic(uint32_t magic)
402{
403 switch (magic)
404 {
405 case HeaderMagic32:
406 case HeaderMagic32Swapped:
407 return sizeof(struct mach_header);
408
409 case HeaderMagic64:
410 case HeaderMagic64Swapped:
411 return sizeof(struct mach_header_64);
412 break;
413
414 default:
415 break;
416 }
417 return 0;
418}
419
Greg Claytonb1888f22011-03-19 01:12:21 +0000420#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +0000421
422void
423ObjectFileMachO::Initialize()
424{
425 PluginManager::RegisterPlugin (GetPluginNameStatic(),
426 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000427 CreateInstance,
Greg Clayton36b877d2013-04-24 22:29:28 +0000428 CreateMemoryInstance,
429 GetModuleSpecifications);
Chris Lattner24943d22010-06-08 16:52:24 +0000430}
431
432void
433ObjectFileMachO::Terminate()
434{
435 PluginManager::UnregisterPlugin (CreateInstance);
436}
437
438
Greg Clayton0e191602013-05-10 21:47:16 +0000439lldb_private::ConstString
Chris Lattner24943d22010-06-08 16:52:24 +0000440ObjectFileMachO::GetPluginNameStatic()
441{
Greg Clayton0e191602013-05-10 21:47:16 +0000442 static ConstString g_name("mach-o");
443 return g_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000444}
445
446const char *
447ObjectFileMachO::GetPluginDescriptionStatic()
448{
449 return "Mach-o object file reader (32 and 64 bit)";
450}
451
Chris Lattner24943d22010-06-08 16:52:24 +0000452ObjectFile *
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000453ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
454 DataBufferSP& data_sp,
455 lldb::offset_t data_offset,
456 const FileSpec* file,
457 lldb::offset_t file_offset,
458 lldb::offset_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000459{
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000460 if (!data_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000461 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000462 data_sp = file->MemoryMapFileContents(file_offset, length);
463 data_offset = 0;
464 }
465
466 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
467 {
468 // Update the data to contain the entire file if it doesn't already
469 if (data_sp->GetByteSize() < length)
470 {
471 data_sp = file->MemoryMapFileContents(file_offset, length);
472 data_offset = 0;
473 }
Greg Clayton102b2c22013-04-18 22:45:39 +0000474 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 +0000475 if (objfile_ap.get() && objfile_ap->ParseHeader())
476 return objfile_ap.release();
477 }
478 return NULL;
479}
480
Greg Claytonb5a8f142012-02-05 02:38:54 +0000481ObjectFile *
Jason Molenda9badb6c2013-03-06 23:19:17 +0000482ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
483 DataBufferSP& data_sp,
484 const ProcessSP &process_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000485 lldb::addr_t header_addr)
486{
487 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
488 {
Greg Clayton102b2c22013-04-18 22:45:39 +0000489 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonb5a8f142012-02-05 02:38:54 +0000490 if (objfile_ap.get() && objfile_ap->ParseHeader())
491 return objfile_ap.release();
492 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000493 return NULL;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000494}
495
Greg Clayton36b877d2013-04-24 22:29:28 +0000496size_t
497ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
498 lldb::DataBufferSP& data_sp,
499 lldb::offset_t data_offset,
500 lldb::offset_t file_offset,
501 lldb::offset_t length,
502 lldb_private::ModuleSpecList &specs)
503{
504 const size_t initial_count = specs.GetSize();
505
506 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
507 {
508 DataExtractor data;
509 data.SetData(data_sp);
510 llvm::MachO::mach_header header;
511 if (ParseHeader (data, &data_offset, header))
512 {
513 if (header.sizeofcmds >= data_sp->GetByteSize())
514 {
515 data_sp = file.ReadFileContents(file_offset, header.sizeofcmds);
Greg Clayton95fd2852013-05-15 19:52:08 +0000516 data_offset = MachHeaderSizeFromMagic(header.magic) + file_offset;
Greg Clayton36b877d2013-04-24 22:29:28 +0000517 }
518 if (data_sp)
519 {
520 ModuleSpec spec;
521 spec.GetFileSpec() = file;
522 spec.GetArchitecture().SetArchitecture(eArchTypeMachO,
523 header.cputype,
524 header.cpusubtype);
525 if (spec.GetArchitecture().IsValid())
526 {
527 GetUUID (header, data, data_offset, spec.GetUUID());
528 specs.Append(spec);
529 }
530 }
531 }
532 }
533 return specs.GetSize() - initial_count;
534}
535
536
Greg Claytonb5a8f142012-02-05 02:38:54 +0000537
538const ConstString &
539ObjectFileMachO::GetSegmentNameTEXT()
540{
541 static ConstString g_segment_name_TEXT ("__TEXT");
542 return g_segment_name_TEXT;
543}
544
545const ConstString &
546ObjectFileMachO::GetSegmentNameDATA()
547{
548 static ConstString g_segment_name_DATA ("__DATA");
549 return g_segment_name_DATA;
550}
551
552const ConstString &
553ObjectFileMachO::GetSegmentNameOBJC()
554{
555 static ConstString g_segment_name_OBJC ("__OBJC");
556 return g_segment_name_OBJC;
557}
558
559const ConstString &
560ObjectFileMachO::GetSegmentNameLINKEDIT()
561{
562 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
563 return g_section_name_LINKEDIT;
564}
565
566const ConstString &
567ObjectFileMachO::GetSectionNameEHFrame()
568{
569 static ConstString g_section_name_eh_frame ("__eh_frame");
570 return g_section_name_eh_frame;
571}
572
Chris Lattner24943d22010-06-08 16:52:24 +0000573bool
Jason Molenda9badb6c2013-03-06 23:19:17 +0000574ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
575 lldb::addr_t data_offset,
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000576 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000577{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000578 DataExtractor data;
579 data.SetData (data_sp, data_offset, data_length);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000580 lldb::offset_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000581 uint32_t magic = data.GetU32(&offset);
582 return MachHeaderSizeFromMagic(magic) != 0;
583}
584
585
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000586ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
587 DataBufferSP& data_sp,
588 lldb::offset_t data_offset,
589 const FileSpec* file,
590 lldb::offset_t file_offset,
591 lldb::offset_t length) :
592 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Clayton46c9a352012-02-09 06:16:32 +0000593 m_mach_segments(),
594 m_mach_sections(),
595 m_entry_point_address(),
596 m_thread_context_offsets(),
597 m_thread_context_offsets_valid(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000598{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000599 ::memset (&m_header, 0, sizeof(m_header));
600 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000601}
602
Greg Clayton3508c382012-02-24 01:59:29 +0000603ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000604 lldb::DataBufferSP& header_data_sp,
605 const lldb::ProcessSP &process_sp,
606 lldb::addr_t header_addr) :
Greg Clayton3508c382012-02-24 01:59:29 +0000607 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Clayton46c9a352012-02-09 06:16:32 +0000608 m_mach_segments(),
609 m_mach_sections(),
610 m_entry_point_address(),
611 m_thread_context_offsets(),
612 m_thread_context_offsets_valid(false)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000613{
614 ::memset (&m_header, 0, sizeof(m_header));
615 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
616}
Chris Lattner24943d22010-06-08 16:52:24 +0000617
618ObjectFileMachO::~ObjectFileMachO()
619{
620}
621
Greg Clayton36b877d2013-04-24 22:29:28 +0000622bool
623ObjectFileMachO::ParseHeader (DataExtractor &data,
624 lldb::offset_t *data_offset_ptr,
625 llvm::MachO::mach_header &header)
626{
627 data.SetByteOrder (lldb::endian::InlHostByteOrder());
628 // Leave magic in the original byte order
629 header.magic = data.GetU32(data_offset_ptr);
630 bool can_parse = false;
631 bool is_64_bit = false;
632 switch (header.magic)
633 {
634 case HeaderMagic32:
635 data.SetByteOrder (lldb::endian::InlHostByteOrder());
636 data.SetAddressByteSize(4);
637 can_parse = true;
638 break;
639
640 case HeaderMagic64:
641 data.SetByteOrder (lldb::endian::InlHostByteOrder());
642 data.SetAddressByteSize(8);
643 can_parse = true;
644 is_64_bit = true;
645 break;
646
647 case HeaderMagic32Swapped:
648 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
649 data.SetAddressByteSize(4);
650 can_parse = true;
651 break;
652
653 case HeaderMagic64Swapped:
654 data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
655 data.SetAddressByteSize(8);
656 is_64_bit = true;
657 can_parse = true;
658 break;
659
660 default:
661 break;
662 }
663
664 if (can_parse)
665 {
666 data.GetU32(data_offset_ptr, &header.cputype, 6);
667 if (is_64_bit)
668 *data_offset_ptr += 4;
669 return true;
670 }
671 else
672 {
673 memset(&header, 0, sizeof(header));
674 }
675 return false;
676}
Chris Lattner24943d22010-06-08 16:52:24 +0000677
678bool
679ObjectFileMachO::ParseHeader ()
680{
Greg Clayton9482f052012-03-13 23:14:29 +0000681 ModuleSP module_sp(GetModule());
682 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000683 {
Greg Clayton9482f052012-03-13 23:14:29 +0000684 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
685 bool can_parse = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000686 lldb::offset_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000687 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Greg Clayton9482f052012-03-13 23:14:29 +0000688 // Leave magic in the original byte order
689 m_header.magic = m_data.GetU32(&offset);
690 switch (m_header.magic)
Greg Claytonb5a8f142012-02-05 02:38:54 +0000691 {
Greg Clayton9482f052012-03-13 23:14:29 +0000692 case HeaderMagic32:
693 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
694 m_data.SetAddressByteSize(4);
695 can_parse = true;
696 break;
697
698 case HeaderMagic64:
699 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
700 m_data.SetAddressByteSize(8);
701 can_parse = true;
702 break;
703
704 case HeaderMagic32Swapped:
705 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
706 m_data.SetAddressByteSize(4);
707 can_parse = true;
708 break;
709
710 case HeaderMagic64Swapped:
711 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
712 m_data.SetAddressByteSize(8);
713 can_parse = true;
714 break;
715
716 default:
717 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000718 }
Greg Clayton9482f052012-03-13 23:14:29 +0000719
720 if (can_parse)
721 {
722 m_data.GetU32(&offset, &m_header.cputype, 6);
723
724 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +0000725
Greg Clayton21a25432012-11-16 21:36:10 +0000726 // Check if the module has a required architecture
727 const ArchSpec &module_arch = module_sp->GetArchitecture();
Sean Callanan40e278c2012-12-13 22:07:14 +0000728 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
Greg Clayton21a25432012-11-16 21:36:10 +0000729 return false;
730
Greg Clayton9482f052012-03-13 23:14:29 +0000731 if (SetModulesArchitecture (mach_arch))
732 {
733 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
734 if (m_data.GetByteSize() < header_and_lc_size)
735 {
736 DataBufferSP data_sp;
737 ProcessSP process_sp (m_process_wp.lock());
738 if (process_sp)
739 {
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000740 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000741 }
742 else
743 {
744 // Read in all only the load command data from the file on disk
Greg Claytoncbe61bd2013-02-06 17:22:03 +0000745 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
Greg Clayton9482f052012-03-13 23:14:29 +0000746 if (data_sp->GetByteSize() != header_and_lc_size)
747 return false;
748 }
749 if (data_sp)
750 m_data.SetData (data_sp);
751 }
752 }
753 return true;
754 }
755 else
756 {
757 memset(&m_header, 0, sizeof(struct mach_header));
758 }
Chris Lattner24943d22010-06-08 16:52:24 +0000759 }
760 return false;
761}
762
763
764ByteOrder
765ObjectFileMachO::GetByteOrder () const
766{
Chris Lattner24943d22010-06-08 16:52:24 +0000767 return m_data.GetByteOrder ();
768}
769
Jim Ingham7508e732010-08-09 23:31:02 +0000770bool
771ObjectFileMachO::IsExecutable() const
772{
773 return m_header.filetype == HeaderFileTypeExecutable;
774}
Chris Lattner24943d22010-06-08 16:52:24 +0000775
Greg Clayton36da2aa2013-01-25 18:06:21 +0000776uint32_t
Chris Lattner24943d22010-06-08 16:52:24 +0000777ObjectFileMachO::GetAddressByteSize () const
778{
Chris Lattner24943d22010-06-08 16:52:24 +0000779 return m_data.GetAddressByteSize ();
780}
781
Greg Claytonb3448432011-03-24 21:19:54 +0000782AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000783ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
784{
785 Symtab *symtab = GetSymtab();
786 if (symtab)
787 {
788 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
789 if (symbol)
790 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000791 if (symbol->ValueIsAddress())
Greg Claytonb1888f22011-03-19 01:12:21 +0000792 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000793 SectionSP section_sp (symbol->GetAddress().GetSection());
Greg Clayton3508c382012-02-24 01:59:29 +0000794 if (section_sp)
Greg Claytonb1888f22011-03-19 01:12:21 +0000795 {
Greg Clayton3508c382012-02-24 01:59:29 +0000796 const SectionType section_type = section_sp->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000797 switch (section_type)
798 {
799 case eSectionTypeInvalid: return eAddressClassUnknown;
800 case eSectionTypeCode:
801 if (m_header.cputype == llvm::MachO::CPUTypeARM)
802 {
803 // For ARM we have a bit in the n_desc field of the symbol
804 // that tells us ARM/Thumb which is bit 0x0008.
805 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
806 return eAddressClassCodeAlternateISA;
807 }
808 return eAddressClassCode;
809
810 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000811 case eSectionTypeData:
812 case eSectionTypeDataCString:
813 case eSectionTypeDataCStringPointers:
814 case eSectionTypeDataSymbolAddress:
815 case eSectionTypeData4:
816 case eSectionTypeData8:
817 case eSectionTypeData16:
818 case eSectionTypeDataPointers:
819 case eSectionTypeZeroFill:
820 case eSectionTypeDataObjCMessageRefs:
821 case eSectionTypeDataObjCCFStrings:
822 return eAddressClassData;
823 case eSectionTypeDebug:
824 case eSectionTypeDWARFDebugAbbrev:
825 case eSectionTypeDWARFDebugAranges:
826 case eSectionTypeDWARFDebugFrame:
827 case eSectionTypeDWARFDebugInfo:
828 case eSectionTypeDWARFDebugLine:
829 case eSectionTypeDWARFDebugLoc:
830 case eSectionTypeDWARFDebugMacInfo:
831 case eSectionTypeDWARFDebugPubNames:
832 case eSectionTypeDWARFDebugPubTypes:
833 case eSectionTypeDWARFDebugRanges:
834 case eSectionTypeDWARFDebugStr:
835 case eSectionTypeDWARFAppleNames:
836 case eSectionTypeDWARFAppleTypes:
837 case eSectionTypeDWARFAppleNamespaces:
838 case eSectionTypeDWARFAppleObjC:
839 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000840 case eSectionTypeEHFrame: return eAddressClassRuntime;
841 case eSectionTypeOther: return eAddressClassUnknown;
842 }
843 }
844 }
Jason Molenda9badb6c2013-03-06 23:19:17 +0000845
Greg Claytonb3448432011-03-24 21:19:54 +0000846 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000847 switch (symbol_type)
848 {
849 case eSymbolTypeAny: return eAddressClassUnknown;
850 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000851
Greg Claytonb1888f22011-03-19 01:12:21 +0000852 case eSymbolTypeCode:
853 case eSymbolTypeTrampoline:
Greg Clayton06884352013-02-27 21:16:04 +0000854 case eSymbolTypeResolver:
Greg Claytonb1888f22011-03-19 01:12:21 +0000855 if (m_header.cputype == llvm::MachO::CPUTypeARM)
856 {
857 // For ARM we have a bit in the n_desc field of the symbol
858 // that tells us ARM/Thumb which is bit 0x0008.
859 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
860 return eAddressClassCodeAlternateISA;
861 }
862 return eAddressClassCode;
863
864 case eSymbolTypeData: return eAddressClassData;
865 case eSymbolTypeRuntime: return eAddressClassRuntime;
866 case eSymbolTypeException: return eAddressClassRuntime;
867 case eSymbolTypeSourceFile: return eAddressClassDebug;
868 case eSymbolTypeHeaderFile: return eAddressClassDebug;
869 case eSymbolTypeObjectFile: return eAddressClassDebug;
870 case eSymbolTypeCommonBlock: return eAddressClassDebug;
871 case eSymbolTypeBlock: return eAddressClassDebug;
872 case eSymbolTypeLocal: return eAddressClassData;
873 case eSymbolTypeParam: return eAddressClassData;
874 case eSymbolTypeVariable: return eAddressClassData;
875 case eSymbolTypeVariableType: return eAddressClassDebug;
876 case eSymbolTypeLineEntry: return eAddressClassDebug;
877 case eSymbolTypeLineHeader: return eAddressClassDebug;
878 case eSymbolTypeScopeBegin: return eAddressClassDebug;
879 case eSymbolTypeScopeEnd: return eAddressClassDebug;
880 case eSymbolTypeAdditional: return eAddressClassUnknown;
881 case eSymbolTypeCompiler: return eAddressClassDebug;
882 case eSymbolTypeInstrumentation:return eAddressClassDebug;
883 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000884 case eSymbolTypeObjCClass: return eAddressClassRuntime;
885 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
886 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000887 }
888 }
889 }
890 return eAddressClassUnknown;
891}
Chris Lattner24943d22010-06-08 16:52:24 +0000892
893Symtab *
894ObjectFileMachO::GetSymtab()
895{
Greg Clayton9482f052012-03-13 23:14:29 +0000896 ModuleSP module_sp(GetModule());
897 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000898 {
Greg Clayton9482f052012-03-13 23:14:29 +0000899 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
900 if (m_symtab_ap.get() == NULL)
901 {
902 m_symtab_ap.reset(new Symtab(this));
903 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
904 ParseSymtab (true);
905 m_symtab_ap->Finalize ();
906 }
Chris Lattner24943d22010-06-08 16:52:24 +0000907 }
908 return m_symtab_ap.get();
909}
910
911
912SectionList *
913ObjectFileMachO::GetSectionList()
914{
Greg Clayton9482f052012-03-13 23:14:29 +0000915 ModuleSP module_sp(GetModule());
916 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000917 {
Greg Clayton9482f052012-03-13 23:14:29 +0000918 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
919 if (m_sections_ap.get() == NULL)
920 {
921 m_sections_ap.reset(new SectionList());
922 ParseSections();
923 }
Chris Lattner24943d22010-06-08 16:52:24 +0000924 }
925 return m_sections_ap.get();
926}
927
928
929size_t
930ObjectFileMachO::ParseSections ()
931{
932 lldb::user_id_t segID = 0;
933 lldb::user_id_t sectID = 0;
Greg Clayton36da2aa2013-01-25 18:06:21 +0000934 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +0000935 uint32_t i;
Greg Clayton46c9a352012-02-09 06:16:32 +0000936 const bool is_core = GetType() == eTypeCoreFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000937 //bool dump_sections = false;
Greg Clayton3508c382012-02-24 01:59:29 +0000938 ModuleSP module_sp (GetModule());
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000939 // First look up any LC_ENCRYPTION_INFO load commands
940 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
941 EncryptedFileRanges encrypted_file_ranges;
Greg Clayton54e33712012-05-25 18:09:55 +0000942 encryption_info_command encryption_cmd;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000943 for (i=0; i<m_header.ncmds; ++i)
944 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000945 const lldb::offset_t load_cmd_offset = offset;
Greg Clayton54e33712012-05-25 18:09:55 +0000946 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000947 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +0000948
Greg Clayton54e33712012-05-25 18:09:55 +0000949 if (encryption_cmd.cmd == LoadCommandEncryptionInfo)
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000950 {
Greg Clayton54e33712012-05-25 18:09:55 +0000951 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
952 {
953 if (encryption_cmd.cryptid != 0)
954 {
955 EncryptedFileRanges::Entry entry;
956 entry.SetRangeBase(encryption_cmd.cryptoff);
957 entry.SetByteSize(encryption_cmd.cryptsize);
958 encrypted_file_ranges.Append(entry);
959 }
960 }
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000961 }
Greg Clayton54e33712012-05-25 18:09:55 +0000962 offset = load_cmd_offset + encryption_cmd.cmdsize;
Greg Clayton6f7f8da2012-04-24 03:06:13 +0000963 }
964
965 offset = MachHeaderSizeFromMagic(m_header.magic);
966
Greg Clayton54e33712012-05-25 18:09:55 +0000967 struct segment_command_64 load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000968 for (i=0; i<m_header.ncmds; ++i)
969 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000970 const lldb::offset_t load_cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000971 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
972 break;
973
Greg Clayton1674b122010-07-21 22:12:05 +0000974 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000975 {
976 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
977 {
978 load_cmd.vmaddr = m_data.GetAddress(&offset);
979 load_cmd.vmsize = m_data.GetAddress(&offset);
980 load_cmd.fileoff = m_data.GetAddress(&offset);
981 load_cmd.filesize = m_data.GetAddress(&offset);
Jason Molendaadf9e3d2013-04-10 05:58:57 +0000982 if (m_length != 0 && load_cmd.filesize != 0)
983 {
Greg Claytonbb759862013-04-16 16:51:19 +0000984 if (load_cmd.fileoff > m_length)
985 {
986 // We have a load command that says it extends past the end of hte file. This is likely
987 // a corrupt file. We don't have any way to return an error condition here (this method
988 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
989 // is null out the SectionList vector and if a process has been set up, dump a message
990 // to stdout. The most common case here is core file debugging with a truncated file.
991 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
992 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 ")",
993 i,
994 lc_segment_name,
995 load_cmd.fileoff,
996 m_length);
997 m_sections_ap->Clear();
998 return 0;
999 }
1000
Jason Molendaadf9e3d2013-04-10 05:58:57 +00001001 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1002 {
1003 // We have a load command that says it extends past the end of hte file. This is likely
1004 // a corrupt file. We don't have any way to return an error condition here (this method
1005 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1006 // is null out the SectionList vector and if a process has been set up, dump a message
Greg Claytonbb759862013-04-16 16:51:19 +00001007 // to stdout. The most common case here is core file debugging with a truncated file.
1008 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
1009 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 ")",
1010 i,
1011 lc_segment_name,
1012 load_cmd.fileoff + load_cmd.filesize,
1013 m_length);
Jason Molendaadf9e3d2013-04-10 05:58:57 +00001014 m_sections_ap->Clear();
1015 return 0;
1016 }
1017 }
Chris Lattner24943d22010-06-08 16:52:24 +00001018 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1019 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001020
Greg Clayton68ca8232011-01-25 02:58:48 +00001021 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
1022
Chris Lattner24943d22010-06-08 16:52:24 +00001023 // Keep a list of mach segments around in case we need to
1024 // get at data that isn't stored in the abstracted Sections.
1025 m_mach_segments.push_back (load_cmd);
1026
Greg Clayton36da2aa2013-01-25 18:06:21 +00001027 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 +00001028 // Use a segment ID of the segment index shifted left by 8 so they
1029 // never conflict with any of the sections.
1030 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +00001031 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +00001032 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001033 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +00001034 ++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
1035 segment_name, // Name of this section
1036 eSectionTypeContainer, // This section is a container of other sections.
1037 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1038 load_cmd.vmsize, // VM size in bytes of this section
1039 load_cmd.fileoff, // Offset to the data for this section in the file
1040 load_cmd.filesize, // Size in bytes of this section as found in the the file
1041 load_cmd.flags)); // Flags for this section
1042
Greg Clayton68ca8232011-01-25 02:58:48 +00001043 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001044 m_sections_ap->AddSection(segment_sp);
1045 }
1046
1047 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +00001048 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +00001049 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +00001050 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +00001051 // mach sections yet...
1052 if (m_mach_sections.empty())
1053 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +00001054 uint32_t segment_sect_idx;
1055 const lldb::user_id_t first_segment_sectID = sectID + 1;
1056
1057
Greg Clayton1674b122010-07-21 22:12:05 +00001058 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +00001059 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
1060 {
1061 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
1062 break;
1063 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1064 break;
1065 sect64.addr = m_data.GetAddress(&offset);
1066 sect64.size = m_data.GetAddress(&offset);
1067
1068 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1069 break;
1070
1071 // Keep a list of mach sections around in case we need to
1072 // get at data that isn't stored in the abstracted Sections.
1073 m_mach_sections.push_back (sect64);
1074
1075 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1076 if (!segment_name)
1077 {
1078 // We have a segment with no name so we need to conjure up
1079 // segments that correspond to the section's segname if there
1080 // isn't already such a section. If there is such a section,
1081 // we resize the section so that it spans all sections.
1082 // We also mark these sections as fake so address matches don't
1083 // hit if they land in the gaps between the child sections.
1084 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1085 segment_sp = m_sections_ap->FindSectionByName (segment_name);
1086 if (segment_sp.get())
1087 {
1088 Section *segment = segment_sp.get();
1089 // Grow the section size as needed.
1090 const lldb::addr_t sect64_min_addr = sect64.addr;
1091 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1092 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1093 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1094 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1095 if (sect64_min_addr >= curr_seg_min_addr)
1096 {
1097 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1098 // Only grow the section size if needed
1099 if (new_seg_byte_size > curr_seg_byte_size)
1100 segment->SetByteSize (new_seg_byte_size);
1101 }
1102 else
1103 {
1104 // We need to change the base address of the segment and
1105 // adjust the child section offsets for all existing children.
1106 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1107 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +00001108 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +00001109 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1110 }
Greg Clayton661825b2010-06-28 23:51:11 +00001111
1112 // Grow the section size as needed.
1113 if (sect64.offset)
1114 {
1115 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1116 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1117
1118 const lldb::addr_t section_min_file_offset = sect64.offset;
1119 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1120 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1121 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1122 segment->SetFileOffset (new_file_offset);
1123 segment->SetFileSize (new_file_size);
1124 }
Chris Lattner24943d22010-06-08 16:52:24 +00001125 }
1126 else
1127 {
1128 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +00001129 segment_sp.reset(new Section (segment_sp, // Parent section
1130 module_sp, // Module to which this section belongs
1131 ++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
1132 segment_name, // Name of this section
1133 eSectionTypeContainer, // This section is a container of other sections.
1134 sect64.addr, // File VM address == addresses as they are found in the object file
1135 sect64.size, // VM size in bytes of this section
1136 sect64.offset, // Offset to the data for this section in the file
1137 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1138 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +00001139 segment_sp->SetIsFake(true);
1140 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +00001141 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001142 }
1143 }
1144 assert (segment_sp.get());
1145
Greg Clayton1674b122010-07-21 22:12:05 +00001146 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +00001147 static ConstString g_sect_name_objc_data ("__objc_data");
1148 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1149 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1150 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1151 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1152 static ConstString g_sect_name_objc_const ("__objc_const");
1153 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1154 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001155
1156 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1157 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1158 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1159 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1160 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1161 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1162 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1163 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1164 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1165 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1166 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +00001167 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1168 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +00001169 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +00001170 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001171 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +00001172 static ConstString g_sect_name_DATA ("__DATA");
1173 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001174
Chris Lattner24943d22010-06-08 16:52:24 +00001175 SectionType sect_type = eSectionTypeOther;
1176
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001177 if (section_name == g_sect_name_dwarf_debug_abbrev)
1178 sect_type = eSectionTypeDWARFDebugAbbrev;
1179 else if (section_name == g_sect_name_dwarf_debug_aranges)
1180 sect_type = eSectionTypeDWARFDebugAranges;
1181 else if (section_name == g_sect_name_dwarf_debug_frame)
1182 sect_type = eSectionTypeDWARFDebugFrame;
1183 else if (section_name == g_sect_name_dwarf_debug_info)
1184 sect_type = eSectionTypeDWARFDebugInfo;
1185 else if (section_name == g_sect_name_dwarf_debug_line)
1186 sect_type = eSectionTypeDWARFDebugLine;
1187 else if (section_name == g_sect_name_dwarf_debug_loc)
1188 sect_type = eSectionTypeDWARFDebugLoc;
1189 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1190 sect_type = eSectionTypeDWARFDebugMacInfo;
1191 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1192 sect_type = eSectionTypeDWARFDebugPubNames;
1193 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1194 sect_type = eSectionTypeDWARFDebugPubTypes;
1195 else if (section_name == g_sect_name_dwarf_debug_ranges)
1196 sect_type = eSectionTypeDWARFDebugRanges;
1197 else if (section_name == g_sect_name_dwarf_debug_str)
1198 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001199 else if (section_name == g_sect_name_dwarf_apple_names)
1200 sect_type = eSectionTypeDWARFAppleNames;
1201 else if (section_name == g_sect_name_dwarf_apple_types)
1202 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001203 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1204 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001205 else if (section_name == g_sect_name_dwarf_apple_objc)
1206 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001207 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001208 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001209 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001210 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001211 else if (section_name == g_sect_name_eh_frame)
1212 sect_type = eSectionTypeEHFrame;
1213 else if (section_name == g_sect_name_cfstring)
1214 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001215 else if (section_name == g_sect_name_objc_data ||
1216 section_name == g_sect_name_objc_classrefs ||
1217 section_name == g_sect_name_objc_superrefs ||
1218 section_name == g_sect_name_objc_const ||
1219 section_name == g_sect_name_objc_classlist)
1220 {
1221 sect_type = eSectionTypeDataPointers;
1222 }
Chris Lattner24943d22010-06-08 16:52:24 +00001223
1224 if (sect_type == eSectionTypeOther)
1225 {
1226 switch (mach_sect_type)
1227 {
1228 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001229 case SectionTypeRegular:
1230 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001231 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001232 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001233 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001234 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001235 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001236 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001237 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1238 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1239 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1240 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1241 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1242 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1243 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1244 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1245 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1246 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1247 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1248 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1249 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1250 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1251 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1252 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001253 default: break;
1254 }
1255 }
1256
Greg Clayton3508c382012-02-24 01:59:29 +00001257 SectionSP section_sp(new Section (segment_sp,
1258 module_sp,
1259 ++sectID,
1260 section_name,
1261 sect_type,
1262 sect64.addr - segment_sp->GetFileAddress(),
1263 sect64.size,
1264 sect64.offset,
1265 sect64.offset == 0 ? 0 : sect64.size,
1266 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001267 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001268
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001269 bool section_is_encrypted = false;
1270 if (!segment_is_encrypted && load_cmd.filesize != 0)
1271 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001272
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001273 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001274 segment_sp->GetChildren().AddSection(section_sp);
1275
1276 if (segment_sp->IsFake())
1277 {
1278 segment_sp.reset();
1279 segment_name.Clear();
1280 }
1281 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001282 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001283 {
1284 if (first_segment_sectID <= sectID)
1285 {
1286 lldb::user_id_t sect_uid;
1287 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1288 {
1289 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1290 SectionSP next_section_sp;
1291 if (sect_uid + 1 <= sectID)
1292 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1293
1294 if (curr_section_sp.get())
1295 {
1296 if (curr_section_sp->GetByteSize() == 0)
1297 {
1298 if (next_section_sp.get() != NULL)
1299 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1300 else
1301 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1302 }
1303 }
1304 }
1305 }
1306 }
1307 }
1308 }
1309 }
Greg Clayton1674b122010-07-21 22:12:05 +00001310 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001311 {
1312 m_dysymtab.cmd = load_cmd.cmd;
1313 m_dysymtab.cmdsize = load_cmd.cmdsize;
1314 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1315 }
1316
1317 offset = load_cmd_offset + load_cmd.cmdsize;
1318 }
1319// if (dump_sections)
1320// {
1321// StreamFile s(stdout);
1322// m_sections_ap->Dump(&s, true);
1323// }
1324 return sectID; // Return the number of sections we registered with the module
1325}
1326
1327class MachSymtabSectionInfo
1328{
1329public:
1330
1331 MachSymtabSectionInfo (SectionList *section_list) :
1332 m_section_list (section_list),
1333 m_section_infos()
1334 {
1335 // Get the number of sections down to a depth of 1 to include
1336 // all segments and their sections, but no other sections that
1337 // may be added for debug map or
1338 m_section_infos.resize(section_list->GetNumSections(1));
1339 }
1340
1341
Greg Clayton3508c382012-02-24 01:59:29 +00001342 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001343 GetSection (uint8_t n_sect, addr_t file_addr)
1344 {
1345 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001346 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001347 if (n_sect < m_section_infos.size())
1348 {
Greg Clayton3508c382012-02-24 01:59:29 +00001349 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001350 {
Greg Clayton3508c382012-02-24 01:59:29 +00001351 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1352 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001353 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001354 {
Greg Clayton3508c382012-02-24 01:59:29 +00001355 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1356 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001357 }
1358 else
1359 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001360 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001361 }
Chris Lattner24943d22010-06-08 16:52:24 +00001362 }
1363 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001364 {
1365 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001366 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001367 }
1368 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1369 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1370 {
1371 // Symbol is in section with zero size, but has the same start
1372 // address as the section. This can happen with linker symbols
1373 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001374 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001375 }
Chris Lattner24943d22010-06-08 16:52:24 +00001376 }
Greg Clayton3508c382012-02-24 01:59:29 +00001377 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001378 }
1379
1380protected:
1381 struct SectionInfo
1382 {
1383 SectionInfo () :
1384 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001385 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001386 {
1387 }
1388
1389 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001390 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001391 };
1392 SectionList *m_section_list;
1393 std::vector<SectionInfo> m_section_infos;
1394};
1395
Chris Lattner24943d22010-06-08 16:52:24 +00001396size_t
1397ObjectFileMachO::ParseSymtab (bool minimize)
1398{
1399 Timer scoped_timer(__PRETTY_FUNCTION__,
1400 "ObjectFileMachO::ParseSymtab () module = %s",
1401 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001402 ModuleSP module_sp (GetModule());
1403 if (!module_sp)
1404 return 0;
1405
1406 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1407 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1408 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1409 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001410 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001411 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001412
Greg Clayton952e9dc2013-03-27 23:08:40 +00001413 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001414
Chris Lattner24943d22010-06-08 16:52:24 +00001415 for (i=0; i<m_header.ncmds; ++i)
1416 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001417 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001418 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001419 struct load_command lc;
1420 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001421 break;
1422 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001423 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001424 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001425 case LoadCommandSymtab:
1426 symtab_load_command.cmd = lc.cmd;
1427 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001428 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001429 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1430 return 0;
1431 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001432 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001433 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001434 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001435 return 0;
1436 }
1437
1438 if (symtab_load_command.stroff == 0)
1439 {
1440 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001441 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001442 return 0;
1443 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001444
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001445 if (symtab_load_command.nsyms == 0)
1446 {
1447 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001448 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001449 return 0;
1450 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001451
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001452 if (symtab_load_command.strsize == 0)
1453 {
1454 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001455 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001456 return 0;
1457 }
1458 break;
1459
1460 case LoadCommandFunctionStarts:
1461 function_starts_load_command.cmd = lc.cmd;
1462 function_starts_load_command.cmdsize = lc.cmdsize;
1463 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1464 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1465 break;
1466
1467 default:
1468 break;
1469 }
1470 offset = cmd_offset + lc.cmdsize;
1471 }
1472
1473 if (symtab_load_command.cmd)
1474 {
1475 Symtab *symtab = m_symtab_ap.get();
1476 SectionList *section_list = GetSectionList();
1477 if (section_list == NULL)
1478 return 0;
1479
1480 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001481 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001482
Greg Clayton36da2aa2013-01-25 18:06:21 +00001483 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1484 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001485 bool bit_width_32 = addr_byte_size == 4;
1486 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1487
Greg Clayton36da2aa2013-01-25 18:06:21 +00001488 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1489 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1490 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001491 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001492
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001493 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1494 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001495 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1496 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001497 {
Greg Claytondd29b972012-05-18 23:20:01 +00001498 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001499 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1500 // Reading mach file from memory in a process or core file...
1501
1502 if (linkedit_section_sp)
1503 {
1504 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1505 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1506 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001507 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001508
1509 bool data_was_read = false;
1510
1511#if defined (__APPLE__) && defined (__arm__)
1512 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001513 {
Greg Clayton29021d32012-04-18 05:19:20 +00001514 // This mach-o memory file is in the dyld shared cache. If this
1515 // program is not remote and this is iOS, then this process will
1516 // share the same shared cache as the process we are debugging and
1517 // we can read the entire __LINKEDIT from the address space in this
1518 // process. This is a needed optimization that is used for local iOS
1519 // debugging only since all shared libraries in the shared cache do
1520 // not have corresponding files that exist in the file system of the
1521 // device. They have been combined into a single file. This means we
1522 // always have to load these files from memory. All of the symbol and
1523 // string tables from all of the __LINKEDIT sections from the shared
1524 // libraries in the shared cache have been merged into a single large
1525 // symbol and string table. Reading all of this symbol and string table
1526 // data across can slow down debug launch times, so we optimize this by
1527 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda45c75502013-04-16 06:24:42 +00001528
1529 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1530 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1531 bool use_lldb_cache = true;
1532 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1533 {
1534 use_lldb_cache = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001535 ModuleSP module_sp (GetModule());
1536 if (module_sp)
1537 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1538
Jason Molenda45c75502013-04-16 06:24:42 +00001539 }
1540
Greg Clayton29021d32012-04-18 05:19:20 +00001541 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda45c75502013-04-16 06:24:42 +00001542 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton29021d32012-04-18 05:19:20 +00001543 {
1544 data_was_read = true;
1545 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001546 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001547 if (function_starts_load_command.cmd)
1548 {
1549 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1550 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1551 }
1552 }
1553 }
1554#endif
1555
1556 if (!data_was_read)
1557 {
1558 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1559 if (nlist_data_sp)
1560 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001561 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1562 //if (strtab_data_sp)
1563 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001564 if (m_dysymtab.nindirectsyms != 0)
1565 {
1566 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1567 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1568 if (indirect_syms_data_sp)
1569 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1570 }
Greg Clayton29021d32012-04-18 05:19:20 +00001571 if (function_starts_load_command.cmd)
1572 {
1573 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1574 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1575 if (func_start_data_sp)
1576 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1577 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001578 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001579 }
1580 }
1581 else
1582 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001583 nlist_data.SetData (m_data,
1584 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001585 nlist_data_byte_size);
1586 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001587 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001588 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001589 if (m_dysymtab.nindirectsyms != 0)
1590 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001591 indirect_symbol_index_data.SetData (m_data,
1592 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001593 m_dysymtab.nindirectsyms * 4);
1594 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001595 if (function_starts_load_command.cmd)
1596 {
1597 function_starts_data.SetData (m_data,
1598 function_starts_load_command.dataoff,
1599 function_starts_load_command.datasize);
1600 }
1601 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001602
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001603 if (nlist_data.GetByteSize() == 0)
1604 {
1605 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001606 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001607 return 0;
1608 }
1609
1610
Greg Clayton3a5dc012012-05-25 17:04:00 +00001611 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1612 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001613 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001614 if (process)
1615 {
1616 if (strtab_addr == LLDB_INVALID_ADDRESS)
1617 {
1618 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001619 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001620 return 0;
1621 }
1622 }
1623 else
Greg Claytondd29b972012-05-18 23:20:01 +00001624 {
1625 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001626 module_sp->LogMessage(log, "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001627 return 0;
1628 }
1629 }
Greg Claytondd29b972012-05-18 23:20:01 +00001630
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001631 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1632 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1633 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1634 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1635 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1636 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1637 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1638 SectionSP eh_frame_section_sp;
1639 if (text_section_sp.get())
1640 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1641 else
1642 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1643
Greg Claytond2653c22012-03-14 01:53:24 +00001644 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001645
1646 // lldb works best if it knows the start addresss of all functions in a module.
1647 // Linker symbols or debug info are normally the best source of information for start addr / size but
1648 // they may be stripped in a released binary.
Jason Molenda2120aef2013-04-16 00:18:44 +00001649 // Two additional sources of information exist in Mach-O binaries:
Jason Molendad7938392013-03-21 03:36:01 +00001650 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1651 // binary, relative to the text section.
1652 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1653 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1654 // Binaries built to run on older releases may need to use eh_frame information.
1655
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001656 if (text_section_sp && function_starts_data.GetByteSize())
1657 {
1658 FunctionStarts::Entry function_start_entry;
1659 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001660 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001661 function_start_entry.addr = text_section_sp->GetFileAddress();
1662 uint64_t delta;
1663 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1664 {
1665 // Now append the current entry
1666 function_start_entry.addr += delta;
1667 function_starts.Append(function_start_entry);
1668 }
Jason Molenda2120aef2013-04-16 00:18:44 +00001669 }
Jason Molendad7938392013-03-21 03:36:01 +00001670 else
1671 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001672 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1673 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1674 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1675 // the module.
1676 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001677 {
1678 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1679 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1680 eh_frame.GetFunctionAddressAndSizeVector (functions);
1681 addr_t text_base_addr = text_section_sp->GetFileAddress();
1682 size_t count = functions.GetSize();
1683 for (size_t i = 0; i < count; ++i)
1684 {
1685 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1686 if (func)
1687 {
1688 FunctionStarts::Entry function_start_entry;
1689 function_start_entry.addr = func->base - text_base_addr;
1690 function_starts.Append(function_start_entry);
1691 }
1692 }
1693 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001694 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001695
Greg Clayton36da2aa2013-01-25 18:06:21 +00001696 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001697
Greg Clayton36da2aa2013-01-25 18:06:21 +00001698 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 +00001699
Greg Clayton36da2aa2013-01-25 18:06:21 +00001700 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001701
1702 uint32_t N_SO_index = UINT32_MAX;
1703
1704 MachSymtabSectionInfo section_info (section_list);
1705 std::vector<uint32_t> N_FUN_indexes;
1706 std::vector<uint32_t> N_NSYM_indexes;
1707 std::vector<uint32_t> N_INCL_indexes;
1708 std::vector<uint32_t> N_BRAC_indexes;
1709 std::vector<uint32_t> N_COMM_indexes;
1710 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1711 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytona8364e92013-05-14 22:19:37 +00001712 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001713 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1714 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytona8364e92013-05-14 22:19:37 +00001715 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001716 // Any symbols that get merged into another will get an entry
1717 // in this map so we know
1718 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1719 uint32_t nlist_idx = 0;
1720 Symbol *symbol_ptr = NULL;
1721
1722 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001723 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001724 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001725 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001726 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001727
Jason Molendab62abd52012-06-21 01:51:02 +00001728#if defined (__APPLE__) && defined (__arm__)
1729
1730 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1731 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1732 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1733 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1734 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1735 // nlist parser to ignore all LOCAL symbols.
1736
1737 if (m_header.flags & 0x80000000u)
1738 {
1739 // Before we can start mapping the DSC, we need to make certain the target process is actually
1740 // using the cache we can find.
1741
Jason Molendab62abd52012-06-21 01:51:02 +00001742 // Next we need to determine the correct path for the dyld shared cache.
1743
1744 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1745 char dsc_path[PATH_MAX];
1746
1747 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001748 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1749 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001750 header_arch.GetArchitectureName());
1751
1752 FileSpec dsc_filespec(dsc_path, false);
1753
1754 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda2120aef2013-04-16 00:18:44 +00001755 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001756 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001757 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1758 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1759 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001760 uint32_t imagesOffset;
1761 uint32_t imagesCount;
1762 uint64_t dyldBaseAddress;
1763 uint64_t codeSignatureOffset;
1764 uint64_t codeSignatureSize;
1765 uint64_t slideInfoOffset;
1766 uint64_t slideInfoSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001767 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1768 uint64_t localSymbolsSize; // size of local symbols information
1769 };
1770 struct lldb_copy_dyld_cache_header_v1
1771 {
1772 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1773 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1774 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1775 uint32_t imagesOffset;
1776 uint32_t imagesCount;
1777 uint64_t dyldBaseAddress;
1778 uint64_t codeSignatureOffset;
1779 uint64_t codeSignatureSize;
1780 uint64_t slideInfoOffset;
1781 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001782 uint64_t localSymbolsOffset;
1783 uint64_t localSymbolsSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001784 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001785 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001786
Jason Molenda2120aef2013-04-16 00:18:44 +00001787 struct lldb_copy_dyld_cache_mapping_info
1788 {
1789 uint64_t address;
1790 uint64_t size;
1791 uint64_t fileOffset;
1792 uint32_t maxProt;
1793 uint32_t initProt;
1794 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001795
Greg Claytonab77dcb2012-09-05 22:30:51 +00001796 struct lldb_copy_dyld_cache_local_symbols_info
1797 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001798 uint32_t nlistOffset;
1799 uint32_t nlistCount;
1800 uint32_t stringsOffset;
1801 uint32_t stringsSize;
1802 uint32_t entriesOffset;
1803 uint32_t entriesCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001804 };
1805 struct lldb_copy_dyld_cache_local_symbols_entry
1806 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001807 uint32_t dylibOffset;
1808 uint32_t nlistStartIndex;
1809 uint32_t nlistCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001810 };
Jason Molendab62abd52012-06-21 01:51:02 +00001811
Jason Molendafd3b35d2012-06-22 03:28:35 +00001812 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1813 The dyld_cache_local_symbols_info structure gives us three things:
1814 1. The start and count of the nlist records in the dyld_shared_cache file
1815 2. The start and size of the strings for these nlist records
1816 3. The start and count of dyld_cache_local_symbols_entry entries
1817
1818 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1819 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001820 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001821 and the count of how many nlist records there are for this dylib/framework.
1822 */
1823
Jason Molendab62abd52012-06-21 01:51:02 +00001824 // Process the dsc header to find the unmapped symbols
1825 //
1826 // Save some VM space, do not map the entire cache in one shot.
1827
Jason Molenda6bcabae2013-03-06 23:17:36 +00001828 DataBufferSP dsc_data_sp;
1829 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1830
1831 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001832 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001833 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001834
Jason Molenda6bcabae2013-03-06 23:17:36 +00001835 char version_str[17];
1836 int version = -1;
1837 lldb::offset_t offset = 0;
1838 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1839 version_str[16] = '\0';
1840 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1841 {
1842 int v;
1843 if (::sscanf (version_str + 6, "%d", &v) == 1)
1844 {
1845 version = v;
1846 }
1847 }
1848
Jason Molenda45c75502013-04-16 06:24:42 +00001849 UUID dsc_uuid;
1850 if (version >= 1)
1851 {
1852 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1853 uint8_t uuid_bytes[sizeof (uuid_t)];
1854 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1855 dsc_uuid.SetBytes (uuid_bytes);
1856 }
1857
1858 bool uuid_match = true;
1859 if (dsc_uuid.IsValid() && process)
1860 {
1861 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1862
1863 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1864 {
1865 // The on-disk dyld_shared_cache file is not the same as the one in this
1866 // process' memory, don't use it.
1867 uuid_match = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001868 ModuleSP module_sp (GetModule());
1869 if (module_sp)
1870 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 +00001871 }
1872 }
1873
Jason Molenda9badb6c2013-03-06 23:19:17 +00001874 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001875
Jason Molendab62abd52012-06-21 01:51:02 +00001876 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1877
1878 // If the mappingOffset points to a location inside the header, we've
1879 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda45c75502013-04-16 06:24:42 +00001880 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendab62abd52012-06-21 01:51:02 +00001881 {
1882
Jason Molenda6bcabae2013-03-06 23:17:36 +00001883 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1884 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1885 offset = 0;
1886
1887 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1888 // in the shared library cache need to be adjusted by an offset to match up with the
1889 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1890 // recorded in mapping_offset_value.
1891 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1892
1893 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001894 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1895 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1896
Jason Molenda9badb6c2013-03-06 23:19:17 +00001897 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001898 {
1899 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001900 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001901 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001902 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001903
1904 offset = 0;
1905
1906 // Read the local_symbols_infos struct in one shot
1907 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1908 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1909
Jason Molendab62abd52012-06-21 01:51:02 +00001910 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1911
Jason Molenda6bcabae2013-03-06 23:17:36 +00001912 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001913
1914 offset = local_symbols_info.entriesOffset;
1915 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1916 {
1917 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1918 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1919 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1920 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1921
Jason Molenda9badb6c2013-03-06 23:19:17 +00001922 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001923 {
1924 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1925
1926 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1927 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1928 num_syms = symtab->GetNumSymbols();
1929
1930 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1931 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1932
Jason Molenda9badb6c2013-03-06 23:19:17 +00001933 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001934 {
1935 /////////////////////////////
1936 {
1937 struct nlist_64 nlist;
1938 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1939 break;
1940
1941 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1942 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1943 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1944 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1945 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1946
1947 SymbolType type = eSymbolTypeInvalid;
1948 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1949
1950 if (symbol_name == NULL)
1951 {
1952 // No symbol should be NULL, even the symbols with no
1953 // string values should have an offset zero which points
1954 // to an empty C-string
1955 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00001956 "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 +00001957 entry_index,
1958 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00001959 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendab62abd52012-06-21 01:51:02 +00001960 continue;
1961 }
1962 if (symbol_name[0] == '\0')
1963 symbol_name = NULL;
1964
1965 const char *symbol_name_non_abi_mangled = NULL;
1966
1967 SectionSP symbol_section;
1968 uint32_t symbol_byte_size = 0;
1969 bool add_nlist = true;
1970 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001971 bool demangled_is_synthesized = false;
Greg Claytona8364e92013-05-14 22:19:37 +00001972 bool is_gsym = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001973
1974 assert (sym_idx < num_syms);
1975
1976 sym[sym_idx].SetDebug (is_debug);
1977
1978 if (is_debug)
1979 {
1980 switch (nlist.n_type)
1981 {
1982 case StabGlobalSymbol:
1983 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1984 // Sometimes the N_GSYM value contains the address.
1985
1986 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1987 // have the same address, but we want to ensure that we always find only the real symbol,
1988 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1989 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1990 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1991 // same address.
1992
1993 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1994 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1995 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1996 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1997 add_nlist = false;
1998 else
1999 {
Greg Claytona8364e92013-05-14 22:19:37 +00002000 is_gsym = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002001 sym[sym_idx].SetExternal(true);
2002 if (nlist.n_value != 0)
2003 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2004 type = eSymbolTypeData;
2005 }
2006 break;
2007
2008 case StabFunctionName:
2009 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2010 type = eSymbolTypeCompiler;
2011 break;
2012
2013 case StabFunction:
2014 // N_FUN -- procedure: name,,n_sect,linenumber,address
2015 if (symbol_name)
2016 {
2017 type = eSymbolTypeCode;
2018 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2019
2020 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2021 // We use the current number of symbols in the symbol table in lieu of
2022 // using nlist_idx in case we ever start trimming entries out
2023 N_FUN_indexes.push_back(sym_idx);
2024 }
2025 else
2026 {
2027 type = eSymbolTypeCompiler;
2028
2029 if ( !N_FUN_indexes.empty() )
2030 {
2031 // Copy the size of the function into the original STAB entry so we don't have
2032 // to hunt for it later
2033 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2034 N_FUN_indexes.pop_back();
2035 // We don't really need the end function STAB as it contains the size which
2036 // we already placed with the original symbol, so don't add it if we want a
2037 // minimal symbol table
2038 if (minimize)
2039 add_nlist = false;
2040 }
2041 }
2042 break;
2043
2044 case StabStaticSymbol:
2045 // N_STSYM -- static symbol: name,,n_sect,type,address
2046 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2047 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2048 type = eSymbolTypeData;
2049 break;
2050
2051 case StabLocalCommon:
2052 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2053 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2054 type = eSymbolTypeCommonBlock;
2055 break;
2056
2057 case StabBeginSymbol:
2058 // N_BNSYM
2059 // We use the current number of symbols in the symbol table in lieu of
2060 // using nlist_idx in case we ever start trimming entries out
2061 if (minimize)
2062 {
2063 // Skip these if we want minimal symbol tables
2064 add_nlist = false;
2065 }
2066 else
2067 {
2068 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2069 N_NSYM_indexes.push_back(sym_idx);
2070 type = eSymbolTypeScopeBegin;
2071 }
2072 break;
2073
2074 case StabEndSymbol:
2075 // N_ENSYM
2076 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2077 // so that we can always skip the entire symbol if we need to navigate
2078 // more quickly at the source level when parsing STABS
2079 if (minimize)
2080 {
2081 // Skip these if we want minimal symbol tables
2082 add_nlist = false;
2083 }
2084 else
2085 {
2086 if ( !N_NSYM_indexes.empty() )
2087 {
2088 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2089 symbol_ptr->SetByteSize(sym_idx + 1);
2090 symbol_ptr->SetSizeIsSibling(true);
2091 N_NSYM_indexes.pop_back();
2092 }
2093 type = eSymbolTypeScopeEnd;
2094 }
2095 break;
2096
2097
2098 case StabSourceFileOptions:
2099 // N_OPT - emitted with gcc2_compiled and in gcc source
2100 type = eSymbolTypeCompiler;
2101 break;
2102
2103 case StabRegisterSymbol:
2104 // N_RSYM - register sym: name,,NO_SECT,type,register
2105 type = eSymbolTypeVariable;
2106 break;
2107
2108 case StabSourceLine:
2109 // N_SLINE - src line: 0,,n_sect,linenumber,address
2110 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2111 type = eSymbolTypeLineEntry;
2112 break;
2113
2114 case StabStructureType:
2115 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2116 type = eSymbolTypeVariableType;
2117 break;
2118
2119 case StabSourceFileName:
2120 // N_SO - source file name
2121 type = eSymbolTypeSourceFile;
2122 if (symbol_name == NULL)
2123 {
2124 if (minimize)
2125 add_nlist = false;
2126 if (N_SO_index != UINT32_MAX)
2127 {
2128 // Set the size of the N_SO to the terminating index of this N_SO
2129 // so that we can always skip the entire N_SO if we need to navigate
2130 // more quickly at the source level when parsing STABS
2131 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2132 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2133 symbol_ptr->SetSizeIsSibling(true);
2134 }
2135 N_NSYM_indexes.clear();
2136 N_INCL_indexes.clear();
2137 N_BRAC_indexes.clear();
2138 N_COMM_indexes.clear();
2139 N_FUN_indexes.clear();
2140 N_SO_index = UINT32_MAX;
2141 }
2142 else
2143 {
2144 // We use the current number of symbols in the symbol table in lieu of
2145 // using nlist_idx in case we ever start trimming entries out
2146 const bool N_SO_has_full_path = symbol_name[0] == '/';
2147 if (N_SO_has_full_path)
2148 {
2149 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2150 {
2151 // We have two consecutive N_SO entries where the first contains a directory
2152 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00002153 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002154 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2155 add_nlist = false;
2156 }
2157 else
2158 {
2159 // This is the first entry in a N_SO that contains a directory or
2160 // a full path to the source file
2161 N_SO_index = sym_idx;
2162 }
2163 }
2164 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2165 {
2166 // This is usually the second N_SO entry that contains just the filename,
2167 // so here we combine it with the first one if we are minimizing the symbol table
2168 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2169 if (so_path && so_path[0])
2170 {
2171 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002172 const size_t double_slash_pos = full_so_path.find("//");
2173 if (double_slash_pos != std::string::npos)
2174 {
2175 // The linker has been generating bad N_SO entries with doubled up paths
2176 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2177 // and the second is the directory for the source file so you end up with
2178 // a path that looks like "/tmp/src//tmp/src/"
2179 FileSpec so_dir(so_path, false);
2180 if (!so_dir.Exists())
2181 {
2182 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2183 if (so_dir.Exists())
2184 {
2185 // Trim off the incorrect path
2186 full_so_path.erase(0, double_slash_pos + 1);
2187 }
2188 }
2189 }
Jason Molendab62abd52012-06-21 01:51:02 +00002190 if (*full_so_path.rbegin() != '/')
2191 full_so_path += '/';
2192 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00002193 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002194 add_nlist = false;
2195 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2196 }
2197 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002198 else
2199 {
2200 // This could be a relative path to a N_SO
2201 N_SO_index = sym_idx;
2202 }
Jason Molendab62abd52012-06-21 01:51:02 +00002203 }
Jason Molendab62abd52012-06-21 01:51:02 +00002204 break;
2205
2206 case StabObjectFileName:
2207 // N_OSO - object file name: name,,0,0,st_mtime
2208 type = eSymbolTypeObjectFile;
2209 break;
2210
2211 case StabLocalSymbol:
2212 // N_LSYM - local sym: name,,NO_SECT,type,offset
2213 type = eSymbolTypeLocal;
2214 break;
2215
2216 //----------------------------------------------------------------------
2217 // INCL scopes
2218 //----------------------------------------------------------------------
2219 case StabBeginIncludeFileName:
2220 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2221 // We use the current number of symbols in the symbol table in lieu of
2222 // using nlist_idx in case we ever start trimming entries out
2223 N_INCL_indexes.push_back(sym_idx);
2224 type = eSymbolTypeScopeBegin;
2225 break;
2226
2227 case StabEndIncludeFile:
2228 // N_EINCL - include file end: name,,NO_SECT,0,0
2229 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2230 // so that we can always skip the entire symbol if we need to navigate
2231 // more quickly at the source level when parsing STABS
2232 if ( !N_INCL_indexes.empty() )
2233 {
2234 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2235 symbol_ptr->SetByteSize(sym_idx + 1);
2236 symbol_ptr->SetSizeIsSibling(true);
2237 N_INCL_indexes.pop_back();
2238 }
2239 type = eSymbolTypeScopeEnd;
2240 break;
2241
2242 case StabIncludeFileName:
2243 // N_SOL - #included file name: name,,n_sect,0,address
2244 type = eSymbolTypeHeaderFile;
2245
2246 // We currently don't use the header files on darwin
2247 if (minimize)
2248 add_nlist = false;
2249 break;
2250
2251 case StabCompilerParameters:
2252 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2253 type = eSymbolTypeCompiler;
2254 break;
2255
2256 case StabCompilerVersion:
2257 // N_VERSION - compiler version: name,,NO_SECT,0,0
2258 type = eSymbolTypeCompiler;
2259 break;
2260
2261 case StabCompilerOptLevel:
2262 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2263 type = eSymbolTypeCompiler;
2264 break;
2265
2266 case StabParameter:
2267 // N_PSYM - parameter: name,,NO_SECT,type,offset
2268 type = eSymbolTypeVariable;
2269 break;
2270
2271 case StabAlternateEntry:
2272 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2273 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2274 type = eSymbolTypeLineEntry;
2275 break;
2276
2277 //----------------------------------------------------------------------
2278 // Left and Right Braces
2279 //----------------------------------------------------------------------
2280 case StabLeftBracket:
2281 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2282 // We use the current number of symbols in the symbol table in lieu of
2283 // using nlist_idx in case we ever start trimming entries out
2284 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2285 N_BRAC_indexes.push_back(sym_idx);
2286 type = eSymbolTypeScopeBegin;
2287 break;
2288
2289 case StabRightBracket:
2290 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2291 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2292 // so that we can always skip the entire symbol if we need to navigate
2293 // more quickly at the source level when parsing STABS
2294 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2295 if ( !N_BRAC_indexes.empty() )
2296 {
2297 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2298 symbol_ptr->SetByteSize(sym_idx + 1);
2299 symbol_ptr->SetSizeIsSibling(true);
2300 N_BRAC_indexes.pop_back();
2301 }
2302 type = eSymbolTypeScopeEnd;
2303 break;
2304
2305 case StabDeletedIncludeFile:
2306 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2307 type = eSymbolTypeHeaderFile;
2308 break;
2309
2310 //----------------------------------------------------------------------
2311 // COMM scopes
2312 //----------------------------------------------------------------------
2313 case StabBeginCommon:
2314 // N_BCOMM - begin common: name,,NO_SECT,0,0
2315 // We use the current number of symbols in the symbol table in lieu of
2316 // using nlist_idx in case we ever start trimming entries out
2317 type = eSymbolTypeScopeBegin;
2318 N_COMM_indexes.push_back(sym_idx);
2319 break;
2320
2321 case StabEndCommonLocal:
2322 // N_ECOML - end common (local name): 0,,n_sect,0,address
2323 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2324 // Fall through
2325
2326 case StabEndCommon:
2327 // N_ECOMM - end common: name,,n_sect,0,0
2328 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2329 // so that we can always skip the entire symbol if we need to navigate
2330 // more quickly at the source level when parsing STABS
2331 if ( !N_COMM_indexes.empty() )
2332 {
2333 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2334 symbol_ptr->SetByteSize(sym_idx + 1);
2335 symbol_ptr->SetSizeIsSibling(true);
2336 N_COMM_indexes.pop_back();
2337 }
2338 type = eSymbolTypeScopeEnd;
2339 break;
2340
2341 case StabLength:
2342 // N_LENG - second stab entry with length information
2343 type = eSymbolTypeAdditional;
2344 break;
2345
2346 default: break;
2347 }
2348 }
2349 else
2350 {
2351 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2352 uint8_t n_type = NlistMaskType & nlist.n_type;
2353 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2354
2355 switch (n_type)
2356 {
2357 case NListTypeIndirect: // N_INDR - Fall through
2358 case NListTypePreboundUndefined:// N_PBUD - Fall through
2359 case NListTypeUndefined: // N_UNDF
2360 type = eSymbolTypeUndefined;
2361 break;
2362
2363 case NListTypeAbsolute: // N_ABS
2364 type = eSymbolTypeAbsolute;
2365 break;
2366
2367 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002368 {
2369 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002370
Greg Clayton01e6a582012-11-27 01:52:16 +00002371 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002372 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002373 // TODO: warn about this?
2374 add_nlist = false;
2375 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002376 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002377
2378 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002379 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002380 type = eSymbolTypeException;
2381 }
2382 else
2383 {
2384 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002385
Greg Clayton01e6a582012-11-27 01:52:16 +00002386 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002387 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002388 case SectionTypeRegular: break; // regular section
2389 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2390 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2391 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2392 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2393 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2394 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2395 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2396 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2397 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2398 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2399 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2400 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2401 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2402 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2403 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2404 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2405 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002406 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002407
Greg Clayton01e6a582012-11-27 01:52:16 +00002408 if (type == eSymbolTypeInvalid)
2409 {
2410 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2411 if (symbol_section->IsDescendant (text_section_sp.get()))
2412 {
2413 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2414 SectionAttrUserSelfModifyingCode |
2415 SectionAttrSytemSomeInstructions))
2416 type = eSymbolTypeData;
2417 else
2418 type = eSymbolTypeCode;
2419 }
2420 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002421 {
2422 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2423 {
2424 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002425
Greg Clayton01e6a582012-11-27 01:52:16 +00002426 if (symbol_name &&
2427 symbol_name[0] == '_' &&
2428 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002429 symbol_name[2] == 'B')
2430 {
2431 llvm::StringRef symbol_name_ref(symbol_name);
2432 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2433 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2434 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2435 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2436 {
2437 symbol_name_non_abi_mangled = symbol_name + 1;
2438 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2439 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002440 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002441 }
2442 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2443 {
2444 symbol_name_non_abi_mangled = symbol_name + 1;
2445 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2446 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002447 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002448 }
2449 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2450 {
2451 symbol_name_non_abi_mangled = symbol_name + 1;
2452 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2453 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002454 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002455 }
2456 }
2457 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002458 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002459 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002460 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002461 }
2462 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002463 {
2464 type = eSymbolTypeData;
2465 }
2466 }
2467 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2468 {
2469 type = eSymbolTypeTrampoline;
2470 }
2471 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2472 {
2473 type = eSymbolTypeRuntime;
2474 if (symbol_name && symbol_name[0] == '.')
2475 {
2476 llvm::StringRef symbol_name_ref(symbol_name);
2477 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2478 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002479 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002480 symbol_name_non_abi_mangled = symbol_name;
2481 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2482 type = eSymbolTypeObjCClass;
2483 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002484 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002485 }
2486 }
2487 }
Jason Molendab62abd52012-06-21 01:51:02 +00002488 }
2489 }
Jason Molendab62abd52012-06-21 01:51:02 +00002490 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002491 }
Jason Molendab62abd52012-06-21 01:51:02 +00002492 }
2493
2494 if (add_nlist)
2495 {
2496 uint64_t symbol_value = nlist.n_value;
2497 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002498
Jason Molendab62abd52012-06-21 01:51:02 +00002499 if (symbol_name_non_abi_mangled)
2500 {
Jason Molenda292cca82012-07-20 03:35:44 +00002501 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2502 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002503 }
2504 else
2505 {
2506 if (symbol_name && symbol_name[0] == '_')
2507 {
2508 symbol_name_is_mangled = symbol_name[1] == '_';
2509 symbol_name++; // Skip the leading underscore
2510 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002511
Jason Molendab62abd52012-06-21 01:51:02 +00002512 if (symbol_name)
2513 {
Greg Claytona8364e92013-05-14 22:19:37 +00002514 ConstString const_symbol_name(symbol_name);
2515 if (is_gsym)
2516 N_GSYM_name_to_sym_idx[const_symbol_name.GetCString()] = sym_idx;
2517 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002518 }
2519 }
2520 if (symbol_section)
2521 {
2522 const addr_t section_file_addr = symbol_section->GetFileAddress();
2523 if (symbol_byte_size == 0 && function_starts_count > 0)
2524 {
2525 addr_t symbol_lookup_file_addr = nlist.n_value;
2526 // Do an exact address match for non-ARM addresses, else get the closest since
2527 // the symbol might be a thumb symbol which has an address with bit zero set
2528 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2529 if (is_arm && func_start_entry)
2530 {
2531 // Verify that the function start address is the symbol address (ARM)
2532 // or the symbol address + 1 (thumb)
2533 if (func_start_entry->addr != symbol_lookup_file_addr &&
2534 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2535 {
2536 // Not the right entry, NULL it out...
2537 func_start_entry = NULL;
2538 }
2539 }
2540 if (func_start_entry)
2541 {
2542 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002543
Jason Molendab62abd52012-06-21 01:51:02 +00002544 addr_t symbol_file_addr = func_start_entry->addr;
2545 uint32_t symbol_flags = 0;
2546 if (is_arm)
2547 {
2548 if (symbol_file_addr & 1)
2549 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2550 symbol_file_addr &= 0xfffffffffffffffeull;
2551 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002552
Jason Molendab62abd52012-06-21 01:51:02 +00002553 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2554 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2555 if (next_func_start_entry)
2556 {
2557 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2558 // Be sure the clear the Thumb address bit when we calculate the size
2559 // from the current and next address
2560 if (is_arm)
2561 next_symbol_file_addr &= 0xfffffffffffffffeull;
2562 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2563 }
2564 else
2565 {
2566 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2567 }
2568 }
2569 }
2570 symbol_value -= section_file_addr;
2571 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002572
Greg Claytona8364e92013-05-14 22:19:37 +00002573 if (is_debug == false)
2574 {
2575 if (type == eSymbolTypeCode)
2576 {
2577 // See if we can find a N_FUN entry for any code symbols.
2578 // If we do find a match, and the name matches, then we
2579 // can merge the two into just the function symbol to avoid
2580 // duplicate entries in the symbol table
2581 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2582 if (pos != N_FUN_addr_to_sym_idx.end())
2583 {
2584 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2585 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2586 {
2587 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2588 // We just need the flags from the linker symbol, so put these flags
2589 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2590 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2591 sym[sym_idx].Clear();
2592 continue;
2593 }
2594 }
2595 }
2596 else if (type == eSymbolTypeData)
2597 {
2598 // See if we can find a N_STSYM entry for any data symbols.
2599 // If we do find a match, and the name matches, then we
2600 // can merge the two into just the Static symbol to avoid
2601 // duplicate entries in the symbol table
2602 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2603 if (pos != N_STSYM_addr_to_sym_idx.end())
2604 {
2605 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2606 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2607 {
2608 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2609 // We just need the flags from the linker symbol, so put these flags
2610 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2611 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2612 sym[sym_idx].Clear();
2613 continue;
2614 }
2615 }
2616 else
2617 {
2618 // Combine N_GSYM stab entries with the non stab symbol
2619 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetMangledName().GetCString());
2620 if (pos != N_GSYM_name_to_sym_idx.end())
2621 {
2622 const uint32_t GSYM_sym_idx = pos->second;
2623 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
2624 // Copy the address, because often the N_GSYM address has an invalid address of zero
2625 // when the global is a common symbol
2626 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
2627 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
2628 // We just need the flags from the linker symbol, so put these flags
2629 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2630 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2631 sym[sym_idx].Clear();
2632 continue;
2633 }
2634 }
2635 }
2636 }
2637
Jason Molendab62abd52012-06-21 01:51:02 +00002638 sym[sym_idx].SetID (nlist_idx);
2639 sym[sym_idx].SetType (type);
2640 sym[sym_idx].GetAddress().SetSection (symbol_section);
2641 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2642 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002643
Jason Molendab62abd52012-06-21 01:51:02 +00002644 if (symbol_byte_size > 0)
2645 sym[sym_idx].SetByteSize(symbol_byte_size);
2646
Greg Clayton01e6a582012-11-27 01:52:16 +00002647 if (demangled_is_synthesized)
2648 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002649 ++sym_idx;
2650 }
2651 else
2652 {
2653 sym[sym_idx].Clear();
2654 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002655
Jason Molendab62abd52012-06-21 01:51:02 +00002656 }
2657 /////////////////////////////
2658 }
2659 break; // No more entries to consider
2660 }
2661 }
2662 }
2663 }
2664 }
2665 }
2666 }
2667
2668 // Must reset this in case it was mutated above!
2669 nlist_data_offset = 0;
2670#endif
2671
2672 // If the sym array was not created while parsing the DSC unmapped
2673 // symbols, create it now.
2674 if (sym == NULL)
2675 {
2676 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2677 num_syms = symtab->GetNumSymbols();
2678 }
2679
2680 if (unmapped_local_symbols_found)
2681 {
2682 assert(m_dysymtab.ilocalsym == 0);
2683 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2684 nlist_idx = m_dysymtab.nlocalsym;
2685 }
2686 else
2687 {
2688 nlist_idx = 0;
2689 }
2690
2691 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002692 {
2693 struct nlist_64 nlist;
2694 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2695 break;
2696
2697 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2698 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2699 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2700 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2701 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2702
2703 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002704 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002705
Greg Clayton3a5dc012012-05-25 17:04:00 +00002706 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002707 {
2708 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002709
Greg Claytondd29b972012-05-18 23:20:01 +00002710 if (symbol_name == NULL)
2711 {
2712 // No symbol should be NULL, even the symbols with no
2713 // string values should have an offset zero which points
2714 // to an empty C-string
2715 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00002716 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002717 nlist_idx,
2718 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00002719 module_sp->GetFileSpec().GetPath().c_str());
Greg Claytondd29b972012-05-18 23:20:01 +00002720 continue;
2721 }
2722 if (symbol_name[0] == '\0')
2723 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002724 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002725 else
2726 {
2727 const addr_t str_addr = strtab_addr + nlist.n_strx;
2728 Error str_error;
2729 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2730 symbol_name = memory_symbol_name.c_str();
2731 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002732 const char *symbol_name_non_abi_mangled = NULL;
2733
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002734 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002735 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002736 bool add_nlist = true;
Greg Claytona8364e92013-05-14 22:19:37 +00002737 bool is_gsym = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002738 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002739 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002740
2741 assert (sym_idx < num_syms);
2742
2743 sym[sym_idx].SetDebug (is_debug);
2744
2745 if (is_debug)
2746 {
2747 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002748 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002749 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002750 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2751 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002752
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002753 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2754 // have the same address, but we want to ensure that we always find only the real symbol,
2755 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2756 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2757 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2758 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002759
2760 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002761 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2762 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2763 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2764 add_nlist = false;
2765 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002766 {
Greg Claytona8364e92013-05-14 22:19:37 +00002767 is_gsym = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002768 sym[sym_idx].SetExternal(true);
2769 if (nlist.n_value != 0)
2770 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2771 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002772 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002773 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002774
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002775 case StabFunctionName:
2776 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2777 type = eSymbolTypeCompiler;
2778 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002779
Jason Molenda9badb6c2013-03-06 23:19:17 +00002780 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002781 // N_FUN -- procedure: name,,n_sect,linenumber,address
2782 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002783 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002784 type = eSymbolTypeCode;
2785 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002786
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002787 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2788 // We use the current number of symbols in the symbol table in lieu of
2789 // using nlist_idx in case we ever start trimming entries out
2790 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002791 }
2792 else
2793 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002794 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002795
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002796 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002797 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002798 // Copy the size of the function into the original STAB entry so we don't have
2799 // to hunt for it later
2800 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2801 N_FUN_indexes.pop_back();
2802 // We don't really need the end function STAB as it contains the size which
2803 // we already placed with the original symbol, so don't add it if we want a
2804 // minimal symbol table
2805 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002806 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002807 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002808 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002809 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002810
Jason Molenda9badb6c2013-03-06 23:19:17 +00002811 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002812 // N_STSYM -- static symbol: name,,n_sect,type,address
2813 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2814 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2815 type = eSymbolTypeData;
2816 break;
2817
2818 case StabLocalCommon:
2819 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2820 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2821 type = eSymbolTypeCommonBlock;
2822 break;
2823
2824 case StabBeginSymbol:
2825 // N_BNSYM
2826 // We use the current number of symbols in the symbol table in lieu of
2827 // using nlist_idx in case we ever start trimming entries out
2828 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002829 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002830 // Skip these if we want minimal symbol tables
2831 add_nlist = false;
2832 }
2833 else
2834 {
2835 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2836 N_NSYM_indexes.push_back(sym_idx);
2837 type = eSymbolTypeScopeBegin;
2838 }
2839 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002840
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002841 case StabEndSymbol:
2842 // N_ENSYM
2843 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2844 // so that we can always skip the entire symbol if we need to navigate
2845 // more quickly at the source level when parsing STABS
2846 if (minimize)
2847 {
2848 // Skip these if we want minimal symbol tables
2849 add_nlist = false;
2850 }
2851 else
2852 {
2853 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002854 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002855 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2856 symbol_ptr->SetByteSize(sym_idx + 1);
2857 symbol_ptr->SetSizeIsSibling(true);
2858 N_NSYM_indexes.pop_back();
2859 }
2860 type = eSymbolTypeScopeEnd;
2861 }
2862 break;
2863
2864
2865 case StabSourceFileOptions:
2866 // N_OPT - emitted with gcc2_compiled and in gcc source
2867 type = eSymbolTypeCompiler;
2868 break;
2869
2870 case StabRegisterSymbol:
2871 // N_RSYM - register sym: name,,NO_SECT,type,register
2872 type = eSymbolTypeVariable;
2873 break;
2874
2875 case StabSourceLine:
2876 // N_SLINE - src line: 0,,n_sect,linenumber,address
2877 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2878 type = eSymbolTypeLineEntry;
2879 break;
2880
2881 case StabStructureType:
2882 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2883 type = eSymbolTypeVariableType;
2884 break;
2885
2886 case StabSourceFileName:
2887 // N_SO - source file name
2888 type = eSymbolTypeSourceFile;
2889 if (symbol_name == NULL)
2890 {
2891 if (minimize)
2892 add_nlist = false;
2893 if (N_SO_index != UINT32_MAX)
2894 {
2895 // Set the size of the N_SO to the terminating index of this N_SO
2896 // so that we can always skip the entire N_SO if we need to navigate
2897 // more quickly at the source level when parsing STABS
2898 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2899 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2900 symbol_ptr->SetSizeIsSibling(true);
2901 }
2902 N_NSYM_indexes.clear();
2903 N_INCL_indexes.clear();
2904 N_BRAC_indexes.clear();
2905 N_COMM_indexes.clear();
2906 N_FUN_indexes.clear();
2907 N_SO_index = UINT32_MAX;
2908 }
2909 else
2910 {
2911 // We use the current number of symbols in the symbol table in lieu of
2912 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002913 const bool N_SO_has_full_path = symbol_name[0] == '/';
2914 if (N_SO_has_full_path)
2915 {
2916 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2917 {
2918 // We have two consecutive N_SO entries where the first contains a directory
2919 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002920 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002921 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2922 add_nlist = false;
2923 }
2924 else
2925 {
2926 // This is the first entry in a N_SO that contains a directory or
2927 // a full path to the source file
2928 N_SO_index = sym_idx;
2929 }
2930 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002931 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2932 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002933 // This is usually the second N_SO entry that contains just the filename,
2934 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002935 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2936 if (so_path && so_path[0])
2937 {
2938 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002939 const size_t double_slash_pos = full_so_path.find("//");
2940 if (double_slash_pos != std::string::npos)
2941 {
2942 // The linker has been generating bad N_SO entries with doubled up paths
2943 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2944 // and the second is the directory for the source file so you end up with
2945 // a path that looks like "/tmp/src//tmp/src/"
2946 FileSpec so_dir(so_path, false);
2947 if (!so_dir.Exists())
2948 {
2949 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2950 if (so_dir.Exists())
2951 {
2952 // Trim off the incorrect path
2953 full_so_path.erase(0, double_slash_pos + 1);
2954 }
2955 }
2956 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002957 if (*full_so_path.rbegin() != '/')
2958 full_so_path += '/';
2959 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002960 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002961 add_nlist = false;
2962 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2963 }
2964 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002965 else
2966 {
2967 // This could be a relative path to a N_SO
2968 N_SO_index = sym_idx;
2969 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002970 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002971
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002972 break;
2973
2974 case StabObjectFileName:
2975 // N_OSO - object file name: name,,0,0,st_mtime
2976 type = eSymbolTypeObjectFile;
2977 break;
2978
2979 case StabLocalSymbol:
2980 // N_LSYM - local sym: name,,NO_SECT,type,offset
2981 type = eSymbolTypeLocal;
2982 break;
2983
2984 //----------------------------------------------------------------------
2985 // INCL scopes
2986 //----------------------------------------------------------------------
2987 case StabBeginIncludeFileName:
2988 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2989 // We use the current number of symbols in the symbol table in lieu of
2990 // using nlist_idx in case we ever start trimming entries out
2991 N_INCL_indexes.push_back(sym_idx);
2992 type = eSymbolTypeScopeBegin;
2993 break;
2994
2995 case StabEndIncludeFile:
2996 // N_EINCL - include file end: name,,NO_SECT,0,0
2997 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2998 // so that we can always skip the entire symbol if we need to navigate
2999 // more quickly at the source level when parsing STABS
3000 if ( !N_INCL_indexes.empty() )
3001 {
3002 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3003 symbol_ptr->SetByteSize(sym_idx + 1);
3004 symbol_ptr->SetSizeIsSibling(true);
3005 N_INCL_indexes.pop_back();
3006 }
3007 type = eSymbolTypeScopeEnd;
3008 break;
3009
3010 case StabIncludeFileName:
3011 // N_SOL - #included file name: name,,n_sect,0,address
3012 type = eSymbolTypeHeaderFile;
3013
3014 // We currently don't use the header files on darwin
3015 if (minimize)
3016 add_nlist = false;
3017 break;
3018
Jason Molenda9badb6c2013-03-06 23:19:17 +00003019 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003020 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
3021 type = eSymbolTypeCompiler;
3022 break;
3023
3024 case StabCompilerVersion:
3025 // N_VERSION - compiler version: name,,NO_SECT,0,0
3026 type = eSymbolTypeCompiler;
3027 break;
3028
3029 case StabCompilerOptLevel:
3030 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
3031 type = eSymbolTypeCompiler;
3032 break;
3033
3034 case StabParameter:
3035 // N_PSYM - parameter: name,,NO_SECT,type,offset
3036 type = eSymbolTypeVariable;
3037 break;
3038
3039 case StabAlternateEntry:
3040 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
3041 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3042 type = eSymbolTypeLineEntry;
3043 break;
3044
3045 //----------------------------------------------------------------------
3046 // Left and Right Braces
3047 //----------------------------------------------------------------------
3048 case StabLeftBracket:
3049 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
3050 // We use the current number of symbols in the symbol table in lieu of
3051 // using nlist_idx in case we ever start trimming entries out
3052 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3053 N_BRAC_indexes.push_back(sym_idx);
3054 type = eSymbolTypeScopeBegin;
3055 break;
3056
3057 case StabRightBracket:
3058 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
3059 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3060 // so that we can always skip the entire symbol if we need to navigate
3061 // more quickly at the source level when parsing STABS
3062 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3063 if ( !N_BRAC_indexes.empty() )
3064 {
3065 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3066 symbol_ptr->SetByteSize(sym_idx + 1);
3067 symbol_ptr->SetSizeIsSibling(true);
3068 N_BRAC_indexes.pop_back();
3069 }
3070 type = eSymbolTypeScopeEnd;
3071 break;
3072
3073 case StabDeletedIncludeFile:
3074 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3075 type = eSymbolTypeHeaderFile;
3076 break;
3077
3078 //----------------------------------------------------------------------
3079 // COMM scopes
3080 //----------------------------------------------------------------------
3081 case StabBeginCommon:
3082 // N_BCOMM - begin common: name,,NO_SECT,0,0
3083 // We use the current number of symbols in the symbol table in lieu of
3084 // using nlist_idx in case we ever start trimming entries out
3085 type = eSymbolTypeScopeBegin;
3086 N_COMM_indexes.push_back(sym_idx);
3087 break;
3088
3089 case StabEndCommonLocal:
3090 // N_ECOML - end common (local name): 0,,n_sect,0,address
3091 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3092 // Fall through
3093
3094 case StabEndCommon:
3095 // N_ECOMM - end common: name,,n_sect,0,0
3096 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3097 // so that we can always skip the entire symbol if we need to navigate
3098 // more quickly at the source level when parsing STABS
3099 if ( !N_COMM_indexes.empty() )
3100 {
3101 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3102 symbol_ptr->SetByteSize(sym_idx + 1);
3103 symbol_ptr->SetSizeIsSibling(true);
3104 N_COMM_indexes.pop_back();
3105 }
3106 type = eSymbolTypeScopeEnd;
3107 break;
3108
3109 case StabLength:
3110 // N_LENG - second stab entry with length information
3111 type = eSymbolTypeAdditional;
3112 break;
3113
3114 default: break;
3115 }
3116 }
3117 else
3118 {
3119 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3120 uint8_t n_type = NlistMaskType & nlist.n_type;
3121 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3122
3123 switch (n_type)
3124 {
3125 case NListTypeIndirect: // N_INDR - Fall through
3126 case NListTypePreboundUndefined:// N_PBUD - Fall through
3127 case NListTypeUndefined: // N_UNDF
3128 type = eSymbolTypeUndefined;
3129 break;
3130
3131 case NListTypeAbsolute: // N_ABS
3132 type = eSymbolTypeAbsolute;
3133 break;
3134
3135 case NListTypeSection: // N_SECT
3136 {
3137 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3138
Sean Callananb386d822012-08-09 00:50:26 +00003139 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003140 {
3141 // TODO: warn about this?
3142 add_nlist = false;
3143 break;
3144 }
3145
3146 if (TEXT_eh_frame_sectID == nlist.n_sect)
3147 {
3148 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00003149 }
3150 else
3151 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003152 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3153
3154 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00003155 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003156 case SectionTypeRegular: break; // regular section
3157 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3158 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3159 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3160 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3161 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3162 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3163 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3164 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3165 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3166 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3167 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3168 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3169 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3170 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3171 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3172 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3173 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00003174 }
Chris Lattner24943d22010-06-08 16:52:24 +00003175
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003176 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00003177 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003178 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3179 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003180 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003181 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3182 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003183 SectionAttrSytemSomeInstructions))
3184 type = eSymbolTypeData;
3185 else
3186 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00003187 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003188 else
3189 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003190 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003191 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00003192 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003193 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00003194
Jason Molenda9badb6c2013-03-06 23:19:17 +00003195 if (symbol_name &&
3196 symbol_name[0] == '_' &&
3197 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003198 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00003199 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003200 llvm::StringRef symbol_name_ref(symbol_name);
3201 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3202 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3203 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3204 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00003205 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003206 symbol_name_non_abi_mangled = symbol_name + 1;
3207 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3208 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003209 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003210 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003211 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003212 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003213 symbol_name_non_abi_mangled = symbol_name + 1;
3214 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3215 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003216 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003217 }
3218 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3219 {
3220 symbol_name_non_abi_mangled = symbol_name + 1;
3221 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3222 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003223 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003224 }
3225 }
3226 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003227 else
3228 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3229 {
3230 type = eSymbolTypeException;
3231 }
3232 else
3233 {
3234 type = eSymbolTypeData;
3235 }
3236 }
3237 else
3238 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3239 {
3240 type = eSymbolTypeTrampoline;
3241 }
3242 else
3243 if (symbol_section->IsDescendant(objc_section_sp.get()))
3244 {
3245 type = eSymbolTypeRuntime;
3246 if (symbol_name && symbol_name[0] == '.')
3247 {
3248 llvm::StringRef symbol_name_ref(symbol_name);
3249 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3250 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3251 {
3252 symbol_name_non_abi_mangled = symbol_name;
3253 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3254 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003255 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003256 }
3257 }
Chris Lattner24943d22010-06-08 16:52:24 +00003258 }
3259 }
3260 }
3261 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003262 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003263 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003264 }
3265
3266 if (add_nlist)
3267 {
3268 uint64_t symbol_value = nlist.n_value;
3269 bool symbol_name_is_mangled = false;
3270
3271 if (symbol_name_non_abi_mangled)
3272 {
Greg Claytonc0240042012-07-18 23:18:10 +00003273 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3274 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003275 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003276 else
3277 {
3278 if (symbol_name && symbol_name[0] == '_')
3279 {
3280 symbol_name_is_mangled = symbol_name[1] == '_';
3281 symbol_name++; // Skip the leading underscore
3282 }
3283
3284 if (symbol_name)
3285 {
Greg Claytona8364e92013-05-14 22:19:37 +00003286 ConstString const_symbol_name(symbol_name);
3287 if (is_gsym)
3288 N_GSYM_name_to_sym_idx[const_symbol_name.GetCString()] = sym_idx;
3289 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003290 }
3291 }
3292 if (symbol_section)
3293 {
3294 const addr_t section_file_addr = symbol_section->GetFileAddress();
3295 if (symbol_byte_size == 0 && function_starts_count > 0)
3296 {
Greg Claytond2653c22012-03-14 01:53:24 +00003297 addr_t symbol_lookup_file_addr = nlist.n_value;
3298 // Do an exact address match for non-ARM addresses, else get the closest since
3299 // the symbol might be a thumb symbol which has an address with bit zero set
3300 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3301 if (is_arm && func_start_entry)
3302 {
3303 // Verify that the function start address is the symbol address (ARM)
3304 // or the symbol address + 1 (thumb)
3305 if (func_start_entry->addr != symbol_lookup_file_addr &&
3306 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3307 {
3308 // Not the right entry, NULL it out...
3309 func_start_entry = NULL;
3310 }
3311 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003312 if (func_start_entry)
3313 {
3314 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003315
Greg Claytond2653c22012-03-14 01:53:24 +00003316 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003317 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003318 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003319
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003320 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3321 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3322 if (next_func_start_entry)
3323 {
Greg Claytond2653c22012-03-14 01:53:24 +00003324 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3325 // Be sure the clear the Thumb address bit when we calculate the size
3326 // from the current and next address
3327 if (is_arm)
3328 next_symbol_file_addr &= 0xfffffffffffffffeull;
3329 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 +00003330 }
3331 else
3332 {
Greg Claytond2653c22012-03-14 01:53:24 +00003333 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003334 }
3335 }
3336 }
3337 symbol_value -= section_file_addr;
3338 }
3339
Greg Claytona8364e92013-05-14 22:19:37 +00003340 if (is_debug == false)
3341 {
3342 if (type == eSymbolTypeCode)
3343 {
3344 // See if we can find a N_FUN entry for any code symbols.
3345 // If we do find a match, and the name matches, then we
3346 // can merge the two into just the function symbol to avoid
3347 // duplicate entries in the symbol table
3348 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3349 if (pos != N_FUN_addr_to_sym_idx.end())
3350 {
3351 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3352 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3353 {
3354 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3355 // We just need the flags from the linker symbol, so put these flags
3356 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3357 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3358 sym[sym_idx].Clear();
3359 continue;
3360 }
3361 }
3362 }
3363 else if (type == eSymbolTypeData)
3364 {
3365 // See if we can find a N_STSYM entry for any data symbols.
3366 // If we do find a match, and the name matches, then we
3367 // can merge the two into just the Static symbol to avoid
3368 // duplicate entries in the symbol table
3369 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3370 if (pos != N_STSYM_addr_to_sym_idx.end())
3371 {
3372 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3373 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3374 {
3375 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3376 // We just need the flags from the linker symbol, so put these flags
3377 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3378 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3379 sym[sym_idx].Clear();
3380 continue;
3381 }
3382 }
3383 else
3384 {
3385 // Combine N_GSYM stab entries with the non stab symbol
3386 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetMangledName().GetCString());
3387 if (pos != N_GSYM_name_to_sym_idx.end())
3388 {
3389 const uint32_t GSYM_sym_idx = pos->second;
3390 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3391 // Copy the address, because often the N_GSYM address has an invalid address of zero
3392 // when the global is a common symbol
3393 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3394 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3395 // We just need the flags from the linker symbol, so put these flags
3396 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3397 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3398 sym[sym_idx].Clear();
3399 continue;
3400 }
3401 }
3402 }
3403 }
3404
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003405 sym[sym_idx].SetID (nlist_idx);
3406 sym[sym_idx].SetType (type);
3407 sym[sym_idx].GetAddress().SetSection (symbol_section);
3408 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3409 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3410
3411 if (symbol_byte_size > 0)
3412 sym[sym_idx].SetByteSize(symbol_byte_size);
3413
Greg Clayton01e6a582012-11-27 01:52:16 +00003414 if (demangled_is_synthesized)
3415 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3416
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003417 ++sym_idx;
3418 }
3419 else
3420 {
3421 sym[sym_idx].Clear();
3422 }
3423
3424 }
3425
3426 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3427 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3428 // such entries by figuring out what the address for the global is by looking up this non-STAB
3429 // entry and copying the value into the debug symbol's value to save us the hassle in the
3430 // debug symbol parser.
3431
3432 Symbol *global_symbol = NULL;
3433 for (nlist_idx = 0;
3434 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3435 nlist_idx++)
3436 {
3437 if (global_symbol->GetAddress().GetFileAddress() == 0)
3438 {
3439 std::vector<uint32_t> indexes;
3440 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3441 {
3442 std::vector<uint32_t>::const_iterator pos;
3443 std::vector<uint32_t>::const_iterator end = indexes.end();
3444 for (pos = indexes.begin(); pos != end; ++pos)
3445 {
3446 symbol_ptr = symtab->SymbolAtIndex(*pos);
3447 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3448 {
3449 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3450 break;
3451 }
3452 }
3453 }
Chris Lattner24943d22010-06-08 16:52:24 +00003454 }
3455 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003456
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003457 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3458
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003459 if (function_starts_count > 0)
3460 {
3461 char synthetic_function_symbol[PATH_MAX];
3462 uint32_t num_synthetic_function_symbols = 0;
3463 for (i=0; i<function_starts_count; ++i)
3464 {
3465 if (function_starts.GetEntryRef (i).data == false)
3466 ++num_synthetic_function_symbols;
3467 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003468
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003469 if (num_synthetic_function_symbols > 0)
3470 {
3471 if (num_syms < sym_idx + num_synthetic_function_symbols)
3472 {
3473 num_syms = sym_idx + num_synthetic_function_symbols;
3474 sym = symtab->Resize (num_syms);
3475 }
3476 uint32_t synthetic_function_symbol_idx = 0;
3477 for (i=0; i<function_starts_count; ++i)
3478 {
3479 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3480 if (func_start_entry->data == false)
3481 {
Greg Claytond2653c22012-03-14 01:53:24 +00003482 addr_t symbol_file_addr = func_start_entry->addr;
3483 uint32_t symbol_flags = 0;
3484 if (is_arm)
3485 {
3486 if (symbol_file_addr & 1)
3487 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3488 symbol_file_addr &= 0xfffffffffffffffeull;
3489 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003490 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003491 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003492 {
3493 SectionSP symbol_section (symbol_addr.GetSection());
3494 uint32_t symbol_byte_size = 0;
3495 if (symbol_section)
3496 {
3497 const addr_t section_file_addr = symbol_section->GetFileAddress();
3498 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3499 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3500 if (next_func_start_entry)
3501 {
Greg Claytond2653c22012-03-14 01:53:24 +00003502 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3503 if (is_arm)
3504 next_symbol_file_addr &= 0xfffffffffffffffeull;
3505 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 +00003506 }
3507 else
3508 {
Greg Claytond2653c22012-03-14 01:53:24 +00003509 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003510 }
3511 snprintf (synthetic_function_symbol,
3512 sizeof(synthetic_function_symbol),
3513 "___lldb_unnamed_function%u$$%s",
3514 ++synthetic_function_symbol_idx,
3515 module_sp->GetFileSpec().GetFilename().GetCString());
3516 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003517 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003518 sym[sym_idx].SetType (eSymbolTypeCode);
3519 sym[sym_idx].SetIsSynthetic (true);
3520 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003521 if (symbol_flags)
3522 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003523 if (symbol_byte_size)
3524 sym[sym_idx].SetByteSize (symbol_byte_size);
3525 ++sym_idx;
3526 }
3527 }
3528 }
3529 }
3530 }
3531 }
3532
3533 // Trim our symbols down to just what we ended up with after
3534 // removing any symbols.
3535 if (sym_idx < num_syms)
3536 {
3537 num_syms = sym_idx;
3538 sym = symtab->Resize (num_syms);
3539 }
3540
3541 // Now synthesize indirect symbols
3542 if (m_dysymtab.nindirectsyms != 0)
3543 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003544 if (indirect_symbol_index_data.GetByteSize())
3545 {
3546 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3547
3548 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3549 {
3550 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3551 {
3552 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3553 if (symbol_stub_byte_size == 0)
3554 continue;
3555
3556 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3557
3558 if (num_symbol_stubs == 0)
3559 continue;
3560
3561 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3562 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3563 {
3564 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3565 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 +00003566 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003567 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3568 {
3569 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3570 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3571 continue;
3572
3573 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3574 Symbol *stub_symbol = NULL;
3575 if (index_pos != end_index_pos)
3576 {
3577 // We have a remapping from the original nlist index to
3578 // a current symbol index, so just look this up by index
3579 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3580 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003581 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003582 {
3583 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003584 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003585 // S_SYMBOL_STUBS
3586 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3587 }
3588
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003589 if (stub_symbol)
3590 {
3591 Address so_addr(symbol_stub_addr, section_list);
3592
3593 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3594 {
3595 // Change the external symbol into a trampoline that makes sense
3596 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3597 // can re-use them so we don't have to make up a synthetic symbol
3598 // for no good reason.
3599 stub_symbol->SetType (eSymbolTypeTrampoline);
3600 stub_symbol->SetExternal (false);
3601 stub_symbol->GetAddress() = so_addr;
3602 stub_symbol->SetByteSize (symbol_stub_byte_size);
3603 }
3604 else
3605 {
3606 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003607 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003608 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003609 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003610 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003611 stub_symbol = NULL; // this pointer no longer valid
3612 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003613 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003614 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003615 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3616 sym[sym_idx].SetIsSynthetic (true);
3617 sym[sym_idx].GetAddress() = so_addr;
3618 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3619 ++sym_idx;
3620 }
3621 }
Greg Claytond4330e62012-09-05 01:38:55 +00003622 else
3623 {
3624 if (log)
3625 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3626 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003627 }
3628 }
3629 }
3630 }
3631 }
3632 }
3633 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003634 }
3635 return 0;
3636}
3637
3638
3639void
3640ObjectFileMachO::Dump (Stream *s)
3641{
Greg Clayton9482f052012-03-13 23:14:29 +00003642 ModuleSP module_sp(GetModule());
3643 if (module_sp)
3644 {
3645 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3646 s->Printf("%p: ", this);
3647 s->Indent();
3648 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3649 s->PutCString("ObjectFileMachO64");
3650 else
3651 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003652
Greg Clayton9482f052012-03-13 23:14:29 +00003653 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003654
Greg Clayton9482f052012-03-13 23:14:29 +00003655 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003656
Greg Clayton9482f052012-03-13 23:14:29 +00003657 if (m_sections_ap.get())
3658 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003659
Greg Clayton9482f052012-03-13 23:14:29 +00003660 if (m_symtab_ap.get())
3661 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3662 }
Chris Lattner24943d22010-06-08 16:52:24 +00003663}
3664
Greg Clayton36b877d2013-04-24 22:29:28 +00003665bool
3666ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3667 const lldb_private::DataExtractor &data,
3668 lldb::offset_t lc_offset,
3669 lldb_private::UUID& uuid)
3670{
3671 uint32_t i;
3672 struct uuid_command load_cmd;
3673
3674 lldb::offset_t offset = lc_offset;
3675 for (i=0; i<header.ncmds; ++i)
3676 {
3677 const lldb::offset_t cmd_offset = offset;
3678 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3679 break;
3680
3681 if (load_cmd.cmd == LoadCommandUUID)
3682 {
3683 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3684
3685 if (uuid_bytes)
3686 {
3687 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3688 // We pretend these object files have no UUID to prevent crashing.
3689
3690 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3691 0x3b, 0xa8,
3692 0x4b, 0x16,
3693 0xb6, 0xa4,
3694 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3695
3696 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3697 return false;
3698
3699 uuid.SetBytes (uuid_bytes);
3700 return true;
3701 }
3702 return false;
3703 }
3704 offset = cmd_offset + load_cmd.cmdsize;
3705 }
3706 return false;
3707}
Chris Lattner24943d22010-06-08 16:52:24 +00003708
3709bool
Greg Clayton0467c782011-02-04 18:53:10 +00003710ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003711{
Greg Clayton9482f052012-03-13 23:14:29 +00003712 ModuleSP module_sp(GetModule());
3713 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003714 {
Greg Clayton9482f052012-03-13 23:14:29 +00003715 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00003716 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton36b877d2013-04-24 22:29:28 +00003717 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner24943d22010-06-08 16:52:24 +00003718 }
3719 return false;
3720}
3721
3722
3723uint32_t
3724ObjectFileMachO::GetDependentModules (FileSpecList& files)
3725{
Chris Lattner24943d22010-06-08 16:52:24 +00003726 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003727 ModuleSP module_sp(GetModule());
3728 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003729 {
Greg Clayton9482f052012-03-13 23:14:29 +00003730 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3731 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003732 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003733 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3734 uint32_t i;
3735 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003736 {
Greg Clayton9482f052012-03-13 23:14:29 +00003737 const uint32_t cmd_offset = offset;
3738 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3739 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003740
Greg Clayton9482f052012-03-13 23:14:29 +00003741 switch (load_cmd.cmd)
3742 {
3743 case LoadCommandDylibLoad:
3744 case LoadCommandDylibLoadWeak:
3745 case LoadCommandDylibReexport:
3746 case LoadCommandDynamicLinkerLoad:
3747 case LoadCommandFixedVMShlibLoad:
3748 case LoadCommandDylibLoadUpward:
3749 {
3750 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3751 const char *path = m_data.PeekCStr(name_offset);
3752 // Skip any path that starts with '@' since these are usually:
3753 // @executable_path/.../file
3754 // @rpath/.../file
3755 if (path && path[0] != '@')
3756 {
3757 FileSpec file_spec(path, resolve_path);
3758 if (files.AppendIfUnique(file_spec))
3759 count++;
3760 }
3761 }
3762 break;
3763
3764 default:
3765 break;
3766 }
3767 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003768 }
Chris Lattner24943d22010-06-08 16:52:24 +00003769 }
3770 return count;
3771}
3772
Jim Ingham28775942011-03-07 23:44:08 +00003773lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003774ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003775{
3776 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3777 // is initialized to an invalid address, so we can just return that.
3778 // 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 +00003779
Jim Ingham28775942011-03-07 23:44:08 +00003780 if (!IsExecutable() || m_entry_point_address.IsValid())
3781 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003782
3783 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003784 // /usr/include/mach-o.h, but it is basically:
3785 //
3786 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3787 // uint32_t count - this is the count of longs in the thread state data
3788 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3789 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003790 //
Jim Ingham28775942011-03-07 23:44:08 +00003791 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3792 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3793 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3794 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3795 //
3796 // For now we hard-code the offsets and flavors we need:
3797 //
3798 //
3799
Greg Clayton9482f052012-03-13 23:14:29 +00003800 ModuleSP module_sp(GetModule());
3801 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003802 {
Greg Clayton9482f052012-03-13 23:14:29 +00003803 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3804 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003805 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003806 uint32_t i;
3807 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3808 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003809
Greg Clayton9482f052012-03-13 23:14:29 +00003810 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003811 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003812 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003813 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3814 break;
3815
3816 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003817 {
Greg Clayton9482f052012-03-13 23:14:29 +00003818 case LoadCommandUnixThread:
3819 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003820 {
Greg Clayton9482f052012-03-13 23:14:29 +00003821 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003822 {
Greg Clayton9482f052012-03-13 23:14:29 +00003823 uint32_t flavor = m_data.GetU32(&offset);
3824 uint32_t count = m_data.GetU32(&offset);
3825 if (count == 0)
3826 {
3827 // We've gotten off somehow, log and exit;
3828 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003829 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003830
Greg Clayton9482f052012-03-13 23:14:29 +00003831 switch (m_header.cputype)
3832 {
3833 case llvm::MachO::CPUTypeARM:
3834 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3835 {
3836 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3837 start_address = m_data.GetU32(&offset);
3838 done = true;
3839 }
Jim Ingham28775942011-03-07 23:44:08 +00003840 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003841 case llvm::MachO::CPUTypeI386:
3842 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3843 {
3844 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3845 start_address = m_data.GetU32(&offset);
3846 done = true;
3847 }
3848 break;
3849 case llvm::MachO::CPUTypeX86_64:
3850 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3851 {
3852 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3853 start_address = m_data.GetU64(&offset);
3854 done = true;
3855 }
3856 break;
3857 default:
3858 return m_entry_point_address;
3859 }
3860 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3861 if (done)
3862 break;
3863 offset += count * 4;
3864 }
Jim Ingham28775942011-03-07 23:44:08 +00003865 }
Greg Clayton9482f052012-03-13 23:14:29 +00003866 break;
3867 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003868 {
Greg Clayton9482f052012-03-13 23:14:29 +00003869 ConstString text_segment_name ("__TEXT");
3870 uint64_t entryoffset = m_data.GetU64(&offset);
3871 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3872 if (text_segment_sp)
3873 {
3874 done = true;
3875 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3876 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003877 }
Greg Clayton9482f052012-03-13 23:14:29 +00003878
3879 default:
3880 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003881 }
Greg Clayton9482f052012-03-13 23:14:29 +00003882 if (done)
3883 break;
Jim Ingham28775942011-03-07 23:44:08 +00003884
Greg Clayton9482f052012-03-13 23:14:29 +00003885 // Go to the next load command:
3886 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003887 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003888
Greg Clayton9482f052012-03-13 23:14:29 +00003889 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003890 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003891 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003892 // of this ObjectFile:
3893 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003894 {
Greg Clayton9482f052012-03-13 23:14:29 +00003895 m_entry_point_address.Clear();
3896 }
3897 }
3898 else
3899 {
3900 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3901 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003902
Greg Clayton9482f052012-03-13 23:14:29 +00003903 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003904
Greg Clayton9482f052012-03-13 23:14:29 +00003905 if (module_sp)
3906 {
3907 SymbolContextList contexts;
3908 SymbolContext context;
3909 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3910 {
3911 if (contexts.GetContextAtIndex(0, context))
3912 m_entry_point_address = context.symbol->GetAddress();
3913 }
Greg Clayton3508c382012-02-24 01:59:29 +00003914 }
3915 }
Jim Ingham28775942011-03-07 23:44:08 +00003916 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003917
Jim Ingham28775942011-03-07 23:44:08 +00003918 return m_entry_point_address;
3919
3920}
3921
Greg Claytonb5a8f142012-02-05 02:38:54 +00003922lldb_private::Address
3923ObjectFileMachO::GetHeaderAddress ()
3924{
3925 lldb_private::Address header_addr;
3926 SectionList *section_list = GetSectionList();
3927 if (section_list)
3928 {
3929 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3930 if (text_segment_sp)
3931 {
Greg Clayton3508c382012-02-24 01:59:29 +00003932 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003933 header_addr.SetOffset (0);
3934 }
3935 }
3936 return header_addr;
3937}
3938
Greg Clayton46c9a352012-02-09 06:16:32 +00003939uint32_t
3940ObjectFileMachO::GetNumThreadContexts ()
3941{
Greg Clayton9482f052012-03-13 23:14:29 +00003942 ModuleSP module_sp(GetModule());
3943 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003944 {
Greg Clayton9482f052012-03-13 23:14:29 +00003945 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3946 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003947 {
Greg Clayton9482f052012-03-13 23:14:29 +00003948 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003949 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003950 FileRangeArray::Entry file_range;
3951 thread_command thread_cmd;
3952 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003953 {
Greg Clayton9482f052012-03-13 23:14:29 +00003954 const uint32_t cmd_offset = offset;
3955 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3956 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003957
Greg Clayton9482f052012-03-13 23:14:29 +00003958 if (thread_cmd.cmd == LoadCommandThread)
3959 {
3960 file_range.SetRangeBase (offset);
3961 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3962 m_thread_context_offsets.Append (file_range);
3963 }
3964 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003965 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003966 }
3967 }
3968 return m_thread_context_offsets.GetSize();
3969}
3970
3971lldb::RegisterContextSP
3972ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3973{
Greg Clayton46c9a352012-02-09 06:16:32 +00003974 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003975
Greg Clayton9482f052012-03-13 23:14:29 +00003976 ModuleSP module_sp(GetModule());
3977 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003978 {
Greg Clayton9482f052012-03-13 23:14:29 +00003979 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3980 if (!m_thread_context_offsets_valid)
3981 GetNumThreadContexts ();
3982
3983 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003984 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003985 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003986
3987 DataExtractor data (m_data,
3988 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003989 thread_context_file_range->GetByteSize());
3990
3991 switch (m_header.cputype)
3992 {
3993 case llvm::MachO::CPUTypeARM:
3994 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3995 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003996
Jim Ingham6f01c932012-10-12 17:34:26 +00003997 case llvm::MachO::CPUTypeI386:
3998 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
3999 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00004000
Jim Ingham6f01c932012-10-12 17:34:26 +00004001 case llvm::MachO::CPUTypeX86_64:
4002 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
4003 break;
4004 }
Greg Clayton9482f052012-03-13 23:14:29 +00004005 }
Greg Clayton46c9a352012-02-09 06:16:32 +00004006 }
4007 return reg_ctx_sp;
4008}
4009
Greg Claytonb5a8f142012-02-05 02:38:54 +00004010
Greg Claytonca319972011-07-09 00:41:34 +00004011ObjectFile::Type
4012ObjectFileMachO::CalculateType()
4013{
4014 switch (m_header.filetype)
4015 {
4016 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4017 if (GetAddressByteSize () == 4)
4018 {
4019 // 32 bit kexts are just object files, but they do have a valid
4020 // UUID load command.
4021 UUID uuid;
4022 if (GetUUID(&uuid))
4023 {
4024 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00004025 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00004026 // "OSKextGetCurrentIdentifier" as this is required of kexts
4027 if (m_strata == eStrataInvalid)
4028 m_strata = eStrataKernel;
4029 return eTypeSharedLibrary;
4030 }
4031 }
4032 return eTypeObjectFile;
4033
4034 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
4035 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
4036 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
4037 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
4038 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
4039 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
4040 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
4041 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
4042 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
4043 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
4044 default:
4045 break;
4046 }
4047 return eTypeUnknown;
4048}
4049
4050ObjectFile::Strata
4051ObjectFileMachO::CalculateStrata()
4052{
4053 switch (m_header.filetype)
4054 {
4055 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4056 {
4057 // 32 bit kexts are just object files, but they do have a valid
4058 // UUID load command.
4059 UUID uuid;
4060 if (GetUUID(&uuid))
4061 {
4062 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00004063 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00004064 // "OSKextGetCurrentIdentifier" as this is required of kexts
4065 if (m_type == eTypeInvalid)
4066 m_type = eTypeSharedLibrary;
4067
4068 return eStrataKernel;
4069 }
4070 }
4071 return eStrataUnknown;
4072
4073 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
4074 // Check for the MH_DYLDLINK bit in the flags
4075 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00004076 {
Greg Claytonca319972011-07-09 00:41:34 +00004077 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00004078 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004079 else
Sean Callananac725af2012-02-10 20:22:35 +00004080 {
4081 SectionList *section_list = GetSectionList();
4082 if (section_list)
4083 {
4084 static ConstString g_kld_section_name ("__KLD");
4085 if (section_list->FindSectionByName(g_kld_section_name))
4086 return eStrataKernel;
4087 }
4088 }
4089 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00004090
4091 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4092 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00004093 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00004094 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4095 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4096 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4097 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4098 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4099 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4100 default:
4101 break;
4102 }
4103 return eStrataUnknown;
4104}
4105
4106
Greg Clayton49f4bf22012-02-22 19:41:02 +00004107uint32_t
4108ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4109{
Greg Clayton9482f052012-03-13 23:14:29 +00004110 ModuleSP module_sp(GetModule());
4111 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004112 {
Greg Clayton9482f052012-03-13 23:14:29 +00004113 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4114 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00004115 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00004116 uint32_t version_cmd = 0;
4117 uint64_t version = 0;
4118 uint32_t i;
4119 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004120 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00004121 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00004122 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4123 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00004124
Greg Clayton9482f052012-03-13 23:14:29 +00004125 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004126 {
Greg Clayton9482f052012-03-13 23:14:29 +00004127 if (version_cmd == 0)
4128 {
4129 version_cmd = load_cmd.cmd;
4130 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4131 break;
4132 version = load_cmd.dylib.current_version;
4133 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004134 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00004135 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00004136 }
Greg Clayton9482f052012-03-13 23:14:29 +00004137 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004138 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004139
Greg Clayton9482f052012-03-13 23:14:29 +00004140 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004141 {
Greg Clayton9482f052012-03-13 23:14:29 +00004142 if (versions != NULL && num_versions > 0)
4143 {
4144 if (num_versions > 0)
4145 versions[0] = (version & 0xFFFF0000ull) >> 16;
4146 if (num_versions > 1)
4147 versions[1] = (version & 0x0000FF00ull) >> 8;
4148 if (num_versions > 2)
4149 versions[2] = (version & 0x000000FFull);
4150 // Fill in an remaining version numbers with invalid values
4151 for (i=3; i<num_versions; ++i)
4152 versions[i] = UINT32_MAX;
4153 }
4154 // The LC_ID_DYLIB load command has a version with 3 version numbers
4155 // in it, so always return 3
4156 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004157 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00004158 }
4159 return false;
4160}
4161
Chris Lattner24943d22010-06-08 16:52:24 +00004162bool
Greg Clayton395fc332011-02-15 21:59:32 +00004163ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00004164{
Greg Clayton9482f052012-03-13 23:14:29 +00004165 ModuleSP module_sp(GetModule());
4166 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004167 {
Greg Clayton9482f052012-03-13 23:14:29 +00004168 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4169 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00004170
Greg Clayton9482f052012-03-13 23:14:29 +00004171 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00004172 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00004173 // unknown to make sure we use the DynamicLoaderStatic()...
4174 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4175 {
4176 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4177 }
4178 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004179 }
Greg Clayton9482f052012-03-13 23:14:29 +00004180 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00004181}
4182
4183
Jason Molenda45c75502013-04-16 06:24:42 +00004184UUID
4185ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4186{
4187 UUID uuid;
4188 if (process)
4189 {
4190 addr_t all_image_infos = process->GetImageInfoAddress();
4191
4192 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4193 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4194 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4195 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4196
4197 Error err;
4198 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4199 if (version_or_magic != -1
4200 && version_or_magic != HeaderMagic32
4201 && version_or_magic != HeaderMagic32Swapped
4202 && version_or_magic != HeaderMagic64
4203 && version_or_magic != HeaderMagic64Swapped
4204 && version_or_magic >= 13)
4205 {
4206 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4207 int wordsize = process->GetAddressByteSize();
4208 if (wordsize == 8)
4209 {
4210 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4211 }
4212 if (wordsize == 4)
4213 {
4214 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4215 }
4216 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4217 {
4218 uuid_t shared_cache_uuid;
4219 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4220 {
4221 uuid.SetBytes (shared_cache_uuid);
4222 }
4223 }
4224 }
4225 }
4226 return uuid;
4227}
4228
4229UUID
4230ObjectFileMachO::GetLLDBSharedCacheUUID ()
4231{
4232 UUID uuid;
4233#if defined (__APPLE__) && defined (__arm__)
4234 uint8_t *(*dyld_get_all_image_infos)(void);
4235 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4236 if (dyld_get_all_image_infos)
4237 {
4238 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4239 if (dyld_all_image_infos_address)
4240 {
Jason Molenda6ff916f2013-04-16 21:42:58 +00004241 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4242 if (*version >= 13)
Jason Molenda45c75502013-04-16 06:24:42 +00004243 {
Jason Molenda2ceae992013-04-16 22:56:17 +00004244 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 +00004245 uuid.SetBytes (sharedCacheUUID_address);
4246 }
4247 }
4248 }
4249#endif
4250 return uuid;
4251}
4252
4253
Chris Lattner24943d22010-06-08 16:52:24 +00004254//------------------------------------------------------------------
4255// PluginInterface protocol
4256//------------------------------------------------------------------
Greg Clayton0e191602013-05-10 21:47:16 +00004257lldb_private::ConstString
Chris Lattner24943d22010-06-08 16:52:24 +00004258ObjectFileMachO::GetPluginName()
4259{
Chris Lattner24943d22010-06-08 16:52:24 +00004260 return GetPluginNameStatic();
4261}
4262
4263uint32_t
4264ObjectFileMachO::GetPluginVersion()
4265{
4266 return 1;
4267}
4268