blob: ccb9105174f948d14f77a522afea9b558d63a6d9 [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);
Jim Ingham5aee1622010-08-09 23:31:02 +0000164
165 if (SetModulesArchitecture (mach_arch))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166 {
167 // Read in all only the load command data
168 DataBufferSP data_sp(m_file.ReadFileContents(m_offset, m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic)));
169 m_data.SetData (data_sp);
170 return true;
171 }
172 }
173 else
174 {
175 memset(&m_header, 0, sizeof(struct mach_header));
176 }
177 return false;
178}
179
180
181ByteOrder
182ObjectFileMachO::GetByteOrder () const
183{
184 lldb_private::Mutex::Locker locker(m_mutex);
185 return m_data.GetByteOrder ();
186}
187
Jim Ingham5aee1622010-08-09 23:31:02 +0000188bool
189ObjectFileMachO::IsExecutable() const
190{
191 return m_header.filetype == HeaderFileTypeExecutable;
192}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193
194size_t
195ObjectFileMachO::GetAddressByteSize () const
196{
197 lldb_private::Mutex::Locker locker(m_mutex);
198 return m_data.GetAddressByteSize ();
199}
200
201
202Symtab *
203ObjectFileMachO::GetSymtab()
204{
205 lldb_private::Mutex::Locker locker(m_mutex);
206 if (m_symtab_ap.get() == NULL)
207 {
208 m_symtab_ap.reset(new Symtab(this));
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000209 ParseSymtab (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210 }
211 return m_symtab_ap.get();
212}
213
214
215SectionList *
216ObjectFileMachO::GetSectionList()
217{
218 lldb_private::Mutex::Locker locker(m_mutex);
219 if (m_sections_ap.get() == NULL)
220 {
221 m_sections_ap.reset(new SectionList());
222 ParseSections();
223 }
224 return m_sections_ap.get();
225}
226
227
228size_t
229ObjectFileMachO::ParseSections ()
230{
231 lldb::user_id_t segID = 0;
232 lldb::user_id_t sectID = 0;
233 struct segment_command_64 load_cmd;
234 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
235 uint32_t i;
236 //bool dump_sections = false;
237 for (i=0; i<m_header.ncmds; ++i)
238 {
239 const uint32_t load_cmd_offset = offset;
240 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
241 break;
242
Greg Claytone1a916a2010-07-21 22:12:05 +0000243 if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244 {
245 if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16))
246 {
247 load_cmd.vmaddr = m_data.GetAddress(&offset);
248 load_cmd.vmsize = m_data.GetAddress(&offset);
249 load_cmd.fileoff = m_data.GetAddress(&offset);
250 load_cmd.filesize = m_data.GetAddress(&offset);
251 if (m_data.GetU32(&offset, &load_cmd.maxprot, 4))
252 {
253 // Keep a list of mach segments around in case we need to
254 // get at data that isn't stored in the abstracted Sections.
255 m_mach_segments.push_back (load_cmd);
256
257 ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname)));
258 // Use a segment ID of the segment index shifted left by 8 so they
259 // never conflict with any of the sections.
260 SectionSP segment_sp;
261 if (segment_name)
262 {
263 segment_sp.reset(new Section (NULL,
264 GetModule(), // Module to which this section belongs
265 ++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
266 segment_name, // Name of this section
267 eSectionTypeContainer, // This section is a container of other sections.
268 load_cmd.vmaddr, // File VM address == addresses as they are found in the object file
269 load_cmd.vmsize, // VM size in bytes of this section
270 load_cmd.fileoff, // Offset to the data for this section in the file
271 load_cmd.filesize, // Size in bytes of this section as found in the the file
272 load_cmd.flags)); // Flags for this section
273
274 m_sections_ap->AddSection(segment_sp);
275 }
276
277 struct section_64 sect64;
278 ::bzero (&sect64, sizeof(sect64));
279 // Push a section into our mach sections for the section at
Greg Claytonf4abd0d2010-10-06 01:26:32 +0000280 // index zero (NListSectionNoSection) if we don't have any
281 // mach sections yet...
282 if (m_mach_sections.empty())
283 m_mach_sections.push_back(sect64);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284 uint32_t segment_sect_idx;
285 const lldb::user_id_t first_segment_sectID = sectID + 1;
286
287
Greg Claytone1a916a2010-07-21 22:12:05 +0000288 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
290 {
291 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
292 break;
293 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
294 break;
295 sect64.addr = m_data.GetAddress(&offset);
296 sect64.size = m_data.GetAddress(&offset);
297
298 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
299 break;
300
301 // Keep a list of mach sections around in case we need to
302 // get at data that isn't stored in the abstracted Sections.
303 m_mach_sections.push_back (sect64);
304
305 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
306 if (!segment_name)
307 {
308 // We have a segment with no name so we need to conjure up
309 // segments that correspond to the section's segname if there
310 // isn't already such a section. If there is such a section,
311 // we resize the section so that it spans all sections.
312 // We also mark these sections as fake so address matches don't
313 // hit if they land in the gaps between the child sections.
314 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
315 segment_sp = m_sections_ap->FindSectionByName (segment_name);
316 if (segment_sp.get())
317 {
318 Section *segment = segment_sp.get();
319 // Grow the section size as needed.
320 const lldb::addr_t sect64_min_addr = sect64.addr;
321 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
322 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
323 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
324 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
325 if (sect64_min_addr >= curr_seg_min_addr)
326 {
327 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
328 // Only grow the section size if needed
329 if (new_seg_byte_size > curr_seg_byte_size)
330 segment->SetByteSize (new_seg_byte_size);
331 }
332 else
333 {
334 // We need to change the base address of the segment and
335 // adjust the child section offsets for all existing children.
336 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
337 segment->Slide(slide_amount, false);
338 segment->GetChildren().Slide (-slide_amount, false);
339 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
340 }
Greg Clayton8d38ac42010-06-28 23:51:11 +0000341
342 // Grow the section size as needed.
343 if (sect64.offset)
344 {
345 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
346 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
347
348 const lldb::addr_t section_min_file_offset = sect64.offset;
349 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
350 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
351 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
352 segment->SetFileOffset (new_file_offset);
353 segment->SetFileSize (new_file_size);
354 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355 }
356 else
357 {
358 // Create a fake section for the section's named segment
359 segment_sp.reset(new Section(segment_sp.get(), // Parent section
360 GetModule(), // Module to which this section belongs
361 ++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
362 segment_name, // Name of this section
363 eSectionTypeContainer, // This section is a container of other sections.
364 sect64.addr, // File VM address == addresses as they are found in the object file
365 sect64.size, // VM size in bytes of this section
366 sect64.offset, // Offset to the data for this section in the file
367 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
368 load_cmd.flags)); // Flags for this section
369 segment_sp->SetIsFake(true);
370 m_sections_ap->AddSection(segment_sp);
371 }
372 }
373 assert (segment_sp.get());
374
Greg Claytone1a916a2010-07-21 22:12:05 +0000375 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 static ConstString g_sect_name_objc_data ("__objc_data");
377 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
378 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
379 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
380 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
381 static ConstString g_sect_name_objc_const ("__objc_const");
382 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
383 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000384
385 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
386 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
387 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
388 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
389 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
390 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
391 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
392 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
393 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
394 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
395 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
396 static ConstString g_sect_name_eh_frame ("__eh_frame");
397
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398 SectionType sect_type = eSectionTypeOther;
399
Greg Clayton4ceb9982010-07-21 22:54:26 +0000400
401 if (section_name == g_sect_name_dwarf_debug_abbrev)
402 sect_type = eSectionTypeDWARFDebugAbbrev;
403 else if (section_name == g_sect_name_dwarf_debug_aranges)
404 sect_type = eSectionTypeDWARFDebugAranges;
405 else if (section_name == g_sect_name_dwarf_debug_frame)
406 sect_type = eSectionTypeDWARFDebugFrame;
407 else if (section_name == g_sect_name_dwarf_debug_info)
408 sect_type = eSectionTypeDWARFDebugInfo;
409 else if (section_name == g_sect_name_dwarf_debug_line)
410 sect_type = eSectionTypeDWARFDebugLine;
411 else if (section_name == g_sect_name_dwarf_debug_loc)
412 sect_type = eSectionTypeDWARFDebugLoc;
413 else if (section_name == g_sect_name_dwarf_debug_macinfo)
414 sect_type = eSectionTypeDWARFDebugMacInfo;
415 else if (section_name == g_sect_name_dwarf_debug_pubnames)
416 sect_type = eSectionTypeDWARFDebugPubNames;
417 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
418 sect_type = eSectionTypeDWARFDebugPubTypes;
419 else if (section_name == g_sect_name_dwarf_debug_ranges)
420 sect_type = eSectionTypeDWARFDebugRanges;
421 else if (section_name == g_sect_name_dwarf_debug_str)
422 sect_type = eSectionTypeDWARFDebugStr;
423 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton4ceb9982010-07-21 22:54:26 +0000427 else if (section_name == g_sect_name_eh_frame)
428 sect_type = eSectionTypeEHFrame;
429 else if (section_name == g_sect_name_cfstring)
430 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431 else if (section_name == g_sect_name_objc_data ||
432 section_name == g_sect_name_objc_classrefs ||
433 section_name == g_sect_name_objc_superrefs ||
434 section_name == g_sect_name_objc_const ||
435 section_name == g_sect_name_objc_classlist)
436 {
437 sect_type = eSectionTypeDataPointers;
438 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439
440 if (sect_type == eSectionTypeOther)
441 {
442 switch (mach_sect_type)
443 {
444 // TODO: categorize sections by other flags for regular sections
Greg Claytone1a916a2010-07-21 22:12:05 +0000445 case SectionTypeRegular: sect_type = eSectionTypeOther; break;
446 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
447 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
448 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
449 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
450 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
451 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
452 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
453 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
454 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
455 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
456 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
457 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
458 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
459 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
460 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
461 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 default: break;
463 }
464 }
465
466 SectionSP section_sp(new Section(segment_sp.get(),
467 GetModule(),
468 ++sectID,
469 section_name,
470 sect_type,
471 sect64.addr - segment_sp->GetFileAddress(),
472 sect64.size,
473 sect64.offset,
474 sect64.offset == 0 ? 0 : sect64.size,
475 sect64.flags));
476 segment_sp->GetChildren().AddSection(section_sp);
477
478 if (segment_sp->IsFake())
479 {
480 segment_sp.reset();
481 segment_name.Clear();
482 }
483 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000484 if (m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485 {
486 if (first_segment_sectID <= sectID)
487 {
488 lldb::user_id_t sect_uid;
489 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
490 {
491 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
492 SectionSP next_section_sp;
493 if (sect_uid + 1 <= sectID)
494 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
495
496 if (curr_section_sp.get())
497 {
498 if (curr_section_sp->GetByteSize() == 0)
499 {
500 if (next_section_sp.get() != NULL)
501 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
502 else
503 curr_section_sp->SetByteSize ( load_cmd.vmsize );
504 }
505 }
506 }
507 }
508 }
509 }
510 }
511 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000512 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 {
514 m_dysymtab.cmd = load_cmd.cmd;
515 m_dysymtab.cmdsize = load_cmd.cmdsize;
516 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
517 }
518
519 offset = load_cmd_offset + load_cmd.cmdsize;
520 }
521// if (dump_sections)
522// {
523// StreamFile s(stdout);
524// m_sections_ap->Dump(&s, true);
525// }
526 return sectID; // Return the number of sections we registered with the module
527}
528
529class MachSymtabSectionInfo
530{
531public:
532
533 MachSymtabSectionInfo (SectionList *section_list) :
534 m_section_list (section_list),
535 m_section_infos()
536 {
537 // Get the number of sections down to a depth of 1 to include
538 // all segments and their sections, but no other sections that
539 // may be added for debug map or
540 m_section_infos.resize(section_list->GetNumSections(1));
541 }
542
543
544 Section *
545 GetSection (uint8_t n_sect, addr_t file_addr)
546 {
547 if (n_sect == 0)
548 return NULL;
549 if (n_sect < m_section_infos.size())
550 {
551 if (m_section_infos[n_sect].section == NULL)
552 {
553 Section *section = m_section_list->FindSectionByID (n_sect).get();
554 m_section_infos[n_sect].section = section;
555 assert (section != NULL);
556 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
557 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
558 }
559 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
560 return m_section_infos[n_sect].section;
561 }
562 return m_section_list->FindSectionContainingFileAddress(file_addr).get();
563 }
564
565protected:
566 struct SectionInfo
567 {
568 SectionInfo () :
569 vm_range(),
570 section (NULL)
571 {
572 }
573
574 VMRange vm_range;
575 Section *section;
576 };
577 SectionList *m_section_list;
578 std::vector<SectionInfo> m_section_infos;
579};
580
581
582
583size_t
584ObjectFileMachO::ParseSymtab (bool minimize)
585{
586 Timer scoped_timer(__PRETTY_FUNCTION__,
587 "ObjectFileMachO::ParseSymtab () module = %s",
588 m_file.GetFilename().AsCString(""));
589 struct symtab_command symtab_load_command;
590 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
591 uint32_t i;
592 for (i=0; i<m_header.ncmds; ++i)
593 {
594 const uint32_t cmd_offset = offset;
595 // Read in the load command and load command size
596 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
597 break;
598 // Watch for the symbol table load command
Greg Claytone1a916a2010-07-21 22:12:05 +0000599 if (symtab_load_command.cmd == LoadCommandSymtab)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 {
601 // Read in the rest of the symtab load command
Jason Molendaea84e762010-07-06 22:38:03 +0000602 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603 {
604 Symtab *symtab = m_symtab_ap.get();
605 SectionList *section_list = GetSectionList();
606 assert(section_list);
607 const size_t addr_size = m_data.GetAddressByteSize();
608 const ByteOrder endian = m_data.GetByteOrder();
609 bool bit_width_32 = addr_size == 4;
610 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
611
612 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size));
613 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize));
614
615 const char *strtab_data = (const char *)strtab_data_sp->GetBytes();
616// DataExtractor symtab_data(symtab_data_sp, endian, addr_size);
617// DataExtractor strtab_data(strtab_data_sp, endian, addr_size);
618
619 static ConstString g_segment_name_TEXT ("__TEXT");
620 static ConstString g_segment_name_DATA ("__DATA");
621 static ConstString g_segment_name_OBJC ("__OBJC");
622 static ConstString g_section_name_eh_frame ("__eh_frame");
623 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
624 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
625 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
626 SectionSP eh_frame_section_sp;
627 if (text_section_sp.get())
628 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
629 else
630 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
631
Greg Claytone1a916a2010-07-21 22:12:05 +0000632 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 //uint32_t symtab_offset = 0;
634 const uint8_t* nlist_data = symtab_data_sp->GetBytes();
635 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms);
636
637
638 if (endian != eByteOrderHost)
639 {
640 // ...
641 assert (!"UNIMPLEMENTED: Swap all nlist entries");
642 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000643 uint32_t N_SO_index = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644
645 MachSymtabSectionInfo section_info (section_list);
646 std::vector<uint32_t> N_FUN_indexes;
647 std::vector<uint32_t> N_NSYM_indexes;
648 std::vector<uint32_t> N_INCL_indexes;
649 std::vector<uint32_t> N_BRAC_indexes;
650 std::vector<uint32_t> N_COMM_indexes;
Greg Clayton928d8292010-09-08 16:38:06 +0000651 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000652 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Clayton928d8292010-09-08 16:38:06 +0000653 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
654 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000655 // Any symbols that get merged into another will get an entry
656 // in this map so we know
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000657 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 uint32_t nlist_idx = 0;
659 Symbol *symbol_ptr = NULL;
660
661 uint32_t sym_idx = 0;
662 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
663 uint32_t num_syms = symtab->GetNumSymbols();
664
665 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
666 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
667 {
668 struct nlist_64 nlist;
669 if (bit_width_32)
670 {
671 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size));
Greg Claytone1a916a2010-07-21 22:12:05 +0000672 nlist.n_strx = nlist32_ptr->n_strx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 nlist.n_type = nlist32_ptr->n_type;
674 nlist.n_sect = nlist32_ptr->n_sect;
675 nlist.n_desc = nlist32_ptr->n_desc;
676 nlist.n_value = nlist32_ptr->n_value;
677 }
678 else
679 {
680 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size)));
681 }
682
683 SymbolType type = eSymbolTypeInvalid;
Greg Claytone1a916a2010-07-21 22:12:05 +0000684 const char* symbol_name = &strtab_data[nlist.n_strx];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 if (symbol_name[0] == '\0')
686 symbol_name = NULL;
687 Section* symbol_section = NULL;
688 bool add_nlist = true;
Greg Claytone1a916a2010-07-21 22:12:05 +0000689 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690
691 assert (sym_idx < num_syms);
692
693 sym[sym_idx].SetDebug (is_debug);
694
695 if (is_debug)
696 {
697 switch (nlist.n_type)
698 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000699 case StabGlobalSymbol:
700 // N_GSYM -- global symbol: name,,NO_SECT,type,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 // Sometimes the N_GSYM value contains the address.
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000702 sym[sym_idx].SetExternal(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 if (nlist.n_value != 0)
704 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000705 type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706 break;
707
Greg Claytone1a916a2010-07-21 22:12:05 +0000708 case StabFunctionName:
709 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000710 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711 break;
712
Greg Claytone1a916a2010-07-21 22:12:05 +0000713 case StabFunction:
714 // N_FUN -- procedure: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 if (symbol_name)
716 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000717 type = eSymbolTypeCode;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton928d8292010-09-08 16:38:06 +0000719
720 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721 // We use the current number of symbols in the symbol table in lieu of
722 // using nlist_idx in case we ever start trimming entries out
723 N_FUN_indexes.push_back(sym_idx);
724 }
725 else
726 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000727 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728
729 if ( !N_FUN_indexes.empty() )
730 {
731 // Copy the size of the function into the original STAB entry so we don't have
732 // to hunt for it later
733 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
734 N_FUN_indexes.pop_back();
Jason Molendaea84e762010-07-06 22:38:03 +0000735 // We don't really need the end function STAB as it contains the size which
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736 // we already placed with the original symbol, so don't add it if we want a
737 // minimal symbol table
738 if (minimize)
739 add_nlist = false;
740 }
741 }
742 break;
743
Greg Claytone1a916a2010-07-21 22:12:05 +0000744 case StabStaticSymbol:
745 // N_STSYM -- static symbol: name,,n_sect,type,address
Greg Clayton928d8292010-09-08 16:38:06 +0000746 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000748 type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749 break;
750
Greg Claytone1a916a2010-07-21 22:12:05 +0000751 case StabLocalCommon:
752 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
754 type = eSymbolTypeCommonBlock;
755 break;
756
Greg Claytone1a916a2010-07-21 22:12:05 +0000757 case StabBeginSymbol:
758 // N_BNSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759 // We use the current number of symbols in the symbol table in lieu of
760 // using nlist_idx in case we ever start trimming entries out
761 if (minimize)
762 {
763 // Skip these if we want minimal symbol tables
764 add_nlist = false;
765 }
766 else
767 {
768 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
769 N_NSYM_indexes.push_back(sym_idx);
770 type = eSymbolTypeScopeBegin;
771 }
772 break;
773
Greg Claytone1a916a2010-07-21 22:12:05 +0000774 case StabEndSymbol:
775 // N_ENSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
777 // so that we can always skip the entire symbol if we need to navigate
778 // more quickly at the source level when parsing STABS
779 if (minimize)
780 {
781 // Skip these if we want minimal symbol tables
782 add_nlist = false;
783 }
784 else
785 {
786 if ( !N_NSYM_indexes.empty() )
787 {
788 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
789 symbol_ptr->SetByteSize(sym_idx + 1);
790 symbol_ptr->SetSizeIsSibling(true);
791 N_NSYM_indexes.pop_back();
792 }
793 type = eSymbolTypeScopeEnd;
794 }
795 break;
796
797
Greg Claytone1a916a2010-07-21 22:12:05 +0000798 case StabSourceFileOptions:
799 // N_OPT - emitted with gcc2_compiled and in gcc source
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800 type = eSymbolTypeCompiler;
801 break;
802
Greg Claytone1a916a2010-07-21 22:12:05 +0000803 case StabRegisterSymbol:
804 // N_RSYM - register sym: name,,NO_SECT,type,register
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805 type = eSymbolTypeVariable;
806 break;
807
Greg Claytone1a916a2010-07-21 22:12:05 +0000808 case StabSourceLine:
809 // N_SLINE - src line: 0,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
811 type = eSymbolTypeLineEntry;
812 break;
813
Greg Claytone1a916a2010-07-21 22:12:05 +0000814 case StabStructureType:
815 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 type = eSymbolTypeVariableType;
817 break;
818
Greg Claytone1a916a2010-07-21 22:12:05 +0000819 case StabSourceFileName:
820 // N_SO - source file name
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000821 type = eSymbolTypeSourceFile;
822 if (symbol_name == NULL)
823 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000824 if (minimize)
825 add_nlist = false;
826 if (N_SO_index != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000827 {
828 // Set the size of the N_SO to the terminating index of this N_SO
829 // so that we can always skip the entire N_SO if we need to navigate
830 // more quickly at the source level when parsing STABS
831 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000832 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833 symbol_ptr->SetSizeIsSibling(true);
834 }
835 N_NSYM_indexes.clear();
836 N_INCL_indexes.clear();
837 N_BRAC_indexes.clear();
838 N_COMM_indexes.clear();
839 N_FUN_indexes.clear();
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000840 N_SO_index = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000842 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000843 {
844 // We use the current number of symbols in the symbol table in lieu of
845 // using nlist_idx in case we ever start trimming entries out
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000846 if (symbol_name[0] == '/')
847 N_SO_index = sym_idx;
848 else if (minimize && (N_SO_index == sym_idx - 1))
849 {
850 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
851 if (so_path && so_path[0])
852 {
853 std::string full_so_path (so_path);
854 if (*full_so_path.rbegin() != '/')
855 full_so_path += '/';
856 full_so_path += symbol_name;
857 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
858 add_nlist = false;
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000859 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000860 }
861 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000863
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000864 break;
865
Greg Claytone1a916a2010-07-21 22:12:05 +0000866 case StabObjectFileName:
867 // N_OSO - object file name: name,,0,0,st_mtime
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868 type = eSymbolTypeObjectFile;
869 break;
870
Greg Claytone1a916a2010-07-21 22:12:05 +0000871 case StabLocalSymbol:
872 // N_LSYM - local sym: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873 type = eSymbolTypeLocal;
874 break;
875
876 //----------------------------------------------------------------------
877 // INCL scopes
878 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +0000879 case StabBeginIncludeFileName:
880 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881 // We use the current number of symbols in the symbol table in lieu of
882 // using nlist_idx in case we ever start trimming entries out
883 N_INCL_indexes.push_back(sym_idx);
884 type = eSymbolTypeScopeBegin;
885 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886
Greg Claytone1a916a2010-07-21 22:12:05 +0000887 case StabEndIncludeFile:
888 // N_EINCL - include file end: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889 // Set the size of the N_BINCL to the terminating index of this N_EINCL
890 // so that we can always skip the entire symbol if we need to navigate
891 // more quickly at the source level when parsing STABS
892 if ( !N_INCL_indexes.empty() )
893 {
894 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
895 symbol_ptr->SetByteSize(sym_idx + 1);
896 symbol_ptr->SetSizeIsSibling(true);
897 N_INCL_indexes.pop_back();
898 }
899 type = eSymbolTypeScopeEnd;
900 break;
901
Greg Claytone1a916a2010-07-21 22:12:05 +0000902 case StabIncludeFileName:
903 // N_SOL - #included file name: name,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904 type = eSymbolTypeHeaderFile;
Greg Clayton49bd1c82010-09-07 17:36:17 +0000905
906 // We currently don't use the header files on darwin
907 if (minimize)
908 add_nlist = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000909 break;
910
Greg Claytone1a916a2010-07-21 22:12:05 +0000911 case StabCompilerParameters:
912 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913 type = eSymbolTypeCompiler;
914 break;
915
Greg Claytone1a916a2010-07-21 22:12:05 +0000916 case StabCompilerVersion:
917 // N_VERSION - compiler version: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918 type = eSymbolTypeCompiler;
919 break;
920
Greg Claytone1a916a2010-07-21 22:12:05 +0000921 case StabCompilerOptLevel:
922 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000923 type = eSymbolTypeCompiler;
924 break;
925
Greg Claytone1a916a2010-07-21 22:12:05 +0000926 case StabParameter:
927 // N_PSYM - parameter: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928 type = eSymbolTypeVariable;
929 break;
930
Greg Claytone1a916a2010-07-21 22:12:05 +0000931 case StabAlternateEntry:
932 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
934 type = eSymbolTypeLineEntry;
935 break;
936
937 //----------------------------------------------------------------------
938 // Left and Right Braces
939 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +0000940 case StabLeftBracket:
941 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942 // We use the current number of symbols in the symbol table in lieu of
943 // using nlist_idx in case we ever start trimming entries out
944 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
945 N_BRAC_indexes.push_back(sym_idx);
946 type = eSymbolTypeScopeBegin;
947 break;
948
Greg Claytone1a916a2010-07-21 22:12:05 +0000949 case StabRightBracket:
950 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
952 // so that we can always skip the entire symbol if we need to navigate
953 // more quickly at the source level when parsing STABS
954 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
955 if ( !N_BRAC_indexes.empty() )
956 {
957 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
958 symbol_ptr->SetByteSize(sym_idx + 1);
959 symbol_ptr->SetSizeIsSibling(true);
960 N_BRAC_indexes.pop_back();
961 }
962 type = eSymbolTypeScopeEnd;
963 break;
964
Greg Claytone1a916a2010-07-21 22:12:05 +0000965 case StabDeletedIncludeFile:
966 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000967 type = eSymbolTypeHeaderFile;
968 break;
969
970 //----------------------------------------------------------------------
971 // COMM scopes
972 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +0000973 case StabBeginCommon:
974 // N_BCOMM - begin common: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000975 // We use the current number of symbols in the symbol table in lieu of
976 // using nlist_idx in case we ever start trimming entries out
977 type = eSymbolTypeScopeBegin;
978 N_COMM_indexes.push_back(sym_idx);
979 break;
980
Greg Claytone1a916a2010-07-21 22:12:05 +0000981 case StabEndCommonLocal:
982 // N_ECOML - end common (local name): 0,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
984 // Fall through
985
Greg Claytone1a916a2010-07-21 22:12:05 +0000986 case StabEndCommon:
987 // N_ECOMM - end common: name,,n_sect,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
989 // so that we can always skip the entire symbol if we need to navigate
990 // more quickly at the source level when parsing STABS
991 if ( !N_COMM_indexes.empty() )
992 {
993 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
994 symbol_ptr->SetByteSize(sym_idx + 1);
995 symbol_ptr->SetSizeIsSibling(true);
996 N_COMM_indexes.pop_back();
997 }
998 type = eSymbolTypeScopeEnd;
999 break;
1000
Greg Claytone1a916a2010-07-21 22:12:05 +00001001 case StabLength:
1002 // N_LENG - second stab entry with length information
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003 type = eSymbolTypeAdditional;
1004 break;
1005
1006 default: break;
1007 }
1008 }
1009 else
1010 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001011 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
1012 uint8_t n_type = NlistMaskType & nlist.n_type;
1013 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001014
1015 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name)
1016 {
1017 type = eSymbolTypeRuntime;
1018 }
1019 else
1020 {
1021 switch (n_type)
1022 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001023 case NListTypeIndirect: // N_INDR - Fall through
1024 case NListTypePreboundUndefined:// N_PBUD - Fall through
1025 case NListTypeUndefined: // N_UNDF
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026 type = eSymbolTypeExtern;
1027 break;
1028
Greg Claytone1a916a2010-07-21 22:12:05 +00001029 case NListTypeAbsolute: // N_ABS
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030 type = eSymbolTypeAbsolute;
1031 break;
1032
Greg Claytone1a916a2010-07-21 22:12:05 +00001033 case NListTypeSection: // N_SECT
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001034 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1035
1036 assert(symbol_section != NULL);
1037 if (TEXT_eh_frame_sectID == nlist.n_sect)
1038 {
1039 type = eSymbolTypeException;
1040 }
1041 else
1042 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001043 uint32_t section_type = symbol_section->GetAllFlagBits() & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001044
1045 switch (section_type)
1046 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001047 case SectionTypeRegular: break; // regular section
1048 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
1049 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
1050 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
1051 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
1052 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1053 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1054 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1055 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1056 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
1057 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
1058 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
1059 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
1060 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
1061 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
1062 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
1063 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064 default: break;
1065 }
1066
1067 if (type == eSymbolTypeInvalid)
1068 {
1069 const char *symbol_sect_name = symbol_section->GetName().AsCString();
1070 if (symbol_section->IsDescendant (text_section_sp.get()))
1071 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001072 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1073 SectionAttrUserSelfModifyingCode |
1074 SectionAttrSytemSomeInstructions))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075 type = eSymbolTypeData;
1076 else
1077 type = eSymbolTypeCode;
1078 }
1079 else
1080 if (symbol_section->IsDescendant(data_section_sp.get()))
1081 {
1082 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1083 {
1084 type = eSymbolTypeRuntime;
1085 }
1086 else
1087 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1088 {
1089 type = eSymbolTypeException;
1090 }
1091 else
1092 {
1093 type = eSymbolTypeData;
1094 }
1095 }
1096 else
1097 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1098 {
1099 type = eSymbolTypeTrampoline;
1100 }
1101 else
1102 if (symbol_section->IsDescendant(objc_section_sp.get()))
1103 {
1104 type = eSymbolTypeRuntime;
1105 }
1106 }
1107 }
1108 break;
Greg Clayton928d8292010-09-08 16:38:06 +00001109 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110 }
1111 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001112 if (add_nlist)
1113 {
1114 bool symbol_name_is_mangled = false;
1115 if (symbol_name && symbol_name[0] == '_')
1116 {
1117 symbol_name_is_mangled = symbol_name[1] == '_';
1118 symbol_name++; // Skip the leading underscore
1119 }
1120 uint64_t symbol_value = nlist.n_value;
Greg Clayton928d8292010-09-08 16:38:06 +00001121
1122 if (symbol_name)
1123 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001124 if (is_debug == false)
Greg Clayton928d8292010-09-08 16:38:06 +00001125 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001126 if (type == eSymbolTypeCode)
Greg Clayton928d8292010-09-08 16:38:06 +00001127 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001128 // See if we can find a N_FUN entry for any code symbols.
1129 // If we do find a match, and the name matches, then we
1130 // can merge the two into just the function symbol to avoid
1131 // duplicate entries in the symbol table
1132 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1133 if (pos != N_FUN_addr_to_sym_idx.end())
Greg Clayton928d8292010-09-08 16:38:06 +00001134 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001135 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1136 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1137 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001138 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001139 // We just need the flags from the linker symbol, so put these flags
1140 // into the N_FUN flags to avoid duplicate symbols in the symbol table
1141 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1142 sym[sym_idx].Clear();
1143 continue;
1144 }
Greg Clayton928d8292010-09-08 16:38:06 +00001145 }
1146 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001147 else if (type == eSymbolTypeData)
Greg Clayton928d8292010-09-08 16:38:06 +00001148 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001149 // See if we can find a N_STSYM entry for any data symbols.
1150 // If we do find a match, and the name matches, then we
1151 // can merge the two into just the Static symbol to avoid
1152 // duplicate entries in the symbol table
1153 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
1154 if (pos != N_STSYM_addr_to_sym_idx.end())
Greg Clayton928d8292010-09-08 16:38:06 +00001155 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001156 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1157 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1158 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001159 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001160 // We just need the flags from the linker symbol, so put these flags
1161 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
1162 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1163 sym[sym_idx].Clear();
1164 continue;
1165 }
Greg Clayton928d8292010-09-08 16:38:06 +00001166 }
1167 }
1168 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169 if (symbol_section != NULL)
1170 symbol_value -= symbol_section->GetFileAddress();
1171
1172 sym[sym_idx].SetID (nlist_idx);
1173 sym[sym_idx].SetType (type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1175 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1176 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1177
1178 ++sym_idx;
1179 }
1180 else
1181 {
1182 sym[sym_idx].Clear();
1183 }
1184
1185 }
1186
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1188 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1189 // such entries by figuring out what the address for the global is by looking up this non-STAB
1190 // entry and copying the value into the debug symbol's value to save us the hassle in the
1191 // debug symbol parser.
1192
1193 Symbol *global_symbol = NULL;
1194 for (nlist_idx = 0;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001195 nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196 nlist_idx++)
1197 {
1198 if (global_symbol->GetValue().GetFileAddress() == 0)
1199 {
1200 std::vector<uint32_t> indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001201 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202 {
1203 std::vector<uint32_t>::const_iterator pos;
1204 std::vector<uint32_t>::const_iterator end = indexes.end();
1205 for (pos = indexes.begin(); pos != end; ++pos)
1206 {
1207 symbol_ptr = symtab->SymbolAtIndex(*pos);
1208 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1209 {
1210 global_symbol->SetValue(symbol_ptr->GetValue());
1211 break;
1212 }
1213 }
1214 }
1215 }
1216 }
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001217
1218 // Trim our symbols down to just what we ended up with after
1219 // removing any symbols.
1220 if (sym_idx < num_syms)
1221 {
1222 num_syms = sym_idx;
1223 sym = symtab->Resize (num_syms);
1224 }
1225
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226 // Now synthesize indirect symbols
1227 if (m_dysymtab.nindirectsyms != 0)
1228 {
1229 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4));
1230
1231 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize())
1232 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001233 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001234 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1235
1236 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1237 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001238 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239 {
1240 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1241 if (symbol_stub_byte_size == 0)
1242 continue;
1243
1244 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1245
1246 if (num_symbol_stubs == 0)
1247 continue;
1248
1249 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001250 uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1252 {
1253 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1254 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1255 uint32_t symbol_stub_offset = symbol_stub_index * 4;
1256 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1257 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001258 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Greg Claytonf4abd0d2010-10-06 01:26:32 +00001259 if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal))
1260 continue;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001261
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001262 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
1263 Symbol *stub_symbol = NULL;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001264 if (index_pos != end_index_pos)
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001265 {
1266 // We have a remapping from the original nlist index to
1267 // a current symbol index, so just look this up by index
1268 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
1269 }
1270 else
1271 {
1272 // We need to lookup a symbol using the original nlist
1273 // symbol index since this index is coming from the
1274 // S_SYMBOL_STUBS
1275 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
1276 }
Greg Clayton49bd1c82010-09-07 17:36:17 +00001277
1278 assert (stub_symbol);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001279 if (stub_symbol)
1280 {
1281 Address so_addr(symbol_stub_addr, section_list);
1282
1283 if (stub_symbol->GetType() == eSymbolTypeExtern)
1284 {
1285 // Change the external symbol into a trampoline that makes sense
1286 // These symbols were N_UNDF N_EXT, and are useless to us, so we
1287 // can re-use them so we don't have to make up a synthetic symbol
1288 // for no good reason.
1289 stub_symbol->SetType (eSymbolTypeTrampoline);
1290 stub_symbol->SetExternal (false);
1291 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1292 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1293 }
1294 else
1295 {
1296 // Make a synthetic symbol to describe the trampoline stub
1297 if (sym_idx >= num_syms)
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001298 sym = symtab->Resize (++num_syms);
1299 sym[sym_idx].SetID (synthetic_stub_sym_id++);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001300 sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1301 sym[sym_idx].SetType (eSymbolTypeTrampoline);
1302 sym[sym_idx].SetIsSynthetic (true);
1303 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1304 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1305 ++sym_idx;
1306 }
1307 }
1308 }
1309 }
1310 }
1311 }
1312 }
1313 }
1314
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001315 return symtab->GetNumSymbols();
1316 }
1317 }
1318 offset = cmd_offset + symtab_load_command.cmdsize;
1319 }
1320 return 0;
1321}
1322
1323
1324void
1325ObjectFileMachO::Dump (Stream *s)
1326{
1327 lldb_private::Mutex::Locker locker(m_mutex);
1328 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1329 s->Indent();
Greg Claytone1a916a2010-07-21 22:12:05 +00001330 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331 s->PutCString("ObjectFileMachO64");
1332 else
1333 s->PutCString("ObjectFileMachO32");
1334
Greg Clayton41f92322010-06-11 03:25:34 +00001335 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001336
1337 *s << ", file = '" << m_file << "', arch = " << header_arch.AsCString() << "\n";
1338
1339 if (m_sections_ap.get())
1340 m_sections_ap->Dump(s, NULL, true);
1341
1342 if (m_symtab_ap.get())
1343 m_symtab_ap->Dump(s, NULL);
1344}
1345
1346
1347bool
1348ObjectFileMachO::GetUUID (UUID* uuid)
1349{
1350 lldb_private::Mutex::Locker locker(m_mutex);
1351 struct uuid_command load_cmd;
1352 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1353 uint32_t i;
1354 for (i=0; i<m_header.ncmds; ++i)
1355 {
1356 const uint32_t cmd_offset = offset;
1357 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1358 break;
1359
Greg Claytone1a916a2010-07-21 22:12:05 +00001360 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361 {
1362 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1363 if (uuid_bytes)
1364 {
1365 uuid->SetBytes (uuid_bytes);
1366 return true;
1367 }
1368 return false;
1369 }
1370 offset = cmd_offset + load_cmd.cmdsize;
1371 }
1372 return false;
1373}
1374
1375
1376uint32_t
1377ObjectFileMachO::GetDependentModules (FileSpecList& files)
1378{
1379 lldb_private::Mutex::Locker locker(m_mutex);
1380 struct load_command load_cmd;
1381 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1382 uint32_t count = 0;
1383 uint32_t i;
1384 for (i=0; i<m_header.ncmds; ++i)
1385 {
1386 const uint32_t cmd_offset = offset;
1387 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1388 break;
1389
1390 switch (load_cmd.cmd)
1391 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001392 case LoadCommandDylibLoad:
1393 case LoadCommandDylibLoadWeak:
1394 case LoadCommandDylibReexport:
1395 case LoadCommandDynamicLinkerLoad:
1396 case LoadCommandFixedVMShlibLoad:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397 {
1398 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1399 const char *path = m_data.PeekCStr(name_offset);
1400 // Skip any path that starts with '@' since these are usually:
1401 // @executable_path/.../file
1402 // @rpath/.../file
1403 if (path && path[0] != '@')
1404 {
1405 FileSpec file_spec(path);
1406 if (files.AppendIfUnique(file_spec))
1407 count++;
1408 }
1409 }
1410 break;
1411
1412 default:
1413 break;
1414 }
1415 offset = cmd_offset + load_cmd.cmdsize;
1416 }
1417 return count;
1418}
1419
1420bool
1421ObjectFileMachO::GetTargetTriple (ConstString &target_triple)
1422{
1423 lldb_private::Mutex::Locker locker(m_mutex);
1424 std::string triple(GetModule()->GetArchitecture().AsCString());
1425 triple += "-apple-darwin";
1426 target_triple.SetCString(triple.c_str());
1427 if (target_triple)
1428 return true;
1429 return false;
1430}
1431
1432
1433//------------------------------------------------------------------
1434// PluginInterface protocol
1435//------------------------------------------------------------------
1436const char *
1437ObjectFileMachO::GetPluginName()
1438{
1439 return "ObjectFileMachO";
1440}
1441
1442const char *
1443ObjectFileMachO::GetShortPluginName()
1444{
1445 return GetPluginNameStatic();
1446}
1447
1448uint32_t
1449ObjectFileMachO::GetPluginVersion()
1450{
1451 return 1;
1452}
1453
1454void
1455ObjectFileMachO::GetPluginCommandHelp (const char *command, Stream *strm)
1456{
1457}
1458
1459Error
1460ObjectFileMachO::ExecutePluginCommand (Args &command, Stream *strm)
1461{
1462 Error error;
1463 error.SetErrorString("No plug-in command are currently supported.");
1464 return error;
1465}
1466
1467Log *
1468ObjectFileMachO::EnablePluginLogging (Stream *strm, Args &command)
1469{
1470 return NULL;
1471}
1472
1473
1474
1475