blob: fa88b184a311c76fdb329b1785cffa0f5388397c [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
Jim Ingham28775942011-03-07 23:44:08 +000010#include "llvm/Support/MachO.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "ObjectFileMachO.h"
13
Chris Lattner24943d22010-06-08 16:52:24 +000014#include "lldb/Core/ArchSpec.h"
15#include "lldb/Core/DataBuffer.h"
Greg Clayton5f54ac32011-02-08 05:05:52 +000016#include "lldb/Host/FileSpec.h"
Chris Lattner24943d22010-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 Lattner24943d22010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000030using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000031
32
33void
34ObjectFileMachO::Initialize()
35{
36 PluginManager::RegisterPlugin (GetPluginNameStatic(),
37 GetPluginDescriptionStatic(),
38 CreateInstance);
39}
40
41void
42ObjectFileMachO::Terminate()
43{
44 PluginManager::UnregisterPlugin (CreateInstance);
45}
46
47
48const char *
49ObjectFileMachO::GetPluginNameStatic()
50{
51 return "object-file.mach-o";
52}
53
54const char *
55ObjectFileMachO::GetPluginDescriptionStatic()
56{
57 return "Mach-o object file reader (32 and 64 bit)";
58}
59
60
61ObjectFile *
62ObjectFileMachO::CreateInstance (Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length)
63{
64 if (ObjectFileMachO::MagicBytesMatch(dataSP))
65 {
66 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module, dataSP, file, offset, length));
67 if (objfile_ap.get() && objfile_ap->ParseHeader())
68 return objfile_ap.release();
69 }
70 return NULL;
71}
72
73
74static uint32_t
75MachHeaderSizeFromMagic(uint32_t magic)
76{
77 switch (magic)
78 {
Greg Clayton1674b122010-07-21 22:12:05 +000079 case HeaderMagic32:
80 case HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +000081 return sizeof(struct mach_header);
82
Greg Clayton1674b122010-07-21 22:12:05 +000083 case HeaderMagic64:
84 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +000085 return sizeof(struct mach_header_64);
86 break;
87
88 default:
89 break;
90 }
91 return 0;
92}
93
94
95bool
96ObjectFileMachO::MagicBytesMatch (DataBufferSP& dataSP)
97{
Greg Claytoncd548032011-02-01 01:31:41 +000098 DataExtractor data(dataSP, lldb::endian::InlHostByteOrder(), 4);
Chris Lattner24943d22010-06-08 16:52:24 +000099 uint32_t offset = 0;
100 uint32_t magic = data.GetU32(&offset);
101 return MachHeaderSizeFromMagic(magic) != 0;
102}
103
104
105ObjectFileMachO::ObjectFileMachO(Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) :
106 ObjectFile(module, file, offset, length, dataSP),
107 m_mutex (Mutex::eMutexTypeRecursive),
108 m_header(),
109 m_sections_ap(),
Jim Ingham28775942011-03-07 23:44:08 +0000110 m_symtab_ap(),
111 m_entry_point_address ()
Chris Lattner24943d22010-06-08 16:52:24 +0000112{
Greg Claytonddff7cc2011-02-04 21:13:05 +0000113 ::memset (&m_header, 0, sizeof(m_header));
114 ::memset (&m_dysymtab, 0, sizeof(m_dysymtab));
Chris Lattner24943d22010-06-08 16:52:24 +0000115}
116
117
118ObjectFileMachO::~ObjectFileMachO()
119{
120}
121
122
123bool
124ObjectFileMachO::ParseHeader ()
125{
126 lldb_private::Mutex::Locker locker(m_mutex);
127 bool can_parse = false;
128 uint32_t offset = 0;
Greg Claytoncd548032011-02-01 01:31:41 +0000129 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +0000130 // Leave magic in the original byte order
131 m_header.magic = m_data.GetU32(&offset);
132 switch (m_header.magic)
133 {
Greg Clayton1674b122010-07-21 22:12:05 +0000134 case HeaderMagic32:
Greg Claytoncd548032011-02-01 01:31:41 +0000135 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +0000136 m_data.SetAddressByteSize(4);
137 can_parse = true;
138 break;
139
Greg Clayton1674b122010-07-21 22:12:05 +0000140 case HeaderMagic64:
Greg Claytoncd548032011-02-01 01:31:41 +0000141 m_data.SetByteOrder (lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +0000142 m_data.SetAddressByteSize(8);
143 can_parse = true;
144 break;
145
Greg Clayton1674b122010-07-21 22:12:05 +0000146 case HeaderMagic32Swapped:
Greg Claytoncd548032011-02-01 01:31:41 +0000147 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Chris Lattner24943d22010-06-08 16:52:24 +0000148 m_data.SetAddressByteSize(4);
149 can_parse = true;
150 break;
151
Greg Clayton1674b122010-07-21 22:12:05 +0000152 case HeaderMagic64Swapped:
Greg Claytoncd548032011-02-01 01:31:41 +0000153 m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
Chris Lattner24943d22010-06-08 16:52:24 +0000154 m_data.SetAddressByteSize(8);
155 can_parse = true;
156 break;
157
158 default:
159 break;
160 }
161
162 if (can_parse)
163 {
164 m_data.GetU32(&offset, &m_header.cputype, 6);
165
Greg Claytoncf015052010-06-11 03:25:34 +0000166 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Jim Ingham7508e732010-08-09 23:31:02 +0000167
168 if (SetModulesArchitecture (mach_arch))
Chris Lattner24943d22010-06-08 16:52:24 +0000169 {
170 // Read in all only the load command data
171 DataBufferSP data_sp(m_file.ReadFileContents(m_offset, m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic)));
172 m_data.SetData (data_sp);
173 return true;
174 }
175 }
176 else
177 {
178 memset(&m_header, 0, sizeof(struct mach_header));
179 }
180 return false;
181}
182
183
184ByteOrder
185ObjectFileMachO::GetByteOrder () const
186{
187 lldb_private::Mutex::Locker locker(m_mutex);
188 return m_data.GetByteOrder ();
189}
190
Jim Ingham7508e732010-08-09 23:31:02 +0000191bool
192ObjectFileMachO::IsExecutable() const
193{
194 return m_header.filetype == HeaderFileTypeExecutable;
195}
Chris Lattner24943d22010-06-08 16:52:24 +0000196
197size_t
198ObjectFileMachO::GetAddressByteSize () const
199{
200 lldb_private::Mutex::Locker locker(m_mutex);
201 return m_data.GetAddressByteSize ();
202}
203
204
205Symtab *
206ObjectFileMachO::GetSymtab()
207{
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000208 lldb_private::Mutex::Locker symfile_locker(m_mutex);
Chris Lattner24943d22010-06-08 16:52:24 +0000209 if (m_symtab_ap.get() == NULL)
210 {
211 m_symtab_ap.reset(new Symtab(this));
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000212 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton7c36fa02010-09-11 03:13:28 +0000213 ParseSymtab (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000214 }
215 return m_symtab_ap.get();
216}
217
218
219SectionList *
220ObjectFileMachO::GetSectionList()
221{
222 lldb_private::Mutex::Locker locker(m_mutex);
223 if (m_sections_ap.get() == NULL)
224 {
225 m_sections_ap.reset(new SectionList());
226 ParseSections();
227 }
228 return m_sections_ap.get();
229}
230
231
232size_t
233ObjectFileMachO::ParseSections ()
234{
235 lldb::user_id_t segID = 0;
236 lldb::user_id_t sectID = 0;
237 struct segment_command_64 load_cmd;
238 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
239 uint32_t i;
240 //bool dump_sections = false;
241 for (i=0; i<m_header.ncmds; ++i)
242 {
243 const uint32_t load_cmd_offset = offset;
244 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
245 break;
246
Greg Clayton1674b122010-07-21 22:12:05 +0000247 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner24943d22010-06-08 16:52:24 +0000248 {
249 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
250 {
251 load_cmd.vmaddr = m_data.GetAddress(&offset);
252 load_cmd.vmsize = m_data.GetAddress(&offset);
253 load_cmd.fileoff = m_data.GetAddress(&offset);
254 load_cmd.filesize = m_data.GetAddress(&offset);
255 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
256 {
Greg Clayton68ca8232011-01-25 02:58:48 +0000257
258 const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0;
259
Chris Lattner24943d22010-06-08 16:52:24 +0000260 // Keep a list of mach segments around in case we need to
261 // get at data that isn't stored in the abstracted Sections.
262 m_mach_segments.push_back (load_cmd);
263
264 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
265 // Use a segment ID of the segment index shifted left by 8 so they
266 // never conflict with any of the sections.
267 SectionSP segment_sp;
268 if (segment_name)
269 {
270 segment_sp.reset(new Section (NULL,
271 GetModule(), // Module to which this section belongs
272 ++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
273 segment_name, // Name of this section
274 eSectionTypeContainer, // This section is a container of other sections.
275 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
276 load_cmd.vmsize, // VM size in bytes of this section
277 load_cmd.fileoff, // Offset to the data for this section in the file
278 load_cmd.filesize, // Size in bytes of this section as found in the the file
279 load_cmd.flags)); // Flags for this section
280
Greg Clayton68ca8232011-01-25 02:58:48 +0000281 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000282 m_sections_ap->AddSection(segment_sp);
283 }
284
285 struct section_64 sect64;
Greg Claytonddff7cc2011-02-04 21:13:05 +0000286 ::memset (&sect64, 0, sizeof(sect64));
Chris Lattner24943d22010-06-08 16:52:24 +0000287 // Push a section into our mach sections for the section at
Greg Clayton6af4fad2010-10-06 01:26:32 +0000288 // index zero (NListSectionNoSection) if we don't have any
289 // mach sections yet...
290 if (m_mach_sections.empty())
291 m_mach_sections.push_back(sect64);
Chris Lattner24943d22010-06-08 16:52:24 +0000292 uint32_t segment_sect_idx;
293 const lldb::user_id_t first_segment_sectID = sectID + 1;
294
295
Greg Clayton1674b122010-07-21 22:12:05 +0000296 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000297 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
298 {
299 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
300 break;
301 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
302 break;
303 sect64.addr = m_data.GetAddress(&offset);
304 sect64.size = m_data.GetAddress(&offset);
305
306 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
307 break;
308
309 // Keep a list of mach sections around in case we need to
310 // get at data that isn't stored in the abstracted Sections.
311 m_mach_sections.push_back (sect64);
312
313 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
314 if (!segment_name)
315 {
316 // We have a segment with no name so we need to conjure up
317 // segments that correspond to the section's segname if there
318 // isn't already such a section. If there is such a section,
319 // we resize the section so that it spans all sections.
320 // We also mark these sections as fake so address matches don't
321 // hit if they land in the gaps between the child sections.
322 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
323 segment_sp = m_sections_ap->FindSectionByName (segment_name);
324 if (segment_sp.get())
325 {
326 Section *segment = segment_sp.get();
327 // Grow the section size as needed.
328 const lldb::addr_t sect64_min_addr = sect64.addr;
329 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
330 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
331 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
332 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
333 if (sect64_min_addr >= curr_seg_min_addr)
334 {
335 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
336 // Only grow the section size if needed
337 if (new_seg_byte_size > curr_seg_byte_size)
338 segment->SetByteSize (new_seg_byte_size);
339 }
340 else
341 {
342 // We need to change the base address of the segment and
343 // adjust the child section offsets for all existing children.
344 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
345 segment->Slide(slide_amount, false);
346 segment->GetChildren().Slide (-slide_amount, false);
347 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
348 }
Greg Clayton661825b2010-06-28 23:51:11 +0000349
350 // Grow the section size as needed.
351 if (sect64.offset)
352 {
353 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
354 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
355
356 const lldb::addr_t section_min_file_offset = sect64.offset;
357 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
358 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
359 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
360 segment->SetFileOffset (new_file_offset);
361 segment->SetFileSize (new_file_size);
362 }
Chris Lattner24943d22010-06-08 16:52:24 +0000363 }
364 else
365 {
366 // Create a fake section for the section's named segment
367 segment_sp.reset(new Section(segment_sp.get(), // Parent section
368 GetModule(), // Module to which this section belongs
369 ++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
370 segment_name, // Name of this section
371 eSectionTypeContainer, // This section is a container of other sections.
372 sect64.addr, // File VM address == addresses as they are found in the object file
373 sect64.size, // VM size in bytes of this section
374 sect64.offset, // Offset to the data for this section in the file
375 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
376 load_cmd.flags)); // Flags for this section
377 segment_sp->SetIsFake(true);
378 m_sections_ap->AddSection(segment_sp);
Greg Clayton68ca8232011-01-25 02:58:48 +0000379 segment_sp->SetIsEncrypted (segment_is_encrypted);
Chris Lattner24943d22010-06-08 16:52:24 +0000380 }
381 }
382 assert (segment_sp.get());
383
Greg Clayton1674b122010-07-21 22:12:05 +0000384 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +0000385 static ConstString g_sect_name_objc_data ("__objc_data");
386 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
387 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
388 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
389 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
390 static ConstString g_sect_name_objc_const ("__objc_const");
391 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
392 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000393
394 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
395 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
396 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
397 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
398 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
399 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
400 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
401 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
402 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
403 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
404 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
405 static ConstString g_sect_name_eh_frame ("__eh_frame");
Greg Clayton3fed8b92010-10-08 00:21:05 +0000406 static ConstString g_sect_name_DATA ("__DATA");
407 static ConstString g_sect_name_TEXT ("__TEXT");
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000408
Chris Lattner24943d22010-06-08 16:52:24 +0000409 SectionType sect_type = eSectionTypeOther;
410
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000411 if (section_name == g_sect_name_dwarf_debug_abbrev)
412 sect_type = eSectionTypeDWARFDebugAbbrev;
413 else if (section_name == g_sect_name_dwarf_debug_aranges)
414 sect_type = eSectionTypeDWARFDebugAranges;
415 else if (section_name == g_sect_name_dwarf_debug_frame)
416 sect_type = eSectionTypeDWARFDebugFrame;
417 else if (section_name == g_sect_name_dwarf_debug_info)
418 sect_type = eSectionTypeDWARFDebugInfo;
419 else if (section_name == g_sect_name_dwarf_debug_line)
420 sect_type = eSectionTypeDWARFDebugLine;
421 else if (section_name == g_sect_name_dwarf_debug_loc)
422 sect_type = eSectionTypeDWARFDebugLoc;
423 else if (section_name == g_sect_name_dwarf_debug_macinfo)
424 sect_type = eSectionTypeDWARFDebugMacInfo;
425 else if (section_name == g_sect_name_dwarf_debug_pubnames)
426 sect_type = eSectionTypeDWARFDebugPubNames;
427 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
428 sect_type = eSectionTypeDWARFDebugPubTypes;
429 else if (section_name == g_sect_name_dwarf_debug_ranges)
430 sect_type = eSectionTypeDWARFDebugRanges;
431 else if (section_name == g_sect_name_dwarf_debug_str)
432 sect_type = eSectionTypeDWARFDebugStr;
433 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner24943d22010-06-08 16:52:24 +0000434 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner24943d22010-06-08 16:52:24 +0000435 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner24943d22010-06-08 16:52:24 +0000436 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000437 else if (section_name == g_sect_name_eh_frame)
438 sect_type = eSectionTypeEHFrame;
439 else if (section_name == g_sect_name_cfstring)
440 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner24943d22010-06-08 16:52:24 +0000441 else if (section_name == g_sect_name_objc_data ||
442 section_name == g_sect_name_objc_classrefs ||
443 section_name == g_sect_name_objc_superrefs ||
444 section_name == g_sect_name_objc_const ||
445 section_name == g_sect_name_objc_classlist)
446 {
447 sect_type = eSectionTypeDataPointers;
448 }
Chris Lattner24943d22010-06-08 16:52:24 +0000449
450 if (sect_type == eSectionTypeOther)
451 {
452 switch (mach_sect_type)
453 {
454 // TODO: categorize sections by other flags for regular sections
Greg Clayton3fed8b92010-10-08 00:21:05 +0000455 case SectionTypeRegular:
456 if (segment_sp->GetName() == g_sect_name_TEXT)
457 sect_type = eSectionTypeCode;
458 else if (segment_sp->GetName() == g_sect_name_DATA)
459 sect_type = eSectionTypeData;
460 else
461 sect_type = eSectionTypeOther;
462 break;
Greg Clayton1674b122010-07-21 22:12:05 +0000463 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
464 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
465 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
466 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
467 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
468 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
469 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
470 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
471 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
472 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
473 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
474 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
475 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
476 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
477 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
478 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner24943d22010-06-08 16:52:24 +0000479 default: break;
480 }
481 }
482
483 SectionSP section_sp(new Section(segment_sp.get(),
484 GetModule(),
485 ++sectID,
486 section_name,
487 sect_type,
488 sect64.addr - segment_sp->GetFileAddress(),
489 sect64.size,
490 sect64.offset,
491 sect64.offset == 0 ? 0 : sect64.size,
492 sect64.flags));
Greg Clayton68ca8232011-01-25 02:58:48 +0000493 // Set the section to be encrypted to match the segment
494 section_sp->SetIsEncrypted (segment_is_encrypted);
495
Chris Lattner24943d22010-06-08 16:52:24 +0000496 segment_sp->GetChildren().AddSection(section_sp);
497
498 if (segment_sp->IsFake())
499 {
500 segment_sp.reset();
501 segment_name.Clear();
502 }
503 }
Greg Clayton1674b122010-07-21 22:12:05 +0000504 if (m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner24943d22010-06-08 16:52:24 +0000505 {
506 if (first_segment_sectID <= sectID)
507 {
508 lldb::user_id_t sect_uid;
509 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
510 {
511 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
512 SectionSP next_section_sp;
513 if (sect_uid + 1 <= sectID)
514 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
515
516 if (curr_section_sp.get())
517 {
518 if (curr_section_sp->GetByteSize() == 0)
519 {
520 if (next_section_sp.get() != NULL)
521 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
522 else
523 curr_section_sp->SetByteSize ( load_cmd.vmsize );
524 }
525 }
526 }
527 }
528 }
529 }
530 }
531 }
Greg Clayton1674b122010-07-21 22:12:05 +0000532 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner24943d22010-06-08 16:52:24 +0000533 {
534 m_dysymtab.cmd = load_cmd.cmd;
535 m_dysymtab.cmdsize = load_cmd.cmdsize;
536 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
537 }
538
539 offset = load_cmd_offset + load_cmd.cmdsize;
540 }
541// if (dump_sections)
542// {
543// StreamFile s(stdout);
544// m_sections_ap->Dump(&s, true);
545// }
546 return sectID; // Return the number of sections we registered with the module
547}
548
549class MachSymtabSectionInfo
550{
551public:
552
553 MachSymtabSectionInfo (SectionList *section_list) :
554 m_section_list (section_list),
555 m_section_infos()
556 {
557 // Get the number of sections down to a depth of 1 to include
558 // all segments and their sections, but no other sections that
559 // may be added for debug map or
560 m_section_infos.resize(section_list->GetNumSections(1));
561 }
562
563
564 Section *
565 GetSection (uint8_t n_sect, addr_t file_addr)
566 {
567 if (n_sect == 0)
568 return NULL;
569 if (n_sect < m_section_infos.size())
570 {
571 if (m_section_infos[n_sect].section == NULL)
572 {
573 Section *section = m_section_list->FindSectionByID (n_sect).get();
574 m_section_infos[n_sect].section = section;
575 assert (section != NULL);
576 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
577 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
578 }
579 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
580 return m_section_infos[n_sect].section;
581 }
582 return m_section_list->FindSectionContainingFileAddress(file_addr).get();
583 }
584
585protected:
586 struct SectionInfo
587 {
588 SectionInfo () :
589 vm_range(),
590 section (NULL)
591 {
592 }
593
594 VMRange vm_range;
595 Section *section;
596 };
597 SectionList *m_section_list;
598 std::vector<SectionInfo> m_section_infos;
599};
600
601
602
603size_t
604ObjectFileMachO::ParseSymtab (bool minimize)
605{
606 Timer scoped_timer(__PRETTY_FUNCTION__,
607 "ObjectFileMachO::ParseSymtab () module = %s",
608 m_file.GetFilename().AsCString(""));
609 struct symtab_command symtab_load_command;
610 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
611 uint32_t i;
612 for (i=0; i<m_header.ncmds; ++i)
613 {
614 const uint32_t cmd_offset = offset;
615 // Read in the load command and load command size
616 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
617 break;
618 // Watch for the symbol table load command
Greg Clayton1674b122010-07-21 22:12:05 +0000619 if (symtab_load_command.cmd == LoadCommandSymtab)
Chris Lattner24943d22010-06-08 16:52:24 +0000620 {
621 // Read in the rest of the symtab load command
Jason Molendaccfba722010-07-06 22:38:03 +0000622 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
Chris Lattner24943d22010-06-08 16:52:24 +0000623 {
624 Symtab *symtab = m_symtab_ap.get();
625 SectionList *section_list = GetSectionList();
626 assert(section_list);
627 const size_t addr_size = m_data.GetAddressByteSize();
628 const ByteOrder endian = m_data.GetByteOrder();
629 bool bit_width_32 = addr_size == 4;
630 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
631
632 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size));
633 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize));
634
635 const char *strtab_data = (const char *)strtab_data_sp->GetBytes();
636// DataExtractor symtab_data(symtab_data_sp, endian, addr_size);
637// DataExtractor strtab_data(strtab_data_sp, endian, addr_size);
638
639 static ConstString g_segment_name_TEXT ("__TEXT");
640 static ConstString g_segment_name_DATA ("__DATA");
641 static ConstString g_segment_name_OBJC ("__OBJC");
642 static ConstString g_section_name_eh_frame ("__eh_frame");
643 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
644 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
645 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
646 SectionSP eh_frame_section_sp;
647 if (text_section_sp.get())
648 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
649 else
650 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
651
Greg Clayton1674b122010-07-21 22:12:05 +0000652 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Chris Lattner24943d22010-06-08 16:52:24 +0000653 //uint32_t symtab_offset = 0;
654 const uint8_t* nlist_data = symtab_data_sp->GetBytes();
655 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms);
656
657
Greg Claytoncd548032011-02-01 01:31:41 +0000658 if (endian != lldb::endian::InlHostByteOrder())
Chris Lattner24943d22010-06-08 16:52:24 +0000659 {
660 // ...
661 assert (!"UNIMPLEMENTED: Swap all nlist entries");
662 }
Greg Clayton7c36fa02010-09-11 03:13:28 +0000663 uint32_t N_SO_index = UINT32_MAX;
Chris Lattner24943d22010-06-08 16:52:24 +0000664
665 MachSymtabSectionInfo section_info (section_list);
666 std::vector<uint32_t> N_FUN_indexes;
667 std::vector<uint32_t> N_NSYM_indexes;
668 std::vector<uint32_t> N_INCL_indexes;
669 std::vector<uint32_t> N_BRAC_indexes;
670 std::vector<uint32_t> N_COMM_indexes;
Greg Clayton576a68b2010-09-08 16:38:06 +0000671 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Clayton637029b2010-09-12 05:25:16 +0000672 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Clayton576a68b2010-09-08 16:38:06 +0000673 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
674 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Clayton7c36fa02010-09-11 03:13:28 +0000675 // Any symbols that get merged into another will get an entry
676 // in this map so we know
Greg Clayton637029b2010-09-12 05:25:16 +0000677 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000678 uint32_t nlist_idx = 0;
679 Symbol *symbol_ptr = NULL;
680
681 uint32_t sym_idx = 0;
682 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
683 uint32_t num_syms = symtab->GetNumSymbols();
684
685 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
686 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
687 {
688 struct nlist_64 nlist;
689 if (bit_width_32)
690 {
691 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size));
Greg Clayton1674b122010-07-21 22:12:05 +0000692 nlist.n_strx = nlist32_ptr->n_strx;
Chris Lattner24943d22010-06-08 16:52:24 +0000693 nlist.n_type = nlist32_ptr->n_type;
694 nlist.n_sect = nlist32_ptr->n_sect;
695 nlist.n_desc = nlist32_ptr->n_desc;
696 nlist.n_value = nlist32_ptr->n_value;
697 }
698 else
699 {
700 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size)));
701 }
702
703 SymbolType type = eSymbolTypeInvalid;
Greg Clayton1674b122010-07-21 22:12:05 +0000704 const char* symbol_name = &strtab_data[nlist.n_strx];
Chris Lattner24943d22010-06-08 16:52:24 +0000705 if (symbol_name[0] == '\0')
706 symbol_name = NULL;
707 Section* symbol_section = NULL;
708 bool add_nlist = true;
Greg Clayton1674b122010-07-21 22:12:05 +0000709 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Chris Lattner24943d22010-06-08 16:52:24 +0000710
711 assert (sym_idx < num_syms);
712
713 sym[sym_idx].SetDebug (is_debug);
714
715 if (is_debug)
716 {
717 switch (nlist.n_type)
718 {
Greg Clayton1674b122010-07-21 22:12:05 +0000719 case StabGlobalSymbol:
720 // N_GSYM -- global symbol: name,,NO_SECT,type,0
Chris Lattner24943d22010-06-08 16:52:24 +0000721 // Sometimes the N_GSYM value contains the address.
Greg Clayton7c36fa02010-09-11 03:13:28 +0000722 sym[sym_idx].SetExternal(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000723 if (nlist.n_value != 0)
724 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton7c36fa02010-09-11 03:13:28 +0000725 type = eSymbolTypeData;
Chris Lattner24943d22010-06-08 16:52:24 +0000726 break;
727
Greg Clayton1674b122010-07-21 22:12:05 +0000728 case StabFunctionName:
729 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Clayton7c36fa02010-09-11 03:13:28 +0000730 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +0000731 break;
732
Greg Clayton1674b122010-07-21 22:12:05 +0000733 case StabFunction:
734 // N_FUN -- procedure: name,,n_sect,linenumber,address
Chris Lattner24943d22010-06-08 16:52:24 +0000735 if (symbol_name)
736 {
Greg Clayton7c36fa02010-09-11 03:13:28 +0000737 type = eSymbolTypeCode;
Chris Lattner24943d22010-06-08 16:52:24 +0000738 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton576a68b2010-09-08 16:38:06 +0000739
740 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000741 // We use the current number of symbols in the symbol table in lieu of
742 // using nlist_idx in case we ever start trimming entries out
743 N_FUN_indexes.push_back(sym_idx);
744 }
745 else
746 {
Greg Clayton7c36fa02010-09-11 03:13:28 +0000747 type = eSymbolTypeCompiler;
Chris Lattner24943d22010-06-08 16:52:24 +0000748
749 if ( !N_FUN_indexes.empty() )
750 {
751 // Copy the size of the function into the original STAB entry so we don't have
752 // to hunt for it later
753 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
754 N_FUN_indexes.pop_back();
Jason Molendaccfba722010-07-06 22:38:03 +0000755 // We don't really need the end function STAB as it contains the size which
Chris Lattner24943d22010-06-08 16:52:24 +0000756 // we already placed with the original symbol, so don't add it if we want a
757 // minimal symbol table
758 if (minimize)
759 add_nlist = false;
760 }
761 }
762 break;
763
Greg Clayton1674b122010-07-21 22:12:05 +0000764 case StabStaticSymbol:
765 // N_STSYM -- static symbol: name,,n_sect,type,address
Greg Clayton576a68b2010-09-08 16:38:06 +0000766 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000767 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton7c36fa02010-09-11 03:13:28 +0000768 type = eSymbolTypeData;
Chris Lattner24943d22010-06-08 16:52:24 +0000769 break;
770
Greg Clayton1674b122010-07-21 22:12:05 +0000771 case StabLocalCommon:
772 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
Chris Lattner24943d22010-06-08 16:52:24 +0000773 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
774 type = eSymbolTypeCommonBlock;
775 break;
776
Greg Clayton1674b122010-07-21 22:12:05 +0000777 case StabBeginSymbol:
778 // N_BNSYM
Chris Lattner24943d22010-06-08 16:52:24 +0000779 // We use the current number of symbols in the symbol table in lieu of
780 // using nlist_idx in case we ever start trimming entries out
781 if (minimize)
782 {
783 // Skip these if we want minimal symbol tables
784 add_nlist = false;
785 }
786 else
787 {
788 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
789 N_NSYM_indexes.push_back(sym_idx);
790 type = eSymbolTypeScopeBegin;
791 }
792 break;
793
Greg Clayton1674b122010-07-21 22:12:05 +0000794 case StabEndSymbol:
795 // N_ENSYM
Chris Lattner24943d22010-06-08 16:52:24 +0000796 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
797 // so that we can always skip the entire symbol if we need to navigate
798 // more quickly at the source level when parsing STABS
799 if (minimize)
800 {
801 // Skip these if we want minimal symbol tables
802 add_nlist = false;
803 }
804 else
805 {
806 if ( !N_NSYM_indexes.empty() )
807 {
808 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
809 symbol_ptr->SetByteSize(sym_idx + 1);
810 symbol_ptr->SetSizeIsSibling(true);
811 N_NSYM_indexes.pop_back();
812 }
813 type = eSymbolTypeScopeEnd;
814 }
815 break;
816
817
Greg Clayton1674b122010-07-21 22:12:05 +0000818 case StabSourceFileOptions:
819 // N_OPT - emitted with gcc2_compiled and in gcc source
Chris Lattner24943d22010-06-08 16:52:24 +0000820 type = eSymbolTypeCompiler;
821 break;
822
Greg Clayton1674b122010-07-21 22:12:05 +0000823 case StabRegisterSymbol:
824 // N_RSYM - register sym: name,,NO_SECT,type,register
Chris Lattner24943d22010-06-08 16:52:24 +0000825 type = eSymbolTypeVariable;
826 break;
827
Greg Clayton1674b122010-07-21 22:12:05 +0000828 case StabSourceLine:
829 // N_SLINE - src line: 0,,n_sect,linenumber,address
Chris Lattner24943d22010-06-08 16:52:24 +0000830 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
831 type = eSymbolTypeLineEntry;
832 break;
833
Greg Clayton1674b122010-07-21 22:12:05 +0000834 case StabStructureType:
835 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
Chris Lattner24943d22010-06-08 16:52:24 +0000836 type = eSymbolTypeVariableType;
837 break;
838
Greg Clayton1674b122010-07-21 22:12:05 +0000839 case StabSourceFileName:
840 // N_SO - source file name
Chris Lattner24943d22010-06-08 16:52:24 +0000841 type = eSymbolTypeSourceFile;
842 if (symbol_name == NULL)
843 {
Greg Clayton7c36fa02010-09-11 03:13:28 +0000844 if (minimize)
845 add_nlist = false;
846 if (N_SO_index != UINT32_MAX)
Chris Lattner24943d22010-06-08 16:52:24 +0000847 {
848 // Set the size of the N_SO to the terminating index of this N_SO
849 // so that we can always skip the entire N_SO if we need to navigate
850 // more quickly at the source level when parsing STABS
851 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Clayton7c36fa02010-09-11 03:13:28 +0000852 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
Chris Lattner24943d22010-06-08 16:52:24 +0000853 symbol_ptr->SetSizeIsSibling(true);
854 }
855 N_NSYM_indexes.clear();
856 N_INCL_indexes.clear();
857 N_BRAC_indexes.clear();
858 N_COMM_indexes.clear();
859 N_FUN_indexes.clear();
Greg Clayton7c36fa02010-09-11 03:13:28 +0000860 N_SO_index = UINT32_MAX;
Chris Lattner24943d22010-06-08 16:52:24 +0000861 }
Greg Clayton7c36fa02010-09-11 03:13:28 +0000862 else
Chris Lattner24943d22010-06-08 16:52:24 +0000863 {
864 // We use the current number of symbols in the symbol table in lieu of
865 // using nlist_idx in case we ever start trimming entries out
Greg Clayton7c36fa02010-09-11 03:13:28 +0000866 if (symbol_name[0] == '/')
867 N_SO_index = sym_idx;
868 else if (minimize && (N_SO_index == sym_idx - 1))
869 {
870 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
871 if (so_path && so_path[0])
872 {
873 std::string full_so_path (so_path);
874 if (*full_so_path.rbegin() != '/')
875 full_so_path += '/';
876 full_so_path += symbol_name;
877 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
878 add_nlist = false;
Greg Clayton637029b2010-09-12 05:25:16 +0000879 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
Greg Clayton7c36fa02010-09-11 03:13:28 +0000880 }
881 }
Chris Lattner24943d22010-06-08 16:52:24 +0000882 }
Greg Clayton7c36fa02010-09-11 03:13:28 +0000883
Chris Lattner24943d22010-06-08 16:52:24 +0000884 break;
885
Greg Clayton1674b122010-07-21 22:12:05 +0000886 case StabObjectFileName:
887 // N_OSO - object file name: name,,0,0,st_mtime
Chris Lattner24943d22010-06-08 16:52:24 +0000888 type = eSymbolTypeObjectFile;
889 break;
890
Greg Clayton1674b122010-07-21 22:12:05 +0000891 case StabLocalSymbol:
892 // N_LSYM - local sym: name,,NO_SECT,type,offset
Chris Lattner24943d22010-06-08 16:52:24 +0000893 type = eSymbolTypeLocal;
894 break;
895
896 //----------------------------------------------------------------------
897 // INCL scopes
898 //----------------------------------------------------------------------
Greg Clayton1674b122010-07-21 22:12:05 +0000899 case StabBeginIncludeFileName:
900 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
Chris Lattner24943d22010-06-08 16:52:24 +0000901 // We use the current number of symbols in the symbol table in lieu of
902 // using nlist_idx in case we ever start trimming entries out
903 N_INCL_indexes.push_back(sym_idx);
904 type = eSymbolTypeScopeBegin;
905 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000906
Greg Clayton1674b122010-07-21 22:12:05 +0000907 case StabEndIncludeFile:
908 // N_EINCL - include file end: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +0000909 // Set the size of the N_BINCL to the terminating index of this N_EINCL
910 // so that we can always skip the entire symbol if we need to navigate
911 // more quickly at the source level when parsing STABS
912 if ( !N_INCL_indexes.empty() )
913 {
914 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
915 symbol_ptr->SetByteSize(sym_idx + 1);
916 symbol_ptr->SetSizeIsSibling(true);
917 N_INCL_indexes.pop_back();
918 }
919 type = eSymbolTypeScopeEnd;
920 break;
921
Greg Clayton1674b122010-07-21 22:12:05 +0000922 case StabIncludeFileName:
923 // N_SOL - #included file name: name,,n_sect,0,address
Chris Lattner24943d22010-06-08 16:52:24 +0000924 type = eSymbolTypeHeaderFile;
Greg Clayton0ad086f2010-09-07 17:36:17 +0000925
926 // We currently don't use the header files on darwin
927 if (minimize)
928 add_nlist = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000929 break;
930
Greg Clayton1674b122010-07-21 22:12:05 +0000931 case StabCompilerParameters:
932 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +0000933 type = eSymbolTypeCompiler;
934 break;
935
Greg Clayton1674b122010-07-21 22:12:05 +0000936 case StabCompilerVersion:
937 // N_VERSION - compiler version: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +0000938 type = eSymbolTypeCompiler;
939 break;
940
Greg Clayton1674b122010-07-21 22:12:05 +0000941 case StabCompilerOptLevel:
942 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +0000943 type = eSymbolTypeCompiler;
944 break;
945
Greg Clayton1674b122010-07-21 22:12:05 +0000946 case StabParameter:
947 // N_PSYM - parameter: name,,NO_SECT,type,offset
Chris Lattner24943d22010-06-08 16:52:24 +0000948 type = eSymbolTypeVariable;
949 break;
950
Greg Clayton1674b122010-07-21 22:12:05 +0000951 case StabAlternateEntry:
952 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
Chris Lattner24943d22010-06-08 16:52:24 +0000953 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
954 type = eSymbolTypeLineEntry;
955 break;
956
957 //----------------------------------------------------------------------
958 // Left and Right Braces
959 //----------------------------------------------------------------------
Greg Clayton1674b122010-07-21 22:12:05 +0000960 case StabLeftBracket:
961 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
Chris Lattner24943d22010-06-08 16:52:24 +0000962 // We use the current number of symbols in the symbol table in lieu of
963 // using nlist_idx in case we ever start trimming entries out
964 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
965 N_BRAC_indexes.push_back(sym_idx);
966 type = eSymbolTypeScopeBegin;
967 break;
968
Greg Clayton1674b122010-07-21 22:12:05 +0000969 case StabRightBracket:
970 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
Chris Lattner24943d22010-06-08 16:52:24 +0000971 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
972 // so that we can always skip the entire symbol if we need to navigate
973 // more quickly at the source level when parsing STABS
974 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
975 if ( !N_BRAC_indexes.empty() )
976 {
977 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
978 symbol_ptr->SetByteSize(sym_idx + 1);
979 symbol_ptr->SetSizeIsSibling(true);
980 N_BRAC_indexes.pop_back();
981 }
982 type = eSymbolTypeScopeEnd;
983 break;
984
Greg Clayton1674b122010-07-21 22:12:05 +0000985 case StabDeletedIncludeFile:
986 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
Chris Lattner24943d22010-06-08 16:52:24 +0000987 type = eSymbolTypeHeaderFile;
988 break;
989
990 //----------------------------------------------------------------------
991 // COMM scopes
992 //----------------------------------------------------------------------
Greg Clayton1674b122010-07-21 22:12:05 +0000993 case StabBeginCommon:
994 // N_BCOMM - begin common: name,,NO_SECT,0,0
Chris Lattner24943d22010-06-08 16:52:24 +0000995 // We use the current number of symbols in the symbol table in lieu of
996 // using nlist_idx in case we ever start trimming entries out
997 type = eSymbolTypeScopeBegin;
998 N_COMM_indexes.push_back(sym_idx);
999 break;
1000
Greg Clayton1674b122010-07-21 22:12:05 +00001001 case StabEndCommonLocal:
1002 // N_ECOML - end common (local name): 0,,n_sect,0,address
Chris Lattner24943d22010-06-08 16:52:24 +00001003 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1004 // Fall through
1005
Greg Clayton1674b122010-07-21 22:12:05 +00001006 case StabEndCommon:
1007 // N_ECOMM - end common: name,,n_sect,0,0
Chris Lattner24943d22010-06-08 16:52:24 +00001008 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
1009 // so that we can always skip the entire symbol if we need to navigate
1010 // more quickly at the source level when parsing STABS
1011 if ( !N_COMM_indexes.empty() )
1012 {
1013 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
1014 symbol_ptr->SetByteSize(sym_idx + 1);
1015 symbol_ptr->SetSizeIsSibling(true);
1016 N_COMM_indexes.pop_back();
1017 }
1018 type = eSymbolTypeScopeEnd;
1019 break;
1020
Greg Clayton1674b122010-07-21 22:12:05 +00001021 case StabLength:
1022 // N_LENG - second stab entry with length information
Chris Lattner24943d22010-06-08 16:52:24 +00001023 type = eSymbolTypeAdditional;
1024 break;
1025
1026 default: break;
1027 }
1028 }
1029 else
1030 {
Greg Clayton1674b122010-07-21 22:12:05 +00001031 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
1032 uint8_t n_type = NlistMaskType & nlist.n_type;
1033 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001034
1035 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name)
1036 {
1037 type = eSymbolTypeRuntime;
1038 }
1039 else
1040 {
1041 switch (n_type)
1042 {
Greg Clayton1674b122010-07-21 22:12:05 +00001043 case NListTypeIndirect: // N_INDR - Fall through
1044 case NListTypePreboundUndefined:// N_PBUD - Fall through
1045 case NListTypeUndefined: // N_UNDF
Chris Lattner24943d22010-06-08 16:52:24 +00001046 type = eSymbolTypeExtern;
1047 break;
1048
Greg Clayton1674b122010-07-21 22:12:05 +00001049 case NListTypeAbsolute: // N_ABS
Chris Lattner24943d22010-06-08 16:52:24 +00001050 type = eSymbolTypeAbsolute;
1051 break;
1052
Greg Clayton1674b122010-07-21 22:12:05 +00001053 case NListTypeSection: // N_SECT
Chris Lattner24943d22010-06-08 16:52:24 +00001054 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1055
1056 assert(symbol_section != NULL);
1057 if (TEXT_eh_frame_sectID == nlist.n_sect)
1058 {
1059 type = eSymbolTypeException;
1060 }
1061 else
1062 {
Greg Claytonf3d0b0c2010-10-27 03:32:59 +00001063 uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType;
Chris Lattner24943d22010-06-08 16:52:24 +00001064
1065 switch (section_type)
1066 {
Greg Clayton1674b122010-07-21 22:12:05 +00001067 case SectionTypeRegular: break; // regular section
1068 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
1069 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
1070 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
1071 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
1072 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1073 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1074 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1075 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1076 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
1077 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
1078 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
1079 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
1080 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
1081 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
1082 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
1083 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
Chris Lattner24943d22010-06-08 16:52:24 +00001084 default: break;
1085 }
1086
1087 if (type == eSymbolTypeInvalid)
1088 {
1089 const char *symbol_sect_name = symbol_section->GetName().AsCString();
1090 if (symbol_section->IsDescendant (text_section_sp.get()))
1091 {
Greg Clayton1674b122010-07-21 22:12:05 +00001092 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1093 SectionAttrUserSelfModifyingCode |
1094 SectionAttrSytemSomeInstructions))
Chris Lattner24943d22010-06-08 16:52:24 +00001095 type = eSymbolTypeData;
1096 else
1097 type = eSymbolTypeCode;
1098 }
1099 else
1100 if (symbol_section->IsDescendant(data_section_sp.get()))
1101 {
1102 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1103 {
1104 type = eSymbolTypeRuntime;
1105 }
1106 else
1107 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1108 {
1109 type = eSymbolTypeException;
1110 }
1111 else
1112 {
1113 type = eSymbolTypeData;
1114 }
1115 }
1116 else
1117 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1118 {
1119 type = eSymbolTypeTrampoline;
1120 }
1121 else
1122 if (symbol_section->IsDescendant(objc_section_sp.get()))
1123 {
1124 type = eSymbolTypeRuntime;
1125 }
1126 }
1127 }
1128 break;
Greg Clayton576a68b2010-09-08 16:38:06 +00001129 }
Chris Lattner24943d22010-06-08 16:52:24 +00001130 }
1131 }
Chris Lattner24943d22010-06-08 16:52:24 +00001132 if (add_nlist)
1133 {
1134 bool symbol_name_is_mangled = false;
1135 if (symbol_name && symbol_name[0] == '_')
1136 {
1137 symbol_name_is_mangled = symbol_name[1] == '_';
1138 symbol_name++; // Skip the leading underscore
1139 }
1140 uint64_t symbol_value = nlist.n_value;
Greg Clayton576a68b2010-09-08 16:38:06 +00001141
1142 if (symbol_name)
1143 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
Greg Clayton7c36fa02010-09-11 03:13:28 +00001144 if (is_debug == false)
Greg Clayton576a68b2010-09-08 16:38:06 +00001145 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001146 if (type == eSymbolTypeCode)
Greg Clayton576a68b2010-09-08 16:38:06 +00001147 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001148 // See if we can find a N_FUN entry for any code symbols.
1149 // If we do find a match, and the name matches, then we
1150 // can merge the two into just the function symbol to avoid
1151 // duplicate entries in the symbol table
1152 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1153 if (pos != N_FUN_addr_to_sym_idx.end())
Greg Clayton576a68b2010-09-08 16:38:06 +00001154 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001155 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1156 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1157 {
Greg Clayton637029b2010-09-12 05:25:16 +00001158 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001159 // We just need the flags from the linker symbol, so put these flags
1160 // into the N_FUN flags to avoid duplicate symbols in the symbol table
1161 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1162 sym[sym_idx].Clear();
1163 continue;
1164 }
Greg Clayton576a68b2010-09-08 16:38:06 +00001165 }
1166 }
Greg Clayton7c36fa02010-09-11 03:13:28 +00001167 else if (type == eSymbolTypeData)
Greg Clayton576a68b2010-09-08 16:38:06 +00001168 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001169 // See if we can find a N_STSYM entry for any data symbols.
1170 // If we do find a match, and the name matches, then we
1171 // can merge the two into just the Static symbol to avoid
1172 // duplicate entries in the symbol table
1173 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
1174 if (pos != N_STSYM_addr_to_sym_idx.end())
Greg Clayton576a68b2010-09-08 16:38:06 +00001175 {
Greg Clayton7c36fa02010-09-11 03:13:28 +00001176 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1177 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1178 {
Greg Clayton637029b2010-09-12 05:25:16 +00001179 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001180 // We just need the flags from the linker symbol, so put these flags
1181 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
1182 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1183 sym[sym_idx].Clear();
1184 continue;
1185 }
Greg Clayton576a68b2010-09-08 16:38:06 +00001186 }
1187 }
1188 }
Chris Lattner24943d22010-06-08 16:52:24 +00001189 if (symbol_section != NULL)
1190 symbol_value -= symbol_section->GetFileAddress();
1191
1192 sym[sym_idx].SetID (nlist_idx);
1193 sym[sym_idx].SetType (type);
Chris Lattner24943d22010-06-08 16:52:24 +00001194 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1195 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1196 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1197
1198 ++sym_idx;
1199 }
1200 else
1201 {
1202 sym[sym_idx].Clear();
1203 }
1204
1205 }
1206
Chris Lattner24943d22010-06-08 16:52:24 +00001207 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1208 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1209 // such entries by figuring out what the address for the global is by looking up this non-STAB
1210 // entry and copying the value into the debug symbol's value to save us the hassle in the
1211 // debug symbol parser.
1212
1213 Symbol *global_symbol = NULL;
1214 for (nlist_idx = 0;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001215 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 +00001216 nlist_idx++)
1217 {
1218 if (global_symbol->GetValue().GetFileAddress() == 0)
1219 {
1220 std::vector<uint32_t> indexes;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001221 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001222 {
1223 std::vector<uint32_t>::const_iterator pos;
1224 std::vector<uint32_t>::const_iterator end = indexes.end();
1225 for (pos = indexes.begin(); pos != end; ++pos)
1226 {
1227 symbol_ptr = symtab->SymbolAtIndex(*pos);
1228 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1229 {
1230 global_symbol->SetValue(symbol_ptr->GetValue());
1231 break;
1232 }
1233 }
1234 }
1235 }
1236 }
Greg Clayton637029b2010-09-12 05:25:16 +00001237
1238 // Trim our symbols down to just what we ended up with after
1239 // removing any symbols.
1240 if (sym_idx < num_syms)
1241 {
1242 num_syms = sym_idx;
1243 sym = symtab->Resize (num_syms);
1244 }
1245
Chris Lattner24943d22010-06-08 16:52:24 +00001246 // Now synthesize indirect symbols
1247 if (m_dysymtab.nindirectsyms != 0)
1248 {
1249 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4));
1250
1251 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize())
1252 {
Greg Clayton637029b2010-09-12 05:25:16 +00001253 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
Chris Lattner24943d22010-06-08 16:52:24 +00001254 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1255
1256 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1257 {
Greg Clayton1674b122010-07-21 22:12:05 +00001258 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
Chris Lattner24943d22010-06-08 16:52:24 +00001259 {
1260 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1261 if (symbol_stub_byte_size == 0)
1262 continue;
1263
1264 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1265
1266 if (num_symbol_stubs == 0)
1267 continue;
1268
1269 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
Greg Clayton637029b2010-09-12 05:25:16 +00001270 uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms;
Chris Lattner24943d22010-06-08 16:52:24 +00001271 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1272 {
1273 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1274 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1275 uint32_t symbol_stub_offset = symbol_stub_index * 4;
1276 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1277 {
Greg Clayton637029b2010-09-12 05:25:16 +00001278 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Greg Clayton6af4fad2010-10-06 01:26:32 +00001279 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
1280 continue;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001281
Greg Clayton637029b2010-09-12 05:25:16 +00001282 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
1283 Symbol *stub_symbol = NULL;
Greg Clayton7c36fa02010-09-11 03:13:28 +00001284 if (index_pos != end_index_pos)
Greg Clayton637029b2010-09-12 05:25:16 +00001285 {
1286 // We have a remapping from the original nlist index to
1287 // a current symbol index, so just look this up by index
1288 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
1289 }
1290 else
1291 {
1292 // We need to lookup a symbol using the original nlist
1293 // symbol index since this index is coming from the
1294 // S_SYMBOL_STUBS
1295 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
1296 }
Greg Clayton0ad086f2010-09-07 17:36:17 +00001297
1298 assert (stub_symbol);
Chris Lattner24943d22010-06-08 16:52:24 +00001299 if (stub_symbol)
1300 {
1301 Address so_addr(symbol_stub_addr, section_list);
1302
1303 if (stub_symbol->GetType() == eSymbolTypeExtern)
1304 {
1305 // Change the external symbol into a trampoline that makes sense
1306 // These symbols were N_UNDF N_EXT, and are useless to us, so we
1307 // can re-use them so we don't have to make up a synthetic symbol
1308 // for no good reason.
1309 stub_symbol->SetType (eSymbolTypeTrampoline);
1310 stub_symbol->SetExternal (false);
1311 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1312 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1313 }
1314 else
1315 {
1316 // Make a synthetic symbol to describe the trampoline stub
1317 if (sym_idx >= num_syms)
Greg Clayton637029b2010-09-12 05:25:16 +00001318 sym = symtab->Resize (++num_syms);
1319 sym[sym_idx].SetID (synthetic_stub_sym_id++);
Chris Lattner24943d22010-06-08 16:52:24 +00001320 sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1321 sym[sym_idx].SetType (eSymbolTypeTrampoline);
1322 sym[sym_idx].SetIsSynthetic (true);
1323 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1324 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1325 ++sym_idx;
1326 }
1327 }
1328 }
1329 }
1330 }
1331 }
1332 }
1333 }
1334
Chris Lattner24943d22010-06-08 16:52:24 +00001335 return symtab->GetNumSymbols();
1336 }
1337 }
1338 offset = cmd_offset + symtab_load_command.cmdsize;
1339 }
1340 return 0;
1341}
1342
1343
1344void
1345ObjectFileMachO::Dump (Stream *s)
1346{
1347 lldb_private::Mutex::Locker locker(m_mutex);
1348 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1349 s->Indent();
Greg Clayton1674b122010-07-21 22:12:05 +00001350 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
Chris Lattner24943d22010-06-08 16:52:24 +00001351 s->PutCString("ObjectFileMachO64");
1352 else
1353 s->PutCString("ObjectFileMachO32");
1354
Greg Claytoncf015052010-06-11 03:25:34 +00001355 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +00001356
Greg Clayton940b1032011-02-23 00:35:02 +00001357 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Chris Lattner24943d22010-06-08 16:52:24 +00001358
1359 if (m_sections_ap.get())
Greg Clayton58e844b2010-12-08 05:08:21 +00001360 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00001361
1362 if (m_symtab_ap.get())
Greg Clayton8d3802d2010-10-08 04:20:14 +00001363 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
Chris Lattner24943d22010-06-08 16:52:24 +00001364}
1365
1366
1367bool
Greg Clayton0467c782011-02-04 18:53:10 +00001368ObjectFileMachO::GetUUID (lldb_private::UUID* uuid)
Chris Lattner24943d22010-06-08 16:52:24 +00001369{
1370 lldb_private::Mutex::Locker locker(m_mutex);
1371 struct uuid_command load_cmd;
1372 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1373 uint32_t i;
1374 for (i=0; i<m_header.ncmds; ++i)
1375 {
1376 const uint32_t cmd_offset = offset;
1377 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1378 break;
1379
Greg Clayton1674b122010-07-21 22:12:05 +00001380 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +00001381 {
1382 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1383 if (uuid_bytes)
1384 {
1385 uuid->SetBytes (uuid_bytes);
1386 return true;
1387 }
1388 return false;
1389 }
1390 offset = cmd_offset + load_cmd.cmdsize;
1391 }
1392 return false;
1393}
1394
1395
1396uint32_t
1397ObjectFileMachO::GetDependentModules (FileSpecList& files)
1398{
1399 lldb_private::Mutex::Locker locker(m_mutex);
1400 struct load_command load_cmd;
1401 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1402 uint32_t count = 0;
1403 uint32_t i;
1404 for (i=0; i<m_header.ncmds; ++i)
1405 {
1406 const uint32_t cmd_offset = offset;
1407 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1408 break;
1409
1410 switch (load_cmd.cmd)
1411 {
Greg Clayton1674b122010-07-21 22:12:05 +00001412 case LoadCommandDylibLoad:
1413 case LoadCommandDylibLoadWeak:
1414 case LoadCommandDylibReexport:
1415 case LoadCommandDynamicLinkerLoad:
1416 case LoadCommandFixedVMShlibLoad:
Greg Clayton08a73202010-10-09 00:48:53 +00001417 case LoadCommandDylibLoadUpward:
Chris Lattner24943d22010-06-08 16:52:24 +00001418 {
1419 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1420 const char *path = m_data.PeekCStr(name_offset);
1421 // Skip any path that starts with '@' since these are usually:
1422 // @executable_path/.../file
1423 // @rpath/.../file
1424 if (path && path[0] != '@')
1425 {
Greg Clayton537a7a82010-10-20 20:54:39 +00001426 FileSpec file_spec(path, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001427 if (files.AppendIfUnique(file_spec))
1428 count++;
1429 }
1430 }
1431 break;
1432
1433 default:
1434 break;
1435 }
1436 offset = cmd_offset + load_cmd.cmdsize;
1437 }
1438 return count;
1439}
1440
Jim Ingham28775942011-03-07 23:44:08 +00001441lldb_private::Address
1442ObjectFileMachO::GetEntryPointAddress ()
1443{
1444 // If the object file is not an executable it can't hold the entry point. m_entry_point_address
1445 // is initialized to an invalid address, so we can just return that.
1446 // If m_entry_point_address is valid it means we've found it already, so return the cached value.
1447
1448 if (!IsExecutable() || m_entry_point_address.IsValid())
1449 return m_entry_point_address;
1450
1451 // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in
1452 // /usr/include/mach-o.h, but it is basically:
1453 //
1454 // uint32_t flavor - this is the flavor argument you would pass to thread_get_state
1455 // uint32_t count - this is the count of longs in the thread state data
1456 // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor.
1457 // <repeat this trio>
1458 //
1459 // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there.
1460 // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers
1461 // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin,
1462 // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here.
1463 //
1464 // For now we hard-code the offsets and flavors we need:
1465 //
1466 //
1467
1468 lldb_private::Mutex::Locker locker(m_mutex);
1469 struct load_command load_cmd;
1470 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1471 uint32_t i;
1472 lldb::addr_t start_address = LLDB_INVALID_ADDRESS;
1473 bool done = false;
1474
1475 for (i=0; i<m_header.ncmds; ++i)
1476 {
1477 const uint32_t cmd_offset = offset;
1478 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1479 break;
1480
1481 switch (load_cmd.cmd)
1482 {
1483 case LoadCommandUnixThread:
1484 case LoadCommandThread:
1485 {
1486 while (offset < cmd_offset + load_cmd.cmdsize)
1487 {
1488 uint32_t flavor = m_data.GetU32(&offset);
1489 uint32_t count = m_data.GetU32(&offset);
1490 if (count == 0)
1491 {
1492 // We've gotten off somehow, log and exit;
1493 return m_entry_point_address;
1494 }
1495
1496 switch (m_header.cputype)
1497 {
1498 case llvm::MachO::CPUTypeARM:
1499 if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h
1500 {
1501 offset += 60; // This is the offset of pc in the GPR thread state data structure.
1502 start_address = m_data.GetU32(&offset);
1503 done = true;
1504 }
1505 break;
1506 case llvm::MachO::CPUTypeI386:
1507 if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h
1508 {
1509 offset += 40; // This is the offset of eip in the GPR thread state data structure.
1510 start_address = m_data.GetU32(&offset);
1511 done = true;
1512 }
1513 break;
1514 case llvm::MachO::CPUTypeX86_64:
1515 if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h
1516 {
1517 offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure.
1518 start_address = m_data.GetU64(&offset);
1519 done = true;
1520 }
1521 break;
1522 default:
1523 return m_entry_point_address;
1524 }
1525 // Haven't found the GPR flavor yet, skip over the data for this flavor:
1526 if (done)
1527 break;
1528 offset += count * 4;
1529 }
1530 }
1531 break;
1532
1533 default:
1534 break;
1535 }
1536 if (done)
1537 break;
1538
1539 // Go to the next load command:
1540 offset = cmd_offset + load_cmd.cmdsize;
1541 }
1542
1543 if (start_address != LLDB_INVALID_ADDRESS)
1544 {
1545 // We got the start address from the load commands, so now resolve that address in the sections
1546 // of this ObjectFile:
1547 if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList()))
1548 {
1549 m_entry_point_address.Clear();
1550 }
1551 }
1552 else
1553 {
1554 // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the
1555 // "start" symbol in the main executable.
1556
1557 SymbolContextList contexts;
1558 SymbolContext context;
1559 if (!m_module->FindSymbolsWithNameAndType(ConstString ("start"), lldb::eSymbolTypeCode, contexts))
1560 return m_entry_point_address;
1561
1562 contexts.GetContextAtIndex(0, context);
1563
1564 m_entry_point_address = context.symbol->GetValue();
1565 }
1566
1567 return m_entry_point_address;
1568
1569}
1570
Chris Lattner24943d22010-06-08 16:52:24 +00001571bool
Greg Clayton395fc332011-02-15 21:59:32 +00001572ObjectFileMachO::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00001573{
1574 lldb_private::Mutex::Locker locker(m_mutex);
Greg Clayton940b1032011-02-23 00:35:02 +00001575 arch.SetArchitecture (lldb::eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Greg Clayton395fc332011-02-15 21:59:32 +00001576 return true;
Chris Lattner24943d22010-06-08 16:52:24 +00001577}
1578
1579
1580//------------------------------------------------------------------
1581// PluginInterface protocol
1582//------------------------------------------------------------------
1583const char *
1584ObjectFileMachO::GetPluginName()
1585{
1586 return "ObjectFileMachO";
1587}
1588
1589const char *
1590ObjectFileMachO::GetShortPluginName()
1591{
1592 return GetPluginNameStatic();
1593}
1594
1595uint32_t
1596ObjectFileMachO::GetPluginVersion()
1597{
1598 return 1;
1599}
1600
1601void
1602ObjectFileMachO::GetPluginCommandHelp (const char *command, Stream *strm)
1603{
1604}
1605
1606Error
1607ObjectFileMachO::ExecutePluginCommand (Args &command, Stream *strm)
1608{
1609 Error error;
1610 error.SetErrorString("No plug-in command are currently supported.");
1611 return error;
1612}
1613
1614Log *
1615ObjectFileMachO::EnablePluginLogging (Stream *strm, Args &command)
1616{
1617 return NULL;
1618}
1619
1620
1621
1622