blob: bfd6eb872a65c94c7784bee2751489ad8b828a3f [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ObjectFileMachO.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ObjectFileMachO.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "lldb/Core/ArchSpec.h"
13#include "lldb/Core/DataBuffer.h"
Greg Clayton5f54ac32011-02-08 05:05:52 +000014#include "lldb/Host/FileSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Core/FileSpecList.h"
16#include "lldb/Core/Module.h"
17#include "lldb/Core/PluginManager.h"
18#include "lldb/Core/Section.h"
19#include "lldb/Core/StreamFile.h"
20#include "lldb/Core/StreamString.h"
21#include "lldb/Core/Timer.h"
22#include "lldb/Core/UUID.h"
23#include "lldb/Symbol/ObjectFile.h"
24
Chris Lattner24943d22010-06-08 16:52:24 +000025
26using namespace lldb;
27using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000028using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000029
30
31void
32ObjectFileMachO::Initialize()
33{
34 PluginManager::RegisterPlugin (GetPluginNameStatic(),
35 GetPluginDescriptionStatic(),
36 CreateInstance);
37}
38
39void
40ObjectFileMachO::Terminate()
41{
42 PluginManager::UnregisterPlugin (CreateInstance);
43}
44
45
46const char *
47ObjectFileMachO::GetPluginNameStatic()
48{
49 return "object-file.mach-o";
50}
51
52const char *
53ObjectFileMachO::GetPluginDescriptionStatic()
54{
55 return "Mach-o object file reader (32 and 64 bit)";
56}
57
58
59ObjectFile *
60ObjectFileMachO::CreateInstance (Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length)
61{
62 if (ObjectFileMachO::MagicBytesMatch(dataSP))
63 {
64 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module, dataSP, file, offset, length));
65 if (objfile_ap.get() && objfile_ap->ParseHeader())
66 return objfile_ap.release();
67 }
68 return NULL;
69}
70
71
72static uint32_t
73MachHeaderSizeFromMagic(uint32_t magic)
74{
75 switch (magic)
76 {
Greg Clayton1674b122010-07-21 22:12:05 +000077 case HeaderMagic32:
78 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +000079 return sizeof(struct mach_header);
80
Greg Clayton1674b122010-07-21 22:12:05 +000081 case HeaderMagic64:
82 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +000083 return sizeof(struct mach_header_64);
84 break;
85
86 default:
87 break;
88 }
89 return 0;
90}
91
92
93bool
94ObjectFileMachO::MagicBytesMatch (DataBufferSP& dataSP)
95{
Greg Claytoncd548032011-02-01 01:31:41 +000096 DataExtractor data(dataSP, lldb::endian::InlHostByteOrder(), 4);
Chris Lattner24943d22010-06-08 16:52:24 +000097 uint32_t offset = 0;
98 uint32_t magic = data.GetU32(&offset);
99 return MachHeaderSizeFromMagic(magic) != 0;
100}
101
102
103ObjectFileMachO::ObjectFileMachO(Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) :
104 ObjectFile(module, file, offset, length, dataSP),
105 m_mutex (Mutex::eMutexTypeRecursive),
106 m_header(),
107 m_sections_ap(),
108 m_symtab_ap()
109{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000110 ::memset (&m_header, 0, sizeof(m_header));
111 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000112}
113
114
115ObjectFileMachO::~ObjectFileMachO()
116{
117}
118
119
120bool
121ObjectFileMachO::ParseHeader ()
122{
123 lldb_private::Mutex::Locker locker(m_mutex);
124 bool can_parse = false;
125 uint32_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000126 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +0000127 // Leave magic in the original byte order
128 m_header.magic = m_data.GetU32(&offset);
129 switch (m_header.magic)
130 {
Greg Clayton1674b122010-07-21 22:12:05 +0000131 case HeaderMagic32:
Greg Claytoncd548032011-02-01 01:31:41 +0000132 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +0000133 m_data.SetAddressByteSize(4);
134 can_parse = true;
135 break;
136
Greg Clayton1674b122010-07-21 22:12:05 +0000137 case HeaderMagic64:
Greg Claytoncd548032011-02-01 01:31:41 +0000138 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +0000139 m_data.SetAddressByteSize(8);
140 can_parse = true;
141 break;
142
Greg Clayton1674b122010-07-21 22:12:05 +0000143 case HeaderMagic32Swapped:
Greg Claytoncd548032011-02-01 01:31:41 +0000144 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Chris Lattner24943d22010-06-08 16:52:24 +0000145 m_data.SetAddressByteSize(4);
146 can_parse = true;
147 break;
148
Greg Clayton1674b122010-07-21 22:12:05 +0000149 case HeaderMagic64Swapped:
Greg Claytoncd548032011-02-01 01:31:41 +0000150 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Chris Lattner24943d22010-06-08 16:52:24 +0000151 m_data.SetAddressByteSize(8);
152 can_parse = true;
153 break;
154
155 default:
156 break;
157 }
158
159 if (can_parse)
160 {
161 m_data.GetU32(&offset, &m_header.cputype, 6);
162
Greg Claytoncf015052010-06-11 03:25:34 +0000163 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jim Ingham7508e732010-08-09 23:31:02 +0000164
165 if (SetModulesArchitecture (mach_arch))
Chris Lattner24943d22010-06-08 16:52:24 +0000166 {
167 // Read in all only the load command data
168 DataBufferSP data_sp(m_file.ReadFileContents(m_offset, m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic)));
169 m_data.SetData (data_sp);
170 return true;
171 }
172 }
173 else
174 {
175 memset(&m_header, 0, sizeof(struct mach_header));
176 }
177 return false;
178}
179
180
181ByteOrder
182ObjectFileMachO::GetByteOrder () const
183{
184 lldb_private::Mutex::Locker locker(m_mutex);
185 return m_data.GetByteOrder ();
186}
187
Jim Ingham7508e732010-08-09 23:31:02 +0000188bool
189ObjectFileMachO::IsExecutable() const
190{
191 return m_header.filetype == HeaderFileTypeExecutable;
192}
Chris Lattner24943d22010-06-08 16:52:24 +0000193
194size_t
195ObjectFileMachO::GetAddressByteSize () const
196{
197 lldb_private::Mutex::Locker locker(m_mutex);
198 return m_data.GetAddressByteSize ();
199}
200
201
202Symtab *
203ObjectFileMachO::GetSymtab()
204{
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000205 lldb_private::Mutex::Locker symfile_locker(m_mutex);
Chris Lattner24943d22010-06-08 16:52:24 +0000206 if (m_symtab_ap.get() == NULL)
207 {
208 m_symtab_ap.reset(new Symtab(this));
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000209 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton7c36fa02010-09-11 03:13:28 +0000210 ParseSymtab (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000211 }
212 return m_symtab_ap.get();
213}
214
215
216SectionList *
217ObjectFileMachO::GetSectionList()
218{
219 lldb_private::Mutex::Locker locker(m_mutex);
220 if (m_sections_ap.get() == NULL)
221 {
222 m_sections_ap.reset(new SectionList());
223 ParseSections();
224 }
225 return m_sections_ap.get();
226}
227
228
229size_t
230ObjectFileMachO::ParseSections ()
231{
232 lldb::user_id_t segID = 0;
233 lldb::user_id_t sectID = 0;
234 struct segment_command_64 load_cmd;
235 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
236 uint32_t i;
237 //bool dump_sections = false;
238 for (i=0; i<m_header.ncmds; ++i)
239 {
240 const uint32_t load_cmd_offset = offset;
241 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
242 break;
243
Greg Clayton1674b122010-07-21 22:12:05 +0000244 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000245 {
246 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
247 {
248 load_cmd.vmaddr = m_data.GetAddress(&offset);
249 load_cmd.vmsize = m_data.GetAddress(&offset);
250 load_cmd.fileoff = m_data.GetAddress(&offset);
251 load_cmd.filesize = m_data.GetAddress(&offset);
252 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
253 {
Greg Clayton68ca8232011-01-25 02:58:48 +0000254
255 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
256
Chris Lattner24943d22010-06-08 16:52:24 +0000257 // Keep a list of mach segments around in case we need to
258 // get at data that isn't stored in the abstracted Sections.
259 m_mach_segments.push_back (load_cmd);
260
261 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
262 // Use a segment ID of the segment index shifted left by 8 so they
263 // never conflict with any of the sections.
264 SectionSP segment_sp;
265 if (segment_name)
266 {
267 segment_sp.reset(new Section (NULL,
268 GetModule(), // Module to which this section belongs
269 ++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
270 segment_name, // Name of this section
271 eSectionTypeContainer, // This section is a container of other sections.
272 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
273 load_cmd.vmsize, // VM size in bytes of this section
274 load_cmd.fileoff, // Offset to the data for this section in the file
275 load_cmd.filesize, // Size in bytes of this section as found in the the file
276 load_cmd.flags)); // Flags for this section
277
Greg Clayton68ca8232011-01-25 02:58:48 +0000278 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000279 m_sections_ap->AddSection(segment_sp);
280 }
281
282 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000283 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000284 // Push a section into our mach sections for the section at
Greg Clayton6af4fad2010-10-06 01:26:32 +0000285 // index zero (NListSectionNoSection) if we don't have any
286 // mach sections yet...
287 if (m_mach_sections.empty())
288 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000289 uint32_t segment_sect_idx;
290 const lldb::user_id_t first_segment_sectID = sectID + 1;
291
292
Greg Clayton1674b122010-07-21 22:12:05 +0000293 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000294 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
295 {
296 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
297 break;
298 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
299 break;
300 sect64.addr = m_data.GetAddress(&offset);
301 sect64.size = m_data.GetAddress(&offset);
302
303 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
304 break;
305
306 // Keep a list of mach sections around in case we need to
307 // get at data that isn't stored in the abstracted Sections.
308 m_mach_sections.push_back (sect64);
309
310 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
311 if (!segment_name)
312 {
313 // We have a segment with no name so we need to conjure up
314 // segments that correspond to the section's segname if there
315 // isn't already such a section. If there is such a section,
316 // we resize the section so that it spans all sections.
317 // We also mark these sections as fake so address matches don't
318 // hit if they land in the gaps between the child sections.
319 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
320 segment_sp = m_sections_ap->FindSectionByName (segment_name);
321 if (segment_sp.get())
322 {
323 Section *segment = segment_sp.get();
324 // Grow the section size as needed.
325 const lldb::addr_t sect64_min_addr = sect64.addr;
326 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
327 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
328 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
329 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
330 if (sect64_min_addr >= curr_seg_min_addr)
331 {
332 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
333 // Only grow the section size if needed
334 if (new_seg_byte_size > curr_seg_byte_size)
335 segment->SetByteSize (new_seg_byte_size);
336 }
337 else
338 {
339 // We need to change the base address of the segment and
340 // adjust the child section offsets for all existing children.
341 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
342 segment->Slide(slide_amount, false);
343 segment->GetChildren().Slide (-slide_amount, false);
344 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
345 }
Greg Clayton661825b2010-06-28 23:51:11 +0000346
347 // Grow the section size as needed.
348 if (sect64.offset)
349 {
350 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
351 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
352
353 const lldb::addr_t section_min_file_offset = sect64.offset;
354 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
355 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
356 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
357 segment->SetFileOffset (new_file_offset);
358 segment->SetFileSize (new_file_size);
359 }
Chris Lattner24943d22010-06-08 16:52:24 +0000360 }
361 else
362 {
363 // Create a fake section for the section's named segment
364 segment_sp.reset(new Section(segment_sp.get(), // Parent section
365 GetModule(), // Module to which this section belongs
366 ++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
367 segment_name, // Name of this section
368 eSectionTypeContainer, // This section is a container of other sections.
369 sect64.addr, // File VM address == addresses as they are found in the object file
370 sect64.size, // VM size in bytes of this section
371 sect64.offset, // Offset to the data for this section in the file
372 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
373 load_cmd.flags)); // Flags for this section
374 segment_sp->SetIsFake(true);
375 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000376 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000377 }
378 }
379 assert (segment_sp.get());
380
Greg Clayton1674b122010-07-21 22:12:05 +0000381 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000382 static ConstString g_sect_name_objc_data ("__objc_data");
383 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
384 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
385 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
386 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
387 static ConstString g_sect_name_objc_const ("__objc_const");
388 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
389 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000390
391 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
392 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
393 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
394 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
395 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
396 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
397 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
398 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
399 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
400 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
401 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
402 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +0000403 static ConstString g_sect_name_DATA ("__DATA");
404 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000405
Chris Lattner24943d22010-06-08 16:52:24 +0000406 SectionType sect_type = eSectionTypeOther;
407
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000408 if (section_name == g_sect_name_dwarf_debug_abbrev)
409 sect_type = eSectionTypeDWARFDebugAbbrev;
410 else if (section_name == g_sect_name_dwarf_debug_aranges)
411 sect_type = eSectionTypeDWARFDebugAranges;
412 else if (section_name == g_sect_name_dwarf_debug_frame)
413 sect_type = eSectionTypeDWARFDebugFrame;
414 else if (section_name == g_sect_name_dwarf_debug_info)
415 sect_type = eSectionTypeDWARFDebugInfo;
416 else if (section_name == g_sect_name_dwarf_debug_line)
417 sect_type = eSectionTypeDWARFDebugLine;
418 else if (section_name == g_sect_name_dwarf_debug_loc)
419 sect_type = eSectionTypeDWARFDebugLoc;
420 else if (section_name == g_sect_name_dwarf_debug_macinfo)
421 sect_type = eSectionTypeDWARFDebugMacInfo;
422 else if (section_name == g_sect_name_dwarf_debug_pubnames)
423 sect_type = eSectionTypeDWARFDebugPubNames;
424 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
425 sect_type = eSectionTypeDWARFDebugPubTypes;
426 else if (section_name == g_sect_name_dwarf_debug_ranges)
427 sect_type = eSectionTypeDWARFDebugRanges;
428 else if (section_name == g_sect_name_dwarf_debug_str)
429 sect_type = eSectionTypeDWARFDebugStr;
430 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +0000431 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +0000432 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +0000433 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000434 else if (section_name == g_sect_name_eh_frame)
435 sect_type = eSectionTypeEHFrame;
436 else if (section_name == g_sect_name_cfstring)
437 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +0000438 else if (section_name == g_sect_name_objc_data ||
439 section_name == g_sect_name_objc_classrefs ||
440 section_name == g_sect_name_objc_superrefs ||
441 section_name == g_sect_name_objc_const ||
442 section_name == g_sect_name_objc_classlist)
443 {
444 sect_type = eSectionTypeDataPointers;
445 }
Chris Lattner24943d22010-06-08 16:52:24 +0000446
447 if (sect_type == eSectionTypeOther)
448 {
449 switch (mach_sect_type)
450 {
451 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +0000452 case SectionTypeRegular:
453 if (segment_sp->GetName() == g_sect_name_TEXT)
454 sect_type = eSectionTypeCode;
455 else if (segment_sp->GetName() == g_sect_name_DATA)
456 sect_type = eSectionTypeData;
457 else
458 sect_type = eSectionTypeOther;
459 break;
Greg Clayton1674b122010-07-21 22:12:05 +0000460 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
461 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
462 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
463 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
464 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
465 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
466 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
467 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
468 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
469 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
470 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
471 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
472 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
473 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
474 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
475 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +0000476 default: break;
477 }
478 }
479
480 SectionSP section_sp(new Section(segment_sp.get(),
481 GetModule(),
482 ++sectID,
483 section_name,
484 sect_type,
485 sect64.addr - segment_sp->GetFileAddress(),
486 sect64.size,
487 sect64.offset,
488 sect64.offset == 0 ? 0 : sect64.size,
489 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +0000490 // Set the section to be encrypted to match the segment
491 section_sp->SetIsEncrypted (segment_is_encrypted);
492
Chris Lattner24943d22010-06-08 16:52:24 +0000493 segment_sp->GetChildren().AddSection(section_sp);
494
495 if (segment_sp->IsFake())
496 {
497 segment_sp.reset();
498 segment_name.Clear();
499 }
500 }
Greg Clayton1674b122010-07-21 22:12:05 +0000501 if (m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +0000502 {
503 if (first_segment_sectID <= sectID)
504 {
505 lldb::user_id_t sect_uid;
506 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
507 {
508 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
509 SectionSP next_section_sp;
510 if (sect_uid + 1 <= sectID)
511 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
512
513 if (curr_section_sp.get())
514 {
515 if (curr_section_sp->GetByteSize() == 0)
516 {
517 if (next_section_sp.get() != NULL)
518 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
519 else
520 curr_section_sp->SetByteSize ( load_cmd.vmsize );
521 }
522 }
523 }
524 }
525 }
526 }
527 }
528 }
Greg Clayton1674b122010-07-21 22:12:05 +0000529 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +0000530 {
531 m_dysymtab.cmd = load_cmd.cmd;
532 m_dysymtab.cmdsize = load_cmd.cmdsize;
533 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
534 }
535
536 offset = load_cmd_offset + load_cmd.cmdsize;
537 }
538// if (dump_sections)
539// {
540// StreamFile s(stdout);
541// m_sections_ap->Dump(&s, true);
542// }
543 return sectID; // Return the number of sections we registered with the module
544}
545
546class MachSymtabSectionInfo
547{
548public:
549
550 MachSymtabSectionInfo (SectionList *section_list) :
551 m_section_list (section_list),
552 m_section_infos()
553 {
554 // Get the number of sections down to a depth of 1 to include
555 // all segments and their sections, but no other sections that
556 // may be added for debug map or
557 m_section_infos.resize(section_list->GetNumSections(1));
558 }
559
560
561 Section *
562 GetSection (uint8_t n_sect, addr_t file_addr)
563 {
564 if (n_sect == 0)
565 return NULL;
566 if (n_sect < m_section_infos.size())
567 {
568 if (m_section_infos[n_sect].section == NULL)
569 {
570 Section *section = m_section_list->FindSectionByID (n_sect).get();
571 m_section_infos[n_sect].section = section;
572 assert (section != NULL);
573 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
574 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
575 }
576 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
577 return m_section_infos[n_sect].section;
578 }
579 return m_section_list->FindSectionContainingFileAddress(file_addr).get();
580 }
581
582protected:
583 struct SectionInfo
584 {
585 SectionInfo () :
586 vm_range(),
587 section (NULL)
588 {
589 }
590
591 VMRange vm_range;
592 Section *section;
593 };
594 SectionList *m_section_list;
595 std::vector<SectionInfo> m_section_infos;
596};
597
598
599
600size_t
601ObjectFileMachO::ParseSymtab (bool minimize)
602{
603 Timer scoped_timer(__PRETTY_FUNCTION__,
604 "ObjectFileMachO::ParseSymtab () module = %s",
605 m_file.GetFilename().AsCString(""));
606 struct symtab_command symtab_load_command;
607 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
608 uint32_t i;
609 for (i=0; i<m_header.ncmds; ++i)
610 {
611 const uint32_t cmd_offset = offset;
612 // Read in the load command and load command size
613 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
614 break;
615 // Watch for the symbol table load command
Greg Clayton1674b122010-07-21 22:12:05 +0000616 if (symtab_load_command.cmd == LoadCommandSymtab)
Chris Lattner24943d22010-06-08 16:52:24 +0000617 {
618 // Read in the rest of the symtab load command
Jason Molendaccfba722010-07-06 22:38:03 +0000619 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
Chris Lattner24943d22010-06-08 16:52:24 +0000620 {
621 Symtab *symtab = m_symtab_ap.get();
622 SectionList *section_list = GetSectionList();
623 assert(section_list);
624 const size_t addr_size = m_data.GetAddressByteSize();
625 const ByteOrder endian = m_data.GetByteOrder();
626 bool bit_width_32 = addr_size == 4;
627 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
628
629 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size));
630 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize));
631
632 const char *strtab_data = (const char *)strtab_data_sp->GetBytes();
633// DataExtractor symtab_data(symtab_data_sp, endian, addr_size);
634// DataExtractor strtab_data(strtab_data_sp, endian, addr_size);
635
636 static ConstString g_segment_name_TEXT ("__TEXT");
637 static ConstString g_segment_name_DATA ("__DATA");
638 static ConstString g_segment_name_OBJC ("__OBJC");
639 static ConstString g_section_name_eh_frame ("__eh_frame");
640 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
641 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
642 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
643 SectionSP eh_frame_section_sp;
644 if (text_section_sp.get())
645 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
646 else
647 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
648
Greg Clayton1674b122010-07-21 22:12:05 +0000649 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Chris Lattner24943d22010-06-08 16:52:24 +0000650 //uint32_t symtab_offset = 0;
651 const uint8_t* nlist_data = symtab_data_sp->GetBytes();
652 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms);
653
654
Greg Claytoncd548032011-02-01 01:31:41 +0000655 if (endian != lldb::endian::InlHostByteOrder())
Chris Lattner24943d22010-06-08 16:52:24 +0000656 {
657 // ...
658 assert (!"UNIMPLEMENTED: Swap all nlist entries");
659 }
Greg Clayton7c36fa02010-09-11 03:13:28 +0000660 uint32_t N_SO_index = UINT32_MAX;
Chris Lattner24943d22010-06-08 16:52:24 +0000661
662 MachSymtabSectionInfo section_info (section_list);
663 std::vector<uint32_t> N_FUN_indexes;
664 std::vector<uint32_t> N_NSYM_indexes;
665 std::vector<uint32_t> N_INCL_indexes;
666 std::vector<uint32_t> N_BRAC_indexes;
667 std::vector<uint32_t> N_COMM_indexes;
Greg Clayton576a68b2010-09-08 16:38:06 +0000668 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Clayton637029b2010-09-12 05:25:16 +0000669 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Clayton576a68b2010-09-08 16:38:06 +0000670 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
671 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Clayton7c36fa02010-09-11 03:13:28 +0000672 // Any symbols that get merged into another will get an entry
673 // in this map so we know
Greg Clayton637029b2010-09-12 05:25:16 +0000674 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000675 uint32_t nlist_idx = 0;
676 Symbol *symbol_ptr = NULL;
677
678 uint32_t sym_idx = 0;
679 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
680 uint32_t num_syms = symtab->GetNumSymbols();
681
682 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
683 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
684 {
685 struct nlist_64 nlist;
686 if (bit_width_32)
687 {
688 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size));
Greg Clayton1674b122010-07-21 22:12:05 +0000689 nlist.n_strx = nlist32_ptr->n_strx;
Chris Lattner24943d22010-06-08 16:52:24 +0000690 nlist.n_type = nlist32_ptr->n_type;
691 nlist.n_sect = nlist32_ptr->n_sect;
692 nlist.n_desc = nlist32_ptr->n_desc;
693 nlist.n_value = nlist32_ptr->n_value;
694 }
695 else
696 {
697 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size)));
698 }
699
700 SymbolType type = eSymbolTypeInvalid;
Greg Clayton1674b122010-07-21 22:12:05 +0000701 const char* symbol_name = &strtab_data[nlist.n_strx];
Chris Lattner24943d22010-06-08 16:52:24 +0000702 if (symbol_name[0] == '\0')
703 symbol_name = NULL;
704 Section* symbol_section = NULL;
705 bool add_nlist = true;
Greg Clayton1674b122010-07-21 22:12:05 +0000706 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Chris Lattner24943d22010-06-08 16:52:24 +0000707
708 assert (sym_idx < num_syms);
709
710 sym[sym_idx].SetDebug (is_debug);
711
712 if (is_debug)
713 {
714 switch (nlist.n_type)
715 {
Greg Clayton1674b122010-07-21 22:12:05 +0000716 case StabGlobalSymbol:
717 // N_GSYM -- global symbol: name,,NO_SECT,type,0
Chris Lattner24943d22010-06-08 16:52:24 +0000718 // Sometimes the N_GSYM value contains the address.
Greg Clayton7c36fa02010-09-11 03:13:28 +0000719 sym[sym_idx].SetExternal(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000720 if (nlist.n_value != 0)
721 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton7c36fa02010-09-11 03:13:28 +0000722 type = eSymbolTypeData;
Chris Lattner24943d22010-06-08 16:52:24 +0000723 break;
724
Greg Clayton1674b122010-07-21 22:12:05 +0000725 case StabFunctionName:
726 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Clayton7c36fa02010-09-11 03:13:28 +0000727 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +0000728 break;
729
Greg Clayton1674b122010-07-21 22:12:05 +0000730 case StabFunction:
731 // N_FUN -- procedure: name,,n_sect,linenumber,address
Chris Lattner24943d22010-06-08 16:52:24 +0000732 if (symbol_name)
733 {
Greg Clayton7c36fa02010-09-11 03:13:28 +0000734 type = eSymbolTypeCode;
Chris Lattner24943d22010-06-08 16:52:24 +0000735 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton576a68b2010-09-08 16:38:06 +0000736
737 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000738 // We use the current number of symbols in the symbol table in lieu of
739 // using nlist_idx in case we ever start trimming entries out
740 N_FUN_indexes.push_back(sym_idx);
741 }
742 else
743 {
Greg Clayton7c36fa02010-09-11 03:13:28 +0000744 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +0000745
746 if ( !N_FUN_indexes.empty() )
747 {
748 // Copy the size of the function into the original STAB entry so we don't have
749 // to hunt for it later
750 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
751 N_FUN_indexes.pop_back();
Jason Molendaccfba722010-07-06 22:38:03 +0000752 // We don't really need the end function STAB as it contains the size which
Chris Lattner24943d22010-06-08 16:52:24 +0000753 // we already placed with the original symbol, so don't add it if we want a
754 // minimal symbol table
755 if (minimize)
756 add_nlist = false;
757 }
758 }
759 break;
760
Greg Clayton1674b122010-07-21 22:12:05 +0000761 case StabStaticSymbol:
762 // N_STSYM -- static symbol: name,,n_sect,type,address
Greg Clayton576a68b2010-09-08 16:38:06 +0000763 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000764 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton7c36fa02010-09-11 03:13:28 +0000765 type = eSymbolTypeData;
Chris Lattner24943d22010-06-08 16:52:24 +0000766 break;
767
Greg Clayton1674b122010-07-21 22:12:05 +0000768 case StabLocalCommon:
769 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
Chris Lattner24943d22010-06-08 16:52:24 +0000770 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
771 type = eSymbolTypeCommonBlock;
772 break;
773
Greg Clayton1674b122010-07-21 22:12:05 +0000774 case StabBeginSymbol:
775 // N_BNSYM
Chris Lattner24943d22010-06-08 16:52:24 +0000776 // We use the current number of symbols in the symbol table in lieu of
777 // using nlist_idx in case we ever start trimming entries out
778 if (minimize)
779 {
780 // Skip these if we want minimal symbol tables
781 add_nlist = false;
782 }
783 else
784 {
785 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
786 N_NSYM_indexes.push_back(sym_idx);
787 type = eSymbolTypeScopeBegin;
788 }
789 break;
790
Greg Clayton1674b122010-07-21 22:12:05 +0000791 case StabEndSymbol:
792 // N_ENSYM
Chris Lattner24943d22010-06-08 16:52:24 +0000793 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
794 // so that we can always skip the entire symbol if we need to navigate
795 // more quickly at the source level when parsing STABS
796 if (minimize)
797 {
798 // Skip these if we want minimal symbol tables
799 add_nlist = false;
800 }
801 else
802 {
803 if ( !N_NSYM_indexes.empty() )
804 {
805 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
806 symbol_ptr->SetByteSize(sym_idx + 1);
807 symbol_ptr->SetSizeIsSibling(true);
808 N_NSYM_indexes.pop_back();
809 }
810 type = eSymbolTypeScopeEnd;
811 }
812 break;
813
814
Greg Clayton1674b122010-07-21 22:12:05 +0000815 case StabSourceFileOptions:
816 // N_OPT - emitted with gcc2_compiled and in gcc source
Chris Lattner24943d22010-06-08 16:52:24 +0000817 type = eSymbolTypeCompiler;
818 break;
819
Greg Clayton1674b122010-07-21 22:12:05 +0000820 case StabRegisterSymbol:
821 // N_RSYM - register sym: name,,NO_SECT,type,register
Chris Lattner24943d22010-06-08 16:52:24 +0000822 type = eSymbolTypeVariable;
823 break;
824
Greg Clayton1674b122010-07-21 22:12:05 +0000825 case StabSourceLine:
826 // N_SLINE - src line: 0,,n_sect,linenumber,address
Chris Lattner24943d22010-06-08 16:52:24 +0000827 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
828 type = eSymbolTypeLineEntry;
829 break;
830
Greg Clayton1674b122010-07-21 22:12:05 +0000831 case StabStructureType:
832 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
Chris Lattner24943d22010-06-08 16:52:24 +0000833 type = eSymbolTypeVariableType;
834 break;
835
Greg Clayton1674b122010-07-21 22:12:05 +0000836 case StabSourceFileName:
837 // N_SO - source file name
Chris Lattner24943d22010-06-08 16:52:24 +0000838 type = eSymbolTypeSourceFile;
839 if (symbol_name == NULL)
840 {
Greg Clayton7c36fa02010-09-11 03:13:28 +0000841 if (minimize)
842 add_nlist = false;
843 if (N_SO_index != UINT32_MAX)
Chris Lattner24943d22010-06-08 16:52:24 +0000844 {
845 // Set the size of the N_SO to the terminating index of this N_SO
846 // so that we can always skip the entire N_SO if we need to navigate
847 // more quickly at the source level when parsing STABS
848 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton7c36fa02010-09-11 03:13:28 +0000849 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
Chris Lattner24943d22010-06-08 16:52:24 +0000850 symbol_ptr->SetSizeIsSibling(true);
851 }
852 N_NSYM_indexes.clear();
853 N_INCL_indexes.clear();
854 N_BRAC_indexes.clear();
855 N_COMM_indexes.clear();
856 N_FUN_indexes.clear();
Greg Clayton7c36fa02010-09-11 03:13:28 +0000857 N_SO_index = UINT32_MAX;
Chris Lattner24943d22010-06-08 16:52:24 +0000858 }
Greg Clayton7c36fa02010-09-11 03:13:28 +0000859 else
Chris Lattner24943d22010-06-08 16:52:24 +0000860 {
861 // We use the current number of symbols in the symbol table in lieu of
862 // using nlist_idx in case we ever start trimming entries out
Greg Clayton7c36fa02010-09-11 03:13:28 +0000863 if (symbol_name[0] == '/')
864 N_SO_index = sym_idx;
865 else if (minimize && (N_SO_index == sym_idx - 1))
866 {
867 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
868 if (so_path && so_path[0])
869 {
870 std::string full_so_path (so_path);
871 if (*full_so_path.rbegin() != '/')
872 full_so_path += '/';
873 full_so_path += symbol_name;
874 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
875 add_nlist = false;
Greg Clayton637029b2010-09-12 05:25:16 +0000876 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
Greg Clayton7c36fa02010-09-11 03:13:28 +0000877 }
878 }
Chris Lattner24943d22010-06-08 16:52:24 +0000879 }
Greg Clayton7c36fa02010-09-11 03:13:28 +0000880
Chris Lattner24943d22010-06-08 16:52:24 +0000881 break;
882
Greg Clayton1674b122010-07-21 22:12:05 +0000883 case StabObjectFileName:
884 // N_OSO - object file name: name,,0,0,st_mtime
Chris Lattner24943d22010-06-08 16:52:24 +0000885 type = eSymbolTypeObjectFile;
886 break;
887
Greg Clayton1674b122010-07-21 22:12:05 +0000888 case StabLocalSymbol:
889 // N_LSYM - local sym: name,,NO_SECT,type,offset
Chris Lattner24943d22010-06-08 16:52:24 +0000890 type = eSymbolTypeLocal;
891 break;
892
893 //----------------------------------------------------------------------
894 // INCL scopes
895 //----------------------------------------------------------------------
Greg Clayton1674b122010-07-21 22:12:05 +0000896 case StabBeginIncludeFileName:
897 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
Chris Lattner24943d22010-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_INCL_indexes.push_back(sym_idx);
901 type = eSymbolTypeScopeBegin;
902 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000903
Greg Clayton1674b122010-07-21 22:12:05 +0000904 case StabEndIncludeFile:
905 // N_EINCL - include file end: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +0000906 // Set the size of the N_BINCL to the terminating index of this N_EINCL
907 // so that we can always skip the entire symbol if we need to navigate
908 // more quickly at the source level when parsing STABS
909 if ( !N_INCL_indexes.empty() )
910 {
911 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
912 symbol_ptr->SetByteSize(sym_idx + 1);
913 symbol_ptr->SetSizeIsSibling(true);
914 N_INCL_indexes.pop_back();
915 }
916 type = eSymbolTypeScopeEnd;
917 break;
918
Greg Clayton1674b122010-07-21 22:12:05 +0000919 case StabIncludeFileName:
920 // N_SOL - #included file name: name,,n_sect,0,address
Chris Lattner24943d22010-06-08 16:52:24 +0000921 type = eSymbolTypeHeaderFile;
Greg Clayton0ad086f2010-09-07 17:36:17 +0000922
923 // We currently don't use the header files on darwin
924 if (minimize)
925 add_nlist = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000926 break;
927
Greg Clayton1674b122010-07-21 22:12:05 +0000928 case StabCompilerParameters:
929 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +0000930 type = eSymbolTypeCompiler;
931 break;
932
Greg Clayton1674b122010-07-21 22:12:05 +0000933 case StabCompilerVersion:
934 // N_VERSION - compiler version: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +0000935 type = eSymbolTypeCompiler;
936 break;
937
Greg Clayton1674b122010-07-21 22:12:05 +0000938 case StabCompilerOptLevel:
939 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +0000940 type = eSymbolTypeCompiler;
941 break;
942
Greg Clayton1674b122010-07-21 22:12:05 +0000943 case StabParameter:
944 // N_PSYM - parameter: name,,NO_SECT,type,offset
Chris Lattner24943d22010-06-08 16:52:24 +0000945 type = eSymbolTypeVariable;
946 break;
947
Greg Clayton1674b122010-07-21 22:12:05 +0000948 case StabAlternateEntry:
949 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
Chris Lattner24943d22010-06-08 16:52:24 +0000950 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
951 type = eSymbolTypeLineEntry;
952 break;
953
954 //----------------------------------------------------------------------
955 // Left and Right Braces
956 //----------------------------------------------------------------------
Greg Clayton1674b122010-07-21 22:12:05 +0000957 case StabLeftBracket:
958 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
Chris Lattner24943d22010-06-08 16:52:24 +0000959 // We use the current number of symbols in the symbol table in lieu of
960 // using nlist_idx in case we ever start trimming entries out
961 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
962 N_BRAC_indexes.push_back(sym_idx);
963 type = eSymbolTypeScopeBegin;
964 break;
965
Greg Clayton1674b122010-07-21 22:12:05 +0000966 case StabRightBracket:
967 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
Chris Lattner24943d22010-06-08 16:52:24 +0000968 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
969 // so that we can always skip the entire symbol if we need to navigate
970 // more quickly at the source level when parsing STABS
971 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
972 if ( !N_BRAC_indexes.empty() )
973 {
974 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
975 symbol_ptr->SetByteSize(sym_idx + 1);
976 symbol_ptr->SetSizeIsSibling(true);
977 N_BRAC_indexes.pop_back();
978 }
979 type = eSymbolTypeScopeEnd;
980 break;
981
Greg Clayton1674b122010-07-21 22:12:05 +0000982 case StabDeletedIncludeFile:
983 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
Chris Lattner24943d22010-06-08 16:52:24 +0000984 type = eSymbolTypeHeaderFile;
985 break;
986
987 //----------------------------------------------------------------------
988 // COMM scopes
989 //----------------------------------------------------------------------
Greg Clayton1674b122010-07-21 22:12:05 +0000990 case StabBeginCommon:
991 // N_BCOMM - begin common: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +0000992 // We use the current number of symbols in the symbol table in lieu of
993 // using nlist_idx in case we ever start trimming entries out
994 type = eSymbolTypeScopeBegin;
995 N_COMM_indexes.push_back(sym_idx);
996 break;
997
Greg Clayton1674b122010-07-21 22:12:05 +0000998 case StabEndCommonLocal:
999 // N_ECOML - end common (local name): 0,,n_sect,0,address
Chris Lattner24943d22010-06-08 16:52:24 +00001000 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1001 // Fall through
1002
Greg Clayton1674b122010-07-21 22:12:05 +00001003 case StabEndCommon:
1004 // N_ECOMM - end common: name,,n_sect,0,0
Chris Lattner24943d22010-06-08 16:52:24 +00001005 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1006 // so that we can always skip the entire symbol if we need to navigate
1007 // more quickly at the source level when parsing STABS
1008 if ( !N_COMM_indexes.empty() )
1009 {
1010 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1011 symbol_ptr->SetByteSize(sym_idx + 1);
1012 symbol_ptr->SetSizeIsSibling(true);
1013 N_COMM_indexes.pop_back();
1014 }
1015 type = eSymbolTypeScopeEnd;
1016 break;
1017
Greg Clayton1674b122010-07-21 22:12:05 +00001018 case StabLength:
1019 // N_LENG - second stab entry with length information
Chris Lattner24943d22010-06-08 16:52:24 +00001020 type = eSymbolTypeAdditional;
1021 break;
1022
1023 default: break;
1024 }
1025 }
1026 else
1027 {
Greg Clayton1674b122010-07-21 22:12:05 +00001028 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
1029 uint8_t n_type = NlistMaskType & nlist.n_type;
1030 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001031
1032 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name)
1033 {
1034 type = eSymbolTypeRuntime;
1035 }
1036 else
1037 {
1038 switch (n_type)
1039 {
Greg Clayton1674b122010-07-21 22:12:05 +00001040 case NListTypeIndirect: // N_INDR - Fall through
1041 case NListTypePreboundUndefined:// N_PBUD - Fall through
1042 case NListTypeUndefined: // N_UNDF
Chris Lattner24943d22010-06-08 16:52:24 +00001043 type = eSymbolTypeExtern;
1044 break;
1045
Greg Clayton1674b122010-07-21 22:12:05 +00001046 case NListTypeAbsolute: // N_ABS
Chris Lattner24943d22010-06-08 16:52:24 +00001047 type = eSymbolTypeAbsolute;
1048 break;
1049
Greg Clayton1674b122010-07-21 22:12:05 +00001050 case NListTypeSection: // N_SECT
Chris Lattner24943d22010-06-08 16:52:24 +00001051 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1052
1053 assert(symbol_section != NULL);
1054 if (TEXT_eh_frame_sectID == nlist.n_sect)
1055 {
1056 type = eSymbolTypeException;
1057 }
1058 else
1059 {
Greg Claytonf3d0b0c2010-10-27 03:32:59 +00001060 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +00001061
1062 switch (section_type)
1063 {
Greg Clayton1674b122010-07-21 22:12:05 +00001064 case SectionTypeRegular: break; // regular section
1065 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
1066 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
1067 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
1068 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
1069 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1070 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1071 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1072 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1073 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
1074 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
1075 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
1076 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
1077 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
1078 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
1079 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
1080 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001081 default: break;
1082 }
1083
1084 if (type == eSymbolTypeInvalid)
1085 {
1086 const char *symbol_sect_name = symbol_section->GetName().AsCString();
1087 if (symbol_section->IsDescendant (text_section_sp.get()))
1088 {
Greg Clayton1674b122010-07-21 22:12:05 +00001089 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1090 SectionAttrUserSelfModifyingCode |
1091 SectionAttrSytemSomeInstructions))
Chris Lattner24943d22010-06-08 16:52:24 +00001092 type = eSymbolTypeData;
1093 else
1094 type = eSymbolTypeCode;
1095 }
1096 else
1097 if (symbol_section->IsDescendant(data_section_sp.get()))
1098 {
1099 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1100 {
1101 type = eSymbolTypeRuntime;
1102 }
1103 else
1104 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1105 {
1106 type = eSymbolTypeException;
1107 }
1108 else
1109 {
1110 type = eSymbolTypeData;
1111 }
1112 }
1113 else
1114 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1115 {
1116 type = eSymbolTypeTrampoline;
1117 }
1118 else
1119 if (symbol_section->IsDescendant(objc_section_sp.get()))
1120 {
1121 type = eSymbolTypeRuntime;
1122 }
1123 }
1124 }
1125 break;
Greg Clayton576a68b2010-09-08 16:38:06 +00001126 }
Chris Lattner24943d22010-06-08 16:52:24 +00001127 }
1128 }
Chris Lattner24943d22010-06-08 16:52:24 +00001129 if (add_nlist)
1130 {
1131 bool symbol_name_is_mangled = false;
1132 if (symbol_name && symbol_name[0] == '_')
1133 {
1134 symbol_name_is_mangled = symbol_name[1] == '_';
1135 symbol_name++; // Skip the leading underscore
1136 }
1137 uint64_t symbol_value = nlist.n_value;
Greg Clayton576a68b2010-09-08 16:38:06 +00001138
1139 if (symbol_name)
1140 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
Greg Clayton7c36fa02010-09-11 03:13:28 +00001141 if (is_debug == false)
Greg Clayton576a68b2010-09-08 16:38:06 +00001142 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001143 if (type == eSymbolTypeCode)
Greg Clayton576a68b2010-09-08 16:38:06 +00001144 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001145 // See if we can find a N_FUN entry for any code symbols.
1146 // If we do find a match, and the name matches, then we
1147 // can merge the two into just the function symbol to avoid
1148 // duplicate entries in the symbol table
1149 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1150 if (pos != N_FUN_addr_to_sym_idx.end())
Greg Clayton576a68b2010-09-08 16:38:06 +00001151 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001152 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1153 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1154 {
Greg Clayton637029b2010-09-12 05:25:16 +00001155 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001156 // We just need the flags from the linker symbol, so put these flags
1157 // into the N_FUN flags to avoid duplicate symbols in the symbol table
1158 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1159 sym[sym_idx].Clear();
1160 continue;
1161 }
Greg Clayton576a68b2010-09-08 16:38:06 +00001162 }
1163 }
Greg Clayton7c36fa02010-09-11 03:13:28 +00001164 else if (type == eSymbolTypeData)
Greg Clayton576a68b2010-09-08 16:38:06 +00001165 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001166 // See if we can find a N_STSYM entry for any data symbols.
1167 // If we do find a match, and the name matches, then we
1168 // can merge the two into just the Static symbol to avoid
1169 // duplicate entries in the symbol table
1170 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
1171 if (pos != N_STSYM_addr_to_sym_idx.end())
Greg Clayton576a68b2010-09-08 16:38:06 +00001172 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001173 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1174 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1175 {
Greg Clayton637029b2010-09-12 05:25:16 +00001176 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001177 // We just need the flags from the linker symbol, so put these flags
1178 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
1179 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1180 sym[sym_idx].Clear();
1181 continue;
1182 }
Greg Clayton576a68b2010-09-08 16:38:06 +00001183 }
1184 }
1185 }
Chris Lattner24943d22010-06-08 16:52:24 +00001186 if (symbol_section != NULL)
1187 symbol_value -= symbol_section->GetFileAddress();
1188
1189 sym[sym_idx].SetID (nlist_idx);
1190 sym[sym_idx].SetType (type);
Chris Lattner24943d22010-06-08 16:52:24 +00001191 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1192 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1193 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1194
1195 ++sym_idx;
1196 }
1197 else
1198 {
1199 sym[sym_idx].Clear();
1200 }
1201
1202 }
1203
Chris Lattner24943d22010-06-08 16:52:24 +00001204 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1205 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1206 // such entries by figuring out what the address for the global is by looking up this non-STAB
1207 // entry and copying the value into the debug symbol's value to save us the hassle in the
1208 // debug symbol parser.
1209
1210 Symbol *global_symbol = NULL;
1211 for (nlist_idx = 0;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001212 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +00001213 nlist_idx++)
1214 {
1215 if (global_symbol->GetValue().GetFileAddress() == 0)
1216 {
1217 std::vector<uint32_t> indexes;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001218 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001219 {
1220 std::vector<uint32_t>::const_iterator pos;
1221 std::vector<uint32_t>::const_iterator end = indexes.end();
1222 for (pos = indexes.begin(); pos != end; ++pos)
1223 {
1224 symbol_ptr = symtab->SymbolAtIndex(*pos);
1225 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1226 {
1227 global_symbol->SetValue(symbol_ptr->GetValue());
1228 break;
1229 }
1230 }
1231 }
1232 }
1233 }
Greg Clayton637029b2010-09-12 05:25:16 +00001234
1235 // Trim our symbols down to just what we ended up with after
1236 // removing any symbols.
1237 if (sym_idx < num_syms)
1238 {
1239 num_syms = sym_idx;
1240 sym = symtab->Resize (num_syms);
1241 }
1242
Chris Lattner24943d22010-06-08 16:52:24 +00001243 // Now synthesize indirect symbols
1244 if (m_dysymtab.nindirectsyms != 0)
1245 {
1246 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4));
1247
1248 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize())
1249 {
Greg Clayton637029b2010-09-12 05:25:16 +00001250 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
Chris Lattner24943d22010-06-08 16:52:24 +00001251 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1252
1253 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1254 {
Greg Clayton1674b122010-07-21 22:12:05 +00001255 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
Chris Lattner24943d22010-06-08 16:52:24 +00001256 {
1257 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1258 if (symbol_stub_byte_size == 0)
1259 continue;
1260
1261 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1262
1263 if (num_symbol_stubs == 0)
1264 continue;
1265
1266 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
Greg Clayton637029b2010-09-12 05:25:16 +00001267 uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms;
Chris Lattner24943d22010-06-08 16:52:24 +00001268 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1269 {
1270 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1271 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1272 uint32_t symbol_stub_offset = symbol_stub_index * 4;
1273 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1274 {
Greg Clayton637029b2010-09-12 05:25:16 +00001275 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Greg Clayton6af4fad2010-10-06 01:26:32 +00001276 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
1277 continue;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001278
Greg Clayton637029b2010-09-12 05:25:16 +00001279 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
1280 Symbol *stub_symbol = NULL;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001281 if (index_pos != end_index_pos)
Greg Clayton637029b2010-09-12 05:25:16 +00001282 {
1283 // We have a remapping from the original nlist index to
1284 // a current symbol index, so just look this up by index
1285 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
1286 }
1287 else
1288 {
1289 // We need to lookup a symbol using the original nlist
1290 // symbol index since this index is coming from the
1291 // S_SYMBOL_STUBS
1292 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
1293 }
Greg Clayton0ad086f2010-09-07 17:36:17 +00001294
1295 assert (stub_symbol);
Chris Lattner24943d22010-06-08 16:52:24 +00001296 if (stub_symbol)
1297 {
1298 Address so_addr(symbol_stub_addr, section_list);
1299
1300 if (stub_symbol->GetType() == eSymbolTypeExtern)
1301 {
1302 // Change the external symbol into a trampoline that makes sense
1303 // These symbols were N_UNDF N_EXT, and are useless to us, so we
1304 // can re-use them so we don't have to make up a synthetic symbol
1305 // for no good reason.
1306 stub_symbol->SetType (eSymbolTypeTrampoline);
1307 stub_symbol->SetExternal (false);
1308 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1309 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1310 }
1311 else
1312 {
1313 // Make a synthetic symbol to describe the trampoline stub
1314 if (sym_idx >= num_syms)
Greg Clayton637029b2010-09-12 05:25:16 +00001315 sym = symtab->Resize (++num_syms);
1316 sym[sym_idx].SetID (synthetic_stub_sym_id++);
Chris Lattner24943d22010-06-08 16:52:24 +00001317 sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1318 sym[sym_idx].SetType (eSymbolTypeTrampoline);
1319 sym[sym_idx].SetIsSynthetic (true);
1320 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1321 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1322 ++sym_idx;
1323 }
1324 }
1325 }
1326 }
1327 }
1328 }
1329 }
1330 }
1331
Chris Lattner24943d22010-06-08 16:52:24 +00001332 return symtab->GetNumSymbols();
1333 }
1334 }
1335 offset = cmd_offset + symtab_load_command.cmdsize;
1336 }
1337 return 0;
1338}
1339
1340
1341void
1342ObjectFileMachO::Dump (Stream *s)
1343{
1344 lldb_private::Mutex::Locker locker(m_mutex);
1345 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1346 s->Indent();
Greg Clayton1674b122010-07-21 22:12:05 +00001347 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
Chris Lattner24943d22010-06-08 16:52:24 +00001348 s->PutCString("ObjectFileMachO64");
1349 else
1350 s->PutCString("ObjectFileMachO32");
1351
Greg Claytoncf015052010-06-11 03:25:34 +00001352 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00001353
1354 *s << ", file = '" << m_file << "', arch = " << header_arch.AsCString() << "\n";
1355
1356 if (m_sections_ap.get())
Greg Clayton58e844b2010-12-08 05:08:21 +00001357 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00001358
1359 if (m_symtab_ap.get())
Greg Clayton8d3802d2010-10-08 04:20:14 +00001360 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
Chris Lattner24943d22010-06-08 16:52:24 +00001361}
1362
1363
1364bool
Greg Clayton0467c782011-02-04 18:53:10 +00001365ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00001366{
1367 lldb_private::Mutex::Locker locker(m_mutex);
1368 struct uuid_command load_cmd;
1369 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1370 uint32_t i;
1371 for (i=0; i<m_header.ncmds; ++i)
1372 {
1373 const uint32_t cmd_offset = offset;
1374 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1375 break;
1376
Greg Clayton1674b122010-07-21 22:12:05 +00001377 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00001378 {
1379 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1380 if (uuid_bytes)
1381 {
1382 uuid->SetBytes (uuid_bytes);
1383 return true;
1384 }
1385 return false;
1386 }
1387 offset = cmd_offset + load_cmd.cmdsize;
1388 }
1389 return false;
1390}
1391
1392
1393uint32_t
1394ObjectFileMachO::GetDependentModules (FileSpecList& files)
1395{
1396 lldb_private::Mutex::Locker locker(m_mutex);
1397 struct load_command load_cmd;
1398 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1399 uint32_t count = 0;
1400 uint32_t i;
1401 for (i=0; i<m_header.ncmds; ++i)
1402 {
1403 const uint32_t cmd_offset = offset;
1404 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1405 break;
1406
1407 switch (load_cmd.cmd)
1408 {
Greg Clayton1674b122010-07-21 22:12:05 +00001409 case LoadCommandDylibLoad:
1410 case LoadCommandDylibLoadWeak:
1411 case LoadCommandDylibReexport:
1412 case LoadCommandDynamicLinkerLoad:
1413 case LoadCommandFixedVMShlibLoad:
Greg Clayton08a73202010-10-09 00:48:53 +00001414 case LoadCommandDylibLoadUpward:
Chris Lattner24943d22010-06-08 16:52:24 +00001415 {
1416 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1417 const char *path = m_data.PeekCStr(name_offset);
1418 // Skip any path that starts with '@' since these are usually:
1419 // @executable_path/.../file
1420 // @rpath/.../file
1421 if (path && path[0] != '@')
1422 {
Greg Clayton537a7a82010-10-20 20:54:39 +00001423 FileSpec file_spec(path, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001424 if (files.AppendIfUnique(file_spec))
1425 count++;
1426 }
1427 }
1428 break;
1429
1430 default:
1431 break;
1432 }
1433 offset = cmd_offset + load_cmd.cmdsize;
1434 }
1435 return count;
1436}
1437
1438bool
1439ObjectFileMachO::GetTargetTriple (ConstString &target_triple)
1440{
1441 lldb_private::Mutex::Locker locker(m_mutex);
1442 std::string triple(GetModule()->GetArchitecture().AsCString());
1443 triple += "-apple-darwin";
1444 target_triple.SetCString(triple.c_str());
1445 if (target_triple)
1446 return true;
1447 return false;
1448}
1449
1450
1451//------------------------------------------------------------------
1452// PluginInterface protocol
1453//------------------------------------------------------------------
1454const char *
1455ObjectFileMachO::GetPluginName()
1456{
1457 return "ObjectFileMachO";
1458}
1459
1460const char *
1461ObjectFileMachO::GetShortPluginName()
1462{
1463 return GetPluginNameStatic();
1464}
1465
1466uint32_t
1467ObjectFileMachO::GetPluginVersion()
1468{
1469 return 1;
1470}
1471
1472void
1473ObjectFileMachO::GetPluginCommandHelp (const char *command, Stream *strm)
1474{
1475}
1476
1477Error
1478ObjectFileMachO::ExecutePluginCommand (Args &command, Stream *strm)
1479{
1480 Error error;
1481 error.SetErrorString("No plug-in command are currently supported.");
1482 return error;
1483}
1484
1485Log *
1486ObjectFileMachO::EnablePluginLogging (Stream *strm, Args &command)
1487{
1488 return NULL;
1489}
1490
1491
1492
1493