blob: 60b94cd71738ec1fac3738434c5658727ef08f57 [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
Greg Clayton456809c2011-12-03 02:30:59 +000010#include "llvm/ADT/StringRef.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000011#include "llvm/Support/MachO.h"
12
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "ObjectFileMachO.h"
14
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/ArchSpec.h"
16#include "lldb/Core/DataBuffer.h"
Greg Clayton53239f02011-02-08 05:05:52 +000017#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/FileSpecList.h"
19#include "lldb/Core/Module.h"
20#include "lldb/Core/PluginManager.h"
21#include "lldb/Core/Section.h"
22#include "lldb/Core/StreamFile.h"
23#include "lldb/Core/StreamString.h"
24#include "lldb/Core/Timer.h"
25#include "lldb/Core/UUID.h"
Sean Callananb6d70eb2011-10-12 02:08:07 +000026#include "lldb/Symbol/ClangNamespaceDecl.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Symbol/ObjectFile.h"
28
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
Greg Claytone1a916a2010-07-21 22:12:05 +000032using namespace llvm::MachO;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
Greg Claytonded470d2011-03-19 01:12:21 +000034#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
36void
37ObjectFileMachO::Initialize()
38{
39 PluginManager::RegisterPlugin (GetPluginNameStatic(),
40 GetPluginDescriptionStatic(),
41 CreateInstance);
42}
43
44void
45ObjectFileMachO::Terminate()
46{
47 PluginManager::UnregisterPlugin (CreateInstance);
48}
49
50
51const char *
52ObjectFileMachO::GetPluginNameStatic()
53{
54 return "object-file.mach-o";
55}
56
57const char *
58ObjectFileMachO::GetPluginDescriptionStatic()
59{
60 return "Mach-o object file reader (32 and 64 bit)";
61}
62
63
64ObjectFile *
65ObjectFileMachO::CreateInstance (Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length)
66{
67 if (ObjectFileMachO::MagicBytesMatch(dataSP))
68 {
69 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module, dataSP, file, offset, length));
70 if (objfile_ap.get() && objfile_ap->ParseHeader())
71 return objfile_ap.release();
72 }
73 return NULL;
74}
75
76
77static uint32_t
78MachHeaderSizeFromMagic(uint32_t magic)
79{
80 switch (magic)
81 {
Greg Claytone1a916a2010-07-21 22:12:05 +000082 case HeaderMagic32:
83 case HeaderMagic32Swapped:
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 return sizeof(struct mach_header);
85
Greg Claytone1a916a2010-07-21 22:12:05 +000086 case HeaderMagic64:
87 case HeaderMagic64Swapped:
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088 return sizeof(struct mach_header_64);
89 break;
90
91 default:
92 break;
93 }
94 return 0;
95}
96
97
98bool
99ObjectFileMachO::MagicBytesMatch (DataBufferSP& dataSP)
100{
Greg Clayton7fb56d02011-02-01 01:31:41 +0000101 DataExtractor data(dataSP, lldb::endian::InlHostByteOrder(), 4);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 uint32_t offset = 0;
103 uint32_t magic = data.GetU32(&offset);
104 return MachHeaderSizeFromMagic(magic) != 0;
105}
106
107
108ObjectFileMachO::ObjectFileMachO(Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) :
109 ObjectFile(module, file, offset, length, dataSP),
110 m_mutex (Mutex::eMutexTypeRecursive),
111 m_header(),
112 m_sections_ap(),
Jim Ingham672e6f52011-03-07 23:44:08 +0000113 m_symtab_ap(),
114 m_entry_point_address ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115{
Greg Clayton72b77eb2011-02-04 21:13:05 +0000116 ::memset (&m_header, 0, sizeof(m_header));
117 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118}
119
120
121ObjectFileMachO::~ObjectFileMachO()
122{
123}
124
125
126bool
127ObjectFileMachO::ParseHeader ()
128{
129 lldb_private::Mutex::Locker locker(m_mutex);
130 bool can_parse = false;
131 uint32_t offset = 0;
Greg Clayton7fb56d02011-02-01 01:31:41 +0000132 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133 // Leave magic in the original byte order
134 m_header.magic = m_data.GetU32(&offset);
135 switch (m_header.magic)
136 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000137 case HeaderMagic32:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000138 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139 m_data.SetAddressByteSize(4);
140 can_parse = true;
141 break;
142
Greg Claytone1a916a2010-07-21 22:12:05 +0000143 case HeaderMagic64:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000144 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145 m_data.SetAddressByteSize(8);
146 can_parse = true;
147 break;
148
Greg Claytone1a916a2010-07-21 22:12:05 +0000149 case HeaderMagic32Swapped:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000150 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151 m_data.SetAddressByteSize(4);
152 can_parse = true;
153 break;
154
Greg Claytone1a916a2010-07-21 22:12:05 +0000155 case HeaderMagic64Swapped:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000156 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 m_data.SetAddressByteSize(8);
158 can_parse = true;
159 break;
160
161 default:
162 break;
163 }
164
165 if (can_parse)
166 {
167 m_data.GetU32(&offset, &m_header.cputype, 6);
168
Greg Clayton41f92322010-06-11 03:25:34 +0000169 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jim Ingham5aee1622010-08-09 23:31:02 +0000170
171 if (SetModulesArchitecture (mach_arch))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172 {
173 // Read in all only the load command data
174 DataBufferSP data_sp(m_file.ReadFileContents(m_offset, m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic)));
175 m_data.SetData (data_sp);
176 return true;
177 }
178 }
179 else
180 {
181 memset(&m_header, 0, sizeof(struct mach_header));
182 }
183 return false;
184}
185
186
187ByteOrder
188ObjectFileMachO::GetByteOrder () const
189{
190 lldb_private::Mutex::Locker locker(m_mutex);
191 return m_data.GetByteOrder ();
192}
193
Jim Ingham5aee1622010-08-09 23:31:02 +0000194bool
195ObjectFileMachO::IsExecutable() const
196{
197 return m_header.filetype == HeaderFileTypeExecutable;
198}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199
200size_t
201ObjectFileMachO::GetAddressByteSize () const
202{
203 lldb_private::Mutex::Locker locker(m_mutex);
204 return m_data.GetAddressByteSize ();
205}
206
Greg Claytone0d378b2011-03-24 21:19:54 +0000207AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +0000208ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
209{
210 Symtab *symtab = GetSymtab();
211 if (symtab)
212 {
213 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
214 if (symbol)
215 {
216 const AddressRange *range_ptr = symbol->GetAddressRangePtr();
217 if (range_ptr)
218 {
219 const Section *section = range_ptr->GetBaseAddress().GetSection();
220 if (section)
221 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000222 const SectionType section_type = section->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000223 switch (section_type)
224 {
225 case eSectionTypeInvalid: return eAddressClassUnknown;
226 case eSectionTypeCode:
227 if (m_header.cputype == llvm::MachO::CPUTypeARM)
228 {
229 // For ARM we have a bit in the n_desc field of the symbol
230 // that tells us ARM/Thumb which is bit 0x0008.
231 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
232 return eAddressClassCodeAlternateISA;
233 }
234 return eAddressClassCode;
235
236 case eSectionTypeContainer: return eAddressClassUnknown;
Greg Clayton5009f9d2011-10-27 17:55:14 +0000237 case eSectionTypeData:
238 case eSectionTypeDataCString:
239 case eSectionTypeDataCStringPointers:
240 case eSectionTypeDataSymbolAddress:
241 case eSectionTypeData4:
242 case eSectionTypeData8:
243 case eSectionTypeData16:
244 case eSectionTypeDataPointers:
245 case eSectionTypeZeroFill:
246 case eSectionTypeDataObjCMessageRefs:
247 case eSectionTypeDataObjCCFStrings:
248 return eAddressClassData;
249 case eSectionTypeDebug:
250 case eSectionTypeDWARFDebugAbbrev:
251 case eSectionTypeDWARFDebugAranges:
252 case eSectionTypeDWARFDebugFrame:
253 case eSectionTypeDWARFDebugInfo:
254 case eSectionTypeDWARFDebugLine:
255 case eSectionTypeDWARFDebugLoc:
256 case eSectionTypeDWARFDebugMacInfo:
257 case eSectionTypeDWARFDebugPubNames:
258 case eSectionTypeDWARFDebugPubTypes:
259 case eSectionTypeDWARFDebugRanges:
260 case eSectionTypeDWARFDebugStr:
261 case eSectionTypeDWARFAppleNames:
262 case eSectionTypeDWARFAppleTypes:
263 case eSectionTypeDWARFAppleNamespaces:
264 case eSectionTypeDWARFAppleObjC:
265 return eAddressClassDebug;
Greg Claytonded470d2011-03-19 01:12:21 +0000266 case eSectionTypeEHFrame: return eAddressClassRuntime;
267 case eSectionTypeOther: return eAddressClassUnknown;
268 }
269 }
270 }
271
Greg Claytone0d378b2011-03-24 21:19:54 +0000272 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000273 switch (symbol_type)
274 {
275 case eSymbolTypeAny: return eAddressClassUnknown;
276 case eSymbolTypeAbsolute: return eAddressClassUnknown;
Greg Claytonded470d2011-03-19 01:12:21 +0000277
278 case eSymbolTypeCode:
279 case eSymbolTypeTrampoline:
280 if (m_header.cputype == llvm::MachO::CPUTypeARM)
281 {
282 // For ARM we have a bit in the n_desc field of the symbol
283 // that tells us ARM/Thumb which is bit 0x0008.
284 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
285 return eAddressClassCodeAlternateISA;
286 }
287 return eAddressClassCode;
288
289 case eSymbolTypeData: return eAddressClassData;
290 case eSymbolTypeRuntime: return eAddressClassRuntime;
291 case eSymbolTypeException: return eAddressClassRuntime;
292 case eSymbolTypeSourceFile: return eAddressClassDebug;
293 case eSymbolTypeHeaderFile: return eAddressClassDebug;
294 case eSymbolTypeObjectFile: return eAddressClassDebug;
295 case eSymbolTypeCommonBlock: return eAddressClassDebug;
296 case eSymbolTypeBlock: return eAddressClassDebug;
297 case eSymbolTypeLocal: return eAddressClassData;
298 case eSymbolTypeParam: return eAddressClassData;
299 case eSymbolTypeVariable: return eAddressClassData;
300 case eSymbolTypeVariableType: return eAddressClassDebug;
301 case eSymbolTypeLineEntry: return eAddressClassDebug;
302 case eSymbolTypeLineHeader: return eAddressClassDebug;
303 case eSymbolTypeScopeBegin: return eAddressClassDebug;
304 case eSymbolTypeScopeEnd: return eAddressClassDebug;
305 case eSymbolTypeAdditional: return eAddressClassUnknown;
306 case eSymbolTypeCompiler: return eAddressClassDebug;
307 case eSymbolTypeInstrumentation:return eAddressClassDebug;
308 case eSymbolTypeUndefined: return eAddressClassUnknown;
Greg Clayton456809c2011-12-03 02:30:59 +0000309 case eSymbolTypeObjCClass: return eAddressClassRuntime;
310 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime;
311 case eSymbolTypeObjCIVar: return eAddressClassRuntime;
Greg Claytonded470d2011-03-19 01:12:21 +0000312 }
313 }
314 }
315 return eAddressClassUnknown;
316}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317
318Symtab *
319ObjectFileMachO::GetSymtab()
320{
Greg Clayton1a65ae12011-01-25 23:55:37 +0000321 lldb_private::Mutex::Locker symfile_locker(m_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 if (m_symtab_ap.get() == NULL)
323 {
324 m_symtab_ap.reset(new Symtab(this));
Greg Clayton1a65ae12011-01-25 23:55:37 +0000325 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000326 ParseSymtab (true);
Greg Claytonddfda812011-11-22 18:47:24 +0000327 m_symtab_ap->Finalize ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328 }
329 return m_symtab_ap.get();
330}
331
332
333SectionList *
334ObjectFileMachO::GetSectionList()
335{
336 lldb_private::Mutex::Locker locker(m_mutex);
337 if (m_sections_ap.get() == NULL)
338 {
339 m_sections_ap.reset(new SectionList());
340 ParseSections();
341 }
342 return m_sections_ap.get();
343}
344
345
346size_t
347ObjectFileMachO::ParseSections ()
348{
349 lldb::user_id_t segID = 0;
350 lldb::user_id_t sectID = 0;
351 struct segment_command_64 load_cmd;
352 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
353 uint32_t i;
354 //bool dump_sections = false;
355 for (i=0; i<m_header.ncmds; ++i)
356 {
357 const uint32_t load_cmd_offset = offset;
358 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
359 break;
360
Greg Claytone1a916a2010-07-21 22:12:05 +0000361 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362 {
363 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
364 {
365 load_cmd.vmaddr = m_data.GetAddress(&offset);
366 load_cmd.vmsize = m_data.GetAddress(&offset);
367 load_cmd.fileoff = m_data.GetAddress(&offset);
368 load_cmd.filesize = m_data.GetAddress(&offset);
369 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
370 {
Greg Clayton414f5d32011-01-25 02:58:48 +0000371
372 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
373
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 // Keep a list of mach segments around in case we need to
375 // get at data that isn't stored in the abstracted Sections.
376 m_mach_segments.push_back (load_cmd);
377
378 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
379 // Use a segment ID of the segment index shifted left by 8 so they
380 // never conflict with any of the sections.
381 SectionSP segment_sp;
382 if (segment_name)
383 {
384 segment_sp.reset(new Section (NULL,
385 GetModule(), // Module to which this section belongs
386 ++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
387 segment_name, // Name of this section
388 eSectionTypeContainer, // This section is a container of other sections.
389 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
390 load_cmd.vmsize, // VM size in bytes of this section
391 load_cmd.fileoff, // Offset to the data for this section in the file
392 load_cmd.filesize, // Size in bytes of this section as found in the the file
393 load_cmd.flags)); // Flags for this section
394
Greg Clayton414f5d32011-01-25 02:58:48 +0000395 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396 m_sections_ap->AddSection(segment_sp);
397 }
398
399 struct section_64 sect64;
Greg Clayton72b77eb2011-02-04 21:13:05 +0000400 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401 // Push a section into our mach sections for the section at
Greg Claytonf4abd0d2010-10-06 01:26:32 +0000402 // index zero (NListSectionNoSection) if we don't have any
403 // mach sections yet...
404 if (m_mach_sections.empty())
405 m_mach_sections.push_back(sect64);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406 uint32_t segment_sect_idx;
407 const lldb::user_id_t first_segment_sectID = sectID + 1;
408
409
Greg Claytone1a916a2010-07-21 22:12:05 +0000410 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
412 {
413 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
414 break;
415 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
416 break;
417 sect64.addr = m_data.GetAddress(&offset);
418 sect64.size = m_data.GetAddress(&offset);
419
420 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
421 break;
422
423 // Keep a list of mach sections around in case we need to
424 // get at data that isn't stored in the abstracted Sections.
425 m_mach_sections.push_back (sect64);
426
427 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
428 if (!segment_name)
429 {
430 // We have a segment with no name so we need to conjure up
431 // segments that correspond to the section's segname if there
432 // isn't already such a section. If there is such a section,
433 // we resize the section so that it spans all sections.
434 // We also mark these sections as fake so address matches don't
435 // hit if they land in the gaps between the child sections.
436 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
437 segment_sp = m_sections_ap->FindSectionByName (segment_name);
438 if (segment_sp.get())
439 {
440 Section *segment = segment_sp.get();
441 // Grow the section size as needed.
442 const lldb::addr_t sect64_min_addr = sect64.addr;
443 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
444 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
445 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
446 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
447 if (sect64_min_addr >= curr_seg_min_addr)
448 {
449 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
450 // Only grow the section size if needed
451 if (new_seg_byte_size > curr_seg_byte_size)
452 segment->SetByteSize (new_seg_byte_size);
453 }
454 else
455 {
456 // We need to change the base address of the segment and
457 // adjust the child section offsets for all existing children.
458 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
459 segment->Slide(slide_amount, false);
460 segment->GetChildren().Slide (-slide_amount, false);
461 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
462 }
Greg Clayton8d38ac42010-06-28 23:51:11 +0000463
464 // Grow the section size as needed.
465 if (sect64.offset)
466 {
467 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
468 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
469
470 const lldb::addr_t section_min_file_offset = sect64.offset;
471 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
472 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
473 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
474 segment->SetFileOffset (new_file_offset);
475 segment->SetFileSize (new_file_size);
476 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477 }
478 else
479 {
480 // Create a fake section for the section's named segment
481 segment_sp.reset(new Section(segment_sp.get(), // Parent section
482 GetModule(), // Module to which this section belongs
483 ++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
484 segment_name, // Name of this section
485 eSectionTypeContainer, // This section is a container of other sections.
486 sect64.addr, // File VM address == addresses as they are found in the object file
487 sect64.size, // VM size in bytes of this section
488 sect64.offset, // Offset to the data for this section in the file
489 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
490 load_cmd.flags)); // Flags for this section
491 segment_sp->SetIsFake(true);
492 m_sections_ap->AddSection(segment_sp);
Greg Clayton414f5d32011-01-25 02:58:48 +0000493 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 }
495 }
496 assert (segment_sp.get());
497
Greg Claytone1a916a2010-07-21 22:12:05 +0000498 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499 static ConstString g_sect_name_objc_data ("__objc_data");
500 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
501 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
502 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
503 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
504 static ConstString g_sect_name_objc_const ("__objc_const");
505 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
506 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000507
508 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
509 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
510 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
511 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
512 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
513 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
514 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
515 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
516 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
517 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
518 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
Greg Clayton17674402011-09-28 17:06:40 +0000519 static ConstString g_sect_name_dwarf_apple_names ("__apple_names");
520 static ConstString g_sect_name_dwarf_apple_types ("__apple_types");
Greg Clayton7f995132011-10-04 22:41:51 +0000521 static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac");
Greg Clayton5009f9d2011-10-27 17:55:14 +0000522 static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000523 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton89411422010-10-08 00:21:05 +0000524 static ConstString g_sect_name_DATA ("__DATA");
525 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000526
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527 SectionType sect_type = eSectionTypeOther;
528
Greg Clayton4ceb9982010-07-21 22:54:26 +0000529 if (section_name == g_sect_name_dwarf_debug_abbrev)
530 sect_type = eSectionTypeDWARFDebugAbbrev;
531 else if (section_name == g_sect_name_dwarf_debug_aranges)
532 sect_type = eSectionTypeDWARFDebugAranges;
533 else if (section_name == g_sect_name_dwarf_debug_frame)
534 sect_type = eSectionTypeDWARFDebugFrame;
535 else if (section_name == g_sect_name_dwarf_debug_info)
536 sect_type = eSectionTypeDWARFDebugInfo;
537 else if (section_name == g_sect_name_dwarf_debug_line)
538 sect_type = eSectionTypeDWARFDebugLine;
539 else if (section_name == g_sect_name_dwarf_debug_loc)
540 sect_type = eSectionTypeDWARFDebugLoc;
541 else if (section_name == g_sect_name_dwarf_debug_macinfo)
542 sect_type = eSectionTypeDWARFDebugMacInfo;
543 else if (section_name == g_sect_name_dwarf_debug_pubnames)
544 sect_type = eSectionTypeDWARFDebugPubNames;
545 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
546 sect_type = eSectionTypeDWARFDebugPubTypes;
547 else if (section_name == g_sect_name_dwarf_debug_ranges)
548 sect_type = eSectionTypeDWARFDebugRanges;
549 else if (section_name == g_sect_name_dwarf_debug_str)
550 sect_type = eSectionTypeDWARFDebugStr;
Greg Clayton17674402011-09-28 17:06:40 +0000551 else if (section_name == g_sect_name_dwarf_apple_names)
552 sect_type = eSectionTypeDWARFAppleNames;
553 else if (section_name == g_sect_name_dwarf_apple_types)
554 sect_type = eSectionTypeDWARFAppleTypes;
Greg Clayton7f995132011-10-04 22:41:51 +0000555 else if (section_name == g_sect_name_dwarf_apple_namespaces)
556 sect_type = eSectionTypeDWARFAppleNamespaces;
Greg Clayton5009f9d2011-10-27 17:55:14 +0000557 else if (section_name == g_sect_name_dwarf_apple_objc)
558 sect_type = eSectionTypeDWARFAppleObjC;
Greg Clayton4ceb9982010-07-21 22:54:26 +0000559 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton4ceb9982010-07-21 22:54:26 +0000563 else if (section_name == g_sect_name_eh_frame)
564 sect_type = eSectionTypeEHFrame;
565 else if (section_name == g_sect_name_cfstring)
566 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567 else if (section_name == g_sect_name_objc_data ||
568 section_name == g_sect_name_objc_classrefs ||
569 section_name == g_sect_name_objc_superrefs ||
570 section_name == g_sect_name_objc_const ||
571 section_name == g_sect_name_objc_classlist)
572 {
573 sect_type = eSectionTypeDataPointers;
574 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575
576 if (sect_type == eSectionTypeOther)
577 {
578 switch (mach_sect_type)
579 {
580 // TODO: categorize sections by other flags for regular sections
Greg Clayton89411422010-10-08 00:21:05 +0000581 case SectionTypeRegular:
582 if (segment_sp->GetName() == g_sect_name_TEXT)
583 sect_type = eSectionTypeCode;
584 else if (segment_sp->GetName() == g_sect_name_DATA)
585 sect_type = eSectionTypeData;
586 else
587 sect_type = eSectionTypeOther;
588 break;
Greg Claytone1a916a2010-07-21 22:12:05 +0000589 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
590 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
591 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
592 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
593 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
594 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
595 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
596 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
597 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
598 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
599 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
600 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
601 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
602 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
603 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
604 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605 default: break;
606 }
607 }
608
609 SectionSP section_sp(new Section(segment_sp.get(),
610 GetModule(),
611 ++sectID,
612 section_name,
613 sect_type,
614 sect64.addr - segment_sp->GetFileAddress(),
615 sect64.size,
616 sect64.offset,
617 sect64.offset == 0 ? 0 : sect64.size,
618 sect64.flags));
Greg Clayton414f5d32011-01-25 02:58:48 +0000619 // Set the section to be encrypted to match the segment
620 section_sp->SetIsEncrypted (segment_is_encrypted);
621
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622 segment_sp->GetChildren().AddSection(section_sp);
623
624 if (segment_sp->IsFake())
625 {
626 segment_sp.reset();
627 segment_name.Clear();
628 }
629 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000630 if (segment_sp && m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631 {
632 if (first_segment_sectID <= sectID)
633 {
634 lldb::user_id_t sect_uid;
635 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
636 {
637 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
638 SectionSP next_section_sp;
639 if (sect_uid + 1 <= sectID)
640 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
641
642 if (curr_section_sp.get())
643 {
644 if (curr_section_sp->GetByteSize() == 0)
645 {
646 if (next_section_sp.get() != NULL)
647 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
648 else
649 curr_section_sp->SetByteSize ( load_cmd.vmsize );
650 }
651 }
652 }
653 }
654 }
655 }
656 }
657 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000658 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659 {
660 m_dysymtab.cmd = load_cmd.cmd;
661 m_dysymtab.cmdsize = load_cmd.cmdsize;
662 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
663 }
664
665 offset = load_cmd_offset + load_cmd.cmdsize;
666 }
667// if (dump_sections)
668// {
669// StreamFile s(stdout);
670// m_sections_ap->Dump(&s, true);
671// }
672 return sectID; // Return the number of sections we registered with the module
673}
674
675class MachSymtabSectionInfo
676{
677public:
678
679 MachSymtabSectionInfo (SectionList *section_list) :
680 m_section_list (section_list),
681 m_section_infos()
682 {
683 // Get the number of sections down to a depth of 1 to include
684 // all segments and their sections, but no other sections that
685 // may be added for debug map or
686 m_section_infos.resize(section_list->GetNumSections(1));
687 }
688
689
690 Section *
691 GetSection (uint8_t n_sect, addr_t file_addr)
692 {
693 if (n_sect == 0)
694 return NULL;
695 if (n_sect < m_section_infos.size())
696 {
697 if (m_section_infos[n_sect].section == NULL)
698 {
699 Section *section = m_section_list->FindSectionByID (n_sect).get();
700 m_section_infos[n_sect].section = section;
Greg Claytondda0d122011-07-10 17:32:33 +0000701 if (section != NULL)
702 {
703 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
704 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
705 }
706 else
707 {
708 fprintf (stderr, "error: unable to find section for section %u\n", n_sect);
709 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710 }
711 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
Greg Clayton8f258512011-08-26 20:01:35 +0000712 {
713 // Symbol is in section.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 return m_section_infos[n_sect].section;
Greg Clayton8f258512011-08-26 20:01:35 +0000715 }
716 else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 &&
717 m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr)
718 {
719 // Symbol is in section with zero size, but has the same start
720 // address as the section. This can happen with linker symbols
721 // (symbols that start with the letter 'l' or 'L'.
722 return m_section_infos[n_sect].section;
723 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 }
725 return m_section_list->FindSectionContainingFileAddress(file_addr).get();
726 }
727
728protected:
729 struct SectionInfo
730 {
731 SectionInfo () :
732 vm_range(),
733 section (NULL)
734 {
735 }
736
737 VMRange vm_range;
738 Section *section;
739 };
740 SectionList *m_section_list;
741 std::vector<SectionInfo> m_section_infos;
742};
743
744
745
746size_t
747ObjectFileMachO::ParseSymtab (bool minimize)
748{
749 Timer scoped_timer(__PRETTY_FUNCTION__,
750 "ObjectFileMachO::ParseSymtab () module = %s",
751 m_file.GetFilename().AsCString(""));
752 struct symtab_command symtab_load_command;
753 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
754 uint32_t i;
755 for (i=0; i<m_header.ncmds; ++i)
756 {
757 const uint32_t cmd_offset = offset;
758 // Read in the load command and load command size
759 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
760 break;
761 // Watch for the symbol table load command
Greg Claytone1a916a2010-07-21 22:12:05 +0000762 if (symtab_load_command.cmd == LoadCommandSymtab)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763 {
764 // Read in the rest of the symtab load command
Jason Molendaea84e762010-07-06 22:38:03 +0000765 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766 {
767 Symtab *symtab = m_symtab_ap.get();
768 SectionList *section_list = GetSectionList();
769 assert(section_list);
770 const size_t addr_size = m_data.GetAddressByteSize();
771 const ByteOrder endian = m_data.GetByteOrder();
772 bool bit_width_32 = addr_size == 4;
773 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
774
775 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size));
776 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize));
777
778 const char *strtab_data = (const char *)strtab_data_sp->GetBytes();
Greg Clayton4f8e8692011-10-31 20:50:40 +0000779 const size_t strtab_data_len = strtab_data_sp->GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780
781 static ConstString g_segment_name_TEXT ("__TEXT");
782 static ConstString g_segment_name_DATA ("__DATA");
783 static ConstString g_segment_name_OBJC ("__OBJC");
784 static ConstString g_section_name_eh_frame ("__eh_frame");
785 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
786 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
787 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
788 SectionSP eh_frame_section_sp;
789 if (text_section_sp.get())
790 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
791 else
792 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
793
Greg Claytone1a916a2010-07-21 22:12:05 +0000794 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795 //uint32_t symtab_offset = 0;
796 const uint8_t* nlist_data = symtab_data_sp->GetBytes();
797 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms);
798
799
Greg Clayton7fb56d02011-02-01 01:31:41 +0000800 if (endian != lldb::endian::InlHostByteOrder())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801 {
802 // ...
803 assert (!"UNIMPLEMENTED: Swap all nlist entries");
804 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000805 uint32_t N_SO_index = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806
807 MachSymtabSectionInfo section_info (section_list);
808 std::vector<uint32_t> N_FUN_indexes;
809 std::vector<uint32_t> N_NSYM_indexes;
810 std::vector<uint32_t> N_INCL_indexes;
811 std::vector<uint32_t> N_BRAC_indexes;
812 std::vector<uint32_t> N_COMM_indexes;
Greg Clayton928d8292010-09-08 16:38:06 +0000813 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000814 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Clayton928d8292010-09-08 16:38:06 +0000815 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
816 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000817 // Any symbols that get merged into another will get an entry
818 // in this map so we know
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000819 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820 uint32_t nlist_idx = 0;
821 Symbol *symbol_ptr = NULL;
822
823 uint32_t sym_idx = 0;
824 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
825 uint32_t num_syms = symtab->GetNumSymbols();
826
827 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
828 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
829 {
830 struct nlist_64 nlist;
831 if (bit_width_32)
832 {
833 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size));
Greg Claytone1a916a2010-07-21 22:12:05 +0000834 nlist.n_strx = nlist32_ptr->n_strx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835 nlist.n_type = nlist32_ptr->n_type;
836 nlist.n_sect = nlist32_ptr->n_sect;
837 nlist.n_desc = nlist32_ptr->n_desc;
838 nlist.n_value = nlist32_ptr->n_value;
839 }
840 else
841 {
842 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size)));
843 }
844
845 SymbolType type = eSymbolTypeInvalid;
Greg Clayton4f8e8692011-10-31 20:50:40 +0000846 if (nlist.n_strx >= strtab_data_len)
847 {
848 // No symbol should be NULL, even the symbols with no
849 // string values should have an offset zero which points
850 // to an empty C-string
851 fprintf (stderr,
852 "error: symbol[%u] has invalid string table offset 0x%x in %s/%s, ignoring symbol\n",
853 nlist_idx,
854 nlist.n_strx,
855 m_module->GetFileSpec().GetDirectory().GetCString(),
856 m_module->GetFileSpec().GetFilename().GetCString());
857 continue;
858 }
Greg Clayton456809c2011-12-03 02:30:59 +0000859 const char *symbol_name = &strtab_data[nlist.n_strx];
860 const char *symbol_name_non_abi_mangled = NULL;
Greg Clayton4f8e8692011-10-31 20:50:40 +0000861
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862 if (symbol_name[0] == '\0')
863 symbol_name = NULL;
864 Section* symbol_section = NULL;
865 bool add_nlist = true;
Greg Claytone1a916a2010-07-21 22:12:05 +0000866 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867
868 assert (sym_idx < num_syms);
869
870 sym[sym_idx].SetDebug (is_debug);
871
872 if (is_debug)
873 {
874 switch (nlist.n_type)
875 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000876 case StabGlobalSymbol:
877 // N_GSYM -- global symbol: name,,NO_SECT,type,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878 // Sometimes the N_GSYM value contains the address.
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000879 sym[sym_idx].SetExternal(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000880 if (nlist.n_value != 0)
881 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000882 type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883 break;
884
Greg Claytone1a916a2010-07-21 22:12:05 +0000885 case StabFunctionName:
886 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000887 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000888 break;
889
Greg Claytone1a916a2010-07-21 22:12:05 +0000890 case StabFunction:
891 // N_FUN -- procedure: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892 if (symbol_name)
893 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000894 type = eSymbolTypeCode;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton928d8292010-09-08 16:38:06 +0000896
897 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000898 // We use the current number of symbols in the symbol table in lieu of
899 // using nlist_idx in case we ever start trimming entries out
900 N_FUN_indexes.push_back(sym_idx);
901 }
902 else
903 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000904 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000905
906 if ( !N_FUN_indexes.empty() )
907 {
908 // Copy the size of the function into the original STAB entry so we don't have
909 // to hunt for it later
910 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
911 N_FUN_indexes.pop_back();
Jason Molendaea84e762010-07-06 22:38:03 +0000912 // We don't really need the end function STAB as it contains the size which
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913 // we already placed with the original symbol, so don't add it if we want a
914 // minimal symbol table
915 if (minimize)
916 add_nlist = false;
917 }
918 }
919 break;
920
Greg Claytone1a916a2010-07-21 22:12:05 +0000921 case StabStaticSymbol:
922 // N_STSYM -- static symbol: name,,n_sect,type,address
Greg Clayton928d8292010-09-08 16:38:06 +0000923 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000925 type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926 break;
927
Greg Claytone1a916a2010-07-21 22:12:05 +0000928 case StabLocalCommon:
929 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000930 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
931 type = eSymbolTypeCommonBlock;
932 break;
933
Greg Claytone1a916a2010-07-21 22:12:05 +0000934 case StabBeginSymbol:
935 // N_BNSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000936 // We use the current number of symbols in the symbol table in lieu of
937 // using nlist_idx in case we ever start trimming entries out
938 if (minimize)
939 {
940 // Skip these if we want minimal symbol tables
941 add_nlist = false;
942 }
943 else
944 {
945 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
946 N_NSYM_indexes.push_back(sym_idx);
947 type = eSymbolTypeScopeBegin;
948 }
949 break;
950
Greg Claytone1a916a2010-07-21 22:12:05 +0000951 case StabEndSymbol:
952 // N_ENSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000953 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
954 // so that we can always skip the entire symbol if we need to navigate
955 // more quickly at the source level when parsing STABS
956 if (minimize)
957 {
958 // Skip these if we want minimal symbol tables
959 add_nlist = false;
960 }
961 else
962 {
963 if ( !N_NSYM_indexes.empty() )
964 {
965 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
966 symbol_ptr->SetByteSize(sym_idx + 1);
967 symbol_ptr->SetSizeIsSibling(true);
968 N_NSYM_indexes.pop_back();
969 }
970 type = eSymbolTypeScopeEnd;
971 }
972 break;
973
974
Greg Claytone1a916a2010-07-21 22:12:05 +0000975 case StabSourceFileOptions:
976 // N_OPT - emitted with gcc2_compiled and in gcc source
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977 type = eSymbolTypeCompiler;
978 break;
979
Greg Claytone1a916a2010-07-21 22:12:05 +0000980 case StabRegisterSymbol:
981 // N_RSYM - register sym: name,,NO_SECT,type,register
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000982 type = eSymbolTypeVariable;
983 break;
984
Greg Claytone1a916a2010-07-21 22:12:05 +0000985 case StabSourceLine:
986 // N_SLINE - src line: 0,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
988 type = eSymbolTypeLineEntry;
989 break;
990
Greg Claytone1a916a2010-07-21 22:12:05 +0000991 case StabStructureType:
992 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993 type = eSymbolTypeVariableType;
994 break;
995
Greg Claytone1a916a2010-07-21 22:12:05 +0000996 case StabSourceFileName:
997 // N_SO - source file name
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998 type = eSymbolTypeSourceFile;
999 if (symbol_name == NULL)
1000 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001001 if (minimize)
1002 add_nlist = false;
1003 if (N_SO_index != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004 {
1005 // Set the size of the N_SO to the terminating index of this N_SO
1006 // so that we can always skip the entire N_SO if we need to navigate
1007 // more quickly at the source level when parsing STABS
1008 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001009 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010 symbol_ptr->SetSizeIsSibling(true);
1011 }
1012 N_NSYM_indexes.clear();
1013 N_INCL_indexes.clear();
1014 N_BRAC_indexes.clear();
1015 N_COMM_indexes.clear();
1016 N_FUN_indexes.clear();
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001017 N_SO_index = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001019 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020 {
1021 // We use the current number of symbols in the symbol table in lieu of
1022 // using nlist_idx in case we ever start trimming entries out
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001023 if (symbol_name[0] == '/')
1024 N_SO_index = sym_idx;
Greg Clayton5cf21f52011-06-19 04:26:01 +00001025 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001026 {
1027 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
1028 if (so_path && so_path[0])
1029 {
1030 std::string full_so_path (so_path);
1031 if (*full_so_path.rbegin() != '/')
1032 full_so_path += '/';
1033 full_so_path += symbol_name;
1034 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
1035 add_nlist = false;
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001036 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001037 }
1038 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001040
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 break;
1042
Greg Claytone1a916a2010-07-21 22:12:05 +00001043 case StabObjectFileName:
1044 // N_OSO - object file name: name,,0,0,st_mtime
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001045 type = eSymbolTypeObjectFile;
1046 break;
1047
Greg Claytone1a916a2010-07-21 22:12:05 +00001048 case StabLocalSymbol:
1049 // N_LSYM - local sym: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050 type = eSymbolTypeLocal;
1051 break;
1052
1053 //----------------------------------------------------------------------
1054 // INCL scopes
1055 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +00001056 case StabBeginIncludeFileName:
1057 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058 // We use the current number of symbols in the symbol table in lieu of
1059 // using nlist_idx in case we ever start trimming entries out
1060 N_INCL_indexes.push_back(sym_idx);
1061 type = eSymbolTypeScopeBegin;
1062 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063
Greg Claytone1a916a2010-07-21 22:12:05 +00001064 case StabEndIncludeFile:
1065 // N_EINCL - include file end: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066 // Set the size of the N_BINCL to the terminating index of this N_EINCL
1067 // so that we can always skip the entire symbol if we need to navigate
1068 // more quickly at the source level when parsing STABS
1069 if ( !N_INCL_indexes.empty() )
1070 {
1071 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1072 symbol_ptr->SetByteSize(sym_idx + 1);
1073 symbol_ptr->SetSizeIsSibling(true);
1074 N_INCL_indexes.pop_back();
1075 }
1076 type = eSymbolTypeScopeEnd;
1077 break;
1078
Greg Claytone1a916a2010-07-21 22:12:05 +00001079 case StabIncludeFileName:
1080 // N_SOL - #included file name: name,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081 type = eSymbolTypeHeaderFile;
Greg Clayton49bd1c82010-09-07 17:36:17 +00001082
1083 // We currently don't use the header files on darwin
1084 if (minimize)
1085 add_nlist = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086 break;
1087
Greg Claytone1a916a2010-07-21 22:12:05 +00001088 case StabCompilerParameters:
1089 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090 type = eSymbolTypeCompiler;
1091 break;
1092
Greg Claytone1a916a2010-07-21 22:12:05 +00001093 case StabCompilerVersion:
1094 // N_VERSION - compiler version: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095 type = eSymbolTypeCompiler;
1096 break;
1097
Greg Claytone1a916a2010-07-21 22:12:05 +00001098 case StabCompilerOptLevel:
1099 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100 type = eSymbolTypeCompiler;
1101 break;
1102
Greg Claytone1a916a2010-07-21 22:12:05 +00001103 case StabParameter:
1104 // N_PSYM - parameter: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001105 type = eSymbolTypeVariable;
1106 break;
1107
Greg Claytone1a916a2010-07-21 22:12:05 +00001108 case StabAlternateEntry:
1109 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1111 type = eSymbolTypeLineEntry;
1112 break;
1113
1114 //----------------------------------------------------------------------
1115 // Left and Right Braces
1116 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +00001117 case StabLeftBracket:
1118 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119 // We use the current number of symbols in the symbol table in lieu of
1120 // using nlist_idx in case we ever start trimming entries out
1121 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1122 N_BRAC_indexes.push_back(sym_idx);
1123 type = eSymbolTypeScopeBegin;
1124 break;
1125
Greg Claytone1a916a2010-07-21 22:12:05 +00001126 case StabRightBracket:
1127 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
1129 // so that we can always skip the entire symbol if we need to navigate
1130 // more quickly at the source level when parsing STABS
1131 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1132 if ( !N_BRAC_indexes.empty() )
1133 {
1134 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1135 symbol_ptr->SetByteSize(sym_idx + 1);
1136 symbol_ptr->SetSizeIsSibling(true);
1137 N_BRAC_indexes.pop_back();
1138 }
1139 type = eSymbolTypeScopeEnd;
1140 break;
1141
Greg Claytone1a916a2010-07-21 22:12:05 +00001142 case StabDeletedIncludeFile:
1143 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 type = eSymbolTypeHeaderFile;
1145 break;
1146
1147 //----------------------------------------------------------------------
1148 // COMM scopes
1149 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +00001150 case StabBeginCommon:
1151 // N_BCOMM - begin common: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001152 // We use the current number of symbols in the symbol table in lieu of
1153 // using nlist_idx in case we ever start trimming entries out
1154 type = eSymbolTypeScopeBegin;
1155 N_COMM_indexes.push_back(sym_idx);
1156 break;
1157
Greg Claytone1a916a2010-07-21 22:12:05 +00001158 case StabEndCommonLocal:
1159 // N_ECOML - end common (local name): 0,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001160 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1161 // Fall through
1162
Greg Claytone1a916a2010-07-21 22:12:05 +00001163 case StabEndCommon:
1164 // N_ECOMM - end common: name,,n_sect,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1166 // so that we can always skip the entire symbol if we need to navigate
1167 // more quickly at the source level when parsing STABS
1168 if ( !N_COMM_indexes.empty() )
1169 {
1170 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1171 symbol_ptr->SetByteSize(sym_idx + 1);
1172 symbol_ptr->SetSizeIsSibling(true);
1173 N_COMM_indexes.pop_back();
1174 }
1175 type = eSymbolTypeScopeEnd;
1176 break;
1177
Greg Claytone1a916a2010-07-21 22:12:05 +00001178 case StabLength:
1179 // N_LENG - second stab entry with length information
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180 type = eSymbolTypeAdditional;
1181 break;
1182
1183 default: break;
1184 }
1185 }
1186 else
1187 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001188 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
1189 uint8_t n_type = NlistMaskType & nlist.n_type;
1190 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191
Greg Clayton456809c2011-12-03 02:30:59 +00001192 switch (n_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001193 {
Greg Clayton456809c2011-12-03 02:30:59 +00001194 case NListTypeIndirect: // N_INDR - Fall through
1195 case NListTypePreboundUndefined:// N_PBUD - Fall through
1196 case NListTypeUndefined: // N_UNDF
1197 type = eSymbolTypeUndefined;
1198 break;
1199
1200 case NListTypeAbsolute: // N_ABS
1201 type = eSymbolTypeAbsolute;
1202 break;
1203
1204 case NListTypeSection: // N_SECT
1205 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1206
1207 if (symbol_section == NULL)
1208 {
1209 // TODO: warn about this?
1210 add_nlist = false;
1211 break;
1212 }
1213
1214 if (TEXT_eh_frame_sectID == nlist.n_sect)
1215 {
1216 type = eSymbolTypeException;
1217 }
1218 else
1219 {
1220 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
1221
1222 switch (section_type)
1223 {
1224 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;
1241 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 {
1249 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1250 SectionAttrUserSelfModifyingCode |
1251 SectionAttrSytemSomeInstructions))
1252 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 if (symbol_name &&
1264 symbol_name[0] == '_' &&
1265 symbol_name[1] == 'O' &&
1266 symbol_name[2] == 'B')
1267 {
1268 llvm::StringRef symbol_name_ref(symbol_name);
1269 static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_");
1270 static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_");
1271 static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_");
1272 if (symbol_name_ref.startswith(g_objc_v2_prefix_class))
1273 {
Sean Callananbfaf54d2011-12-03 04:38:43 +00001274 symbol_name_non_abi_mangled = symbol_name + 1;
Greg Clayton456809c2011-12-03 02:30:59 +00001275 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
1276 type = eSymbolTypeObjCClass;
1277 }
1278 else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass))
1279 {
Sean Callananbfaf54d2011-12-03 04:38:43 +00001280 symbol_name_non_abi_mangled = symbol_name + 1;
Greg Clayton456809c2011-12-03 02:30:59 +00001281 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
1282 type = eSymbolTypeObjCMetaClass;
1283 }
1284 else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar))
1285 {
Sean Callananbfaf54d2011-12-03 04:38:43 +00001286 symbol_name_non_abi_mangled = symbol_name + 1;
Greg Clayton456809c2011-12-03 02:30:59 +00001287 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
1288 type = eSymbolTypeObjCIVar;
1289 }
1290 }
1291 }
1292 else
1293 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1294 {
1295 type = eSymbolTypeException;
1296 }
1297 else
1298 {
1299 type = eSymbolTypeData;
1300 }
1301 }
1302 else
1303 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1304 {
1305 type = eSymbolTypeTrampoline;
1306 }
1307 else
1308 if (symbol_section->IsDescendant(objc_section_sp.get()))
1309 {
1310 type = eSymbolTypeRuntime;
1311 if (symbol_name && symbol_name[0] == '.')
1312 {
1313 llvm::StringRef symbol_name_ref(symbol_name);
1314 static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_");
1315 if (symbol_name_ref.startswith(g_objc_v1_prefix_class))
1316 {
1317 symbol_name_non_abi_mangled = symbol_name;
1318 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
1319 type = eSymbolTypeObjCClass;
1320 }
1321 }
1322 }
1323 }
1324 }
1325 break;
1326 }
1327 }
1328
1329 if (add_nlist)
1330 {
1331 uint64_t symbol_value = nlist.n_value;
1332 bool symbol_name_is_mangled = false;
1333
1334 if (symbol_name_non_abi_mangled)
1335 {
1336 sym[sym_idx].GetMangled().SetMangledName (symbol_name_non_abi_mangled);
1337 sym[sym_idx].GetMangled().SetDemangledName (symbol_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338 }
1339 else
1340 {
Greg Clayton456809c2011-12-03 02:30:59 +00001341 if (symbol_name && symbol_name[0] == '_')
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001342 {
Greg Clayton456809c2011-12-03 02:30:59 +00001343 symbol_name_is_mangled = symbol_name[1] == '_';
1344 symbol_name++; // Skip the leading underscore
1345 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001346
Greg Claytonef3ad872011-12-03 03:02:17 +00001347 if (symbol_name)
Greg Clayton456809c2011-12-03 02:30:59 +00001348 {
1349 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
1350 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001351 }
Greg Clayton928d8292010-09-08 16:38:06 +00001352
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001353 if (is_debug == false)
Greg Clayton928d8292010-09-08 16:38:06 +00001354 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001355 if (type == eSymbolTypeCode)
Greg Clayton928d8292010-09-08 16:38:06 +00001356 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001357 // See if we can find a N_FUN entry for any code symbols.
1358 // If we do find a match, and the name matches, then we
1359 // can merge the two into just the function symbol to avoid
1360 // duplicate entries in the symbol table
1361 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1362 if (pos != N_FUN_addr_to_sym_idx.end())
Greg Clayton928d8292010-09-08 16:38:06 +00001363 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001364 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1365 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1366 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001367 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001368 // We just need the flags from the linker symbol, so put these flags
1369 // into the N_FUN flags to avoid duplicate symbols in the symbol table
1370 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1371 sym[sym_idx].Clear();
1372 continue;
1373 }
Greg Clayton928d8292010-09-08 16:38:06 +00001374 }
1375 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001376 else if (type == eSymbolTypeData)
Greg Clayton928d8292010-09-08 16:38:06 +00001377 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001378 // See if we can find a N_STSYM entry for any data symbols.
1379 // If we do find a match, and the name matches, then we
1380 // can merge the two into just the Static symbol to avoid
1381 // duplicate entries in the symbol table
1382 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
1383 if (pos != N_STSYM_addr_to_sym_idx.end())
Greg Clayton928d8292010-09-08 16:38:06 +00001384 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001385 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1386 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1387 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001388 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001389 // We just need the flags from the linker symbol, so put these flags
1390 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
1391 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1392 sym[sym_idx].Clear();
1393 continue;
1394 }
Greg Clayton928d8292010-09-08 16:38:06 +00001395 }
1396 }
1397 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001398 if (symbol_section != NULL)
1399 symbol_value -= symbol_section->GetFileAddress();
1400
1401 sym[sym_idx].SetID (nlist_idx);
1402 sym[sym_idx].SetType (type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001403 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1404 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1405 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1406
1407 ++sym_idx;
1408 }
1409 else
1410 {
1411 sym[sym_idx].Clear();
1412 }
1413
1414 }
1415
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001416 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1417 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1418 // such entries by figuring out what the address for the global is by looking up this non-STAB
1419 // entry and copying the value into the debug symbol's value to save us the hassle in the
1420 // debug symbol parser.
1421
1422 Symbol *global_symbol = NULL;
1423 for (nlist_idx = 0;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001424 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 +00001425 nlist_idx++)
1426 {
1427 if (global_symbol->GetValue().GetFileAddress() == 0)
1428 {
1429 std::vector<uint32_t> indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001430 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001431 {
1432 std::vector<uint32_t>::const_iterator pos;
1433 std::vector<uint32_t>::const_iterator end = indexes.end();
1434 for (pos = indexes.begin(); pos != end; ++pos)
1435 {
1436 symbol_ptr = symtab->SymbolAtIndex(*pos);
1437 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1438 {
1439 global_symbol->SetValue(symbol_ptr->GetValue());
1440 break;
1441 }
1442 }
1443 }
1444 }
1445 }
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001446
1447 // Trim our symbols down to just what we ended up with after
1448 // removing any symbols.
1449 if (sym_idx < num_syms)
1450 {
1451 num_syms = sym_idx;
1452 sym = symtab->Resize (num_syms);
1453 }
1454
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001455 // Now synthesize indirect symbols
1456 if (m_dysymtab.nindirectsyms != 0)
1457 {
1458 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4));
1459
1460 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize())
1461 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001462 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001463 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1464
1465 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1466 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001467 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001468 {
1469 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1470 if (symbol_stub_byte_size == 0)
1471 continue;
1472
1473 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1474
1475 if (num_symbol_stubs == 0)
1476 continue;
1477
1478 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001479 uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1481 {
1482 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1483 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1484 uint32_t symbol_stub_offset = symbol_stub_index * 4;
1485 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1486 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001487 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Greg Claytonf4abd0d2010-10-06 01:26:32 +00001488 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
1489 continue;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001490
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001491 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
1492 Symbol *stub_symbol = NULL;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001493 if (index_pos != end_index_pos)
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001494 {
1495 // We have a remapping from the original nlist index to
1496 // a current symbol index, so just look this up by index
1497 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
1498 }
1499 else
1500 {
1501 // We need to lookup a symbol using the original nlist
1502 // symbol index since this index is coming from the
1503 // S_SYMBOL_STUBS
1504 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
1505 }
Greg Clayton49bd1c82010-09-07 17:36:17 +00001506
1507 assert (stub_symbol);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508 if (stub_symbol)
1509 {
1510 Address so_addr(symbol_stub_addr, section_list);
1511
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001512 if (stub_symbol->GetType() == eSymbolTypeUndefined)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001513 {
1514 // Change the external symbol into a trampoline that makes sense
1515 // These symbols were N_UNDF N_EXT, and are useless to us, so we
1516 // can re-use them so we don't have to make up a synthetic symbol
1517 // for no good reason.
1518 stub_symbol->SetType (eSymbolTypeTrampoline);
1519 stub_symbol->SetExternal (false);
1520 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1521 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1522 }
1523 else
1524 {
1525 // Make a synthetic symbol to describe the trampoline stub
1526 if (sym_idx >= num_syms)
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001527 sym = symtab->Resize (++num_syms);
1528 sym[sym_idx].SetID (synthetic_stub_sym_id++);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001529 sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1530 sym[sym_idx].SetType (eSymbolTypeTrampoline);
1531 sym[sym_idx].SetIsSynthetic (true);
1532 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1533 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1534 ++sym_idx;
1535 }
1536 }
1537 }
1538 }
1539 }
1540 }
1541 }
1542 }
Greg Claytonddfda812011-11-22 18:47:24 +00001543
1544
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001545
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001546 return symtab->GetNumSymbols();
1547 }
1548 }
1549 offset = cmd_offset + symtab_load_command.cmdsize;
1550 }
1551 return 0;
1552}
1553
1554
1555void
1556ObjectFileMachO::Dump (Stream *s)
1557{
1558 lldb_private::Mutex::Locker locker(m_mutex);
Jason Molendafd54b362011-09-20 21:44:10 +00001559 s->Printf("%p: ", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001560 s->Indent();
Greg Claytone1a916a2010-07-21 22:12:05 +00001561 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001562 s->PutCString("ObjectFileMachO64");
1563 else
1564 s->PutCString("ObjectFileMachO32");
1565
Greg Clayton41f92322010-06-11 03:25:34 +00001566 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001567
Greg Clayton64195a22011-02-23 00:35:02 +00001568 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001569
1570 if (m_sections_ap.get())
Greg Clayton10177aa2010-12-08 05:08:21 +00001571 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001572
1573 if (m_symtab_ap.get())
Greg Clayton8087ca22010-10-08 04:20:14 +00001574 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001575}
1576
1577
1578bool
Greg Clayton60830262011-02-04 18:53:10 +00001579ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001580{
1581 lldb_private::Mutex::Locker locker(m_mutex);
1582 struct uuid_command load_cmd;
1583 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1584 uint32_t i;
1585 for (i=0; i<m_header.ncmds; ++i)
1586 {
1587 const uint32_t cmd_offset = offset;
1588 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1589 break;
1590
Greg Claytone1a916a2010-07-21 22:12:05 +00001591 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001592 {
1593 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1594 if (uuid_bytes)
1595 {
1596 uuid->SetBytes (uuid_bytes);
1597 return true;
1598 }
1599 return false;
1600 }
1601 offset = cmd_offset + load_cmd.cmdsize;
1602 }
1603 return false;
1604}
1605
1606
1607uint32_t
1608ObjectFileMachO::GetDependentModules (FileSpecList& files)
1609{
1610 lldb_private::Mutex::Locker locker(m_mutex);
1611 struct load_command load_cmd;
1612 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1613 uint32_t count = 0;
Greg Clayton9b72eb72011-05-24 23:06:02 +00001614 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 +00001615 uint32_t i;
1616 for (i=0; i<m_header.ncmds; ++i)
1617 {
1618 const uint32_t cmd_offset = offset;
1619 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1620 break;
1621
1622 switch (load_cmd.cmd)
1623 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001624 case LoadCommandDylibLoad:
1625 case LoadCommandDylibLoadWeak:
1626 case LoadCommandDylibReexport:
1627 case LoadCommandDynamicLinkerLoad:
1628 case LoadCommandFixedVMShlibLoad:
Greg Clayton74f6e9f2010-10-09 00:48:53 +00001629 case LoadCommandDylibLoadUpward:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001630 {
1631 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1632 const char *path = m_data.PeekCStr(name_offset);
1633 // Skip any path that starts with '@' since these are usually:
1634 // @executable_path/.../file
1635 // @rpath/.../file
1636 if (path && path[0] != '@')
1637 {
Greg Clayton9b72eb72011-05-24 23:06:02 +00001638 FileSpec file_spec(path, resolve_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639 if (files.AppendIfUnique(file_spec))
1640 count++;
1641 }
1642 }
1643 break;
1644
1645 default:
1646 break;
1647 }
1648 offset = cmd_offset + load_cmd.cmdsize;
1649 }
1650 return count;
1651}
1652
Jim Ingham672e6f52011-03-07 23:44:08 +00001653lldb_private::Address
1654ObjectFileMachO::GetEntryPointAddress ()
1655{
1656 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
1657 // is initialized to an invalid address, so we can just return that.
1658 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
1659
1660 if (!IsExecutable() || m_entry_point_address.IsValid())
1661 return m_entry_point_address;
1662
1663 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
1664 // /usr/include/mach-o.h, but it is basically:
1665 //
1666 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
1667 // uint32_t count - this is the count of longs in the thread state data
1668 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
1669 // <repeat this trio>
1670 //
1671 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
1672 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
1673 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
1674 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
1675 //
1676 // For now we hard-code the offsets and flavors we need:
1677 //
1678 //
1679
1680 lldb_private::Mutex::Locker locker(m_mutex);
1681 struct load_command load_cmd;
1682 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1683 uint32_t i;
1684 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
1685 bool done = false;
1686
1687 for (i=0; i<m_header.ncmds; ++i)
1688 {
1689 const uint32_t cmd_offset = offset;
1690 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1691 break;
1692
1693 switch (load_cmd.cmd)
1694 {
1695 case LoadCommandUnixThread:
1696 case LoadCommandThread:
1697 {
1698 while (offset < cmd_offset + load_cmd.cmdsize)
1699 {
1700 uint32_t flavor = m_data.GetU32(&offset);
1701 uint32_t count = m_data.GetU32(&offset);
1702 if (count == 0)
1703 {
1704 // We've gotten off somehow, log and exit;
1705 return m_entry_point_address;
1706 }
1707
1708 switch (m_header.cputype)
1709 {
1710 case llvm::MachO::CPUTypeARM:
1711 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
1712 {
1713 offset += 60; // This is the offset of pc in the GPR thread state data structure.
1714 start_address = m_data.GetU32(&offset);
1715 done = true;
1716 }
1717 break;
1718 case llvm::MachO::CPUTypeI386:
1719 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
1720 {
1721 offset += 40; // This is the offset of eip in the GPR thread state data structure.
1722 start_address = m_data.GetU32(&offset);
1723 done = true;
1724 }
1725 break;
1726 case llvm::MachO::CPUTypeX86_64:
1727 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
1728 {
1729 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
1730 start_address = m_data.GetU64(&offset);
1731 done = true;
1732 }
1733 break;
1734 default:
1735 return m_entry_point_address;
1736 }
1737 // Haven't found the GPR flavor yet, skip over the data for this flavor:
1738 if (done)
1739 break;
1740 offset += count * 4;
1741 }
1742 }
1743 break;
1744
1745 default:
1746 break;
1747 }
1748 if (done)
1749 break;
1750
1751 // Go to the next load command:
1752 offset = cmd_offset + load_cmd.cmdsize;
1753 }
1754
1755 if (start_address != LLDB_INVALID_ADDRESS)
1756 {
1757 // We got the start address from the load commands, so now resolve that address in the sections
1758 // of this ObjectFile:
1759 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
1760 {
1761 m_entry_point_address.Clear();
1762 }
1763 }
1764 else
1765 {
1766 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
1767 // "start" symbol in the main executable.
1768
1769 SymbolContextList contexts;
1770 SymbolContext context;
Sean Callananb96ff332011-10-13 16:49:47 +00001771 if (!m_module->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
Jim Ingham672e6f52011-03-07 23:44:08 +00001772 return m_entry_point_address;
1773
1774 contexts.GetContextAtIndex(0, context);
1775
1776 m_entry_point_address = context.symbol->GetValue();
1777 }
1778
1779 return m_entry_point_address;
1780
1781}
1782
Greg Clayton9e00b6a652011-07-09 00:41:34 +00001783ObjectFile::Type
1784ObjectFileMachO::CalculateType()
1785{
1786 switch (m_header.filetype)
1787 {
1788 case HeaderFileTypeObject: // 0x1u MH_OBJECT
1789 if (GetAddressByteSize () == 4)
1790 {
1791 // 32 bit kexts are just object files, but they do have a valid
1792 // UUID load command.
1793 UUID uuid;
1794 if (GetUUID(&uuid))
1795 {
1796 // this checking for the UUID load command is not enough
1797 // we could eventually look for the symbol named
1798 // "OSKextGetCurrentIdentifier" as this is required of kexts
1799 if (m_strata == eStrataInvalid)
1800 m_strata = eStrataKernel;
1801 return eTypeSharedLibrary;
1802 }
1803 }
1804 return eTypeObjectFile;
1805
1806 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
1807 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
1808 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
1809 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
1810 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
1811 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
1812 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
1813 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
1814 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
1815 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
1816 default:
1817 break;
1818 }
1819 return eTypeUnknown;
1820}
1821
1822ObjectFile::Strata
1823ObjectFileMachO::CalculateStrata()
1824{
1825 switch (m_header.filetype)
1826 {
1827 case HeaderFileTypeObject: // 0x1u MH_OBJECT
1828 {
1829 // 32 bit kexts are just object files, but they do have a valid
1830 // UUID load command.
1831 UUID uuid;
1832 if (GetUUID(&uuid))
1833 {
1834 // this checking for the UUID load command is not enough
1835 // we could eventually look for the symbol named
1836 // "OSKextGetCurrentIdentifier" as this is required of kexts
1837 if (m_type == eTypeInvalid)
1838 m_type = eTypeSharedLibrary;
1839
1840 return eStrataKernel;
1841 }
1842 }
1843 return eStrataUnknown;
1844
1845 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
1846 // Check for the MH_DYLDLINK bit in the flags
1847 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
1848 return eStrataUser;
1849 return eStrataKernel;
1850
1851 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
1852 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
1853 case HeaderFileTypePreloadedExecutable: return eStrataUser; // 0x5u MH_PRELOAD
1854 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
1855 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
1856 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
1857 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
1858 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
1859 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
1860 default:
1861 break;
1862 }
1863 return eStrataUnknown;
1864}
1865
1866
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001867bool
Greg Clayton514487e2011-02-15 21:59:32 +00001868ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001869{
1870 lldb_private::Mutex::Locker locker(m_mutex);
Greg Claytone0d378b2011-03-24 21:19:54 +00001871 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Greg Clayton593577a2011-09-21 03:57:31 +00001872
1873 // Files with type MH_PRELOAD are currently used in cases where the image
1874 // debugs at the addresses in the file itself. Below we set the OS to
1875 // unknown to make sure we use the DynamicLoaderStatic()...
1876 if (m_header.filetype == HeaderFileTypePreloadedExecutable)
1877 {
1878 arch.GetTriple().setOS (llvm::Triple::UnknownOS);
1879 }
1880
Greg Clayton514487e2011-02-15 21:59:32 +00001881 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001882}
1883
1884
1885//------------------------------------------------------------------
1886// PluginInterface protocol
1887//------------------------------------------------------------------
1888const char *
1889ObjectFileMachO::GetPluginName()
1890{
1891 return "ObjectFileMachO";
1892}
1893
1894const char *
1895ObjectFileMachO::GetShortPluginName()
1896{
1897 return GetPluginNameStatic();
1898}
1899
1900uint32_t
1901ObjectFileMachO::GetPluginVersion()
1902{
1903 return 1;
1904}
1905