blob: 457e8bd4caab68003032471dfedecaf7473d26f1 [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(),
Jason Molenda955dcf22016-05-04 03:09:40 +00001133 m_thread_context_offsets_valid(false),
1134 m_reexported_dylibs (),
1135 m_allow_assembly_emulation_unwind_plans (true)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001136{
Greg Clayton72b77eb2011-02-04 21:13:05 +00001137 ::memset (&m_header, 0, sizeof(m_header));
1138 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139}
1140
Greg Claytone72dfb32012-02-24 01:59:29 +00001141ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +00001142 lldb::DataBufferSP& header_data_sp,
1143 const lldb::ProcessSP &process_sp,
1144 lldb::addr_t header_addr) :
Greg Claytone72dfb32012-02-24 01:59:29 +00001145 ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
Greg Claytonc3776bf2012-02-09 06:16:32 +00001146 m_mach_segments(),
1147 m_mach_sections(),
1148 m_entry_point_address(),
1149 m_thread_context_offsets(),
Jason Molenda955dcf22016-05-04 03:09:40 +00001150 m_thread_context_offsets_valid(false),
1151 m_reexported_dylibs (),
1152 m_allow_assembly_emulation_unwind_plans (true)
Greg Claytonc9660542012-02-05 02:38:54 +00001153{
1154 ::memset (&m_header, 0, sizeof(m_header));
1155 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
1156}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157
Greg Claytonf4d6de62013-04-24 22:29:28 +00001158bool
1159ObjectFileMachO::ParseHeader (DataExtractor &data,
1160 lldb::offset_t *data_offset_ptr,
1161 llvm::MachO::mach_header &header)
1162{
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001163 data.SetByteOrder (endian::InlHostByteOrder());
Greg Claytonf4d6de62013-04-24 22:29:28 +00001164 // Leave magic in the original byte order
1165 header.magic = data.GetU32(data_offset_ptr);
1166 bool can_parse = false;
1167 bool is_64_bit = false;
1168 switch (header.magic)
1169 {
Charles Davis510938e2013-08-27 05:04:57 +00001170 case MH_MAGIC:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001171 data.SetByteOrder (endian::InlHostByteOrder());
Greg Claytonf4d6de62013-04-24 22:29:28 +00001172 data.SetAddressByteSize(4);
1173 can_parse = true;
1174 break;
1175
Charles Davis510938e2013-08-27 05:04:57 +00001176 case MH_MAGIC_64:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001177 data.SetByteOrder (endian::InlHostByteOrder());
Greg Claytonf4d6de62013-04-24 22:29:28 +00001178 data.SetAddressByteSize(8);
1179 can_parse = true;
1180 is_64_bit = true;
1181 break;
1182
Charles Davis510938e2013-08-27 05:04:57 +00001183 case MH_CIGAM:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001184 data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Greg Claytonf4d6de62013-04-24 22:29:28 +00001185 data.SetAddressByteSize(4);
1186 can_parse = true;
1187 break;
1188
Charles Davis510938e2013-08-27 05:04:57 +00001189 case MH_CIGAM_64:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001190 data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Greg Claytonf4d6de62013-04-24 22:29:28 +00001191 data.SetAddressByteSize(8);
1192 is_64_bit = true;
1193 can_parse = true;
1194 break;
1195
1196 default:
1197 break;
1198 }
1199
1200 if (can_parse)
1201 {
1202 data.GetU32(data_offset_ptr, &header.cputype, 6);
1203 if (is_64_bit)
1204 *data_offset_ptr += 4;
1205 return true;
1206 }
1207 else
1208 {
1209 memset(&header, 0, sizeof(header));
1210 }
1211 return false;
1212}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001213
1214bool
1215ObjectFileMachO::ParseHeader ()
1216{
Greg Claytona1743492012-03-13 23:14:29 +00001217 ModuleSP module_sp(GetModule());
1218 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001219 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001220 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Greg Claytona1743492012-03-13 23:14:29 +00001221 bool can_parse = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00001222 lldb::offset_t offset = 0;
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001223 m_data.SetByteOrder (endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +00001224 // Leave magic in the original byte order
1225 m_header.magic = m_data.GetU32(&offset);
1226 switch (m_header.magic)
Greg Claytonc9660542012-02-05 02:38:54 +00001227 {
Charles Davis510938e2013-08-27 05:04:57 +00001228 case MH_MAGIC:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001229 m_data.SetByteOrder (endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +00001230 m_data.SetAddressByteSize(4);
1231 can_parse = true;
1232 break;
1233
Charles Davis510938e2013-08-27 05:04:57 +00001234 case MH_MAGIC_64:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001235 m_data.SetByteOrder (endian::InlHostByteOrder());
Greg Claytona1743492012-03-13 23:14:29 +00001236 m_data.SetAddressByteSize(8);
1237 can_parse = true;
1238 break;
1239
Charles Davis510938e2013-08-27 05:04:57 +00001240 case MH_CIGAM:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001241 m_data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Greg Claytona1743492012-03-13 23:14:29 +00001242 m_data.SetAddressByteSize(4);
1243 can_parse = true;
1244 break;
1245
Charles Davis510938e2013-08-27 05:04:57 +00001246 case MH_CIGAM_64:
Bruce Mitchener9ccb9702015-11-07 04:40:13 +00001247 m_data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Greg Claytona1743492012-03-13 23:14:29 +00001248 m_data.SetAddressByteSize(8);
1249 can_parse = true;
1250 break;
1251
1252 default:
1253 break;
Greg Claytonc9660542012-02-05 02:38:54 +00001254 }
Greg Claytona1743492012-03-13 23:14:29 +00001255
1256 if (can_parse)
1257 {
1258 m_data.GetU32(&offset, &m_header.cputype, 6);
1259
Greg Clayton7ab7f892014-05-29 21:33:45 +00001260
1261 ArchSpec mach_arch;
1262
1263 if (GetArchitecture (mach_arch))
Greg Claytona1743492012-03-13 23:14:29 +00001264 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001265 // Check if the module has a required architecture
1266 const ArchSpec &module_arch = module_sp->GetArchitecture();
1267 if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch))
1268 return false;
1269
1270 if (SetModulesArchitecture (mach_arch))
Greg Claytona1743492012-03-13 23:14:29 +00001271 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001272 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
1273 if (m_data.GetByteSize() < header_and_lc_size)
Greg Claytona1743492012-03-13 23:14:29 +00001274 {
Greg Clayton7ab7f892014-05-29 21:33:45 +00001275 DataBufferSP data_sp;
1276 ProcessSP process_sp (m_process_wp.lock());
1277 if (process_sp)
1278 {
1279 data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size);
1280 }
1281 else
1282 {
1283 // Read in all only the load command data from the file on disk
1284 data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size);
1285 if (data_sp->GetByteSize() != header_and_lc_size)
1286 return false;
1287 }
1288 if (data_sp)
1289 m_data.SetData (data_sp);
Greg Claytona1743492012-03-13 23:14:29 +00001290 }
Greg Claytona1743492012-03-13 23:14:29 +00001291 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00001292 return true;
Greg Claytona1743492012-03-13 23:14:29 +00001293 }
Greg Claytona1743492012-03-13 23:14:29 +00001294 }
1295 else
1296 {
1297 memset(&m_header, 0, sizeof(struct mach_header));
1298 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001299 }
1300 return false;
1301}
1302
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303ByteOrder
1304ObjectFileMachO::GetByteOrder () const
1305{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001306 return m_data.GetByteOrder ();
1307}
1308
Jim Ingham5aee1622010-08-09 23:31:02 +00001309bool
1310ObjectFileMachO::IsExecutable() const
1311{
Charles Davis510938e2013-08-27 05:04:57 +00001312 return m_header.filetype == MH_EXECUTE;
Jim Ingham5aee1622010-08-09 23:31:02 +00001313}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001314
Greg Claytonc7bece562013-01-25 18:06:21 +00001315uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316ObjectFileMachO::GetAddressByteSize () const
1317{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318 return m_data.GetAddressByteSize ();
1319}
1320
Greg Claytone0d378b2011-03-24 21:19:54 +00001321AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +00001322ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
1323{
1324 Symtab *symtab = GetSymtab();
1325 if (symtab)
1326 {
1327 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
1328 if (symbol)
1329 {
Greg Claytone7612132012-03-07 21:03:09 +00001330 if (symbol->ValueIsAddress())
Greg Claytonded470d2011-03-19 01:12:21 +00001331 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00001332 SectionSP section_sp (symbol->GetAddressRef().GetSection());
Greg Claytone72dfb32012-02-24 01:59:29 +00001333 if (section_sp)
Greg Claytonded470d2011-03-19 01:12:21 +00001334 {
Charles Davis510938e2013-08-27 05:04:57 +00001335 const lldb::SectionType section_type = section_sp->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +00001336 switch (section_type)
1337 {
Jason Molendae589e7e2014-12-08 03:09:00 +00001338 case eSectionTypeInvalid:
1339 return eAddressClassUnknown;
1340
Greg Claytonded470d2011-03-19 01:12:21 +00001341 case eSectionTypeCode:
Charles Davis510938e2013-08-27 05:04:57 +00001342 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +00001343 {
1344 // For ARM we have a bit in the n_desc field of the symbol
1345 // that tells us ARM/Thumb which is bit 0x0008.
1346 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1347 return eAddressClassCodeAlternateISA;
1348 }
1349 return eAddressClassCode;
1350
Jason Molendae589e7e2014-12-08 03:09:00 +00001351 case eSectionTypeContainer:
1352 return eAddressClassUnknown;
1353
Greg Clayton5009f9d2011-10-27 17:55:14 +00001354 case eSectionTypeData:
1355 case eSectionTypeDataCString:
1356 case eSectionTypeDataCStringPointers:
1357 case eSectionTypeDataSymbolAddress:
1358 case eSectionTypeData4:
1359 case eSectionTypeData8:
1360 case eSectionTypeData16:
1361 case eSectionTypeDataPointers:
1362 case eSectionTypeZeroFill:
1363 case eSectionTypeDataObjCMessageRefs:
1364 case eSectionTypeDataObjCCFStrings:
Ryan Brown65d4d5c2015-09-16 21:20:44 +00001365 case eSectionTypeGoSymtab:
Greg Clayton5009f9d2011-10-27 17:55:14 +00001366 return eAddressClassData;
Jason Molendae589e7e2014-12-08 03:09:00 +00001367
Greg Clayton5009f9d2011-10-27 17:55:14 +00001368 case eSectionTypeDebug:
1369 case eSectionTypeDWARFDebugAbbrev:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001370 case eSectionTypeDWARFDebugAddr:
Greg Clayton5009f9d2011-10-27 17:55:14 +00001371 case eSectionTypeDWARFDebugAranges:
1372 case eSectionTypeDWARFDebugFrame:
1373 case eSectionTypeDWARFDebugInfo:
1374 case eSectionTypeDWARFDebugLine:
1375 case eSectionTypeDWARFDebugLoc:
1376 case eSectionTypeDWARFDebugMacInfo:
Siva Chandrad8335e92015-12-16 00:22:08 +00001377 case eSectionTypeDWARFDebugMacro:
Greg Clayton5009f9d2011-10-27 17:55:14 +00001378 case eSectionTypeDWARFDebugPubNames:
1379 case eSectionTypeDWARFDebugPubTypes:
1380 case eSectionTypeDWARFDebugRanges:
1381 case eSectionTypeDWARFDebugStr:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001382 case eSectionTypeDWARFDebugStrOffsets:
Greg Clayton5009f9d2011-10-27 17:55:14 +00001383 case eSectionTypeDWARFAppleNames:
1384 case eSectionTypeDWARFAppleTypes:
1385 case eSectionTypeDWARFAppleNamespaces:
1386 case eSectionTypeDWARFAppleObjC:
1387 return eAddressClassDebug;
Jason Molendae589e7e2014-12-08 03:09:00 +00001388
1389 case eSectionTypeEHFrame:
Tamas Berghammer648f3c72015-09-30 13:50:14 +00001390 case eSectionTypeARMexidx:
1391 case eSectionTypeARMextab:
Jason Molendae589e7e2014-12-08 03:09:00 +00001392 case eSectionTypeCompactUnwind:
1393 return eAddressClassRuntime;
1394
Tamas Berghammer31a2f8f2016-02-25 12:23:43 +00001395 case eSectionTypeAbsoluteAddress:
Michael Sartaina7499c92013-07-01 19:45:50 +00001396 case eSectionTypeELFSymbolTable:
1397 case eSectionTypeELFDynamicSymbols:
1398 case eSectionTypeELFRelocationEntries:
1399 case eSectionTypeELFDynamicLinkInfo:
Jason Molendae589e7e2014-12-08 03:09:00 +00001400 case eSectionTypeOther:
1401 return eAddressClassUnknown;
Greg Claytonded470d2011-03-19 01:12:21 +00001402 }
1403 }
1404 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00001405
Greg Claytone0d378b2011-03-24 21:19:54 +00001406 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +00001407 switch (symbol_type)
1408 {
1409 case eSymbolTypeAny: return eAddressClassUnknown;
1410 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Jason Molenda4e7511e2013-03-06 23:19:17 +00001411
Greg Claytonded470d2011-03-19 01:12:21 +00001412 case eSymbolTypeCode:
1413 case eSymbolTypeTrampoline:
Greg Clayton059f7242013-02-27 21:16:04 +00001414 case eSymbolTypeResolver:
Charles Davis510938e2013-08-27 05:04:57 +00001415 if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM)
Greg Claytonded470d2011-03-19 01:12:21 +00001416 {
1417 // For ARM we have a bit in the n_desc field of the symbol
1418 // that tells us ARM/Thumb which is bit 0x0008.
1419 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
1420 return eAddressClassCodeAlternateISA;
1421 }
1422 return eAddressClassCode;
1423
1424 case eSymbolTypeData: return eAddressClassData;
1425 case eSymbolTypeRuntime: return eAddressClassRuntime;
1426 case eSymbolTypeException: return eAddressClassRuntime;
1427 case eSymbolTypeSourceFile: return eAddressClassDebug;
1428 case eSymbolTypeHeaderFile: return eAddressClassDebug;
1429 case eSymbolTypeObjectFile: return eAddressClassDebug;
1430 case eSymbolTypeCommonBlock: return eAddressClassDebug;
1431 case eSymbolTypeBlock: return eAddressClassDebug;
1432 case eSymbolTypeLocal: return eAddressClassData;
1433 case eSymbolTypeParam: return eAddressClassData;
1434 case eSymbolTypeVariable: return eAddressClassData;
1435 case eSymbolTypeVariableType: return eAddressClassDebug;
1436 case eSymbolTypeLineEntry: return eAddressClassDebug;
1437 case eSymbolTypeLineHeader: return eAddressClassDebug;
1438 case eSymbolTypeScopeBegin: return eAddressClassDebug;
1439 case eSymbolTypeScopeEnd: return eAddressClassDebug;
1440 case eSymbolTypeAdditional: return eAddressClassUnknown;
1441 case eSymbolTypeCompiler: return eAddressClassDebug;
1442 case eSymbolTypeInstrumentation:return eAddressClassDebug;
1443 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +00001444 case eSymbolTypeObjCClass: return eAddressClassRuntime;
1445 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
1446 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Clayton9191db42013-10-21 18:40:51 +00001447 case eSymbolTypeReExported: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +00001448 }
1449 }
1450 }
1451 return eAddressClassUnknown;
1452}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001453
1454Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +00001455ObjectFileMachO::GetSymtab()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001456{
Greg Claytona1743492012-03-13 23:14:29 +00001457 ModuleSP module_sp(GetModule());
1458 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001459 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001460 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Greg Claytona1743492012-03-13 23:14:29 +00001461 if (m_symtab_ap.get() == NULL)
1462 {
1463 m_symtab_ap.reset(new Symtab(this));
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00001464 std::lock_guard<std::recursive_mutex> symtab_guard(m_symtab_ap->GetMutex());
Greg Clayton3046e662013-07-10 01:23:25 +00001465 ParseSymtab ();
Greg Claytona1743492012-03-13 23:14:29 +00001466 m_symtab_ap->Finalize ();
1467 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001468 }
1469 return m_symtab_ap.get();
1470}
1471
Greg Clayton3046e662013-07-10 01:23:25 +00001472bool
1473ObjectFileMachO::IsStripped ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001474{
Greg Clayton3046e662013-07-10 01:23:25 +00001475 if (m_dysymtab.cmd == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476 {
Greg Clayton3046e662013-07-10 01:23:25 +00001477 ModuleSP module_sp(GetModule());
1478 if (module_sp)
Greg Claytona1743492012-03-13 23:14:29 +00001479 {
Greg Clayton3046e662013-07-10 01:23:25 +00001480 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1481 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Clayton4d78c402012-05-25 18:09:55 +00001482 {
Greg Clayton3046e662013-07-10 01:23:25 +00001483 const lldb::offset_t load_cmd_offset = offset;
1484
1485 load_command lc;
1486 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
1487 break;
Charles Davis510938e2013-08-27 05:04:57 +00001488 if (lc.cmd == LC_DYSYMTAB)
Greg Clayton4d78c402012-05-25 18:09:55 +00001489 {
Greg Clayton3046e662013-07-10 01:23:25 +00001490 m_dysymtab.cmd = lc.cmd;
1491 m_dysymtab.cmdsize = lc.cmdsize;
1492 if (m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == NULL)
1493 {
1494 // Clear m_dysymtab if we were unable to read all items from the load command
1495 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
1496 }
Greg Clayton4d78c402012-05-25 18:09:55 +00001497 }
Greg Clayton3046e662013-07-10 01:23:25 +00001498 offset = load_cmd_offset + lc.cmdsize;
Greg Clayton4d78c402012-05-25 18:09:55 +00001499 }
Greg Clayton1eac0c72012-04-24 03:06:13 +00001500 }
Greg Clayton1eac0c72012-04-24 03:06:13 +00001501 }
Greg Clayton3046e662013-07-10 01:23:25 +00001502 if (m_dysymtab.cmd)
Greg Clayton93e28612013-10-11 22:03:48 +00001503 return m_dysymtab.nlocalsym <= 1;
Greg Clayton3046e662013-07-10 01:23:25 +00001504 return false;
1505}
Greg Clayton1eac0c72012-04-24 03:06:13 +00001506
Greg Clayton3046e662013-07-10 01:23:25 +00001507void
1508ObjectFileMachO::CreateSections (SectionList &unified_section_list)
1509{
1510 if (!m_sections_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001511 {
Greg Clayton3046e662013-07-10 01:23:25 +00001512 m_sections_ap.reset(new SectionList());
1513
Charles Davis510938e2013-08-27 05:04:57 +00001514 const bool is_dsym = (m_header.filetype == MH_DSYM);
Greg Clayton3046e662013-07-10 01:23:25 +00001515 lldb::user_id_t segID = 0;
1516 lldb::user_id_t sectID = 0;
1517 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
1518 uint32_t i;
1519 const bool is_core = GetType() == eTypeCoreFile;
1520 //bool dump_sections = false;
1521 ModuleSP module_sp (GetModule());
1522 // First look up any LC_ENCRYPTION_INFO load commands
1523 typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
1524 EncryptedFileRanges encrypted_file_ranges;
1525 encryption_info_command encryption_cmd;
1526 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001527 {
Greg Clayton3046e662013-07-10 01:23:25 +00001528 const lldb::offset_t load_cmd_offset = offset;
1529 if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL)
1530 break;
1531
Jason Molendadfb02a92015-04-02 05:19:33 +00001532 // LC_ENCRYPTION_INFO and LC_ENCRYPTION_INFO_64 have the same sizes for
1533 // the 3 fields we care about, so treat them the same.
1534 if (encryption_cmd.cmd == LC_ENCRYPTION_INFO || encryption_cmd.cmd == LC_ENCRYPTION_INFO_64)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535 {
Greg Clayton3046e662013-07-10 01:23:25 +00001536 if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3))
Jason Molendaf6ce26f2013-04-10 05:58:57 +00001537 {
Greg Clayton3046e662013-07-10 01:23:25 +00001538 if (encryption_cmd.cryptid != 0)
Greg Claytond37d6922013-04-16 16:51:19 +00001539 {
Greg Clayton3046e662013-07-10 01:23:25 +00001540 EncryptedFileRanges::Entry entry;
1541 entry.SetRangeBase(encryption_cmd.cryptoff);
1542 entry.SetByteSize(encryption_cmd.cryptsize);
1543 encrypted_file_ranges.Append(entry);
Jason Molendaf6ce26f2013-04-10 05:58:57 +00001544 }
1545 }
Greg Clayton3046e662013-07-10 01:23:25 +00001546 }
1547 offset = load_cmd_offset + encryption_cmd.cmdsize;
1548 }
1549
Jason Molenda05a09c62014-08-22 02:46:46 +00001550 bool section_file_addresses_changed = false;
1551
Greg Clayton3046e662013-07-10 01:23:25 +00001552 offset = MachHeaderSizeFromMagic(m_header.magic);
1553
1554 struct segment_command_64 load_cmd;
1555 for (i=0; i<m_header.ncmds; ++i)
1556 {
1557 const lldb::offset_t load_cmd_offset = offset;
1558 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1559 break;
1560
Charles Davis510938e2013-08-27 05:04:57 +00001561 if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64)
Greg Clayton3046e662013-07-10 01:23:25 +00001562 {
1563 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001564 {
Greg Clayton3046e662013-07-10 01:23:25 +00001565 bool add_section = true;
1566 bool add_to_unified = true;
1567 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 +00001568
Greg Clayton3046e662013-07-10 01:23:25 +00001569 SectionSP unified_section_sp(unified_section_list.FindSectionByName(const_segname));
1570 if (is_dsym && unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571 {
Greg Clayton3046e662013-07-10 01:23:25 +00001572 if (const_segname == GetSegmentNameLINKEDIT())
1573 {
1574 // We need to keep the __LINKEDIT segment private to this object file only
1575 add_to_unified = false;
1576 }
1577 else
1578 {
1579 // This is the dSYM file and this section has already been created by
1580 // the object file, no need to create it.
1581 add_section = false;
1582 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001583 }
Greg Clayton3046e662013-07-10 01:23:25 +00001584 load_cmd.vmaddr = m_data.GetAddress(&offset);
1585 load_cmd.vmsize = m_data.GetAddress(&offset);
1586 load_cmd.fileoff = m_data.GetAddress(&offset);
1587 load_cmd.filesize = m_data.GetAddress(&offset);
1588 if (m_length != 0 && load_cmd.filesize != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001589 {
Greg Clayton3046e662013-07-10 01:23:25 +00001590 if (load_cmd.fileoff > m_length)
1591 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001592 // 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 +00001593 // a corrupt file. We don't have any way to return an error condition here (this method
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001594 // was likely invoked from something like ObjectFile::GetSectionList()) -- all we can do
Greg Clayton3046e662013-07-10 01:23:25 +00001595 // is null out the SectionList vector and if a process has been set up, dump a message
1596 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001597 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001598 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 +00001599 i,
1600 lc_segment_name,
1601 load_cmd.fileoff,
1602 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001603
1604 load_cmd.fileoff = 0;
1605 load_cmd.filesize = 0;
1606 }
1607
1608 if (load_cmd.fileoff + load_cmd.filesize > m_length)
1609 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001610 // 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 +00001611 // a corrupt file. We don't have any way to return an error condition here (this method
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001612 // was likely invoked from something like ObjectFile::GetSectionList()) -- all we can do
Greg Clayton3046e662013-07-10 01:23:25 +00001613 // is null out the SectionList vector and if a process has been set up, dump a message
1614 // to stdout. The most common case here is core file debugging with a truncated file.
Charles Davis510938e2013-08-27 05:04:57 +00001615 const char *lc_segment_name = load_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT";
Jason Molenda7e50d912013-09-14 05:20:02 +00001616 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 +00001617 i,
1618 lc_segment_name,
1619 load_cmd.fileoff + load_cmd.filesize,
1620 m_length);
Greg Clayton3046e662013-07-10 01:23:25 +00001621
1622 // Tuncase the length
1623 load_cmd.filesize = m_length - load_cmd.fileoff;
1624 }
1625 }
1626 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
1627 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001628
Charles Davis510938e2013-08-27 05:04:57 +00001629 const bool segment_is_encrypted = (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001630
Greg Clayton3046e662013-07-10 01:23:25 +00001631 // Keep a list of mach segments around in case we need to
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001632 // get at data that isn't stored in the abstracted Sections.
Greg Clayton3046e662013-07-10 01:23:25 +00001633 m_mach_segments.push_back (load_cmd);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001634
Greg Clayton3046e662013-07-10 01:23:25 +00001635 // Use a segment ID of the segment index shifted left by 8 so they
1636 // never conflict with any of the sections.
1637 SectionSP segment_sp;
1638 if (add_section && (const_segname || is_core))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639 {
Greg Clayton3046e662013-07-10 01:23:25 +00001640 segment_sp.reset(new Section (module_sp, // Module to which this section belongs
1641 this, // Object file to which this sections belongs
1642 ++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
1643 const_segname, // Name of this section
1644 eSectionTypeContainer, // This section is a container of other sections.
1645 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
1646 load_cmd.vmsize, // VM size in bytes of this section
1647 load_cmd.fileoff, // Offset to the data for this section in the file
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001648 load_cmd.filesize, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +00001649 0, // Segments have no alignment information
Greg Clayton3046e662013-07-10 01:23:25 +00001650 load_cmd.flags)); // Flags for this section
Greg Clayton8d38ac42010-06-28 23:51:11 +00001651
Greg Clayton3046e662013-07-10 01:23:25 +00001652 segment_sp->SetIsEncrypted (segment_is_encrypted);
1653 m_sections_ap->AddSection(segment_sp);
1654 if (add_to_unified)
1655 unified_section_list.AddSection(segment_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001656 }
Greg Clayton3046e662013-07-10 01:23:25 +00001657 else if (unified_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001658 {
Jason Molenda20eb31b2013-08-16 03:20:42 +00001659 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr)
1660 {
1661 // Check to see if the module was read from memory?
1662 if (module_sp->GetObjectFile()->GetHeaderAddress().IsValid())
1663 {
1664 // We have a module that is in memory and needs to have its
1665 // file address adjusted. We need to do this because when we
1666 // load a file from memory, its addresses will be slid already,
1667 // yet the addresses in the new symbol file will still be unslid.
1668 // Since everything is stored as section offset, this shouldn't
1669 // cause any problems.
Jason Molenda5894a732013-08-17 03:39:52 +00001670
1671 // Make sure we've parsed the symbol table from the
1672 // ObjectFile before we go around changing its Sections.
1673 module_sp->GetObjectFile()->GetSymtab();
1674 // eh_frame would present the same problems but we parse that on
1675 // a per-function basis as-needed so it's more difficult to
1676 // remove its use of the Sections. Realistically, the environments
1677 // where this code path will be taken will not have eh_frame sections.
1678
Jason Molenda20eb31b2013-08-16 03:20:42 +00001679 unified_section_sp->SetFileAddress(load_cmd.vmaddr);
Jason Molenda05a09c62014-08-22 02:46:46 +00001680
1681 // Notify the module that the section addresses have been changed once
1682 // we're done so any file-address caches can be updated.
1683 section_file_addresses_changed = true;
Jason Molenda20eb31b2013-08-16 03:20:42 +00001684 }
1685 }
Greg Clayton3046e662013-07-10 01:23:25 +00001686 m_sections_ap->AddSection(unified_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001687 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001688
Greg Clayton3046e662013-07-10 01:23:25 +00001689 struct section_64 sect64;
1690 ::memset (&sect64, 0, sizeof(sect64));
1691 // Push a section into our mach sections for the section at
Charles Davis510938e2013-08-27 05:04:57 +00001692 // index zero (NO_SECT) if we don't have any mach sections yet...
Greg Clayton3046e662013-07-10 01:23:25 +00001693 if (m_mach_sections.empty())
1694 m_mach_sections.push_back(sect64);
1695 uint32_t segment_sect_idx;
1696 const lldb::user_id_t first_segment_sectID = sectID + 1;
1697
1698
Charles Davis510938e2013-08-27 05:04:57 +00001699 const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8;
Greg Clayton3046e662013-07-10 01:23:25 +00001700 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001701 {
Greg Clayton3046e662013-07-10 01:23:25 +00001702 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
Greg Clayton89411422010-10-08 00:21:05 +00001703 break;
Greg Clayton3046e662013-07-10 01:23:25 +00001704 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
1705 break;
1706 sect64.addr = m_data.GetAddress(&offset);
1707 sect64.size = m_data.GetAddress(&offset);
1708
1709 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
1710 break;
1711
1712 // Keep a list of mach sections around in case we need to
1713 // get at data that isn't stored in the abstracted Sections.
1714 m_mach_sections.push_back (sect64);
1715
1716 if (add_section)
1717 {
1718 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
1719 if (!const_segname)
1720 {
1721 // We have a segment with no name so we need to conjure up
1722 // segments that correspond to the section's segname if there
1723 // isn't already such a section. If there is such a section,
1724 // we resize the section so that it spans all sections.
1725 // We also mark these sections as fake so address matches don't
1726 // hit if they land in the gaps between the child sections.
1727 const_segname.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
1728 segment_sp = unified_section_list.FindSectionByName (const_segname);
1729 if (segment_sp.get())
1730 {
1731 Section *segment = segment_sp.get();
1732 // Grow the section size as needed.
1733 const lldb::addr_t sect64_min_addr = sect64.addr;
1734 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1735 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
1736 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
1737 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
1738 if (sect64_min_addr >= curr_seg_min_addr)
1739 {
1740 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
1741 // Only grow the section size if needed
1742 if (new_seg_byte_size > curr_seg_byte_size)
1743 segment->SetByteSize (new_seg_byte_size);
1744 }
1745 else
1746 {
1747 // We need to change the base address of the segment and
1748 // adjust the child section offsets for all existing children.
1749 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
1750 segment->Slide(slide_amount, false);
1751 segment->GetChildren().Slide(-slide_amount, false);
1752 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
1753 }
1754
1755 // Grow the section size as needed.
1756 if (sect64.offset)
1757 {
1758 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
1759 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
1760
1761 const lldb::addr_t section_min_file_offset = sect64.offset;
1762 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
1763 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
1764 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
1765 segment->SetFileOffset (new_file_offset);
1766 segment->SetFileSize (new_file_size);
1767 }
1768 }
1769 else
1770 {
1771 // Create a fake section for the section's named segment
1772 segment_sp.reset(new Section (segment_sp, // Parent section
1773 module_sp, // Module to which this section belongs
1774 this, // Object file to which this section belongs
1775 ++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
1776 const_segname, // Name of this section
1777 eSectionTypeContainer, // This section is a container of other sections.
1778 sect64.addr, // File VM address == addresses as they are found in the object file
1779 sect64.size, // VM size in bytes of this section
1780 sect64.offset, // Offset to the data for this section in the file
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001781 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +00001782 sect64.align,
Greg Clayton3046e662013-07-10 01:23:25 +00001783 load_cmd.flags)); // Flags for this section
1784 segment_sp->SetIsFake(true);
1785
1786 m_sections_ap->AddSection(segment_sp);
1787 if (add_to_unified)
1788 unified_section_list.AddSection(segment_sp);
1789 segment_sp->SetIsEncrypted (segment_is_encrypted);
1790 }
1791 }
1792 assert (segment_sp.get());
1793
Charles Davis510938e2013-08-27 05:04:57 +00001794 lldb::SectionType sect_type = eSectionTypeOther;
Greg Clayton3046e662013-07-10 01:23:25 +00001795
Greg Clayton38f9cc42014-06-16 22:53:16 +00001796 if (sect64.flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS))
1797 sect_type = eSectionTypeCode;
1798 else
Greg Clayton3046e662013-07-10 01:23:25 +00001799 {
Greg Clayton38f9cc42014-06-16 22:53:16 +00001800 uint32_t mach_sect_type = sect64.flags & SECTION_TYPE;
1801 static ConstString g_sect_name_objc_data ("__objc_data");
1802 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
1803 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
1804 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
1805 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
1806 static ConstString g_sect_name_objc_const ("__objc_const");
1807 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
1808 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton3046e662013-07-10 01:23:25 +00001809
Greg Clayton38f9cc42014-06-16 22:53:16 +00001810 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
1811 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
1812 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
1813 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
1814 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
1815 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
1816 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
1817 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
1818 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
1819 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
1820 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
1821 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
1822 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
1823 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
1824 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
1825 static ConstString g_sect_name_eh_frame ("__eh_frame");
Jason Molendae589e7e2014-12-08 03:09:00 +00001826 static ConstString g_sect_name_compact_unwind ("__unwind_info");
Greg Clayton38f9cc42014-06-16 22:53:16 +00001827 static ConstString g_sect_name_text ("__text");
1828 static ConstString g_sect_name_data ("__data");
Ryan Brown65d4d5c2015-09-16 21:20:44 +00001829 static ConstString g_sect_name_go_symtab ("__gosymtab");
Greg Clayton38f9cc42014-06-16 22:53:16 +00001830
Greg Clayton38f9cc42014-06-16 22:53:16 +00001831 if (section_name == g_sect_name_dwarf_debug_abbrev)
1832 sect_type = eSectionTypeDWARFDebugAbbrev;
1833 else if (section_name == g_sect_name_dwarf_debug_aranges)
1834 sect_type = eSectionTypeDWARFDebugAranges;
1835 else if (section_name == g_sect_name_dwarf_debug_frame)
1836 sect_type = eSectionTypeDWARFDebugFrame;
1837 else if (section_name == g_sect_name_dwarf_debug_info)
1838 sect_type = eSectionTypeDWARFDebugInfo;
1839 else if (section_name == g_sect_name_dwarf_debug_line)
1840 sect_type = eSectionTypeDWARFDebugLine;
1841 else if (section_name == g_sect_name_dwarf_debug_loc)
1842 sect_type = eSectionTypeDWARFDebugLoc;
1843 else if (section_name == g_sect_name_dwarf_debug_macinfo)
1844 sect_type = eSectionTypeDWARFDebugMacInfo;
1845 else if (section_name == g_sect_name_dwarf_debug_pubnames)
1846 sect_type = eSectionTypeDWARFDebugPubNames;
1847 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
1848 sect_type = eSectionTypeDWARFDebugPubTypes;
1849 else if (section_name == g_sect_name_dwarf_debug_ranges)
1850 sect_type = eSectionTypeDWARFDebugRanges;
1851 else if (section_name == g_sect_name_dwarf_debug_str)
1852 sect_type = eSectionTypeDWARFDebugStr;
1853 else if (section_name == g_sect_name_dwarf_apple_names)
1854 sect_type = eSectionTypeDWARFAppleNames;
1855 else if (section_name == g_sect_name_dwarf_apple_types)
1856 sect_type = eSectionTypeDWARFAppleTypes;
1857 else if (section_name == g_sect_name_dwarf_apple_namespaces)
1858 sect_type = eSectionTypeDWARFAppleNamespaces;
1859 else if (section_name == g_sect_name_dwarf_apple_objc)
1860 sect_type = eSectionTypeDWARFAppleObjC;
1861 else if (section_name == g_sect_name_objc_selrefs)
1862 sect_type = eSectionTypeDataCStringPointers;
1863 else if (section_name == g_sect_name_objc_msgrefs)
1864 sect_type = eSectionTypeDataObjCMessageRefs;
1865 else if (section_name == g_sect_name_eh_frame)
1866 sect_type = eSectionTypeEHFrame;
Jason Molendae589e7e2014-12-08 03:09:00 +00001867 else if (section_name == g_sect_name_compact_unwind)
1868 sect_type = eSectionTypeCompactUnwind;
Greg Clayton38f9cc42014-06-16 22:53:16 +00001869 else if (section_name == g_sect_name_cfstring)
1870 sect_type = eSectionTypeDataObjCCFStrings;
Ryan Brown65d4d5c2015-09-16 21:20:44 +00001871 else if (section_name == g_sect_name_go_symtab)
1872 sect_type = eSectionTypeGoSymtab;
Greg Clayton38f9cc42014-06-16 22:53:16 +00001873 else if (section_name == g_sect_name_objc_data ||
1874 section_name == g_sect_name_objc_classrefs ||
1875 section_name == g_sect_name_objc_superrefs ||
1876 section_name == g_sect_name_objc_const ||
1877 section_name == g_sect_name_objc_classlist)
Greg Clayton3046e662013-07-10 01:23:25 +00001878 {
Greg Clayton38f9cc42014-06-16 22:53:16 +00001879 sect_type = eSectionTypeDataPointers;
1880 }
1881
1882 if (sect_type == eSectionTypeOther)
1883 {
1884 switch (mach_sect_type)
1885 {
1886 // TODO: categorize sections by other flags for regular sections
1887 case S_REGULAR:
1888 if (section_name == g_sect_name_text)
1889 sect_type = eSectionTypeCode;
1890 else if (section_name == g_sect_name_data)
1891 sect_type = eSectionTypeData;
1892 else
1893 sect_type = eSectionTypeOther;
1894 break;
1895 case S_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1896 case S_CSTRING_LITERALS: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
1897 case S_4BYTE_LITERALS: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
1898 case S_8BYTE_LITERALS: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
1899 case S_LITERAL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
1900 case S_NON_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
1901 case S_LAZY_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
1902 case S_SYMBOL_STUBS: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1903 case S_MOD_INIT_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
1904 case S_MOD_TERM_FUNC_POINTERS: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
1905 case S_COALESCED: sect_type = eSectionTypeOther; break;
1906 case S_GB_ZEROFILL: sect_type = eSectionTypeZeroFill; break;
1907 case S_INTERPOSING: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
1908 case S_16BYTE_LITERALS: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
1909 case S_DTRACE_DOF: sect_type = eSectionTypeDebug; break;
1910 case S_LAZY_DYLIB_SYMBOL_POINTERS: sect_type = eSectionTypeDataPointers; break;
1911 default: break;
1912 }
Greg Clayton3046e662013-07-10 01:23:25 +00001913 }
1914 }
1915
1916 SectionSP section_sp(new Section (segment_sp,
1917 module_sp,
1918 this,
1919 ++sectID,
1920 section_name,
1921 sect_type,
1922 sect64.addr - segment_sp->GetFileAddress(),
1923 sect64.size,
1924 sect64.offset,
1925 sect64.offset == 0 ? 0 : sect64.size,
Greg Clayton48672af2014-06-24 22:22:43 +00001926 sect64.align,
Greg Clayton3046e662013-07-10 01:23:25 +00001927 sect64.flags));
1928 // Set the section to be encrypted to match the segment
1929
1930 bool section_is_encrypted = false;
1931 if (!segment_is_encrypted && load_cmd.filesize != 0)
1932 section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL;
1933
1934 section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted);
1935 segment_sp->GetChildren().AddSection(section_sp);
1936
1937 if (segment_sp->IsFake())
1938 {
1939 segment_sp.reset();
1940 const_segname.Clear();
1941 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001942 }
1943 }
Greg Clayton3046e662013-07-10 01:23:25 +00001944 if (segment_sp && is_dsym)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001945 {
Greg Clayton3046e662013-07-10 01:23:25 +00001946 if (first_segment_sectID <= sectID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001947 {
Greg Clayton3046e662013-07-10 01:23:25 +00001948 lldb::user_id_t sect_uid;
1949 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001950 {
Greg Clayton3046e662013-07-10 01:23:25 +00001951 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
1952 SectionSP next_section_sp;
1953 if (sect_uid + 1 <= sectID)
1954 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
1955
1956 if (curr_section_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001957 {
Greg Clayton3046e662013-07-10 01:23:25 +00001958 if (curr_section_sp->GetByteSize() == 0)
1959 {
1960 if (next_section_sp.get() != NULL)
1961 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
1962 else
1963 curr_section_sp->SetByteSize ( load_cmd.vmsize );
1964 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965 }
1966 }
1967 }
1968 }
1969 }
1970 }
1971 }
Charles Davis510938e2013-08-27 05:04:57 +00001972 else if (load_cmd.cmd == LC_DYSYMTAB)
Greg Clayton3046e662013-07-10 01:23:25 +00001973 {
1974 m_dysymtab.cmd = load_cmd.cmd;
1975 m_dysymtab.cmdsize = load_cmd.cmdsize;
1976 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
1977 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001978
Greg Clayton3046e662013-07-10 01:23:25 +00001979 offset = load_cmd_offset + load_cmd.cmdsize;
1980 }
Jason Molenda05a09c62014-08-22 02:46:46 +00001981
1982
1983 if (section_file_addresses_changed && module_sp.get())
1984 {
1985 module_sp->SectionFileAddressesChanged();
1986 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001987 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001988}
1989
1990class MachSymtabSectionInfo
1991{
1992public:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001993 MachSymtabSectionInfo (SectionList *section_list) :
1994 m_section_list (section_list),
1995 m_section_infos()
1996 {
1997 // Get the number of sections down to a depth of 1 to include
1998 // all segments and their sections, but no other sections that
1999 // may be added for debug map or
2000 m_section_infos.resize(section_list->GetNumSections(1));
2001 }
2002
Greg Claytone72dfb32012-02-24 01:59:29 +00002003 SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002004 GetSection (uint8_t n_sect, addr_t file_addr)
2005 {
2006 if (n_sect == 0)
Greg Claytone72dfb32012-02-24 01:59:29 +00002007 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002008 if (n_sect < m_section_infos.size())
2009 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002010 if (!m_section_infos[n_sect].section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002011 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002012 SectionSP section_sp (m_section_list->FindSectionByID (n_sect));
2013 m_section_infos[n_sect].section_sp = section_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002014 if (section_sp)
Greg Claytondda0d122011-07-10 17:32:33 +00002015 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002016 m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress());
2017 m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize());
Greg Claytondda0d122011-07-10 17:32:33 +00002018 }
2019 else
2020 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002021 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Claytondda0d122011-07-10 17:32:33 +00002022 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002023 }
2024 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +00002025 {
2026 // Symbol is in section.
Greg Claytone72dfb32012-02-24 01:59:29 +00002027 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00002028 }
2029 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
2030 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
2031 {
2032 // Symbol is in section with zero size, but has the same start
2033 // address as the section. This can happen with linker symbols
2034 // (symbols that start with the letter 'l' or 'L'.
Greg Claytone72dfb32012-02-24 01:59:29 +00002035 return m_section_infos[n_sect].section_sp;
Greg Clayton8f258512011-08-26 20:01:35 +00002036 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002037 }
Greg Claytone72dfb32012-02-24 01:59:29 +00002038 return m_section_list->FindSectionContainingFileAddress(file_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002039 }
2040
2041protected:
2042 struct SectionInfo
2043 {
2044 SectionInfo () :
2045 vm_range(),
Greg Claytone72dfb32012-02-24 01:59:29 +00002046 section_sp ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002047 {
2048 }
2049
2050 VMRange vm_range;
Greg Claytone72dfb32012-02-24 01:59:29 +00002051 SectionSP section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002052 };
2053 SectionList *m_section_list;
2054 std::vector<SectionInfo> m_section_infos;
2055};
2056
Greg Clayton9191db42013-10-21 18:40:51 +00002057struct TrieEntry
2058{
2059 TrieEntry () :
2060 name(),
2061 address(LLDB_INVALID_ADDRESS),
2062 flags (0),
2063 other(0),
2064 import_name()
2065 {
2066 }
2067
2068 void
2069 Clear ()
2070 {
2071 name.Clear();
2072 address = LLDB_INVALID_ADDRESS;
2073 flags = 0;
2074 other = 0;
2075 import_name.Clear();
2076 }
2077
2078 void
2079 Dump () const
2080 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002081 printf ("0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"",
2082 static_cast<unsigned long long>(address),
2083 static_cast<unsigned long long>(flags),
2084 static_cast<unsigned long long>(other), name.GetCString());
Greg Clayton9191db42013-10-21 18:40:51 +00002085 if (import_name)
2086 printf (" -> \"%s\"\n", import_name.GetCString());
2087 else
2088 printf ("\n");
2089 }
2090 ConstString name;
2091 uint64_t address;
2092 uint64_t flags;
2093 uint64_t other;
2094 ConstString import_name;
2095};
2096
2097struct TrieEntryWithOffset
2098{
2099 lldb::offset_t nodeOffset;
2100 TrieEntry entry;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002101
Greg Clayton9191db42013-10-21 18:40:51 +00002102 TrieEntryWithOffset (lldb::offset_t offset) :
2103 nodeOffset (offset),
2104 entry()
2105 {
2106 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002107
Greg Clayton9191db42013-10-21 18:40:51 +00002108 void
2109 Dump (uint32_t idx) const
2110 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002111 printf ("[%3u] 0x%16.16llx: ", idx,
2112 static_cast<unsigned long long>(nodeOffset));
Greg Clayton9191db42013-10-21 18:40:51 +00002113 entry.Dump();
2114 }
2115
2116 bool
2117 operator<(const TrieEntryWithOffset& other) const
2118 {
2119 return ( nodeOffset < other.nodeOffset );
2120 }
2121};
2122
Greg Clayton8f265f72015-10-28 20:49:34 +00002123static bool
Greg Clayton9191db42013-10-21 18:40:51 +00002124ParseTrieEntries (DataExtractor &data,
2125 lldb::offset_t offset,
Greg Claytonb887da12015-07-16 19:50:57 +00002126 const bool is_arm,
Greg Clayton9191db42013-10-21 18:40:51 +00002127 std::vector<llvm::StringRef> &nameSlices,
2128 std::set<lldb::addr_t> &resolver_addresses,
2129 std::vector<TrieEntryWithOffset>& output)
2130{
2131 if (!data.ValidOffset(offset))
Greg Clayton8f265f72015-10-28 20:49:34 +00002132 return true;
Greg Clayton9191db42013-10-21 18:40:51 +00002133
2134 const uint64_t terminalSize = data.GetULEB128(&offset);
2135 lldb::offset_t children_offset = offset + terminalSize;
2136 if ( terminalSize != 0 ) {
2137 TrieEntryWithOffset e (offset);
2138 e.entry.flags = data.GetULEB128(&offset);
2139 const char *import_name = NULL;
2140 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
2141 e.entry.address = 0;
2142 e.entry.other = data.GetULEB128(&offset); // dylib ordinal
2143 import_name = data.GetCStr(&offset);
2144 }
2145 else {
2146 e.entry.address = data.GetULEB128(&offset);
2147 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
2148 {
Greg Clayton9191db42013-10-21 18:40:51 +00002149 e.entry.other = data.GetULEB128(&offset);
Greg Claytonb887da12015-07-16 19:50:57 +00002150 uint64_t resolver_addr = e.entry.other;
2151 if (is_arm)
2152 resolver_addr &= THUMB_ADDRESS_BIT_MASK;
2153 resolver_addresses.insert(resolver_addr);
Greg Clayton9191db42013-10-21 18:40:51 +00002154 }
2155 else
2156 e.entry.other = 0;
2157 }
2158 // Only add symbols that are reexport symbols with a valid import name
2159 if (EXPORT_SYMBOL_FLAGS_REEXPORT & e.entry.flags && import_name && import_name[0])
2160 {
2161 std::string name;
2162 if (!nameSlices.empty())
2163 {
2164 for (auto name_slice: nameSlices)
2165 name.append(name_slice.data(), name_slice.size());
2166 }
2167 if (name.size() > 1)
2168 {
2169 // Skip the leading '_'
2170 e.entry.name.SetCStringWithLength(name.c_str() + 1,name.size() - 1);
2171 }
2172 if (import_name)
2173 {
2174 // Skip the leading '_'
2175 e.entry.import_name.SetCString(import_name+1);
2176 }
2177 output.push_back(e);
2178 }
2179 }
2180
2181 const uint8_t childrenCount = data.GetU8(&children_offset);
2182 for (uint8_t i=0; i < childrenCount; ++i) {
Greg Clayton8f265f72015-10-28 20:49:34 +00002183 const char *cstr = data.GetCStr(&children_offset);
2184 if (cstr)
2185 nameSlices.push_back(llvm::StringRef(cstr));
2186 else
2187 return false; // Corrupt data
Greg Clayton9191db42013-10-21 18:40:51 +00002188 lldb::offset_t childNodeOffset = data.GetULEB128(&children_offset);
2189 if (childNodeOffset)
2190 {
Greg Clayton8f265f72015-10-28 20:49:34 +00002191 if (!ParseTrieEntries(data,
2192 childNodeOffset,
2193 is_arm,
2194 nameSlices,
2195 resolver_addresses,
2196 output))
2197 {
2198 return false;
2199 }
Greg Clayton9191db42013-10-21 18:40:51 +00002200 }
2201 nameSlices.pop_back();
2202 }
Greg Clayton8f265f72015-10-28 20:49:34 +00002203 return true;
Greg Clayton9191db42013-10-21 18:40:51 +00002204}
2205
Jason Molenda649a6072015-11-10 05:21:54 +00002206// Read the UUID out of a dyld_shared_cache file on-disk.
2207UUID
2208ObjectFileMachO::GetSharedCacheUUID (FileSpec dyld_shared_cache, const ByteOrder byte_order, const uint32_t addr_byte_size)
2209{
2210 UUID dsc_uuid;
2211 DataBufferSP dsc_data_sp = dyld_shared_cache.MemoryMapFileContentsIfLocal(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
2212 if (dsc_data_sp)
2213 {
2214 DataExtractor dsc_header_data (dsc_data_sp, byte_order, addr_byte_size);
2215
2216 char version_str[7];
2217 lldb::offset_t offset = 0;
2218 memcpy (version_str, dsc_header_data.GetData (&offset, 6), 6);
2219 version_str[6] = '\0';
2220 if (strcmp (version_str, "dyld_v") == 0)
2221 {
2222 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid);
2223 uint8_t uuid_bytes[sizeof (uuid_t)];
2224 memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t));
2225 dsc_uuid.SetBytes (uuid_bytes);
2226 }
2227 }
2228 return dsc_uuid;
2229}
2230
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002231size_t
Greg Clayton3046e662013-07-10 01:23:25 +00002232ObjectFileMachO::ParseSymtab ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002233{
2234 Timer scoped_timer(__PRETTY_FUNCTION__,
2235 "ObjectFileMachO::ParseSymtab () module = %s",
2236 m_file.GetFilename().AsCString(""));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002237 ModuleSP module_sp (GetModule());
2238 if (!module_sp)
2239 return 0;
2240
2241 struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 };
2242 struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 };
Greg Clayton9191db42013-10-21 18:40:51 +00002243 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 +00002244 typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
2245 FunctionStarts function_starts;
Greg Claytonc7bece562013-01-25 18:06:21 +00002246 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002247 uint32_t i;
Greg Clayton9191db42013-10-21 18:40:51 +00002248 FileSpecList dylib_files;
Greg Clayton5160ce52013-03-27 23:08:40 +00002249 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
Greg Clayton1e28adf2015-02-25 17:25:02 +00002250 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
2251 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
2252 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
Greg Clayton77ccca72011-12-30 00:32:24 +00002253
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002254 for (i=0; i<m_header.ncmds; ++i)
2255 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002256 const lldb::offset_t cmd_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002257 // Read in the load command and load command size
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002258 struct load_command lc;
2259 if (m_data.GetU32(&offset, &lc, 2) == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002260 break;
2261 // Watch for the symbol table load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002262 switch (lc.cmd)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002263 {
Charles Davis510938e2013-08-27 05:04:57 +00002264 case LC_SYMTAB:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002265 symtab_load_command.cmd = lc.cmd;
2266 symtab_load_command.cmdsize = lc.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002267 // Read in the rest of the symtab load command
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002268 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields
2269 return 0;
2270 if (symtab_load_command.symoff == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002271 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002272 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002273 module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002274 return 0;
2275 }
2276
2277 if (symtab_load_command.stroff == 0)
2278 {
2279 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002280 module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002281 return 0;
2282 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002283
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002284 if (symtab_load_command.nsyms == 0)
2285 {
2286 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002287 module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002288 return 0;
2289 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002290
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002291 if (symtab_load_command.strsize == 0)
2292 {
2293 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002294 module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0");
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002295 return 0;
2296 }
2297 break;
2298
Greg Clayton9191db42013-10-21 18:40:51 +00002299 case LC_DYLD_INFO:
2300 case LC_DYLD_INFO_ONLY:
2301 if (m_data.GetU32(&offset, &dyld_info.rebase_off, 10))
2302 {
2303 dyld_info.cmd = lc.cmd;
2304 dyld_info.cmdsize = lc.cmdsize;
2305 }
2306 else
2307 {
2308 memset (&dyld_info, 0, sizeof(dyld_info));
2309 }
2310 break;
2311
2312 case LC_LOAD_DYLIB:
2313 case LC_LOAD_WEAK_DYLIB:
2314 case LC_REEXPORT_DYLIB:
2315 case LC_LOADFVMLIB:
2316 case LC_LOAD_UPWARD_DYLIB:
2317 {
2318 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
2319 const char *path = m_data.PeekCStr(name_offset);
2320 if (path)
2321 {
2322 FileSpec file_spec(path, false);
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002323 // Strip the path if there is @rpath, @executable, etc so we just use the basename
Greg Clayton9191db42013-10-21 18:40:51 +00002324 if (path[0] == '@')
2325 file_spec.GetDirectory().Clear();
Jim Inghamfbe0b9a2014-05-21 03:58:03 +00002326
2327 if (lc.cmd == LC_REEXPORT_DYLIB)
2328 {
2329 m_reexported_dylibs.AppendIfUnique(file_spec);
2330 }
Greg Clayton9191db42013-10-21 18:40:51 +00002331
2332 dylib_files.Append(file_spec);
2333 }
2334 }
2335 break;
2336
Charles Davis510938e2013-08-27 05:04:57 +00002337 case LC_FUNCTION_STARTS:
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002338 function_starts_load_command.cmd = lc.cmd;
2339 function_starts_load_command.cmdsize = lc.cmdsize;
2340 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 +00002341 memset (&function_starts_load_command, 0, sizeof(function_starts_load_command));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002342 break;
2343
2344 default:
2345 break;
2346 }
2347 offset = cmd_offset + lc.cmdsize;
2348 }
2349
2350 if (symtab_load_command.cmd)
2351 {
2352 Symtab *symtab = m_symtab_ap.get();
2353 SectionList *section_list = GetSectionList();
2354 if (section_list == NULL)
2355 return 0;
2356
Greg Claytonc7bece562013-01-25 18:06:21 +00002357 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
2358 const ByteOrder byte_order = m_data.GetByteOrder();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002359 bool bit_width_32 = addr_byte_size == 4;
2360 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
2361
Greg Claytonc7bece562013-01-25 18:06:21 +00002362 DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size);
2363 DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size);
2364 DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size);
Jason Molendad34e6522013-02-05 22:31:24 +00002365 DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00002366 DataExtractor dyld_trie_data (NULL, 0, byte_order, addr_byte_size);
Jason Molenda4e7511e2013-03-06 23:19:17 +00002367
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002368 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
2369 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
Greg Clayton4c82d422012-05-18 23:20:01 +00002370 addr_t strtab_addr = LLDB_INVALID_ADDRESS;
Greg Claytonfd814c52013-08-13 01:42:25 +00002371
2372 ProcessSP process_sp (m_process_wp.lock());
2373 Process *process = process_sp.get();
2374
Greg Clayton86eac942013-08-13 21:32:34 +00002375 uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete;
2376
Greg Clayton48672af2014-06-24 22:22:43 +00002377 if (process && m_header.filetype != llvm::MachO::MH_OBJECT)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002378 {
Greg Clayton4c82d422012-05-18 23:20:01 +00002379 Target &target = process->GetTarget();
Greg Claytonfd814c52013-08-13 01:42:25 +00002380
Greg Clayton86eac942013-08-13 21:32:34 +00002381 memory_module_load_level = target.GetMemoryModuleLoadLevel();
Greg Claytonfd814c52013-08-13 01:42:25 +00002382
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002383 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2384 // Reading mach file from memory in a process or core file...
2385
2386 if (linkedit_section_sp)
2387 {
Greg Clayton07347372015-06-08 21:53:11 +00002388 addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
2389 if (linkedit_load_addr == LLDB_INVALID_ADDRESS)
2390 {
2391 // We might be trying to access the symbol table before the __LINKEDIT's load
2392 // address has been set in the target. We can't fail to read the symbol table,
2393 // so calculate the right address manually
2394 linkedit_load_addr = CalculateSectionLoadAddressForMemoryImage(m_memory_addr, GetMachHeaderSection(), linkedit_section_sp.get());
2395 }
2396
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002397 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2398 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
Greg Clayton4c82d422012-05-18 23:20:01 +00002399 strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
Greg Clayton26b47e22012-04-18 05:19:20 +00002400
2401 bool data_was_read = false;
2402
Todd Fiala013434e2014-07-09 01:29:05 +00002403#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molendaa3329782014-03-29 18:54:20 +00002404 if (m_header.flags & 0x80000000u && process->GetAddressByteSize() == sizeof (void*))
Greg Clayton77ccca72011-12-30 00:32:24 +00002405 {
Greg Clayton26b47e22012-04-18 05:19:20 +00002406 // This mach-o memory file is in the dyld shared cache. If this
2407 // program is not remote and this is iOS, then this process will
2408 // share the same shared cache as the process we are debugging and
2409 // we can read the entire __LINKEDIT from the address space in this
2410 // process. This is a needed optimization that is used for local iOS
2411 // debugging only since all shared libraries in the shared cache do
2412 // not have corresponding files that exist in the file system of the
2413 // device. They have been combined into a single file. This means we
2414 // always have to load these files from memory. All of the symbol and
2415 // string tables from all of the __LINKEDIT sections from the shared
2416 // libraries in the shared cache have been merged into a single large
2417 // symbol and string table. Reading all of this symbol and string table
2418 // data across can slow down debug launch times, so we optimize this by
2419 // reading the memory for the __LINKEDIT section from this process.
Jason Molenda0e0954c2013-04-16 06:24:42 +00002420
2421 UUID lldb_shared_cache(GetLLDBSharedCacheUUID());
2422 UUID process_shared_cache(GetProcessSharedCacheUUID(process));
2423 bool use_lldb_cache = true;
2424 if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache)
2425 {
2426 use_lldb_cache = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002427 ModuleSP module_sp (GetModule());
2428 if (module_sp)
2429 module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow.");
2430
Jason Molenda0e0954c2013-04-16 06:24:42 +00002431 }
2432
Greg Clayton26b47e22012-04-18 05:19:20 +00002433 PlatformSP platform_sp (target.GetPlatform());
Jason Molenda0e0954c2013-04-16 06:24:42 +00002434 if (platform_sp && platform_sp->IsHost() && use_lldb_cache)
Greg Clayton26b47e22012-04-18 05:19:20 +00002435 {
2436 data_was_read = true;
2437 nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle);
Greg Clayton4c82d422012-05-18 23:20:01 +00002438 strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle);
Greg Clayton26b47e22012-04-18 05:19:20 +00002439 if (function_starts_load_command.cmd)
2440 {
2441 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2442 function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle);
2443 }
2444 }
2445 }
2446#endif
2447
2448 if (!data_was_read)
2449 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002450 if (memory_module_load_level == eMemoryModuleLoadLevelComplete)
Jason Molendad34e6522013-02-05 22:31:24 +00002451 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002452 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
2453 if (nlist_data_sp)
2454 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2455 // Load strings individually from memory when loading from memory since shared cache
2456 // string tables contain strings for all symbols from all shared cached libraries
2457 //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size));
2458 //if (strtab_data_sp)
2459 // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
2460 if (m_dysymtab.nindirectsyms != 0)
2461 {
2462 const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset;
2463 DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4));
2464 if (indirect_syms_data_sp)
2465 indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
2466 }
Jason Molendad34e6522013-02-05 22:31:24 +00002467 }
Greg Claytonfd814c52013-08-13 01:42:25 +00002468
2469 if (memory_module_load_level >= eMemoryModuleLoadLevelPartial)
Greg Clayton26b47e22012-04-18 05:19:20 +00002470 {
Greg Claytonfd814c52013-08-13 01:42:25 +00002471 if (function_starts_load_command.cmd)
2472 {
2473 const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset;
2474 DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize));
2475 if (func_start_data_sp)
2476 function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize());
2477 }
Greg Clayton26b47e22012-04-18 05:19:20 +00002478 }
Greg Clayton77ccca72011-12-30 00:32:24 +00002479 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002480 }
2481 }
2482 else
2483 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002484 nlist_data.SetData (m_data,
2485 symtab_load_command.symoff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002486 nlist_data_byte_size);
2487 strtab_data.SetData (m_data,
Jason Molenda4e7511e2013-03-06 23:19:17 +00002488 symtab_load_command.stroff,
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002489 strtab_data_byte_size);
Greg Clayton9191db42013-10-21 18:40:51 +00002490
2491 if (dyld_info.export_size > 0)
2492 {
2493 dyld_trie_data.SetData (m_data,
2494 dyld_info.export_off,
2495 dyld_info.export_size);
2496 }
2497
Jason Molendad34e6522013-02-05 22:31:24 +00002498 if (m_dysymtab.nindirectsyms != 0)
2499 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00002500 indirect_symbol_index_data.SetData (m_data,
2501 m_dysymtab.indirectsymoff,
Jason Molendad34e6522013-02-05 22:31:24 +00002502 m_dysymtab.nindirectsyms * 4);
2503 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002504 if (function_starts_load_command.cmd)
2505 {
2506 function_starts_data.SetData (m_data,
2507 function_starts_load_command.dataoff,
2508 function_starts_load_command.datasize);
2509 }
2510 }
Greg Clayton77ccca72011-12-30 00:32:24 +00002511
Greg Clayton86eac942013-08-13 21:32:34 +00002512 if (nlist_data.GetByteSize() == 0 && memory_module_load_level == eMemoryModuleLoadLevelComplete)
2513 {
2514 if (log)
2515 module_sp->LogMessage(log, "failed to read nlist data");
2516 return 0;
2517 }
2518
Greg Claytondebb8812012-05-25 17:04:00 +00002519 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2520 if (!have_strtab_data)
Greg Clayton4c82d422012-05-18 23:20:01 +00002521 {
Greg Claytondebb8812012-05-25 17:04:00 +00002522 if (process)
2523 {
2524 if (strtab_addr == LLDB_INVALID_ADDRESS)
2525 {
2526 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002527 module_sp->LogMessage(log, "failed to locate the strtab in memory");
Greg Claytondebb8812012-05-25 17:04:00 +00002528 return 0;
2529 }
2530 }
2531 else
Greg Clayton4c82d422012-05-18 23:20:01 +00002532 {
2533 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002534 module_sp->LogMessage(log, "failed to read strtab data");
Greg Clayton4c82d422012-05-18 23:20:01 +00002535 return 0;
2536 }
2537 }
Greg Clayton4c82d422012-05-18 23:20:01 +00002538
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002539 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
2540 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
Greg Claytona381e102015-07-27 23:21:05 +00002541 const ConstString &g_segment_name_DATA_DIRTY = GetSegmentNameDATA_DIRTY();
2542 const ConstString &g_segment_name_DATA_CONST = GetSegmentNameDATA_CONST();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002543 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
2544 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
2545 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
2546 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
Greg Claytona381e102015-07-27 23:21:05 +00002547 SectionSP data_dirty_section_sp(section_list->FindSectionByName(g_segment_name_DATA_DIRTY));
2548 SectionSP data_const_section_sp(section_list->FindSectionByName(g_segment_name_DATA_CONST));
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002549 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
2550 SectionSP eh_frame_section_sp;
2551 if (text_section_sp.get())
2552 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
2553 else
2554 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
2555
Charles Davis510938e2013-08-27 05:04:57 +00002556 const bool is_arm = (m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
Jason Molenda5635f772013-03-21 03:36:01 +00002557
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002558 // lldb works best if it knows the start address of all functions in a module.
Jason Molenda5635f772013-03-21 03:36:01 +00002559 // Linker symbols or debug info are normally the best source of information for start addr / size but
2560 // they may be stripped in a released binary.
Jason Molendad63d3c72013-04-16 00:18:44 +00002561 // Two additional sources of information exist in Mach-O binaries:
Jason Molenda5635f772013-03-21 03:36:01 +00002562 // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the
2563 // binary, relative to the text section.
2564 // eh_frame - the eh_frame FDEs have the start addr & size of each function
2565 // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries.
2566 // Binaries built to run on older releases may need to use eh_frame information.
2567
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002568 if (text_section_sp && function_starts_data.GetByteSize())
2569 {
2570 FunctionStarts::Entry function_start_entry;
2571 function_start_entry.data = false;
Greg Claytonc7bece562013-01-25 18:06:21 +00002572 lldb::offset_t function_start_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002573 function_start_entry.addr = text_section_sp->GetFileAddress();
2574 uint64_t delta;
2575 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0)
2576 {
2577 // Now append the current entry
2578 function_start_entry.addr += delta;
2579 function_starts.Append(function_start_entry);
2580 }
Jason Molendad63d3c72013-04-16 00:18:44 +00002581 }
Jason Molenda5635f772013-03-21 03:36:01 +00002582 else
2583 {
Jason Molenda584ce2f2013-03-22 00:38:45 +00002584 // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame
2585 // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any
2586 // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in
2587 // the module.
2588 if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo)
Jason Molenda5635f772013-03-21 03:36:01 +00002589 {
Jason Molendaa18f7072015-08-15 01:21:01 +00002590 DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindEHFrame, true);
Jason Molenda5635f772013-03-21 03:36:01 +00002591 DWARFCallFrameInfo::FunctionAddressAndSizeVector functions;
2592 eh_frame.GetFunctionAddressAndSizeVector (functions);
2593 addr_t text_base_addr = text_section_sp->GetFileAddress();
2594 size_t count = functions.GetSize();
2595 for (size_t i = 0; i < count; ++i)
2596 {
2597 const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i);
2598 if (func)
2599 {
2600 FunctionStarts::Entry function_start_entry;
2601 function_start_entry.addr = func->base - text_base_addr;
2602 function_starts.Append(function_start_entry);
2603 }
2604 }
2605 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002606 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00002607
Greg Claytonc7bece562013-01-25 18:06:21 +00002608 const size_t function_starts_count = function_starts.GetSize();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002609
Jason Molenda0bc89942016-05-26 04:22:47 +00002610 // For user process binaries (executables, dylibs, frameworks, bundles), if we don't have
2611 // LC_FUNCTION_STARTS/eh_frame section in this binary, we're going to assume the binary
2612 // has been stripped. Don't allow assembly language instruction emulation because we don't
2613 // know proper function start boundaries.
2614 //
2615 // For all other types of binaries (kernels, stand-alone bare board binaries, kexts), they
2616 // may not have LC_FUNCTION_STARTS / eh_frame sections - we should not make any assumptions
2617 // about them based on that.
2618 if (function_starts_count == 0 && CalculateStrata() == eStrataUser)
Jason Molenda955dcf22016-05-04 03:09:40 +00002619 {
Jason Molenda955dcf22016-05-04 03:09:40 +00002620 m_allow_assembly_emulation_unwind_plans = false;
2621 Log *unwind_or_symbol_log (lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_SYMBOLS | LIBLLDB_LOG_UNWIND));
2622
2623 if (unwind_or_symbol_log)
2624 module_sp->LogMessage(unwind_or_symbol_log, "no LC_FUNCTION_STARTS, will not allow assembly profiled unwinds");
2625 }
2626
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +00002627 const user_id_t TEXT_eh_frame_sectID =
2628 eh_frame_section_sp.get() ? eh_frame_section_sp->GetID()
2629 : static_cast<user_id_t>(NO_SECT);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002630
Greg Claytonc7bece562013-01-25 18:06:21 +00002631 lldb::offset_t nlist_data_offset = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002632
2633 uint32_t N_SO_index = UINT32_MAX;
2634
2635 MachSymtabSectionInfo section_info (section_list);
2636 std::vector<uint32_t> N_FUN_indexes;
2637 std::vector<uint32_t> N_NSYM_indexes;
2638 std::vector<uint32_t> N_INCL_indexes;
2639 std::vector<uint32_t> N_BRAC_indexes;
2640 std::vector<uint32_t> N_COMM_indexes;
Greg Claytond81088c2014-01-16 01:38:29 +00002641 typedef std::multimap <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002642 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Claytondacc4a92013-05-14 22:19:37 +00002643 typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002644 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2645 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytondacc4a92013-05-14 22:19:37 +00002646 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002647 // Any symbols that get merged into another will get an entry
2648 // in this map so we know
2649 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2650 uint32_t nlist_idx = 0;
2651 Symbol *symbol_ptr = NULL;
2652
2653 uint32_t sym_idx = 0;
Jason Molendaa5609c82012-06-21 01:51:02 +00002654 Symbol *sym = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +00002655 size_t num_syms = 0;
Greg Clayton4c82d422012-05-18 23:20:01 +00002656 std::string memory_symbol_name;
Jason Molendaa5609c82012-06-21 01:51:02 +00002657 uint32_t unmapped_local_symbols_found = 0;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00002658
Jim Inghamea3ac272014-01-10 22:55:37 +00002659 std::vector<TrieEntryWithOffset> trie_entries;
2660 std::set<lldb::addr_t> resolver_addresses;
2661
2662 if (dyld_trie_data.GetByteSize() > 0)
2663 {
2664 std::vector<llvm::StringRef> nameSlices;
2665 ParseTrieEntries (dyld_trie_data,
2666 0,
Greg Claytonb887da12015-07-16 19:50:57 +00002667 is_arm,
Jim Inghamea3ac272014-01-10 22:55:37 +00002668 nameSlices,
2669 resolver_addresses,
2670 trie_entries);
2671
2672 ConstString text_segment_name ("__TEXT");
2673 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
2674 if (text_segment_sp)
2675 {
2676 const lldb::addr_t text_segment_file_addr = text_segment_sp->GetFileAddress();
2677 if (text_segment_file_addr != LLDB_INVALID_ADDRESS)
2678 {
2679 for (auto &e : trie_entries)
2680 e.entry.address += text_segment_file_addr;
2681 }
2682 }
2683 }
2684
Greg Claytonb65c6292015-02-20 22:20:05 +00002685 typedef std::set<ConstString> IndirectSymbols;
2686 IndirectSymbols indirect_symbol_names;
2687
Todd Fiala013434e2014-07-09 01:29:05 +00002688#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molendaa5609c82012-06-21 01:51:02 +00002689
2690 // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL
2691 // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained,
2692 // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some*
2693 // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt
2694 // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal
2695 // nlist parser to ignore all LOCAL symbols.
2696
2697 if (m_header.flags & 0x80000000u)
2698 {
2699 // Before we can start mapping the DSC, we need to make certain the target process is actually
2700 // using the cache we can find.
2701
Jason Molendaa5609c82012-06-21 01:51:02 +00002702 // Next we need to determine the correct path for the dyld shared cache.
2703
Greg Clayton7ab7f892014-05-29 21:33:45 +00002704 ArchSpec header_arch;
2705 GetArchitecture(header_arch);
Jason Molendaa5609c82012-06-21 01:51:02 +00002706 char dsc_path[PATH_MAX];
Jason Molenda649a6072015-11-10 05:21:54 +00002707 char dsc_path_development[PATH_MAX];
Jason Molendaa5609c82012-06-21 01:51:02 +00002708
2709 snprintf(dsc_path, sizeof(dsc_path), "%s%s%s",
Jason Molenda4e7511e2013-03-06 23:19:17 +00002710 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
2711 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
Jason Molendaa5609c82012-06-21 01:51:02 +00002712 header_arch.GetArchitectureName());
2713
Jason Molenda649a6072015-11-10 05:21:54 +00002714 snprintf(dsc_path_development, sizeof(dsc_path), "%s%s%s%s",
2715 "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */
2716 "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */
2717 header_arch.GetArchitectureName(),
2718 ".development");
Jason Molendaa5609c82012-06-21 01:51:02 +00002719
Jason Molenda649a6072015-11-10 05:21:54 +00002720 FileSpec dsc_nondevelopment_filespec(dsc_path, false);
2721 FileSpec dsc_development_filespec(dsc_path_development, false);
2722 FileSpec dsc_filespec;
Eugene Zelenko8157a882015-10-23 16:56:07 +00002723
Jason Molenda649a6072015-11-10 05:21:54 +00002724 UUID dsc_uuid;
2725 UUID process_shared_cache_uuid;
Jason Molenda255f9bb2013-03-06 23:17:36 +00002726
Jason Molenda649a6072015-11-10 05:21:54 +00002727 if (process)
Jason Molendad63d3c72013-04-16 00:18:44 +00002728 {
Jason Molenda649a6072015-11-10 05:21:54 +00002729 process_shared_cache_uuid = GetProcessSharedCacheUUID(process);
2730 }
Jason Molenda255f9bb2013-03-06 23:17:36 +00002731
Jason Molenda649a6072015-11-10 05:21:54 +00002732 // First see if we can find an exact match for the inferior process shared cache UUID in
2733 // the development or non-development shared caches on disk.
2734 if (process_shared_cache_uuid.IsValid())
Greg Clayton946f8902012-09-05 22:30:51 +00002735 {
Jason Molenda649a6072015-11-10 05:21:54 +00002736 if (dsc_development_filespec.Exists())
2737 {
2738 UUID dsc_development_uuid = GetSharedCacheUUID (dsc_development_filespec, byte_order, addr_byte_size);
2739 if (dsc_development_uuid.IsValid() && dsc_development_uuid == process_shared_cache_uuid)
2740 {
2741 dsc_filespec = dsc_development_filespec;
2742 dsc_uuid = dsc_development_uuid;
2743 }
2744 }
2745 if (!dsc_uuid.IsValid() && dsc_nondevelopment_filespec.Exists())
2746 {
2747 UUID dsc_nondevelopment_uuid = GetSharedCacheUUID (dsc_nondevelopment_filespec, byte_order, addr_byte_size);
2748 if (dsc_nondevelopment_uuid.IsValid() && dsc_nondevelopment_uuid == process_shared_cache_uuid)
2749 {
2750 dsc_filespec = dsc_nondevelopment_filespec;
2751 dsc_uuid = dsc_nondevelopment_uuid;
2752 }
2753 }
2754 }
Eugene Zelenko8157a882015-10-23 16:56:07 +00002755
Jason Molenda649a6072015-11-10 05:21:54 +00002756 // Failing a UUID match, prefer the development dyld_shared cache if both are present.
2757 if (!dsc_filespec.Exists())
Greg Clayton946f8902012-09-05 22:30:51 +00002758 {
Jason Molenda649a6072015-11-10 05:21:54 +00002759 if (dsc_development_filespec.Exists())
2760 {
2761 dsc_filespec = dsc_development_filespec;
2762 }
2763 else
2764 {
2765 dsc_filespec = dsc_nondevelopment_filespec;
2766 }
2767 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002768
Jason Molendaf8130862012-06-22 03:28:35 +00002769 /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset).
2770 The dyld_cache_local_symbols_info structure gives us three things:
2771 1. The start and count of the nlist records in the dyld_shared_cache file
2772 2. The start and size of the strings for these nlist records
2773 3. The start and count of dyld_cache_local_symbols_entry entries
2774
2775 There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache.
2776 The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache.
Jason Molenda4e7511e2013-03-06 23:19:17 +00002777 The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records
Jason Molendaf8130862012-06-22 03:28:35 +00002778 and the count of how many nlist records there are for this dylib/framework.
2779 */
2780
Jason Molenda649a6072015-11-10 05:21:54 +00002781 // Process the dyld shared cache header to find the unmapped symbols
Jason Molendaa5609c82012-06-21 01:51:02 +00002782
Jason Molenda649a6072015-11-10 05:21:54 +00002783 DataBufferSP dsc_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(0, sizeof(struct lldb_copy_dyld_cache_header_v1));
2784 if (!dsc_uuid.IsValid())
2785 {
2786 dsc_uuid = GetSharedCacheUUID (dsc_filespec, byte_order, addr_byte_size);
2787 }
Jason Molenda255f9bb2013-03-06 23:17:36 +00002788 if (dsc_data_sp)
Jason Molendaa5609c82012-06-21 01:51:02 +00002789 {
Jason Molenda649a6072015-11-10 05:21:54 +00002790 DataExtractor dsc_header_data (dsc_data_sp, byte_order, addr_byte_size);
Jason Molenda0e0954c2013-04-16 06:24:42 +00002791
2792 bool uuid_match = true;
2793 if (dsc_uuid.IsValid() && process)
2794 {
Jason Molenda649a6072015-11-10 05:21:54 +00002795 if (process_shared_cache_uuid.IsValid() && dsc_uuid != process_shared_cache_uuid)
Jason Molenda0e0954c2013-04-16 06:24:42 +00002796 {
2797 // The on-disk dyld_shared_cache file is not the same as the one in this
2798 // process' memory, don't use it.
2799 uuid_match = false;
Jason Molendac9cb7d22013-04-16 21:42:58 +00002800 ModuleSP module_sp (GetModule());
2801 if (module_sp)
2802 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 +00002803 }
2804 }
2805
Jason Molenda4e7511e2013-03-06 23:19:17 +00002806 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset);
Jason Molenda255f9bb2013-03-06 23:17:36 +00002807
Jason Molendaa5609c82012-06-21 01:51:02 +00002808 uint32_t mappingOffset = dsc_header_data.GetU32(&offset);
2809
2810 // If the mappingOffset points to a location inside the header, we've
2811 // opened an old dyld shared cache, and should not proceed further.
Jason Molenda649a6072015-11-10 05:21:54 +00002812 if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v1))
Jason Molendaa5609c82012-06-21 01:51:02 +00002813 {
2814
Greg Clayton736888c2015-02-23 23:47:09 +00002815 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 +00002816 DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size);
2817 offset = 0;
2818
2819 // The File addresses (from the in-memory Mach-O load commands) for the shared libraries
2820 // in the shared library cache need to be adjusted by an offset to match up with the
2821 // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is
2822 // recorded in mapping_offset_value.
2823 const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset);
2824
2825 offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset);
Jason Molendaa5609c82012-06-21 01:51:02 +00002826 uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset);
2827 uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset);
2828
Jason Molenda4e7511e2013-03-06 23:19:17 +00002829 if (localSymbolsOffset && localSymbolsSize)
Jason Molendaa5609c82012-06-21 01:51:02 +00002830 {
2831 // Map the local symbols
Greg Clayton736888c2015-02-23 23:47:09 +00002832 if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContentsIfLocal(localSymbolsOffset, localSymbolsSize))
Jason Molendaa5609c82012-06-21 01:51:02 +00002833 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002834 DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size);
Jason Molendaa5609c82012-06-21 01:51:02 +00002835
2836 offset = 0;
2837
Greg Claytonb65c6292015-02-20 22:20:05 +00002838 typedef std::map<ConstString, uint16_t> UndefinedNameToDescMap;
2839 typedef std::map<uint32_t, ConstString> SymbolIndexToName;
2840 UndefinedNameToDescMap undefined_name_to_desc;
2841 SymbolIndexToName reexport_shlib_needs_fixup;
2842
2843
Jason Molendaa5609c82012-06-21 01:51:02 +00002844 // Read the local_symbols_infos struct in one shot
2845 struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info;
2846 dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6);
2847
Jason Molendaa5609c82012-06-21 01:51:02 +00002848 SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT()));
2849
Jason Molenda255f9bb2013-03-06 23:17:36 +00002850 uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00002851
2852 offset = local_symbols_info.entriesOffset;
2853 for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++)
2854 {
2855 struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry;
2856 local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset);
2857 local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset);
2858 local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset);
2859
Jason Molenda4e7511e2013-03-06 23:19:17 +00002860 if (header_file_offset == local_symbols_entry.dylibOffset)
Jason Molendaa5609c82012-06-21 01:51:02 +00002861 {
2862 unmapped_local_symbols_found = local_symbols_entry.nlistCount;
2863
2864 // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here.
2865 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym);
2866 num_syms = symtab->GetNumSymbols();
2867
2868 nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex);
2869 uint32_t string_table_offset = local_symbols_info.stringsOffset;
2870
Jason Molenda4e7511e2013-03-06 23:19:17 +00002871 for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++)
Jason Molendaa5609c82012-06-21 01:51:02 +00002872 {
2873 /////////////////////////////
2874 {
2875 struct nlist_64 nlist;
2876 if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
2877 break;
2878
2879 nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset);
2880 nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2881 nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset);
2882 nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset);
2883 nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset);
2884
2885 SymbolType type = eSymbolTypeInvalid;
2886 const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx);
2887
2888 if (symbol_name == NULL)
2889 {
2890 // No symbol should be NULL, even the symbols with no
2891 // string values should have an offset zero which points
2892 // to an empty C-string
2893 Host::SystemLog (Host::eSystemLogError,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002894 "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 +00002895 entry_index,
2896 nlist.n_strx,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002897 module_sp->GetFileSpec().GetPath().c_str());
Jason Molendaa5609c82012-06-21 01:51:02 +00002898 continue;
2899 }
2900 if (symbol_name[0] == '\0')
2901 symbol_name = NULL;
2902
2903 const char *symbol_name_non_abi_mangled = NULL;
2904
2905 SectionSP symbol_section;
2906 uint32_t symbol_byte_size = 0;
2907 bool add_nlist = true;
Charles Davis510938e2013-08-27 05:04:57 +00002908 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Clayton3d51b9f2012-11-27 01:52:16 +00002909 bool demangled_is_synthesized = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00002910 bool is_gsym = false;
Greg Clayton60038be2015-02-14 00:51:13 +00002911 bool set_value = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00002912
2913 assert (sym_idx < num_syms);
2914
2915 sym[sym_idx].SetDebug (is_debug);
2916
2917 if (is_debug)
2918 {
2919 switch (nlist.n_type)
2920 {
Charles Davis510938e2013-08-27 05:04:57 +00002921 case N_GSYM:
2922 // global symbol: name,,NO_SECT,type,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002923 // Sometimes the N_GSYM value contains the address.
2924
2925 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
2926 // have the same address, but we want to ensure that we always find only the real symbol,
2927 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
2928 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
2929 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
2930 // same address.
2931
Greg Clayton1e28adf2015-02-25 17:25:02 +00002932 is_gsym = true;
2933 sym[sym_idx].SetExternal(true);
2934
2935 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O')
2936 {
2937 llvm::StringRef symbol_name_ref(symbol_name);
2938 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
2939 {
2940 symbol_name_non_abi_mangled = symbol_name + 1;
2941 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2942 type = eSymbolTypeObjCClass;
2943 demangled_is_synthesized = true;
2944
2945 }
2946 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
2947 {
2948 symbol_name_non_abi_mangled = symbol_name + 1;
2949 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2950 type = eSymbolTypeObjCMetaClass;
2951 demangled_is_synthesized = true;
2952 }
2953 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
2954 {
2955 symbol_name_non_abi_mangled = symbol_name + 1;
2956 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2957 type = eSymbolTypeObjCIVar;
2958 demangled_is_synthesized = true;
2959 }
2960 }
Jason Molendaa5609c82012-06-21 01:51:02 +00002961 else
2962 {
Jason Molendaa5609c82012-06-21 01:51:02 +00002963 if (nlist.n_value != 0)
2964 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2965 type = eSymbolTypeData;
2966 }
2967 break;
2968
Charles Davis510938e2013-08-27 05:04:57 +00002969 case N_FNAME:
2970 // procedure name (f77 kludge): name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00002971 type = eSymbolTypeCompiler;
2972 break;
2973
Charles Davis510938e2013-08-27 05:04:57 +00002974 case N_FUN:
2975 // procedure: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00002976 if (symbol_name)
2977 {
2978 type = eSymbolTypeCode;
2979 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
2980
Greg Claytond81088c2014-01-16 01:38:29 +00002981 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00002982 // We use the current number of symbols in the symbol table in lieu of
2983 // using nlist_idx in case we ever start trimming entries out
2984 N_FUN_indexes.push_back(sym_idx);
2985 }
2986 else
2987 {
2988 type = eSymbolTypeCompiler;
2989
2990 if ( !N_FUN_indexes.empty() )
2991 {
2992 // Copy the size of the function into the original STAB entry so we don't have
2993 // to hunt for it later
2994 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
2995 N_FUN_indexes.pop_back();
2996 // We don't really need the end function STAB as it contains the size which
2997 // we already placed with the original symbol, so don't add it if we want a
2998 // minimal symbol table
Greg Clayton3046e662013-07-10 01:23:25 +00002999 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00003000 }
3001 }
3002 break;
3003
Charles Davis510938e2013-08-27 05:04:57 +00003004 case N_STSYM:
3005 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00003006 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Jason Molendaa5609c82012-06-21 01:51:02 +00003007 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda649a6072015-11-10 05:21:54 +00003008 if (symbol_name && symbol_name[0])
3009 {
3010 type = ObjectFile::GetSymbolTypeFromName(symbol_name+1, eSymbolTypeData);
3011 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003012 break;
3013
Charles Davis510938e2013-08-27 05:04:57 +00003014 case N_LCSYM:
3015 // .lcomm symbol: name,,n_sect,type,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003016 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3017 type = eSymbolTypeCommonBlock;
3018 break;
3019
Charles Davis510938e2013-08-27 05:04:57 +00003020 case N_BNSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00003021 // We use the current number of symbols in the symbol table in lieu of
3022 // using nlist_idx in case we ever start trimming entries out
Greg Clayton3046e662013-07-10 01:23:25 +00003023 // Skip these if we want minimal symbol tables
3024 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00003025 break;
3026
Charles Davis510938e2013-08-27 05:04:57 +00003027 case N_ENSYM:
Jason Molendaa5609c82012-06-21 01:51:02 +00003028 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
3029 // so that we can always skip the entire symbol if we need to navigate
3030 // more quickly at the source level when parsing STABS
Greg Clayton3046e662013-07-10 01:23:25 +00003031 // Skip these if we want minimal symbol tables
3032 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00003033 break;
3034
Charles Davis510938e2013-08-27 05:04:57 +00003035 case N_OPT:
3036 // emitted with gcc2_compiled and in gcc source
Jason Molendaa5609c82012-06-21 01:51:02 +00003037 type = eSymbolTypeCompiler;
3038 break;
3039
Charles Davis510938e2013-08-27 05:04:57 +00003040 case N_RSYM:
3041 // register sym: name,,NO_SECT,type,register
Jason Molendaa5609c82012-06-21 01:51:02 +00003042 type = eSymbolTypeVariable;
3043 break;
3044
Charles Davis510938e2013-08-27 05:04:57 +00003045 case N_SLINE:
3046 // src line: 0,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003047 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3048 type = eSymbolTypeLineEntry;
3049 break;
3050
Charles Davis510938e2013-08-27 05:04:57 +00003051 case N_SSYM:
3052 // structure elt: name,,NO_SECT,type,struct_offset
Jason Molendaa5609c82012-06-21 01:51:02 +00003053 type = eSymbolTypeVariableType;
3054 break;
3055
Charles Davis510938e2013-08-27 05:04:57 +00003056 case N_SO:
3057 // source file name
Jason Molendaa5609c82012-06-21 01:51:02 +00003058 type = eSymbolTypeSourceFile;
3059 if (symbol_name == NULL)
3060 {
Greg Clayton3046e662013-07-10 01:23:25 +00003061 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00003062 if (N_SO_index != UINT32_MAX)
3063 {
3064 // Set the size of the N_SO to the terminating index of this N_SO
3065 // so that we can always skip the entire N_SO if we need to navigate
3066 // more quickly at the source level when parsing STABS
3067 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton3046e662013-07-10 01:23:25 +00003068 symbol_ptr->SetByteSize(sym_idx);
Jason Molendaa5609c82012-06-21 01:51:02 +00003069 symbol_ptr->SetSizeIsSibling(true);
3070 }
3071 N_NSYM_indexes.clear();
3072 N_INCL_indexes.clear();
3073 N_BRAC_indexes.clear();
3074 N_COMM_indexes.clear();
3075 N_FUN_indexes.clear();
3076 N_SO_index = UINT32_MAX;
3077 }
3078 else
3079 {
3080 // We use the current number of symbols in the symbol table in lieu of
3081 // using nlist_idx in case we ever start trimming entries out
3082 const bool N_SO_has_full_path = symbol_name[0] == '/';
3083 if (N_SO_has_full_path)
3084 {
Greg Clayton3046e662013-07-10 01:23:25 +00003085 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00003086 {
3087 // We have two consecutive N_SO entries where the first contains a directory
3088 // and the second contains a full path.
Jason Molendad9d5cf52012-07-20 03:35:44 +00003089 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00003090 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3091 add_nlist = false;
3092 }
3093 else
3094 {
3095 // This is the first entry in a N_SO that contains a directory or
3096 // a full path to the source file
3097 N_SO_index = sym_idx;
3098 }
3099 }
Greg Clayton3046e662013-07-10 01:23:25 +00003100 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Jason Molendaa5609c82012-06-21 01:51:02 +00003101 {
3102 // This is usually the second N_SO entry that contains just the filename,
3103 // so here we combine it with the first one if we are minimizing the symbol table
Jason Molendacc9e92e2016-03-01 05:34:05 +00003104 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName(lldb::eLanguageTypeUnknown).AsCString();
Jason Molendaa5609c82012-06-21 01:51:02 +00003105 if (so_path && so_path[0])
3106 {
3107 std::string full_so_path (so_path);
Greg Clayton0662d962012-09-07 20:29:13 +00003108 const size_t double_slash_pos = full_so_path.find("//");
3109 if (double_slash_pos != std::string::npos)
3110 {
3111 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00003112 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Clayton0662d962012-09-07 20:29:13 +00003113 // and the second is the directory for the source file so you end up with
3114 // a path that looks like "/tmp/src//tmp/src/"
3115 FileSpec so_dir(so_path, false);
3116 if (!so_dir.Exists())
3117 {
3118 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3119 if (so_dir.Exists())
3120 {
3121 // Trim off the incorrect path
3122 full_so_path.erase(0, double_slash_pos + 1);
3123 }
3124 }
3125 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003126 if (*full_so_path.rbegin() != '/')
3127 full_so_path += '/';
3128 full_so_path += symbol_name;
Jason Molendad9d5cf52012-07-20 03:35:44 +00003129 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
Jason Molendaa5609c82012-06-21 01:51:02 +00003130 add_nlist = false;
3131 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3132 }
3133 }
Greg Clayton946f8902012-09-05 22:30:51 +00003134 else
3135 {
3136 // This could be a relative path to a N_SO
3137 N_SO_index = sym_idx;
3138 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003139 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003140 break;
3141
Charles Davis510938e2013-08-27 05:04:57 +00003142 case N_OSO:
3143 // object file name: name,,0,0,st_mtime
Jason Molendaa5609c82012-06-21 01:51:02 +00003144 type = eSymbolTypeObjectFile;
3145 break;
3146
Charles Davis510938e2013-08-27 05:04:57 +00003147 case N_LSYM:
3148 // local sym: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00003149 type = eSymbolTypeLocal;
3150 break;
3151
3152 //----------------------------------------------------------------------
3153 // INCL scopes
3154 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003155 case N_BINCL:
3156 // include file beginning: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00003157 // We use the current number of symbols in the symbol table in lieu of
3158 // using nlist_idx in case we ever start trimming entries out
3159 N_INCL_indexes.push_back(sym_idx);
3160 type = eSymbolTypeScopeBegin;
3161 break;
3162
Charles Davis510938e2013-08-27 05:04:57 +00003163 case N_EINCL:
3164 // include file end: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003165 // Set the size of the N_BINCL to the terminating index of this N_EINCL
3166 // so that we can always skip the entire symbol if we need to navigate
3167 // more quickly at the source level when parsing STABS
3168 if ( !N_INCL_indexes.empty() )
3169 {
3170 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
3171 symbol_ptr->SetByteSize(sym_idx + 1);
3172 symbol_ptr->SetSizeIsSibling(true);
3173 N_INCL_indexes.pop_back();
3174 }
3175 type = eSymbolTypeScopeEnd;
3176 break;
3177
Charles Davis510938e2013-08-27 05:04:57 +00003178 case N_SOL:
3179 // #included file name: name,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003180 type = eSymbolTypeHeaderFile;
3181
3182 // We currently don't use the header files on darwin
Greg Clayton3046e662013-07-10 01:23:25 +00003183 add_nlist = false;
Jason Molendaa5609c82012-06-21 01:51:02 +00003184 break;
3185
Charles Davis510938e2013-08-27 05:04:57 +00003186 case N_PARAMS:
3187 // compiler parameters: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003188 type = eSymbolTypeCompiler;
3189 break;
3190
Charles Davis510938e2013-08-27 05:04:57 +00003191 case N_VERSION:
3192 // compiler version: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003193 type = eSymbolTypeCompiler;
3194 break;
3195
Charles Davis510938e2013-08-27 05:04:57 +00003196 case N_OLEVEL:
3197 // compiler -O level: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003198 type = eSymbolTypeCompiler;
3199 break;
3200
Charles Davis510938e2013-08-27 05:04:57 +00003201 case N_PSYM:
3202 // parameter: name,,NO_SECT,type,offset
Jason Molendaa5609c82012-06-21 01:51:02 +00003203 type = eSymbolTypeVariable;
3204 break;
3205
Charles Davis510938e2013-08-27 05:04:57 +00003206 case N_ENTRY:
3207 // alternate entry: name,,n_sect,linenumber,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003208 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3209 type = eSymbolTypeLineEntry;
3210 break;
3211
3212 //----------------------------------------------------------------------
3213 // Left and Right Braces
3214 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003215 case N_LBRAC:
3216 // left bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003217 // We use the current number of symbols in the symbol table in lieu of
3218 // using nlist_idx in case we ever start trimming entries out
3219 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3220 N_BRAC_indexes.push_back(sym_idx);
3221 type = eSymbolTypeScopeBegin;
3222 break;
3223
Charles Davis510938e2013-08-27 05:04:57 +00003224 case N_RBRAC:
3225 // right bracket: 0,,NO_SECT,nesting level,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003226 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
3227 // so that we can always skip the entire symbol if we need to navigate
3228 // more quickly at the source level when parsing STABS
3229 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3230 if ( !N_BRAC_indexes.empty() )
3231 {
3232 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
3233 symbol_ptr->SetByteSize(sym_idx + 1);
3234 symbol_ptr->SetSizeIsSibling(true);
3235 N_BRAC_indexes.pop_back();
3236 }
3237 type = eSymbolTypeScopeEnd;
3238 break;
3239
Charles Davis510938e2013-08-27 05:04:57 +00003240 case N_EXCL:
3241 // deleted include file: name,,NO_SECT,0,sum
Jason Molendaa5609c82012-06-21 01:51:02 +00003242 type = eSymbolTypeHeaderFile;
3243 break;
3244
3245 //----------------------------------------------------------------------
3246 // COMM scopes
3247 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00003248 case N_BCOMM:
3249 // begin common: name,,NO_SECT,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003250 // We use the current number of symbols in the symbol table in lieu of
3251 // using nlist_idx in case we ever start trimming entries out
3252 type = eSymbolTypeScopeBegin;
3253 N_COMM_indexes.push_back(sym_idx);
3254 break;
3255
Charles Davis510938e2013-08-27 05:04:57 +00003256 case N_ECOML:
3257 // end common (local name): 0,,n_sect,0,address
Jason Molendaa5609c82012-06-21 01:51:02 +00003258 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3259 // Fall through
3260
Charles Davis510938e2013-08-27 05:04:57 +00003261 case N_ECOMM:
3262 // end common: name,,n_sect,0,0
Jason Molendaa5609c82012-06-21 01:51:02 +00003263 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
3264 // so that we can always skip the entire symbol if we need to navigate
3265 // more quickly at the source level when parsing STABS
3266 if ( !N_COMM_indexes.empty() )
3267 {
3268 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
3269 symbol_ptr->SetByteSize(sym_idx + 1);
3270 symbol_ptr->SetSizeIsSibling(true);
3271 N_COMM_indexes.pop_back();
3272 }
3273 type = eSymbolTypeScopeEnd;
3274 break;
3275
Charles Davis510938e2013-08-27 05:04:57 +00003276 case N_LENG:
3277 // second stab entry with length information
Jason Molendaa5609c82012-06-21 01:51:02 +00003278 type = eSymbolTypeAdditional;
3279 break;
3280
3281 default: break;
3282 }
3283 }
3284 else
3285 {
Charles Davis510938e2013-08-27 05:04:57 +00003286 //uint8_t n_pext = N_PEXT & nlist.n_type;
3287 uint8_t n_type = N_TYPE & nlist.n_type;
3288 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Jason Molendaa5609c82012-06-21 01:51:02 +00003289
3290 switch (n_type)
3291 {
Greg Clayton60038be2015-02-14 00:51:13 +00003292 case N_INDR:
3293 {
3294 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
3295 if (reexport_name_cstr && reexport_name_cstr[0])
3296 {
3297 type = eSymbolTypeReExported;
3298 ConstString reexport_name(reexport_name_cstr + ((reexport_name_cstr[0] == '_') ? 1 : 0));
3299 sym[sym_idx].SetReExportedSymbolName(reexport_name);
3300 set_value = false;
3301 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3302 indirect_symbol_names.insert(ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0)));
3303 }
3304 else
3305 type = eSymbolTypeUndefined;
3306 }
3307 break;
3308
Charles Davis510938e2013-08-27 05:04:57 +00003309 case N_UNDF:
Greg Clayton786ad182015-03-03 01:40:46 +00003310 if (symbol_name && symbol_name[0])
Greg Clayton60038be2015-02-14 00:51:13 +00003311 {
3312 ConstString undefined_name(symbol_name + ((symbol_name[0] == '_') ? 1 : 0));
3313 undefined_name_to_desc[undefined_name] = nlist.n_desc;
3314 }
3315 // Fall through
3316 case N_PBUD:
Jason Molendaa5609c82012-06-21 01:51:02 +00003317 type = eSymbolTypeUndefined;
3318 break;
3319
Charles Davis510938e2013-08-27 05:04:57 +00003320 case N_ABS:
Jason Molendaa5609c82012-06-21 01:51:02 +00003321 type = eSymbolTypeAbsolute;
3322 break;
3323
Charles Davis510938e2013-08-27 05:04:57 +00003324 case N_SECT:
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003325 {
3326 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molendaa5609c82012-06-21 01:51:02 +00003327
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003328 if (symbol_section == NULL)
Jason Molendaa5609c82012-06-21 01:51:02 +00003329 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003330 // TODO: warn about this?
3331 add_nlist = false;
3332 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00003333 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003334
3335 if (TEXT_eh_frame_sectID == nlist.n_sect)
Jason Molendaa5609c82012-06-21 01:51:02 +00003336 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003337 type = eSymbolTypeException;
3338 }
3339 else
3340 {
Charles Davis510938e2013-08-27 05:04:57 +00003341 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003342
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003343 switch (section_type)
Jason Molendaa5609c82012-06-21 01:51:02 +00003344 {
Charles Davis510938e2013-08-27 05:04:57 +00003345 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
3346 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
3347 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
3348 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
3349 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
3350 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
3351 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
3352 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
3353 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
Charles Davis510938e2013-08-27 05:04:57 +00003354 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
3355 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
3356 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
3357 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton38f9cc42014-06-16 22:53:16 +00003358 default:
3359 switch (symbol_section->GetType())
3360 {
3361 case lldb::eSectionTypeCode:
3362 type = eSymbolTypeCode;
3363 break;
3364 case eSectionTypeData:
3365 case eSectionTypeDataCString: // Inlined C string data
3366 case eSectionTypeDataCStringPointers: // Pointers to C string data
3367 case eSectionTypeDataSymbolAddress: // Address of a symbol in the symbol table
3368 case eSectionTypeData4:
3369 case eSectionTypeData8:
3370 case eSectionTypeData16:
Greg Clayton38f9cc42014-06-16 22:53:16 +00003371 type = eSymbolTypeData;
3372 break;
3373 default:
3374 break;
3375 }
3376 break;
Jason Molendaa5609c82012-06-21 01:51:02 +00003377 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003378
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003379 if (type == eSymbolTypeInvalid)
3380 {
3381 const char *symbol_sect_name = symbol_section->GetName().AsCString();
3382 if (symbol_section->IsDescendant (text_section_sp.get()))
3383 {
Charles Davis510938e2013-08-27 05:04:57 +00003384 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
3385 S_ATTR_SELF_MODIFYING_CODE |
3386 S_ATTR_SOME_INSTRUCTIONS))
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003387 type = eSymbolTypeData;
3388 else
3389 type = eSymbolTypeCode;
3390 }
Greg Claytona381e102015-07-27 23:21:05 +00003391 else if (symbol_section->IsDescendant(data_section_sp.get()) ||
3392 symbol_section->IsDescendant(data_dirty_section_sp.get()) ||
3393 symbol_section->IsDescendant(data_const_section_sp.get()))
Jason Molendaa5609c82012-06-21 01:51:02 +00003394 {
3395 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
3396 {
3397 type = eSymbolTypeRuntime;
Jason Molenda649a6072015-11-10 05:21:54 +00003398
3399 if (symbol_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00003400 {
3401 llvm::StringRef symbol_name_ref(symbol_name);
Jason Molenda649a6072015-11-10 05:21:54 +00003402 if (symbol_name_ref.startswith("_OBJC_"))
Jason Molendaa5609c82012-06-21 01:51:02 +00003403 {
Jason Molenda649a6072015-11-10 05:21:54 +00003404 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
3405 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
3406 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
3407 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3408 {
3409 symbol_name_non_abi_mangled = symbol_name + 1;
3410 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3411 type = eSymbolTypeObjCClass;
3412 demangled_is_synthesized = true;
3413 }
3414 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3415 {
3416 symbol_name_non_abi_mangled = symbol_name + 1;
3417 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3418 type = eSymbolTypeObjCMetaClass;
3419 demangled_is_synthesized = true;
3420 }
3421 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3422 {
3423 symbol_name_non_abi_mangled = symbol_name + 1;
3424 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3425 type = eSymbolTypeObjCIVar;
3426 demangled_is_synthesized = true;
3427 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003428 }
3429 }
3430 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003431 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
Jason Molendaa5609c82012-06-21 01:51:02 +00003432 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003433 type = eSymbolTypeException;
Jason Molendaa5609c82012-06-21 01:51:02 +00003434 }
3435 else
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003436 {
3437 type = eSymbolTypeData;
3438 }
3439 }
3440 else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
3441 {
3442 type = eSymbolTypeTrampoline;
3443 }
3444 else if (symbol_section->IsDescendant(objc_section_sp.get()))
3445 {
3446 type = eSymbolTypeRuntime;
3447 if (symbol_name && symbol_name[0] == '.')
3448 {
3449 llvm::StringRef symbol_name_ref(symbol_name);
3450 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
3451 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
Jason Molendaa5609c82012-06-21 01:51:02 +00003452 {
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003453 symbol_name_non_abi_mangled = symbol_name;
3454 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
3455 type = eSymbolTypeObjCClass;
3456 demangled_is_synthesized = true;
Jason Molendaa5609c82012-06-21 01:51:02 +00003457 }
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003458 }
3459 }
3460 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003461 }
3462 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003463 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003464 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003465 }
3466
3467 if (add_nlist)
3468 {
3469 uint64_t symbol_value = nlist.n_value;
Jason Molendaa5609c82012-06-21 01:51:02 +00003470 if (symbol_name_non_abi_mangled)
3471 {
Jason Molendad9d5cf52012-07-20 03:35:44 +00003472 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
3473 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
Jason Molendaa5609c82012-06-21 01:51:02 +00003474 }
3475 else
3476 {
Greg Clayton3046e662013-07-10 01:23:25 +00003477 bool symbol_name_is_mangled = false;
3478
Jason Molendaa5609c82012-06-21 01:51:02 +00003479 if (symbol_name && symbol_name[0] == '_')
3480 {
3481 symbol_name_is_mangled = symbol_name[1] == '_';
3482 symbol_name++; // Skip the leading underscore
3483 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003484
Jason Molendaa5609c82012-06-21 01:51:02 +00003485 if (symbol_name)
3486 {
Greg Claytondacc4a92013-05-14 22:19:37 +00003487 ConstString const_symbol_name(symbol_name);
Greg Claytondacc4a92013-05-14 22:19:37 +00003488 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Clayton3046e662013-07-10 01:23:25 +00003489 if (is_gsym && is_debug)
Greg Clayton14cd13c2015-07-01 23:29:06 +00003490 {
Jason Molendacc9e92e2016-03-01 05:34:05 +00003491 const char *gsym_name = sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled).GetCString();
Greg Clayton14cd13c2015-07-01 23:29:06 +00003492 if (gsym_name)
3493 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
3494 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003495 }
3496 }
3497 if (symbol_section)
3498 {
3499 const addr_t section_file_addr = symbol_section->GetFileAddress();
3500 if (symbol_byte_size == 0 && function_starts_count > 0)
3501 {
3502 addr_t symbol_lookup_file_addr = nlist.n_value;
3503 // Do an exact address match for non-ARM addresses, else get the closest since
3504 // the symbol might be a thumb symbol which has an address with bit zero set
3505 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
3506 if (is_arm && func_start_entry)
3507 {
3508 // Verify that the function start address is the symbol address (ARM)
3509 // or the symbol address + 1 (thumb)
3510 if (func_start_entry->addr != symbol_lookup_file_addr &&
3511 func_start_entry->addr != (symbol_lookup_file_addr + 1))
3512 {
3513 // Not the right entry, NULL it out...
3514 func_start_entry = NULL;
3515 }
3516 }
3517 if (func_start_entry)
3518 {
3519 func_start_entry->data = true;
Jason Molenda4e7511e2013-03-06 23:19:17 +00003520
Jason Molendaa5609c82012-06-21 01:51:02 +00003521 addr_t symbol_file_addr = func_start_entry->addr;
3522 uint32_t symbol_flags = 0;
3523 if (is_arm)
3524 {
3525 if (symbol_file_addr & 1)
3526 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
Greg Claytonb887da12015-07-16 19:50:57 +00003527 symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Jason Molendaa5609c82012-06-21 01:51:02 +00003528 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003529
Jason Molendaa5609c82012-06-21 01:51:02 +00003530 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
3531 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
3532 if (next_func_start_entry)
3533 {
3534 addr_t next_symbol_file_addr = next_func_start_entry->addr;
3535 // Be sure the clear the Thumb address bit when we calculate the size
3536 // from the current and next address
3537 if (is_arm)
Greg Claytonb887da12015-07-16 19:50:57 +00003538 next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Jason Molendaa5609c82012-06-21 01:51:02 +00003539 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
3540 }
3541 else
3542 {
3543 symbol_byte_size = section_end_file_addr - symbol_file_addr;
3544 }
3545 }
3546 }
3547 symbol_value -= section_file_addr;
3548 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003549
Greg Claytondacc4a92013-05-14 22:19:37 +00003550 if (is_debug == false)
3551 {
3552 if (type == eSymbolTypeCode)
3553 {
3554 // See if we can find a N_FUN entry for any code symbols.
3555 // If we do find a match, and the name matches, then we
3556 // can merge the two into just the function symbol to avoid
3557 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003558 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3559 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3560 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00003561 {
Greg Claytond81088c2014-01-16 01:38:29 +00003562 bool found_it = false;
3563 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00003564 {
Jason Molendacc9e92e2016-03-01 05:34:05 +00003565 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 +00003566 {
3567 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3568 // We just need the flags from the linker symbol, so put these flags
3569 // into the N_FUN flags to avoid duplicate symbols in the symbol table
3570 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3571 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3572 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
3573 sym[pos->second].SetType (eSymbolTypeResolver);
3574 sym[sym_idx].Clear();
3575 found_it = true;
3576 break;
3577 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003578 }
Greg Claytond81088c2014-01-16 01:38:29 +00003579 if (found_it)
3580 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00003581 }
Jim Inghamea3ac272014-01-10 22:55:37 +00003582 else
3583 {
Greg Claytond81088c2014-01-16 01:38:29 +00003584 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00003585 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00003586 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003587 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00003588 else if (type == eSymbolTypeData ||
3589 type == eSymbolTypeObjCClass ||
3590 type == eSymbolTypeObjCMetaClass ||
3591 type == eSymbolTypeObjCIVar )
Greg Claytondacc4a92013-05-14 22:19:37 +00003592 {
3593 // See if we can find a N_STSYM entry for any data symbols.
3594 // If we do find a match, and the name matches, then we
3595 // can merge the two into just the Static symbol to avoid
3596 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00003597 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
3598 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
3599 if (range.first != range.second)
Greg Claytondacc4a92013-05-14 22:19:37 +00003600 {
Greg Claytond81088c2014-01-16 01:38:29 +00003601 bool found_it = false;
3602 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytondacc4a92013-05-14 22:19:37 +00003603 {
Jason Molendacc9e92e2016-03-01 05:34:05 +00003604 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 +00003605 {
3606 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3607 // We just need the flags from the linker symbol, so put these flags
3608 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
3609 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
3610 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3611 sym[sym_idx].Clear();
3612 found_it = true;
3613 break;
3614 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003615 }
Greg Claytond81088c2014-01-16 01:38:29 +00003616 if (found_it)
3617 continue;
Greg Claytondacc4a92013-05-14 22:19:37 +00003618 }
3619 else
3620 {
Jason Molendacc9e92e2016-03-01 05:34:05 +00003621 const char *gsym_name = sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled).GetCString();
Greg Clayton14cd13c2015-07-01 23:29:06 +00003622 if (gsym_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00003623 {
Greg Clayton14cd13c2015-07-01 23:29:06 +00003624 // Combine N_GSYM stab entries with the non stab symbol
3625 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(gsym_name);
3626 if (pos != N_GSYM_name_to_sym_idx.end())
3627 {
3628 const uint32_t GSYM_sym_idx = pos->second;
3629 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
3630 // Copy the address, because often the N_GSYM address has an invalid address of zero
3631 // when the global is a common symbol
3632 sym[GSYM_sym_idx].GetAddressRef().SetSection (symbol_section);
3633 sym[GSYM_sym_idx].GetAddressRef().SetOffset (symbol_value);
3634 // We just need the flags from the linker symbol, so put these flags
3635 // into the N_GSYM flags to avoid duplicate symbols in the symbol table
3636 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
3637 sym[sym_idx].Clear();
3638 continue;
3639 }
Greg Claytondacc4a92013-05-14 22:19:37 +00003640 }
3641 }
3642 }
3643 }
3644
Jason Molendaa5609c82012-06-21 01:51:02 +00003645 sym[sym_idx].SetID (nlist_idx);
3646 sym[sym_idx].SetType (type);
Greg Clayton60038be2015-02-14 00:51:13 +00003647 if (set_value)
3648 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00003649 sym[sym_idx].GetAddressRef().SetSection (symbol_section);
3650 sym[sym_idx].GetAddressRef().SetOffset (symbol_value);
Greg Clayton60038be2015-02-14 00:51:13 +00003651 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003652 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003653
Jason Molendaa5609c82012-06-21 01:51:02 +00003654 if (symbol_byte_size > 0)
3655 sym[sym_idx].SetByteSize(symbol_byte_size);
3656
Greg Clayton3d51b9f2012-11-27 01:52:16 +00003657 if (demangled_is_synthesized)
3658 sym[sym_idx].SetDemangledNameIsSynthesized(true);
Jason Molendaa5609c82012-06-21 01:51:02 +00003659 ++sym_idx;
3660 }
3661 else
3662 {
3663 sym[sym_idx].Clear();
3664 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003665
Jason Molendaa5609c82012-06-21 01:51:02 +00003666 }
3667 /////////////////////////////
3668 }
3669 break; // No more entries to consider
3670 }
3671 }
Greg Clayton60038be2015-02-14 00:51:13 +00003672
3673 for (const auto &pos :reexport_shlib_needs_fixup)
3674 {
3675 const auto undef_pos = undefined_name_to_desc.find(pos.second);
3676 if (undef_pos != undefined_name_to_desc.end())
3677 {
3678 const uint8_t dylib_ordinal = llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
3679 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
3680 sym[pos.first].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(dylib_ordinal-1));
3681 }
3682 }
Jason Molendaa5609c82012-06-21 01:51:02 +00003683 }
3684 }
3685 }
3686 }
3687 }
3688
3689 // Must reset this in case it was mutated above!
3690 nlist_data_offset = 0;
3691#endif
Jim Inghamea3ac272014-01-10 22:55:37 +00003692
Greg Claytonfd814c52013-08-13 01:42:25 +00003693 if (nlist_data.GetByteSize() > 0)
Jason Molendaa5609c82012-06-21 01:51:02 +00003694 {
Jason Molendaa5609c82012-06-21 01:51:02 +00003695
Greg Claytonfd814c52013-08-13 01:42:25 +00003696 // If the sym array was not created while parsing the DSC unmapped
3697 // symbols, create it now.
3698 if (sym == NULL)
Greg Clayton4c82d422012-05-18 23:20:01 +00003699 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003700 sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
3701 num_syms = symtab->GetNumSymbols();
3702 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00003703
Greg Claytonfd814c52013-08-13 01:42:25 +00003704 if (unmapped_local_symbols_found)
3705 {
3706 assert(m_dysymtab.ilocalsym == 0);
3707 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
3708 nlist_idx = m_dysymtab.nlocalsym;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003709 }
Greg Claytondebb8812012-05-25 17:04:00 +00003710 else
3711 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003712 nlist_idx = 0;
Greg Claytondebb8812012-05-25 17:04:00 +00003713 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003714
Greg Clayton60038be2015-02-14 00:51:13 +00003715 typedef std::map<ConstString, uint16_t> UndefinedNameToDescMap;
3716 typedef std::map<uint32_t, ConstString> SymbolIndexToName;
3717 UndefinedNameToDescMap undefined_name_to_desc;
3718 SymbolIndexToName reexport_shlib_needs_fixup;
Greg Claytonfd814c52013-08-13 01:42:25 +00003719 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003720 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003721 struct nlist_64 nlist;
3722 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
3723 break;
3724
3725 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
3726 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
3727 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
3728 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
3729 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
3730
3731 SymbolType type = eSymbolTypeInvalid;
3732 const char *symbol_name = NULL;
3733
3734 if (have_strtab_data)
Greg Clayton77ccca72011-12-30 00:32:24 +00003735 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003736 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
Jason Molenda4e7511e2013-03-06 23:19:17 +00003737
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003738 if (symbol_name == NULL)
3739 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003740 // No symbol should be NULL, even the symbols with no
3741 // string values should have an offset zero which points
3742 // to an empty C-string
3743 Host::SystemLog (Host::eSystemLogError,
3744 "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n",
3745 nlist_idx,
3746 nlist.n_strx,
3747 module_sp->GetFileSpec().GetPath().c_str());
3748 continue;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003749 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003750 if (symbol_name[0] == '\0')
3751 symbol_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003752 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003753 else
3754 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003755 const addr_t str_addr = strtab_addr + nlist.n_strx;
3756 Error str_error;
3757 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error))
3758 symbol_name = memory_symbol_name.c_str();
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003759 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003760 const char *symbol_name_non_abi_mangled = NULL;
3761
3762 SectionSP symbol_section;
3763 lldb::addr_t symbol_byte_size = 0;
3764 bool add_nlist = true;
3765 bool is_gsym = false;
Charles Davis510938e2013-08-27 05:04:57 +00003766 bool is_debug = ((nlist.n_type & N_STAB) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00003767 bool demangled_is_synthesized = false;
Greg Clayton60038be2015-02-14 00:51:13 +00003768 bool set_value = true;
Greg Claytonfd814c52013-08-13 01:42:25 +00003769 assert (sym_idx < num_syms);
3770
3771 sym[sym_idx].SetDebug (is_debug);
3772
3773 if (is_debug)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003774 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003775 switch (nlist.n_type)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003776 {
Charles Davis510938e2013-08-27 05:04:57 +00003777 case N_GSYM:
3778 // global symbol: name,,NO_SECT,type,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003779 // Sometimes the N_GSYM value contains the address.
Jason Molenda4e7511e2013-03-06 23:19:17 +00003780
Greg Claytonfd814c52013-08-13 01:42:25 +00003781 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
3782 // have the same address, but we want to ensure that we always find only the real symbol,
3783 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
3784 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
3785 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
3786 // same address.
Greg Clayton1e28adf2015-02-25 17:25:02 +00003787 is_gsym = true;
3788 sym[sym_idx].SetExternal(true);
Greg Clayton29e08cb2012-03-14 01:53:24 +00003789
Greg Clayton1e28adf2015-02-25 17:25:02 +00003790 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O')
3791 {
3792 llvm::StringRef symbol_name_ref(symbol_name);
3793 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
3794 {
3795 symbol_name_non_abi_mangled = symbol_name + 1;
3796 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3797 type = eSymbolTypeObjCClass;
3798 demangled_is_synthesized = true;
3799
3800 }
3801 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
3802 {
3803 symbol_name_non_abi_mangled = symbol_name + 1;
3804 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3805 type = eSymbolTypeObjCMetaClass;
3806 demangled_is_synthesized = true;
3807 }
3808 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
3809 {
3810 symbol_name_non_abi_mangled = symbol_name + 1;
3811 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3812 type = eSymbolTypeObjCIVar;
3813 demangled_is_synthesized = true;
3814 }
3815 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003816 else
3817 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003818 if (nlist.n_value != 0)
3819 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3820 type = eSymbolTypeData;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003821 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003822 break;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00003823
Charles Davis510938e2013-08-27 05:04:57 +00003824 case N_FNAME:
3825 // procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00003826 type = eSymbolTypeCompiler;
3827 break;
3828
Charles Davis510938e2013-08-27 05:04:57 +00003829 case N_FUN:
3830 // procedure: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003831 if (symbol_name)
Greg Claytondacc4a92013-05-14 22:19:37 +00003832 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003833 type = eSymbolTypeCode;
3834 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3835
Greg Claytond81088c2014-01-16 01:38:29 +00003836 N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003837 // We use the current number of symbols in the symbol table in lieu of
3838 // using nlist_idx in case we ever start trimming entries out
3839 N_FUN_indexes.push_back(sym_idx);
Greg Claytondacc4a92013-05-14 22:19:37 +00003840 }
3841 else
3842 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003843 type = eSymbolTypeCompiler;
3844
3845 if ( !N_FUN_indexes.empty() )
Greg Claytondacc4a92013-05-14 22:19:37 +00003846 {
Greg Claytonfd814c52013-08-13 01:42:25 +00003847 // Copy the size of the function into the original STAB entry so we don't have
3848 // to hunt for it later
3849 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
3850 N_FUN_indexes.pop_back();
3851 // We don't really need the end function STAB as it contains the size which
3852 // we already placed with the original symbol, so don't add it if we want a
3853 // minimal symbol table
3854 add_nlist = false;
Greg Claytondacc4a92013-05-14 22:19:37 +00003855 }
3856 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003857 break;
3858
Charles Davis510938e2013-08-27 05:04:57 +00003859 case N_STSYM:
3860 // static symbol: name,,n_sect,type,address
Greg Claytond81088c2014-01-16 01:38:29 +00003861 N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
Greg Claytonfd814c52013-08-13 01:42:25 +00003862 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda649a6072015-11-10 05:21:54 +00003863 if (symbol_name && symbol_name[0])
3864 {
3865 type = ObjectFile::GetSymbolTypeFromName(symbol_name+1, eSymbolTypeData);
3866 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003867 break;
3868
Charles Davis510938e2013-08-27 05:04:57 +00003869 case N_LCSYM:
3870 // .lcomm symbol: name,,n_sect,type,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003871 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3872 type = eSymbolTypeCommonBlock;
3873 break;
3874
Charles Davis510938e2013-08-27 05:04:57 +00003875 case N_BNSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003876 // We use the current number of symbols in the symbol table in lieu of
3877 // using nlist_idx in case we ever start trimming entries out
3878 // Skip these if we want minimal symbol tables
3879 add_nlist = false;
3880 break;
3881
Charles Davis510938e2013-08-27 05:04:57 +00003882 case N_ENSYM:
Greg Claytonfd814c52013-08-13 01:42:25 +00003883 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
3884 // so that we can always skip the entire symbol if we need to navigate
3885 // more quickly at the source level when parsing STABS
3886 // Skip these if we want minimal symbol tables
3887 add_nlist = false;
3888 break;
3889
3890
Charles Davis510938e2013-08-27 05:04:57 +00003891 case N_OPT:
3892 // emitted with gcc2_compiled and in gcc source
Greg Claytonfd814c52013-08-13 01:42:25 +00003893 type = eSymbolTypeCompiler;
3894 break;
3895
Charles Davis510938e2013-08-27 05:04:57 +00003896 case N_RSYM:
3897 // register sym: name,,NO_SECT,type,register
Greg Claytonfd814c52013-08-13 01:42:25 +00003898 type = eSymbolTypeVariable;
3899 break;
3900
Charles Davis510938e2013-08-27 05:04:57 +00003901 case N_SLINE:
3902 // src line: 0,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00003903 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
3904 type = eSymbolTypeLineEntry;
3905 break;
3906
Charles Davis510938e2013-08-27 05:04:57 +00003907 case N_SSYM:
3908 // structure elt: name,,NO_SECT,type,struct_offset
Greg Claytonfd814c52013-08-13 01:42:25 +00003909 type = eSymbolTypeVariableType;
3910 break;
3911
Charles Davis510938e2013-08-27 05:04:57 +00003912 case N_SO:
3913 // source file name
Greg Claytonfd814c52013-08-13 01:42:25 +00003914 type = eSymbolTypeSourceFile;
3915 if (symbol_name == NULL)
3916 {
3917 add_nlist = false;
3918 if (N_SO_index != UINT32_MAX)
3919 {
3920 // Set the size of the N_SO to the terminating index of this N_SO
3921 // so that we can always skip the entire N_SO if we need to navigate
3922 // more quickly at the source level when parsing STABS
3923 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
3924 symbol_ptr->SetByteSize(sym_idx);
3925 symbol_ptr->SetSizeIsSibling(true);
3926 }
3927 N_NSYM_indexes.clear();
3928 N_INCL_indexes.clear();
3929 N_BRAC_indexes.clear();
3930 N_COMM_indexes.clear();
3931 N_FUN_indexes.clear();
3932 N_SO_index = UINT32_MAX;
3933 }
3934 else
3935 {
3936 // We use the current number of symbols in the symbol table in lieu of
3937 // using nlist_idx in case we ever start trimming entries out
3938 const bool N_SO_has_full_path = symbol_name[0] == '/';
3939 if (N_SO_has_full_path)
3940 {
3941 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3942 {
3943 // We have two consecutive N_SO entries where the first contains a directory
3944 // and the second contains a full path.
3945 sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false);
3946 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3947 add_nlist = false;
3948 }
3949 else
3950 {
3951 // This is the first entry in a N_SO that contains a directory or
3952 // a full path to the source file
3953 N_SO_index = sym_idx;
3954 }
3955 }
3956 else if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
3957 {
3958 // This is usually the second N_SO entry that contains just the filename,
3959 // so here we combine it with the first one if we are minimizing the symbol table
Greg Claytonddaf6a72015-07-08 22:32:23 +00003960 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName(lldb::eLanguageTypeUnknown).AsCString();
Greg Claytonfd814c52013-08-13 01:42:25 +00003961 if (so_path && so_path[0])
3962 {
3963 std::string full_so_path (so_path);
3964 const size_t double_slash_pos = full_so_path.find("//");
3965 if (double_slash_pos != std::string::npos)
3966 {
3967 // The linker has been generating bad N_SO entries with doubled up paths
Ashok Thirumurthi03520b72013-08-27 14:56:58 +00003968 // in the format "%s%s" where the first string in the DW_AT_comp_dir,
Greg Claytonfd814c52013-08-13 01:42:25 +00003969 // and the second is the directory for the source file so you end up with
3970 // a path that looks like "/tmp/src//tmp/src/"
3971 FileSpec so_dir(so_path, false);
3972 if (!so_dir.Exists())
3973 {
3974 so_dir.SetFile(&full_so_path[double_slash_pos + 1], false);
3975 if (so_dir.Exists())
3976 {
3977 // Trim off the incorrect path
3978 full_so_path.erase(0, double_slash_pos + 1);
3979 }
3980 }
3981 }
3982 if (*full_so_path.rbegin() != '/')
3983 full_so_path += '/';
3984 full_so_path += symbol_name;
3985 sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false);
3986 add_nlist = false;
3987 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3988 }
3989 }
3990 else
3991 {
3992 // This could be a relative path to a N_SO
3993 N_SO_index = sym_idx;
3994 }
3995 }
Greg Claytonfd814c52013-08-13 01:42:25 +00003996 break;
3997
Charles Davis510938e2013-08-27 05:04:57 +00003998 case N_OSO:
3999 // object file name: name,,0,0,st_mtime
Greg Claytonfd814c52013-08-13 01:42:25 +00004000 type = eSymbolTypeObjectFile;
4001 break;
4002
Charles Davis510938e2013-08-27 05:04:57 +00004003 case N_LSYM:
4004 // local sym: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00004005 type = eSymbolTypeLocal;
4006 break;
4007
4008 //----------------------------------------------------------------------
4009 // INCL scopes
4010 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00004011 case N_BINCL:
4012 // include file beginning: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00004013 // We use the current number of symbols in the symbol table in lieu of
4014 // using nlist_idx in case we ever start trimming entries out
4015 N_INCL_indexes.push_back(sym_idx);
4016 type = eSymbolTypeScopeBegin;
4017 break;
4018
Charles Davis510938e2013-08-27 05:04:57 +00004019 case N_EINCL:
4020 // include file end: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004021 // Set the size of the N_BINCL to the terminating index of this N_EINCL
4022 // so that we can always skip the entire symbol if we need to navigate
4023 // more quickly at the source level when parsing STABS
4024 if ( !N_INCL_indexes.empty() )
4025 {
4026 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
4027 symbol_ptr->SetByteSize(sym_idx + 1);
4028 symbol_ptr->SetSizeIsSibling(true);
4029 N_INCL_indexes.pop_back();
4030 }
4031 type = eSymbolTypeScopeEnd;
4032 break;
4033
Charles Davis510938e2013-08-27 05:04:57 +00004034 case N_SOL:
4035 // #included file name: name,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004036 type = eSymbolTypeHeaderFile;
4037
4038 // We currently don't use the header files on darwin
4039 add_nlist = false;
4040 break;
4041
Charles Davis510938e2013-08-27 05:04:57 +00004042 case N_PARAMS:
4043 // compiler parameters: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004044 type = eSymbolTypeCompiler;
4045 break;
4046
Charles Davis510938e2013-08-27 05:04:57 +00004047 case N_VERSION:
4048 // compiler version: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004049 type = eSymbolTypeCompiler;
4050 break;
4051
Charles Davis510938e2013-08-27 05:04:57 +00004052 case N_OLEVEL:
4053 // compiler -O level: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004054 type = eSymbolTypeCompiler;
4055 break;
4056
Charles Davis510938e2013-08-27 05:04:57 +00004057 case N_PSYM:
4058 // parameter: name,,NO_SECT,type,offset
Greg Claytonfd814c52013-08-13 01:42:25 +00004059 type = eSymbolTypeVariable;
4060 break;
4061
Charles Davis510938e2013-08-27 05:04:57 +00004062 case N_ENTRY:
4063 // alternate entry: name,,n_sect,linenumber,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004064 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4065 type = eSymbolTypeLineEntry;
4066 break;
4067
4068 //----------------------------------------------------------------------
4069 // Left and Right Braces
4070 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00004071 case N_LBRAC:
4072 // left bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004073 // We use the current number of symbols in the symbol table in lieu of
4074 // using nlist_idx in case we ever start trimming entries out
4075 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4076 N_BRAC_indexes.push_back(sym_idx);
4077 type = eSymbolTypeScopeBegin;
4078 break;
4079
Charles Davis510938e2013-08-27 05:04:57 +00004080 case N_RBRAC:
4081 // right bracket: 0,,NO_SECT,nesting level,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004082 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
4083 // so that we can always skip the entire symbol if we need to navigate
4084 // more quickly at the source level when parsing STABS
4085 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4086 if ( !N_BRAC_indexes.empty() )
4087 {
4088 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
4089 symbol_ptr->SetByteSize(sym_idx + 1);
4090 symbol_ptr->SetSizeIsSibling(true);
4091 N_BRAC_indexes.pop_back();
4092 }
4093 type = eSymbolTypeScopeEnd;
4094 break;
4095
Charles Davis510938e2013-08-27 05:04:57 +00004096 case N_EXCL:
4097 // deleted include file: name,,NO_SECT,0,sum
Greg Claytonfd814c52013-08-13 01:42:25 +00004098 type = eSymbolTypeHeaderFile;
4099 break;
4100
4101 //----------------------------------------------------------------------
4102 // COMM scopes
4103 //----------------------------------------------------------------------
Charles Davis510938e2013-08-27 05:04:57 +00004104 case N_BCOMM:
4105 // begin common: name,,NO_SECT,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004106 // We use the current number of symbols in the symbol table in lieu of
4107 // using nlist_idx in case we ever start trimming entries out
4108 type = eSymbolTypeScopeBegin;
4109 N_COMM_indexes.push_back(sym_idx);
4110 break;
4111
Charles Davis510938e2013-08-27 05:04:57 +00004112 case N_ECOML:
4113 // end common (local name): 0,,n_sect,0,address
Greg Claytonfd814c52013-08-13 01:42:25 +00004114 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Jason Molenda62e06812016-02-16 04:14:33 +00004115 LLVM_FALLTHROUGH;
Greg Claytonfd814c52013-08-13 01:42:25 +00004116
Charles Davis510938e2013-08-27 05:04:57 +00004117 case N_ECOMM:
4118 // end common: name,,n_sect,0,0
Greg Claytonfd814c52013-08-13 01:42:25 +00004119 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
4120 // so that we can always skip the entire symbol if we need to navigate
4121 // more quickly at the source level when parsing STABS
4122 if ( !N_COMM_indexes.empty() )
4123 {
4124 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
4125 symbol_ptr->SetByteSize(sym_idx + 1);
4126 symbol_ptr->SetSizeIsSibling(true);
4127 N_COMM_indexes.pop_back();
4128 }
4129 type = eSymbolTypeScopeEnd;
4130 break;
4131
Charles Davis510938e2013-08-27 05:04:57 +00004132 case N_LENG:
4133 // second stab entry with length information
Greg Claytonfd814c52013-08-13 01:42:25 +00004134 type = eSymbolTypeAdditional;
4135 break;
4136
4137 default: break;
4138 }
4139 }
4140 else
4141 {
Charles Davis510938e2013-08-27 05:04:57 +00004142 //uint8_t n_pext = N_PEXT & nlist.n_type;
4143 uint8_t n_type = N_TYPE & nlist.n_type;
4144 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
Greg Claytonfd814c52013-08-13 01:42:25 +00004145
4146 switch (n_type)
4147 {
Greg Clayton60038be2015-02-14 00:51:13 +00004148 case N_INDR:
4149 {
4150 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
4151 if (reexport_name_cstr && reexport_name_cstr[0])
4152 {
4153 type = eSymbolTypeReExported;
4154 ConstString reexport_name(reexport_name_cstr + ((reexport_name_cstr[0] == '_') ? 1 : 0));
4155 sym[sym_idx].SetReExportedSymbolName(reexport_name);
4156 set_value = false;
4157 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
4158 indirect_symbol_names.insert(ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0)));
4159 }
4160 else
4161 type = eSymbolTypeUndefined;
4162 }
4163 break;
4164
Charles Davis510938e2013-08-27 05:04:57 +00004165 case N_UNDF:
Greg Clayton786ad182015-03-03 01:40:46 +00004166 if (symbol_name && symbol_name[0])
Greg Clayton60038be2015-02-14 00:51:13 +00004167 {
4168 ConstString undefined_name(symbol_name + ((symbol_name[0] == '_') ? 1 : 0));
4169 undefined_name_to_desc[undefined_name] = nlist.n_desc;
4170 }
Jason Molenda62e06812016-02-16 04:14:33 +00004171 LLVM_FALLTHROUGH;
4172
Greg Clayton60038be2015-02-14 00:51:13 +00004173 case N_PBUD:
Greg Claytonfd814c52013-08-13 01:42:25 +00004174 type = eSymbolTypeUndefined;
4175 break;
4176
Charles Davis510938e2013-08-27 05:04:57 +00004177 case N_ABS:
Greg Claytonfd814c52013-08-13 01:42:25 +00004178 type = eSymbolTypeAbsolute;
4179 break;
4180
Charles Davis510938e2013-08-27 05:04:57 +00004181 case N_SECT:
Greg Claytonfd814c52013-08-13 01:42:25 +00004182 {
4183 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
4184
4185 if (!symbol_section)
4186 {
4187 // TODO: warn about this?
4188 add_nlist = false;
4189 break;
4190 }
4191
4192 if (TEXT_eh_frame_sectID == nlist.n_sect)
4193 {
4194 type = eSymbolTypeException;
4195 }
4196 else
4197 {
Charles Davis510938e2013-08-27 05:04:57 +00004198 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
Greg Claytonfd814c52013-08-13 01:42:25 +00004199
4200 switch (section_type)
4201 {
Charles Davis510938e2013-08-27 05:04:57 +00004202 case S_CSTRING_LITERALS: type = eSymbolTypeData; break; // section with only literal C strings
4203 case S_4BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 4 byte literals
4204 case S_8BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 8 byte literals
4205 case S_LITERAL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
4206 case S_NON_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
4207 case S_LAZY_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
4208 case S_SYMBOL_STUBS: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
4209 case S_MOD_INIT_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for initialization
4210 case S_MOD_TERM_FUNC_POINTERS: type = eSymbolTypeCode; break; // section with only function pointers for termination
Charles Davis510938e2013-08-27 05:04:57 +00004211 case S_INTERPOSING: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
4212 case S_16BYTE_LITERALS: type = eSymbolTypeData; break; // section with only 16 byte literals
4213 case S_DTRACE_DOF: type = eSymbolTypeInstrumentation; break;
4214 case S_LAZY_DYLIB_SYMBOL_POINTERS: type = eSymbolTypeTrampoline; break;
Greg Clayton38f9cc42014-06-16 22:53:16 +00004215 default:
4216 switch (symbol_section->GetType())
4217 {
4218 case lldb::eSectionTypeCode:
4219 type = eSymbolTypeCode;
4220 break;
4221 case eSectionTypeData:
4222 case eSectionTypeDataCString: // Inlined C string data
4223 case eSectionTypeDataCStringPointers: // Pointers to C string data
4224 case eSectionTypeDataSymbolAddress: // Address of a symbol in the symbol table
4225 case eSectionTypeData4:
4226 case eSectionTypeData8:
4227 case eSectionTypeData16:
Greg Clayton38f9cc42014-06-16 22:53:16 +00004228 type = eSymbolTypeData;
4229 break;
4230 default:
4231 break;
4232 }
4233 break;
Greg Claytonfd814c52013-08-13 01:42:25 +00004234 }
4235
4236 if (type == eSymbolTypeInvalid)
4237 {
4238 const char *symbol_sect_name = symbol_section->GetName().AsCString();
4239 if (symbol_section->IsDescendant (text_section_sp.get()))
4240 {
Charles Davis510938e2013-08-27 05:04:57 +00004241 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
4242 S_ATTR_SELF_MODIFYING_CODE |
4243 S_ATTR_SOME_INSTRUCTIONS))
Greg Claytonfd814c52013-08-13 01:42:25 +00004244 type = eSymbolTypeData;
4245 else
4246 type = eSymbolTypeCode;
4247 }
4248 else
Greg Claytona381e102015-07-27 23:21:05 +00004249 if (symbol_section->IsDescendant(data_section_sp.get()) ||
4250 symbol_section->IsDescendant(data_dirty_section_sp.get()) ||
4251 symbol_section->IsDescendant(data_const_section_sp.get()))
Greg Claytonfd814c52013-08-13 01:42:25 +00004252 {
4253 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
4254 {
4255 type = eSymbolTypeRuntime;
4256
Jason Molenda649a6072015-11-10 05:21:54 +00004257 if (symbol_name)
Greg Claytonfd814c52013-08-13 01:42:25 +00004258 {
4259 llvm::StringRef symbol_name_ref(symbol_name);
Jason Molenda649a6072015-11-10 05:21:54 +00004260 if (symbol_name_ref.startswith("_OBJC_"))
Greg Claytonfd814c52013-08-13 01:42:25 +00004261 {
Jason Molenda649a6072015-11-10 05:21:54 +00004262 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
4263 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
4264 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
4265 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
4266 {
4267 symbol_name_non_abi_mangled = symbol_name + 1;
4268 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
4269 type = eSymbolTypeObjCClass;
4270 demangled_is_synthesized = true;
4271 }
4272 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
4273 {
4274 symbol_name_non_abi_mangled = symbol_name + 1;
4275 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
4276 type = eSymbolTypeObjCMetaClass;
4277 demangled_is_synthesized = true;
4278 }
4279 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
4280 {
4281 symbol_name_non_abi_mangled = symbol_name + 1;
4282 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
4283 type = eSymbolTypeObjCIVar;
4284 demangled_is_synthesized = true;
4285 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004286 }
4287 }
4288 }
4289 else
4290 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
4291 {
4292 type = eSymbolTypeException;
4293 }
4294 else
4295 {
4296 type = eSymbolTypeData;
4297 }
4298 }
4299 else
4300 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
4301 {
4302 type = eSymbolTypeTrampoline;
4303 }
4304 else
4305 if (symbol_section->IsDescendant(objc_section_sp.get()))
4306 {
4307 type = eSymbolTypeRuntime;
4308 if (symbol_name && symbol_name[0] == '.')
4309 {
4310 llvm::StringRef symbol_name_ref(symbol_name);
4311 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
4312 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
4313 {
4314 symbol_name_non_abi_mangled = symbol_name;
4315 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
4316 type = eSymbolTypeObjCClass;
4317 demangled_is_synthesized = true;
4318 }
4319 }
4320 }
4321 }
4322 }
4323 }
4324 break;
Greg Claytondacc4a92013-05-14 22:19:37 +00004325 }
4326 }
4327
Greg Claytonfd814c52013-08-13 01:42:25 +00004328 if (add_nlist)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004329 {
Greg Claytonfd814c52013-08-13 01:42:25 +00004330 uint64_t symbol_value = nlist.n_value;
4331
4332 if (symbol_name_non_abi_mangled)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004333 {
Greg Claytonfd814c52013-08-13 01:42:25 +00004334 sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled));
4335 sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name));
4336 }
4337 else
4338 {
4339 bool symbol_name_is_mangled = false;
4340
4341 if (symbol_name && symbol_name[0] == '_')
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004342 {
Greg Claytonfd814c52013-08-13 01:42:25 +00004343 symbol_name_is_mangled = symbol_name[1] == '_';
4344 symbol_name++; // Skip the leading underscore
4345 }
4346
4347 if (symbol_name)
4348 {
4349 ConstString const_symbol_name(symbol_name);
4350 sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled);
Greg Claytonfd814c52013-08-13 01:42:25 +00004351 }
4352 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00004353
4354 if (is_gsym)
Greg Clayton14cd13c2015-07-01 23:29:06 +00004355 {
Greg Claytonddaf6a72015-07-08 22:32:23 +00004356 const char *gsym_name = sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled).GetCString();
Greg Clayton14cd13c2015-07-01 23:29:06 +00004357 if (gsym_name)
4358 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
4359 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00004360
Greg Claytonfd814c52013-08-13 01:42:25 +00004361 if (symbol_section)
4362 {
4363 const addr_t section_file_addr = symbol_section->GetFileAddress();
4364 if (symbol_byte_size == 0 && function_starts_count > 0)
4365 {
4366 addr_t symbol_lookup_file_addr = nlist.n_value;
4367 // Do an exact address match for non-ARM addresses, else get the closest since
4368 // the symbol might be a thumb symbol which has an address with bit zero set
4369 FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm);
4370 if (is_arm && func_start_entry)
4371 {
4372 // Verify that the function start address is the symbol address (ARM)
4373 // or the symbol address + 1 (thumb)
4374 if (func_start_entry->addr != symbol_lookup_file_addr &&
4375 func_start_entry->addr != (symbol_lookup_file_addr + 1))
4376 {
4377 // Not the right entry, NULL it out...
4378 func_start_entry = NULL;
4379 }
4380 }
4381 if (func_start_entry)
4382 {
4383 func_start_entry->data = true;
4384
4385 addr_t symbol_file_addr = func_start_entry->addr;
4386 if (is_arm)
Greg Claytonb887da12015-07-16 19:50:57 +00004387 symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Greg Claytonfd814c52013-08-13 01:42:25 +00004388
4389 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
4390 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
4391 if (next_func_start_entry)
4392 {
4393 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4394 // Be sure the clear the Thumb address bit when we calculate the size
4395 // from the current and next address
4396 if (is_arm)
Greg Claytonb887da12015-07-16 19:50:57 +00004397 next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Greg Claytonfd814c52013-08-13 01:42:25 +00004398 symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr);
4399 }
4400 else
4401 {
4402 symbol_byte_size = section_end_file_addr - symbol_file_addr;
4403 }
4404 }
4405 }
4406 symbol_value -= section_file_addr;
4407 }
4408
4409 if (is_debug == false)
4410 {
4411 if (type == eSymbolTypeCode)
4412 {
4413 // See if we can find a N_FUN entry for any code symbols.
4414 // If we do find a match, and the name matches, then we
4415 // can merge the two into just the function symbol to avoid
4416 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00004417 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
4418 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
4419 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00004420 {
Greg Claytond81088c2014-01-16 01:38:29 +00004421 bool found_it = false;
4422 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00004423 {
Greg Claytonddaf6a72015-07-08 22:32:23 +00004424 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 +00004425 {
4426 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4427 // We just need the flags from the linker symbol, so put these flags
4428 // into the N_FUN flags to avoid duplicate symbols in the symbol table
4429 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4430 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4431 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
4432 sym[pos->second].SetType (eSymbolTypeResolver);
4433 sym[sym_idx].Clear();
4434 found_it = true;
4435 break;
4436 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004437 }
Greg Claytond81088c2014-01-16 01:38:29 +00004438 if (found_it)
4439 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00004440 }
Jim Inghamea3ac272014-01-10 22:55:37 +00004441 else
4442 {
4443 if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
Greg Claytonbaf2c222014-01-16 01:48:44 +00004444 type = eSymbolTypeResolver;
Jim Inghamea3ac272014-01-10 22:55:37 +00004445 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004446 }
Greg Clayton1e28adf2015-02-25 17:25:02 +00004447 else if (type == eSymbolTypeData ||
4448 type == eSymbolTypeObjCClass ||
4449 type == eSymbolTypeObjCMetaClass ||
4450 type == eSymbolTypeObjCIVar )
Greg Claytonfd814c52013-08-13 01:42:25 +00004451 {
4452 // See if we can find a N_STSYM entry for any data symbols.
4453 // If we do find a match, and the name matches, then we
4454 // can merge the two into just the Static symbol to avoid
4455 // duplicate entries in the symbol table
Greg Claytond81088c2014-01-16 01:38:29 +00004456 std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
4457 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
4458 if (range.first != range.second)
Greg Claytonfd814c52013-08-13 01:42:25 +00004459 {
Greg Claytond81088c2014-01-16 01:38:29 +00004460 bool found_it = false;
4461 for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
Greg Claytonfd814c52013-08-13 01:42:25 +00004462 {
Greg Claytonddaf6a72015-07-08 22:32:23 +00004463 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 +00004464 {
4465 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4466 // We just need the flags from the linker symbol, so put these flags
4467 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
4468 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4469 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4470 sym[sym_idx].Clear();
4471 found_it = true;
4472 break;
4473 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004474 }
Greg Claytond81088c2014-01-16 01:38:29 +00004475 if (found_it)
4476 continue;
Greg Claytonfd814c52013-08-13 01:42:25 +00004477 }
4478 else
4479 {
4480 // Combine N_GSYM stab entries with the non stab symbol
Greg Claytonddaf6a72015-07-08 22:32:23 +00004481 const char *gsym_name = sym[sym_idx].GetMangled().GetName(lldb::eLanguageTypeUnknown, Mangled::ePreferMangled).GetCString();
Greg Clayton14cd13c2015-07-01 23:29:06 +00004482 if (gsym_name)
Greg Claytonfd814c52013-08-13 01:42:25 +00004483 {
Greg Clayton14cd13c2015-07-01 23:29:06 +00004484 ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(gsym_name);
4485 if (pos != N_GSYM_name_to_sym_idx.end())
4486 {
4487 const uint32_t GSYM_sym_idx = pos->second;
4488 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
4489 // Copy the address, because often the N_GSYM address has an invalid address of zero
4490 // when the global is a common symbol
4491 sym[GSYM_sym_idx].GetAddressRef().SetSection (symbol_section);
4492 sym[GSYM_sym_idx].GetAddressRef().SetOffset (symbol_value);
4493 // We just need the flags from the linker symbol, so put these flags
4494 // into the N_GSYM flags to avoid duplicate symbols in the symbol table
4495 sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4496 sym[sym_idx].Clear();
4497 continue;
4498 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004499 }
4500 }
4501 }
4502 }
4503
4504 sym[sym_idx].SetID (nlist_idx);
4505 sym[sym_idx].SetType (type);
Greg Clayton60038be2015-02-14 00:51:13 +00004506 if (set_value)
4507 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00004508 sym[sym_idx].GetAddressRef().SetSection (symbol_section);
4509 sym[sym_idx].GetAddressRef().SetOffset (symbol_value);
Greg Clayton60038be2015-02-14 00:51:13 +00004510 }
Greg Claytonfd814c52013-08-13 01:42:25 +00004511 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
4512
4513 if (symbol_byte_size > 0)
4514 sym[sym_idx].SetByteSize(symbol_byte_size);
4515
4516 if (demangled_is_synthesized)
4517 sym[sym_idx].SetDemangledNameIsSynthesized(true);
4518
4519 ++sym_idx;
4520 }
4521 else
4522 {
4523 sym[sym_idx].Clear();
4524 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004525 }
Greg Clayton60038be2015-02-14 00:51:13 +00004526
4527 for (const auto &pos :reexport_shlib_needs_fixup)
4528 {
4529 const auto undef_pos = undefined_name_to_desc.find(pos.second);
4530 if (undef_pos != undefined_name_to_desc.end())
4531 {
4532 const uint8_t dylib_ordinal = llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
4533 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
4534 sym[pos.first].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(dylib_ordinal-1));
4535 }
4536 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004537 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004538
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004539 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
4540
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004541 if (function_starts_count > 0)
4542 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004543 uint32_t num_synthetic_function_symbols = 0;
4544 for (i=0; i<function_starts_count; ++i)
4545 {
4546 if (function_starts.GetEntryRef (i).data == false)
4547 ++num_synthetic_function_symbols;
4548 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004549
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004550 if (num_synthetic_function_symbols > 0)
4551 {
4552 if (num_syms < sym_idx + num_synthetic_function_symbols)
4553 {
4554 num_syms = sym_idx + num_synthetic_function_symbols;
4555 sym = symtab->Resize (num_syms);
4556 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004557 for (i=0; i<function_starts_count; ++i)
4558 {
4559 const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i);
4560 if (func_start_entry->data == false)
4561 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004562 addr_t symbol_file_addr = func_start_entry->addr;
4563 uint32_t symbol_flags = 0;
4564 if (is_arm)
4565 {
4566 if (symbol_file_addr & 1)
4567 symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB;
Greg Claytonb887da12015-07-16 19:50:57 +00004568 symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004569 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004570 Address symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004571 if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004572 {
4573 SectionSP symbol_section (symbol_addr.GetSection());
4574 uint32_t symbol_byte_size = 0;
4575 if (symbol_section)
4576 {
4577 const addr_t section_file_addr = symbol_section->GetFileAddress();
4578 const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry);
4579 const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize();
4580 if (next_func_start_entry)
4581 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004582 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4583 if (is_arm)
Greg Claytonb887da12015-07-16 19:50:57 +00004584 next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004585 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 +00004586 }
4587 else
4588 {
Greg Clayton29e08cb2012-03-14 01:53:24 +00004589 symbol_byte_size = section_end_file_addr - symbol_file_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004590 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004591 sym[sym_idx].SetID (synthetic_sym_id++);
Tamas Berghammer6b63b142016-02-18 11:12:18 +00004592 sym[sym_idx].GetMangled().SetDemangledName(GetNextSyntheticSymbolName());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004593 sym[sym_idx].SetType (eSymbolTypeCode);
4594 sym[sym_idx].SetIsSynthetic (true);
Greg Clayton358cf1e2015-06-25 21:46:34 +00004595 sym[sym_idx].GetAddressRef() = symbol_addr;
Greg Clayton29e08cb2012-03-14 01:53:24 +00004596 if (symbol_flags)
4597 sym[sym_idx].SetFlags (symbol_flags);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004598 if (symbol_byte_size)
4599 sym[sym_idx].SetByteSize (symbol_byte_size);
4600 ++sym_idx;
4601 }
4602 }
4603 }
4604 }
4605 }
4606 }
4607
4608 // Trim our symbols down to just what we ended up with after
4609 // removing any symbols.
4610 if (sym_idx < num_syms)
4611 {
4612 num_syms = sym_idx;
4613 sym = symtab->Resize (num_syms);
4614 }
4615
4616 // Now synthesize indirect symbols
4617 if (m_dysymtab.nindirectsyms != 0)
4618 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004619 if (indirect_symbol_index_data.GetByteSize())
4620 {
4621 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
4622
4623 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
4624 {
Charles Davis510938e2013-08-27 05:04:57 +00004625 if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) == S_SYMBOL_STUBS)
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004626 {
4627 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
4628 if (symbol_stub_byte_size == 0)
4629 continue;
4630
4631 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
4632
4633 if (num_symbol_stubs == 0)
4634 continue;
4635
4636 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
4637 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
4638 {
4639 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
4640 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 +00004641 lldb::offset_t symbol_stub_offset = symbol_stub_index * 4;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004642 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
4643 {
4644 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Charles Davis510938e2013-08-27 05:04:57 +00004645 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004646 continue;
4647
4648 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
4649 Symbol *stub_symbol = NULL;
4650 if (index_pos != end_index_pos)
4651 {
4652 // We have a remapping from the original nlist index to
4653 // a current symbol index, so just look this up by index
4654 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
4655 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00004656 else
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004657 {
4658 // We need to lookup a symbol using the original nlist
Jason Molenda4e7511e2013-03-06 23:19:17 +00004659 // symbol index since this index is coming from the
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004660 // S_SYMBOL_STUBS
4661 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
4662 }
4663
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004664 if (stub_symbol)
4665 {
4666 Address so_addr(symbol_stub_addr, section_list);
4667
4668 if (stub_symbol->GetType() == eSymbolTypeUndefined)
4669 {
4670 // Change the external symbol into a trampoline that makes sense
4671 // These symbols were N_UNDF N_EXT, and are useless to us, so we
4672 // can re-use them so we don't have to make up a synthetic symbol
4673 // for no good reason.
Greg Clayton9191db42013-10-21 18:40:51 +00004674 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4675 stub_symbol->SetType (eSymbolTypeTrampoline);
4676 else
4677 stub_symbol->SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004678 stub_symbol->SetExternal (false);
Greg Clayton358cf1e2015-06-25 21:46:34 +00004679 stub_symbol->GetAddressRef() = so_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004680 stub_symbol->SetByteSize (symbol_stub_byte_size);
4681 }
4682 else
4683 {
4684 // Make a synthetic symbol to describe the trampoline stub
Jason Molenda0a287e02012-04-24 02:09:58 +00004685 Mangled stub_symbol_mangled_name(stub_symbol->GetMangled());
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004686 if (sym_idx >= num_syms)
Jason Molenda0a287e02012-04-24 02:09:58 +00004687 {
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004688 sym = symtab->Resize (++num_syms);
Jason Molenda0a287e02012-04-24 02:09:58 +00004689 stub_symbol = NULL; // this pointer no longer valid
4690 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004691 sym[sym_idx].SetID (synthetic_sym_id++);
Jason Molenda0a287e02012-04-24 02:09:58 +00004692 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
Greg Clayton9191db42013-10-21 18:40:51 +00004693 if (resolver_addresses.find(symbol_stub_addr) == resolver_addresses.end())
4694 sym[sym_idx].SetType (eSymbolTypeTrampoline);
4695 else
4696 sym[sym_idx].SetType (eSymbolTypeResolver);
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004697 sym[sym_idx].SetIsSynthetic (true);
Greg Clayton358cf1e2015-06-25 21:46:34 +00004698 sym[sym_idx].GetAddressRef() = so_addr;
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004699 sym[sym_idx].SetByteSize (symbol_stub_byte_size);
4700 ++sym_idx;
4701 }
4702 }
Greg Clayton3f839a32012-09-05 01:38:55 +00004703 else
4704 {
4705 if (log)
4706 log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id);
4707 }
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004708 }
4709 }
4710 }
4711 }
4712 }
4713 }
Greg Clayton9191db42013-10-21 18:40:51 +00004714
Greg Clayton9191db42013-10-21 18:40:51 +00004715 if (!trie_entries.empty())
4716 {
4717 for (const auto &e : trie_entries)
4718 {
4719 if (e.entry.import_name)
4720 {
Greg Clayton60038be2015-02-14 00:51:13 +00004721 // Only add indirect symbols from the Trie entries if we
4722 // didn't have a N_INDR nlist entry for this already
4723 if (indirect_symbol_names.find(e.entry.name) == indirect_symbol_names.end())
Greg Clayton9191db42013-10-21 18:40:51 +00004724 {
Greg Clayton60038be2015-02-14 00:51:13 +00004725 // Make a synthetic symbol to describe re-exported symbol.
4726 if (sym_idx >= num_syms)
4727 sym = symtab->Resize (++num_syms);
4728 sym[sym_idx].SetID (synthetic_sym_id++);
4729 sym[sym_idx].GetMangled() = Mangled(e.entry.name);
4730 sym[sym_idx].SetType (eSymbolTypeReExported);
4731 sym[sym_idx].SetIsSynthetic (true);
4732 sym[sym_idx].SetReExportedSymbolName(e.entry.import_name);
4733 if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize())
4734 {
4735 sym[sym_idx].SetReExportedSymbolSharedLibrary(dylib_files.GetFileSpecAtIndex(e.entry.other-1));
4736 }
4737 ++sym_idx;
Greg Clayton9191db42013-10-21 18:40:51 +00004738 }
Greg Clayton9191db42013-10-21 18:40:51 +00004739 }
4740 }
4741 }
Eugene Zelenko8157a882015-10-23 16:56:07 +00004742
Greg Clayton3046e662013-07-10 01:23:25 +00004743// StreamFile s(stdout, false);
4744// s.Printf ("Symbol table before CalculateSymbolSizes():\n");
4745// symtab->Dump(&s, NULL, eSortOrderNone);
4746 // Set symbol byte sizes correctly since mach-o nlist entries don't have sizes
4747 symtab->CalculateSymbolSizes();
4748
4749// s.Printf ("Symbol table after CalculateSymbolSizes():\n");
4750// symtab->Dump(&s, NULL, eSortOrderNone);
4751
Greg Claytonf3bb3e42012-03-09 04:26:05 +00004752 return symtab->GetNumSymbols();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004753 }
4754 return 0;
4755}
4756
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004757void
4758ObjectFileMachO::Dump (Stream *s)
4759{
Greg Claytona1743492012-03-13 23:14:29 +00004760 ModuleSP module_sp(GetModule());
4761 if (module_sp)
4762 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00004763 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004764 s->Printf("%p: ", static_cast<void*>(this));
Greg Claytona1743492012-03-13 23:14:29 +00004765 s->Indent();
Charles Davis510938e2013-08-27 05:04:57 +00004766 if (m_header.magic == MH_MAGIC_64 || m_header.magic == MH_CIGAM_64)
Greg Claytona1743492012-03-13 23:14:29 +00004767 s->PutCString("ObjectFileMachO64");
4768 else
4769 s->PutCString("ObjectFileMachO32");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004770
Greg Clayton7ab7f892014-05-29 21:33:45 +00004771 ArchSpec header_arch;
4772 GetArchitecture(header_arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004773
Greg Claytona1743492012-03-13 23:14:29 +00004774 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004775
Greg Clayton3046e662013-07-10 01:23:25 +00004776 SectionList *sections = GetSectionList();
4777 if (sections)
4778 sections->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004779
Greg Claytona1743492012-03-13 23:14:29 +00004780 if (m_symtab_ap.get())
4781 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
4782 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004783}
4784
Greg Claytonf4d6de62013-04-24 22:29:28 +00004785bool
4786ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header,
4787 const lldb_private::DataExtractor &data,
4788 lldb::offset_t lc_offset,
4789 lldb_private::UUID& uuid)
4790{
4791 uint32_t i;
4792 struct uuid_command load_cmd;
4793
4794 lldb::offset_t offset = lc_offset;
4795 for (i=0; i<header.ncmds; ++i)
4796 {
4797 const lldb::offset_t cmd_offset = offset;
4798 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4799 break;
4800
Charles Davis510938e2013-08-27 05:04:57 +00004801 if (load_cmd.cmd == LC_UUID)
Greg Claytonf4d6de62013-04-24 22:29:28 +00004802 {
4803 const uint8_t *uuid_bytes = data.PeekData(offset, 16);
4804
4805 if (uuid_bytes)
4806 {
4807 // OpenCL on Mac OS X uses the same UUID for each of its object files.
4808 // We pretend these object files have no UUID to prevent crashing.
4809
4810 const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b,
4811 0x3b, 0xa8,
4812 0x4b, 0x16,
4813 0xb6, 0xa4,
4814 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d };
4815
4816 if (!memcmp(uuid_bytes, opencl_uuid, 16))
4817 return false;
4818
4819 uuid.SetBytes (uuid_bytes);
4820 return true;
4821 }
4822 return false;
4823 }
4824 offset = cmd_offset + load_cmd.cmdsize;
4825 }
4826 return false;
4827}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004828
Greg Clayton7ab7f892014-05-29 21:33:45 +00004829bool
4830ObjectFileMachO::GetArchitecture (const llvm::MachO::mach_header &header,
4831 const lldb_private::DataExtractor &data,
4832 lldb::offset_t lc_offset,
4833 ArchSpec &arch)
4834{
4835 arch.SetArchitecture (eArchTypeMachO, header.cputype, header.cpusubtype);
4836
4837 if (arch.IsValid())
4838 {
4839 llvm::Triple &triple = arch.GetTriple();
Greg Claytona3706882015-10-28 23:26:59 +00004840
4841 // Set OS to an unspecified unknown or a "*" so it can match any OS
4842 triple.setOS(llvm::Triple::UnknownOS);
4843 triple.setOSName(llvm::StringRef());
4844
Greg Clayton7ab7f892014-05-29 21:33:45 +00004845 if (header.filetype == MH_PRELOAD)
4846 {
Jason Molenda1a75b0d2016-01-21 04:38:05 +00004847 if (header.cputype == CPU_TYPE_ARM)
4848 {
4849 // If this is a 32-bit arm binary, and it's a standalone binary,
4850 // force the Vendor to Apple so we don't accidentally pick up
4851 // the generic armv7 ABI at runtime. Apple's armv7 ABI always uses
4852 // r7 for the frame pointer register; most other armv7 ABIs use a
4853 // combination of r7 and r11.
4854 triple.setVendor(llvm::Triple::Apple);
4855 }
4856 else
4857 {
4858 // Set vendor to an unspecified unknown or a "*" so it can match any vendor
4859 // This is required for correct behavior of EFI debugging on x86_64
4860 triple.setVendor(llvm::Triple::UnknownVendor);
4861 triple.setVendorName(llvm::StringRef());
4862 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00004863 return true;
4864 }
4865 else
4866 {
4867 struct load_command load_cmd;
Greg Claytona3706882015-10-28 23:26:59 +00004868
Greg Clayton7ab7f892014-05-29 21:33:45 +00004869 lldb::offset_t offset = lc_offset;
4870 for (uint32_t i=0; i<header.ncmds; ++i)
4871 {
4872 const lldb::offset_t cmd_offset = offset;
4873 if (data.GetU32(&offset, &load_cmd, 2) == NULL)
4874 break;
4875
4876 switch (load_cmd.cmd)
4877 {
Jason Molenda649a6072015-11-10 05:21:54 +00004878 case llvm::MachO::LC_VERSION_MIN_IPHONEOS:
Greg Clayton7ab7f892014-05-29 21:33:45 +00004879 triple.setOS (llvm::Triple::IOS);
4880 return true;
4881
Jason Molenda649a6072015-11-10 05:21:54 +00004882 case llvm::MachO::LC_VERSION_MIN_MACOSX:
Greg Clayton7ab7f892014-05-29 21:33:45 +00004883 triple.setOS (llvm::Triple::MacOSX);
4884 return true;
Jason Molenda649a6072015-11-10 05:21:54 +00004885
4886 case llvm::MachO::LC_VERSION_MIN_TVOS:
4887 triple.setOS (llvm::Triple::TvOS);
4888 return true;
Greg Clayton7ab7f892014-05-29 21:33:45 +00004889
Jason Molenda649a6072015-11-10 05:21:54 +00004890 case llvm::MachO::LC_VERSION_MIN_WATCHOS:
4891 triple.setOS (llvm::Triple::WatchOS);
4892 return true;
4893
Greg Clayton7ab7f892014-05-29 21:33:45 +00004894 default:
4895 break;
4896 }
4897
4898 offset = cmd_offset + load_cmd.cmdsize;
4899 }
4900
Greg Claytona3706882015-10-28 23:26:59 +00004901 if (header.filetype != MH_KEXT_BUNDLE)
4902 {
4903 // We didn't find a LC_VERSION_MIN load command and this isn't a KEXT
4904 // so lets not say our Vendor is Apple, leave it as an unspecified unknown
4905 triple.setVendor(llvm::Triple::UnknownVendor);
4906 triple.setVendorName(llvm::StringRef());
4907 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00004908 }
4909 }
4910 return arch.IsValid();
4911}
4912
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004913bool
Greg Clayton60830262011-02-04 18:53:10 +00004914ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004915{
Greg Claytona1743492012-03-13 23:14:29 +00004916 ModuleSP module_sp(GetModule());
4917 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004918 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00004919 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004920 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytonf4d6de62013-04-24 22:29:28 +00004921 return GetUUID (m_header, m_data, offset, *uuid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004922 }
4923 return false;
4924}
4925
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004926uint32_t
4927ObjectFileMachO::GetDependentModules (FileSpecList& files)
4928{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004929 uint32_t count = 0;
Greg Claytona1743492012-03-13 23:14:29 +00004930 ModuleSP module_sp(GetModule());
4931 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004932 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00004933 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Greg Claytona1743492012-03-13 23:14:29 +00004934 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00004935 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Clayton5a271952015-06-02 22:43:29 +00004936 std::vector<std::string> rpath_paths;
4937 std::vector<std::string> rpath_relative_paths;
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00004938 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 +00004939 uint32_t i;
4940 for (i=0; i<m_header.ncmds; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004941 {
Greg Claytona1743492012-03-13 23:14:29 +00004942 const uint32_t cmd_offset = offset;
4943 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
4944 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004945
Greg Claytona1743492012-03-13 23:14:29 +00004946 switch (load_cmd.cmd)
4947 {
Greg Clayton5a271952015-06-02 22:43:29 +00004948 case LC_RPATH:
Charles Davis510938e2013-08-27 05:04:57 +00004949 case LC_LOAD_DYLIB:
4950 case LC_LOAD_WEAK_DYLIB:
4951 case LC_REEXPORT_DYLIB:
4952 case LC_LOAD_DYLINKER:
4953 case LC_LOADFVMLIB:
4954 case LC_LOAD_UPWARD_DYLIB:
Greg Claytona1743492012-03-13 23:14:29 +00004955 {
4956 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
4957 const char *path = m_data.PeekCStr(name_offset);
Greg Clayton5a271952015-06-02 22:43:29 +00004958 if (path)
Greg Claytona1743492012-03-13 23:14:29 +00004959 {
Greg Clayton5a271952015-06-02 22:43:29 +00004960 if (load_cmd.cmd == LC_RPATH)
4961 rpath_paths.push_back(path);
4962 else
4963 {
4964 if (path[0] == '@')
4965 {
4966 if (strncmp(path, "@rpath", strlen("@rpath")) == 0)
4967 rpath_relative_paths.push_back(path + strlen("@rpath"));
4968 }
4969 else
4970 {
4971 FileSpec file_spec(path, resolve_path);
4972 if (files.AppendIfUnique(file_spec))
4973 count++;
4974 }
4975 }
Greg Claytona1743492012-03-13 23:14:29 +00004976 }
4977 }
4978 break;
4979
4980 default:
4981 break;
4982 }
4983 offset = cmd_offset + load_cmd.cmdsize;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004984 }
Greg Clayton5a271952015-06-02 22:43:29 +00004985
4986 if (!rpath_paths.empty())
4987 {
4988 // Fixup all LC_RPATH values to be absolute paths
4989 FileSpec this_file_spec(m_file);
4990 this_file_spec.ResolvePath();
4991 std::string loader_path("@loader_path");
4992 std::string executable_path("@executable_path");
4993 for (auto &rpath : rpath_paths)
4994 {
4995 if (rpath.find(loader_path) == 0)
4996 {
4997 rpath.erase(0, loader_path.size());
4998 rpath.insert(0, this_file_spec.GetDirectory().GetCString());
4999 }
5000 else if (rpath.find(executable_path) == 0)
5001 {
5002 rpath.erase(0, executable_path.size());
5003 rpath.insert(0, this_file_spec.GetDirectory().GetCString());
5004 }
5005 }
5006
5007 for (const auto &rpath_relative_path : rpath_relative_paths)
5008 {
5009 for (const auto &rpath : rpath_paths)
5010 {
5011 std::string path = rpath;
5012 path += rpath_relative_path;
5013 // It is OK to resolve this path because we must find a file on
5014 // disk for us to accept it anyway if it is rpath relative.
5015 FileSpec file_spec(path, true);
5016 // Remove any redundant parts of the path (like "../foo") since
5017 // LC_RPATH values often contain "..".
5018 file_spec.NormalizePath ();
5019 if (file_spec.Exists() && files.AppendIfUnique(file_spec))
5020 {
5021 count++;
5022 break;
5023 }
5024 }
5025 }
5026 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005027 }
5028 return count;
5029}
5030
Jim Ingham672e6f52011-03-07 23:44:08 +00005031lldb_private::Address
Jason Molenda4e7511e2013-03-06 23:19:17 +00005032ObjectFileMachO::GetEntryPointAddress ()
Jim Ingham672e6f52011-03-07 23:44:08 +00005033{
5034 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
5035 // is initialized to an invalid address, so we can just return that.
5036 // 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 +00005037
Jim Ingham672e6f52011-03-07 23:44:08 +00005038 if (!IsExecutable() || m_entry_point_address.IsValid())
5039 return m_entry_point_address;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005040
5041 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
Jim Ingham672e6f52011-03-07 23:44:08 +00005042 // /usr/include/mach-o.h, but it is basically:
5043 //
5044 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
5045 // uint32_t count - this is the count of longs in the thread state data
5046 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
5047 // <repeat this trio>
Jason Molenda4e7511e2013-03-06 23:19:17 +00005048 //
Jim Ingham672e6f52011-03-07 23:44:08 +00005049 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
5050 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
5051 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
5052 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
5053 //
5054 // For now we hard-code the offsets and flavors we need:
5055 //
5056 //
5057
Greg Claytona1743492012-03-13 23:14:29 +00005058 ModuleSP module_sp(GetModule());
5059 if (module_sp)
Jim Ingham672e6f52011-03-07 23:44:08 +00005060 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00005061 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Greg Claytona1743492012-03-13 23:14:29 +00005062 struct load_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00005063 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00005064 uint32_t i;
5065 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
5066 bool done = false;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005067
Greg Claytona1743492012-03-13 23:14:29 +00005068 for (i=0; i<m_header.ncmds; ++i)
Jim Ingham672e6f52011-03-07 23:44:08 +00005069 {
Greg Claytonc7bece562013-01-25 18:06:21 +00005070 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00005071 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
5072 break;
5073
5074 switch (load_cmd.cmd)
Jim Ingham672e6f52011-03-07 23:44:08 +00005075 {
Charles Davis510938e2013-08-27 05:04:57 +00005076 case LC_UNIXTHREAD:
5077 case LC_THREAD:
Jim Ingham672e6f52011-03-07 23:44:08 +00005078 {
Greg Claytona1743492012-03-13 23:14:29 +00005079 while (offset < cmd_offset + load_cmd.cmdsize)
Jim Ingham672e6f52011-03-07 23:44:08 +00005080 {
Greg Claytona1743492012-03-13 23:14:29 +00005081 uint32_t flavor = m_data.GetU32(&offset);
5082 uint32_t count = m_data.GetU32(&offset);
5083 if (count == 0)
5084 {
5085 // We've gotten off somehow, log and exit;
5086 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00005087 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005088
Greg Claytona1743492012-03-13 23:14:29 +00005089 switch (m_header.cputype)
5090 {
Charles Davis510938e2013-08-27 05:04:57 +00005091 case llvm::MachO::CPU_TYPE_ARM:
Jason Molendac197e812016-01-20 05:17:13 +00005092 if (flavor == 1 || flavor == 9) // ARM_THREAD_STATE/ARM_THREAD_STATE32 from mach/arm/thread_status.h
Greg Claytona1743492012-03-13 23:14:29 +00005093 {
5094 offset += 60; // This is the offset of pc in the GPR thread state data structure.
5095 start_address = m_data.GetU32(&offset);
5096 done = true;
5097 }
Jim Ingham672e6f52011-03-07 23:44:08 +00005098 break;
Jason Molendaa3329782014-03-29 18:54:20 +00005099 case llvm::MachO::CPU_TYPE_ARM64:
5100 if (flavor == 6) // ARM_THREAD_STATE64 from mach/arm/thread_status.h
5101 {
5102 offset += 256; // This is the offset of pc in the GPR thread state data structure.
5103 start_address = m_data.GetU64(&offset);
5104 done = true;
5105 }
5106 break;
Charles Davis510938e2013-08-27 05:04:57 +00005107 case llvm::MachO::CPU_TYPE_I386:
Greg Claytona1743492012-03-13 23:14:29 +00005108 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
5109 {
5110 offset += 40; // This is the offset of eip in the GPR thread state data structure.
5111 start_address = m_data.GetU32(&offset);
5112 done = true;
5113 }
5114 break;
Charles Davis510938e2013-08-27 05:04:57 +00005115 case llvm::MachO::CPU_TYPE_X86_64:
Greg Claytona1743492012-03-13 23:14:29 +00005116 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
5117 {
5118 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
5119 start_address = m_data.GetU64(&offset);
5120 done = true;
5121 }
5122 break;
5123 default:
5124 return m_entry_point_address;
5125 }
5126 // Haven't found the GPR flavor yet, skip over the data for this flavor:
5127 if (done)
5128 break;
5129 offset += count * 4;
5130 }
Jim Ingham672e6f52011-03-07 23:44:08 +00005131 }
Greg Claytona1743492012-03-13 23:14:29 +00005132 break;
Charles Davis510938e2013-08-27 05:04:57 +00005133 case LC_MAIN:
Sean Callanan226b70c2012-03-08 02:39:03 +00005134 {
Greg Claytona1743492012-03-13 23:14:29 +00005135 ConstString text_segment_name ("__TEXT");
5136 uint64_t entryoffset = m_data.GetU64(&offset);
5137 SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name);
5138 if (text_segment_sp)
5139 {
5140 done = true;
5141 start_address = text_segment_sp->GetFileAddress() + entryoffset;
5142 }
Sean Callanan226b70c2012-03-08 02:39:03 +00005143 }
Greg Claytoncec91ef2016-02-26 01:20:20 +00005144 break;
Greg Claytona1743492012-03-13 23:14:29 +00005145
5146 default:
5147 break;
Sean Callanan226b70c2012-03-08 02:39:03 +00005148 }
Greg Claytona1743492012-03-13 23:14:29 +00005149 if (done)
5150 break;
Jim Ingham672e6f52011-03-07 23:44:08 +00005151
Greg Claytona1743492012-03-13 23:14:29 +00005152 // Go to the next load command:
5153 offset = cmd_offset + load_cmd.cmdsize;
Jim Ingham672e6f52011-03-07 23:44:08 +00005154 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005155
Greg Claytona1743492012-03-13 23:14:29 +00005156 if (start_address != LLDB_INVALID_ADDRESS)
Greg Claytone72dfb32012-02-24 01:59:29 +00005157 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00005158 // We got the start address from the load commands, so now resolve that address in the sections
Greg Claytona1743492012-03-13 23:14:29 +00005159 // of this ObjectFile:
5160 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
Greg Claytone72dfb32012-02-24 01:59:29 +00005161 {
Greg Claytona1743492012-03-13 23:14:29 +00005162 m_entry_point_address.Clear();
5163 }
5164 }
5165 else
5166 {
5167 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
5168 // "start" symbol in the main executable.
Jason Molenda4e7511e2013-03-06 23:19:17 +00005169
Greg Claytona1743492012-03-13 23:14:29 +00005170 ModuleSP module_sp (GetModule());
Jason Molenda4e7511e2013-03-06 23:19:17 +00005171
Greg Claytona1743492012-03-13 23:14:29 +00005172 if (module_sp)
5173 {
5174 SymbolContextList contexts;
5175 SymbolContext context;
5176 if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
5177 {
5178 if (contexts.GetContextAtIndex(0, context))
5179 m_entry_point_address = context.symbol->GetAddress();
5180 }
Greg Claytone72dfb32012-02-24 01:59:29 +00005181 }
5182 }
Jim Ingham672e6f52011-03-07 23:44:08 +00005183 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005184
Jim Ingham672e6f52011-03-07 23:44:08 +00005185 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00005186}
5187
Greg Claytonc9660542012-02-05 02:38:54 +00005188lldb_private::Address
5189ObjectFileMachO::GetHeaderAddress ()
5190{
5191 lldb_private::Address header_addr;
5192 SectionList *section_list = GetSectionList();
5193 if (section_list)
5194 {
5195 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
5196 if (text_segment_sp)
5197 {
Greg Claytone72dfb32012-02-24 01:59:29 +00005198 header_addr.SetSection (text_segment_sp);
Greg Claytonc9660542012-02-05 02:38:54 +00005199 header_addr.SetOffset (0);
5200 }
5201 }
5202 return header_addr;
5203}
5204
Greg Claytonc3776bf2012-02-09 06:16:32 +00005205uint32_t
5206ObjectFileMachO::GetNumThreadContexts ()
5207{
Greg Claytona1743492012-03-13 23:14:29 +00005208 ModuleSP module_sp(GetModule());
5209 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005210 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00005211 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Greg Claytona1743492012-03-13 23:14:29 +00005212 if (!m_thread_context_offsets_valid)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005213 {
Greg Claytona1743492012-03-13 23:14:29 +00005214 m_thread_context_offsets_valid = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00005215 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00005216 FileRangeArray::Entry file_range;
5217 thread_command thread_cmd;
5218 for (uint32_t i=0; i<m_header.ncmds; ++i)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005219 {
Greg Claytona1743492012-03-13 23:14:29 +00005220 const uint32_t cmd_offset = offset;
5221 if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL)
5222 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005223
Charles Davis510938e2013-08-27 05:04:57 +00005224 if (thread_cmd.cmd == LC_THREAD)
Greg Claytona1743492012-03-13 23:14:29 +00005225 {
5226 file_range.SetRangeBase (offset);
5227 file_range.SetByteSize (thread_cmd.cmdsize - 8);
5228 m_thread_context_offsets.Append (file_range);
5229 }
5230 offset = cmd_offset + thread_cmd.cmdsize;
Greg Claytonc3776bf2012-02-09 06:16:32 +00005231 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00005232 }
5233 }
5234 return m_thread_context_offsets.GetSize();
5235}
5236
5237lldb::RegisterContextSP
5238ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
5239{
Greg Claytonc3776bf2012-02-09 06:16:32 +00005240 lldb::RegisterContextSP reg_ctx_sp;
Greg Claytonc859e2d2012-02-13 23:10:39 +00005241
Greg Claytona1743492012-03-13 23:14:29 +00005242 ModuleSP module_sp(GetModule());
5243 if (module_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +00005244 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00005245 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Greg Claytona1743492012-03-13 23:14:29 +00005246 if (!m_thread_context_offsets_valid)
5247 GetNumThreadContexts ();
5248
5249 const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx);
Jim Ingham28eb5712012-10-12 17:34:26 +00005250 if (thread_context_file_range)
Greg Claytona1743492012-03-13 23:14:29 +00005251 {
Jason Molenda4e7511e2013-03-06 23:19:17 +00005252
5253 DataExtractor data (m_data,
5254 thread_context_file_range->GetRangeBase(),
Jim Ingham28eb5712012-10-12 17:34:26 +00005255 thread_context_file_range->GetByteSize());
5256
5257 switch (m_header.cputype)
5258 {
Jason Molendaa3329782014-03-29 18:54:20 +00005259 case llvm::MachO::CPU_TYPE_ARM64:
5260 reg_ctx_sp.reset (new RegisterContextDarwin_arm64_Mach (thread, data));
5261 break;
5262
Charles Davis510938e2013-08-27 05:04:57 +00005263 case llvm::MachO::CPU_TYPE_ARM:
Jim Ingham28eb5712012-10-12 17:34:26 +00005264 reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data));
5265 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005266
Charles Davis510938e2013-08-27 05:04:57 +00005267 case llvm::MachO::CPU_TYPE_I386:
Jim Ingham28eb5712012-10-12 17:34:26 +00005268 reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data));
5269 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005270
Charles Davis510938e2013-08-27 05:04:57 +00005271 case llvm::MachO::CPU_TYPE_X86_64:
Jim Ingham28eb5712012-10-12 17:34:26 +00005272 reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data));
5273 break;
5274 }
Greg Claytona1743492012-03-13 23:14:29 +00005275 }
Greg Claytonc3776bf2012-02-09 06:16:32 +00005276 }
5277 return reg_ctx_sp;
5278}
5279
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005280ObjectFile::Type
5281ObjectFileMachO::CalculateType()
5282{
5283 switch (m_header.filetype)
5284 {
Charles Davis510938e2013-08-27 05:04:57 +00005285 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005286 if (GetAddressByteSize () == 4)
5287 {
5288 // 32 bit kexts are just object files, but they do have a valid
5289 // UUID load command.
5290 UUID uuid;
5291 if (GetUUID(&uuid))
5292 {
5293 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00005294 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005295 // "OSKextGetCurrentIdentifier" as this is required of kexts
5296 if (m_strata == eStrataInvalid)
5297 m_strata = eStrataKernel;
5298 return eTypeSharedLibrary;
5299 }
5300 }
5301 return eTypeObjectFile;
5302
Charles Davis510938e2013-08-27 05:04:57 +00005303 case MH_EXECUTE: return eTypeExecutable; // 0x2u
5304 case MH_FVMLIB: return eTypeSharedLibrary; // 0x3u
5305 case MH_CORE: return eTypeCoreFile; // 0x4u
5306 case MH_PRELOAD: return eTypeSharedLibrary; // 0x5u
5307 case MH_DYLIB: return eTypeSharedLibrary; // 0x6u
5308 case MH_DYLINKER: return eTypeDynamicLinker; // 0x7u
5309 case MH_BUNDLE: return eTypeSharedLibrary; // 0x8u
5310 case MH_DYLIB_STUB: return eTypeStubLibrary; // 0x9u
5311 case MH_DSYM: return eTypeDebugInfo; // 0xAu
5312 case MH_KEXT_BUNDLE: return eTypeSharedLibrary; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005313 default:
5314 break;
5315 }
5316 return eTypeUnknown;
5317}
5318
5319ObjectFile::Strata
5320ObjectFileMachO::CalculateStrata()
5321{
5322 switch (m_header.filetype)
5323 {
Charles Davis510938e2013-08-27 05:04:57 +00005324 case MH_OBJECT: // 0x1u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005325 {
5326 // 32 bit kexts are just object files, but they do have a valid
5327 // UUID load command.
5328 UUID uuid;
5329 if (GetUUID(&uuid))
5330 {
5331 // this checking for the UUID load command is not enough
Jason Molenda4e7511e2013-03-06 23:19:17 +00005332 // we could eventually look for the symbol named
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005333 // "OSKextGetCurrentIdentifier" as this is required of kexts
5334 if (m_type == eTypeInvalid)
5335 m_type = eTypeSharedLibrary;
5336
5337 return eStrataKernel;
5338 }
5339 }
5340 return eStrataUnknown;
5341
Charles Davis510938e2013-08-27 05:04:57 +00005342 case MH_EXECUTE: // 0x2u
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005343 // Check for the MH_DYLDLINK bit in the flags
Charles Davis510938e2013-08-27 05:04:57 +00005344 if (m_header.flags & MH_DYLDLINK)
Sean Callanan49bce8e2012-02-10 20:22:35 +00005345 {
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005346 return eStrataUser;
Sean Callanan49bce8e2012-02-10 20:22:35 +00005347 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005348 else
Sean Callanan49bce8e2012-02-10 20:22:35 +00005349 {
5350 SectionList *section_list = GetSectionList();
5351 if (section_list)
5352 {
5353 static ConstString g_kld_section_name ("__KLD");
5354 if (section_list->FindSectionByName(g_kld_section_name))
5355 return eStrataKernel;
5356 }
5357 }
5358 return eStrataRawImage;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005359
Charles Davis510938e2013-08-27 05:04:57 +00005360 case MH_FVMLIB: return eStrataUser; // 0x3u
5361 case MH_CORE: return eStrataUnknown; // 0x4u
5362 case MH_PRELOAD: return eStrataRawImage; // 0x5u
5363 case MH_DYLIB: return eStrataUser; // 0x6u
5364 case MH_DYLINKER: return eStrataUser; // 0x7u
5365 case MH_BUNDLE: return eStrataUser; // 0x8u
5366 case MH_DYLIB_STUB: return eStrataUser; // 0x9u
5367 case MH_DSYM: return eStrataUnknown; // 0xAu
5368 case MH_KEXT_BUNDLE: return eStrataKernel; // 0xBu
Greg Clayton9e00b6a652011-07-09 00:41:34 +00005369 default:
5370 break;
5371 }
5372 return eStrataUnknown;
5373}
5374
Greg Claytonc2ff9312012-02-22 19:41:02 +00005375uint32_t
5376ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions)
5377{
Greg Claytona1743492012-03-13 23:14:29 +00005378 ModuleSP module_sp(GetModule());
5379 if (module_sp)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005380 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00005381 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Greg Claytona1743492012-03-13 23:14:29 +00005382 struct dylib_command load_cmd;
Greg Claytonc7bece562013-01-25 18:06:21 +00005383 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
Greg Claytona1743492012-03-13 23:14:29 +00005384 uint32_t version_cmd = 0;
5385 uint64_t version = 0;
5386 uint32_t i;
5387 for (i=0; i<m_header.ncmds; ++i)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005388 {
Greg Claytonc7bece562013-01-25 18:06:21 +00005389 const lldb::offset_t cmd_offset = offset;
Greg Claytona1743492012-03-13 23:14:29 +00005390 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
5391 break;
Jason Molenda4e7511e2013-03-06 23:19:17 +00005392
Charles Davis510938e2013-08-27 05:04:57 +00005393 if (load_cmd.cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005394 {
Greg Claytona1743492012-03-13 23:14:29 +00005395 if (version_cmd == 0)
5396 {
5397 version_cmd = load_cmd.cmd;
5398 if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL)
5399 break;
5400 version = load_cmd.dylib.current_version;
5401 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005402 break; // Break for now unless there is another more complete version
Greg Claytona1743492012-03-13 23:14:29 +00005403 // number load command in the future.
Greg Claytonc2ff9312012-02-22 19:41:02 +00005404 }
Greg Claytona1743492012-03-13 23:14:29 +00005405 offset = cmd_offset + load_cmd.cmdsize;
Greg Claytonc2ff9312012-02-22 19:41:02 +00005406 }
Jason Molenda4e7511e2013-03-06 23:19:17 +00005407
Charles Davis510938e2013-08-27 05:04:57 +00005408 if (version_cmd == LC_ID_DYLIB)
Greg Claytonc2ff9312012-02-22 19:41:02 +00005409 {
Greg Claytona1743492012-03-13 23:14:29 +00005410 if (versions != NULL && num_versions > 0)
5411 {
5412 if (num_versions > 0)
5413 versions[0] = (version & 0xFFFF0000ull) >> 16;
5414 if (num_versions > 1)
5415 versions[1] = (version & 0x0000FF00ull) >> 8;
5416 if (num_versions > 2)
5417 versions[2] = (version & 0x000000FFull);
5418 // Fill in an remaining version numbers with invalid values
5419 for (i=3; i<num_versions; ++i)
5420 versions[i] = UINT32_MAX;
5421 }
5422 // The LC_ID_DYLIB load command has a version with 3 version numbers
5423 // in it, so always return 3
5424 return 3;
Greg Claytonc2ff9312012-02-22 19:41:02 +00005425 }
Greg Claytonc2ff9312012-02-22 19:41:02 +00005426 }
5427 return false;
5428}
5429
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005430bool
Greg Clayton514487e2011-02-15 21:59:32 +00005431ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005432{
Greg Claytona1743492012-03-13 23:14:29 +00005433 ModuleSP module_sp(GetModule());
5434 if (module_sp)
Greg Clayton593577a2011-09-21 03:57:31 +00005435 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00005436 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Greg Clayton7ab7f892014-05-29 21:33:45 +00005437 return GetArchitecture (m_header, m_data, MachHeaderSizeFromMagic(m_header.magic), arch);
Greg Clayton593577a2011-09-21 03:57:31 +00005438 }
Greg Claytona1743492012-03-13 23:14:29 +00005439 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005440}
5441
Jason Molenda0e0954c2013-04-16 06:24:42 +00005442UUID
5443ObjectFileMachO::GetProcessSharedCacheUUID (Process *process)
5444{
5445 UUID uuid;
5446 if (process)
5447 {
5448 addr_t all_image_infos = process->GetImageInfoAddress();
5449
5450 // The address returned by GetImageInfoAddress may be the address of dyld (don't want)
5451 // or it may be the address of the dyld_all_image_infos structure (want). The first four
5452 // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant.
5453 // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field.
5454
5455 Error err;
5456 uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00005457 if (version_or_magic != static_cast<uint32_t>(-1)
Charles Davis510938e2013-08-27 05:04:57 +00005458 && version_or_magic != MH_MAGIC
5459 && version_or_magic != MH_CIGAM
5460 && version_or_magic != MH_MAGIC_64
5461 && version_or_magic != MH_CIGAM_64
Jason Molenda0e0954c2013-04-16 06:24:42 +00005462 && version_or_magic >= 13)
5463 {
5464 addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS;
5465 int wordsize = process->GetAddressByteSize();
5466 if (wordsize == 8)
5467 {
5468 sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h>
5469 }
5470 if (wordsize == 4)
5471 {
5472 sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h>
5473 }
5474 if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS)
5475 {
5476 uuid_t shared_cache_uuid;
5477 if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t))
5478 {
5479 uuid.SetBytes (shared_cache_uuid);
5480 }
5481 }
5482 }
5483 }
5484 return uuid;
5485}
5486
5487UUID
5488ObjectFileMachO::GetLLDBSharedCacheUUID ()
5489{
5490 UUID uuid;
Todd Fiala013434e2014-07-09 01:29:05 +00005491#if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
Jason Molenda0e0954c2013-04-16 06:24:42 +00005492 uint8_t *(*dyld_get_all_image_infos)(void);
5493 dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos");
5494 if (dyld_get_all_image_infos)
5495 {
5496 uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos();
5497 if (dyld_all_image_infos_address)
5498 {
Jason Molendac9cb7d22013-04-16 21:42:58 +00005499 uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h>
5500 if (*version >= 13)
Jason Molenda0e0954c2013-04-16 06:24:42 +00005501 {
Jason Molendaa3329782014-03-29 18:54:20 +00005502 uuid_t *sharedCacheUUID_address = 0;
5503 int wordsize = sizeof (uint8_t *);
5504 if (wordsize == 8)
5505 {
5506 sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 160); // sharedCacheUUID <mach-o/dyld_images.h>
5507 }
5508 else
5509 {
5510 sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84); // sharedCacheUUID <mach-o/dyld_images.h>
5511 }
Jason Molenda0e0954c2013-04-16 06:24:42 +00005512 uuid.SetBytes (sharedCacheUUID_address);
5513 }
5514 }
5515 }
5516#endif
5517 return uuid;
5518}
5519
Greg Clayton9b234982013-10-24 22:54:08 +00005520uint32_t
5521ObjectFileMachO::GetMinimumOSVersion (uint32_t *versions, uint32_t num_versions)
5522{
5523 if (m_min_os_versions.empty())
5524 {
5525 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5526 bool success = false;
5527 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
5528 {
5529 const lldb::offset_t load_cmd_offset = offset;
5530
5531 version_min_command lc;
5532 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
5533 break;
Jason Molenda649a6072015-11-10 05:21:54 +00005534 if (lc.cmd == llvm::MachO::LC_VERSION_MIN_MACOSX
5535 || lc.cmd == llvm::MachO::LC_VERSION_MIN_IPHONEOS
5536 || lc.cmd == llvm::MachO::LC_VERSION_MIN_TVOS
5537 || lc.cmd == llvm::MachO::LC_VERSION_MIN_WATCHOS)
Greg Clayton9b234982013-10-24 22:54:08 +00005538 {
5539 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
5540 {
5541 const uint32_t xxxx = lc.version >> 16;
5542 const uint32_t yy = (lc.version >> 8) & 0xffu;
5543 const uint32_t zz = lc.version & 0xffu;
5544 if (xxxx)
5545 {
5546 m_min_os_versions.push_back(xxxx);
Greg Clayton8031d282015-02-04 00:40:25 +00005547 m_min_os_versions.push_back(yy);
5548 m_min_os_versions.push_back(zz);
Greg Clayton9b234982013-10-24 22:54:08 +00005549 }
5550 success = true;
5551 }
5552 }
5553 offset = load_cmd_offset + lc.cmdsize;
5554 }
5555
5556 if (success == false)
5557 {
5558 // Push an invalid value so we don't keep trying to
5559 m_min_os_versions.push_back(UINT32_MAX);
5560 }
5561 }
5562
5563 if (m_min_os_versions.size() > 1 || m_min_os_versions[0] != UINT32_MAX)
5564 {
5565 if (versions != NULL && num_versions > 0)
5566 {
5567 for (size_t i=0; i<num_versions; ++i)
5568 {
5569 if (i < m_min_os_versions.size())
5570 versions[i] = m_min_os_versions[i];
5571 else
5572 versions[i] = 0;
5573 }
5574 }
5575 return m_min_os_versions.size();
5576 }
5577 // Call the superclasses version that will empty out the data
5578 return ObjectFile::GetMinimumOSVersion (versions, num_versions);
5579}
5580
5581uint32_t
5582ObjectFileMachO::GetSDKVersion(uint32_t *versions, uint32_t num_versions)
5583{
5584 if (m_sdk_versions.empty())
5585 {
5586 lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic);
5587 bool success = false;
5588 for (uint32_t i=0; success == false && i < m_header.ncmds; ++i)
5589 {
5590 const lldb::offset_t load_cmd_offset = offset;
5591
5592 version_min_command lc;
5593 if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL)
5594 break;
Jason Molenda649a6072015-11-10 05:21:54 +00005595 if (lc.cmd == llvm::MachO::LC_VERSION_MIN_MACOSX
5596 || lc.cmd == llvm::MachO::LC_VERSION_MIN_IPHONEOS
5597 || lc.cmd == llvm::MachO::LC_VERSION_MIN_TVOS
5598 || lc.cmd == llvm::MachO::LC_VERSION_MIN_WATCHOS)
Greg Clayton9b234982013-10-24 22:54:08 +00005599 {
5600 if (m_data.GetU32 (&offset, &lc.version, (sizeof(lc) / sizeof(uint32_t)) - 2))
5601 {
Todd Fialaebecb382014-09-04 19:31:52 +00005602 const uint32_t xxxx = lc.sdk >> 16;
5603 const uint32_t yy = (lc.sdk >> 8) & 0xffu;
5604 const uint32_t zz = lc.sdk & 0xffu;
Greg Clayton9b234982013-10-24 22:54:08 +00005605 if (xxxx)
5606 {
5607 m_sdk_versions.push_back(xxxx);
Greg Clayton8031d282015-02-04 00:40:25 +00005608 m_sdk_versions.push_back(yy);
5609 m_sdk_versions.push_back(zz);
Greg Clayton9b234982013-10-24 22:54:08 +00005610 }
5611 success = true;
5612 }
5613 }
5614 offset = load_cmd_offset + lc.cmdsize;
5615 }
5616
5617 if (success == false)
5618 {
5619 // Push an invalid value so we don't keep trying to
5620 m_sdk_versions.push_back(UINT32_MAX);
5621 }
5622 }
5623
5624 if (m_sdk_versions.size() > 1 || m_sdk_versions[0] != UINT32_MAX)
5625 {
5626 if (versions != NULL && num_versions > 0)
5627 {
5628 for (size_t i=0; i<num_versions; ++i)
5629 {
5630 if (i < m_sdk_versions.size())
5631 versions[i] = m_sdk_versions[i];
5632 else
5633 versions[i] = 0;
5634 }
5635 }
5636 return m_sdk_versions.size();
5637 }
5638 // Call the superclasses version that will empty out the data
5639 return ObjectFile::GetSDKVersion (versions, num_versions);
5640}
5641
Greg Clayton08928f32015-02-05 02:01:34 +00005642bool
5643ObjectFileMachO::GetIsDynamicLinkEditor()
5644{
5645 return m_header.filetype == llvm::MachO::MH_DYLINKER;
5646}
5647
Jason Molenda955dcf22016-05-04 03:09:40 +00005648bool
5649ObjectFileMachO::AllowAssemblyEmulationUnwindPlans ()
5650{
5651 return m_allow_assembly_emulation_unwind_plans;
5652}
5653
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005654//------------------------------------------------------------------
5655// PluginInterface protocol
5656//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00005657lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005658ObjectFileMachO::GetPluginName()
5659{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005660 return GetPluginNameStatic();
5661}
5662
5663uint32_t
5664ObjectFileMachO::GetPluginVersion()
5665{
5666 return 1;
5667}
5668
Greg Clayton07347372015-06-08 21:53:11 +00005669Section *
5670ObjectFileMachO::GetMachHeaderSection()
5671{
5672 // Find the first address of the mach header which is the first non-zero
5673 // file sized section whose file offset is zero. This is the base file address
5674 // of the mach-o file which can be subtracted from the vmaddr of the other
5675 // segments found in memory and added to the load address
5676 ModuleSP module_sp = GetModule();
5677 if (module_sp)
5678 {
5679 SectionList *section_list = GetSectionList ();
5680 if (section_list)
5681 {
5682 lldb::addr_t mach_base_file_addr = LLDB_INVALID_ADDRESS;
5683 const size_t num_sections = section_list->GetSize();
5684
5685 for (size_t sect_idx = 0;
5686 sect_idx < num_sections && mach_base_file_addr == LLDB_INVALID_ADDRESS;
5687 ++sect_idx)
5688 {
5689 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
5690 if (section &&
5691 section->GetFileSize() > 0 &&
5692 section->GetFileOffset() == 0 &&
5693 section->IsThreadSpecific() == false &&
5694 module_sp.get() == section->GetModule().get())
5695 {
5696 return section;
5697 }
5698 }
5699 }
5700 }
5701 return nullptr;
5702}
5703
5704lldb::addr_t
5705ObjectFileMachO::CalculateSectionLoadAddressForMemoryImage(lldb::addr_t mach_header_load_address, const Section *mach_header_section, const Section *section)
5706{
5707 ModuleSP module_sp = GetModule();
5708 if (module_sp && mach_header_section && section && mach_header_load_address != LLDB_INVALID_ADDRESS)
5709 {
5710 lldb::addr_t mach_header_file_addr = mach_header_section->GetFileAddress();
5711 if (mach_header_file_addr != LLDB_INVALID_ADDRESS)
5712 {
5713 if (section &&
5714 section->GetFileSize() > 0 &&
5715 section->IsThreadSpecific() == false &&
5716 module_sp.get() == section->GetModule().get())
5717 {
5718 // Ignore __LINKEDIT and __DWARF segments
5719 if (section->GetName() == GetSegmentNameLINKEDIT())
5720 {
5721 // Only map __LINKEDIT if we have an in memory image and this isn't
5722 // a kernel binary like a kext or mach_kernel.
5723 const bool is_memory_image = (bool)m_process_wp.lock();
5724 const Strata strata = GetStrata();
5725 if (is_memory_image == false || strata == eStrataKernel)
5726 return LLDB_INVALID_ADDRESS;
5727 }
5728 return section->GetFileAddress() - mach_header_file_addr + mach_header_load_address;
5729 }
5730 }
5731 }
5732 return LLDB_INVALID_ADDRESS;
5733}
5734
Greg Clayton7524e092014-02-06 20:10:16 +00005735bool
Greg Clayton751caf62014-02-07 22:54:47 +00005736ObjectFileMachO::SetLoadAddress (Target &target,
5737 lldb::addr_t value,
5738 bool value_is_offset)
Greg Clayton7524e092014-02-06 20:10:16 +00005739{
Greg Clayton7524e092014-02-06 20:10:16 +00005740 ModuleSP module_sp = GetModule();
5741 if (module_sp)
5742 {
5743 size_t num_loaded_sections = 0;
5744 SectionList *section_list = GetSectionList ();
5745 if (section_list)
5746 {
Greg Clayton7524e092014-02-06 20:10:16 +00005747 const size_t num_sections = section_list->GetSize();
5748
Greg Clayton751caf62014-02-07 22:54:47 +00005749 if (value_is_offset)
Greg Clayton7524e092014-02-06 20:10:16 +00005750 {
Greg Clayton751caf62014-02-07 22:54:47 +00005751 // "value" is an offset to apply to each top level segment
Greg Clayton7524e092014-02-06 20:10:16 +00005752 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5753 {
5754 // Iterate through the object file sections to find all
5755 // of the sections that size on disk (to avoid __PAGEZERO)
5756 // and load them
5757 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Greg Clayton751caf62014-02-07 22:54:47 +00005758 if (section_sp &&
5759 section_sp->GetFileSize() > 0 &&
5760 section_sp->IsThreadSpecific() == false &&
5761 module_sp.get() == section_sp->GetModule().get())
Greg Clayton7524e092014-02-06 20:10:16 +00005762 {
Greg Clayton751caf62014-02-07 22:54:47 +00005763 // Ignore __LINKEDIT and __DWARF segments
Greg Clayton07347372015-06-08 21:53:11 +00005764 if (section_sp->GetName() == GetSegmentNameLINKEDIT())
Greg Clayton751caf62014-02-07 22:54:47 +00005765 {
5766 // Only map __LINKEDIT if we have an in memory image and this isn't
5767 // a kernel binary like a kext or mach_kernel.
Greg Clayton07347372015-06-08 21:53:11 +00005768 const bool is_memory_image = (bool)m_process_wp.lock();
5769 const Strata strata = GetStrata();
Greg Clayton751caf62014-02-07 22:54:47 +00005770 if (is_memory_image == false || strata == eStrataKernel)
5771 continue;
5772 }
5773 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
Greg Clayton7524e092014-02-06 20:10:16 +00005774 ++num_loaded_sections;
5775 }
5776 }
5777 }
Greg Clayton751caf62014-02-07 22:54:47 +00005778 else
5779 {
5780 // "value" is the new base address of the mach_header, adjust each
5781 // section accordingly
5782
Greg Clayton07347372015-06-08 21:53:11 +00005783 Section *mach_header_section = GetMachHeaderSection();
5784 if (mach_header_section)
Greg Clayton751caf62014-02-07 22:54:47 +00005785 {
5786 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
5787 {
Greg Clayton751caf62014-02-07 22:54:47 +00005788 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
Greg Clayton07347372015-06-08 21:53:11 +00005789
5790 lldb::addr_t section_load_addr = CalculateSectionLoadAddressForMemoryImage(value, mach_header_section, section_sp.get());
5791 if (section_load_addr != LLDB_INVALID_ADDRESS)
Greg Clayton751caf62014-02-07 22:54:47 +00005792 {
Greg Clayton07347372015-06-08 21:53:11 +00005793 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_load_addr))
Greg Clayton751caf62014-02-07 22:54:47 +00005794 ++num_loaded_sections;
5795 }
5796 }
5797 }
5798 }
Greg Clayton7524e092014-02-06 20:10:16 +00005799 }
Greg Clayton7524e092014-02-06 20:10:16 +00005800 return num_loaded_sections > 0;
5801 }
Jason Molenda5cf1e232014-10-16 07:41:32 +00005802 return false;
Greg Clayton7524e092014-02-06 20:10:16 +00005803}
5804
Greg Claytona2715cf2014-06-13 00:54:12 +00005805bool
5806ObjectFileMachO::SaveCore (const lldb::ProcessSP &process_sp,
5807 const FileSpec &outfile,
5808 Error &error)
5809{
5810 if (process_sp)
5811 {
5812 Target &target = process_sp->GetTarget();
5813 const ArchSpec target_arch = target.GetArchitecture();
5814 const llvm::Triple &target_triple = target_arch.GetTriple();
5815 if (target_triple.getVendor() == llvm::Triple::Apple &&
Jason Molenda649a6072015-11-10 05:21:54 +00005816 (target_triple.getOS() == llvm::Triple::MacOSX
5817 || target_triple.getOS() == llvm::Triple::IOS
5818 || target_triple.getOS() == llvm::Triple::WatchOS
5819 || target_triple.getOS() == llvm::Triple::TvOS))
Greg Claytona2715cf2014-06-13 00:54:12 +00005820 {
5821 bool make_core = false;
5822 switch (target_arch.GetMachine())
5823 {
Jason Molenda16dc86d2015-06-25 23:58:25 +00005824 case llvm::Triple::aarch64:
Jason Molenda4b0c1182014-11-12 02:39:14 +00005825 case llvm::Triple::arm:
Jason Molenda6d9fe8c2015-08-21 00:13:37 +00005826 case llvm::Triple::thumb:
Greg Claytona2715cf2014-06-13 00:54:12 +00005827 case llvm::Triple::x86:
5828 case llvm::Triple::x86_64:
5829 make_core = true;
5830 break;
5831 default:
5832 error.SetErrorStringWithFormat ("unsupported core architecture: %s", target_triple.str().c_str());
5833 break;
5834 }
5835
5836 if (make_core)
5837 {
5838 std::vector<segment_command_64> segment_load_commands;
Saleem Abdulrasool3924d752014-06-13 03:30:39 +00005839// uint32_t range_info_idx = 0;
Greg Claytona2715cf2014-06-13 00:54:12 +00005840 MemoryRegionInfo range_info;
5841 Error range_error = process_sp->GetMemoryRegionInfo(0, range_info);
Jason Molendad20359d2014-11-11 10:59:15 +00005842 const uint32_t addr_byte_size = target_arch.GetAddressByteSize();
5843 const ByteOrder byte_order = target_arch.GetByteOrder();
Greg Claytona2715cf2014-06-13 00:54:12 +00005844 if (range_error.Success())
5845 {
5846 while (range_info.GetRange().GetRangeBase() != LLDB_INVALID_ADDRESS)
5847 {
5848 const addr_t addr = range_info.GetRange().GetRangeBase();
5849 const addr_t size = range_info.GetRange().GetByteSize();
5850
5851 if (size == 0)
5852 break;
5853
5854 // Calculate correct protections
5855 uint32_t prot = 0;
5856 if (range_info.GetReadable() == MemoryRegionInfo::eYes)
5857 prot |= VM_PROT_READ;
5858 if (range_info.GetWritable() == MemoryRegionInfo::eYes)
5859 prot |= VM_PROT_WRITE;
5860 if (range_info.GetExecutable() == MemoryRegionInfo::eYes)
5861 prot |= VM_PROT_EXECUTE;
5862
5863// printf ("[%3u] [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ") %c%c%c\n",
5864// range_info_idx,
5865// addr,
5866// size,
5867// (prot & VM_PROT_READ ) ? 'r' : '-',
5868// (prot & VM_PROT_WRITE ) ? 'w' : '-',
5869// (prot & VM_PROT_EXECUTE) ? 'x' : '-');
5870
5871 if (prot != 0)
5872 {
Jason Molendad20359d2014-11-11 10:59:15 +00005873 uint32_t cmd_type = LC_SEGMENT_64;
5874 uint32_t segment_size = sizeof (segment_command_64);
5875 if (addr_byte_size == 4)
5876 {
5877 cmd_type = LC_SEGMENT;
5878 segment_size = sizeof (segment_command);
5879 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005880 segment_command_64 segment = {
Jason Molendad20359d2014-11-11 10:59:15 +00005881 cmd_type, // uint32_t cmd;
5882 segment_size, // uint32_t cmdsize;
Greg Claytona2715cf2014-06-13 00:54:12 +00005883 {0}, // char segname[16];
Jason Molendad20359d2014-11-11 10:59:15 +00005884 addr, // uint64_t vmaddr; // uint32_t for 32-bit Mach-O
5885 size, // uint64_t vmsize; // uint32_t for 32-bit Mach-O
5886 0, // uint64_t fileoff; // uint32_t for 32-bit Mach-O
5887 size, // uint64_t filesize; // uint32_t for 32-bit Mach-O
Greg Claytona2715cf2014-06-13 00:54:12 +00005888 prot, // uint32_t maxprot;
5889 prot, // uint32_t initprot;
5890 0, // uint32_t nsects;
5891 0 }; // uint32_t flags;
5892 segment_load_commands.push_back(segment);
5893 }
5894 else
5895 {
5896 // No protections and a size of 1 used to be returned from old
5897 // debugservers when we asked about a region that was past the
5898 // last memory region and it indicates the end...
5899 if (size == 1)
5900 break;
5901 }
5902
5903 range_error = process_sp->GetMemoryRegionInfo(range_info.GetRange().GetRangeEnd(), range_info);
5904 if (range_error.Fail())
5905 break;
5906 }
5907
Greg Claytona2715cf2014-06-13 00:54:12 +00005908 StreamString buffer (Stream::eBinary,
5909 addr_byte_size,
5910 byte_order);
5911
5912 mach_header_64 mach_header;
Jason Molendad20359d2014-11-11 10:59:15 +00005913 if (addr_byte_size == 8)
5914 {
5915 mach_header.magic = MH_MAGIC_64;
5916 }
5917 else
5918 {
5919 mach_header.magic = MH_MAGIC;
5920 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005921 mach_header.cputype = target_arch.GetMachOCPUType();
5922 mach_header.cpusubtype = target_arch.GetMachOCPUSubType();
5923 mach_header.filetype = MH_CORE;
5924 mach_header.ncmds = segment_load_commands.size();
5925 mach_header.flags = 0;
5926 mach_header.reserved = 0;
5927 ThreadList &thread_list = process_sp->GetThreadList();
5928 const uint32_t num_threads = thread_list.GetSize();
5929
5930 // Make an array of LC_THREAD data items. Each one contains
5931 // the contents of the LC_THREAD load command. The data doesn't
5932 // contain the load command + load command size, we will
5933 // add the load command and load command size as we emit the data.
5934 std::vector<StreamString> LC_THREAD_datas(num_threads);
5935 for (auto &LC_THREAD_data : LC_THREAD_datas)
5936 {
5937 LC_THREAD_data.GetFlags().Set(Stream::eBinary);
5938 LC_THREAD_data.SetAddressByteSize(addr_byte_size);
5939 LC_THREAD_data.SetByteOrder(byte_order);
5940 }
5941 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx)
5942 {
5943 ThreadSP thread_sp (thread_list.GetThreadAtIndex(thread_idx));
5944 if (thread_sp)
5945 {
5946 switch (mach_header.cputype)
5947 {
Jason Molenda22952582014-11-12 01:11:36 +00005948 case llvm::MachO::CPU_TYPE_ARM64:
5949 RegisterContextDarwin_arm64_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
Greg Claytona2715cf2014-06-13 00:54:12 +00005950 break;
Jason Molenda22952582014-11-12 01:11:36 +00005951
5952 case llvm::MachO::CPU_TYPE_ARM:
5953 RegisterContextDarwin_arm_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5954 break;
5955
Greg Claytona2715cf2014-06-13 00:54:12 +00005956 case llvm::MachO::CPU_TYPE_I386:
Jason Molendad20359d2014-11-11 10:59:15 +00005957 RegisterContextDarwin_i386_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
Greg Claytona2715cf2014-06-13 00:54:12 +00005958 break;
5959
5960 case llvm::MachO::CPU_TYPE_X86_64:
5961 RegisterContextDarwin_x86_64_Mach::Create_LC_THREAD (thread_sp.get(), LC_THREAD_datas[thread_idx]);
5962 break;
5963 }
5964
5965 }
5966 }
5967
5968 // The size of the load command is the size of the segments...
Jason Molendad20359d2014-11-11 10:59:15 +00005969 if (addr_byte_size == 8)
5970 {
5971 mach_header.sizeofcmds = segment_load_commands.size() * sizeof (struct segment_command_64);
5972 }
5973 else
5974 {
5975 mach_header.sizeofcmds = segment_load_commands.size() * sizeof (struct segment_command);
5976 }
Greg Claytona2715cf2014-06-13 00:54:12 +00005977
5978 // and the size of all LC_THREAD load command
5979 for (const auto &LC_THREAD_data : LC_THREAD_datas)
5980 {
Greg Claytone37df2e2014-09-16 20:50:29 +00005981 ++mach_header.ncmds;
Greg Claytona2715cf2014-06-13 00:54:12 +00005982 mach_header.sizeofcmds += 8 + LC_THREAD_data.GetSize();
5983 }
5984
5985 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",
5986 mach_header.magic,
5987 mach_header.cputype,
5988 mach_header.cpusubtype,
5989 mach_header.filetype,
5990 mach_header.ncmds,
5991 mach_header.sizeofcmds,
5992 mach_header.flags,
5993 mach_header.reserved);
5994
5995 // Write the mach header
5996 buffer.PutHex32(mach_header.magic);
5997 buffer.PutHex32(mach_header.cputype);
5998 buffer.PutHex32(mach_header.cpusubtype);
5999 buffer.PutHex32(mach_header.filetype);
6000 buffer.PutHex32(mach_header.ncmds);
6001 buffer.PutHex32(mach_header.sizeofcmds);
6002 buffer.PutHex32(mach_header.flags);
Jason Molendad20359d2014-11-11 10:59:15 +00006003 if (addr_byte_size == 8)
6004 {
6005 buffer.PutHex32(mach_header.reserved);
6006 }
Greg Claytona2715cf2014-06-13 00:54:12 +00006007
6008 // Skip the mach header and all load commands and align to the next
6009 // 0x1000 byte boundary
6010 addr_t file_offset = buffer.GetSize() + mach_header.sizeofcmds;
6011 if (file_offset & 0x00000fff)
6012 {
6013 file_offset += 0x00001000ull;
6014 file_offset &= (~0x00001000ull + 1);
6015 }
6016
6017 for (auto &segment : segment_load_commands)
6018 {
6019 segment.fileoff = file_offset;
6020 file_offset += segment.filesize;
6021 }
6022
6023 // Write out all of the LC_THREAD load commands
6024 for (const auto &LC_THREAD_data : LC_THREAD_datas)
6025 {
6026 const size_t LC_THREAD_data_size = LC_THREAD_data.GetSize();
6027 buffer.PutHex32(LC_THREAD);
6028 buffer.PutHex32(8 + LC_THREAD_data_size); // cmd + cmdsize + data
6029 buffer.Write(LC_THREAD_data.GetData(), LC_THREAD_data_size);
6030 }
6031
6032 // Write out all of the segment load commands
6033 for (const auto &segment : segment_load_commands)
6034 {
6035 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",
6036 segment.cmd,
6037 segment.cmdsize,
6038 segment.vmaddr,
6039 segment.vmaddr + segment.vmsize,
6040 segment.fileoff,
6041 segment.filesize,
6042 segment.maxprot,
6043 segment.initprot,
6044 segment.nsects,
6045 segment.flags);
6046
6047 buffer.PutHex32(segment.cmd);
6048 buffer.PutHex32(segment.cmdsize);
6049 buffer.PutRawBytes(segment.segname, sizeof(segment.segname));
Jason Molendad20359d2014-11-11 10:59:15 +00006050 if (addr_byte_size == 8)
6051 {
6052 buffer.PutHex64(segment.vmaddr);
6053 buffer.PutHex64(segment.vmsize);
6054 buffer.PutHex64(segment.fileoff);
6055 buffer.PutHex64(segment.filesize);
6056 }
6057 else
6058 {
6059 buffer.PutHex32(static_cast<uint32_t>(segment.vmaddr));
6060 buffer.PutHex32(static_cast<uint32_t>(segment.vmsize));
6061 buffer.PutHex32(static_cast<uint32_t>(segment.fileoff));
6062 buffer.PutHex32(static_cast<uint32_t>(segment.filesize));
6063 }
Greg Claytona2715cf2014-06-13 00:54:12 +00006064 buffer.PutHex32(segment.maxprot);
6065 buffer.PutHex32(segment.initprot);
6066 buffer.PutHex32(segment.nsects);
6067 buffer.PutHex32(segment.flags);
6068 }
6069
6070 File core_file;
6071 std::string core_file_path(outfile.GetPath());
6072 error = core_file.Open(core_file_path.c_str(),
6073 File::eOpenOptionWrite |
6074 File::eOpenOptionTruncate |
6075 File::eOpenOptionCanCreate);
6076 if (error.Success())
6077 {
6078 // Read 1 page at a time
6079 uint8_t bytes[0x1000];
6080 // Write the mach header and load commands out to the core file
6081 size_t bytes_written = buffer.GetString().size();
6082 error = core_file.Write(buffer.GetString().data(), bytes_written);
6083 if (error.Success())
6084 {
6085 // Now write the file data for all memory segments in the process
6086 for (const auto &segment : segment_load_commands)
6087 {
David Majnemerb98a5e02014-07-24 00:24:12 +00006088 if (core_file.SeekFromStart(segment.fileoff) == -1)
Greg Claytona2715cf2014-06-13 00:54:12 +00006089 {
6090 error.SetErrorStringWithFormat("unable to seek to offset 0x%" PRIx64 " in '%s'", segment.fileoff, core_file_path.c_str());
6091 break;
6092 }
6093
Jason Molendad20359d2014-11-11 10:59:15 +00006094 printf ("Saving %" PRId64 " bytes of data for memory region at 0x%" PRIx64 "\n", segment.vmsize, segment.vmaddr);
Greg Claytona2715cf2014-06-13 00:54:12 +00006095 addr_t bytes_left = segment.vmsize;
6096 addr_t addr = segment.vmaddr;
6097 Error memory_read_error;
6098 while (bytes_left > 0 && error.Success())
6099 {
6100 const size_t bytes_to_read = bytes_left > sizeof(bytes) ? sizeof(bytes) : bytes_left;
6101 const size_t bytes_read = process_sp->ReadMemory(addr, bytes, bytes_to_read, memory_read_error);
6102 if (bytes_read == bytes_to_read)
6103 {
6104 size_t bytes_written = bytes_read;
6105 error = core_file.Write(bytes, bytes_written);
6106 bytes_left -= bytes_read;
6107 addr += bytes_read;
6108 }
6109 else
6110 {
6111 // Some pages within regions are not readable, those
6112 // should be zero filled
6113 memset (bytes, 0, bytes_to_read);
6114 size_t bytes_written = bytes_to_read;
6115 error = core_file.Write(bytes, bytes_written);
6116 bytes_left -= bytes_to_read;
6117 addr += bytes_to_read;
6118 }
6119 }
6120 }
6121 }
6122 }
6123 }
6124 else
6125 {
6126 error.SetErrorString("process doesn't support getting memory region info");
6127 }
6128 }
6129 return true; // This is the right plug to handle saving core files for this process
6130 }
6131 }
6132 return false;
6133}