blob: 0b86403354a0417cf5ac438b7852e82079c92cd4 [file] [log] [blame]
Chris Lattner30fdc8d2010-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
Eugene Zelenko8157a882015-10-23 16:56:07 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
Jim Ingham46d005d2014-04-02 22:53:21 +000013#include "llvm/ADT/StringRef.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
Eugene Zelenko8157a882015-10-23 16:56:07 +000015// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/ArchSpec.h"
17#include "lldb/Core/DataBuffer.h"
Jason Molendaf6ce26f2013-04-10 05:58:57 +000018#include "lldb/Core/Debugger.h"
Greg Claytona2715cf2014-06-13 00:54:12 +000019#include "lldb/Core/Error.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/FileSpecList.h"
Greg Clayton3f839a32012-09-05 01:38:55 +000021#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000023#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/PluginManager.h"
Greg Clayton1eac0c72012-04-24 03:06:13 +000025#include "lldb/Core/RangeMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Core/Section.h"
27#include "lldb/Core/StreamFile.h"
28#include "lldb/Core/StreamString.h"
29#include "lldb/Core/Timer.h"
30#include "lldb/Core/UUID.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000031#include "lldb/Host/Host.h"
32#include "lldb/Host/FileSpec.h"
Jason Molenda5635f772013-03-21 03:36:01 +000033#include "lldb/Symbol/DWARFCallFrameInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Symbol/ObjectFile.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000035#include "lldb/Target/MemoryRegionInfo.h"
Greg Clayton26b47e22012-04-18 05:19:20 +000036#include "lldb/Target/Platform.h"
Greg Claytonc9660542012-02-05 02:38:54 +000037#include "lldb/Target/Process.h"
Greg Clayton7524e092014-02-06 20:10:16 +000038#include "lldb/Target/SectionLoadList.h"
Greg Clayton26b47e22012-04-18 05:19:20 +000039#include "lldb/Target/Target.h"
Greg Claytona2715cf2014-06-13 00:54:12 +000040#include "lldb/Target/Thread.h"
41#include "lldb/Target/ThreadList.h"
Greg Claytonc859e2d2012-02-13 23:10:39 +000042#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h"
Jason Molendaa3329782014-03-29 18:54:20 +000043#include "Plugins/Process/Utility/RegisterContextDarwin_arm64.h"
Greg Claytonc859e2d2012-02-13 23:10:39 +000044#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h"
Greg Claytonc3776bf2012-02-09 06:16:32 +000045#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
Jim Ingham46d005d2014-04-02 22:53:21 +000047#include "lldb/Utility/SafeMachO.h"
48
49#include "ObjectFileMachO.h"
50
Todd Fiala013434e2014-07-09 01:29:05 +000051#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molenda0e0954c2013-04-16 06:24:42 +000052// GetLLDBSharedCacheUUID() needs to call dlsym()
53#include <dlfcn.h>
54#endif
55
Daniel Maleaffeb4b62013-04-17 19:24:22 +000056#ifndef __APPLE__
57#include "Utility/UuidCompatibility.h"
58#endif
59
Greg Claytonb887da12015-07-16 19:50:57 +000060#define THUMB_ADDRESS_BIT_MASK 0xfffffffffffffffeull
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061using namespace lldb;
62using namespace lldb_private;
Greg Claytone1a916a2010-07-21 22:12:05 +000063using namespace llvm::MachO;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064
Jason Molenda649a6072015-11-10 05:21:54 +000065// Some structure definitions needed for parsing the dyld shared cache files
66// found on iOS devices.
67
68struct lldb_copy_dyld_cache_header_v1
69{
70 char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc.
71 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info
72 uint32_t mappingCount; // number of dyld_cache_mapping_info entries
73 uint32_t imagesOffset;
74 uint32_t imagesCount;
75 uint64_t dyldBaseAddress;
76 uint64_t codeSignatureOffset;
77 uint64_t codeSignatureSize;
78 uint64_t slideInfoOffset;
79 uint64_t slideInfoSize;
80 uint64_t localSymbolsOffset;
81 uint64_t localSymbolsSize;
82 uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later
83};
84
85struct lldb_copy_dyld_cache_mapping_info
86{
87 uint64_t address;
88 uint64_t size;
89 uint64_t fileOffset;
90 uint32_t maxProt;
91 uint32_t initProt;
92};
93
94struct lldb_copy_dyld_cache_local_symbols_info
95{
96 uint32_t nlistOffset;
97 uint32_t nlistCount;
98 uint32_t stringsOffset;
99 uint32_t stringsSize;
100 uint32_t entriesOffset;
101 uint32_t entriesCount;
102};
103struct lldb_copy_dyld_cache_local_symbols_entry
104{
105 uint32_t dylibOffset;
106 uint32_t nlistStartIndex;
107 uint32_t nlistCount;
108};
109
110
Jason Molenda4e7511e2013-03-06 23:19:17 +0000111class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64
Greg Claytonc3776bf2012-02-09 06:16:32 +0000112{
113public:
114 RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
115 RegisterContextDarwin_x86_64 (thread, 0)
116 {
117 SetRegisterDataFrom_LC_THREAD (data);
118 }
119
Eugene Zelenko8157a882015-10-23 16:56:07 +0000120 void
121 InvalidateAllRegisters() override
Greg Claytonc3776bf2012-02-09 06:16:32 +0000122 {
123 // Do nothing... registers are always valid...
124 }
125
126 void
127 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
128 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000129 lldb::offset_t offset = 0;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000130 SetError (GPRRegSet, Read, -1);
131 SetError (FPURegSet, Read, -1);
132 SetError (EXCRegSet, Read, -1);
Greg Claytonc859e2d2012-02-13 23:10:39 +0000133 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000134
Greg Claytonc859e2d2012-02-13 23:10:39 +0000135 while (!done)
Greg Claytonc3776bf2012-02-09 06:16:32 +0000136 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000137 int flavor = data.GetU32 (&offset);
138 if (flavor == 0)
139 done = true;
140 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000141 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000142 uint32_t i;
143 uint32_t count = data.GetU32 (&offset);
144 switch (flavor)
145 {
146 case GPRRegSet:
147 for (i=0; i<count; ++i)
148 (&gpr.rax)[i] = data.GetU64(&offset);
149 SetError (GPRRegSet, Read, 0);
150 done = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000151
Greg Claytonc859e2d2012-02-13 23:10:39 +0000152 break;
153 case FPURegSet:
154 // TODO: fill in FPU regs....
155 //SetError (FPURegSet, Read, -1);
156 done = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000157
Greg Claytonc859e2d2012-02-13 23:10:39 +0000158 break;
159 case EXCRegSet:
160 exc.trapno = data.GetU32(&offset);
161 exc.err = data.GetU32(&offset);
162 exc.faultvaddr = data.GetU64(&offset);
163 SetError (EXCRegSet, Read, 0);
164 done = true;
165 break;
166 case 7:
167 case 8:
168 case 9:
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000169 // fancy flavors that encapsulate of the above
170 // flavors...
Greg Claytonc859e2d2012-02-13 23:10:39 +0000171 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000172
Greg Claytonc859e2d2012-02-13 23:10:39 +0000173 default:
174 done = true;
175 break;
176 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000177 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000178 }
179 }
Jason Molendad20359d2014-11-11 10:59:15 +0000180
Greg Claytona2715cf2014-06-13 00:54:12 +0000181 static size_t
182 WriteRegister (RegisterContext *reg_ctx, const char *name, const char *alt_name, size_t reg_byte_size, Stream &data)
183 {
184 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
185 if (reg_info == NULL)
186 reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
187 if (reg_info)
188 {
189 lldb_private::RegisterValue reg_value;
190 if (reg_ctx->ReadRegister(reg_info, reg_value))
191 {
192 if (reg_info->byte_size >= reg_byte_size)
193 data.Write(reg_value.GetBytes(), reg_byte_size);
194 else
195 {
196 data.Write(reg_value.GetBytes(), reg_info->byte_size);
197 for (size_t i=0, n = reg_byte_size - reg_info->byte_size; i<n; ++ i)
198 data.PutChar(0);
199 }
200 return reg_byte_size;
201 }
202 }
203 // Just write zeros if all else fails
204 for (size_t i=0; i<reg_byte_size; ++ i)
205 data.PutChar(0);
206 return reg_byte_size;
207 }
Jason Molendad20359d2014-11-11 10:59:15 +0000208
Greg Claytona2715cf2014-06-13 00:54:12 +0000209 static bool
210 Create_LC_THREAD (Thread *thread, Stream &data)
211 {
212 RegisterContextSP reg_ctx_sp (thread->GetRegisterContext());
213 if (reg_ctx_sp)
214 {
215 RegisterContext *reg_ctx = reg_ctx_sp.get();
216
217 data.PutHex32 (GPRRegSet); // Flavor
Jason Molenda22952582014-11-12 01:11:36 +0000218 data.PutHex32 (GPRWordCount);
Greg Claytona2715cf2014-06-13 00:54:12 +0000219 WriteRegister (reg_ctx, "rax", NULL, 8, data);
220 WriteRegister (reg_ctx, "rbx", NULL, 8, data);
221 WriteRegister (reg_ctx, "rcx", NULL, 8, data);
222 WriteRegister (reg_ctx, "rdx", NULL, 8, data);
223 WriteRegister (reg_ctx, "rdi", NULL, 8, data);
224 WriteRegister (reg_ctx, "rsi", NULL, 8, data);
225 WriteRegister (reg_ctx, "rbp", NULL, 8, data);
226 WriteRegister (reg_ctx, "rsp", NULL, 8, data);
227 WriteRegister (reg_ctx, "r8", NULL, 8, data);
228 WriteRegister (reg_ctx, "r9", NULL, 8, data);
229 WriteRegister (reg_ctx, "r10", NULL, 8, data);
230 WriteRegister (reg_ctx, "r11", NULL, 8, data);
231 WriteRegister (reg_ctx, "r12", NULL, 8, data);
232 WriteRegister (reg_ctx, "r13", NULL, 8, data);
233 WriteRegister (reg_ctx, "r14", NULL, 8, data);
234 WriteRegister (reg_ctx, "r15", NULL, 8, data);
235 WriteRegister (reg_ctx, "rip", NULL, 8, data);
236 WriteRegister (reg_ctx, "rflags", NULL, 8, data);
237 WriteRegister (reg_ctx, "cs", NULL, 8, data);
238 WriteRegister (reg_ctx, "fs", NULL, 8, data);
239 WriteRegister (reg_ctx, "gs", NULL, 8, data);
240
241// // Write out the FPU registers
242// const size_t fpu_byte_size = sizeof(FPU);
243// size_t bytes_written = 0;
244// data.PutHex32 (FPURegSet);
245// data.PutHex32 (fpu_byte_size/sizeof(uint64_t));
246// bytes_written += data.PutHex32(0); // uint32_t pad[0]
247// bytes_written += data.PutHex32(0); // uint32_t pad[1]
248// bytes_written += WriteRegister (reg_ctx, "fcw", "fctrl", 2, data); // uint16_t fcw; // "fctrl"
249// bytes_written += WriteRegister (reg_ctx, "fsw" , "fstat", 2, data); // uint16_t fsw; // "fstat"
250// bytes_written += WriteRegister (reg_ctx, "ftw" , "ftag", 1, data); // uint8_t ftw; // "ftag"
251// bytes_written += data.PutHex8 (0); // uint8_t pad1;
252// bytes_written += WriteRegister (reg_ctx, "fop" , NULL, 2, data); // uint16_t fop; // "fop"
253// bytes_written += WriteRegister (reg_ctx, "fioff", "ip", 4, data); // uint32_t ip; // "fioff"
254// bytes_written += WriteRegister (reg_ctx, "fiseg", NULL, 2, data); // uint16_t cs; // "fiseg"
255// bytes_written += data.PutHex16 (0); // uint16_t pad2;
256// bytes_written += WriteRegister (reg_ctx, "dp", "fooff" , 4, data); // uint32_t dp; // "fooff"
257// bytes_written += WriteRegister (reg_ctx, "foseg", NULL, 2, data); // uint16_t ds; // "foseg"
258// bytes_written += data.PutHex16 (0); // uint16_t pad3;
259// bytes_written += WriteRegister (reg_ctx, "mxcsr", NULL, 4, data); // uint32_t mxcsr;
260// bytes_written += WriteRegister (reg_ctx, "mxcsrmask", NULL, 4, data);// uint32_t mxcsrmask;
261// bytes_written += WriteRegister (reg_ctx, "stmm0", NULL, sizeof(MMSReg), data);
262// bytes_written += WriteRegister (reg_ctx, "stmm1", NULL, sizeof(MMSReg), data);
263// bytes_written += WriteRegister (reg_ctx, "stmm2", NULL, sizeof(MMSReg), data);
264// bytes_written += WriteRegister (reg_ctx, "stmm3", NULL, sizeof(MMSReg), data);
265// bytes_written += WriteRegister (reg_ctx, "stmm4", NULL, sizeof(MMSReg), data);
266// bytes_written += WriteRegister (reg_ctx, "stmm5", NULL, sizeof(MMSReg), data);
267// bytes_written += WriteRegister (reg_ctx, "stmm6", NULL, sizeof(MMSReg), data);
268// bytes_written += WriteRegister (reg_ctx, "stmm7", NULL, sizeof(MMSReg), data);
269// bytes_written += WriteRegister (reg_ctx, "xmm0" , NULL, sizeof(XMMReg), data);
270// bytes_written += WriteRegister (reg_ctx, "xmm1" , NULL, sizeof(XMMReg), data);
271// bytes_written += WriteRegister (reg_ctx, "xmm2" , NULL, sizeof(XMMReg), data);
272// bytes_written += WriteRegister (reg_ctx, "xmm3" , NULL, sizeof(XMMReg), data);
273// bytes_written += WriteRegister (reg_ctx, "xmm4" , NULL, sizeof(XMMReg), data);
274// bytes_written += WriteRegister (reg_ctx, "xmm5" , NULL, sizeof(XMMReg), data);
275// bytes_written += WriteRegister (reg_ctx, "xmm6" , NULL, sizeof(XMMReg), data);
276// bytes_written += WriteRegister (reg_ctx, "xmm7" , NULL, sizeof(XMMReg), data);
277// bytes_written += WriteRegister (reg_ctx, "xmm8" , NULL, sizeof(XMMReg), data);
278// bytes_written += WriteRegister (reg_ctx, "xmm9" , NULL, sizeof(XMMReg), data);
279// bytes_written += WriteRegister (reg_ctx, "xmm10", NULL, sizeof(XMMReg), data);
280// bytes_written += WriteRegister (reg_ctx, "xmm11", NULL, sizeof(XMMReg), data);
281// bytes_written += WriteRegister (reg_ctx, "xmm12", NULL, sizeof(XMMReg), data);
282// bytes_written += WriteRegister (reg_ctx, "xmm13", NULL, sizeof(XMMReg), data);
283// bytes_written += WriteRegister (reg_ctx, "xmm14", NULL, sizeof(XMMReg), data);
284// bytes_written += WriteRegister (reg_ctx, "xmm15", NULL, sizeof(XMMReg), data);
285//
286// // Fill rest with zeros
287// for (size_t i=0, n = fpu_byte_size - bytes_written; i<n; ++ i)
288// data.PutChar(0);
289
290 // Write out the EXC registers
291 data.PutHex32 (EXCRegSet);
Jason Molenda22952582014-11-12 01:11:36 +0000292 data.PutHex32 (EXCWordCount);
Greg Claytona2715cf2014-06-13 00:54:12 +0000293 WriteRegister (reg_ctx, "trapno", NULL, 4, data);
294 WriteRegister (reg_ctx, "err", NULL, 4, data);
295 WriteRegister (reg_ctx, "faultvaddr", NULL, 8, data);
296 return true;
297 }
298 return false;
299 }
300
Greg Claytonc859e2d2012-02-13 23:10:39 +0000301protected:
Eugene Zelenko8157a882015-10-23 16:56:07 +0000302 int
303 DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000304 {
305 return 0;
306 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000307
Eugene Zelenko8157a882015-10-23 16:56:07 +0000308 int
309 DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000310 {
311 return 0;
312 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000313
Eugene Zelenko8157a882015-10-23 16:56:07 +0000314 int
315 DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000316 {
317 return 0;
318 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000319
Eugene Zelenko8157a882015-10-23 16:56:07 +0000320 int
321 DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000322 {
323 return 0;
324 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000325
Eugene Zelenko8157a882015-10-23 16:56:07 +0000326 int
327 DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000328 {
329 return 0;
330 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000331
Eugene Zelenko8157a882015-10-23 16:56:07 +0000332 int
333 DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000334 {
335 return 0;
336 }
337};
Greg Claytonc3776bf2012-02-09 06:16:32 +0000338
Jason Molenda4e7511e2013-03-06 23:19:17 +0000339class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386
Greg Claytonc859e2d2012-02-13 23:10:39 +0000340{
341public:
342 RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
343 RegisterContextDarwin_i386 (thread, 0)
344 {
345 SetRegisterDataFrom_LC_THREAD (data);
346 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000347
Eugene Zelenko8157a882015-10-23 16:56:07 +0000348 void
349 InvalidateAllRegisters() override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000350 {
351 // Do nothing... registers are always valid...
352 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000353
Greg Claytonc859e2d2012-02-13 23:10:39 +0000354 void
355 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
356 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000357 lldb::offset_t offset = 0;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000358 SetError (GPRRegSet, Read, -1);
359 SetError (FPURegSet, Read, -1);
360 SetError (EXCRegSet, Read, -1);
361 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000362
Greg Claytonc859e2d2012-02-13 23:10:39 +0000363 while (!done)
364 {
365 int flavor = data.GetU32 (&offset);
366 if (flavor == 0)
367 done = true;
368 else
Greg Claytonc3776bf2012-02-09 06:16:32 +0000369 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000370 uint32_t i;
371 uint32_t count = data.GetU32 (&offset);
372 switch (flavor)
373 {
374 case GPRRegSet:
375 for (i=0; i<count; ++i)
376 (&gpr.eax)[i] = data.GetU32(&offset);
377 SetError (GPRRegSet, Read, 0);
378 done = true;
379
380 break;
381 case FPURegSet:
382 // TODO: fill in FPU regs....
383 //SetError (FPURegSet, Read, -1);
384 done = true;
385
386 break;
387 case EXCRegSet:
388 exc.trapno = data.GetU32(&offset);
389 exc.err = data.GetU32(&offset);
390 exc.faultvaddr = data.GetU32(&offset);
391 SetError (EXCRegSet, Read, 0);
392 done = true;
393 break;
394 case 7:
395 case 8:
396 case 9:
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000397 // fancy flavors that encapsulate of the above
398 // flavors...
Greg Claytonc859e2d2012-02-13 23:10:39 +0000399 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +0000400
Greg Claytonc859e2d2012-02-13 23:10:39 +0000401 default:
402 done = true;
403 break;
404 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000405 }
406 }
407 }
Jason Molendad20359d2014-11-11 10:59:15 +0000408
409 static size_t
410 WriteRegister (RegisterContext *reg_ctx, const char *name, const char *alt_name, size_t reg_byte_size, Stream &data)
411 {
412 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
413 if (reg_info == NULL)
414 reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
415 if (reg_info)
416 {
417 lldb_private::RegisterValue reg_value;
418 if (reg_ctx->ReadRegister(reg_info, reg_value))
419 {
420 if (reg_info->byte_size >= reg_byte_size)
421 data.Write(reg_value.GetBytes(), reg_byte_size);
422 else
423 {
424 data.Write(reg_value.GetBytes(), reg_info->byte_size);
425 for (size_t i=0, n = reg_byte_size - reg_info->byte_size; i<n; ++ i)
426 data.PutChar(0);
427 }
428 return reg_byte_size;
429 }
430 }
431 // Just write zeros if all else fails
432 for (size_t i=0; i<reg_byte_size; ++ i)
433 data.PutChar(0);
434 return reg_byte_size;
435 }
436
437 static bool
438 Create_LC_THREAD (Thread *thread, Stream &data)
439 {
440 RegisterContextSP reg_ctx_sp (thread->GetRegisterContext());
441 if (reg_ctx_sp)
442 {
443 RegisterContext *reg_ctx = reg_ctx_sp.get();
444
445 data.PutHex32 (GPRRegSet); // Flavor
Jason Molenda22952582014-11-12 01:11:36 +0000446 data.PutHex32 (GPRWordCount);
Jason Molendad20359d2014-11-11 10:59:15 +0000447 WriteRegister (reg_ctx, "eax", NULL, 4, data);
448 WriteRegister (reg_ctx, "ebx", NULL, 4, data);
449 WriteRegister (reg_ctx, "ecx", NULL, 4, data);
450 WriteRegister (reg_ctx, "edx", NULL, 4, data);
451 WriteRegister (reg_ctx, "edi", NULL, 4, data);
452 WriteRegister (reg_ctx, "esi", NULL, 4, data);
453 WriteRegister (reg_ctx, "ebp", NULL, 4, data);
454 WriteRegister (reg_ctx, "esp", NULL, 4, data);
455 WriteRegister (reg_ctx, "ss", NULL, 4, data);
456 WriteRegister (reg_ctx, "eflags", NULL, 4, data);
457 WriteRegister (reg_ctx, "eip", NULL, 4, data);
458 WriteRegister (reg_ctx, "cs", NULL, 4, data);
459 WriteRegister (reg_ctx, "ds", NULL, 4, data);
460 WriteRegister (reg_ctx, "es", NULL, 4, data);
461 WriteRegister (reg_ctx, "fs", NULL, 4, data);
462 WriteRegister (reg_ctx, "gs", NULL, 4, data);
463
464 // Write out the EXC registers
465 data.PutHex32 (EXCRegSet);
Jason Molenda22952582014-11-12 01:11:36 +0000466 data.PutHex32 (EXCWordCount);
Jason Molendad20359d2014-11-11 10:59:15 +0000467 WriteRegister (reg_ctx, "trapno", NULL, 4, data);
468 WriteRegister (reg_ctx, "err", NULL, 4, data);
Jason Molenda22952582014-11-12 01:11:36 +0000469 WriteRegister (reg_ctx, "faultvaddr", NULL, 4, data);
Jason Molendad20359d2014-11-11 10:59:15 +0000470 return true;
471 }
472 return false;
473 }
474
Greg Claytonc3776bf2012-02-09 06:16:32 +0000475protected:
Eugene Zelenko8157a882015-10-23 16:56:07 +0000476 int
477 DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override
Greg Claytonc3776bf2012-02-09 06:16:32 +0000478 {
479 return 0;
480 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000481
Eugene Zelenko8157a882015-10-23 16:56:07 +0000482 int
483 DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override
Greg Claytonc3776bf2012-02-09 06:16:32 +0000484 {
485 return 0;
486 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000487
Eugene Zelenko8157a882015-10-23 16:56:07 +0000488 int
489 DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override
Greg Claytonc3776bf2012-02-09 06:16:32 +0000490 {
491 return 0;
492 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000493
Eugene Zelenko8157a882015-10-23 16:56:07 +0000494 int
495 DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override
Greg Claytonc3776bf2012-02-09 06:16:32 +0000496 {
497 return 0;
498 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000499
Eugene Zelenko8157a882015-10-23 16:56:07 +0000500 int
501 DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override
Greg Claytonc3776bf2012-02-09 06:16:32 +0000502 {
503 return 0;
504 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000505
Eugene Zelenko8157a882015-10-23 16:56:07 +0000506 int
507 DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override
Greg Claytonc3776bf2012-02-09 06:16:32 +0000508 {
509 return 0;
510 }
511};
512
Jason Molenda4e7511e2013-03-06 23:19:17 +0000513class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm
Greg Claytonc859e2d2012-02-13 23:10:39 +0000514{
515public:
516 RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
Greg Claytonc2807462012-10-30 23:57:32 +0000517 RegisterContextDarwin_arm (thread, 0)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000518 {
519 SetRegisterDataFrom_LC_THREAD (data);
520 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000521
Eugene Zelenko8157a882015-10-23 16:56:07 +0000522 void
523 InvalidateAllRegisters() override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000524 {
525 // Do nothing... registers are always valid...
526 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000527
Greg Claytonc859e2d2012-02-13 23:10:39 +0000528 void
529 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
530 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000531 lldb::offset_t offset = 0;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000532 SetError (GPRRegSet, Read, -1);
533 SetError (FPURegSet, Read, -1);
534 SetError (EXCRegSet, Read, -1);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000535 bool done = false;
536
537 while (!done)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000538 {
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000539 int flavor = data.GetU32 (&offset);
540 uint32_t count = data.GetU32 (&offset);
Jason Molendaddf91772013-05-14 04:50:47 +0000541 lldb::offset_t next_thread_state = offset + (count * 4);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000542 switch (flavor)
543 {
Jason Molendac197e812016-01-20 05:17:13 +0000544 case GPRAltRegSet:
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000545 case GPRRegSet:
546 for (uint32_t i=0; i<count; ++i)
Jason Molendaddf91772013-05-14 04:50:47 +0000547 {
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000548 gpr.r[i] = data.GetU32(&offset);
Jason Molendaddf91772013-05-14 04:50:47 +0000549 }
550
551 // Note that gpr.cpsr is also copied by the above loop; this loop technically extends
552 // one element past the end of the gpr.r[] array.
553
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000554 SetError (GPRRegSet, Read, 0);
Jason Molendaddf91772013-05-14 04:50:47 +0000555 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000556 break;
557
558 case FPURegSet:
559 {
Jason Molenda663d2e12013-05-14 03:52:22 +0000560 uint8_t *fpu_reg_buf = (uint8_t*) &fpu.floats.s[0];
561 const int fpu_reg_buf_size = sizeof (fpu.floats);
562 if (data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000563 {
Jason Molenda663d2e12013-05-14 03:52:22 +0000564 offset += fpu_reg_buf_size;
565 fpu.fpscr = data.GetU32(&offset);
566 SetError (FPURegSet, Read, 0);
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000567 }
Jason Molenda663d2e12013-05-14 03:52:22 +0000568 else
569 {
570 done = true;
571 }
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000572 }
Jason Molendaddf91772013-05-14 04:50:47 +0000573 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000574 break;
575
576 case EXCRegSet:
Jason Molendaddf91772013-05-14 04:50:47 +0000577 if (count == 3)
578 {
579 exc.exception = data.GetU32(&offset);
580 exc.fsr = data.GetU32(&offset);
581 exc.far = data.GetU32(&offset);
582 SetError (EXCRegSet, Read, 0);
583 }
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000584 done = true;
Jason Molendaddf91772013-05-14 04:50:47 +0000585 offset = next_thread_state;
Jason Molenda2e7236fa2013-05-14 03:25:58 +0000586 break;
587
588 // Unknown register set flavor, stop trying to parse.
589 default:
590 done = true;
591 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000592 }
593 }
Jason Molenda22952582014-11-12 01:11:36 +0000594
595 static size_t
596 WriteRegister (RegisterContext *reg_ctx, const char *name, const char *alt_name, size_t reg_byte_size, Stream &data)
597 {
598 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
599 if (reg_info == NULL)
600 reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
601 if (reg_info)
602 {
603 lldb_private::RegisterValue reg_value;
604 if (reg_ctx->ReadRegister(reg_info, reg_value))
605 {
606 if (reg_info->byte_size >= reg_byte_size)
607 data.Write(reg_value.GetBytes(), reg_byte_size);
608 else
609 {
610 data.Write(reg_value.GetBytes(), reg_info->byte_size);
611 for (size_t i=0, n = reg_byte_size - reg_info->byte_size; i<n; ++ i)
612 data.PutChar(0);
613 }
614 return reg_byte_size;
615 }
616 }
617 // Just write zeros if all else fails
618 for (size_t i=0; i<reg_byte_size; ++ i)
619 data.PutChar(0);
620 return reg_byte_size;
621 }
622
623 static bool
624 Create_LC_THREAD (Thread *thread, Stream &data)
625 {
626 RegisterContextSP reg_ctx_sp (thread->GetRegisterContext());
627 if (reg_ctx_sp)
628 {
629 RegisterContext *reg_ctx = reg_ctx_sp.get();
630
631 data.PutHex32 (GPRRegSet); // Flavor
632 data.PutHex32 (GPRWordCount);
633 WriteRegister (reg_ctx, "r0", NULL, 4, data);
634 WriteRegister (reg_ctx, "r1", NULL, 4, data);
635 WriteRegister (reg_ctx, "r2", NULL, 4, data);
636 WriteRegister (reg_ctx, "r3", NULL, 4, data);
637 WriteRegister (reg_ctx, "r4", NULL, 4, data);
638 WriteRegister (reg_ctx, "r5", NULL, 4, data);
639 WriteRegister (reg_ctx, "r6", NULL, 4, data);
640 WriteRegister (reg_ctx, "r7", NULL, 4, data);
641 WriteRegister (reg_ctx, "r8", NULL, 4, data);
642 WriteRegister (reg_ctx, "r9", NULL, 4, data);
643 WriteRegister (reg_ctx, "r10", NULL, 4, data);
644 WriteRegister (reg_ctx, "r11", NULL, 4, data);
645 WriteRegister (reg_ctx, "r12", NULL, 4, data);
646 WriteRegister (reg_ctx, "sp", NULL, 4, data);
647 WriteRegister (reg_ctx, "lr", NULL, 4, data);
648 WriteRegister (reg_ctx, "pc", NULL, 4, data);
649 WriteRegister (reg_ctx, "cpsr", NULL, 4, data);
650
651 // Write out the EXC registers
652// data.PutHex32 (EXCRegSet);
653// data.PutHex32 (EXCWordCount);
654// WriteRegister (reg_ctx, "exception", NULL, 4, data);
655// WriteRegister (reg_ctx, "fsr", NULL, 4, data);
656// WriteRegister (reg_ctx, "far", NULL, 4, data);
657 return true;
658 }
659 return false;
660 }
661
Greg Claytonc859e2d2012-02-13 23:10:39 +0000662protected:
Eugene Zelenko8157a882015-10-23 16:56:07 +0000663 int
664 DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000665 {
Jason Molendaddf91772013-05-14 04:50:47 +0000666 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000667 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000668
Eugene Zelenko8157a882015-10-23 16:56:07 +0000669 int
670 DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000671 {
Jason Molendaddf91772013-05-14 04:50:47 +0000672 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000673 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000674
Eugene Zelenko8157a882015-10-23 16:56:07 +0000675 int
676 DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000677 {
Jason Molendaddf91772013-05-14 04:50:47 +0000678 return -1;
Greg Claytonc859e2d2012-02-13 23:10:39 +0000679 }
Greg Claytonc2807462012-10-30 23:57:32 +0000680
Eugene Zelenko8157a882015-10-23 16:56:07 +0000681 int
682 DoReadDBG(lldb::tid_t tid, int flavor, DBG &dbg) override
Greg Claytonc2807462012-10-30 23:57:32 +0000683 {
684 return -1;
685 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000686
Eugene Zelenko8157a882015-10-23 16:56:07 +0000687 int
688 DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000689 {
690 return 0;
691 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000692
Eugene Zelenko8157a882015-10-23 16:56:07 +0000693 int
694 DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000695 {
696 return 0;
697 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000698
Eugene Zelenko8157a882015-10-23 16:56:07 +0000699 int
700 DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override
Greg Claytonc859e2d2012-02-13 23:10:39 +0000701 {
702 return 0;
703 }
Jason Molenda4e7511e2013-03-06 23:19:17 +0000704
Eugene Zelenko8157a882015-10-23 16:56:07 +0000705 int
706 DoWriteDBG(lldb::tid_t tid, int flavor, const DBG &dbg) override
Greg Claytonc2807462012-10-30 23:57:32 +0000707 {
708 return -1;
709 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000710};
711
Jason Molendaa3329782014-03-29 18:54:20 +0000712class RegisterContextDarwin_arm64_Mach : public RegisterContextDarwin_arm64
713{
714public:
715 RegisterContextDarwin_arm64_Mach (lldb_private::Thread &thread, const DataExtractor &data) :
716 RegisterContextDarwin_arm64 (thread, 0)
717 {
718 SetRegisterDataFrom_LC_THREAD (data);
719 }
720
Eugene Zelenko8157a882015-10-23 16:56:07 +0000721 void
722 InvalidateAllRegisters() override
Jason Molendaa3329782014-03-29 18:54:20 +0000723 {
724 // Do nothing... registers are always valid...
725 }
726
727 void
728 SetRegisterDataFrom_LC_THREAD (const DataExtractor &data)
729 {
730 lldb::offset_t offset = 0;
731 SetError (GPRRegSet, Read, -1);
732 SetError (FPURegSet, Read, -1);
733 SetError (EXCRegSet, Read, -1);
734 bool done = false;
735 while (!done)
736 {
737 int flavor = data.GetU32 (&offset);
738 uint32_t count = data.GetU32 (&offset);
739 lldb::offset_t next_thread_state = offset + (count * 4);
740 switch (flavor)
741 {
742 case GPRRegSet:
743 // x0-x29 + fp + lr + sp + pc (== 33 64-bit registers) plus cpsr (1 32-bit register)
744 if (count >= (33 * 2) + 1)
745 {
746 for (uint32_t i=0; i<33; ++i)
747 gpr.x[i] = data.GetU64(&offset);
748 gpr.cpsr = data.GetU32(&offset);
749 SetError (GPRRegSet, Read, 0);
750 }
751 offset = next_thread_state;
752 break;
753 case FPURegSet:
754 {
755 uint8_t *fpu_reg_buf = (uint8_t*) &fpu.v[0];
756 const int fpu_reg_buf_size = sizeof (fpu);
757 if (fpu_reg_buf_size == count
758 && data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size)
759 {
760 SetError (FPURegSet, Read, 0);
761 }
762 else
763 {
764 done = true;
765 }
766 }
767 offset = next_thread_state;
768 break;
769 case EXCRegSet:
770 if (count == 4)
771 {
772 exc.far = data.GetU64(&offset);
773 exc.esr = data.GetU32(&offset);
774 exc.exception = data.GetU32(&offset);
775 SetError (EXCRegSet, Read, 0);
776 }
777 offset = next_thread_state;
778 break;
779 default:
780 done = true;
781 break;
782 }
783 }
784 }
Jason Molenda22952582014-11-12 01:11:36 +0000785
786 static size_t
787 WriteRegister (RegisterContext *reg_ctx, const char *name, const char *alt_name, size_t reg_byte_size, Stream &data)
788 {
789 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
790 if (reg_info == NULL)
791 reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
792 if (reg_info)
793 {
794 lldb_private::RegisterValue reg_value;
795 if (reg_ctx->ReadRegister(reg_info, reg_value))
796 {
797 if (reg_info->byte_size >= reg_byte_size)
798 data.Write(reg_value.GetBytes(), reg_byte_size);
799 else
800 {
801 data.Write(reg_value.GetBytes(), reg_info->byte_size);
802 for (size_t i=0, n = reg_byte_size - reg_info->byte_size; i<n; ++ i)
803 data.PutChar(0);
804 }
805 return reg_byte_size;
806 }
807 }
808 // Just write zeros if all else fails
809 for (size_t i=0; i<reg_byte_size; ++ i)
810 data.PutChar(0);
811 return reg_byte_size;
812 }
813
814 static bool
815 Create_LC_THREAD (Thread *thread, Stream &data)
816 {
817 RegisterContextSP reg_ctx_sp (thread->GetRegisterContext());
818 if (reg_ctx_sp)
819 {
820 RegisterContext *reg_ctx = reg_ctx_sp.get();
821
822 data.PutHex32 (GPRRegSet); // Flavor
823 data.PutHex32 (GPRWordCount);
824 WriteRegister (reg_ctx, "x0", NULL, 8, data);
825 WriteRegister (reg_ctx, "x1", NULL, 8, data);
826 WriteRegister (reg_ctx, "x2", NULL, 8, data);
827 WriteRegister (reg_ctx, "x3", NULL, 8, data);
828 WriteRegister (reg_ctx, "x4", NULL, 8, data);
829 WriteRegister (reg_ctx, "x5", NULL, 8, data);
830 WriteRegister (reg_ctx, "x6", NULL, 8, data);
831 WriteRegister (reg_ctx, "x7", NULL, 8, data);
832 WriteRegister (reg_ctx, "x8", NULL, 8, data);
833 WriteRegister (reg_ctx, "x9", NULL, 8, data);
834 WriteRegister (reg_ctx, "x10", NULL, 8, data);
835 WriteRegister (reg_ctx, "x11", NULL, 8, data);
836 WriteRegister (reg_ctx, "x12", NULL, 8, data);
837 WriteRegister (reg_ctx, "x13", NULL, 8, data);
838 WriteRegister (reg_ctx, "x14", NULL, 8, data);
839 WriteRegister (reg_ctx, "x15", NULL, 8, data);
840 WriteRegister (reg_ctx, "x16", NULL, 8, data);
841 WriteRegister (reg_ctx, "x17", NULL, 8, data);
842 WriteRegister (reg_ctx, "x18", NULL, 8, data);
843 WriteRegister (reg_ctx, "x19", NULL, 8, data);
844 WriteRegister (reg_ctx, "x20", NULL, 8, data);
845 WriteRegister (reg_ctx, "x21", NULL, 8, data);
846 WriteRegister (reg_ctx, "x22", NULL, 8, data);
847 WriteRegister (reg_ctx, "x23", NULL, 8, data);
848 WriteRegister (reg_ctx, "x24", NULL, 8, data);
849 WriteRegister (reg_ctx, "x25", NULL, 8, data);
850 WriteRegister (reg_ctx, "x26", NULL, 8, data);
851 WriteRegister (reg_ctx, "x27", NULL, 8, data);
852 WriteRegister (reg_ctx, "x28", NULL, 8, data);
853 WriteRegister (reg_ctx, "fp", NULL, 8, data);
854 WriteRegister (reg_ctx, "lr", NULL, 8, data);
855 WriteRegister (reg_ctx, "sp", NULL, 8, data);
856 WriteRegister (reg_ctx, "pc", NULL, 8, data);
857 WriteRegister (reg_ctx, "cpsr", NULL, 4, data);
858
859 // Write out the EXC registers
860// data.PutHex32 (EXCRegSet);
861// data.PutHex32 (EXCWordCount);
862// WriteRegister (reg_ctx, "far", NULL, 8, data);
863// WriteRegister (reg_ctx, "esr", NULL, 4, data);
864// WriteRegister (reg_ctx, "exception", NULL, 4, data);
865 return true;
866 }
867 return false;
868 }
869
Jason Molendaa3329782014-03-29 18:54:20 +0000870protected:
Eugene Zelenko8157a882015-10-23 16:56:07 +0000871 int
872 DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override
Jason Molendaa3329782014-03-29 18:54:20 +0000873 {
874 return -1;
875 }
876
Eugene Zelenko8157a882015-10-23 16:56:07 +0000877 int
878 DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override
Jason Molendaa3329782014-03-29 18:54:20 +0000879 {
880 return -1;
881 }
882
Eugene Zelenko8157a882015-10-23 16:56:07 +0000883 int
884 DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override
Jason Molendaa3329782014-03-29 18:54:20 +0000885 {
886 return -1;
887 }
888
Eugene Zelenko8157a882015-10-23 16:56:07 +0000889 int
890 DoReadDBG(lldb::tid_t tid, int flavor, DBG &dbg) override
Jason Molendaa3329782014-03-29 18:54:20 +0000891 {
892 return -1;
893 }
894
Eugene Zelenko8157a882015-10-23 16:56:07 +0000895 int
896 DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override
Jason Molendaa3329782014-03-29 18:54:20 +0000897 {
898 return 0;
899 }
900
Eugene Zelenko8157a882015-10-23 16:56:07 +0000901 int
902 DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override
Jason Molendaa3329782014-03-29 18:54:20 +0000903 {
904 return 0;
905 }
906
Eugene Zelenko8157a882015-10-23 16:56:07 +0000907 int
908 DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override
Jason Molendaa3329782014-03-29 18:54:20 +0000909 {
910 return 0;
911 }
912
Eugene Zelenko8157a882015-10-23 16:56:07 +0000913 int
914 DoWriteDBG(lldb::tid_t tid, int flavor, const DBG &dbg) override
Jason Molendaa3329782014-03-29 18:54:20 +0000915 {
916 return -1;
917 }
918};
919
Greg Clayton9aae0a12013-05-15 19:52:08 +0000920static uint32_t
921MachHeaderSizeFromMagic(uint32_t magic)
922{
923 switch (magic)
924 {
Charles Davis510938e2013-08-27 05:04:57 +0000925 case MH_MAGIC:
926 case MH_CIGAM:
Greg Clayton9aae0a12013-05-15 19:52:08 +0000927 return sizeof(struct mach_header);
928
Charles Davis510938e2013-08-27 05:04:57 +0000929 case MH_MAGIC_64:
930 case MH_CIGAM_64:
Greg Clayton9aae0a12013-05-15 19:52:08 +0000931 return sizeof(struct mach_header_64);
932 break;
933
934 default:
935 break;
936 }
937 return 0;
938}
939
Greg Claytonded470d2011-03-19 01:12:21 +0000940#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941
942void
943ObjectFileMachO::Initialize()
944{
945 PluginManager::RegisterPlugin (GetPluginNameStatic(),
946 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +0000947 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000948 CreateMemoryInstance,
Greg Claytona2715cf2014-06-13 00:54:12 +0000949 GetModuleSpecifications,
950 SaveCore);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951}
952
953void
954ObjectFileMachO::Terminate()
955{
956 PluginManager::UnregisterPlugin (CreateInstance);
957}
958
Greg Clayton57abc5d2013-05-10 21:47:16 +0000959lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000960ObjectFileMachO::GetPluginNameStatic()
961{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000962 static ConstString g_name("mach-o");
963 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000964}
965
966const char *
967ObjectFileMachO::GetPluginDescriptionStatic()
968{
969 return "Mach-o object file reader (32 and 64 bit)";
970}
971
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972ObjectFile *
Greg Clayton5ce9c562013-02-06 17:22:03 +0000973ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp,
974 DataBufferSP& data_sp,
975 lldb::offset_t data_offset,
976 const FileSpec* file,
977 lldb::offset_t file_offset,
978 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000979{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000980 if (!data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981 {
Greg Clayton736888c2015-02-23 23:47:09 +0000982 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
Greg Clayton5ce9c562013-02-06 17:22:03 +0000983 data_offset = 0;
984 }
985
986 if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length))
987 {
988 // Update the data to contain the entire file if it doesn't already
989 if (data_sp->GetByteSize() < length)
990 {
Greg Clayton736888c2015-02-23 23:47:09 +0000991 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
Greg Clayton5ce9c562013-02-06 17:22:03 +0000992 data_offset = 0;
993 }
Greg Clayton7b0992d2013-04-18 22:45:39 +0000994 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, data_offset, file, file_offset, length));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995 if (objfile_ap.get() && objfile_ap->ParseHeader())
996 return objfile_ap.release();
997 }
998 return NULL;
999}
1000
Greg Claytonc9660542012-02-05 02:38:54 +00001001ObjectFile *
Jason Molenda4e7511e2013-03-06 23:19:17 +00001002ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
1003 DataBufferSP& data_sp,
1004 const ProcessSP &process_sp,
Greg Claytonc9660542012-02-05 02:38:54 +00001005 lldb::addr_t header_addr)
1006{
1007 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
1008 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001009 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr));
Greg Claytonc9660542012-02-05 02:38:54 +00001010 if (objfile_ap.get() && objfile_ap->ParseHeader())
1011 return objfile_ap.release();
1012 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001013 return NULL;
Greg Claytonc9660542012-02-05 02:38:54 +00001014}
1015
Greg Claytonf4d6de62013-04-24 22:29:28 +00001016size_t
1017ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file,
1018 lldb::DataBufferSP& data_sp,
1019 lldb::offset_t data_offset,
1020 lldb::offset_t file_offset,
1021 lldb::offset_t length,
1022 lldb_private::ModuleSpecList &specs)
1023{
1024 const size_t initial_count = specs.GetSize();
1025
1026 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
1027 {
1028 DataExtractor data;
1029 data.SetData(data_sp);
1030 llvm::MachO::mach_header header;
1031 if (ParseHeader (data, &data_offset, header))
1032 {
Jason Molenda48cd3332014-04-22 04:52:30 +00001033 size_t header_and_load_cmds = header.sizeofcmds + MachHeaderSizeFromMagic(header.magic);
1034 if (header_and_load_cmds >= data_sp->GetByteSize())
Greg Claytonf4d6de62013-04-24 22:29:28 +00001035 {
Jason Molenda48cd3332014-04-22 04:52:30 +00001036 data_sp = file.ReadFileContents(file_offset, header_and_load_cmds);
Greg Clayton2540a8a2013-07-12 22:07:46 +00001037 data.SetData(data_sp);
1038 data_offset = MachHeaderSizeFromMagic(header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00001039 }
1040 if (data_sp)
1041 {
1042 ModuleSpec spec;
1043 spec.GetFileSpec() = file;
Greg Clayton7ab7f892014-05-29 21:33:45 +00001044 spec.SetObjectOffset(file_offset);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001045 spec.SetObjectSize(length);
1046
Greg Clayton7ab7f892014-05-29 21:33:45 +00001047 if (GetArchitecture (header, data, data_offset, spec.GetArchitecture()))
Jason Molendab000e4d2013-08-27 02:22:06 +00001048 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001049 if (spec.GetArchitecture().IsValid())
1050 {
1051 GetUUID (header, data, data_offset, spec.GetUUID());
1052 specs.Append(spec);
1053 }
Greg Claytonf4d6de62013-04-24 22:29:28 +00001054 }
1055 }
1056 }
1057 }
1058 return specs.GetSize() - initial_count;
1059}
1060
Greg Claytonc9660542012-02-05 02:38:54 +00001061const ConstString &
1062ObjectFileMachO::GetSegmentNameTEXT()
1063{
1064 static ConstString g_segment_name_TEXT ("__TEXT");
1065 return g_segment_name_TEXT;
1066}
1067
1068const ConstString &
1069ObjectFileMachO::GetSegmentNameDATA()
1070{
1071 static ConstString g_segment_name_DATA ("__DATA");
1072 return g_segment_name_DATA;
1073}
1074
1075const ConstString &
Greg Claytona381e102015-07-27 23:21:05 +00001076ObjectFileMachO::GetSegmentNameDATA_DIRTY()
1077{
1078 static ConstString g_segment_name ("__DATA_DIRTY");
1079 return g_segment_name;
1080}
1081
1082const ConstString &
1083ObjectFileMachO::GetSegmentNameDATA_CONST()
1084{
1085 static ConstString g_segment_name ("__DATA_CONST");
1086 return g_segment_name;
1087}
1088
1089const ConstString &
Greg Claytonc9660542012-02-05 02:38:54 +00001090ObjectFileMachO::GetSegmentNameOBJC()
1091{
1092 static ConstString g_segment_name_OBJC ("__OBJC");
1093 return g_segment_name_OBJC;
1094}
1095
1096const ConstString &
1097ObjectFileMachO::GetSegmentNameLINKEDIT()
1098{
1099 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
1100 return g_section_name_LINKEDIT;
1101}
1102
1103const ConstString &
1104ObjectFileMachO::GetSectionNameEHFrame()
1105{
1106 static ConstString g_section_name_eh_frame ("__eh_frame");
1107 return g_section_name_eh_frame;
1108}
1109
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110bool
Jason Molenda4e7511e2013-03-06 23:19:17 +00001111ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
1112 lldb::addr_t data_offset,
Greg Clayton44435ed2012-01-12 05:25:17 +00001113 lldb::addr_t data_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114{
Greg Clayton44435ed2012-01-12 05:25:17 +00001115 DataExtractor data;
1116 data.SetData (data_sp, data_offset, data_length);
Greg Claytonc7bece562013-01-25 18:06:21 +00001117 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118 uint32_t magic = data.GetU32(&offset);
1119 return MachHeaderSizeFromMagic(magic) != 0;
1120}
1121
Greg Clayton5ce9c562013-02-06 17:22:03 +00001122ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp,
1123 DataBufferSP& data_sp,
1124 lldb::offset_t data_offset,
1125 const FileSpec* file,
1126 lldb::offset_t file_offset,
1127 lldb::offset_t length) :
1128 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonc3776bf2012-02-09 06:16:32 +00001129 m_mach_segments(),
1130 m_mach_sections(),
1131 m_entry_point_address(),
1132 m_thread_context_offsets(),
1133 m_thread_context_offsets_valid(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134{
Greg Clayton72b77eb2011-02-04 21:13:05 +00001135 ::memset (&m_header, 0, sizeof(m_header));
1136 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001137}
1138
Greg Claytone72dfb32012-02-24 01:59:29 +00001139ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +00001140 lldb::DataBufferSP& header_data_sp,
1141 const lldb::ProcessSP &process_sp,
1142 lldb::addr_t header_addr) :
Greg Claytone72dfb32012-02-24 01:59:29 +00001143 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonc3776bf2012-02-09 06:16:32 +00001144 m_mach_segments(),
1145 m_mach_sections(),
1146 m_entry_point_address(),
1147 m_thread_context_offsets(),
1148 m_thread_context_offsets_valid(false)
Greg Claytonc9660542012-02-05 02:38:54 +00001149{
1150 ::memset (&m_header, 0, sizeof(m_header));
1151 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
1152}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153
Greg Claytonf4d6de62013-04-24 22:29:28 +00001154bool
1155ObjectFileMachO::ParseHeader (DataExtractor &data,
1156 lldb::offset_t *data_offset_ptr,
1157 llvm::MachO::mach_header &header)
1158{
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001159 data.SetByteOrder (endian::InlHostByteOrder());
Greg Claytonf4d6de62013-04-24 22:29:28 +00001160 // Leave magic in the original byte order
1161 header.magic = data.GetU32(data_offset_ptr);
1162 bool can_parse = false;
1163 bool is_64_bit = false;
1164 switch (header.magic)
1165 {
Charles Davis510938e2013-08-27 05:04:57 +00001166 case MH_MAGIC:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001167 data.SetByteOrder (endian::InlHostByteOrder());
Greg Claytonf4d6de62013-04-24 22:29:28 +00001168 data.SetAddressByteSize(4);
1169 can_parse = true;
1170 break;
1171
Charles Davis510938e2013-08-27 05:04:57 +00001172 case MH_MAGIC_64:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001173 data.SetByteOrder (endian::InlHostByteOrder());
Greg Claytonf4d6de62013-04-24 22:29:28 +00001174 data.SetAddressByteSize(8);
1175 can_parse = true;
1176 is_64_bit = true;
1177 break;
1178
Charles Davis510938e2013-08-27 05:04:57 +00001179 case MH_CIGAM:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001180 data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Greg Claytonf4d6de62013-04-24 22:29:28 +00001181 data.SetAddressByteSize(4);
1182 can_parse = true;
1183 break;
1184
Charles Davis510938e2013-08-27 05:04:57 +00001185 case MH_CIGAM_64:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001186 data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Greg Claytonf4d6de62013-04-24 22:29:28 +00001187 data.SetAddressByteSize(8);
1188 is_64_bit = true;
1189 can_parse = true;
1190 break;
1191
1192 default:
1193 break;
1194 }
1195
1196 if (can_parse)
1197 {
1198 data.GetU32(data_offset_ptr, &header.cputype, 6);
1199 if (is_64_bit)
1200 *data_offset_ptr += 4;
1201 return true;
1202 }
1203 else
1204 {
1205 memset(&header, 0, sizeof(header));
1206 }
1207 return false;
1208}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209
1210bool
1211ObjectFileMachO::ParseHeader ()
1212{
Greg Claytona1743492012-03-13 23:14:29 +00001213 ModuleSP module_sp(GetModule());
1214 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215 {
Greg Claytona1743492012-03-13 23:14:29 +00001216 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
1217 bool can_parse = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00001218 lldb::offset_t offset = 0;
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001219 m_data.SetByteOrder (endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +00001220 // Leave magic in the original byte order
1221 m_header.magic = m_data.GetU32(&offset);
1222 switch (m_header.magic)
Greg Claytonc9660542012-02-05 02:38:54 +00001223 {
Charles Davis510938e2013-08-27 05:04:57 +00001224 case MH_MAGIC:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001225 m_data.SetByteOrder (endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +00001226 m_data.SetAddressByteSize(4);
1227 can_parse = true;
1228 break;
1229
Charles Davis510938e2013-08-27 05:04:57 +00001230 case MH_MAGIC_64:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001231 m_data.SetByteOrder (endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +00001232 m_data.SetAddressByteSize(8);
1233 can_parse = true;
1234 break;
1235
Charles Davis510938e2013-08-27 05:04:57 +00001236 case MH_CIGAM:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001237 m_data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Greg Claytona1743492012-03-13 23:14:29 +00001238 m_data.SetAddressByteSize(4);
1239 can_parse = true;
1240 break;
1241
Charles Davis510938e2013-08-27 05:04:57 +00001242 case MH_CIGAM_64:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001243 m_data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Greg Claytona1743492012-03-13 23:14:29 +00001244 m_data.SetAddressByteSize(8);
1245 can_parse = true;
1246 break;
1247
1248 default:
1249 break;
Greg Claytonc9660542012-02-05 02:38:54 +00001250 }
Greg Claytona1743492012-03-13 23:14:29 +00001251
1252 if (can_parse)
1253 {
1254 m_data.GetU32(&offset, &m_header.cputype, 6);
1255
Greg Clayton7ab7f892014-05-29 21:33:45 +00001256
1257 ArchSpec mach_arch;
1258
1259 if (GetArchitecture (mach_arch))
Greg Claytona1743492012-03-13 23:14:29 +00001260 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001261 // Check if the module has a required architecture
1262 const ArchSpec &module_arch = module_sp->GetArchitecture();
1263 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
1264 return false;
1265
1266 if (SetModulesArchitecture (mach_arch))
Greg Claytona1743492012-03-13 23:14:29 +00001267 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001268 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
1269 if (m_data.GetByteSize() < header_and_lc_size)
Greg Claytona1743492012-03-13 23:14:29 +00001270 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001271 DataBufferSP data_sp;
1272 ProcessSP process_sp (m_process_wp.lock());
1273 if (process_sp)
1274 {
1275 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
1276 }
1277 else
1278 {
1279 // Read in all only the load command data from the file on disk
1280 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
1281 if (data_sp->GetByteSize() != header_and_lc_size)
1282 return false;
1283 }
1284 if (data_sp)
1285 m_data.SetData (data_sp);
Greg Claytona1743492012-03-13 23:14:29 +00001286 }
Greg Claytona1743492012-03-13 23:14:29 +00001287 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00001288 return true;
Greg Claytona1743492012-03-13 23:14:29 +00001289 }
Greg Claytona1743492012-03-13 23:14:29 +00001290 }
1291 else
1292 {
1293 memset(&m_header, 0, sizeof(struct mach_header));
1294 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295 }
1296 return false;
1297}
1298
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001299ByteOrder
1300ObjectFileMachO::GetByteOrder () const
1301{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001302 return m_data.GetByteOrder ();
1303}
1304
Jim Ingham5aee1622010-08-09 23:31:02 +00001305bool
1306ObjectFileMachO::IsExecutable() const
1307{
Charles Davis510938e2013-08-27 05:04:57 +00001308 return m_header.filetype == MH_EXECUTE;
Jim Ingham5aee1622010-08-09 23:31:02 +00001309}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001310
Greg Claytonc7bece562013-01-25 18:06:21 +00001311uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312ObjectFileMachO::GetAddressByteSize () const
1313{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001314 return m_data.GetAddressByteSize ();
1315}
1316
Greg Claytone0d378b2011-03-24 21:19:54 +00001317AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +00001318ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
1319{
1320 Symtab *symtab = GetSymtab();
1321 if (symtab)
1322 {
1323 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
1324 if (symbol)
1325 {
Greg Claytone7612132012-03-07 21:03:09 +00001326 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +00001327 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00001328 SectionSP section_sp (symbol->GetAddressRef().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +00001329 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +00001330 {
Charles Davis510938e2013-08-27 05:04:57 +00001331 const lldb::SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +00001332 switch (section_type)
1333 {
Jason Molendae589e7e2014-12-08 03:09:00 +00001334 case eSectionTypeInvalid:
1335 return eAddressClassUnknown;
1336
Greg Claytonded470d2011-03-19 01:12:21 +00001337 case eSectionTypeCode:
Charles Davis510938e2013-08-27 05:04:57 +00001338 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +00001339 {
1340 // For ARM we have a bit in the n_desc field of the symbol
1341 // that tells us ARM/Thumb which is bit 0x0008.
1342 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1343 return eAddressClassCodeAlternateISA;
1344 }
1345 return eAddressClassCode;
1346
Jason Molendae589e7e2014-12-08 03:09:00 +00001347 case eSectionTypeContainer:
1348 return eAddressClassUnknown;
1349
Greg Clayton5009f9d2011-10-27 17:55:14 +00001350 case eSectionTypeData:
1351 case eSectionTypeDataCString:
1352 case eSectionTypeDataCStringPointers:
1353 case eSectionTypeDataSymbolAddress:
1354 case eSectionTypeData4:
1355 case eSectionTypeData8:
1356 case eSectionTypeData16:
1357 case eSectionTypeDataPointers:
1358 case eSectionTypeZeroFill:
1359 case eSectionTypeDataObjCMessageRefs:
1360 case eSectionTypeDataObjCCFStrings:
Ryan Brown65d4d5c2015-09-16 21:20:44 +00001361 case eSectionTypeGoSymtab:
Greg Clayton5009f9d2011-10-27 17:55:14 +00001362 return eAddressClassData;
Jason Molendae589e7e2014-12-08 03:09:00 +00001363
Greg Clayton5009f9d2011-10-27 17:55:14 +00001364 case eSectionTypeDebug:
1365 case eSectionTypeDWARFDebugAbbrev:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001366 case eSectionTypeDWARFDebugAddr:
Greg Clayton5009f9d2011-10-27 17:55:14 +00001367 case eSectionTypeDWARFDebugAranges:
1368 case eSectionTypeDWARFDebugFrame:
1369 case eSectionTypeDWARFDebugInfo:
1370 case eSectionTypeDWARFDebugLine:
1371 case eSectionTypeDWARFDebugLoc:
1372 case eSectionTypeDWARFDebugMacInfo:
Siva Chandrad8335e92015-12-16 00:22:08 +00001373 case eSectionTypeDWARFDebugMacro:
Greg Clayton5009f9d2011-10-27 17:55:14 +00001374 case eSectionTypeDWARFDebugPubNames:
1375 case eSectionTypeDWARFDebugPubTypes:
1376 case eSectionTypeDWARFDebugRanges:
1377 case eSectionTypeDWARFDebugStr:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001378 case eSectionTypeDWARFDebugStrOffsets:
Greg Clayton5009f9d2011-10-27 17:55:14 +00001379 case eSectionTypeDWARFAppleNames:
1380 case eSectionTypeDWARFAppleTypes:
1381 case eSectionTypeDWARFAppleNamespaces:
1382 case eSectionTypeDWARFAppleObjC:
1383 return eAddressClassDebug;
Jason Molendae589e7e2014-12-08 03:09:00 +00001384
1385 case eSectionTypeEHFrame:
Tamas Berghammer648f3c72015-09-30 13:50:14 +00001386 case eSectionTypeARMexidx:
1387 case eSectionTypeARMextab:
Jason Molendae589e7e2014-12-08 03:09:00 +00001388 case eSectionTypeCompactUnwind:
1389 return eAddressClassRuntime;
1390
Michael Sartaina7499c92013-07-01 19:45:50 +00001391 case eSectionTypeELFSymbolTable:
1392 case eSectionTypeELFDynamicSymbols:
1393 case eSectionTypeELFRelocationEntries:
1394 case eSectionTypeELFDynamicLinkInfo:
Jason Molendae589e7e2014-12-08 03:09:00 +00001395 case eSectionTypeOther:
1396 return eAddressClassUnknown;
Greg Claytonded470d2011-03-19 01:12:21 +00001397 }
1398 }
1399 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001400
Greg Claytone0d378b2011-03-24 21:19:54 +00001401 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +00001402 switch (symbol_type)
1403 {
1404 case eSymbolTypeAny: return eAddressClassUnknown;
1405 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda4e7511e2013-03-06 23:19:17 +00001406
Greg Claytonded470d2011-03-19 01:12:21 +00001407 case eSymbolTypeCode:
1408 case eSymbolTypeTrampoline:
Greg Clayton059f7242013-02-27 21:16:04 +00001409 case eSymbolTypeResolver:
Charles Davis510938e2013-08-27 05:04:57 +00001410 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +00001411 {
1412 // For ARM we have a bit in the n_desc field of the symbol
1413 // that tells us ARM/Thumb which is bit 0x0008.
1414 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1415 return eAddressClassCodeAlternateISA;
1416 }
1417 return eAddressClassCode;
1418
1419 case eSymbolTypeData: return eAddressClassData;
1420 case eSymbolTypeRuntime: return eAddressClassRuntime;
1421 case eSymbolTypeException: return eAddressClassRuntime;
1422 case eSymbolTypeSourceFile: return eAddressClassDebug;
1423 case eSymbolTypeHeaderFile: return eAddressClassDebug;
1424 case eSymbolTypeObjectFile: return eAddressClassDebug;
1425 case eSymbolTypeCommonBlock: return eAddressClassDebug;
1426 case eSymbolTypeBlock: return eAddressClassDebug;
1427 case eSymbolTypeLocal: return eAddressClassData;
1428 case eSymbolTypeParam: return eAddressClassData;
1429 case eSymbolTypeVariable: return eAddressClassData;
1430 case eSymbolTypeVariableType: return eAddressClassDebug;
1431 case eSymbolTypeLineEntry: return eAddressClassDebug;
1432 case eSymbolTypeLineHeader: return eAddressClassDebug;
1433 case eSymbolTypeScopeBegin: return eAddressClassDebug;
1434 case eSymbolTypeScopeEnd: return eAddressClassDebug;
1435 case eSymbolTypeAdditional: return eAddressClassUnknown;
1436 case eSymbolTypeCompiler: return eAddressClassDebug;
1437 case eSymbolTypeInstrumentation:return eAddressClassDebug;
1438 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +00001439 case eSymbolTypeObjCClass: return eAddressClassRuntime;
1440 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
1441 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Clayton9191db42013-10-21 18:40:51 +00001442 case eSymbolTypeReExported: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +00001443 }
1444 }
1445 }
1446 return eAddressClassUnknown;
1447}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001448
1449Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +00001450ObjectFileMachO::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451{
Greg Claytona1743492012-03-13 23:14:29 +00001452 ModuleSP module_sp(GetModule());
1453 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001454 {
Greg Claytona1743492012-03-13 23:14:29 +00001455 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
1456 if (m_symtab_ap.get() == NULL)
1457 {
1458 m_symtab_ap.reset(new Symtab(this));
1459 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton3046e662013-07-10 01:23:25 +00001460 ParseSymtab ();
Greg Claytona1743492012-03-13 23:14:29 +00001461 m_symtab_ap->Finalize ();
1462 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001463 }
1464 return m_symtab_ap.get();
1465}
1466
Greg Clayton3046e662013-07-10 01:23:25 +00001467bool
1468ObjectFileMachO::IsStripped ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001469{
Greg Clayton3046e662013-07-10 01:23:25 +00001470 if (m_dysymtab.cmd == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001471 {
Greg Clayton3046e662013-07-10 01:23:25 +00001472 ModuleSP module_sp(GetModule());
1473 if (module_sp)
Greg Claytona1743492012-03-13 23:14:29 +00001474 {
Greg Clayton3046e662013-07-10 01:23:25 +00001475 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1476 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton4d78c402012-05-25 18:09:55 +00001477 {
Greg Clayton3046e662013-07-10 01:23:25 +00001478 const lldb::offset_t load_cmd_offset = offset;
1479
1480 load_command lc;
1481 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
1482 break;
Charles Davis510938e2013-08-27 05:04:57 +00001483 if (lc.cmd == LC_DYSYMTAB)
Greg Clayton4d78c402012-05-25 18:09:55 +00001484 {
Greg Clayton3046e662013-07-10 01:23:25 +00001485 m_dysymtab.cmd = lc.cmd;
1486 m_dysymtab.cmdsize = lc.cmdsize;
1487 if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
1488 {
1489 // Clear m_dysymtab if we were unable to read all items from the load command
1490 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
1491 }
Greg Clayton4d78c402012-05-25 18:09:55 +00001492 }
Greg Clayton3046e662013-07-10 01:23:25 +00001493 offset = load_cmd_offset + lc.cmdsize;
Greg Clayton4d78c402012-05-25 18:09:55 +00001494 }
Greg Clayton1eac0c72012-04-24 03:06:13 +00001495 }
Greg Clayton1eac0c72012-04-24 03:06:13 +00001496 }
Greg Clayton3046e662013-07-10 01:23:25 +00001497 if (m_dysymtab.cmd)
Greg Clayton93e28612013-10-11 22:03:48 +00001498 return m_dysymtab.nlocalsym <= 1;
Greg Clayton3046e662013-07-10 01:23:25 +00001499 return false;
1500}
Greg Clayton1eac0c72012-04-24 03:06:13 +00001501
Greg Clayton3046e662013-07-10 01:23:25 +00001502void
1503ObjectFileMachO::CreateSections (SectionList &unified_section_list)
1504{
1505 if (!m_sections_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001506 {
Greg Clayton3046e662013-07-10 01:23:25 +00001507 m_sections_ap.reset(new SectionList());
1508
Charles Davis510938e2013-08-27 05:04:57 +00001509 const bool is_dsym = (m_header.filetype == MH_DSYM);
Greg Clayton3046e662013-07-10 01:23:25 +00001510 lldb::user_id_t segID = 0;
1511 lldb::user_id_t sectID = 0;
1512 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1513 uint32_t i;
1514 const bool is_core = GetType() == eTypeCoreFile;
1515 //bool dump_sections = false;
1516 ModuleSP module_sp (GetModule());
1517 // First look up any LC_ENCRYPTION_INFO load commands
1518 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
1519 EncryptedFileRanges encrypted_file_ranges;
1520 encryption_info_command encryption_cmd;
1521 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001522 {
Greg Clayton3046e662013-07-10 01:23:25 +00001523 const lldb::offset_t load_cmd_offset = offset;
1524 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
1525 break;
1526
Jason Molendadfb02a92015-04-02 05:19:33 +00001527 // LC_ENCRYPTION_INFO and LC_ENCRYPTION_INFO_64 have the same sizes for
1528 // the 3 fields we care about, so treat them the same.
1529 if (encryption_cmd.cmd == LC_ENCRYPTION_INFO || encryption_cmd.cmd == LC_ENCRYPTION_INFO_64)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001530 {
Greg Clayton3046e662013-07-10 01:23:25 +00001531 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
Jason Molendaf6ce26f2013-04-10 05:58:57 +00001532 {
Greg Clayton3046e662013-07-10 01:23:25 +00001533 if (encryption_cmd.cryptid != 0)
Greg Claytond37d6922013-04-16 16:51:19 +00001534 {
Greg Clayton3046e662013-07-10 01:23:25 +00001535 EncryptedFileRanges::Entry entry;
1536 entry.SetRangeBase(encryption_cmd.cryptoff);
1537 entry.SetByteSize(encryption_cmd.cryptsize);
1538 encrypted_file_ranges.Append(entry);
Jason Molendaf6ce26f2013-04-10 05:58:57 +00001539 }
1540 }
Greg Clayton3046e662013-07-10 01:23:25 +00001541 }
1542 offset = load_cmd_offset + encryption_cmd.cmdsize;
1543 }
1544
Jason Molenda05a09c62014-08-22 02:46:46 +00001545 bool section_file_addresses_changed = false;
1546
Greg Clayton3046e662013-07-10 01:23:25 +00001547 offset = MachHeaderSizeFromMagic(m_header.magic);
1548
1549 struct segment_command_64 load_cmd;
1550 for (i=0; i<m_header.ncmds; ++i)
1551 {
1552 const lldb::offset_t load_cmd_offset = offset;
1553 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1554 break;
1555
Charles Davis510938e2013-08-27 05:04:57 +00001556 if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64)
Greg Clayton3046e662013-07-10 01:23:25 +00001557 {
1558 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001559 {
Greg Clayton3046e662013-07-10 01:23:25 +00001560 bool add_section = true;
1561 bool add_to_unified = true;
1562 ConstString const_segname (load_cmd.segname, std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
Jason Molenda4e7511e2013-03-06 23:19:17 +00001563
Greg Clayton3046e662013-07-10 01:23:25 +00001564 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1565 if (is_dsym && unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001566 {
Greg Clayton3046e662013-07-10 01:23:25 +00001567 if (const_segname == GetSegmentNameLINKEDIT())
1568 {
1569 // We need to keep the __LINKEDIT segment private to this object file only
1570 add_to_unified = false;
1571 }
1572 else
1573 {
1574 // This is the dSYM file and this section has already been created by
1575 // the object file, no need to create it.
1576 add_section = false;
1577 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001578 }
Greg Clayton3046e662013-07-10 01:23:25 +00001579 load_cmd.vmaddr = m_data.GetAddress(&offset);
1580 load_cmd.vmsize = m_data.GetAddress(&offset);
1581 load_cmd.fileoff = m_data.GetAddress(&offset);
1582 load_cmd.filesize = m_data.GetAddress(&offset);
1583 if (m_length != 0 && load_cmd.filesize != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001584 {
Greg Clayton3046e662013-07-10 01:23:25 +00001585 if (load_cmd.fileoff > m_length)
1586 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001587 // We have a load command that says it extends past the end of the file. This is likely
Greg Clayton3046e662013-07-10 01:23:25 +00001588 // a corrupt file. We don't have any way to return an error condition here (this method
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001589 // was likely invoked from something like ObjectFile::GetSectionList()) -- all we can do
Greg Clayton3046e662013-07-10 01:23:25 +00001590 // is null out the SectionList vector and if a process has been set up, dump a message
1591 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001592 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001593 module_sp->ReportWarning("load command %u %s has a fileoff (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 "), ignoring this section",
Jason Molenda20eb31b2013-08-16 03:20:42 +00001594 i,
1595 lc_segment_name,
1596 load_cmd.fileoff,
1597 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001598
1599 load_cmd.fileoff = 0;
1600 load_cmd.filesize = 0;
1601 }
1602
1603 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1604 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001605 // We have a load command that says it extends past the end of the file. This is likely
Greg Clayton3046e662013-07-10 01:23:25 +00001606 // a corrupt file. We don't have any way to return an error condition here (this method
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001607 // was likely invoked from something like ObjectFile::GetSectionList()) -- all we can do
Greg Clayton3046e662013-07-10 01:23:25 +00001608 // is null out the SectionList vector and if a process has been set up, dump a message
1609 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001610 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001611 GetModule()->ReportWarning("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 to match",
Charles Davis510938e2013-08-27 05:04:57 +00001612 i,
1613 lc_segment_name,
1614 load_cmd.fileoff + load_cmd.filesize,
1615 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001616
1617 // Tuncase the length
1618 load_cmd.filesize = m_length - load_cmd.fileoff;
1619 }
1620 }
1621 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1622 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001623
Charles Davis510938e2013-08-27 05:04:57 +00001624 const bool segment_is_encrypted = (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001625
Greg Clayton3046e662013-07-10 01:23:25 +00001626 // Keep a list of mach segments around in case we need to
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001627 // get at data that isn't stored in the abstracted Sections.
Greg Clayton3046e662013-07-10 01:23:25 +00001628 m_mach_segments.push_back (load_cmd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001629
Greg Clayton3046e662013-07-10 01:23:25 +00001630 // Use a segment ID of the segment index shifted left by 8 so they
1631 // never conflict with any of the sections.
1632 SectionSP segment_sp;
1633 if (add_section && (const_segname || is_core))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001634 {
Greg Clayton3046e662013-07-10 01:23:25 +00001635 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
1636 this, // Object file to which this sections belongs
1637 ++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
1638 const_segname, // Name of this section
1639 eSectionTypeContainer, // This section is a container of other sections.
1640 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1641 load_cmd.vmsize, // VM size in bytes of this section
1642 load_cmd.fileoff, // Offset to the data for this section in the file
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001643 load_cmd.filesize, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +00001644 0, // Segments have no alignment information
Greg Clayton3046e662013-07-10 01:23:25 +00001645 load_cmd.flags)); // Flags for this section
Greg Clayton8d38ac42010-06-28 23:51:11 +00001646
Greg Clayton3046e662013-07-10 01:23:25 +00001647 segment_sp->SetIsEncrypted (segment_is_encrypted);
1648 m_sections_ap->AddSection(segment_sp);
1649 if (add_to_unified)
1650 unified_section_list.AddSection(segment_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001651 }
Greg Clayton3046e662013-07-10 01:23:25 +00001652 else if (unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001653 {
Jason Molenda20eb31b2013-08-16 03:20:42 +00001654 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr)
1655 {
1656 // Check to see if the module was read from memory?
1657 if (module_sp->GetObjectFile()->GetHeaderAddress().IsValid())
1658 {
1659 // We have a module that is in memory and needs to have its
1660 // file address adjusted. We need to do this because when we
1661 // load a file from memory, its addresses will be slid already,
1662 // yet the addresses in the new symbol file will still be unslid.
1663 // Since everything is stored as section offset, this shouldn't
1664 // cause any problems.
Jason Molenda5894a732013-08-17 03:39:52 +00001665
1666 // Make sure we've parsed the symbol table from the
1667 // ObjectFile before we go around changing its Sections.
1668 module_sp->GetObjectFile()->GetSymtab();
1669 // eh_frame would present the same problems but we parse that on
1670 // a per-function basis as-needed so it's more difficult to
1671 // remove its use of the Sections. Realistically, the environments
1672 // where this code path will be taken will not have eh_frame sections.
1673
Jason Molenda20eb31b2013-08-16 03:20:42 +00001674 unified_section_sp->SetFileAddress(load_cmd.vmaddr);
Jason Molenda05a09c62014-08-22 02:46:46 +00001675
1676 // Notify the module that the section addresses have been changed once
1677 // we're done so any file-address caches can be updated.
1678 section_file_addresses_changed = true;
Jason Molenda20eb31b2013-08-16 03:20:42 +00001679 }
1680 }
Greg Clayton3046e662013-07-10 01:23:25 +00001681 m_sections_ap->AddSection(unified_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001682 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683
Greg Clayton3046e662013-07-10 01:23:25 +00001684 struct section_64 sect64;
1685 ::memset (&sect64, 0, sizeof(sect64));
1686 // Push a section into our mach sections for the section at
Charles Davis510938e2013-08-27 05:04:57 +00001687 // index zero (NO_SECT) if we don't have any mach sections yet...
Greg Clayton3046e662013-07-10 01:23:25 +00001688 if (m_mach_sections.empty())
1689 m_mach_sections.push_back(sect64);
1690 uint32_t segment_sect_idx;
1691 const lldb::user_id_t first_segment_sectID = sectID + 1;
1692
1693
Charles Davis510938e2013-08-27 05:04:57 +00001694 const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8;
Greg Clayton3046e662013-07-10 01:23:25 +00001695 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001696 {
Greg Clayton3046e662013-07-10 01:23:25 +00001697 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
Greg Clayton89411422010-10-08 00:21:05 +00001698 break;
Greg Clayton3046e662013-07-10 01:23:25 +00001699 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1700 break;
1701 sect64.addr = m_data.GetAddress(&offset);
1702 sect64.size = m_data.GetAddress(&offset);
1703
1704 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1705 break;
1706
1707 // Keep a list of mach sections around in case we need to
1708 // get at data that isn't stored in the abstracted Sections.
1709 m_mach_sections.push_back (sect64);
1710
1711 if (add_section)
1712 {
1713 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1714 if (!const_segname)
1715 {
1716 // We have a segment with no name so we need to conjure up
1717 // segments that correspond to the section's segname if there
1718 // isn't already such a section. If there is such a section,
1719 // we resize the section so that it spans all sections.
1720 // We also mark these sections as fake so address matches don't
1721 // hit if they land in the gaps between the child sections.
1722 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1723 segment_sp = unified_section_list.FindSectionByName (const_segname);
1724 if (segment_sp.get())
1725 {
1726 Section *segment = segment_sp.get();
1727 // Grow the section size as needed.
1728 const lldb::addr_t sect64_min_addr = sect64.addr;
1729 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1730 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1731 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1732 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1733 if (sect64_min_addr >= curr_seg_min_addr)
1734 {
1735 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1736 // Only grow the section size if needed
1737 if (new_seg_byte_size > curr_seg_byte_size)
1738 segment->SetByteSize (new_seg_byte_size);
1739 }
1740 else
1741 {
1742 // We need to change the base address of the segment and
1743 // adjust the child section offsets for all existing children.
1744 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1745 segment->Slide(slide_amount, false);
1746 segment->GetChildren().Slide(-slide_amount, false);
1747 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1748 }
1749
1750 // Grow the section size as needed.
1751 if (sect64.offset)
1752 {
1753 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1754 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1755
1756 const lldb::addr_t section_min_file_offset = sect64.offset;
1757 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1758 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1759 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1760 segment->SetFileOffset (new_file_offset);
1761 segment->SetFileSize (new_file_size);
1762 }
1763 }
1764 else
1765 {
1766 // Create a fake section for the section's named segment
1767 segment_sp.reset(new Section (segment_sp, // Parent section
1768 module_sp, // Module to which this section belongs
1769 this, // Object file to which this section belongs
1770 ++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
1771 const_segname, // Name of this section
1772 eSectionTypeContainer, // This section is a container of other sections.
1773 sect64.addr, // File VM address == addresses as they are found in the object file
1774 sect64.size, // VM size in bytes of this section
1775 sect64.offset, // Offset to the data for this section in the file
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001776 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +00001777 sect64.align,
Greg Clayton3046e662013-07-10 01:23:25 +00001778 load_cmd.flags)); // Flags for this section
1779 segment_sp->SetIsFake(true);
1780
1781 m_sections_ap->AddSection(segment_sp);
1782 if (add_to_unified)
1783 unified_section_list.AddSection(segment_sp);
1784 segment_sp->SetIsEncrypted (segment_is_encrypted);
1785 }
1786 }
1787 assert (segment_sp.get());
1788
Charles Davis510938e2013-08-27 05:04:57 +00001789 lldb::SectionType sect_type = eSectionTypeOther;
Greg Clayton3046e662013-07-10 01:23:25 +00001790
Greg Clayton38f9cc42014-06-16 22:53:16 +00001791 if (sect64.flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS))
1792 sect_type = eSectionTypeCode;
1793 else
Greg Clayton3046e662013-07-10 01:23:25 +00001794 {
Greg Clayton38f9cc42014-06-16 22:53:16 +00001795 uint32_t mach_sect_type = sect64.flags & SECTION_TYPE;
1796 static ConstString g_sect_name_objc_data ("__objc_data");
1797 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1798 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1799 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1800 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1801 static ConstString g_sect_name_objc_const ("__objc_const");
1802 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1803 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton3046e662013-07-10 01:23:25 +00001804
Greg Clayton38f9cc42014-06-16 22:53:16 +00001805 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1806 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1807 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1808 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1809 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1810 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1811 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1812 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1813 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1814 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1815 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1816 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1817 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1818 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1819 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1820 static ConstString g_sect_name_eh_frame ("__eh_frame");
Jason Molendae589e7e2014-12-08 03:09:00 +00001821 static ConstString g_sect_name_compact_unwind ("__unwind_info");
Greg Clayton38f9cc42014-06-16 22:53:16 +00001822 static ConstString g_sect_name_text ("__text");
1823 static ConstString g_sect_name_data ("__data");
Ryan Brown65d4d5c2015-09-16 21:20:44 +00001824 static ConstString g_sect_name_go_symtab ("__gosymtab");
Greg Clayton38f9cc42014-06-16 22:53:16 +00001825
Greg Clayton38f9cc42014-06-16 22:53:16 +00001826 if (section_name == g_sect_name_dwarf_debug_abbrev)
1827 sect_type = eSectionTypeDWARFDebugAbbrev;
1828 else if (section_name == g_sect_name_dwarf_debug_aranges)
1829 sect_type = eSectionTypeDWARFDebugAranges;
1830 else if (section_name == g_sect_name_dwarf_debug_frame)
1831 sect_type = eSectionTypeDWARFDebugFrame;
1832 else if (section_name == g_sect_name_dwarf_debug_info)
1833 sect_type = eSectionTypeDWARFDebugInfo;
1834 else if (section_name == g_sect_name_dwarf_debug_line)
1835 sect_type = eSectionTypeDWARFDebugLine;
1836 else if (section_name == g_sect_name_dwarf_debug_loc)
1837 sect_type = eSectionTypeDWARFDebugLoc;
1838 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1839 sect_type = eSectionTypeDWARFDebugMacInfo;
1840 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1841 sect_type = eSectionTypeDWARFDebugPubNames;
1842 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1843 sect_type = eSectionTypeDWARFDebugPubTypes;
1844 else if (section_name == g_sect_name_dwarf_debug_ranges)
1845 sect_type = eSectionTypeDWARFDebugRanges;
1846 else if (section_name == g_sect_name_dwarf_debug_str)
1847 sect_type = eSectionTypeDWARFDebugStr;
1848 else if (section_name == g_sect_name_dwarf_apple_names)
1849 sect_type = eSectionTypeDWARFAppleNames;
1850 else if (section_name == g_sect_name_dwarf_apple_types)
1851 sect_type = eSectionTypeDWARFAppleTypes;
1852 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1853 sect_type = eSectionTypeDWARFAppleNamespaces;
1854 else if (section_name == g_sect_name_dwarf_apple_objc)
1855 sect_type = eSectionTypeDWARFAppleObjC;
1856 else if (section_name == g_sect_name_objc_selrefs)
1857 sect_type = eSectionTypeDataCStringPointers;
1858 else if (section_name == g_sect_name_objc_msgrefs)
1859 sect_type = eSectionTypeDataObjCMessageRefs;
1860 else if (section_name == g_sect_name_eh_frame)
1861 sect_type = eSectionTypeEHFrame;
Jason Molendae589e7e2014-12-08 03:09:00 +00001862 else if (section_name == g_sect_name_compact_unwind)
1863 sect_type = eSectionTypeCompactUnwind;
Greg Clayton38f9cc42014-06-16 22:53:16 +00001864 else if (section_name == g_sect_name_cfstring)
1865 sect_type = eSectionTypeDataObjCCFStrings;
Ryan Brown65d4d5c2015-09-16 21:20:44 +00001866 else if (section_name == g_sect_name_go_symtab)
1867 sect_type = eSectionTypeGoSymtab;
Greg Clayton38f9cc42014-06-16 22:53:16 +00001868 else if (section_name == g_sect_name_objc_data ||
1869 section_name == g_sect_name_objc_classrefs ||
1870 section_name == g_sect_name_objc_superrefs ||
1871 section_name == g_sect_name_objc_const ||
1872 section_name == g_sect_name_objc_classlist)
Greg Clayton3046e662013-07-10 01:23:25 +00001873 {
Greg Clayton38f9cc42014-06-16 22:53:16 +00001874 sect_type = eSectionTypeDataPointers;
1875 }
1876
1877 if (sect_type == eSectionTypeOther)
1878 {
1879 switch (mach_sect_type)
1880 {
1881 // TODO: categorize sections by other flags for regular sections
1882 case S_REGULAR:
1883 if (section_name == g_sect_name_text)
1884 sect_type = eSectionTypeCode;
1885 else if (section_name == g_sect_name_data)
1886 sect_type = eSectionTypeData;
1887 else
1888 sect_type = eSectionTypeOther;
1889 break;
1890 case S_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1891 case S_CSTRING_LITERALS: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1892 case S_4BYTE_LITERALS: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1893 case S_8BYTE_LITERALS: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1894 case S_LITERAL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1895 case S_NON_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1896 case S_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1897 case S_SYMBOL_STUBS: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1898 case S_MOD_INIT_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1899 case S_MOD_TERM_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1900 case S_COALESCED: sect_type = eSectionTypeOther; break;
1901 case S_GB_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1902 case S_INTERPOSING: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1903 case S_16BYTE_LITERALS: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1904 case S_DTRACE_DOF: sect_type = eSectionTypeDebug; break;
1905 case S_LAZY_DYLIB_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break;
1906 default: break;
1907 }
Greg Clayton3046e662013-07-10 01:23:25 +00001908 }
1909 }
1910
1911 SectionSP section_sp(new Section (segment_sp,
1912 module_sp,
1913 this,
1914 ++sectID,
1915 section_name,
1916 sect_type,
1917 sect64.addr - segment_sp->GetFileAddress(),
1918 sect64.size,
1919 sect64.offset,
1920 sect64.offset == 0 ? 0 : sect64.size,
Greg Clayton48672af2014-06-24 22:22:43 +00001921 sect64.align,
Greg Clayton3046e662013-07-10 01:23:25 +00001922 sect64.flags));
1923 // Set the section to be encrypted to match the segment
1924
1925 bool section_is_encrypted = false;
1926 if (!segment_is_encrypted && load_cmd.filesize != 0)
1927 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1928
1929 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1930 segment_sp->GetChildren().AddSection(section_sp);
1931
1932 if (segment_sp->IsFake())
1933 {
1934 segment_sp.reset();
1935 const_segname.Clear();
1936 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001937 }
1938 }
Greg Clayton3046e662013-07-10 01:23:25 +00001939 if (segment_sp && is_dsym)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001940 {
Greg Clayton3046e662013-07-10 01:23:25 +00001941 if (first_segment_sectID <= sectID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001942 {
Greg Clayton3046e662013-07-10 01:23:25 +00001943 lldb::user_id_t sect_uid;
1944 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001945 {
Greg Clayton3046e662013-07-10 01:23:25 +00001946 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1947 SectionSP next_section_sp;
1948 if (sect_uid + 1 <= sectID)
1949 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1950
1951 if (curr_section_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001952 {
Greg Clayton3046e662013-07-10 01:23:25 +00001953 if (curr_section_sp->GetByteSize() == 0)
1954 {
1955 if (next_section_sp.get() != NULL)
1956 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1957 else
1958 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1959 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001960 }
1961 }
1962 }
1963 }
1964 }
1965 }
1966 }
Charles Davis510938e2013-08-27 05:04:57 +00001967 else if (load_cmd.cmd == LC_DYSYMTAB)
Greg Clayton3046e662013-07-10 01:23:25 +00001968 {
1969 m_dysymtab.cmd = load_cmd.cmd;
1970 m_dysymtab.cmdsize = load_cmd.cmdsize;
1971 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1972 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001973
Greg Clayton3046e662013-07-10 01:23:25 +00001974 offset = load_cmd_offset + load_cmd.cmdsize;
1975 }
Jason Molenda05a09c62014-08-22 02:46:46 +00001976
1977
1978 if (section_file_addresses_changed && module_sp.get())
1979 {
1980 module_sp->SectionFileAddressesChanged();
1981 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001982 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001983}
1984
1985class MachSymtabSectionInfo
1986{
1987public:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001988 MachSymtabSectionInfo (SectionList *section_list) :
1989 m_section_list (section_list),
1990 m_section_infos()
1991 {
1992 // Get the number of sections down to a depth of 1 to include
1993 // all segments and their sections, but no other sections that
1994 // may be added for debug map or
1995 m_section_infos.resize(section_list->GetNumSections(1));
1996 }
1997
Greg Claytone72dfb32012-02-24 01:59:29 +00001998 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001999 GetSection (uint8_t n_sect, addr_t file_addr)
2000 {
2001 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00002002 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002003 if (n_sect < m_section_infos.size())
2004 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002005 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002006 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002007 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
2008 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002009 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00002010 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002011 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
2012 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00002013 }
2014 else
2015 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002016 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00002017 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002018 }
2019 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00002020 {
2021 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00002022 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00002023 }
2024 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
2025 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
2026 {
2027 // Symbol is in section with zero size, but has the same start
2028 // address as the section. This can happen with linker symbols
2029 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00002030 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00002031 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002032 }
Greg Claytone72dfb32012-02-24 01:59:29 +00002033 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002034 }
2035
2036protected:
2037 struct SectionInfo
2038 {
2039 SectionInfo () :
2040 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00002041 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002042 {
2043 }
2044
2045 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00002046 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002047 };
2048 SectionList *m_section_list;
2049 std::vector<SectionInfo> m_section_infos;
2050};
2051
Greg Clayton9191db42013-10-21 18:40:51 +00002052struct TrieEntry
2053{
2054 TrieEntry () :
2055 name(),
2056 address(LLDB_INVALID_ADDRESS),
2057 flags (0),
2058 other(0),
2059 import_name()
2060 {
2061 }
2062
2063 void
2064 Clear ()
2065 {
2066 name.Clear();
2067 address = LLDB_INVALID_ADDRESS;
2068 flags = 0;
2069 other = 0;
2070 import_name.Clear();
2071 }
2072
2073 void
2074 Dump () const
2075 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002076 printf ("0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"",
2077 static_cast<unsigned long long>(address),
2078 static_cast<unsigned long long>(flags),
2079 static_cast<unsigned long long>(other), name.GetCString());
Greg Clayton9191db42013-10-21 18:40:51 +00002080 if (import_name)
2081 printf (" -> \"%s\"\n", import_name.GetCString());
2082 else
2083 printf ("\n");
2084 }
2085 ConstString name;
2086 uint64_t address;
2087 uint64_t flags;
2088 uint64_t other;
2089 ConstString import_name;
2090};
2091
2092struct TrieEntryWithOffset
2093{
2094 lldb::offset_t nodeOffset;
2095 TrieEntry entry;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002096
Greg Clayton9191db42013-10-21 18:40:51 +00002097 TrieEntryWithOffset (lldb::offset_t offset) :
2098 nodeOffset (offset),
2099 entry()
2100 {
2101 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002102
Greg Clayton9191db42013-10-21 18:40:51 +00002103 void
2104 Dump (uint32_t idx) const
2105 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002106 printf ("[%3u] 0x%16.16llx: ", idx,
2107 static_cast<unsigned long long>(nodeOffset));
Greg Clayton9191db42013-10-21 18:40:51 +00002108 entry.Dump();
2109 }
2110
2111 bool
2112 operator<(const TrieEntryWithOffset& other) const
2113 {
2114 return ( nodeOffset < other.nodeOffset );
2115 }
2116};
2117
Greg Clayton8f265f72015-10-28 20:49:34 +00002118static bool
Greg Clayton9191db42013-10-21 18:40:51 +00002119ParseTrieEntries (DataExtractor &data,
2120 lldb::offset_t offset,
Greg Claytonb887da12015-07-16 19:50:57 +00002121 const bool is_arm,
Greg Clayton9191db42013-10-21 18:40:51 +00002122 std::vector<llvm::StringRef> &nameSlices,
2123 std::set<lldb::addr_t> &resolver_addresses,
2124 std::vector<TrieEntryWithOffset>& output)
2125{
2126 if (!data.ValidOffset(offset))
Greg Clayton8f265f72015-10-28 20:49:34 +00002127 return true;
Greg Clayton9191db42013-10-21 18:40:51 +00002128
2129 const uint64_t terminalSize = data.GetULEB128(&offset);
2130 lldb::offset_t children_offset = offset + terminalSize;
2131 if ( terminalSize != 0 ) {
2132 TrieEntryWithOffset e (offset);
2133 e.entry.flags = data.GetULEB128(&offset);
2134 const char *import_name = NULL;
2135 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
2136 e.entry.address = 0;
2137 e.entry.other = data.GetULEB128(&offset); // dylib ordinal
2138 import_name = data.GetCStr(&offset);
2139 }
2140 else {
2141 e.entry.address = data.GetULEB128(&offset);
2142 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
2143 {
Greg Clayton9191db42013-10-21 18:40:51 +00002144 e.entry.other = data.GetULEB128(&offset);
Greg Claytonb887da12015-07-16 19:50:57 +00002145 uint64_t resolver_addr = e.entry.other;
2146 if (is_arm)
2147 resolver_addr &= THUMB_ADDRESS_BIT_MASK;
2148 resolver_addresses.insert(resolver_addr);
Greg Clayton9191db42013-10-21 18:40:51 +00002149 }
2150 else
2151 e.entry.other = 0;
2152 }
2153 // Only add symbols that are reexport symbols with a valid import name
2154 if (EXPORT_SYMBOL_FLAGS_REEXPORT & e.entry.flags && import_name && import_name[0])
2155 {
2156 std::string name;
2157 if (!nameSlices.empty())
2158 {
2159 for (auto name_slice: nameSlices)
2160 name.append(name_slice.data(), name_slice.size());
2161 }
2162 if (name.size() > 1)
2163 {
2164 // Skip the leading '_'
2165 e.entry.name.SetCStringWithLength(name.c_str() + 1,name.size() - 1);
2166 }
2167 if (import_name)
2168 {
2169 // Skip the leading '_'
2170 e.entry.import_name.SetCString(import_name+1);
2171 }
2172 output.push_back(e);
2173 }
2174 }
2175
2176 const uint8_t childrenCount = data.GetU8(&children_offset);
2177 for (uint8_t i=0; i < childrenCount; ++i) {
Greg Clayton8f265f72015-10-28 20:49:34 +00002178 const char *cstr = data.GetCStr(&children_offset);
2179 if (cstr)
2180 nameSlices.push_back(llvm::StringRef(cstr));
2181 else
2182 return false; // Corrupt data
Greg Clayton9191db42013-10-21 18:40:51 +00002183 lldb::offset_t childNodeOffset = data.GetULEB128(&children_offset);
2184 if (childNodeOffset)
2185 {
Greg Clayton8f265f72015-10-28 20:49:34 +00002186 if (!ParseTrieEntries(data,
2187 childNodeOffset,
2188 is_arm,
2189 nameSlices,
2190 resolver_addresses,
2191 output))
2192 {
2193 return false;
2194 }
Greg Clayton9191db42013-10-21 18:40:51 +00002195 }
2196 nameSlices.pop_back();
2197 }
Greg Clayton8f265f72015-10-28 20:49:34 +00002198 return true;
Greg Clayton9191db42013-10-21 18:40:51 +00002199}
2200
Jason Molenda649a6072015-11-10 05:21:54 +00002201// Read the UUID out of a dyld_shared_cache file on-disk.
2202UUID
2203ObjectFileMachO::GetSharedCacheUUID (FileSpec dyld_shared_cache, const ByteOrder byte_order, const uint32_t addr_byte_size)
2204{
2205 UUID dsc_uuid;
2206 DataBufferSP dsc_data_sp = dyld_shared_cache.MemoryMapFileContentsIfLocal(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
2207 if (dsc_data_sp)
2208 {
2209 DataExtractor dsc_header_data (dsc_data_sp, byte_order, addr_byte_size);
2210
2211 char version_str[7];
2212 lldb::offset_t offset = 0;
2213 memcpy (version_str, dsc_header_data.GetData (&offset, 6), 6);
2214 version_str[6] = '\0';
2215 if (strcmp (version_str, "dyld_v") == 0)
2216 {
2217 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
2218 uint8_t uuid_bytes[sizeof (uuid_t)];
2219 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
2220 dsc_uuid.SetBytes (uuid_bytes);
2221 }
2222 }
2223 return dsc_uuid;
2224}
2225
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002226size_t
Greg Clayton3046e662013-07-10 01:23:25 +00002227ObjectFileMachO::ParseSymtab ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002228{
2229 Timer scoped_timer(__PRETTY_FUNCTION__,
2230 "ObjectFileMachO::ParseSymtab () module = %s",
2231 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002232 ModuleSP module_sp (GetModule());
2233 if (!module_sp)
2234 return 0;
2235
2236 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
2237 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
Greg Clayton9191db42013-10-21 18:40:51 +00002238 struct dyld_info_command dyld_info = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002239 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
2240 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00002241 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002242 uint32_t i;
Greg Clayton9191db42013-10-21 18:40:51 +00002243 FileSpecList dylib_files;
Greg Clayton5160ce52013-03-27 23:08:40 +00002244 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton1e28adf2015-02-25 17:25:02 +00002245 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2246 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2247 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
Greg Clayton77ccca72011-12-30 00:32:24 +00002248
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002249 for (i=0; i<m_header.ncmds; ++i)
2250 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002251 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002252 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002253 struct load_command lc;
2254 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002255 break;
2256 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002257 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002258 {
Charles Davis510938e2013-08-27 05:04:57 +00002259 case LC_SYMTAB:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002260 symtab_load_command.cmd = lc.cmd;
2261 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002262 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002263 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
2264 return 0;
2265 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002266 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002267 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002268 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002269 return 0;
2270 }
2271
2272 if (symtab_load_command.stroff == 0)
2273 {
2274 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002275 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002276 return 0;
2277 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002278
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002279 if (symtab_load_command.nsyms == 0)
2280 {
2281 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002282 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002283 return 0;
2284 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002285
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002286 if (symtab_load_command.strsize == 0)
2287 {
2288 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002289 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002290 return 0;
2291 }
2292 break;
2293
Greg Clayton9191db42013-10-21 18:40:51 +00002294 case LC_DYLD_INFO:
2295 case LC_DYLD_INFO_ONLY:
2296 if (m_data.GetU32(&offset, &dyld_info.rebase_off, 10))
2297 {
2298 dyld_info.cmd = lc.cmd;
2299 dyld_info.cmdsize = lc.cmdsize;
2300 }
2301 else
2302 {
2303 memset (&dyld_info, 0, sizeof(dyld_info));
2304 }
2305 break;
2306
2307 case LC_LOAD_DYLIB:
2308 case LC_LOAD_WEAK_DYLIB:
2309 case LC_REEXPORT_DYLIB:
2310 case LC_LOADFVMLIB:
2311 case LC_LOAD_UPWARD_DYLIB:
2312 {
2313 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
2314 const char *path = m_data.PeekCStr(name_offset);
2315 if (path)
2316 {
2317 FileSpec file_spec(path, false);
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002318 // Strip the path if there is @rpath, @executable, etc so we just use the basename
Greg Clayton9191db42013-10-21 18:40:51 +00002319 if (path[0] == '@')
2320 file_spec.GetDirectory().Clear();
Jim Inghamfbe0b9a2014-05-21 03:58:03 +00002321
2322 if (lc.cmd == LC_REEXPORT_DYLIB)
2323 {
2324 m_reexported_dylibs.AppendIfUnique(file_spec);
2325 }
Greg Clayton9191db42013-10-21 18:40:51 +00002326
2327 dylib_files.Append(file_spec);
2328 }
2329 }
2330 break;
2331
Charles Davis510938e2013-08-27 05:04:57 +00002332 case LC_FUNCTION_STARTS:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002333 function_starts_load_command.cmd = lc.cmd;
2334 function_starts_load_command.cmdsize = lc.cmdsize;
2335 if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields
Virgile Bellob2f1fb22013-08-23 12:44:05 +00002336 memset (&function_starts_load_command, 0, sizeof(function_starts_load_command));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002337 break;
2338
2339 default:
2340 break;
2341 }
2342 offset = cmd_offset + lc.cmdsize;
2343 }
2344
2345 if (symtab_load_command.cmd)
2346 {
2347 Symtab *symtab = m_symtab_ap.get();
2348 SectionList *section_list = GetSectionList();
2349 if (section_list == NULL)
2350 return 0;
2351
Greg Claytonc7bece562013-01-25 18:06:21 +00002352 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
2353 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002354 bool bit_width_32 = addr_byte_size == 4;
2355 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
2356
Greg Claytonc7bece562013-01-25 18:06:21 +00002357 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
2358 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
2359 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00002360 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00002361 DataExtractor dyld_trie_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002362
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002363 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
2364 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00002365 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
Greg Claytonfd814c52013-08-13 01:42:25 +00002366
2367 ProcessSP process_sp (m_process_wp.lock());
2368 Process *process = process_sp.get();
2369
Greg Clayton86eac942013-08-13 21:32:34 +00002370 uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete;
2371
Greg Clayton48672af2014-06-24 22:22:43 +00002372 if (process && m_header.filetype != llvm::MachO::MH_OBJECT)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002373 {
Greg Clayton4c82d422012-05-18 23:20:01 +00002374 Target &target = process->GetTarget();
Greg Claytonfd814c52013-08-13 01:42:25 +00002375
Greg Clayton86eac942013-08-13 21:32:34 +00002376 memory_module_load_level = target.GetMemoryModuleLoadLevel();
Greg Claytonfd814c52013-08-13 01:42:25 +00002377
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002378 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2379 // Reading mach file from memory in a process or core file...
2380
2381 if (linkedit_section_sp)
2382 {
Greg Clayton07347372015-06-08 21:53:11 +00002383 addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
2384 if (linkedit_load_addr == LLDB_INVALID_ADDRESS)
2385 {
2386 // We might be trying to access the symbol table before the __LINKEDIT's load
2387 // address has been set in the target. We can't fail to read the symbol table,
2388 // so calculate the right address manually
2389 linkedit_load_addr = CalculateSectionLoadAddressForMemoryImage(m_memory_addr, GetMachHeaderSection(), linkedit_section_sp.get());
2390 }
2391
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002392 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2393 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00002394 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00002395
2396 bool data_was_read = false;
2397
Todd Fiala013434e2014-07-09 01:29:05 +00002398#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molendaa3329782014-03-29 18:54:20 +00002399 if (m_header.flags & 0x80000000u && process->GetAddressByteSize() == sizeof (void*))
Greg Clayton77ccca72011-12-30 00:32:24 +00002400 {
Greg Clayton26b47e22012-04-18 05:19:20 +00002401 // This mach-o memory file is in the dyld shared cache. If this
2402 // program is not remote and this is iOS, then this process will
2403 // share the same shared cache as the process we are debugging and
2404 // we can read the entire __LINKEDIT from the address space in this
2405 // process. This is a needed optimization that is used for local iOS
2406 // debugging only since all shared libraries in the shared cache do
2407 // not have corresponding files that exist in the file system of the
2408 // device. They have been combined into a single file. This means we
2409 // always have to load these files from memory. All of the symbol and
2410 // string tables from all of the __LINKEDIT sections from the shared
2411 // libraries in the shared cache have been merged into a single large
2412 // symbol and string table. Reading all of this symbol and string table
2413 // data across can slow down debug launch times, so we optimize this by
2414 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00002415
2416 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
2417 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
2418 bool use_lldb_cache = true;
2419 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
2420 {
2421 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002422 ModuleSP module_sp (GetModule());
2423 if (module_sp)
2424 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
2425
Jason Molenda0e0954c2013-04-16 06:24:42 +00002426 }
2427
Greg Clayton26b47e22012-04-18 05:19:20 +00002428 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00002429 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00002430 {
2431 data_was_read = true;
2432 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00002433 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00002434 if (function_starts_load_command.cmd)
2435 {
2436 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2437 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
2438 }
2439 }
2440 }
2441#endif
2442
2443 if (!data_was_read)
2444 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002445 if (memory_module_load_level == eMemoryModuleLoadLevelComplete)
Jason Molendad34e6522013-02-05 22:31:24 +00002446 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002447 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
2448 if (nlist_data_sp)
2449 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2450 // Load strings individually from memory when loading from memory since shared cache
2451 // string tables contain strings for all symbols from all shared cached libraries
2452 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
2453 //if (strtab_data_sp)
2454 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
2455 if (m_dysymtab.nindirectsyms != 0)
2456 {
2457 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
2458 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
2459 if (indirect_syms_data_sp)
2460 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
2461 }
Jason Molendad34e6522013-02-05 22:31:24 +00002462 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002463
2464 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial)
Greg Clayton26b47e22012-04-18 05:19:20 +00002465 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002466 if (function_starts_load_command.cmd)
2467 {
2468 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2469 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
2470 if (func_start_data_sp)
2471 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
2472 }
Greg Clayton26b47e22012-04-18 05:19:20 +00002473 }
Greg Clayton77ccca72011-12-30 00:32:24 +00002474 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002475 }
2476 }
2477 else
2478 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002479 nlist_data.SetData (m_data,
2480 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002481 nlist_data_byte_size);
2482 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00002483 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002484 strtab_data_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00002485
2486 if (dyld_info.export_size > 0)
2487 {
2488 dyld_trie_data.SetData (m_data,
2489 dyld_info.export_off,
2490 dyld_info.export_size);
2491 }
2492
Jason Molendad34e6522013-02-05 22:31:24 +00002493 if (m_dysymtab.nindirectsyms != 0)
2494 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002495 indirect_symbol_index_data.SetData (m_data,
2496 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00002497 m_dysymtab.nindirectsyms * 4);
2498 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002499 if (function_starts_load_command.cmd)
2500 {
2501 function_starts_data.SetData (m_data,
2502 function_starts_load_command.dataoff,
2503 function_starts_load_command.datasize);
2504 }
2505 }
Greg Clayton77ccca72011-12-30 00:32:24 +00002506
Greg Clayton86eac942013-08-13 21:32:34 +00002507 if (nlist_data.GetByteSize() == 0 && memory_module_load_level == eMemoryModuleLoadLevelComplete)
2508 {
2509 if (log)
2510 module_sp->LogMessage(log, "failed to read nlist data");
2511 return 0;
2512 }
2513
Greg Claytondebb8812012-05-25 17:04:00 +00002514 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2515 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00002516 {
Greg Claytondebb8812012-05-25 17:04:00 +00002517 if (process)
2518 {
2519 if (strtab_addr == LLDB_INVALID_ADDRESS)
2520 {
2521 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002522 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00002523 return 0;
2524 }
2525 }
2526 else
Greg Clayton4c82d422012-05-18 23:20:01 +00002527 {
2528 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002529 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00002530 return 0;
2531 }
2532 }
Greg Clayton4c82d422012-05-18 23:20:01 +00002533
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002534 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
2535 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
Greg Claytona381e102015-07-27 23:21:05 +00002536 const ConstString &g_segment_name_DATA_DIRTY = GetSegmentNameDATA_DIRTY();
2537 const ConstString &g_segment_name_DATA_CONST = GetSegmentNameDATA_CONST();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002538 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
2539 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
2540 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
2541 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
Greg Claytona381e102015-07-27 23:21:05 +00002542 SectionSP data_dirty_section_sp(section_list->FindSectionByName(g_segment_name_DATA_DIRTY));
2543 SectionSP data_const_section_sp(section_list->FindSectionByName(g_segment_name_DATA_CONST));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002544 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
2545 SectionSP eh_frame_section_sp;
2546 if (text_section_sp.get())
2547 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
2548 else
2549 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
2550
Charles Davis510938e2013-08-27 05:04:57 +00002551 const bool is_arm = (m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
Jason Molenda5635f772013-03-21 03:36:01 +00002552
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002553 // lldb works best if it knows the start address of all functions in a module.
Jason Molenda5635f772013-03-21 03:36:01 +00002554 // Linker symbols or debug info are normally the best source of information for start addr / size but
2555 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00002556 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00002557 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
2558 // binary, relative to the text section.
2559 // eh_frame - the eh_frame FDEs have the start addr & size of each function
2560 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
2561 // Binaries built to run on older releases may need to use eh_frame information.
2562
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002563 if (text_section_sp && function_starts_data.GetByteSize())
2564 {
2565 FunctionStarts::Entry function_start_entry;
2566 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00002567 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002568 function_start_entry.addr = text_section_sp->GetFileAddress();
2569 uint64_t delta;
2570 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
2571 {
2572 // Now append the current entry
2573 function_start_entry.addr += delta;
2574 function_starts.Append(function_start_entry);
2575 }
Jason Molendad63d3c72013-04-16 00:18:44 +00002576 }
Jason Molenda5635f772013-03-21 03:36:01 +00002577 else
2578 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00002579 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
2580 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
2581 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
2582 // the module.
2583 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00002584 {
Jason Molendaa18f7072015-08-15 01:21:01 +00002585 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindEHFrame, true);
Jason Molenda5635f772013-03-21 03:36:01 +00002586 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
2587 eh_frame.GetFunctionAddressAndSizeVector (functions);
2588 addr_t text_base_addr = text_section_sp->GetFileAddress();
2589 size_t count = functions.GetSize();
2590 for (size_t i = 0; i < count; ++i)
2591 {
2592 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
2593 if (func)
2594 {
2595 FunctionStarts::Entry function_start_entry;
2596 function_start_entry.addr = func->base - text_base_addr;
2597 function_starts.Append(function_start_entry);
2598 }
2599 }
2600 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002601 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002602
Greg Claytonc7bece562013-01-25 18:06:21 +00002603 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002604
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +00002605 const user_id_t TEXT_eh_frame_sectID =
2606 eh_frame_section_sp.get() ? eh_frame_section_sp->GetID()
2607 : static_cast<user_id_t>(NO_SECT);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002608
Greg Claytonc7bece562013-01-25 18:06:21 +00002609 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002610
2611 uint32_t N_SO_index = UINT32_MAX;
2612
2613 MachSymtabSectionInfo section_info (section_list);
2614 std::vector<uint32_t> N_FUN_indexes;
2615 std::vector<uint32_t> N_NSYM_indexes;
2616 std::vector<uint32_t> N_INCL_indexes;
2617 std::vector<uint32_t> N_BRAC_indexes;
2618 std::vector<uint32_t> N_COMM_indexes;
Greg Claytond81088c2014-01-16 01:38:29 +00002619 typedef std::multimap <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002620 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00002621 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002622 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2623 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00002624 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002625 // Any symbols that get merged into another will get an entry
2626 // in this map so we know
2627 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2628 uint32_t nlist_idx = 0;
2629 Symbol *symbol_ptr = NULL;
2630
2631 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00002632 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00002633 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00002634 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00002635 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002636
Jim Inghamea3ac272014-01-10 22:55:37 +00002637 std::vector<TrieEntryWithOffset> trie_entries;
2638 std::set<lldb::addr_t> resolver_addresses;
2639
2640 if (dyld_trie_data.GetByteSize() > 0)
2641 {
2642 std::vector<llvm::StringRef> nameSlices;
2643 ParseTrieEntries (dyld_trie_data,
2644 0,
Greg Claytonb887da12015-07-16 19:50:57 +00002645 is_arm,
Jim Inghamea3ac272014-01-10 22:55:37 +00002646 nameSlices,
2647 resolver_addresses,
2648 trie_entries);
2649
2650 ConstString text_segment_name ("__TEXT");
2651 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
2652 if (text_segment_sp)
2653 {
2654 const lldb::addr_t text_segment_file_addr = text_segment_sp->GetFileAddress();
2655 if (text_segment_file_addr != LLDB_INVALID_ADDRESS)
2656 {
2657 for (auto &e : trie_entries)
2658 e.entry.address += text_segment_file_addr;
2659 }
2660 }
2661 }
2662
Greg Claytonb65c6292015-02-20 22:20:05 +00002663 typedef std::set<ConstString> IndirectSymbols;
2664 IndirectSymbols indirect_symbol_names;
2665
Todd Fiala013434e2014-07-09 01:29:05 +00002666#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molendaa5609c82012-06-21 01:51:02 +00002667
2668 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
2669 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
2670 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
2671 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
2672 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
2673 // nlist parser to ignore all LOCAL symbols.
2674
2675 if (m_header.flags & 0x80000000u)
2676 {
2677 // Before we can start mapping the DSC, we need to make certain the target process is actually
2678 // using the cache we can find.
2679
Jason Molendaa5609c82012-06-21 01:51:02 +00002680 // Next we need to determine the correct path for the dyld shared cache.
2681
Greg Clayton7ab7f892014-05-29 21:33:45 +00002682 ArchSpec header_arch;
2683 GetArchitecture(header_arch);
Jason Molendaa5609c82012-06-21 01:51:02 +00002684 char dsc_path[PATH_MAX];
Jason Molenda649a6072015-11-10 05:21:54 +00002685 char dsc_path_development[PATH_MAX];
Jason Molendaa5609c82012-06-21 01:51:02 +00002686
2687 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00002688 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
2689 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00002690 header_arch.GetArchitectureName());
2691
Jason Molenda649a6072015-11-10 05:21:54 +00002692 snprintf(dsc_path_development, sizeof(dsc_path), "%s%s%s%s",
2693 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
2694 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
2695 header_arch.GetArchitectureName(),
2696 ".development");
Jason Molendaa5609c82012-06-21 01:51:02 +00002697
Jason Molenda649a6072015-11-10 05:21:54 +00002698 FileSpec dsc_nondevelopment_filespec(dsc_path, false);
2699 FileSpec dsc_development_filespec(dsc_path_development, false);
2700 FileSpec dsc_filespec;
Eugene Zelenko8157a882015-10-23 16:56:07 +00002701
Jason Molenda649a6072015-11-10 05:21:54 +00002702 UUID dsc_uuid;
2703 UUID process_shared_cache_uuid;
Jason Molenda255f9bb2013-03-06 23:17:36 +00002704
Jason Molenda649a6072015-11-10 05:21:54 +00002705 if (process)
Jason Molendad63d3c72013-04-16 00:18:44 +00002706 {
Jason Molenda649a6072015-11-10 05:21:54 +00002707 process_shared_cache_uuid = GetProcessSharedCacheUUID(process);
2708 }
Jason Molenda255f9bb2013-03-06 23:17:36 +00002709
Jason Molenda649a6072015-11-10 05:21:54 +00002710 // First see if we can find an exact match for the inferior process shared cache UUID in
2711 // the development or non-development shared caches on disk.
2712 if (process_shared_cache_uuid.IsValid())
Greg Clayton946f8902012-09-05 22:30:51 +00002713 {
Jason Molenda649a6072015-11-10 05:21:54 +00002714 if (dsc_development_filespec.Exists())
2715 {
2716 UUID dsc_development_uuid = GetSharedCacheUUID (dsc_development_filespec, byte_order, addr_byte_size);
2717 if (dsc_development_uuid.IsValid() && dsc_development_uuid == process_shared_cache_uuid)
2718 {
2719 dsc_filespec = dsc_development_filespec;
2720 dsc_uuid = dsc_development_uuid;
2721 }
2722 }
2723 if (!dsc_uuid.IsValid() && dsc_nondevelopment_filespec.Exists())
2724 {
2725 UUID dsc_nondevelopment_uuid = GetSharedCacheUUID (dsc_nondevelopment_filespec, byte_order, addr_byte_size);
2726 if (dsc_nondevelopment_uuid.IsValid() && dsc_nondevelopment_uuid == process_shared_cache_uuid)
2727 {
2728 dsc_filespec = dsc_nondevelopment_filespec;
2729 dsc_uuid = dsc_nondevelopment_uuid;
2730 }
2731 }
2732 }
Eugene Zelenko8157a882015-10-23 16:56:07 +00002733
Jason Molenda649a6072015-11-10 05:21:54 +00002734 // Failing a UUID match, prefer the development dyld_shared cache if both are present.
2735 if (!dsc_filespec.Exists())
Greg Clayton946f8902012-09-05 22:30:51 +00002736 {
Jason Molenda649a6072015-11-10 05:21:54 +00002737 if (dsc_development_filespec.Exists())
2738 {
2739 dsc_filespec = dsc_development_filespec;
2740 }
2741 else
2742 {
2743 dsc_filespec = dsc_nondevelopment_filespec;
2744 }
2745 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002746
Jason Molendaf8130862012-06-22 03:28:35 +00002747 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
2748 The dyld_cache_local_symbols_info structure gives us three things:
2749 1. The start and count of the nlist records in the dyld_shared_cache file
2750 2. The start and size of the strings for these nlist records
2751 3. The start and count of dyld_cache_local_symbols_entry entries
2752
2753 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
2754 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002755 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00002756 and the count of how many nlist records there are for this dylib/framework.
2757 */
2758
Jason Molenda649a6072015-11-10 05:21:54 +00002759 // Process the dyld shared cache header to find the unmapped symbols
Jason Molendaa5609c82012-06-21 01:51:02 +00002760
Jason Molenda649a6072015-11-10 05:21:54 +00002761 DataBufferSP dsc_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
2762 if (!dsc_uuid.IsValid())
2763 {
2764 dsc_uuid = GetSharedCacheUUID (dsc_filespec, byte_order, addr_byte_size);
2765 }
Jason Molenda255f9bb2013-03-06 23:17:36 +00002766 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00002767 {
Jason Molenda649a6072015-11-10 05:21:54 +00002768 DataExtractor dsc_header_data (dsc_data_sp, byte_order, addr_byte_size);
Jason Molenda0e0954c2013-04-16 06:24:42 +00002769
2770 bool uuid_match = true;
2771 if (dsc_uuid.IsValid() && process)
2772 {
Jason Molenda649a6072015-11-10 05:21:54 +00002773 if (process_shared_cache_uuid.IsValid() && dsc_uuid != process_shared_cache_uuid)
Jason Molenda0e0954c2013-04-16 06:24:42 +00002774 {
2775 // The on-disk dyld_shared_cache file is not the same as the one in this
2776 // process' memory, don't use it.
2777 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002778 ModuleSP module_sp (GetModule());
2779 if (module_sp)
2780 module_sp->ReportWarning ("process shared cache does not match on-disk dyld_shared_cache file, some symbol names will be missing.");
Jason Molenda0e0954c2013-04-16 06:24:42 +00002781 }
2782 }
2783
Jason Molenda4e7511e2013-03-06 23:19:17 +00002784 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00002785
Jason Molendaa5609c82012-06-21 01:51:02 +00002786 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
2787
2788 // If the mappingOffset points to a location inside the header, we've
2789 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda649a6072015-11-10 05:21:54 +00002790 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v1))
Jason Molendaa5609c82012-06-21 01:51:02 +00002791 {
2792
Greg Clayton736888c2015-02-23 23:47:09 +00002793 DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info));
Jason Molenda255f9bb2013-03-06 23:17:36 +00002794 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
2795 offset = 0;
2796
2797 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
2798 // in the shared library cache need to be adjusted by an offset to match up with the
2799 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
2800 // recorded in mapping_offset_value.
2801 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
2802
2803 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00002804 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
2805 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
2806
Jason Molenda4e7511e2013-03-06 23:19:17 +00002807 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00002808 {
2809 // Map the local symbols
Greg Clayton736888c2015-02-23 23:47:09 +00002810 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00002811 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002812 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002813
2814 offset = 0;
2815
Greg Claytonb65c6292015-02-20 22:20:05 +00002816 typedef std::map<ConstString, uint16_t> UndefinedNameToDescMap;
2817 typedef std::map<uint32_t, ConstString> SymbolIndexToName;
2818 UndefinedNameToDescMap undefined_name_to_desc;
2819 SymbolIndexToName reexport_shlib_needs_fixup;
2820
2821
Jason Molendaa5609c82012-06-21 01:51:02 +00002822 // Read the local_symbols_infos struct in one shot
2823 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
2824 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
2825
Jason Molendaa5609c82012-06-21 01:51:02 +00002826 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
2827
Jason Molenda255f9bb2013-03-06 23:17:36 +00002828 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002829
2830 offset = local_symbols_info.entriesOffset;
2831 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
2832 {
2833 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
2834 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
2835 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
2836 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
2837
Jason Molenda4e7511e2013-03-06 23:19:17 +00002838 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00002839 {
2840 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
2841
2842 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
2843 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
2844 num_syms = symtab->GetNumSymbols();
2845
2846 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
2847 uint32_t string_table_offset = local_symbols_info.stringsOffset;
2848
Jason Molenda4e7511e2013-03-06 23:19:17 +00002849 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00002850 {
2851 /////////////////////////////
2852 {
2853 struct nlist_64 nlist;
2854 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2855 break;
2856
2857 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2858 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2859 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2860 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2861 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2862
2863 SymbolType type = eSymbolTypeInvalid;
2864 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2865
2866 if (symbol_name == NULL)
2867 {
2868 // No symbol should be NULL, even the symbols with no
2869 // string values should have an offset zero which points
2870 // to an empty C-string
2871 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002872 "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
Jason Molendaa5609c82012-06-21 01:51:02 +00002873 entry_index,
2874 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002875 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00002876 continue;
2877 }
2878 if (symbol_name[0] == '\0')
2879 symbol_name = NULL;
2880
2881 const char *symbol_name_non_abi_mangled = NULL;
2882
2883 SectionSP symbol_section;
2884 uint32_t symbol_byte_size = 0;
2885 bool add_nlist = true;
Charles Davis510938e2013-08-27 05:04:57 +00002886 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002887 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002888 bool is_gsym = false;
Greg Clayton60038be2015-02-14 00:51:13 +00002889 bool set_value = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002890
2891 assert (sym_idx < num_syms);
2892
2893 sym[sym_idx].SetDebug (is_debug);
2894
2895 if (is_debug)
2896 {
2897 switch (nlist.n_type)
2898 {
Charles Davis510938e2013-08-27 05:04:57 +00002899 case N_GSYM:
2900 // global symbol: name,,NO_SECT,type,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002901 // Sometimes the N_GSYM value contains the address.
2902
2903 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2904 // have the same address, but we want to ensure that we always find only the real symbol,
2905 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2906 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2907 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2908 // same address.
2909
Greg Clayton1e28adf2015-02-25 17:25:02 +00002910 is_gsym = true;
2911 sym[sym_idx].SetExternal(true);
2912
2913 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O')
2914 {
2915 llvm::StringRef symbol_name_ref(symbol_name);
2916 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2917 {
2918 symbol_name_non_abi_mangled = symbol_name + 1;
2919 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2920 type = eSymbolTypeObjCClass;
2921 demangled_is_synthesized = true;
2922
2923 }
2924 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2925 {
2926 symbol_name_non_abi_mangled = symbol_name + 1;
2927 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2928 type = eSymbolTypeObjCMetaClass;
2929 demangled_is_synthesized = true;
2930 }
2931 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2932 {
2933 symbol_name_non_abi_mangled = symbol_name + 1;
2934 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2935 type = eSymbolTypeObjCIVar;
2936 demangled_is_synthesized = true;
2937 }
2938 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002939 else
2940 {
Jason Molendaa5609c82012-06-21 01:51:02 +00002941 if (nlist.n_value != 0)
2942 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2943 type = eSymbolTypeData;
2944 }
2945 break;
2946
Charles Davis510938e2013-08-27 05:04:57 +00002947 case N_FNAME:
2948 // procedure name (f77 kludge): name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002949 type = eSymbolTypeCompiler;
2950 break;
2951
Charles Davis510938e2013-08-27 05:04:57 +00002952 case N_FUN:
2953 // procedure: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002954 if (symbol_name)
2955 {
2956 type = eSymbolTypeCode;
2957 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2958
Greg Claytond81088c2014-01-16 01:38:29 +00002959 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002960 // We use the current number of symbols in the symbol table in lieu of
2961 // using nlist_idx in case we ever start trimming entries out
2962 N_FUN_indexes.push_back(sym_idx);
2963 }
2964 else
2965 {
2966 type = eSymbolTypeCompiler;
2967
2968 if ( !N_FUN_indexes.empty() )
2969 {
2970 // Copy the size of the function into the original STAB entry so we don't have
2971 // to hunt for it later
2972 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2973 N_FUN_indexes.pop_back();
2974 // We don't really need the end function STAB as it contains the size which
2975 // we already placed with the original symbol, so don't add it if we want a
2976 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002977 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00002978 }
2979 }
2980 break;
2981
Charles Davis510938e2013-08-27 05:04:57 +00002982 case N_STSYM:
2983 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00002984 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002985 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda649a6072015-11-10 05:21:54 +00002986 if (symbol_name && symbol_name[0])
2987 {
2988 type = ObjectFile::GetSymbolTypeFromName(symbol_name+1, eSymbolTypeData);
2989 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002990 break;
2991
Charles Davis510938e2013-08-27 05:04:57 +00002992 case N_LCSYM:
2993 // .lcomm symbol: name,,n_sect,type,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002994 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2995 type = eSymbolTypeCommonBlock;
2996 break;
2997
Charles Davis510938e2013-08-27 05:04:57 +00002998 case N_BNSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00002999 // We use the current number of symbols in the symbol table in lieu of
3000 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00003001 // Skip these if we want minimal symbol tables
3002 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00003003 break;
3004
Charles Davis510938e2013-08-27 05:04:57 +00003005 case N_ENSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00003006 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
3007 // so that we can always skip the entire symbol if we need to navigate
3008 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00003009 // Skip these if we want minimal symbol tables
3010 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00003011 break;
3012
Charles Davis510938e2013-08-27 05:04:57 +00003013 case N_OPT:
3014 // emitted with gcc2_compiled and in gcc source
Jason Molendaa5609c82012-06-21 01:51:02 +00003015 type = eSymbolTypeCompiler;
3016 break;
3017
Charles Davis510938e2013-08-27 05:04:57 +00003018 case N_RSYM:
3019 // register sym: name,,NO_SECT,type,register
Jason Molendaa5609c82012-06-21 01:51:02 +00003020 type = eSymbolTypeVariable;
3021 break;
3022
Charles Davis510938e2013-08-27 05:04:57 +00003023 case N_SLINE:
3024 // src line: 0,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003025 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3026 type = eSymbolTypeLineEntry;
3027 break;
3028
Charles Davis510938e2013-08-27 05:04:57 +00003029 case N_SSYM:
3030 // structure elt: name,,NO_SECT,type,struct_offset
Jason Molendaa5609c82012-06-21 01:51:02 +00003031 type = eSymbolTypeVariableType;
3032 break;
3033
Charles Davis510938e2013-08-27 05:04:57 +00003034 case N_SO:
3035 // source file name
Jason Molendaa5609c82012-06-21 01:51:02 +00003036 type = eSymbolTypeSourceFile;
3037 if (symbol_name == NULL)
3038 {
Greg Clayton3046e662013-07-10 01:23:25 +00003039 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00003040 if (N_SO_index != UINT32_MAX)
3041 {
3042 // Set the size of the N_SO to the terminating index of this N_SO
3043 // so that we can always skip the entire N_SO if we need to navigate
3044 // more quickly at the source level when parsing STABS
3045 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00003046 symbol_ptr->SetByteSize(sym_idx);
Jason Molendaa5609c82012-06-21 01:51:02 +00003047 symbol_ptr->SetSizeIsSibling(true);
3048 }
3049 N_NSYM_indexes.clear();
3050 N_INCL_indexes.clear();
3051 N_BRAC_indexes.clear();
3052 N_COMM_indexes.clear();
3053 N_FUN_indexes.clear();
3054 N_SO_index = UINT32_MAX;
3055 }
3056 else
3057 {
3058 // We use the current number of symbols in the symbol table in lieu of
3059 // using nlist_idx in case we ever start trimming entries out
3060 const bool N_SO_has_full_path = symbol_name[0] == '/';
3061 if (N_SO_has_full_path)
3062 {
Greg Clayton3046e662013-07-10 01:23:25 +00003063 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00003064 {
3065 // We have two consecutive N_SO entries where the first contains a directory
3066 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00003067 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00003068 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3069 add_nlist = false;
3070 }
3071 else
3072 {
3073 // This is the first entry in a N_SO that contains a directory or
3074 // a full path to the source file
3075 N_SO_index = sym_idx;
3076 }
3077 }
Greg Clayton3046e662013-07-10 01:23:25 +00003078 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00003079 {
3080 // This is usually the second N_SO entry that contains just the filename,
3081 // so here we combine it with the first one if we are minimizing the symbol table
3082 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
3083 if (so_path && so_path[0])
3084 {
3085 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00003086 const size_t double_slash_pos = full_so_path.find("//");
3087 if (double_slash_pos != std::string::npos)
3088 {
3089 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00003090 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Clayton0662d962012-09-07 20:29:13 +00003091 // and the second is the directory for the source file so you end up with
3092 // a path that looks like "/tmp/src//tmp/src/"
3093 FileSpec so_dir(so_path, false);
3094 if (!so_dir.Exists())
3095 {
3096 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3097 if (so_dir.Exists())
3098 {
3099 // Trim off the incorrect path
3100 full_so_path.erase(0, double_slash_pos + 1);
3101 }
3102 }
3103 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003104 if (*full_so_path.rbegin() != '/')
3105 full_so_path += '/';
3106 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00003107 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00003108 add_nlist = false;
3109 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3110 }
3111 }
Greg Clayton946f8902012-09-05 22:30:51 +00003112 else
3113 {
3114 // This could be a relative path to a N_SO
3115 N_SO_index = sym_idx;
3116 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003117 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003118 break;
3119
Charles Davis510938e2013-08-27 05:04:57 +00003120 case N_OSO:
3121 // object file name: name,,0,0,st_mtime
Jason Molendaa5609c82012-06-21 01:51:02 +00003122 type = eSymbolTypeObjectFile;
3123 break;
3124
Charles Davis510938e2013-08-27 05:04:57 +00003125 case N_LSYM:
3126 // local sym: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00003127 type = eSymbolTypeLocal;
3128 break;
3129
3130 //----------------------------------------------------------------------
3131 // INCL scopes
3132 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003133 case N_BINCL:
3134 // include file beginning: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00003135 // We use the current number of symbols in the symbol table in lieu of
3136 // using nlist_idx in case we ever start trimming entries out
3137 N_INCL_indexes.push_back(sym_idx);
3138 type = eSymbolTypeScopeBegin;
3139 break;
3140
Charles Davis510938e2013-08-27 05:04:57 +00003141 case N_EINCL:
3142 // include file end: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003143 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3144 // so that we can always skip the entire symbol if we need to navigate
3145 // more quickly at the source level when parsing STABS
3146 if ( !N_INCL_indexes.empty() )
3147 {
3148 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3149 symbol_ptr->SetByteSize(sym_idx + 1);
3150 symbol_ptr->SetSizeIsSibling(true);
3151 N_INCL_indexes.pop_back();
3152 }
3153 type = eSymbolTypeScopeEnd;
3154 break;
3155
Charles Davis510938e2013-08-27 05:04:57 +00003156 case N_SOL:
3157 // #included file name: name,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003158 type = eSymbolTypeHeaderFile;
3159
3160 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00003161 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00003162 break;
3163
Charles Davis510938e2013-08-27 05:04:57 +00003164 case N_PARAMS:
3165 // compiler parameters: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003166 type = eSymbolTypeCompiler;
3167 break;
3168
Charles Davis510938e2013-08-27 05:04:57 +00003169 case N_VERSION:
3170 // compiler version: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003171 type = eSymbolTypeCompiler;
3172 break;
3173
Charles Davis510938e2013-08-27 05:04:57 +00003174 case N_OLEVEL:
3175 // compiler -O level: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003176 type = eSymbolTypeCompiler;
3177 break;
3178
Charles Davis510938e2013-08-27 05:04:57 +00003179 case N_PSYM:
3180 // parameter: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00003181 type = eSymbolTypeVariable;
3182 break;
3183
Charles Davis510938e2013-08-27 05:04:57 +00003184 case N_ENTRY:
3185 // alternate entry: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003186 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3187 type = eSymbolTypeLineEntry;
3188 break;
3189
3190 //----------------------------------------------------------------------
3191 // Left and Right Braces
3192 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003193 case N_LBRAC:
3194 // left bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003195 // We use the current number of symbols in the symbol table in lieu of
3196 // using nlist_idx in case we ever start trimming entries out
3197 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3198 N_BRAC_indexes.push_back(sym_idx);
3199 type = eSymbolTypeScopeBegin;
3200 break;
3201
Charles Davis510938e2013-08-27 05:04:57 +00003202 case N_RBRAC:
3203 // right bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003204 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3205 // so that we can always skip the entire symbol if we need to navigate
3206 // more quickly at the source level when parsing STABS
3207 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3208 if ( !N_BRAC_indexes.empty() )
3209 {
3210 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3211 symbol_ptr->SetByteSize(sym_idx + 1);
3212 symbol_ptr->SetSizeIsSibling(true);
3213 N_BRAC_indexes.pop_back();
3214 }
3215 type = eSymbolTypeScopeEnd;
3216 break;
3217
Charles Davis510938e2013-08-27 05:04:57 +00003218 case N_EXCL:
3219 // deleted include file: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00003220 type = eSymbolTypeHeaderFile;
3221 break;
3222
3223 //----------------------------------------------------------------------
3224 // COMM scopes
3225 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003226 case N_BCOMM:
3227 // begin common: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003228 // We use the current number of symbols in the symbol table in lieu of
3229 // using nlist_idx in case we ever start trimming entries out
3230 type = eSymbolTypeScopeBegin;
3231 N_COMM_indexes.push_back(sym_idx);
3232 break;
3233
Charles Davis510938e2013-08-27 05:04:57 +00003234 case N_ECOML:
3235 // end common (local name): 0,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003236 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3237 // Fall through
3238
Charles Davis510938e2013-08-27 05:04:57 +00003239 case N_ECOMM:
3240 // end common: name,,n_sect,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003241 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3242 // so that we can always skip the entire symbol if we need to navigate
3243 // more quickly at the source level when parsing STABS
3244 if ( !N_COMM_indexes.empty() )
3245 {
3246 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3247 symbol_ptr->SetByteSize(sym_idx + 1);
3248 symbol_ptr->SetSizeIsSibling(true);
3249 N_COMM_indexes.pop_back();
3250 }
3251 type = eSymbolTypeScopeEnd;
3252 break;
3253
Charles Davis510938e2013-08-27 05:04:57 +00003254 case N_LENG:
3255 // second stab entry with length information
Jason Molendaa5609c82012-06-21 01:51:02 +00003256 type = eSymbolTypeAdditional;
3257 break;
3258
3259 default: break;
3260 }
3261 }
3262 else
3263 {
Charles Davis510938e2013-08-27 05:04:57 +00003264 //uint8_t n_pext = N_PEXT & nlist.n_type;
3265 uint8_t n_type = N_TYPE & nlist.n_type;
3266 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Jason Molendaa5609c82012-06-21 01:51:02 +00003267
3268 switch (n_type)
3269 {
Greg Clayton60038be2015-02-14 00:51:13 +00003270 case N_INDR:
3271 {
3272 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
3273 if (reexport_name_cstr && reexport_name_cstr[0])
3274 {
3275 type = eSymbolTypeReExported;
3276 ConstString reexport_name(reexport_name_cstr + ((reexport_name_cstr[0] == '_') ? 1 : 0));
3277 sym[sym_idx].SetReExportedSymbolName(reexport_name);
3278 set_value = false;
3279 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3280 indirect_symbol_names.insert(ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0)));
3281 }
3282 else
3283 type = eSymbolTypeUndefined;
3284 }
3285 break;
3286
Charles Davis510938e2013-08-27 05:04:57 +00003287 case N_UNDF:
Greg Clayton786ad182015-03-03 01:40:46 +00003288 if (symbol_name && symbol_name[0])
Greg Clayton60038be2015-02-14 00:51:13 +00003289 {
3290 ConstString undefined_name(symbol_name + ((symbol_name[0] == '_') ? 1 : 0));
3291 undefined_name_to_desc[undefined_name] = nlist.n_desc;
3292 }
3293 // Fall through
3294 case N_PBUD:
Jason Molendaa5609c82012-06-21 01:51:02 +00003295 type = eSymbolTypeUndefined;
3296 break;
3297
Charles Davis510938e2013-08-27 05:04:57 +00003298 case N_ABS:
Jason Molendaa5609c82012-06-21 01:51:02 +00003299 type = eSymbolTypeAbsolute;
3300 break;
3301
Charles Davis510938e2013-08-27 05:04:57 +00003302 case N_SECT:
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003303 {
3304 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00003305
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003306 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00003307 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003308 // TODO: warn about this?
3309 add_nlist = false;
3310 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00003311 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003312
3313 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00003314 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003315 type = eSymbolTypeException;
3316 }
3317 else
3318 {
Charles Davis510938e2013-08-27 05:04:57 +00003319 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003320
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003321 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00003322 {
Charles Davis510938e2013-08-27 05:04:57 +00003323 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
3324 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
3325 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
3326 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3327 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3328 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3329 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3330 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3331 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
Charles Davis510938e2013-08-27 05:04:57 +00003332 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3333 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
3334 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
3335 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton38f9cc42014-06-16 22:53:16 +00003336 default:
3337 switch (symbol_section->GetType())
3338 {
3339 case lldb::eSectionTypeCode:
3340 type = eSymbolTypeCode;
3341 break;
3342 case eSectionTypeData:
3343 case eSectionTypeDataCString: // Inlined C string data
3344 case eSectionTypeDataCStringPointers: // Pointers to C string data
3345 case eSectionTypeDataSymbolAddress: // Address of a symbol in the symbol table
3346 case eSectionTypeData4:
3347 case eSectionTypeData8:
3348 case eSectionTypeData16:
Greg Clayton38f9cc42014-06-16 22:53:16 +00003349 type = eSymbolTypeData;
3350 break;
3351 default:
3352 break;
3353 }
3354 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00003355 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003356
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003357 if (type == eSymbolTypeInvalid)
3358 {
3359 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3360 if (symbol_section->IsDescendant (text_section_sp.get()))
3361 {
Charles Davis510938e2013-08-27 05:04:57 +00003362 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
3363 S_ATTR_SELF_MODIFYING_CODE |
3364 S_ATTR_SOME_INSTRUCTIONS))
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003365 type = eSymbolTypeData;
3366 else
3367 type = eSymbolTypeCode;
3368 }
Greg Claytona381e102015-07-27 23:21:05 +00003369 else if (symbol_section->IsDescendant(data_section_sp.get()) ||
3370 symbol_section->IsDescendant(data_dirty_section_sp.get()) ||
3371 symbol_section->IsDescendant(data_const_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00003372 {
3373 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
3374 {
3375 type = eSymbolTypeRuntime;
Jason Molenda649a6072015-11-10 05:21:54 +00003376
3377 if (symbol_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00003378 {
3379 llvm::StringRef symbol_name_ref(symbol_name);
Jason Molenda649a6072015-11-10 05:21:54 +00003380 if (symbol_name_ref.startswith("_OBJC_"))
Jason Molendaa5609c82012-06-21 01:51:02 +00003381 {
Jason Molenda649a6072015-11-10 05:21:54 +00003382 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3383 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3384 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3385 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3386 {
3387 symbol_name_non_abi_mangled = symbol_name + 1;
3388 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3389 type = eSymbolTypeObjCClass;
3390 demangled_is_synthesized = true;
3391 }
3392 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3393 {
3394 symbol_name_non_abi_mangled = symbol_name + 1;
3395 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3396 type = eSymbolTypeObjCMetaClass;
3397 demangled_is_synthesized = true;
3398 }
3399 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3400 {
3401 symbol_name_non_abi_mangled = symbol_name + 1;
3402 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3403 type = eSymbolTypeObjCIVar;
3404 demangled_is_synthesized = true;
3405 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003406 }
3407 }
3408 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003409 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00003410 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003411 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00003412 }
3413 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003414 {
3415 type = eSymbolTypeData;
3416 }
3417 }
3418 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3419 {
3420 type = eSymbolTypeTrampoline;
3421 }
3422 else if (symbol_section->IsDescendant(objc_section_sp.get()))
3423 {
3424 type = eSymbolTypeRuntime;
3425 if (symbol_name && symbol_name[0] == '.')
3426 {
3427 llvm::StringRef symbol_name_ref(symbol_name);
3428 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3429 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00003430 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003431 symbol_name_non_abi_mangled = symbol_name;
3432 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3433 type = eSymbolTypeObjCClass;
3434 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003435 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003436 }
3437 }
3438 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003439 }
3440 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003441 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003442 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003443 }
3444
3445 if (add_nlist)
3446 {
3447 uint64_t symbol_value = nlist.n_value;
Jason Molendaa5609c82012-06-21 01:51:02 +00003448 if (symbol_name_non_abi_mangled)
3449 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00003450 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3451 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00003452 }
3453 else
3454 {
Greg Clayton3046e662013-07-10 01:23:25 +00003455 bool symbol_name_is_mangled = false;
3456
Jason Molendaa5609c82012-06-21 01:51:02 +00003457 if (symbol_name && symbol_name[0] == '_')
3458 {
3459 symbol_name_is_mangled = symbol_name[1] == '_';
3460 symbol_name++; // Skip the leading underscore
3461 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003462
Jason Molendaa5609c82012-06-21 01:51:02 +00003463 if (symbol_name)
3464 {
Greg Claytondacc4a92013-05-14 22:19:37 +00003465 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00003466 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00003467 if (is_gsym && is_debug)
Greg Clayton14cd13c2015-07-01 23:29:06 +00003468 {
3469 const char *gsym_name = sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString();
3470 if (gsym_name)
3471 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
3472 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003473 }
3474 }
3475 if (symbol_section)
3476 {
3477 const addr_t section_file_addr = symbol_section->GetFileAddress();
3478 if (symbol_byte_size == 0 && function_starts_count > 0)
3479 {
3480 addr_t symbol_lookup_file_addr = nlist.n_value;
3481 // Do an exact address match for non-ARM addresses, else get the closest since
3482 // the symbol might be a thumb symbol which has an address with bit zero set
3483 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3484 if (is_arm && func_start_entry)
3485 {
3486 // Verify that the function start address is the symbol address (ARM)
3487 // or the symbol address + 1 (thumb)
3488 if (func_start_entry->addr != symbol_lookup_file_addr &&
3489 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3490 {
3491 // Not the right entry, NULL it out...
3492 func_start_entry = NULL;
3493 }
3494 }
3495 if (func_start_entry)
3496 {
3497 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003498
Jason Molendaa5609c82012-06-21 01:51:02 +00003499 addr_t symbol_file_addr = func_start_entry->addr;
3500 uint32_t symbol_flags = 0;
3501 if (is_arm)
3502 {
3503 if (symbol_file_addr & 1)
3504 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
Greg Claytonb887da12015-07-16 19:50:57 +00003505 symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Jason Molendaa5609c82012-06-21 01:51:02 +00003506 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003507
Jason Molendaa5609c82012-06-21 01:51:02 +00003508 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3509 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3510 if (next_func_start_entry)
3511 {
3512 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3513 // Be sure the clear the Thumb address bit when we calculate the size
3514 // from the current and next address
3515 if (is_arm)
Greg Claytonb887da12015-07-16 19:50:57 +00003516 next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Jason Molendaa5609c82012-06-21 01:51:02 +00003517 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3518 }
3519 else
3520 {
3521 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3522 }
3523 }
3524 }
3525 symbol_value -= section_file_addr;
3526 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003527
Greg Claytondacc4a92013-05-14 22:19:37 +00003528 if (is_debug == false)
3529 {
3530 if (type == eSymbolTypeCode)
3531 {
3532 // See if we can find a N_FUN entry for any code symbols.
3533 // If we do find a match, and the name matches, then we
3534 // can merge the two into just the function symbol to avoid
3535 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003536 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3537 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3538 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00003539 {
Greg Claytond81088c2014-01-16 01:38:29 +00003540 bool found_it = false;
3541 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00003542 {
Greg Claytond81088c2014-01-16 01:38:29 +00003543 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3544 {
3545 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3546 // We just need the flags from the linker symbol, so put these flags
3547 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3548 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3549 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3550 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3551 sym[pos->second].SetType (eSymbolTypeResolver);
3552 sym[sym_idx].Clear();
3553 found_it = true;
3554 break;
3555 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003556 }
Greg Claytond81088c2014-01-16 01:38:29 +00003557 if (found_it)
3558 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00003559 }
Jim Inghamea3ac272014-01-10 22:55:37 +00003560 else
3561 {
Greg Claytond81088c2014-01-16 01:38:29 +00003562 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00003563 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00003564 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003565 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00003566 else if (type == eSymbolTypeData ||
3567 type == eSymbolTypeObjCClass ||
3568 type == eSymbolTypeObjCMetaClass ||
3569 type == eSymbolTypeObjCIVar )
Greg Claytondacc4a92013-05-14 22:19:37 +00003570 {
3571 // See if we can find a N_STSYM entry for any data symbols.
3572 // If we do find a match, and the name matches, then we
3573 // can merge the two into just the Static symbol to avoid
3574 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003575 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3576 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
3577 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00003578 {
Greg Claytond81088c2014-01-16 01:38:29 +00003579 bool found_it = false;
3580 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00003581 {
Greg Claytond81088c2014-01-16 01:38:29 +00003582 if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
3583 {
3584 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3585 // We just need the flags from the linker symbol, so put these flags
3586 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3587 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3588 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3589 sym[sym_idx].Clear();
3590 found_it = true;
3591 break;
3592 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003593 }
Greg Claytond81088c2014-01-16 01:38:29 +00003594 if (found_it)
3595 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00003596 }
3597 else
3598 {
Greg Clayton14cd13c2015-07-01 23:29:06 +00003599 const char *gsym_name = sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled).GetCString();
3600 if (gsym_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00003601 {
Greg Clayton14cd13c2015-07-01 23:29:06 +00003602 // Combine N_GSYM stab entries with the non stab symbol
3603 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(gsym_name);
3604 if (pos != N_GSYM_name_to_sym_idx.end())
3605 {
3606 const uint32_t GSYM_sym_idx = pos->second;
3607 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3608 // Copy the address, because often the N_GSYM address has an invalid address of zero
3609 // when the global is a common symbol
3610 sym[GSYM_sym_idx].GetAddressRef().SetSection (symbol_section);
3611 sym[GSYM_sym_idx].GetAddressRef().SetOffset (symbol_value);
3612 // We just need the flags from the linker symbol, so put these flags
3613 // into the N_GSYM flags to avoid duplicate symbols in the symbol table
3614 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3615 sym[sym_idx].Clear();
3616 continue;
3617 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003618 }
3619 }
3620 }
3621 }
3622
Jason Molendaa5609c82012-06-21 01:51:02 +00003623 sym[sym_idx].SetID (nlist_idx);
3624 sym[sym_idx].SetType (type);
Greg Clayton60038be2015-02-14 00:51:13 +00003625 if (set_value)
3626 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00003627 sym[sym_idx].GetAddressRef().SetSection (symbol_section);
3628 sym[sym_idx].GetAddressRef().SetOffset (symbol_value);
Greg Clayton60038be2015-02-14 00:51:13 +00003629 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003630 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003631
Jason Molendaa5609c82012-06-21 01:51:02 +00003632 if (symbol_byte_size > 0)
3633 sym[sym_idx].SetByteSize(symbol_byte_size);
3634
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003635 if (demangled_is_synthesized)
3636 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00003637 ++sym_idx;
3638 }
3639 else
3640 {
3641 sym[sym_idx].Clear();
3642 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003643
Jason Molendaa5609c82012-06-21 01:51:02 +00003644 }
3645 /////////////////////////////
3646 }
3647 break; // No more entries to consider
3648 }
3649 }
Greg Clayton60038be2015-02-14 00:51:13 +00003650
3651 for (const auto &pos :reexport_shlib_needs_fixup)
3652 {
3653 const auto undef_pos = undefined_name_to_desc.find(pos.second);
3654 if (undef_pos != undefined_name_to_desc.end())
3655 {
3656 const uint8_t dylib_ordinal = llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
3657 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
3658 sym[pos.first].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(dylib_ordinal-1));
3659 }
3660 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003661 }
3662 }
3663 }
3664 }
3665 }
3666
3667 // Must reset this in case it was mutated above!
3668 nlist_data_offset = 0;
3669#endif
Jim Inghamea3ac272014-01-10 22:55:37 +00003670
Greg Claytonfd814c52013-08-13 01:42:25 +00003671 if (nlist_data.GetByteSize() > 0)
Jason Molendaa5609c82012-06-21 01:51:02 +00003672 {
Jason Molendaa5609c82012-06-21 01:51:02 +00003673
Greg Claytonfd814c52013-08-13 01:42:25 +00003674 // If the sym array was not created while parsing the DSC unmapped
3675 // symbols, create it now.
3676 if (sym == NULL)
Greg Clayton4c82d422012-05-18 23:20:01 +00003677 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003678 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
3679 num_syms = symtab->GetNumSymbols();
3680 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003681
Greg Claytonfd814c52013-08-13 01:42:25 +00003682 if (unmapped_local_symbols_found)
3683 {
3684 assert(m_dysymtab.ilocalsym == 0);
3685 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
3686 nlist_idx = m_dysymtab.nlocalsym;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003687 }
Greg Claytondebb8812012-05-25 17:04:00 +00003688 else
3689 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003690 nlist_idx = 0;
Greg Claytondebb8812012-05-25 17:04:00 +00003691 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003692
Greg Clayton60038be2015-02-14 00:51:13 +00003693 typedef std::map<ConstString, uint16_t> UndefinedNameToDescMap;
3694 typedef std::map<uint32_t, ConstString> SymbolIndexToName;
3695 UndefinedNameToDescMap undefined_name_to_desc;
3696 SymbolIndexToName reexport_shlib_needs_fixup;
Greg Claytonfd814c52013-08-13 01:42:25 +00003697 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003698 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003699 struct nlist_64 nlist;
3700 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
3701 break;
3702
3703 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
3704 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
3705 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
3706 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
3707 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
3708
3709 SymbolType type = eSymbolTypeInvalid;
3710 const char *symbol_name = NULL;
3711
3712 if (have_strtab_data)
Greg Clayton77ccca72011-12-30 00:32:24 +00003713 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003714 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003715
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003716 if (symbol_name == NULL)
3717 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003718 // No symbol should be NULL, even the symbols with no
3719 // string values should have an offset zero which points
3720 // to an empty C-string
3721 Host::SystemLog (Host::eSystemLogError,
3722 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
3723 nlist_idx,
3724 nlist.n_strx,
3725 module_sp->GetFileSpec().GetPath().c_str());
3726 continue;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003727 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003728 if (symbol_name[0] == '\0')
3729 symbol_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003730 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003731 else
3732 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003733 const addr_t str_addr = strtab_addr + nlist.n_strx;
3734 Error str_error;
3735 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
3736 symbol_name = memory_symbol_name.c_str();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003737 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003738 const char *symbol_name_non_abi_mangled = NULL;
3739
3740 SectionSP symbol_section;
3741 lldb::addr_t symbol_byte_size = 0;
3742 bool add_nlist = true;
3743 bool is_gsym = false;
Charles Davis510938e2013-08-27 05:04:57 +00003744 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00003745 bool demangled_is_synthesized = false;
Greg Clayton60038be2015-02-14 00:51:13 +00003746 bool set_value = true;
Greg Claytonfd814c52013-08-13 01:42:25 +00003747 assert (sym_idx < num_syms);
3748
3749 sym[sym_idx].SetDebug (is_debug);
3750
3751 if (is_debug)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003752 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003753 switch (nlist.n_type)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003754 {
Charles Davis510938e2013-08-27 05:04:57 +00003755 case N_GSYM:
3756 // global symbol: name,,NO_SECT,type,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003757 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003758
Greg Claytonfd814c52013-08-13 01:42:25 +00003759 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
3760 // have the same address, but we want to ensure that we always find only the real symbol,
3761 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
3762 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
3763 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
3764 // same address.
Greg Clayton1e28adf2015-02-25 17:25:02 +00003765 is_gsym = true;
3766 sym[sym_idx].SetExternal(true);
Greg Clayton29e08cb2012-03-14 01:53:24 +00003767
Greg Clayton1e28adf2015-02-25 17:25:02 +00003768 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O')
3769 {
3770 llvm::StringRef symbol_name_ref(symbol_name);
3771 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3772 {
3773 symbol_name_non_abi_mangled = symbol_name + 1;
3774 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3775 type = eSymbolTypeObjCClass;
3776 demangled_is_synthesized = true;
3777
3778 }
3779 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3780 {
3781 symbol_name_non_abi_mangled = symbol_name + 1;
3782 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3783 type = eSymbolTypeObjCMetaClass;
3784 demangled_is_synthesized = true;
3785 }
3786 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3787 {
3788 symbol_name_non_abi_mangled = symbol_name + 1;
3789 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3790 type = eSymbolTypeObjCIVar;
3791 demangled_is_synthesized = true;
3792 }
3793 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003794 else
3795 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003796 if (nlist.n_value != 0)
3797 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3798 type = eSymbolTypeData;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003799 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003800 break;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003801
Charles Davis510938e2013-08-27 05:04:57 +00003802 case N_FNAME:
3803 // procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003804 type = eSymbolTypeCompiler;
3805 break;
3806
Charles Davis510938e2013-08-27 05:04:57 +00003807 case N_FUN:
3808 // procedure: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003809 if (symbol_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00003810 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003811 type = eSymbolTypeCode;
3812 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3813
Greg Claytond81088c2014-01-16 01:38:29 +00003814 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003815 // We use the current number of symbols in the symbol table in lieu of
3816 // using nlist_idx in case we ever start trimming entries out
3817 N_FUN_indexes.push_back(sym_idx);
Greg Claytondacc4a92013-05-14 22:19:37 +00003818 }
3819 else
3820 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003821 type = eSymbolTypeCompiler;
3822
3823 if ( !N_FUN_indexes.empty() )
Greg Claytondacc4a92013-05-14 22:19:37 +00003824 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003825 // Copy the size of the function into the original STAB entry so we don't have
3826 // to hunt for it later
3827 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
3828 N_FUN_indexes.pop_back();
3829 // We don't really need the end function STAB as it contains the size which
3830 // we already placed with the original symbol, so don't add it if we want a
3831 // minimal symbol table
3832 add_nlist = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00003833 }
3834 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003835 break;
3836
Charles Davis510938e2013-08-27 05:04:57 +00003837 case N_STSYM:
3838 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00003839 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003840 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda649a6072015-11-10 05:21:54 +00003841 if (symbol_name && symbol_name[0])
3842 {
3843 type = ObjectFile::GetSymbolTypeFromName(symbol_name+1, eSymbolTypeData);
3844 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003845 break;
3846
Charles Davis510938e2013-08-27 05:04:57 +00003847 case N_LCSYM:
3848 // .lcomm symbol: name,,n_sect,type,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003849 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3850 type = eSymbolTypeCommonBlock;
3851 break;
3852
Charles Davis510938e2013-08-27 05:04:57 +00003853 case N_BNSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003854 // We use the current number of symbols in the symbol table in lieu of
3855 // using nlist_idx in case we ever start trimming entries out
3856 // Skip these if we want minimal symbol tables
3857 add_nlist = false;
3858 break;
3859
Charles Davis510938e2013-08-27 05:04:57 +00003860 case N_ENSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003861 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
3862 // so that we can always skip the entire symbol if we need to navigate
3863 // more quickly at the source level when parsing STABS
3864 // Skip these if we want minimal symbol tables
3865 add_nlist = false;
3866 break;
3867
3868
Charles Davis510938e2013-08-27 05:04:57 +00003869 case N_OPT:
3870 // emitted with gcc2_compiled and in gcc source
Greg Claytonfd814c52013-08-13 01:42:25 +00003871 type = eSymbolTypeCompiler;
3872 break;
3873
Charles Davis510938e2013-08-27 05:04:57 +00003874 case N_RSYM:
3875 // register sym: name,,NO_SECT,type,register
Greg Claytonfd814c52013-08-13 01:42:25 +00003876 type = eSymbolTypeVariable;
3877 break;
3878
Charles Davis510938e2013-08-27 05:04:57 +00003879 case N_SLINE:
3880 // src line: 0,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003881 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3882 type = eSymbolTypeLineEntry;
3883 break;
3884
Charles Davis510938e2013-08-27 05:04:57 +00003885 case N_SSYM:
3886 // structure elt: name,,NO_SECT,type,struct_offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003887 type = eSymbolTypeVariableType;
3888 break;
3889
Charles Davis510938e2013-08-27 05:04:57 +00003890 case N_SO:
3891 // source file name
Greg Claytonfd814c52013-08-13 01:42:25 +00003892 type = eSymbolTypeSourceFile;
3893 if (symbol_name == NULL)
3894 {
3895 add_nlist = false;
3896 if (N_SO_index != UINT32_MAX)
3897 {
3898 // Set the size of the N_SO to the terminating index of this N_SO
3899 // so that we can always skip the entire N_SO if we need to navigate
3900 // more quickly at the source level when parsing STABS
3901 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
3902 symbol_ptr->SetByteSize(sym_idx);
3903 symbol_ptr->SetSizeIsSibling(true);
3904 }
3905 N_NSYM_indexes.clear();
3906 N_INCL_indexes.clear();
3907 N_BRAC_indexes.clear();
3908 N_COMM_indexes.clear();
3909 N_FUN_indexes.clear();
3910 N_SO_index = UINT32_MAX;
3911 }
3912 else
3913 {
3914 // We use the current number of symbols in the symbol table in lieu of
3915 // using nlist_idx in case we ever start trimming entries out
3916 const bool N_SO_has_full_path = symbol_name[0] == '/';
3917 if (N_SO_has_full_path)
3918 {
3919 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3920 {
3921 // We have two consecutive N_SO entries where the first contains a directory
3922 // and the second contains a full path.
3923 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
3924 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3925 add_nlist = false;
3926 }
3927 else
3928 {
3929 // This is the first entry in a N_SO that contains a directory or
3930 // a full path to the source file
3931 N_SO_index = sym_idx;
3932 }
3933 }
3934 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3935 {
3936 // This is usually the second N_SO entry that contains just the filename,
3937 // so here we combine it with the first one if we are minimizing the symbol table
Greg Claytonddaf6a72015-07-08 22:32:23 +00003938 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName(lldb::eLanguageTypeUnknown).AsCString();
Greg Claytonfd814c52013-08-13 01:42:25 +00003939 if (so_path && so_path[0])
3940 {
3941 std::string full_so_path (so_path);
3942 const size_t double_slash_pos = full_so_path.find("//");
3943 if (double_slash_pos != std::string::npos)
3944 {
3945 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00003946 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Claytonfd814c52013-08-13 01:42:25 +00003947 // and the second is the directory for the source file so you end up with
3948 // a path that looks like "/tmp/src//tmp/src/"
3949 FileSpec so_dir(so_path, false);
3950 if (!so_dir.Exists())
3951 {
3952 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3953 if (so_dir.Exists())
3954 {
3955 // Trim off the incorrect path
3956 full_so_path.erase(0, double_slash_pos + 1);
3957 }
3958 }
3959 }
3960 if (*full_so_path.rbegin() != '/')
3961 full_so_path += '/';
3962 full_so_path += symbol_name;
3963 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
3964 add_nlist = false;
3965 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3966 }
3967 }
3968 else
3969 {
3970 // This could be a relative path to a N_SO
3971 N_SO_index = sym_idx;
3972 }
3973 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003974 break;
3975
Charles Davis510938e2013-08-27 05:04:57 +00003976 case N_OSO:
3977 // object file name: name,,0,0,st_mtime
Greg Claytonfd814c52013-08-13 01:42:25 +00003978 type = eSymbolTypeObjectFile;
3979 break;
3980
Charles Davis510938e2013-08-27 05:04:57 +00003981 case N_LSYM:
3982 // local sym: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003983 type = eSymbolTypeLocal;
3984 break;
3985
3986 //----------------------------------------------------------------------
3987 // INCL scopes
3988 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003989 case N_BINCL:
3990 // include file beginning: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00003991 // We use the current number of symbols in the symbol table in lieu of
3992 // using nlist_idx in case we ever start trimming entries out
3993 N_INCL_indexes.push_back(sym_idx);
3994 type = eSymbolTypeScopeBegin;
3995 break;
3996
Charles Davis510938e2013-08-27 05:04:57 +00003997 case N_EINCL:
3998 // include file end: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003999 // Set the size of the N_BINCL to the terminating index of this N_EINCL
4000 // so that we can always skip the entire symbol if we need to navigate
4001 // more quickly at the source level when parsing STABS
4002 if ( !N_INCL_indexes.empty() )
4003 {
4004 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
4005 symbol_ptr->SetByteSize(sym_idx + 1);
4006 symbol_ptr->SetSizeIsSibling(true);
4007 N_INCL_indexes.pop_back();
4008 }
4009 type = eSymbolTypeScopeEnd;
4010 break;
4011
Charles Davis510938e2013-08-27 05:04:57 +00004012 case N_SOL:
4013 // #included file name: name,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004014 type = eSymbolTypeHeaderFile;
4015
4016 // We currently don't use the header files on darwin
4017 add_nlist = false;
4018 break;
4019
Charles Davis510938e2013-08-27 05:04:57 +00004020 case N_PARAMS:
4021 // compiler parameters: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004022 type = eSymbolTypeCompiler;
4023 break;
4024
Charles Davis510938e2013-08-27 05:04:57 +00004025 case N_VERSION:
4026 // compiler version: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004027 type = eSymbolTypeCompiler;
4028 break;
4029
Charles Davis510938e2013-08-27 05:04:57 +00004030 case N_OLEVEL:
4031 // compiler -O level: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004032 type = eSymbolTypeCompiler;
4033 break;
4034
Charles Davis510938e2013-08-27 05:04:57 +00004035 case N_PSYM:
4036 // parameter: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00004037 type = eSymbolTypeVariable;
4038 break;
4039
Charles Davis510938e2013-08-27 05:04:57 +00004040 case N_ENTRY:
4041 // alternate entry: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004042 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4043 type = eSymbolTypeLineEntry;
4044 break;
4045
4046 //----------------------------------------------------------------------
4047 // Left and Right Braces
4048 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00004049 case N_LBRAC:
4050 // left bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004051 // We use the current number of symbols in the symbol table in lieu of
4052 // using nlist_idx in case we ever start trimming entries out
4053 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4054 N_BRAC_indexes.push_back(sym_idx);
4055 type = eSymbolTypeScopeBegin;
4056 break;
4057
Charles Davis510938e2013-08-27 05:04:57 +00004058 case N_RBRAC:
4059 // right bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004060 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
4061 // so that we can always skip the entire symbol if we need to navigate
4062 // more quickly at the source level when parsing STABS
4063 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4064 if ( !N_BRAC_indexes.empty() )
4065 {
4066 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
4067 symbol_ptr->SetByteSize(sym_idx + 1);
4068 symbol_ptr->SetSizeIsSibling(true);
4069 N_BRAC_indexes.pop_back();
4070 }
4071 type = eSymbolTypeScopeEnd;
4072 break;
4073
Charles Davis510938e2013-08-27 05:04:57 +00004074 case N_EXCL:
4075 // deleted include file: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00004076 type = eSymbolTypeHeaderFile;
4077 break;
4078
4079 //----------------------------------------------------------------------
4080 // COMM scopes
4081 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00004082 case N_BCOMM:
4083 // begin common: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004084 // We use the current number of symbols in the symbol table in lieu of
4085 // using nlist_idx in case we ever start trimming entries out
4086 type = eSymbolTypeScopeBegin;
4087 N_COMM_indexes.push_back(sym_idx);
4088 break;
4089
Charles Davis510938e2013-08-27 05:04:57 +00004090 case N_ECOML:
4091 // end common (local name): 0,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004092 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda62e06812016-02-16 04:14:33 +00004093 LLVM_FALLTHROUGH;
Greg Claytonfd814c52013-08-13 01:42:25 +00004094
Charles Davis510938e2013-08-27 05:04:57 +00004095 case N_ECOMM:
4096 // end common: name,,n_sect,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004097 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
4098 // so that we can always skip the entire symbol if we need to navigate
4099 // more quickly at the source level when parsing STABS
4100 if ( !N_COMM_indexes.empty() )
4101 {
4102 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
4103 symbol_ptr->SetByteSize(sym_idx + 1);
4104 symbol_ptr->SetSizeIsSibling(true);
4105 N_COMM_indexes.pop_back();
4106 }
4107 type = eSymbolTypeScopeEnd;
4108 break;
4109
Charles Davis510938e2013-08-27 05:04:57 +00004110 case N_LENG:
4111 // second stab entry with length information
Greg Claytonfd814c52013-08-13 01:42:25 +00004112 type = eSymbolTypeAdditional;
4113 break;
4114
4115 default: break;
4116 }
4117 }
4118 else
4119 {
Charles Davis510938e2013-08-27 05:04:57 +00004120 //uint8_t n_pext = N_PEXT & nlist.n_type;
4121 uint8_t n_type = N_TYPE & nlist.n_type;
4122 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00004123
4124 switch (n_type)
4125 {
Greg Clayton60038be2015-02-14 00:51:13 +00004126 case N_INDR:
4127 {
4128 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
4129 if (reexport_name_cstr && reexport_name_cstr[0])
4130 {
4131 type = eSymbolTypeReExported;
4132 ConstString reexport_name(reexport_name_cstr + ((reexport_name_cstr[0] == '_') ? 1 : 0));
4133 sym[sym_idx].SetReExportedSymbolName(reexport_name);
4134 set_value = false;
4135 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
4136 indirect_symbol_names.insert(ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0)));
4137 }
4138 else
4139 type = eSymbolTypeUndefined;
4140 }
4141 break;
4142
Charles Davis510938e2013-08-27 05:04:57 +00004143 case N_UNDF:
Greg Clayton786ad182015-03-03 01:40:46 +00004144 if (symbol_name && symbol_name[0])
Greg Clayton60038be2015-02-14 00:51:13 +00004145 {
4146 ConstString undefined_name(symbol_name + ((symbol_name[0] == '_') ? 1 : 0));
4147 undefined_name_to_desc[undefined_name] = nlist.n_desc;
4148 }
Jason Molenda62e06812016-02-16 04:14:33 +00004149 LLVM_FALLTHROUGH;
4150
Greg Clayton60038be2015-02-14 00:51:13 +00004151 case N_PBUD:
Greg Claytonfd814c52013-08-13 01:42:25 +00004152 type = eSymbolTypeUndefined;
4153 break;
4154
Charles Davis510938e2013-08-27 05:04:57 +00004155 case N_ABS:
Greg Claytonfd814c52013-08-13 01:42:25 +00004156 type = eSymbolTypeAbsolute;
4157 break;
4158
Charles Davis510938e2013-08-27 05:04:57 +00004159 case N_SECT:
Greg Claytonfd814c52013-08-13 01:42:25 +00004160 {
4161 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4162
4163 if (!symbol_section)
4164 {
4165 // TODO: warn about this?
4166 add_nlist = false;
4167 break;
4168 }
4169
4170 if (TEXT_eh_frame_sectID == nlist.n_sect)
4171 {
4172 type = eSymbolTypeException;
4173 }
4174 else
4175 {
Charles Davis510938e2013-08-27 05:04:57 +00004176 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Greg Claytonfd814c52013-08-13 01:42:25 +00004177
4178 switch (section_type)
4179 {
Charles Davis510938e2013-08-27 05:04:57 +00004180 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
4181 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
4182 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
4183 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
4184 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
4185 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
4186 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
4187 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
4188 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
Charles Davis510938e2013-08-27 05:04:57 +00004189 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
4190 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
4191 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
4192 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton38f9cc42014-06-16 22:53:16 +00004193 default:
4194 switch (symbol_section->GetType())
4195 {
4196 case lldb::eSectionTypeCode:
4197 type = eSymbolTypeCode;
4198 break;
4199 case eSectionTypeData:
4200 case eSectionTypeDataCString: // Inlined C string data
4201 case eSectionTypeDataCStringPointers: // Pointers to C string data
4202 case eSectionTypeDataSymbolAddress: // Address of a symbol in the symbol table
4203 case eSectionTypeData4:
4204 case eSectionTypeData8:
4205 case eSectionTypeData16:
Greg Clayton38f9cc42014-06-16 22:53:16 +00004206 type = eSymbolTypeData;
4207 break;
4208 default:
4209 break;
4210 }
4211 break;
Greg Claytonfd814c52013-08-13 01:42:25 +00004212 }
4213
4214 if (type == eSymbolTypeInvalid)
4215 {
4216 const char *symbol_sect_name = symbol_section->GetName().AsCString();
4217 if (symbol_section->IsDescendant (text_section_sp.get()))
4218 {
Charles Davis510938e2013-08-27 05:04:57 +00004219 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
4220 S_ATTR_SELF_MODIFYING_CODE |
4221 S_ATTR_SOME_INSTRUCTIONS))
Greg Claytonfd814c52013-08-13 01:42:25 +00004222 type = eSymbolTypeData;
4223 else
4224 type = eSymbolTypeCode;
4225 }
4226 else
Greg Claytona381e102015-07-27 23:21:05 +00004227 if (symbol_section->IsDescendant(data_section_sp.get()) ||
4228 symbol_section->IsDescendant(data_dirty_section_sp.get()) ||
4229 symbol_section->IsDescendant(data_const_section_sp.get()))
Greg Claytonfd814c52013-08-13 01:42:25 +00004230 {
4231 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
4232 {
4233 type = eSymbolTypeRuntime;
4234
Jason Molenda649a6072015-11-10 05:21:54 +00004235 if (symbol_name)
Greg Claytonfd814c52013-08-13 01:42:25 +00004236 {
4237 llvm::StringRef symbol_name_ref(symbol_name);
Jason Molenda649a6072015-11-10 05:21:54 +00004238 if (symbol_name_ref.startswith("_OBJC_"))
Greg Claytonfd814c52013-08-13 01:42:25 +00004239 {
Jason Molenda649a6072015-11-10 05:21:54 +00004240 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
4241 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
4242 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
4243 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
4244 {
4245 symbol_name_non_abi_mangled = symbol_name + 1;
4246 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
4247 type = eSymbolTypeObjCClass;
4248 demangled_is_synthesized = true;
4249 }
4250 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
4251 {
4252 symbol_name_non_abi_mangled = symbol_name + 1;
4253 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
4254 type = eSymbolTypeObjCMetaClass;
4255 demangled_is_synthesized = true;
4256 }
4257 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
4258 {
4259 symbol_name_non_abi_mangled = symbol_name + 1;
4260 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
4261 type = eSymbolTypeObjCIVar;
4262 demangled_is_synthesized = true;
4263 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004264 }
4265 }
4266 }
4267 else
4268 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
4269 {
4270 type = eSymbolTypeException;
4271 }
4272 else
4273 {
4274 type = eSymbolTypeData;
4275 }
4276 }
4277 else
4278 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
4279 {
4280 type = eSymbolTypeTrampoline;
4281 }
4282 else
4283 if (symbol_section->IsDescendant(objc_section_sp.get()))
4284 {
4285 type = eSymbolTypeRuntime;
4286 if (symbol_name && symbol_name[0] == '.')
4287 {
4288 llvm::StringRef symbol_name_ref(symbol_name);
4289 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
4290 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
4291 {
4292 symbol_name_non_abi_mangled = symbol_name;
4293 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
4294 type = eSymbolTypeObjCClass;
4295 demangled_is_synthesized = true;
4296 }
4297 }
4298 }
4299 }
4300 }
4301 }
4302 break;
Greg Claytondacc4a92013-05-14 22:19:37 +00004303 }
4304 }
4305
Greg Claytonfd814c52013-08-13 01:42:25 +00004306 if (add_nlist)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004307 {
Greg Claytonfd814c52013-08-13 01:42:25 +00004308 uint64_t symbol_value = nlist.n_value;
4309
4310 if (symbol_name_non_abi_mangled)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004311 {
Greg Claytonfd814c52013-08-13 01:42:25 +00004312 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
4313 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
4314 }
4315 else
4316 {
4317 bool symbol_name_is_mangled = false;
4318
4319 if (symbol_name && symbol_name[0] == '_')
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004320 {
Greg Claytonfd814c52013-08-13 01:42:25 +00004321 symbol_name_is_mangled = symbol_name[1] == '_';
4322 symbol_name++; // Skip the leading underscore
4323 }
4324
4325 if (symbol_name)
4326 {
4327 ConstString const_symbol_name(symbol_name);
4328 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Claytonfd814c52013-08-13 01:42:25 +00004329 }
4330 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00004331
4332 if (is_gsym)
Greg Clayton14cd13c2015-07-01 23:29:06 +00004333 {
Greg Claytonddaf6a72015-07-08 22:32:23 +00004334 const char *gsym_name = sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled).GetCString();
Greg Clayton14cd13c2015-07-01 23:29:06 +00004335 if (gsym_name)
4336 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
4337 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00004338
Greg Claytonfd814c52013-08-13 01:42:25 +00004339 if (symbol_section)
4340 {
4341 const addr_t section_file_addr = symbol_section->GetFileAddress();
4342 if (symbol_byte_size == 0 && function_starts_count > 0)
4343 {
4344 addr_t symbol_lookup_file_addr = nlist.n_value;
4345 // Do an exact address match for non-ARM addresses, else get the closest since
4346 // the symbol might be a thumb symbol which has an address with bit zero set
4347 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
4348 if (is_arm && func_start_entry)
4349 {
4350 // Verify that the function start address is the symbol address (ARM)
4351 // or the symbol address + 1 (thumb)
4352 if (func_start_entry->addr != symbol_lookup_file_addr &&
4353 func_start_entry->addr != (symbol_lookup_file_addr + 1))
4354 {
4355 // Not the right entry, NULL it out...
4356 func_start_entry = NULL;
4357 }
4358 }
4359 if (func_start_entry)
4360 {
4361 func_start_entry->data = true;
4362
4363 addr_t symbol_file_addr = func_start_entry->addr;
4364 if (is_arm)
Greg Claytonb887da12015-07-16 19:50:57 +00004365 symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Greg Claytonfd814c52013-08-13 01:42:25 +00004366
4367 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
4368 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
4369 if (next_func_start_entry)
4370 {
4371 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4372 // Be sure the clear the Thumb address bit when we calculate the size
4373 // from the current and next address
4374 if (is_arm)
Greg Claytonb887da12015-07-16 19:50:57 +00004375 next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Greg Claytonfd814c52013-08-13 01:42:25 +00004376 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
4377 }
4378 else
4379 {
4380 symbol_byte_size = section_end_file_addr - symbol_file_addr;
4381 }
4382 }
4383 }
4384 symbol_value -= section_file_addr;
4385 }
4386
4387 if (is_debug == false)
4388 {
4389 if (type == eSymbolTypeCode)
4390 {
4391 // See if we can find a N_FUN entry for any code symbols.
4392 // If we do find a match, and the name matches, then we
4393 // can merge the two into just the function symbol to avoid
4394 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00004395 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
4396 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
4397 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00004398 {
Greg Claytond81088c2014-01-16 01:38:29 +00004399 bool found_it = false;
4400 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00004401 {
Greg Claytonddaf6a72015-07-08 22:32:23 +00004402 if (sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled))
Greg Claytond81088c2014-01-16 01:38:29 +00004403 {
4404 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4405 // We just need the flags from the linker symbol, so put these flags
4406 // into the N_FUN flags to avoid duplicate symbols in the symbol table
4407 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4408 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4409 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
4410 sym[pos->second].SetType (eSymbolTypeResolver);
4411 sym[sym_idx].Clear();
4412 found_it = true;
4413 break;
4414 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004415 }
Greg Claytond81088c2014-01-16 01:38:29 +00004416 if (found_it)
4417 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00004418 }
Jim Inghamea3ac272014-01-10 22:55:37 +00004419 else
4420 {
4421 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00004422 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00004423 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004424 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00004425 else if (type == eSymbolTypeData ||
4426 type == eSymbolTypeObjCClass ||
4427 type == eSymbolTypeObjCMetaClass ||
4428 type == eSymbolTypeObjCIVar )
Greg Claytonfd814c52013-08-13 01:42:25 +00004429 {
4430 // See if we can find a N_STSYM entry for any data symbols.
4431 // If we do find a match, and the name matches, then we
4432 // can merge the two into just the Static symbol to avoid
4433 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00004434 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
4435 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
4436 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00004437 {
Greg Claytond81088c2014-01-16 01:38:29 +00004438 bool found_it = false;
4439 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00004440 {
Greg Claytonddaf6a72015-07-08 22:32:23 +00004441 if (sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled))
Greg Claytond81088c2014-01-16 01:38:29 +00004442 {
4443 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4444 // We just need the flags from the linker symbol, so put these flags
4445 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
4446 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4447 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4448 sym[sym_idx].Clear();
4449 found_it = true;
4450 break;
4451 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004452 }
Greg Claytond81088c2014-01-16 01:38:29 +00004453 if (found_it)
4454 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00004455 }
4456 else
4457 {
4458 // Combine N_GSYM stab entries with the non stab symbol
Greg Claytonddaf6a72015-07-08 22:32:23 +00004459 const char *gsym_name = sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled).GetCString();
Greg Clayton14cd13c2015-07-01 23:29:06 +00004460 if (gsym_name)
Greg Claytonfd814c52013-08-13 01:42:25 +00004461 {
Greg Clayton14cd13c2015-07-01 23:29:06 +00004462 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(gsym_name);
4463 if (pos != N_GSYM_name_to_sym_idx.end())
4464 {
4465 const uint32_t GSYM_sym_idx = pos->second;
4466 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
4467 // Copy the address, because often the N_GSYM address has an invalid address of zero
4468 // when the global is a common symbol
4469 sym[GSYM_sym_idx].GetAddressRef().SetSection (symbol_section);
4470 sym[GSYM_sym_idx].GetAddressRef().SetOffset (symbol_value);
4471 // We just need the flags from the linker symbol, so put these flags
4472 // into the N_GSYM flags to avoid duplicate symbols in the symbol table
4473 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4474 sym[sym_idx].Clear();
4475 continue;
4476 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004477 }
4478 }
4479 }
4480 }
4481
4482 sym[sym_idx].SetID (nlist_idx);
4483 sym[sym_idx].SetType (type);
Greg Clayton60038be2015-02-14 00:51:13 +00004484 if (set_value)
4485 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00004486 sym[sym_idx].GetAddressRef().SetSection (symbol_section);
4487 sym[sym_idx].GetAddressRef().SetOffset (symbol_value);
Greg Clayton60038be2015-02-14 00:51:13 +00004488 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004489 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4490
4491 if (symbol_byte_size > 0)
4492 sym[sym_idx].SetByteSize(symbol_byte_size);
4493
4494 if (demangled_is_synthesized)
4495 sym[sym_idx].SetDemangledNameIsSynthesized(true);
4496
4497 ++sym_idx;
4498 }
4499 else
4500 {
4501 sym[sym_idx].Clear();
4502 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004503 }
Greg Clayton60038be2015-02-14 00:51:13 +00004504
4505 for (const auto &pos :reexport_shlib_needs_fixup)
4506 {
4507 const auto undef_pos = undefined_name_to_desc.find(pos.second);
4508 if (undef_pos != undefined_name_to_desc.end())
4509 {
4510 const uint8_t dylib_ordinal = llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
4511 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
4512 sym[pos.first].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(dylib_ordinal-1));
4513 }
4514 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004515 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004516
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004517 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
4518
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004519 if (function_starts_count > 0)
4520 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004521 uint32_t num_synthetic_function_symbols = 0;
4522 for (i=0; i<function_starts_count; ++i)
4523 {
4524 if (function_starts.GetEntryRef (i).data == false)
4525 ++num_synthetic_function_symbols;
4526 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004527
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004528 if (num_synthetic_function_symbols > 0)
4529 {
4530 if (num_syms < sym_idx + num_synthetic_function_symbols)
4531 {
4532 num_syms = sym_idx + num_synthetic_function_symbols;
4533 sym = symtab->Resize (num_syms);
4534 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004535 for (i=0; i<function_starts_count; ++i)
4536 {
4537 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
4538 if (func_start_entry->data == false)
4539 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004540 addr_t symbol_file_addr = func_start_entry->addr;
4541 uint32_t symbol_flags = 0;
4542 if (is_arm)
4543 {
4544 if (symbol_file_addr & 1)
4545 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
Greg Claytonb887da12015-07-16 19:50:57 +00004546 symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004547 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004548 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004549 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004550 {
4551 SectionSP symbol_section (symbol_addr.GetSection());
4552 uint32_t symbol_byte_size = 0;
4553 if (symbol_section)
4554 {
4555 const addr_t section_file_addr = symbol_section->GetFileAddress();
4556 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
4557 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
4558 if (next_func_start_entry)
4559 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004560 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4561 if (is_arm)
Greg Claytonb887da12015-07-16 19:50:57 +00004562 next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004563 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004564 }
4565 else
4566 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004567 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004568 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004569 sym[sym_idx].SetID (synthetic_sym_id++);
Tamas Berghammer6b63b142016-02-18 11:12:18 +00004570 sym[sym_idx].GetMangled().SetDemangledName(GetNextSyntheticSymbolName());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004571 sym[sym_idx].SetType (eSymbolTypeCode);
4572 sym[sym_idx].SetIsSynthetic (true);
Greg Clayton358cf1e2015-06-25 21:46:34 +00004573 sym[sym_idx].GetAddressRef() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004574 if (symbol_flags)
4575 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004576 if (symbol_byte_size)
4577 sym[sym_idx].SetByteSize (symbol_byte_size);
4578 ++sym_idx;
4579 }
4580 }
4581 }
4582 }
4583 }
4584 }
4585
4586 // Trim our symbols down to just what we ended up with after
4587 // removing any symbols.
4588 if (sym_idx < num_syms)
4589 {
4590 num_syms = sym_idx;
4591 sym = symtab->Resize (num_syms);
4592 }
4593
4594 // Now synthesize indirect symbols
4595 if (m_dysymtab.nindirectsyms != 0)
4596 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004597 if (indirect_symbol_index_data.GetByteSize())
4598 {
4599 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
4600
4601 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
4602 {
Charles Davis510938e2013-08-27 05:04:57 +00004603 if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) == S_SYMBOL_STUBS)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004604 {
4605 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
4606 if (symbol_stub_byte_size == 0)
4607 continue;
4608
4609 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
4610
4611 if (num_symbol_stubs == 0)
4612 continue;
4613
4614 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
4615 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
4616 {
4617 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
4618 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
Greg Claytonc7bece562013-01-25 18:06:21 +00004619 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004620 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
4621 {
4622 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Charles Davis510938e2013-08-27 05:04:57 +00004623 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004624 continue;
4625
4626 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
4627 Symbol *stub_symbol = NULL;
4628 if (index_pos != end_index_pos)
4629 {
4630 // We have a remapping from the original nlist index to
4631 // a current symbol index, so just look this up by index
4632 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
4633 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004634 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004635 {
4636 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00004637 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004638 // S_SYMBOL_STUBS
4639 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
4640 }
4641
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004642 if (stub_symbol)
4643 {
4644 Address so_addr(symbol_stub_addr, section_list);
4645
4646 if (stub_symbol->GetType() == eSymbolTypeUndefined)
4647 {
4648 // Change the external symbol into a trampoline that makes sense
4649 // These symbols were N_UNDF N_EXT, and are useless to us, so we
4650 // can re-use them so we don't have to make up a synthetic symbol
4651 // for no good reason.
Greg Clayton9191db42013-10-21 18:40:51 +00004652 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4653 stub_symbol->SetType (eSymbolTypeTrampoline);
4654 else
4655 stub_symbol->SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004656 stub_symbol->SetExternal (false);
Greg Clayton358cf1e2015-06-25 21:46:34 +00004657 stub_symbol->GetAddressRef() = so_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004658 stub_symbol->SetByteSize (symbol_stub_byte_size);
4659 }
4660 else
4661 {
4662 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00004663 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004664 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00004665 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004666 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00004667 stub_symbol = NULL; // this pointer no longer valid
4668 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004669 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00004670 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton9191db42013-10-21 18:40:51 +00004671 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4672 sym[sym_idx].SetType (eSymbolTypeTrampoline);
4673 else
4674 sym[sym_idx].SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004675 sym[sym_idx].SetIsSynthetic (true);
Greg Clayton358cf1e2015-06-25 21:46:34 +00004676 sym[sym_idx].GetAddressRef() = so_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004677 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
4678 ++sym_idx;
4679 }
4680 }
Greg Clayton3f839a32012-09-05 01:38:55 +00004681 else
4682 {
4683 if (log)
4684 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
4685 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004686 }
4687 }
4688 }
4689 }
4690 }
4691 }
Greg Clayton9191db42013-10-21 18:40:51 +00004692
Greg Clayton9191db42013-10-21 18:40:51 +00004693 if (!trie_entries.empty())
4694 {
4695 for (const auto &e : trie_entries)
4696 {
4697 if (e.entry.import_name)
4698 {
Greg Clayton60038be2015-02-14 00:51:13 +00004699 // Only add indirect symbols from the Trie entries if we
4700 // didn't have a N_INDR nlist entry for this already
4701 if (indirect_symbol_names.find(e.entry.name) == indirect_symbol_names.end())
Greg Clayton9191db42013-10-21 18:40:51 +00004702 {
Greg Clayton60038be2015-02-14 00:51:13 +00004703 // Make a synthetic symbol to describe re-exported symbol.
4704 if (sym_idx >= num_syms)
4705 sym = symtab->Resize (++num_syms);
4706 sym[sym_idx].SetID (synthetic_sym_id++);
4707 sym[sym_idx].GetMangled() = Mangled(e.entry.name);
4708 sym[sym_idx].SetType (eSymbolTypeReExported);
4709 sym[sym_idx].SetIsSynthetic (true);
4710 sym[sym_idx].SetReExportedSymbolName(e.entry.import_name);
4711 if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize())
4712 {
4713 sym[sym_idx].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(e.entry.other-1));
4714 }
4715 ++sym_idx;
Greg Clayton9191db42013-10-21 18:40:51 +00004716 }
Greg Clayton9191db42013-10-21 18:40:51 +00004717 }
4718 }
4719 }
Eugene Zelenko8157a882015-10-23 16:56:07 +00004720
Greg Clayton3046e662013-07-10 01:23:25 +00004721// StreamFile s(stdout, false);
4722// s.Printf ("Symbol table before CalculateSymbolSizes():\n");
4723// symtab->Dump(&s, NULL, eSortOrderNone);
4724 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
4725 symtab->CalculateSymbolSizes();
4726
4727// s.Printf ("Symbol table after CalculateSymbolSizes():\n");
4728// symtab->Dump(&s, NULL, eSortOrderNone);
4729
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004730 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004731 }
4732 return 0;
4733}
4734
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004735void
4736ObjectFileMachO::Dump (Stream *s)
4737{
Greg Claytona1743492012-03-13 23:14:29 +00004738 ModuleSP module_sp(GetModule());
4739 if (module_sp)
4740 {
4741 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004742 s->Printf("%p: ", static_cast<void*>(this));
Greg Claytona1743492012-03-13 23:14:29 +00004743 s->Indent();
Charles Davis510938e2013-08-27 05:04:57 +00004744 if (m_header.magic == MH_MAGIC_64 || m_header.magic == MH_CIGAM_64)
Greg Claytona1743492012-03-13 23:14:29 +00004745 s->PutCString("ObjectFileMachO64");
4746 else
4747 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004748
Greg Clayton7ab7f892014-05-29 21:33:45 +00004749 ArchSpec header_arch;
4750 GetArchitecture(header_arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004751
Greg Claytona1743492012-03-13 23:14:29 +00004752 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004753
Greg Clayton3046e662013-07-10 01:23:25 +00004754 SectionList *sections = GetSectionList();
4755 if (sections)
4756 sections->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004757
Greg Claytona1743492012-03-13 23:14:29 +00004758 if (m_symtab_ap.get())
4759 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
4760 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004761}
4762
Greg Claytonf4d6de62013-04-24 22:29:28 +00004763bool
4764ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
4765 const lldb_private::DataExtractor &data,
4766 lldb::offset_t lc_offset,
4767 lldb_private::UUID& uuid)
4768{
4769 uint32_t i;
4770 struct uuid_command load_cmd;
4771
4772 lldb::offset_t offset = lc_offset;
4773 for (i=0; i<header.ncmds; ++i)
4774 {
4775 const lldb::offset_t cmd_offset = offset;
4776 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4777 break;
4778
Charles Davis510938e2013-08-27 05:04:57 +00004779 if (load_cmd.cmd == LC_UUID)
Greg Claytonf4d6de62013-04-24 22:29:28 +00004780 {
4781 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
4782
4783 if (uuid_bytes)
4784 {
4785 // OpenCL on Mac OS X uses the same UUID for each of its object files.
4786 // We pretend these object files have no UUID to prevent crashing.
4787
4788 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
4789 0x3b, 0xa8,
4790 0x4b, 0x16,
4791 0xb6, 0xa4,
4792 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
4793
4794 if (!memcmp(uuid_bytes, opencl_uuid, 16))
4795 return false;
4796
4797 uuid.SetBytes (uuid_bytes);
4798 return true;
4799 }
4800 return false;
4801 }
4802 offset = cmd_offset + load_cmd.cmdsize;
4803 }
4804 return false;
4805}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004806
Greg Clayton7ab7f892014-05-29 21:33:45 +00004807bool
4808ObjectFileMachO::GetArchitecture (const llvm::MachO::mach_header &header,
4809 const lldb_private::DataExtractor &data,
4810 lldb::offset_t lc_offset,
4811 ArchSpec &arch)
4812{
4813 arch.SetArchitecture (eArchTypeMachO, header.cputype, header.cpusubtype);
4814
4815 if (arch.IsValid())
4816 {
4817 llvm::Triple &triple = arch.GetTriple();
Greg Claytona3706882015-10-28 23:26:59 +00004818
4819 // Set OS to an unspecified unknown or a "*" so it can match any OS
4820 triple.setOS(llvm::Triple::UnknownOS);
4821 triple.setOSName(llvm::StringRef());
4822
Greg Clayton7ab7f892014-05-29 21:33:45 +00004823 if (header.filetype == MH_PRELOAD)
4824 {
Jason Molenda1a75b0d2016-01-21 04:38:05 +00004825 if (header.cputype == CPU_TYPE_ARM)
4826 {
4827 // If this is a 32-bit arm binary, and it's a standalone binary,
4828 // force the Vendor to Apple so we don't accidentally pick up
4829 // the generic armv7 ABI at runtime. Apple's armv7 ABI always uses
4830 // r7 for the frame pointer register; most other armv7 ABIs use a
4831 // combination of r7 and r11.
4832 triple.setVendor(llvm::Triple::Apple);
4833 }
4834 else
4835 {
4836 // Set vendor to an unspecified unknown or a "*" so it can match any vendor
4837 // This is required for correct behavior of EFI debugging on x86_64
4838 triple.setVendor(llvm::Triple::UnknownVendor);
4839 triple.setVendorName(llvm::StringRef());
4840 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00004841 return true;
4842 }
4843 else
4844 {
4845 struct load_command load_cmd;
Greg Claytona3706882015-10-28 23:26:59 +00004846
Greg Clayton7ab7f892014-05-29 21:33:45 +00004847 lldb::offset_t offset = lc_offset;
4848 for (uint32_t i=0; i<header.ncmds; ++i)
4849 {
4850 const lldb::offset_t cmd_offset = offset;
4851 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4852 break;
4853
4854 switch (load_cmd.cmd)
4855 {
Jason Molenda649a6072015-11-10 05:21:54 +00004856 case llvm::MachO::LC_VERSION_MIN_IPHONEOS:
Greg Clayton7ab7f892014-05-29 21:33:45 +00004857 triple.setOS (llvm::Triple::IOS);
4858 return true;
4859
Jason Molenda649a6072015-11-10 05:21:54 +00004860 case llvm::MachO::LC_VERSION_MIN_MACOSX:
Greg Clayton7ab7f892014-05-29 21:33:45 +00004861 triple.setOS (llvm::Triple::MacOSX);
4862 return true;
Jason Molenda649a6072015-11-10 05:21:54 +00004863
4864 case llvm::MachO::LC_VERSION_MIN_TVOS:
4865 triple.setOS (llvm::Triple::TvOS);
4866 return true;
Greg Clayton7ab7f892014-05-29 21:33:45 +00004867
Jason Molenda649a6072015-11-10 05:21:54 +00004868 case llvm::MachO::LC_VERSION_MIN_WATCHOS:
4869 triple.setOS (llvm::Triple::WatchOS);
4870 return true;
4871
Greg Clayton7ab7f892014-05-29 21:33:45 +00004872 default:
4873 break;
4874 }
4875
4876 offset = cmd_offset + load_cmd.cmdsize;
4877 }
4878
Greg Claytona3706882015-10-28 23:26:59 +00004879 if (header.filetype != MH_KEXT_BUNDLE)
4880 {
4881 // We didn't find a LC_VERSION_MIN load command and this isn't a KEXT
4882 // so lets not say our Vendor is Apple, leave it as an unspecified unknown
4883 triple.setVendor(llvm::Triple::UnknownVendor);
4884 triple.setVendorName(llvm::StringRef());
4885 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00004886 }
4887 }
4888 return arch.IsValid();
4889}
4890
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004891bool
Greg Clayton60830262011-02-04 18:53:10 +00004892ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004893{
Greg Claytona1743492012-03-13 23:14:29 +00004894 ModuleSP module_sp(GetModule());
4895 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004896 {
Greg Claytona1743492012-03-13 23:14:29 +00004897 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004898 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00004899 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004900 }
4901 return false;
4902}
4903
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004904uint32_t
4905ObjectFileMachO::GetDependentModules (FileSpecList& files)
4906{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004907 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00004908 ModuleSP module_sp(GetModule());
4909 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004910 {
Greg Claytona1743492012-03-13 23:14:29 +00004911 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
4912 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004913 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton5a271952015-06-02 22:43:29 +00004914 std::vector<std::string> rpath_paths;
4915 std::vector<std::string> rpath_relative_paths;
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004916 const bool resolve_path = false; // Don't resolve the dependent file paths since they may not reside on this system
Greg Claytona1743492012-03-13 23:14:29 +00004917 uint32_t i;
4918 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004919 {
Greg Claytona1743492012-03-13 23:14:29 +00004920 const uint32_t cmd_offset = offset;
4921 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4922 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004923
Greg Claytona1743492012-03-13 23:14:29 +00004924 switch (load_cmd.cmd)
4925 {
Greg Clayton5a271952015-06-02 22:43:29 +00004926 case LC_RPATH:
Charles Davis510938e2013-08-27 05:04:57 +00004927 case LC_LOAD_DYLIB:
4928 case LC_LOAD_WEAK_DYLIB:
4929 case LC_REEXPORT_DYLIB:
4930 case LC_LOAD_DYLINKER:
4931 case LC_LOADFVMLIB:
4932 case LC_LOAD_UPWARD_DYLIB:
Greg Claytona1743492012-03-13 23:14:29 +00004933 {
4934 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
4935 const char *path = m_data.PeekCStr(name_offset);
Greg Clayton5a271952015-06-02 22:43:29 +00004936 if (path)
Greg Claytona1743492012-03-13 23:14:29 +00004937 {
Greg Clayton5a271952015-06-02 22:43:29 +00004938 if (load_cmd.cmd == LC_RPATH)
4939 rpath_paths.push_back(path);
4940 else
4941 {
4942 if (path[0] == '@')
4943 {
4944 if (strncmp(path, "@rpath", strlen("@rpath")) == 0)
4945 rpath_relative_paths.push_back(path + strlen("@rpath"));
4946 }
4947 else
4948 {
4949 FileSpec file_spec(path, resolve_path);
4950 if (files.AppendIfUnique(file_spec))
4951 count++;
4952 }
4953 }
Greg Claytona1743492012-03-13 23:14:29 +00004954 }
4955 }
4956 break;
4957
4958 default:
4959 break;
4960 }
4961 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004962 }
Greg Clayton5a271952015-06-02 22:43:29 +00004963
4964 if (!rpath_paths.empty())
4965 {
4966 // Fixup all LC_RPATH values to be absolute paths
4967 FileSpec this_file_spec(m_file);
4968 this_file_spec.ResolvePath();
4969 std::string loader_path("@loader_path");
4970 std::string executable_path("@executable_path");
4971 for (auto &rpath : rpath_paths)
4972 {
4973 if (rpath.find(loader_path) == 0)
4974 {
4975 rpath.erase(0, loader_path.size());
4976 rpath.insert(0, this_file_spec.GetDirectory().GetCString());
4977 }
4978 else if (rpath.find(executable_path) == 0)
4979 {
4980 rpath.erase(0, executable_path.size());
4981 rpath.insert(0, this_file_spec.GetDirectory().GetCString());
4982 }
4983 }
4984
4985 for (const auto &rpath_relative_path : rpath_relative_paths)
4986 {
4987 for (const auto &rpath : rpath_paths)
4988 {
4989 std::string path = rpath;
4990 path += rpath_relative_path;
4991 // It is OK to resolve this path because we must find a file on
4992 // disk for us to accept it anyway if it is rpath relative.
4993 FileSpec file_spec(path, true);
4994 // Remove any redundant parts of the path (like "../foo") since
4995 // LC_RPATH values often contain "..".
4996 file_spec.NormalizePath ();
4997 if (file_spec.Exists() && files.AppendIfUnique(file_spec))
4998 {
4999 count++;
5000 break;
5001 }
5002 }
5003 }
5004 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005005 }
5006 return count;
5007}
5008
Jim Ingham672e6f52011-03-07 23:44:08 +00005009lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00005010ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00005011{
5012 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
5013 // is initialized to an invalid address, so we can just return that.
5014 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
Jason Molenda4e7511e2013-03-06 23:19:17 +00005015
Jim Ingham672e6f52011-03-07 23:44:08 +00005016 if (!IsExecutable() || m_entry_point_address.IsValid())
5017 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005018
5019 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00005020 // /usr/include/mach-o.h, but it is basically:
5021 //
5022 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
5023 // uint32_t count - this is the count of longs in the thread state data
5024 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
5025 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00005026 //
Jim Ingham672e6f52011-03-07 23:44:08 +00005027 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
5028 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
5029 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
5030 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
5031 //
5032 // For now we hard-code the offsets and flavors we need:
5033 //
5034 //
5035
Greg Claytona1743492012-03-13 23:14:29 +00005036 ModuleSP module_sp(GetModule());
5037 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00005038 {
Greg Claytona1743492012-03-13 23:14:29 +00005039 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5040 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00005041 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00005042 uint32_t i;
5043 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
5044 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005045
Greg Claytona1743492012-03-13 23:14:29 +00005046 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00005047 {
Greg Claytonc7bece562013-01-25 18:06:21 +00005048 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00005049 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
5050 break;
5051
5052 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00005053 {
Charles Davis510938e2013-08-27 05:04:57 +00005054 case LC_UNIXTHREAD:
5055 case LC_THREAD:
Jim Ingham672e6f52011-03-07 23:44:08 +00005056 {
Greg Claytona1743492012-03-13 23:14:29 +00005057 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00005058 {
Greg Claytona1743492012-03-13 23:14:29 +00005059 uint32_t flavor = m_data.GetU32(&offset);
5060 uint32_t count = m_data.GetU32(&offset);
5061 if (count == 0)
5062 {
5063 // We've gotten off somehow, log and exit;
5064 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00005065 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005066
Greg Claytona1743492012-03-13 23:14:29 +00005067 switch (m_header.cputype)
5068 {
Charles Davis510938e2013-08-27 05:04:57 +00005069 case llvm::MachO::CPU_TYPE_ARM:
Jason Molendac197e812016-01-20 05:17:13 +00005070 if (flavor == 1 || flavor == 9) // ARM_THREAD_STATE/ARM_THREAD_STATE32 from mach/arm/thread_status.h
Greg Claytona1743492012-03-13 23:14:29 +00005071 {
5072 offset += 60; // This is the offset of pc in the GPR thread state data structure.
5073 start_address = m_data.GetU32(&offset);
5074 done = true;
5075 }
Jim Ingham672e6f52011-03-07 23:44:08 +00005076 break;
Jason Molendaa3329782014-03-29 18:54:20 +00005077 case llvm::MachO::CPU_TYPE_ARM64:
5078 if (flavor == 6) // ARM_THREAD_STATE64 from mach/arm/thread_status.h
5079 {
5080 offset += 256; // This is the offset of pc in the GPR thread state data structure.
5081 start_address = m_data.GetU64(&offset);
5082 done = true;
5083 }
5084 break;
Charles Davis510938e2013-08-27 05:04:57 +00005085 case llvm::MachO::CPU_TYPE_I386:
Greg Claytona1743492012-03-13 23:14:29 +00005086 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
5087 {
5088 offset += 40; // This is the offset of eip in the GPR thread state data structure.
5089 start_address = m_data.GetU32(&offset);
5090 done = true;
5091 }
5092 break;
Charles Davis510938e2013-08-27 05:04:57 +00005093 case llvm::MachO::CPU_TYPE_X86_64:
Greg Claytona1743492012-03-13 23:14:29 +00005094 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
5095 {
5096 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
5097 start_address = m_data.GetU64(&offset);
5098 done = true;
5099 }
5100 break;
5101 default:
5102 return m_entry_point_address;
5103 }
5104 // Haven't found the GPR flavor yet, skip over the data for this flavor:
5105 if (done)
5106 break;
5107 offset += count * 4;
5108 }
Jim Ingham672e6f52011-03-07 23:44:08 +00005109 }
Greg Claytona1743492012-03-13 23:14:29 +00005110 break;
Charles Davis510938e2013-08-27 05:04:57 +00005111 case LC_MAIN:
Sean Callanan226b70c2012-03-08 02:39:03 +00005112 {
Greg Claytona1743492012-03-13 23:14:29 +00005113 ConstString text_segment_name ("__TEXT");
5114 uint64_t entryoffset = m_data.GetU64(&offset);
5115 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
5116 if (text_segment_sp)
5117 {
5118 done = true;
5119 start_address = text_segment_sp->GetFileAddress() + entryoffset;
5120 }
Sean Callanan226b70c2012-03-08 02:39:03 +00005121 }
Greg Claytona1743492012-03-13 23:14:29 +00005122
5123 default:
5124 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00005125 }
Greg Claytona1743492012-03-13 23:14:29 +00005126 if (done)
5127 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00005128
Greg Claytona1743492012-03-13 23:14:29 +00005129 // Go to the next load command:
5130 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00005131 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005132
Greg Claytona1743492012-03-13 23:14:29 +00005133 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00005134 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00005135 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00005136 // of this ObjectFile:
5137 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00005138 {
Greg Claytona1743492012-03-13 23:14:29 +00005139 m_entry_point_address.Clear();
5140 }
5141 }
5142 else
5143 {
5144 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
5145 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00005146
Greg Claytona1743492012-03-13 23:14:29 +00005147 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00005148
Greg Claytona1743492012-03-13 23:14:29 +00005149 if (module_sp)
5150 {
5151 SymbolContextList contexts;
5152 SymbolContext context;
5153 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
5154 {
5155 if (contexts.GetContextAtIndex(0, context))
5156 m_entry_point_address = context.symbol->GetAddress();
5157 }
Greg Claytone72dfb32012-02-24 01:59:29 +00005158 }
5159 }
Jim Ingham672e6f52011-03-07 23:44:08 +00005160 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005161
Jim Ingham672e6f52011-03-07 23:44:08 +00005162 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00005163}
5164
Greg Claytonc9660542012-02-05 02:38:54 +00005165lldb_private::Address
5166ObjectFileMachO::GetHeaderAddress ()
5167{
5168 lldb_private::Address header_addr;
5169 SectionList *section_list = GetSectionList();
5170 if (section_list)
5171 {
5172 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
5173 if (text_segment_sp)
5174 {
Greg Claytone72dfb32012-02-24 01:59:29 +00005175 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00005176 header_addr.SetOffset (0);
5177 }
5178 }
5179 return header_addr;
5180}
5181
Greg Claytonc3776bf2012-02-09 06:16:32 +00005182uint32_t
5183ObjectFileMachO::GetNumThreadContexts ()
5184{
Greg Claytona1743492012-03-13 23:14:29 +00005185 ModuleSP module_sp(GetModule());
5186 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005187 {
Greg Claytona1743492012-03-13 23:14:29 +00005188 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5189 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005190 {
Greg Claytona1743492012-03-13 23:14:29 +00005191 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00005192 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00005193 FileRangeArray::Entry file_range;
5194 thread_command thread_cmd;
5195 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005196 {
Greg Claytona1743492012-03-13 23:14:29 +00005197 const uint32_t cmd_offset = offset;
5198 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
5199 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005200
Charles Davis510938e2013-08-27 05:04:57 +00005201 if (thread_cmd.cmd == LC_THREAD)
Greg Claytona1743492012-03-13 23:14:29 +00005202 {
5203 file_range.SetRangeBase (offset);
5204 file_range.SetByteSize (thread_cmd.cmdsize - 8);
5205 m_thread_context_offsets.Append (file_range);
5206 }
5207 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00005208 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00005209 }
5210 }
5211 return m_thread_context_offsets.GetSize();
5212}
5213
5214lldb::RegisterContextSP
5215ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
5216{
Greg Claytonc3776bf2012-02-09 06:16:32 +00005217 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00005218
Greg Claytona1743492012-03-13 23:14:29 +00005219 ModuleSP module_sp(GetModule());
5220 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005221 {
Greg Claytona1743492012-03-13 23:14:29 +00005222 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5223 if (!m_thread_context_offsets_valid)
5224 GetNumThreadContexts ();
5225
5226 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00005227 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00005228 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00005229
5230 DataExtractor data (m_data,
5231 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00005232 thread_context_file_range->GetByteSize());
5233
5234 switch (m_header.cputype)
5235 {
Jason Molendaa3329782014-03-29 18:54:20 +00005236 case llvm::MachO::CPU_TYPE_ARM64:
5237 reg_ctx_sp.reset (new RegisterContextDarwin_arm64_Mach (thread, data));
5238 break;
5239
Charles Davis510938e2013-08-27 05:04:57 +00005240 case llvm::MachO::CPU_TYPE_ARM:
Jim Ingham28eb5712012-10-12 17:34:26 +00005241 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
5242 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005243
Charles Davis510938e2013-08-27 05:04:57 +00005244 case llvm::MachO::CPU_TYPE_I386:
Jim Ingham28eb5712012-10-12 17:34:26 +00005245 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
5246 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005247
Charles Davis510938e2013-08-27 05:04:57 +00005248 case llvm::MachO::CPU_TYPE_X86_64:
Jim Ingham28eb5712012-10-12 17:34:26 +00005249 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
5250 break;
5251 }
Greg Claytona1743492012-03-13 23:14:29 +00005252 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00005253 }
5254 return reg_ctx_sp;
5255}
5256
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005257ObjectFile::Type
5258ObjectFileMachO::CalculateType()
5259{
5260 switch (m_header.filetype)
5261 {
Charles Davis510938e2013-08-27 05:04:57 +00005262 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005263 if (GetAddressByteSize () == 4)
5264 {
5265 // 32 bit kexts are just object files, but they do have a valid
5266 // UUID load command.
5267 UUID uuid;
5268 if (GetUUID(&uuid))
5269 {
5270 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00005271 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005272 // "OSKextGetCurrentIdentifier" as this is required of kexts
5273 if (m_strata == eStrataInvalid)
5274 m_strata = eStrataKernel;
5275 return eTypeSharedLibrary;
5276 }
5277 }
5278 return eTypeObjectFile;
5279
Charles Davis510938e2013-08-27 05:04:57 +00005280 case MH_EXECUTE: return eTypeExecutable; // 0x2u
5281 case MH_FVMLIB: return eTypeSharedLibrary; // 0x3u
5282 case MH_CORE: return eTypeCoreFile; // 0x4u
5283 case MH_PRELOAD: return eTypeSharedLibrary; // 0x5u
5284 case MH_DYLIB: return eTypeSharedLibrary; // 0x6u
5285 case MH_DYLINKER: return eTypeDynamicLinker; // 0x7u
5286 case MH_BUNDLE: return eTypeSharedLibrary; // 0x8u
5287 case MH_DYLIB_STUB: return eTypeStubLibrary; // 0x9u
5288 case MH_DSYM: return eTypeDebugInfo; // 0xAu
5289 case MH_KEXT_BUNDLE: return eTypeSharedLibrary; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005290 default:
5291 break;
5292 }
5293 return eTypeUnknown;
5294}
5295
5296ObjectFile::Strata
5297ObjectFileMachO::CalculateStrata()
5298{
5299 switch (m_header.filetype)
5300 {
Charles Davis510938e2013-08-27 05:04:57 +00005301 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005302 {
5303 // 32 bit kexts are just object files, but they do have a valid
5304 // UUID load command.
5305 UUID uuid;
5306 if (GetUUID(&uuid))
5307 {
5308 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00005309 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005310 // "OSKextGetCurrentIdentifier" as this is required of kexts
5311 if (m_type == eTypeInvalid)
5312 m_type = eTypeSharedLibrary;
5313
5314 return eStrataKernel;
5315 }
5316 }
5317 return eStrataUnknown;
5318
Charles Davis510938e2013-08-27 05:04:57 +00005319 case MH_EXECUTE: // 0x2u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005320 // Check for the MH_DYLDLINK bit in the flags
Charles Davis510938e2013-08-27 05:04:57 +00005321 if (m_header.flags & MH_DYLDLINK)
Sean Callanan49bce8e2012-02-10 20:22:35 +00005322 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005323 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00005324 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005325 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00005326 {
5327 SectionList *section_list = GetSectionList();
5328 if (section_list)
5329 {
5330 static ConstString g_kld_section_name ("__KLD");
5331 if (section_list->FindSectionByName(g_kld_section_name))
5332 return eStrataKernel;
5333 }
5334 }
5335 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005336
Charles Davis510938e2013-08-27 05:04:57 +00005337 case MH_FVMLIB: return eStrataUser; // 0x3u
5338 case MH_CORE: return eStrataUnknown; // 0x4u
5339 case MH_PRELOAD: return eStrataRawImage; // 0x5u
5340 case MH_DYLIB: return eStrataUser; // 0x6u
5341 case MH_DYLINKER: return eStrataUser; // 0x7u
5342 case MH_BUNDLE: return eStrataUser; // 0x8u
5343 case MH_DYLIB_STUB: return eStrataUser; // 0x9u
5344 case MH_DSYM: return eStrataUnknown; // 0xAu
5345 case MH_KEXT_BUNDLE: return eStrataKernel; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005346 default:
5347 break;
5348 }
5349 return eStrataUnknown;
5350}
5351
Greg Claytonc2ff9312012-02-22 19:41:02 +00005352uint32_t
5353ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
5354{
Greg Claytona1743492012-03-13 23:14:29 +00005355 ModuleSP module_sp(GetModule());
5356 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005357 {
Greg Claytona1743492012-03-13 23:14:29 +00005358 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
5359 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00005360 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00005361 uint32_t version_cmd = 0;
5362 uint64_t version = 0;
5363 uint32_t i;
5364 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005365 {
Greg Claytonc7bece562013-01-25 18:06:21 +00005366 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00005367 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
5368 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005369
Charles Davis510938e2013-08-27 05:04:57 +00005370 if (load_cmd.cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005371 {
Greg Claytona1743492012-03-13 23:14:29 +00005372 if (version_cmd == 0)
5373 {
5374 version_cmd = load_cmd.cmd;
5375 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
5376 break;
5377 version = load_cmd.dylib.current_version;
5378 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005379 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00005380 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00005381 }
Greg Claytona1743492012-03-13 23:14:29 +00005382 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00005383 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005384
Charles Davis510938e2013-08-27 05:04:57 +00005385 if (version_cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005386 {
Greg Claytona1743492012-03-13 23:14:29 +00005387 if (versions != NULL && num_versions > 0)
5388 {
5389 if (num_versions > 0)
5390 versions[0] = (version & 0xFFFF0000ull) >> 16;
5391 if (num_versions > 1)
5392 versions[1] = (version & 0x0000FF00ull) >> 8;
5393 if (num_versions > 2)
5394 versions[2] = (version & 0x000000FFull);
5395 // Fill in an remaining version numbers with invalid values
5396 for (i=3; i<num_versions; ++i)
5397 versions[i] = UINT32_MAX;
5398 }
5399 // The LC_ID_DYLIB load command has a version with 3 version numbers
5400 // in it, so always return 3
5401 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00005402 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00005403 }
5404 return false;
5405}
5406
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005407bool
Greg Clayton514487e2011-02-15 21:59:32 +00005408ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005409{
Greg Claytona1743492012-03-13 23:14:29 +00005410 ModuleSP module_sp(GetModule());
5411 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00005412 {
Greg Claytona1743492012-03-13 23:14:29 +00005413 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Clayton7ab7f892014-05-29 21:33:45 +00005414 return GetArchitecture (m_header, m_data, MachHeaderSizeFromMagic(m_header.magic), arch);
Greg Clayton593577a2011-09-21 03:57:31 +00005415 }
Greg Claytona1743492012-03-13 23:14:29 +00005416 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005417}
5418
Jason Molenda0e0954c2013-04-16 06:24:42 +00005419UUID
5420ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
5421{
5422 UUID uuid;
5423 if (process)
5424 {
5425 addr_t all_image_infos = process->GetImageInfoAddress();
5426
5427 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
5428 // or it may be the address of the dyld_all_image_infos structure (want). The first four
5429 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
5430 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
5431
5432 Error err;
5433 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00005434 if (version_or_magic != static_cast<uint32_t>(-1)
Charles Davis510938e2013-08-27 05:04:57 +00005435 && version_or_magic != MH_MAGIC
5436 && version_or_magic != MH_CIGAM
5437 && version_or_magic != MH_MAGIC_64
5438 && version_or_magic != MH_CIGAM_64
Jason Molenda0e0954c2013-04-16 06:24:42 +00005439 && version_or_magic >= 13)
5440 {
5441 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
5442 int wordsize = process->GetAddressByteSize();
5443 if (wordsize == 8)
5444 {
5445 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
5446 }
5447 if (wordsize == 4)
5448 {
5449 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
5450 }
5451 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
5452 {
5453 uuid_t shared_cache_uuid;
5454 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
5455 {
5456 uuid.SetBytes (shared_cache_uuid);
5457 }
5458 }
5459 }
5460 }
5461 return uuid;
5462}
5463
5464UUID
5465ObjectFileMachO::GetLLDBSharedCacheUUID ()
5466{
5467 UUID uuid;
Todd Fiala013434e2014-07-09 01:29:05 +00005468#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molenda0e0954c2013-04-16 06:24:42 +00005469 uint8_t *(*dyld_get_all_image_infos)(void);
5470 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
5471 if (dyld_get_all_image_infos)
5472 {
5473 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
5474 if (dyld_all_image_infos_address)
5475 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00005476 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
5477 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00005478 {
Jason Molendaa3329782014-03-29 18:54:20 +00005479 uuid_t *sharedCacheUUID_address = 0;
5480 int wordsize = sizeof (uint8_t *);
5481 if (wordsize == 8)
5482 {
5483 sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 160); // sharedCacheUUID <mach-o/dyld_images.h>
5484 }
5485 else
5486 {
5487 sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84); // sharedCacheUUID <mach-o/dyld_images.h>
5488 }
Jason Molenda0e0954c2013-04-16 06:24:42 +00005489 uuid.SetBytes (sharedCacheUUID_address);
5490 }
5491 }
5492 }
5493#endif
5494 return uuid;
5495}
5496
Greg Clayton9b234982013-10-24 22:54:08 +00005497uint32_t
5498ObjectFileMachO::GetMinimumOSVersion (uint32_t *versions, uint32_t num_versions)
5499{
5500 if (m_min_os_versions.empty())
5501 {
5502 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5503 bool success = false;
5504 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
5505 {
5506 const lldb::offset_t load_cmd_offset = offset;
5507
5508 version_min_command lc;
5509 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
5510 break;
Jason Molenda649a6072015-11-10 05:21:54 +00005511 if (lc.cmd == llvm::MachO::LC_VERSION_MIN_MACOSX
5512 || lc.cmd == llvm::MachO::LC_VERSION_MIN_IPHONEOS
5513 || lc.cmd == llvm::MachO::LC_VERSION_MIN_TVOS
5514 || lc.cmd == llvm::MachO::LC_VERSION_MIN_WATCHOS)
Greg Clayton9b234982013-10-24 22:54:08 +00005515 {
5516 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
5517 {
5518 const uint32_t xxxx = lc.version >> 16;
5519 const uint32_t yy = (lc.version >> 8) & 0xffu;
5520 const uint32_t zz = lc.version & 0xffu;
5521 if (xxxx)
5522 {
5523 m_min_os_versions.push_back(xxxx);
Greg Clayton8031d282015-02-04 00:40:25 +00005524 m_min_os_versions.push_back(yy);
5525 m_min_os_versions.push_back(zz);
Greg Clayton9b234982013-10-24 22:54:08 +00005526 }
5527 success = true;
5528 }
5529 }
5530 offset = load_cmd_offset + lc.cmdsize;
5531 }
5532
5533 if (success == false)
5534 {
5535 // Push an invalid value so we don't keep trying to
5536 m_min_os_versions.push_back(UINT32_MAX);
5537 }
5538 }
5539
5540 if (m_min_os_versions.size() > 1 || m_min_os_versions[0] != UINT32_MAX)
5541 {
5542 if (versions != NULL && num_versions > 0)
5543 {
5544 for (size_t i=0; i<num_versions; ++i)
5545 {
5546 if (i < m_min_os_versions.size())
5547 versions[i] = m_min_os_versions[i];
5548 else
5549 versions[i] = 0;
5550 }
5551 }
5552 return m_min_os_versions.size();
5553 }
5554 // Call the superclasses version that will empty out the data
5555 return ObjectFile::GetMinimumOSVersion (versions, num_versions);
5556}
5557
5558uint32_t
5559ObjectFileMachO::GetSDKVersion(uint32_t *versions, uint32_t num_versions)
5560{
5561 if (m_sdk_versions.empty())
5562 {
5563 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5564 bool success = false;
5565 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
5566 {
5567 const lldb::offset_t load_cmd_offset = offset;
5568
5569 version_min_command lc;
5570 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
5571 break;
Jason Molenda649a6072015-11-10 05:21:54 +00005572 if (lc.cmd == llvm::MachO::LC_VERSION_MIN_MACOSX
5573 || lc.cmd == llvm::MachO::LC_VERSION_MIN_IPHONEOS
5574 || lc.cmd == llvm::MachO::LC_VERSION_MIN_TVOS
5575 || lc.cmd == llvm::MachO::LC_VERSION_MIN_WATCHOS)
Greg Clayton9b234982013-10-24 22:54:08 +00005576 {
5577 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
5578 {
Todd Fialaebecb382014-09-04 19:31:52 +00005579 const uint32_t xxxx = lc.sdk >> 16;
5580 const uint32_t yy = (lc.sdk >> 8) & 0xffu;
5581 const uint32_t zz = lc.sdk & 0xffu;
Greg Clayton9b234982013-10-24 22:54:08 +00005582 if (xxxx)
5583 {
5584 m_sdk_versions.push_back(xxxx);
Greg Clayton8031d282015-02-04 00:40:25 +00005585 m_sdk_versions.push_back(yy);
5586 m_sdk_versions.push_back(zz);
Greg Clayton9b234982013-10-24 22:54:08 +00005587 }
5588 success = true;
5589 }
5590 }
5591 offset = load_cmd_offset + lc.cmdsize;
5592 }
5593
5594 if (success == false)
5595 {
5596 // Push an invalid value so we don't keep trying to
5597 m_sdk_versions.push_back(UINT32_MAX);
5598 }
5599 }
5600
5601 if (m_sdk_versions.size() > 1 || m_sdk_versions[0] != UINT32_MAX)
5602 {
5603 if (versions != NULL && num_versions > 0)
5604 {
5605 for (size_t i=0; i<num_versions; ++i)
5606 {
5607 if (i < m_sdk_versions.size())
5608 versions[i] = m_sdk_versions[i];
5609 else
5610 versions[i] = 0;
5611 }
5612 }
5613 return m_sdk_versions.size();
5614 }
5615 // Call the superclasses version that will empty out the data
5616 return ObjectFile::GetSDKVersion (versions, num_versions);
5617}
5618
Greg Clayton08928f32015-02-05 02:01:34 +00005619bool
5620ObjectFileMachO::GetIsDynamicLinkEditor()
5621{
5622 return m_header.filetype == llvm::MachO::MH_DYLINKER;
5623}
5624
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005625//------------------------------------------------------------------
5626// PluginInterface protocol
5627//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00005628lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005629ObjectFileMachO::GetPluginName()
5630{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005631 return GetPluginNameStatic();
5632}
5633
5634uint32_t
5635ObjectFileMachO::GetPluginVersion()
5636{
5637 return 1;
5638}
5639
Greg Clayton07347372015-06-08 21:53:11 +00005640Section *
5641ObjectFileMachO::GetMachHeaderSection()
5642{
5643 // Find the first address of the mach header which is the first non-zero
5644 // file sized section whose file offset is zero. This is the base file address
5645 // of the mach-o file which can be subtracted from the vmaddr of the other
5646 // segments found in memory and added to the load address
5647 ModuleSP module_sp = GetModule();
5648 if (module_sp)
5649 {
5650 SectionList *section_list = GetSectionList ();
5651 if (section_list)
5652 {
5653 lldb::addr_t mach_base_file_addr = LLDB_INVALID_ADDRESS;
5654 const size_t num_sections = section_list->GetSize();
5655
5656 for (size_t sect_idx = 0;
5657 sect_idx < num_sections && mach_base_file_addr == LLDB_INVALID_ADDRESS;
5658 ++sect_idx)
5659 {
5660 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
5661 if (section &&
5662 section->GetFileSize() > 0 &&
5663 section->GetFileOffset() == 0 &&
5664 section->IsThreadSpecific() == false &&
5665 module_sp.get() == section->GetModule().get())
5666 {
5667 return section;
5668 }
5669 }
5670 }
5671 }
5672 return nullptr;
5673}
5674
5675lldb::addr_t
5676ObjectFileMachO::CalculateSectionLoadAddressForMemoryImage(lldb::addr_t mach_header_load_address, const Section *mach_header_section, const Section *section)
5677{
5678 ModuleSP module_sp = GetModule();
5679 if (module_sp && mach_header_section && section && mach_header_load_address != LLDB_INVALID_ADDRESS)
5680 {
5681 lldb::addr_t mach_header_file_addr = mach_header_section->GetFileAddress();
5682 if (mach_header_file_addr != LLDB_INVALID_ADDRESS)
5683 {
5684 if (section &&
5685 section->GetFileSize() > 0 &&
5686 section->IsThreadSpecific() == false &&
5687 module_sp.get() == section->GetModule().get())
5688 {
5689 // Ignore __LINKEDIT and __DWARF segments
5690 if (section->GetName() == GetSegmentNameLINKEDIT())
5691 {
5692 // Only map __LINKEDIT if we have an in memory image and this isn't
5693 // a kernel binary like a kext or mach_kernel.
5694 const bool is_memory_image = (bool)m_process_wp.lock();
5695 const Strata strata = GetStrata();
5696 if (is_memory_image == false || strata == eStrataKernel)
5697 return LLDB_INVALID_ADDRESS;
5698 }
5699 return section->GetFileAddress() - mach_header_file_addr + mach_header_load_address;
5700 }
5701 }
5702 }
5703 return LLDB_INVALID_ADDRESS;
5704}
5705
Greg Clayton7524e092014-02-06 20:10:16 +00005706bool
Greg Clayton751caf62014-02-07 22:54:47 +00005707ObjectFileMachO::SetLoadAddress (Target &target,
5708 lldb::addr_t value,
5709 bool value_is_offset)
Greg Clayton7524e092014-02-06 20:10:16 +00005710{
Greg Clayton7524e092014-02-06 20:10:16 +00005711 ModuleSP module_sp = GetModule();
5712 if (module_sp)
5713 {
5714 size_t num_loaded_sections = 0;
5715 SectionList *section_list = GetSectionList ();
5716 if (section_list)
5717 {
Greg Clayton7524e092014-02-06 20:10:16 +00005718 const size_t num_sections = section_list->GetSize();
5719
Greg Clayton751caf62014-02-07 22:54:47 +00005720 if (value_is_offset)
Greg Clayton7524e092014-02-06 20:10:16 +00005721 {
Greg Clayton751caf62014-02-07 22:54:47 +00005722 // "value" is an offset to apply to each top level segment
Greg Clayton7524e092014-02-06 20:10:16 +00005723 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5724 {
5725 // Iterate through the object file sections to find all
5726 // of the sections that size on disk (to avoid __PAGEZERO)
5727 // and load them
5728 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Greg Clayton751caf62014-02-07 22:54:47 +00005729 if (section_sp &&
5730 section_sp->GetFileSize() > 0 &&
5731 section_sp->IsThreadSpecific() == false &&
5732 module_sp.get() == section_sp->GetModule().get())
Greg Clayton7524e092014-02-06 20:10:16 +00005733 {
Greg Clayton751caf62014-02-07 22:54:47 +00005734 // Ignore __LINKEDIT and __DWARF segments
Greg Clayton07347372015-06-08 21:53:11 +00005735 if (section_sp->GetName() == GetSegmentNameLINKEDIT())
Greg Clayton751caf62014-02-07 22:54:47 +00005736 {
5737 // Only map __LINKEDIT if we have an in memory image and this isn't
5738 // a kernel binary like a kext or mach_kernel.
Greg Clayton07347372015-06-08 21:53:11 +00005739 const bool is_memory_image = (bool)m_process_wp.lock();
5740 const Strata strata = GetStrata();
Greg Clayton751caf62014-02-07 22:54:47 +00005741 if (is_memory_image == false || strata == eStrataKernel)
5742 continue;
5743 }
5744 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
Greg Clayton7524e092014-02-06 20:10:16 +00005745 ++num_loaded_sections;
5746 }
5747 }
5748 }
Greg Clayton751caf62014-02-07 22:54:47 +00005749 else
5750 {
5751 // "value" is the new base address of the mach_header, adjust each
5752 // section accordingly
5753
Greg Clayton07347372015-06-08 21:53:11 +00005754 Section *mach_header_section = GetMachHeaderSection();
5755 if (mach_header_section)
Greg Clayton751caf62014-02-07 22:54:47 +00005756 {
5757 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5758 {
Greg Clayton751caf62014-02-07 22:54:47 +00005759 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Greg Clayton07347372015-06-08 21:53:11 +00005760
5761 lldb::addr_t section_load_addr = CalculateSectionLoadAddressForMemoryImage(value, mach_header_section, section_sp.get());
5762 if (section_load_addr != LLDB_INVALID_ADDRESS)
Greg Clayton751caf62014-02-07 22:54:47 +00005763 {
Greg Clayton07347372015-06-08 21:53:11 +00005764 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_load_addr))
Greg Clayton751caf62014-02-07 22:54:47 +00005765 ++num_loaded_sections;
5766 }
5767 }
5768 }
5769 }
Greg Clayton7524e092014-02-06 20:10:16 +00005770 }
Greg Clayton7524e092014-02-06 20:10:16 +00005771 return num_loaded_sections > 0;
5772 }
Jason Molenda5cf1e232014-10-16 07:41:32 +00005773 return false;
Greg Clayton7524e092014-02-06 20:10:16 +00005774}
5775
Greg Claytona2715cf2014-06-13 00:54:12 +00005776bool
5777ObjectFileMachO::SaveCore (const lldb::ProcessSP &process_sp,
5778 const FileSpec &outfile,
5779 Error &error)
5780{
5781 if (process_sp)
5782 {
5783 Target &target = process_sp->GetTarget();
5784 const ArchSpec target_arch = target.GetArchitecture();
5785 const llvm::Triple &target_triple = target_arch.GetTriple();
5786 if (target_triple.getVendor() == llvm::Triple::Apple &&
Jason Molenda649a6072015-11-10 05:21:54 +00005787 (target_triple.getOS() == llvm::Triple::MacOSX
5788 || target_triple.getOS() == llvm::Triple::IOS
5789 || target_triple.getOS() == llvm::Triple::WatchOS
5790 || target_triple.getOS() == llvm::Triple::TvOS))
Greg Claytona2715cf2014-06-13 00:54:12 +00005791 {
5792 bool make_core = false;
5793 switch (target_arch.GetMachine())
5794 {
Jason Molenda16dc86d2015-06-25 23:58:25 +00005795 case llvm::Triple::aarch64:
Jason Molenda4b0c1182014-11-12 02:39:14 +00005796 case llvm::Triple::arm:
Jason Molenda6d9fe8c2015-08-21 00:13:37 +00005797 case llvm::Triple::thumb:
Greg Claytona2715cf2014-06-13 00:54:12 +00005798 case llvm::Triple::x86:
5799 case llvm::Triple::x86_64:
5800 make_core = true;
5801 break;
5802 default:
5803 error.SetErrorStringWithFormat ("unsupported core architecture: %s", target_triple.str().c_str());
5804 break;
5805 }
5806
5807 if (make_core)
5808 {
5809 std::vector<segment_command_64> segment_load_commands;
Saleem Abdulrasool3924d752014-06-13 03:30:39 +00005810// uint32_t range_info_idx = 0;
Greg Claytona2715cf2014-06-13 00:54:12 +00005811 MemoryRegionInfo range_info;
5812 Error range_error = process_sp->GetMemoryRegionInfo(0, range_info);
Jason Molendad20359d2014-11-11 10:59:15 +00005813 const uint32_t addr_byte_size = target_arch.GetAddressByteSize();
5814 const ByteOrder byte_order = target_arch.GetByteOrder();
Greg Claytona2715cf2014-06-13 00:54:12 +00005815 if (range_error.Success())
5816 {
5817 while (range_info.GetRange().GetRangeBase() != LLDB_INVALID_ADDRESS)
5818 {
5819 const addr_t addr = range_info.GetRange().GetRangeBase();
5820 const addr_t size = range_info.GetRange().GetByteSize();
5821
5822 if (size == 0)
5823 break;
5824
5825 // Calculate correct protections
5826 uint32_t prot = 0;
5827 if (range_info.GetReadable() == MemoryRegionInfo::eYes)
5828 prot |= VM_PROT_READ;
5829 if (range_info.GetWritable() == MemoryRegionInfo::eYes)
5830 prot |= VM_PROT_WRITE;
5831 if (range_info.GetExecutable() == MemoryRegionInfo::eYes)
5832 prot |= VM_PROT_EXECUTE;
5833
5834// printf ("[%3u] [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") %c%c%c\n",
5835// range_info_idx,
5836// addr,
5837// size,
5838// (prot & VM_PROT_READ ) ? 'r' : '-',
5839// (prot & VM_PROT_WRITE ) ? 'w' : '-',
5840// (prot & VM_PROT_EXECUTE) ? 'x' : '-');
5841
5842 if (prot != 0)
5843 {
Jason Molendad20359d2014-11-11 10:59:15 +00005844 uint32_t cmd_type = LC_SEGMENT_64;
5845 uint32_t segment_size = sizeof (segment_command_64);
5846 if (addr_byte_size == 4)
5847 {
5848 cmd_type = LC_SEGMENT;
5849 segment_size = sizeof (segment_command);
5850 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005851 segment_command_64 segment = {
Jason Molendad20359d2014-11-11 10:59:15 +00005852 cmd_type, // uint32_t cmd;
5853 segment_size, // uint32_t cmdsize;
Greg Claytona2715cf2014-06-13 00:54:12 +00005854 {0}, // char segname[16];
Jason Molendad20359d2014-11-11 10:59:15 +00005855 addr, // uint64_t vmaddr; // uint32_t for 32-bit Mach-O
5856 size, // uint64_t vmsize; // uint32_t for 32-bit Mach-O
5857 0, // uint64_t fileoff; // uint32_t for 32-bit Mach-O
5858 size, // uint64_t filesize; // uint32_t for 32-bit Mach-O
Greg Claytona2715cf2014-06-13 00:54:12 +00005859 prot, // uint32_t maxprot;
5860 prot, // uint32_t initprot;
5861 0, // uint32_t nsects;
5862 0 }; // uint32_t flags;
5863 segment_load_commands.push_back(segment);
5864 }
5865 else
5866 {
5867 // No protections and a size of 1 used to be returned from old
5868 // debugservers when we asked about a region that was past the
5869 // last memory region and it indicates the end...
5870 if (size == 1)
5871 break;
5872 }
5873
5874 range_error = process_sp->GetMemoryRegionInfo(range_info.GetRange().GetRangeEnd(), range_info);
5875 if (range_error.Fail())
5876 break;
5877 }
5878
Greg Claytona2715cf2014-06-13 00:54:12 +00005879 StreamString buffer (Stream::eBinary,
5880 addr_byte_size,
5881 byte_order);
5882
5883 mach_header_64 mach_header;
Jason Molendad20359d2014-11-11 10:59:15 +00005884 if (addr_byte_size == 8)
5885 {
5886 mach_header.magic = MH_MAGIC_64;
5887 }
5888 else
5889 {
5890 mach_header.magic = MH_MAGIC;
5891 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005892 mach_header.cputype = target_arch.GetMachOCPUType();
5893 mach_header.cpusubtype = target_arch.GetMachOCPUSubType();
5894 mach_header.filetype = MH_CORE;
5895 mach_header.ncmds = segment_load_commands.size();
5896 mach_header.flags = 0;
5897 mach_header.reserved = 0;
5898 ThreadList &thread_list = process_sp->GetThreadList();
5899 const uint32_t num_threads = thread_list.GetSize();
5900
5901 // Make an array of LC_THREAD data items. Each one contains
5902 // the contents of the LC_THREAD load command. The data doesn't
5903 // contain the load command + load command size, we will
5904 // add the load command and load command size as we emit the data.
5905 std::vector<StreamString> LC_THREAD_datas(num_threads);
5906 for (auto &LC_THREAD_data : LC_THREAD_datas)
5907 {
5908 LC_THREAD_data.GetFlags().Set(Stream::eBinary);
5909 LC_THREAD_data.SetAddressByteSize(addr_byte_size);
5910 LC_THREAD_data.SetByteOrder(byte_order);
5911 }
5912 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx)
5913 {
5914 ThreadSP thread_sp (thread_list.GetThreadAtIndex(thread_idx));
5915 if (thread_sp)
5916 {
5917 switch (mach_header.cputype)
5918 {
Jason Molenda22952582014-11-12 01:11:36 +00005919 case llvm::MachO::CPU_TYPE_ARM64:
5920 RegisterContextDarwin_arm64_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
Greg Claytona2715cf2014-06-13 00:54:12 +00005921 break;
Jason Molenda22952582014-11-12 01:11:36 +00005922
5923 case llvm::MachO::CPU_TYPE_ARM:
5924 RegisterContextDarwin_arm_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5925 break;
5926
Greg Claytona2715cf2014-06-13 00:54:12 +00005927 case llvm::MachO::CPU_TYPE_I386:
Jason Molendad20359d2014-11-11 10:59:15 +00005928 RegisterContextDarwin_i386_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
Greg Claytona2715cf2014-06-13 00:54:12 +00005929 break;
5930
5931 case llvm::MachO::CPU_TYPE_X86_64:
5932 RegisterContextDarwin_x86_64_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5933 break;
5934 }
5935
5936 }
5937 }
5938
5939 // The size of the load command is the size of the segments...
Jason Molendad20359d2014-11-11 10:59:15 +00005940 if (addr_byte_size == 8)
5941 {
5942 mach_header.sizeofcmds = segment_load_commands.size() * sizeof (struct segment_command_64);
5943 }
5944 else
5945 {
5946 mach_header.sizeofcmds = segment_load_commands.size() * sizeof (struct segment_command);
5947 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005948
5949 // and the size of all LC_THREAD load command
5950 for (const auto &LC_THREAD_data : LC_THREAD_datas)
5951 {
Greg Claytone37df2e2014-09-16 20:50:29 +00005952 ++mach_header.ncmds;
Greg Claytona2715cf2014-06-13 00:54:12 +00005953 mach_header.sizeofcmds += 8 + LC_THREAD_data.GetSize();
5954 }
5955
5956 printf ("mach_header: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x\n",
5957 mach_header.magic,
5958 mach_header.cputype,
5959 mach_header.cpusubtype,
5960 mach_header.filetype,
5961 mach_header.ncmds,
5962 mach_header.sizeofcmds,
5963 mach_header.flags,
5964 mach_header.reserved);
5965
5966 // Write the mach header
5967 buffer.PutHex32(mach_header.magic);
5968 buffer.PutHex32(mach_header.cputype);
5969 buffer.PutHex32(mach_header.cpusubtype);
5970 buffer.PutHex32(mach_header.filetype);
5971 buffer.PutHex32(mach_header.ncmds);
5972 buffer.PutHex32(mach_header.sizeofcmds);
5973 buffer.PutHex32(mach_header.flags);
Jason Molendad20359d2014-11-11 10:59:15 +00005974 if (addr_byte_size == 8)
5975 {
5976 buffer.PutHex32(mach_header.reserved);
5977 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005978
5979 // Skip the mach header and all load commands and align to the next
5980 // 0x1000 byte boundary
5981 addr_t file_offset = buffer.GetSize() + mach_header.sizeofcmds;
5982 if (file_offset & 0x00000fff)
5983 {
5984 file_offset += 0x00001000ull;
5985 file_offset &= (~0x00001000ull + 1);
5986 }
5987
5988 for (auto &segment : segment_load_commands)
5989 {
5990 segment.fileoff = file_offset;
5991 file_offset += segment.filesize;
5992 }
5993
5994 // Write out all of the LC_THREAD load commands
5995 for (const auto &LC_THREAD_data : LC_THREAD_datas)
5996 {
5997 const size_t LC_THREAD_data_size = LC_THREAD_data.GetSize();
5998 buffer.PutHex32(LC_THREAD);
5999 buffer.PutHex32(8 + LC_THREAD_data_size); // cmd + cmdsize + data
6000 buffer.Write(LC_THREAD_data.GetData(), LC_THREAD_data_size);
6001 }
6002
6003 // Write out all of the segment load commands
6004 for (const auto &segment : segment_load_commands)
6005 {
6006 printf ("0x%8.8x 0x%8.8x [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") [0x%16.16" PRIx64 " 0x%16.16" PRIx64 ") 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x]\n",
6007 segment.cmd,
6008 segment.cmdsize,
6009 segment.vmaddr,
6010 segment.vmaddr + segment.vmsize,
6011 segment.fileoff,
6012 segment.filesize,
6013 segment.maxprot,
6014 segment.initprot,
6015 segment.nsects,
6016 segment.flags);
6017
6018 buffer.PutHex32(segment.cmd);
6019 buffer.PutHex32(segment.cmdsize);
6020 buffer.PutRawBytes(segment.segname, sizeof(segment.segname));
Jason Molendad20359d2014-11-11 10:59:15 +00006021 if (addr_byte_size == 8)
6022 {
6023 buffer.PutHex64(segment.vmaddr);
6024 buffer.PutHex64(segment.vmsize);
6025 buffer.PutHex64(segment.fileoff);
6026 buffer.PutHex64(segment.filesize);
6027 }
6028 else
6029 {
6030 buffer.PutHex32(static_cast<uint32_t>(segment.vmaddr));
6031 buffer.PutHex32(static_cast<uint32_t>(segment.vmsize));
6032 buffer.PutHex32(static_cast<uint32_t>(segment.fileoff));
6033 buffer.PutHex32(static_cast<uint32_t>(segment.filesize));
6034 }
Greg Claytona2715cf2014-06-13 00:54:12 +00006035 buffer.PutHex32(segment.maxprot);
6036 buffer.PutHex32(segment.initprot);
6037 buffer.PutHex32(segment.nsects);
6038 buffer.PutHex32(segment.flags);
6039 }
6040
6041 File core_file;
6042 std::string core_file_path(outfile.GetPath());
6043 error = core_file.Open(core_file_path.c_str(),
6044 File::eOpenOptionWrite |
6045 File::eOpenOptionTruncate |
6046 File::eOpenOptionCanCreate);
6047 if (error.Success())
6048 {
6049 // Read 1 page at a time
6050 uint8_t bytes[0x1000];
6051 // Write the mach header and load commands out to the core file
6052 size_t bytes_written = buffer.GetString().size();
6053 error = core_file.Write(buffer.GetString().data(), bytes_written);
6054 if (error.Success())
6055 {
6056 // Now write the file data for all memory segments in the process
6057 for (const auto &segment : segment_load_commands)
6058 {
David Majnemerb98a5e02014-07-24 00:24:12 +00006059 if (core_file.SeekFromStart(segment.fileoff) == -1)
Greg Claytona2715cf2014-06-13 00:54:12 +00006060 {
6061 error.SetErrorStringWithFormat("unable to seek to offset 0x%" PRIx64 " in '%s'", segment.fileoff, core_file_path.c_str());
6062 break;
6063 }
6064
Jason Molendad20359d2014-11-11 10:59:15 +00006065 printf ("Saving %" PRId64 " bytes of data for memory region at 0x%" PRIx64 "\n", segment.vmsize, segment.vmaddr);
Greg Claytona2715cf2014-06-13 00:54:12 +00006066 addr_t bytes_left = segment.vmsize;
6067 addr_t addr = segment.vmaddr;
6068 Error memory_read_error;
6069 while (bytes_left > 0 && error.Success())
6070 {
6071 const size_t bytes_to_read = bytes_left > sizeof(bytes) ? sizeof(bytes) : bytes_left;
6072 const size_t bytes_read = process_sp->ReadMemory(addr, bytes, bytes_to_read, memory_read_error);
6073 if (bytes_read == bytes_to_read)
6074 {
6075 size_t bytes_written = bytes_read;
6076 error = core_file.Write(bytes, bytes_written);
6077 bytes_left -= bytes_read;
6078 addr += bytes_read;
6079 }
6080 else
6081 {
6082 // Some pages within regions are not readable, those
6083 // should be zero filled
6084 memset (bytes, 0, bytes_to_read);
6085 size_t bytes_written = bytes_to_read;
6086 error = core_file.Write(bytes, bytes_written);
6087 bytes_left -= bytes_to_read;
6088 addr += bytes_to_read;
6089 }
6090 }
6091 }
6092 }
6093 }
6094 }
6095 else
6096 {
6097 error.SetErrorString("process doesn't support getting memory region info");
6098 }
6099 }
6100 return true; // This is the right plug to handle saving core files for this process
6101 }
6102 }
6103 return false;
6104}