blob: 9f27c1c5107bd86f8bc58a8f6ec1a56683fb5368 [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);
Greg Clayton7c6bda42013-06-04 20:27:06 +0000997
998 load_cmd.fileoff = 0;
999 load_cmd.filesize = 0;
Greg Claytonbb759862013-04-16 16:51:19 +00001000 }
1001
Jason Molendaadf9e3d2013-04-10 05:58:57 +00001002 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1003 {
1004 // We have a load command that says it extends past the end of hte file. This is likely
1005 // a corrupt file. We don't have any way to return an error condition here (this method
1006 // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do
1007 // is null out the SectionList vector and if a process has been set up, dump a message
Greg Claytonbb759862013-04-16 16:51:19 +00001008 // to stdout. The most common case here is core file debugging with a truncated file.
1009 const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Greg Clayton7c6bda42013-06-04 20:27:06 +00001010 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 "), the segment will be truncated",
Greg Claytonbb759862013-04-16 16:51:19 +00001011 i,
1012 lc_segment_name,
1013 load_cmd.fileoff + load_cmd.filesize,
1014 m_length);
Greg Clayton7c6bda42013-06-04 20:27:06 +00001015
1016 // Tuncase the length
1017 load_cmd.filesize = m_length - load_cmd.fileoff;
Jason Molendaadf9e3d2013-04-10 05:58:57 +00001018 }
1019 }
Chris Lattner24943d22010-06-08 16:52:24 +00001020 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1021 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001022
Greg Clayton68ca8232011-01-25 02:58:48 +00001023 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
1024
Chris Lattner24943d22010-06-08 16:52:24 +00001025 // Keep a list of mach segments around in case we need to
1026 // get at data that isn't stored in the abstracted Sections.
1027 m_mach_segments.push_back (load_cmd);
1028
Greg Clayton36da2aa2013-01-25 18:06:21 +00001029 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 +00001030 // Use a segment ID of the segment index shifted left by 8 so they
1031 // never conflict with any of the sections.
1032 SectionSP segment_sp;
Greg Clayton46c9a352012-02-09 06:16:32 +00001033 if (segment_name || is_core)
Chris Lattner24943d22010-06-08 16:52:24 +00001034 {
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001035 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
Chris Lattner24943d22010-06-08 16:52:24 +00001036 ++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
1037 segment_name, // Name of this section
1038 eSectionTypeContainer, // This section is a container of other sections.
1039 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1040 load_cmd.vmsize, // VM size in bytes of this section
1041 load_cmd.fileoff, // Offset to the data for this section in the file
1042 load_cmd.filesize, // Size in bytes of this section as found in the the file
1043 load_cmd.flags)); // Flags for this section
1044
Greg Clayton68ca8232011-01-25 02:58:48 +00001045 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001046 m_sections_ap->AddSection(segment_sp);
1047 }
1048
1049 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +00001050 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +00001051 // Push a section into our mach sections for the section at
Jason Molenda9badb6c2013-03-06 23:19:17 +00001052 // index zero (NListSectionNoSection) if we don't have any
Greg Clayton6af4fad2010-10-06 01:26:32 +00001053 // mach sections yet...
1054 if (m_mach_sections.empty())
1055 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +00001056 uint32_t segment_sect_idx;
1057 const lldb::user_id_t first_segment_sectID = sectID + 1;
1058
1059
Greg Clayton1674b122010-07-21 22:12:05 +00001060 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +00001061 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
1062 {
1063 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
1064 break;
1065 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1066 break;
1067 sect64.addr = m_data.GetAddress(&offset);
1068 sect64.size = m_data.GetAddress(&offset);
1069
1070 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1071 break;
1072
1073 // Keep a list of mach sections around in case we need to
1074 // get at data that isn't stored in the abstracted Sections.
1075 m_mach_sections.push_back (sect64);
1076
1077 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1078 if (!segment_name)
1079 {
1080 // We have a segment with no name so we need to conjure up
1081 // segments that correspond to the section's segname if there
1082 // isn't already such a section. If there is such a section,
1083 // we resize the section so that it spans all sections.
1084 // We also mark these sections as fake so address matches don't
1085 // hit if they land in the gaps between the child sections.
1086 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1087 segment_sp = m_sections_ap->FindSectionByName (segment_name);
1088 if (segment_sp.get())
1089 {
1090 Section *segment = segment_sp.get();
1091 // Grow the section size as needed.
1092 const lldb::addr_t sect64_min_addr = sect64.addr;
1093 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1094 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1095 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1096 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1097 if (sect64_min_addr >= curr_seg_min_addr)
1098 {
1099 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1100 // Only grow the section size if needed
1101 if (new_seg_byte_size > curr_seg_byte_size)
1102 segment->SetByteSize (new_seg_byte_size);
1103 }
1104 else
1105 {
1106 // We need to change the base address of the segment and
1107 // adjust the child section offsets for all existing children.
1108 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1109 segment->Slide(slide_amount, false);
Sean Callanan716a6642012-06-08 02:16:08 +00001110 segment->GetChildren().Slide(-slide_amount, false);
Chris Lattner24943d22010-06-08 16:52:24 +00001111 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1112 }
Greg Clayton661825b2010-06-28 23:51:11 +00001113
1114 // Grow the section size as needed.
1115 if (sect64.offset)
1116 {
1117 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1118 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1119
1120 const lldb::addr_t section_min_file_offset = sect64.offset;
1121 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1122 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1123 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1124 segment->SetFileOffset (new_file_offset);
1125 segment->SetFileSize (new_file_size);
1126 }
Chris Lattner24943d22010-06-08 16:52:24 +00001127 }
1128 else
1129 {
1130 // Create a fake section for the section's named segment
Greg Clayton3508c382012-02-24 01:59:29 +00001131 segment_sp.reset(new Section (segment_sp, // Parent section
1132 module_sp, // Module to which this section belongs
1133 ++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
1134 segment_name, // Name of this section
1135 eSectionTypeContainer, // This section is a container of other sections.
1136 sect64.addr, // File VM address == addresses as they are found in the object file
1137 sect64.size, // VM size in bytes of this section
1138 sect64.offset, // Offset to the data for this section in the file
1139 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
1140 load_cmd.flags)); // Flags for this section
Chris Lattner24943d22010-06-08 16:52:24 +00001141 segment_sp->SetIsFake(true);
1142 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +00001143 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001144 }
1145 }
1146 assert (segment_sp.get());
1147
Greg Clayton1674b122010-07-21 22:12:05 +00001148 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +00001149 static ConstString g_sect_name_objc_data ("__objc_data");
1150 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1151 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1152 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1153 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1154 static ConstString g_sect_name_objc_const ("__objc_const");
1155 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1156 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001157
1158 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1159 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1160 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1161 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1162 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1163 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1164 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1165 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1166 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1167 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1168 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +00001169 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1170 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +00001171 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +00001172 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001173 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +00001174 static ConstString g_sect_name_DATA ("__DATA");
1175 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001176
Chris Lattner24943d22010-06-08 16:52:24 +00001177 SectionType sect_type = eSectionTypeOther;
1178
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001179 if (section_name == g_sect_name_dwarf_debug_abbrev)
1180 sect_type = eSectionTypeDWARFDebugAbbrev;
1181 else if (section_name == g_sect_name_dwarf_debug_aranges)
1182 sect_type = eSectionTypeDWARFDebugAranges;
1183 else if (section_name == g_sect_name_dwarf_debug_frame)
1184 sect_type = eSectionTypeDWARFDebugFrame;
1185 else if (section_name == g_sect_name_dwarf_debug_info)
1186 sect_type = eSectionTypeDWARFDebugInfo;
1187 else if (section_name == g_sect_name_dwarf_debug_line)
1188 sect_type = eSectionTypeDWARFDebugLine;
1189 else if (section_name == g_sect_name_dwarf_debug_loc)
1190 sect_type = eSectionTypeDWARFDebugLoc;
1191 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1192 sect_type = eSectionTypeDWARFDebugMacInfo;
1193 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1194 sect_type = eSectionTypeDWARFDebugPubNames;
1195 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1196 sect_type = eSectionTypeDWARFDebugPubTypes;
1197 else if (section_name == g_sect_name_dwarf_debug_ranges)
1198 sect_type = eSectionTypeDWARFDebugRanges;
1199 else if (section_name == g_sect_name_dwarf_debug_str)
1200 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +00001201 else if (section_name == g_sect_name_dwarf_apple_names)
1202 sect_type = eSectionTypeDWARFAppleNames;
1203 else if (section_name == g_sect_name_dwarf_apple_types)
1204 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +00001205 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1206 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001207 else if (section_name == g_sect_name_dwarf_apple_objc)
1208 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001209 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001210 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +00001211 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +00001212 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +00001213 else if (section_name == g_sect_name_eh_frame)
1214 sect_type = eSectionTypeEHFrame;
1215 else if (section_name == g_sect_name_cfstring)
1216 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +00001217 else if (section_name == g_sect_name_objc_data ||
1218 section_name == g_sect_name_objc_classrefs ||
1219 section_name == g_sect_name_objc_superrefs ||
1220 section_name == g_sect_name_objc_const ||
1221 section_name == g_sect_name_objc_classlist)
1222 {
1223 sect_type = eSectionTypeDataPointers;
1224 }
Chris Lattner24943d22010-06-08 16:52:24 +00001225
1226 if (sect_type == eSectionTypeOther)
1227 {
1228 switch (mach_sect_type)
1229 {
1230 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +00001231 case SectionTypeRegular:
1232 if (segment_sp->GetName() == g_sect_name_TEXT)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001233 sect_type = eSectionTypeCode;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001234 else if (segment_sp->GetName() == g_sect_name_DATA)
Jason Molenda9badb6c2013-03-06 23:19:17 +00001235 sect_type = eSectionTypeData;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001236 else
Jason Molenda9badb6c2013-03-06 23:19:17 +00001237 sect_type = eSectionTypeOther;
Greg Clayton3fed8b92010-10-08 00:21:05 +00001238 break;
Greg Clayton1674b122010-07-21 22:12:05 +00001239 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
1240 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1241 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1242 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1243 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1244 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1245 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1246 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1247 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1248 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1249 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
1250 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
1251 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1252 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1253 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
1254 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001255 default: break;
1256 }
1257 }
1258
Greg Clayton3508c382012-02-24 01:59:29 +00001259 SectionSP section_sp(new Section (segment_sp,
1260 module_sp,
1261 ++sectID,
1262 section_name,
1263 sect_type,
1264 sect64.addr - segment_sp->GetFileAddress(),
1265 sect64.size,
1266 sect64.offset,
1267 sect64.offset == 0 ? 0 : sect64.size,
1268 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +00001269 // Set the section to be encrypted to match the segment
Jason Molenda9badb6c2013-03-06 23:19:17 +00001270
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001271 bool section_is_encrypted = false;
1272 if (!segment_is_encrypted && load_cmd.filesize != 0)
1273 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
Greg Clayton68ca8232011-01-25 02:58:48 +00001274
Greg Clayton6f7f8da2012-04-24 03:06:13 +00001275 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +00001276 segment_sp->GetChildren().AddSection(section_sp);
1277
1278 if (segment_sp->IsFake())
1279 {
1280 segment_sp.reset();
1281 segment_name.Clear();
1282 }
1283 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001284 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001285 {
1286 if (first_segment_sectID <= sectID)
1287 {
1288 lldb::user_id_t sect_uid;
1289 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
1290 {
1291 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1292 SectionSP next_section_sp;
1293 if (sect_uid + 1 <= sectID)
1294 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1295
1296 if (curr_section_sp.get())
1297 {
1298 if (curr_section_sp->GetByteSize() == 0)
1299 {
1300 if (next_section_sp.get() != NULL)
1301 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1302 else
1303 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1304 }
1305 }
1306 }
1307 }
1308 }
1309 }
1310 }
1311 }
Greg Clayton1674b122010-07-21 22:12:05 +00001312 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +00001313 {
1314 m_dysymtab.cmd = load_cmd.cmd;
1315 m_dysymtab.cmdsize = load_cmd.cmdsize;
1316 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1317 }
1318
1319 offset = load_cmd_offset + load_cmd.cmdsize;
1320 }
1321// if (dump_sections)
1322// {
1323// StreamFile s(stdout);
1324// m_sections_ap->Dump(&s, true);
1325// }
1326 return sectID; // Return the number of sections we registered with the module
1327}
1328
1329class MachSymtabSectionInfo
1330{
1331public:
1332
1333 MachSymtabSectionInfo (SectionList *section_list) :
1334 m_section_list (section_list),
1335 m_section_infos()
1336 {
1337 // Get the number of sections down to a depth of 1 to include
1338 // all segments and their sections, but no other sections that
1339 // may be added for debug map or
1340 m_section_infos.resize(section_list->GetNumSections(1));
1341 }
1342
1343
Greg Clayton3508c382012-02-24 01:59:29 +00001344 SectionSP
Chris Lattner24943d22010-06-08 16:52:24 +00001345 GetSection (uint8_t n_sect, addr_t file_addr)
1346 {
1347 if (n_sect == 0)
Greg Clayton3508c382012-02-24 01:59:29 +00001348 return SectionSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001349 if (n_sect < m_section_infos.size())
1350 {
Greg Clayton3508c382012-02-24 01:59:29 +00001351 if (!m_section_infos[n_sect].section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001352 {
Greg Clayton3508c382012-02-24 01:59:29 +00001353 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
1354 m_section_infos[n_sect].section_sp = section_sp;
Sean Callananb386d822012-08-09 00:50:26 +00001355 if (section_sp)
Greg Clayton5638d2c2011-07-10 17:32:33 +00001356 {
Greg Clayton3508c382012-02-24 01:59:29 +00001357 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
1358 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Clayton5638d2c2011-07-10 17:32:33 +00001359 }
1360 else
1361 {
Greg Claytondf6dc882012-01-05 03:57:59 +00001362 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +00001363 }
Chris Lattner24943d22010-06-08 16:52:24 +00001364 }
1365 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +00001366 {
1367 // Symbol is in section.
Greg Clayton3508c382012-02-24 01:59:29 +00001368 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001369 }
1370 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
1371 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
1372 {
1373 // Symbol is in section with zero size, but has the same start
1374 // address as the section. This can happen with linker symbols
1375 // (symbols that start with the letter 'l' or 'L'.
Greg Clayton3508c382012-02-24 01:59:29 +00001376 return m_section_infos[n_sect].section_sp;
Greg Clayton811b9c52011-08-26 20:01:35 +00001377 }
Chris Lattner24943d22010-06-08 16:52:24 +00001378 }
Greg Clayton3508c382012-02-24 01:59:29 +00001379 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001380 }
1381
1382protected:
1383 struct SectionInfo
1384 {
1385 SectionInfo () :
1386 vm_range(),
Greg Clayton3508c382012-02-24 01:59:29 +00001387 section_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +00001388 {
1389 }
1390
1391 VMRange vm_range;
Greg Clayton3508c382012-02-24 01:59:29 +00001392 SectionSP section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001393 };
1394 SectionList *m_section_list;
1395 std::vector<SectionInfo> m_section_infos;
1396};
1397
Chris Lattner24943d22010-06-08 16:52:24 +00001398size_t
1399ObjectFileMachO::ParseSymtab (bool minimize)
1400{
1401 Timer scoped_timer(__PRETTY_FUNCTION__,
1402 "ObjectFileMachO::ParseSymtab () module = %s",
1403 m_file.GetFilename().AsCString(""));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001404 ModuleSP module_sp (GetModule());
1405 if (!module_sp)
1406 return 0;
1407
1408 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
1409 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
1410 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
1411 FunctionStarts function_starts;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001412 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner24943d22010-06-08 16:52:24 +00001413 uint32_t i;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001414
Greg Clayton952e9dc2013-03-27 23:08:40 +00001415 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton0fea0512011-12-30 00:32:24 +00001416
Chris Lattner24943d22010-06-08 16:52:24 +00001417 for (i=0; i<m_header.ncmds; ++i)
1418 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001419 const lldb::offset_t cmd_offset = offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001420 // Read in the load command and load command size
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001421 struct load_command lc;
1422 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001423 break;
1424 // Watch for the symbol table load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001425 switch (lc.cmd)
Chris Lattner24943d22010-06-08 16:52:24 +00001426 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001427 case LoadCommandSymtab:
1428 symtab_load_command.cmd = lc.cmd;
1429 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00001430 // Read in the rest of the symtab load command
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001431 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
1432 return 0;
1433 if (symtab_load_command.symoff == 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001434 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001435 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001436 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001437 return 0;
1438 }
1439
1440 if (symtab_load_command.stroff == 0)
1441 {
1442 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001443 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001444 return 0;
1445 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001446
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001447 if (symtab_load_command.nsyms == 0)
1448 {
1449 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001450 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001451 return 0;
1452 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001453
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001454 if (symtab_load_command.strsize == 0)
1455 {
1456 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001457 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001458 return 0;
1459 }
1460 break;
1461
1462 case LoadCommandFunctionStarts:
1463 function_starts_load_command.cmd = lc.cmd;
1464 function_starts_load_command.cmdsize = lc.cmdsize;
1465 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
1466 bzero (&function_starts_load_command, sizeof(function_starts_load_command));
1467 break;
1468
1469 default:
1470 break;
1471 }
1472 offset = cmd_offset + lc.cmdsize;
1473 }
1474
1475 if (symtab_load_command.cmd)
1476 {
1477 Symtab *symtab = m_symtab_ap.get();
1478 SectionList *section_list = GetSectionList();
1479 if (section_list == NULL)
1480 return 0;
1481
1482 ProcessSP process_sp (m_process_wp.lock());
Greg Claytondd29b972012-05-18 23:20:01 +00001483 Process *process = process_sp.get();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001484
Greg Clayton36da2aa2013-01-25 18:06:21 +00001485 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1486 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001487 bool bit_width_32 = addr_byte_size == 4;
1488 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
1489
Greg Clayton36da2aa2013-01-25 18:06:21 +00001490 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
1491 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
1492 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001493 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda9badb6c2013-03-06 23:19:17 +00001494
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001495 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
1496 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Claytondd29b972012-05-18 23:20:01 +00001497 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
1498 if (process)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001499 {
Greg Claytondd29b972012-05-18 23:20:01 +00001500 Target &target = process->GetTarget();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001501 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
1502 // Reading mach file from memory in a process or core file...
1503
1504 if (linkedit_section_sp)
1505 {
1506 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
1507 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
1508 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Claytondd29b972012-05-18 23:20:01 +00001509 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton29021d32012-04-18 05:19:20 +00001510
1511 bool data_was_read = false;
1512
1513#if defined (__APPLE__) && defined (__arm__)
1514 if (m_header.flags & 0x80000000u)
Greg Clayton0fea0512011-12-30 00:32:24 +00001515 {
Greg Clayton29021d32012-04-18 05:19:20 +00001516 // This mach-o memory file is in the dyld shared cache. If this
1517 // program is not remote and this is iOS, then this process will
1518 // share the same shared cache as the process we are debugging and
1519 // we can read the entire __LINKEDIT from the address space in this
1520 // process. This is a needed optimization that is used for local iOS
1521 // debugging only since all shared libraries in the shared cache do
1522 // not have corresponding files that exist in the file system of the
1523 // device. They have been combined into a single file. This means we
1524 // always have to load these files from memory. All of the symbol and
1525 // string tables from all of the __LINKEDIT sections from the shared
1526 // libraries in the shared cache have been merged into a single large
1527 // symbol and string table. Reading all of this symbol and string table
1528 // data across can slow down debug launch times, so we optimize this by
1529 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda45c75502013-04-16 06:24:42 +00001530
1531 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
1532 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
1533 bool use_lldb_cache = true;
1534 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
1535 {
1536 use_lldb_cache = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001537 ModuleSP module_sp (GetModule());
1538 if (module_sp)
1539 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
1540
Jason Molenda45c75502013-04-16 06:24:42 +00001541 }
1542
Greg Clayton29021d32012-04-18 05:19:20 +00001543 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda45c75502013-04-16 06:24:42 +00001544 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton29021d32012-04-18 05:19:20 +00001545 {
1546 data_was_read = true;
1547 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Claytondd29b972012-05-18 23:20:01 +00001548 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton29021d32012-04-18 05:19:20 +00001549 if (function_starts_load_command.cmd)
1550 {
1551 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1552 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
1553 }
1554 }
1555 }
1556#endif
1557
1558 if (!data_was_read)
1559 {
1560 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
1561 if (nlist_data_sp)
1562 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
Greg Claytondd29b972012-05-18 23:20:01 +00001563 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
1564 //if (strtab_data_sp)
1565 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
Jason Molenda0bf22382013-02-05 22:31:24 +00001566 if (m_dysymtab.nindirectsyms != 0)
1567 {
1568 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
1569 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
1570 if (indirect_syms_data_sp)
1571 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
1572 }
Greg Clayton29021d32012-04-18 05:19:20 +00001573 if (function_starts_load_command.cmd)
1574 {
1575 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
1576 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
1577 if (func_start_data_sp)
1578 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
1579 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001580 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001581 }
1582 }
1583 else
1584 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001585 nlist_data.SetData (m_data,
1586 symtab_load_command.symoff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001587 nlist_data_byte_size);
1588 strtab_data.SetData (m_data,
Jason Molenda9badb6c2013-03-06 23:19:17 +00001589 symtab_load_command.stroff,
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001590 strtab_data_byte_size);
Jason Molenda0bf22382013-02-05 22:31:24 +00001591 if (m_dysymtab.nindirectsyms != 0)
1592 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00001593 indirect_symbol_index_data.SetData (m_data,
1594 m_dysymtab.indirectsymoff,
Jason Molenda0bf22382013-02-05 22:31:24 +00001595 m_dysymtab.nindirectsyms * 4);
1596 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001597 if (function_starts_load_command.cmd)
1598 {
1599 function_starts_data.SetData (m_data,
1600 function_starts_load_command.dataoff,
1601 function_starts_load_command.datasize);
1602 }
1603 }
Greg Clayton0fea0512011-12-30 00:32:24 +00001604
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001605 if (nlist_data.GetByteSize() == 0)
1606 {
1607 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001608 module_sp->LogMessage(log, "failed to read nlist data");
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001609 return 0;
1610 }
1611
1612
Greg Clayton3a5dc012012-05-25 17:04:00 +00001613 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
1614 if (!have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00001615 {
Greg Clayton3a5dc012012-05-25 17:04:00 +00001616 if (process)
1617 {
1618 if (strtab_addr == LLDB_INVALID_ADDRESS)
1619 {
1620 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001621 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Clayton3a5dc012012-05-25 17:04:00 +00001622 return 0;
1623 }
1624 }
1625 else
Greg Claytondd29b972012-05-18 23:20:01 +00001626 {
1627 if (log)
Greg Clayton952e9dc2013-03-27 23:08:40 +00001628 module_sp->LogMessage(log, "failed to read strtab data");
Greg Claytondd29b972012-05-18 23:20:01 +00001629 return 0;
1630 }
1631 }
Greg Claytondd29b972012-05-18 23:20:01 +00001632
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001633 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
1634 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
1635 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
1636 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
1637 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
1638 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
1639 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
1640 SectionSP eh_frame_section_sp;
1641 if (text_section_sp.get())
1642 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
1643 else
1644 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
1645
Greg Claytond2653c22012-03-14 01:53:24 +00001646 const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM);
Jason Molendad7938392013-03-21 03:36:01 +00001647
1648 // lldb works best if it knows the start addresss of all functions in a module.
1649 // Linker symbols or debug info are normally the best source of information for start addr / size but
1650 // they may be stripped in a released binary.
Jason Molenda2120aef2013-04-16 00:18:44 +00001651 // Two additional sources of information exist in Mach-O binaries:
Jason Molendad7938392013-03-21 03:36:01 +00001652 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
1653 // binary, relative to the text section.
1654 // eh_frame - the eh_frame FDEs have the start addr & size of each function
1655 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
1656 // Binaries built to run on older releases may need to use eh_frame information.
1657
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001658 if (text_section_sp && function_starts_data.GetByteSize())
1659 {
1660 FunctionStarts::Entry function_start_entry;
1661 function_start_entry.data = false;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001662 lldb::offset_t function_start_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001663 function_start_entry.addr = text_section_sp->GetFileAddress();
1664 uint64_t delta;
1665 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
1666 {
1667 // Now append the current entry
1668 function_start_entry.addr += delta;
1669 function_starts.Append(function_start_entry);
1670 }
Jason Molenda2120aef2013-04-16 00:18:44 +00001671 }
Jason Molendad7938392013-03-21 03:36:01 +00001672 else
1673 {
Jason Molenda825a96a2013-03-22 00:38:45 +00001674 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
1675 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
1676 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
1677 // the module.
1678 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molendad7938392013-03-21 03:36:01 +00001679 {
1680 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true);
1681 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
1682 eh_frame.GetFunctionAddressAndSizeVector (functions);
1683 addr_t text_base_addr = text_section_sp->GetFileAddress();
1684 size_t count = functions.GetSize();
1685 for (size_t i = 0; i < count; ++i)
1686 {
1687 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
1688 if (func)
1689 {
1690 FunctionStarts::Entry function_start_entry;
1691 function_start_entry.addr = func->base - text_base_addr;
1692 function_starts.Append(function_start_entry);
1693 }
1694 }
1695 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001696 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00001697
Greg Clayton36da2aa2013-01-25 18:06:21 +00001698 const size_t function_starts_count = function_starts.GetSize();
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001699
Greg Clayton36da2aa2013-01-25 18:06:21 +00001700 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 +00001701
Greg Clayton36da2aa2013-01-25 18:06:21 +00001702 lldb::offset_t nlist_data_offset = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001703
1704 uint32_t N_SO_index = UINT32_MAX;
1705
1706 MachSymtabSectionInfo section_info (section_list);
1707 std::vector<uint32_t> N_FUN_indexes;
1708 std::vector<uint32_t> N_NSYM_indexes;
1709 std::vector<uint32_t> N_INCL_indexes;
1710 std::vector<uint32_t> N_BRAC_indexes;
1711 std::vector<uint32_t> N_COMM_indexes;
1712 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
1713 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytona8364e92013-05-14 22:19:37 +00001714 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001715 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
1716 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytona8364e92013-05-14 22:19:37 +00001717 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001718 // Any symbols that get merged into another will get an entry
1719 // in this map so we know
1720 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
1721 uint32_t nlist_idx = 0;
1722 Symbol *symbol_ptr = NULL;
1723
1724 uint32_t sym_idx = 0;
Jason Molendab62abd52012-06-21 01:51:02 +00001725 Symbol *sym = NULL;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001726 size_t num_syms = 0;
Greg Claytondd29b972012-05-18 23:20:01 +00001727 std::string memory_symbol_name;
Jason Molendab62abd52012-06-21 01:51:02 +00001728 uint32_t unmapped_local_symbols_found = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00001729
Jason Molendab62abd52012-06-21 01:51:02 +00001730#if defined (__APPLE__) && defined (__arm__)
1731
1732 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
1733 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
1734 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
1735 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
1736 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
1737 // nlist parser to ignore all LOCAL symbols.
1738
1739 if (m_header.flags & 0x80000000u)
1740 {
1741 // Before we can start mapping the DSC, we need to make certain the target process is actually
1742 // using the cache we can find.
1743
Jason Molendab62abd52012-06-21 01:51:02 +00001744 // Next we need to determine the correct path for the dyld shared cache.
1745
1746 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
1747 char dsc_path[PATH_MAX];
1748
1749 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda9badb6c2013-03-06 23:19:17 +00001750 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
1751 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendab62abd52012-06-21 01:51:02 +00001752 header_arch.GetArchitectureName());
1753
1754 FileSpec dsc_filespec(dsc_path, false);
1755
1756 // We need definitions of two structures in the on-disk DSC, copy them here manually
Jason Molenda2120aef2013-04-16 00:18:44 +00001757 struct lldb_copy_dyld_cache_header_v0
Greg Claytonab77dcb2012-09-05 22:30:51 +00001758 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001759 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1760 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1761 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
Jason Molenda9badb6c2013-03-06 23:19:17 +00001762 uint32_t imagesOffset;
1763 uint32_t imagesCount;
1764 uint64_t dyldBaseAddress;
1765 uint64_t codeSignatureOffset;
1766 uint64_t codeSignatureSize;
1767 uint64_t slideInfoOffset;
1768 uint64_t slideInfoSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001769 uint64_t localSymbolsOffset; // file offset of where local symbols are stored
1770 uint64_t localSymbolsSize; // size of local symbols information
1771 };
1772 struct lldb_copy_dyld_cache_header_v1
1773 {
1774 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
1775 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
1776 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
1777 uint32_t imagesOffset;
1778 uint32_t imagesCount;
1779 uint64_t dyldBaseAddress;
1780 uint64_t codeSignatureOffset;
1781 uint64_t codeSignatureSize;
1782 uint64_t slideInfoOffset;
1783 uint64_t slideInfoSize;
Jason Molenda9badb6c2013-03-06 23:19:17 +00001784 uint64_t localSymbolsOffset;
1785 uint64_t localSymbolsSize;
Jason Molenda2120aef2013-04-16 00:18:44 +00001786 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
Greg Claytonab77dcb2012-09-05 22:30:51 +00001787 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001788
Jason Molenda2120aef2013-04-16 00:18:44 +00001789 struct lldb_copy_dyld_cache_mapping_info
1790 {
1791 uint64_t address;
1792 uint64_t size;
1793 uint64_t fileOffset;
1794 uint32_t maxProt;
1795 uint32_t initProt;
1796 };
Jason Molenda6bcabae2013-03-06 23:17:36 +00001797
Greg Claytonab77dcb2012-09-05 22:30:51 +00001798 struct lldb_copy_dyld_cache_local_symbols_info
1799 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001800 uint32_t nlistOffset;
1801 uint32_t nlistCount;
1802 uint32_t stringsOffset;
1803 uint32_t stringsSize;
1804 uint32_t entriesOffset;
1805 uint32_t entriesCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001806 };
1807 struct lldb_copy_dyld_cache_local_symbols_entry
1808 {
Jason Molenda2120aef2013-04-16 00:18:44 +00001809 uint32_t dylibOffset;
1810 uint32_t nlistStartIndex;
1811 uint32_t nlistCount;
Greg Claytonab77dcb2012-09-05 22:30:51 +00001812 };
Jason Molendab62abd52012-06-21 01:51:02 +00001813
Jason Molendafd3b35d2012-06-22 03:28:35 +00001814 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
1815 The dyld_cache_local_symbols_info structure gives us three things:
1816 1. The start and count of the nlist records in the dyld_shared_cache file
1817 2. The start and size of the strings for these nlist records
1818 3. The start and count of dyld_cache_local_symbols_entry entries
1819
1820 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
1821 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda9badb6c2013-03-06 23:19:17 +00001822 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendafd3b35d2012-06-22 03:28:35 +00001823 and the count of how many nlist records there are for this dylib/framework.
1824 */
1825
Jason Molendab62abd52012-06-21 01:51:02 +00001826 // Process the dsc header to find the unmapped symbols
1827 //
1828 // Save some VM space, do not map the entire cache in one shot.
1829
Jason Molenda6bcabae2013-03-06 23:17:36 +00001830 DataBufferSP dsc_data_sp;
1831 dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
1832
1833 if (dsc_data_sp)
Jason Molendab62abd52012-06-21 01:51:02 +00001834 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001835 DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001836
Jason Molenda6bcabae2013-03-06 23:17:36 +00001837 char version_str[17];
1838 int version = -1;
1839 lldb::offset_t offset = 0;
1840 memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16);
1841 version_str[16] = '\0';
1842 if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6]))
1843 {
1844 int v;
1845 if (::sscanf (version_str + 6, "%d", &v) == 1)
1846 {
1847 version = v;
1848 }
1849 }
1850
Jason Molenda45c75502013-04-16 06:24:42 +00001851 UUID dsc_uuid;
1852 if (version >= 1)
1853 {
1854 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
1855 uint8_t uuid_bytes[sizeof (uuid_t)];
1856 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
1857 dsc_uuid.SetBytes (uuid_bytes);
1858 }
1859
1860 bool uuid_match = true;
1861 if (dsc_uuid.IsValid() && process)
1862 {
1863 UUID shared_cache_uuid(GetProcessSharedCacheUUID(process));
1864
1865 if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid)
1866 {
1867 // The on-disk dyld_shared_cache file is not the same as the one in this
1868 // process' memory, don't use it.
1869 uuid_match = false;
Jason Molenda6ff916f2013-04-16 21:42:58 +00001870 ModuleSP module_sp (GetModule());
1871 if (module_sp)
1872 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 +00001873 }
1874 }
1875
Jason Molenda9badb6c2013-03-06 23:19:17 +00001876 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda6bcabae2013-03-06 23:17:36 +00001877
Jason Molendab62abd52012-06-21 01:51:02 +00001878 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
1879
1880 // If the mappingOffset points to a location inside the header, we've
1881 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda45c75502013-04-16 06:24:42 +00001882 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0))
Jason Molendab62abd52012-06-21 01:51:02 +00001883 {
1884
Jason Molenda6bcabae2013-03-06 23:17:36 +00001885 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
1886 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
1887 offset = 0;
1888
1889 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
1890 // in the shared library cache need to be adjusted by an offset to match up with the
1891 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
1892 // recorded in mapping_offset_value.
1893 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
1894
1895 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendab62abd52012-06-21 01:51:02 +00001896 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
1897 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
1898
Jason Molenda9badb6c2013-03-06 23:19:17 +00001899 if (localSymbolsOffset && localSymbolsSize)
Jason Molendab62abd52012-06-21 01:51:02 +00001900 {
1901 // Map the local symbols
Jason Molenda9badb6c2013-03-06 23:19:17 +00001902 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize))
Jason Molendab62abd52012-06-21 01:51:02 +00001903 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001904 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendab62abd52012-06-21 01:51:02 +00001905
1906 offset = 0;
1907
1908 // Read the local_symbols_infos struct in one shot
1909 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
1910 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
1911
Jason Molendab62abd52012-06-21 01:51:02 +00001912 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
1913
Jason Molenda6bcabae2013-03-06 23:17:36 +00001914 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendab62abd52012-06-21 01:51:02 +00001915
1916 offset = local_symbols_info.entriesOffset;
1917 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
1918 {
1919 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
1920 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
1921 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
1922 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
1923
Jason Molenda9badb6c2013-03-06 23:19:17 +00001924 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendab62abd52012-06-21 01:51:02 +00001925 {
1926 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
1927
1928 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
1929 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
1930 num_syms = symtab->GetNumSymbols();
1931
1932 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
1933 uint32_t string_table_offset = local_symbols_info.stringsOffset;
1934
Jason Molenda9badb6c2013-03-06 23:19:17 +00001935 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendab62abd52012-06-21 01:51:02 +00001936 {
1937 /////////////////////////////
1938 {
1939 struct nlist_64 nlist;
1940 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
1941 break;
1942
1943 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
1944 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1945 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
1946 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
1947 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
1948
1949 SymbolType type = eSymbolTypeInvalid;
1950 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
1951
1952 if (symbol_name == NULL)
1953 {
1954 // No symbol should be NULL, even the symbols with no
1955 // string values should have an offset zero which points
1956 // to an empty C-string
1957 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00001958 "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 +00001959 entry_index,
1960 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00001961 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendab62abd52012-06-21 01:51:02 +00001962 continue;
1963 }
1964 if (symbol_name[0] == '\0')
1965 symbol_name = NULL;
1966
1967 const char *symbol_name_non_abi_mangled = NULL;
1968
1969 SectionSP symbol_section;
1970 uint32_t symbol_byte_size = 0;
1971 bool add_nlist = true;
1972 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00001973 bool demangled_is_synthesized = false;
Greg Claytona8364e92013-05-14 22:19:37 +00001974 bool is_gsym = false;
Jason Molendab62abd52012-06-21 01:51:02 +00001975
1976 assert (sym_idx < num_syms);
1977
1978 sym[sym_idx].SetDebug (is_debug);
1979
1980 if (is_debug)
1981 {
1982 switch (nlist.n_type)
1983 {
1984 case StabGlobalSymbol:
1985 // N_GSYM -- global symbol: name,,NO_SECT,type,0
1986 // Sometimes the N_GSYM value contains the address.
1987
1988 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1989 // have the same address, but we want to ensure that we always find only the real symbol,
1990 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1991 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1992 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1993 // same address.
1994
1995 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1996 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1997 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1998 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1999 add_nlist = false;
2000 else
2001 {
Greg Claytona8364e92013-05-14 22:19:37 +00002002 is_gsym = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002003 sym[sym_idx].SetExternal(true);
2004 if (nlist.n_value != 0)
2005 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2006 type = eSymbolTypeData;
2007 }
2008 break;
2009
2010 case StabFunctionName:
2011 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2012 type = eSymbolTypeCompiler;
2013 break;
2014
2015 case StabFunction:
2016 // N_FUN -- procedure: name,,n_sect,linenumber,address
2017 if (symbol_name)
2018 {
2019 type = eSymbolTypeCode;
2020 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2021
2022 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2023 // We use the current number of symbols in the symbol table in lieu of
2024 // using nlist_idx in case we ever start trimming entries out
2025 N_FUN_indexes.push_back(sym_idx);
2026 }
2027 else
2028 {
2029 type = eSymbolTypeCompiler;
2030
2031 if ( !N_FUN_indexes.empty() )
2032 {
2033 // Copy the size of the function into the original STAB entry so we don't have
2034 // to hunt for it later
2035 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2036 N_FUN_indexes.pop_back();
2037 // We don't really need the end function STAB as it contains the size which
2038 // we already placed with the original symbol, so don't add it if we want a
2039 // minimal symbol table
2040 if (minimize)
2041 add_nlist = false;
2042 }
2043 }
2044 break;
2045
2046 case StabStaticSymbol:
2047 // N_STSYM -- static symbol: name,,n_sect,type,address
2048 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2049 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2050 type = eSymbolTypeData;
2051 break;
2052
2053 case StabLocalCommon:
2054 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2055 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2056 type = eSymbolTypeCommonBlock;
2057 break;
2058
2059 case StabBeginSymbol:
2060 // N_BNSYM
2061 // We use the current number of symbols in the symbol table in lieu of
2062 // using nlist_idx in case we ever start trimming entries out
2063 if (minimize)
2064 {
2065 // Skip these if we want minimal symbol tables
2066 add_nlist = false;
2067 }
2068 else
2069 {
2070 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2071 N_NSYM_indexes.push_back(sym_idx);
2072 type = eSymbolTypeScopeBegin;
2073 }
2074 break;
2075
2076 case StabEndSymbol:
2077 // N_ENSYM
2078 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2079 // so that we can always skip the entire symbol if we need to navigate
2080 // more quickly at the source level when parsing STABS
2081 if (minimize)
2082 {
2083 // Skip these if we want minimal symbol tables
2084 add_nlist = false;
2085 }
2086 else
2087 {
2088 if ( !N_NSYM_indexes.empty() )
2089 {
2090 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2091 symbol_ptr->SetByteSize(sym_idx + 1);
2092 symbol_ptr->SetSizeIsSibling(true);
2093 N_NSYM_indexes.pop_back();
2094 }
2095 type = eSymbolTypeScopeEnd;
2096 }
2097 break;
2098
2099
2100 case StabSourceFileOptions:
2101 // N_OPT - emitted with gcc2_compiled and in gcc source
2102 type = eSymbolTypeCompiler;
2103 break;
2104
2105 case StabRegisterSymbol:
2106 // N_RSYM - register sym: name,,NO_SECT,type,register
2107 type = eSymbolTypeVariable;
2108 break;
2109
2110 case StabSourceLine:
2111 // N_SLINE - src line: 0,,n_sect,linenumber,address
2112 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2113 type = eSymbolTypeLineEntry;
2114 break;
2115
2116 case StabStructureType:
2117 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2118 type = eSymbolTypeVariableType;
2119 break;
2120
2121 case StabSourceFileName:
2122 // N_SO - source file name
2123 type = eSymbolTypeSourceFile;
2124 if (symbol_name == NULL)
2125 {
2126 if (minimize)
2127 add_nlist = false;
2128 if (N_SO_index != UINT32_MAX)
2129 {
2130 // Set the size of the N_SO to the terminating index of this N_SO
2131 // so that we can always skip the entire N_SO if we need to navigate
2132 // more quickly at the source level when parsing STABS
2133 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2134 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2135 symbol_ptr->SetSizeIsSibling(true);
2136 }
2137 N_NSYM_indexes.clear();
2138 N_INCL_indexes.clear();
2139 N_BRAC_indexes.clear();
2140 N_COMM_indexes.clear();
2141 N_FUN_indexes.clear();
2142 N_SO_index = UINT32_MAX;
2143 }
2144 else
2145 {
2146 // We use the current number of symbols in the symbol table in lieu of
2147 // using nlist_idx in case we ever start trimming entries out
2148 const bool N_SO_has_full_path = symbol_name[0] == '/';
2149 if (N_SO_has_full_path)
2150 {
2151 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2152 {
2153 // We have two consecutive N_SO entries where the first contains a directory
2154 // and the second contains a full path.
Jason Molenda292cca82012-07-20 03:35:44 +00002155 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002156 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2157 add_nlist = false;
2158 }
2159 else
2160 {
2161 // This is the first entry in a N_SO that contains a directory or
2162 // a full path to the source file
2163 N_SO_index = sym_idx;
2164 }
2165 }
2166 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2167 {
2168 // This is usually the second N_SO entry that contains just the filename,
2169 // so here we combine it with the first one if we are minimizing the symbol table
2170 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2171 if (so_path && so_path[0])
2172 {
2173 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002174 const size_t double_slash_pos = full_so_path.find("//");
2175 if (double_slash_pos != std::string::npos)
2176 {
2177 // The linker has been generating bad N_SO entries with doubled up paths
2178 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2179 // and the second is the directory for the source file so you end up with
2180 // a path that looks like "/tmp/src//tmp/src/"
2181 FileSpec so_dir(so_path, false);
2182 if (!so_dir.Exists())
2183 {
2184 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2185 if (so_dir.Exists())
2186 {
2187 // Trim off the incorrect path
2188 full_so_path.erase(0, double_slash_pos + 1);
2189 }
2190 }
2191 }
Jason Molendab62abd52012-06-21 01:51:02 +00002192 if (*full_so_path.rbegin() != '/')
2193 full_so_path += '/';
2194 full_so_path += symbol_name;
Jason Molenda292cca82012-07-20 03:35:44 +00002195 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendab62abd52012-06-21 01:51:02 +00002196 add_nlist = false;
2197 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2198 }
2199 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002200 else
2201 {
2202 // This could be a relative path to a N_SO
2203 N_SO_index = sym_idx;
2204 }
Jason Molendab62abd52012-06-21 01:51:02 +00002205 }
Jason Molendab62abd52012-06-21 01:51:02 +00002206 break;
2207
2208 case StabObjectFileName:
2209 // N_OSO - object file name: name,,0,0,st_mtime
2210 type = eSymbolTypeObjectFile;
2211 break;
2212
2213 case StabLocalSymbol:
2214 // N_LSYM - local sym: name,,NO_SECT,type,offset
2215 type = eSymbolTypeLocal;
2216 break;
2217
2218 //----------------------------------------------------------------------
2219 // INCL scopes
2220 //----------------------------------------------------------------------
2221 case StabBeginIncludeFileName:
2222 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2223 // We use the current number of symbols in the symbol table in lieu of
2224 // using nlist_idx in case we ever start trimming entries out
2225 N_INCL_indexes.push_back(sym_idx);
2226 type = eSymbolTypeScopeBegin;
2227 break;
2228
2229 case StabEndIncludeFile:
2230 // N_EINCL - include file end: name,,NO_SECT,0,0
2231 // Set the size of the N_BINCL to the terminating index of this N_EINCL
2232 // so that we can always skip the entire symbol if we need to navigate
2233 // more quickly at the source level when parsing STABS
2234 if ( !N_INCL_indexes.empty() )
2235 {
2236 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
2237 symbol_ptr->SetByteSize(sym_idx + 1);
2238 symbol_ptr->SetSizeIsSibling(true);
2239 N_INCL_indexes.pop_back();
2240 }
2241 type = eSymbolTypeScopeEnd;
2242 break;
2243
2244 case StabIncludeFileName:
2245 // N_SOL - #included file name: name,,n_sect,0,address
2246 type = eSymbolTypeHeaderFile;
2247
2248 // We currently don't use the header files on darwin
2249 if (minimize)
2250 add_nlist = false;
2251 break;
2252
2253 case StabCompilerParameters:
2254 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
2255 type = eSymbolTypeCompiler;
2256 break;
2257
2258 case StabCompilerVersion:
2259 // N_VERSION - compiler version: name,,NO_SECT,0,0
2260 type = eSymbolTypeCompiler;
2261 break;
2262
2263 case StabCompilerOptLevel:
2264 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
2265 type = eSymbolTypeCompiler;
2266 break;
2267
2268 case StabParameter:
2269 // N_PSYM - parameter: name,,NO_SECT,type,offset
2270 type = eSymbolTypeVariable;
2271 break;
2272
2273 case StabAlternateEntry:
2274 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
2275 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2276 type = eSymbolTypeLineEntry;
2277 break;
2278
2279 //----------------------------------------------------------------------
2280 // Left and Right Braces
2281 //----------------------------------------------------------------------
2282 case StabLeftBracket:
2283 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
2284 // We use the current number of symbols in the symbol table in lieu of
2285 // using nlist_idx in case we ever start trimming entries out
2286 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2287 N_BRAC_indexes.push_back(sym_idx);
2288 type = eSymbolTypeScopeBegin;
2289 break;
2290
2291 case StabRightBracket:
2292 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
2293 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
2294 // so that we can always skip the entire symbol if we need to navigate
2295 // more quickly at the source level when parsing STABS
2296 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2297 if ( !N_BRAC_indexes.empty() )
2298 {
2299 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
2300 symbol_ptr->SetByteSize(sym_idx + 1);
2301 symbol_ptr->SetSizeIsSibling(true);
2302 N_BRAC_indexes.pop_back();
2303 }
2304 type = eSymbolTypeScopeEnd;
2305 break;
2306
2307 case StabDeletedIncludeFile:
2308 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
2309 type = eSymbolTypeHeaderFile;
2310 break;
2311
2312 //----------------------------------------------------------------------
2313 // COMM scopes
2314 //----------------------------------------------------------------------
2315 case StabBeginCommon:
2316 // N_BCOMM - begin common: name,,NO_SECT,0,0
2317 // We use the current number of symbols in the symbol table in lieu of
2318 // using nlist_idx in case we ever start trimming entries out
2319 type = eSymbolTypeScopeBegin;
2320 N_COMM_indexes.push_back(sym_idx);
2321 break;
2322
2323 case StabEndCommonLocal:
2324 // N_ECOML - end common (local name): 0,,n_sect,0,address
2325 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2326 // Fall through
2327
2328 case StabEndCommon:
2329 // N_ECOMM - end common: name,,n_sect,0,0
2330 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
2331 // so that we can always skip the entire symbol if we need to navigate
2332 // more quickly at the source level when parsing STABS
2333 if ( !N_COMM_indexes.empty() )
2334 {
2335 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
2336 symbol_ptr->SetByteSize(sym_idx + 1);
2337 symbol_ptr->SetSizeIsSibling(true);
2338 N_COMM_indexes.pop_back();
2339 }
2340 type = eSymbolTypeScopeEnd;
2341 break;
2342
2343 case StabLength:
2344 // N_LENG - second stab entry with length information
2345 type = eSymbolTypeAdditional;
2346 break;
2347
2348 default: break;
2349 }
2350 }
2351 else
2352 {
2353 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
2354 uint8_t n_type = NlistMaskType & nlist.n_type;
2355 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
2356
2357 switch (n_type)
2358 {
2359 case NListTypeIndirect: // N_INDR - Fall through
2360 case NListTypePreboundUndefined:// N_PBUD - Fall through
2361 case NListTypeUndefined: // N_UNDF
2362 type = eSymbolTypeUndefined;
2363 break;
2364
2365 case NListTypeAbsolute: // N_ABS
2366 type = eSymbolTypeAbsolute;
2367 break;
2368
2369 case NListTypeSection: // N_SECT
Greg Clayton01e6a582012-11-27 01:52:16 +00002370 {
2371 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendab62abd52012-06-21 01:51:02 +00002372
Greg Clayton01e6a582012-11-27 01:52:16 +00002373 if (symbol_section == NULL)
Jason Molendab62abd52012-06-21 01:51:02 +00002374 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002375 // TODO: warn about this?
2376 add_nlist = false;
2377 break;
Jason Molendab62abd52012-06-21 01:51:02 +00002378 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002379
2380 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendab62abd52012-06-21 01:51:02 +00002381 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002382 type = eSymbolTypeException;
2383 }
2384 else
2385 {
2386 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002387
Greg Clayton01e6a582012-11-27 01:52:16 +00002388 switch (section_type)
Jason Molendab62abd52012-06-21 01:51:02 +00002389 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002390 case SectionTypeRegular: break; // regular section
2391 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
2392 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
2393 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
2394 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
2395 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
2396 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
2397 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
2398 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
2399 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
2400 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
2401 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
2402 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
2403 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
2404 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
2405 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
2406 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
2407 default: break;
Jason Molendab62abd52012-06-21 01:51:02 +00002408 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002409
Greg Clayton01e6a582012-11-27 01:52:16 +00002410 if (type == eSymbolTypeInvalid)
2411 {
2412 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2413 if (symbol_section->IsDescendant (text_section_sp.get()))
2414 {
2415 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
2416 SectionAttrUserSelfModifyingCode |
2417 SectionAttrSytemSomeInstructions))
2418 type = eSymbolTypeData;
2419 else
2420 type = eSymbolTypeCode;
2421 }
2422 else if (symbol_section->IsDescendant(data_section_sp.get()))
Jason Molendab62abd52012-06-21 01:51:02 +00002423 {
2424 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
2425 {
2426 type = eSymbolTypeRuntime;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002427
Greg Clayton01e6a582012-11-27 01:52:16 +00002428 if (symbol_name &&
2429 symbol_name[0] == '_' &&
2430 symbol_name[1] == 'O' &&
Jason Molendab62abd52012-06-21 01:51:02 +00002431 symbol_name[2] == 'B')
2432 {
2433 llvm::StringRef symbol_name_ref(symbol_name);
2434 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2435 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2436 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
2437 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2438 {
2439 symbol_name_non_abi_mangled = symbol_name + 1;
2440 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2441 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002442 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002443 }
2444 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2445 {
2446 symbol_name_non_abi_mangled = symbol_name + 1;
2447 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2448 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00002449 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002450 }
2451 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2452 {
2453 symbol_name_non_abi_mangled = symbol_name + 1;
2454 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2455 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00002456 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002457 }
2458 }
2459 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002460 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendab62abd52012-06-21 01:51:02 +00002461 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002462 type = eSymbolTypeException;
Jason Molendab62abd52012-06-21 01:51:02 +00002463 }
2464 else
Greg Clayton01e6a582012-11-27 01:52:16 +00002465 {
2466 type = eSymbolTypeData;
2467 }
2468 }
2469 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
2470 {
2471 type = eSymbolTypeTrampoline;
2472 }
2473 else if (symbol_section->IsDescendant(objc_section_sp.get()))
2474 {
2475 type = eSymbolTypeRuntime;
2476 if (symbol_name && symbol_name[0] == '.')
2477 {
2478 llvm::StringRef symbol_name_ref(symbol_name);
2479 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
2480 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendab62abd52012-06-21 01:51:02 +00002481 {
Greg Clayton01e6a582012-11-27 01:52:16 +00002482 symbol_name_non_abi_mangled = symbol_name;
2483 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
2484 type = eSymbolTypeObjCClass;
2485 demangled_is_synthesized = true;
Jason Molendab62abd52012-06-21 01:51:02 +00002486 }
Greg Clayton01e6a582012-11-27 01:52:16 +00002487 }
2488 }
2489 }
Jason Molendab62abd52012-06-21 01:51:02 +00002490 }
2491 }
Jason Molendab62abd52012-06-21 01:51:02 +00002492 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002493 }
Jason Molendab62abd52012-06-21 01:51:02 +00002494 }
2495
2496 if (add_nlist)
2497 {
2498 uint64_t symbol_value = nlist.n_value;
2499 bool symbol_name_is_mangled = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002500
Jason Molendab62abd52012-06-21 01:51:02 +00002501 if (symbol_name_non_abi_mangled)
2502 {
Jason Molenda292cca82012-07-20 03:35:44 +00002503 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
2504 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendab62abd52012-06-21 01:51:02 +00002505 }
2506 else
2507 {
2508 if (symbol_name && symbol_name[0] == '_')
2509 {
2510 symbol_name_is_mangled = symbol_name[1] == '_';
2511 symbol_name++; // Skip the leading underscore
2512 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002513
Jason Molendab62abd52012-06-21 01:51:02 +00002514 if (symbol_name)
2515 {
Greg Claytona8364e92013-05-14 22:19:37 +00002516 ConstString const_symbol_name(symbol_name);
2517 if (is_gsym)
2518 N_GSYM_name_to_sym_idx[const_symbol_name.GetCString()] = sym_idx;
2519 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Jason Molendab62abd52012-06-21 01:51:02 +00002520 }
2521 }
2522 if (symbol_section)
2523 {
2524 const addr_t section_file_addr = symbol_section->GetFileAddress();
2525 if (symbol_byte_size == 0 && function_starts_count > 0)
2526 {
2527 addr_t symbol_lookup_file_addr = nlist.n_value;
2528 // Do an exact address match for non-ARM addresses, else get the closest since
2529 // the symbol might be a thumb symbol which has an address with bit zero set
2530 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
2531 if (is_arm && func_start_entry)
2532 {
2533 // Verify that the function start address is the symbol address (ARM)
2534 // or the symbol address + 1 (thumb)
2535 if (func_start_entry->addr != symbol_lookup_file_addr &&
2536 func_start_entry->addr != (symbol_lookup_file_addr + 1))
2537 {
2538 // Not the right entry, NULL it out...
2539 func_start_entry = NULL;
2540 }
2541 }
2542 if (func_start_entry)
2543 {
2544 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002545
Jason Molendab62abd52012-06-21 01:51:02 +00002546 addr_t symbol_file_addr = func_start_entry->addr;
2547 uint32_t symbol_flags = 0;
2548 if (is_arm)
2549 {
2550 if (symbol_file_addr & 1)
2551 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
2552 symbol_file_addr &= 0xfffffffffffffffeull;
2553 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002554
Jason Molendab62abd52012-06-21 01:51:02 +00002555 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
2556 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
2557 if (next_func_start_entry)
2558 {
2559 addr_t next_symbol_file_addr = next_func_start_entry->addr;
2560 // Be sure the clear the Thumb address bit when we calculate the size
2561 // from the current and next address
2562 if (is_arm)
2563 next_symbol_file_addr &= 0xfffffffffffffffeull;
2564 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
2565 }
2566 else
2567 {
2568 symbol_byte_size = section_end_file_addr - symbol_file_addr;
2569 }
2570 }
2571 }
2572 symbol_value -= section_file_addr;
2573 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002574
Greg Claytona8364e92013-05-14 22:19:37 +00002575 if (is_debug == false)
2576 {
2577 if (type == eSymbolTypeCode)
2578 {
2579 // See if we can find a N_FUN entry for any code symbols.
2580 // If we do find a match, and the name matches, then we
2581 // can merge the two into just the function symbol to avoid
2582 // duplicate entries in the symbol table
2583 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
2584 if (pos != N_FUN_addr_to_sym_idx.end())
2585 {
2586 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2587 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2588 {
2589 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2590 // We just need the flags from the linker symbol, so put these flags
2591 // into the N_FUN flags to avoid duplicate symbols in the symbol table
2592 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2593 sym[sym_idx].Clear();
2594 continue;
2595 }
2596 }
2597 }
2598 else if (type == eSymbolTypeData)
2599 {
2600 // See if we can find a N_STSYM entry for any data symbols.
2601 // If we do find a match, and the name matches, then we
2602 // can merge the two into just the Static symbol to avoid
2603 // duplicate entries in the symbol table
2604 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
2605 if (pos != N_STSYM_addr_to_sym_idx.end())
2606 {
2607 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
2608 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
2609 {
2610 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
2611 // We just need the flags from the linker symbol, so put these flags
2612 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2613 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2614 sym[sym_idx].Clear();
2615 continue;
2616 }
2617 }
2618 else
2619 {
2620 // Combine N_GSYM stab entries with the non stab symbol
2621 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetMangledName().GetCString());
2622 if (pos != N_GSYM_name_to_sym_idx.end())
2623 {
2624 const uint32_t GSYM_sym_idx = pos->second;
2625 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
2626 // Copy the address, because often the N_GSYM address has an invalid address of zero
2627 // when the global is a common symbol
2628 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
2629 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
2630 // We just need the flags from the linker symbol, so put these flags
2631 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
2632 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
2633 sym[sym_idx].Clear();
2634 continue;
2635 }
2636 }
2637 }
2638 }
2639
Jason Molendab62abd52012-06-21 01:51:02 +00002640 sym[sym_idx].SetID (nlist_idx);
2641 sym[sym_idx].SetType (type);
2642 sym[sym_idx].GetAddress().SetSection (symbol_section);
2643 sym[sym_idx].GetAddress().SetOffset (symbol_value);
2644 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002645
Jason Molendab62abd52012-06-21 01:51:02 +00002646 if (symbol_byte_size > 0)
2647 sym[sym_idx].SetByteSize(symbol_byte_size);
2648
Greg Clayton01e6a582012-11-27 01:52:16 +00002649 if (demangled_is_synthesized)
2650 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendab62abd52012-06-21 01:51:02 +00002651 ++sym_idx;
2652 }
2653 else
2654 {
2655 sym[sym_idx].Clear();
2656 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002657
Jason Molendab62abd52012-06-21 01:51:02 +00002658 }
2659 /////////////////////////////
2660 }
2661 break; // No more entries to consider
2662 }
2663 }
2664 }
2665 }
2666 }
2667 }
2668 }
2669
2670 // Must reset this in case it was mutated above!
2671 nlist_data_offset = 0;
2672#endif
2673
2674 // If the sym array was not created while parsing the DSC unmapped
2675 // symbols, create it now.
2676 if (sym == NULL)
2677 {
2678 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
2679 num_syms = symtab->GetNumSymbols();
2680 }
2681
2682 if (unmapped_local_symbols_found)
2683 {
2684 assert(m_dysymtab.ilocalsym == 0);
2685 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
2686 nlist_idx = m_dysymtab.nlocalsym;
2687 }
2688 else
2689 {
2690 nlist_idx = 0;
2691 }
2692
2693 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002694 {
2695 struct nlist_64 nlist;
2696 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2697 break;
2698
2699 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
2700 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
2701 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
2702 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
2703 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
2704
2705 SymbolType type = eSymbolTypeInvalid;
Greg Claytondd29b972012-05-18 23:20:01 +00002706 const char *symbol_name = NULL;
Jason Molenda9badb6c2013-03-06 23:19:17 +00002707
Greg Clayton3a5dc012012-05-25 17:04:00 +00002708 if (have_strtab_data)
Greg Claytondd29b972012-05-18 23:20:01 +00002709 {
2710 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002711
Greg Claytondd29b972012-05-18 23:20:01 +00002712 if (symbol_name == NULL)
2713 {
2714 // No symbol should be NULL, even the symbols with no
2715 // string values should have an offset zero which points
2716 // to an empty C-string
2717 Host::SystemLog (Host::eSystemLogError,
Greg Clayton97a19b22013-04-29 17:25:54 +00002718 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Greg Claytondd29b972012-05-18 23:20:01 +00002719 nlist_idx,
2720 nlist.n_strx,
Greg Clayton97a19b22013-04-29 17:25:54 +00002721 module_sp->GetFileSpec().GetPath().c_str());
Greg Claytondd29b972012-05-18 23:20:01 +00002722 continue;
2723 }
2724 if (symbol_name[0] == '\0')
2725 symbol_name = NULL;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002726 }
Greg Clayton3a5dc012012-05-25 17:04:00 +00002727 else
2728 {
2729 const addr_t str_addr = strtab_addr + nlist.n_strx;
2730 Error str_error;
2731 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
2732 symbol_name = memory_symbol_name.c_str();
2733 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002734 const char *symbol_name_non_abi_mangled = NULL;
2735
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002736 SectionSP symbol_section;
Greg Clayton36da2aa2013-01-25 18:06:21 +00002737 lldb::addr_t symbol_byte_size = 0;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002738 bool add_nlist = true;
Greg Claytona8364e92013-05-14 22:19:37 +00002739 bool is_gsym = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002740 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Greg Clayton01e6a582012-11-27 01:52:16 +00002741 bool demangled_is_synthesized = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002742
2743 assert (sym_idx < num_syms);
2744
2745 sym[sym_idx].SetDebug (is_debug);
2746
2747 if (is_debug)
2748 {
2749 switch (nlist.n_type)
Greg Clayton0fea0512011-12-30 00:32:24 +00002750 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00002751 case StabGlobalSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002752 // N_GSYM -- global symbol: name,,NO_SECT,type,0
2753 // Sometimes the N_GSYM value contains the address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002754
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002755 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2756 // have the same address, but we want to ensure that we always find only the real symbol,
2757 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2758 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2759 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2760 // same address.
Jason Molenda9badb6c2013-03-06 23:19:17 +00002761
2762 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002763 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
2764 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
2765 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
2766 add_nlist = false;
2767 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002768 {
Greg Claytona8364e92013-05-14 22:19:37 +00002769 is_gsym = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002770 sym[sym_idx].SetExternal(true);
2771 if (nlist.n_value != 0)
2772 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2773 type = eSymbolTypeData;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002774 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002775 break;
Greg Claytonb5a8f142012-02-05 02:38:54 +00002776
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002777 case StabFunctionName:
2778 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
2779 type = eSymbolTypeCompiler;
2780 break;
Greg Clayton0fea0512011-12-30 00:32:24 +00002781
Jason Molenda9badb6c2013-03-06 23:19:17 +00002782 case StabFunction:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002783 // N_FUN -- procedure: name,,n_sect,linenumber,address
2784 if (symbol_name)
Greg Claytona9c4f312011-10-31 20:50:40 +00002785 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002786 type = eSymbolTypeCode;
2787 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda9badb6c2013-03-06 23:19:17 +00002788
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002789 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
2790 // We use the current number of symbols in the symbol table in lieu of
2791 // using nlist_idx in case we ever start trimming entries out
2792 N_FUN_indexes.push_back(sym_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00002793 }
2794 else
2795 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002796 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00002797
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002798 if ( !N_FUN_indexes.empty() )
Chris Lattner24943d22010-06-08 16:52:24 +00002799 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002800 // Copy the size of the function into the original STAB entry so we don't have
2801 // to hunt for it later
2802 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2803 N_FUN_indexes.pop_back();
2804 // We don't really need the end function STAB as it contains the size which
2805 // we already placed with the original symbol, so don't add it if we want a
2806 // minimal symbol table
2807 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002808 add_nlist = false;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002809 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00002810 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002811 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002812
Jason Molenda9badb6c2013-03-06 23:19:17 +00002813 case StabStaticSymbol:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002814 // N_STSYM -- static symbol: name,,n_sect,type,address
2815 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
2816 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2817 type = eSymbolTypeData;
2818 break;
2819
2820 case StabLocalCommon:
2821 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
2822 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2823 type = eSymbolTypeCommonBlock;
2824 break;
2825
2826 case StabBeginSymbol:
2827 // N_BNSYM
2828 // We use the current number of symbols in the symbol table in lieu of
2829 // using nlist_idx in case we ever start trimming entries out
2830 if (minimize)
Greg Clayton3f69eac2011-12-03 02:30:59 +00002831 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002832 // Skip these if we want minimal symbol tables
2833 add_nlist = false;
2834 }
2835 else
2836 {
2837 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2838 N_NSYM_indexes.push_back(sym_idx);
2839 type = eSymbolTypeScopeBegin;
2840 }
2841 break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00002842
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002843 case StabEndSymbol:
2844 // N_ENSYM
2845 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
2846 // so that we can always skip the entire symbol if we need to navigate
2847 // more quickly at the source level when parsing STABS
2848 if (minimize)
2849 {
2850 // Skip these if we want minimal symbol tables
2851 add_nlist = false;
2852 }
2853 else
2854 {
2855 if ( !N_NSYM_indexes.empty() )
Greg Clayton3f69eac2011-12-03 02:30:59 +00002856 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002857 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
2858 symbol_ptr->SetByteSize(sym_idx + 1);
2859 symbol_ptr->SetSizeIsSibling(true);
2860 N_NSYM_indexes.pop_back();
2861 }
2862 type = eSymbolTypeScopeEnd;
2863 }
2864 break;
2865
2866
2867 case StabSourceFileOptions:
2868 // N_OPT - emitted with gcc2_compiled and in gcc source
2869 type = eSymbolTypeCompiler;
2870 break;
2871
2872 case StabRegisterSymbol:
2873 // N_RSYM - register sym: name,,NO_SECT,type,register
2874 type = eSymbolTypeVariable;
2875 break;
2876
2877 case StabSourceLine:
2878 // N_SLINE - src line: 0,,n_sect,linenumber,address
2879 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2880 type = eSymbolTypeLineEntry;
2881 break;
2882
2883 case StabStructureType:
2884 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
2885 type = eSymbolTypeVariableType;
2886 break;
2887
2888 case StabSourceFileName:
2889 // N_SO - source file name
2890 type = eSymbolTypeSourceFile;
2891 if (symbol_name == NULL)
2892 {
2893 if (minimize)
2894 add_nlist = false;
2895 if (N_SO_index != UINT32_MAX)
2896 {
2897 // Set the size of the N_SO to the terminating index of this N_SO
2898 // so that we can always skip the entire N_SO if we need to navigate
2899 // more quickly at the source level when parsing STABS
2900 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
2901 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
2902 symbol_ptr->SetSizeIsSibling(true);
2903 }
2904 N_NSYM_indexes.clear();
2905 N_INCL_indexes.clear();
2906 N_BRAC_indexes.clear();
2907 N_COMM_indexes.clear();
2908 N_FUN_indexes.clear();
2909 N_SO_index = UINT32_MAX;
2910 }
2911 else
2912 {
2913 // We use the current number of symbols in the symbol table in lieu of
2914 // using nlist_idx in case we ever start trimming entries out
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002915 const bool N_SO_has_full_path = symbol_name[0] == '/';
2916 if (N_SO_has_full_path)
2917 {
2918 if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2919 {
2920 // We have two consecutive N_SO entries where the first contains a directory
2921 // and the second contains a full path.
Greg Claytonc0240042012-07-18 23:18:10 +00002922 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002923 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2924 add_nlist = false;
2925 }
2926 else
2927 {
2928 // This is the first entry in a N_SO that contains a directory or
2929 // a full path to the source file
2930 N_SO_index = sym_idx;
2931 }
2932 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002933 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
2934 {
Greg Clayton5fa6cd32012-05-30 20:20:34 +00002935 // This is usually the second N_SO entry that contains just the filename,
2936 // so here we combine it with the first one if we are minimizing the symbol table
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002937 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
2938 if (so_path && so_path[0])
2939 {
2940 std::string full_so_path (so_path);
Greg Clayton4df2b7f2012-09-07 20:29:13 +00002941 const size_t double_slash_pos = full_so_path.find("//");
2942 if (double_slash_pos != std::string::npos)
2943 {
2944 // The linker has been generating bad N_SO entries with doubled up paths
2945 // in the format "%s%s" where the first stirng in the DW_AT_comp_dir,
2946 // and the second is the directory for the source file so you end up with
2947 // a path that looks like "/tmp/src//tmp/src/"
2948 FileSpec so_dir(so_path, false);
2949 if (!so_dir.Exists())
2950 {
2951 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
2952 if (so_dir.Exists())
2953 {
2954 // Trim off the incorrect path
2955 full_so_path.erase(0, double_slash_pos + 1);
2956 }
2957 }
2958 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002959 if (*full_so_path.rbegin() != '/')
2960 full_so_path += '/';
2961 full_so_path += symbol_name;
Greg Claytonc0240042012-07-18 23:18:10 +00002962 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002963 add_nlist = false;
2964 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2965 }
2966 }
Greg Claytonab77dcb2012-09-05 22:30:51 +00002967 else
2968 {
2969 // This could be a relative path to a N_SO
2970 N_SO_index = sym_idx;
2971 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002972 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00002973
Greg Clayton4aa2edf2012-03-09 04:26:05 +00002974 break;
2975
2976 case StabObjectFileName:
2977 // N_OSO - object file name: name,,0,0,st_mtime
2978 type = eSymbolTypeObjectFile;
2979 break;
2980
2981 case StabLocalSymbol:
2982 // N_LSYM - local sym: name,,NO_SECT,type,offset
2983 type = eSymbolTypeLocal;
2984 break;
2985
2986 //----------------------------------------------------------------------
2987 // INCL scopes
2988 //----------------------------------------------------------------------
2989 case StabBeginIncludeFileName:
2990 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
2991 // We use the current number of symbols in the symbol table in lieu of
2992 // using nlist_idx in case we ever start trimming entries out
2993 N_INCL_indexes.push_back(sym_idx);
2994 type = eSymbolTypeScopeBegin;
2995 break;
2996
2997 case StabEndIncludeFile:
2998 // N_EINCL - include file end: name,,NO_SECT,0,0
2999 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3000 // so that we can always skip the entire symbol if we need to navigate
3001 // more quickly at the source level when parsing STABS
3002 if ( !N_INCL_indexes.empty() )
3003 {
3004 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3005 symbol_ptr->SetByteSize(sym_idx + 1);
3006 symbol_ptr->SetSizeIsSibling(true);
3007 N_INCL_indexes.pop_back();
3008 }
3009 type = eSymbolTypeScopeEnd;
3010 break;
3011
3012 case StabIncludeFileName:
3013 // N_SOL - #included file name: name,,n_sect,0,address
3014 type = eSymbolTypeHeaderFile;
3015
3016 // We currently don't use the header files on darwin
3017 if (minimize)
3018 add_nlist = false;
3019 break;
3020
Jason Molenda9badb6c2013-03-06 23:19:17 +00003021 case StabCompilerParameters:
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003022 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
3023 type = eSymbolTypeCompiler;
3024 break;
3025
3026 case StabCompilerVersion:
3027 // N_VERSION - compiler version: name,,NO_SECT,0,0
3028 type = eSymbolTypeCompiler;
3029 break;
3030
3031 case StabCompilerOptLevel:
3032 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
3033 type = eSymbolTypeCompiler;
3034 break;
3035
3036 case StabParameter:
3037 // N_PSYM - parameter: name,,NO_SECT,type,offset
3038 type = eSymbolTypeVariable;
3039 break;
3040
3041 case StabAlternateEntry:
3042 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
3043 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3044 type = eSymbolTypeLineEntry;
3045 break;
3046
3047 //----------------------------------------------------------------------
3048 // Left and Right Braces
3049 //----------------------------------------------------------------------
3050 case StabLeftBracket:
3051 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
3052 // We use the current number of symbols in the symbol table in lieu of
3053 // using nlist_idx in case we ever start trimming entries out
3054 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3055 N_BRAC_indexes.push_back(sym_idx);
3056 type = eSymbolTypeScopeBegin;
3057 break;
3058
3059 case StabRightBracket:
3060 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
3061 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3062 // so that we can always skip the entire symbol if we need to navigate
3063 // more quickly at the source level when parsing STABS
3064 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3065 if ( !N_BRAC_indexes.empty() )
3066 {
3067 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3068 symbol_ptr->SetByteSize(sym_idx + 1);
3069 symbol_ptr->SetSizeIsSibling(true);
3070 N_BRAC_indexes.pop_back();
3071 }
3072 type = eSymbolTypeScopeEnd;
3073 break;
3074
3075 case StabDeletedIncludeFile:
3076 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
3077 type = eSymbolTypeHeaderFile;
3078 break;
3079
3080 //----------------------------------------------------------------------
3081 // COMM scopes
3082 //----------------------------------------------------------------------
3083 case StabBeginCommon:
3084 // N_BCOMM - begin common: name,,NO_SECT,0,0
3085 // We use the current number of symbols in the symbol table in lieu of
3086 // using nlist_idx in case we ever start trimming entries out
3087 type = eSymbolTypeScopeBegin;
3088 N_COMM_indexes.push_back(sym_idx);
3089 break;
3090
3091 case StabEndCommonLocal:
3092 // N_ECOML - end common (local name): 0,,n_sect,0,address
3093 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3094 // Fall through
3095
3096 case StabEndCommon:
3097 // N_ECOMM - end common: name,,n_sect,0,0
3098 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3099 // so that we can always skip the entire symbol if we need to navigate
3100 // more quickly at the source level when parsing STABS
3101 if ( !N_COMM_indexes.empty() )
3102 {
3103 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3104 symbol_ptr->SetByteSize(sym_idx + 1);
3105 symbol_ptr->SetSizeIsSibling(true);
3106 N_COMM_indexes.pop_back();
3107 }
3108 type = eSymbolTypeScopeEnd;
3109 break;
3110
3111 case StabLength:
3112 // N_LENG - second stab entry with length information
3113 type = eSymbolTypeAdditional;
3114 break;
3115
3116 default: break;
3117 }
3118 }
3119 else
3120 {
3121 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
3122 uint8_t n_type = NlistMaskType & nlist.n_type;
3123 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
3124
3125 switch (n_type)
3126 {
3127 case NListTypeIndirect: // N_INDR - Fall through
3128 case NListTypePreboundUndefined:// N_PBUD - Fall through
3129 case NListTypeUndefined: // N_UNDF
3130 type = eSymbolTypeUndefined;
3131 break;
3132
3133 case NListTypeAbsolute: // N_ABS
3134 type = eSymbolTypeAbsolute;
3135 break;
3136
3137 case NListTypeSection: // N_SECT
3138 {
3139 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3140
Sean Callananb386d822012-08-09 00:50:26 +00003141 if (!symbol_section)
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003142 {
3143 // TODO: warn about this?
3144 add_nlist = false;
3145 break;
3146 }
3147
3148 if (TEXT_eh_frame_sectID == nlist.n_sect)
3149 {
3150 type = eSymbolTypeException;
Chris Lattner24943d22010-06-08 16:52:24 +00003151 }
3152 else
3153 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003154 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
3155
3156 switch (section_type)
Chris Lattner24943d22010-06-08 16:52:24 +00003157 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003158 case SectionTypeRegular: break; // regular section
3159 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
3160 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
3161 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
3162 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
3163 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3164 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3165 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3166 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3167 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3168 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
3169 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
3170 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
3171 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3172 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
3173 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
3174 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
3175 default: break;
Greg Clayton3f69eac2011-12-03 02:30:59 +00003176 }
Chris Lattner24943d22010-06-08 16:52:24 +00003177
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003178 if (type == eSymbolTypeInvalid)
Greg Clayton3f69eac2011-12-03 02:30:59 +00003179 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003180 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3181 if (symbol_section->IsDescendant (text_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003182 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003183 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
3184 SectionAttrUserSelfModifyingCode |
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003185 SectionAttrSytemSomeInstructions))
3186 type = eSymbolTypeData;
3187 else
3188 type = eSymbolTypeCode;
Greg Clayton576a68b2010-09-08 16:38:06 +00003189 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003190 else
3191 if (symbol_section->IsDescendant(data_section_sp.get()))
Greg Clayton576a68b2010-09-08 16:38:06 +00003192 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003193 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
Greg Clayton7c36fa02010-09-11 03:13:28 +00003194 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003195 type = eSymbolTypeRuntime;
Chris Lattner24943d22010-06-08 16:52:24 +00003196
Jason Molenda9badb6c2013-03-06 23:19:17 +00003197 if (symbol_name &&
3198 symbol_name[0] == '_' &&
3199 symbol_name[1] == 'O' &&
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003200 symbol_name[2] == 'B')
Greg Clayton637029b2010-09-12 05:25:16 +00003201 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003202 llvm::StringRef symbol_name_ref(symbol_name);
3203 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3204 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3205 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3206 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
Chris Lattner24943d22010-06-08 16:52:24 +00003207 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003208 symbol_name_non_abi_mangled = symbol_name + 1;
3209 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3210 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003211 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003212 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003213 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
Chris Lattner24943d22010-06-08 16:52:24 +00003214 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003215 symbol_name_non_abi_mangled = symbol_name + 1;
3216 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3217 type = eSymbolTypeObjCMetaClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003218 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003219 }
3220 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3221 {
3222 symbol_name_non_abi_mangled = symbol_name + 1;
3223 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3224 type = eSymbolTypeObjCIVar;
Greg Clayton01e6a582012-11-27 01:52:16 +00003225 demangled_is_synthesized = true;
Chris Lattner24943d22010-06-08 16:52:24 +00003226 }
3227 }
3228 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003229 else
3230 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
3231 {
3232 type = eSymbolTypeException;
3233 }
3234 else
3235 {
3236 type = eSymbolTypeData;
3237 }
3238 }
3239 else
3240 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3241 {
3242 type = eSymbolTypeTrampoline;
3243 }
3244 else
3245 if (symbol_section->IsDescendant(objc_section_sp.get()))
3246 {
3247 type = eSymbolTypeRuntime;
3248 if (symbol_name && symbol_name[0] == '.')
3249 {
3250 llvm::StringRef symbol_name_ref(symbol_name);
3251 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3252 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
3253 {
3254 symbol_name_non_abi_mangled = symbol_name;
3255 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3256 type = eSymbolTypeObjCClass;
Greg Clayton01e6a582012-11-27 01:52:16 +00003257 demangled_is_synthesized = true;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003258 }
3259 }
Chris Lattner24943d22010-06-08 16:52:24 +00003260 }
3261 }
3262 }
3263 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003264 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003265 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003266 }
3267
3268 if (add_nlist)
3269 {
3270 uint64_t symbol_value = nlist.n_value;
3271 bool symbol_name_is_mangled = false;
3272
3273 if (symbol_name_non_abi_mangled)
3274 {
Greg Claytonc0240042012-07-18 23:18:10 +00003275 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3276 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Chris Lattner24943d22010-06-08 16:52:24 +00003277 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003278 else
3279 {
3280 if (symbol_name && symbol_name[0] == '_')
3281 {
3282 symbol_name_is_mangled = symbol_name[1] == '_';
3283 symbol_name++; // Skip the leading underscore
3284 }
3285
3286 if (symbol_name)
3287 {
Greg Claytona8364e92013-05-14 22:19:37 +00003288 ConstString const_symbol_name(symbol_name);
3289 if (is_gsym)
3290 N_GSYM_name_to_sym_idx[const_symbol_name.GetCString()] = sym_idx;
3291 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003292 }
3293 }
3294 if (symbol_section)
3295 {
3296 const addr_t section_file_addr = symbol_section->GetFileAddress();
3297 if (symbol_byte_size == 0 && function_starts_count > 0)
3298 {
Greg Claytond2653c22012-03-14 01:53:24 +00003299 addr_t symbol_lookup_file_addr = nlist.n_value;
3300 // Do an exact address match for non-ARM addresses, else get the closest since
3301 // the symbol might be a thumb symbol which has an address with bit zero set
3302 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3303 if (is_arm && func_start_entry)
3304 {
3305 // Verify that the function start address is the symbol address (ARM)
3306 // or the symbol address + 1 (thumb)
3307 if (func_start_entry->addr != symbol_lookup_file_addr &&
3308 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3309 {
3310 // Not the right entry, NULL it out...
3311 func_start_entry = NULL;
3312 }
3313 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003314 if (func_start_entry)
3315 {
3316 func_start_entry->data = true;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003317
Greg Claytond2653c22012-03-14 01:53:24 +00003318 addr_t symbol_file_addr = func_start_entry->addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003319 if (is_arm)
Greg Claytond2653c22012-03-14 01:53:24 +00003320 symbol_file_addr &= 0xfffffffffffffffeull;
Greg Claytond2653c22012-03-14 01:53:24 +00003321
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003322 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3323 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3324 if (next_func_start_entry)
3325 {
Greg Claytond2653c22012-03-14 01:53:24 +00003326 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3327 // Be sure the clear the Thumb address bit when we calculate the size
3328 // from the current and next address
3329 if (is_arm)
3330 next_symbol_file_addr &= 0xfffffffffffffffeull;
3331 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 +00003332 }
3333 else
3334 {
Greg Claytond2653c22012-03-14 01:53:24 +00003335 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003336 }
3337 }
3338 }
3339 symbol_value -= section_file_addr;
3340 }
3341
Greg Claytona8364e92013-05-14 22:19:37 +00003342 if (is_debug == false)
3343 {
3344 if (type == eSymbolTypeCode)
3345 {
3346 // See if we can find a N_FUN entry for any code symbols.
3347 // If we do find a match, and the name matches, then we
3348 // can merge the two into just the function symbol to avoid
3349 // duplicate entries in the symbol table
3350 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
3351 if (pos != N_FUN_addr_to_sym_idx.end())
3352 {
3353 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3354 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3355 {
3356 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3357 // We just need the flags from the linker symbol, so put these flags
3358 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3359 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3360 sym[sym_idx].Clear();
3361 continue;
3362 }
3363 }
3364 }
3365 else if (type == eSymbolTypeData)
3366 {
3367 // See if we can find a N_STSYM entry for any data symbols.
3368 // If we do find a match, and the name matches, then we
3369 // can merge the two into just the Static symbol to avoid
3370 // duplicate entries in the symbol table
3371 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
3372 if (pos != N_STSYM_addr_to_sym_idx.end())
3373 {
3374 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
3375 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
3376 {
3377 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3378 // We just need the flags from the linker symbol, so put these flags
3379 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3380 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3381 sym[sym_idx].Clear();
3382 continue;
3383 }
3384 }
3385 else
3386 {
3387 // Combine N_GSYM stab entries with the non stab symbol
3388 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetMangledName().GetCString());
3389 if (pos != N_GSYM_name_to_sym_idx.end())
3390 {
3391 const uint32_t GSYM_sym_idx = pos->second;
3392 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3393 // Copy the address, because often the N_GSYM address has an invalid address of zero
3394 // when the global is a common symbol
3395 sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section);
3396 sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value);
3397 // We just need the flags from the linker symbol, so put these flags
3398 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3399 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3400 sym[sym_idx].Clear();
3401 continue;
3402 }
3403 }
3404 }
3405 }
3406
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003407 sym[sym_idx].SetID (nlist_idx);
3408 sym[sym_idx].SetType (type);
3409 sym[sym_idx].GetAddress().SetSection (symbol_section);
3410 sym[sym_idx].GetAddress().SetOffset (symbol_value);
3411 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3412
3413 if (symbol_byte_size > 0)
3414 sym[sym_idx].SetByteSize(symbol_byte_size);
3415
Greg Clayton01e6a582012-11-27 01:52:16 +00003416 if (demangled_is_synthesized)
3417 sym[sym_idx].SetDemangledNameIsSynthesized(true);
3418
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003419 ++sym_idx;
3420 }
3421 else
3422 {
3423 sym[sym_idx].Clear();
3424 }
3425
3426 }
3427
3428 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
3429 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
3430 // such entries by figuring out what the address for the global is by looking up this non-STAB
3431 // entry and copying the value into the debug symbol's value to save us the hassle in the
3432 // debug symbol parser.
3433
3434 Symbol *global_symbol = NULL;
3435 for (nlist_idx = 0;
3436 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
3437 nlist_idx++)
3438 {
3439 if (global_symbol->GetAddress().GetFileAddress() == 0)
3440 {
3441 std::vector<uint32_t> indexes;
3442 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
3443 {
3444 std::vector<uint32_t>::const_iterator pos;
3445 std::vector<uint32_t>::const_iterator end = indexes.end();
3446 for (pos = indexes.begin(); pos != end; ++pos)
3447 {
3448 symbol_ptr = symtab->SymbolAtIndex(*pos);
3449 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
3450 {
3451 global_symbol->GetAddress() = symbol_ptr->GetAddress();
3452 break;
3453 }
3454 }
3455 }
Chris Lattner24943d22010-06-08 16:52:24 +00003456 }
3457 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003458
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003459 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
3460
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003461 if (function_starts_count > 0)
3462 {
3463 char synthetic_function_symbol[PATH_MAX];
3464 uint32_t num_synthetic_function_symbols = 0;
3465 for (i=0; i<function_starts_count; ++i)
3466 {
3467 if (function_starts.GetEntryRef (i).data == false)
3468 ++num_synthetic_function_symbols;
3469 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003470
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003471 if (num_synthetic_function_symbols > 0)
3472 {
3473 if (num_syms < sym_idx + num_synthetic_function_symbols)
3474 {
3475 num_syms = sym_idx + num_synthetic_function_symbols;
3476 sym = symtab->Resize (num_syms);
3477 }
3478 uint32_t synthetic_function_symbol_idx = 0;
3479 for (i=0; i<function_starts_count; ++i)
3480 {
3481 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
3482 if (func_start_entry->data == false)
3483 {
Greg Claytond2653c22012-03-14 01:53:24 +00003484 addr_t symbol_file_addr = func_start_entry->addr;
3485 uint32_t symbol_flags = 0;
3486 if (is_arm)
3487 {
3488 if (symbol_file_addr & 1)
3489 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
3490 symbol_file_addr &= 0xfffffffffffffffeull;
3491 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003492 Address symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003493 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003494 {
3495 SectionSP symbol_section (symbol_addr.GetSection());
3496 uint32_t symbol_byte_size = 0;
3497 if (symbol_section)
3498 {
3499 const addr_t section_file_addr = symbol_section->GetFileAddress();
3500 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3501 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3502 if (next_func_start_entry)
3503 {
Greg Claytond2653c22012-03-14 01:53:24 +00003504 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3505 if (is_arm)
3506 next_symbol_file_addr &= 0xfffffffffffffffeull;
3507 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 +00003508 }
3509 else
3510 {
Greg Claytond2653c22012-03-14 01:53:24 +00003511 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003512 }
3513 snprintf (synthetic_function_symbol,
3514 sizeof(synthetic_function_symbol),
3515 "___lldb_unnamed_function%u$$%s",
3516 ++synthetic_function_symbol_idx,
3517 module_sp->GetFileSpec().GetFilename().GetCString());
3518 sym[sym_idx].SetID (synthetic_sym_id++);
Greg Claytonc0240042012-07-18 23:18:10 +00003519 sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol));
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003520 sym[sym_idx].SetType (eSymbolTypeCode);
3521 sym[sym_idx].SetIsSynthetic (true);
3522 sym[sym_idx].GetAddress() = symbol_addr;
Greg Claytond2653c22012-03-14 01:53:24 +00003523 if (symbol_flags)
3524 sym[sym_idx].SetFlags (symbol_flags);
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003525 if (symbol_byte_size)
3526 sym[sym_idx].SetByteSize (symbol_byte_size);
3527 ++sym_idx;
3528 }
3529 }
3530 }
3531 }
3532 }
3533 }
3534
3535 // Trim our symbols down to just what we ended up with after
3536 // removing any symbols.
3537 if (sym_idx < num_syms)
3538 {
3539 num_syms = sym_idx;
3540 sym = symtab->Resize (num_syms);
3541 }
3542
3543 // Now synthesize indirect symbols
3544 if (m_dysymtab.nindirectsyms != 0)
3545 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003546 if (indirect_symbol_index_data.GetByteSize())
3547 {
3548 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
3549
3550 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
3551 {
3552 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
3553 {
3554 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
3555 if (symbol_stub_byte_size == 0)
3556 continue;
3557
3558 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
3559
3560 if (num_symbol_stubs == 0)
3561 continue;
3562
3563 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
3564 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
3565 {
3566 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
3567 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 +00003568 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003569 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
3570 {
3571 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
3572 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
3573 continue;
3574
3575 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
3576 Symbol *stub_symbol = NULL;
3577 if (index_pos != end_index_pos)
3578 {
3579 // We have a remapping from the original nlist index to
3580 // a current symbol index, so just look this up by index
3581 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
3582 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003583 else
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003584 {
3585 // We need to lookup a symbol using the original nlist
Jason Molenda9badb6c2013-03-06 23:19:17 +00003586 // symbol index since this index is coming from the
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003587 // S_SYMBOL_STUBS
3588 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
3589 }
3590
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003591 if (stub_symbol)
3592 {
3593 Address so_addr(symbol_stub_addr, section_list);
3594
3595 if (stub_symbol->GetType() == eSymbolTypeUndefined)
3596 {
3597 // Change the external symbol into a trampoline that makes sense
3598 // These symbols were N_UNDF N_EXT, and are useless to us, so we
3599 // can re-use them so we don't have to make up a synthetic symbol
3600 // for no good reason.
3601 stub_symbol->SetType (eSymbolTypeTrampoline);
3602 stub_symbol->SetExternal (false);
3603 stub_symbol->GetAddress() = so_addr;
3604 stub_symbol->SetByteSize (symbol_stub_byte_size);
3605 }
3606 else
3607 {
3608 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003609 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003610 if (sym_idx >= num_syms)
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003611 {
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003612 sym = symtab->Resize (++num_syms);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003613 stub_symbol = NULL; // this pointer no longer valid
3614 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003615 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda2a76fbf2012-04-24 02:09:58 +00003616 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003617 sym[sym_idx].SetType (eSymbolTypeTrampoline);
3618 sym[sym_idx].SetIsSynthetic (true);
3619 sym[sym_idx].GetAddress() = so_addr;
3620 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
3621 ++sym_idx;
3622 }
3623 }
Greg Claytond4330e62012-09-05 01:38:55 +00003624 else
3625 {
3626 if (log)
3627 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
3628 }
Greg Clayton4aa2edf2012-03-09 04:26:05 +00003629 }
3630 }
3631 }
3632 }
3633 }
3634 }
3635 return symtab->GetNumSymbols();
Chris Lattner24943d22010-06-08 16:52:24 +00003636 }
3637 return 0;
3638}
3639
3640
3641void
3642ObjectFileMachO::Dump (Stream *s)
3643{
Greg Clayton9482f052012-03-13 23:14:29 +00003644 ModuleSP module_sp(GetModule());
3645 if (module_sp)
3646 {
3647 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3648 s->Printf("%p: ", this);
3649 s->Indent();
3650 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
3651 s->PutCString("ObjectFileMachO64");
3652 else
3653 s->PutCString("ObjectFileMachO32");
Chris Lattner24943d22010-06-08 16:52:24 +00003654
Greg Clayton9482f052012-03-13 23:14:29 +00003655 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00003656
Greg Clayton9482f052012-03-13 23:14:29 +00003657 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00003658
Greg Clayton9482f052012-03-13 23:14:29 +00003659 if (m_sections_ap.get())
3660 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00003661
Greg Clayton9482f052012-03-13 23:14:29 +00003662 if (m_symtab_ap.get())
3663 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
3664 }
Chris Lattner24943d22010-06-08 16:52:24 +00003665}
3666
Greg Clayton36b877d2013-04-24 22:29:28 +00003667bool
3668ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
3669 const lldb_private::DataExtractor &data,
3670 lldb::offset_t lc_offset,
3671 lldb_private::UUID& uuid)
3672{
3673 uint32_t i;
3674 struct uuid_command load_cmd;
3675
3676 lldb::offset_t offset = lc_offset;
3677 for (i=0; i<header.ncmds; ++i)
3678 {
3679 const lldb::offset_t cmd_offset = offset;
3680 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
3681 break;
3682
3683 if (load_cmd.cmd == LoadCommandUUID)
3684 {
3685 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
3686
3687 if (uuid_bytes)
3688 {
3689 // OpenCL on Mac OS X uses the same UUID for each of its object files.
3690 // We pretend these object files have no UUID to prevent crashing.
3691
3692 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
3693 0x3b, 0xa8,
3694 0x4b, 0x16,
3695 0xb6, 0xa4,
3696 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
3697
3698 if (!memcmp(uuid_bytes, opencl_uuid, 16))
3699 return false;
3700
3701 uuid.SetBytes (uuid_bytes);
3702 return true;
3703 }
3704 return false;
3705 }
3706 offset = cmd_offset + load_cmd.cmdsize;
3707 }
3708 return false;
3709}
Chris Lattner24943d22010-06-08 16:52:24 +00003710
3711bool
Greg Clayton0467c782011-02-04 18:53:10 +00003712ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00003713{
Greg Clayton9482f052012-03-13 23:14:29 +00003714 ModuleSP module_sp(GetModule());
3715 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003716 {
Greg Clayton9482f052012-03-13 23:14:29 +00003717 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00003718 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton36b877d2013-04-24 22:29:28 +00003719 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner24943d22010-06-08 16:52:24 +00003720 }
3721 return false;
3722}
3723
3724
3725uint32_t
3726ObjectFileMachO::GetDependentModules (FileSpecList& files)
3727{
Chris Lattner24943d22010-06-08 16:52:24 +00003728 uint32_t count = 0;
Greg Clayton9482f052012-03-13 23:14:29 +00003729 ModuleSP module_sp(GetModule());
3730 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00003731 {
Greg Clayton9482f052012-03-13 23:14:29 +00003732 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3733 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003734 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003735 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
3736 uint32_t i;
3737 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00003738 {
Greg Clayton9482f052012-03-13 23:14:29 +00003739 const uint32_t cmd_offset = offset;
3740 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3741 break;
Chris Lattner24943d22010-06-08 16:52:24 +00003742
Greg Clayton9482f052012-03-13 23:14:29 +00003743 switch (load_cmd.cmd)
3744 {
3745 case LoadCommandDylibLoad:
3746 case LoadCommandDylibLoadWeak:
3747 case LoadCommandDylibReexport:
3748 case LoadCommandDynamicLinkerLoad:
3749 case LoadCommandFixedVMShlibLoad:
3750 case LoadCommandDylibLoadUpward:
3751 {
3752 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
3753 const char *path = m_data.PeekCStr(name_offset);
3754 // Skip any path that starts with '@' since these are usually:
3755 // @executable_path/.../file
3756 // @rpath/.../file
3757 if (path && path[0] != '@')
3758 {
3759 FileSpec file_spec(path, resolve_path);
3760 if (files.AppendIfUnique(file_spec))
3761 count++;
3762 }
3763 }
3764 break;
3765
3766 default:
3767 break;
3768 }
3769 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner24943d22010-06-08 16:52:24 +00003770 }
Chris Lattner24943d22010-06-08 16:52:24 +00003771 }
3772 return count;
3773}
3774
Jim Ingham28775942011-03-07 23:44:08 +00003775lldb_private::Address
Jason Molenda9badb6c2013-03-06 23:19:17 +00003776ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham28775942011-03-07 23:44:08 +00003777{
3778 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
3779 // is initialized to an invalid address, so we can just return that.
3780 // 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 +00003781
Jim Ingham28775942011-03-07 23:44:08 +00003782 if (!IsExecutable() || m_entry_point_address.IsValid())
3783 return m_entry_point_address;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003784
3785 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham28775942011-03-07 23:44:08 +00003786 // /usr/include/mach-o.h, but it is basically:
3787 //
3788 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
3789 // uint32_t count - this is the count of longs in the thread state data
3790 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
3791 // <repeat this trio>
Jason Molenda9badb6c2013-03-06 23:19:17 +00003792 //
Jim Ingham28775942011-03-07 23:44:08 +00003793 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
3794 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
3795 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
3796 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
3797 //
3798 // For now we hard-code the offsets and flavors we need:
3799 //
3800 //
3801
Greg Clayton9482f052012-03-13 23:14:29 +00003802 ModuleSP module_sp(GetModule());
3803 if (module_sp)
Jim Ingham28775942011-03-07 23:44:08 +00003804 {
Greg Clayton9482f052012-03-13 23:14:29 +00003805 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3806 struct load_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003807 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003808 uint32_t i;
3809 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
3810 bool done = false;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003811
Greg Clayton9482f052012-03-13 23:14:29 +00003812 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham28775942011-03-07 23:44:08 +00003813 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003814 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00003815 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
3816 break;
3817
3818 switch (load_cmd.cmd)
Jim Ingham28775942011-03-07 23:44:08 +00003819 {
Greg Clayton9482f052012-03-13 23:14:29 +00003820 case LoadCommandUnixThread:
3821 case LoadCommandThread:
Jim Ingham28775942011-03-07 23:44:08 +00003822 {
Greg Clayton9482f052012-03-13 23:14:29 +00003823 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham28775942011-03-07 23:44:08 +00003824 {
Greg Clayton9482f052012-03-13 23:14:29 +00003825 uint32_t flavor = m_data.GetU32(&offset);
3826 uint32_t count = m_data.GetU32(&offset);
3827 if (count == 0)
3828 {
3829 // We've gotten off somehow, log and exit;
3830 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +00003831 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003832
Greg Clayton9482f052012-03-13 23:14:29 +00003833 switch (m_header.cputype)
3834 {
3835 case llvm::MachO::CPUTypeARM:
3836 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
3837 {
3838 offset += 60; // This is the offset of pc in the GPR thread state data structure.
3839 start_address = m_data.GetU32(&offset);
3840 done = true;
3841 }
Jim Ingham28775942011-03-07 23:44:08 +00003842 break;
Greg Clayton9482f052012-03-13 23:14:29 +00003843 case llvm::MachO::CPUTypeI386:
3844 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
3845 {
3846 offset += 40; // This is the offset of eip in the GPR thread state data structure.
3847 start_address = m_data.GetU32(&offset);
3848 done = true;
3849 }
3850 break;
3851 case llvm::MachO::CPUTypeX86_64:
3852 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
3853 {
3854 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
3855 start_address = m_data.GetU64(&offset);
3856 done = true;
3857 }
3858 break;
3859 default:
3860 return m_entry_point_address;
3861 }
3862 // Haven't found the GPR flavor yet, skip over the data for this flavor:
3863 if (done)
3864 break;
3865 offset += count * 4;
3866 }
Jim Ingham28775942011-03-07 23:44:08 +00003867 }
Greg Clayton9482f052012-03-13 23:14:29 +00003868 break;
3869 case LoadCommandMain:
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003870 {
Greg Clayton9482f052012-03-13 23:14:29 +00003871 ConstString text_segment_name ("__TEXT");
3872 uint64_t entryoffset = m_data.GetU64(&offset);
3873 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
3874 if (text_segment_sp)
3875 {
3876 done = true;
3877 start_address = text_segment_sp->GetFileAddress() + entryoffset;
3878 }
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003879 }
Greg Clayton9482f052012-03-13 23:14:29 +00003880
3881 default:
3882 break;
Sean Callanan6e12c7a2012-03-08 02:39:03 +00003883 }
Greg Clayton9482f052012-03-13 23:14:29 +00003884 if (done)
3885 break;
Jim Ingham28775942011-03-07 23:44:08 +00003886
Greg Clayton9482f052012-03-13 23:14:29 +00003887 // Go to the next load command:
3888 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham28775942011-03-07 23:44:08 +00003889 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003890
Greg Clayton9482f052012-03-13 23:14:29 +00003891 if (start_address != LLDB_INVALID_ADDRESS)
Greg Clayton3508c382012-02-24 01:59:29 +00003892 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003893 // We got the start address from the load commands, so now resolve that address in the sections
Greg Clayton9482f052012-03-13 23:14:29 +00003894 // of this ObjectFile:
3895 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Clayton3508c382012-02-24 01:59:29 +00003896 {
Greg Clayton9482f052012-03-13 23:14:29 +00003897 m_entry_point_address.Clear();
3898 }
3899 }
3900 else
3901 {
3902 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
3903 // "start" symbol in the main executable.
Jason Molenda9badb6c2013-03-06 23:19:17 +00003904
Greg Clayton9482f052012-03-13 23:14:29 +00003905 ModuleSP module_sp (GetModule());
Jason Molenda9badb6c2013-03-06 23:19:17 +00003906
Greg Clayton9482f052012-03-13 23:14:29 +00003907 if (module_sp)
3908 {
3909 SymbolContextList contexts;
3910 SymbolContext context;
3911 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
3912 {
3913 if (contexts.GetContextAtIndex(0, context))
3914 m_entry_point_address = context.symbol->GetAddress();
3915 }
Greg Clayton3508c382012-02-24 01:59:29 +00003916 }
3917 }
Jim Ingham28775942011-03-07 23:44:08 +00003918 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00003919
Jim Ingham28775942011-03-07 23:44:08 +00003920 return m_entry_point_address;
3921
3922}
3923
Greg Claytonb5a8f142012-02-05 02:38:54 +00003924lldb_private::Address
3925ObjectFileMachO::GetHeaderAddress ()
3926{
3927 lldb_private::Address header_addr;
3928 SectionList *section_list = GetSectionList();
3929 if (section_list)
3930 {
3931 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
3932 if (text_segment_sp)
3933 {
Greg Clayton3508c382012-02-24 01:59:29 +00003934 header_addr.SetSection (text_segment_sp);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003935 header_addr.SetOffset (0);
3936 }
3937 }
3938 return header_addr;
3939}
3940
Greg Clayton46c9a352012-02-09 06:16:32 +00003941uint32_t
3942ObjectFileMachO::GetNumThreadContexts ()
3943{
Greg Clayton9482f052012-03-13 23:14:29 +00003944 ModuleSP module_sp(GetModule());
3945 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003946 {
Greg Clayton9482f052012-03-13 23:14:29 +00003947 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3948 if (!m_thread_context_offsets_valid)
Greg Clayton46c9a352012-02-09 06:16:32 +00003949 {
Greg Clayton9482f052012-03-13 23:14:29 +00003950 m_thread_context_offsets_valid = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003951 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00003952 FileRangeArray::Entry file_range;
3953 thread_command thread_cmd;
3954 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton46c9a352012-02-09 06:16:32 +00003955 {
Greg Clayton9482f052012-03-13 23:14:29 +00003956 const uint32_t cmd_offset = offset;
3957 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
3958 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003959
Greg Clayton9482f052012-03-13 23:14:29 +00003960 if (thread_cmd.cmd == LoadCommandThread)
3961 {
3962 file_range.SetRangeBase (offset);
3963 file_range.SetByteSize (thread_cmd.cmdsize - 8);
3964 m_thread_context_offsets.Append (file_range);
3965 }
3966 offset = cmd_offset + thread_cmd.cmdsize;
Greg Clayton46c9a352012-02-09 06:16:32 +00003967 }
Greg Clayton46c9a352012-02-09 06:16:32 +00003968 }
3969 }
3970 return m_thread_context_offsets.GetSize();
3971}
3972
3973lldb::RegisterContextSP
3974ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
3975{
Greg Clayton46c9a352012-02-09 06:16:32 +00003976 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton9ce95382012-02-13 23:10:39 +00003977
Greg Clayton9482f052012-03-13 23:14:29 +00003978 ModuleSP module_sp(GetModule());
3979 if (module_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +00003980 {
Greg Clayton9482f052012-03-13 23:14:29 +00003981 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
3982 if (!m_thread_context_offsets_valid)
3983 GetNumThreadContexts ();
3984
3985 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham6f01c932012-10-12 17:34:26 +00003986 if (thread_context_file_range)
Greg Clayton9482f052012-03-13 23:14:29 +00003987 {
Jason Molenda9badb6c2013-03-06 23:19:17 +00003988
3989 DataExtractor data (m_data,
3990 thread_context_file_range->GetRangeBase(),
Jim Ingham6f01c932012-10-12 17:34:26 +00003991 thread_context_file_range->GetByteSize());
3992
3993 switch (m_header.cputype)
3994 {
3995 case llvm::MachO::CPUTypeARM:
3996 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
3997 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00003998
Jim Ingham6f01c932012-10-12 17:34:26 +00003999 case llvm::MachO::CPUTypeI386:
4000 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
4001 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00004002
Jim Ingham6f01c932012-10-12 17:34:26 +00004003 case llvm::MachO::CPUTypeX86_64:
4004 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
4005 break;
4006 }
Greg Clayton9482f052012-03-13 23:14:29 +00004007 }
Greg Clayton46c9a352012-02-09 06:16:32 +00004008 }
4009 return reg_ctx_sp;
4010}
4011
Greg Claytonb5a8f142012-02-05 02:38:54 +00004012
Greg Claytonca319972011-07-09 00:41:34 +00004013ObjectFile::Type
4014ObjectFileMachO::CalculateType()
4015{
4016 switch (m_header.filetype)
4017 {
4018 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4019 if (GetAddressByteSize () == 4)
4020 {
4021 // 32 bit kexts are just object files, but they do have a valid
4022 // UUID load command.
4023 UUID uuid;
4024 if (GetUUID(&uuid))
4025 {
4026 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00004027 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00004028 // "OSKextGetCurrentIdentifier" as this is required of kexts
4029 if (m_strata == eStrataInvalid)
4030 m_strata = eStrataKernel;
4031 return eTypeSharedLibrary;
4032 }
4033 }
4034 return eTypeObjectFile;
4035
4036 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
4037 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
4038 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
4039 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
4040 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
4041 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
4042 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
4043 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
4044 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
4045 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
4046 default:
4047 break;
4048 }
4049 return eTypeUnknown;
4050}
4051
4052ObjectFile::Strata
4053ObjectFileMachO::CalculateStrata()
4054{
4055 switch (m_header.filetype)
4056 {
4057 case HeaderFileTypeObject: // 0x1u MH_OBJECT
4058 {
4059 // 32 bit kexts are just object files, but they do have a valid
4060 // UUID load command.
4061 UUID uuid;
4062 if (GetUUID(&uuid))
4063 {
4064 // this checking for the UUID load command is not enough
Jason Molenda9badb6c2013-03-06 23:19:17 +00004065 // we could eventually look for the symbol named
Greg Claytonca319972011-07-09 00:41:34 +00004066 // "OSKextGetCurrentIdentifier" as this is required of kexts
4067 if (m_type == eTypeInvalid)
4068 m_type = eTypeSharedLibrary;
4069
4070 return eStrataKernel;
4071 }
4072 }
4073 return eStrataUnknown;
4074
4075 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
4076 // Check for the MH_DYLDLINK bit in the flags
4077 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
Sean Callananac725af2012-02-10 20:22:35 +00004078 {
Greg Claytonca319972011-07-09 00:41:34 +00004079 return eStrataUser;
Sean Callananac725af2012-02-10 20:22:35 +00004080 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004081 else
Sean Callananac725af2012-02-10 20:22:35 +00004082 {
4083 SectionList *section_list = GetSectionList();
4084 if (section_list)
4085 {
4086 static ConstString g_kld_section_name ("__KLD");
4087 if (section_list->FindSectionByName(g_kld_section_name))
4088 return eStrataKernel;
4089 }
4090 }
4091 return eStrataRawImage;
Greg Claytonca319972011-07-09 00:41:34 +00004092
4093 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
4094 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
Sean Callananac725af2012-02-10 20:22:35 +00004095 case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD
Greg Claytonca319972011-07-09 00:41:34 +00004096 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
4097 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
4098 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
4099 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
4100 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
4101 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
4102 default:
4103 break;
4104 }
4105 return eStrataUnknown;
4106}
4107
4108
Greg Clayton49f4bf22012-02-22 19:41:02 +00004109uint32_t
4110ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
4111{
Greg Clayton9482f052012-03-13 23:14:29 +00004112 ModuleSP module_sp(GetModule());
4113 if (module_sp)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004114 {
Greg Clayton9482f052012-03-13 23:14:29 +00004115 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4116 struct dylib_command load_cmd;
Greg Clayton36da2aa2013-01-25 18:06:21 +00004117 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton9482f052012-03-13 23:14:29 +00004118 uint32_t version_cmd = 0;
4119 uint64_t version = 0;
4120 uint32_t i;
4121 for (i=0; i<m_header.ncmds; ++i)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004122 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00004123 const lldb::offset_t cmd_offset = offset;
Greg Clayton9482f052012-03-13 23:14:29 +00004124 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4125 break;
Jason Molenda9badb6c2013-03-06 23:19:17 +00004126
Greg Clayton9482f052012-03-13 23:14:29 +00004127 if (load_cmd.cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004128 {
Greg Clayton9482f052012-03-13 23:14:29 +00004129 if (version_cmd == 0)
4130 {
4131 version_cmd = load_cmd.cmd;
4132 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
4133 break;
4134 version = load_cmd.dylib.current_version;
4135 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004136 break; // Break for now unless there is another more complete version
Greg Clayton9482f052012-03-13 23:14:29 +00004137 // number load command in the future.
Greg Clayton49f4bf22012-02-22 19:41:02 +00004138 }
Greg Clayton9482f052012-03-13 23:14:29 +00004139 offset = cmd_offset + load_cmd.cmdsize;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004140 }
Jason Molenda9badb6c2013-03-06 23:19:17 +00004141
Greg Clayton9482f052012-03-13 23:14:29 +00004142 if (version_cmd == LoadCommandDylibIdent)
Greg Clayton49f4bf22012-02-22 19:41:02 +00004143 {
Greg Clayton9482f052012-03-13 23:14:29 +00004144 if (versions != NULL && num_versions > 0)
4145 {
4146 if (num_versions > 0)
4147 versions[0] = (version & 0xFFFF0000ull) >> 16;
4148 if (num_versions > 1)
4149 versions[1] = (version & 0x0000FF00ull) >> 8;
4150 if (num_versions > 2)
4151 versions[2] = (version & 0x000000FFull);
4152 // Fill in an remaining version numbers with invalid values
4153 for (i=3; i<num_versions; ++i)
4154 versions[i] = UINT32_MAX;
4155 }
4156 // The LC_ID_DYLIB load command has a version with 3 version numbers
4157 // in it, so always return 3
4158 return 3;
Greg Clayton49f4bf22012-02-22 19:41:02 +00004159 }
Greg Clayton49f4bf22012-02-22 19:41:02 +00004160 }
4161 return false;
4162}
4163
Chris Lattner24943d22010-06-08 16:52:24 +00004164bool
Greg Clayton395fc332011-02-15 21:59:32 +00004165ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00004166{
Greg Clayton9482f052012-03-13 23:14:29 +00004167 ModuleSP module_sp(GetModule());
4168 if (module_sp)
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004169 {
Greg Clayton9482f052012-03-13 23:14:29 +00004170 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4171 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jason Molenda9badb6c2013-03-06 23:19:17 +00004172
Greg Clayton9482f052012-03-13 23:14:29 +00004173 // Files with type MH_PRELOAD are currently used in cases where the image
Jason Molenda9badb6c2013-03-06 23:19:17 +00004174 // debugs at the addresses in the file itself. Below we set the OS to
Greg Clayton9482f052012-03-13 23:14:29 +00004175 // unknown to make sure we use the DynamicLoaderStatic()...
4176 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
4177 {
4178 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
4179 }
4180 return true;
Greg Clayton6a64bbf2011-09-21 03:57:31 +00004181 }
Greg Clayton9482f052012-03-13 23:14:29 +00004182 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00004183}
4184
4185
Jason Molenda45c75502013-04-16 06:24:42 +00004186UUID
4187ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
4188{
4189 UUID uuid;
4190 if (process)
4191 {
4192 addr_t all_image_infos = process->GetImageInfoAddress();
4193
4194 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
4195 // or it may be the address of the dyld_all_image_infos structure (want). The first four
4196 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
4197 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
4198
4199 Error err;
4200 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
4201 if (version_or_magic != -1
4202 && version_or_magic != HeaderMagic32
4203 && version_or_magic != HeaderMagic32Swapped
4204 && version_or_magic != HeaderMagic64
4205 && version_or_magic != HeaderMagic64Swapped
4206 && version_or_magic >= 13)
4207 {
4208 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
4209 int wordsize = process->GetAddressByteSize();
4210 if (wordsize == 8)
4211 {
4212 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
4213 }
4214 if (wordsize == 4)
4215 {
4216 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
4217 }
4218 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
4219 {
4220 uuid_t shared_cache_uuid;
4221 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
4222 {
4223 uuid.SetBytes (shared_cache_uuid);
4224 }
4225 }
4226 }
4227 }
4228 return uuid;
4229}
4230
4231UUID
4232ObjectFileMachO::GetLLDBSharedCacheUUID ()
4233{
4234 UUID uuid;
4235#if defined (__APPLE__) && defined (__arm__)
4236 uint8_t *(*dyld_get_all_image_infos)(void);
4237 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
4238 if (dyld_get_all_image_infos)
4239 {
4240 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
4241 if (dyld_all_image_infos_address)
4242 {
Jason Molenda6ff916f2013-04-16 21:42:58 +00004243 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
4244 if (*version >= 13)
Jason Molenda45c75502013-04-16 06:24:42 +00004245 {
Jason Molenda2ceae992013-04-16 22:56:17 +00004246 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 +00004247 uuid.SetBytes (sharedCacheUUID_address);
4248 }
4249 }
4250 }
4251#endif
4252 return uuid;
4253}
4254
4255
Chris Lattner24943d22010-06-08 16:52:24 +00004256//------------------------------------------------------------------
4257// PluginInterface protocol
4258//------------------------------------------------------------------
Greg Clayton0e191602013-05-10 21:47:16 +00004259lldb_private::ConstString
Chris Lattner24943d22010-06-08 16:52:24 +00004260ObjectFileMachO::GetPluginName()
4261{
Chris Lattner24943d22010-06-08 16:52:24 +00004262 return GetPluginNameStatic();
4263}
4264
4265uint32_t
4266ObjectFileMachO::GetPluginVersion()
4267{
4268 return 1;
4269}
4270