Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ObjectFileMachO.cpp -------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 10 | #include "llvm/Support/MachO.h" |
| 11 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "ObjectFileMachO.h" |
| 13 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 14 | #include "lldb/Core/ArchSpec.h" |
| 15 | #include "lldb/Core/DataBuffer.h" |
Greg Clayton | 5f54ac3 | 2011-02-08 05:05:52 +0000 | [diff] [blame] | 16 | #include "lldb/Host/FileSpec.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/FileSpecList.h" |
| 18 | #include "lldb/Core/Module.h" |
| 19 | #include "lldb/Core/PluginManager.h" |
| 20 | #include "lldb/Core/Section.h" |
| 21 | #include "lldb/Core/StreamFile.h" |
| 22 | #include "lldb/Core/StreamString.h" |
| 23 | #include "lldb/Core/Timer.h" |
| 24 | #include "lldb/Core/UUID.h" |
| 25 | #include "lldb/Symbol/ObjectFile.h" |
| 26 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace lldb; |
| 29 | using namespace lldb_private; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 30 | using namespace llvm::MachO; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | void |
| 34 | ObjectFileMachO::Initialize() |
| 35 | { |
| 36 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 37 | GetPluginDescriptionStatic(), |
| 38 | CreateInstance); |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | ObjectFileMachO::Terminate() |
| 43 | { |
| 44 | PluginManager::UnregisterPlugin (CreateInstance); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | const char * |
| 49 | ObjectFileMachO::GetPluginNameStatic() |
| 50 | { |
| 51 | return "object-file.mach-o"; |
| 52 | } |
| 53 | |
| 54 | const char * |
| 55 | ObjectFileMachO::GetPluginDescriptionStatic() |
| 56 | { |
| 57 | return "Mach-o object file reader (32 and 64 bit)"; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | ObjectFile * |
| 62 | ObjectFileMachO::CreateInstance (Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) |
| 63 | { |
| 64 | if (ObjectFileMachO::MagicBytesMatch(dataSP)) |
| 65 | { |
| 66 | std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module, dataSP, file, offset, length)); |
| 67 | if (objfile_ap.get() && objfile_ap->ParseHeader()) |
| 68 | return objfile_ap.release(); |
| 69 | } |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | static uint32_t |
| 75 | MachHeaderSizeFromMagic(uint32_t magic) |
| 76 | { |
| 77 | switch (magic) |
| 78 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 79 | case HeaderMagic32: |
| 80 | case HeaderMagic32Swapped: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | return sizeof(struct mach_header); |
| 82 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 83 | case HeaderMagic64: |
| 84 | case HeaderMagic64Swapped: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | return sizeof(struct mach_header_64); |
| 86 | break; |
| 87 | |
| 88 | default: |
| 89 | break; |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | bool |
| 96 | ObjectFileMachO::MagicBytesMatch (DataBufferSP& dataSP) |
| 97 | { |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 98 | DataExtractor data(dataSP, lldb::endian::InlHostByteOrder(), 4); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 99 | uint32_t offset = 0; |
| 100 | uint32_t magic = data.GetU32(&offset); |
| 101 | return MachHeaderSizeFromMagic(magic) != 0; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | ObjectFileMachO::ObjectFileMachO(Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) : |
| 106 | ObjectFile(module, file, offset, length, dataSP), |
| 107 | m_mutex (Mutex::eMutexTypeRecursive), |
| 108 | m_header(), |
| 109 | m_sections_ap(), |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 110 | m_symtab_ap(), |
| 111 | m_entry_point_address () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | { |
Greg Clayton | ddff7cc | 2011-02-04 21:13:05 +0000 | [diff] [blame] | 113 | ::memset (&m_header, 0, sizeof(m_header)); |
| 114 | ::memset (&m_dysymtab, 0, sizeof(m_dysymtab)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
| 118 | ObjectFileMachO::~ObjectFileMachO() |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | |
| 123 | bool |
| 124 | ObjectFileMachO::ParseHeader () |
| 125 | { |
| 126 | lldb_private::Mutex::Locker locker(m_mutex); |
| 127 | bool can_parse = false; |
| 128 | uint32_t offset = 0; |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 129 | m_data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 130 | // Leave magic in the original byte order |
| 131 | m_header.magic = m_data.GetU32(&offset); |
| 132 | switch (m_header.magic) |
| 133 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 134 | case HeaderMagic32: |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 135 | m_data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | m_data.SetAddressByteSize(4); |
| 137 | can_parse = true; |
| 138 | break; |
| 139 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 140 | case HeaderMagic64: |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 141 | m_data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | m_data.SetAddressByteSize(8); |
| 143 | can_parse = true; |
| 144 | break; |
| 145 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 146 | case HeaderMagic32Swapped: |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 147 | m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 148 | m_data.SetAddressByteSize(4); |
| 149 | can_parse = true; |
| 150 | break; |
| 151 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 152 | case HeaderMagic64Swapped: |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 153 | m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | m_data.SetAddressByteSize(8); |
| 155 | can_parse = true; |
| 156 | break; |
| 157 | |
| 158 | default: |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | if (can_parse) |
| 163 | { |
| 164 | m_data.GetU32(&offset, &m_header.cputype, 6); |
| 165 | |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 166 | ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 167 | |
| 168 | if (SetModulesArchitecture (mach_arch)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 169 | { |
| 170 | // Read in all only the load command data |
| 171 | DataBufferSP data_sp(m_file.ReadFileContents(m_offset, m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic))); |
| 172 | m_data.SetData (data_sp); |
| 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | memset(&m_header, 0, sizeof(struct mach_header)); |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | ByteOrder |
| 185 | ObjectFileMachO::GetByteOrder () const |
| 186 | { |
| 187 | lldb_private::Mutex::Locker locker(m_mutex); |
| 188 | return m_data.GetByteOrder (); |
| 189 | } |
| 190 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 191 | bool |
| 192 | ObjectFileMachO::IsExecutable() const |
| 193 | { |
| 194 | return m_header.filetype == HeaderFileTypeExecutable; |
| 195 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 196 | |
| 197 | size_t |
| 198 | ObjectFileMachO::GetAddressByteSize () const |
| 199 | { |
| 200 | lldb_private::Mutex::Locker locker(m_mutex); |
| 201 | return m_data.GetAddressByteSize (); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | Symtab * |
| 206 | ObjectFileMachO::GetSymtab() |
| 207 | { |
Greg Clayton | bdcb6ab | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 208 | lldb_private::Mutex::Locker symfile_locker(m_mutex); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 209 | if (m_symtab_ap.get() == NULL) |
| 210 | { |
| 211 | m_symtab_ap.reset(new Symtab(this)); |
Greg Clayton | bdcb6ab | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 212 | Mutex::Locker symtab_locker (m_symtab_ap->GetMutex()); |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 213 | ParseSymtab (true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 214 | } |
| 215 | return m_symtab_ap.get(); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | SectionList * |
| 220 | ObjectFileMachO::GetSectionList() |
| 221 | { |
| 222 | lldb_private::Mutex::Locker locker(m_mutex); |
| 223 | if (m_sections_ap.get() == NULL) |
| 224 | { |
| 225 | m_sections_ap.reset(new SectionList()); |
| 226 | ParseSections(); |
| 227 | } |
| 228 | return m_sections_ap.get(); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | size_t |
| 233 | ObjectFileMachO::ParseSections () |
| 234 | { |
| 235 | lldb::user_id_t segID = 0; |
| 236 | lldb::user_id_t sectID = 0; |
| 237 | struct segment_command_64 load_cmd; |
| 238 | uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 239 | uint32_t i; |
| 240 | //bool dump_sections = false; |
| 241 | for (i=0; i<m_header.ncmds; ++i) |
| 242 | { |
| 243 | const uint32_t load_cmd_offset = offset; |
| 244 | if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 245 | break; |
| 246 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 247 | if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 248 | { |
| 249 | if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16)) |
| 250 | { |
| 251 | load_cmd.vmaddr = m_data.GetAddress(&offset); |
| 252 | load_cmd.vmsize = m_data.GetAddress(&offset); |
| 253 | load_cmd.fileoff = m_data.GetAddress(&offset); |
| 254 | load_cmd.filesize = m_data.GetAddress(&offset); |
| 255 | if (m_data.GetU32(&offset, &load_cmd.maxprot, 4)) |
| 256 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 257 | |
| 258 | const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0; |
| 259 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | // Keep a list of mach segments around in case we need to |
| 261 | // get at data that isn't stored in the abstracted Sections. |
| 262 | m_mach_segments.push_back (load_cmd); |
| 263 | |
| 264 | ConstString segment_name (load_cmd.segname, std::min<int>(strlen(load_cmd.segname), sizeof(load_cmd.segname))); |
| 265 | // Use a segment ID of the segment index shifted left by 8 so they |
| 266 | // never conflict with any of the sections. |
| 267 | SectionSP segment_sp; |
| 268 | if (segment_name) |
| 269 | { |
| 270 | segment_sp.reset(new Section (NULL, |
| 271 | GetModule(), // Module to which this section belongs |
| 272 | ++segID << 8, // Section ID is the 1 based segment index shifted right by 8 bits as not to collide with any of the 256 section IDs that are possible |
| 273 | segment_name, // Name of this section |
| 274 | eSectionTypeContainer, // This section is a container of other sections. |
| 275 | load_cmd.vmaddr, // File VM address == addresses as they are found in the object file |
| 276 | load_cmd.vmsize, // VM size in bytes of this section |
| 277 | load_cmd.fileoff, // Offset to the data for this section in the file |
| 278 | load_cmd.filesize, // Size in bytes of this section as found in the the file |
| 279 | load_cmd.flags)); // Flags for this section |
| 280 | |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 281 | segment_sp->SetIsEncrypted (segment_is_encrypted); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 282 | m_sections_ap->AddSection(segment_sp); |
| 283 | } |
| 284 | |
| 285 | struct section_64 sect64; |
Greg Clayton | ddff7cc | 2011-02-04 21:13:05 +0000 | [diff] [blame] | 286 | ::memset (§64, 0, sizeof(sect64)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 287 | // Push a section into our mach sections for the section at |
Greg Clayton | 6af4fad | 2010-10-06 01:26:32 +0000 | [diff] [blame] | 288 | // index zero (NListSectionNoSection) if we don't have any |
| 289 | // mach sections yet... |
| 290 | if (m_mach_sections.empty()) |
| 291 | m_mach_sections.push_back(sect64); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | uint32_t segment_sect_idx; |
| 293 | const lldb::user_id_t first_segment_sectID = sectID + 1; |
| 294 | |
| 295 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 296 | const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 297 | for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx) |
| 298 | { |
| 299 | if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL) |
| 300 | break; |
| 301 | if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL) |
| 302 | break; |
| 303 | sect64.addr = m_data.GetAddress(&offset); |
| 304 | sect64.size = m_data.GetAddress(&offset); |
| 305 | |
| 306 | if (m_data.GetU32(&offset, §64.offset, num_u32s) == NULL) |
| 307 | break; |
| 308 | |
| 309 | // Keep a list of mach sections around in case we need to |
| 310 | // get at data that isn't stored in the abstracted Sections. |
| 311 | m_mach_sections.push_back (sect64); |
| 312 | |
| 313 | ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname))); |
| 314 | if (!segment_name) |
| 315 | { |
| 316 | // We have a segment with no name so we need to conjure up |
| 317 | // segments that correspond to the section's segname if there |
| 318 | // isn't already such a section. If there is such a section, |
| 319 | // we resize the section so that it spans all sections. |
| 320 | // We also mark these sections as fake so address matches don't |
| 321 | // hit if they land in the gaps between the child sections. |
| 322 | segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname)); |
| 323 | segment_sp = m_sections_ap->FindSectionByName (segment_name); |
| 324 | if (segment_sp.get()) |
| 325 | { |
| 326 | Section *segment = segment_sp.get(); |
| 327 | // Grow the section size as needed. |
| 328 | const lldb::addr_t sect64_min_addr = sect64.addr; |
| 329 | const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size; |
| 330 | const lldb::addr_t curr_seg_byte_size = segment->GetByteSize(); |
| 331 | const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress(); |
| 332 | const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size; |
| 333 | if (sect64_min_addr >= curr_seg_min_addr) |
| 334 | { |
| 335 | const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr; |
| 336 | // Only grow the section size if needed |
| 337 | if (new_seg_byte_size > curr_seg_byte_size) |
| 338 | segment->SetByteSize (new_seg_byte_size); |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | // We need to change the base address of the segment and |
| 343 | // adjust the child section offsets for all existing children. |
| 344 | const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr; |
| 345 | segment->Slide(slide_amount, false); |
| 346 | segment->GetChildren().Slide (-slide_amount, false); |
| 347 | segment->SetByteSize (curr_seg_max_addr - sect64_min_addr); |
| 348 | } |
Greg Clayton | 661825b | 2010-06-28 23:51:11 +0000 | [diff] [blame] | 349 | |
| 350 | // Grow the section size as needed. |
| 351 | if (sect64.offset) |
| 352 | { |
| 353 | const lldb::addr_t segment_min_file_offset = segment->GetFileOffset(); |
| 354 | const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize(); |
| 355 | |
| 356 | const lldb::addr_t section_min_file_offset = sect64.offset; |
| 357 | const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size; |
| 358 | const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset); |
| 359 | const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset; |
| 360 | segment->SetFileOffset (new_file_offset); |
| 361 | segment->SetFileSize (new_file_size); |
| 362 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 363 | } |
| 364 | else |
| 365 | { |
| 366 | // Create a fake section for the section's named segment |
| 367 | segment_sp.reset(new Section(segment_sp.get(), // Parent section |
| 368 | GetModule(), // Module to which this section belongs |
| 369 | ++segID << 8, // Section ID is the 1 based segment index shifted right by 8 bits as not to collide with any of the 256 section IDs that are possible |
| 370 | segment_name, // Name of this section |
| 371 | eSectionTypeContainer, // This section is a container of other sections. |
| 372 | sect64.addr, // File VM address == addresses as they are found in the object file |
| 373 | sect64.size, // VM size in bytes of this section |
| 374 | sect64.offset, // Offset to the data for this section in the file |
| 375 | sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file |
| 376 | load_cmd.flags)); // Flags for this section |
| 377 | segment_sp->SetIsFake(true); |
| 378 | m_sections_ap->AddSection(segment_sp); |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 379 | segment_sp->SetIsEncrypted (segment_is_encrypted); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | assert (segment_sp.get()); |
| 383 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 384 | uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 385 | static ConstString g_sect_name_objc_data ("__objc_data"); |
| 386 | static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs"); |
| 387 | static ConstString g_sect_name_objc_selrefs ("__objc_selrefs"); |
| 388 | static ConstString g_sect_name_objc_classrefs ("__objc_classrefs"); |
| 389 | static ConstString g_sect_name_objc_superrefs ("__objc_superrefs"); |
| 390 | static ConstString g_sect_name_objc_const ("__objc_const"); |
| 391 | static ConstString g_sect_name_objc_classlist ("__objc_classlist"); |
| 392 | static ConstString g_sect_name_cfstring ("__cfstring"); |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 393 | |
| 394 | static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev"); |
| 395 | static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges"); |
| 396 | static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame"); |
| 397 | static ConstString g_sect_name_dwarf_debug_info ("__debug_info"); |
| 398 | static ConstString g_sect_name_dwarf_debug_line ("__debug_line"); |
| 399 | static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc"); |
| 400 | static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo"); |
| 401 | static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames"); |
| 402 | static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes"); |
| 403 | static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges"); |
| 404 | static ConstString g_sect_name_dwarf_debug_str ("__debug_str"); |
| 405 | static ConstString g_sect_name_eh_frame ("__eh_frame"); |
Greg Clayton | 3fed8b9 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 406 | static ConstString g_sect_name_DATA ("__DATA"); |
| 407 | static ConstString g_sect_name_TEXT ("__TEXT"); |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 409 | SectionType sect_type = eSectionTypeOther; |
| 410 | |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 411 | if (section_name == g_sect_name_dwarf_debug_abbrev) |
| 412 | sect_type = eSectionTypeDWARFDebugAbbrev; |
| 413 | else if (section_name == g_sect_name_dwarf_debug_aranges) |
| 414 | sect_type = eSectionTypeDWARFDebugAranges; |
| 415 | else if (section_name == g_sect_name_dwarf_debug_frame) |
| 416 | sect_type = eSectionTypeDWARFDebugFrame; |
| 417 | else if (section_name == g_sect_name_dwarf_debug_info) |
| 418 | sect_type = eSectionTypeDWARFDebugInfo; |
| 419 | else if (section_name == g_sect_name_dwarf_debug_line) |
| 420 | sect_type = eSectionTypeDWARFDebugLine; |
| 421 | else if (section_name == g_sect_name_dwarf_debug_loc) |
| 422 | sect_type = eSectionTypeDWARFDebugLoc; |
| 423 | else if (section_name == g_sect_name_dwarf_debug_macinfo) |
| 424 | sect_type = eSectionTypeDWARFDebugMacInfo; |
| 425 | else if (section_name == g_sect_name_dwarf_debug_pubnames) |
| 426 | sect_type = eSectionTypeDWARFDebugPubNames; |
| 427 | else if (section_name == g_sect_name_dwarf_debug_pubtypes) |
| 428 | sect_type = eSectionTypeDWARFDebugPubTypes; |
| 429 | else if (section_name == g_sect_name_dwarf_debug_ranges) |
| 430 | sect_type = eSectionTypeDWARFDebugRanges; |
| 431 | else if (section_name == g_sect_name_dwarf_debug_str) |
| 432 | sect_type = eSectionTypeDWARFDebugStr; |
| 433 | else if (section_name == g_sect_name_objc_selrefs) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 434 | sect_type = eSectionTypeDataCStringPointers; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 435 | else if (section_name == g_sect_name_objc_msgrefs) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 436 | sect_type = eSectionTypeDataObjCMessageRefs; |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 437 | else if (section_name == g_sect_name_eh_frame) |
| 438 | sect_type = eSectionTypeEHFrame; |
| 439 | else if (section_name == g_sect_name_cfstring) |
| 440 | sect_type = eSectionTypeDataObjCCFStrings; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 441 | else if (section_name == g_sect_name_objc_data || |
| 442 | section_name == g_sect_name_objc_classrefs || |
| 443 | section_name == g_sect_name_objc_superrefs || |
| 444 | section_name == g_sect_name_objc_const || |
| 445 | section_name == g_sect_name_objc_classlist) |
| 446 | { |
| 447 | sect_type = eSectionTypeDataPointers; |
| 448 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 449 | |
| 450 | if (sect_type == eSectionTypeOther) |
| 451 | { |
| 452 | switch (mach_sect_type) |
| 453 | { |
| 454 | // TODO: categorize sections by other flags for regular sections |
Greg Clayton | 3fed8b9 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 455 | case SectionTypeRegular: |
| 456 | if (segment_sp->GetName() == g_sect_name_TEXT) |
| 457 | sect_type = eSectionTypeCode; |
| 458 | else if (segment_sp->GetName() == g_sect_name_DATA) |
| 459 | sect_type = eSectionTypeData; |
| 460 | else |
| 461 | sect_type = eSectionTypeOther; |
| 462 | break; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 463 | case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break; |
| 464 | case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings |
| 465 | case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals |
| 466 | case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals |
| 467 | case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals |
| 468 | case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers |
| 469 | case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers |
| 470 | case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field |
| 471 | case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization |
| 472 | case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination |
| 473 | case SectionTypeCoalesced: sect_type = eSectionTypeOther; break; |
| 474 | case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break; |
| 475 | case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing |
| 476 | case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals |
| 477 | case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break; |
| 478 | case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 479 | default: break; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | SectionSP section_sp(new Section(segment_sp.get(), |
| 484 | GetModule(), |
| 485 | ++sectID, |
| 486 | section_name, |
| 487 | sect_type, |
| 488 | sect64.addr - segment_sp->GetFileAddress(), |
| 489 | sect64.size, |
| 490 | sect64.offset, |
| 491 | sect64.offset == 0 ? 0 : sect64.size, |
| 492 | sect64.flags)); |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 493 | // Set the section to be encrypted to match the segment |
| 494 | section_sp->SetIsEncrypted (segment_is_encrypted); |
| 495 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 496 | segment_sp->GetChildren().AddSection(section_sp); |
| 497 | |
| 498 | if (segment_sp->IsFake()) |
| 499 | { |
| 500 | segment_sp.reset(); |
| 501 | segment_name.Clear(); |
| 502 | } |
| 503 | } |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 504 | if (m_header.filetype == HeaderFileTypeDSYM) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 505 | { |
| 506 | if (first_segment_sectID <= sectID) |
| 507 | { |
| 508 | lldb::user_id_t sect_uid; |
| 509 | for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid) |
| 510 | { |
| 511 | SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid)); |
| 512 | SectionSP next_section_sp; |
| 513 | if (sect_uid + 1 <= sectID) |
| 514 | next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1); |
| 515 | |
| 516 | if (curr_section_sp.get()) |
| 517 | { |
| 518 | if (curr_section_sp->GetByteSize() == 0) |
| 519 | { |
| 520 | if (next_section_sp.get() != NULL) |
| 521 | curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() ); |
| 522 | else |
| 523 | curr_section_sp->SetByteSize ( load_cmd.vmsize ); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 532 | else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 533 | { |
| 534 | m_dysymtab.cmd = load_cmd.cmd; |
| 535 | m_dysymtab.cmdsize = load_cmd.cmdsize; |
| 536 | m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2); |
| 537 | } |
| 538 | |
| 539 | offset = load_cmd_offset + load_cmd.cmdsize; |
| 540 | } |
| 541 | // if (dump_sections) |
| 542 | // { |
| 543 | // StreamFile s(stdout); |
| 544 | // m_sections_ap->Dump(&s, true); |
| 545 | // } |
| 546 | return sectID; // Return the number of sections we registered with the module |
| 547 | } |
| 548 | |
| 549 | class MachSymtabSectionInfo |
| 550 | { |
| 551 | public: |
| 552 | |
| 553 | MachSymtabSectionInfo (SectionList *section_list) : |
| 554 | m_section_list (section_list), |
| 555 | m_section_infos() |
| 556 | { |
| 557 | // Get the number of sections down to a depth of 1 to include |
| 558 | // all segments and their sections, but no other sections that |
| 559 | // may be added for debug map or |
| 560 | m_section_infos.resize(section_list->GetNumSections(1)); |
| 561 | } |
| 562 | |
| 563 | |
| 564 | Section * |
| 565 | GetSection (uint8_t n_sect, addr_t file_addr) |
| 566 | { |
| 567 | if (n_sect == 0) |
| 568 | return NULL; |
| 569 | if (n_sect < m_section_infos.size()) |
| 570 | { |
| 571 | if (m_section_infos[n_sect].section == NULL) |
| 572 | { |
| 573 | Section *section = m_section_list->FindSectionByID (n_sect).get(); |
| 574 | m_section_infos[n_sect].section = section; |
| 575 | assert (section != NULL); |
| 576 | m_section_infos[n_sect].vm_range.SetBaseAddress (section->GetFileAddress()); |
| 577 | m_section_infos[n_sect].vm_range.SetByteSize (section->GetByteSize()); |
| 578 | } |
| 579 | if (m_section_infos[n_sect].vm_range.Contains(file_addr)) |
| 580 | return m_section_infos[n_sect].section; |
| 581 | } |
| 582 | return m_section_list->FindSectionContainingFileAddress(file_addr).get(); |
| 583 | } |
| 584 | |
| 585 | protected: |
| 586 | struct SectionInfo |
| 587 | { |
| 588 | SectionInfo () : |
| 589 | vm_range(), |
| 590 | section (NULL) |
| 591 | { |
| 592 | } |
| 593 | |
| 594 | VMRange vm_range; |
| 595 | Section *section; |
| 596 | }; |
| 597 | SectionList *m_section_list; |
| 598 | std::vector<SectionInfo> m_section_infos; |
| 599 | }; |
| 600 | |
| 601 | |
| 602 | |
| 603 | size_t |
| 604 | ObjectFileMachO::ParseSymtab (bool minimize) |
| 605 | { |
| 606 | Timer scoped_timer(__PRETTY_FUNCTION__, |
| 607 | "ObjectFileMachO::ParseSymtab () module = %s", |
| 608 | m_file.GetFilename().AsCString("")); |
| 609 | struct symtab_command symtab_load_command; |
| 610 | uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 611 | uint32_t i; |
| 612 | for (i=0; i<m_header.ncmds; ++i) |
| 613 | { |
| 614 | const uint32_t cmd_offset = offset; |
| 615 | // Read in the load command and load command size |
| 616 | if (m_data.GetU32(&offset, &symtab_load_command, 2) == NULL) |
| 617 | break; |
| 618 | // Watch for the symbol table load command |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 619 | if (symtab_load_command.cmd == LoadCommandSymtab) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 620 | { |
| 621 | // Read in the rest of the symtab load command |
Jason Molenda | ccfba72 | 2010-07-06 22:38:03 +0000 | [diff] [blame] | 622 | if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4)) // fill in symoff, nsyms, stroff, strsize fields |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 623 | { |
| 624 | Symtab *symtab = m_symtab_ap.get(); |
| 625 | SectionList *section_list = GetSectionList(); |
| 626 | assert(section_list); |
| 627 | const size_t addr_size = m_data.GetAddressByteSize(); |
| 628 | const ByteOrder endian = m_data.GetByteOrder(); |
| 629 | bool bit_width_32 = addr_size == 4; |
| 630 | const size_t nlist_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64); |
| 631 | |
| 632 | DataBufferSP symtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.symoff, symtab_load_command.nsyms * nlist_size)); |
| 633 | DataBufferSP strtab_data_sp(m_file.ReadFileContents(m_offset + symtab_load_command.stroff, symtab_load_command.strsize)); |
| 634 | |
| 635 | const char *strtab_data = (const char *)strtab_data_sp->GetBytes(); |
| 636 | // DataExtractor symtab_data(symtab_data_sp, endian, addr_size); |
| 637 | // DataExtractor strtab_data(strtab_data_sp, endian, addr_size); |
| 638 | |
| 639 | static ConstString g_segment_name_TEXT ("__TEXT"); |
| 640 | static ConstString g_segment_name_DATA ("__DATA"); |
| 641 | static ConstString g_segment_name_OBJC ("__OBJC"); |
| 642 | static ConstString g_section_name_eh_frame ("__eh_frame"); |
| 643 | SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT)); |
| 644 | SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA)); |
| 645 | SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC)); |
| 646 | SectionSP eh_frame_section_sp; |
| 647 | if (text_section_sp.get()) |
| 648 | eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame); |
| 649 | else |
| 650 | eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame); |
| 651 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 652 | uint8_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 653 | //uint32_t symtab_offset = 0; |
| 654 | const uint8_t* nlist_data = symtab_data_sp->GetBytes(); |
| 655 | assert (symtab_data_sp->GetByteSize()/nlist_size >= symtab_load_command.nsyms); |
| 656 | |
| 657 | |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 658 | if (endian != lldb::endian::InlHostByteOrder()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 659 | { |
| 660 | // ... |
| 661 | assert (!"UNIMPLEMENTED: Swap all nlist entries"); |
| 662 | } |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 663 | uint32_t N_SO_index = UINT32_MAX; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 664 | |
| 665 | MachSymtabSectionInfo section_info (section_list); |
| 666 | std::vector<uint32_t> N_FUN_indexes; |
| 667 | std::vector<uint32_t> N_NSYM_indexes; |
| 668 | std::vector<uint32_t> N_INCL_indexes; |
| 669 | std::vector<uint32_t> N_BRAC_indexes; |
| 670 | std::vector<uint32_t> N_COMM_indexes; |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 671 | typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap; |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 672 | typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap; |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 673 | ValueToSymbolIndexMap N_FUN_addr_to_sym_idx; |
| 674 | ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx; |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 675 | // Any symbols that get merged into another will get an entry |
| 676 | // in this map so we know |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 677 | NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 678 | uint32_t nlist_idx = 0; |
| 679 | Symbol *symbol_ptr = NULL; |
| 680 | |
| 681 | uint32_t sym_idx = 0; |
| 682 | Symbol *sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms); |
| 683 | uint32_t num_syms = symtab->GetNumSymbols(); |
| 684 | |
| 685 | //symtab->Reserve (symtab_load_command.nsyms + m_dysymtab.nindirectsyms); |
| 686 | for (nlist_idx = 0; nlist_idx < symtab_load_command.nsyms; ++nlist_idx) |
| 687 | { |
| 688 | struct nlist_64 nlist; |
| 689 | if (bit_width_32) |
| 690 | { |
| 691 | struct nlist* nlist32_ptr = (struct nlist*)(nlist_data + (nlist_idx * nlist_size)); |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 692 | nlist.n_strx = nlist32_ptr->n_strx; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 693 | nlist.n_type = nlist32_ptr->n_type; |
| 694 | nlist.n_sect = nlist32_ptr->n_sect; |
| 695 | nlist.n_desc = nlist32_ptr->n_desc; |
| 696 | nlist.n_value = nlist32_ptr->n_value; |
| 697 | } |
| 698 | else |
| 699 | { |
| 700 | nlist = *((struct nlist_64*)(nlist_data + (nlist_idx * nlist_size))); |
| 701 | } |
| 702 | |
| 703 | SymbolType type = eSymbolTypeInvalid; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 704 | const char* symbol_name = &strtab_data[nlist.n_strx]; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 705 | if (symbol_name[0] == '\0') |
| 706 | symbol_name = NULL; |
| 707 | Section* symbol_section = NULL; |
| 708 | bool add_nlist = true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 709 | bool is_debug = ((nlist.n_type & NlistMaskStab) != 0); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 710 | |
| 711 | assert (sym_idx < num_syms); |
| 712 | |
| 713 | sym[sym_idx].SetDebug (is_debug); |
| 714 | |
| 715 | if (is_debug) |
| 716 | { |
| 717 | switch (nlist.n_type) |
| 718 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 719 | case StabGlobalSymbol: |
| 720 | // N_GSYM -- global symbol: name,,NO_SECT,type,0 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 721 | // Sometimes the N_GSYM value contains the address. |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 722 | sym[sym_idx].SetExternal(true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 723 | if (nlist.n_value != 0) |
| 724 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 725 | type = eSymbolTypeData; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 726 | break; |
| 727 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 728 | case StabFunctionName: |
| 729 | // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0 |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 730 | type = eSymbolTypeCompiler; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 731 | break; |
| 732 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 733 | case StabFunction: |
| 734 | // N_FUN -- procedure: name,,n_sect,linenumber,address |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 735 | if (symbol_name) |
| 736 | { |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 737 | type = eSymbolTypeCode; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 738 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 739 | |
| 740 | N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 741 | // We use the current number of symbols in the symbol table in lieu of |
| 742 | // using nlist_idx in case we ever start trimming entries out |
| 743 | N_FUN_indexes.push_back(sym_idx); |
| 744 | } |
| 745 | else |
| 746 | { |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 747 | type = eSymbolTypeCompiler; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 748 | |
| 749 | if ( !N_FUN_indexes.empty() ) |
| 750 | { |
| 751 | // Copy the size of the function into the original STAB entry so we don't have |
| 752 | // to hunt for it later |
| 753 | symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value); |
| 754 | N_FUN_indexes.pop_back(); |
Jason Molenda | ccfba72 | 2010-07-06 22:38:03 +0000 | [diff] [blame] | 755 | // We don't really need the end function STAB as it contains the size which |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 756 | // we already placed with the original symbol, so don't add it if we want a |
| 757 | // minimal symbol table |
| 758 | if (minimize) |
| 759 | add_nlist = false; |
| 760 | } |
| 761 | } |
| 762 | break; |
| 763 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 764 | case StabStaticSymbol: |
| 765 | // N_STSYM -- static symbol: name,,n_sect,type,address |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 766 | N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 767 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 768 | type = eSymbolTypeData; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 769 | break; |
| 770 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 771 | case StabLocalCommon: |
| 772 | // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 773 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 774 | type = eSymbolTypeCommonBlock; |
| 775 | break; |
| 776 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 777 | case StabBeginSymbol: |
| 778 | // N_BNSYM |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 779 | // We use the current number of symbols in the symbol table in lieu of |
| 780 | // using nlist_idx in case we ever start trimming entries out |
| 781 | if (minimize) |
| 782 | { |
| 783 | // Skip these if we want minimal symbol tables |
| 784 | add_nlist = false; |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 789 | N_NSYM_indexes.push_back(sym_idx); |
| 790 | type = eSymbolTypeScopeBegin; |
| 791 | } |
| 792 | break; |
| 793 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 794 | case StabEndSymbol: |
| 795 | // N_ENSYM |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 796 | // Set the size of the N_BNSYM to the terminating index of this N_ENSYM |
| 797 | // so that we can always skip the entire symbol if we need to navigate |
| 798 | // more quickly at the source level when parsing STABS |
| 799 | if (minimize) |
| 800 | { |
| 801 | // Skip these if we want minimal symbol tables |
| 802 | add_nlist = false; |
| 803 | } |
| 804 | else |
| 805 | { |
| 806 | if ( !N_NSYM_indexes.empty() ) |
| 807 | { |
| 808 | symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back()); |
| 809 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 810 | symbol_ptr->SetSizeIsSibling(true); |
| 811 | N_NSYM_indexes.pop_back(); |
| 812 | } |
| 813 | type = eSymbolTypeScopeEnd; |
| 814 | } |
| 815 | break; |
| 816 | |
| 817 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 818 | case StabSourceFileOptions: |
| 819 | // N_OPT - emitted with gcc2_compiled and in gcc source |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 820 | type = eSymbolTypeCompiler; |
| 821 | break; |
| 822 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 823 | case StabRegisterSymbol: |
| 824 | // N_RSYM - register sym: name,,NO_SECT,type,register |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 825 | type = eSymbolTypeVariable; |
| 826 | break; |
| 827 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 828 | case StabSourceLine: |
| 829 | // N_SLINE - src line: 0,,n_sect,linenumber,address |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 830 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 831 | type = eSymbolTypeLineEntry; |
| 832 | break; |
| 833 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 834 | case StabStructureType: |
| 835 | // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 836 | type = eSymbolTypeVariableType; |
| 837 | break; |
| 838 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 839 | case StabSourceFileName: |
| 840 | // N_SO - source file name |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 841 | type = eSymbolTypeSourceFile; |
| 842 | if (symbol_name == NULL) |
| 843 | { |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 844 | if (minimize) |
| 845 | add_nlist = false; |
| 846 | if (N_SO_index != UINT32_MAX) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 847 | { |
| 848 | // Set the size of the N_SO to the terminating index of this N_SO |
| 849 | // so that we can always skip the entire N_SO if we need to navigate |
| 850 | // more quickly at the source level when parsing STABS |
| 851 | symbol_ptr = symtab->SymbolAtIndex(N_SO_index); |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 852 | symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 853 | symbol_ptr->SetSizeIsSibling(true); |
| 854 | } |
| 855 | N_NSYM_indexes.clear(); |
| 856 | N_INCL_indexes.clear(); |
| 857 | N_BRAC_indexes.clear(); |
| 858 | N_COMM_indexes.clear(); |
| 859 | N_FUN_indexes.clear(); |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 860 | N_SO_index = UINT32_MAX; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 861 | } |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 862 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 863 | { |
| 864 | // We use the current number of symbols in the symbol table in lieu of |
| 865 | // using nlist_idx in case we ever start trimming entries out |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 866 | if (symbol_name[0] == '/') |
| 867 | N_SO_index = sym_idx; |
| 868 | else if (minimize && (N_SO_index == sym_idx - 1)) |
| 869 | { |
| 870 | const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString(); |
| 871 | if (so_path && so_path[0]) |
| 872 | { |
| 873 | std::string full_so_path (so_path); |
| 874 | if (*full_so_path.rbegin() != '/') |
| 875 | full_so_path += '/'; |
| 876 | full_so_path += symbol_name; |
| 877 | sym[sym_idx - 1].GetMangled().SetValue(full_so_path.c_str(), false); |
| 878 | add_nlist = false; |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 879 | m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 882 | } |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 883 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 884 | break; |
| 885 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 886 | case StabObjectFileName: |
| 887 | // N_OSO - object file name: name,,0,0,st_mtime |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 888 | type = eSymbolTypeObjectFile; |
| 889 | break; |
| 890 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 891 | case StabLocalSymbol: |
| 892 | // N_LSYM - local sym: name,,NO_SECT,type,offset |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 893 | type = eSymbolTypeLocal; |
| 894 | break; |
| 895 | |
| 896 | //---------------------------------------------------------------------- |
| 897 | // INCL scopes |
| 898 | //---------------------------------------------------------------------- |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 899 | case StabBeginIncludeFileName: |
| 900 | // N_BINCL - include file beginning: name,,NO_SECT,0,sum |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 901 | // We use the current number of symbols in the symbol table in lieu of |
| 902 | // using nlist_idx in case we ever start trimming entries out |
| 903 | N_INCL_indexes.push_back(sym_idx); |
| 904 | type = eSymbolTypeScopeBegin; |
| 905 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 906 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 907 | case StabEndIncludeFile: |
| 908 | // N_EINCL - include file end: name,,NO_SECT,0,0 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 909 | // Set the size of the N_BINCL to the terminating index of this N_EINCL |
| 910 | // so that we can always skip the entire symbol if we need to navigate |
| 911 | // more quickly at the source level when parsing STABS |
| 912 | if ( !N_INCL_indexes.empty() ) |
| 913 | { |
| 914 | symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back()); |
| 915 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 916 | symbol_ptr->SetSizeIsSibling(true); |
| 917 | N_INCL_indexes.pop_back(); |
| 918 | } |
| 919 | type = eSymbolTypeScopeEnd; |
| 920 | break; |
| 921 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 922 | case StabIncludeFileName: |
| 923 | // N_SOL - #included file name: name,,n_sect,0,address |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 924 | type = eSymbolTypeHeaderFile; |
Greg Clayton | 0ad086f | 2010-09-07 17:36:17 +0000 | [diff] [blame] | 925 | |
| 926 | // We currently don't use the header files on darwin |
| 927 | if (minimize) |
| 928 | add_nlist = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 929 | break; |
| 930 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 931 | case StabCompilerParameters: |
| 932 | // N_PARAMS - compiler parameters: name,,NO_SECT,0,0 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 933 | type = eSymbolTypeCompiler; |
| 934 | break; |
| 935 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 936 | case StabCompilerVersion: |
| 937 | // N_VERSION - compiler version: name,,NO_SECT,0,0 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 938 | type = eSymbolTypeCompiler; |
| 939 | break; |
| 940 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 941 | case StabCompilerOptLevel: |
| 942 | // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 943 | type = eSymbolTypeCompiler; |
| 944 | break; |
| 945 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 946 | case StabParameter: |
| 947 | // N_PSYM - parameter: name,,NO_SECT,type,offset |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 948 | type = eSymbolTypeVariable; |
| 949 | break; |
| 950 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 951 | case StabAlternateEntry: |
| 952 | // N_ENTRY - alternate entry: name,,n_sect,linenumber,address |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 953 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 954 | type = eSymbolTypeLineEntry; |
| 955 | break; |
| 956 | |
| 957 | //---------------------------------------------------------------------- |
| 958 | // Left and Right Braces |
| 959 | //---------------------------------------------------------------------- |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 960 | case StabLeftBracket: |
| 961 | // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 962 | // We use the current number of symbols in the symbol table in lieu of |
| 963 | // using nlist_idx in case we ever start trimming entries out |
| 964 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 965 | N_BRAC_indexes.push_back(sym_idx); |
| 966 | type = eSymbolTypeScopeBegin; |
| 967 | break; |
| 968 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 969 | case StabRightBracket: |
| 970 | // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 971 | // Set the size of the N_LBRAC to the terminating index of this N_RBRAC |
| 972 | // so that we can always skip the entire symbol if we need to navigate |
| 973 | // more quickly at the source level when parsing STABS |
| 974 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 975 | if ( !N_BRAC_indexes.empty() ) |
| 976 | { |
| 977 | symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back()); |
| 978 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 979 | symbol_ptr->SetSizeIsSibling(true); |
| 980 | N_BRAC_indexes.pop_back(); |
| 981 | } |
| 982 | type = eSymbolTypeScopeEnd; |
| 983 | break; |
| 984 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 985 | case StabDeletedIncludeFile: |
| 986 | // N_EXCL - deleted include file: name,,NO_SECT,0,sum |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 987 | type = eSymbolTypeHeaderFile; |
| 988 | break; |
| 989 | |
| 990 | //---------------------------------------------------------------------- |
| 991 | // COMM scopes |
| 992 | //---------------------------------------------------------------------- |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 993 | case StabBeginCommon: |
| 994 | // N_BCOMM - begin common: name,,NO_SECT,0,0 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 995 | // We use the current number of symbols in the symbol table in lieu of |
| 996 | // using nlist_idx in case we ever start trimming entries out |
| 997 | type = eSymbolTypeScopeBegin; |
| 998 | N_COMM_indexes.push_back(sym_idx); |
| 999 | break; |
| 1000 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1001 | case StabEndCommonLocal: |
| 1002 | // N_ECOML - end common (local name): 0,,n_sect,0,address |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1003 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 1004 | // Fall through |
| 1005 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1006 | case StabEndCommon: |
| 1007 | // N_ECOMM - end common: name,,n_sect,0,0 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1008 | // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML |
| 1009 | // so that we can always skip the entire symbol if we need to navigate |
| 1010 | // more quickly at the source level when parsing STABS |
| 1011 | if ( !N_COMM_indexes.empty() ) |
| 1012 | { |
| 1013 | symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back()); |
| 1014 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 1015 | symbol_ptr->SetSizeIsSibling(true); |
| 1016 | N_COMM_indexes.pop_back(); |
| 1017 | } |
| 1018 | type = eSymbolTypeScopeEnd; |
| 1019 | break; |
| 1020 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1021 | case StabLength: |
| 1022 | // N_LENG - second stab entry with length information |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1023 | type = eSymbolTypeAdditional; |
| 1024 | break; |
| 1025 | |
| 1026 | default: break; |
| 1027 | } |
| 1028 | } |
| 1029 | else |
| 1030 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1031 | //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type; |
| 1032 | uint8_t n_type = NlistMaskType & nlist.n_type; |
| 1033 | sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1034 | |
| 1035 | if (symbol_name && ::strstr (symbol_name, ".objc") == symbol_name) |
| 1036 | { |
| 1037 | type = eSymbolTypeRuntime; |
| 1038 | } |
| 1039 | else |
| 1040 | { |
| 1041 | switch (n_type) |
| 1042 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1043 | case NListTypeIndirect: // N_INDR - Fall through |
| 1044 | case NListTypePreboundUndefined:// N_PBUD - Fall through |
| 1045 | case NListTypeUndefined: // N_UNDF |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1046 | type = eSymbolTypeExtern; |
| 1047 | break; |
| 1048 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1049 | case NListTypeAbsolute: // N_ABS |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1050 | type = eSymbolTypeAbsolute; |
| 1051 | break; |
| 1052 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1053 | case NListTypeSection: // N_SECT |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1054 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 1055 | |
| 1056 | assert(symbol_section != NULL); |
| 1057 | if (TEXT_eh_frame_sectID == nlist.n_sect) |
| 1058 | { |
| 1059 | type = eSymbolTypeException; |
| 1060 | } |
| 1061 | else |
| 1062 | { |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 1063 | uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1064 | |
| 1065 | switch (section_type) |
| 1066 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1067 | case SectionTypeRegular: break; // regular section |
| 1068 | //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section |
| 1069 | case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings |
| 1070 | case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals |
| 1071 | case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals |
| 1072 | case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals |
| 1073 | case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers |
| 1074 | case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers |
| 1075 | case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field |
| 1076 | case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization |
| 1077 | case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination |
| 1078 | //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced |
| 1079 | //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes) |
| 1080 | case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing |
| 1081 | case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals |
| 1082 | case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break; |
| 1083 | case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1084 | default: break; |
| 1085 | } |
| 1086 | |
| 1087 | if (type == eSymbolTypeInvalid) |
| 1088 | { |
| 1089 | const char *symbol_sect_name = symbol_section->GetName().AsCString(); |
| 1090 | if (symbol_section->IsDescendant (text_section_sp.get())) |
| 1091 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1092 | if (symbol_section->IsClear(SectionAttrUserPureInstructions | |
| 1093 | SectionAttrUserSelfModifyingCode | |
| 1094 | SectionAttrSytemSomeInstructions)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1095 | type = eSymbolTypeData; |
| 1096 | else |
| 1097 | type = eSymbolTypeCode; |
| 1098 | } |
| 1099 | else |
| 1100 | if (symbol_section->IsDescendant(data_section_sp.get())) |
| 1101 | { |
| 1102 | if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name) |
| 1103 | { |
| 1104 | type = eSymbolTypeRuntime; |
| 1105 | } |
| 1106 | else |
| 1107 | if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name) |
| 1108 | { |
| 1109 | type = eSymbolTypeException; |
| 1110 | } |
| 1111 | else |
| 1112 | { |
| 1113 | type = eSymbolTypeData; |
| 1114 | } |
| 1115 | } |
| 1116 | else |
| 1117 | if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name) |
| 1118 | { |
| 1119 | type = eSymbolTypeTrampoline; |
| 1120 | } |
| 1121 | else |
| 1122 | if (symbol_section->IsDescendant(objc_section_sp.get())) |
| 1123 | { |
| 1124 | type = eSymbolTypeRuntime; |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | break; |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 1129 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1130 | } |
| 1131 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1132 | if (add_nlist) |
| 1133 | { |
| 1134 | bool symbol_name_is_mangled = false; |
| 1135 | if (symbol_name && symbol_name[0] == '_') |
| 1136 | { |
| 1137 | symbol_name_is_mangled = symbol_name[1] == '_'; |
| 1138 | symbol_name++; // Skip the leading underscore |
| 1139 | } |
| 1140 | uint64_t symbol_value = nlist.n_value; |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 1141 | |
| 1142 | if (symbol_name) |
| 1143 | sym[sym_idx].GetMangled().SetValue(symbol_name, symbol_name_is_mangled); |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1144 | if (is_debug == false) |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 1145 | { |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1146 | if (type == eSymbolTypeCode) |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 1147 | { |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1148 | // See if we can find a N_FUN entry for any code symbols. |
| 1149 | // If we do find a match, and the name matches, then we |
| 1150 | // can merge the two into just the function symbol to avoid |
| 1151 | // duplicate entries in the symbol table |
| 1152 | ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value); |
| 1153 | if (pos != N_FUN_addr_to_sym_idx.end()) |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 1154 | { |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1155 | if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) || |
| 1156 | (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName())) |
| 1157 | { |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 1158 | m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1159 | // We just need the flags from the linker symbol, so put these flags |
| 1160 | // into the N_FUN flags to avoid duplicate symbols in the symbol table |
| 1161 | sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); |
| 1162 | sym[sym_idx].Clear(); |
| 1163 | continue; |
| 1164 | } |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 1165 | } |
| 1166 | } |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1167 | else if (type == eSymbolTypeData) |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 1168 | { |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1169 | // See if we can find a N_STSYM entry for any data symbols. |
| 1170 | // If we do find a match, and the name matches, then we |
| 1171 | // can merge the two into just the Static symbol to avoid |
| 1172 | // duplicate entries in the symbol table |
| 1173 | ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value); |
| 1174 | if (pos != N_STSYM_addr_to_sym_idx.end()) |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 1175 | { |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1176 | if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) || |
| 1177 | (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName())) |
| 1178 | { |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 1179 | m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1180 | // We just need the flags from the linker symbol, so put these flags |
| 1181 | // into the N_STSYM flags to avoid duplicate symbols in the symbol table |
| 1182 | sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); |
| 1183 | sym[sym_idx].Clear(); |
| 1184 | continue; |
| 1185 | } |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1189 | if (symbol_section != NULL) |
| 1190 | symbol_value -= symbol_section->GetFileAddress(); |
| 1191 | |
| 1192 | sym[sym_idx].SetID (nlist_idx); |
| 1193 | sym[sym_idx].SetType (type); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1194 | sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetSection (symbol_section); |
| 1195 | sym[sym_idx].GetAddressRangeRef().GetBaseAddress().SetOffset (symbol_value); |
| 1196 | sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); |
| 1197 | |
| 1198 | ++sym_idx; |
| 1199 | } |
| 1200 | else |
| 1201 | { |
| 1202 | sym[sym_idx].Clear(); |
| 1203 | } |
| 1204 | |
| 1205 | } |
| 1206 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1207 | // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value |
| 1208 | // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all |
| 1209 | // such entries by figuring out what the address for the global is by looking up this non-STAB |
| 1210 | // entry and copying the value into the debug symbol's value to save us the hassle in the |
| 1211 | // debug symbol parser. |
| 1212 | |
| 1213 | Symbol *global_symbol = NULL; |
| 1214 | for (nlist_idx = 0; |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1215 | nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1216 | nlist_idx++) |
| 1217 | { |
| 1218 | if (global_symbol->GetValue().GetFileAddress() == 0) |
| 1219 | { |
| 1220 | std::vector<uint32_t> indexes; |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1221 | if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1222 | { |
| 1223 | std::vector<uint32_t>::const_iterator pos; |
| 1224 | std::vector<uint32_t>::const_iterator end = indexes.end(); |
| 1225 | for (pos = indexes.begin(); pos != end; ++pos) |
| 1226 | { |
| 1227 | symbol_ptr = symtab->SymbolAtIndex(*pos); |
| 1228 | if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false) |
| 1229 | { |
| 1230 | global_symbol->SetValue(symbol_ptr->GetValue()); |
| 1231 | break; |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | } |
| 1236 | } |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 1237 | |
| 1238 | // Trim our symbols down to just what we ended up with after |
| 1239 | // removing any symbols. |
| 1240 | if (sym_idx < num_syms) |
| 1241 | { |
| 1242 | num_syms = sym_idx; |
| 1243 | sym = symtab->Resize (num_syms); |
| 1244 | } |
| 1245 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1246 | // Now synthesize indirect symbols |
| 1247 | if (m_dysymtab.nindirectsyms != 0) |
| 1248 | { |
| 1249 | DataBufferSP indirect_symbol_indexes_sp(m_file.ReadFileContents(m_offset + m_dysymtab.indirectsymoff, m_dysymtab.nindirectsyms * 4)); |
| 1250 | |
| 1251 | if (indirect_symbol_indexes_sp && indirect_symbol_indexes_sp->GetByteSize()) |
| 1252 | { |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 1253 | NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1254 | DataExtractor indirect_symbol_index_data (indirect_symbol_indexes_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize()); |
| 1255 | |
| 1256 | for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx) |
| 1257 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1258 | if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1259 | { |
| 1260 | uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2; |
| 1261 | if (symbol_stub_byte_size == 0) |
| 1262 | continue; |
| 1263 | |
| 1264 | const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size; |
| 1265 | |
| 1266 | if (num_symbol_stubs == 0) |
| 1267 | continue; |
| 1268 | |
| 1269 | const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1; |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 1270 | uint32_t synthetic_stub_sym_id = symtab_load_command.nsyms; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1271 | for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) |
| 1272 | { |
| 1273 | const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx; |
| 1274 | const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size); |
| 1275 | uint32_t symbol_stub_offset = symbol_stub_index * 4; |
| 1276 | if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4)) |
| 1277 | { |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 1278 | const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset); |
Greg Clayton | 6af4fad | 2010-10-06 01:26:32 +0000 | [diff] [blame] | 1279 | if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal)) |
| 1280 | continue; |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1281 | |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 1282 | NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id); |
| 1283 | Symbol *stub_symbol = NULL; |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 1284 | if (index_pos != end_index_pos) |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 1285 | { |
| 1286 | // We have a remapping from the original nlist index to |
| 1287 | // a current symbol index, so just look this up by index |
| 1288 | stub_symbol = symtab->SymbolAtIndex (index_pos->second); |
| 1289 | } |
| 1290 | else |
| 1291 | { |
| 1292 | // We need to lookup a symbol using the original nlist |
| 1293 | // symbol index since this index is coming from the |
| 1294 | // S_SYMBOL_STUBS |
| 1295 | stub_symbol = symtab->FindSymbolByID (stub_sym_id); |
| 1296 | } |
Greg Clayton | 0ad086f | 2010-09-07 17:36:17 +0000 | [diff] [blame] | 1297 | |
| 1298 | assert (stub_symbol); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1299 | if (stub_symbol) |
| 1300 | { |
| 1301 | Address so_addr(symbol_stub_addr, section_list); |
| 1302 | |
| 1303 | if (stub_symbol->GetType() == eSymbolTypeExtern) |
| 1304 | { |
| 1305 | // Change the external symbol into a trampoline that makes sense |
| 1306 | // These symbols were N_UNDF N_EXT, and are useless to us, so we |
| 1307 | // can re-use them so we don't have to make up a synthetic symbol |
| 1308 | // for no good reason. |
| 1309 | stub_symbol->SetType (eSymbolTypeTrampoline); |
| 1310 | stub_symbol->SetExternal (false); |
| 1311 | stub_symbol->GetAddressRangeRef().GetBaseAddress() = so_addr; |
| 1312 | stub_symbol->GetAddressRangeRef().SetByteSize (symbol_stub_byte_size); |
| 1313 | } |
| 1314 | else |
| 1315 | { |
| 1316 | // Make a synthetic symbol to describe the trampoline stub |
| 1317 | if (sym_idx >= num_syms) |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 1318 | sym = symtab->Resize (++num_syms); |
| 1319 | sym[sym_idx].SetID (synthetic_stub_sym_id++); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1320 | sym[sym_idx].GetMangled() = stub_symbol->GetMangled(); |
| 1321 | sym[sym_idx].SetType (eSymbolTypeTrampoline); |
| 1322 | sym[sym_idx].SetIsSynthetic (true); |
| 1323 | sym[sym_idx].GetAddressRangeRef().GetBaseAddress() = so_addr; |
| 1324 | sym[sym_idx].GetAddressRangeRef().SetByteSize (symbol_stub_byte_size); |
| 1325 | ++sym_idx; |
| 1326 | } |
| 1327 | } |
| 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1335 | return symtab->GetNumSymbols(); |
| 1336 | } |
| 1337 | } |
| 1338 | offset = cmd_offset + symtab_load_command.cmdsize; |
| 1339 | } |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | |
| 1344 | void |
| 1345 | ObjectFileMachO::Dump (Stream *s) |
| 1346 | { |
| 1347 | lldb_private::Mutex::Locker locker(m_mutex); |
| 1348 | s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
| 1349 | s->Indent(); |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1350 | if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1351 | s->PutCString("ObjectFileMachO64"); |
| 1352 | else |
| 1353 | s->PutCString("ObjectFileMachO32"); |
| 1354 | |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 1355 | ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1356 | |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 1357 | *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n"; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1358 | |
| 1359 | if (m_sections_ap.get()) |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 1360 | m_sections_ap->Dump(s, NULL, true, UINT32_MAX); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1361 | |
| 1362 | if (m_symtab_ap.get()) |
Greg Clayton | 8d3802d | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 1363 | m_symtab_ap->Dump(s, NULL, eSortOrderNone); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | |
| 1367 | bool |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 1368 | ObjectFileMachO::GetUUID (lldb_private::UUID* uuid) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1369 | { |
| 1370 | lldb_private::Mutex::Locker locker(m_mutex); |
| 1371 | struct uuid_command load_cmd; |
| 1372 | uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 1373 | uint32_t i; |
| 1374 | for (i=0; i<m_header.ncmds; ++i) |
| 1375 | { |
| 1376 | const uint32_t cmd_offset = offset; |
| 1377 | if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 1378 | break; |
| 1379 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1380 | if (load_cmd.cmd == LoadCommandUUID) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1381 | { |
| 1382 | const uint8_t *uuid_bytes = m_data.PeekData(offset, 16); |
| 1383 | if (uuid_bytes) |
| 1384 | { |
| 1385 | uuid->SetBytes (uuid_bytes); |
| 1386 | return true; |
| 1387 | } |
| 1388 | return false; |
| 1389 | } |
| 1390 | offset = cmd_offset + load_cmd.cmdsize; |
| 1391 | } |
| 1392 | return false; |
| 1393 | } |
| 1394 | |
| 1395 | |
| 1396 | uint32_t |
| 1397 | ObjectFileMachO::GetDependentModules (FileSpecList& files) |
| 1398 | { |
| 1399 | lldb_private::Mutex::Locker locker(m_mutex); |
| 1400 | struct load_command load_cmd; |
| 1401 | uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 1402 | uint32_t count = 0; |
| 1403 | uint32_t i; |
| 1404 | for (i=0; i<m_header.ncmds; ++i) |
| 1405 | { |
| 1406 | const uint32_t cmd_offset = offset; |
| 1407 | if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 1408 | break; |
| 1409 | |
| 1410 | switch (load_cmd.cmd) |
| 1411 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1412 | case LoadCommandDylibLoad: |
| 1413 | case LoadCommandDylibLoadWeak: |
| 1414 | case LoadCommandDylibReexport: |
| 1415 | case LoadCommandDynamicLinkerLoad: |
| 1416 | case LoadCommandFixedVMShlibLoad: |
Greg Clayton | 08a7320 | 2010-10-09 00:48:53 +0000 | [diff] [blame] | 1417 | case LoadCommandDylibLoadUpward: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1418 | { |
| 1419 | uint32_t name_offset = cmd_offset + m_data.GetU32(&offset); |
| 1420 | const char *path = m_data.PeekCStr(name_offset); |
| 1421 | // Skip any path that starts with '@' since these are usually: |
| 1422 | // @executable_path/.../file |
| 1423 | // @rpath/.../file |
| 1424 | if (path && path[0] != '@') |
| 1425 | { |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 1426 | FileSpec file_spec(path, true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1427 | if (files.AppendIfUnique(file_spec)) |
| 1428 | count++; |
| 1429 | } |
| 1430 | } |
| 1431 | break; |
| 1432 | |
| 1433 | default: |
| 1434 | break; |
| 1435 | } |
| 1436 | offset = cmd_offset + load_cmd.cmdsize; |
| 1437 | } |
| 1438 | return count; |
| 1439 | } |
| 1440 | |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 1441 | lldb_private::Address |
| 1442 | ObjectFileMachO::GetEntryPointAddress () |
| 1443 | { |
| 1444 | // If the object file is not an executable it can't hold the entry point. m_entry_point_address |
| 1445 | // is initialized to an invalid address, so we can just return that. |
| 1446 | // If m_entry_point_address is valid it means we've found it already, so return the cached value. |
| 1447 | |
| 1448 | if (!IsExecutable() || m_entry_point_address.IsValid()) |
| 1449 | return m_entry_point_address; |
| 1450 | |
| 1451 | // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in |
| 1452 | // /usr/include/mach-o.h, but it is basically: |
| 1453 | // |
| 1454 | // uint32_t flavor - this is the flavor argument you would pass to thread_get_state |
| 1455 | // uint32_t count - this is the count of longs in the thread state data |
| 1456 | // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor. |
| 1457 | // <repeat this trio> |
| 1458 | // |
| 1459 | // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there. |
| 1460 | // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers |
| 1461 | // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin, |
| 1462 | // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here. |
| 1463 | // |
| 1464 | // For now we hard-code the offsets and flavors we need: |
| 1465 | // |
| 1466 | // |
| 1467 | |
| 1468 | lldb_private::Mutex::Locker locker(m_mutex); |
| 1469 | struct load_command load_cmd; |
| 1470 | uint32_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 1471 | uint32_t i; |
| 1472 | lldb::addr_t start_address = LLDB_INVALID_ADDRESS; |
| 1473 | bool done = false; |
| 1474 | |
| 1475 | for (i=0; i<m_header.ncmds; ++i) |
| 1476 | { |
| 1477 | const uint32_t cmd_offset = offset; |
| 1478 | if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 1479 | break; |
| 1480 | |
| 1481 | switch (load_cmd.cmd) |
| 1482 | { |
| 1483 | case LoadCommandUnixThread: |
| 1484 | case LoadCommandThread: |
| 1485 | { |
| 1486 | while (offset < cmd_offset + load_cmd.cmdsize) |
| 1487 | { |
| 1488 | uint32_t flavor = m_data.GetU32(&offset); |
| 1489 | uint32_t count = m_data.GetU32(&offset); |
| 1490 | if (count == 0) |
| 1491 | { |
| 1492 | // We've gotten off somehow, log and exit; |
| 1493 | return m_entry_point_address; |
| 1494 | } |
| 1495 | |
| 1496 | switch (m_header.cputype) |
| 1497 | { |
| 1498 | case llvm::MachO::CPUTypeARM: |
| 1499 | if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h |
| 1500 | { |
| 1501 | offset += 60; // This is the offset of pc in the GPR thread state data structure. |
| 1502 | start_address = m_data.GetU32(&offset); |
| 1503 | done = true; |
| 1504 | } |
| 1505 | break; |
| 1506 | case llvm::MachO::CPUTypeI386: |
| 1507 | if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h |
| 1508 | { |
| 1509 | offset += 40; // This is the offset of eip in the GPR thread state data structure. |
| 1510 | start_address = m_data.GetU32(&offset); |
| 1511 | done = true; |
| 1512 | } |
| 1513 | break; |
| 1514 | case llvm::MachO::CPUTypeX86_64: |
| 1515 | if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h |
| 1516 | { |
| 1517 | offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure. |
| 1518 | start_address = m_data.GetU64(&offset); |
| 1519 | done = true; |
| 1520 | } |
| 1521 | break; |
| 1522 | default: |
| 1523 | return m_entry_point_address; |
| 1524 | } |
| 1525 | // Haven't found the GPR flavor yet, skip over the data for this flavor: |
| 1526 | if (done) |
| 1527 | break; |
| 1528 | offset += count * 4; |
| 1529 | } |
| 1530 | } |
| 1531 | break; |
| 1532 | |
| 1533 | default: |
| 1534 | break; |
| 1535 | } |
| 1536 | if (done) |
| 1537 | break; |
| 1538 | |
| 1539 | // Go to the next load command: |
| 1540 | offset = cmd_offset + load_cmd.cmdsize; |
| 1541 | } |
| 1542 | |
| 1543 | if (start_address != LLDB_INVALID_ADDRESS) |
| 1544 | { |
| 1545 | // We got the start address from the load commands, so now resolve that address in the sections |
| 1546 | // of this ObjectFile: |
| 1547 | if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList())) |
| 1548 | { |
| 1549 | m_entry_point_address.Clear(); |
| 1550 | } |
| 1551 | } |
| 1552 | else |
| 1553 | { |
| 1554 | // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the |
| 1555 | // "start" symbol in the main executable. |
| 1556 | |
| 1557 | SymbolContextList contexts; |
| 1558 | SymbolContext context; |
| 1559 | if (!m_module->FindSymbolsWithNameAndType(ConstString ("start"), lldb::eSymbolTypeCode, contexts)) |
| 1560 | return m_entry_point_address; |
| 1561 | |
| 1562 | contexts.GetContextAtIndex(0, context); |
| 1563 | |
| 1564 | m_entry_point_address = context.symbol->GetValue(); |
| 1565 | } |
| 1566 | |
| 1567 | return m_entry_point_address; |
| 1568 | |
| 1569 | } |
| 1570 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1571 | bool |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 1572 | ObjectFileMachO::GetArchitecture (ArchSpec &arch) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1573 | { |
| 1574 | lldb_private::Mutex::Locker locker(m_mutex); |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 1575 | arch.SetArchitecture (lldb::eArchTypeMachO, m_header.cputype, m_header.cpusubtype); |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 1576 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | |
| 1580 | //------------------------------------------------------------------ |
| 1581 | // PluginInterface protocol |
| 1582 | //------------------------------------------------------------------ |
| 1583 | const char * |
| 1584 | ObjectFileMachO::GetPluginName() |
| 1585 | { |
| 1586 | return "ObjectFileMachO"; |
| 1587 | } |
| 1588 | |
| 1589 | const char * |
| 1590 | ObjectFileMachO::GetShortPluginName() |
| 1591 | { |
| 1592 | return GetPluginNameStatic(); |
| 1593 | } |
| 1594 | |
| 1595 | uint32_t |
| 1596 | ObjectFileMachO::GetPluginVersion() |
| 1597 | { |
| 1598 | return 1; |
| 1599 | } |
| 1600 | |
| 1601 | void |
| 1602 | ObjectFileMachO::GetPluginCommandHelp (const char *command, Stream *strm) |
| 1603 | { |
| 1604 | } |
| 1605 | |
| 1606 | Error |
| 1607 | ObjectFileMachO::ExecutePluginCommand (Args &command, Stream *strm) |
| 1608 | { |
| 1609 | Error error; |
| 1610 | error.SetErrorString("No plug-in command are currently supported."); |
| 1611 | return error; |
| 1612 | } |
| 1613 | |
| 1614 | Log * |
| 1615 | ObjectFileMachO::EnablePluginLogging (Stream *strm, Args &command) |
| 1616 | { |
| 1617 | return NULL; |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | |
| 1622 | |