blob: f1c7a182180052e317102307757536f569633482 [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 Claytone1a916a2010-07-21 22:12:05 +0000280 // index zero (NListSectionNoSection)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 m_mach_sections.push_back(sect64);
282 uint32_t segment_sect_idx;
283 const lldb::user_id_t first_segment_sectID = sectID + 1;
284
285
Greg Claytone1a916a2010-07-21 22:12:05 +0000286 const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287 for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx)
288 {
289 if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL)
290 break;
291 if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL)
292 break;
293 sect64.addr = m_data.GetAddress(&offset);
294 sect64.size = m_data.GetAddress(&offset);
295
296 if (m_data.GetU32(&offset, &sect64.offset, num_u32s) == NULL)
297 break;
298
299 // Keep a list of mach sections around in case we need to
300 // get at data that isn't stored in the abstracted Sections.
301 m_mach_sections.push_back (sect64);
302
303 ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname)));
304 if (!segment_name)
305 {
306 // We have a segment with no name so we need to conjure up
307 // segments that correspond to the section's segname if there
308 // isn't already such a section. If there is such a section,
309 // we resize the section so that it spans all sections.
310 // We also mark these sections as fake so address matches don't
311 // hit if they land in the gaps between the child sections.
312 segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname));
313 segment_sp = m_sections_ap->FindSectionByName (segment_name);
314 if (segment_sp.get())
315 {
316 Section *segment = segment_sp.get();
317 // Grow the section size as needed.
318 const lldb::addr_t sect64_min_addr = sect64.addr;
319 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
320 const lldb::addr_t curr_seg_byte_size = segment->GetByteSize();
321 const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress();
322 const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size;
323 if (sect64_min_addr >= curr_seg_min_addr)
324 {
325 const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr;
326 // Only grow the section size if needed
327 if (new_seg_byte_size > curr_seg_byte_size)
328 segment->SetByteSize (new_seg_byte_size);
329 }
330 else
331 {
332 // We need to change the base address of the segment and
333 // adjust the child section offsets for all existing children.
334 const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr;
335 segment->Slide(slide_amount, false);
336 segment->GetChildren().Slide (-slide_amount, false);
337 segment->SetByteSize (curr_seg_max_addr - sect64_min_addr);
338 }
Greg Clayton8d38ac42010-06-28 23:51:11 +0000339
340 // Grow the section size as needed.
341 if (sect64.offset)
342 {
343 const lldb::addr_t segment_min_file_offset = segment->GetFileOffset();
344 const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize();
345
346 const lldb::addr_t section_min_file_offset = sect64.offset;
347 const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size;
348 const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset);
349 const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset;
350 segment->SetFileOffset (new_file_offset);
351 segment->SetFileSize (new_file_size);
352 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353 }
354 else
355 {
356 // Create a fake section for the section's named segment
357 segment_sp.reset(new Section(segment_sp.get(), // Parent section
358 GetModule(), // Module to which this section belongs
359 ++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
360 segment_name, // Name of this section
361 eSectionTypeContainer, // This section is a container of other sections.
362 sect64.addr, // File VM address == addresses as they are found in the object file
363 sect64.size, // VM size in bytes of this section
364 sect64.offset, // Offset to the data for this section in the file
365 sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file
366 load_cmd.flags)); // Flags for this section
367 segment_sp->SetIsFake(true);
368 m_sections_ap->AddSection(segment_sp);
369 }
370 }
371 assert (segment_sp.get());
372
Greg Claytone1a916a2010-07-21 22:12:05 +0000373 uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 static ConstString g_sect_name_objc_data ("__objc_data");
375 static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs");
376 static ConstString g_sect_name_objc_selrefs ("__objc_selrefs");
377 static ConstString g_sect_name_objc_classrefs ("__objc_classrefs");
378 static ConstString g_sect_name_objc_superrefs ("__objc_superrefs");
379 static ConstString g_sect_name_objc_const ("__objc_const");
380 static ConstString g_sect_name_objc_classlist ("__objc_classlist");
381 static ConstString g_sect_name_cfstring ("__cfstring");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000382
383 static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev");
384 static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges");
385 static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame");
386 static ConstString g_sect_name_dwarf_debug_info ("__debug_info");
387 static ConstString g_sect_name_dwarf_debug_line ("__debug_line");
388 static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc");
389 static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo");
390 static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames");
391 static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes");
392 static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges");
393 static ConstString g_sect_name_dwarf_debug_str ("__debug_str");
394 static ConstString g_sect_name_eh_frame ("__eh_frame");
395
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396 SectionType sect_type = eSectionTypeOther;
397
Greg Clayton4ceb9982010-07-21 22:54:26 +0000398
399 if (section_name == g_sect_name_dwarf_debug_abbrev)
400 sect_type = eSectionTypeDWARFDebugAbbrev;
401 else if (section_name == g_sect_name_dwarf_debug_aranges)
402 sect_type = eSectionTypeDWARFDebugAranges;
403 else if (section_name == g_sect_name_dwarf_debug_frame)
404 sect_type = eSectionTypeDWARFDebugFrame;
405 else if (section_name == g_sect_name_dwarf_debug_info)
406 sect_type = eSectionTypeDWARFDebugInfo;
407 else if (section_name == g_sect_name_dwarf_debug_line)
408 sect_type = eSectionTypeDWARFDebugLine;
409 else if (section_name == g_sect_name_dwarf_debug_loc)
410 sect_type = eSectionTypeDWARFDebugLoc;
411 else if (section_name == g_sect_name_dwarf_debug_macinfo)
412 sect_type = eSectionTypeDWARFDebugMacInfo;
413 else if (section_name == g_sect_name_dwarf_debug_pubnames)
414 sect_type = eSectionTypeDWARFDebugPubNames;
415 else if (section_name == g_sect_name_dwarf_debug_pubtypes)
416 sect_type = eSectionTypeDWARFDebugPubTypes;
417 else if (section_name == g_sect_name_dwarf_debug_ranges)
418 sect_type = eSectionTypeDWARFDebugRanges;
419 else if (section_name == g_sect_name_dwarf_debug_str)
420 sect_type = eSectionTypeDWARFDebugStr;
421 else if (section_name == g_sect_name_objc_selrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422 sect_type = eSectionTypeDataCStringPointers;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 else if (section_name == g_sect_name_objc_msgrefs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 sect_type = eSectionTypeDataObjCMessageRefs;
Greg Clayton4ceb9982010-07-21 22:54:26 +0000425 else if (section_name == g_sect_name_eh_frame)
426 sect_type = eSectionTypeEHFrame;
427 else if (section_name == g_sect_name_cfstring)
428 sect_type = eSectionTypeDataObjCCFStrings;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429 else if (section_name == g_sect_name_objc_data ||
430 section_name == g_sect_name_objc_classrefs ||
431 section_name == g_sect_name_objc_superrefs ||
432 section_name == g_sect_name_objc_const ||
433 section_name == g_sect_name_objc_classlist)
434 {
435 sect_type = eSectionTypeDataPointers;
436 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437
438 if (sect_type == eSectionTypeOther)
439 {
440 switch (mach_sect_type)
441 {
442 // TODO: categorize sections by other flags for regular sections
Greg Claytone1a916a2010-07-21 22:12:05 +0000443 case SectionTypeRegular: sect_type = eSectionTypeOther; break;
444 case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break;
445 case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings
446 case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals
447 case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals
448 case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals
449 case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers
450 case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers
451 case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field
452 case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization
453 case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination
454 case SectionTypeCoalesced: sect_type = eSectionTypeOther; break;
455 case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break;
456 case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing
457 case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals
458 case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break;
459 case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 default: break;
461 }
462 }
463
464 SectionSP section_sp(new Section(segment_sp.get(),
465 GetModule(),
466 ++sectID,
467 section_name,
468 sect_type,
469 sect64.addr - segment_sp->GetFileAddress(),
470 sect64.size,
471 sect64.offset,
472 sect64.offset == 0 ? 0 : sect64.size,
473 sect64.flags));
474 segment_sp->GetChildren().AddSection(section_sp);
475
476 if (segment_sp->IsFake())
477 {
478 segment_sp.reset();
479 segment_name.Clear();
480 }
481 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000482 if (m_header.filetype == HeaderFileTypeDSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 {
484 if (first_segment_sectID <= sectID)
485 {
486 lldb::user_id_t sect_uid;
487 for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid)
488 {
489 SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid));
490 SectionSP next_section_sp;
491 if (sect_uid + 1 <= sectID)
492 next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1);
493
494 if (curr_section_sp.get())
495 {
496 if (curr_section_sp->GetByteSize() == 0)
497 {
498 if (next_section_sp.get() != NULL)
499 curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() );
500 else
501 curr_section_sp->SetByteSize ( load_cmd.vmsize );
502 }
503 }
504 }
505 }
506 }
507 }
508 }
509 }
Greg Claytone1a916a2010-07-21 22:12:05 +0000510 else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511 {
512 m_dysymtab.cmd = load_cmd.cmd;
513 m_dysymtab.cmdsize = load_cmd.cmdsize;
514 m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2);
515 }
516
517 offset = load_cmd_offset + load_cmd.cmdsize;
518 }
519// if (dump_sections)
520// {
521// StreamFile s(stdout);
522// m_sections_ap->Dump(&s, true);
523// }
524 return sectID; // Return the number of sections we registered with the module
525}
526
527class MachSymtabSectionInfo
528{
529public:
530
531 MachSymtabSectionInfo (SectionList *section_list) :
532 m_section_list (section_list),
533 m_section_infos()
534 {
535 // Get the number of sections down to a depth of 1 to include
536 // all segments and their sections, but no other sections that
537 // may be added for debug map or
538 m_section_infos.resize(section_list->GetNumSections(1));
539 }
540
541
542 Section *
543 GetSection (uint8_t n_sect, addr_t file_addr)
544 {
545 if (n_sect == 0)
546 return NULL;
547 if (n_sect < m_section_infos.size())
548 {
549 if (m_section_infos[n_sect].section == NULL)
550 {
551 Section *section = m_section_list->FindSectionByID (n_sect).get();
552 m_section_infos[n_sect].section = section;
553 assert (section != NULL);
554 m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress());
555 m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize());
556 }
557 if (m_section_infos[n_sect].vm_range.Contains(file_addr))
558 return m_section_infos[n_sect].section;
559 }
560 return m_section_list->FindSectionContainingFileAddress(file_addr).get();
561 }
562
563protected:
564 struct SectionInfo
565 {
566 SectionInfo () :
567 vm_range(),
568 section (NULL)
569 {
570 }
571
572 VMRange vm_range;
573 Section *section;
574 };
575 SectionList *m_section_list;
576 std::vector<SectionInfo> m_section_infos;
577};
578
579
580
581size_t
582ObjectFileMachO::ParseSymtab (bool minimize)
583{
584 Timer scoped_timer(__PRETTY_FUNCTION__,
585 "ObjectFileMachO::ParseSymtab () module = %s",
586 m_file.GetFilename().AsCString(""));
587 struct symtab_command symtab_load_command;
588 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
589 uint32_t i;
590 for (i=0; i<m_header.ncmds; ++i)
591 {
592 const uint32_t cmd_offset = offset;
593 // Read in the load command and load command size
594 if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL)
595 break;
596 // Watch for the symbol table load command
Greg Claytone1a916a2010-07-21 22:12:05 +0000597 if (symtab_load_command.cmd == LoadCommandSymtab)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 {
599 // Read in the rest of the symtab load command
Jason Molendaea84e762010-07-06 22:38:03 +0000600 if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601 {
602 Symtab *symtab = m_symtab_ap.get();
603 SectionList *section_list = GetSectionList();
604 assert(section_list);
605 const size_t addr_size = m_data.GetAddressByteSize();
606 const ByteOrder endian = m_data.GetByteOrder();
607 bool bit_width_32 = addr_size == 4;
608 const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64);
609
610 DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size));
611 DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize));
612
613 const char *strtab_data = (const char *)strtab_data_sp->GetBytes();
614// DataExtractor symtab_data(symtab_data_sp, endian, addr_size);
615// DataExtractor strtab_data(strtab_data_sp, endian, addr_size);
616
617 static ConstString g_segment_name_TEXT ("__TEXT");
618 static ConstString g_segment_name_DATA ("__DATA");
619 static ConstString g_segment_name_OBJC ("__OBJC");
620 static ConstString g_section_name_eh_frame ("__eh_frame");
621 SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT));
622 SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA));
623 SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC));
624 SectionSP eh_frame_section_sp;
625 if (text_section_sp.get())
626 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame);
627 else
628 eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame);
629
Greg Claytone1a916a2010-07-21 22:12:05 +0000630 uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631 //uint32_t symtab_offset = 0;
632 const uint8_t* nlist_data = symtab_data_sp->GetBytes();
633 assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms);
634
635
636 if (endian != eByteOrderHost)
637 {
638 // ...
639 assert (!"UNIMPLEMENTED: Swap all nlist entries");
640 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000641 uint32_t N_SO_index = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642
643 MachSymtabSectionInfo section_info (section_list);
644 std::vector<uint32_t> N_FUN_indexes;
645 std::vector<uint32_t> N_NSYM_indexes;
646 std::vector<uint32_t> N_INCL_indexes;
647 std::vector<uint32_t> N_BRAC_indexes;
648 std::vector<uint32_t> N_COMM_indexes;
Greg Clayton928d8292010-09-08 16:38:06 +0000649 typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000650 typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
Greg Clayton928d8292010-09-08 16:38:06 +0000651 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
652 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000653 // Any symbols that get merged into another will get an entry
654 // in this map so we know
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000655 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656 uint32_t nlist_idx = 0;
657 Symbol *symbol_ptr = NULL;
658
659 uint32_t sym_idx = 0;
660 Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
661 uint32_t num_syms = symtab->GetNumSymbols();
662
663 //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms);
664 for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx)
665 {
666 struct nlist_64 nlist;
667 if (bit_width_32)
668 {
669 struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size));
Greg Claytone1a916a2010-07-21 22:12:05 +0000670 nlist.n_strx = nlist32_ptr->n_strx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671 nlist.n_type = nlist32_ptr->n_type;
672 nlist.n_sect = nlist32_ptr->n_sect;
673 nlist.n_desc = nlist32_ptr->n_desc;
674 nlist.n_value = nlist32_ptr->n_value;
675 }
676 else
677 {
678 nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size)));
679 }
680
681 SymbolType type = eSymbolTypeInvalid;
Greg Claytone1a916a2010-07-21 22:12:05 +0000682 const char* symbol_name = &strtab_data[nlist.n_strx];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 if (symbol_name[0] == '\0')
684 symbol_name = NULL;
685 Section* symbol_section = NULL;
686 bool add_nlist = true;
Greg Claytone1a916a2010-07-21 22:12:05 +0000687 bool is_debug = ((nlist.n_type & NlistMaskStab) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000688
689 assert (sym_idx < num_syms);
690
691 sym[sym_idx].SetDebug (is_debug);
692
693 if (is_debug)
694 {
695 switch (nlist.n_type)
696 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000697 case StabGlobalSymbol:
698 // N_GSYM -- global symbol: name,,NO_SECT,type,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699 // Sometimes the N_GSYM value contains the address.
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000700 sym[sym_idx].SetExternal(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 if (nlist.n_value != 0)
702 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000703 type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 break;
705
Greg Claytone1a916a2010-07-21 22:12:05 +0000706 case StabFunctionName:
707 // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000708 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 break;
710
Greg Claytone1a916a2010-07-21 22:12:05 +0000711 case StabFunction:
712 // N_FUN -- procedure: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 if (symbol_name)
714 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000715 type = eSymbolTypeCode;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Clayton928d8292010-09-08 16:38:06 +0000717
718 N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 // We use the current number of symbols in the symbol table in lieu of
720 // using nlist_idx in case we ever start trimming entries out
721 N_FUN_indexes.push_back(sym_idx);
722 }
723 else
724 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000725 type = eSymbolTypeCompiler;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726
727 if ( !N_FUN_indexes.empty() )
728 {
729 // Copy the size of the function into the original STAB entry so we don't have
730 // to hunt for it later
731 symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value);
732 N_FUN_indexes.pop_back();
Jason Molendaea84e762010-07-06 22:38:03 +0000733 // We don't really need the end function STAB as it contains the size which
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734 // we already placed with the original symbol, so don't add it if we want a
735 // minimal symbol table
736 if (minimize)
737 add_nlist = false;
738 }
739 }
740 break;
741
Greg Claytone1a916a2010-07-21 22:12:05 +0000742 case StabStaticSymbol:
743 // N_STSYM -- static symbol: name,,n_sect,type,address
Greg Clayton928d8292010-09-08 16:38:06 +0000744 N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000746 type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 break;
748
Greg Claytone1a916a2010-07-21 22:12:05 +0000749 case StabLocalCommon:
750 // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
752 type = eSymbolTypeCommonBlock;
753 break;
754
Greg Claytone1a916a2010-07-21 22:12:05 +0000755 case StabBeginSymbol:
756 // N_BNSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757 // We use the current number of symbols in the symbol table in lieu of
758 // using nlist_idx in case we ever start trimming entries out
759 if (minimize)
760 {
761 // Skip these if we want minimal symbol tables
762 add_nlist = false;
763 }
764 else
765 {
766 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
767 N_NSYM_indexes.push_back(sym_idx);
768 type = eSymbolTypeScopeBegin;
769 }
770 break;
771
Greg Claytone1a916a2010-07-21 22:12:05 +0000772 case StabEndSymbol:
773 // N_ENSYM
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774 // Set the size of the N_BNSYM to the terminating index of this N_ENSYM
775 // so that we can always skip the entire symbol if we need to navigate
776 // more quickly at the source level when parsing STABS
777 if (minimize)
778 {
779 // Skip these if we want minimal symbol tables
780 add_nlist = false;
781 }
782 else
783 {
784 if ( !N_NSYM_indexes.empty() )
785 {
786 symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back());
787 symbol_ptr->SetByteSize(sym_idx + 1);
788 symbol_ptr->SetSizeIsSibling(true);
789 N_NSYM_indexes.pop_back();
790 }
791 type = eSymbolTypeScopeEnd;
792 }
793 break;
794
795
Greg Claytone1a916a2010-07-21 22:12:05 +0000796 case StabSourceFileOptions:
797 // N_OPT - emitted with gcc2_compiled and in gcc source
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798 type = eSymbolTypeCompiler;
799 break;
800
Greg Claytone1a916a2010-07-21 22:12:05 +0000801 case StabRegisterSymbol:
802 // N_RSYM - register sym: name,,NO_SECT,type,register
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803 type = eSymbolTypeVariable;
804 break;
805
Greg Claytone1a916a2010-07-21 22:12:05 +0000806 case StabSourceLine:
807 // N_SLINE - src line: 0,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
809 type = eSymbolTypeLineEntry;
810 break;
811
Greg Claytone1a916a2010-07-21 22:12:05 +0000812 case StabStructureType:
813 // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814 type = eSymbolTypeVariableType;
815 break;
816
Greg Claytone1a916a2010-07-21 22:12:05 +0000817 case StabSourceFileName:
818 // N_SO - source file name
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819 type = eSymbolTypeSourceFile;
820 if (symbol_name == NULL)
821 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000822 if (minimize)
823 add_nlist = false;
824 if (N_SO_index != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000825 {
826 // Set the size of the N_SO to the terminating index of this N_SO
827 // so that we can always skip the entire N_SO if we need to navigate
828 // more quickly at the source level when parsing STABS
829 symbol_ptr = symtab->SymbolAtIndex(N_SO_index);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000830 symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000831 symbol_ptr->SetSizeIsSibling(true);
832 }
833 N_NSYM_indexes.clear();
834 N_INCL_indexes.clear();
835 N_BRAC_indexes.clear();
836 N_COMM_indexes.clear();
837 N_FUN_indexes.clear();
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000838 N_SO_index = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000840 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 {
842 // We use the current number of symbols in the symbol table in lieu of
843 // using nlist_idx in case we ever start trimming entries out
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000844 if (symbol_name[0] == '/')
845 N_SO_index = sym_idx;
846 else if (minimize && (N_SO_index == sym_idx - 1))
847 {
848 const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString();
849 if (so_path && so_path[0])
850 {
851 std::string full_so_path (so_path);
852 if (*full_so_path.rbegin() != '/')
853 full_so_path += '/';
854 full_so_path += symbol_name;
855 sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false);
856 add_nlist = false;
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000857 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000858 }
859 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000861
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862 break;
863
Greg Claytone1a916a2010-07-21 22:12:05 +0000864 case StabObjectFileName:
865 // N_OSO - object file name: name,,0,0,st_mtime
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866 type = eSymbolTypeObjectFile;
867 break;
868
Greg Claytone1a916a2010-07-21 22:12:05 +0000869 case StabLocalSymbol:
870 // N_LSYM - local sym: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871 type = eSymbolTypeLocal;
872 break;
873
874 //----------------------------------------------------------------------
875 // INCL scopes
876 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +0000877 case StabBeginIncludeFileName:
878 // N_BINCL - include file beginning: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879 // We use the current number of symbols in the symbol table in lieu of
880 // using nlist_idx in case we ever start trimming entries out
881 N_INCL_indexes.push_back(sym_idx);
882 type = eSymbolTypeScopeBegin;
883 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884
Greg Claytone1a916a2010-07-21 22:12:05 +0000885 case StabEndIncludeFile:
886 // N_EINCL - include file end: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000887 // Set the size of the N_BINCL to the terminating index of this N_EINCL
888 // so that we can always skip the entire symbol if we need to navigate
889 // more quickly at the source level when parsing STABS
890 if ( !N_INCL_indexes.empty() )
891 {
892 symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back());
893 symbol_ptr->SetByteSize(sym_idx + 1);
894 symbol_ptr->SetSizeIsSibling(true);
895 N_INCL_indexes.pop_back();
896 }
897 type = eSymbolTypeScopeEnd;
898 break;
899
Greg Claytone1a916a2010-07-21 22:12:05 +0000900 case StabIncludeFileName:
901 // N_SOL - #included file name: name,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902 type = eSymbolTypeHeaderFile;
Greg Clayton49bd1c82010-09-07 17:36:17 +0000903
904 // We currently don't use the header files on darwin
905 if (minimize)
906 add_nlist = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907 break;
908
Greg Claytone1a916a2010-07-21 22:12:05 +0000909 case StabCompilerParameters:
910 // N_PARAMS - compiler parameters: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911 type = eSymbolTypeCompiler;
912 break;
913
Greg Claytone1a916a2010-07-21 22:12:05 +0000914 case StabCompilerVersion:
915 // N_VERSION - compiler version: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000916 type = eSymbolTypeCompiler;
917 break;
918
Greg Claytone1a916a2010-07-21 22:12:05 +0000919 case StabCompilerOptLevel:
920 // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000921 type = eSymbolTypeCompiler;
922 break;
923
Greg Claytone1a916a2010-07-21 22:12:05 +0000924 case StabParameter:
925 // N_PSYM - parameter: name,,NO_SECT,type,offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926 type = eSymbolTypeVariable;
927 break;
928
Greg Claytone1a916a2010-07-21 22:12:05 +0000929 case StabAlternateEntry:
930 // N_ENTRY - alternate entry: name,,n_sect,linenumber,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
932 type = eSymbolTypeLineEntry;
933 break;
934
935 //----------------------------------------------------------------------
936 // Left and Right Braces
937 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +0000938 case StabLeftBracket:
939 // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940 // We use the current number of symbols in the symbol table in lieu of
941 // using nlist_idx in case we ever start trimming entries out
942 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
943 N_BRAC_indexes.push_back(sym_idx);
944 type = eSymbolTypeScopeBegin;
945 break;
946
Greg Claytone1a916a2010-07-21 22:12:05 +0000947 case StabRightBracket:
948 // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949 // Set the size of the N_LBRAC to the terminating index of this N_RBRAC
950 // so that we can always skip the entire symbol if we need to navigate
951 // more quickly at the source level when parsing STABS
952 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
953 if ( !N_BRAC_indexes.empty() )
954 {
955 symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back());
956 symbol_ptr->SetByteSize(sym_idx + 1);
957 symbol_ptr->SetSizeIsSibling(true);
958 N_BRAC_indexes.pop_back();
959 }
960 type = eSymbolTypeScopeEnd;
961 break;
962
Greg Claytone1a916a2010-07-21 22:12:05 +0000963 case StabDeletedIncludeFile:
964 // N_EXCL - deleted include file: name,,NO_SECT,0,sum
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965 type = eSymbolTypeHeaderFile;
966 break;
967
968 //----------------------------------------------------------------------
969 // COMM scopes
970 //----------------------------------------------------------------------
Greg Claytone1a916a2010-07-21 22:12:05 +0000971 case StabBeginCommon:
972 // N_BCOMM - begin common: name,,NO_SECT,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000973 // We use the current number of symbols in the symbol table in lieu of
974 // using nlist_idx in case we ever start trimming entries out
975 type = eSymbolTypeScopeBegin;
976 N_COMM_indexes.push_back(sym_idx);
977 break;
978
Greg Claytone1a916a2010-07-21 22:12:05 +0000979 case StabEndCommonLocal:
980 // N_ECOML - end common (local name): 0,,n_sect,0,address
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
982 // Fall through
983
Greg Claytone1a916a2010-07-21 22:12:05 +0000984 case StabEndCommon:
985 // N_ECOMM - end common: name,,n_sect,0,0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986 // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML
987 // so that we can always skip the entire symbol if we need to navigate
988 // more quickly at the source level when parsing STABS
989 if ( !N_COMM_indexes.empty() )
990 {
991 symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back());
992 symbol_ptr->SetByteSize(sym_idx + 1);
993 symbol_ptr->SetSizeIsSibling(true);
994 N_COMM_indexes.pop_back();
995 }
996 type = eSymbolTypeScopeEnd;
997 break;
998
Greg Claytone1a916a2010-07-21 22:12:05 +0000999 case StabLength:
1000 // N_LENG - second stab entry with length information
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001001 type = eSymbolTypeAdditional;
1002 break;
1003
1004 default: break;
1005 }
1006 }
1007 else
1008 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001009 //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type;
1010 uint8_t n_type = NlistMaskType & nlist.n_type;
1011 sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012
1013 if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name)
1014 {
1015 type = eSymbolTypeRuntime;
1016 }
1017 else
1018 {
1019 switch (n_type)
1020 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001021 case NListTypeIndirect: // N_INDR - Fall through
1022 case NListTypePreboundUndefined:// N_PBUD - Fall through
1023 case NListTypeUndefined: // N_UNDF
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001024 type = eSymbolTypeExtern;
1025 break;
1026
Greg Claytone1a916a2010-07-21 22:12:05 +00001027 case NListTypeAbsolute: // N_ABS
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028 type = eSymbolTypeAbsolute;
1029 break;
1030
Greg Claytone1a916a2010-07-21 22:12:05 +00001031 case NListTypeSection: // N_SECT
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001032 symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
1033
1034 assert(symbol_section != NULL);
1035 if (TEXT_eh_frame_sectID == nlist.n_sect)
1036 {
1037 type = eSymbolTypeException;
1038 }
1039 else
1040 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001041 uint32_t section_type = symbol_section->GetAllFlagBits() & SectionFlagMaskSectionType;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042
1043 switch (section_type)
1044 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001045 case SectionTypeRegular: break; // regular section
1046 //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section
1047 case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings
1048 case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals
1049 case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals
1050 case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals
1051 case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers
1052 case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers
1053 case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field
1054 case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization
1055 case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination
1056 //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced
1057 //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes)
1058 case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing
1059 case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals
1060 case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break;
1061 case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062 default: break;
1063 }
1064
1065 if (type == eSymbolTypeInvalid)
1066 {
1067 const char *symbol_sect_name = symbol_section->GetName().AsCString();
1068 if (symbol_section->IsDescendant (text_section_sp.get()))
1069 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001070 if (symbol_section->IsClear(SectionAttrUserPureInstructions |
1071 SectionAttrUserSelfModifyingCode |
1072 SectionAttrSytemSomeInstructions))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073 type = eSymbolTypeData;
1074 else
1075 type = eSymbolTypeCode;
1076 }
1077 else
1078 if (symbol_section->IsDescendant(data_section_sp.get()))
1079 {
1080 if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name)
1081 {
1082 type = eSymbolTypeRuntime;
1083 }
1084 else
1085 if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name)
1086 {
1087 type = eSymbolTypeException;
1088 }
1089 else
1090 {
1091 type = eSymbolTypeData;
1092 }
1093 }
1094 else
1095 if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name)
1096 {
1097 type = eSymbolTypeTrampoline;
1098 }
1099 else
1100 if (symbol_section->IsDescendant(objc_section_sp.get()))
1101 {
1102 type = eSymbolTypeRuntime;
1103 }
1104 }
1105 }
1106 break;
Greg Clayton928d8292010-09-08 16:38:06 +00001107 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108 }
1109 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110 if (add_nlist)
1111 {
1112 bool symbol_name_is_mangled = false;
1113 if (symbol_name && symbol_name[0] == '_')
1114 {
1115 symbol_name_is_mangled = symbol_name[1] == '_';
1116 symbol_name++; // Skip the leading underscore
1117 }
1118 uint64_t symbol_value = nlist.n_value;
Greg Clayton928d8292010-09-08 16:38:06 +00001119
1120 if (symbol_name)
1121 sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001122 if (is_debug == false)
Greg Clayton928d8292010-09-08 16:38:06 +00001123 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001124 if (type == eSymbolTypeCode)
Greg Clayton928d8292010-09-08 16:38:06 +00001125 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001126 // See if we can find a N_FUN entry for any code symbols.
1127 // If we do find a match, and the name matches, then we
1128 // can merge the two into just the function symbol to avoid
1129 // duplicate entries in the symbol table
1130 ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
1131 if (pos != N_FUN_addr_to_sym_idx.end())
Greg Clayton928d8292010-09-08 16:38:06 +00001132 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001133 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1134 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1135 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001136 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001137 // We just need the flags from the linker symbol, so put these flags
1138 // into the N_FUN flags to avoid duplicate symbols in the symbol table
1139 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1140 sym[sym_idx].Clear();
1141 continue;
1142 }
Greg Clayton928d8292010-09-08 16:38:06 +00001143 }
1144 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001145 else if (type == eSymbolTypeData)
Greg Clayton928d8292010-09-08 16:38:06 +00001146 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001147 // See if we can find a N_STSYM entry for any data symbols.
1148 // If we do find a match, and the name matches, then we
1149 // can merge the two into just the Static symbol to avoid
1150 // duplicate entries in the symbol table
1151 ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
1152 if (pos != N_STSYM_addr_to_sym_idx.end())
Greg Clayton928d8292010-09-08 16:38:06 +00001153 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001154 if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) ||
1155 (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName()))
1156 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001157 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001158 // We just need the flags from the linker symbol, so put these flags
1159 // into the N_STSYM flags to avoid duplicate symbols in the symbol table
1160 sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1161 sym[sym_idx].Clear();
1162 continue;
1163 }
Greg Clayton928d8292010-09-08 16:38:06 +00001164 }
1165 }
1166 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167 if (symbol_section != NULL)
1168 symbol_value -= symbol_section->GetFileAddress();
1169
1170 sym[sym_idx].SetID (nlist_idx);
1171 sym[sym_idx].SetType (type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001172 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section);
1173 sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value);
1174 sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc);
1175
1176 ++sym_idx;
1177 }
1178 else
1179 {
1180 sym[sym_idx].Clear();
1181 }
1182
1183 }
1184
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001185 // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value
1186 // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all
1187 // such entries by figuring out what the address for the global is by looking up this non-STAB
1188 // entry and copying the value into the debug symbol's value to save us the hassle in the
1189 // debug symbol parser.
1190
1191 Symbol *global_symbol = NULL;
1192 for (nlist_idx = 0;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001193 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 +00001194 nlist_idx++)
1195 {
1196 if (global_symbol->GetValue().GetFileAddress() == 0)
1197 {
1198 std::vector<uint32_t> indexes;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001199 if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001200 {
1201 std::vector<uint32_t>::const_iterator pos;
1202 std::vector<uint32_t>::const_iterator end = indexes.end();
1203 for (pos = indexes.begin(); pos != end; ++pos)
1204 {
1205 symbol_ptr = symtab->SymbolAtIndex(*pos);
1206 if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false)
1207 {
1208 global_symbol->SetValue(symbol_ptr->GetValue());
1209 break;
1210 }
1211 }
1212 }
1213 }
1214 }
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001215
1216 // Trim our symbols down to just what we ended up with after
1217 // removing any symbols.
1218 if (sym_idx < num_syms)
1219 {
1220 num_syms = sym_idx;
1221 sym = symtab->Resize (num_syms);
1222 }
1223
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001224 // Now synthesize indirect symbols
1225 if (m_dysymtab.nindirectsyms != 0)
1226 {
1227 DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4));
1228
1229 if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize())
1230 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001231 NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001232 DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize());
1233
1234 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx)
1235 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001236 if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237 {
1238 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
1239 if (symbol_stub_byte_size == 0)
1240 continue;
1241
1242 const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size;
1243
1244 if (num_symbol_stubs == 0)
1245 continue;
1246
1247 const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1;
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001248 uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx)
1250 {
1251 const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx;
1252 const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size);
1253 uint32_t symbol_stub_offset = symbol_stub_index * 4;
1254 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4))
1255 {
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001256 const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset);
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001257
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001258 NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id);
1259 Symbol *stub_symbol = NULL;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +00001260 if (index_pos != end_index_pos)
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001261 {
1262 // We have a remapping from the original nlist index to
1263 // a current symbol index, so just look this up by index
1264 stub_symbol = symtab->SymbolAtIndex (index_pos->second);
1265 }
1266 else
1267 {
1268 // We need to lookup a symbol using the original nlist
1269 // symbol index since this index is coming from the
1270 // S_SYMBOL_STUBS
1271 stub_symbol = symtab->FindSymbolByID (stub_sym_id);
1272 }
Greg Clayton49bd1c82010-09-07 17:36:17 +00001273
1274 assert (stub_symbol);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001275 if (stub_symbol)
1276 {
1277 Address so_addr(symbol_stub_addr, section_list);
1278
1279 if (stub_symbol->GetType() == eSymbolTypeExtern)
1280 {
1281 // Change the external symbol into a trampoline that makes sense
1282 // These symbols were N_UNDF N_EXT, and are useless to us, so we
1283 // can re-use them so we don't have to make up a synthetic symbol
1284 // for no good reason.
1285 stub_symbol->SetType (eSymbolTypeTrampoline);
1286 stub_symbol->SetExternal (false);
1287 stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr;
1288 stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1289 }
1290 else
1291 {
1292 // Make a synthetic symbol to describe the trampoline stub
1293 if (sym_idx >= num_syms)
Greg Clayton0c38b0d2010-09-12 05:25:16 +00001294 sym = symtab->Resize (++num_syms);
1295 sym[sym_idx].SetID (synthetic_stub_sym_id++);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296 sym[sym_idx].GetMangled() = stub_symbol->GetMangled();
1297 sym[sym_idx].SetType (eSymbolTypeTrampoline);
1298 sym[sym_idx].SetIsSynthetic (true);
1299 sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr;
1300 sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size);
1301 ++sym_idx;
1302 }
1303 }
1304 }
1305 }
1306 }
1307 }
1308 }
1309 }
1310
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001311 return symtab->GetNumSymbols();
1312 }
1313 }
1314 offset = cmd_offset + symtab_load_command.cmdsize;
1315 }
1316 return 0;
1317}
1318
1319
1320void
1321ObjectFileMachO::Dump (Stream *s)
1322{
1323 lldb_private::Mutex::Locker locker(m_mutex);
1324 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1325 s->Indent();
Greg Claytone1a916a2010-07-21 22:12:05 +00001326 if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001327 s->PutCString("ObjectFileMachO64");
1328 else
1329 s->PutCString("ObjectFileMachO32");
1330
Greg Clayton41f92322010-06-11 03:25:34 +00001331 ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001332
1333 *s << ", file = '" << m_file << "', arch = " << header_arch.AsCString() << "\n";
1334
1335 if (m_sections_ap.get())
1336 m_sections_ap->Dump(s, NULL, true);
1337
1338 if (m_symtab_ap.get())
1339 m_symtab_ap->Dump(s, NULL);
1340}
1341
1342
1343bool
1344ObjectFileMachO::GetUUID (UUID* uuid)
1345{
1346 lldb_private::Mutex::Locker locker(m_mutex);
1347 struct uuid_command load_cmd;
1348 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1349 uint32_t i;
1350 for (i=0; i<m_header.ncmds; ++i)
1351 {
1352 const uint32_t cmd_offset = offset;
1353 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1354 break;
1355
Greg Claytone1a916a2010-07-21 22:12:05 +00001356 if (load_cmd.cmd == LoadCommandUUID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357 {
1358 const uint8_t *uuid_bytes = m_data.PeekData(offset, 16);
1359 if (uuid_bytes)
1360 {
1361 uuid->SetBytes (uuid_bytes);
1362 return true;
1363 }
1364 return false;
1365 }
1366 offset = cmd_offset + load_cmd.cmdsize;
1367 }
1368 return false;
1369}
1370
1371
1372uint32_t
1373ObjectFileMachO::GetDependentModules (FileSpecList& files)
1374{
1375 lldb_private::Mutex::Locker locker(m_mutex);
1376 struct load_command load_cmd;
1377 uint32_t offset = MachHeaderSizeFromMagic(m_header.magic);
1378 uint32_t count = 0;
1379 uint32_t i;
1380 for (i=0; i<m_header.ncmds; ++i)
1381 {
1382 const uint32_t cmd_offset = offset;
1383 if (m_data.GetU32(&offset, &load_cmd, 2) == NULL)
1384 break;
1385
1386 switch (load_cmd.cmd)
1387 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001388 case LoadCommandDylibLoad:
1389 case LoadCommandDylibLoadWeak:
1390 case LoadCommandDylibReexport:
1391 case LoadCommandDynamicLinkerLoad:
1392 case LoadCommandFixedVMShlibLoad:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393 {
1394 uint32_t name_offset = cmd_offset + m_data.GetU32(&offset);
1395 const char *path = m_data.PeekCStr(name_offset);
1396 // Skip any path that starts with '@' since these are usually:
1397 // @executable_path/.../file
1398 // @rpath/.../file
1399 if (path && path[0] != '@')
1400 {
1401 FileSpec file_spec(path);
1402 if (files.AppendIfUnique(file_spec))
1403 count++;
1404 }
1405 }
1406 break;
1407
1408 default:
1409 break;
1410 }
1411 offset = cmd_offset + load_cmd.cmdsize;
1412 }
1413 return count;
1414}
1415
1416bool
1417ObjectFileMachO::GetTargetTriple (ConstString &target_triple)
1418{
1419 lldb_private::Mutex::Locker locker(m_mutex);
1420 std::string triple(GetModule()->GetArchitecture().AsCString());
1421 triple += "-apple-darwin";
1422 target_triple.SetCString(triple.c_str());
1423 if (target_triple)
1424 return true;
1425 return false;
1426}
1427
1428
1429//------------------------------------------------------------------
1430// PluginInterface protocol
1431//------------------------------------------------------------------
1432const char *
1433ObjectFileMachO::GetPluginName()
1434{
1435 return "ObjectFileMachO";
1436}
1437
1438const char *
1439ObjectFileMachO::GetShortPluginName()
1440{
1441 return GetPluginNameStatic();
1442}
1443
1444uint32_t
1445ObjectFileMachO::GetPluginVersion()
1446{
1447 return 1;
1448}
1449
1450void
1451ObjectFileMachO::GetPluginCommandHelp (const char *command, Stream *strm)
1452{
1453}
1454
1455Error
1456ObjectFileMachO::ExecutePluginCommand (Args &command, Stream *strm)
1457{
1458 Error error;
1459 error.SetErrorString("No plug-in command are currently supported.");
1460 return error;
1461}
1462
1463Log *
1464ObjectFileMachO::EnablePluginLogging (Stream *strm, Args &command)
1465{
1466 return NULL;
1467}
1468
1469
1470
1471