blob: 5819defb273e11c7247c604ff0fb9404bccb2cb8 [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;
236 case eSectionTypeData: return eAddressClassData;
Greg Claytone0d378b2011-03-24 21:19:54 +0000237 case eSectionTypeDataCString: return eAddressClassData;
Greg Claytonded470d2011-03-19 01:12:21 +0000238 case eSectionTypeDataCStringPointers: return eAddressClassData;
239 case eSectionTypeDataSymbolAddress: return eAddressClassData;
240 case eSectionTypeData4: return eAddressClassData;
241 case eSectionTypeData8: return eAddressClassData;
242 case eSectionTypeData16: return eAddressClassData;
243 case eSectionTypeDataPointers: return eAddressClassData;
244 case eSectionTypeZeroFill: return eAddressClassData;
Greg Claytone0d378b2011-03-24 21:19:54 +0000245 case eSectionTypeDataObjCMessageRefs: return eAddressClassData;
246 case eSectionTypeDataObjCCFStrings: return eAddressClassData;
Greg Claytonded470d2011-03-19 01:12:21 +0000247 case eSectionTypeDebug: return eAddressClassDebug;
248 case eSectionTypeDWARFDebugAbbrev: return eAddressClassDebug;
249 case eSectionTypeDWARFDebugAranges: return eAddressClassDebug;
250 case eSectionTypeDWARFDebugFrame: return eAddressClassDebug;
251 case eSectionTypeDWARFDebugInfo: return eAddressClassDebug;
252 case eSectionTypeDWARFDebugLine: return eAddressClassDebug;
253 case eSectionTypeDWARFDebugLoc: return eAddressClassDebug;
254 case eSectionTypeDWARFDebugMacInfo: return eAddressClassDebug;
255 case eSectionTypeDWARFDebugPubNames: return eAddressClassDebug;
256 case eSectionTypeDWARFDebugPubTypes: return eAddressClassDebug;
257 case eSectionTypeDWARFDebugRanges: return eAddressClassDebug;
258 case eSectionTypeDWARFDebugStr: return eAddressClassDebug;
Greg Clayton17674402011-09-28 17:06:40 +0000259 case eSectionTypeDWARFAppleNames: return eAddressClassDebug;
260 case eSectionTypeDWARFAppleTypes: return eAddressClassDebug;
Greg Clayton7f995132011-10-04 22:41:51 +0000261 case eSectionTypeDWARFAppleNamespaces: return eAddressClassDebug;
Greg Claytonded470d2011-03-19 01:12:21 +0000262 case eSectionTypeEHFrame: return eAddressClassRuntime;
263 case eSectionTypeOther: return eAddressClassUnknown;
264 }
265 }
266 }
267
Greg Claytone0d378b2011-03-24 21:19:54 +0000268 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000269 switch (symbol_type)
270 {
271 case eSymbolTypeAny: return eAddressClassUnknown;
272 case eSymbolTypeAbsolute: return eAddressClassUnknown;
273 case eSymbolTypeExtern: return eAddressClassUnknown;
274
275 case eSymbolTypeCode:
276 case eSymbolTypeTrampoline:
277 if (m_header.cputype == llvm::MachO::CPUTypeARM)
278 {
279 // For ARM we have a bit in the n_desc field of the symbol
280 // that tells us ARM/Thumb which is bit 0x0008.
281 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
282 return eAddressClassCodeAlternateISA;
283 }
284 return eAddressClassCode;
285
286 case eSymbolTypeData: return eAddressClassData;
287 case eSymbolTypeRuntime: return eAddressClassRuntime;
288 case eSymbolTypeException: return eAddressClassRuntime;
289 case eSymbolTypeSourceFile: return eAddressClassDebug;
290 case eSymbolTypeHeaderFile: return eAddressClassDebug;
291 case eSymbolTypeObjectFile: return eAddressClassDebug;
292 case eSymbolTypeCommonBlock: return eAddressClassDebug;
293 case eSymbolTypeBlock: return eAddressClassDebug;
294 case eSymbolTypeLocal: return eAddressClassData;
295 case eSymbolTypeParam: return eAddressClassData;
296 case eSymbolTypeVariable: return eAddressClassData;
297 case eSymbolTypeVariableType: return eAddressClassDebug;
298 case eSymbolTypeLineEntry: return eAddressClassDebug;
299 case eSymbolTypeLineHeader: return eAddressClassDebug;
300 case eSymbolTypeScopeBegin: return eAddressClassDebug;
301 case eSymbolTypeScopeEnd: return eAddressClassDebug;
302 case eSymbolTypeAdditional: return eAddressClassUnknown;
303 case eSymbolTypeCompiler: return eAddressClassDebug;
304 case eSymbolTypeInstrumentation:return eAddressClassDebug;
305 case eSymbolTypeUndefined: return eAddressClassUnknown;
306 }
307 }
308 }
309 return eAddressClassUnknown;
310}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311
312Symtab *
313ObjectFileMachO::GetSymtab()
314{
Greg Clayton1a65ae12011-01-25 23:55:37 +0000315 lldb_private::Mutex::Locker symfile_locker(m_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316 if (m_symtab_ap.get() == NULL)
317 {
318 m_symtab_ap.reset(new Symtab(this));
Greg Clayton1a65ae12011-01-25 23:55:37 +0000319 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000320 ParseSymtab (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 }
322 return m_symtab_ap.get();
323}
324
325
326SectionList *
327ObjectFileMachO::GetSectionList()
328{
329 lldb_private::Mutex::Locker locker(m_mutex);
330 if (m_sections_ap.get() == NULL)
331 {
332 m_sections_ap.reset(new SectionList());
333 ParseSections();
334 }
335 return m_sections_ap.get();
336}
337
338
339size_t
340ObjectFileMachO::ParseSections ()
341{
342 lldb::user_id_t segID = 0;
343 lldb::user_id_t sectID = 0;
344 struct segment_command_64 load_cmd;
345 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
346 uint32_t i;
347 //bool dump_sections = false;
348 for (i=0; i<m_header.ncmds; ++i)
349 {
350 const uint32_t load_cmd_offset = offset;
351 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
352 break;
353
Greg Claytone1a916a2010-07-21 22:12:05 +0000354 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355 {
356 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
357 {
358 load_cmd.vmaddr = m_data.GetAddress(&offset);
359 load_cmd.vmsize = m_data.GetAddress(&offset);
360 load_cmd.fileoff = m_data.GetAddress(&offset);
361 load_cmd.filesize = m_data.GetAddress(&offset);
362 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
363 {
Greg Clayton414f5d32011-01-25 02:58:48 +0000364
365 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
366
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367 // Keep a list of mach segments around in case we need to
368 // get at data that isn't stored in the abstracted Sections.
369 m_mach_segments.push_back (load_cmd);
370
371 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
372 // Use a segment ID of the segment index shifted left by 8 so they
373 // never conflict with any of the sections.
374 SectionSP segment_sp;
375 if (segment_name)
376 {
377 segment_sp.reset(new Section (NULL,
378 GetModule(), // Module to which this section belongs
379 ++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
380 segment_name, // Name of this section
381 eSectionTypeContainer, // This section is a container of other sections.
382 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
383 load_cmd.vmsize, // VM size in bytes of this section
384 load_cmd.fileoff, // Offset to the data for this section in the file
385 load_cmd.filesize, // Size in bytes of this section as found in the the file
386 load_cmd.flags)); // Flags for this section
387
Greg Clayton414f5d32011-01-25 02:58:48 +0000388 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 m_sections_ap->AddSection(segment_sp);
390 }
391
392 struct section_64 sect64;
Greg Clayton72b77eb2011-02-04 21:13:05 +0000393 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 // Push a section into our mach sections for the section at
Greg Claytonf4abd0d2010-10-06 01:26:32 +0000395 // index zero (NListSectionNoSection) if we don't have any
396 // mach sections yet...
397 if (m_mach_sections.empty())
398 m_mach_sections.push_back(sect64);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399 uint32_t segment_sect_idx;
400 const lldb::user_id_t first_segment_sectID = sectID + 1;
401
402
Greg Claytone1a916a2010-07-21 22:12:05 +0000403 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
405 {
406 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
407 break;
408 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
409 break;
410 sect64.addr = m_data.GetAddress(&offset);
411 sect64.size = m_data.GetAddress(&offset);
412
413 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
414 break;
415
416 // Keep a list of mach sections around in case we need to
417 // get at data that isn't stored in the abstracted Sections.
418 m_mach_sections.push_back (sect64);
419
420 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
421 if (!segment_name)
422 {
423 // We have a segment with no name so we need to conjure up
424 // segments that correspond to the section's segname if there
425 // isn't already such a section. If there is such a section,
426 // we resize the section so that it spans all sections.
427 // We also mark these sections as fake so address matches don't
428 // hit if they land in the gaps between the child sections.
429 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
430 segment_sp = m_sections_ap->FindSectionByName (segment_name);
431 if (segment_sp.get())
432 {
433 Section *segment = segment_sp.get();
434 // Grow the section size as needed.
435 const lldb::addr_t sect64_min_addr = sect64.addr;
436 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
437 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
438 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
439 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
440 if (sect64_min_addr >= curr_seg_min_addr)
441 {
442 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
443 // Only grow the section size if needed
444 if (new_seg_byte_size > curr_seg_byte_size)
445 segment->SetByteSize (new_seg_byte_size);
446 }
447 else
448 {
449 // We need to change the base address of the segment and
450 // adjust the child section offsets for all existing children.
451 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
452 segment->Slide(slide_amount, false);
453 segment->GetChildren().Slide (-slide_amount, false);
454 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
455 }
Greg Clayton8d38ac42010-06-28 23:51:11 +0000456
457 // Grow the section size as needed.
458 if (sect64.offset)
459 {
460 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
461 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
462
463 const lldb::addr_t section_min_file_offset = sect64.offset;
464 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
465 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
466 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
467 segment->SetFileOffset (new_file_offset);
468 segment->SetFileSize (new_file_size);
469 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470 }
471 else
472 {
473 // Create a fake section for the section's named segment
474 segment_sp.reset(new Section(segment_sp.get(), // Parent section
475 GetModule(), // Module to which this section belongs
476 ++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
477 segment_name, // Name of this section
478 eSectionTypeContainer, // This section is a container of other sections.
479 sect64.addr, // File VM address == addresses as they are found in the object file
480 sect64.size, // VM size in bytes of this section
481 sect64.offset, // Offset to the data for this section in the file
482 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
483 load_cmd.flags)); // Flags for this section
484 segment_sp->SetIsFake(true);
485 m_sections_ap->AddSection(segment_sp);
Greg Clayton414f5d32011-01-25 02:58:48 +0000486 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 }
488 }
489 assert (segment_sp.get());
490
Greg Claytone1a916a2010-07-21 22:12:05 +0000491 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492 static ConstString g_sect_name_objc_data ("__objc_data");
493 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
494 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
495 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
496 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
497 static ConstString g_sect_name_objc_const ("__objc_const");
498 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
499 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000500
501 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
502 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
503 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
504 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
505 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
506 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
507 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
508 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
509 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
510 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
511 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Clayton17674402011-09-28 17:06:40 +0000512 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
513 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton7f995132011-10-04 22:41:51 +0000514 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000515 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton89411422010-10-08 00:21:05 +0000516 static ConstString g_sect_name_DATA ("__DATA");
517 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000518
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519 SectionType sect_type = eSectionTypeOther;
520
Greg Clayton4ceb9982010-07-21 22:54:26 +0000521 if (section_name == g_sect_name_dwarf_debug_abbrev)
522 sect_type = eSectionTypeDWARFDebugAbbrev;
523 else if (section_name == g_sect_name_dwarf_debug_aranges)
524 sect_type = eSectionTypeDWARFDebugAranges;
525 else if (section_name == g_sect_name_dwarf_debug_frame)
526 sect_type = eSectionTypeDWARFDebugFrame;
527 else if (section_name == g_sect_name_dwarf_debug_info)
528 sect_type = eSectionTypeDWARFDebugInfo;
529 else if (section_name == g_sect_name_dwarf_debug_line)
530 sect_type = eSectionTypeDWARFDebugLine;
531 else if (section_name == g_sect_name_dwarf_debug_loc)
532 sect_type = eSectionTypeDWARFDebugLoc;
533 else if (section_name == g_sect_name_dwarf_debug_macinfo)
534 sect_type = eSectionTypeDWARFDebugMacInfo;
535 else if (section_name == g_sect_name_dwarf_debug_pubnames)
536 sect_type = eSectionTypeDWARFDebugPubNames;
537 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
538 sect_type = eSectionTypeDWARFDebugPubTypes;
539 else if (section_name == g_sect_name_dwarf_debug_ranges)
540 sect_type = eSectionTypeDWARFDebugRanges;
541 else if (section_name == g_sect_name_dwarf_debug_str)
542 sect_type = eSectionTypeDWARFDebugStr;
Greg Clayton17674402011-09-28 17:06:40 +0000543 else if (section_name == g_sect_name_dwarf_apple_names)
544 sect_type = eSectionTypeDWARFAppleNames;
545 else if (section_name == g_sect_name_dwarf_apple_types)
546 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton7f995132011-10-04 22:41:51 +0000547 else if (section_name == g_sect_name_dwarf_apple_namespaces)
548 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton4ceb9982010-07-21 22:54:26 +0000549 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000552 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton4ceb9982010-07-21 22:54:26 +0000553 else if (section_name == g_sect_name_eh_frame)
554 sect_type = eSectionTypeEHFrame;
555 else if (section_name == g_sect_name_cfstring)
556 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557 else if (section_name == g_sect_name_objc_data ||
558 section_name == g_sect_name_objc_classrefs ||
559 section_name == g_sect_name_objc_superrefs ||
560 section_name == g_sect_name_objc_const ||
561 section_name == g_sect_name_objc_classlist)
562 {
563 sect_type = eSectionTypeDataPointers;
564 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565
566 if (sect_type == eSectionTypeOther)
567 {
568 switch (mach_sect_type)
569 {
570 // TODO: categorize sections by other flags for regular sections
Greg Clayton89411422010-10-08 00:21:05 +0000571 case SectionTypeRegular:
572 if (segment_sp->GetName() == g_sect_name_TEXT)
573 sect_type = eSectionTypeCode;
574 else if (segment_sp->GetName() == g_sect_name_DATA)
575 sect_type = eSectionTypeData;
576 else
577 sect_type = eSectionTypeOther;
578 break;
Greg Claytone1a916a2010-07-21 22:12:05 +0000579 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
580 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
581 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
582 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
583 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
584 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
585 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
586 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
587 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
588 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
589 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
590 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
591 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
592 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
593 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
594 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595 default: break;
596 }
597 }
598
599 SectionSP section_sp(new Section(segment_sp.get(),
600 GetModule(),
601 ++sectID,
602 section_name,
603 sect_type,
604 sect64.addr - segment_sp->GetFileAddress(),
605 sect64.size,
606 sect64.offset,
607 sect64.offset == 0 ? 0 : sect64.size,
608 sect64.flags));
Greg Clayton414f5d32011-01-25 02:58:48 +0000609 // Set the section to be encrypted to match the segment
610 section_sp->SetIsEncrypted (segment_is_encrypted);
611
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612 segment_sp->GetChildren().AddSection(section_sp);
613
614 if (segment_sp->IsFake())
615 {
616 segment_sp.reset();
617 segment_name.Clear();
618 }
619 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000620 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621 {
622 if (first_segment_sectID <= sectID)
623 {
624 lldb::user_id_t sect_uid;
625 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
626 {
627 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
628 SectionSP next_section_sp;
629 if (sect_uid + 1 <= sectID)
630 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
631
632 if (curr_section_sp.get())
633 {
634 if (curr_section_sp->GetByteSize() == 0)
635 {
636 if (next_section_sp.get() != NULL)
637 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
638 else
639 curr_section_sp->SetByteSize ( load_cmd.vmsize );
640 }
641 }
642 }
643 }
644 }
645 }
646 }
647 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000648 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649 {
650 m_dysymtab.cmd = load_cmd.cmd;
651 m_dysymtab.cmdsize = load_cmd.cmdsize;
652 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
653 }
654
655 offset = load_cmd_offset + load_cmd.cmdsize;
656 }
657// if (dump_sections)
658// {
659// StreamFile s(stdout);
660// m_sections_ap->Dump(&s, true);
661// }
662 return sectID; // Return the number of sections we registered with the module
663}
664
665class MachSymtabSectionInfo
666{
667public:
668
669 MachSymtabSectionInfo (SectionList *section_list) :
670 m_section_list (section_list),
671 m_section_infos()
672 {
673 // Get the number of sections down to a depth of 1 to include
674 // all segments and their sections, but no other sections that
675 // may be added for debug map or
676 m_section_infos.resize(section_list->GetNumSections(1));
677 }
678
679
680 Section *
681 GetSection (uint8_t n_sect, addr_t file_addr)
682 {
683 if (n_sect == 0)
684 return NULL;
685 if (n_sect < m_section_infos.size())
686 {
687 if (m_section_infos[n_sect].section == NULL)
688 {
689 Section *section = m_section_list->FindSectionByID (n_sect).get();
690 m_section_infos[n_sect].section = section;
Greg Claytondda0d122011-07-10 17:32:33 +0000691 if (section != NULL)
692 {
693 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
694 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
695 }
696 else
697 {
698 fprintf (stderr, "error: unable to find section for section %u\n", n_sect);
699 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700 }
701 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +0000702 {
703 // Symbol is in section.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 return m_section_infos[n_sect].section;
Greg Clayton8f258512011-08-26 20:01:35 +0000705 }
706 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
707 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
708 {
709 // Symbol is in section with zero size, but has the same start
710 // address as the section. This can happen with linker symbols
711 // (symbols that start with the letter 'l' or 'L'.
712 return m_section_infos[n_sect].section;
713 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 }
715 return m_section_list->FindSectionContainingFileAddress(file_addr).get();
716 }
717
718protected:
719 struct SectionInfo
720 {
721 SectionInfo () :
722 vm_range(),
723 section (NULL)
724 {
725 }
726
727 VMRange vm_range;
728 Section *section;
729 };
730 SectionList *m_section_list;
731 std::vector<SectionInfo> m_section_infos;
732};
733
734
735
736size_t
737ObjectFileMachO::ParseSymtab (bool minimize)
738{
739 Timer scoped_timer(__PRETTY_FUNCTION__,
740 "ObjectFileMachO::ParseSymtab () module = %s",
741 m_file.GetFilename().AsCString(""));
742 struct symtab_command symtab_load_command;
743 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
744 uint32_t i;
745 for (i=0; i<m_header.ncmds; ++i)
746 {
747 const uint32_t cmd_offset = offset;
748 // Read in the load command and load command size
749 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
750 break;
751 // Watch for the symbol table load command
Greg Claytone1a916a2010-07-21 22:12:05 +0000752 if (symtab_load_command.cmd == LoadCommandSymtab)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753 {
754 // Read in the rest of the symtab load command
Jason Molendaea84e762010-07-06 22:38:03 +0000755 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756 {
757 Symtab *symtab = m_symtab_ap.get();
758 SectionList *section_list = GetSectionList();
759 assert(section_list);
760 const size_t addr_size = m_data.GetAddressByteSize();
761 const ByteOrder endian = m_data.GetByteOrder();
762 bool bit_width_32 = addr_size == 4;
763 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
764
765 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size));
766 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize));
767
768 const char *strtab_data = (const char *)strtab_data_sp->GetBytes();
769// DataExtractor symtab_data(symtab_data_sp, endian, addr_size);
770// DataExtractor strtab_data(strtab_data_sp, endian, addr_size);
771
772 static ConstString g_segment_name_TEXT ("__TEXT");
773 static ConstString g_segment_name_DATA ("__DATA");
774 static ConstString g_segment_name_OBJC ("__OBJC");
775 static ConstString g_section_name_eh_frame ("__eh_frame");
776 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
777 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
778 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
779 SectionSP eh_frame_section_sp;
780 if (text_section_sp.get())
781 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
782 else
783 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
784
Greg Claytone1a916a2010-07-21 22:12:05 +0000785 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786 //uint32_t symtab_offset = 0;
787 const uint8_t* nlist_data = symtab_data_sp->GetBytes();
788 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms);
789
790
Greg Clayton7fb56d02011-02-01 01:31:41 +0000791 if (endian != lldb::endian::InlHostByteOrder())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792 {
793 // ...
794 assert (!"UNIMPLEMENTED: Swap all nlist entries");
795 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000796 uint32_t N_SO_index = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797
798 MachSymtabSectionInfo section_info (section_list);
799 std::vector<uint32_t> N_FUN_indexes;
800 std::vector<uint32_t> N_NSYM_indexes;
801 std::vector<uint32_t> N_INCL_indexes;
802 std::vector<uint32_t> N_BRAC_indexes;
803 std::vector<uint32_t> N_COMM_indexes;
Greg Clayton928d8292010-09-08 16:38:06 +0000804 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000805 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Clayton928d8292010-09-08 16:38:06 +0000806 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
807 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000808 // Any symbols that get merged into another will get an entry
809 // in this map so we know
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000810 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811 uint32_t nlist_idx = 0;
812 Symbol *symbol_ptr = NULL;
813
814 uint32_t sym_idx = 0;
815 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
816 uint32_t num_syms = symtab->GetNumSymbols();
817
818 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
819 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
820 {
821 struct nlist_64 nlist;
822 if (bit_width_32)
823 {
824 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size));
Greg Claytone1a916a2010-07-21 22:12:05 +0000825 nlist.n_strx = nlist32_ptr->n_strx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 nlist.n_type = nlist32_ptr->n_type;
827 nlist.n_sect = nlist32_ptr->n_sect;
828 nlist.n_desc = nlist32_ptr->n_desc;
829 nlist.n_value = nlist32_ptr->n_value;
830 }
831 else
832 {
833 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size)));
834 }
835
836 SymbolType type = eSymbolTypeInvalid;
Greg Claytone1a916a2010-07-21 22:12:05 +0000837 const char* symbol_name = &strtab_data[nlist.n_strx];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838 if (symbol_name[0] == '\0')
839 symbol_name = NULL;
840 Section* symbol_section = NULL;
841 bool add_nlist = true;
Greg Claytone1a916a2010-07-21 22:12:05 +0000842 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000843
844 assert (sym_idx < num_syms);
845
846 sym[sym_idx].SetDebug (is_debug);
847
848 if (is_debug)
849 {
850 switch (nlist.n_type)
851 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000852 case StabGlobalSymbol:
853 // N_GSYM -- global symbol: name,,NO_SECT,type,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854 // Sometimes the N_GSYM value contains the address.
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000855 sym[sym_idx].SetExternal(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856 if (nlist.n_value != 0)
857 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000858 type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859 break;
860
Greg Claytone1a916a2010-07-21 22:12:05 +0000861 case StabFunctionName:
862 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000863 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000864 break;
865
Greg Claytone1a916a2010-07-21 22:12:05 +0000866 case StabFunction:
867 // N_FUN -- procedure: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868 if (symbol_name)
869 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000870 type = eSymbolTypeCode;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton928d8292010-09-08 16:38:06 +0000872
873 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874 // We use the current number of symbols in the symbol table in lieu of
875 // using nlist_idx in case we ever start trimming entries out
876 N_FUN_indexes.push_back(sym_idx);
877 }
878 else
879 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000880 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881
882 if ( !N_FUN_indexes.empty() )
883 {
884 // Copy the size of the function into the original STAB entry so we don't have
885 // to hunt for it later
886 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
887 N_FUN_indexes.pop_back();
Jason Molendaea84e762010-07-06 22:38:03 +0000888 // We don't really need the end function STAB as it contains the size which
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889 // we already placed with the original symbol, so don't add it if we want a
890 // minimal symbol table
891 if (minimize)
892 add_nlist = false;
893 }
894 }
895 break;
896
Greg Claytone1a916a2010-07-21 22:12:05 +0000897 case StabStaticSymbol:
898 // N_STSYM -- static symbol: name,,n_sect,type,address
Greg Clayton928d8292010-09-08 16:38:06 +0000899 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000901 type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902 break;
903
Greg Claytone1a916a2010-07-21 22:12:05 +0000904 case StabLocalCommon:
905 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000906 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
907 type = eSymbolTypeCommonBlock;
908 break;
909
Greg Claytone1a916a2010-07-21 22:12:05 +0000910 case StabBeginSymbol:
911 // N_BNSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000912 // We use the current number of symbols in the symbol table in lieu of
913 // using nlist_idx in case we ever start trimming entries out
914 if (minimize)
915 {
916 // Skip these if we want minimal symbol tables
917 add_nlist = false;
918 }
919 else
920 {
921 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
922 N_NSYM_indexes.push_back(sym_idx);
923 type = eSymbolTypeScopeBegin;
924 }
925 break;
926
Greg Claytone1a916a2010-07-21 22:12:05 +0000927 case StabEndSymbol:
928 // N_ENSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000929 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
930 // so that we can always skip the entire symbol if we need to navigate
931 // more quickly at the source level when parsing STABS
932 if (minimize)
933 {
934 // Skip these if we want minimal symbol tables
935 add_nlist = false;
936 }
937 else
938 {
939 if ( !N_NSYM_indexes.empty() )
940 {
941 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
942 symbol_ptr->SetByteSize(sym_idx + 1);
943 symbol_ptr->SetSizeIsSibling(true);
944 N_NSYM_indexes.pop_back();
945 }
946 type = eSymbolTypeScopeEnd;
947 }
948 break;
949
950
Greg Claytone1a916a2010-07-21 22:12:05 +0000951 case StabSourceFileOptions:
952 // N_OPT - emitted with gcc2_compiled and in gcc source
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000953 type = eSymbolTypeCompiler;
954 break;
955
Greg Claytone1a916a2010-07-21 22:12:05 +0000956 case StabRegisterSymbol:
957 // N_RSYM - register sym: name,,NO_SECT,type,register
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000958 type = eSymbolTypeVariable;
959 break;
960
Greg Claytone1a916a2010-07-21 22:12:05 +0000961 case StabSourceLine:
962 // N_SLINE - src line: 0,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
964 type = eSymbolTypeLineEntry;
965 break;
966
Greg Claytone1a916a2010-07-21 22:12:05 +0000967 case StabStructureType:
968 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000969 type = eSymbolTypeVariableType;
970 break;
971
Greg Claytone1a916a2010-07-21 22:12:05 +0000972 case StabSourceFileName:
973 // N_SO - source file name
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000974 type = eSymbolTypeSourceFile;
975 if (symbol_name == NULL)
976 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000977 if (minimize)
978 add_nlist = false;
979 if (N_SO_index != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980 {
981 // Set the size of the N_SO to the terminating index of this N_SO
982 // so that we can always skip the entire N_SO if we need to navigate
983 // more quickly at the source level when parsing STABS
984 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000985 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986 symbol_ptr->SetSizeIsSibling(true);
987 }
988 N_NSYM_indexes.clear();
989 N_INCL_indexes.clear();
990 N_BRAC_indexes.clear();
991 N_COMM_indexes.clear();
992 N_FUN_indexes.clear();
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000993 N_SO_index = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000994 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000995 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996 {
997 // We use the current number of symbols in the symbol table in lieu of
998 // using nlist_idx in case we ever start trimming entries out
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000999 if (symbol_name[0] == '/')
1000 N_SO_index = sym_idx;
Greg Clayton5cf21f52011-06-19 04:26:01 +00001001 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001002 {
1003 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1004 if (so_path && so_path[0])
1005 {
1006 std::string full_so_path (so_path);
1007 if (*full_so_path.rbegin() != '/')
1008 full_so_path += '/';
1009 full_so_path += symbol_name;
1010 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
1011 add_nlist = false;
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001012 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001013 }
1014 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001016
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017 break;
1018
Greg Claytone1a916a2010-07-21 22:12:05 +00001019 case StabObjectFileName:
1020 // N_OSO - object file name: name,,0,0,st_mtime
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021 type = eSymbolTypeObjectFile;
1022 break;
1023
Greg Claytone1a916a2010-07-21 22:12:05 +00001024 case StabLocalSymbol:
1025 // N_LSYM - local sym: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026 type = eSymbolTypeLocal;
1027 break;
1028
1029 //----------------------------------------------------------------------
1030 // INCL scopes
1031 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +00001032 case StabBeginIncludeFileName:
1033 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001034 // We use the current number of symbols in the symbol table in lieu of
1035 // using nlist_idx in case we ever start trimming entries out
1036 N_INCL_indexes.push_back(sym_idx);
1037 type = eSymbolTypeScopeBegin;
1038 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039
Greg Claytone1a916a2010-07-21 22:12:05 +00001040 case StabEndIncludeFile:
1041 // N_EINCL - include file end: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042 // Set the size of the N_BINCL to the terminating index of this N_EINCL
1043 // so that we can always skip the entire symbol if we need to navigate
1044 // more quickly at the source level when parsing STABS
1045 if ( !N_INCL_indexes.empty() )
1046 {
1047 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1048 symbol_ptr->SetByteSize(sym_idx + 1);
1049 symbol_ptr->SetSizeIsSibling(true);
1050 N_INCL_indexes.pop_back();
1051 }
1052 type = eSymbolTypeScopeEnd;
1053 break;
1054
Greg Claytone1a916a2010-07-21 22:12:05 +00001055 case StabIncludeFileName:
1056 // N_SOL - #included file name: name,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057 type = eSymbolTypeHeaderFile;
Greg Clayton49bd1c82010-09-07 17:36:17 +00001058
1059 // We currently don't use the header files on darwin
1060 if (minimize)
1061 add_nlist = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062 break;
1063
Greg Claytone1a916a2010-07-21 22:12:05 +00001064 case StabCompilerParameters:
1065 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066 type = eSymbolTypeCompiler;
1067 break;
1068
Greg Claytone1a916a2010-07-21 22:12:05 +00001069 case StabCompilerVersion:
1070 // N_VERSION - compiler version: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001071 type = eSymbolTypeCompiler;
1072 break;
1073
Greg Claytone1a916a2010-07-21 22:12:05 +00001074 case StabCompilerOptLevel:
1075 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076 type = eSymbolTypeCompiler;
1077 break;
1078
Greg Claytone1a916a2010-07-21 22:12:05 +00001079 case StabParameter:
1080 // N_PSYM - parameter: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081 type = eSymbolTypeVariable;
1082 break;
1083
Greg Claytone1a916a2010-07-21 22:12:05 +00001084 case StabAlternateEntry:
1085 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1087 type = eSymbolTypeLineEntry;
1088 break;
1089
1090 //----------------------------------------------------------------------
1091 // Left and Right Braces
1092 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +00001093 case StabLeftBracket:
1094 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095 // We use the current number of symbols in the symbol table in lieu of
1096 // using nlist_idx in case we ever start trimming entries out
1097 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1098 N_BRAC_indexes.push_back(sym_idx);
1099 type = eSymbolTypeScopeBegin;
1100 break;
1101
Greg Claytone1a916a2010-07-21 22:12:05 +00001102 case StabRightBracket:
1103 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
1105 // so that we can always skip the entire symbol if we need to navigate
1106 // more quickly at the source level when parsing STABS
1107 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1108 if ( !N_BRAC_indexes.empty() )
1109 {
1110 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1111 symbol_ptr->SetByteSize(sym_idx + 1);
1112 symbol_ptr->SetSizeIsSibling(true);
1113 N_BRAC_indexes.pop_back();
1114 }
1115 type = eSymbolTypeScopeEnd;
1116 break;
1117
Greg Claytone1a916a2010-07-21 22:12:05 +00001118 case StabDeletedIncludeFile:
1119 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120 type = eSymbolTypeHeaderFile;
1121 break;
1122
1123 //----------------------------------------------------------------------
1124 // COMM scopes
1125 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +00001126 case StabBeginCommon:
1127 // N_BCOMM - begin common: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128 // We use the current number of symbols in the symbol table in lieu of
1129 // using nlist_idx in case we ever start trimming entries out
1130 type = eSymbolTypeScopeBegin;
1131 N_COMM_indexes.push_back(sym_idx);
1132 break;
1133
Greg Claytone1a916a2010-07-21 22:12:05 +00001134 case StabEndCommonLocal:
1135 // N_ECOML - end common (local name): 0,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001136 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1137 // Fall through
1138
Greg Claytone1a916a2010-07-21 22:12:05 +00001139 case StabEndCommon:
1140 // N_ECOMM - end common: name,,n_sect,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1142 // so that we can always skip the entire symbol if we need to navigate
1143 // more quickly at the source level when parsing STABS
1144 if ( !N_COMM_indexes.empty() )
1145 {
1146 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1147 symbol_ptr->SetByteSize(sym_idx + 1);
1148 symbol_ptr->SetSizeIsSibling(true);
1149 N_COMM_indexes.pop_back();
1150 }
1151 type = eSymbolTypeScopeEnd;
1152 break;
1153
Greg Claytone1a916a2010-07-21 22:12:05 +00001154 case StabLength:
1155 // N_LENG - second stab entry with length information
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 type = eSymbolTypeAdditional;
1157 break;
1158
1159 default: break;
1160 }
1161 }
1162 else
1163 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001164 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
1165 uint8_t n_type = NlistMaskType & nlist.n_type;
1166 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167
1168 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name)
1169 {
1170 type = eSymbolTypeRuntime;
1171 }
1172 else
1173 {
1174 switch (n_type)
1175 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001176 case NListTypeIndirect: // N_INDR - Fall through
1177 case NListTypePreboundUndefined:// N_PBUD - Fall through
1178 case NListTypeUndefined: // N_UNDF
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001179 type = eSymbolTypeExtern;
1180 break;
1181
Greg Claytone1a916a2010-07-21 22:12:05 +00001182 case NListTypeAbsolute: // N_ABS
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 type = eSymbolTypeAbsolute;
1184 break;
1185
Greg Claytone1a916a2010-07-21 22:12:05 +00001186 case NListTypeSection: // N_SECT
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1188
Greg Clayton8f258512011-08-26 20:01:35 +00001189 if (symbol_section == NULL)
1190 {
1191 // TODO: warn about this?
1192 add_nlist = false;
1193 break;
1194 }
1195
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196 if (TEXT_eh_frame_sectID == nlist.n_sect)
1197 {
1198 type = eSymbolTypeException;
1199 }
1200 else
1201 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001202 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203
1204 switch (section_type)
1205 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001206 case SectionTypeRegular: break; // regular section
1207 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
1208 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
1209 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
1210 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
1211 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1212 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1213 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1214 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1215 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
1216 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
1217 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
1218 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
1219 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
1220 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
1221 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
1222 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001223 default: break;
1224 }
1225
1226 if (type == eSymbolTypeInvalid)
1227 {
1228 const char *symbol_sect_name = symbol_section->GetName().AsCString();
1229 if (symbol_section->IsDescendant (text_section_sp.get()))
1230 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001231 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1232 SectionAttrUserSelfModifyingCode |
1233 SectionAttrSytemSomeInstructions))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001234 type = eSymbolTypeData;
1235 else
1236 type = eSymbolTypeCode;
1237 }
1238 else
1239 if (symbol_section->IsDescendant(data_section_sp.get()))
1240 {
1241 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1242 {
1243 type = eSymbolTypeRuntime;
1244 }
1245 else
1246 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1247 {
1248 type = eSymbolTypeException;
1249 }
1250 else
1251 {
1252 type = eSymbolTypeData;
1253 }
1254 }
1255 else
1256 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1257 {
1258 type = eSymbolTypeTrampoline;
1259 }
1260 else
1261 if (symbol_section->IsDescendant(objc_section_sp.get()))
1262 {
1263 type = eSymbolTypeRuntime;
1264 }
1265 }
1266 }
1267 break;
Greg Clayton928d8292010-09-08 16:38:06 +00001268 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269 }
1270 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001271 if (add_nlist)
1272 {
1273 bool symbol_name_is_mangled = false;
1274 if (symbol_name && symbol_name[0] == '_')
1275 {
1276 symbol_name_is_mangled = symbol_name[1] == '_';
1277 symbol_name++; // Skip the leading underscore
1278 }
1279 uint64_t symbol_value = nlist.n_value;
Greg Clayton928d8292010-09-08 16:38:06 +00001280
1281 if (symbol_name)
1282 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001283 if (is_debug == false)
Greg Clayton928d8292010-09-08 16:38:06 +00001284 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001285 if (type == eSymbolTypeCode)
Greg Clayton928d8292010-09-08 16:38:06 +00001286 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001287 // See if we can find a N_FUN entry for any code symbols.
1288 // If we do find a match, and the name matches, then we
1289 // can merge the two into just the function symbol to avoid
1290 // duplicate entries in the symbol table
1291 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1292 if (pos != N_FUN_addr_to_sym_idx.end())
Greg Clayton928d8292010-09-08 16:38:06 +00001293 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001294 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1295 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1296 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001297 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001298 // We just need the flags from the linker symbol, so put these flags
1299 // into the N_FUN flags to avoid duplicate symbols in the symbol table
1300 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1301 sym[sym_idx].Clear();
1302 continue;
1303 }
Greg Clayton928d8292010-09-08 16:38:06 +00001304 }
1305 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001306 else if (type == eSymbolTypeData)
Greg Clayton928d8292010-09-08 16:38:06 +00001307 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001308 // See if we can find a N_STSYM entry for any data symbols.
1309 // If we do find a match, and the name matches, then we
1310 // can merge the two into just the Static symbol to avoid
1311 // duplicate entries in the symbol table
1312 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
1313 if (pos != N_STSYM_addr_to_sym_idx.end())
Greg Clayton928d8292010-09-08 16:38:06 +00001314 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001315 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1316 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1317 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001318 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001319 // We just need the flags from the linker symbol, so put these flags
1320 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
1321 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1322 sym[sym_idx].Clear();
1323 continue;
1324 }
Greg Clayton928d8292010-09-08 16:38:06 +00001325 }
1326 }
1327 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328 if (symbol_section != NULL)
1329 symbol_value -= symbol_section->GetFileAddress();
1330
1331 sym[sym_idx].SetID (nlist_idx);
1332 sym[sym_idx].SetType (type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001333 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1334 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1335 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1336
1337 ++sym_idx;
1338 }
1339 else
1340 {
1341 sym[sym_idx].Clear();
1342 }
1343
1344 }
1345
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001346 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1347 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1348 // such entries by figuring out what the address for the global is by looking up this non-STAB
1349 // entry and copying the value into the debug symbol's value to save us the hassle in the
1350 // debug symbol parser.
1351
1352 Symbol *global_symbol = NULL;
1353 for (nlist_idx = 0;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001354 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 +00001355 nlist_idx++)
1356 {
1357 if (global_symbol->GetValue().GetFileAddress() == 0)
1358 {
1359 std::vector<uint32_t> indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001360 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361 {
1362 std::vector<uint32_t>::const_iterator pos;
1363 std::vector<uint32_t>::const_iterator end = indexes.end();
1364 for (pos = indexes.begin(); pos != end; ++pos)
1365 {
1366 symbol_ptr = symtab->SymbolAtIndex(*pos);
1367 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1368 {
1369 global_symbol->SetValue(symbol_ptr->GetValue());
1370 break;
1371 }
1372 }
1373 }
1374 }
1375 }
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001376
1377 // Trim our symbols down to just what we ended up with after
1378 // removing any symbols.
1379 if (sym_idx < num_syms)
1380 {
1381 num_syms = sym_idx;
1382 sym = symtab->Resize (num_syms);
1383 }
1384
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385 // Now synthesize indirect symbols
1386 if (m_dysymtab.nindirectsyms != 0)
1387 {
1388 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4));
1389
1390 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize())
1391 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001392 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1394
1395 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1396 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001397 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001398 {
1399 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1400 if (symbol_stub_byte_size == 0)
1401 continue;
1402
1403 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1404
1405 if (num_symbol_stubs == 0)
1406 continue;
1407
1408 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001409 uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001410 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1411 {
1412 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1413 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1414 uint32_t symbol_stub_offset = symbol_stub_index * 4;
1415 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1416 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001417 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Greg Claytonf4abd0d2010-10-06 01:26:32 +00001418 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
1419 continue;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001420
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001421 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
1422 Symbol *stub_symbol = NULL;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001423 if (index_pos != end_index_pos)
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001424 {
1425 // We have a remapping from the original nlist index to
1426 // a current symbol index, so just look this up by index
1427 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
1428 }
1429 else
1430 {
1431 // We need to lookup a symbol using the original nlist
1432 // symbol index since this index is coming from the
1433 // S_SYMBOL_STUBS
1434 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
1435 }
Greg Clayton49bd1c82010-09-07 17:36:17 +00001436
1437 assert (stub_symbol);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001438 if (stub_symbol)
1439 {
1440 Address so_addr(symbol_stub_addr, section_list);
1441
1442 if (stub_symbol->GetType() == eSymbolTypeExtern)
1443 {
1444 // Change the external symbol into a trampoline that makes sense
1445 // These symbols were N_UNDF N_EXT, and are useless to us, so we
1446 // can re-use them so we don't have to make up a synthetic symbol
1447 // for no good reason.
1448 stub_symbol->SetType (eSymbolTypeTrampoline);
1449 stub_symbol->SetExternal (false);
1450 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1451 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1452 }
1453 else
1454 {
1455 // Make a synthetic symbol to describe the trampoline stub
1456 if (sym_idx >= num_syms)
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001457 sym = symtab->Resize (++num_syms);
1458 sym[sym_idx].SetID (synthetic_stub_sym_id++);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001459 sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1460 sym[sym_idx].SetType (eSymbolTypeTrampoline);
1461 sym[sym_idx].SetIsSynthetic (true);
1462 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1463 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1464 ++sym_idx;
1465 }
1466 }
1467 }
1468 }
1469 }
1470 }
1471 }
1472 }
1473
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001474 return symtab->GetNumSymbols();
1475 }
1476 }
1477 offset = cmd_offset + symtab_load_command.cmdsize;
1478 }
1479 return 0;
1480}
1481
1482
1483void
1484ObjectFileMachO::Dump (Stream *s)
1485{
1486 lldb_private::Mutex::Locker locker(m_mutex);
Jason Molendafd54b362011-09-20 21:44:10 +00001487 s->Printf("%p: ", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001488 s->Indent();
Greg Claytone1a916a2010-07-21 22:12:05 +00001489 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001490 s->PutCString("ObjectFileMachO64");
1491 else
1492 s->PutCString("ObjectFileMachO32");
1493
Greg Clayton41f92322010-06-11 03:25:34 +00001494 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495
Greg Clayton64195a22011-02-23 00:35:02 +00001496 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001497
1498 if (m_sections_ap.get())
Greg Clayton10177aa2010-12-08 05:08:21 +00001499 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001500
1501 if (m_symtab_ap.get())
Greg Clayton8087ca22010-10-08 04:20:14 +00001502 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503}
1504
1505
1506bool
Greg Clayton60830262011-02-04 18:53:10 +00001507ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508{
1509 lldb_private::Mutex::Locker locker(m_mutex);
1510 struct uuid_command load_cmd;
1511 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1512 uint32_t i;
1513 for (i=0; i<m_header.ncmds; ++i)
1514 {
1515 const uint32_t cmd_offset = offset;
1516 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1517 break;
1518
Greg Claytone1a916a2010-07-21 22:12:05 +00001519 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001520 {
1521 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1522 if (uuid_bytes)
1523 {
1524 uuid->SetBytes (uuid_bytes);
1525 return true;
1526 }
1527 return false;
1528 }
1529 offset = cmd_offset + load_cmd.cmdsize;
1530 }
1531 return false;
1532}
1533
1534
1535uint32_t
1536ObjectFileMachO::GetDependentModules (FileSpecList& files)
1537{
1538 lldb_private::Mutex::Locker locker(m_mutex);
1539 struct load_command load_cmd;
1540 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1541 uint32_t count = 0;
Greg Clayton9b72eb72011-05-24 23:06:02 +00001542 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 +00001543 uint32_t i;
1544 for (i=0; i<m_header.ncmds; ++i)
1545 {
1546 const uint32_t cmd_offset = offset;
1547 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1548 break;
1549
1550 switch (load_cmd.cmd)
1551 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001552 case LoadCommandDylibLoad:
1553 case LoadCommandDylibLoadWeak:
1554 case LoadCommandDylibReexport:
1555 case LoadCommandDynamicLinkerLoad:
1556 case LoadCommandFixedVMShlibLoad:
Greg Clayton74f6e9f2010-10-09 00:48:53 +00001557 case LoadCommandDylibLoadUpward:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001558 {
1559 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1560 const char *path = m_data.PeekCStr(name_offset);
1561 // Skip any path that starts with '@' since these are usually:
1562 // @executable_path/.../file
1563 // @rpath/.../file
1564 if (path && path[0] != '@')
1565 {
Greg Clayton9b72eb72011-05-24 23:06:02 +00001566 FileSpec file_spec(path, resolve_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001567 if (files.AppendIfUnique(file_spec))
1568 count++;
1569 }
1570 }
1571 break;
1572
1573 default:
1574 break;
1575 }
1576 offset = cmd_offset + load_cmd.cmdsize;
1577 }
1578 return count;
1579}
1580
Jim Ingham672e6f52011-03-07 23:44:08 +00001581lldb_private::Address
1582ObjectFileMachO::GetEntryPointAddress ()
1583{
1584 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
1585 // is initialized to an invalid address, so we can just return that.
1586 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
1587
1588 if (!IsExecutable() || m_entry_point_address.IsValid())
1589 return m_entry_point_address;
1590
1591 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
1592 // /usr/include/mach-o.h, but it is basically:
1593 //
1594 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
1595 // uint32_t count - this is the count of longs in the thread state data
1596 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
1597 // <repeat this trio>
1598 //
1599 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
1600 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
1601 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
1602 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
1603 //
1604 // For now we hard-code the offsets and flavors we need:
1605 //
1606 //
1607
1608 lldb_private::Mutex::Locker locker(m_mutex);
1609 struct load_command load_cmd;
1610 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1611 uint32_t i;
1612 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
1613 bool done = false;
1614
1615 for (i=0; i<m_header.ncmds; ++i)
1616 {
1617 const uint32_t cmd_offset = offset;
1618 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1619 break;
1620
1621 switch (load_cmd.cmd)
1622 {
1623 case LoadCommandUnixThread:
1624 case LoadCommandThread:
1625 {
1626 while (offset < cmd_offset + load_cmd.cmdsize)
1627 {
1628 uint32_t flavor = m_data.GetU32(&offset);
1629 uint32_t count = m_data.GetU32(&offset);
1630 if (count == 0)
1631 {
1632 // We've gotten off somehow, log and exit;
1633 return m_entry_point_address;
1634 }
1635
1636 switch (m_header.cputype)
1637 {
1638 case llvm::MachO::CPUTypeARM:
1639 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
1640 {
1641 offset += 60; // This is the offset of pc in the GPR thread state data structure.
1642 start_address = m_data.GetU32(&offset);
1643 done = true;
1644 }
1645 break;
1646 case llvm::MachO::CPUTypeI386:
1647 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
1648 {
1649 offset += 40; // This is the offset of eip in the GPR thread state data structure.
1650 start_address = m_data.GetU32(&offset);
1651 done = true;
1652 }
1653 break;
1654 case llvm::MachO::CPUTypeX86_64:
1655 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
1656 {
1657 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
1658 start_address = m_data.GetU64(&offset);
1659 done = true;
1660 }
1661 break;
1662 default:
1663 return m_entry_point_address;
1664 }
1665 // Haven't found the GPR flavor yet, skip over the data for this flavor:
1666 if (done)
1667 break;
1668 offset += count * 4;
1669 }
1670 }
1671 break;
1672
1673 default:
1674 break;
1675 }
1676 if (done)
1677 break;
1678
1679 // Go to the next load command:
1680 offset = cmd_offset + load_cmd.cmdsize;
1681 }
1682
1683 if (start_address != LLDB_INVALID_ADDRESS)
1684 {
1685 // We got the start address from the load commands, so now resolve that address in the sections
1686 // of this ObjectFile:
1687 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
1688 {
1689 m_entry_point_address.Clear();
1690 }
1691 }
1692 else
1693 {
1694 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
1695 // "start" symbol in the main executable.
1696
1697 SymbolContextList contexts;
1698 SymbolContext context;
Sean Callananb6d70eb2011-10-12 02:08:07 +00001699 if (!m_module->FindSymbolsWithNameAndType(ConstString ("start"), NULL, eSymbolTypeCode, contexts))
Jim Ingham672e6f52011-03-07 23:44:08 +00001700 return m_entry_point_address;
1701
1702 contexts.GetContextAtIndex(0, context);
1703
1704 m_entry_point_address = context.symbol->GetValue();
1705 }
1706
1707 return m_entry_point_address;
1708
1709}
1710
Greg Clayton9e00b6a652011-07-09 00:41:34 +00001711ObjectFile::Type
1712ObjectFileMachO::CalculateType()
1713{
1714 switch (m_header.filetype)
1715 {
1716 case HeaderFileTypeObject: // 0x1u MH_OBJECT
1717 if (GetAddressByteSize () == 4)
1718 {
1719 // 32 bit kexts are just object files, but they do have a valid
1720 // UUID load command.
1721 UUID uuid;
1722 if (GetUUID(&uuid))
1723 {
1724 // this checking for the UUID load command is not enough
1725 // we could eventually look for the symbol named
1726 // "OSKextGetCurrentIdentifier" as this is required of kexts
1727 if (m_strata == eStrataInvalid)
1728 m_strata = eStrataKernel;
1729 return eTypeSharedLibrary;
1730 }
1731 }
1732 return eTypeObjectFile;
1733
1734 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
1735 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
1736 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
1737 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
1738 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
1739 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
1740 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
1741 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
1742 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
1743 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
1744 default:
1745 break;
1746 }
1747 return eTypeUnknown;
1748}
1749
1750ObjectFile::Strata
1751ObjectFileMachO::CalculateStrata()
1752{
1753 switch (m_header.filetype)
1754 {
1755 case HeaderFileTypeObject: // 0x1u MH_OBJECT
1756 {
1757 // 32 bit kexts are just object files, but they do have a valid
1758 // UUID load command.
1759 UUID uuid;
1760 if (GetUUID(&uuid))
1761 {
1762 // this checking for the UUID load command is not enough
1763 // we could eventually look for the symbol named
1764 // "OSKextGetCurrentIdentifier" as this is required of kexts
1765 if (m_type == eTypeInvalid)
1766 m_type = eTypeSharedLibrary;
1767
1768 return eStrataKernel;
1769 }
1770 }
1771 return eStrataUnknown;
1772
1773 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
1774 // Check for the MH_DYLDLINK bit in the flags
1775 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
1776 return eStrataUser;
1777 return eStrataKernel;
1778
1779 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
1780 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
1781 case HeaderFileTypePreloadedExecutable: return eStrataUser; // 0x5u MH_PRELOAD
1782 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
1783 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
1784 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
1785 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
1786 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
1787 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
1788 default:
1789 break;
1790 }
1791 return eStrataUnknown;
1792}
1793
1794
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001795bool
Greg Clayton514487e2011-02-15 21:59:32 +00001796ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001797{
1798 lldb_private::Mutex::Locker locker(m_mutex);
Greg Claytone0d378b2011-03-24 21:19:54 +00001799 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Greg Clayton593577a2011-09-21 03:57:31 +00001800
1801 // Files with type MH_PRELOAD are currently used in cases where the image
1802 // debugs at the addresses in the file itself. Below we set the OS to
1803 // unknown to make sure we use the DynamicLoaderStatic()...
1804 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
1805 {
1806 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
1807 }
1808
Greg Clayton514487e2011-02-15 21:59:32 +00001809 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001810}
1811
1812
1813//------------------------------------------------------------------
1814// PluginInterface protocol
1815//------------------------------------------------------------------
1816const char *
1817ObjectFileMachO::GetPluginName()
1818{
1819 return "ObjectFileMachO";
1820}
1821
1822const char *
1823ObjectFileMachO::GetShortPluginName()
1824{
1825 return GetPluginNameStatic();
1826}
1827
1828uint32_t
1829ObjectFileMachO::GetPluginVersion()
1830{
1831 return 1;
1832}
1833