blob: 67773e7b2c8c06f589f5a085232b9dd0c5670df8 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ObjectFileMachO.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Greg Clayton3f69eac2011-12-03 02:30:59 +000010#include "llvm/ADT/StringRef.h"
Jim Ingham28775942011-03-07 23:44:08 +000011#include "llvm/Support/MachO.h"
12
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "ObjectFileMachO.h"
14
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Core/ArchSpec.h"
16#include "lldb/Core/DataBuffer.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/FileSpecList.h"
18#include "lldb/Core/Module.h"
19#include "lldb/Core/PluginManager.h"
20#include "lldb/Core/Section.h"
21#include "lldb/Core/StreamFile.h"
22#include "lldb/Core/StreamString.h"
23#include "lldb/Core/Timer.h"
24#include "lldb/Core/UUID.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000025#include "lldb/Host/Host.h"
26#include "lldb/Host/FileSpec.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000027#include "lldb/Symbol/ClangNamespaceDecl.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Symbol/ObjectFile.h"
Greg Claytonb5a8f142012-02-05 02:38:54 +000029#include "lldb/Target/Process.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030
Chris Lattner24943d22010-06-08 16:52:24 +000031
32using namespace lldb;
33using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000034using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000035
Greg Claytonb1888f22011-03-19 01:12:21 +000036#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner24943d22010-06-08 16:52:24 +000037
38void
39ObjectFileMachO::Initialize()
40{
41 PluginManager::RegisterPlugin (GetPluginNameStatic(),
42 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +000043 CreateInstance,
44 CreateMemoryInstance);
Chris Lattner24943d22010-06-08 16:52:24 +000045}
46
47void
48ObjectFileMachO::Terminate()
49{
50 PluginManager::UnregisterPlugin (CreateInstance);
51}
52
53
54const char *
55ObjectFileMachO::GetPluginNameStatic()
56{
57 return "object-file.mach-o";
58}
59
60const char *
61ObjectFileMachO::GetPluginDescriptionStatic()
62{
63 return "Mach-o object file reader (32 and 64 bit)";
64}
65
66
67ObjectFile *
Greg Claytondb2dc2b2012-01-12 05:25:17 +000068ObjectFileMachO::CreateInstance (Module* module, DataBufferSP& data_sp, const FileSpec* file, addr_t offset, addr_t length)
Chris Lattner24943d22010-06-08 16:52:24 +000069{
Greg Claytondb2dc2b2012-01-12 05:25:17 +000070 if (ObjectFileMachO::MagicBytesMatch(data_sp, offset, length))
Chris Lattner24943d22010-06-08 16:52:24 +000071 {
Greg Claytondb2dc2b2012-01-12 05:25:17 +000072 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module, data_sp, file, offset, length));
Chris Lattner24943d22010-06-08 16:52:24 +000073 if (objfile_ap.get() && objfile_ap->ParseHeader())
74 return objfile_ap.release();
75 }
76 return NULL;
77}
78
Greg Claytonb5a8f142012-02-05 02:38:54 +000079ObjectFile *
80ObjectFileMachO::CreateMemoryInstance (Module* module,
81 DataBufferSP& data_sp,
82 const ProcessSP &process_sp,
83 lldb::addr_t header_addr)
84{
85 if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
86 {
87 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module, data_sp, process_sp, header_addr));
88 if (objfile_ap.get() && objfile_ap->ParseHeader())
89 return objfile_ap.release();
90 }
91 return NULL;
92}
93
94
95const ConstString &
96ObjectFileMachO::GetSegmentNameTEXT()
97{
98 static ConstString g_segment_name_TEXT ("__TEXT");
99 return g_segment_name_TEXT;
100}
101
102const ConstString &
103ObjectFileMachO::GetSegmentNameDATA()
104{
105 static ConstString g_segment_name_DATA ("__DATA");
106 return g_segment_name_DATA;
107}
108
109const ConstString &
110ObjectFileMachO::GetSegmentNameOBJC()
111{
112 static ConstString g_segment_name_OBJC ("__OBJC");
113 return g_segment_name_OBJC;
114}
115
116const ConstString &
117ObjectFileMachO::GetSegmentNameLINKEDIT()
118{
119 static ConstString g_section_name_LINKEDIT ("__LINKEDIT");
120 return g_section_name_LINKEDIT;
121}
122
123const ConstString &
124ObjectFileMachO::GetSectionNameEHFrame()
125{
126 static ConstString g_section_name_eh_frame ("__eh_frame");
127 return g_section_name_eh_frame;
128}
129
130
Chris Lattner24943d22010-06-08 16:52:24 +0000131
132static uint32_t
133MachHeaderSizeFromMagic(uint32_t magic)
134{
135 switch (magic)
136 {
Greg Clayton1674b122010-07-21 22:12:05 +0000137 case HeaderMagic32:
138 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000139 return sizeof(struct mach_header);
140
Greg Clayton1674b122010-07-21 22:12:05 +0000141 case HeaderMagic64:
142 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000143 return sizeof(struct mach_header_64);
144 break;
145
146 default:
147 break;
148 }
149 return 0;
150}
151
152
153bool
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000154ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp,
155 lldb::addr_t data_offset,
156 lldb::addr_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000157{
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000158 DataExtractor data;
159 data.SetData (data_sp, data_offset, data_length);
Chris Lattner24943d22010-06-08 16:52:24 +0000160 uint32_t offset = 0;
161 uint32_t magic = data.GetU32(&offset);
162 return MachHeaderSizeFromMagic(magic) != 0;
163}
164
165
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000166ObjectFileMachO::ObjectFileMachO(Module* module, DataBufferSP& data_sp, const FileSpec* file, addr_t offset, addr_t length) :
167 ObjectFile(module, file, offset, length, data_sp),
Chris Lattner24943d22010-06-08 16:52:24 +0000168 m_mutex (Mutex::eMutexTypeRecursive),
169 m_header(),
170 m_sections_ap(),
Jim Ingham28775942011-03-07 23:44:08 +0000171 m_symtab_ap(),
172 m_entry_point_address ()
Chris Lattner24943d22010-06-08 16:52:24 +0000173{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000174 ::memset (&m_header, 0, sizeof(m_header));
175 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000176}
177
Greg Claytonb5a8f142012-02-05 02:38:54 +0000178ObjectFileMachO::ObjectFileMachO (lldb_private::Module* module,
179 lldb::DataBufferSP& header_data_sp,
180 const lldb::ProcessSP &process_sp,
181 lldb::addr_t header_addr) :
182 ObjectFile(module, process_sp, header_addr, header_data_sp),
183 m_mutex (Mutex::eMutexTypeRecursive),
184 m_header(),
185 m_sections_ap(),
186 m_symtab_ap(),
187 m_entry_point_address ()
188{
189 ::memset (&m_header, 0, sizeof(m_header));
190 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
191}
Chris Lattner24943d22010-06-08 16:52:24 +0000192
193ObjectFileMachO::~ObjectFileMachO()
194{
195}
196
197
198bool
199ObjectFileMachO::ParseHeader ()
200{
201 lldb_private::Mutex::Locker locker(m_mutex);
202 bool can_parse = false;
203 uint32_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000204 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +0000205 // Leave magic in the original byte order
206 m_header.magic = m_data.GetU32(&offset);
207 switch (m_header.magic)
208 {
Greg Clayton1674b122010-07-21 22:12:05 +0000209 case HeaderMagic32:
Greg Claytoncd548032011-02-01 01:31:41 +0000210 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +0000211 m_data.SetAddressByteSize(4);
212 can_parse = true;
213 break;
214
Greg Clayton1674b122010-07-21 22:12:05 +0000215 case HeaderMagic64:
Greg Claytoncd548032011-02-01 01:31:41 +0000216 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +0000217 m_data.SetAddressByteSize(8);
218 can_parse = true;
219 break;
220
Greg Clayton1674b122010-07-21 22:12:05 +0000221 case HeaderMagic32Swapped:
Greg Claytoncd548032011-02-01 01:31:41 +0000222 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Chris Lattner24943d22010-06-08 16:52:24 +0000223 m_data.SetAddressByteSize(4);
224 can_parse = true;
225 break;
226
Greg Clayton1674b122010-07-21 22:12:05 +0000227 case HeaderMagic64Swapped:
Greg Claytoncd548032011-02-01 01:31:41 +0000228 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Chris Lattner24943d22010-06-08 16:52:24 +0000229 m_data.SetAddressByteSize(8);
230 can_parse = true;
231 break;
232
233 default:
234 break;
235 }
236
237 if (can_parse)
238 {
239 m_data.GetU32(&offset, &m_header.cputype, 6);
240
Greg Claytoncf015052010-06-11 03:25:34 +0000241 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jim Ingham7508e732010-08-09 23:31:02 +0000242
243 if (SetModulesArchitecture (mach_arch))
Greg Claytonb5a8f142012-02-05 02:38:54 +0000244 {
245 const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic);
246 if (m_data.GetByteSize() < header_and_lc_size)
247 {
248 DataBufferSP data_sp;
249 ProcessSP process_sp (m_process_wp.lock());
250 if (process_sp)
251 {
252 data_sp = ReadMemory (process_sp, m_offset, header_and_lc_size);
253 }
254 else
255 {
256 // Read in all only the load command data from the file on disk
257 data_sp = m_file.ReadFileContents(m_offset, header_and_lc_size);
258 if (data_sp->GetByteSize() != header_and_lc_size)
259 return false;
260 }
261 if (data_sp)
262 m_data.SetData (data_sp);
263 }
264 }
265 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000266 }
267 else
268 {
269 memset(&m_header, 0, sizeof(struct mach_header));
270 }
271 return false;
272}
273
274
275ByteOrder
276ObjectFileMachO::GetByteOrder () const
277{
278 lldb_private::Mutex::Locker locker(m_mutex);
279 return m_data.GetByteOrder ();
280}
281
Jim Ingham7508e732010-08-09 23:31:02 +0000282bool
283ObjectFileMachO::IsExecutable() const
284{
285 return m_header.filetype == HeaderFileTypeExecutable;
286}
Chris Lattner24943d22010-06-08 16:52:24 +0000287
288size_t
289ObjectFileMachO::GetAddressByteSize () const
290{
291 lldb_private::Mutex::Locker locker(m_mutex);
292 return m_data.GetAddressByteSize ();
293}
294
Greg Claytonb3448432011-03-24 21:19:54 +0000295AddressClass
Greg Claytonb1888f22011-03-19 01:12:21 +0000296ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
297{
298 Symtab *symtab = GetSymtab();
299 if (symtab)
300 {
301 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
302 if (symbol)
303 {
304 const AddressRange *range_ptr = symbol->GetAddressRangePtr();
305 if (range_ptr)
306 {
307 const Section *section = range_ptr->GetBaseAddress().GetSection();
308 if (section)
309 {
Greg Claytonb3448432011-03-24 21:19:54 +0000310 const SectionType section_type = section->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000311 switch (section_type)
312 {
313 case eSectionTypeInvalid: return eAddressClassUnknown;
314 case eSectionTypeCode:
315 if (m_header.cputype == llvm::MachO::CPUTypeARM)
316 {
317 // For ARM we have a bit in the n_desc field of the symbol
318 // that tells us ARM/Thumb which is bit 0x0008.
319 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
320 return eAddressClassCodeAlternateISA;
321 }
322 return eAddressClassCode;
323
324 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000325 case eSectionTypeData:
326 case eSectionTypeDataCString:
327 case eSectionTypeDataCStringPointers:
328 case eSectionTypeDataSymbolAddress:
329 case eSectionTypeData4:
330 case eSectionTypeData8:
331 case eSectionTypeData16:
332 case eSectionTypeDataPointers:
333 case eSectionTypeZeroFill:
334 case eSectionTypeDataObjCMessageRefs:
335 case eSectionTypeDataObjCCFStrings:
336 return eAddressClassData;
337 case eSectionTypeDebug:
338 case eSectionTypeDWARFDebugAbbrev:
339 case eSectionTypeDWARFDebugAranges:
340 case eSectionTypeDWARFDebugFrame:
341 case eSectionTypeDWARFDebugInfo:
342 case eSectionTypeDWARFDebugLine:
343 case eSectionTypeDWARFDebugLoc:
344 case eSectionTypeDWARFDebugMacInfo:
345 case eSectionTypeDWARFDebugPubNames:
346 case eSectionTypeDWARFDebugPubTypes:
347 case eSectionTypeDWARFDebugRanges:
348 case eSectionTypeDWARFDebugStr:
349 case eSectionTypeDWARFAppleNames:
350 case eSectionTypeDWARFAppleTypes:
351 case eSectionTypeDWARFAppleNamespaces:
352 case eSectionTypeDWARFAppleObjC:
353 return eAddressClassDebug;
Greg Claytonb1888f22011-03-19 01:12:21 +0000354 case eSectionTypeEHFrame: return eAddressClassRuntime;
355 case eSectionTypeOther: return eAddressClassUnknown;
356 }
357 }
358 }
359
Greg Claytonb3448432011-03-24 21:19:54 +0000360 const SymbolType symbol_type = symbol->GetType();
Greg Claytonb1888f22011-03-19 01:12:21 +0000361 switch (symbol_type)
362 {
363 case eSymbolTypeAny: return eAddressClassUnknown;
364 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Greg Claytonb1888f22011-03-19 01:12:21 +0000365
366 case eSymbolTypeCode:
367 case eSymbolTypeTrampoline:
368 if (m_header.cputype == llvm::MachO::CPUTypeARM)
369 {
370 // For ARM we have a bit in the n_desc field of the symbol
371 // that tells us ARM/Thumb which is bit 0x0008.
372 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
373 return eAddressClassCodeAlternateISA;
374 }
375 return eAddressClassCode;
376
377 case eSymbolTypeData: return eAddressClassData;
378 case eSymbolTypeRuntime: return eAddressClassRuntime;
379 case eSymbolTypeException: return eAddressClassRuntime;
380 case eSymbolTypeSourceFile: return eAddressClassDebug;
381 case eSymbolTypeHeaderFile: return eAddressClassDebug;
382 case eSymbolTypeObjectFile: return eAddressClassDebug;
383 case eSymbolTypeCommonBlock: return eAddressClassDebug;
384 case eSymbolTypeBlock: return eAddressClassDebug;
385 case eSymbolTypeLocal: return eAddressClassData;
386 case eSymbolTypeParam: return eAddressClassData;
387 case eSymbolTypeVariable: return eAddressClassData;
388 case eSymbolTypeVariableType: return eAddressClassDebug;
389 case eSymbolTypeLineEntry: return eAddressClassDebug;
390 case eSymbolTypeLineHeader: return eAddressClassDebug;
391 case eSymbolTypeScopeBegin: return eAddressClassDebug;
392 case eSymbolTypeScopeEnd: return eAddressClassDebug;
393 case eSymbolTypeAdditional: return eAddressClassUnknown;
394 case eSymbolTypeCompiler: return eAddressClassDebug;
395 case eSymbolTypeInstrumentation:return eAddressClassDebug;
396 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton3f69eac2011-12-03 02:30:59 +0000397 case eSymbolTypeObjCClass: return eAddressClassRuntime;
398 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
399 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonb1888f22011-03-19 01:12:21 +0000400 }
401 }
402 }
403 return eAddressClassUnknown;
404}
Chris Lattner24943d22010-06-08 16:52:24 +0000405
406Symtab *
407ObjectFileMachO::GetSymtab()
408{
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000409 lldb_private::Mutex::Locker symfile_locker(m_mutex);
Chris Lattner24943d22010-06-08 16:52:24 +0000410 if (m_symtab_ap.get() == NULL)
411 {
412 m_symtab_ap.reset(new Symtab(this));
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000413 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton7c36fa02010-09-11 03:13:28 +0000414 ParseSymtab (true);
Greg Clayton0c496cd2011-11-22 18:47:24 +0000415 m_symtab_ap->Finalize ();
Chris Lattner24943d22010-06-08 16:52:24 +0000416 }
417 return m_symtab_ap.get();
418}
419
420
421SectionList *
422ObjectFileMachO::GetSectionList()
423{
424 lldb_private::Mutex::Locker locker(m_mutex);
425 if (m_sections_ap.get() == NULL)
426 {
427 m_sections_ap.reset(new SectionList());
428 ParseSections();
429 }
430 return m_sections_ap.get();
431}
432
433
434size_t
435ObjectFileMachO::ParseSections ()
436{
437 lldb::user_id_t segID = 0;
438 lldb::user_id_t sectID = 0;
439 struct segment_command_64 load_cmd;
440 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
441 uint32_t i;
442 //bool dump_sections = false;
443 for (i=0; i<m_header.ncmds; ++i)
444 {
445 const uint32_t load_cmd_offset = offset;
446 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
447 break;
448
Greg Clayton1674b122010-07-21 22:12:05 +0000449 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000450 {
451 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
452 {
453 load_cmd.vmaddr = m_data.GetAddress(&offset);
454 load_cmd.vmsize = m_data.GetAddress(&offset);
455 load_cmd.fileoff = m_data.GetAddress(&offset);
456 load_cmd.filesize = m_data.GetAddress(&offset);
457 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
458 {
Greg Clayton68ca8232011-01-25 02:58:48 +0000459
460 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
461
Chris Lattner24943d22010-06-08 16:52:24 +0000462 // Keep a list of mach segments around in case we need to
463 // get at data that isn't stored in the abstracted Sections.
464 m_mach_segments.push_back (load_cmd);
465
466 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
467 // Use a segment ID of the segment index shifted left by 8 so they
468 // never conflict with any of the sections.
469 SectionSP segment_sp;
470 if (segment_name)
471 {
472 segment_sp.reset(new Section (NULL,
473 GetModule(), // Module to which this section belongs
474 ++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
475 segment_name, // Name of this section
476 eSectionTypeContainer, // This section is a container of other sections.
477 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
478 load_cmd.vmsize, // VM size in bytes of this section
479 load_cmd.fileoff, // Offset to the data for this section in the file
480 load_cmd.filesize, // Size in bytes of this section as found in the the file
481 load_cmd.flags)); // Flags for this section
482
Greg Clayton68ca8232011-01-25 02:58:48 +0000483 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000484 m_sections_ap->AddSection(segment_sp);
485 }
486
487 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000488 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000489 // Push a section into our mach sections for the section at
Greg Clayton6af4fad2010-10-06 01:26:32 +0000490 // index zero (NListSectionNoSection) if we don't have any
491 // mach sections yet...
492 if (m_mach_sections.empty())
493 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000494 uint32_t segment_sect_idx;
495 const lldb::user_id_t first_segment_sectID = sectID + 1;
496
497
Greg Clayton1674b122010-07-21 22:12:05 +0000498 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000499 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
500 {
501 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
502 break;
503 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
504 break;
505 sect64.addr = m_data.GetAddress(&offset);
506 sect64.size = m_data.GetAddress(&offset);
507
508 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
509 break;
510
511 // Keep a list of mach sections around in case we need to
512 // get at data that isn't stored in the abstracted Sections.
513 m_mach_sections.push_back (sect64);
514
515 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
516 if (!segment_name)
517 {
518 // We have a segment with no name so we need to conjure up
519 // segments that correspond to the section's segname if there
520 // isn't already such a section. If there is such a section,
521 // we resize the section so that it spans all sections.
522 // We also mark these sections as fake so address matches don't
523 // hit if they land in the gaps between the child sections.
524 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
525 segment_sp = m_sections_ap->FindSectionByName (segment_name);
526 if (segment_sp.get())
527 {
528 Section *segment = segment_sp.get();
529 // Grow the section size as needed.
530 const lldb::addr_t sect64_min_addr = sect64.addr;
531 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
532 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
533 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
534 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
535 if (sect64_min_addr >= curr_seg_min_addr)
536 {
537 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
538 // Only grow the section size if needed
539 if (new_seg_byte_size > curr_seg_byte_size)
540 segment->SetByteSize (new_seg_byte_size);
541 }
542 else
543 {
544 // We need to change the base address of the segment and
545 // adjust the child section offsets for all existing children.
546 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
547 segment->Slide(slide_amount, false);
548 segment->GetChildren().Slide (-slide_amount, false);
549 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
550 }
Greg Clayton661825b2010-06-28 23:51:11 +0000551
552 // Grow the section size as needed.
553 if (sect64.offset)
554 {
555 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
556 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
557
558 const lldb::addr_t section_min_file_offset = sect64.offset;
559 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
560 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
561 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
562 segment->SetFileOffset (new_file_offset);
563 segment->SetFileSize (new_file_size);
564 }
Chris Lattner24943d22010-06-08 16:52:24 +0000565 }
566 else
567 {
568 // Create a fake section for the section's named segment
569 segment_sp.reset(new Section(segment_sp.get(), // Parent section
570 GetModule(), // Module to which this section belongs
571 ++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
572 segment_name, // Name of this section
573 eSectionTypeContainer, // This section is a container of other sections.
574 sect64.addr, // File VM address == addresses as they are found in the object file
575 sect64.size, // VM size in bytes of this section
576 sect64.offset, // Offset to the data for this section in the file
577 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
578 load_cmd.flags)); // Flags for this section
579 segment_sp->SetIsFake(true);
580 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000581 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000582 }
583 }
584 assert (segment_sp.get());
585
Greg Clayton1674b122010-07-21 22:12:05 +0000586 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000587 static ConstString g_sect_name_objc_data ("__objc_data");
588 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
589 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
590 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
591 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
592 static ConstString g_sect_name_objc_const ("__objc_const");
593 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
594 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000595
596 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
597 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
598 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
599 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
600 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
601 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
602 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
603 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
604 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
605 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
606 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Claytonf6e3de22011-09-28 17:06:40 +0000607 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
608 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton00db2152011-10-04 22:41:51 +0000609 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton24a6bd92011-10-27 17:55:14 +0000610 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000611 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +0000612 static ConstString g_sect_name_DATA ("__DATA");
613 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000614
Chris Lattner24943d22010-06-08 16:52:24 +0000615 SectionType sect_type = eSectionTypeOther;
616
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000617 if (section_name == g_sect_name_dwarf_debug_abbrev)
618 sect_type = eSectionTypeDWARFDebugAbbrev;
619 else if (section_name == g_sect_name_dwarf_debug_aranges)
620 sect_type = eSectionTypeDWARFDebugAranges;
621 else if (section_name == g_sect_name_dwarf_debug_frame)
622 sect_type = eSectionTypeDWARFDebugFrame;
623 else if (section_name == g_sect_name_dwarf_debug_info)
624 sect_type = eSectionTypeDWARFDebugInfo;
625 else if (section_name == g_sect_name_dwarf_debug_line)
626 sect_type = eSectionTypeDWARFDebugLine;
627 else if (section_name == g_sect_name_dwarf_debug_loc)
628 sect_type = eSectionTypeDWARFDebugLoc;
629 else if (section_name == g_sect_name_dwarf_debug_macinfo)
630 sect_type = eSectionTypeDWARFDebugMacInfo;
631 else if (section_name == g_sect_name_dwarf_debug_pubnames)
632 sect_type = eSectionTypeDWARFDebugPubNames;
633 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
634 sect_type = eSectionTypeDWARFDebugPubTypes;
635 else if (section_name == g_sect_name_dwarf_debug_ranges)
636 sect_type = eSectionTypeDWARFDebugRanges;
637 else if (section_name == g_sect_name_dwarf_debug_str)
638 sect_type = eSectionTypeDWARFDebugStr;
Greg Claytonf6e3de22011-09-28 17:06:40 +0000639 else if (section_name == g_sect_name_dwarf_apple_names)
640 sect_type = eSectionTypeDWARFAppleNames;
641 else if (section_name == g_sect_name_dwarf_apple_types)
642 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton00db2152011-10-04 22:41:51 +0000643 else if (section_name == g_sect_name_dwarf_apple_namespaces)
644 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton24a6bd92011-10-27 17:55:14 +0000645 else if (section_name == g_sect_name_dwarf_apple_objc)
646 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000647 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +0000648 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +0000649 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +0000650 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000651 else if (section_name == g_sect_name_eh_frame)
652 sect_type = eSectionTypeEHFrame;
653 else if (section_name == g_sect_name_cfstring)
654 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +0000655 else if (section_name == g_sect_name_objc_data ||
656 section_name == g_sect_name_objc_classrefs ||
657 section_name == g_sect_name_objc_superrefs ||
658 section_name == g_sect_name_objc_const ||
659 section_name == g_sect_name_objc_classlist)
660 {
661 sect_type = eSectionTypeDataPointers;
662 }
Chris Lattner24943d22010-06-08 16:52:24 +0000663
664 if (sect_type == eSectionTypeOther)
665 {
666 switch (mach_sect_type)
667 {
668 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +0000669 case SectionTypeRegular:
670 if (segment_sp->GetName() == g_sect_name_TEXT)
671 sect_type = eSectionTypeCode;
672 else if (segment_sp->GetName() == g_sect_name_DATA)
673 sect_type = eSectionTypeData;
674 else
675 sect_type = eSectionTypeOther;
676 break;
Greg Clayton1674b122010-07-21 22:12:05 +0000677 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
678 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
679 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
680 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
681 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
682 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
683 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
684 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
685 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
686 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
687 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
688 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
689 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
690 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
691 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
692 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +0000693 default: break;
694 }
695 }
696
697 SectionSP section_sp(new Section(segment_sp.get(),
698 GetModule(),
699 ++sectID,
700 section_name,
701 sect_type,
702 sect64.addr - segment_sp->GetFileAddress(),
703 sect64.size,
704 sect64.offset,
705 sect64.offset == 0 ? 0 : sect64.size,
706 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +0000707 // Set the section to be encrypted to match the segment
708 section_sp->SetIsEncrypted (segment_is_encrypted);
709
Chris Lattner24943d22010-06-08 16:52:24 +0000710 segment_sp->GetChildren().AddSection(section_sp);
711
712 if (segment_sp->IsFake())
713 {
714 segment_sp.reset();
715 segment_name.Clear();
716 }
717 }
Greg Clayton0fa51242011-07-19 03:57:15 +0000718 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +0000719 {
720 if (first_segment_sectID <= sectID)
721 {
722 lldb::user_id_t sect_uid;
723 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
724 {
725 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
726 SectionSP next_section_sp;
727 if (sect_uid + 1 <= sectID)
728 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
729
730 if (curr_section_sp.get())
731 {
732 if (curr_section_sp->GetByteSize() == 0)
733 {
734 if (next_section_sp.get() != NULL)
735 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
736 else
737 curr_section_sp->SetByteSize ( load_cmd.vmsize );
738 }
739 }
740 }
741 }
742 }
743 }
744 }
745 }
Greg Clayton1674b122010-07-21 22:12:05 +0000746 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +0000747 {
748 m_dysymtab.cmd = load_cmd.cmd;
749 m_dysymtab.cmdsize = load_cmd.cmdsize;
750 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
751 }
752
753 offset = load_cmd_offset + load_cmd.cmdsize;
754 }
755// if (dump_sections)
756// {
757// StreamFile s(stdout);
758// m_sections_ap->Dump(&s, true);
759// }
760 return sectID; // Return the number of sections we registered with the module
761}
762
763class MachSymtabSectionInfo
764{
765public:
766
767 MachSymtabSectionInfo (SectionList *section_list) :
768 m_section_list (section_list),
769 m_section_infos()
770 {
771 // Get the number of sections down to a depth of 1 to include
772 // all segments and their sections, but no other sections that
773 // may be added for debug map or
774 m_section_infos.resize(section_list->GetNumSections(1));
775 }
776
777
778 Section *
779 GetSection (uint8_t n_sect, addr_t file_addr)
780 {
781 if (n_sect == 0)
782 return NULL;
783 if (n_sect < m_section_infos.size())
784 {
785 if (m_section_infos[n_sect].section == NULL)
786 {
787 Section *section = m_section_list->FindSectionByID (n_sect).get();
788 m_section_infos[n_sect].section = section;
Greg Clayton5638d2c2011-07-10 17:32:33 +0000789 if (section != NULL)
790 {
791 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
792 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
793 }
794 else
795 {
Greg Claytondf6dc882012-01-05 03:57:59 +0000796 Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect);
Greg Clayton5638d2c2011-07-10 17:32:33 +0000797 }
Chris Lattner24943d22010-06-08 16:52:24 +0000798 }
799 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton811b9c52011-08-26 20:01:35 +0000800 {
801 // Symbol is in section.
Chris Lattner24943d22010-06-08 16:52:24 +0000802 return m_section_infos[n_sect].section;
Greg Clayton811b9c52011-08-26 20:01:35 +0000803 }
804 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
805 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
806 {
807 // Symbol is in section with zero size, but has the same start
808 // address as the section. This can happen with linker symbols
809 // (symbols that start with the letter 'l' or 'L'.
810 return m_section_infos[n_sect].section;
811 }
Chris Lattner24943d22010-06-08 16:52:24 +0000812 }
813 return m_section_list->FindSectionContainingFileAddress(file_addr).get();
814 }
815
816protected:
817 struct SectionInfo
818 {
819 SectionInfo () :
820 vm_range(),
821 section (NULL)
822 {
823 }
824
825 VMRange vm_range;
826 Section *section;
827 };
828 SectionList *m_section_list;
829 std::vector<SectionInfo> m_section_infos;
830};
831
832
833
834size_t
835ObjectFileMachO::ParseSymtab (bool minimize)
836{
837 Timer scoped_timer(__PRETTY_FUNCTION__,
838 "ObjectFileMachO::ParseSymtab () module = %s",
839 m_file.GetFilename().AsCString(""));
840 struct symtab_command symtab_load_command;
841 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
842 uint32_t i;
Greg Clayton0fea0512011-12-30 00:32:24 +0000843
844 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS));
845
Chris Lattner24943d22010-06-08 16:52:24 +0000846 for (i=0; i<m_header.ncmds; ++i)
847 {
848 const uint32_t cmd_offset = offset;
849 // Read in the load command and load command size
850 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
851 break;
852 // Watch for the symbol table load command
Greg Clayton1674b122010-07-21 22:12:05 +0000853 if (symtab_load_command.cmd == LoadCommandSymtab)
Chris Lattner24943d22010-06-08 16:52:24 +0000854 {
855 // Read in the rest of the symtab load command
Jason Molendaccfba722010-07-06 22:38:03 +0000856 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
Chris Lattner24943d22010-06-08 16:52:24 +0000857 {
Greg Clayton0fea0512011-12-30 00:32:24 +0000858 if (symtab_load_command.symoff == 0)
859 {
860 if (log)
861 GetModule()->LogMessage(log.get(), "LC_SYMTAB.symoff == 0");
862 return 0;
863 }
864
865 if (symtab_load_command.stroff == 0)
866 {
867 if (log)
868 GetModule()->LogMessage(log.get(), "LC_SYMTAB.stroff == 0");
869 return 0;
870 }
871
872 if (symtab_load_command.nsyms == 0)
873 {
874 if (log)
875 GetModule()->LogMessage(log.get(), "LC_SYMTAB.nsyms == 0");
876 return 0;
877 }
878
879 if (symtab_load_command.strsize == 0)
880 {
881 if (log)
882 GetModule()->LogMessage(log.get(), "LC_SYMTAB.strsize == 0");
883 return 0;
884 }
885
Chris Lattner24943d22010-06-08 16:52:24 +0000886 Symtab *symtab = m_symtab_ap.get();
887 SectionList *section_list = GetSectionList();
Greg Clayton0fea0512011-12-30 00:32:24 +0000888 if (section_list == NULL)
889 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000890
Greg Claytonb5a8f142012-02-05 02:38:54 +0000891 ProcessSP process_sp (m_process_wp.lock());
892
Greg Clayton0fea0512011-12-30 00:32:24 +0000893 const size_t addr_byte_size = m_data.GetAddressByteSize();
Greg Clayton0fea0512011-12-30 00:32:24 +0000894 bool bit_width_32 = addr_byte_size == 4;
895 const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
896
Greg Claytonb5a8f142012-02-05 02:38:54 +0000897 DataExtractor nlist_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
898 DataExtractor strtab_data (NULL, 0, m_data.GetByteOrder(), m_data.GetAddressByteSize());
899
900 const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size;
901 const addr_t strtab_data_byte_size = symtab_load_command.strsize;
902 if (process_sp)
903 {
904 Target &target = process_sp->GetTarget();
905 SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
906 // Reading mach file from memory in a process or core file...
907
908 if (linkedit_section_sp)
909 {
910 const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target);
911 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
912 const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset;
913 const addr_t stroff_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset;
914 DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size));
915 DataBufferSP strtab_data_sp (ReadMemory (process_sp, stroff_addr, strtab_data_byte_size));
916 nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize());
917 strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize());
918 }
919 }
920 else
921 {
922 nlist_data.SetData (m_data,
923 symtab_load_command.symoff,
924 nlist_data_byte_size);
925 strtab_data.SetData (m_data,
926 symtab_load_command.stroff,
927 strtab_data_byte_size);
928
929 }
Greg Clayton0fea0512011-12-30 00:32:24 +0000930
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000931 if (nlist_data.GetByteSize() == 0)
Greg Clayton0fea0512011-12-30 00:32:24 +0000932 {
933 if (log)
934 GetModule()->LogMessage(log.get(), "failed to read nlist data");
935 return 0;
936 }
937
Greg Clayton0fea0512011-12-30 00:32:24 +0000938
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000939 if (strtab_data.GetByteSize() == 0)
Greg Clayton0fea0512011-12-30 00:32:24 +0000940 {
941 if (log)
942 GetModule()->LogMessage(log.get(), "failed to read strtab data");
943 return 0;
944 }
Chris Lattner24943d22010-06-08 16:52:24 +0000945
Greg Claytonb5a8f142012-02-05 02:38:54 +0000946 const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT();
947 const ConstString &g_segment_name_DATA = GetSegmentNameDATA();
948 const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC();
949 const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame();
Chris Lattner24943d22010-06-08 16:52:24 +0000950 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
951 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
952 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
953 SectionSP eh_frame_section_sp;
954 if (text_section_sp.get())
955 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
956 else
957 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
958
Greg Clayton1674b122010-07-21 22:12:05 +0000959 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Chris Lattner24943d22010-06-08 16:52:24 +0000960
Greg Clayton0fea0512011-12-30 00:32:24 +0000961 uint32_t nlist_data_offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000962
Greg Clayton7c36fa02010-09-11 03:13:28 +0000963 uint32_t N_SO_index = UINT32_MAX;
Chris Lattner24943d22010-06-08 16:52:24 +0000964
965 MachSymtabSectionInfo section_info (section_list);
966 std::vector<uint32_t> N_FUN_indexes;
967 std::vector<uint32_t> N_NSYM_indexes;
968 std::vector<uint32_t> N_INCL_indexes;
969 std::vector<uint32_t> N_BRAC_indexes;
970 std::vector<uint32_t> N_COMM_indexes;
Greg Clayton576a68b2010-09-08 16:38:06 +0000971 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Clayton637029b2010-09-12 05:25:16 +0000972 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Clayton576a68b2010-09-08 16:38:06 +0000973 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
974 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Clayton7c36fa02010-09-11 03:13:28 +0000975 // Any symbols that get merged into another will get an entry
976 // in this map so we know
Greg Clayton637029b2010-09-12 05:25:16 +0000977 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000978 uint32_t nlist_idx = 0;
979 Symbol *symbol_ptr = NULL;
980
981 uint32_t sym_idx = 0;
982 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
983 uint32_t num_syms = symtab->GetNumSymbols();
984
985 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
986 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
987 {
988 struct nlist_64 nlist;
Greg Clayton0fea0512011-12-30 00:32:24 +0000989 if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size))
990 break;
991
992 nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset);
993 nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset);
994 nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset);
995 nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset);
996 nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000997
998 SymbolType type = eSymbolTypeInvalid;
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000999 const char *symbol_name = strtab_data.PeekCStr(nlist.n_strx);
1000 if (symbol_name == NULL)
Greg Claytona9c4f312011-10-31 20:50:40 +00001001 {
1002 // No symbol should be NULL, even the symbols with no
1003 // string values should have an offset zero which points
1004 // to an empty C-string
Greg Claytondf6dc882012-01-05 03:57:59 +00001005 Host::SystemLog (Host::eSystemLogError,
1006 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
1007 nlist_idx,
1008 nlist.n_strx,
1009 m_module->GetFileSpec().GetDirectory().GetCString(),
1010 m_module->GetFileSpec().GetFilename().GetCString());
Greg Claytona9c4f312011-10-31 20:50:40 +00001011 continue;
1012 }
Greg Clayton3f69eac2011-12-03 02:30:59 +00001013 const char *symbol_name_non_abi_mangled = NULL;
Greg Claytona9c4f312011-10-31 20:50:40 +00001014
Chris Lattner24943d22010-06-08 16:52:24 +00001015 if (symbol_name[0] == '\0')
1016 symbol_name = NULL;
1017 Section* symbol_section = NULL;
1018 bool add_nlist = true;
Greg Clayton1674b122010-07-21 22:12:05 +00001019 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001020
1021 assert (sym_idx < num_syms);
1022
1023 sym[sym_idx].SetDebug (is_debug);
1024
1025 if (is_debug)
1026 {
1027 switch (nlist.n_type)
1028 {
Greg Clayton1674b122010-07-21 22:12:05 +00001029 case StabGlobalSymbol:
1030 // N_GSYM -- global symbol: name,,NO_SECT,type,0
Chris Lattner24943d22010-06-08 16:52:24 +00001031 // Sometimes the N_GSYM value contains the address.
Jim Ingham115213c2011-12-16 00:05:58 +00001032
1033 // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They
1034 // have the same address, but we want to ensure that we always find only the real symbol,
1035 // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass
1036 // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated
1037 // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the
1038 // same address.
1039
1040 if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O'
1041 && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0
1042 || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0
1043 || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0))
1044 add_nlist = false;
1045 else
1046 {
1047 sym[sym_idx].SetExternal(true);
1048 if (nlist.n_value != 0)
1049 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1050 type = eSymbolTypeData;
1051 }
Chris Lattner24943d22010-06-08 16:52:24 +00001052 break;
1053
Greg Clayton1674b122010-07-21 22:12:05 +00001054 case StabFunctionName:
1055 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Clayton7c36fa02010-09-11 03:13:28 +00001056 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00001057 break;
1058
Greg Clayton1674b122010-07-21 22:12:05 +00001059 case StabFunction:
1060 // N_FUN -- procedure: name,,n_sect,linenumber,address
Chris Lattner24943d22010-06-08 16:52:24 +00001061 if (symbol_name)
1062 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001063 type = eSymbolTypeCode;
Chris Lattner24943d22010-06-08 16:52:24 +00001064 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton576a68b2010-09-08 16:38:06 +00001065
1066 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner24943d22010-06-08 16:52:24 +00001067 // We use the current number of symbols in the symbol table in lieu of
1068 // using nlist_idx in case we ever start trimming entries out
1069 N_FUN_indexes.push_back(sym_idx);
1070 }
1071 else
1072 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001073 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +00001074
1075 if ( !N_FUN_indexes.empty() )
1076 {
1077 // Copy the size of the function into the original STAB entry so we don't have
1078 // to hunt for it later
1079 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
1080 N_FUN_indexes.pop_back();
Jason Molendaccfba722010-07-06 22:38:03 +00001081 // We don't really need the end function STAB as it contains the size which
Chris Lattner24943d22010-06-08 16:52:24 +00001082 // we already placed with the original symbol, so don't add it if we want a
1083 // minimal symbol table
1084 if (minimize)
1085 add_nlist = false;
1086 }
1087 }
1088 break;
1089
Greg Clayton1674b122010-07-21 22:12:05 +00001090 case StabStaticSymbol:
1091 // N_STSYM -- static symbol: name,,n_sect,type,address
Greg Clayton576a68b2010-09-08 16:38:06 +00001092 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner24943d22010-06-08 16:52:24 +00001093 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton7c36fa02010-09-11 03:13:28 +00001094 type = eSymbolTypeData;
Chris Lattner24943d22010-06-08 16:52:24 +00001095 break;
1096
Greg Clayton1674b122010-07-21 22:12:05 +00001097 case StabLocalCommon:
1098 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
Chris Lattner24943d22010-06-08 16:52:24 +00001099 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1100 type = eSymbolTypeCommonBlock;
1101 break;
1102
Greg Clayton1674b122010-07-21 22:12:05 +00001103 case StabBeginSymbol:
1104 // N_BNSYM
Chris Lattner24943d22010-06-08 16:52:24 +00001105 // We use the current number of symbols in the symbol table in lieu of
1106 // using nlist_idx in case we ever start trimming entries out
1107 if (minimize)
1108 {
1109 // Skip these if we want minimal symbol tables
1110 add_nlist = false;
1111 }
1112 else
1113 {
1114 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1115 N_NSYM_indexes.push_back(sym_idx);
1116 type = eSymbolTypeScopeBegin;
1117 }
1118 break;
1119
Greg Clayton1674b122010-07-21 22:12:05 +00001120 case StabEndSymbol:
1121 // N_ENSYM
Chris Lattner24943d22010-06-08 16:52:24 +00001122 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
1123 // so that we can always skip the entire symbol if we need to navigate
1124 // more quickly at the source level when parsing STABS
1125 if (minimize)
1126 {
1127 // Skip these if we want minimal symbol tables
1128 add_nlist = false;
1129 }
1130 else
1131 {
1132 if ( !N_NSYM_indexes.empty() )
1133 {
1134 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
1135 symbol_ptr->SetByteSize(sym_idx + 1);
1136 symbol_ptr->SetSizeIsSibling(true);
1137 N_NSYM_indexes.pop_back();
1138 }
1139 type = eSymbolTypeScopeEnd;
1140 }
1141 break;
1142
1143
Greg Clayton1674b122010-07-21 22:12:05 +00001144 case StabSourceFileOptions:
1145 // N_OPT - emitted with gcc2_compiled and in gcc source
Chris Lattner24943d22010-06-08 16:52:24 +00001146 type = eSymbolTypeCompiler;
1147 break;
1148
Greg Clayton1674b122010-07-21 22:12:05 +00001149 case StabRegisterSymbol:
1150 // N_RSYM - register sym: name,,NO_SECT,type,register
Chris Lattner24943d22010-06-08 16:52:24 +00001151 type = eSymbolTypeVariable;
1152 break;
1153
Greg Clayton1674b122010-07-21 22:12:05 +00001154 case StabSourceLine:
1155 // N_SLINE - src line: 0,,n_sect,linenumber,address
Chris Lattner24943d22010-06-08 16:52:24 +00001156 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1157 type = eSymbolTypeLineEntry;
1158 break;
1159
Greg Clayton1674b122010-07-21 22:12:05 +00001160 case StabStructureType:
1161 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
Chris Lattner24943d22010-06-08 16:52:24 +00001162 type = eSymbolTypeVariableType;
1163 break;
1164
Greg Clayton1674b122010-07-21 22:12:05 +00001165 case StabSourceFileName:
1166 // N_SO - source file name
Chris Lattner24943d22010-06-08 16:52:24 +00001167 type = eSymbolTypeSourceFile;
1168 if (symbol_name == NULL)
1169 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001170 if (minimize)
1171 add_nlist = false;
1172 if (N_SO_index != UINT32_MAX)
Chris Lattner24943d22010-06-08 16:52:24 +00001173 {
1174 // Set the size of the N_SO to the terminating index of this N_SO
1175 // so that we can always skip the entire N_SO if we need to navigate
1176 // more quickly at the source level when parsing STABS
1177 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton7c36fa02010-09-11 03:13:28 +00001178 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
Chris Lattner24943d22010-06-08 16:52:24 +00001179 symbol_ptr->SetSizeIsSibling(true);
1180 }
1181 N_NSYM_indexes.clear();
1182 N_INCL_indexes.clear();
1183 N_BRAC_indexes.clear();
1184 N_COMM_indexes.clear();
1185 N_FUN_indexes.clear();
Greg Clayton7c36fa02010-09-11 03:13:28 +00001186 N_SO_index = UINT32_MAX;
Chris Lattner24943d22010-06-08 16:52:24 +00001187 }
Greg Clayton7c36fa02010-09-11 03:13:28 +00001188 else
Chris Lattner24943d22010-06-08 16:52:24 +00001189 {
1190 // We use the current number of symbols in the symbol table in lieu of
1191 // using nlist_idx in case we ever start trimming entries out
Greg Clayton7c36fa02010-09-11 03:13:28 +00001192 if (symbol_name[0] == '/')
1193 N_SO_index = sym_idx;
Greg Claytondab471f2011-06-19 04:26:01 +00001194 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Greg Clayton7c36fa02010-09-11 03:13:28 +00001195 {
1196 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1197 if (so_path && so_path[0])
1198 {
1199 std::string full_so_path (so_path);
1200 if (*full_so_path.rbegin() != '/')
1201 full_so_path += '/';
1202 full_so_path += symbol_name;
1203 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
1204 add_nlist = false;
Greg Clayton637029b2010-09-12 05:25:16 +00001205 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001206 }
1207 }
Chris Lattner24943d22010-06-08 16:52:24 +00001208 }
Greg Clayton7c36fa02010-09-11 03:13:28 +00001209
Chris Lattner24943d22010-06-08 16:52:24 +00001210 break;
1211
Greg Clayton1674b122010-07-21 22:12:05 +00001212 case StabObjectFileName:
1213 // N_OSO - object file name: name,,0,0,st_mtime
Chris Lattner24943d22010-06-08 16:52:24 +00001214 type = eSymbolTypeObjectFile;
1215 break;
1216
Greg Clayton1674b122010-07-21 22:12:05 +00001217 case StabLocalSymbol:
1218 // N_LSYM - local sym: name,,NO_SECT,type,offset
Chris Lattner24943d22010-06-08 16:52:24 +00001219 type = eSymbolTypeLocal;
1220 break;
1221
1222 //----------------------------------------------------------------------
1223 // INCL scopes
1224 //----------------------------------------------------------------------
Greg Clayton1674b122010-07-21 22:12:05 +00001225 case StabBeginIncludeFileName:
1226 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
Chris Lattner24943d22010-06-08 16:52:24 +00001227 // We use the current number of symbols in the symbol table in lieu of
1228 // using nlist_idx in case we ever start trimming entries out
1229 N_INCL_indexes.push_back(sym_idx);
1230 type = eSymbolTypeScopeBegin;
1231 break;
Chris Lattner24943d22010-06-08 16:52:24 +00001232
Greg Clayton1674b122010-07-21 22:12:05 +00001233 case StabEndIncludeFile:
1234 // N_EINCL - include file end: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +00001235 // Set the size of the N_BINCL to the terminating index of this N_EINCL
1236 // so that we can always skip the entire symbol if we need to navigate
1237 // more quickly at the source level when parsing STABS
1238 if ( !N_INCL_indexes.empty() )
1239 {
1240 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1241 symbol_ptr->SetByteSize(sym_idx + 1);
1242 symbol_ptr->SetSizeIsSibling(true);
1243 N_INCL_indexes.pop_back();
1244 }
1245 type = eSymbolTypeScopeEnd;
1246 break;
1247
Greg Clayton1674b122010-07-21 22:12:05 +00001248 case StabIncludeFileName:
1249 // N_SOL - #included file name: name,,n_sect,0,address
Chris Lattner24943d22010-06-08 16:52:24 +00001250 type = eSymbolTypeHeaderFile;
Greg Clayton0ad086f2010-09-07 17:36:17 +00001251
1252 // We currently don't use the header files on darwin
1253 if (minimize)
1254 add_nlist = false;
Chris Lattner24943d22010-06-08 16:52:24 +00001255 break;
1256
Greg Clayton1674b122010-07-21 22:12:05 +00001257 case StabCompilerParameters:
1258 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +00001259 type = eSymbolTypeCompiler;
1260 break;
1261
Greg Clayton1674b122010-07-21 22:12:05 +00001262 case StabCompilerVersion:
1263 // N_VERSION - compiler version: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +00001264 type = eSymbolTypeCompiler;
1265 break;
1266
Greg Clayton1674b122010-07-21 22:12:05 +00001267 case StabCompilerOptLevel:
1268 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +00001269 type = eSymbolTypeCompiler;
1270 break;
1271
Greg Clayton1674b122010-07-21 22:12:05 +00001272 case StabParameter:
1273 // N_PSYM - parameter: name,,NO_SECT,type,offset
Chris Lattner24943d22010-06-08 16:52:24 +00001274 type = eSymbolTypeVariable;
1275 break;
1276
Greg Clayton1674b122010-07-21 22:12:05 +00001277 case StabAlternateEntry:
1278 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
Chris Lattner24943d22010-06-08 16:52:24 +00001279 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1280 type = eSymbolTypeLineEntry;
1281 break;
1282
1283 //----------------------------------------------------------------------
1284 // Left and Right Braces
1285 //----------------------------------------------------------------------
Greg Clayton1674b122010-07-21 22:12:05 +00001286 case StabLeftBracket:
1287 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
Chris Lattner24943d22010-06-08 16:52:24 +00001288 // We use the current number of symbols in the symbol table in lieu of
1289 // using nlist_idx in case we ever start trimming entries out
1290 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1291 N_BRAC_indexes.push_back(sym_idx);
1292 type = eSymbolTypeScopeBegin;
1293 break;
1294
Greg Clayton1674b122010-07-21 22:12:05 +00001295 case StabRightBracket:
1296 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
Chris Lattner24943d22010-06-08 16:52:24 +00001297 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
1298 // so that we can always skip the entire symbol if we need to navigate
1299 // more quickly at the source level when parsing STABS
1300 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1301 if ( !N_BRAC_indexes.empty() )
1302 {
1303 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1304 symbol_ptr->SetByteSize(sym_idx + 1);
1305 symbol_ptr->SetSizeIsSibling(true);
1306 N_BRAC_indexes.pop_back();
1307 }
1308 type = eSymbolTypeScopeEnd;
1309 break;
1310
Greg Clayton1674b122010-07-21 22:12:05 +00001311 case StabDeletedIncludeFile:
1312 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
Chris Lattner24943d22010-06-08 16:52:24 +00001313 type = eSymbolTypeHeaderFile;
1314 break;
1315
1316 //----------------------------------------------------------------------
1317 // COMM scopes
1318 //----------------------------------------------------------------------
Greg Clayton1674b122010-07-21 22:12:05 +00001319 case StabBeginCommon:
1320 // N_BCOMM - begin common: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +00001321 // We use the current number of symbols in the symbol table in lieu of
1322 // using nlist_idx in case we ever start trimming entries out
1323 type = eSymbolTypeScopeBegin;
1324 N_COMM_indexes.push_back(sym_idx);
1325 break;
1326
Greg Clayton1674b122010-07-21 22:12:05 +00001327 case StabEndCommonLocal:
1328 // N_ECOML - end common (local name): 0,,n_sect,0,address
Chris Lattner24943d22010-06-08 16:52:24 +00001329 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1330 // Fall through
1331
Greg Clayton1674b122010-07-21 22:12:05 +00001332 case StabEndCommon:
1333 // N_ECOMM - end common: name,,n_sect,0,0
Chris Lattner24943d22010-06-08 16:52:24 +00001334 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1335 // so that we can always skip the entire symbol if we need to navigate
1336 // more quickly at the source level when parsing STABS
1337 if ( !N_COMM_indexes.empty() )
1338 {
1339 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1340 symbol_ptr->SetByteSize(sym_idx + 1);
1341 symbol_ptr->SetSizeIsSibling(true);
1342 N_COMM_indexes.pop_back();
1343 }
1344 type = eSymbolTypeScopeEnd;
1345 break;
1346
Greg Clayton1674b122010-07-21 22:12:05 +00001347 case StabLength:
1348 // N_LENG - second stab entry with length information
Chris Lattner24943d22010-06-08 16:52:24 +00001349 type = eSymbolTypeAdditional;
1350 break;
1351
1352 default: break;
1353 }
1354 }
1355 else
1356 {
Greg Clayton1674b122010-07-21 22:12:05 +00001357 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
1358 uint8_t n_type = NlistMaskType & nlist.n_type;
1359 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001360
Greg Clayton3f69eac2011-12-03 02:30:59 +00001361 switch (n_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001362 {
Greg Clayton3f69eac2011-12-03 02:30:59 +00001363 case NListTypeIndirect: // N_INDR - Fall through
1364 case NListTypePreboundUndefined:// N_PBUD - Fall through
1365 case NListTypeUndefined: // N_UNDF
1366 type = eSymbolTypeUndefined;
1367 break;
1368
1369 case NListTypeAbsolute: // N_ABS
1370 type = eSymbolTypeAbsolute;
1371 break;
1372
1373 case NListTypeSection: // N_SECT
1374 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1375
1376 if (symbol_section == NULL)
1377 {
1378 // TODO: warn about this?
1379 add_nlist = false;
1380 break;
1381 }
1382
1383 if (TEXT_eh_frame_sectID == nlist.n_sect)
1384 {
1385 type = eSymbolTypeException;
1386 }
1387 else
1388 {
1389 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
1390
1391 switch (section_type)
1392 {
1393 case SectionTypeRegular: break; // regular section
1394 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
1395 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
1396 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
1397 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
1398 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1399 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1400 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1401 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1402 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
1403 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
1404 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
1405 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
1406 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
1407 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
1408 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
1409 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
1410 default: break;
1411 }
1412
1413 if (type == eSymbolTypeInvalid)
1414 {
1415 const char *symbol_sect_name = symbol_section->GetName().AsCString();
1416 if (symbol_section->IsDescendant (text_section_sp.get()))
1417 {
1418 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1419 SectionAttrUserSelfModifyingCode |
1420 SectionAttrSytemSomeInstructions))
1421 type = eSymbolTypeData;
1422 else
1423 type = eSymbolTypeCode;
1424 }
1425 else
1426 if (symbol_section->IsDescendant(data_section_sp.get()))
1427 {
1428 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1429 {
1430 type = eSymbolTypeRuntime;
1431
1432 if (symbol_name &&
1433 symbol_name[0] == '_' &&
1434 symbol_name[1] == 'O' &&
1435 symbol_name[2] == 'B')
1436 {
1437 llvm::StringRef symbol_name_ref(symbol_name);
1438 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
1439 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
1440 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
1441 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
1442 {
Sean Callanand0b7cfa2011-12-03 04:38:43 +00001443 symbol_name_non_abi_mangled = symbol_name + 1;
Greg Clayton3f69eac2011-12-03 02:30:59 +00001444 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
1445 type = eSymbolTypeObjCClass;
1446 }
1447 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
1448 {
Sean Callanand0b7cfa2011-12-03 04:38:43 +00001449 symbol_name_non_abi_mangled = symbol_name + 1;
Greg Clayton3f69eac2011-12-03 02:30:59 +00001450 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
1451 type = eSymbolTypeObjCMetaClass;
1452 }
1453 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
1454 {
Sean Callanand0b7cfa2011-12-03 04:38:43 +00001455 symbol_name_non_abi_mangled = symbol_name + 1;
Greg Clayton3f69eac2011-12-03 02:30:59 +00001456 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
1457 type = eSymbolTypeObjCIVar;
1458 }
1459 }
1460 }
1461 else
1462 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1463 {
1464 type = eSymbolTypeException;
1465 }
1466 else
1467 {
1468 type = eSymbolTypeData;
1469 }
1470 }
1471 else
1472 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1473 {
1474 type = eSymbolTypeTrampoline;
1475 }
1476 else
1477 if (symbol_section->IsDescendant(objc_section_sp.get()))
1478 {
1479 type = eSymbolTypeRuntime;
1480 if (symbol_name && symbol_name[0] == '.')
1481 {
1482 llvm::StringRef symbol_name_ref(symbol_name);
1483 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
1484 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
1485 {
1486 symbol_name_non_abi_mangled = symbol_name;
1487 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
1488 type = eSymbolTypeObjCClass;
1489 }
1490 }
1491 }
1492 }
1493 }
1494 break;
1495 }
1496 }
1497
1498 if (add_nlist)
1499 {
1500 uint64_t symbol_value = nlist.n_value;
1501 bool symbol_name_is_mangled = false;
1502
1503 if (symbol_name_non_abi_mangled)
1504 {
1505 sym[sym_idx].GetMangled().SetMangledName (symbol_name_non_abi_mangled);
1506 sym[sym_idx].GetMangled().SetDemangledName (symbol_name);
Chris Lattner24943d22010-06-08 16:52:24 +00001507 }
1508 else
1509 {
Greg Clayton3f69eac2011-12-03 02:30:59 +00001510 if (symbol_name && symbol_name[0] == '_')
Chris Lattner24943d22010-06-08 16:52:24 +00001511 {
Greg Clayton3f69eac2011-12-03 02:30:59 +00001512 symbol_name_is_mangled = symbol_name[1] == '_';
1513 symbol_name++; // Skip the leading underscore
1514 }
Chris Lattner24943d22010-06-08 16:52:24 +00001515
Greg Clayton038f1c02011-12-03 03:02:17 +00001516 if (symbol_name)
Greg Clayton3f69eac2011-12-03 02:30:59 +00001517 {
1518 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
1519 }
Chris Lattner24943d22010-06-08 16:52:24 +00001520 }
Greg Clayton576a68b2010-09-08 16:38:06 +00001521
Greg Clayton7c36fa02010-09-11 03:13:28 +00001522 if (is_debug == false)
Greg Clayton576a68b2010-09-08 16:38:06 +00001523 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001524 if (type == eSymbolTypeCode)
Greg Clayton576a68b2010-09-08 16:38:06 +00001525 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001526 // See if we can find a N_FUN entry for any code symbols.
1527 // If we do find a match, and the name matches, then we
1528 // can merge the two into just the function symbol to avoid
1529 // duplicate entries in the symbol table
1530 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1531 if (pos != N_FUN_addr_to_sym_idx.end())
Greg Clayton576a68b2010-09-08 16:38:06 +00001532 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001533 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1534 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1535 {
Greg Clayton637029b2010-09-12 05:25:16 +00001536 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001537 // We just need the flags from the linker symbol, so put these flags
1538 // into the N_FUN flags to avoid duplicate symbols in the symbol table
1539 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1540 sym[sym_idx].Clear();
1541 continue;
1542 }
Greg Clayton576a68b2010-09-08 16:38:06 +00001543 }
1544 }
Greg Clayton7c36fa02010-09-11 03:13:28 +00001545 else if (type == eSymbolTypeData)
Greg Clayton576a68b2010-09-08 16:38:06 +00001546 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001547 // See if we can find a N_STSYM entry for any data symbols.
1548 // If we do find a match, and the name matches, then we
1549 // can merge the two into just the Static symbol to avoid
1550 // duplicate entries in the symbol table
1551 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
1552 if (pos != N_STSYM_addr_to_sym_idx.end())
Greg Clayton576a68b2010-09-08 16:38:06 +00001553 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001554 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1555 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1556 {
Greg Clayton637029b2010-09-12 05:25:16 +00001557 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001558 // We just need the flags from the linker symbol, so put these flags
1559 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
1560 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1561 sym[sym_idx].Clear();
1562 continue;
1563 }
Greg Clayton576a68b2010-09-08 16:38:06 +00001564 }
1565 }
1566 }
Chris Lattner24943d22010-06-08 16:52:24 +00001567 if (symbol_section != NULL)
1568 symbol_value -= symbol_section->GetFileAddress();
1569
1570 sym[sym_idx].SetID (nlist_idx);
1571 sym[sym_idx].SetType (type);
Chris Lattner24943d22010-06-08 16:52:24 +00001572 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1573 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1574 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1575
1576 ++sym_idx;
1577 }
1578 else
1579 {
1580 sym[sym_idx].Clear();
1581 }
1582
1583 }
1584
Chris Lattner24943d22010-06-08 16:52:24 +00001585 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1586 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1587 // such entries by figuring out what the address for the global is by looking up this non-STAB
1588 // entry and copying the value into the debug symbol's value to save us the hassle in the
1589 // debug symbol parser.
1590
1591 Symbol *global_symbol = NULL;
1592 for (nlist_idx = 0;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001593 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +00001594 nlist_idx++)
1595 {
1596 if (global_symbol->GetValue().GetFileAddress() == 0)
1597 {
1598 std::vector<uint32_t> indexes;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001599 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001600 {
1601 std::vector<uint32_t>::const_iterator pos;
1602 std::vector<uint32_t>::const_iterator end = indexes.end();
1603 for (pos = indexes.begin(); pos != end; ++pos)
1604 {
1605 symbol_ptr = symtab->SymbolAtIndex(*pos);
1606 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1607 {
1608 global_symbol->SetValue(symbol_ptr->GetValue());
1609 break;
1610 }
1611 }
1612 }
1613 }
1614 }
Greg Clayton637029b2010-09-12 05:25:16 +00001615
1616 // Trim our symbols down to just what we ended up with after
1617 // removing any symbols.
1618 if (sym_idx < num_syms)
1619 {
1620 num_syms = sym_idx;
1621 sym = symtab->Resize (num_syms);
1622 }
1623
Chris Lattner24943d22010-06-08 16:52:24 +00001624 // Now synthesize indirect symbols
1625 if (m_dysymtab.nindirectsyms != 0)
1626 {
Greg Claytondb2dc2b2012-01-12 05:25:17 +00001627 DataExtractor indirect_symbol_index_data (m_data, m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4);
Chris Lattner24943d22010-06-08 16:52:24 +00001628
Greg Claytondb2dc2b2012-01-12 05:25:17 +00001629 if (indirect_symbol_index_data.GetByteSize())
Chris Lattner24943d22010-06-08 16:52:24 +00001630 {
Greg Clayton637029b2010-09-12 05:25:16 +00001631 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
Chris Lattner24943d22010-06-08 16:52:24 +00001632
1633 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1634 {
Greg Clayton1674b122010-07-21 22:12:05 +00001635 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
Chris Lattner24943d22010-06-08 16:52:24 +00001636 {
1637 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1638 if (symbol_stub_byte_size == 0)
1639 continue;
1640
1641 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1642
1643 if (num_symbol_stubs == 0)
1644 continue;
1645
1646 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
Greg Clayton637029b2010-09-12 05:25:16 +00001647 uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms;
Chris Lattner24943d22010-06-08 16:52:24 +00001648 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1649 {
1650 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1651 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1652 uint32_t symbol_stub_offset = symbol_stub_index * 4;
1653 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1654 {
Greg Clayton637029b2010-09-12 05:25:16 +00001655 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Greg Clayton6af4fad2010-10-06 01:26:32 +00001656 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
1657 continue;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001658
Greg Clayton637029b2010-09-12 05:25:16 +00001659 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
1660 Symbol *stub_symbol = NULL;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001661 if (index_pos != end_index_pos)
Greg Clayton637029b2010-09-12 05:25:16 +00001662 {
1663 // We have a remapping from the original nlist index to
1664 // a current symbol index, so just look this up by index
1665 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
1666 }
1667 else
1668 {
1669 // We need to lookup a symbol using the original nlist
1670 // symbol index since this index is coming from the
1671 // S_SYMBOL_STUBS
1672 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
1673 }
Greg Clayton0ad086f2010-09-07 17:36:17 +00001674
1675 assert (stub_symbol);
Chris Lattner24943d22010-06-08 16:52:24 +00001676 if (stub_symbol)
1677 {
1678 Address so_addr(symbol_stub_addr, section_list);
1679
Greg Claytona1b9a902011-11-13 04:15:56 +00001680 if (stub_symbol->GetType() == eSymbolTypeUndefined)
Chris Lattner24943d22010-06-08 16:52:24 +00001681 {
1682 // Change the external symbol into a trampoline that makes sense
1683 // These symbols were N_UNDF N_EXT, and are useless to us, so we
1684 // can re-use them so we don't have to make up a synthetic symbol
1685 // for no good reason.
1686 stub_symbol->SetType (eSymbolTypeTrampoline);
1687 stub_symbol->SetExternal (false);
1688 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1689 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1690 }
1691 else
1692 {
1693 // Make a synthetic symbol to describe the trampoline stub
1694 if (sym_idx >= num_syms)
Greg Clayton637029b2010-09-12 05:25:16 +00001695 sym = symtab->Resize (++num_syms);
1696 sym[sym_idx].SetID (synthetic_stub_sym_id++);
Chris Lattner24943d22010-06-08 16:52:24 +00001697 sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1698 sym[sym_idx].SetType (eSymbolTypeTrampoline);
1699 sym[sym_idx].SetIsSynthetic (true);
1700 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1701 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1702 ++sym_idx;
1703 }
1704 }
1705 }
1706 }
1707 }
1708 }
1709 }
1710 }
Chris Lattner24943d22010-06-08 16:52:24 +00001711 return symtab->GetNumSymbols();
1712 }
1713 }
1714 offset = cmd_offset + symtab_load_command.cmdsize;
1715 }
1716 return 0;
1717}
1718
1719
1720void
1721ObjectFileMachO::Dump (Stream *s)
1722{
1723 lldb_private::Mutex::Locker locker(m_mutex);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001724 s->Printf("%p: ", this);
Chris Lattner24943d22010-06-08 16:52:24 +00001725 s->Indent();
Greg Clayton1674b122010-07-21 22:12:05 +00001726 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
Chris Lattner24943d22010-06-08 16:52:24 +00001727 s->PutCString("ObjectFileMachO64");
1728 else
1729 s->PutCString("ObjectFileMachO32");
1730
Greg Claytoncf015052010-06-11 03:25:34 +00001731 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00001732
Greg Clayton940b1032011-02-23 00:35:02 +00001733 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00001734
1735 if (m_sections_ap.get())
Greg Clayton58e844b2010-12-08 05:08:21 +00001736 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00001737
1738 if (m_symtab_ap.get())
Greg Clayton8d3802d2010-10-08 04:20:14 +00001739 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
Chris Lattner24943d22010-06-08 16:52:24 +00001740}
1741
1742
1743bool
Greg Clayton0467c782011-02-04 18:53:10 +00001744ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00001745{
1746 lldb_private::Mutex::Locker locker(m_mutex);
1747 struct uuid_command load_cmd;
1748 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1749 uint32_t i;
1750 for (i=0; i<m_header.ncmds; ++i)
1751 {
1752 const uint32_t cmd_offset = offset;
1753 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1754 break;
1755
Greg Clayton1674b122010-07-21 22:12:05 +00001756 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00001757 {
1758 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1759 if (uuid_bytes)
1760 {
1761 uuid->SetBytes (uuid_bytes);
1762 return true;
1763 }
1764 return false;
1765 }
1766 offset = cmd_offset + load_cmd.cmdsize;
1767 }
1768 return false;
1769}
1770
1771
1772uint32_t
1773ObjectFileMachO::GetDependentModules (FileSpecList& files)
1774{
1775 lldb_private::Mutex::Locker locker(m_mutex);
1776 struct load_command load_cmd;
1777 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1778 uint32_t count = 0;
Greg Clayton54b38412011-05-24 23:06:02 +00001779 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
Chris Lattner24943d22010-06-08 16:52:24 +00001780 uint32_t i;
1781 for (i=0; i<m_header.ncmds; ++i)
1782 {
1783 const uint32_t cmd_offset = offset;
1784 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1785 break;
1786
1787 switch (load_cmd.cmd)
1788 {
Greg Clayton1674b122010-07-21 22:12:05 +00001789 case LoadCommandDylibLoad:
1790 case LoadCommandDylibLoadWeak:
1791 case LoadCommandDylibReexport:
1792 case LoadCommandDynamicLinkerLoad:
1793 case LoadCommandFixedVMShlibLoad:
Greg Clayton08a73202010-10-09 00:48:53 +00001794 case LoadCommandDylibLoadUpward:
Chris Lattner24943d22010-06-08 16:52:24 +00001795 {
1796 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1797 const char *path = m_data.PeekCStr(name_offset);
1798 // Skip any path that starts with '@' since these are usually:
1799 // @executable_path/.../file
1800 // @rpath/.../file
1801 if (path && path[0] != '@')
1802 {
Greg Clayton54b38412011-05-24 23:06:02 +00001803 FileSpec file_spec(path, resolve_path);
Chris Lattner24943d22010-06-08 16:52:24 +00001804 if (files.AppendIfUnique(file_spec))
1805 count++;
1806 }
1807 }
1808 break;
1809
1810 default:
1811 break;
1812 }
1813 offset = cmd_offset + load_cmd.cmdsize;
1814 }
1815 return count;
1816}
1817
Jim Ingham28775942011-03-07 23:44:08 +00001818lldb_private::Address
1819ObjectFileMachO::GetEntryPointAddress ()
1820{
1821 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
1822 // is initialized to an invalid address, so we can just return that.
1823 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
1824
1825 if (!IsExecutable() || m_entry_point_address.IsValid())
1826 return m_entry_point_address;
1827
1828 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
1829 // /usr/include/mach-o.h, but it is basically:
1830 //
1831 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
1832 // uint32_t count - this is the count of longs in the thread state data
1833 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
1834 // <repeat this trio>
1835 //
1836 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
1837 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
1838 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
1839 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
1840 //
1841 // For now we hard-code the offsets and flavors we need:
1842 //
1843 //
1844
1845 lldb_private::Mutex::Locker locker(m_mutex);
1846 struct load_command load_cmd;
1847 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1848 uint32_t i;
1849 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
1850 bool done = false;
1851
1852 for (i=0; i<m_header.ncmds; ++i)
1853 {
1854 const uint32_t cmd_offset = offset;
1855 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1856 break;
1857
1858 switch (load_cmd.cmd)
1859 {
1860 case LoadCommandUnixThread:
1861 case LoadCommandThread:
1862 {
1863 while (offset < cmd_offset + load_cmd.cmdsize)
1864 {
1865 uint32_t flavor = m_data.GetU32(&offset);
1866 uint32_t count = m_data.GetU32(&offset);
1867 if (count == 0)
1868 {
1869 // We've gotten off somehow, log and exit;
1870 return m_entry_point_address;
1871 }
1872
1873 switch (m_header.cputype)
1874 {
1875 case llvm::MachO::CPUTypeARM:
1876 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
1877 {
1878 offset += 60; // This is the offset of pc in the GPR thread state data structure.
1879 start_address = m_data.GetU32(&offset);
1880 done = true;
1881 }
1882 break;
1883 case llvm::MachO::CPUTypeI386:
1884 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
1885 {
1886 offset += 40; // This is the offset of eip in the GPR thread state data structure.
1887 start_address = m_data.GetU32(&offset);
1888 done = true;
1889 }
1890 break;
1891 case llvm::MachO::CPUTypeX86_64:
1892 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
1893 {
1894 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
1895 start_address = m_data.GetU64(&offset);
1896 done = true;
1897 }
1898 break;
1899 default:
1900 return m_entry_point_address;
1901 }
1902 // Haven't found the GPR flavor yet, skip over the data for this flavor:
1903 if (done)
1904 break;
1905 offset += count * 4;
1906 }
1907 }
1908 break;
1909
1910 default:
1911 break;
1912 }
1913 if (done)
1914 break;
1915
1916 // Go to the next load command:
1917 offset = cmd_offset + load_cmd.cmdsize;
1918 }
1919
1920 if (start_address != LLDB_INVALID_ADDRESS)
1921 {
1922 // We got the start address from the load commands, so now resolve that address in the sections
1923 // of this ObjectFile:
1924 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
1925 {
1926 m_entry_point_address.Clear();
1927 }
1928 }
1929 else
1930 {
1931 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
1932 // "start" symbol in the main executable.
1933
1934 SymbolContextList contexts;
1935 SymbolContext context;
Sean Callananaa4a5532011-10-13 16:49:47 +00001936 if (!m_module->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
Jim Ingham28775942011-03-07 23:44:08 +00001937 return m_entry_point_address;
1938
1939 contexts.GetContextAtIndex(0, context);
1940
1941 m_entry_point_address = context.symbol->GetValue();
1942 }
1943
1944 return m_entry_point_address;
1945
1946}
1947
Greg Claytonb5a8f142012-02-05 02:38:54 +00001948lldb_private::Address
1949ObjectFileMachO::GetHeaderAddress ()
1950{
1951 lldb_private::Address header_addr;
1952 SectionList *section_list = GetSectionList();
1953 if (section_list)
1954 {
1955 SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT()));
1956 if (text_segment_sp)
1957 {
1958 header_addr.SetSection (text_segment_sp.get());
1959 header_addr.SetOffset (0);
1960 }
1961 }
1962 return header_addr;
1963}
1964
1965
Greg Claytonca319972011-07-09 00:41:34 +00001966ObjectFile::Type
1967ObjectFileMachO::CalculateType()
1968{
1969 switch (m_header.filetype)
1970 {
1971 case HeaderFileTypeObject: // 0x1u MH_OBJECT
1972 if (GetAddressByteSize () == 4)
1973 {
1974 // 32 bit kexts are just object files, but they do have a valid
1975 // UUID load command.
1976 UUID uuid;
1977 if (GetUUID(&uuid))
1978 {
1979 // this checking for the UUID load command is not enough
1980 // we could eventually look for the symbol named
1981 // "OSKextGetCurrentIdentifier" as this is required of kexts
1982 if (m_strata == eStrataInvalid)
1983 m_strata = eStrataKernel;
1984 return eTypeSharedLibrary;
1985 }
1986 }
1987 return eTypeObjectFile;
1988
1989 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
1990 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
1991 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
1992 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
1993 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
1994 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
1995 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
1996 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
1997 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
1998 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
1999 default:
2000 break;
2001 }
2002 return eTypeUnknown;
2003}
2004
2005ObjectFile::Strata
2006ObjectFileMachO::CalculateStrata()
2007{
2008 switch (m_header.filetype)
2009 {
2010 case HeaderFileTypeObject: // 0x1u MH_OBJECT
2011 {
2012 // 32 bit kexts are just object files, but they do have a valid
2013 // UUID load command.
2014 UUID uuid;
2015 if (GetUUID(&uuid))
2016 {
2017 // this checking for the UUID load command is not enough
2018 // we could eventually look for the symbol named
2019 // "OSKextGetCurrentIdentifier" as this is required of kexts
2020 if (m_type == eTypeInvalid)
2021 m_type = eTypeSharedLibrary;
2022
2023 return eStrataKernel;
2024 }
2025 }
2026 return eStrataUnknown;
2027
2028 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
2029 // Check for the MH_DYLDLINK bit in the flags
2030 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
2031 return eStrataUser;
2032 return eStrataKernel;
2033
2034 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
2035 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
2036 case HeaderFileTypePreloadedExecutable: return eStrataUser; // 0x5u MH_PRELOAD
2037 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
2038 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
2039 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
2040 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
2041 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
2042 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
2043 default:
2044 break;
2045 }
2046 return eStrataUnknown;
2047}
2048
2049
Chris Lattner24943d22010-06-08 16:52:24 +00002050bool
Greg Clayton395fc332011-02-15 21:59:32 +00002051ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00002052{
2053 lldb_private::Mutex::Locker locker(m_mutex);
Greg Claytonb3448432011-03-24 21:19:54 +00002054 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Greg Clayton6a64bbf2011-09-21 03:57:31 +00002055
2056 // Files with type MH_PRELOAD are currently used in cases where the image
2057 // debugs at the addresses in the file itself. Below we set the OS to
2058 // unknown to make sure we use the DynamicLoaderStatic()...
2059 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
2060 {
2061 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
2062 }
2063
Greg Clayton395fc332011-02-15 21:59:32 +00002064 return true;
Chris Lattner24943d22010-06-08 16:52:24 +00002065}
2066
2067
2068//------------------------------------------------------------------
2069// PluginInterface protocol
2070//------------------------------------------------------------------
2071const char *
2072ObjectFileMachO::GetPluginName()
2073{
2074 return "ObjectFileMachO";
2075}
2076
2077const char *
2078ObjectFileMachO::GetShortPluginName()
2079{
2080 return GetPluginNameStatic();
2081}
2082
2083uint32_t
2084ObjectFileMachO::GetPluginVersion()
2085{
2086 return 1;
2087}
2088