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