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