blob: c0d7eecef232b6db9bcb2b19b16517c89c7ac81f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ObjectFileMachO.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ObjectFileMachO.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/ArchSpec.h"
13#include "lldb/Core/DataBuffer.h"
14#include "lldb/Core/FileSpec.h"
15#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 Lattner30fdc8d2010-06-08 16:52:24 +000025
26using namespace lldb;
27using namespace lldb_private;
Greg Claytone1a916a2010-07-21 22:12:05 +000028using namespace llvm::MachO;
Chris Lattner30fdc8d2010-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 Claytone1a916a2010-07-21 22:12:05 +000077 case HeaderMagic32:
78 case HeaderMagic32Swapped:
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079 return sizeof(struct mach_header);
80
Greg Claytone1a916a2010-07-21 22:12:05 +000081 case HeaderMagic64:
82 case HeaderMagic64Swapped:
Chris Lattner30fdc8d2010-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{
96 DataExtractor data(dataSP, eByteOrderHost, 4);
97 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{
110 ::bzero (&m_header, sizeof(m_header));
111 ::bzero (&m_dysymtab, sizeof(m_dysymtab));
112}
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;
126 m_data.SetByteOrder (eByteOrderHost);
127 // Leave magic in the original byte order
128 m_header.magic = m_data.GetU32(&offset);
129 switch (m_header.magic)
130 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000131 case HeaderMagic32:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132 m_data.SetByteOrder (eByteOrderHost);
133 m_data.SetAddressByteSize(4);
134 can_parse = true;
135 break;
136
Greg Claytone1a916a2010-07-21 22:12:05 +0000137 case HeaderMagic64:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 m_data.SetByteOrder (eByteOrderHost);
139 m_data.SetAddressByteSize(8);
140 can_parse = true;
141 break;
142
Greg Claytone1a916a2010-07-21 22:12:05 +0000143 case HeaderMagic32Swapped:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144 m_data.SetByteOrder(eByteOrderHost == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
145 m_data.SetAddressByteSize(4);
146 can_parse = true;
147 break;
148
Greg Claytone1a916a2010-07-21 22:12:05 +0000149 case HeaderMagic64Swapped:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150 m_data.SetByteOrder(eByteOrderHost == eByteOrderBig ? eByteOrderLittle : eByteOrderBig);
151 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 Clayton41f92322010-06-11 03:25:34 +0000163 ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164 if (mach_arch == m_module->GetArchitecture())
165 {
166 // Read in all only the load command data
167 DataBufferSP data_sp(m_file.ReadFileContents(m_offset, m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic)));
168 m_data.SetData (data_sp);
169 return true;
170 }
171 }
172 else
173 {
174 memset(&m_header, 0, sizeof(struct mach_header));
175 }
176 return false;
177}
178
179
180ByteOrder
181ObjectFileMachO::GetByteOrder () const
182{
183 lldb_private::Mutex::Locker locker(m_mutex);
184 return m_data.GetByteOrder ();
185}
186
187
188size_t
189ObjectFileMachO::GetAddressByteSize () const
190{
191 lldb_private::Mutex::Locker locker(m_mutex);
192 return m_data.GetAddressByteSize ();
193}
194
195
196Symtab *
197ObjectFileMachO::GetSymtab()
198{
199 lldb_private::Mutex::Locker locker(m_mutex);
200 if (m_symtab_ap.get() == NULL)
201 {
202 m_symtab_ap.reset(new Symtab(this));
203 ParseSymtab(false);
204 }
205 return m_symtab_ap.get();
206}
207
208
209SectionList *
210ObjectFileMachO::GetSectionList()
211{
212 lldb_private::Mutex::Locker locker(m_mutex);
213 if (m_sections_ap.get() == NULL)
214 {
215 m_sections_ap.reset(new SectionList());
216 ParseSections();
217 }
218 return m_sections_ap.get();
219}
220
221
222size_t
223ObjectFileMachO::ParseSections ()
224{
225 lldb::user_id_t segID = 0;
226 lldb::user_id_t sectID = 0;
227 struct segment_command_64 load_cmd;
228 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
229 uint32_t i;
230 //bool dump_sections = false;
231 for (i=0; i<m_header.ncmds; ++i)
232 {
233 const uint32_t load_cmd_offset = offset;
234 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
235 break;
236
Greg Claytone1a916a2010-07-21 22:12:05 +0000237 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 {
239 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
240 {
241 load_cmd.vmaddr = m_data.GetAddress(&offset);
242 load_cmd.vmsize = m_data.GetAddress(&offset);
243 load_cmd.fileoff = m_data.GetAddress(&offset);
244 load_cmd.filesize = m_data.GetAddress(&offset);
245 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
246 {
247 // Keep a list of mach segments around in case we need to
248 // get at data that isn't stored in the abstracted Sections.
249 m_mach_segments.push_back (load_cmd);
250
251 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
252 // Use a segment ID of the segment index shifted left by 8 so they
253 // never conflict with any of the sections.
254 SectionSP segment_sp;
255 if (segment_name)
256 {
257 segment_sp.reset(new Section (NULL,
258 GetModule(), // Module to which this section belongs
259 ++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
260 segment_name, // Name of this section
261 eSectionTypeContainer, // This section is a container of other sections.
262 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
263 load_cmd.vmsize, // VM size in bytes of this section
264 load_cmd.fileoff, // Offset to the data for this section in the file
265 load_cmd.filesize, // Size in bytes of this section as found in the the file
266 load_cmd.flags)); // Flags for this section
267
268 m_sections_ap->AddSection(segment_sp);
269 }
270
271 struct section_64 sect64;
272 ::bzero (&sect64, sizeof(sect64));
273 // Push a section into our mach sections for the section at
Greg Claytone1a916a2010-07-21 22:12:05 +0000274 // index zero (NListSectionNoSection)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 m_mach_sections.push_back(sect64);
276 uint32_t segment_sect_idx;
277 const lldb::user_id_t first_segment_sectID = sectID + 1;
278
279
Greg Claytone1a916a2010-07-21 22:12:05 +0000280 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
282 {
283 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
284 break;
285 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
286 break;
287 sect64.addr = m_data.GetAddress(&offset);
288 sect64.size = m_data.GetAddress(&offset);
289
290 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
291 break;
292
293 // Keep a list of mach sections around in case we need to
294 // get at data that isn't stored in the abstracted Sections.
295 m_mach_sections.push_back (sect64);
296
297 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
298 if (!segment_name)
299 {
300 // We have a segment with no name so we need to conjure up
301 // segments that correspond to the section's segname if there
302 // isn't already such a section. If there is such a section,
303 // we resize the section so that it spans all sections.
304 // We also mark these sections as fake so address matches don't
305 // hit if they land in the gaps between the child sections.
306 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
307 segment_sp = m_sections_ap->FindSectionByName (segment_name);
308 if (segment_sp.get())
309 {
310 Section *segment = segment_sp.get();
311 // Grow the section size as needed.
312 const lldb::addr_t sect64_min_addr = sect64.addr;
313 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
314 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
315 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
316 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
317 if (sect64_min_addr >= curr_seg_min_addr)
318 {
319 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
320 // Only grow the section size if needed
321 if (new_seg_byte_size > curr_seg_byte_size)
322 segment->SetByteSize (new_seg_byte_size);
323 }
324 else
325 {
326 // We need to change the base address of the segment and
327 // adjust the child section offsets for all existing children.
328 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
329 segment->Slide(slide_amount, false);
330 segment->GetChildren().Slide (-slide_amount, false);
331 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
332 }
Greg Clayton8d38ac42010-06-28 23:51:11 +0000333
334 // Grow the section size as needed.
335 if (sect64.offset)
336 {
337 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
338 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
339
340 const lldb::addr_t section_min_file_offset = sect64.offset;
341 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
342 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
343 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
344 segment->SetFileOffset (new_file_offset);
345 segment->SetFileSize (new_file_size);
346 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347 }
348 else
349 {
350 // Create a fake section for the section's named segment
351 segment_sp.reset(new Section(segment_sp.get(), // Parent section
352 GetModule(), // Module to which this section belongs
353 ++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
354 segment_name, // Name of this section
355 eSectionTypeContainer, // This section is a container of other sections.
356 sect64.addr, // File VM address == addresses as they are found in the object file
357 sect64.size, // VM size in bytes of this section
358 sect64.offset, // Offset to the data for this section in the file
359 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
360 load_cmd.flags)); // Flags for this section
361 segment_sp->SetIsFake(true);
362 m_sections_ap->AddSection(segment_sp);
363 }
364 }
365 assert (segment_sp.get());
366
Greg Claytone1a916a2010-07-21 22:12:05 +0000367 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368 static ConstString g_sect_name_objc_data ("__objc_data");
369 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
370 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
371 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
372 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
373 static ConstString g_sect_name_objc_const ("__objc_const");
374 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
375 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000376
377 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
378 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
379 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
380 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
381 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
382 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
383 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
384 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
385 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
386 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
387 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
388 static ConstString g_sect_name_eh_frame ("__eh_frame");
389
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 SectionType sect_type = eSectionTypeOther;
391
Greg Clayton4ceb9982010-07-21 22:54:26 +0000392
393 if (section_name == g_sect_name_dwarf_debug_abbrev)
394 sect_type = eSectionTypeDWARFDebugAbbrev;
395 else if (section_name == g_sect_name_dwarf_debug_aranges)
396 sect_type = eSectionTypeDWARFDebugAranges;
397 else if (section_name == g_sect_name_dwarf_debug_frame)
398 sect_type = eSectionTypeDWARFDebugFrame;
399 else if (section_name == g_sect_name_dwarf_debug_info)
400 sect_type = eSectionTypeDWARFDebugInfo;
401 else if (section_name == g_sect_name_dwarf_debug_line)
402 sect_type = eSectionTypeDWARFDebugLine;
403 else if (section_name == g_sect_name_dwarf_debug_loc)
404 sect_type = eSectionTypeDWARFDebugLoc;
405 else if (section_name == g_sect_name_dwarf_debug_macinfo)
406 sect_type = eSectionTypeDWARFDebugMacInfo;
407 else if (section_name == g_sect_name_dwarf_debug_pubnames)
408 sect_type = eSectionTypeDWARFDebugPubNames;
409 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
410 sect_type = eSectionTypeDWARFDebugPubTypes;
411 else if (section_name == g_sect_name_dwarf_debug_ranges)
412 sect_type = eSectionTypeDWARFDebugRanges;
413 else if (section_name == g_sect_name_dwarf_debug_str)
414 sect_type = eSectionTypeDWARFDebugStr;
415 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton4ceb9982010-07-21 22:54:26 +0000419 else if (section_name == g_sect_name_eh_frame)
420 sect_type = eSectionTypeEHFrame;
421 else if (section_name == g_sect_name_cfstring)
422 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 else if (section_name == g_sect_name_objc_data ||
424 section_name == g_sect_name_objc_classrefs ||
425 section_name == g_sect_name_objc_superrefs ||
426 section_name == g_sect_name_objc_const ||
427 section_name == g_sect_name_objc_classlist)
428 {
429 sect_type = eSectionTypeDataPointers;
430 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431
432 if (sect_type == eSectionTypeOther)
433 {
434 switch (mach_sect_type)
435 {
436 // TODO: categorize sections by other flags for regular sections
Greg Claytone1a916a2010-07-21 22:12:05 +0000437 case SectionTypeRegular: sect_type = eSectionTypeOther; break;
438 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
439 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
440 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
441 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
442 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
443 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
444 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
445 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
446 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
447 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
448 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
449 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
450 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
451 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
452 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
453 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454 default: break;
455 }
456 }
457
458 SectionSP section_sp(new Section(segment_sp.get(),
459 GetModule(),
460 ++sectID,
461 section_name,
462 sect_type,
463 sect64.addr - segment_sp->GetFileAddress(),
464 sect64.size,
465 sect64.offset,
466 sect64.offset == 0 ? 0 : sect64.size,
467 sect64.flags));
468 segment_sp->GetChildren().AddSection(section_sp);
469
470 if (segment_sp->IsFake())
471 {
472 segment_sp.reset();
473 segment_name.Clear();
474 }
475 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000476 if (m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477 {
478 if (first_segment_sectID <= sectID)
479 {
480 lldb::user_id_t sect_uid;
481 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
482 {
483 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
484 SectionSP next_section_sp;
485 if (sect_uid + 1 <= sectID)
486 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
487
488 if (curr_section_sp.get())
489 {
490 if (curr_section_sp->GetByteSize() == 0)
491 {
492 if (next_section_sp.get() != NULL)
493 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
494 else
495 curr_section_sp->SetByteSize ( load_cmd.vmsize );
496 }
497 }
498 }
499 }
500 }
501 }
502 }
503 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000504 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505 {
506 m_dysymtab.cmd = load_cmd.cmd;
507 m_dysymtab.cmdsize = load_cmd.cmdsize;
508 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
509 }
510
511 offset = load_cmd_offset + load_cmd.cmdsize;
512 }
513// if (dump_sections)
514// {
515// StreamFile s(stdout);
516// m_sections_ap->Dump(&s, true);
517// }
518 return sectID; // Return the number of sections we registered with the module
519}
520
521class MachSymtabSectionInfo
522{
523public:
524
525 MachSymtabSectionInfo (SectionList *section_list) :
526 m_section_list (section_list),
527 m_section_infos()
528 {
529 // Get the number of sections down to a depth of 1 to include
530 // all segments and their sections, but no other sections that
531 // may be added for debug map or
532 m_section_infos.resize(section_list->GetNumSections(1));
533 }
534
535
536 Section *
537 GetSection (uint8_t n_sect, addr_t file_addr)
538 {
539 if (n_sect == 0)
540 return NULL;
541 if (n_sect < m_section_infos.size())
542 {
543 if (m_section_infos[n_sect].section == NULL)
544 {
545 Section *section = m_section_list->FindSectionByID (n_sect).get();
546 m_section_infos[n_sect].section = section;
547 assert (section != NULL);
548 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
549 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
550 }
551 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
552 return m_section_infos[n_sect].section;
553 }
554 return m_section_list->FindSectionContainingFileAddress(file_addr).get();
555 }
556
557protected:
558 struct SectionInfo
559 {
560 SectionInfo () :
561 vm_range(),
562 section (NULL)
563 {
564 }
565
566 VMRange vm_range;
567 Section *section;
568 };
569 SectionList *m_section_list;
570 std::vector<SectionInfo> m_section_infos;
571};
572
573
574
575size_t
576ObjectFileMachO::ParseSymtab (bool minimize)
577{
578 Timer scoped_timer(__PRETTY_FUNCTION__,
579 "ObjectFileMachO::ParseSymtab () module = %s",
580 m_file.GetFilename().AsCString(""));
581 struct symtab_command symtab_load_command;
582 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
583 uint32_t i;
584 for (i=0; i<m_header.ncmds; ++i)
585 {
586 const uint32_t cmd_offset = offset;
587 // Read in the load command and load command size
588 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
589 break;
590 // Watch for the symbol table load command
Greg Claytone1a916a2010-07-21 22:12:05 +0000591 if (symtab_load_command.cmd == LoadCommandSymtab)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592 {
593 // Read in the rest of the symtab load command
Jason Molendaea84e762010-07-06 22:38:03 +0000594 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595 {
596 Symtab *symtab = m_symtab_ap.get();
597 SectionList *section_list = GetSectionList();
598 assert(section_list);
599 const size_t addr_size = m_data.GetAddressByteSize();
600 const ByteOrder endian = m_data.GetByteOrder();
601 bool bit_width_32 = addr_size == 4;
602 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
603
604 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size));
605 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize));
606
607 const char *strtab_data = (const char *)strtab_data_sp->GetBytes();
608// DataExtractor symtab_data(symtab_data_sp, endian, addr_size);
609// DataExtractor strtab_data(strtab_data_sp, endian, addr_size);
610
611 static ConstString g_segment_name_TEXT ("__TEXT");
612 static ConstString g_segment_name_DATA ("__DATA");
613 static ConstString g_segment_name_OBJC ("__OBJC");
614 static ConstString g_section_name_eh_frame ("__eh_frame");
615 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
616 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
617 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
618 SectionSP eh_frame_section_sp;
619 if (text_section_sp.get())
620 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
621 else
622 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
623
Greg Claytone1a916a2010-07-21 22:12:05 +0000624 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625 //uint32_t symtab_offset = 0;
626 const uint8_t* nlist_data = symtab_data_sp->GetBytes();
627 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms);
628
629
630 if (endian != eByteOrderHost)
631 {
632 // ...
633 assert (!"UNIMPLEMENTED: Swap all nlist entries");
634 }
635 uint32_t N_SO_index = UINT_MAX;
636
637 MachSymtabSectionInfo section_info (section_list);
638 std::vector<uint32_t> N_FUN_indexes;
639 std::vector<uint32_t> N_NSYM_indexes;
640 std::vector<uint32_t> N_INCL_indexes;
641 std::vector<uint32_t> N_BRAC_indexes;
642 std::vector<uint32_t> N_COMM_indexes;
643 uint32_t nlist_idx = 0;
644 Symbol *symbol_ptr = NULL;
645
646 uint32_t sym_idx = 0;
647 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
648 uint32_t num_syms = symtab->GetNumSymbols();
649
650 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
651 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
652 {
653 struct nlist_64 nlist;
654 if (bit_width_32)
655 {
656 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size));
Greg Claytone1a916a2010-07-21 22:12:05 +0000657 nlist.n_strx = nlist32_ptr->n_strx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 nlist.n_type = nlist32_ptr->n_type;
659 nlist.n_sect = nlist32_ptr->n_sect;
660 nlist.n_desc = nlist32_ptr->n_desc;
661 nlist.n_value = nlist32_ptr->n_value;
662 }
663 else
664 {
665 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size)));
666 }
667
668 SymbolType type = eSymbolTypeInvalid;
Greg Claytone1a916a2010-07-21 22:12:05 +0000669 const char* symbol_name = &strtab_data[nlist.n_strx];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670 if (symbol_name[0] == '\0')
671 symbol_name = NULL;
672 Section* symbol_section = NULL;
673 bool add_nlist = true;
Greg Claytone1a916a2010-07-21 22:12:05 +0000674 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675
676 assert (sym_idx < num_syms);
677
678 sym[sym_idx].SetDebug (is_debug);
679
680 if (is_debug)
681 {
682 switch (nlist.n_type)
683 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000684 case StabGlobalSymbol:
685 // N_GSYM -- global symbol: name,,NO_SECT,type,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686 // Sometimes the N_GSYM value contains the address.
687 if (nlist.n_value != 0)
688 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
689 type = eSymbolTypeGlobal;
690 break;
691
Greg Claytone1a916a2010-07-21 22:12:05 +0000692 case StabFunctionName:
693 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694 type = eSymbolTypeFunction;
695 break;
696
Greg Claytone1a916a2010-07-21 22:12:05 +0000697 case StabFunction:
698 // N_FUN -- procedure: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699 if (symbol_name)
700 {
701 type = eSymbolTypeFunction;
702 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
703 // We use the current number of symbols in the symbol table in lieu of
704 // using nlist_idx in case we ever start trimming entries out
705 N_FUN_indexes.push_back(sym_idx);
706 }
707 else
708 {
709 type = eSymbolTypeFunctionEnd;
710
711 if ( !N_FUN_indexes.empty() )
712 {
713 // Copy the size of the function into the original STAB entry so we don't have
714 // to hunt for it later
715 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
716 N_FUN_indexes.pop_back();
Jason Molendaea84e762010-07-06 22:38:03 +0000717 // We don't really need the end function STAB as it contains the size which
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 // we already placed with the original symbol, so don't add it if we want a
719 // minimal symbol table
720 if (minimize)
721 add_nlist = false;
722 }
723 }
724 break;
725
Greg Claytone1a916a2010-07-21 22:12:05 +0000726 case StabStaticSymbol:
727 // N_STSYM -- static symbol: name,,n_sect,type,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
729 type = eSymbolTypeStatic;
730 break;
731
Greg Claytone1a916a2010-07-21 22:12:05 +0000732 case StabLocalCommon:
733 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
735 type = eSymbolTypeCommonBlock;
736 break;
737
Greg Claytone1a916a2010-07-21 22:12:05 +0000738 case StabBeginSymbol:
739 // N_BNSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000740 // We use the current number of symbols in the symbol table in lieu of
741 // using nlist_idx in case we ever start trimming entries out
742 if (minimize)
743 {
744 // Skip these if we want minimal symbol tables
745 add_nlist = false;
746 }
747 else
748 {
749 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
750 N_NSYM_indexes.push_back(sym_idx);
751 type = eSymbolTypeScopeBegin;
752 }
753 break;
754
Greg Claytone1a916a2010-07-21 22:12:05 +0000755 case StabEndSymbol:
756 // N_ENSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
758 // so that we can always skip the entire symbol if we need to navigate
759 // more quickly at the source level when parsing STABS
760 if (minimize)
761 {
762 // Skip these if we want minimal symbol tables
763 add_nlist = false;
764 }
765 else
766 {
767 if ( !N_NSYM_indexes.empty() )
768 {
769 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
770 symbol_ptr->SetByteSize(sym_idx + 1);
771 symbol_ptr->SetSizeIsSibling(true);
772 N_NSYM_indexes.pop_back();
773 }
774 type = eSymbolTypeScopeEnd;
775 }
776 break;
777
778
Greg Claytone1a916a2010-07-21 22:12:05 +0000779 case StabSourceFileOptions:
780 // N_OPT - emitted with gcc2_compiled and in gcc source
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781 type = eSymbolTypeCompiler;
782 break;
783
Greg Claytone1a916a2010-07-21 22:12:05 +0000784 case StabRegisterSymbol:
785 // N_RSYM - register sym: name,,NO_SECT,type,register
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786 type = eSymbolTypeVariable;
787 break;
788
Greg Claytone1a916a2010-07-21 22:12:05 +0000789 case StabSourceLine:
790 // N_SLINE - src line: 0,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
792 type = eSymbolTypeLineEntry;
793 break;
794
Greg Claytone1a916a2010-07-21 22:12:05 +0000795 case StabStructureType:
796 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797 type = eSymbolTypeVariableType;
798 break;
799
Greg Claytone1a916a2010-07-21 22:12:05 +0000800 case StabSourceFileName:
801 // N_SO - source file name
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802 type = eSymbolTypeSourceFile;
803 if (symbol_name == NULL)
804 {
805 if (N_SO_index == UINT_MAX)
806 {
807 // Skip the extra blank N_SO entries that happen when the entire
808 // path is contained in the second consecutive N_SO STAB.
809 if (minimize)
810 add_nlist = false;
811 }
812 else
813 {
814 // Set the size of the N_SO to the terminating index of this N_SO
815 // so that we can always skip the entire N_SO if we need to navigate
816 // more quickly at the source level when parsing STABS
817 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
818 symbol_ptr->SetByteSize(sym_idx + 1);
819 symbol_ptr->SetSizeIsSibling(true);
820 }
821 N_NSYM_indexes.clear();
822 N_INCL_indexes.clear();
823 N_BRAC_indexes.clear();
824 N_COMM_indexes.clear();
825 N_FUN_indexes.clear();
826 N_SO_index = UINT_MAX;
827 }
828 else if (symbol_name[0] == '/')
829 {
830 // We use the current number of symbols in the symbol table in lieu of
831 // using nlist_idx in case we ever start trimming entries out
832 N_SO_index = sym_idx;
833 }
834 break;
835
Greg Claytone1a916a2010-07-21 22:12:05 +0000836 case StabObjectFileName:
837 // N_OSO - object file name: name,,0,0,st_mtime
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838 type = eSymbolTypeObjectFile;
839 break;
840
Greg Claytone1a916a2010-07-21 22:12:05 +0000841 case StabLocalSymbol:
842 // N_LSYM - local sym: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000843 type = eSymbolTypeLocal;
844 break;
845
846 //----------------------------------------------------------------------
847 // INCL scopes
848 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +0000849 case StabBeginIncludeFileName:
850 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851 // We use the current number of symbols in the symbol table in lieu of
852 // using nlist_idx in case we ever start trimming entries out
853 N_INCL_indexes.push_back(sym_idx);
854 type = eSymbolTypeScopeBegin;
855 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856
Greg Claytone1a916a2010-07-21 22:12:05 +0000857 case StabEndIncludeFile:
858 // N_EINCL - include file end: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859 // Set the size of the N_BINCL to the terminating index of this N_EINCL
860 // so that we can always skip the entire symbol if we need to navigate
861 // more quickly at the source level when parsing STABS
862 if ( !N_INCL_indexes.empty() )
863 {
864 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
865 symbol_ptr->SetByteSize(sym_idx + 1);
866 symbol_ptr->SetSizeIsSibling(true);
867 N_INCL_indexes.pop_back();
868 }
869 type = eSymbolTypeScopeEnd;
870 break;
871
Greg Claytone1a916a2010-07-21 22:12:05 +0000872 case StabIncludeFileName:
873 // N_SOL - #included file name: name,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874 type = eSymbolTypeHeaderFile;
875 break;
876
Greg Claytone1a916a2010-07-21 22:12:05 +0000877 case StabCompilerParameters:
878 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879 type = eSymbolTypeCompiler;
880 break;
881
Greg Claytone1a916a2010-07-21 22:12:05 +0000882 case StabCompilerVersion:
883 // N_VERSION - compiler version: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884 type = eSymbolTypeCompiler;
885 break;
886
Greg Claytone1a916a2010-07-21 22:12:05 +0000887 case StabCompilerOptLevel:
888 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889 type = eSymbolTypeCompiler;
890 break;
891
Greg Claytone1a916a2010-07-21 22:12:05 +0000892 case StabParameter:
893 // N_PSYM - parameter: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000894 type = eSymbolTypeVariable;
895 break;
896
Greg Claytone1a916a2010-07-21 22:12:05 +0000897 case StabAlternateEntry:
898 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
900 type = eSymbolTypeLineEntry;
901 break;
902
903 //----------------------------------------------------------------------
904 // Left and Right Braces
905 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +0000906 case StabLeftBracket:
907 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908 // We use the current number of symbols in the symbol table in lieu of
909 // using nlist_idx in case we ever start trimming entries out
910 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
911 N_BRAC_indexes.push_back(sym_idx);
912 type = eSymbolTypeScopeBegin;
913 break;
914
Greg Claytone1a916a2010-07-21 22:12:05 +0000915 case StabRightBracket:
916 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000917 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
918 // so that we can always skip the entire symbol if we need to navigate
919 // more quickly at the source level when parsing STABS
920 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
921 if ( !N_BRAC_indexes.empty() )
922 {
923 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
924 symbol_ptr->SetByteSize(sym_idx + 1);
925 symbol_ptr->SetSizeIsSibling(true);
926 N_BRAC_indexes.pop_back();
927 }
928 type = eSymbolTypeScopeEnd;
929 break;
930
Greg Claytone1a916a2010-07-21 22:12:05 +0000931 case StabDeletedIncludeFile:
932 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933 type = eSymbolTypeHeaderFile;
934 break;
935
936 //----------------------------------------------------------------------
937 // COMM scopes
938 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +0000939 case StabBeginCommon:
940 // N_BCOMM - begin common: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941 // We use the current number of symbols in the symbol table in lieu of
942 // using nlist_idx in case we ever start trimming entries out
943 type = eSymbolTypeScopeBegin;
944 N_COMM_indexes.push_back(sym_idx);
945 break;
946
Greg Claytone1a916a2010-07-21 22:12:05 +0000947 case StabEndCommonLocal:
948 // N_ECOML - end common (local name): 0,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
950 // Fall through
951
Greg Claytone1a916a2010-07-21 22:12:05 +0000952 case StabEndCommon:
953 // N_ECOMM - end common: name,,n_sect,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
955 // so that we can always skip the entire symbol if we need to navigate
956 // more quickly at the source level when parsing STABS
957 if ( !N_COMM_indexes.empty() )
958 {
959 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
960 symbol_ptr->SetByteSize(sym_idx + 1);
961 symbol_ptr->SetSizeIsSibling(true);
962 N_COMM_indexes.pop_back();
963 }
964 type = eSymbolTypeScopeEnd;
965 break;
966
Greg Claytone1a916a2010-07-21 22:12:05 +0000967 case StabLength:
968 // N_LENG - second stab entry with length information
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000969 type = eSymbolTypeAdditional;
970 break;
971
972 default: break;
973 }
974 }
975 else
976 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000977 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
978 uint8_t n_type = NlistMaskType & nlist.n_type;
979 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980
981 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name)
982 {
983 type = eSymbolTypeRuntime;
984 }
985 else
986 {
987 switch (n_type)
988 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000989 case NListTypeIndirect: // N_INDR - Fall through
990 case NListTypePreboundUndefined:// N_PBUD - Fall through
991 case NListTypeUndefined: // N_UNDF
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992 type = eSymbolTypeExtern;
993 break;
994
Greg Claytone1a916a2010-07-21 22:12:05 +0000995 case NListTypeAbsolute: // N_ABS
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996 type = eSymbolTypeAbsolute;
997 break;
998
Greg Claytone1a916a2010-07-21 22:12:05 +0000999 case NListTypeSection: // N_SECT
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001000 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1001
1002 assert(symbol_section != NULL);
1003 if (TEXT_eh_frame_sectID == nlist.n_sect)
1004 {
1005 type = eSymbolTypeException;
1006 }
1007 else
1008 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001009 uint32_t section_type = symbol_section->GetAllFlagBits() & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010
1011 switch (section_type)
1012 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001013 case SectionTypeRegular: break; // regular section
1014 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
1015 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
1016 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
1017 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
1018 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1019 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1020 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1021 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1022 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
1023 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
1024 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
1025 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
1026 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
1027 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
1028 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
1029 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030 default: break;
1031 }
1032
1033 if (type == eSymbolTypeInvalid)
1034 {
1035 const char *symbol_sect_name = symbol_section->GetName().AsCString();
1036 if (symbol_section->IsDescendant (text_section_sp.get()))
1037 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001038 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1039 SectionAttrUserSelfModifyingCode |
1040 SectionAttrSytemSomeInstructions))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 type = eSymbolTypeData;
1042 else
1043 type = eSymbolTypeCode;
1044 }
1045 else
1046 if (symbol_section->IsDescendant(data_section_sp.get()))
1047 {
1048 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1049 {
1050 type = eSymbolTypeRuntime;
1051 }
1052 else
1053 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1054 {
1055 type = eSymbolTypeException;
1056 }
1057 else
1058 {
1059 type = eSymbolTypeData;
1060 }
1061 }
1062 else
1063 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1064 {
1065 type = eSymbolTypeTrampoline;
1066 }
1067 else
1068 if (symbol_section->IsDescendant(objc_section_sp.get()))
1069 {
1070 type = eSymbolTypeRuntime;
1071 }
1072 }
1073 }
1074 break;
1075 }
1076 }
1077 }
1078
1079 if (add_nlist)
1080 {
1081 bool symbol_name_is_mangled = false;
1082 if (symbol_name && symbol_name[0] == '_')
1083 {
1084 symbol_name_is_mangled = symbol_name[1] == '_';
1085 symbol_name++; // Skip the leading underscore
1086 }
1087 uint64_t symbol_value = nlist.n_value;
1088 if (symbol_section != NULL)
1089 symbol_value -= symbol_section->GetFileAddress();
1090
1091 sym[sym_idx].SetID (nlist_idx);
1092 sym[sym_idx].SetType (type);
1093 if (symbol_name)
1094 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
1095 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1096 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1097 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1098
1099 ++sym_idx;
1100 }
1101 else
1102 {
1103 sym[sym_idx].Clear();
1104 }
1105
1106 }
1107
1108
1109 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1110 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1111 // such entries by figuring out what the address for the global is by looking up this non-STAB
1112 // entry and copying the value into the debug symbol's value to save us the hassle in the
1113 // debug symbol parser.
1114
1115 Symbol *global_symbol = NULL;
1116 for (nlist_idx = 0;
1117 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType(eSymbolTypeGlobal, nlist_idx)) != NULL;
1118 nlist_idx++)
1119 {
1120 if (global_symbol->GetValue().GetFileAddress() == 0)
1121 {
1122 std::vector<uint32_t> indexes;
1123 if (symtab->AppendSymbolIndexesWithName(global_symbol->GetMangled().GetName(), indexes) > 0)
1124 {
1125 std::vector<uint32_t>::const_iterator pos;
1126 std::vector<uint32_t>::const_iterator end = indexes.end();
1127 for (pos = indexes.begin(); pos != end; ++pos)
1128 {
1129 symbol_ptr = symtab->SymbolAtIndex(*pos);
1130 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1131 {
1132 global_symbol->SetValue(symbol_ptr->GetValue());
1133 break;
1134 }
1135 }
1136 }
1137 }
1138 }
1139 // Now synthesize indirect symbols
1140 if (m_dysymtab.nindirectsyms != 0)
1141 {
1142 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4));
1143
1144 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize())
1145 {
1146 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1147
1148 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1149 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001150 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001151 {
1152 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1153 if (symbol_stub_byte_size == 0)
1154 continue;
1155
1156 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1157
1158 if (num_symbol_stubs == 0)
1159 continue;
1160
1161 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
1162 uint32_t stub_sym_id = symtab_load_command.nsyms;
1163 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1164 {
1165 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1166 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1167 uint32_t symbol_stub_offset = symbol_stub_index * 4;
1168 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1169 {
1170 const uint32_t symbol_index = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
1171
1172 Symbol *stub_symbol = symtab->SymbolAtIndex(symbol_index);
1173 if (stub_symbol)
1174 {
1175 Address so_addr(symbol_stub_addr, section_list);
1176
1177 if (stub_symbol->GetType() == eSymbolTypeExtern)
1178 {
1179 // Change the external symbol into a trampoline that makes sense
1180 // These symbols were N_UNDF N_EXT, and are useless to us, so we
1181 // can re-use them so we don't have to make up a synthetic symbol
1182 // for no good reason.
1183 stub_symbol->SetType (eSymbolTypeTrampoline);
1184 stub_symbol->SetExternal (false);
1185 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1186 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1187 }
1188 else
1189 {
1190 // Make a synthetic symbol to describe the trampoline stub
1191 if (sym_idx >= num_syms)
1192 {
1193 sym = symtab->Resize (num_syms + 16);
1194 num_syms = symtab->GetNumSymbols();
1195 }
1196 sym[sym_idx].SetID (stub_sym_id++);
1197 sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1198 sym[sym_idx].SetType (eSymbolTypeTrampoline);
1199 sym[sym_idx].SetIsSynthetic (true);
1200 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1201 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1202 ++sym_idx;
1203 }
1204 }
1205 }
1206 }
1207 }
1208 }
1209 }
1210 }
1211
1212 if (sym_idx != symtab->GetNumSymbols())
1213 symtab->Resize (sym_idx);
1214
1215 return symtab->GetNumSymbols();
1216 }
1217 }
1218 offset = cmd_offset + symtab_load_command.cmdsize;
1219 }
1220 return 0;
1221}
1222
1223
1224void
1225ObjectFileMachO::Dump (Stream *s)
1226{
1227 lldb_private::Mutex::Locker locker(m_mutex);
1228 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1229 s->Indent();
Greg Claytone1a916a2010-07-21 22:12:05 +00001230 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231 s->PutCString("ObjectFileMachO64");
1232 else
1233 s->PutCString("ObjectFileMachO32");
1234
Greg Clayton41f92322010-06-11 03:25:34 +00001235 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001236
1237 *s << ", file = '" << m_file << "', arch = " << header_arch.AsCString() << "\n";
1238
1239 if (m_sections_ap.get())
1240 m_sections_ap->Dump(s, NULL, true);
1241
1242 if (m_symtab_ap.get())
1243 m_symtab_ap->Dump(s, NULL);
1244}
1245
1246
1247bool
1248ObjectFileMachO::GetUUID (UUID* uuid)
1249{
1250 lldb_private::Mutex::Locker locker(m_mutex);
1251 struct uuid_command load_cmd;
1252 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1253 uint32_t i;
1254 for (i=0; i<m_header.ncmds; ++i)
1255 {
1256 const uint32_t cmd_offset = offset;
1257 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1258 break;
1259
Greg Claytone1a916a2010-07-21 22:12:05 +00001260 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001261 {
1262 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1263 if (uuid_bytes)
1264 {
1265 uuid->SetBytes (uuid_bytes);
1266 return true;
1267 }
1268 return false;
1269 }
1270 offset = cmd_offset + load_cmd.cmdsize;
1271 }
1272 return false;
1273}
1274
1275
1276uint32_t
1277ObjectFileMachO::GetDependentModules (FileSpecList& files)
1278{
1279 lldb_private::Mutex::Locker locker(m_mutex);
1280 struct load_command load_cmd;
1281 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1282 uint32_t count = 0;
1283 uint32_t i;
1284 for (i=0; i<m_header.ncmds; ++i)
1285 {
1286 const uint32_t cmd_offset = offset;
1287 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1288 break;
1289
1290 switch (load_cmd.cmd)
1291 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001292 case LoadCommandDylibLoad:
1293 case LoadCommandDylibLoadWeak:
1294 case LoadCommandDylibReexport:
1295 case LoadCommandDynamicLinkerLoad:
1296 case LoadCommandFixedVMShlibLoad:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297 {
1298 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1299 const char *path = m_data.PeekCStr(name_offset);
1300 // Skip any path that starts with '@' since these are usually:
1301 // @executable_path/.../file
1302 // @rpath/.../file
1303 if (path && path[0] != '@')
1304 {
1305 FileSpec file_spec(path);
1306 if (files.AppendIfUnique(file_spec))
1307 count++;
1308 }
1309 }
1310 break;
1311
1312 default:
1313 break;
1314 }
1315 offset = cmd_offset + load_cmd.cmdsize;
1316 }
1317 return count;
1318}
1319
1320bool
1321ObjectFileMachO::GetTargetTriple (ConstString &target_triple)
1322{
1323 lldb_private::Mutex::Locker locker(m_mutex);
1324 std::string triple(GetModule()->GetArchitecture().AsCString());
1325 triple += "-apple-darwin";
1326 target_triple.SetCString(triple.c_str());
1327 if (target_triple)
1328 return true;
1329 return false;
1330}
1331
1332
1333//------------------------------------------------------------------
1334// PluginInterface protocol
1335//------------------------------------------------------------------
1336const char *
1337ObjectFileMachO::GetPluginName()
1338{
1339 return "ObjectFileMachO";
1340}
1341
1342const char *
1343ObjectFileMachO::GetShortPluginName()
1344{
1345 return GetPluginNameStatic();
1346}
1347
1348uint32_t
1349ObjectFileMachO::GetPluginVersion()
1350{
1351 return 1;
1352}
1353
1354void
1355ObjectFileMachO::GetPluginCommandHelp (const char *command, Stream *strm)
1356{
1357}
1358
1359Error
1360ObjectFileMachO::ExecutePluginCommand (Args &command, Stream *strm)
1361{
1362 Error error;
1363 error.SetErrorString("No plug-in command are currently supported.");
1364 return error;
1365}
1366
1367Log *
1368ObjectFileMachO::EnablePluginLogging (Stream *strm, Args &command)
1369{
1370 return NULL;
1371}
1372
1373
1374
1375