blob: 0e9b1219d092c9f1dd5d10646379ad0a5f2e895e [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
Jim Ingham672e6f52011-03-07 23:44:08 +000010#include "llvm/Support/MachO.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "ObjectFileMachO.h"
13
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/ArchSpec.h"
15#include "lldb/Core/DataBuffer.h"
Greg Clayton53239f02011-02-08 05:05:52 +000016#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-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"
Sean Callananb6d70eb2011-10-12 02:08:07 +000025#include "lldb/Symbol/ClangNamespaceDecl.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Symbol/ObjectFile.h"
27
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
29using namespace lldb;
30using namespace lldb_private;
Greg Claytone1a916a2010-07-21 22:12:05 +000031using namespace llvm::MachO;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Greg Claytonded470d2011-03-19 01:12:21 +000033#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034
35void
36ObjectFileMachO::Initialize()
37{
38 PluginManager::RegisterPlugin (GetPluginNameStatic(),
39 GetPluginDescriptionStatic(),
40 CreateInstance);
41}
42
43void
44ObjectFileMachO::Terminate()
45{
46 PluginManager::UnregisterPlugin (CreateInstance);
47}
48
49
50const char *
51ObjectFileMachO::GetPluginNameStatic()
52{
53 return "object-file.mach-o";
54}
55
56const char *
57ObjectFileMachO::GetPluginDescriptionStatic()
58{
59 return "Mach-o object file reader (32 and 64 bit)";
60}
61
62
63ObjectFile *
64ObjectFileMachO::CreateInstance (Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length)
65{
66 if (ObjectFileMachO::MagicBytesMatch(dataSP))
67 {
68 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module, dataSP, file, offset, length));
69 if (objfile_ap.get() && objfile_ap->ParseHeader())
70 return objfile_ap.release();
71 }
72 return NULL;
73}
74
75
76static uint32_t
77MachHeaderSizeFromMagic(uint32_t magic)
78{
79 switch (magic)
80 {
Greg Claytone1a916a2010-07-21 22:12:05 +000081 case HeaderMagic32:
82 case HeaderMagic32Swapped:
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083 return sizeof(struct mach_header);
84
Greg Claytone1a916a2010-07-21 22:12:05 +000085 case HeaderMagic64:
86 case HeaderMagic64Swapped:
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 return sizeof(struct mach_header_64);
88 break;
89
90 default:
91 break;
92 }
93 return 0;
94}
95
96
97bool
98ObjectFileMachO::MagicBytesMatch (DataBufferSP& dataSP)
99{
Greg Clayton7fb56d02011-02-01 01:31:41 +0000100 DataExtractor data(dataSP, lldb::endian::InlHostByteOrder(), 4);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 uint32_t offset = 0;
102 uint32_t magic = data.GetU32(&offset);
103 return MachHeaderSizeFromMagic(magic) != 0;
104}
105
106
107ObjectFileMachO::ObjectFileMachO(Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) :
108 ObjectFile(module, file, offset, length, dataSP),
109 m_mutex (Mutex::eMutexTypeRecursive),
110 m_header(),
111 m_sections_ap(),
Jim Ingham672e6f52011-03-07 23:44:08 +0000112 m_symtab_ap(),
113 m_entry_point_address ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114{
Greg Clayton72b77eb2011-02-04 21:13:05 +0000115 ::memset (&m_header, 0, sizeof(m_header));
116 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117}
118
119
120ObjectFileMachO::~ObjectFileMachO()
121{
122}
123
124
125bool
126ObjectFileMachO::ParseHeader ()
127{
128 lldb_private::Mutex::Locker locker(m_mutex);
129 bool can_parse = false;
130 uint32_t offset = 0;
Greg Clayton7fb56d02011-02-01 01:31:41 +0000131 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132 // Leave magic in the original byte order
133 m_header.magic = m_data.GetU32(&offset);
134 switch (m_header.magic)
135 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000136 case HeaderMagic32:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000137 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 m_data.SetAddressByteSize(4);
139 can_parse = true;
140 break;
141
Greg Claytone1a916a2010-07-21 22:12:05 +0000142 case HeaderMagic64:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000143 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144 m_data.SetAddressByteSize(8);
145 can_parse = true;
146 break;
147
Greg Claytone1a916a2010-07-21 22:12:05 +0000148 case HeaderMagic32Swapped:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000149 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150 m_data.SetAddressByteSize(4);
151 can_parse = true;
152 break;
153
Greg Claytone1a916a2010-07-21 22:12:05 +0000154 case HeaderMagic64Swapped:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000155 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156 m_data.SetAddressByteSize(8);
157 can_parse = true;
158 break;
159
160 default:
161 break;
162 }
163
164 if (can_parse)
165 {
166 m_data.GetU32(&offset, &m_header.cputype, 6);
167
Greg Clayton41f92322010-06-11 03:25:34 +0000168 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jim Ingham5aee1622010-08-09 23:31:02 +0000169
170 if (SetModulesArchitecture (mach_arch))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 {
172 // Read in all only the load command data
173 DataBufferSP data_sp(m_file.ReadFileContents(m_offset, m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic)));
174 m_data.SetData (data_sp);
175 return true;
176 }
177 }
178 else
179 {
180 memset(&m_header, 0, sizeof(struct mach_header));
181 }
182 return false;
183}
184
185
186ByteOrder
187ObjectFileMachO::GetByteOrder () const
188{
189 lldb_private::Mutex::Locker locker(m_mutex);
190 return m_data.GetByteOrder ();
191}
192
Jim Ingham5aee1622010-08-09 23:31:02 +0000193bool
194ObjectFileMachO::IsExecutable() const
195{
196 return m_header.filetype == HeaderFileTypeExecutable;
197}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198
199size_t
200ObjectFileMachO::GetAddressByteSize () const
201{
202 lldb_private::Mutex::Locker locker(m_mutex);
203 return m_data.GetAddressByteSize ();
204}
205
Greg Claytone0d378b2011-03-24 21:19:54 +0000206AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +0000207ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
208{
209 Symtab *symtab = GetSymtab();
210 if (symtab)
211 {
212 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
213 if (symbol)
214 {
215 const AddressRange *range_ptr = symbol->GetAddressRangePtr();
216 if (range_ptr)
217 {
218 const Section *section = range_ptr->GetBaseAddress().GetSection();
219 if (section)
220 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000221 const SectionType section_type = section->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000222 switch (section_type)
223 {
224 case eSectionTypeInvalid: return eAddressClassUnknown;
225 case eSectionTypeCode:
226 if (m_header.cputype == llvm::MachO::CPUTypeARM)
227 {
228 // For ARM we have a bit in the n_desc field of the symbol
229 // that tells us ARM/Thumb which is bit 0x0008.
230 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
231 return eAddressClassCodeAlternateISA;
232 }
233 return eAddressClassCode;
234
235 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton5009f9d2011-10-27 17:55:14 +0000236 case eSectionTypeData:
237 case eSectionTypeDataCString:
238 case eSectionTypeDataCStringPointers:
239 case eSectionTypeDataSymbolAddress:
240 case eSectionTypeData4:
241 case eSectionTypeData8:
242 case eSectionTypeData16:
243 case eSectionTypeDataPointers:
244 case eSectionTypeZeroFill:
245 case eSectionTypeDataObjCMessageRefs:
246 case eSectionTypeDataObjCCFStrings:
247 return eAddressClassData;
248 case eSectionTypeDebug:
249 case eSectionTypeDWARFDebugAbbrev:
250 case eSectionTypeDWARFDebugAranges:
251 case eSectionTypeDWARFDebugFrame:
252 case eSectionTypeDWARFDebugInfo:
253 case eSectionTypeDWARFDebugLine:
254 case eSectionTypeDWARFDebugLoc:
255 case eSectionTypeDWARFDebugMacInfo:
256 case eSectionTypeDWARFDebugPubNames:
257 case eSectionTypeDWARFDebugPubTypes:
258 case eSectionTypeDWARFDebugRanges:
259 case eSectionTypeDWARFDebugStr:
260 case eSectionTypeDWARFAppleNames:
261 case eSectionTypeDWARFAppleTypes:
262 case eSectionTypeDWARFAppleNamespaces:
263 case eSectionTypeDWARFAppleObjC:
264 return eAddressClassDebug;
Greg Claytonded470d2011-03-19 01:12:21 +0000265 case eSectionTypeEHFrame: return eAddressClassRuntime;
266 case eSectionTypeOther: return eAddressClassUnknown;
267 }
268 }
269 }
270
Greg Claytone0d378b2011-03-24 21:19:54 +0000271 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000272 switch (symbol_type)
273 {
274 case eSymbolTypeAny: return eAddressClassUnknown;
275 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Greg Claytonded470d2011-03-19 01:12:21 +0000276
277 case eSymbolTypeCode:
278 case eSymbolTypeTrampoline:
279 if (m_header.cputype == llvm::MachO::CPUTypeARM)
280 {
281 // For ARM we have a bit in the n_desc field of the symbol
282 // that tells us ARM/Thumb which is bit 0x0008.
283 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
284 return eAddressClassCodeAlternateISA;
285 }
286 return eAddressClassCode;
287
288 case eSymbolTypeData: return eAddressClassData;
289 case eSymbolTypeRuntime: return eAddressClassRuntime;
290 case eSymbolTypeException: return eAddressClassRuntime;
291 case eSymbolTypeSourceFile: return eAddressClassDebug;
292 case eSymbolTypeHeaderFile: return eAddressClassDebug;
293 case eSymbolTypeObjectFile: return eAddressClassDebug;
294 case eSymbolTypeCommonBlock: return eAddressClassDebug;
295 case eSymbolTypeBlock: return eAddressClassDebug;
296 case eSymbolTypeLocal: return eAddressClassData;
297 case eSymbolTypeParam: return eAddressClassData;
298 case eSymbolTypeVariable: return eAddressClassData;
299 case eSymbolTypeVariableType: return eAddressClassDebug;
300 case eSymbolTypeLineEntry: return eAddressClassDebug;
301 case eSymbolTypeLineHeader: return eAddressClassDebug;
302 case eSymbolTypeScopeBegin: return eAddressClassDebug;
303 case eSymbolTypeScopeEnd: return eAddressClassDebug;
304 case eSymbolTypeAdditional: return eAddressClassUnknown;
305 case eSymbolTypeCompiler: return eAddressClassDebug;
306 case eSymbolTypeInstrumentation:return eAddressClassDebug;
307 case eSymbolTypeUndefined: return eAddressClassUnknown;
308 }
309 }
310 }
311 return eAddressClassUnknown;
312}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313
314Symtab *
315ObjectFileMachO::GetSymtab()
316{
Greg Clayton1a65ae12011-01-25 23:55:37 +0000317 lldb_private::Mutex::Locker symfile_locker(m_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318 if (m_symtab_ap.get() == NULL)
319 {
320 m_symtab_ap.reset(new Symtab(this));
Greg Clayton1a65ae12011-01-25 23:55:37 +0000321 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000322 ParseSymtab (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 }
324 return m_symtab_ap.get();
325}
326
327
328SectionList *
329ObjectFileMachO::GetSectionList()
330{
331 lldb_private::Mutex::Locker locker(m_mutex);
332 if (m_sections_ap.get() == NULL)
333 {
334 m_sections_ap.reset(new SectionList());
335 ParseSections();
336 }
337 return m_sections_ap.get();
338}
339
340
341size_t
342ObjectFileMachO::ParseSections ()
343{
344 lldb::user_id_t segID = 0;
345 lldb::user_id_t sectID = 0;
346 struct segment_command_64 load_cmd;
347 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
348 uint32_t i;
349 //bool dump_sections = false;
350 for (i=0; i<m_header.ncmds; ++i)
351 {
352 const uint32_t load_cmd_offset = offset;
353 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
354 break;
355
Greg Claytone1a916a2010-07-21 22:12:05 +0000356 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357 {
358 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
359 {
360 load_cmd.vmaddr = m_data.GetAddress(&offset);
361 load_cmd.vmsize = m_data.GetAddress(&offset);
362 load_cmd.fileoff = m_data.GetAddress(&offset);
363 load_cmd.filesize = m_data.GetAddress(&offset);
364 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
365 {
Greg Clayton414f5d32011-01-25 02:58:48 +0000366
367 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
368
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 // Keep a list of mach segments around in case we need to
370 // get at data that isn't stored in the abstracted Sections.
371 m_mach_segments.push_back (load_cmd);
372
373 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
374 // Use a segment ID of the segment index shifted left by 8 so they
375 // never conflict with any of the sections.
376 SectionSP segment_sp;
377 if (segment_name)
378 {
379 segment_sp.reset(new Section (NULL,
380 GetModule(), // Module to which this section belongs
381 ++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
382 segment_name, // Name of this section
383 eSectionTypeContainer, // This section is a container of other sections.
384 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
385 load_cmd.vmsize, // VM size in bytes of this section
386 load_cmd.fileoff, // Offset to the data for this section in the file
387 load_cmd.filesize, // Size in bytes of this section as found in the the file
388 load_cmd.flags)); // Flags for this section
389
Greg Clayton414f5d32011-01-25 02:58:48 +0000390 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391 m_sections_ap->AddSection(segment_sp);
392 }
393
394 struct section_64 sect64;
Greg Clayton72b77eb2011-02-04 21:13:05 +0000395 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396 // Push a section into our mach sections for the section at
Greg Claytonf4abd0d2010-10-06 01:26:32 +0000397 // index zero (NListSectionNoSection) if we don't have any
398 // mach sections yet...
399 if (m_mach_sections.empty())
400 m_mach_sections.push_back(sect64);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401 uint32_t segment_sect_idx;
402 const lldb::user_id_t first_segment_sectID = sectID + 1;
403
404
Greg Claytone1a916a2010-07-21 22:12:05 +0000405 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
407 {
408 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
409 break;
410 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
411 break;
412 sect64.addr = m_data.GetAddress(&offset);
413 sect64.size = m_data.GetAddress(&offset);
414
415 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
416 break;
417
418 // Keep a list of mach sections around in case we need to
419 // get at data that isn't stored in the abstracted Sections.
420 m_mach_sections.push_back (sect64);
421
422 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
423 if (!segment_name)
424 {
425 // We have a segment with no name so we need to conjure up
426 // segments that correspond to the section's segname if there
427 // isn't already such a section. If there is such a section,
428 // we resize the section so that it spans all sections.
429 // We also mark these sections as fake so address matches don't
430 // hit if they land in the gaps between the child sections.
431 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
432 segment_sp = m_sections_ap->FindSectionByName (segment_name);
433 if (segment_sp.get())
434 {
435 Section *segment = segment_sp.get();
436 // Grow the section size as needed.
437 const lldb::addr_t sect64_min_addr = sect64.addr;
438 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
439 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
440 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
441 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
442 if (sect64_min_addr >= curr_seg_min_addr)
443 {
444 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
445 // Only grow the section size if needed
446 if (new_seg_byte_size > curr_seg_byte_size)
447 segment->SetByteSize (new_seg_byte_size);
448 }
449 else
450 {
451 // We need to change the base address of the segment and
452 // adjust the child section offsets for all existing children.
453 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
454 segment->Slide(slide_amount, false);
455 segment->GetChildren().Slide (-slide_amount, false);
456 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
457 }
Greg Clayton8d38ac42010-06-28 23:51:11 +0000458
459 // Grow the section size as needed.
460 if (sect64.offset)
461 {
462 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
463 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
464
465 const lldb::addr_t section_min_file_offset = sect64.offset;
466 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
467 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
468 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
469 segment->SetFileOffset (new_file_offset);
470 segment->SetFileSize (new_file_size);
471 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 }
473 else
474 {
475 // Create a fake section for the section's named segment
476 segment_sp.reset(new Section(segment_sp.get(), // Parent section
477 GetModule(), // Module to which this section belongs
478 ++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
479 segment_name, // Name of this section
480 eSectionTypeContainer, // This section is a container of other sections.
481 sect64.addr, // File VM address == addresses as they are found in the object file
482 sect64.size, // VM size in bytes of this section
483 sect64.offset, // Offset to the data for this section in the file
484 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
485 load_cmd.flags)); // Flags for this section
486 segment_sp->SetIsFake(true);
487 m_sections_ap->AddSection(segment_sp);
Greg Clayton414f5d32011-01-25 02:58:48 +0000488 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 }
490 }
491 assert (segment_sp.get());
492
Greg Claytone1a916a2010-07-21 22:12:05 +0000493 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 static ConstString g_sect_name_objc_data ("__objc_data");
495 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
496 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
497 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
498 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
499 static ConstString g_sect_name_objc_const ("__objc_const");
500 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
501 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000502
503 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
504 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
505 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
506 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
507 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
508 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
509 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
510 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
511 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
512 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
513 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Clayton17674402011-09-28 17:06:40 +0000514 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
515 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton7f995132011-10-04 22:41:51 +0000516 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton5009f9d2011-10-27 17:55:14 +0000517 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000518 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton89411422010-10-08 00:21:05 +0000519 static ConstString g_sect_name_DATA ("__DATA");
520 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000521
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522 SectionType sect_type = eSectionTypeOther;
523
Greg Clayton4ceb9982010-07-21 22:54:26 +0000524 if (section_name == g_sect_name_dwarf_debug_abbrev)
525 sect_type = eSectionTypeDWARFDebugAbbrev;
526 else if (section_name == g_sect_name_dwarf_debug_aranges)
527 sect_type = eSectionTypeDWARFDebugAranges;
528 else if (section_name == g_sect_name_dwarf_debug_frame)
529 sect_type = eSectionTypeDWARFDebugFrame;
530 else if (section_name == g_sect_name_dwarf_debug_info)
531 sect_type = eSectionTypeDWARFDebugInfo;
532 else if (section_name == g_sect_name_dwarf_debug_line)
533 sect_type = eSectionTypeDWARFDebugLine;
534 else if (section_name == g_sect_name_dwarf_debug_loc)
535 sect_type = eSectionTypeDWARFDebugLoc;
536 else if (section_name == g_sect_name_dwarf_debug_macinfo)
537 sect_type = eSectionTypeDWARFDebugMacInfo;
538 else if (section_name == g_sect_name_dwarf_debug_pubnames)
539 sect_type = eSectionTypeDWARFDebugPubNames;
540 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
541 sect_type = eSectionTypeDWARFDebugPubTypes;
542 else if (section_name == g_sect_name_dwarf_debug_ranges)
543 sect_type = eSectionTypeDWARFDebugRanges;
544 else if (section_name == g_sect_name_dwarf_debug_str)
545 sect_type = eSectionTypeDWARFDebugStr;
Greg Clayton17674402011-09-28 17:06:40 +0000546 else if (section_name == g_sect_name_dwarf_apple_names)
547 sect_type = eSectionTypeDWARFAppleNames;
548 else if (section_name == g_sect_name_dwarf_apple_types)
549 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton7f995132011-10-04 22:41:51 +0000550 else if (section_name == g_sect_name_dwarf_apple_namespaces)
551 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton5009f9d2011-10-27 17:55:14 +0000552 else if (section_name == g_sect_name_dwarf_apple_objc)
553 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton4ceb9982010-07-21 22:54:26 +0000554 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton4ceb9982010-07-21 22:54:26 +0000558 else if (section_name == g_sect_name_eh_frame)
559 sect_type = eSectionTypeEHFrame;
560 else if (section_name == g_sect_name_cfstring)
561 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562 else if (section_name == g_sect_name_objc_data ||
563 section_name == g_sect_name_objc_classrefs ||
564 section_name == g_sect_name_objc_superrefs ||
565 section_name == g_sect_name_objc_const ||
566 section_name == g_sect_name_objc_classlist)
567 {
568 sect_type = eSectionTypeDataPointers;
569 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570
571 if (sect_type == eSectionTypeOther)
572 {
573 switch (mach_sect_type)
574 {
575 // TODO: categorize sections by other flags for regular sections
Greg Clayton89411422010-10-08 00:21:05 +0000576 case SectionTypeRegular:
577 if (segment_sp->GetName() == g_sect_name_TEXT)
578 sect_type = eSectionTypeCode;
579 else if (segment_sp->GetName() == g_sect_name_DATA)
580 sect_type = eSectionTypeData;
581 else
582 sect_type = eSectionTypeOther;
583 break;
Greg Claytone1a916a2010-07-21 22:12:05 +0000584 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
585 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
586 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
587 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
588 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
589 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
590 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
591 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
592 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
593 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
594 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
595 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
596 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
597 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
598 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
599 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 default: break;
601 }
602 }
603
604 SectionSP section_sp(new Section(segment_sp.get(),
605 GetModule(),
606 ++sectID,
607 section_name,
608 sect_type,
609 sect64.addr - segment_sp->GetFileAddress(),
610 sect64.size,
611 sect64.offset,
612 sect64.offset == 0 ? 0 : sect64.size,
613 sect64.flags));
Greg Clayton414f5d32011-01-25 02:58:48 +0000614 // Set the section to be encrypted to match the segment
615 section_sp->SetIsEncrypted (segment_is_encrypted);
616
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 segment_sp->GetChildren().AddSection(section_sp);
618
619 if (segment_sp->IsFake())
620 {
621 segment_sp.reset();
622 segment_name.Clear();
623 }
624 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000625 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 {
627 if (first_segment_sectID <= sectID)
628 {
629 lldb::user_id_t sect_uid;
630 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
631 {
632 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
633 SectionSP next_section_sp;
634 if (sect_uid + 1 <= sectID)
635 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
636
637 if (curr_section_sp.get())
638 {
639 if (curr_section_sp->GetByteSize() == 0)
640 {
641 if (next_section_sp.get() != NULL)
642 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
643 else
644 curr_section_sp->SetByteSize ( load_cmd.vmsize );
645 }
646 }
647 }
648 }
649 }
650 }
651 }
652 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000653 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 {
655 m_dysymtab.cmd = load_cmd.cmd;
656 m_dysymtab.cmdsize = load_cmd.cmdsize;
657 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
658 }
659
660 offset = load_cmd_offset + load_cmd.cmdsize;
661 }
662// if (dump_sections)
663// {
664// StreamFile s(stdout);
665// m_sections_ap->Dump(&s, true);
666// }
667 return sectID; // Return the number of sections we registered with the module
668}
669
670class MachSymtabSectionInfo
671{
672public:
673
674 MachSymtabSectionInfo (SectionList *section_list) :
675 m_section_list (section_list),
676 m_section_infos()
677 {
678 // Get the number of sections down to a depth of 1 to include
679 // all segments and their sections, but no other sections that
680 // may be added for debug map or
681 m_section_infos.resize(section_list->GetNumSections(1));
682 }
683
684
685 Section *
686 GetSection (uint8_t n_sect, addr_t file_addr)
687 {
688 if (n_sect == 0)
689 return NULL;
690 if (n_sect < m_section_infos.size())
691 {
692 if (m_section_infos[n_sect].section == NULL)
693 {
694 Section *section = m_section_list->FindSectionByID (n_sect).get();
695 m_section_infos[n_sect].section = section;
Greg Claytondda0d122011-07-10 17:32:33 +0000696 if (section != NULL)
697 {
698 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
699 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
700 }
701 else
702 {
703 fprintf (stderr, "error: unable to find section for section %u\n", n_sect);
704 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705 }
706 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +0000707 {
708 // Symbol is in section.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 return m_section_infos[n_sect].section;
Greg Clayton8f258512011-08-26 20:01:35 +0000710 }
711 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
712 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
713 {
714 // Symbol is in section with zero size, but has the same start
715 // address as the section. This can happen with linker symbols
716 // (symbols that start with the letter 'l' or 'L'.
717 return m_section_infos[n_sect].section;
718 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 }
720 return m_section_list->FindSectionContainingFileAddress(file_addr).get();
721 }
722
723protected:
724 struct SectionInfo
725 {
726 SectionInfo () :
727 vm_range(),
728 section (NULL)
729 {
730 }
731
732 VMRange vm_range;
733 Section *section;
734 };
735 SectionList *m_section_list;
736 std::vector<SectionInfo> m_section_infos;
737};
738
739
740
741size_t
742ObjectFileMachO::ParseSymtab (bool minimize)
743{
744 Timer scoped_timer(__PRETTY_FUNCTION__,
745 "ObjectFileMachO::ParseSymtab () module = %s",
746 m_file.GetFilename().AsCString(""));
747 struct symtab_command symtab_load_command;
748 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
749 uint32_t i;
750 for (i=0; i<m_header.ncmds; ++i)
751 {
752 const uint32_t cmd_offset = offset;
753 // Read in the load command and load command size
754 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
755 break;
756 // Watch for the symbol table load command
Greg Claytone1a916a2010-07-21 22:12:05 +0000757 if (symtab_load_command.cmd == LoadCommandSymtab)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758 {
759 // Read in the rest of the symtab load command
Jason Molendaea84e762010-07-06 22:38:03 +0000760 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761 {
762 Symtab *symtab = m_symtab_ap.get();
763 SectionList *section_list = GetSectionList();
764 assert(section_list);
765 const size_t addr_size = m_data.GetAddressByteSize();
766 const ByteOrder endian = m_data.GetByteOrder();
767 bool bit_width_32 = addr_size == 4;
768 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
769
770 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size));
771 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize));
772
773 const char *strtab_data = (const char *)strtab_data_sp->GetBytes();
Greg Clayton4f8e8692011-10-31 20:50:40 +0000774 const size_t strtab_data_len = strtab_data_sp->GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775
776 static ConstString g_segment_name_TEXT ("__TEXT");
777 static ConstString g_segment_name_DATA ("__DATA");
778 static ConstString g_segment_name_OBJC ("__OBJC");
779 static ConstString g_section_name_eh_frame ("__eh_frame");
780 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
781 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
782 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
783 SectionSP eh_frame_section_sp;
784 if (text_section_sp.get())
785 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
786 else
787 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
788
Greg Claytone1a916a2010-07-21 22:12:05 +0000789 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790 //uint32_t symtab_offset = 0;
791 const uint8_t* nlist_data = symtab_data_sp->GetBytes();
792 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms);
793
794
Greg Clayton7fb56d02011-02-01 01:31:41 +0000795 if (endian != lldb::endian::InlHostByteOrder())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796 {
797 // ...
798 assert (!"UNIMPLEMENTED: Swap all nlist entries");
799 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000800 uint32_t N_SO_index = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801
802 MachSymtabSectionInfo section_info (section_list);
803 std::vector<uint32_t> N_FUN_indexes;
804 std::vector<uint32_t> N_NSYM_indexes;
805 std::vector<uint32_t> N_INCL_indexes;
806 std::vector<uint32_t> N_BRAC_indexes;
807 std::vector<uint32_t> N_COMM_indexes;
Greg Clayton928d8292010-09-08 16:38:06 +0000808 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000809 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Clayton928d8292010-09-08 16:38:06 +0000810 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
811 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000812 // Any symbols that get merged into another will get an entry
813 // in this map so we know
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000814 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815 uint32_t nlist_idx = 0;
816 Symbol *symbol_ptr = NULL;
817
818 uint32_t sym_idx = 0;
819 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
820 uint32_t num_syms = symtab->GetNumSymbols();
821
822 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
823 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
824 {
825 struct nlist_64 nlist;
826 if (bit_width_32)
827 {
828 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size));
Greg Claytone1a916a2010-07-21 22:12:05 +0000829 nlist.n_strx = nlist32_ptr->n_strx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000830 nlist.n_type = nlist32_ptr->n_type;
831 nlist.n_sect = nlist32_ptr->n_sect;
832 nlist.n_desc = nlist32_ptr->n_desc;
833 nlist.n_value = nlist32_ptr->n_value;
834 }
835 else
836 {
837 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size)));
838 }
839
840 SymbolType type = eSymbolTypeInvalid;
Greg Clayton4f8e8692011-10-31 20:50:40 +0000841 if (nlist.n_strx >= strtab_data_len)
842 {
843 // No symbol should be NULL, even the symbols with no
844 // string values should have an offset zero which points
845 // to an empty C-string
846 fprintf (stderr,
847 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
848 nlist_idx,
849 nlist.n_strx,
850 m_module->GetFileSpec().GetDirectory().GetCString(),
851 m_module->GetFileSpec().GetFilename().GetCString());
852 continue;
853 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000854 const char* symbol_name = &strtab_data[nlist.n_strx];
Greg Clayton4f8e8692011-10-31 20:50:40 +0000855
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856 if (symbol_name[0] == '\0')
857 symbol_name = NULL;
858 Section* symbol_section = NULL;
859 bool add_nlist = true;
Greg Claytone1a916a2010-07-21 22:12:05 +0000860 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861
862 assert (sym_idx < num_syms);
863
864 sym[sym_idx].SetDebug (is_debug);
865
866 if (is_debug)
867 {
868 switch (nlist.n_type)
869 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000870 case StabGlobalSymbol:
871 // N_GSYM -- global symbol: name,,NO_SECT,type,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872 // Sometimes the N_GSYM value contains the address.
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000873 sym[sym_idx].SetExternal(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874 if (nlist.n_value != 0)
875 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000876 type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000877 break;
878
Greg Claytone1a916a2010-07-21 22:12:05 +0000879 case StabFunctionName:
880 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000881 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882 break;
883
Greg Claytone1a916a2010-07-21 22:12:05 +0000884 case StabFunction:
885 // N_FUN -- procedure: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886 if (symbol_name)
887 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000888 type = eSymbolTypeCode;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton928d8292010-09-08 16:38:06 +0000890
891 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892 // We use the current number of symbols in the symbol table in lieu of
893 // using nlist_idx in case we ever start trimming entries out
894 N_FUN_indexes.push_back(sym_idx);
895 }
896 else
897 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000898 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899
900 if ( !N_FUN_indexes.empty() )
901 {
902 // Copy the size of the function into the original STAB entry so we don't have
903 // to hunt for it later
904 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
905 N_FUN_indexes.pop_back();
Jason Molendaea84e762010-07-06 22:38:03 +0000906 // We don't really need the end function STAB as it contains the size which
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907 // we already placed with the original symbol, so don't add it if we want a
908 // minimal symbol table
909 if (minimize)
910 add_nlist = false;
911 }
912 }
913 break;
914
Greg Claytone1a916a2010-07-21 22:12:05 +0000915 case StabStaticSymbol:
916 // N_STSYM -- static symbol: name,,n_sect,type,address
Greg Clayton928d8292010-09-08 16:38:06 +0000917 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000919 type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000920 break;
921
Greg Claytone1a916a2010-07-21 22:12:05 +0000922 case StabLocalCommon:
923 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
925 type = eSymbolTypeCommonBlock;
926 break;
927
Greg Claytone1a916a2010-07-21 22:12:05 +0000928 case StabBeginSymbol:
929 // N_BNSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000930 // We use the current number of symbols in the symbol table in lieu of
931 // using nlist_idx in case we ever start trimming entries out
932 if (minimize)
933 {
934 // Skip these if we want minimal symbol tables
935 add_nlist = false;
936 }
937 else
938 {
939 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
940 N_NSYM_indexes.push_back(sym_idx);
941 type = eSymbolTypeScopeBegin;
942 }
943 break;
944
Greg Claytone1a916a2010-07-21 22:12:05 +0000945 case StabEndSymbol:
946 // N_ENSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000947 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
948 // so that we can always skip the entire symbol if we need to navigate
949 // more quickly at the source level when parsing STABS
950 if (minimize)
951 {
952 // Skip these if we want minimal symbol tables
953 add_nlist = false;
954 }
955 else
956 {
957 if ( !N_NSYM_indexes.empty() )
958 {
959 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
960 symbol_ptr->SetByteSize(sym_idx + 1);
961 symbol_ptr->SetSizeIsSibling(true);
962 N_NSYM_indexes.pop_back();
963 }
964 type = eSymbolTypeScopeEnd;
965 }
966 break;
967
968
Greg Claytone1a916a2010-07-21 22:12:05 +0000969 case StabSourceFileOptions:
970 // N_OPT - emitted with gcc2_compiled and in gcc source
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971 type = eSymbolTypeCompiler;
972 break;
973
Greg Claytone1a916a2010-07-21 22:12:05 +0000974 case StabRegisterSymbol:
975 // N_RSYM - register sym: name,,NO_SECT,type,register
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000976 type = eSymbolTypeVariable;
977 break;
978
Greg Claytone1a916a2010-07-21 22:12:05 +0000979 case StabSourceLine:
980 // N_SLINE - src line: 0,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
982 type = eSymbolTypeLineEntry;
983 break;
984
Greg Claytone1a916a2010-07-21 22:12:05 +0000985 case StabStructureType:
986 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987 type = eSymbolTypeVariableType;
988 break;
989
Greg Claytone1a916a2010-07-21 22:12:05 +0000990 case StabSourceFileName:
991 // N_SO - source file name
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992 type = eSymbolTypeSourceFile;
993 if (symbol_name == NULL)
994 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000995 if (minimize)
996 add_nlist = false;
997 if (N_SO_index != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998 {
999 // Set the size of the N_SO to the terminating index of this N_SO
1000 // so that we can always skip the entire N_SO if we need to navigate
1001 // more quickly at the source level when parsing STABS
1002 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001003 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004 symbol_ptr->SetSizeIsSibling(true);
1005 }
1006 N_NSYM_indexes.clear();
1007 N_INCL_indexes.clear();
1008 N_BRAC_indexes.clear();
1009 N_COMM_indexes.clear();
1010 N_FUN_indexes.clear();
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001011 N_SO_index = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001013 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001014 {
1015 // We use the current number of symbols in the symbol table in lieu of
1016 // using nlist_idx in case we ever start trimming entries out
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001017 if (symbol_name[0] == '/')
1018 N_SO_index = sym_idx;
Greg Clayton5cf21f52011-06-19 04:26:01 +00001019 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001020 {
1021 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1022 if (so_path && so_path[0])
1023 {
1024 std::string full_so_path (so_path);
1025 if (*full_so_path.rbegin() != '/')
1026 full_so_path += '/';
1027 full_so_path += symbol_name;
1028 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
1029 add_nlist = false;
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001030 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001031 }
1032 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001033 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001034
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001035 break;
1036
Greg Claytone1a916a2010-07-21 22:12:05 +00001037 case StabObjectFileName:
1038 // N_OSO - object file name: name,,0,0,st_mtime
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039 type = eSymbolTypeObjectFile;
1040 break;
1041
Greg Claytone1a916a2010-07-21 22:12:05 +00001042 case StabLocalSymbol:
1043 // N_LSYM - local sym: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001044 type = eSymbolTypeLocal;
1045 break;
1046
1047 //----------------------------------------------------------------------
1048 // INCL scopes
1049 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +00001050 case StabBeginIncludeFileName:
1051 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052 // We use the current number of symbols in the symbol table in lieu of
1053 // using nlist_idx in case we ever start trimming entries out
1054 N_INCL_indexes.push_back(sym_idx);
1055 type = eSymbolTypeScopeBegin;
1056 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057
Greg Claytone1a916a2010-07-21 22:12:05 +00001058 case StabEndIncludeFile:
1059 // N_EINCL - include file end: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001060 // Set the size of the N_BINCL to the terminating index of this N_EINCL
1061 // so that we can always skip the entire symbol if we need to navigate
1062 // more quickly at the source level when parsing STABS
1063 if ( !N_INCL_indexes.empty() )
1064 {
1065 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1066 symbol_ptr->SetByteSize(sym_idx + 1);
1067 symbol_ptr->SetSizeIsSibling(true);
1068 N_INCL_indexes.pop_back();
1069 }
1070 type = eSymbolTypeScopeEnd;
1071 break;
1072
Greg Claytone1a916a2010-07-21 22:12:05 +00001073 case StabIncludeFileName:
1074 // N_SOL - #included file name: name,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075 type = eSymbolTypeHeaderFile;
Greg Clayton49bd1c82010-09-07 17:36:17 +00001076
1077 // We currently don't use the header files on darwin
1078 if (minimize)
1079 add_nlist = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080 break;
1081
Greg Claytone1a916a2010-07-21 22:12:05 +00001082 case StabCompilerParameters:
1083 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084 type = eSymbolTypeCompiler;
1085 break;
1086
Greg Claytone1a916a2010-07-21 22:12:05 +00001087 case StabCompilerVersion:
1088 // N_VERSION - compiler version: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 type = eSymbolTypeCompiler;
1090 break;
1091
Greg Claytone1a916a2010-07-21 22:12:05 +00001092 case StabCompilerOptLevel:
1093 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094 type = eSymbolTypeCompiler;
1095 break;
1096
Greg Claytone1a916a2010-07-21 22:12:05 +00001097 case StabParameter:
1098 // N_PSYM - parameter: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099 type = eSymbolTypeVariable;
1100 break;
1101
Greg Claytone1a916a2010-07-21 22:12:05 +00001102 case StabAlternateEntry:
1103 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1105 type = eSymbolTypeLineEntry;
1106 break;
1107
1108 //----------------------------------------------------------------------
1109 // Left and Right Braces
1110 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +00001111 case StabLeftBracket:
1112 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001113 // We use the current number of symbols in the symbol table in lieu of
1114 // using nlist_idx in case we ever start trimming entries out
1115 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1116 N_BRAC_indexes.push_back(sym_idx);
1117 type = eSymbolTypeScopeBegin;
1118 break;
1119
Greg Claytone1a916a2010-07-21 22:12:05 +00001120 case StabRightBracket:
1121 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
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 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1126 if ( !N_BRAC_indexes.empty() )
1127 {
1128 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1129 symbol_ptr->SetByteSize(sym_idx + 1);
1130 symbol_ptr->SetSizeIsSibling(true);
1131 N_BRAC_indexes.pop_back();
1132 }
1133 type = eSymbolTypeScopeEnd;
1134 break;
1135
Greg Claytone1a916a2010-07-21 22:12:05 +00001136 case StabDeletedIncludeFile:
1137 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138 type = eSymbolTypeHeaderFile;
1139 break;
1140
1141 //----------------------------------------------------------------------
1142 // COMM scopes
1143 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +00001144 case StabBeginCommon:
1145 // N_BCOMM - begin common: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146 // We use the current number of symbols in the symbol table in lieu of
1147 // using nlist_idx in case we ever start trimming entries out
1148 type = eSymbolTypeScopeBegin;
1149 N_COMM_indexes.push_back(sym_idx);
1150 break;
1151
Greg Claytone1a916a2010-07-21 22:12:05 +00001152 case StabEndCommonLocal:
1153 // N_ECOML - end common (local name): 0,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001154 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1155 // Fall through
1156
Greg Claytone1a916a2010-07-21 22:12:05 +00001157 case StabEndCommon:
1158 // N_ECOMM - end common: name,,n_sect,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1160 // so that we can always skip the entire symbol if we need to navigate
1161 // more quickly at the source level when parsing STABS
1162 if ( !N_COMM_indexes.empty() )
1163 {
1164 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1165 symbol_ptr->SetByteSize(sym_idx + 1);
1166 symbol_ptr->SetSizeIsSibling(true);
1167 N_COMM_indexes.pop_back();
1168 }
1169 type = eSymbolTypeScopeEnd;
1170 break;
1171
Greg Claytone1a916a2010-07-21 22:12:05 +00001172 case StabLength:
1173 // N_LENG - second stab entry with length information
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174 type = eSymbolTypeAdditional;
1175 break;
1176
1177 default: break;
1178 }
1179 }
1180 else
1181 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001182 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
1183 uint8_t n_type = NlistMaskType & nlist.n_type;
1184 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001185
1186 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name)
1187 {
1188 type = eSymbolTypeRuntime;
1189 }
1190 else
1191 {
1192 switch (n_type)
1193 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001194 case NListTypeIndirect: // N_INDR - Fall through
1195 case NListTypePreboundUndefined:// N_PBUD - Fall through
1196 case NListTypeUndefined: // N_UNDF
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001197 type = eSymbolTypeUndefined;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 break;
1199
Greg Claytone1a916a2010-07-21 22:12:05 +00001200 case NListTypeAbsolute: // N_ABS
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201 type = eSymbolTypeAbsolute;
1202 break;
1203
Greg Claytone1a916a2010-07-21 22:12:05 +00001204 case NListTypeSection: // N_SECT
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001205 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1206
Greg Clayton8f258512011-08-26 20:01:35 +00001207 if (symbol_section == NULL)
1208 {
1209 // TODO: warn about this?
1210 add_nlist = false;
1211 break;
1212 }
1213
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214 if (TEXT_eh_frame_sectID == nlist.n_sect)
1215 {
1216 type = eSymbolTypeException;
1217 }
1218 else
1219 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001220 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221
1222 switch (section_type)
1223 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001224 case SectionTypeRegular: break; // regular section
1225 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
1226 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
1227 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
1228 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
1229 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1230 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1231 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1232 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1233 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
1234 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
1235 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
1236 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
1237 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
1238 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
1239 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
1240 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001241 default: break;
1242 }
1243
1244 if (type == eSymbolTypeInvalid)
1245 {
1246 const char *symbol_sect_name = symbol_section->GetName().AsCString();
1247 if (symbol_section->IsDescendant (text_section_sp.get()))
1248 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001249 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1250 SectionAttrUserSelfModifyingCode |
1251 SectionAttrSytemSomeInstructions))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252 type = eSymbolTypeData;
1253 else
1254 type = eSymbolTypeCode;
1255 }
1256 else
1257 if (symbol_section->IsDescendant(data_section_sp.get()))
1258 {
1259 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1260 {
1261 type = eSymbolTypeRuntime;
1262 }
1263 else
1264 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1265 {
1266 type = eSymbolTypeException;
1267 }
1268 else
1269 {
1270 type = eSymbolTypeData;
1271 }
1272 }
1273 else
1274 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1275 {
1276 type = eSymbolTypeTrampoline;
1277 }
1278 else
1279 if (symbol_section->IsDescendant(objc_section_sp.get()))
1280 {
1281 type = eSymbolTypeRuntime;
1282 }
1283 }
1284 }
1285 break;
Greg Clayton928d8292010-09-08 16:38:06 +00001286 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287 }
1288 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001289 if (add_nlist)
1290 {
1291 bool symbol_name_is_mangled = false;
1292 if (symbol_name && symbol_name[0] == '_')
1293 {
1294 symbol_name_is_mangled = symbol_name[1] == '_';
1295 symbol_name++; // Skip the leading underscore
1296 }
1297 uint64_t symbol_value = nlist.n_value;
Greg Clayton928d8292010-09-08 16:38:06 +00001298
1299 if (symbol_name)
1300 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001301 if (is_debug == false)
Greg Clayton928d8292010-09-08 16:38:06 +00001302 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001303 if (type == eSymbolTypeCode)
Greg Clayton928d8292010-09-08 16:38:06 +00001304 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001305 // See if we can find a N_FUN entry for any code symbols.
1306 // If we do find a match, and the name matches, then we
1307 // can merge the two into just the function symbol to avoid
1308 // duplicate entries in the symbol table
1309 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1310 if (pos != N_FUN_addr_to_sym_idx.end())
Greg Clayton928d8292010-09-08 16:38:06 +00001311 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001312 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1313 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1314 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001315 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001316 // We just need the flags from the linker symbol, so put these flags
1317 // into the N_FUN flags to avoid duplicate symbols in the symbol table
1318 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1319 sym[sym_idx].Clear();
1320 continue;
1321 }
Greg Clayton928d8292010-09-08 16:38:06 +00001322 }
1323 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001324 else if (type == eSymbolTypeData)
Greg Clayton928d8292010-09-08 16:38:06 +00001325 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001326 // See if we can find a N_STSYM entry for any data symbols.
1327 // If we do find a match, and the name matches, then we
1328 // can merge the two into just the Static symbol to avoid
1329 // duplicate entries in the symbol table
1330 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
1331 if (pos != N_STSYM_addr_to_sym_idx.end())
Greg Clayton928d8292010-09-08 16:38:06 +00001332 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001333 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1334 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1335 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001336 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001337 // We just need the flags from the linker symbol, so put these flags
1338 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
1339 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1340 sym[sym_idx].Clear();
1341 continue;
1342 }
Greg Clayton928d8292010-09-08 16:38:06 +00001343 }
1344 }
1345 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001346 if (symbol_section != NULL)
1347 symbol_value -= symbol_section->GetFileAddress();
1348
1349 sym[sym_idx].SetID (nlist_idx);
1350 sym[sym_idx].SetType (type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001351 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1352 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1353 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1354
1355 ++sym_idx;
1356 }
1357 else
1358 {
1359 sym[sym_idx].Clear();
1360 }
1361
1362 }
1363
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001364 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1365 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1366 // such entries by figuring out what the address for the global is by looking up this non-STAB
1367 // entry and copying the value into the debug symbol's value to save us the hassle in the
1368 // debug symbol parser.
1369
1370 Symbol *global_symbol = NULL;
1371 for (nlist_idx = 0;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001372 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001373 nlist_idx++)
1374 {
1375 if (global_symbol->GetValue().GetFileAddress() == 0)
1376 {
1377 std::vector<uint32_t> indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001378 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379 {
1380 std::vector<uint32_t>::const_iterator pos;
1381 std::vector<uint32_t>::const_iterator end = indexes.end();
1382 for (pos = indexes.begin(); pos != end; ++pos)
1383 {
1384 symbol_ptr = symtab->SymbolAtIndex(*pos);
1385 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1386 {
1387 global_symbol->SetValue(symbol_ptr->GetValue());
1388 break;
1389 }
1390 }
1391 }
1392 }
1393 }
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001394
1395 // Trim our symbols down to just what we ended up with after
1396 // removing any symbols.
1397 if (sym_idx < num_syms)
1398 {
1399 num_syms = sym_idx;
1400 sym = symtab->Resize (num_syms);
1401 }
1402
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001403 // Now synthesize indirect symbols
1404 if (m_dysymtab.nindirectsyms != 0)
1405 {
1406 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4));
1407
1408 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize())
1409 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001410 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1412
1413 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1414 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001415 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001416 {
1417 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1418 if (symbol_stub_byte_size == 0)
1419 continue;
1420
1421 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1422
1423 if (num_symbol_stubs == 0)
1424 continue;
1425
1426 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001427 uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1429 {
1430 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1431 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1432 uint32_t symbol_stub_offset = symbol_stub_index * 4;
1433 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1434 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001435 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Greg Claytonf4abd0d2010-10-06 01:26:32 +00001436 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
1437 continue;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001438
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001439 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
1440 Symbol *stub_symbol = NULL;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001441 if (index_pos != end_index_pos)
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001442 {
1443 // We have a remapping from the original nlist index to
1444 // a current symbol index, so just look this up by index
1445 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
1446 }
1447 else
1448 {
1449 // We need to lookup a symbol using the original nlist
1450 // symbol index since this index is coming from the
1451 // S_SYMBOL_STUBS
1452 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
1453 }
Greg Clayton49bd1c82010-09-07 17:36:17 +00001454
1455 assert (stub_symbol);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001456 if (stub_symbol)
1457 {
1458 Address so_addr(symbol_stub_addr, section_list);
1459
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001460 if (stub_symbol->GetType() == eSymbolTypeUndefined)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461 {
1462 // Change the external symbol into a trampoline that makes sense
1463 // These symbols were N_UNDF N_EXT, and are useless to us, so we
1464 // can re-use them so we don't have to make up a synthetic symbol
1465 // for no good reason.
1466 stub_symbol->SetType (eSymbolTypeTrampoline);
1467 stub_symbol->SetExternal (false);
1468 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1469 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1470 }
1471 else
1472 {
1473 // Make a synthetic symbol to describe the trampoline stub
1474 if (sym_idx >= num_syms)
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001475 sym = symtab->Resize (++num_syms);
1476 sym[sym_idx].SetID (synthetic_stub_sym_id++);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001477 sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1478 sym[sym_idx].SetType (eSymbolTypeTrampoline);
1479 sym[sym_idx].SetIsSynthetic (true);
1480 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1481 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1482 ++sym_idx;
1483 }
1484 }
1485 }
1486 }
1487 }
1488 }
1489 }
1490 }
1491
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001492 return symtab->GetNumSymbols();
1493 }
1494 }
1495 offset = cmd_offset + symtab_load_command.cmdsize;
1496 }
1497 return 0;
1498}
1499
1500
1501void
1502ObjectFileMachO::Dump (Stream *s)
1503{
1504 lldb_private::Mutex::Locker locker(m_mutex);
Jason Molendafd54b362011-09-20 21:44:10 +00001505 s->Printf("%p: ", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001506 s->Indent();
Greg Claytone1a916a2010-07-21 22:12:05 +00001507 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508 s->PutCString("ObjectFileMachO64");
1509 else
1510 s->PutCString("ObjectFileMachO32");
1511
Greg Clayton41f92322010-06-11 03:25:34 +00001512 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001513
Greg Clayton64195a22011-02-23 00:35:02 +00001514 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001515
1516 if (m_sections_ap.get())
Greg Clayton10177aa2010-12-08 05:08:21 +00001517 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001518
1519 if (m_symtab_ap.get())
Greg Clayton8087ca22010-10-08 04:20:14 +00001520 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001521}
1522
1523
1524bool
Greg Clayton60830262011-02-04 18:53:10 +00001525ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001526{
1527 lldb_private::Mutex::Locker locker(m_mutex);
1528 struct uuid_command load_cmd;
1529 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1530 uint32_t i;
1531 for (i=0; i<m_header.ncmds; ++i)
1532 {
1533 const uint32_t cmd_offset = offset;
1534 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1535 break;
1536
Greg Claytone1a916a2010-07-21 22:12:05 +00001537 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001538 {
1539 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1540 if (uuid_bytes)
1541 {
1542 uuid->SetBytes (uuid_bytes);
1543 return true;
1544 }
1545 return false;
1546 }
1547 offset = cmd_offset + load_cmd.cmdsize;
1548 }
1549 return false;
1550}
1551
1552
1553uint32_t
1554ObjectFileMachO::GetDependentModules (FileSpecList& files)
1555{
1556 lldb_private::Mutex::Locker locker(m_mutex);
1557 struct load_command load_cmd;
1558 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1559 uint32_t count = 0;
Greg Clayton9b72eb72011-05-24 23:06:02 +00001560 const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001561 uint32_t i;
1562 for (i=0; i<m_header.ncmds; ++i)
1563 {
1564 const uint32_t cmd_offset = offset;
1565 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1566 break;
1567
1568 switch (load_cmd.cmd)
1569 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001570 case LoadCommandDylibLoad:
1571 case LoadCommandDylibLoadWeak:
1572 case LoadCommandDylibReexport:
1573 case LoadCommandDynamicLinkerLoad:
1574 case LoadCommandFixedVMShlibLoad:
Greg Clayton74f6e9f2010-10-09 00:48:53 +00001575 case LoadCommandDylibLoadUpward:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001576 {
1577 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1578 const char *path = m_data.PeekCStr(name_offset);
1579 // Skip any path that starts with '@' since these are usually:
1580 // @executable_path/.../file
1581 // @rpath/.../file
1582 if (path && path[0] != '@')
1583 {
Greg Clayton9b72eb72011-05-24 23:06:02 +00001584 FileSpec file_spec(path, resolve_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001585 if (files.AppendIfUnique(file_spec))
1586 count++;
1587 }
1588 }
1589 break;
1590
1591 default:
1592 break;
1593 }
1594 offset = cmd_offset + load_cmd.cmdsize;
1595 }
1596 return count;
1597}
1598
Jim Ingham672e6f52011-03-07 23:44:08 +00001599lldb_private::Address
1600ObjectFileMachO::GetEntryPointAddress ()
1601{
1602 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
1603 // is initialized to an invalid address, so we can just return that.
1604 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
1605
1606 if (!IsExecutable() || m_entry_point_address.IsValid())
1607 return m_entry_point_address;
1608
1609 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
1610 // /usr/include/mach-o.h, but it is basically:
1611 //
1612 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
1613 // uint32_t count - this is the count of longs in the thread state data
1614 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
1615 // <repeat this trio>
1616 //
1617 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
1618 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
1619 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
1620 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
1621 //
1622 // For now we hard-code the offsets and flavors we need:
1623 //
1624 //
1625
1626 lldb_private::Mutex::Locker locker(m_mutex);
1627 struct load_command load_cmd;
1628 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1629 uint32_t i;
1630 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
1631 bool done = false;
1632
1633 for (i=0; i<m_header.ncmds; ++i)
1634 {
1635 const uint32_t cmd_offset = offset;
1636 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1637 break;
1638
1639 switch (load_cmd.cmd)
1640 {
1641 case LoadCommandUnixThread:
1642 case LoadCommandThread:
1643 {
1644 while (offset < cmd_offset + load_cmd.cmdsize)
1645 {
1646 uint32_t flavor = m_data.GetU32(&offset);
1647 uint32_t count = m_data.GetU32(&offset);
1648 if (count == 0)
1649 {
1650 // We've gotten off somehow, log and exit;
1651 return m_entry_point_address;
1652 }
1653
1654 switch (m_header.cputype)
1655 {
1656 case llvm::MachO::CPUTypeARM:
1657 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
1658 {
1659 offset += 60; // This is the offset of pc in the GPR thread state data structure.
1660 start_address = m_data.GetU32(&offset);
1661 done = true;
1662 }
1663 break;
1664 case llvm::MachO::CPUTypeI386:
1665 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
1666 {
1667 offset += 40; // This is the offset of eip in the GPR thread state data structure.
1668 start_address = m_data.GetU32(&offset);
1669 done = true;
1670 }
1671 break;
1672 case llvm::MachO::CPUTypeX86_64:
1673 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
1674 {
1675 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
1676 start_address = m_data.GetU64(&offset);
1677 done = true;
1678 }
1679 break;
1680 default:
1681 return m_entry_point_address;
1682 }
1683 // Haven't found the GPR flavor yet, skip over the data for this flavor:
1684 if (done)
1685 break;
1686 offset += count * 4;
1687 }
1688 }
1689 break;
1690
1691 default:
1692 break;
1693 }
1694 if (done)
1695 break;
1696
1697 // Go to the next load command:
1698 offset = cmd_offset + load_cmd.cmdsize;
1699 }
1700
1701 if (start_address != LLDB_INVALID_ADDRESS)
1702 {
1703 // We got the start address from the load commands, so now resolve that address in the sections
1704 // of this ObjectFile:
1705 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
1706 {
1707 m_entry_point_address.Clear();
1708 }
1709 }
1710 else
1711 {
1712 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
1713 // "start" symbol in the main executable.
1714
1715 SymbolContextList contexts;
1716 SymbolContext context;
Sean Callananb96ff332011-10-13 16:49:47 +00001717 if (!m_module->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
Jim Ingham672e6f52011-03-07 23:44:08 +00001718 return m_entry_point_address;
1719
1720 contexts.GetContextAtIndex(0, context);
1721
1722 m_entry_point_address = context.symbol->GetValue();
1723 }
1724
1725 return m_entry_point_address;
1726
1727}
1728
Greg Clayton9e00b6a652011-07-09 00:41:34 +00001729ObjectFile::Type
1730ObjectFileMachO::CalculateType()
1731{
1732 switch (m_header.filetype)
1733 {
1734 case HeaderFileTypeObject: // 0x1u MH_OBJECT
1735 if (GetAddressByteSize () == 4)
1736 {
1737 // 32 bit kexts are just object files, but they do have a valid
1738 // UUID load command.
1739 UUID uuid;
1740 if (GetUUID(&uuid))
1741 {
1742 // this checking for the UUID load command is not enough
1743 // we could eventually look for the symbol named
1744 // "OSKextGetCurrentIdentifier" as this is required of kexts
1745 if (m_strata == eStrataInvalid)
1746 m_strata = eStrataKernel;
1747 return eTypeSharedLibrary;
1748 }
1749 }
1750 return eTypeObjectFile;
1751
1752 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
1753 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
1754 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
1755 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
1756 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
1757 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
1758 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
1759 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
1760 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
1761 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
1762 default:
1763 break;
1764 }
1765 return eTypeUnknown;
1766}
1767
1768ObjectFile::Strata
1769ObjectFileMachO::CalculateStrata()
1770{
1771 switch (m_header.filetype)
1772 {
1773 case HeaderFileTypeObject: // 0x1u MH_OBJECT
1774 {
1775 // 32 bit kexts are just object files, but they do have a valid
1776 // UUID load command.
1777 UUID uuid;
1778 if (GetUUID(&uuid))
1779 {
1780 // this checking for the UUID load command is not enough
1781 // we could eventually look for the symbol named
1782 // "OSKextGetCurrentIdentifier" as this is required of kexts
1783 if (m_type == eTypeInvalid)
1784 m_type = eTypeSharedLibrary;
1785
1786 return eStrataKernel;
1787 }
1788 }
1789 return eStrataUnknown;
1790
1791 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
1792 // Check for the MH_DYLDLINK bit in the flags
1793 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
1794 return eStrataUser;
1795 return eStrataKernel;
1796
1797 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
1798 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
1799 case HeaderFileTypePreloadedExecutable: return eStrataUser; // 0x5u MH_PRELOAD
1800 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
1801 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
1802 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
1803 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
1804 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
1805 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
1806 default:
1807 break;
1808 }
1809 return eStrataUnknown;
1810}
1811
1812
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001813bool
Greg Clayton514487e2011-02-15 21:59:32 +00001814ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001815{
1816 lldb_private::Mutex::Locker locker(m_mutex);
Greg Claytone0d378b2011-03-24 21:19:54 +00001817 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Greg Clayton593577a2011-09-21 03:57:31 +00001818
1819 // Files with type MH_PRELOAD are currently used in cases where the image
1820 // debugs at the addresses in the file itself. Below we set the OS to
1821 // unknown to make sure we use the DynamicLoaderStatic()...
1822 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
1823 {
1824 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
1825 }
1826
Greg Clayton514487e2011-02-15 21:59:32 +00001827 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001828}
1829
1830
1831//------------------------------------------------------------------
1832// PluginInterface protocol
1833//------------------------------------------------------------------
1834const char *
1835ObjectFileMachO::GetPluginName()
1836{
1837 return "ObjectFileMachO";
1838}
1839
1840const char *
1841ObjectFileMachO::GetShortPluginName()
1842{
1843 return GetPluginNameStatic();
1844}
1845
1846uint32_t
1847ObjectFileMachO::GetPluginVersion()
1848{
1849 return 1;
1850}
1851