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