blob: de5c73c84b47ec6d324ad7538f41672bb6b807b2 [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");
376 SectionType sect_type = eSectionTypeOther;
377
378 if (section_name == g_sect_name_objc_selrefs)
379 {
380 sect_type = eSectionTypeDataCStringPointers;
381 }
382 else if (section_name == g_sect_name_objc_msgrefs)
383 {
384 sect_type = eSectionTypeDataObjCMessageRefs;
385 }
386 else if (section_name == g_sect_name_objc_data ||
387 section_name == g_sect_name_objc_classrefs ||
388 section_name == g_sect_name_objc_superrefs ||
389 section_name == g_sect_name_objc_const ||
390 section_name == g_sect_name_objc_classlist)
391 {
392 sect_type = eSectionTypeDataPointers;
393 }
394 else if (section_name == g_sect_name_cfstring)
395 {
396 sect_type = eSectionTypeDataObjCCFStrings;
397 }
398
399 if (sect_type == eSectionTypeOther)
400 {
401 switch (mach_sect_type)
402 {
403 // TODO: categorize sections by other flags for regular sections
Greg Claytone1a916a2010-07-21 22:12:05 +0000404 case SectionTypeRegular: sect_type = eSectionTypeOther; break;
405 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
406 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
407 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
408 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
409 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
410 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
411 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
412 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
413 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
414 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
415 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
416 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
417 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
418 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
419 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
420 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421 default: break;
422 }
423 }
424
425 SectionSP section_sp(new Section(segment_sp.get(),
426 GetModule(),
427 ++sectID,
428 section_name,
429 sect_type,
430 sect64.addr - segment_sp->GetFileAddress(),
431 sect64.size,
432 sect64.offset,
433 sect64.offset == 0 ? 0 : sect64.size,
434 sect64.flags));
435 segment_sp->GetChildren().AddSection(section_sp);
436
437 if (segment_sp->IsFake())
438 {
439 segment_sp.reset();
440 segment_name.Clear();
441 }
442 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000443 if (m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444 {
445 if (first_segment_sectID <= sectID)
446 {
447 lldb::user_id_t sect_uid;
448 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
449 {
450 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
451 SectionSP next_section_sp;
452 if (sect_uid + 1 <= sectID)
453 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
454
455 if (curr_section_sp.get())
456 {
457 if (curr_section_sp->GetByteSize() == 0)
458 {
459 if (next_section_sp.get() != NULL)
460 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
461 else
462 curr_section_sp->SetByteSize ( load_cmd.vmsize );
463 }
464 }
465 }
466 }
467 }
468 }
469 }
470 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000471 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 {
473 m_dysymtab.cmd = load_cmd.cmd;
474 m_dysymtab.cmdsize = load_cmd.cmdsize;
475 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
476 }
477
478 offset = load_cmd_offset + load_cmd.cmdsize;
479 }
480// if (dump_sections)
481// {
482// StreamFile s(stdout);
483// m_sections_ap->Dump(&s, true);
484// }
485 return sectID; // Return the number of sections we registered with the module
486}
487
488class MachSymtabSectionInfo
489{
490public:
491
492 MachSymtabSectionInfo (SectionList *section_list) :
493 m_section_list (section_list),
494 m_section_infos()
495 {
496 // Get the number of sections down to a depth of 1 to include
497 // all segments and their sections, but no other sections that
498 // may be added for debug map or
499 m_section_infos.resize(section_list->GetNumSections(1));
500 }
501
502
503 Section *
504 GetSection (uint8_t n_sect, addr_t file_addr)
505 {
506 if (n_sect == 0)
507 return NULL;
508 if (n_sect < m_section_infos.size())
509 {
510 if (m_section_infos[n_sect].section == NULL)
511 {
512 Section *section = m_section_list->FindSectionByID (n_sect).get();
513 m_section_infos[n_sect].section = section;
514 assert (section != NULL);
515 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
516 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
517 }
518 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
519 return m_section_infos[n_sect].section;
520 }
521 return m_section_list->FindSectionContainingFileAddress(file_addr).get();
522 }
523
524protected:
525 struct SectionInfo
526 {
527 SectionInfo () :
528 vm_range(),
529 section (NULL)
530 {
531 }
532
533 VMRange vm_range;
534 Section *section;
535 };
536 SectionList *m_section_list;
537 std::vector<SectionInfo> m_section_infos;
538};
539
540
541
542size_t
543ObjectFileMachO::ParseSymtab (bool minimize)
544{
545 Timer scoped_timer(__PRETTY_FUNCTION__,
546 "ObjectFileMachO::ParseSymtab () module = %s",
547 m_file.GetFilename().AsCString(""));
548 struct symtab_command symtab_load_command;
549 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
550 uint32_t i;
551 for (i=0; i<m_header.ncmds; ++i)
552 {
553 const uint32_t cmd_offset = offset;
554 // Read in the load command and load command size
555 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
556 break;
557 // Watch for the symbol table load command
Greg Claytone1a916a2010-07-21 22:12:05 +0000558 if (symtab_load_command.cmd == LoadCommandSymtab)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 {
560 // Read in the rest of the symtab load command
Jason Molendaea84e762010-07-06 22:38:03 +0000561 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562 {
563 Symtab *symtab = m_symtab_ap.get();
564 SectionList *section_list = GetSectionList();
565 assert(section_list);
566 const size_t addr_size = m_data.GetAddressByteSize();
567 const ByteOrder endian = m_data.GetByteOrder();
568 bool bit_width_32 = addr_size == 4;
569 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
570
571 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size));
572 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize));
573
574 const char *strtab_data = (const char *)strtab_data_sp->GetBytes();
575// DataExtractor symtab_data(symtab_data_sp, endian, addr_size);
576// DataExtractor strtab_data(strtab_data_sp, endian, addr_size);
577
578 static ConstString g_segment_name_TEXT ("__TEXT");
579 static ConstString g_segment_name_DATA ("__DATA");
580 static ConstString g_segment_name_OBJC ("__OBJC");
581 static ConstString g_section_name_eh_frame ("__eh_frame");
582 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
583 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
584 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
585 SectionSP eh_frame_section_sp;
586 if (text_section_sp.get())
587 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
588 else
589 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
590
Greg Claytone1a916a2010-07-21 22:12:05 +0000591 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592 //uint32_t symtab_offset = 0;
593 const uint8_t* nlist_data = symtab_data_sp->GetBytes();
594 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms);
595
596
597 if (endian != eByteOrderHost)
598 {
599 // ...
600 assert (!"UNIMPLEMENTED: Swap all nlist entries");
601 }
602 uint32_t N_SO_index = UINT_MAX;
603
604 MachSymtabSectionInfo section_info (section_list);
605 std::vector<uint32_t> N_FUN_indexes;
606 std::vector<uint32_t> N_NSYM_indexes;
607 std::vector<uint32_t> N_INCL_indexes;
608 std::vector<uint32_t> N_BRAC_indexes;
609 std::vector<uint32_t> N_COMM_indexes;
610 uint32_t nlist_idx = 0;
611 Symbol *symbol_ptr = NULL;
612
613 uint32_t sym_idx = 0;
614 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
615 uint32_t num_syms = symtab->GetNumSymbols();
616
617 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
618 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
619 {
620 struct nlist_64 nlist;
621 if (bit_width_32)
622 {
623 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size));
Greg Claytone1a916a2010-07-21 22:12:05 +0000624 nlist.n_strx = nlist32_ptr->n_strx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625 nlist.n_type = nlist32_ptr->n_type;
626 nlist.n_sect = nlist32_ptr->n_sect;
627 nlist.n_desc = nlist32_ptr->n_desc;
628 nlist.n_value = nlist32_ptr->n_value;
629 }
630 else
631 {
632 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size)));
633 }
634
635 SymbolType type = eSymbolTypeInvalid;
Greg Claytone1a916a2010-07-21 22:12:05 +0000636 const char* symbol_name = &strtab_data[nlist.n_strx];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637 if (symbol_name[0] == '\0')
638 symbol_name = NULL;
639 Section* symbol_section = NULL;
640 bool add_nlist = true;
Greg Claytone1a916a2010-07-21 22:12:05 +0000641 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642
643 assert (sym_idx < num_syms);
644
645 sym[sym_idx].SetDebug (is_debug);
646
647 if (is_debug)
648 {
649 switch (nlist.n_type)
650 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000651 case StabGlobalSymbol:
652 // N_GSYM -- global symbol: name,,NO_SECT,type,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653 // Sometimes the N_GSYM value contains the address.
654 if (nlist.n_value != 0)
655 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
656 type = eSymbolTypeGlobal;
657 break;
658
Greg Claytone1a916a2010-07-21 22:12:05 +0000659 case StabFunctionName:
660 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661 type = eSymbolTypeFunction;
662 break;
663
Greg Claytone1a916a2010-07-21 22:12:05 +0000664 case StabFunction:
665 // N_FUN -- procedure: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 if (symbol_name)
667 {
668 type = eSymbolTypeFunction;
669 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
670 // We use the current number of symbols in the symbol table in lieu of
671 // using nlist_idx in case we ever start trimming entries out
672 N_FUN_indexes.push_back(sym_idx);
673 }
674 else
675 {
676 type = eSymbolTypeFunctionEnd;
677
678 if ( !N_FUN_indexes.empty() )
679 {
680 // Copy the size of the function into the original STAB entry so we don't have
681 // to hunt for it later
682 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
683 N_FUN_indexes.pop_back();
Jason Molendaea84e762010-07-06 22:38:03 +0000684 // We don't really need the end function STAB as it contains the size which
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 // we already placed with the original symbol, so don't add it if we want a
686 // minimal symbol table
687 if (minimize)
688 add_nlist = false;
689 }
690 }
691 break;
692
Greg Claytone1a916a2010-07-21 22:12:05 +0000693 case StabStaticSymbol:
694 // N_STSYM -- static symbol: name,,n_sect,type,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
696 type = eSymbolTypeStatic;
697 break;
698
Greg Claytone1a916a2010-07-21 22:12:05 +0000699 case StabLocalCommon:
700 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
702 type = eSymbolTypeCommonBlock;
703 break;
704
Greg Claytone1a916a2010-07-21 22:12:05 +0000705 case StabBeginSymbol:
706 // N_BNSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707 // We use the current number of symbols in the symbol table in lieu of
708 // using nlist_idx in case we ever start trimming entries out
709 if (minimize)
710 {
711 // Skip these if we want minimal symbol tables
712 add_nlist = false;
713 }
714 else
715 {
716 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
717 N_NSYM_indexes.push_back(sym_idx);
718 type = eSymbolTypeScopeBegin;
719 }
720 break;
721
Greg Claytone1a916a2010-07-21 22:12:05 +0000722 case StabEndSymbol:
723 // N_ENSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
725 // so that we can always skip the entire symbol if we need to navigate
726 // more quickly at the source level when parsing STABS
727 if (minimize)
728 {
729 // Skip these if we want minimal symbol tables
730 add_nlist = false;
731 }
732 else
733 {
734 if ( !N_NSYM_indexes.empty() )
735 {
736 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
737 symbol_ptr->SetByteSize(sym_idx + 1);
738 symbol_ptr->SetSizeIsSibling(true);
739 N_NSYM_indexes.pop_back();
740 }
741 type = eSymbolTypeScopeEnd;
742 }
743 break;
744
745
Greg Claytone1a916a2010-07-21 22:12:05 +0000746 case StabSourceFileOptions:
747 // N_OPT - emitted with gcc2_compiled and in gcc source
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 type = eSymbolTypeCompiler;
749 break;
750
Greg Claytone1a916a2010-07-21 22:12:05 +0000751 case StabRegisterSymbol:
752 // N_RSYM - register sym: name,,NO_SECT,type,register
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753 type = eSymbolTypeVariable;
754 break;
755
Greg Claytone1a916a2010-07-21 22:12:05 +0000756 case StabSourceLine:
757 // N_SLINE - src line: 0,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
759 type = eSymbolTypeLineEntry;
760 break;
761
Greg Claytone1a916a2010-07-21 22:12:05 +0000762 case StabStructureType:
763 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764 type = eSymbolTypeVariableType;
765 break;
766
Greg Claytone1a916a2010-07-21 22:12:05 +0000767 case StabSourceFileName:
768 // N_SO - source file name
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769 type = eSymbolTypeSourceFile;
770 if (symbol_name == NULL)
771 {
772 if (N_SO_index == UINT_MAX)
773 {
774 // Skip the extra blank N_SO entries that happen when the entire
775 // path is contained in the second consecutive N_SO STAB.
776 if (minimize)
777 add_nlist = false;
778 }
779 else
780 {
781 // Set the size of the N_SO to the terminating index of this N_SO
782 // so that we can always skip the entire N_SO if we need to navigate
783 // more quickly at the source level when parsing STABS
784 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
785 symbol_ptr->SetByteSize(sym_idx + 1);
786 symbol_ptr->SetSizeIsSibling(true);
787 }
788 N_NSYM_indexes.clear();
789 N_INCL_indexes.clear();
790 N_BRAC_indexes.clear();
791 N_COMM_indexes.clear();
792 N_FUN_indexes.clear();
793 N_SO_index = UINT_MAX;
794 }
795 else if (symbol_name[0] == '/')
796 {
797 // We use the current number of symbols in the symbol table in lieu of
798 // using nlist_idx in case we ever start trimming entries out
799 N_SO_index = sym_idx;
800 }
801 break;
802
Greg Claytone1a916a2010-07-21 22:12:05 +0000803 case StabObjectFileName:
804 // N_OSO - object file name: name,,0,0,st_mtime
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805 type = eSymbolTypeObjectFile;
806 break;
807
Greg Claytone1a916a2010-07-21 22:12:05 +0000808 case StabLocalSymbol:
809 // N_LSYM - local sym: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 type = eSymbolTypeLocal;
811 break;
812
813 //----------------------------------------------------------------------
814 // INCL scopes
815 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +0000816 case StabBeginIncludeFileName:
817 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818 // We use the current number of symbols in the symbol table in lieu of
819 // using nlist_idx in case we ever start trimming entries out
820 N_INCL_indexes.push_back(sym_idx);
821 type = eSymbolTypeScopeBegin;
822 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000823
Greg Claytone1a916a2010-07-21 22:12:05 +0000824 case StabEndIncludeFile:
825 // N_EINCL - include file end: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 // Set the size of the N_BINCL to the terminating index of this N_EINCL
827 // so that we can always skip the entire symbol if we need to navigate
828 // more quickly at the source level when parsing STABS
829 if ( !N_INCL_indexes.empty() )
830 {
831 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
832 symbol_ptr->SetByteSize(sym_idx + 1);
833 symbol_ptr->SetSizeIsSibling(true);
834 N_INCL_indexes.pop_back();
835 }
836 type = eSymbolTypeScopeEnd;
837 break;
838
Greg Claytone1a916a2010-07-21 22:12:05 +0000839 case StabIncludeFileName:
840 // N_SOL - #included file name: name,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 type = eSymbolTypeHeaderFile;
842 break;
843
Greg Claytone1a916a2010-07-21 22:12:05 +0000844 case StabCompilerParameters:
845 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000846 type = eSymbolTypeCompiler;
847 break;
848
Greg Claytone1a916a2010-07-21 22:12:05 +0000849 case StabCompilerVersion:
850 // N_VERSION - compiler version: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851 type = eSymbolTypeCompiler;
852 break;
853
Greg Claytone1a916a2010-07-21 22:12:05 +0000854 case StabCompilerOptLevel:
855 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856 type = eSymbolTypeCompiler;
857 break;
858
Greg Claytone1a916a2010-07-21 22:12:05 +0000859 case StabParameter:
860 // N_PSYM - parameter: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861 type = eSymbolTypeVariable;
862 break;
863
Greg Claytone1a916a2010-07-21 22:12:05 +0000864 case StabAlternateEntry:
865 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
867 type = eSymbolTypeLineEntry;
868 break;
869
870 //----------------------------------------------------------------------
871 // Left and Right Braces
872 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +0000873 case StabLeftBracket:
874 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000875 // We use the current number of symbols in the symbol table in lieu of
876 // using nlist_idx in case we ever start trimming entries out
877 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
878 N_BRAC_indexes.push_back(sym_idx);
879 type = eSymbolTypeScopeBegin;
880 break;
881
Greg Claytone1a916a2010-07-21 22:12:05 +0000882 case StabRightBracket:
883 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
885 // so that we can always skip the entire symbol if we need to navigate
886 // more quickly at the source level when parsing STABS
887 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
888 if ( !N_BRAC_indexes.empty() )
889 {
890 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
891 symbol_ptr->SetByteSize(sym_idx + 1);
892 symbol_ptr->SetSizeIsSibling(true);
893 N_BRAC_indexes.pop_back();
894 }
895 type = eSymbolTypeScopeEnd;
896 break;
897
Greg Claytone1a916a2010-07-21 22:12:05 +0000898 case StabDeletedIncludeFile:
899 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900 type = eSymbolTypeHeaderFile;
901 break;
902
903 //----------------------------------------------------------------------
904 // COMM scopes
905 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +0000906 case StabBeginCommon:
907 // N_BCOMM - begin common: name,,NO_SECT,0,0
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 type = eSymbolTypeScopeBegin;
911 N_COMM_indexes.push_back(sym_idx);
912 break;
913
Greg Claytone1a916a2010-07-21 22:12:05 +0000914 case StabEndCommonLocal:
915 // N_ECOML - end common (local name): 0,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000916 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
917 // Fall through
918
Greg Claytone1a916a2010-07-21 22:12:05 +0000919 case StabEndCommon:
920 // N_ECOMM - end common: name,,n_sect,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000921 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
922 // so that we can always skip the entire symbol if we need to navigate
923 // more quickly at the source level when parsing STABS
924 if ( !N_COMM_indexes.empty() )
925 {
926 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
927 symbol_ptr->SetByteSize(sym_idx + 1);
928 symbol_ptr->SetSizeIsSibling(true);
929 N_COMM_indexes.pop_back();
930 }
931 type = eSymbolTypeScopeEnd;
932 break;
933
Greg Claytone1a916a2010-07-21 22:12:05 +0000934 case StabLength:
935 // N_LENG - second stab entry with length information
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000936 type = eSymbolTypeAdditional;
937 break;
938
939 default: break;
940 }
941 }
942 else
943 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000944 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
945 uint8_t n_type = NlistMaskType & nlist.n_type;
946 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000947
948 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name)
949 {
950 type = eSymbolTypeRuntime;
951 }
952 else
953 {
954 switch (n_type)
955 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000956 case NListTypeIndirect: // N_INDR - Fall through
957 case NListTypePreboundUndefined:// N_PBUD - Fall through
958 case NListTypeUndefined: // N_UNDF
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000959 type = eSymbolTypeExtern;
960 break;
961
Greg Claytone1a916a2010-07-21 22:12:05 +0000962 case NListTypeAbsolute: // N_ABS
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963 type = eSymbolTypeAbsolute;
964 break;
965
Greg Claytone1a916a2010-07-21 22:12:05 +0000966 case NListTypeSection: // N_SECT
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000967 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
968
969 assert(symbol_section != NULL);
970 if (TEXT_eh_frame_sectID == nlist.n_sect)
971 {
972 type = eSymbolTypeException;
973 }
974 else
975 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000976 uint32_t section_type = symbol_section->GetAllFlagBits() & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977
978 switch (section_type)
979 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000980 case SectionTypeRegular: break; // regular section
981 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
982 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
983 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
984 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
985 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
986 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
987 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
988 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
989 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
990 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
991 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
992 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
993 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
994 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
995 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
996 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997 default: break;
998 }
999
1000 if (type == eSymbolTypeInvalid)
1001 {
1002 const char *symbol_sect_name = symbol_section->GetName().AsCString();
1003 if (symbol_section->IsDescendant (text_section_sp.get()))
1004 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001005 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1006 SectionAttrUserSelfModifyingCode |
1007 SectionAttrSytemSomeInstructions))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008 type = eSymbolTypeData;
1009 else
1010 type = eSymbolTypeCode;
1011 }
1012 else
1013 if (symbol_section->IsDescendant(data_section_sp.get()))
1014 {
1015 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1016 {
1017 type = eSymbolTypeRuntime;
1018 }
1019 else
1020 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1021 {
1022 type = eSymbolTypeException;
1023 }
1024 else
1025 {
1026 type = eSymbolTypeData;
1027 }
1028 }
1029 else
1030 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1031 {
1032 type = eSymbolTypeTrampoline;
1033 }
1034 else
1035 if (symbol_section->IsDescendant(objc_section_sp.get()))
1036 {
1037 type = eSymbolTypeRuntime;
1038 }
1039 }
1040 }
1041 break;
1042 }
1043 }
1044 }
1045
1046 if (add_nlist)
1047 {
1048 bool symbol_name_is_mangled = false;
1049 if (symbol_name && symbol_name[0] == '_')
1050 {
1051 symbol_name_is_mangled = symbol_name[1] == '_';
1052 symbol_name++; // Skip the leading underscore
1053 }
1054 uint64_t symbol_value = nlist.n_value;
1055 if (symbol_section != NULL)
1056 symbol_value -= symbol_section->GetFileAddress();
1057
1058 sym[sym_idx].SetID (nlist_idx);
1059 sym[sym_idx].SetType (type);
1060 if (symbol_name)
1061 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
1062 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1063 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1064 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1065
1066 ++sym_idx;
1067 }
1068 else
1069 {
1070 sym[sym_idx].Clear();
1071 }
1072
1073 }
1074
1075
1076 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1077 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1078 // such entries by figuring out what the address for the global is by looking up this non-STAB
1079 // entry and copying the value into the debug symbol's value to save us the hassle in the
1080 // debug symbol parser.
1081
1082 Symbol *global_symbol = NULL;
1083 for (nlist_idx = 0;
1084 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType(eSymbolTypeGlobal, nlist_idx)) != NULL;
1085 nlist_idx++)
1086 {
1087 if (global_symbol->GetValue().GetFileAddress() == 0)
1088 {
1089 std::vector<uint32_t> indexes;
1090 if (symtab->AppendSymbolIndexesWithName(global_symbol->GetMangled().GetName(), indexes) > 0)
1091 {
1092 std::vector<uint32_t>::const_iterator pos;
1093 std::vector<uint32_t>::const_iterator end = indexes.end();
1094 for (pos = indexes.begin(); pos != end; ++pos)
1095 {
1096 symbol_ptr = symtab->SymbolAtIndex(*pos);
1097 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1098 {
1099 global_symbol->SetValue(symbol_ptr->GetValue());
1100 break;
1101 }
1102 }
1103 }
1104 }
1105 }
1106 // Now synthesize indirect symbols
1107 if (m_dysymtab.nindirectsyms != 0)
1108 {
1109 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4));
1110
1111 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize())
1112 {
1113 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1114
1115 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1116 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001117 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118 {
1119 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1120 if (symbol_stub_byte_size == 0)
1121 continue;
1122
1123 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1124
1125 if (num_symbol_stubs == 0)
1126 continue;
1127
1128 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
1129 uint32_t stub_sym_id = symtab_load_command.nsyms;
1130 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1131 {
1132 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1133 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1134 uint32_t symbol_stub_offset = symbol_stub_index * 4;
1135 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1136 {
1137 const uint32_t symbol_index = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
1138
1139 Symbol *stub_symbol = symtab->SymbolAtIndex(symbol_index);
1140 if (stub_symbol)
1141 {
1142 Address so_addr(symbol_stub_addr, section_list);
1143
1144 if (stub_symbol->GetType() == eSymbolTypeExtern)
1145 {
1146 // Change the external symbol into a trampoline that makes sense
1147 // These symbols were N_UNDF N_EXT, and are useless to us, so we
1148 // can re-use them so we don't have to make up a synthetic symbol
1149 // for no good reason.
1150 stub_symbol->SetType (eSymbolTypeTrampoline);
1151 stub_symbol->SetExternal (false);
1152 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1153 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1154 }
1155 else
1156 {
1157 // Make a synthetic symbol to describe the trampoline stub
1158 if (sym_idx >= num_syms)
1159 {
1160 sym = symtab->Resize (num_syms + 16);
1161 num_syms = symtab->GetNumSymbols();
1162 }
1163 sym[sym_idx].SetID (stub_sym_id++);
1164 sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1165 sym[sym_idx].SetType (eSymbolTypeTrampoline);
1166 sym[sym_idx].SetIsSynthetic (true);
1167 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1168 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1169 ++sym_idx;
1170 }
1171 }
1172 }
1173 }
1174 }
1175 }
1176 }
1177 }
1178
1179 if (sym_idx != symtab->GetNumSymbols())
1180 symtab->Resize (sym_idx);
1181
1182 return symtab->GetNumSymbols();
1183 }
1184 }
1185 offset = cmd_offset + symtab_load_command.cmdsize;
1186 }
1187 return 0;
1188}
1189
1190
1191void
1192ObjectFileMachO::Dump (Stream *s)
1193{
1194 lldb_private::Mutex::Locker locker(m_mutex);
1195 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1196 s->Indent();
Greg Claytone1a916a2010-07-21 22:12:05 +00001197 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 s->PutCString("ObjectFileMachO64");
1199 else
1200 s->PutCString("ObjectFileMachO32");
1201
Greg Clayton41f92322010-06-11 03:25:34 +00001202 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203
1204 *s << ", file = '" << m_file << "', arch = " << header_arch.AsCString() << "\n";
1205
1206 if (m_sections_ap.get())
1207 m_sections_ap->Dump(s, NULL, true);
1208
1209 if (m_symtab_ap.get())
1210 m_symtab_ap->Dump(s, NULL);
1211}
1212
1213
1214bool
1215ObjectFileMachO::GetUUID (UUID* uuid)
1216{
1217 lldb_private::Mutex::Locker locker(m_mutex);
1218 struct uuid_command load_cmd;
1219 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1220 uint32_t i;
1221 for (i=0; i<m_header.ncmds; ++i)
1222 {
1223 const uint32_t cmd_offset = offset;
1224 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1225 break;
1226
Greg Claytone1a916a2010-07-21 22:12:05 +00001227 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001228 {
1229 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1230 if (uuid_bytes)
1231 {
1232 uuid->SetBytes (uuid_bytes);
1233 return true;
1234 }
1235 return false;
1236 }
1237 offset = cmd_offset + load_cmd.cmdsize;
1238 }
1239 return false;
1240}
1241
1242
1243uint32_t
1244ObjectFileMachO::GetDependentModules (FileSpecList& files)
1245{
1246 lldb_private::Mutex::Locker locker(m_mutex);
1247 struct load_command load_cmd;
1248 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1249 uint32_t count = 0;
1250 uint32_t i;
1251 for (i=0; i<m_header.ncmds; ++i)
1252 {
1253 const uint32_t cmd_offset = offset;
1254 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1255 break;
1256
1257 switch (load_cmd.cmd)
1258 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001259 case LoadCommandDylibLoad:
1260 case LoadCommandDylibLoadWeak:
1261 case LoadCommandDylibReexport:
1262 case LoadCommandDynamicLinkerLoad:
1263 case LoadCommandFixedVMShlibLoad:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001264 {
1265 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1266 const char *path = m_data.PeekCStr(name_offset);
1267 // Skip any path that starts with '@' since these are usually:
1268 // @executable_path/.../file
1269 // @rpath/.../file
1270 if (path && path[0] != '@')
1271 {
1272 FileSpec file_spec(path);
1273 if (files.AppendIfUnique(file_spec))
1274 count++;
1275 }
1276 }
1277 break;
1278
1279 default:
1280 break;
1281 }
1282 offset = cmd_offset + load_cmd.cmdsize;
1283 }
1284 return count;
1285}
1286
1287bool
1288ObjectFileMachO::GetTargetTriple (ConstString &target_triple)
1289{
1290 lldb_private::Mutex::Locker locker(m_mutex);
1291 std::string triple(GetModule()->GetArchitecture().AsCString());
1292 triple += "-apple-darwin";
1293 target_triple.SetCString(triple.c_str());
1294 if (target_triple)
1295 return true;
1296 return false;
1297}
1298
1299
1300//------------------------------------------------------------------
1301// PluginInterface protocol
1302//------------------------------------------------------------------
1303const char *
1304ObjectFileMachO::GetPluginName()
1305{
1306 return "ObjectFileMachO";
1307}
1308
1309const char *
1310ObjectFileMachO::GetShortPluginName()
1311{
1312 return GetPluginNameStatic();
1313}
1314
1315uint32_t
1316ObjectFileMachO::GetPluginVersion()
1317{
1318 return 1;
1319}
1320
1321void
1322ObjectFileMachO::GetPluginCommandHelp (const char *command, Stream *strm)
1323{
1324}
1325
1326Error
1327ObjectFileMachO::ExecutePluginCommand (Args &command, Stream *strm)
1328{
1329 Error error;
1330 error.SetErrorString("No plug-in command are currently supported.");
1331 return error;
1332}
1333
1334Log *
1335ObjectFileMachO::EnablePluginLogging (Stream *strm, Args &command)
1336{
1337 return NULL;
1338}
1339
1340
1341
1342