blob: 0b75a7aef9ce75653934af54ef6fdaaeac193ba3 [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"
25#include "lldb/Symbol/ObjectFile.h"
26
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
Greg Claytone1a916a2010-07-21 22:12:05 +000030using namespace llvm::MachO;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Greg Claytonded470d2011-03-19 01:12:21 +000032#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
34void
35ObjectFileMachO::Initialize()
36{
37 PluginManager::RegisterPlugin (GetPluginNameStatic(),
38 GetPluginDescriptionStatic(),
39 CreateInstance);
40}
41
42void
43ObjectFileMachO::Terminate()
44{
45 PluginManager::UnregisterPlugin (CreateInstance);
46}
47
48
49const char *
50ObjectFileMachO::GetPluginNameStatic()
51{
52 return "object-file.mach-o";
53}
54
55const char *
56ObjectFileMachO::GetPluginDescriptionStatic()
57{
58 return "Mach-o object file reader (32 and 64 bit)";
59}
60
61
62ObjectFile *
63ObjectFileMachO::CreateInstance (Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length)
64{
65 if (ObjectFileMachO::MagicBytesMatch(dataSP))
66 {
67 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module, dataSP, file, offset, length));
68 if (objfile_ap.get() && objfile_ap->ParseHeader())
69 return objfile_ap.release();
70 }
71 return NULL;
72}
73
74
75static uint32_t
76MachHeaderSizeFromMagic(uint32_t magic)
77{
78 switch (magic)
79 {
Greg Claytone1a916a2010-07-21 22:12:05 +000080 case HeaderMagic32:
81 case HeaderMagic32Swapped:
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 return sizeof(struct mach_header);
83
Greg Claytone1a916a2010-07-21 22:12:05 +000084 case HeaderMagic64:
85 case HeaderMagic64Swapped:
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 return sizeof(struct mach_header_64);
87 break;
88
89 default:
90 break;
91 }
92 return 0;
93}
94
95
96bool
97ObjectFileMachO::MagicBytesMatch (DataBufferSP& dataSP)
98{
Greg Clayton7fb56d02011-02-01 01:31:41 +000099 DataExtractor data(dataSP, lldb::endian::InlHostByteOrder(), 4);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 uint32_t offset = 0;
101 uint32_t magic = data.GetU32(&offset);
102 return MachHeaderSizeFromMagic(magic) != 0;
103}
104
105
106ObjectFileMachO::ObjectFileMachO(Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) :
107 ObjectFile(module, file, offset, length, dataSP),
108 m_mutex (Mutex::eMutexTypeRecursive),
109 m_header(),
110 m_sections_ap(),
Jim Ingham672e6f52011-03-07 23:44:08 +0000111 m_symtab_ap(),
112 m_entry_point_address ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113{
Greg Clayton72b77eb2011-02-04 21:13:05 +0000114 ::memset (&m_header, 0, sizeof(m_header));
115 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116}
117
118
119ObjectFileMachO::~ObjectFileMachO()
120{
121}
122
123
124bool
125ObjectFileMachO::ParseHeader ()
126{
127 lldb_private::Mutex::Locker locker(m_mutex);
128 bool can_parse = false;
129 uint32_t offset = 0;
Greg Clayton7fb56d02011-02-01 01:31:41 +0000130 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 // Leave magic in the original byte order
132 m_header.magic = m_data.GetU32(&offset);
133 switch (m_header.magic)
134 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000135 case HeaderMagic32:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000136 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137 m_data.SetAddressByteSize(4);
138 can_parse = true;
139 break;
140
Greg Claytone1a916a2010-07-21 22:12:05 +0000141 case HeaderMagic64:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000142 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143 m_data.SetAddressByteSize(8);
144 can_parse = true;
145 break;
146
Greg Claytone1a916a2010-07-21 22:12:05 +0000147 case HeaderMagic32Swapped:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000148 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149 m_data.SetAddressByteSize(4);
150 can_parse = true;
151 break;
152
Greg Claytone1a916a2010-07-21 22:12:05 +0000153 case HeaderMagic64Swapped:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000154 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 m_data.SetAddressByteSize(8);
156 can_parse = true;
157 break;
158
159 default:
160 break;
161 }
162
163 if (can_parse)
164 {
165 m_data.GetU32(&offset, &m_header.cputype, 6);
166
Greg Clayton41f92322010-06-11 03:25:34 +0000167 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jim Ingham5aee1622010-08-09 23:31:02 +0000168
169 if (SetModulesArchitecture (mach_arch))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170 {
171 // Read in all only the load command data
172 DataBufferSP data_sp(m_file.ReadFileContents(m_offset, m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic)));
173 m_data.SetData (data_sp);
174 return true;
175 }
176 }
177 else
178 {
179 memset(&m_header, 0, sizeof(struct mach_header));
180 }
181 return false;
182}
183
184
185ByteOrder
186ObjectFileMachO::GetByteOrder () const
187{
188 lldb_private::Mutex::Locker locker(m_mutex);
189 return m_data.GetByteOrder ();
190}
191
Jim Ingham5aee1622010-08-09 23:31:02 +0000192bool
193ObjectFileMachO::IsExecutable() const
194{
195 return m_header.filetype == HeaderFileTypeExecutable;
196}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197
198size_t
199ObjectFileMachO::GetAddressByteSize () const
200{
201 lldb_private::Mutex::Locker locker(m_mutex);
202 return m_data.GetAddressByteSize ();
203}
204
Greg Claytone0d378b2011-03-24 21:19:54 +0000205AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +0000206ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr)
207{
208 Symtab *symtab = GetSymtab();
209 if (symtab)
210 {
211 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
212 if (symbol)
213 {
214 const AddressRange *range_ptr = symbol->GetAddressRangePtr();
215 if (range_ptr)
216 {
217 const Section *section = range_ptr->GetBaseAddress().GetSection();
218 if (section)
219 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000220 const SectionType section_type = section->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000221 switch (section_type)
222 {
223 case eSectionTypeInvalid: return eAddressClassUnknown;
224 case eSectionTypeCode:
225 if (m_header.cputype == llvm::MachO::CPUTypeARM)
226 {
227 // For ARM we have a bit in the n_desc field of the symbol
228 // that tells us ARM/Thumb which is bit 0x0008.
229 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
230 return eAddressClassCodeAlternateISA;
231 }
232 return eAddressClassCode;
233
234 case eSectionTypeContainer: return eAddressClassUnknown;
235 case eSectionTypeData: return eAddressClassData;
Greg Claytone0d378b2011-03-24 21:19:54 +0000236 case eSectionTypeDataCString: return eAddressClassData;
Greg Claytonded470d2011-03-19 01:12:21 +0000237 case eSectionTypeDataCStringPointers: return eAddressClassData;
238 case eSectionTypeDataSymbolAddress: return eAddressClassData;
239 case eSectionTypeData4: return eAddressClassData;
240 case eSectionTypeData8: return eAddressClassData;
241 case eSectionTypeData16: return eAddressClassData;
242 case eSectionTypeDataPointers: return eAddressClassData;
243 case eSectionTypeZeroFill: return eAddressClassData;
Greg Claytone0d378b2011-03-24 21:19:54 +0000244 case eSectionTypeDataObjCMessageRefs: return eAddressClassData;
245 case eSectionTypeDataObjCCFStrings: return eAddressClassData;
Greg Claytonded470d2011-03-19 01:12:21 +0000246 case eSectionTypeDebug: return eAddressClassDebug;
247 case eSectionTypeDWARFDebugAbbrev: return eAddressClassDebug;
248 case eSectionTypeDWARFDebugAranges: return eAddressClassDebug;
249 case eSectionTypeDWARFDebugFrame: return eAddressClassDebug;
250 case eSectionTypeDWARFDebugInfo: return eAddressClassDebug;
251 case eSectionTypeDWARFDebugLine: return eAddressClassDebug;
252 case eSectionTypeDWARFDebugLoc: return eAddressClassDebug;
253 case eSectionTypeDWARFDebugMacInfo: return eAddressClassDebug;
254 case eSectionTypeDWARFDebugPubNames: return eAddressClassDebug;
255 case eSectionTypeDWARFDebugPubTypes: return eAddressClassDebug;
256 case eSectionTypeDWARFDebugRanges: return eAddressClassDebug;
257 case eSectionTypeDWARFDebugStr: return eAddressClassDebug;
258 case eSectionTypeEHFrame: return eAddressClassRuntime;
259 case eSectionTypeOther: return eAddressClassUnknown;
260 }
261 }
262 }
263
Greg Claytone0d378b2011-03-24 21:19:54 +0000264 const SymbolType symbol_type = symbol->GetType();
Greg Claytonded470d2011-03-19 01:12:21 +0000265 switch (symbol_type)
266 {
267 case eSymbolTypeAny: return eAddressClassUnknown;
268 case eSymbolTypeAbsolute: return eAddressClassUnknown;
269 case eSymbolTypeExtern: return eAddressClassUnknown;
270
271 case eSymbolTypeCode:
272 case eSymbolTypeTrampoline:
273 if (m_header.cputype == llvm::MachO::CPUTypeARM)
274 {
275 // For ARM we have a bit in the n_desc field of the symbol
276 // that tells us ARM/Thumb which is bit 0x0008.
277 if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB)
278 return eAddressClassCodeAlternateISA;
279 }
280 return eAddressClassCode;
281
282 case eSymbolTypeData: return eAddressClassData;
283 case eSymbolTypeRuntime: return eAddressClassRuntime;
284 case eSymbolTypeException: return eAddressClassRuntime;
285 case eSymbolTypeSourceFile: return eAddressClassDebug;
286 case eSymbolTypeHeaderFile: return eAddressClassDebug;
287 case eSymbolTypeObjectFile: return eAddressClassDebug;
288 case eSymbolTypeCommonBlock: return eAddressClassDebug;
289 case eSymbolTypeBlock: return eAddressClassDebug;
290 case eSymbolTypeLocal: return eAddressClassData;
291 case eSymbolTypeParam: return eAddressClassData;
292 case eSymbolTypeVariable: return eAddressClassData;
293 case eSymbolTypeVariableType: return eAddressClassDebug;
294 case eSymbolTypeLineEntry: return eAddressClassDebug;
295 case eSymbolTypeLineHeader: return eAddressClassDebug;
296 case eSymbolTypeScopeBegin: return eAddressClassDebug;
297 case eSymbolTypeScopeEnd: return eAddressClassDebug;
298 case eSymbolTypeAdditional: return eAddressClassUnknown;
299 case eSymbolTypeCompiler: return eAddressClassDebug;
300 case eSymbolTypeInstrumentation:return eAddressClassDebug;
301 case eSymbolTypeUndefined: return eAddressClassUnknown;
302 }
303 }
304 }
305 return eAddressClassUnknown;
306}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307
308Symtab *
309ObjectFileMachO::GetSymtab()
310{
Greg Clayton1a65ae12011-01-25 23:55:37 +0000311 lldb_private::Mutex::Locker symfile_locker(m_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312 if (m_symtab_ap.get() == NULL)
313 {
314 m_symtab_ap.reset(new Symtab(this));
Greg Clayton1a65ae12011-01-25 23:55:37 +0000315 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000316 ParseSymtab (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 }
318 return m_symtab_ap.get();
319}
320
321
322SectionList *
323ObjectFileMachO::GetSectionList()
324{
325 lldb_private::Mutex::Locker locker(m_mutex);
326 if (m_sections_ap.get() == NULL)
327 {
328 m_sections_ap.reset(new SectionList());
329 ParseSections();
330 }
331 return m_sections_ap.get();
332}
333
334
335size_t
336ObjectFileMachO::ParseSections ()
337{
338 lldb::user_id_t segID = 0;
339 lldb::user_id_t sectID = 0;
340 struct segment_command_64 load_cmd;
341 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
342 uint32_t i;
343 //bool dump_sections = false;
344 for (i=0; i<m_header.ncmds; ++i)
345 {
346 const uint32_t load_cmd_offset = offset;
347 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
348 break;
349
Greg Claytone1a916a2010-07-21 22:12:05 +0000350 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351 {
352 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
353 {
354 load_cmd.vmaddr = m_data.GetAddress(&offset);
355 load_cmd.vmsize = m_data.GetAddress(&offset);
356 load_cmd.fileoff = m_data.GetAddress(&offset);
357 load_cmd.filesize = m_data.GetAddress(&offset);
358 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
359 {
Greg Clayton414f5d32011-01-25 02:58:48 +0000360
361 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
362
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363 // Keep a list of mach segments around in case we need to
364 // get at data that isn't stored in the abstracted Sections.
365 m_mach_segments.push_back (load_cmd);
366
367 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
368 // Use a segment ID of the segment index shifted left by 8 so they
369 // never conflict with any of the sections.
370 SectionSP segment_sp;
371 if (segment_name)
372 {
373 segment_sp.reset(new Section (NULL,
374 GetModule(), // Module to which this section belongs
375 ++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
376 segment_name, // Name of this section
377 eSectionTypeContainer, // This section is a container of other sections.
378 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
379 load_cmd.vmsize, // VM size in bytes of this section
380 load_cmd.fileoff, // Offset to the data for this section in the file
381 load_cmd.filesize, // Size in bytes of this section as found in the the file
382 load_cmd.flags)); // Flags for this section
383
Greg Clayton414f5d32011-01-25 02:58:48 +0000384 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 m_sections_ap->AddSection(segment_sp);
386 }
387
388 struct section_64 sect64;
Greg Clayton72b77eb2011-02-04 21:13:05 +0000389 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 // Push a section into our mach sections for the section at
Greg Claytonf4abd0d2010-10-06 01:26:32 +0000391 // index zero (NListSectionNoSection) if we don't have any
392 // mach sections yet...
393 if (m_mach_sections.empty())
394 m_mach_sections.push_back(sect64);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395 uint32_t segment_sect_idx;
396 const lldb::user_id_t first_segment_sectID = sectID + 1;
397
398
Greg Claytone1a916a2010-07-21 22:12:05 +0000399 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
401 {
402 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
403 break;
404 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
405 break;
406 sect64.addr = m_data.GetAddress(&offset);
407 sect64.size = m_data.GetAddress(&offset);
408
409 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
410 break;
411
412 // Keep a list of mach sections around in case we need to
413 // get at data that isn't stored in the abstracted Sections.
414 m_mach_sections.push_back (sect64);
415
416 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
417 if (!segment_name)
418 {
419 // We have a segment with no name so we need to conjure up
420 // segments that correspond to the section's segname if there
421 // isn't already such a section. If there is such a section,
422 // we resize the section so that it spans all sections.
423 // We also mark these sections as fake so address matches don't
424 // hit if they land in the gaps between the child sections.
425 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
426 segment_sp = m_sections_ap->FindSectionByName (segment_name);
427 if (segment_sp.get())
428 {
429 Section *segment = segment_sp.get();
430 // Grow the section size as needed.
431 const lldb::addr_t sect64_min_addr = sect64.addr;
432 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
433 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
434 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
435 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
436 if (sect64_min_addr >= curr_seg_min_addr)
437 {
438 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
439 // Only grow the section size if needed
440 if (new_seg_byte_size > curr_seg_byte_size)
441 segment->SetByteSize (new_seg_byte_size);
442 }
443 else
444 {
445 // We need to change the base address of the segment and
446 // adjust the child section offsets for all existing children.
447 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
448 segment->Slide(slide_amount, false);
449 segment->GetChildren().Slide (-slide_amount, false);
450 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
451 }
Greg Clayton8d38ac42010-06-28 23:51:11 +0000452
453 // Grow the section size as needed.
454 if (sect64.offset)
455 {
456 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
457 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
458
459 const lldb::addr_t section_min_file_offset = sect64.offset;
460 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
461 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
462 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
463 segment->SetFileOffset (new_file_offset);
464 segment->SetFileSize (new_file_size);
465 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466 }
467 else
468 {
469 // Create a fake section for the section's named segment
470 segment_sp.reset(new Section(segment_sp.get(), // Parent section
471 GetModule(), // Module to which this section belongs
472 ++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
473 segment_name, // Name of this section
474 eSectionTypeContainer, // This section is a container of other sections.
475 sect64.addr, // File VM address == addresses as they are found in the object file
476 sect64.size, // VM size in bytes of this section
477 sect64.offset, // Offset to the data for this section in the file
478 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
479 load_cmd.flags)); // Flags for this section
480 segment_sp->SetIsFake(true);
481 m_sections_ap->AddSection(segment_sp);
Greg Clayton414f5d32011-01-25 02:58:48 +0000482 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 }
484 }
485 assert (segment_sp.get());
486
Greg Claytone1a916a2010-07-21 22:12:05 +0000487 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 static ConstString g_sect_name_objc_data ("__objc_data");
489 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
490 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
491 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
492 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
493 static ConstString g_sect_name_objc_const ("__objc_const");
494 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
495 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000496
497 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
498 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
499 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
500 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
501 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
502 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
503 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
504 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
505 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
506 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
507 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
508 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton89411422010-10-08 00:21:05 +0000509 static ConstString g_sect_name_DATA ("__DATA");
510 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000511
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 SectionType sect_type = eSectionTypeOther;
513
Greg Clayton4ceb9982010-07-21 22:54:26 +0000514 if (section_name == g_sect_name_dwarf_debug_abbrev)
515 sect_type = eSectionTypeDWARFDebugAbbrev;
516 else if (section_name == g_sect_name_dwarf_debug_aranges)
517 sect_type = eSectionTypeDWARFDebugAranges;
518 else if (section_name == g_sect_name_dwarf_debug_frame)
519 sect_type = eSectionTypeDWARFDebugFrame;
520 else if (section_name == g_sect_name_dwarf_debug_info)
521 sect_type = eSectionTypeDWARFDebugInfo;
522 else if (section_name == g_sect_name_dwarf_debug_line)
523 sect_type = eSectionTypeDWARFDebugLine;
524 else if (section_name == g_sect_name_dwarf_debug_loc)
525 sect_type = eSectionTypeDWARFDebugLoc;
526 else if (section_name == g_sect_name_dwarf_debug_macinfo)
527 sect_type = eSectionTypeDWARFDebugMacInfo;
528 else if (section_name == g_sect_name_dwarf_debug_pubnames)
529 sect_type = eSectionTypeDWARFDebugPubNames;
530 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
531 sect_type = eSectionTypeDWARFDebugPubTypes;
532 else if (section_name == g_sect_name_dwarf_debug_ranges)
533 sect_type = eSectionTypeDWARFDebugRanges;
534 else if (section_name == g_sect_name_dwarf_debug_str)
535 sect_type = eSectionTypeDWARFDebugStr;
536 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000537 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton4ceb9982010-07-21 22:54:26 +0000540 else if (section_name == g_sect_name_eh_frame)
541 sect_type = eSectionTypeEHFrame;
542 else if (section_name == g_sect_name_cfstring)
543 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544 else if (section_name == g_sect_name_objc_data ||
545 section_name == g_sect_name_objc_classrefs ||
546 section_name == g_sect_name_objc_superrefs ||
547 section_name == g_sect_name_objc_const ||
548 section_name == g_sect_name_objc_classlist)
549 {
550 sect_type = eSectionTypeDataPointers;
551 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000552
553 if (sect_type == eSectionTypeOther)
554 {
555 switch (mach_sect_type)
556 {
557 // TODO: categorize sections by other flags for regular sections
Greg Clayton89411422010-10-08 00:21:05 +0000558 case SectionTypeRegular:
559 if (segment_sp->GetName() == g_sect_name_TEXT)
560 sect_type = eSectionTypeCode;
561 else if (segment_sp->GetName() == g_sect_name_DATA)
562 sect_type = eSectionTypeData;
563 else
564 sect_type = eSectionTypeOther;
565 break;
Greg Claytone1a916a2010-07-21 22:12:05 +0000566 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
567 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
568 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
569 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
570 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
571 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
572 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
573 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
574 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
575 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
576 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
577 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
578 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
579 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
580 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
581 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582 default: break;
583 }
584 }
585
586 SectionSP section_sp(new Section(segment_sp.get(),
587 GetModule(),
588 ++sectID,
589 section_name,
590 sect_type,
591 sect64.addr - segment_sp->GetFileAddress(),
592 sect64.size,
593 sect64.offset,
594 sect64.offset == 0 ? 0 : sect64.size,
595 sect64.flags));
Greg Clayton414f5d32011-01-25 02:58:48 +0000596 // Set the section to be encrypted to match the segment
597 section_sp->SetIsEncrypted (segment_is_encrypted);
598
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599 segment_sp->GetChildren().AddSection(section_sp);
600
601 if (segment_sp->IsFake())
602 {
603 segment_sp.reset();
604 segment_name.Clear();
605 }
606 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000607 if (m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608 {
609 if (first_segment_sectID <= sectID)
610 {
611 lldb::user_id_t sect_uid;
612 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
613 {
614 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
615 SectionSP next_section_sp;
616 if (sect_uid + 1 <= sectID)
617 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
618
619 if (curr_section_sp.get())
620 {
621 if (curr_section_sp->GetByteSize() == 0)
622 {
623 if (next_section_sp.get() != NULL)
624 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
625 else
626 curr_section_sp->SetByteSize ( load_cmd.vmsize );
627 }
628 }
629 }
630 }
631 }
632 }
633 }
634 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000635 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636 {
637 m_dysymtab.cmd = load_cmd.cmd;
638 m_dysymtab.cmdsize = load_cmd.cmdsize;
639 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
640 }
641
642 offset = load_cmd_offset + load_cmd.cmdsize;
643 }
644// if (dump_sections)
645// {
646// StreamFile s(stdout);
647// m_sections_ap->Dump(&s, true);
648// }
649 return sectID; // Return the number of sections we registered with the module
650}
651
652class MachSymtabSectionInfo
653{
654public:
655
656 MachSymtabSectionInfo (SectionList *section_list) :
657 m_section_list (section_list),
658 m_section_infos()
659 {
660 // Get the number of sections down to a depth of 1 to include
661 // all segments and their sections, but no other sections that
662 // may be added for debug map or
663 m_section_infos.resize(section_list->GetNumSections(1));
664 }
665
666
667 Section *
668 GetSection (uint8_t n_sect, addr_t file_addr)
669 {
670 if (n_sect == 0)
671 return NULL;
672 if (n_sect < m_section_infos.size())
673 {
674 if (m_section_infos[n_sect].section == NULL)
675 {
676 Section *section = m_section_list->FindSectionByID (n_sect).get();
677 m_section_infos[n_sect].section = section;
678 assert (section != NULL);
679 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
680 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
681 }
682 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
683 return m_section_infos[n_sect].section;
684 }
685 return m_section_list->FindSectionContainingFileAddress(file_addr).get();
686 }
687
688protected:
689 struct SectionInfo
690 {
691 SectionInfo () :
692 vm_range(),
693 section (NULL)
694 {
695 }
696
697 VMRange vm_range;
698 Section *section;
699 };
700 SectionList *m_section_list;
701 std::vector<SectionInfo> m_section_infos;
702};
703
704
705
706size_t
707ObjectFileMachO::ParseSymtab (bool minimize)
708{
709 Timer scoped_timer(__PRETTY_FUNCTION__,
710 "ObjectFileMachO::ParseSymtab () module = %s",
711 m_file.GetFilename().AsCString(""));
712 struct symtab_command symtab_load_command;
713 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
714 uint32_t i;
715 for (i=0; i<m_header.ncmds; ++i)
716 {
717 const uint32_t cmd_offset = offset;
718 // Read in the load command and load command size
719 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
720 break;
721 // Watch for the symbol table load command
Greg Claytone1a916a2010-07-21 22:12:05 +0000722 if (symtab_load_command.cmd == LoadCommandSymtab)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723 {
724 // Read in the rest of the symtab load command
Jason Molendaea84e762010-07-06 22:38:03 +0000725 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726 {
727 Symtab *symtab = m_symtab_ap.get();
728 SectionList *section_list = GetSectionList();
729 assert(section_list);
730 const size_t addr_size = m_data.GetAddressByteSize();
731 const ByteOrder endian = m_data.GetByteOrder();
732 bool bit_width_32 = addr_size == 4;
733 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
734
735 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size));
736 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize));
737
738 const char *strtab_data = (const char *)strtab_data_sp->GetBytes();
739// DataExtractor symtab_data(symtab_data_sp, endian, addr_size);
740// DataExtractor strtab_data(strtab_data_sp, endian, addr_size);
741
742 static ConstString g_segment_name_TEXT ("__TEXT");
743 static ConstString g_segment_name_DATA ("__DATA");
744 static ConstString g_segment_name_OBJC ("__OBJC");
745 static ConstString g_section_name_eh_frame ("__eh_frame");
746 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
747 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
748 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
749 SectionSP eh_frame_section_sp;
750 if (text_section_sp.get())
751 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
752 else
753 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
754
Greg Claytone1a916a2010-07-21 22:12:05 +0000755 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756 //uint32_t symtab_offset = 0;
757 const uint8_t* nlist_data = symtab_data_sp->GetBytes();
758 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms);
759
760
Greg Clayton7fb56d02011-02-01 01:31:41 +0000761 if (endian != lldb::endian::InlHostByteOrder())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762 {
763 // ...
764 assert (!"UNIMPLEMENTED: Swap all nlist entries");
765 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000766 uint32_t N_SO_index = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767
768 MachSymtabSectionInfo section_info (section_list);
769 std::vector<uint32_t> N_FUN_indexes;
770 std::vector<uint32_t> N_NSYM_indexes;
771 std::vector<uint32_t> N_INCL_indexes;
772 std::vector<uint32_t> N_BRAC_indexes;
773 std::vector<uint32_t> N_COMM_indexes;
Greg Clayton928d8292010-09-08 16:38:06 +0000774 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000775 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Clayton928d8292010-09-08 16:38:06 +0000776 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
777 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000778 // Any symbols that get merged into another will get an entry
779 // in this map so we know
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000780 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781 uint32_t nlist_idx = 0;
782 Symbol *symbol_ptr = NULL;
783
784 uint32_t sym_idx = 0;
785 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
786 uint32_t num_syms = symtab->GetNumSymbols();
787
788 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
789 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
790 {
791 struct nlist_64 nlist;
792 if (bit_width_32)
793 {
794 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size));
Greg Claytone1a916a2010-07-21 22:12:05 +0000795 nlist.n_strx = nlist32_ptr->n_strx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796 nlist.n_type = nlist32_ptr->n_type;
797 nlist.n_sect = nlist32_ptr->n_sect;
798 nlist.n_desc = nlist32_ptr->n_desc;
799 nlist.n_value = nlist32_ptr->n_value;
800 }
801 else
802 {
803 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size)));
804 }
805
806 SymbolType type = eSymbolTypeInvalid;
Greg Claytone1a916a2010-07-21 22:12:05 +0000807 const char* symbol_name = &strtab_data[nlist.n_strx];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 if (symbol_name[0] == '\0')
809 symbol_name = NULL;
810 Section* symbol_section = NULL;
811 bool add_nlist = true;
Greg Claytone1a916a2010-07-21 22:12:05 +0000812 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813
814 assert (sym_idx < num_syms);
815
816 sym[sym_idx].SetDebug (is_debug);
817
818 if (is_debug)
819 {
820 switch (nlist.n_type)
821 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000822 case StabGlobalSymbol:
823 // N_GSYM -- global symbol: name,,NO_SECT,type,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000824 // Sometimes the N_GSYM value contains the address.
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000825 sym[sym_idx].SetExternal(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 if (nlist.n_value != 0)
827 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000828 type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 break;
830
Greg Claytone1a916a2010-07-21 22:12:05 +0000831 case StabFunctionName:
832 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000833 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834 break;
835
Greg Claytone1a916a2010-07-21 22:12:05 +0000836 case StabFunction:
837 // N_FUN -- procedure: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838 if (symbol_name)
839 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000840 type = eSymbolTypeCode;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton928d8292010-09-08 16:38:06 +0000842
843 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844 // We use the current number of symbols in the symbol table in lieu of
845 // using nlist_idx in case we ever start trimming entries out
846 N_FUN_indexes.push_back(sym_idx);
847 }
848 else
849 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000850 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851
852 if ( !N_FUN_indexes.empty() )
853 {
854 // Copy the size of the function into the original STAB entry so we don't have
855 // to hunt for it later
856 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
857 N_FUN_indexes.pop_back();
Jason Molendaea84e762010-07-06 22:38:03 +0000858 // We don't really need the end function STAB as it contains the size which
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859 // we already placed with the original symbol, so don't add it if we want a
860 // minimal symbol table
861 if (minimize)
862 add_nlist = false;
863 }
864 }
865 break;
866
Greg Claytone1a916a2010-07-21 22:12:05 +0000867 case StabStaticSymbol:
868 // N_STSYM -- static symbol: name,,n_sect,type,address
Greg Clayton928d8292010-09-08 16:38:06 +0000869 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000870 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000871 type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872 break;
873
Greg Claytone1a916a2010-07-21 22:12:05 +0000874 case StabLocalCommon:
875 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
877 type = eSymbolTypeCommonBlock;
878 break;
879
Greg Claytone1a916a2010-07-21 22:12:05 +0000880 case StabBeginSymbol:
881 // N_BNSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882 // We use the current number of symbols in the symbol table in lieu of
883 // using nlist_idx in case we ever start trimming entries out
884 if (minimize)
885 {
886 // Skip these if we want minimal symbol tables
887 add_nlist = false;
888 }
889 else
890 {
891 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
892 N_NSYM_indexes.push_back(sym_idx);
893 type = eSymbolTypeScopeBegin;
894 }
895 break;
896
Greg Claytone1a916a2010-07-21 22:12:05 +0000897 case StabEndSymbol:
898 // N_ENSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
900 // so that we can always skip the entire symbol if we need to navigate
901 // more quickly at the source level when parsing STABS
902 if (minimize)
903 {
904 // Skip these if we want minimal symbol tables
905 add_nlist = false;
906 }
907 else
908 {
909 if ( !N_NSYM_indexes.empty() )
910 {
911 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
912 symbol_ptr->SetByteSize(sym_idx + 1);
913 symbol_ptr->SetSizeIsSibling(true);
914 N_NSYM_indexes.pop_back();
915 }
916 type = eSymbolTypeScopeEnd;
917 }
918 break;
919
920
Greg Claytone1a916a2010-07-21 22:12:05 +0000921 case StabSourceFileOptions:
922 // N_OPT - emitted with gcc2_compiled and in gcc source
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000923 type = eSymbolTypeCompiler;
924 break;
925
Greg Claytone1a916a2010-07-21 22:12:05 +0000926 case StabRegisterSymbol:
927 // N_RSYM - register sym: name,,NO_SECT,type,register
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928 type = eSymbolTypeVariable;
929 break;
930
Greg Claytone1a916a2010-07-21 22:12:05 +0000931 case StabSourceLine:
932 // N_SLINE - src line: 0,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
934 type = eSymbolTypeLineEntry;
935 break;
936
Greg Claytone1a916a2010-07-21 22:12:05 +0000937 case StabStructureType:
938 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939 type = eSymbolTypeVariableType;
940 break;
941
Greg Claytone1a916a2010-07-21 22:12:05 +0000942 case StabSourceFileName:
943 // N_SO - source file name
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000944 type = eSymbolTypeSourceFile;
945 if (symbol_name == NULL)
946 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000947 if (minimize)
948 add_nlist = false;
949 if (N_SO_index != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950 {
951 // Set the size of the N_SO to the terminating index of this N_SO
952 // so that we can always skip the entire N_SO if we need to navigate
953 // more quickly at the source level when parsing STABS
954 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000955 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956 symbol_ptr->SetSizeIsSibling(true);
957 }
958 N_NSYM_indexes.clear();
959 N_INCL_indexes.clear();
960 N_BRAC_indexes.clear();
961 N_COMM_indexes.clear();
962 N_FUN_indexes.clear();
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000963 N_SO_index = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000964 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000965 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966 {
967 // We use the current number of symbols in the symbol table in lieu of
968 // using nlist_idx in case we ever start trimming entries out
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000969 if (symbol_name[0] == '/')
970 N_SO_index = sym_idx;
Greg Clayton5cf21f52011-06-19 04:26:01 +0000971 else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms))
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000972 {
973 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
974 if (so_path && so_path[0])
975 {
976 std::string full_so_path (so_path);
977 if (*full_so_path.rbegin() != '/')
978 full_so_path += '/';
979 full_so_path += symbol_name;
980 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
981 add_nlist = false;
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000982 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000983 }
984 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000986
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987 break;
988
Greg Claytone1a916a2010-07-21 22:12:05 +0000989 case StabObjectFileName:
990 // N_OSO - object file name: name,,0,0,st_mtime
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000991 type = eSymbolTypeObjectFile;
992 break;
993
Greg Claytone1a916a2010-07-21 22:12:05 +0000994 case StabLocalSymbol:
995 // N_LSYM - local sym: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996 type = eSymbolTypeLocal;
997 break;
998
999 //----------------------------------------------------------------------
1000 // INCL scopes
1001 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +00001002 case StabBeginIncludeFileName:
1003 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004 // We use the current number of symbols in the symbol table in lieu of
1005 // using nlist_idx in case we ever start trimming entries out
1006 N_INCL_indexes.push_back(sym_idx);
1007 type = eSymbolTypeScopeBegin;
1008 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009
Greg Claytone1a916a2010-07-21 22:12:05 +00001010 case StabEndIncludeFile:
1011 // N_EINCL - include file end: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012 // Set the size of the N_BINCL to the terminating index of this N_EINCL
1013 // so that we can always skip the entire symbol if we need to navigate
1014 // more quickly at the source level when parsing STABS
1015 if ( !N_INCL_indexes.empty() )
1016 {
1017 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
1018 symbol_ptr->SetByteSize(sym_idx + 1);
1019 symbol_ptr->SetSizeIsSibling(true);
1020 N_INCL_indexes.pop_back();
1021 }
1022 type = eSymbolTypeScopeEnd;
1023 break;
1024
Greg Claytone1a916a2010-07-21 22:12:05 +00001025 case StabIncludeFileName:
1026 // N_SOL - #included file name: name,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027 type = eSymbolTypeHeaderFile;
Greg Clayton49bd1c82010-09-07 17:36:17 +00001028
1029 // We currently don't use the header files on darwin
1030 if (minimize)
1031 add_nlist = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001032 break;
1033
Greg Claytone1a916a2010-07-21 22:12:05 +00001034 case StabCompilerParameters:
1035 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001036 type = eSymbolTypeCompiler;
1037 break;
1038
Greg Claytone1a916a2010-07-21 22:12:05 +00001039 case StabCompilerVersion:
1040 // N_VERSION - compiler version: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 type = eSymbolTypeCompiler;
1042 break;
1043
Greg Claytone1a916a2010-07-21 22:12:05 +00001044 case StabCompilerOptLevel:
1045 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046 type = eSymbolTypeCompiler;
1047 break;
1048
Greg Claytone1a916a2010-07-21 22:12:05 +00001049 case StabParameter:
1050 // N_PSYM - parameter: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051 type = eSymbolTypeVariable;
1052 break;
1053
Greg Claytone1a916a2010-07-21 22:12:05 +00001054 case StabAlternateEntry:
1055 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001056 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1057 type = eSymbolTypeLineEntry;
1058 break;
1059
1060 //----------------------------------------------------------------------
1061 // Left and Right Braces
1062 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +00001063 case StabLeftBracket:
1064 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065 // We use the current number of symbols in the symbol table in lieu of
1066 // using nlist_idx in case we ever start trimming entries out
1067 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1068 N_BRAC_indexes.push_back(sym_idx);
1069 type = eSymbolTypeScopeBegin;
1070 break;
1071
Greg Claytone1a916a2010-07-21 22:12:05 +00001072 case StabRightBracket:
1073 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
1075 // so that we can always skip the entire symbol if we need to navigate
1076 // more quickly at the source level when parsing STABS
1077 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1078 if ( !N_BRAC_indexes.empty() )
1079 {
1080 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
1081 symbol_ptr->SetByteSize(sym_idx + 1);
1082 symbol_ptr->SetSizeIsSibling(true);
1083 N_BRAC_indexes.pop_back();
1084 }
1085 type = eSymbolTypeScopeEnd;
1086 break;
1087
Greg Claytone1a916a2010-07-21 22:12:05 +00001088 case StabDeletedIncludeFile:
1089 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090 type = eSymbolTypeHeaderFile;
1091 break;
1092
1093 //----------------------------------------------------------------------
1094 // COMM scopes
1095 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +00001096 case StabBeginCommon:
1097 // N_BCOMM - begin common: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001098 // We use the current number of symbols in the symbol table in lieu of
1099 // using nlist_idx in case we ever start trimming entries out
1100 type = eSymbolTypeScopeBegin;
1101 N_COMM_indexes.push_back(sym_idx);
1102 break;
1103
Greg Claytone1a916a2010-07-21 22:12:05 +00001104 case StabEndCommonLocal:
1105 // N_ECOML - end common (local name): 0,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1107 // Fall through
1108
Greg Claytone1a916a2010-07-21 22:12:05 +00001109 case StabEndCommon:
1110 // N_ECOMM - end common: name,,n_sect,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1112 // so that we can always skip the entire symbol if we need to navigate
1113 // more quickly at the source level when parsing STABS
1114 if ( !N_COMM_indexes.empty() )
1115 {
1116 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1117 symbol_ptr->SetByteSize(sym_idx + 1);
1118 symbol_ptr->SetSizeIsSibling(true);
1119 N_COMM_indexes.pop_back();
1120 }
1121 type = eSymbolTypeScopeEnd;
1122 break;
1123
Greg Claytone1a916a2010-07-21 22:12:05 +00001124 case StabLength:
1125 // N_LENG - second stab entry with length information
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126 type = eSymbolTypeAdditional;
1127 break;
1128
1129 default: break;
1130 }
1131 }
1132 else
1133 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001134 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
1135 uint8_t n_type = NlistMaskType & nlist.n_type;
1136 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001137
1138 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name)
1139 {
1140 type = eSymbolTypeRuntime;
1141 }
1142 else
1143 {
1144 switch (n_type)
1145 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001146 case NListTypeIndirect: // N_INDR - Fall through
1147 case NListTypePreboundUndefined:// N_PBUD - Fall through
1148 case NListTypeUndefined: // N_UNDF
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149 type = eSymbolTypeExtern;
1150 break;
1151
Greg Claytone1a916a2010-07-21 22:12:05 +00001152 case NListTypeAbsolute: // N_ABS
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153 type = eSymbolTypeAbsolute;
1154 break;
1155
Greg Claytone1a916a2010-07-21 22:12:05 +00001156 case NListTypeSection: // N_SECT
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1158
1159 assert(symbol_section != NULL);
1160 if (TEXT_eh_frame_sectID == nlist.n_sect)
1161 {
1162 type = eSymbolTypeException;
1163 }
1164 else
1165 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001166 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167
1168 switch (section_type)
1169 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001170 case SectionTypeRegular: break; // regular section
1171 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
1172 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
1173 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
1174 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
1175 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1176 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1177 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1178 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1179 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
1180 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
1181 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
1182 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
1183 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
1184 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
1185 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
1186 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187 default: break;
1188 }
1189
1190 if (type == eSymbolTypeInvalid)
1191 {
1192 const char *symbol_sect_name = symbol_section->GetName().AsCString();
1193 if (symbol_section->IsDescendant (text_section_sp.get()))
1194 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001195 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1196 SectionAttrUserSelfModifyingCode |
1197 SectionAttrSytemSomeInstructions))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 type = eSymbolTypeData;
1199 else
1200 type = eSymbolTypeCode;
1201 }
1202 else
1203 if (symbol_section->IsDescendant(data_section_sp.get()))
1204 {
1205 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1206 {
1207 type = eSymbolTypeRuntime;
1208 }
1209 else
1210 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1211 {
1212 type = eSymbolTypeException;
1213 }
1214 else
1215 {
1216 type = eSymbolTypeData;
1217 }
1218 }
1219 else
1220 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1221 {
1222 type = eSymbolTypeTrampoline;
1223 }
1224 else
1225 if (symbol_section->IsDescendant(objc_section_sp.get()))
1226 {
1227 type = eSymbolTypeRuntime;
1228 }
1229 }
1230 }
1231 break;
Greg Clayton928d8292010-09-08 16:38:06 +00001232 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233 }
1234 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001235 if (add_nlist)
1236 {
1237 bool symbol_name_is_mangled = false;
1238 if (symbol_name && symbol_name[0] == '_')
1239 {
1240 symbol_name_is_mangled = symbol_name[1] == '_';
1241 symbol_name++; // Skip the leading underscore
1242 }
1243 uint64_t symbol_value = nlist.n_value;
Greg Clayton928d8292010-09-08 16:38:06 +00001244
1245 if (symbol_name)
1246 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001247 if (is_debug == false)
Greg Clayton928d8292010-09-08 16:38:06 +00001248 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001249 if (type == eSymbolTypeCode)
Greg Clayton928d8292010-09-08 16:38:06 +00001250 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001251 // See if we can find a N_FUN entry for any code symbols.
1252 // If we do find a match, and the name matches, then we
1253 // can merge the two into just the function symbol to avoid
1254 // duplicate entries in the symbol table
1255 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1256 if (pos != N_FUN_addr_to_sym_idx.end())
Greg Clayton928d8292010-09-08 16:38:06 +00001257 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001258 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1259 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1260 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001261 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001262 // We just need the flags from the linker symbol, so put these flags
1263 // into the N_FUN flags to avoid duplicate symbols in the symbol table
1264 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1265 sym[sym_idx].Clear();
1266 continue;
1267 }
Greg Clayton928d8292010-09-08 16:38:06 +00001268 }
1269 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001270 else if (type == eSymbolTypeData)
Greg Clayton928d8292010-09-08 16:38:06 +00001271 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001272 // See if we can find a N_STSYM entry for any data symbols.
1273 // If we do find a match, and the name matches, then we
1274 // can merge the two into just the Static symbol to avoid
1275 // duplicate entries in the symbol table
1276 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
1277 if (pos != N_STSYM_addr_to_sym_idx.end())
Greg Clayton928d8292010-09-08 16:38:06 +00001278 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001279 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1280 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1281 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001282 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001283 // We just need the flags from the linker symbol, so put these flags
1284 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
1285 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1286 sym[sym_idx].Clear();
1287 continue;
1288 }
Greg Clayton928d8292010-09-08 16:38:06 +00001289 }
1290 }
1291 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001292 if (symbol_section != NULL)
1293 symbol_value -= symbol_section->GetFileAddress();
1294
1295 sym[sym_idx].SetID (nlist_idx);
1296 sym[sym_idx].SetType (type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1298 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1299 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1300
1301 ++sym_idx;
1302 }
1303 else
1304 {
1305 sym[sym_idx].Clear();
1306 }
1307
1308 }
1309
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001310 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1311 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1312 // such entries by figuring out what the address for the global is by looking up this non-STAB
1313 // entry and copying the value into the debug symbol's value to save us the hassle in the
1314 // debug symbol parser.
1315
1316 Symbol *global_symbol = NULL;
1317 for (nlist_idx = 0;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001318 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 +00001319 nlist_idx++)
1320 {
1321 if (global_symbol->GetValue().GetFileAddress() == 0)
1322 {
1323 std::vector<uint32_t> indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001324 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001325 {
1326 std::vector<uint32_t>::const_iterator pos;
1327 std::vector<uint32_t>::const_iterator end = indexes.end();
1328 for (pos = indexes.begin(); pos != end; ++pos)
1329 {
1330 symbol_ptr = symtab->SymbolAtIndex(*pos);
1331 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1332 {
1333 global_symbol->SetValue(symbol_ptr->GetValue());
1334 break;
1335 }
1336 }
1337 }
1338 }
1339 }
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001340
1341 // Trim our symbols down to just what we ended up with after
1342 // removing any symbols.
1343 if (sym_idx < num_syms)
1344 {
1345 num_syms = sym_idx;
1346 sym = symtab->Resize (num_syms);
1347 }
1348
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001349 // Now synthesize indirect symbols
1350 if (m_dysymtab.nindirectsyms != 0)
1351 {
1352 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4));
1353
1354 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize())
1355 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001356 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1358
1359 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1360 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001361 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001362 {
1363 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1364 if (symbol_stub_byte_size == 0)
1365 continue;
1366
1367 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1368
1369 if (num_symbol_stubs == 0)
1370 continue;
1371
1372 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001373 uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1375 {
1376 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1377 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1378 uint32_t symbol_stub_offset = symbol_stub_index * 4;
1379 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1380 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001381 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Greg Claytonf4abd0d2010-10-06 01:26:32 +00001382 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
1383 continue;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001384
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001385 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
1386 Symbol *stub_symbol = NULL;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001387 if (index_pos != end_index_pos)
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001388 {
1389 // We have a remapping from the original nlist index to
1390 // a current symbol index, so just look this up by index
1391 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
1392 }
1393 else
1394 {
1395 // We need to lookup a symbol using the original nlist
1396 // symbol index since this index is coming from the
1397 // S_SYMBOL_STUBS
1398 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
1399 }
Greg Clayton49bd1c82010-09-07 17:36:17 +00001400
1401 assert (stub_symbol);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001402 if (stub_symbol)
1403 {
1404 Address so_addr(symbol_stub_addr, section_list);
1405
1406 if (stub_symbol->GetType() == eSymbolTypeExtern)
1407 {
1408 // Change the external symbol into a trampoline that makes sense
1409 // These symbols were N_UNDF N_EXT, and are useless to us, so we
1410 // can re-use them so we don't have to make up a synthetic symbol
1411 // for no good reason.
1412 stub_symbol->SetType (eSymbolTypeTrampoline);
1413 stub_symbol->SetExternal (false);
1414 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1415 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1416 }
1417 else
1418 {
1419 // Make a synthetic symbol to describe the trampoline stub
1420 if (sym_idx >= num_syms)
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001421 sym = symtab->Resize (++num_syms);
1422 sym[sym_idx].SetID (synthetic_stub_sym_id++);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001423 sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1424 sym[sym_idx].SetType (eSymbolTypeTrampoline);
1425 sym[sym_idx].SetIsSynthetic (true);
1426 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1427 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1428 ++sym_idx;
1429 }
1430 }
1431 }
1432 }
1433 }
1434 }
1435 }
1436 }
1437
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001438 return symtab->GetNumSymbols();
1439 }
1440 }
1441 offset = cmd_offset + symtab_load_command.cmdsize;
1442 }
1443 return 0;
1444}
1445
1446
1447void
1448ObjectFileMachO::Dump (Stream *s)
1449{
1450 lldb_private::Mutex::Locker locker(m_mutex);
1451 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1452 s->Indent();
Greg Claytone1a916a2010-07-21 22:12:05 +00001453 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001454 s->PutCString("ObjectFileMachO64");
1455 else
1456 s->PutCString("ObjectFileMachO32");
1457
Greg Clayton41f92322010-06-11 03:25:34 +00001458 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001459
Greg Clayton64195a22011-02-23 00:35:02 +00001460 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461
1462 if (m_sections_ap.get())
Greg Clayton10177aa2010-12-08 05:08:21 +00001463 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001464
1465 if (m_symtab_ap.get())
Greg Clayton8087ca22010-10-08 04:20:14 +00001466 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467}
1468
1469
1470bool
Greg Clayton60830262011-02-04 18:53:10 +00001471ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472{
1473 lldb_private::Mutex::Locker locker(m_mutex);
1474 struct uuid_command load_cmd;
1475 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1476 uint32_t i;
1477 for (i=0; i<m_header.ncmds; ++i)
1478 {
1479 const uint32_t cmd_offset = offset;
1480 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1481 break;
1482
Greg Claytone1a916a2010-07-21 22:12:05 +00001483 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001484 {
1485 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1486 if (uuid_bytes)
1487 {
1488 uuid->SetBytes (uuid_bytes);
1489 return true;
1490 }
1491 return false;
1492 }
1493 offset = cmd_offset + load_cmd.cmdsize;
1494 }
1495 return false;
1496}
1497
1498
1499uint32_t
1500ObjectFileMachO::GetDependentModules (FileSpecList& files)
1501{
1502 lldb_private::Mutex::Locker locker(m_mutex);
1503 struct load_command load_cmd;
1504 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1505 uint32_t count = 0;
Greg Clayton9b72eb72011-05-24 23:06:02 +00001506 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 +00001507 uint32_t i;
1508 for (i=0; i<m_header.ncmds; ++i)
1509 {
1510 const uint32_t cmd_offset = offset;
1511 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1512 break;
1513
1514 switch (load_cmd.cmd)
1515 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001516 case LoadCommandDylibLoad:
1517 case LoadCommandDylibLoadWeak:
1518 case LoadCommandDylibReexport:
1519 case LoadCommandDynamicLinkerLoad:
1520 case LoadCommandFixedVMShlibLoad:
Greg Clayton74f6e9f2010-10-09 00:48:53 +00001521 case LoadCommandDylibLoadUpward:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001522 {
1523 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1524 const char *path = m_data.PeekCStr(name_offset);
1525 // Skip any path that starts with '@' since these are usually:
1526 // @executable_path/.../file
1527 // @rpath/.../file
1528 if (path && path[0] != '@')
1529 {
Greg Clayton9b72eb72011-05-24 23:06:02 +00001530 FileSpec file_spec(path, resolve_path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001531 if (files.AppendIfUnique(file_spec))
1532 count++;
1533 }
1534 }
1535 break;
1536
1537 default:
1538 break;
1539 }
1540 offset = cmd_offset + load_cmd.cmdsize;
1541 }
1542 return count;
1543}
1544
Jim Ingham672e6f52011-03-07 23:44:08 +00001545lldb_private::Address
1546ObjectFileMachO::GetEntryPointAddress ()
1547{
1548 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
1549 // is initialized to an invalid address, so we can just return that.
1550 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
1551
1552 if (!IsExecutable() || m_entry_point_address.IsValid())
1553 return m_entry_point_address;
1554
1555 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
1556 // /usr/include/mach-o.h, but it is basically:
1557 //
1558 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
1559 // uint32_t count - this is the count of longs in the thread state data
1560 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
1561 // <repeat this trio>
1562 //
1563 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
1564 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
1565 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
1566 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
1567 //
1568 // For now we hard-code the offsets and flavors we need:
1569 //
1570 //
1571
1572 lldb_private::Mutex::Locker locker(m_mutex);
1573 struct load_command load_cmd;
1574 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1575 uint32_t i;
1576 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
1577 bool done = false;
1578
1579 for (i=0; i<m_header.ncmds; ++i)
1580 {
1581 const uint32_t cmd_offset = offset;
1582 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1583 break;
1584
1585 switch (load_cmd.cmd)
1586 {
1587 case LoadCommandUnixThread:
1588 case LoadCommandThread:
1589 {
1590 while (offset < cmd_offset + load_cmd.cmdsize)
1591 {
1592 uint32_t flavor = m_data.GetU32(&offset);
1593 uint32_t count = m_data.GetU32(&offset);
1594 if (count == 0)
1595 {
1596 // We've gotten off somehow, log and exit;
1597 return m_entry_point_address;
1598 }
1599
1600 switch (m_header.cputype)
1601 {
1602 case llvm::MachO::CPUTypeARM:
1603 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
1604 {
1605 offset += 60; // This is the offset of pc in the GPR thread state data structure.
1606 start_address = m_data.GetU32(&offset);
1607 done = true;
1608 }
1609 break;
1610 case llvm::MachO::CPUTypeI386:
1611 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
1612 {
1613 offset += 40; // This is the offset of eip in the GPR thread state data structure.
1614 start_address = m_data.GetU32(&offset);
1615 done = true;
1616 }
1617 break;
1618 case llvm::MachO::CPUTypeX86_64:
1619 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
1620 {
1621 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
1622 start_address = m_data.GetU64(&offset);
1623 done = true;
1624 }
1625 break;
1626 default:
1627 return m_entry_point_address;
1628 }
1629 // Haven't found the GPR flavor yet, skip over the data for this flavor:
1630 if (done)
1631 break;
1632 offset += count * 4;
1633 }
1634 }
1635 break;
1636
1637 default:
1638 break;
1639 }
1640 if (done)
1641 break;
1642
1643 // Go to the next load command:
1644 offset = cmd_offset + load_cmd.cmdsize;
1645 }
1646
1647 if (start_address != LLDB_INVALID_ADDRESS)
1648 {
1649 // We got the start address from the load commands, so now resolve that address in the sections
1650 // of this ObjectFile:
1651 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
1652 {
1653 m_entry_point_address.Clear();
1654 }
1655 }
1656 else
1657 {
1658 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
1659 // "start" symbol in the main executable.
1660
1661 SymbolContextList contexts;
1662 SymbolContext context;
Greg Claytone0d378b2011-03-24 21:19:54 +00001663 if (!m_module->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts))
Jim Ingham672e6f52011-03-07 23:44:08 +00001664 return m_entry_point_address;
1665
1666 contexts.GetContextAtIndex(0, context);
1667
1668 m_entry_point_address = context.symbol->GetValue();
1669 }
1670
1671 return m_entry_point_address;
1672
1673}
1674
Greg Clayton9e00b6a652011-07-09 00:41:34 +00001675ObjectFile::Type
1676ObjectFileMachO::CalculateType()
1677{
1678 switch (m_header.filetype)
1679 {
1680 case HeaderFileTypeObject: // 0x1u MH_OBJECT
1681 if (GetAddressByteSize () == 4)
1682 {
1683 // 32 bit kexts are just object files, but they do have a valid
1684 // UUID load command.
1685 UUID uuid;
1686 if (GetUUID(&uuid))
1687 {
1688 // this checking for the UUID load command is not enough
1689 // we could eventually look for the symbol named
1690 // "OSKextGetCurrentIdentifier" as this is required of kexts
1691 if (m_strata == eStrataInvalid)
1692 m_strata = eStrataKernel;
1693 return eTypeSharedLibrary;
1694 }
1695 }
1696 return eTypeObjectFile;
1697
1698 case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE
1699 case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB
1700 case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE
1701 case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD
1702 case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB
1703 case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER
1704 case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE
1705 case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB
1706 case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM
1707 case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE
1708 default:
1709 break;
1710 }
1711 return eTypeUnknown;
1712}
1713
1714ObjectFile::Strata
1715ObjectFileMachO::CalculateStrata()
1716{
1717 switch (m_header.filetype)
1718 {
1719 case HeaderFileTypeObject: // 0x1u MH_OBJECT
1720 {
1721 // 32 bit kexts are just object files, but they do have a valid
1722 // UUID load command.
1723 UUID uuid;
1724 if (GetUUID(&uuid))
1725 {
1726 // this checking for the UUID load command is not enough
1727 // we could eventually look for the symbol named
1728 // "OSKextGetCurrentIdentifier" as this is required of kexts
1729 if (m_type == eTypeInvalid)
1730 m_type = eTypeSharedLibrary;
1731
1732 return eStrataKernel;
1733 }
1734 }
1735 return eStrataUnknown;
1736
1737 case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE
1738 // Check for the MH_DYLDLINK bit in the flags
1739 if (m_header.flags & HeaderFlagBitIsDynamicLinkObject)
1740 return eStrataUser;
1741 return eStrataKernel;
1742
1743 case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB
1744 case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE
1745 case HeaderFileTypePreloadedExecutable: return eStrataUser; // 0x5u MH_PRELOAD
1746 case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB
1747 case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER
1748 case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE
1749 case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB
1750 case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM
1751 case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE
1752 default:
1753 break;
1754 }
1755 return eStrataUnknown;
1756}
1757
1758
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001759bool
Greg Clayton514487e2011-02-15 21:59:32 +00001760ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001761{
1762 lldb_private::Mutex::Locker locker(m_mutex);
Greg Claytone0d378b2011-03-24 21:19:54 +00001763 arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Greg Clayton514487e2011-02-15 21:59:32 +00001764 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001765}
1766
1767
1768//------------------------------------------------------------------
1769// PluginInterface protocol
1770//------------------------------------------------------------------
1771const char *
1772ObjectFileMachO::GetPluginName()
1773{
1774 return "ObjectFileMachO";
1775}
1776
1777const char *
1778ObjectFileMachO::GetShortPluginName()
1779{
1780 return GetPluginNameStatic();
1781}
1782
1783uint32_t
1784ObjectFileMachO::GetPluginVersion()
1785{
1786 return 1;
1787}
1788