Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1 | //===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ObjectFileELF.h" |
| 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include <algorithm> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | #include <cassert> |
Tamas Berghammer | 9fa1147 | 2015-10-27 10:43:27 +0000 | [diff] [blame] | 14 | #include <unordered_map> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 16 | #include "lldb/Core/ArchSpec.h" |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 17 | #include "lldb/Core/FileSpecList.h" |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Module.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 19 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Core/PluginManager.h" |
| 21 | #include "lldb/Core/Section.h" |
Ashok Thirumurthi | 35729bb | 2013-09-24 15:34:13 +0000 | [diff] [blame] | 22 | #include "lldb/Symbol/DWARFCallFrameInfo.h" |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/SymbolContext.h" |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 24 | #include "lldb/Target/SectionLoadList.h" |
Ed Maste | 5480365 | 2013-10-11 17:39:07 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Target.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 26 | #include "lldb/Utility/DataBufferLLVM.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 27 | #include "lldb/Utility/Log.h" |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/Status.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 29 | #include "lldb/Utility/Stream.h" |
Pavel Labath | 38d0632 | 2017-06-29 14:32:17 +0000 | [diff] [blame] | 30 | #include "lldb/Utility/Timer.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/PointerUnion.h" |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringRef.h" |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ARMBuildAttributes.h" |
Zachary Turner | 736d4d8 | 2014-06-25 05:42:32 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MathExtras.h" |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MemoryBuffer.h" |
Sagar Thakur | ad5b55a | 2016-05-24 14:52:50 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MipsABIFlags.h" |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 38 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | #define CASE_AND_STREAM(s, def, width) \ |
| 40 | case def: \ |
| 41 | s->Printf("%-*s", width, #def); \ |
| 42 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | using namespace lldb; |
| 45 | using namespace lldb_private; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 46 | using namespace elf; |
| 47 | using namespace llvm::ELF; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 49 | namespace { |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 50 | |
| 51 | // ELF note owner definitions |
| 52 | const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | const char *const LLDB_NT_OWNER_GNU = "GNU"; |
| 54 | const char *const LLDB_NT_OWNER_NETBSD = "NetBSD"; |
Kamil Rytarowski | 12801f1 | 2017-03-26 15:34:57 +0000 | [diff] [blame] | 55 | const char *const LLDB_NT_OWNER_OPENBSD = "OpenBSD"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | const char *const LLDB_NT_OWNER_CSR = "csr"; |
Tamas Berghammer | db037d9 | 2015-03-18 10:36:27 +0000 | [diff] [blame] | 57 | const char *const LLDB_NT_OWNER_ANDROID = "Android"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | const char *const LLDB_NT_OWNER_CORE = "CORE"; |
| 59 | const char *const LLDB_NT_OWNER_LINUX = "LINUX"; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 60 | |
| 61 | // ELF note type definitions |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | const elf_word LLDB_NT_FREEBSD_ABI_TAG = 0x01; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 63 | const elf_word LLDB_NT_FREEBSD_ABI_SIZE = 4; |
| 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | const elf_word LLDB_NT_GNU_ABI_TAG = 0x01; |
| 66 | const elf_word LLDB_NT_GNU_ABI_SIZE = 16; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 67 | |
| 68 | const elf_word LLDB_NT_GNU_BUILD_ID_TAG = 0x03; |
| 69 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | const elf_word LLDB_NT_NETBSD_ABI_TAG = 0x01; |
| 71 | const elf_word LLDB_NT_NETBSD_ABI_SIZE = 4; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 72 | |
| 73 | // GNU ABI note OS constants |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | const elf_word LLDB_NT_GNU_ABI_OS_LINUX = 0x00; |
| 75 | const elf_word LLDB_NT_GNU_ABI_OS_HURD = 0x01; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 76 | const elf_word LLDB_NT_GNU_ABI_OS_SOLARIS = 0x02; |
| 77 | |
Greg Clayton | b704b69 | 2015-10-28 18:04:38 +0000 | [diff] [blame] | 78 | // LLDB_NT_OWNER_CORE and LLDB_NT_OWNER_LINUX note contants |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | #define NT_PRSTATUS 1 |
| 80 | #define NT_PRFPREG 2 |
| 81 | #define NT_PRPSINFO 3 |
| 82 | #define NT_TASKSTRUCT 4 |
| 83 | #define NT_AUXV 6 |
| 84 | #define NT_SIGINFO 0x53494749 |
| 85 | #define NT_FILE 0x46494c45 |
| 86 | #define NT_PRXFPREG 0x46e62b7f |
| 87 | #define NT_PPC_VMX 0x100 |
| 88 | #define NT_PPC_SPE 0x101 |
| 89 | #define NT_PPC_VSX 0x102 |
| 90 | #define NT_386_TLS 0x200 |
| 91 | #define NT_386_IOPERM 0x201 |
| 92 | #define NT_X86_XSTATE 0x202 |
| 93 | #define NT_S390_HIGH_GPRS 0x300 |
| 94 | #define NT_S390_TIMER 0x301 |
| 95 | #define NT_S390_TODCMP 0x302 |
| 96 | #define NT_S390_TODPREG 0x303 |
| 97 | #define NT_S390_CTRS 0x304 |
| 98 | #define NT_S390_PREFIX 0x305 |
| 99 | #define NT_S390_LAST_BREAK 0x306 |
| 100 | #define NT_S390_SYSTEM_CALL 0x307 |
| 101 | #define NT_S390_TDB 0x308 |
| 102 | #define NT_S390_VXRS_LOW 0x309 |
| 103 | #define NT_S390_VXRS_HIGH 0x30a |
| 104 | #define NT_ARM_VFP 0x400 |
| 105 | #define NT_ARM_TLS 0x401 |
| 106 | #define NT_ARM_HW_BREAK 0x402 |
| 107 | #define NT_ARM_HW_WATCH 0x403 |
| 108 | #define NT_ARM_SYSTEM_CALL 0x404 |
| 109 | #define NT_METAG_CBUF 0x500 |
| 110 | #define NT_METAG_RPIPE 0x501 |
| 111 | #define NT_METAG_TLS 0x502 |
Greg Clayton | b704b69 | 2015-10-28 18:04:38 +0000 | [diff] [blame] | 112 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 113 | //===----------------------------------------------------------------------===// |
| 114 | /// @class ELFRelocation |
| 115 | /// @brief Generic wrapper for ELFRel and ELFRela. |
| 116 | /// |
| 117 | /// This helper class allows us to parse both ELFRel and ELFRela relocation |
| 118 | /// entries in a generic manner. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | class ELFRelocation { |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 120 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | /// Constructs an ELFRelocation entry with a personality as given by @p |
| 122 | /// type. |
| 123 | /// |
| 124 | /// @param type Either DT_REL or DT_RELA. Any other value is invalid. |
| 125 | ELFRelocation(unsigned type); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 126 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | ~ELFRelocation(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | static unsigned RelocType32(const ELFRelocation &rel); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | static unsigned RelocType64(const ELFRelocation &rel); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | static unsigned RelocSymbol32(const ELFRelocation &rel); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 136 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | static unsigned RelocSymbol64(const ELFRelocation &rel); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 138 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | static unsigned RelocOffset32(const ELFRelocation &rel); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 140 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | static unsigned RelocOffset64(const ELFRelocation &rel); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 142 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | static unsigned RelocAddend32(const ELFRelocation &rel); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | static unsigned RelocAddend64(const ELFRelocation &rel); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 146 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 147 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | typedef llvm::PointerUnion<ELFRel *, ELFRela *> RelocUnion; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | RelocUnion reloc; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | ELFRelocation::ELFRelocation(unsigned type) { |
| 154 | if (type == DT_REL || type == SHT_REL) |
| 155 | reloc = new ELFRel(); |
| 156 | else if (type == DT_RELA || type == SHT_RELA) |
| 157 | reloc = new ELFRela(); |
| 158 | else { |
| 159 | assert(false && "unexpected relocation type"); |
| 160 | reloc = static_cast<ELFRel *>(NULL); |
| 161 | } |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | ELFRelocation::~ELFRelocation() { |
| 165 | if (reloc.is<ELFRel *>()) |
| 166 | delete reloc.get<ELFRel *>(); |
| 167 | else |
| 168 | delete reloc.get<ELFRela *>(); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 171 | bool ELFRelocation::Parse(const lldb_private::DataExtractor &data, |
| 172 | lldb::offset_t *offset) { |
| 173 | if (reloc.is<ELFRel *>()) |
| 174 | return reloc.get<ELFRel *>()->Parse(data, offset); |
| 175 | else |
| 176 | return reloc.get<ELFRela *>()->Parse(data, offset); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 179 | unsigned ELFRelocation::RelocType32(const ELFRelocation &rel) { |
| 180 | if (rel.reloc.is<ELFRel *>()) |
| 181 | return ELFRel::RelocType32(*rel.reloc.get<ELFRel *>()); |
| 182 | else |
| 183 | return ELFRela::RelocType32(*rel.reloc.get<ELFRela *>()); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | unsigned ELFRelocation::RelocType64(const ELFRelocation &rel) { |
| 187 | if (rel.reloc.is<ELFRel *>()) |
| 188 | return ELFRel::RelocType64(*rel.reloc.get<ELFRel *>()); |
| 189 | else |
| 190 | return ELFRela::RelocType64(*rel.reloc.get<ELFRela *>()); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | unsigned ELFRelocation::RelocSymbol32(const ELFRelocation &rel) { |
| 194 | if (rel.reloc.is<ELFRel *>()) |
| 195 | return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel *>()); |
| 196 | else |
| 197 | return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela *>()); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | unsigned ELFRelocation::RelocSymbol64(const ELFRelocation &rel) { |
| 201 | if (rel.reloc.is<ELFRel *>()) |
| 202 | return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel *>()); |
| 203 | else |
| 204 | return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela *>()); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | unsigned ELFRelocation::RelocOffset32(const ELFRelocation &rel) { |
| 208 | if (rel.reloc.is<ELFRel *>()) |
| 209 | return rel.reloc.get<ELFRel *>()->r_offset; |
| 210 | else |
| 211 | return rel.reloc.get<ELFRela *>()->r_offset; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | unsigned ELFRelocation::RelocOffset64(const ELFRelocation &rel) { |
| 215 | if (rel.reloc.is<ELFRel *>()) |
| 216 | return rel.reloc.get<ELFRel *>()->r_offset; |
| 217 | else |
| 218 | return rel.reloc.get<ELFRela *>()->r_offset; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | unsigned ELFRelocation::RelocAddend32(const ELFRelocation &rel) { |
| 222 | if (rel.reloc.is<ELFRel *>()) |
| 223 | return 0; |
| 224 | else |
| 225 | return rel.reloc.get<ELFRela *>()->r_addend; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 228 | unsigned ELFRelocation::RelocAddend64(const ELFRelocation &rel) { |
| 229 | if (rel.reloc.is<ELFRel *>()) |
| 230 | return 0; |
| 231 | else |
| 232 | return rel.reloc.get<ELFRela *>()->r_addend; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 235 | } // end anonymous namespace |
| 236 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 237 | bool ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset) { |
| 238 | // Read all fields. |
| 239 | if (data.GetU32(offset, &n_namesz, 3) == NULL) |
| 240 | return false; |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 241 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 242 | // The name field is required to be nul-terminated, and n_namesz |
| 243 | // includes the terminating nul in observed implementations (contrary |
| 244 | // to the ELF-64 spec). A special case is needed for cores generated |
| 245 | // by some older Linux versions, which write a note named "CORE" |
| 246 | // without a nul terminator and n_namesz = 4. |
| 247 | if (n_namesz == 4) { |
| 248 | char buf[4]; |
| 249 | if (data.ExtractBytes(*offset, 4, data.GetByteOrder(), buf) != 4) |
| 250 | return false; |
| 251 | if (strncmp(buf, "CORE", 4) == 0) { |
| 252 | n_name = "CORE"; |
| 253 | *offset += 4; |
| 254 | return true; |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 255 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | } |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 257 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | const char *cstr = data.GetCStr(offset, llvm::alignTo(n_namesz, 4)); |
| 259 | if (cstr == NULL) { |
| 260 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS)); |
| 261 | if (log) |
| 262 | log->Printf("Failed to parse note name lacking nul terminator"); |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 263 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 264 | return false; |
| 265 | } |
| 266 | n_name = cstr; |
| 267 | return true; |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | static uint32_t kalimbaVariantFromElfFlags(const elf::elf_word e_flags) { |
| 271 | const uint32_t dsp_rev = e_flags & 0xFF; |
| 272 | uint32_t kal_arch_variant = LLDB_INVALID_CPUTYPE; |
| 273 | switch (dsp_rev) { |
| 274 | // TODO(mg11) Support more variants |
| 275 | case 10: |
| 276 | kal_arch_variant = llvm::Triple::KalimbaSubArch_v3; |
| 277 | break; |
| 278 | case 14: |
| 279 | kal_arch_variant = llvm::Triple::KalimbaSubArch_v4; |
| 280 | break; |
| 281 | case 17: |
| 282 | case 20: |
| 283 | kal_arch_variant = llvm::Triple::KalimbaSubArch_v5; |
| 284 | break; |
| 285 | default: |
| 286 | break; |
| 287 | } |
| 288 | return kal_arch_variant; |
Matthew Gardiner | 5f67579 | 2014-08-27 12:09:39 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Nitesh Jain | 706c520 | 2017-03-31 11:06:25 +0000 | [diff] [blame] | 291 | static uint32_t mipsVariantFromElfFlags (const elf::ELFHeader &header) { |
| 292 | const uint32_t mips_arch = header.e_flags & llvm::ELF::EF_MIPS_ARCH; |
| 293 | uint32_t endian = header.e_ident[EI_DATA]; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 294 | uint32_t arch_variant = ArchSpec::eMIPSSubType_unknown; |
Nitesh Jain | 706c520 | 2017-03-31 11:06:25 +0000 | [diff] [blame] | 295 | uint32_t fileclass = header.e_ident[EI_CLASS]; |
| 296 | |
| 297 | // If there aren't any elf flags available (e.g core elf file) then return default |
| 298 | // 32 or 64 bit arch (without any architecture revision) based on object file's class. |
| 299 | if (header.e_type == ET_CORE) { |
| 300 | switch (fileclass) { |
| 301 | case llvm::ELF::ELFCLASS32: |
| 302 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el |
| 303 | : ArchSpec::eMIPSSubType_mips32; |
| 304 | case llvm::ELF::ELFCLASS64: |
| 305 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el |
| 306 | : ArchSpec::eMIPSSubType_mips64; |
| 307 | default: |
| 308 | return arch_variant; |
| 309 | } |
| 310 | } |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 311 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 312 | switch (mips_arch) { |
| 313 | case llvm::ELF::EF_MIPS_ARCH_1: |
| 314 | case llvm::ELF::EF_MIPS_ARCH_2: |
| 315 | case llvm::ELF::EF_MIPS_ARCH_32: |
| 316 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el |
| 317 | : ArchSpec::eMIPSSubType_mips32; |
| 318 | case llvm::ELF::EF_MIPS_ARCH_32R2: |
| 319 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r2el |
| 320 | : ArchSpec::eMIPSSubType_mips32r2; |
| 321 | case llvm::ELF::EF_MIPS_ARCH_32R6: |
| 322 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r6el |
| 323 | : ArchSpec::eMIPSSubType_mips32r6; |
| 324 | case llvm::ELF::EF_MIPS_ARCH_3: |
| 325 | case llvm::ELF::EF_MIPS_ARCH_4: |
| 326 | case llvm::ELF::EF_MIPS_ARCH_5: |
| 327 | case llvm::ELF::EF_MIPS_ARCH_64: |
| 328 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el |
| 329 | : ArchSpec::eMIPSSubType_mips64; |
| 330 | case llvm::ELF::EF_MIPS_ARCH_64R2: |
| 331 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r2el |
| 332 | : ArchSpec::eMIPSSubType_mips64r2; |
| 333 | case llvm::ELF::EF_MIPS_ARCH_64R6: |
| 334 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r6el |
| 335 | : ArchSpec::eMIPSSubType_mips64r6; |
| 336 | default: |
| 337 | break; |
| 338 | } |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 339 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 340 | return arch_variant; |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 343 | static uint32_t subTypeFromElfHeader(const elf::ELFHeader &header) { |
| 344 | if (header.e_machine == llvm::ELF::EM_MIPS) |
Nitesh Jain | 706c520 | 2017-03-31 11:06:25 +0000 | [diff] [blame] | 345 | return mipsVariantFromElfFlags(header); |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 346 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 347 | return llvm::ELF::EM_CSR_KALIMBA == header.e_machine |
| 348 | ? kalimbaVariantFromElfFlags(header.e_flags) |
| 349 | : LLDB_INVALID_CPUTYPE; |
Matthew Gardiner | 5f67579 | 2014-08-27 12:09:39 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Matthew Gardiner | f03e6d84 | 2014-09-29 08:02:24 +0000 | [diff] [blame] | 352 | //! The kalimba toolchain identifies a code section as being |
| 353 | //! one with the SHT_PROGBITS set in the section sh_type and the top |
| 354 | //! bit in the 32-bit address field set. |
| 355 | static lldb::SectionType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 356 | kalimbaSectionType(const elf::ELFHeader &header, |
| 357 | const elf::ELFSectionHeader §_hdr) { |
| 358 | if (llvm::ELF::EM_CSR_KALIMBA != header.e_machine) { |
Matthew Gardiner | 6e7b0a0 | 2014-10-15 08:21:54 +0000 | [diff] [blame] | 359 | return eSectionTypeOther; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | if (llvm::ELF::SHT_NOBITS == sect_hdr.sh_type) { |
| 363 | return eSectionTypeZeroFill; |
| 364 | } |
| 365 | |
| 366 | if (llvm::ELF::SHT_PROGBITS == sect_hdr.sh_type) { |
| 367 | const lldb::addr_t KAL_CODE_BIT = 1 << 31; |
| 368 | return KAL_CODE_BIT & sect_hdr.sh_addr ? eSectionTypeCode |
| 369 | : eSectionTypeData; |
| 370 | } |
| 371 | |
| 372 | return eSectionTypeOther; |
Matthew Gardiner | f03e6d84 | 2014-09-29 08:02:24 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 375 | // Arbitrary constant used as UUID prefix for core files. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 376 | const uint32_t ObjectFileELF::g_core_uuid_magic(0xE210C); |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 377 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 378 | //------------------------------------------------------------------ |
| 379 | // Static methods. |
| 380 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 381 | void ObjectFileELF::Initialize() { |
| 382 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 383 | GetPluginDescriptionStatic(), CreateInstance, |
| 384 | CreateMemoryInstance, GetModuleSpecifications); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 387 | void ObjectFileELF::Terminate() { |
| 388 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 391 | lldb_private::ConstString ObjectFileELF::GetPluginNameStatic() { |
| 392 | static ConstString g_name("elf"); |
| 393 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 396 | const char *ObjectFileELF::GetPluginDescriptionStatic() { |
| 397 | return "ELF object file reader."; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 400 | ObjectFile *ObjectFileELF::CreateInstance(const lldb::ModuleSP &module_sp, |
| 401 | DataBufferSP &data_sp, |
| 402 | lldb::offset_t data_offset, |
| 403 | const lldb_private::FileSpec *file, |
| 404 | lldb::offset_t file_offset, |
| 405 | lldb::offset_t length) { |
| 406 | if (!data_sp) { |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 407 | data_sp = |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame^] | 408 | DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset, true); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 409 | if (!data_sp) |
| 410 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 411 | data_offset = 0; |
| 412 | } |
| 413 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 414 | assert(data_sp); |
| 415 | |
| 416 | if (data_sp->GetByteSize() <= (llvm::ELF::EI_NIDENT + data_offset)) |
| 417 | return nullptr; |
| 418 | |
| 419 | const uint8_t *magic = data_sp->GetBytes() + data_offset; |
| 420 | if (!ELFHeader::MagicBytesMatch(magic)) |
| 421 | return nullptr; |
| 422 | |
| 423 | // Update the data to contain the entire file if it doesn't already |
| 424 | if (data_sp->GetByteSize() < length) { |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 425 | data_sp = |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame^] | 426 | DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset, true); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 427 | if (!data_sp) |
| 428 | return nullptr; |
| 429 | data_offset = 0; |
| 430 | magic = data_sp->GetBytes(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 431 | } |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 432 | |
| 433 | unsigned address_size = ELFHeader::AddressSizeInBytes(magic); |
| 434 | if (address_size == 4 || address_size == 8) { |
| 435 | std::unique_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF( |
| 436 | module_sp, data_sp, data_offset, file, file_offset, length)); |
| 437 | ArchSpec spec; |
| 438 | if (objfile_ap->GetArchitecture(spec) && |
| 439 | objfile_ap->SetModulesArchitecture(spec)) |
| 440 | return objfile_ap.release(); |
| 441 | } |
| 442 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 443 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 446 | ObjectFile *ObjectFileELF::CreateMemoryInstance( |
| 447 | const lldb::ModuleSP &module_sp, DataBufferSP &data_sp, |
| 448 | const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) { |
| 449 | if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT)) { |
| 450 | const uint8_t *magic = data_sp->GetBytes(); |
| 451 | if (ELFHeader::MagicBytesMatch(magic)) { |
| 452 | unsigned address_size = ELFHeader::AddressSizeInBytes(magic); |
| 453 | if (address_size == 4 || address_size == 8) { |
Benjamin Kramer | e1a6074 | 2017-09-14 15:01:55 +0000 | [diff] [blame] | 454 | std::unique_ptr<ObjectFileELF> objfile_ap( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 455 | new ObjectFileELF(module_sp, data_sp, process_sp, header_addr)); |
| 456 | ArchSpec spec; |
| 457 | if (objfile_ap->GetArchitecture(spec) && |
| 458 | objfile_ap->SetModulesArchitecture(spec)) |
| 459 | return objfile_ap.release(); |
| 460 | } |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 461 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 462 | } |
| 463 | return NULL; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 466 | bool ObjectFileELF::MagicBytesMatch(DataBufferSP &data_sp, |
| 467 | lldb::addr_t data_offset, |
| 468 | lldb::addr_t data_length) { |
| 469 | if (data_sp && |
| 470 | data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) { |
| 471 | const uint8_t *magic = data_sp->GetBytes() + data_offset; |
| 472 | return ELFHeader::MagicBytesMatch(magic); |
| 473 | } |
| 474 | return false; |
Michael Sartain | 9f0013d | 2013-05-17 00:20:21 +0000 | [diff] [blame] | 475 | } |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 476 | |
Michael Sartain | 9f4517a | 2013-07-03 01:52:14 +0000 | [diff] [blame] | 477 | /* |
| 478 | * crc function from http://svnweb.freebsd.org/base/head/sys/libkern/crc32.c |
| 479 | * |
| 480 | * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or |
| 481 | * code or tables extracted from it, as desired without restriction. |
| 482 | */ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 483 | static uint32_t calc_crc32(uint32_t crc, const void *buf, size_t size) { |
| 484 | static const uint32_t g_crc32_tab[] = { |
| 485 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, |
| 486 | 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, |
| 487 | 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, |
| 488 | 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, |
| 489 | 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, |
| 490 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, |
| 491 | 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, |
| 492 | 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, |
| 493 | 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, |
| 494 | 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, |
| 495 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, |
| 496 | 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, |
| 497 | 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, |
| 498 | 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, |
| 499 | 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, |
| 500 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, |
| 501 | 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, |
| 502 | 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, |
| 503 | 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, |
| 504 | 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, |
| 505 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, |
| 506 | 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, |
| 507 | 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, |
| 508 | 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, |
| 509 | 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, |
| 510 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, |
| 511 | 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, |
| 512 | 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, |
| 513 | 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, |
| 514 | 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, |
| 515 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, |
| 516 | 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, |
| 517 | 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, |
| 518 | 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, |
| 519 | 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, |
| 520 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, |
| 521 | 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, |
| 522 | 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, |
| 523 | 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, |
| 524 | 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, |
| 525 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, |
| 526 | 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, |
| 527 | 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; |
| 528 | const uint8_t *p = (const uint8_t *)buf; |
Michael Sartain | 9f4517a | 2013-07-03 01:52:14 +0000 | [diff] [blame] | 529 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 530 | crc = crc ^ ~0U; |
| 531 | while (size--) |
| 532 | crc = g_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); |
| 533 | return crc ^ ~0U; |
Michael Sartain | 9f4517a | 2013-07-03 01:52:14 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 536 | static uint32_t calc_gnu_debuglink_crc32(const void *buf, size_t size) { |
| 537 | return calc_crc32(0U, buf, size); |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 540 | uint32_t ObjectFileELF::CalculateELFNotesSegmentsCRC32( |
| 541 | const ProgramHeaderColl &program_headers, DataExtractor &object_data) { |
| 542 | typedef ProgramHeaderCollConstIter Iter; |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 543 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 544 | uint32_t core_notes_crc = 0; |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 545 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 546 | for (Iter I = program_headers.begin(); I != program_headers.end(); ++I) { |
| 547 | if (I->p_type == llvm::ELF::PT_NOTE) { |
| 548 | const elf_off ph_offset = I->p_offset; |
| 549 | const size_t ph_size = I->p_filesz; |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 550 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 551 | DataExtractor segment_data; |
| 552 | if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size) { |
| 553 | // The ELF program header contained incorrect data, |
| 554 | // probably corefile is incomplete or corrupted. |
| 555 | break; |
| 556 | } |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 557 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 558 | core_notes_crc = calc_crc32(core_notes_crc, segment_data.GetDataStart(), |
| 559 | segment_data.GetByteSize()); |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 560 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 561 | } |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 562 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 563 | return core_notes_crc; |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 566 | static const char *OSABIAsCString(unsigned char osabi_byte) { |
| 567 | #define _MAKE_OSABI_CASE(x) \ |
| 568 | case x: \ |
| 569 | return #x |
| 570 | switch (osabi_byte) { |
| 571 | _MAKE_OSABI_CASE(ELFOSABI_NONE); |
| 572 | _MAKE_OSABI_CASE(ELFOSABI_HPUX); |
| 573 | _MAKE_OSABI_CASE(ELFOSABI_NETBSD); |
| 574 | _MAKE_OSABI_CASE(ELFOSABI_GNU); |
| 575 | _MAKE_OSABI_CASE(ELFOSABI_HURD); |
| 576 | _MAKE_OSABI_CASE(ELFOSABI_SOLARIS); |
| 577 | _MAKE_OSABI_CASE(ELFOSABI_AIX); |
| 578 | _MAKE_OSABI_CASE(ELFOSABI_IRIX); |
| 579 | _MAKE_OSABI_CASE(ELFOSABI_FREEBSD); |
| 580 | _MAKE_OSABI_CASE(ELFOSABI_TRU64); |
| 581 | _MAKE_OSABI_CASE(ELFOSABI_MODESTO); |
| 582 | _MAKE_OSABI_CASE(ELFOSABI_OPENBSD); |
| 583 | _MAKE_OSABI_CASE(ELFOSABI_OPENVMS); |
| 584 | _MAKE_OSABI_CASE(ELFOSABI_NSK); |
| 585 | _MAKE_OSABI_CASE(ELFOSABI_AROS); |
| 586 | _MAKE_OSABI_CASE(ELFOSABI_FENIXOS); |
| 587 | _MAKE_OSABI_CASE(ELFOSABI_C6000_ELFABI); |
| 588 | _MAKE_OSABI_CASE(ELFOSABI_C6000_LINUX); |
| 589 | _MAKE_OSABI_CASE(ELFOSABI_ARM); |
| 590 | _MAKE_OSABI_CASE(ELFOSABI_STANDALONE); |
| 591 | default: |
| 592 | return "<unknown-osabi>"; |
| 593 | } |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 594 | #undef _MAKE_OSABI_CASE |
| 595 | } |
| 596 | |
Ed Maste | f6a1312 | 2015-06-05 13:03:08 +0000 | [diff] [blame] | 597 | // |
| 598 | // WARNING : This function is being deprecated |
| 599 | // It's functionality has moved to ArchSpec::SetArchitecture |
| 600 | // This function is only being kept to validate the move. |
| 601 | // |
| 602 | // TODO : Remove this function |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 603 | static bool GetOsFromOSABI(unsigned char osabi_byte, |
| 604 | llvm::Triple::OSType &ostype) { |
| 605 | switch (osabi_byte) { |
| 606 | case ELFOSABI_AIX: |
| 607 | ostype = llvm::Triple::OSType::AIX; |
| 608 | break; |
| 609 | case ELFOSABI_FREEBSD: |
| 610 | ostype = llvm::Triple::OSType::FreeBSD; |
| 611 | break; |
| 612 | case ELFOSABI_GNU: |
| 613 | ostype = llvm::Triple::OSType::Linux; |
| 614 | break; |
| 615 | case ELFOSABI_NETBSD: |
| 616 | ostype = llvm::Triple::OSType::NetBSD; |
| 617 | break; |
| 618 | case ELFOSABI_OPENBSD: |
| 619 | ostype = llvm::Triple::OSType::OpenBSD; |
| 620 | break; |
| 621 | case ELFOSABI_SOLARIS: |
| 622 | ostype = llvm::Triple::OSType::Solaris; |
| 623 | break; |
| 624 | default: |
| 625 | ostype = llvm::Triple::OSType::UnknownOS; |
| 626 | } |
| 627 | return ostype != llvm::Triple::OSType::UnknownOS; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 630 | size_t ObjectFileELF::GetModuleSpecifications( |
| 631 | const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, |
| 632 | lldb::offset_t data_offset, lldb::offset_t file_offset, |
| 633 | lldb::offset_t length, lldb_private::ModuleSpecList &specs) { |
| 634 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES)); |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 635 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 636 | const size_t initial_count = specs.GetSize(); |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 637 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 638 | if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) { |
| 639 | DataExtractor data; |
| 640 | data.SetData(data_sp); |
| 641 | elf::ELFHeader header; |
Pavel Labath | 23ccc29 | 2017-01-31 23:09:46 +0000 | [diff] [blame] | 642 | lldb::offset_t header_offset = data_offset; |
| 643 | if (header.Parse(data, &header_offset)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 644 | if (data_sp) { |
| 645 | ModuleSpec spec(file); |
Matthew Gardiner | 5f67579 | 2014-08-27 12:09:39 +0000 | [diff] [blame] | 646 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 647 | const uint32_t sub_type = subTypeFromElfHeader(header); |
| 648 | spec.GetArchitecture().SetArchitecture( |
| 649 | eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]); |
Matthew Gardiner | 5f67579 | 2014-08-27 12:09:39 +0000 | [diff] [blame] | 650 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 651 | if (spec.GetArchitecture().IsValid()) { |
| 652 | llvm::Triple::OSType ostype; |
| 653 | llvm::Triple::VendorType vendor; |
| 654 | llvm::Triple::OSType spec_ostype = |
| 655 | spec.GetArchitecture().GetTriple().getOS(); |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 656 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 657 | if (log) |
| 658 | log->Printf("ObjectFileELF::%s file '%s' module OSABI: %s", |
| 659 | __FUNCTION__, file.GetPath().c_str(), |
| 660 | OSABIAsCString(header.e_ident[EI_OSABI])); |
Ed Maste | f6a1312 | 2015-06-05 13:03:08 +0000 | [diff] [blame] | 661 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 662 | // SetArchitecture should have set the vendor to unknown |
| 663 | vendor = spec.GetArchitecture().GetTriple().getVendor(); |
| 664 | assert(vendor == llvm::Triple::UnknownVendor); |
Hafiz Abid Qadeer | b155431 | 2017-01-20 10:24:03 +0000 | [diff] [blame] | 665 | UNUSED_IF_ASSERT_DISABLED(vendor); |
Ed Maste | f6a1312 | 2015-06-05 13:03:08 +0000 | [diff] [blame] | 666 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 667 | // |
| 668 | // Validate it is ok to remove GetOsFromOSABI |
| 669 | GetOsFromOSABI(header.e_ident[EI_OSABI], ostype); |
| 670 | assert(spec_ostype == ostype); |
| 671 | if (spec_ostype != llvm::Triple::OSType::UnknownOS) { |
| 672 | if (log) |
| 673 | log->Printf("ObjectFileELF::%s file '%s' set ELF module OS type " |
| 674 | "from ELF header OSABI.", |
| 675 | __FUNCTION__, file.GetPath().c_str()); |
| 676 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 677 | |
Pavel Labath | 23ccc29 | 2017-01-31 23:09:46 +0000 | [diff] [blame] | 678 | // In case there is header extension in the section #0, the header |
| 679 | // we parsed above could have sentinel values for e_phnum, e_shnum, |
| 680 | // and e_shstrndx. In this case we need to reparse the header |
| 681 | // with a bigger data source to get the actual values. |
| 682 | size_t section_header_end = header.e_shoff + header.e_shentsize; |
| 683 | if (header.HasHeaderExtension() && |
| 684 | section_header_end > data_sp->GetByteSize()) { |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 685 | data_sp = DataBufferLLVM::CreateSliceFromPath( |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 686 | file.GetPath(), section_header_end, file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 687 | if (data_sp) { |
| 688 | data.SetData(data_sp); |
| 689 | lldb::offset_t header_offset = data_offset; |
| 690 | header.Parse(data, &header_offset); |
| 691 | } |
Pavel Labath | 23ccc29 | 2017-01-31 23:09:46 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 694 | // Try to get the UUID from the section list. Usually that's at the |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 695 | // end, so map the file in if we don't have it already. |
Pavel Labath | 23ccc29 | 2017-01-31 23:09:46 +0000 | [diff] [blame] | 696 | section_header_end = |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 697 | header.e_shoff + header.e_shnum * header.e_shentsize; |
| 698 | if (section_header_end > data_sp->GetByteSize()) { |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 699 | data_sp = DataBufferLLVM::CreateSliceFromPath( |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 700 | file.GetPath(), section_header_end, file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 701 | if (data_sp) |
| 702 | data.SetData(data_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 703 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 704 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 705 | uint32_t gnu_debuglink_crc = 0; |
| 706 | std::string gnu_debuglink_file; |
| 707 | SectionHeaderColl section_headers; |
| 708 | lldb_private::UUID &uuid = spec.GetUUID(); |
Michael Sartain | 9f4517a | 2013-07-03 01:52:14 +0000 | [diff] [blame] | 709 | |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 710 | GetSectionHeaderInfo(section_headers, data, header, uuid, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 711 | gnu_debuglink_file, gnu_debuglink_crc, |
| 712 | spec.GetArchitecture()); |
Ravitheja Addepally | 15f89c4 | 2016-01-19 12:55:21 +0000 | [diff] [blame] | 713 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 714 | llvm::Triple &spec_triple = spec.GetArchitecture().GetTriple(); |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 715 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 716 | if (log) |
| 717 | log->Printf("ObjectFileELF::%s file '%s' module set to triple: %s " |
| 718 | "(architecture %s)", |
| 719 | __FUNCTION__, file.GetPath().c_str(), |
| 720 | spec_triple.getTriple().c_str(), |
| 721 | spec.GetArchitecture().GetArchitectureName()); |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 722 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 723 | if (!uuid.IsValid()) { |
| 724 | uint32_t core_notes_crc = 0; |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 725 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 726 | if (!gnu_debuglink_crc) { |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 727 | static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 728 | lldb_private::Timer scoped_timer( |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 729 | func_cat, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 730 | "Calculating module crc32 %s with size %" PRIu64 " KiB", |
| 731 | file.GetLastPathComponent().AsCString(), |
| 732 | (file.GetByteSize() - file_offset) / 1024); |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 733 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 734 | // For core files - which usually don't happen to have a |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 735 | // gnu_debuglink, and are pretty bulky - calculating whole |
| 736 | // contents crc32 would be too much of luxury. Thus we will need |
| 737 | // to fallback to something simpler. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 738 | if (header.e_type == llvm::ELF::ET_CORE) { |
| 739 | size_t program_headers_end = |
| 740 | header.e_phoff + header.e_phnum * header.e_phentsize; |
| 741 | if (program_headers_end > data_sp->GetByteSize()) { |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 742 | data_sp = DataBufferLLVM::CreateSliceFromPath( |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 743 | file.GetPath(), program_headers_end, file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 744 | if (data_sp) |
| 745 | data.SetData(data_sp); |
Michael Sartain | 9f0013d | 2013-05-17 00:20:21 +0000 | [diff] [blame] | 746 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 747 | ProgramHeaderColl program_headers; |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 748 | GetProgramHeaderInfo(program_headers, data, header); |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 749 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 750 | size_t segment_data_end = 0; |
| 751 | for (ProgramHeaderCollConstIter I = program_headers.begin(); |
| 752 | I != program_headers.end(); ++I) { |
| 753 | segment_data_end = std::max<unsigned long long>( |
| 754 | I->p_offset + I->p_filesz, segment_data_end); |
| 755 | } |
| 756 | |
| 757 | if (segment_data_end > data_sp->GetByteSize()) { |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 758 | data_sp = DataBufferLLVM::CreateSliceFromPath( |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 759 | file.GetPath(), segment_data_end, file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 760 | if (data_sp) |
| 761 | data.SetData(data_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | core_notes_crc = |
| 765 | CalculateELFNotesSegmentsCRC32(program_headers, data); |
| 766 | } else { |
| 767 | // Need to map entire file into memory to calculate the crc. |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 768 | data_sp = DataBufferLLVM::CreateSliceFromPath(file.GetPath(), -1, |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 769 | file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 770 | if (data_sp) { |
| 771 | data.SetData(data_sp); |
| 772 | gnu_debuglink_crc = calc_gnu_debuglink_crc32( |
| 773 | data.GetDataStart(), data.GetByteSize()); |
| 774 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | if (gnu_debuglink_crc) { |
| 778 | // Use 4 bytes of crc from the .gnu_debuglink section. |
| 779 | uint32_t uuidt[4] = {gnu_debuglink_crc, 0, 0, 0}; |
| 780 | uuid.SetBytes(uuidt, sizeof(uuidt)); |
| 781 | } else if (core_notes_crc) { |
| 782 | // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make |
| 783 | // it look different form |
| 784 | // .gnu_debuglink crc followed by 4 bytes of note segments crc. |
| 785 | uint32_t uuidt[4] = {g_core_uuid_magic, core_notes_crc, 0, 0}; |
| 786 | uuid.SetBytes(uuidt, sizeof(uuidt)); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | specs.Append(spec); |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | return specs.GetSize() - initial_count; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 799 | //------------------------------------------------------------------ |
| 800 | // PluginInterface protocol |
| 801 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 802 | lldb_private::ConstString ObjectFileELF::GetPluginName() { |
| 803 | return GetPluginNameStatic(); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 804 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 805 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 806 | uint32_t ObjectFileELF::GetPluginVersion() { return m_plugin_version; } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 807 | //------------------------------------------------------------------ |
| 808 | // ObjectFile protocol |
| 809 | //------------------------------------------------------------------ |
| 810 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 811 | ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp, |
| 812 | DataBufferSP &data_sp, lldb::offset_t data_offset, |
| 813 | const FileSpec *file, lldb::offset_t file_offset, |
| 814 | lldb::offset_t length) |
| 815 | : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), |
| 816 | m_header(), m_uuid(), m_gnu_debuglink_file(), m_gnu_debuglink_crc(0), |
| 817 | m_program_headers(), m_section_headers(), m_dynamic_symbols(), |
| 818 | m_filespec_ap(), m_entry_point_address(), m_arch_spec() { |
| 819 | if (file) |
| 820 | m_file = *file; |
| 821 | ::memset(&m_header, 0, sizeof(m_header)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 824 | ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp, |
| 825 | DataBufferSP &header_data_sp, |
| 826 | const lldb::ProcessSP &process_sp, |
| 827 | addr_t header_addr) |
| 828 | : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), |
| 829 | m_header(), m_uuid(), m_gnu_debuglink_file(), m_gnu_debuglink_crc(0), |
| 830 | m_program_headers(), m_section_headers(), m_dynamic_symbols(), |
| 831 | m_filespec_ap(), m_entry_point_address(), m_arch_spec() { |
| 832 | ::memset(&m_header, 0, sizeof(m_header)); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 835 | ObjectFileELF::~ObjectFileELF() {} |
| 836 | |
| 837 | bool ObjectFileELF::IsExecutable() const { |
| 838 | return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 841 | bool ObjectFileELF::SetLoadAddress(Target &target, lldb::addr_t value, |
| 842 | bool value_is_offset) { |
| 843 | ModuleSP module_sp = GetModule(); |
| 844 | if (module_sp) { |
| 845 | size_t num_loaded_sections = 0; |
| 846 | SectionList *section_list = GetSectionList(); |
| 847 | if (section_list) { |
| 848 | if (!value_is_offset) { |
| 849 | bool found_offset = false; |
Kamil Rytarowski | 12801f1 | 2017-03-26 15:34:57 +0000 | [diff] [blame] | 850 | for (size_t i = 1, count = GetProgramHeaderCount(); i <= count; ++i) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 851 | const elf::ELFProgramHeader *header = GetProgramHeaderByIndex(i); |
| 852 | if (header == nullptr) |
| 853 | continue; |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 854 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 855 | if (header->p_type != PT_LOAD || header->p_offset != 0) |
| 856 | continue; |
Tamas Berghammer | f256184 | 2015-06-30 10:41:23 +0000 | [diff] [blame] | 857 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 858 | value = value - header->p_vaddr; |
| 859 | found_offset = true; |
| 860 | break; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 861 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 862 | if (!found_offset) |
| 863 | return false; |
| 864 | } |
| 865 | |
| 866 | const size_t num_sections = section_list->GetSize(); |
| 867 | size_t sect_idx = 0; |
| 868 | |
| 869 | for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { |
| 870 | // Iterate through the object file sections to find all |
| 871 | // of the sections that have SHF_ALLOC in their flag bits. |
| 872 | SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); |
| 873 | if (section_sp && section_sp->Test(SHF_ALLOC)) { |
| 874 | lldb::addr_t load_addr = section_sp->GetFileAddress(); |
| 875 | // We don't want to update the load address of a section with type |
| 876 | // eSectionTypeAbsoluteAddress as they already have the absolute load |
| 877 | // address |
| 878 | // already specified |
| 879 | if (section_sp->GetType() != eSectionTypeAbsoluteAddress) |
| 880 | load_addr += value; |
| 881 | |
| 882 | // On 32-bit systems the load address have to fit into 4 bytes. The |
| 883 | // rest of |
| 884 | // the bytes are the overflow from the addition. |
| 885 | if (GetAddressByteSize() == 4) |
| 886 | load_addr &= 0xFFFFFFFF; |
| 887 | |
| 888 | if (target.GetSectionLoadList().SetSectionLoadAddress(section_sp, |
| 889 | load_addr)) |
| 890 | ++num_loaded_sections; |
| 891 | } |
| 892 | } |
| 893 | return num_loaded_sections > 0; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 894 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 895 | } |
| 896 | return false; |
| 897 | } |
| 898 | |
| 899 | ByteOrder ObjectFileELF::GetByteOrder() const { |
| 900 | if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) |
| 901 | return eByteOrderBig; |
| 902 | if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) |
| 903 | return eByteOrderLittle; |
| 904 | return eByteOrderInvalid; |
| 905 | } |
| 906 | |
| 907 | uint32_t ObjectFileELF::GetAddressByteSize() const { |
| 908 | return m_data.GetAddressByteSize(); |
| 909 | } |
| 910 | |
| 911 | AddressClass ObjectFileELF::GetAddressClass(addr_t file_addr) { |
| 912 | Symtab *symtab = GetSymtab(); |
| 913 | if (!symtab) |
| 914 | return eAddressClassUnknown; |
| 915 | |
| 916 | // The address class is determined based on the symtab. Ask it from the object |
| 917 | // file what |
| 918 | // contains the symtab information. |
| 919 | ObjectFile *symtab_objfile = symtab->GetObjectFile(); |
| 920 | if (symtab_objfile != nullptr && symtab_objfile != this) |
| 921 | return symtab_objfile->GetAddressClass(file_addr); |
| 922 | |
| 923 | auto res = ObjectFile::GetAddressClass(file_addr); |
| 924 | if (res != eAddressClassCode) |
| 925 | return res; |
| 926 | |
| 927 | auto ub = m_address_class_map.upper_bound(file_addr); |
| 928 | if (ub == m_address_class_map.begin()) { |
| 929 | // No entry in the address class map before the address. Return |
| 930 | // default address class for an address in a code section. |
| 931 | return eAddressClassCode; |
| 932 | } |
| 933 | |
| 934 | // Move iterator to the address class entry preceding address |
| 935 | --ub; |
| 936 | |
| 937 | return ub->second; |
| 938 | } |
| 939 | |
| 940 | size_t ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) { |
| 941 | return std::distance(m_section_headers.begin(), I) + 1u; |
| 942 | } |
| 943 | |
| 944 | size_t ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const { |
| 945 | return std::distance(m_section_headers.begin(), I) + 1u; |
| 946 | } |
| 947 | |
| 948 | bool ObjectFileELF::ParseHeader() { |
| 949 | lldb::offset_t offset = 0; |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 950 | return m_header.Parse(m_data, &offset); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 953 | bool ObjectFileELF::GetUUID(lldb_private::UUID *uuid) { |
| 954 | // Need to parse the section list to get the UUIDs, so make sure that's been |
| 955 | // done. |
| 956 | if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 957 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 958 | |
| 959 | if (m_uuid.IsValid()) { |
| 960 | // We have the full build id uuid. |
| 961 | *uuid = m_uuid; |
| 962 | return true; |
| 963 | } else if (GetType() == ObjectFile::eTypeCoreFile) { |
| 964 | uint32_t core_notes_crc = 0; |
| 965 | |
| 966 | if (!ParseProgramHeaders()) |
| 967 | return false; |
| 968 | |
| 969 | core_notes_crc = CalculateELFNotesSegmentsCRC32(m_program_headers, m_data); |
| 970 | |
| 971 | if (core_notes_crc) { |
| 972 | // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it |
| 973 | // look different form .gnu_debuglink crc - followed by 4 bytes of note |
| 974 | // segments crc. |
| 975 | uint32_t uuidt[4] = {g_core_uuid_magic, core_notes_crc, 0, 0}; |
| 976 | m_uuid.SetBytes(uuidt, sizeof(uuidt)); |
| 977 | } |
| 978 | } else { |
| 979 | if (!m_gnu_debuglink_crc) |
| 980 | m_gnu_debuglink_crc = |
| 981 | calc_gnu_debuglink_crc32(m_data.GetDataStart(), m_data.GetByteSize()); |
| 982 | if (m_gnu_debuglink_crc) { |
| 983 | // Use 4 bytes of crc from the .gnu_debuglink section. |
| 984 | uint32_t uuidt[4] = {m_gnu_debuglink_crc, 0, 0, 0}; |
| 985 | m_uuid.SetBytes(uuidt, sizeof(uuidt)); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | if (m_uuid.IsValid()) { |
| 990 | *uuid = m_uuid; |
| 991 | return true; |
| 992 | } |
| 993 | |
| 994 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 997 | lldb_private::FileSpecList ObjectFileELF::GetDebugSymbolFilePaths() { |
| 998 | FileSpecList file_spec_list; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 999 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1000 | if (!m_gnu_debuglink_file.empty()) { |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 1001 | FileSpec file_spec(m_gnu_debuglink_file, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1002 | file_spec_list.Append(file_spec); |
| 1003 | } |
| 1004 | return file_spec_list; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1007 | uint32_t ObjectFileELF::GetDependentModules(FileSpecList &files) { |
| 1008 | size_t num_modules = ParseDependentModules(); |
| 1009 | uint32_t num_specs = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1010 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1011 | for (unsigned i = 0; i < num_modules; ++i) { |
| 1012 | if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i))) |
| 1013 | num_specs++; |
| 1014 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1015 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1016 | return num_specs; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1019 | Address ObjectFileELF::GetImageInfoAddress(Target *target) { |
| 1020 | if (!ParseDynamicSymbols()) |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 1021 | return Address(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1022 | |
| 1023 | SectionList *section_list = GetSectionList(); |
| 1024 | if (!section_list) |
| 1025 | return Address(); |
| 1026 | |
| 1027 | // Find the SHT_DYNAMIC (.dynamic) section. |
| 1028 | SectionSP dynsym_section_sp( |
| 1029 | section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)); |
| 1030 | if (!dynsym_section_sp) |
| 1031 | return Address(); |
| 1032 | assert(dynsym_section_sp->GetObjectFile() == this); |
| 1033 | |
| 1034 | user_id_t dynsym_id = dynsym_section_sp->GetID(); |
| 1035 | const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id); |
| 1036 | if (!dynsym_hdr) |
| 1037 | return Address(); |
| 1038 | |
| 1039 | for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) { |
| 1040 | ELFDynamic &symbol = m_dynamic_symbols[i]; |
| 1041 | |
| 1042 | if (symbol.d_tag == DT_DEBUG) { |
| 1043 | // Compute the offset as the number of previous entries plus the |
| 1044 | // size of d_tag. |
| 1045 | addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); |
| 1046 | return Address(dynsym_section_sp, offset); |
| 1047 | } |
| 1048 | // MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP |
| 1049 | // exists in non-PIE. |
| 1050 | else if ((symbol.d_tag == DT_MIPS_RLD_MAP || |
| 1051 | symbol.d_tag == DT_MIPS_RLD_MAP_REL) && |
| 1052 | target) { |
| 1053 | addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); |
| 1054 | addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target); |
| 1055 | if (dyn_base == LLDB_INVALID_ADDRESS) |
| 1056 | return Address(); |
| 1057 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1058 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1059 | if (symbol.d_tag == DT_MIPS_RLD_MAP) { |
| 1060 | // DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer. |
| 1061 | Address addr; |
| 1062 | if (target->ReadPointerFromMemory(dyn_base + offset, false, error, |
| 1063 | addr)) |
| 1064 | return addr; |
| 1065 | } |
| 1066 | if (symbol.d_tag == DT_MIPS_RLD_MAP_REL) { |
| 1067 | // DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer, |
| 1068 | // relative to the address of the tag. |
| 1069 | uint64_t rel_offset; |
| 1070 | rel_offset = target->ReadUnsignedIntegerFromMemory( |
| 1071 | dyn_base + offset, false, GetAddressByteSize(), UINT64_MAX, error); |
| 1072 | if (error.Success() && rel_offset != UINT64_MAX) { |
| 1073 | Address addr; |
| 1074 | addr_t debug_ptr_address = |
| 1075 | dyn_base + (offset - GetAddressByteSize()) + rel_offset; |
| 1076 | addr.SetOffset(debug_ptr_address); |
| 1077 | return addr; |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | return Address(); |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1086 | lldb_private::Address ObjectFileELF::GetEntryPointAddress() { |
| 1087 | if (m_entry_point_address.IsValid()) |
Stephen Wilson | d126c8c | 2011-03-08 04:12:15 +0000 | [diff] [blame] | 1088 | return m_entry_point_address; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1089 | |
| 1090 | if (!ParseHeader() || !IsExecutable()) |
| 1091 | return m_entry_point_address; |
| 1092 | |
| 1093 | SectionList *section_list = GetSectionList(); |
| 1094 | addr_t offset = m_header.e_entry; |
| 1095 | |
| 1096 | if (!section_list) |
| 1097 | m_entry_point_address.SetOffset(offset); |
| 1098 | else |
| 1099 | m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list); |
| 1100 | return m_entry_point_address; |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1103 | //---------------------------------------------------------------------- |
| 1104 | // ParseDependentModules |
| 1105 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1106 | size_t ObjectFileELF::ParseDependentModules() { |
| 1107 | if (m_filespec_ap.get()) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1108 | return m_filespec_ap->GetSize(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1109 | |
| 1110 | m_filespec_ap.reset(new FileSpecList()); |
| 1111 | |
| 1112 | if (!ParseSectionHeaders()) |
| 1113 | return 0; |
| 1114 | |
| 1115 | SectionList *section_list = GetSectionList(); |
| 1116 | if (!section_list) |
| 1117 | return 0; |
| 1118 | |
| 1119 | // Find the SHT_DYNAMIC section. |
| 1120 | Section *dynsym = |
| 1121 | section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true) |
| 1122 | .get(); |
| 1123 | if (!dynsym) |
| 1124 | return 0; |
| 1125 | assert(dynsym->GetObjectFile() == this); |
| 1126 | |
| 1127 | const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex(dynsym->GetID()); |
| 1128 | if (!header) |
| 1129 | return 0; |
| 1130 | // sh_link: section header index of string table used by entries in the |
| 1131 | // section. |
| 1132 | Section *dynstr = section_list->FindSectionByID(header->sh_link + 1).get(); |
| 1133 | if (!dynstr) |
| 1134 | return 0; |
| 1135 | |
| 1136 | DataExtractor dynsym_data; |
| 1137 | DataExtractor dynstr_data; |
| 1138 | if (ReadSectionData(dynsym, dynsym_data) && |
| 1139 | ReadSectionData(dynstr, dynstr_data)) { |
| 1140 | ELFDynamic symbol; |
| 1141 | const lldb::offset_t section_size = dynsym_data.GetByteSize(); |
| 1142 | lldb::offset_t offset = 0; |
| 1143 | |
| 1144 | // The only type of entries we are concerned with are tagged DT_NEEDED, |
| 1145 | // yielding the name of a required library. |
| 1146 | while (offset < section_size) { |
| 1147 | if (!symbol.Parse(dynsym_data, &offset)) |
| 1148 | break; |
| 1149 | |
| 1150 | if (symbol.d_tag != DT_NEEDED) |
| 1151 | continue; |
| 1152 | |
| 1153 | uint32_t str_index = static_cast<uint32_t>(symbol.d_val); |
| 1154 | const char *lib_name = dynstr_data.PeekCStr(str_index); |
| 1155 | m_filespec_ap->Append(FileSpec(lib_name, true)); |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | return m_filespec_ap->GetSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | //---------------------------------------------------------------------- |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 1163 | // GetProgramHeaderInfo |
| 1164 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1165 | size_t ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers, |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1166 | DataExtractor &object_data, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1167 | const ELFHeader &header) { |
| 1168 | // We have already parsed the program headers |
| 1169 | if (!program_headers.empty()) |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 1170 | return program_headers.size(); |
| 1171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1172 | // If there are no program headers to read we are done. |
| 1173 | if (header.e_phnum == 0) |
| 1174 | return 0; |
| 1175 | |
| 1176 | program_headers.resize(header.e_phnum); |
| 1177 | if (program_headers.size() != header.e_phnum) |
| 1178 | return 0; |
| 1179 | |
| 1180 | const size_t ph_size = header.e_phnum * header.e_phentsize; |
| 1181 | const elf_off ph_offset = header.e_phoff; |
| 1182 | DataExtractor data; |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1183 | if (data.SetData(object_data, ph_offset, ph_size) != ph_size) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1184 | return 0; |
| 1185 | |
| 1186 | uint32_t idx; |
| 1187 | lldb::offset_t offset; |
| 1188 | for (idx = 0, offset = 0; idx < header.e_phnum; ++idx) { |
| 1189 | if (program_headers[idx].Parse(data, &offset) == false) |
| 1190 | break; |
| 1191 | } |
| 1192 | |
| 1193 | if (idx < program_headers.size()) |
| 1194 | program_headers.resize(idx); |
| 1195 | |
| 1196 | return program_headers.size(); |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | //---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1200 | // ParseProgramHeaders |
| 1201 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1202 | size_t ObjectFileELF::ParseProgramHeaders() { |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1203 | return GetProgramHeaderInfo(m_program_headers, m_data, m_header); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1206 | lldb_private::Status |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1207 | ObjectFileELF::RefineModuleDetailsFromNote(lldb_private::DataExtractor &data, |
| 1208 | lldb_private::ArchSpec &arch_spec, |
| 1209 | lldb_private::UUID &uuid) { |
| 1210 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES)); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1211 | Status error; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 1212 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1213 | lldb::offset_t offset = 0; |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 1214 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1215 | while (true) { |
| 1216 | // Parse the note header. If this fails, bail out. |
| 1217 | const lldb::offset_t note_offset = offset; |
| 1218 | ELFNote note = ELFNote(); |
| 1219 | if (!note.Parse(data, &offset)) { |
| 1220 | // We're done. |
| 1221 | return error; |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 1222 | } |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 1223 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1224 | if (log) |
| 1225 | log->Printf("ObjectFileELF::%s parsing note name='%s', type=%" PRIu32, |
| 1226 | __FUNCTION__, note.n_name.c_str(), note.n_type); |
| 1227 | |
| 1228 | // Process FreeBSD ELF notes. |
| 1229 | if ((note.n_name == LLDB_NT_OWNER_FREEBSD) && |
| 1230 | (note.n_type == LLDB_NT_FREEBSD_ABI_TAG) && |
| 1231 | (note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE)) { |
| 1232 | // Pull out the min version info. |
| 1233 | uint32_t version_info; |
| 1234 | if (data.GetU32(&offset, &version_info, 1) == nullptr) { |
| 1235 | error.SetErrorString("failed to read FreeBSD ABI note payload"); |
| 1236 | return error; |
| 1237 | } |
| 1238 | |
| 1239 | // Convert the version info into a major/minor number. |
| 1240 | const uint32_t version_major = version_info / 100000; |
| 1241 | const uint32_t version_minor = (version_info / 1000) % 100; |
| 1242 | |
| 1243 | char os_name[32]; |
| 1244 | snprintf(os_name, sizeof(os_name), "freebsd%" PRIu32 ".%" PRIu32, |
| 1245 | version_major, version_minor); |
| 1246 | |
| 1247 | // Set the elf OS version to FreeBSD. Also clear the vendor. |
| 1248 | arch_spec.GetTriple().setOSName(os_name); |
| 1249 | arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); |
| 1250 | |
| 1251 | if (log) |
| 1252 | log->Printf("ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu32 |
| 1253 | ".%" PRIu32, |
| 1254 | __FUNCTION__, version_major, version_minor, |
| 1255 | static_cast<uint32_t>(version_info % 1000)); |
| 1256 | } |
| 1257 | // Process GNU ELF notes. |
| 1258 | else if (note.n_name == LLDB_NT_OWNER_GNU) { |
| 1259 | switch (note.n_type) { |
| 1260 | case LLDB_NT_GNU_ABI_TAG: |
| 1261 | if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE) { |
| 1262 | // Pull out the min OS version supporting the ABI. |
| 1263 | uint32_t version_info[4]; |
| 1264 | if (data.GetU32(&offset, &version_info[0], note.n_descsz / 4) == |
| 1265 | nullptr) { |
| 1266 | error.SetErrorString("failed to read GNU ABI note payload"); |
| 1267 | return error; |
| 1268 | } |
| 1269 | |
| 1270 | // Set the OS per the OS field. |
| 1271 | switch (version_info[0]) { |
| 1272 | case LLDB_NT_GNU_ABI_OS_LINUX: |
| 1273 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); |
| 1274 | arch_spec.GetTriple().setVendor( |
| 1275 | llvm::Triple::VendorType::UnknownVendor); |
| 1276 | if (log) |
| 1277 | log->Printf( |
| 1278 | "ObjectFileELF::%s detected Linux, min version %" PRIu32 |
| 1279 | ".%" PRIu32 ".%" PRIu32, |
| 1280 | __FUNCTION__, version_info[1], version_info[2], |
| 1281 | version_info[3]); |
| 1282 | // FIXME we have the minimal version number, we could be propagating |
| 1283 | // that. version_info[1] = OS Major, version_info[2] = OS Minor, |
| 1284 | // version_info[3] = Revision. |
| 1285 | break; |
| 1286 | case LLDB_NT_GNU_ABI_OS_HURD: |
| 1287 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS); |
| 1288 | arch_spec.GetTriple().setVendor( |
| 1289 | llvm::Triple::VendorType::UnknownVendor); |
| 1290 | if (log) |
| 1291 | log->Printf("ObjectFileELF::%s detected Hurd (unsupported), min " |
| 1292 | "version %" PRIu32 ".%" PRIu32 ".%" PRIu32, |
| 1293 | __FUNCTION__, version_info[1], version_info[2], |
| 1294 | version_info[3]); |
| 1295 | break; |
| 1296 | case LLDB_NT_GNU_ABI_OS_SOLARIS: |
| 1297 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Solaris); |
| 1298 | arch_spec.GetTriple().setVendor( |
| 1299 | llvm::Triple::VendorType::UnknownVendor); |
| 1300 | if (log) |
| 1301 | log->Printf( |
| 1302 | "ObjectFileELF::%s detected Solaris, min version %" PRIu32 |
| 1303 | ".%" PRIu32 ".%" PRIu32, |
| 1304 | __FUNCTION__, version_info[1], version_info[2], |
| 1305 | version_info[3]); |
| 1306 | break; |
| 1307 | default: |
| 1308 | if (log) |
| 1309 | log->Printf( |
| 1310 | "ObjectFileELF::%s unrecognized OS in note, id %" PRIu32 |
| 1311 | ", min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, |
| 1312 | __FUNCTION__, version_info[0], version_info[1], |
| 1313 | version_info[2], version_info[3]); |
| 1314 | break; |
| 1315 | } |
| 1316 | } |
| 1317 | break; |
| 1318 | |
| 1319 | case LLDB_NT_GNU_BUILD_ID_TAG: |
| 1320 | // Only bother processing this if we don't already have the uuid set. |
| 1321 | if (!uuid.IsValid()) { |
| 1322 | // 16 bytes is UUID|MD5, 20 bytes is SHA1. Other linkers may produce a |
| 1323 | // build-id of a different |
| 1324 | // length. Accept it as long as it's at least 4 bytes as it will be |
| 1325 | // better than our own crc32. |
| 1326 | if (note.n_descsz >= 4 && note.n_descsz <= 20) { |
| 1327 | uint8_t uuidbuf[20]; |
| 1328 | if (data.GetU8(&offset, &uuidbuf, note.n_descsz) == nullptr) { |
| 1329 | error.SetErrorString("failed to read GNU_BUILD_ID note payload"); |
| 1330 | return error; |
| 1331 | } |
| 1332 | |
| 1333 | // Save the build id as the UUID for the module. |
| 1334 | uuid.SetBytes(uuidbuf, note.n_descsz); |
| 1335 | } |
| 1336 | } |
| 1337 | break; |
| 1338 | } |
Nitesh Jain | 706c520 | 2017-03-31 11:06:25 +0000 | [diff] [blame] | 1339 | if (arch_spec.IsMIPS() && |
| 1340 | arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) |
| 1341 | // The note.n_name == LLDB_NT_OWNER_GNU is valid for Linux platform |
| 1342 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1343 | } |
| 1344 | // Process NetBSD ELF notes. |
| 1345 | else if ((note.n_name == LLDB_NT_OWNER_NETBSD) && |
| 1346 | (note.n_type == LLDB_NT_NETBSD_ABI_TAG) && |
| 1347 | (note.n_descsz == LLDB_NT_NETBSD_ABI_SIZE)) { |
| 1348 | // Pull out the min version info. |
| 1349 | uint32_t version_info; |
| 1350 | if (data.GetU32(&offset, &version_info, 1) == nullptr) { |
| 1351 | error.SetErrorString("failed to read NetBSD ABI note payload"); |
| 1352 | return error; |
| 1353 | } |
| 1354 | |
| 1355 | // Set the elf OS version to NetBSD. Also clear the vendor. |
| 1356 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::NetBSD); |
| 1357 | arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); |
| 1358 | |
| 1359 | if (log) |
| 1360 | log->Printf( |
| 1361 | "ObjectFileELF::%s detected NetBSD, min version constant %" PRIu32, |
| 1362 | __FUNCTION__, version_info); |
| 1363 | } |
Kamil Rytarowski | 12801f1 | 2017-03-26 15:34:57 +0000 | [diff] [blame] | 1364 | // Process OpenBSD ELF notes. |
| 1365 | else if (note.n_name == LLDB_NT_OWNER_OPENBSD) { |
| 1366 | // Set the elf OS version to OpenBSD. Also clear the vendor. |
| 1367 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::OpenBSD); |
| 1368 | arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); |
| 1369 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1370 | // Process CSR kalimba notes |
| 1371 | else if ((note.n_type == LLDB_NT_GNU_ABI_TAG) && |
| 1372 | (note.n_name == LLDB_NT_OWNER_CSR)) { |
| 1373 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS); |
| 1374 | arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::CSR); |
| 1375 | |
| 1376 | // TODO At some point the description string could be processed. |
| 1377 | // It could provide a steer towards the kalimba variant which |
| 1378 | // this ELF targets. |
| 1379 | if (note.n_descsz) { |
| 1380 | const char *cstr = |
| 1381 | data.GetCStr(&offset, llvm::alignTo(note.n_descsz, 4)); |
| 1382 | (void)cstr; |
| 1383 | } |
| 1384 | } else if (note.n_name == LLDB_NT_OWNER_ANDROID) { |
| 1385 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); |
| 1386 | arch_spec.GetTriple().setEnvironment( |
| 1387 | llvm::Triple::EnvironmentType::Android); |
| 1388 | } else if (note.n_name == LLDB_NT_OWNER_LINUX) { |
| 1389 | // This is sometimes found in core files and usually contains extended |
| 1390 | // register info |
| 1391 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); |
| 1392 | } else if (note.n_name == LLDB_NT_OWNER_CORE) { |
| 1393 | // Parse the NT_FILE to look for stuff in paths to shared libraries |
| 1394 | // As the contents look like this in a 64 bit ELF core file: |
| 1395 | // count = 0x000000000000000a (10) |
| 1396 | // page_size = 0x0000000000001000 (4096) |
| 1397 | // Index start end file_ofs path |
| 1398 | // ===== ------------------ ------------------ ------------------ |
| 1399 | // ------------------------------------- |
| 1400 | // [ 0] 0x0000000000400000 0x0000000000401000 0x0000000000000000 |
| 1401 | // /tmp/a.out |
| 1402 | // [ 1] 0x0000000000600000 0x0000000000601000 0x0000000000000000 |
| 1403 | // /tmp/a.out |
| 1404 | // [ 2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 |
| 1405 | // /tmp/a.out |
| 1406 | // [ 3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000 |
| 1407 | // /lib/x86_64-linux-gnu/libc-2.19.so |
| 1408 | // [ 4] 0x00007fa79cba8000 0x00007fa79cda7000 0x00000000000001bb |
| 1409 | // /lib/x86_64-linux-gnu/libc-2.19.so |
| 1410 | // [ 5] 0x00007fa79cda7000 0x00007fa79cdab000 0x00000000000001ba |
| 1411 | // /lib/x86_64-linux-gnu/libc-2.19.so |
| 1412 | // [ 6] 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be |
| 1413 | // /lib/x86_64-linux-gnu/libc-2.19.so |
| 1414 | // [ 7] 0x00007fa79cdb2000 0x00007fa79cdd5000 0x0000000000000000 |
| 1415 | // /lib/x86_64-linux-gnu/ld-2.19.so |
| 1416 | // [ 8] 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 |
| 1417 | // /lib/x86_64-linux-gnu/ld-2.19.so |
| 1418 | // [ 9] 0x00007fa79cfd5000 0x00007fa79cfd6000 0x0000000000000023 |
| 1419 | // /lib/x86_64-linux-gnu/ld-2.19.so |
| 1420 | // In the 32 bit ELFs the count, page_size, start, end, file_ofs are |
| 1421 | // uint32_t |
| 1422 | // For reference: see readelf source code (in binutils). |
| 1423 | if (note.n_type == NT_FILE) { |
| 1424 | uint64_t count = data.GetAddress(&offset); |
| 1425 | const char *cstr; |
| 1426 | data.GetAddress(&offset); // Skip page size |
| 1427 | offset += count * 3 * |
| 1428 | data.GetAddressByteSize(); // Skip all start/end/file_ofs |
| 1429 | for (size_t i = 0; i < count; ++i) { |
| 1430 | cstr = data.GetCStr(&offset); |
| 1431 | if (cstr == nullptr) { |
| 1432 | error.SetErrorStringWithFormat("ObjectFileELF::%s trying to read " |
| 1433 | "at an offset after the end " |
| 1434 | "(GetCStr returned nullptr)", |
| 1435 | __FUNCTION__); |
| 1436 | return error; |
| 1437 | } |
| 1438 | llvm::StringRef path(cstr); |
Richard Chamberlain | a0c82e1 | 2016-10-13 12:11:00 +0000 | [diff] [blame] | 1439 | if (path.contains("/lib/x86_64-linux-gnu") || path.contains("/lib/i386-linux-gnu")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1440 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); |
| 1441 | break; |
| 1442 | } |
| 1443 | } |
Nitesh Jain | 706c520 | 2017-03-31 11:06:25 +0000 | [diff] [blame] | 1444 | if (arch_spec.IsMIPS() && |
| 1445 | arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) |
| 1446 | // In case of MIPSR6, the LLDB_NT_OWNER_GNU note is missing |
| 1447 | // for some cases (e.g. compile with -nostdlib) |
| 1448 | // Hence set OS to Linux |
| 1449 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | // Calculate the offset of the next note just in case "offset" has been used |
| 1454 | // to poke at the contents of the note data |
| 1455 | offset = note_offset + note.GetByteSize(); |
| 1456 | } |
| 1457 | |
| 1458 | return error; |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 1459 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1460 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1461 | void ObjectFileELF::ParseARMAttributes(DataExtractor &data, uint64_t length, |
| 1462 | ArchSpec &arch_spec) { |
| 1463 | lldb::offset_t Offset = 0; |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1464 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1465 | uint8_t FormatVersion = data.GetU8(&Offset); |
| 1466 | if (FormatVersion != llvm::ARMBuildAttrs::Format_Version) |
| 1467 | return; |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1468 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1469 | Offset = Offset + sizeof(uint32_t); // Section Length |
| 1470 | llvm::StringRef VendorName = data.GetCStr(&Offset); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1471 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1472 | if (VendorName != "aeabi") |
| 1473 | return; |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1474 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1475 | if (arch_spec.GetTriple().getEnvironment() == |
| 1476 | llvm::Triple::UnknownEnvironment) |
| 1477 | arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1478 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1479 | while (Offset < length) { |
| 1480 | uint8_t Tag = data.GetU8(&Offset); |
| 1481 | uint32_t Size = data.GetU32(&Offset); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1482 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1483 | if (Tag != llvm::ARMBuildAttrs::File || Size == 0) |
| 1484 | continue; |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1485 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1486 | while (Offset < length) { |
| 1487 | uint64_t Tag = data.GetULEB128(&Offset); |
| 1488 | switch (Tag) { |
| 1489 | default: |
| 1490 | if (Tag < 32) |
| 1491 | data.GetULEB128(&Offset); |
| 1492 | else if (Tag % 2 == 0) |
| 1493 | data.GetULEB128(&Offset); |
| 1494 | else |
| 1495 | data.GetCStr(&Offset); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1496 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1497 | break; |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1498 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1499 | case llvm::ARMBuildAttrs::CPU_raw_name: |
| 1500 | case llvm::ARMBuildAttrs::CPU_name: |
| 1501 | data.GetCStr(&Offset); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1502 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1503 | break; |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1504 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1505 | case llvm::ARMBuildAttrs::ABI_VFP_args: { |
| 1506 | uint64_t VFPArgs = data.GetULEB128(&Offset); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1507 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1508 | if (VFPArgs == llvm::ARMBuildAttrs::BaseAAPCS) { |
| 1509 | if (arch_spec.GetTriple().getEnvironment() == |
| 1510 | llvm::Triple::UnknownEnvironment || |
| 1511 | arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABIHF) |
| 1512 | arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1513 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1514 | arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float); |
| 1515 | } else if (VFPArgs == llvm::ARMBuildAttrs::HardFPAAPCS) { |
| 1516 | if (arch_spec.GetTriple().getEnvironment() == |
| 1517 | llvm::Triple::UnknownEnvironment || |
| 1518 | arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABI) |
| 1519 | arch_spec.GetTriple().setEnvironment(llvm::Triple::EABIHF); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1520 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1521 | arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1522 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1523 | |
| 1524 | break; |
| 1525 | } |
| 1526 | } |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1527 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1528 | } |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1529 | } |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 1530 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1531 | //---------------------------------------------------------------------- |
| 1532 | // GetSectionHeaderInfo |
| 1533 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1534 | size_t ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers, |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1535 | DataExtractor &object_data, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1536 | const elf::ELFHeader &header, |
| 1537 | lldb_private::UUID &uuid, |
| 1538 | std::string &gnu_debuglink_file, |
| 1539 | uint32_t &gnu_debuglink_crc, |
| 1540 | ArchSpec &arch_spec) { |
| 1541 | // Don't reparse the section headers if we already did that. |
| 1542 | if (!section_headers.empty()) |
| 1543 | return section_headers.size(); |
Todd Fiala | 6477ea8 | 2014-07-11 15:13:33 +0000 | [diff] [blame] | 1544 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1545 | // Only initialize the arch_spec to okay defaults if they're not already set. |
| 1546 | // We'll refine this with note data as we parse the notes. |
| 1547 | if (arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) { |
| 1548 | llvm::Triple::OSType ostype; |
| 1549 | llvm::Triple::OSType spec_ostype; |
| 1550 | const uint32_t sub_type = subTypeFromElfHeader(header); |
| 1551 | arch_spec.SetArchitecture(eArchTypeELF, header.e_machine, sub_type, |
| 1552 | header.e_ident[EI_OSABI]); |
Alexander Shaposhnikov | a08ba42 | 2016-12-05 18:42:21 +0000 | [diff] [blame] | 1553 | |
| 1554 | // Validate if it is ok to remove GetOsFromOSABI. |
| 1555 | // Note, that now the OS is determined based on EI_OSABI flag and |
| 1556 | // the info extracted from ELF notes (see RefineModuleDetailsFromNote). |
| 1557 | // However in some cases that still might be not enough: for example |
| 1558 | // a shared library might not have any notes at all |
| 1559 | // and have EI_OSABI flag set to System V, |
| 1560 | // as result the OS will be set to UnknownOS. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1561 | GetOsFromOSABI(header.e_ident[EI_OSABI], ostype); |
| 1562 | spec_ostype = arch_spec.GetTriple().getOS(); |
| 1563 | assert(spec_ostype == ostype); |
Hafiz Abid Qadeer | b155431 | 2017-01-20 10:24:03 +0000 | [diff] [blame] | 1564 | UNUSED_IF_ASSERT_DISABLED(spec_ostype); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | if (arch_spec.GetMachine() == llvm::Triple::mips || |
| 1568 | arch_spec.GetMachine() == llvm::Triple::mipsel || |
| 1569 | arch_spec.GetMachine() == llvm::Triple::mips64 || |
| 1570 | arch_spec.GetMachine() == llvm::Triple::mips64el) { |
| 1571 | switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE) { |
| 1572 | case llvm::ELF::EF_MIPS_MICROMIPS: |
| 1573 | arch_spec.SetFlags(ArchSpec::eMIPSAse_micromips); |
| 1574 | break; |
| 1575 | case llvm::ELF::EF_MIPS_ARCH_ASE_M16: |
| 1576 | arch_spec.SetFlags(ArchSpec::eMIPSAse_mips16); |
| 1577 | break; |
| 1578 | case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX: |
| 1579 | arch_spec.SetFlags(ArchSpec::eMIPSAse_mdmx); |
| 1580 | break; |
| 1581 | default: |
| 1582 | break; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 1583 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1584 | } |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 1585 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1586 | if (arch_spec.GetMachine() == llvm::Triple::arm || |
| 1587 | arch_spec.GetMachine() == llvm::Triple::thumb) { |
| 1588 | if (header.e_flags & llvm::ELF::EF_ARM_SOFT_FLOAT) |
| 1589 | arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float); |
| 1590 | else if (header.e_flags & llvm::ELF::EF_ARM_VFP_FLOAT) |
| 1591 | arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float); |
| 1592 | } |
Jaydeep Patil | 501a781 | 2015-07-16 03:51:55 +0000 | [diff] [blame] | 1593 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1594 | // If there are no section headers we are done. |
| 1595 | if (header.e_shnum == 0) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1596 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1597 | |
| 1598 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES)); |
| 1599 | |
| 1600 | section_headers.resize(header.e_shnum); |
| 1601 | if (section_headers.size() != header.e_shnum) |
| 1602 | return 0; |
| 1603 | |
| 1604 | const size_t sh_size = header.e_shnum * header.e_shentsize; |
| 1605 | const elf_off sh_offset = header.e_shoff; |
| 1606 | DataExtractor sh_data; |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1607 | if (sh_data.SetData(object_data, sh_offset, sh_size) != sh_size) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1608 | return 0; |
| 1609 | |
| 1610 | uint32_t idx; |
| 1611 | lldb::offset_t offset; |
| 1612 | for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) { |
| 1613 | if (section_headers[idx].Parse(sh_data, &offset) == false) |
| 1614 | break; |
| 1615 | } |
| 1616 | if (idx < section_headers.size()) |
| 1617 | section_headers.resize(idx); |
| 1618 | |
| 1619 | const unsigned strtab_idx = header.e_shstrndx; |
| 1620 | if (strtab_idx && strtab_idx < section_headers.size()) { |
| 1621 | const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx]; |
| 1622 | const size_t byte_size = sheader.sh_size; |
| 1623 | const Elf64_Off offset = sheader.sh_offset; |
| 1624 | lldb_private::DataExtractor shstr_data; |
| 1625 | |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1626 | if (shstr_data.SetData(object_data, offset, byte_size) == byte_size) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1627 | for (SectionHeaderCollIter I = section_headers.begin(); |
| 1628 | I != section_headers.end(); ++I) { |
| 1629 | static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink"); |
| 1630 | const ELFSectionHeaderInfo &sheader = *I; |
| 1631 | const uint64_t section_size = |
| 1632 | sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size; |
| 1633 | ConstString name(shstr_data.PeekCStr(I->sh_name)); |
| 1634 | |
| 1635 | I->section_name = name; |
| 1636 | |
| 1637 | if (arch_spec.IsMIPS()) { |
| 1638 | uint32_t arch_flags = arch_spec.GetFlags(); |
| 1639 | DataExtractor data; |
| 1640 | if (sheader.sh_type == SHT_MIPS_ABIFLAGS) { |
| 1641 | |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1642 | if (section_size && (data.SetData(object_data, sheader.sh_offset, |
| 1643 | section_size) == section_size)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1644 | // MIPS ASE Mask is at offset 12 in MIPS.abiflags section |
| 1645 | lldb::offset_t offset = 12; // MIPS ABI Flags Version: 0 |
| 1646 | arch_flags |= data.GetU32(&offset); |
| 1647 | |
| 1648 | // The floating point ABI is at offset 7 |
| 1649 | offset = 7; |
| 1650 | switch (data.GetU8(&offset)) { |
| 1651 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_ANY: |
| 1652 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_ANY; |
| 1653 | break; |
| 1654 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_DOUBLE: |
| 1655 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_DOUBLE; |
| 1656 | break; |
| 1657 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_SINGLE: |
| 1658 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SINGLE; |
| 1659 | break; |
| 1660 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_SOFT: |
| 1661 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SOFT; |
| 1662 | break; |
| 1663 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_OLD_64: |
| 1664 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_OLD_64; |
| 1665 | break; |
| 1666 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_XX: |
| 1667 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_XX; |
| 1668 | break; |
| 1669 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_64: |
| 1670 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64; |
| 1671 | break; |
| 1672 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_64A: |
| 1673 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64A; |
| 1674 | break; |
| 1675 | } |
| 1676 | } |
| 1677 | } |
| 1678 | // Settings appropriate ArchSpec ABI Flags |
| 1679 | switch (header.e_flags & llvm::ELF::EF_MIPS_ABI) { |
| 1680 | case llvm::ELF::EF_MIPS_ABI_O32: |
| 1681 | arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32; |
| 1682 | break; |
| 1683 | case EF_MIPS_ABI_O64: |
| 1684 | arch_flags |= lldb_private::ArchSpec::eMIPSABI_O64; |
| 1685 | break; |
| 1686 | case EF_MIPS_ABI_EABI32: |
| 1687 | arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI32; |
| 1688 | break; |
| 1689 | case EF_MIPS_ABI_EABI64: |
| 1690 | arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI64; |
| 1691 | break; |
| 1692 | default: |
| 1693 | // ABI Mask doesn't cover N32 and N64 ABI. |
| 1694 | if (header.e_ident[EI_CLASS] == llvm::ELF::ELFCLASS64) |
| 1695 | arch_flags |= lldb_private::ArchSpec::eMIPSABI_N64; |
Mehdi Amini | d16a6e3 | 2017-06-23 18:20:13 +0000 | [diff] [blame] | 1696 | else if (header.e_flags & llvm::ELF::EF_MIPS_ABI2) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1697 | arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32; |
| 1698 | break; |
| 1699 | } |
| 1700 | arch_spec.SetFlags(arch_flags); |
| 1701 | } |
| 1702 | |
| 1703 | if (arch_spec.GetMachine() == llvm::Triple::arm || |
| 1704 | arch_spec.GetMachine() == llvm::Triple::thumb) { |
| 1705 | DataExtractor data; |
| 1706 | |
| 1707 | if (sheader.sh_type == SHT_ARM_ATTRIBUTES && section_size != 0 && |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1708 | data.SetData(object_data, sheader.sh_offset, section_size) == section_size) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1709 | ParseARMAttributes(data, section_size, arch_spec); |
| 1710 | } |
| 1711 | |
| 1712 | if (name == g_sect_name_gnu_debuglink) { |
| 1713 | DataExtractor data; |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1714 | if (section_size && (data.SetData(object_data, sheader.sh_offset, |
| 1715 | section_size) == section_size)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1716 | lldb::offset_t gnu_debuglink_offset = 0; |
| 1717 | gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset); |
| 1718 | gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4); |
| 1719 | data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1); |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | // Process ELF note section entries. |
| 1724 | bool is_note_header = (sheader.sh_type == SHT_NOTE); |
| 1725 | |
| 1726 | // The section header ".note.android.ident" is stored as a |
| 1727 | // PROGBITS type header but it is actually a note header. |
| 1728 | static ConstString g_sect_name_android_ident(".note.android.ident"); |
| 1729 | if (!is_note_header && name == g_sect_name_android_ident) |
| 1730 | is_note_header = true; |
| 1731 | |
| 1732 | if (is_note_header) { |
| 1733 | // Allow notes to refine module info. |
| 1734 | DataExtractor data; |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1735 | if (section_size && (data.SetData(object_data, sheader.sh_offset, |
| 1736 | section_size) == section_size)) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1737 | Status error = RefineModuleDetailsFromNote(data, arch_spec, uuid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1738 | if (error.Fail()) { |
| 1739 | if (log) |
| 1740 | log->Printf("ObjectFileELF::%s ELF note processing failed: %s", |
| 1741 | __FUNCTION__, error.AsCString()); |
| 1742 | } |
| 1743 | } |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | // Make any unknown triple components to be unspecified unknowns. |
| 1748 | if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor) |
| 1749 | arch_spec.GetTriple().setVendorName(llvm::StringRef()); |
| 1750 | if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS) |
| 1751 | arch_spec.GetTriple().setOSName(llvm::StringRef()); |
| 1752 | |
| 1753 | return section_headers.size(); |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | section_headers.clear(); |
| 1758 | return 0; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1761 | size_t ObjectFileELF::GetProgramHeaderCount() { return ParseProgramHeaders(); } |
Ashok Thirumurthi | 4822d92 | 2013-07-11 20:39:00 +0000 | [diff] [blame] | 1762 | |
| 1763 | const elf::ELFProgramHeader * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1764 | ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id) { |
| 1765 | if (!id || !ParseProgramHeaders()) |
Ashok Thirumurthi | 4822d92 | 2013-07-11 20:39:00 +0000 | [diff] [blame] | 1766 | return NULL; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1767 | |
| 1768 | if (--id < m_program_headers.size()) |
| 1769 | return &m_program_headers[id]; |
| 1770 | |
| 1771 | return NULL; |
Ashok Thirumurthi | 4822d92 | 2013-07-11 20:39:00 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1774 | DataExtractor ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id) { |
| 1775 | const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id); |
| 1776 | if (segment_header == NULL) |
| 1777 | return DataExtractor(); |
| 1778 | return DataExtractor(m_data, segment_header->p_offset, |
| 1779 | segment_header->p_filesz); |
Ashok Thirumurthi | 4822d92 | 2013-07-11 20:39:00 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1782 | llvm::StringRef |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1783 | ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const { |
| 1784 | size_t pos = symbol_name.find('@'); |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1785 | return symbol_name.substr(0, pos); |
Pavel Labath | c6ae7ea | 2015-03-04 10:25:22 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1788 | //---------------------------------------------------------------------- |
| 1789 | // ParseSectionHeaders |
| 1790 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1791 | size_t ObjectFileELF::ParseSectionHeaders() { |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1792 | return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid, |
| 1793 | m_gnu_debuglink_file, m_gnu_debuglink_crc, |
| 1794 | m_arch_spec); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1797 | const ObjectFileELF::ELFSectionHeaderInfo * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1798 | ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) { |
| 1799 | if (!id || !ParseSectionHeaders()) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1800 | return NULL; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1801 | |
| 1802 | if (--id < m_section_headers.size()) |
| 1803 | return &m_section_headers[id]; |
| 1804 | |
| 1805 | return NULL; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1808 | lldb::user_id_t ObjectFileELF::GetSectionIndexByName(const char *name) { |
| 1809 | if (!name || !name[0] || !ParseSectionHeaders()) |
Tamas Berghammer | 85fadd9 | 2015-05-08 09:40:05 +0000 | [diff] [blame] | 1810 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1811 | for (size_t i = 1; i < m_section_headers.size(); ++i) |
| 1812 | if (m_section_headers[i].section_name == ConstString(name)) |
| 1813 | return i; |
| 1814 | return 0; |
Tamas Berghammer | 85fadd9 | 2015-05-08 09:40:05 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1817 | void ObjectFileELF::CreateSections(SectionList &unified_section_list) { |
| 1818 | if (!m_sections_ap.get() && ParseSectionHeaders()) { |
| 1819 | m_sections_ap.reset(new SectionList()); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 1820 | |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame^] | 1821 | // Object files frequently have 0 for every section address, meaning we |
| 1822 | // need to compute synthetic addresses in order for "file addresses" from |
| 1823 | // different sections to not overlap |
| 1824 | bool synthaddrs = (CalculateType() == ObjectFile::Type::eTypeObjectFile); |
| 1825 | uint64_t nextaddr = 0; |
| 1826 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 1827 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1828 | I != m_section_headers.end(); ++I) { |
| 1829 | const ELFSectionHeaderInfo &header = *I; |
| 1830 | |
| 1831 | ConstString &name = I->section_name; |
| 1832 | const uint64_t file_size = |
| 1833 | header.sh_type == SHT_NOBITS ? 0 : header.sh_size; |
| 1834 | const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0; |
| 1835 | |
| 1836 | static ConstString g_sect_name_text(".text"); |
| 1837 | static ConstString g_sect_name_data(".data"); |
| 1838 | static ConstString g_sect_name_bss(".bss"); |
| 1839 | static ConstString g_sect_name_tdata(".tdata"); |
| 1840 | static ConstString g_sect_name_tbss(".tbss"); |
| 1841 | static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev"); |
| 1842 | static ConstString g_sect_name_dwarf_debug_addr(".debug_addr"); |
| 1843 | static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges"); |
Tamas Berghammer | 963ce48 | 2017-08-25 13:56:14 +0000 | [diff] [blame] | 1844 | static ConstString g_sect_name_dwarf_debug_cu_index(".debug_cu_index"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1845 | static ConstString g_sect_name_dwarf_debug_frame(".debug_frame"); |
| 1846 | static ConstString g_sect_name_dwarf_debug_info(".debug_info"); |
| 1847 | static ConstString g_sect_name_dwarf_debug_line(".debug_line"); |
| 1848 | static ConstString g_sect_name_dwarf_debug_loc(".debug_loc"); |
| 1849 | static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo"); |
| 1850 | static ConstString g_sect_name_dwarf_debug_macro(".debug_macro"); |
| 1851 | static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames"); |
| 1852 | static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes"); |
| 1853 | static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges"); |
| 1854 | static ConstString g_sect_name_dwarf_debug_str(".debug_str"); |
| 1855 | static ConstString g_sect_name_dwarf_debug_str_offsets( |
| 1856 | ".debug_str_offsets"); |
| 1857 | static ConstString g_sect_name_dwarf_debug_abbrev_dwo( |
| 1858 | ".debug_abbrev.dwo"); |
| 1859 | static ConstString g_sect_name_dwarf_debug_info_dwo(".debug_info.dwo"); |
| 1860 | static ConstString g_sect_name_dwarf_debug_line_dwo(".debug_line.dwo"); |
| 1861 | static ConstString g_sect_name_dwarf_debug_macro_dwo(".debug_macro.dwo"); |
| 1862 | static ConstString g_sect_name_dwarf_debug_loc_dwo(".debug_loc.dwo"); |
| 1863 | static ConstString g_sect_name_dwarf_debug_str_dwo(".debug_str.dwo"); |
| 1864 | static ConstString g_sect_name_dwarf_debug_str_offsets_dwo( |
| 1865 | ".debug_str_offsets.dwo"); |
| 1866 | static ConstString g_sect_name_eh_frame(".eh_frame"); |
| 1867 | static ConstString g_sect_name_arm_exidx(".ARM.exidx"); |
| 1868 | static ConstString g_sect_name_arm_extab(".ARM.extab"); |
| 1869 | static ConstString g_sect_name_go_symtab(".gosymtab"); |
| 1870 | |
| 1871 | SectionType sect_type = eSectionTypeOther; |
| 1872 | |
| 1873 | bool is_thread_specific = false; |
| 1874 | |
| 1875 | if (name == g_sect_name_text) |
| 1876 | sect_type = eSectionTypeCode; |
| 1877 | else if (name == g_sect_name_data) |
| 1878 | sect_type = eSectionTypeData; |
| 1879 | else if (name == g_sect_name_bss) |
| 1880 | sect_type = eSectionTypeZeroFill; |
| 1881 | else if (name == g_sect_name_tdata) { |
| 1882 | sect_type = eSectionTypeData; |
| 1883 | is_thread_specific = true; |
| 1884 | } else if (name == g_sect_name_tbss) { |
| 1885 | sect_type = eSectionTypeZeroFill; |
| 1886 | is_thread_specific = true; |
| 1887 | } |
| 1888 | // .debug_abbrev – Abbreviations used in the .debug_info section |
| 1889 | // .debug_aranges – Lookup table for mapping addresses to compilation |
| 1890 | // units |
| 1891 | // .debug_frame – Call frame information |
| 1892 | // .debug_info – The core DWARF information section |
| 1893 | // .debug_line – Line number information |
| 1894 | // .debug_loc – Location lists used in DW_AT_location attributes |
| 1895 | // .debug_macinfo – Macro information |
| 1896 | // .debug_pubnames – Lookup table for mapping object and function names to |
| 1897 | // compilation units |
| 1898 | // .debug_pubtypes – Lookup table for mapping type names to compilation |
| 1899 | // units |
| 1900 | // .debug_ranges – Address ranges used in DW_AT_ranges attributes |
| 1901 | // .debug_str – String table used in .debug_info |
| 1902 | // MISSING? .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section, |
| 1903 | // http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html |
| 1904 | // MISSING? .debug-index - |
| 1905 | // http://src.chromium.org/viewvc/chrome/trunk/src/build/gdb-add-index?pathrev=144644 |
| 1906 | // MISSING? .debug_types - Type descriptions from DWARF 4? See |
| 1907 | // http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo |
| 1908 | else if (name == g_sect_name_dwarf_debug_abbrev) |
| 1909 | sect_type = eSectionTypeDWARFDebugAbbrev; |
| 1910 | else if (name == g_sect_name_dwarf_debug_addr) |
| 1911 | sect_type = eSectionTypeDWARFDebugAddr; |
| 1912 | else if (name == g_sect_name_dwarf_debug_aranges) |
| 1913 | sect_type = eSectionTypeDWARFDebugAranges; |
Tamas Berghammer | 963ce48 | 2017-08-25 13:56:14 +0000 | [diff] [blame] | 1914 | else if (name == g_sect_name_dwarf_debug_cu_index) |
| 1915 | sect_type = eSectionTypeDWARFDebugCuIndex; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1916 | else if (name == g_sect_name_dwarf_debug_frame) |
| 1917 | sect_type = eSectionTypeDWARFDebugFrame; |
| 1918 | else if (name == g_sect_name_dwarf_debug_info) |
| 1919 | sect_type = eSectionTypeDWARFDebugInfo; |
| 1920 | else if (name == g_sect_name_dwarf_debug_line) |
| 1921 | sect_type = eSectionTypeDWARFDebugLine; |
| 1922 | else if (name == g_sect_name_dwarf_debug_loc) |
| 1923 | sect_type = eSectionTypeDWARFDebugLoc; |
| 1924 | else if (name == g_sect_name_dwarf_debug_macinfo) |
| 1925 | sect_type = eSectionTypeDWARFDebugMacInfo; |
| 1926 | else if (name == g_sect_name_dwarf_debug_macro) |
| 1927 | sect_type = eSectionTypeDWARFDebugMacro; |
| 1928 | else if (name == g_sect_name_dwarf_debug_pubnames) |
| 1929 | sect_type = eSectionTypeDWARFDebugPubNames; |
| 1930 | else if (name == g_sect_name_dwarf_debug_pubtypes) |
| 1931 | sect_type = eSectionTypeDWARFDebugPubTypes; |
| 1932 | else if (name == g_sect_name_dwarf_debug_ranges) |
| 1933 | sect_type = eSectionTypeDWARFDebugRanges; |
| 1934 | else if (name == g_sect_name_dwarf_debug_str) |
| 1935 | sect_type = eSectionTypeDWARFDebugStr; |
| 1936 | else if (name == g_sect_name_dwarf_debug_str_offsets) |
| 1937 | sect_type = eSectionTypeDWARFDebugStrOffsets; |
| 1938 | else if (name == g_sect_name_dwarf_debug_abbrev_dwo) |
| 1939 | sect_type = eSectionTypeDWARFDebugAbbrev; |
| 1940 | else if (name == g_sect_name_dwarf_debug_info_dwo) |
| 1941 | sect_type = eSectionTypeDWARFDebugInfo; |
| 1942 | else if (name == g_sect_name_dwarf_debug_line_dwo) |
| 1943 | sect_type = eSectionTypeDWARFDebugLine; |
| 1944 | else if (name == g_sect_name_dwarf_debug_macro_dwo) |
| 1945 | sect_type = eSectionTypeDWARFDebugMacro; |
| 1946 | else if (name == g_sect_name_dwarf_debug_loc_dwo) |
| 1947 | sect_type = eSectionTypeDWARFDebugLoc; |
| 1948 | else if (name == g_sect_name_dwarf_debug_str_dwo) |
| 1949 | sect_type = eSectionTypeDWARFDebugStr; |
| 1950 | else if (name == g_sect_name_dwarf_debug_str_offsets_dwo) |
| 1951 | sect_type = eSectionTypeDWARFDebugStrOffsets; |
| 1952 | else if (name == g_sect_name_eh_frame) |
| 1953 | sect_type = eSectionTypeEHFrame; |
| 1954 | else if (name == g_sect_name_arm_exidx) |
| 1955 | sect_type = eSectionTypeARMexidx; |
| 1956 | else if (name == g_sect_name_arm_extab) |
| 1957 | sect_type = eSectionTypeARMextab; |
| 1958 | else if (name == g_sect_name_go_symtab) |
| 1959 | sect_type = eSectionTypeGoSymtab; |
| 1960 | |
| 1961 | const uint32_t permissions = |
Ilia K | 4f730dc | 2016-09-12 05:25:33 +0000 | [diff] [blame] | 1962 | ((header.sh_flags & SHF_ALLOC) ? ePermissionsReadable : 0u) | |
| 1963 | ((header.sh_flags & SHF_WRITE) ? ePermissionsWritable : 0u) | |
| 1964 | ((header.sh_flags & SHF_EXECINSTR) ? ePermissionsExecutable : 0u); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1965 | switch (header.sh_type) { |
| 1966 | case SHT_SYMTAB: |
| 1967 | assert(sect_type == eSectionTypeOther); |
| 1968 | sect_type = eSectionTypeELFSymbolTable; |
| 1969 | break; |
| 1970 | case SHT_DYNSYM: |
| 1971 | assert(sect_type == eSectionTypeOther); |
| 1972 | sect_type = eSectionTypeELFDynamicSymbols; |
| 1973 | break; |
| 1974 | case SHT_RELA: |
| 1975 | case SHT_REL: |
| 1976 | assert(sect_type == eSectionTypeOther); |
| 1977 | sect_type = eSectionTypeELFRelocationEntries; |
| 1978 | break; |
| 1979 | case SHT_DYNAMIC: |
| 1980 | assert(sect_type == eSectionTypeOther); |
| 1981 | sect_type = eSectionTypeELFDynamicLinkInfo; |
| 1982 | break; |
| 1983 | } |
| 1984 | |
| 1985 | if (eSectionTypeOther == sect_type) { |
| 1986 | // the kalimba toolchain assumes that ELF section names are free-form. |
| 1987 | // It does |
| 1988 | // support linkscripts which (can) give rise to various arbitrarily |
| 1989 | // named |
| 1990 | // sections being "Code" or "Data". |
| 1991 | sect_type = kalimbaSectionType(m_header, header); |
| 1992 | } |
| 1993 | |
| 1994 | const uint32_t target_bytes_size = |
| 1995 | (eSectionTypeData == sect_type || eSectionTypeZeroFill == sect_type) |
| 1996 | ? m_arch_spec.GetDataByteSize() |
| 1997 | : eSectionTypeCode == sect_type ? m_arch_spec.GetCodeByteSize() |
| 1998 | : 1; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1999 | elf::elf_xword log2align = |
| 2000 | (header.sh_addralign == 0) ? 0 : llvm::Log2_64(header.sh_addralign); |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame^] | 2001 | |
| 2002 | uint64_t addr = header.sh_addr; |
| 2003 | |
| 2004 | if ((header.sh_flags & SHF_ALLOC) && synthaddrs) { |
| 2005 | nextaddr = |
| 2006 | (nextaddr + header.sh_addralign - 1) & ~(header.sh_addralign - 1); |
| 2007 | addr = nextaddr; |
| 2008 | nextaddr += vm_size; |
| 2009 | } |
| 2010 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2011 | SectionSP section_sp(new Section( |
| 2012 | GetModule(), // Module to which this section belongs. |
| 2013 | this, // ObjectFile to which this section belongs and should read |
| 2014 | // section data from. |
| 2015 | SectionIndex(I), // Section ID. |
| 2016 | name, // Section name. |
| 2017 | sect_type, // Section type. |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame^] | 2018 | addr, // VM address. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2019 | vm_size, // VM size in bytes of this section. |
| 2020 | header.sh_offset, // Offset of this section in the file. |
| 2021 | file_size, // Size of the section as found in the file. |
| 2022 | log2align, // Alignment of the section |
| 2023 | header.sh_flags, // Flags for this section. |
| 2024 | target_bytes_size)); // Number of host bytes per target byte |
| 2025 | |
| 2026 | section_sp->SetPermissions(permissions); |
| 2027 | if (is_thread_specific) |
| 2028 | section_sp->SetIsThreadSpecific(is_thread_specific); |
| 2029 | m_sections_ap->AddSection(section_sp); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2030 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | if (m_sections_ap.get()) { |
| 2034 | if (GetType() == eTypeDebugInfo) { |
| 2035 | static const SectionType g_sections[] = { |
Tamas Berghammer | 963ce48 | 2017-08-25 13:56:14 +0000 | [diff] [blame] | 2036 | eSectionTypeDWARFDebugAbbrev, eSectionTypeDWARFDebugAddr, |
| 2037 | eSectionTypeDWARFDebugAranges, eSectionTypeDWARFDebugCuIndex, |
| 2038 | eSectionTypeDWARFDebugFrame, eSectionTypeDWARFDebugInfo, |
| 2039 | eSectionTypeDWARFDebugLine, eSectionTypeDWARFDebugLoc, |
| 2040 | eSectionTypeDWARFDebugMacInfo, eSectionTypeDWARFDebugPubNames, |
| 2041 | eSectionTypeDWARFDebugPubTypes, eSectionTypeDWARFDebugRanges, |
| 2042 | eSectionTypeDWARFDebugStr, eSectionTypeDWARFDebugStrOffsets, |
| 2043 | eSectionTypeELFSymbolTable, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2044 | }; |
| 2045 | SectionList *elf_section_list = m_sections_ap.get(); |
| 2046 | for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); |
| 2047 | ++idx) { |
| 2048 | SectionType section_type = g_sections[idx]; |
| 2049 | SectionSP section_sp( |
| 2050 | elf_section_list->FindSectionByType(section_type, true)); |
| 2051 | if (section_sp) { |
| 2052 | SectionSP module_section_sp( |
| 2053 | unified_section_list.FindSectionByType(section_type, true)); |
| 2054 | if (module_section_sp) |
| 2055 | unified_section_list.ReplaceSection(module_section_sp->GetID(), |
| 2056 | section_sp); |
| 2057 | else |
| 2058 | unified_section_list.AddSection(section_sp); |
| 2059 | } |
| 2060 | } |
| 2061 | } else { |
| 2062 | unified_section_list = *m_sections_ap; |
| 2063 | } |
| 2064 | } |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2067 | // Find the arm/aarch64 mapping symbol character in the given symbol name. |
| 2068 | // Mapping symbols have the |
| 2069 | // form of "$<char>[.<any>]*". Additionally we recognize cases when the mapping |
| 2070 | // symbol prefixed by |
| 2071 | // an arbitrary string because if a symbol prefix added to each symbol in the |
| 2072 | // object file with |
| 2073 | // objcopy then the mapping symbols are also prefixed. |
| 2074 | static char FindArmAarch64MappingSymbol(const char *symbol_name) { |
| 2075 | if (!symbol_name) |
| 2076 | return '\0'; |
| 2077 | |
| 2078 | const char *dollar_pos = ::strchr(symbol_name, '$'); |
| 2079 | if (!dollar_pos || dollar_pos[1] == '\0') |
| 2080 | return '\0'; |
| 2081 | |
| 2082 | if (dollar_pos[2] == '\0' || dollar_pos[2] == '.') |
| 2083 | return dollar_pos[1]; |
| 2084 | return '\0'; |
| 2085 | } |
| 2086 | |
| 2087 | #define STO_MIPS_ISA (3 << 6) |
| 2088 | #define STO_MICROMIPS (2 << 6) |
| 2089 | #define IS_MICROMIPS(ST_OTHER) (((ST_OTHER)&STO_MIPS_ISA) == STO_MICROMIPS) |
| 2090 | |
| 2091 | // private |
| 2092 | unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id, |
| 2093 | SectionList *section_list, |
| 2094 | const size_t num_symbols, |
| 2095 | const DataExtractor &symtab_data, |
| 2096 | const DataExtractor &strtab_data) { |
| 2097 | ELFSymbol symbol; |
| 2098 | lldb::offset_t offset = 0; |
| 2099 | |
| 2100 | static ConstString text_section_name(".text"); |
| 2101 | static ConstString init_section_name(".init"); |
| 2102 | static ConstString fini_section_name(".fini"); |
| 2103 | static ConstString ctors_section_name(".ctors"); |
| 2104 | static ConstString dtors_section_name(".dtors"); |
| 2105 | |
| 2106 | static ConstString data_section_name(".data"); |
| 2107 | static ConstString rodata_section_name(".rodata"); |
| 2108 | static ConstString rodata1_section_name(".rodata1"); |
| 2109 | static ConstString data2_section_name(".data1"); |
| 2110 | static ConstString bss_section_name(".bss"); |
| 2111 | static ConstString opd_section_name(".opd"); // For ppc64 |
| 2112 | |
| 2113 | // On Android the oatdata and the oatexec symbols in the oat and odex files |
| 2114 | // covers the full |
| 2115 | // .text section what causes issues with displaying unusable symbol name to |
| 2116 | // the user and very |
| 2117 | // slow unwinding speed because the instruction emulation based unwind plans |
| 2118 | // try to emulate all |
| 2119 | // instructions in these symbols. Don't add these symbols to the symbol list |
| 2120 | // as they have no |
| 2121 | // use for the debugger and they are causing a lot of trouble. |
| 2122 | // Filtering can't be restricted to Android because this special object file |
| 2123 | // don't contain the |
| 2124 | // note section specifying the environment to Android but the custom extension |
| 2125 | // and file name |
| 2126 | // makes it highly unlikely that this will collide with anything else. |
| 2127 | ConstString file_extension = m_file.GetFileNameExtension(); |
| 2128 | bool skip_oatdata_oatexec = file_extension == ConstString("oat") || |
| 2129 | file_extension == ConstString("odex"); |
| 2130 | |
| 2131 | ArchSpec arch; |
| 2132 | GetArchitecture(arch); |
| 2133 | ModuleSP module_sp(GetModule()); |
| 2134 | SectionList *module_section_list = |
| 2135 | module_sp ? module_sp->GetSectionList() : nullptr; |
| 2136 | |
| 2137 | // Local cache to avoid doing a FindSectionByName for each symbol. The "const |
| 2138 | // char*" key must |
| 2139 | // came from a ConstString object so they can be compared by pointer |
| 2140 | std::unordered_map<const char *, lldb::SectionSP> section_name_to_section; |
| 2141 | |
| 2142 | unsigned i; |
| 2143 | for (i = 0; i < num_symbols; ++i) { |
| 2144 | if (symbol.Parse(symtab_data, &offset) == false) |
| 2145 | break; |
| 2146 | |
| 2147 | const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); |
| 2148 | if (!symbol_name) |
| 2149 | symbol_name = ""; |
| 2150 | |
| 2151 | // No need to add non-section symbols that have no names |
| 2152 | if (symbol.getType() != STT_SECTION && |
| 2153 | (symbol_name == nullptr || symbol_name[0] == '\0')) |
| 2154 | continue; |
| 2155 | |
| 2156 | // Skipping oatdata and oatexec sections if it is requested. See details |
| 2157 | // above the |
| 2158 | // definition of skip_oatdata_oatexec for the reasons. |
| 2159 | if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 || |
| 2160 | ::strcmp(symbol_name, "oatexec") == 0)) |
| 2161 | continue; |
| 2162 | |
| 2163 | SectionSP symbol_section_sp; |
| 2164 | SymbolType symbol_type = eSymbolTypeInvalid; |
| 2165 | Elf64_Half section_idx = symbol.st_shndx; |
| 2166 | |
| 2167 | switch (section_idx) { |
| 2168 | case SHN_ABS: |
| 2169 | symbol_type = eSymbolTypeAbsolute; |
| 2170 | break; |
| 2171 | case SHN_UNDEF: |
| 2172 | symbol_type = eSymbolTypeUndefined; |
| 2173 | break; |
| 2174 | default: |
| 2175 | symbol_section_sp = section_list->GetSectionAtIndex(section_idx); |
| 2176 | break; |
| 2177 | } |
| 2178 | |
| 2179 | // If a symbol is undefined do not process it further even if it has a STT |
| 2180 | // type |
| 2181 | if (symbol_type != eSymbolTypeUndefined) { |
| 2182 | switch (symbol.getType()) { |
| 2183 | default: |
| 2184 | case STT_NOTYPE: |
| 2185 | // The symbol's type is not specified. |
| 2186 | break; |
| 2187 | |
| 2188 | case STT_OBJECT: |
| 2189 | // The symbol is associated with a data object, such as a variable, |
| 2190 | // an array, etc. |
| 2191 | symbol_type = eSymbolTypeData; |
| 2192 | break; |
| 2193 | |
| 2194 | case STT_FUNC: |
| 2195 | // The symbol is associated with a function or other executable code. |
| 2196 | symbol_type = eSymbolTypeCode; |
| 2197 | break; |
| 2198 | |
| 2199 | case STT_SECTION: |
| 2200 | // The symbol is associated with a section. Symbol table entries of |
| 2201 | // this type exist primarily for relocation and normally have |
| 2202 | // STB_LOCAL binding. |
| 2203 | break; |
| 2204 | |
| 2205 | case STT_FILE: |
| 2206 | // Conventionally, the symbol's name gives the name of the source |
| 2207 | // file associated with the object file. A file symbol has STB_LOCAL |
| 2208 | // binding, its section index is SHN_ABS, and it precedes the other |
| 2209 | // STB_LOCAL symbols for the file, if it is present. |
| 2210 | symbol_type = eSymbolTypeSourceFile; |
| 2211 | break; |
| 2212 | |
| 2213 | case STT_GNU_IFUNC: |
| 2214 | // The symbol is associated with an indirect function. The actual |
| 2215 | // function will be resolved if it is referenced. |
| 2216 | symbol_type = eSymbolTypeResolver; |
| 2217 | break; |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | if (symbol_type == eSymbolTypeInvalid && symbol.getType() != STT_SECTION) { |
| 2222 | if (symbol_section_sp) { |
| 2223 | const ConstString §_name = symbol_section_sp->GetName(); |
| 2224 | if (sect_name == text_section_name || sect_name == init_section_name || |
| 2225 | sect_name == fini_section_name || sect_name == ctors_section_name || |
| 2226 | sect_name == dtors_section_name) { |
| 2227 | symbol_type = eSymbolTypeCode; |
| 2228 | } else if (sect_name == data_section_name || |
| 2229 | sect_name == data2_section_name || |
| 2230 | sect_name == rodata_section_name || |
| 2231 | sect_name == rodata1_section_name || |
| 2232 | sect_name == bss_section_name) { |
| 2233 | symbol_type = eSymbolTypeData; |
| 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | int64_t symbol_value_offset = 0; |
| 2239 | uint32_t additional_flags = 0; |
| 2240 | |
| 2241 | if (arch.IsValid()) { |
| 2242 | if (arch.GetMachine() == llvm::Triple::arm) { |
| 2243 | if (symbol.getBinding() == STB_LOCAL) { |
| 2244 | char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name); |
| 2245 | if (symbol_type == eSymbolTypeCode) { |
| 2246 | switch (mapping_symbol) { |
| 2247 | case 'a': |
| 2248 | // $a[.<any>]* - marks an ARM instruction sequence |
| 2249 | m_address_class_map[symbol.st_value] = eAddressClassCode; |
| 2250 | break; |
| 2251 | case 'b': |
| 2252 | case 't': |
| 2253 | // $b[.<any>]* - marks a THUMB BL instruction sequence |
| 2254 | // $t[.<any>]* - marks a THUMB instruction sequence |
| 2255 | m_address_class_map[symbol.st_value] = |
| 2256 | eAddressClassCodeAlternateISA; |
| 2257 | break; |
| 2258 | case 'd': |
| 2259 | // $d[.<any>]* - marks a data item sequence (e.g. lit pool) |
| 2260 | m_address_class_map[symbol.st_value] = eAddressClassData; |
| 2261 | break; |
| 2262 | } |
| 2263 | } |
| 2264 | if (mapping_symbol) |
| 2265 | continue; |
| 2266 | } |
| 2267 | } else if (arch.GetMachine() == llvm::Triple::aarch64) { |
| 2268 | if (symbol.getBinding() == STB_LOCAL) { |
| 2269 | char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name); |
| 2270 | if (symbol_type == eSymbolTypeCode) { |
| 2271 | switch (mapping_symbol) { |
| 2272 | case 'x': |
| 2273 | // $x[.<any>]* - marks an A64 instruction sequence |
| 2274 | m_address_class_map[symbol.st_value] = eAddressClassCode; |
| 2275 | break; |
| 2276 | case 'd': |
| 2277 | // $d[.<any>]* - marks a data item sequence (e.g. lit pool) |
| 2278 | m_address_class_map[symbol.st_value] = eAddressClassData; |
| 2279 | break; |
| 2280 | } |
| 2281 | } |
| 2282 | if (mapping_symbol) |
| 2283 | continue; |
| 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | if (arch.GetMachine() == llvm::Triple::arm) { |
| 2288 | if (symbol_type == eSymbolTypeCode) { |
| 2289 | if (symbol.st_value & 1) { |
| 2290 | // Subtracting 1 from the address effectively unsets |
| 2291 | // the low order bit, which results in the address |
| 2292 | // actually pointing to the beginning of the symbol. |
| 2293 | // This delta will be used below in conjunction with |
| 2294 | // symbol.st_value to produce the final symbol_value |
| 2295 | // that we store in the symtab. |
| 2296 | symbol_value_offset = -1; |
| 2297 | m_address_class_map[symbol.st_value ^ 1] = |
| 2298 | eAddressClassCodeAlternateISA; |
| 2299 | } else { |
| 2300 | // This address is ARM |
| 2301 | m_address_class_map[symbol.st_value] = eAddressClassCode; |
| 2302 | } |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | /* |
| 2307 | * MIPS: |
| 2308 | * The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for |
| 2309 | * MIPS). |
| 2310 | * This allows processor to switch between microMIPS and MIPS without any |
| 2311 | * need |
| 2312 | * for special mode-control register. However, apart from .debug_line, |
| 2313 | * none of |
| 2314 | * the ELF/DWARF sections set the ISA bit (for symbol or section). Use |
| 2315 | * st_other |
| 2316 | * flag to check whether the symbol is microMIPS and then set the address |
| 2317 | * class |
| 2318 | * accordingly. |
| 2319 | */ |
| 2320 | const llvm::Triple::ArchType llvm_arch = arch.GetMachine(); |
| 2321 | if (llvm_arch == llvm::Triple::mips || |
| 2322 | llvm_arch == llvm::Triple::mipsel || |
| 2323 | llvm_arch == llvm::Triple::mips64 || |
| 2324 | llvm_arch == llvm::Triple::mips64el) { |
| 2325 | if (IS_MICROMIPS(symbol.st_other)) |
| 2326 | m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA; |
| 2327 | else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) { |
| 2328 | symbol.st_value = symbol.st_value & (~1ull); |
| 2329 | m_address_class_map[symbol.st_value] = eAddressClassCodeAlternateISA; |
| 2330 | } else { |
| 2331 | if (symbol_type == eSymbolTypeCode) |
| 2332 | m_address_class_map[symbol.st_value] = eAddressClassCode; |
| 2333 | else if (symbol_type == eSymbolTypeData) |
| 2334 | m_address_class_map[symbol.st_value] = eAddressClassData; |
| 2335 | else |
| 2336 | m_address_class_map[symbol.st_value] = eAddressClassUnknown; |
| 2337 | } |
| 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | // symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB |
| 2342 | // symbols. See above for |
| 2343 | // more details. |
| 2344 | uint64_t symbol_value = symbol.st_value + symbol_value_offset; |
| 2345 | |
| 2346 | if (symbol_section_sp == nullptr && section_idx == SHN_ABS && |
| 2347 | symbol.st_size != 0) { |
| 2348 | // We don't have a section for a symbol with non-zero size. Create a new |
| 2349 | // section for it |
| 2350 | // so the address range covered by the symbol is also covered by the |
| 2351 | // module (represented |
| 2352 | // through the section list). It is needed so module lookup for the |
| 2353 | // addresses covered |
| 2354 | // by this symbol will be successfull. This case happens for absolute |
| 2355 | // symbols. |
| 2356 | ConstString fake_section_name(std::string(".absolute.") + symbol_name); |
| 2357 | symbol_section_sp = |
| 2358 | std::make_shared<Section>(module_sp, this, SHN_ABS, fake_section_name, |
| 2359 | eSectionTypeAbsoluteAddress, symbol_value, |
| 2360 | symbol.st_size, 0, 0, 0, SHF_ALLOC); |
| 2361 | |
| 2362 | module_section_list->AddSection(symbol_section_sp); |
| 2363 | section_list->AddSection(symbol_section_sp); |
| 2364 | } |
| 2365 | |
| 2366 | if (symbol_section_sp && |
| 2367 | CalculateType() != ObjectFile::Type::eTypeObjectFile) |
| 2368 | symbol_value -= symbol_section_sp->GetFileAddress(); |
| 2369 | |
| 2370 | if (symbol_section_sp && module_section_list && |
| 2371 | module_section_list != section_list) { |
| 2372 | const ConstString §_name = symbol_section_sp->GetName(); |
| 2373 | auto section_it = section_name_to_section.find(sect_name.GetCString()); |
| 2374 | if (section_it == section_name_to_section.end()) |
| 2375 | section_it = |
| 2376 | section_name_to_section |
| 2377 | .emplace(sect_name.GetCString(), |
| 2378 | module_section_list->FindSectionByName(sect_name)) |
| 2379 | .first; |
Pavel Labath | efddda3d | 2017-05-02 12:40:31 +0000 | [diff] [blame] | 2380 | if (section_it->second) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2381 | symbol_section_sp = section_it->second; |
| 2382 | } |
| 2383 | |
| 2384 | bool is_global = symbol.getBinding() == STB_GLOBAL; |
| 2385 | uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags; |
| 2386 | bool is_mangled = (symbol_name[0] == '_' && symbol_name[1] == 'Z'); |
| 2387 | |
| 2388 | llvm::StringRef symbol_ref(symbol_name); |
| 2389 | |
| 2390 | // Symbol names may contain @VERSION suffixes. Find those and strip them |
| 2391 | // temporarily. |
| 2392 | size_t version_pos = symbol_ref.find('@'); |
| 2393 | bool has_suffix = version_pos != llvm::StringRef::npos; |
| 2394 | llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos); |
| 2395 | Mangled mangled(ConstString(symbol_bare), is_mangled); |
| 2396 | |
| 2397 | // Now append the suffix back to mangled and unmangled names. Only do it if |
| 2398 | // the |
| 2399 | // demangling was successful (string is not empty). |
| 2400 | if (has_suffix) { |
| 2401 | llvm::StringRef suffix = symbol_ref.substr(version_pos); |
| 2402 | |
| 2403 | llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef(); |
| 2404 | if (!mangled_name.empty()) |
| 2405 | mangled.SetMangledName(ConstString((mangled_name + suffix).str())); |
| 2406 | |
| 2407 | ConstString demangled = |
| 2408 | mangled.GetDemangledName(lldb::eLanguageTypeUnknown); |
| 2409 | llvm::StringRef demangled_name = demangled.GetStringRef(); |
| 2410 | if (!demangled_name.empty()) |
| 2411 | mangled.SetDemangledName(ConstString((demangled_name + suffix).str())); |
| 2412 | } |
| 2413 | |
| 2414 | // In ELF all symbol should have a valid size but it is not true for some |
| 2415 | // function symbols |
| 2416 | // coming from hand written assembly. As none of the function symbol should |
| 2417 | // have 0 size we |
| 2418 | // try to calculate the size for these symbols in the symtab with saying |
| 2419 | // that their original |
| 2420 | // size is not valid. |
| 2421 | bool symbol_size_valid = |
| 2422 | symbol.st_size != 0 || symbol.getType() != STT_FUNC; |
| 2423 | |
| 2424 | Symbol dc_symbol( |
| 2425 | i + start_id, // ID is the original symbol table index. |
| 2426 | mangled, |
| 2427 | symbol_type, // Type of this symbol |
| 2428 | is_global, // Is this globally visible? |
| 2429 | false, // Is this symbol debug info? |
| 2430 | false, // Is this symbol a trampoline? |
| 2431 | false, // Is this symbol artificial? |
| 2432 | AddressRange(symbol_section_sp, // Section in which this symbol is |
| 2433 | // defined or null. |
| 2434 | symbol_value, // Offset in section or symbol value. |
| 2435 | symbol.st_size), // Size in bytes of this symbol. |
| 2436 | symbol_size_valid, // Symbol size is valid |
| 2437 | has_suffix, // Contains linker annotations? |
| 2438 | flags); // Symbol flags. |
| 2439 | symtab->AddSymbol(dc_symbol); |
| 2440 | } |
| 2441 | return i; |
| 2442 | } |
| 2443 | |
| 2444 | unsigned ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, |
| 2445 | user_id_t start_id, |
| 2446 | lldb_private::Section *symtab) { |
| 2447 | if (symtab->GetObjectFile() != this) { |
| 2448 | // If the symbol table section is owned by a different object file, have it |
| 2449 | // do the |
| 2450 | // parsing. |
| 2451 | ObjectFileELF *obj_file_elf = |
| 2452 | static_cast<ObjectFileELF *>(symtab->GetObjectFile()); |
| 2453 | return obj_file_elf->ParseSymbolTable(symbol_table, start_id, symtab); |
| 2454 | } |
| 2455 | |
| 2456 | // Get section list for this object file. |
| 2457 | SectionList *section_list = m_sections_ap.get(); |
| 2458 | if (!section_list) |
| 2459 | return 0; |
| 2460 | |
| 2461 | user_id_t symtab_id = symtab->GetID(); |
| 2462 | const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id); |
| 2463 | assert(symtab_hdr->sh_type == SHT_SYMTAB || |
| 2464 | symtab_hdr->sh_type == SHT_DYNSYM); |
| 2465 | |
| 2466 | // sh_link: section header index of associated string table. |
| 2467 | // Section ID's are ones based. |
| 2468 | user_id_t strtab_id = symtab_hdr->sh_link + 1; |
| 2469 | Section *strtab = section_list->FindSectionByID(strtab_id).get(); |
| 2470 | |
| 2471 | if (symtab && strtab) { |
| 2472 | assert(symtab->GetObjectFile() == this); |
| 2473 | assert(strtab->GetObjectFile() == this); |
| 2474 | |
| 2475 | DataExtractor symtab_data; |
| 2476 | DataExtractor strtab_data; |
| 2477 | if (ReadSectionData(symtab, symtab_data) && |
| 2478 | ReadSectionData(strtab, strtab_data)) { |
| 2479 | size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize; |
| 2480 | |
| 2481 | return ParseSymbols(symbol_table, start_id, section_list, num_symbols, |
| 2482 | symtab_data, strtab_data); |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | return 0; |
| 2487 | } |
| 2488 | |
| 2489 | size_t ObjectFileELF::ParseDynamicSymbols() { |
| 2490 | if (m_dynamic_symbols.size()) |
| 2491 | return m_dynamic_symbols.size(); |
| 2492 | |
| 2493 | SectionList *section_list = GetSectionList(); |
| 2494 | if (!section_list) |
| 2495 | return 0; |
| 2496 | |
| 2497 | // Find the SHT_DYNAMIC section. |
| 2498 | Section *dynsym = |
| 2499 | section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true) |
| 2500 | .get(); |
| 2501 | if (!dynsym) |
| 2502 | return 0; |
| 2503 | assert(dynsym->GetObjectFile() == this); |
| 2504 | |
| 2505 | ELFDynamic symbol; |
| 2506 | DataExtractor dynsym_data; |
| 2507 | if (ReadSectionData(dynsym, dynsym_data)) { |
| 2508 | const lldb::offset_t section_size = dynsym_data.GetByteSize(); |
| 2509 | lldb::offset_t cursor = 0; |
| 2510 | |
| 2511 | while (cursor < section_size) { |
| 2512 | if (!symbol.Parse(dynsym_data, &cursor)) |
| 2513 | break; |
| 2514 | |
| 2515 | m_dynamic_symbols.push_back(symbol); |
| 2516 | } |
| 2517 | } |
| 2518 | |
| 2519 | return m_dynamic_symbols.size(); |
| 2520 | } |
| 2521 | |
| 2522 | const ELFDynamic *ObjectFileELF::FindDynamicSymbol(unsigned tag) { |
| 2523 | if (!ParseDynamicSymbols()) |
| 2524 | return NULL; |
| 2525 | |
| 2526 | DynamicSymbolCollIter I = m_dynamic_symbols.begin(); |
| 2527 | DynamicSymbolCollIter E = m_dynamic_symbols.end(); |
| 2528 | for (; I != E; ++I) { |
| 2529 | ELFDynamic *symbol = &*I; |
| 2530 | |
| 2531 | if (symbol->d_tag == tag) |
| 2532 | return symbol; |
| 2533 | } |
| 2534 | |
| 2535 | return NULL; |
| 2536 | } |
| 2537 | |
| 2538 | unsigned ObjectFileELF::PLTRelocationType() { |
| 2539 | // DT_PLTREL |
| 2540 | // This member specifies the type of relocation entry to which the |
| 2541 | // procedure linkage table refers. The d_val member holds DT_REL or |
| 2542 | // DT_RELA, as appropriate. All relocations in a procedure linkage table |
| 2543 | // must use the same relocation. |
| 2544 | const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); |
| 2545 | |
| 2546 | if (symbol) |
| 2547 | return symbol->d_val; |
| 2548 | |
| 2549 | return 0; |
| 2550 | } |
| 2551 | |
| 2552 | // Returns the size of the normal plt entries and the offset of the first normal |
| 2553 | // plt entry. The |
| 2554 | // 0th entry in the plt table is usually a resolution entry which have different |
| 2555 | // size in some |
| 2556 | // architectures then the rest of the plt entries. |
| 2557 | static std::pair<uint64_t, uint64_t> |
| 2558 | GetPltEntrySizeAndOffset(const ELFSectionHeader *rel_hdr, |
| 2559 | const ELFSectionHeader *plt_hdr) { |
| 2560 | const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; |
| 2561 | |
| 2562 | // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 16 |
| 2563 | // bytes. |
| 2564 | // So round the entsize up by the alignment if addralign is set. |
| 2565 | elf_xword plt_entsize = |
| 2566 | plt_hdr->sh_addralign |
| 2567 | ? llvm::alignTo(plt_hdr->sh_entsize, plt_hdr->sh_addralign) |
| 2568 | : plt_hdr->sh_entsize; |
| 2569 | |
| 2570 | // Some linkers e.g ld for arm, fill plt_hdr->sh_entsize field incorrectly. |
| 2571 | // PLT entries relocation code in general requires multiple instruction and |
| 2572 | // should be greater than 4 bytes in most cases. Try to guess correct size |
| 2573 | // just in case. |
| 2574 | if (plt_entsize <= 4) { |
| 2575 | // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the |
| 2576 | // size of the plt |
| 2577 | // entries based on the number of entries and the size of the plt section |
| 2578 | // with the |
| 2579 | // assumption that the size of the 0th entry is at least as big as the size |
| 2580 | // of the normal |
| 2581 | // entries and it isn't much bigger then that. |
| 2582 | if (plt_hdr->sh_addralign) |
| 2583 | plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign / |
| 2584 | (num_relocations + 1) * plt_hdr->sh_addralign; |
| 2585 | else |
| 2586 | plt_entsize = plt_hdr->sh_size / (num_relocations + 1); |
| 2587 | } |
| 2588 | |
| 2589 | elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize; |
| 2590 | |
| 2591 | return std::make_pair(plt_entsize, plt_offset); |
| 2592 | } |
| 2593 | |
| 2594 | static unsigned ParsePLTRelocations( |
| 2595 | Symtab *symbol_table, user_id_t start_id, unsigned rel_type, |
| 2596 | const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, |
| 2597 | const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr, |
| 2598 | const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data, |
| 2599 | DataExtractor &symtab_data, DataExtractor &strtab_data) { |
| 2600 | ELFRelocation rel(rel_type); |
| 2601 | ELFSymbol symbol; |
| 2602 | lldb::offset_t offset = 0; |
| 2603 | |
| 2604 | uint64_t plt_offset, plt_entsize; |
| 2605 | std::tie(plt_entsize, plt_offset) = |
| 2606 | GetPltEntrySizeAndOffset(rel_hdr, plt_hdr); |
| 2607 | const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; |
| 2608 | |
| 2609 | typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); |
| 2610 | reloc_info_fn reloc_type; |
| 2611 | reloc_info_fn reloc_symbol; |
| 2612 | |
| 2613 | if (hdr->Is32Bit()) { |
| 2614 | reloc_type = ELFRelocation::RelocType32; |
| 2615 | reloc_symbol = ELFRelocation::RelocSymbol32; |
| 2616 | } else { |
| 2617 | reloc_type = ELFRelocation::RelocType64; |
| 2618 | reloc_symbol = ELFRelocation::RelocSymbol64; |
| 2619 | } |
| 2620 | |
| 2621 | unsigned slot_type = hdr->GetRelocationJumpSlotType(); |
| 2622 | unsigned i; |
| 2623 | for (i = 0; i < num_relocations; ++i) { |
| 2624 | if (rel.Parse(rel_data, &offset) == false) |
| 2625 | break; |
| 2626 | |
| 2627 | if (reloc_type(rel) != slot_type) |
| 2628 | continue; |
| 2629 | |
| 2630 | lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; |
| 2631 | if (!symbol.Parse(symtab_data, &symbol_offset)) |
| 2632 | break; |
| 2633 | |
| 2634 | const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); |
| 2635 | bool is_mangled = |
| 2636 | symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; |
| 2637 | uint64_t plt_index = plt_offset + i * plt_entsize; |
| 2638 | |
| 2639 | Symbol jump_symbol( |
| 2640 | i + start_id, // Symbol table index |
| 2641 | symbol_name, // symbol name. |
| 2642 | is_mangled, // is the symbol name mangled? |
| 2643 | eSymbolTypeTrampoline, // Type of this symbol |
| 2644 | false, // Is this globally visible? |
| 2645 | false, // Is this symbol debug info? |
| 2646 | true, // Is this symbol a trampoline? |
| 2647 | true, // Is this symbol artificial? |
| 2648 | plt_section_sp, // Section in which this symbol is defined or null. |
| 2649 | plt_index, // Offset in section or symbol value. |
| 2650 | plt_entsize, // Size in bytes of this symbol. |
| 2651 | true, // Size is valid |
| 2652 | false, // Contains linker annotations? |
| 2653 | 0); // Symbol flags. |
| 2654 | |
| 2655 | symbol_table->AddSymbol(jump_symbol); |
| 2656 | } |
| 2657 | |
| 2658 | return i; |
| 2659 | } |
| 2660 | |
| 2661 | unsigned |
| 2662 | ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id, |
| 2663 | const ELFSectionHeaderInfo *rel_hdr, |
| 2664 | user_id_t rel_id) { |
| 2665 | assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); |
| 2666 | |
| 2667 | // The link field points to the associated symbol table. |
| 2668 | user_id_t symtab_id = rel_hdr->sh_link; |
| 2669 | |
| 2670 | // If the link field doesn't point to the appropriate symbol name table then |
| 2671 | // try to find it by name as some compiler don't fill in the link fields. |
| 2672 | if (!symtab_id) |
| 2673 | symtab_id = GetSectionIndexByName(".dynsym"); |
| 2674 | |
| 2675 | // Get PLT section. We cannot use rel_hdr->sh_info, since current linkers |
| 2676 | // point that to the .got.plt or .got section instead of .plt. |
| 2677 | user_id_t plt_id = GetSectionIndexByName(".plt"); |
| 2678 | |
| 2679 | if (!symtab_id || !plt_id) |
| 2680 | return 0; |
| 2681 | |
| 2682 | // Section ID's are ones based; |
| 2683 | symtab_id++; |
| 2684 | plt_id++; |
| 2685 | |
| 2686 | const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id); |
| 2687 | if (!plt_hdr) |
| 2688 | return 0; |
| 2689 | |
| 2690 | const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id); |
| 2691 | if (!sym_hdr) |
| 2692 | return 0; |
| 2693 | |
| 2694 | SectionList *section_list = m_sections_ap.get(); |
| 2695 | if (!section_list) |
| 2696 | return 0; |
| 2697 | |
| 2698 | Section *rel_section = section_list->FindSectionByID(rel_id).get(); |
| 2699 | if (!rel_section) |
| 2700 | return 0; |
| 2701 | |
| 2702 | SectionSP plt_section_sp(section_list->FindSectionByID(plt_id)); |
| 2703 | if (!plt_section_sp) |
| 2704 | return 0; |
| 2705 | |
| 2706 | Section *symtab = section_list->FindSectionByID(symtab_id).get(); |
| 2707 | if (!symtab) |
| 2708 | return 0; |
| 2709 | |
| 2710 | // sh_link points to associated string table. |
| 2711 | Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get(); |
| 2712 | if (!strtab) |
| 2713 | return 0; |
| 2714 | |
| 2715 | DataExtractor rel_data; |
| 2716 | if (!ReadSectionData(rel_section, rel_data)) |
| 2717 | return 0; |
| 2718 | |
| 2719 | DataExtractor symtab_data; |
| 2720 | if (!ReadSectionData(symtab, symtab_data)) |
| 2721 | return 0; |
| 2722 | |
| 2723 | DataExtractor strtab_data; |
| 2724 | if (!ReadSectionData(strtab, strtab_data)) |
| 2725 | return 0; |
| 2726 | |
| 2727 | unsigned rel_type = PLTRelocationType(); |
| 2728 | if (!rel_type) |
| 2729 | return 0; |
| 2730 | |
| 2731 | return ParsePLTRelocations(symbol_table, start_id, rel_type, &m_header, |
| 2732 | rel_hdr, plt_hdr, sym_hdr, plt_section_sp, |
| 2733 | rel_data, symtab_data, strtab_data); |
| 2734 | } |
| 2735 | |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame^] | 2736 | unsigned ObjectFileELF::ApplyRelocations( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2737 | Symtab *symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, |
| 2738 | const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr, |
| 2739 | DataExtractor &rel_data, DataExtractor &symtab_data, |
| 2740 | DataExtractor &debug_data, Section *rel_section) { |
| 2741 | ELFRelocation rel(rel_hdr->sh_type); |
| 2742 | lldb::addr_t offset = 0; |
| 2743 | const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; |
| 2744 | typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); |
| 2745 | reloc_info_fn reloc_type; |
| 2746 | reloc_info_fn reloc_symbol; |
| 2747 | |
| 2748 | if (hdr->Is32Bit()) { |
| 2749 | reloc_type = ELFRelocation::RelocType32; |
| 2750 | reloc_symbol = ELFRelocation::RelocSymbol32; |
| 2751 | } else { |
| 2752 | reloc_type = ELFRelocation::RelocType64; |
| 2753 | reloc_symbol = ELFRelocation::RelocSymbol64; |
| 2754 | } |
| 2755 | |
| 2756 | for (unsigned i = 0; i < num_relocations; ++i) { |
| 2757 | if (rel.Parse(rel_data, &offset) == false) |
| 2758 | break; |
| 2759 | |
| 2760 | Symbol *symbol = NULL; |
| 2761 | |
| 2762 | if (hdr->Is32Bit()) { |
| 2763 | switch (reloc_type(rel)) { |
| 2764 | case R_386_32: |
| 2765 | case R_386_PC32: |
| 2766 | default: |
| 2767 | assert(false && "unexpected relocation type"); |
| 2768 | } |
| 2769 | } else { |
| 2770 | switch (reloc_type(rel)) { |
| 2771 | case R_X86_64_64: { |
| 2772 | symbol = symtab->FindSymbolByID(reloc_symbol(rel)); |
| 2773 | if (symbol) { |
| 2774 | addr_t value = symbol->GetAddressRef().GetFileAddress(); |
| 2775 | DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); |
| 2776 | uint64_t *dst = reinterpret_cast<uint64_t *>( |
| 2777 | data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + |
| 2778 | ELFRelocation::RelocOffset64(rel)); |
| 2779 | *dst = value + ELFRelocation::RelocAddend64(rel); |
| 2780 | } |
| 2781 | break; |
| 2782 | } |
| 2783 | case R_X86_64_32: |
| 2784 | case R_X86_64_32S: { |
| 2785 | symbol = symtab->FindSymbolByID(reloc_symbol(rel)); |
| 2786 | if (symbol) { |
| 2787 | addr_t value = symbol->GetAddressRef().GetFileAddress(); |
| 2788 | value += ELFRelocation::RelocAddend32(rel); |
| 2789 | assert( |
| 2790 | (reloc_type(rel) == R_X86_64_32 && (value <= UINT32_MAX)) || |
| 2791 | (reloc_type(rel) == R_X86_64_32S && |
| 2792 | ((int64_t)value <= INT32_MAX && (int64_t)value >= INT32_MIN))); |
| 2793 | uint32_t truncated_addr = (value & 0xFFFFFFFF); |
| 2794 | DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); |
| 2795 | uint32_t *dst = reinterpret_cast<uint32_t *>( |
| 2796 | data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + |
| 2797 | ELFRelocation::RelocOffset32(rel)); |
| 2798 | *dst = truncated_addr; |
| 2799 | } |
| 2800 | break; |
| 2801 | } |
| 2802 | case R_X86_64_PC32: |
| 2803 | default: |
| 2804 | assert(false && "unexpected relocation type"); |
| 2805 | } |
| 2806 | } |
| 2807 | } |
| 2808 | |
| 2809 | return 0; |
| 2810 | } |
| 2811 | |
| 2812 | unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame^] | 2813 | user_id_t rel_id, |
| 2814 | lldb_private::Symtab *thetab) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2815 | assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); |
| 2816 | |
| 2817 | // Parse in the section list if needed. |
| 2818 | SectionList *section_list = GetSectionList(); |
| 2819 | if (!section_list) |
| 2820 | return 0; |
| 2821 | |
| 2822 | // Section ID's are ones based. |
| 2823 | user_id_t symtab_id = rel_hdr->sh_link + 1; |
| 2824 | user_id_t debug_id = rel_hdr->sh_info + 1; |
| 2825 | |
| 2826 | const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id); |
| 2827 | if (!symtab_hdr) |
| 2828 | return 0; |
| 2829 | |
| 2830 | const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id); |
| 2831 | if (!debug_hdr) |
| 2832 | return 0; |
| 2833 | |
| 2834 | Section *rel = section_list->FindSectionByID(rel_id).get(); |
| 2835 | if (!rel) |
| 2836 | return 0; |
| 2837 | |
| 2838 | Section *symtab = section_list->FindSectionByID(symtab_id).get(); |
| 2839 | if (!symtab) |
| 2840 | return 0; |
| 2841 | |
| 2842 | Section *debug = section_list->FindSectionByID(debug_id).get(); |
| 2843 | if (!debug) |
| 2844 | return 0; |
| 2845 | |
| 2846 | DataExtractor rel_data; |
| 2847 | DataExtractor symtab_data; |
| 2848 | DataExtractor debug_data; |
| 2849 | |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame^] | 2850 | if (GetData(rel->GetFileOffset(), rel->GetFileSize(), rel_data) && |
| 2851 | GetData(symtab->GetFileOffset(), symtab->GetFileSize(), symtab_data) && |
| 2852 | GetData(debug->GetFileOffset(), debug->GetFileSize(), debug_data)) { |
| 2853 | ApplyRelocations(thetab, &m_header, rel_hdr, symtab_hdr, debug_hdr, |
| 2854 | rel_data, symtab_data, debug_data, debug); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | return 0; |
| 2858 | } |
| 2859 | |
| 2860 | Symtab *ObjectFileELF::GetSymtab() { |
| 2861 | ModuleSP module_sp(GetModule()); |
| 2862 | if (!module_sp) |
| 2863 | return NULL; |
| 2864 | |
| 2865 | // We always want to use the main object file so we (hopefully) only have one |
| 2866 | // cached copy |
| 2867 | // of our symtab, dynamic sections, etc. |
| 2868 | ObjectFile *module_obj_file = module_sp->GetObjectFile(); |
| 2869 | if (module_obj_file && module_obj_file != this) |
| 2870 | return module_obj_file->GetSymtab(); |
| 2871 | |
| 2872 | if (m_symtab_ap.get() == NULL) { |
| 2873 | SectionList *section_list = module_sp->GetSectionList(); |
Ashok Thirumurthi | 35729bb | 2013-09-24 15:34:13 +0000 | [diff] [blame] | 2874 | if (!section_list) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2875 | return NULL; |
Ashok Thirumurthi | 35729bb | 2013-09-24 15:34:13 +0000 | [diff] [blame] | 2876 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2877 | uint64_t symbol_id = 0; |
| 2878 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
Tamas Berghammer | 6b63b14 | 2016-02-18 11:12:18 +0000 | [diff] [blame] | 2879 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2880 | // Sharable objects and dynamic executables usually have 2 distinct symbol |
| 2881 | // tables, one named ".symtab", and the other ".dynsym". The dynsym is a |
| 2882 | // smaller |
| 2883 | // version of the symtab that only contains global symbols. The information |
| 2884 | // found |
| 2885 | // in the dynsym is therefore also found in the symtab, while the reverse is |
| 2886 | // not |
| 2887 | // necessarily true. |
| 2888 | Section *symtab = |
| 2889 | section_list->FindSectionByType(eSectionTypeELFSymbolTable, true).get(); |
| 2890 | if (!symtab) { |
| 2891 | // The symtab section is non-allocable and can be stripped, so if it |
| 2892 | // doesn't exist |
| 2893 | // then use the dynsym section which should always be there. |
| 2894 | symtab = |
| 2895 | section_list->FindSectionByType(eSectionTypeELFDynamicSymbols, true) |
| 2896 | .get(); |
| 2897 | } |
| 2898 | if (symtab) { |
| 2899 | m_symtab_ap.reset(new Symtab(symtab->GetObjectFile())); |
| 2900 | symbol_id += ParseSymbolTable(m_symtab_ap.get(), symbol_id, symtab); |
| 2901 | } |
| 2902 | |
| 2903 | // DT_JMPREL |
| 2904 | // If present, this entry's d_ptr member holds the address of |
| 2905 | // relocation |
| 2906 | // entries associated solely with the procedure linkage table. |
| 2907 | // Separating |
| 2908 | // these relocation entries lets the dynamic linker ignore them during |
| 2909 | // process initialization, if lazy binding is enabled. If this entry is |
| 2910 | // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must |
| 2911 | // also be present. |
| 2912 | const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); |
| 2913 | if (symbol) { |
| 2914 | // Synthesize trampoline symbols to help navigate the PLT. |
| 2915 | addr_t addr = symbol->d_ptr; |
| 2916 | Section *reloc_section = |
| 2917 | section_list->FindSectionContainingFileAddress(addr).get(); |
| 2918 | if (reloc_section) { |
| 2919 | user_id_t reloc_id = reloc_section->GetID(); |
| 2920 | const ELFSectionHeaderInfo *reloc_header = |
| 2921 | GetSectionHeaderByIndex(reloc_id); |
| 2922 | assert(reloc_header); |
| 2923 | |
| 2924 | if (m_symtab_ap == nullptr) |
| 2925 | m_symtab_ap.reset(new Symtab(reloc_section->GetObjectFile())); |
| 2926 | |
| 2927 | ParseTrampolineSymbols(m_symtab_ap.get(), symbol_id, reloc_header, |
| 2928 | reloc_id); |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | DWARFCallFrameInfo *eh_frame = GetUnwindTable().GetEHFrameInfo(); |
| 2933 | if (eh_frame) { |
| 2934 | if (m_symtab_ap == nullptr) |
| 2935 | m_symtab_ap.reset(new Symtab(this)); |
| 2936 | ParseUnwindSymbols(m_symtab_ap.get(), eh_frame); |
| 2937 | } |
| 2938 | |
| 2939 | // If we still don't have any symtab then create an empty instance to avoid |
| 2940 | // do the section |
| 2941 | // lookup next time. |
| 2942 | if (m_symtab_ap == nullptr) |
| 2943 | m_symtab_ap.reset(new Symtab(this)); |
| 2944 | |
| 2945 | m_symtab_ap->CalculateSymbolSizes(); |
| 2946 | } |
| 2947 | |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame^] | 2948 | return m_symtab_ap.get(); |
| 2949 | } |
| 2950 | |
| 2951 | void ObjectFileELF::RelocateSection(lldb_private::Section *section) |
| 2952 | { |
| 2953 | static const char *debug_prefix = ".debug"; |
| 2954 | |
| 2955 | // Set relocated bit so we stop getting called, regardless of |
| 2956 | // whether we actually relocate. |
| 2957 | section->SetIsRelocated(true); |
| 2958 | |
| 2959 | // We only relocate in ELF relocatable files |
| 2960 | if (CalculateType() != eTypeObjectFile) |
| 2961 | return; |
| 2962 | |
| 2963 | const char *section_name = section->GetName().GetCString(); |
| 2964 | // Can't relocate that which can't be named |
| 2965 | if (section_name == nullptr) |
| 2966 | return; |
| 2967 | |
| 2968 | // We don't relocate non-debug sections at the moment |
| 2969 | if (strncmp(section_name, debug_prefix, strlen(debug_prefix))) |
| 2970 | return; |
| 2971 | |
| 2972 | // Relocation section names to look for |
| 2973 | std::string needle = std::string(".rel") + section_name; |
| 2974 | std::string needlea = std::string(".rela") + section_name; |
| 2975 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2976 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 2977 | I != m_section_headers.end(); ++I) { |
| 2978 | if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) { |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame^] | 2979 | const char *hay_name = I->section_name.GetCString(); |
| 2980 | if (hay_name == nullptr) |
| 2981 | continue; |
| 2982 | if (needle == hay_name || needlea == hay_name) { |
| 2983 | const ELFSectionHeader &reloc_header = *I; |
| 2984 | user_id_t reloc_id = SectionIndex(I); |
| 2985 | RelocateDebugSections(&reloc_header, reloc_id, GetSymtab()); |
| 2986 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2987 | } |
| 2988 | } |
| 2989 | } |
Tamas Berghammer | 6b63b14 | 2016-02-18 11:12:18 +0000 | [diff] [blame] | 2990 | } |
Tamas Berghammer | 5bfd4d0 | 2016-02-10 12:10:58 +0000 | [diff] [blame] | 2991 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2992 | void ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table, |
| 2993 | DWARFCallFrameInfo *eh_frame) { |
| 2994 | SectionList *section_list = GetSectionList(); |
| 2995 | if (!section_list) |
| 2996 | return; |
| 2997 | |
| 2998 | // First we save the new symbols into a separate list and add them to the |
| 2999 | // symbol table after |
| 3000 | // we colleced all symbols we want to add. This is neccessary because adding a |
| 3001 | // new symbol |
| 3002 | // invalidates the internal index of the symtab what causing the next lookup |
| 3003 | // to be slow because |
| 3004 | // it have to recalculate the index first. |
| 3005 | std::vector<Symbol> new_symbols; |
| 3006 | |
| 3007 | eh_frame->ForEachFDEEntries([this, symbol_table, section_list, &new_symbols]( |
| 3008 | lldb::addr_t file_addr, uint32_t size, dw_offset_t) { |
| 3009 | Symbol *symbol = symbol_table->FindSymbolAtFileAddress(file_addr); |
| 3010 | if (symbol) { |
| 3011 | if (!symbol->GetByteSizeIsValid()) { |
| 3012 | symbol->SetByteSize(size); |
| 3013 | symbol->SetSizeIsSynthesized(true); |
| 3014 | } |
| 3015 | } else { |
| 3016 | SectionSP section_sp = |
| 3017 | section_list->FindSectionContainingFileAddress(file_addr); |
| 3018 | if (section_sp) { |
| 3019 | addr_t offset = file_addr - section_sp->GetFileAddress(); |
| 3020 | const char *symbol_name = GetNextSyntheticSymbolName().GetCString(); |
| 3021 | uint64_t symbol_id = symbol_table->GetNumSymbols(); |
| 3022 | Symbol eh_symbol( |
| 3023 | symbol_id, // Symbol table index. |
| 3024 | symbol_name, // Symbol name. |
| 3025 | false, // Is the symbol name mangled? |
| 3026 | eSymbolTypeCode, // Type of this symbol. |
| 3027 | true, // Is this globally visible? |
| 3028 | false, // Is this symbol debug info? |
| 3029 | false, // Is this symbol a trampoline? |
| 3030 | true, // Is this symbol artificial? |
| 3031 | section_sp, // Section in which this symbol is defined or null. |
| 3032 | offset, // Offset in section or symbol value. |
| 3033 | 0, // Size: Don't specify the size as an FDE can |
| 3034 | false, // Size is valid: cover multiple symbols. |
| 3035 | false, // Contains linker annotations? |
| 3036 | 0); // Symbol flags. |
| 3037 | new_symbols.push_back(eh_symbol); |
| 3038 | } |
| 3039 | } |
| 3040 | return true; |
| 3041 | }); |
| 3042 | |
| 3043 | for (const Symbol &s : new_symbols) |
| 3044 | symbol_table->AddSymbol(s); |
| 3045 | } |
| 3046 | |
| 3047 | bool ObjectFileELF::IsStripped() { |
| 3048 | // TODO: determine this for ELF |
| 3049 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3052 | //===----------------------------------------------------------------------===// |
| 3053 | // Dump |
| 3054 | // |
| 3055 | // Dump the specifics of the runtime file container (such as any headers |
| 3056 | // segments, sections, etc). |
| 3057 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3058 | void ObjectFileELF::Dump(Stream *s) { |
| 3059 | ModuleSP module_sp(GetModule()); |
| 3060 | if (!module_sp) { |
| 3061 | return; |
| 3062 | } |
Adrian McCarthy | 543725c | 2016-04-04 21:21:49 +0000 | [diff] [blame] | 3063 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3064 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 3065 | s->Printf("%p: ", static_cast<void *>(this)); |
| 3066 | s->Indent(); |
| 3067 | s->PutCString("ObjectFileELF"); |
Adrian McCarthy | 543725c | 2016-04-04 21:21:49 +0000 | [diff] [blame] | 3068 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3069 | ArchSpec header_arch; |
| 3070 | GetArchitecture(header_arch); |
Adrian McCarthy | 543725c | 2016-04-04 21:21:49 +0000 | [diff] [blame] | 3071 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3072 | *s << ", file = '" << m_file |
| 3073 | << "', arch = " << header_arch.GetArchitectureName() << "\n"; |
Adrian McCarthy | 543725c | 2016-04-04 21:21:49 +0000 | [diff] [blame] | 3074 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3075 | DumpELFHeader(s, m_header); |
| 3076 | s->EOL(); |
| 3077 | DumpELFProgramHeaders(s); |
| 3078 | s->EOL(); |
| 3079 | DumpELFSectionHeaders(s); |
| 3080 | s->EOL(); |
| 3081 | SectionList *section_list = GetSectionList(); |
| 3082 | if (section_list) |
| 3083 | section_list->Dump(s, NULL, true, UINT32_MAX); |
| 3084 | Symtab *symtab = GetSymtab(); |
| 3085 | if (symtab) |
| 3086 | symtab->Dump(s, NULL, eSortOrderNone); |
| 3087 | s->EOL(); |
| 3088 | DumpDependentModules(s); |
| 3089 | s->EOL(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | //---------------------------------------------------------------------- |
| 3093 | // DumpELFHeader |
| 3094 | // |
| 3095 | // Dump the ELF header to the specified output stream |
| 3096 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3097 | void ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) { |
| 3098 | s->PutCString("ELF Header\n"); |
| 3099 | s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); |
| 3100 | s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG1], |
| 3101 | header.e_ident[EI_MAG1]); |
| 3102 | s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG2], |
| 3103 | header.e_ident[EI_MAG2]); |
| 3104 | s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG3], |
| 3105 | header.e_ident[EI_MAG3]); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3107 | s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); |
| 3108 | s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); |
| 3109 | DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); |
| 3110 | s->Printf("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); |
| 3111 | s->Printf("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3113 | s->Printf("e_type = 0x%4.4x ", header.e_type); |
| 3114 | DumpELFHeader_e_type(s, header.e_type); |
| 3115 | s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); |
| 3116 | s->Printf("e_version = 0x%8.8x\n", header.e_version); |
| 3117 | s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry); |
| 3118 | s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff); |
| 3119 | s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff); |
| 3120 | s->Printf("e_flags = 0x%8.8x\n", header.e_flags); |
| 3121 | s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); |
| 3122 | s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); |
Pavel Labath | 23ccc29 | 2017-01-31 23:09:46 +0000 | [diff] [blame] | 3123 | s->Printf("e_phnum = 0x%8.8x\n", header.e_phnum); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3124 | s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); |
Pavel Labath | 23ccc29 | 2017-01-31 23:09:46 +0000 | [diff] [blame] | 3125 | s->Printf("e_shnum = 0x%8.8x\n", header.e_shnum); |
| 3126 | s->Printf("e_shstrndx = 0x%8.8x\n", header.e_shstrndx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | //---------------------------------------------------------------------- |
| 3130 | // DumpELFHeader_e_type |
| 3131 | // |
| 3132 | // Dump an token value for the ELF header member e_type |
| 3133 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3134 | void ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) { |
| 3135 | switch (e_type) { |
| 3136 | case ET_NONE: |
| 3137 | *s << "ET_NONE"; |
| 3138 | break; |
| 3139 | case ET_REL: |
| 3140 | *s << "ET_REL"; |
| 3141 | break; |
| 3142 | case ET_EXEC: |
| 3143 | *s << "ET_EXEC"; |
| 3144 | break; |
| 3145 | case ET_DYN: |
| 3146 | *s << "ET_DYN"; |
| 3147 | break; |
| 3148 | case ET_CORE: |
| 3149 | *s << "ET_CORE"; |
| 3150 | break; |
| 3151 | default: |
| 3152 | break; |
| 3153 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | //---------------------------------------------------------------------- |
| 3157 | // DumpELFHeader_e_ident_EI_DATA |
| 3158 | // |
| 3159 | // Dump an token value for the ELF header member e_ident[EI_DATA] |
| 3160 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3161 | void ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, |
| 3162 | unsigned char ei_data) { |
| 3163 | switch (ei_data) { |
| 3164 | case ELFDATANONE: |
| 3165 | *s << "ELFDATANONE"; |
| 3166 | break; |
| 3167 | case ELFDATA2LSB: |
| 3168 | *s << "ELFDATA2LSB - Little Endian"; |
| 3169 | break; |
| 3170 | case ELFDATA2MSB: |
| 3171 | *s << "ELFDATA2MSB - Big Endian"; |
| 3172 | break; |
| 3173 | default: |
| 3174 | break; |
| 3175 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3178 | //---------------------------------------------------------------------- |
| 3179 | // DumpELFProgramHeader |
| 3180 | // |
| 3181 | // Dump a single ELF program header to the specified output stream |
| 3182 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3183 | void ObjectFileELF::DumpELFProgramHeader(Stream *s, |
| 3184 | const ELFProgramHeader &ph) { |
| 3185 | DumpELFProgramHeader_p_type(s, ph.p_type); |
| 3186 | s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, |
| 3187 | ph.p_vaddr, ph.p_paddr); |
| 3188 | s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, |
| 3189 | ph.p_flags); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 3190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3191 | DumpELFProgramHeader_p_flags(s, ph.p_flags); |
| 3192 | s->Printf(") %8.8" PRIx64, ph.p_align); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | //---------------------------------------------------------------------- |
| 3196 | // DumpELFProgramHeader_p_type |
| 3197 | // |
| 3198 | // Dump an token value for the ELF program header member p_type which |
| 3199 | // describes the type of the program header |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 3200 | // ---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3201 | void ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) { |
| 3202 | const int kStrWidth = 15; |
| 3203 | switch (p_type) { |
| 3204 | CASE_AND_STREAM(s, PT_NULL, kStrWidth); |
| 3205 | CASE_AND_STREAM(s, PT_LOAD, kStrWidth); |
| 3206 | CASE_AND_STREAM(s, PT_DYNAMIC, kStrWidth); |
| 3207 | CASE_AND_STREAM(s, PT_INTERP, kStrWidth); |
| 3208 | CASE_AND_STREAM(s, PT_NOTE, kStrWidth); |
| 3209 | CASE_AND_STREAM(s, PT_SHLIB, kStrWidth); |
| 3210 | CASE_AND_STREAM(s, PT_PHDR, kStrWidth); |
| 3211 | CASE_AND_STREAM(s, PT_TLS, kStrWidth); |
Filipe Cabecinhas | 477d86d | 2013-05-23 23:01:14 +0000 | [diff] [blame] | 3212 | CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3213 | default: |
| 3214 | s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); |
| 3215 | break; |
| 3216 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3219 | //---------------------------------------------------------------------- |
| 3220 | // DumpELFProgramHeader_p_flags |
| 3221 | // |
| 3222 | // Dump an token value for the ELF program header member p_flags |
| 3223 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3224 | void ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) { |
| 3225 | *s << ((p_flags & PF_X) ? "PF_X" : " ") |
| 3226 | << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') |
| 3227 | << ((p_flags & PF_W) ? "PF_W" : " ") |
| 3228 | << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') |
| 3229 | << ((p_flags & PF_R) ? "PF_R" : " "); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | //---------------------------------------------------------------------- |
| 3233 | // DumpELFProgramHeaders |
| 3234 | // |
| 3235 | // Dump all of the ELF program header to the specified output stream |
| 3236 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3237 | void ObjectFileELF::DumpELFProgramHeaders(Stream *s) { |
| 3238 | if (!ParseProgramHeaders()) |
| 3239 | return; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3241 | s->PutCString("Program Headers\n"); |
| 3242 | s->PutCString("IDX p_type p_offset p_vaddr p_paddr " |
| 3243 | "p_filesz p_memsz p_flags p_align\n"); |
| 3244 | s->PutCString("==== --------------- -------- -------- -------- " |
| 3245 | "-------- -------- ------------------------- --------\n"); |
Ed Maste | 3a8ab6e | 2015-02-23 15:33:11 +0000 | [diff] [blame] | 3246 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3247 | uint32_t idx = 0; |
| 3248 | for (ProgramHeaderCollConstIter I = m_program_headers.begin(); |
| 3249 | I != m_program_headers.end(); ++I, ++idx) { |
| 3250 | s->Printf("[%2u] ", idx); |
| 3251 | ObjectFileELF::DumpELFProgramHeader(s, *I); |
| 3252 | s->EOL(); |
| 3253 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3256 | //---------------------------------------------------------------------- |
| 3257 | // DumpELFSectionHeader |
| 3258 | // |
| 3259 | // Dump a single ELF section header to the specified output stream |
| 3260 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3261 | void ObjectFileELF::DumpELFSectionHeader(Stream *s, |
| 3262 | const ELFSectionHeaderInfo &sh) { |
| 3263 | s->Printf("%8.8x ", sh.sh_name); |
| 3264 | DumpELFSectionHeader_sh_type(s, sh.sh_type); |
| 3265 | s->Printf(" %8.8" PRIx64 " (", sh.sh_flags); |
| 3266 | DumpELFSectionHeader_sh_flags(s, sh.sh_flags); |
| 3267 | s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, |
| 3268 | sh.sh_offset, sh.sh_size); |
| 3269 | s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); |
| 3270 | s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | //---------------------------------------------------------------------- |
| 3274 | // DumpELFSectionHeader_sh_type |
| 3275 | // |
| 3276 | // Dump an token value for the ELF section header member sh_type which |
| 3277 | // describes the type of the section |
| 3278 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3279 | void ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) { |
| 3280 | const int kStrWidth = 12; |
| 3281 | switch (sh_type) { |
| 3282 | CASE_AND_STREAM(s, SHT_NULL, kStrWidth); |
| 3283 | CASE_AND_STREAM(s, SHT_PROGBITS, kStrWidth); |
| 3284 | CASE_AND_STREAM(s, SHT_SYMTAB, kStrWidth); |
| 3285 | CASE_AND_STREAM(s, SHT_STRTAB, kStrWidth); |
| 3286 | CASE_AND_STREAM(s, SHT_RELA, kStrWidth); |
| 3287 | CASE_AND_STREAM(s, SHT_HASH, kStrWidth); |
| 3288 | CASE_AND_STREAM(s, SHT_DYNAMIC, kStrWidth); |
| 3289 | CASE_AND_STREAM(s, SHT_NOTE, kStrWidth); |
| 3290 | CASE_AND_STREAM(s, SHT_NOBITS, kStrWidth); |
| 3291 | CASE_AND_STREAM(s, SHT_REL, kStrWidth); |
| 3292 | CASE_AND_STREAM(s, SHT_SHLIB, kStrWidth); |
| 3293 | CASE_AND_STREAM(s, SHT_DYNSYM, kStrWidth); |
| 3294 | CASE_AND_STREAM(s, SHT_LOPROC, kStrWidth); |
| 3295 | CASE_AND_STREAM(s, SHT_HIPROC, kStrWidth); |
| 3296 | CASE_AND_STREAM(s, SHT_LOUSER, kStrWidth); |
| 3297 | CASE_AND_STREAM(s, SHT_HIUSER, kStrWidth); |
| 3298 | default: |
| 3299 | s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); |
| 3300 | break; |
| 3301 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3302 | } |
| 3303 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3304 | //---------------------------------------------------------------------- |
| 3305 | // DumpELFSectionHeader_sh_flags |
| 3306 | // |
| 3307 | // Dump an token value for the ELF section header member sh_flags |
| 3308 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3309 | void ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, |
| 3310 | elf_xword sh_flags) { |
| 3311 | *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") |
| 3312 | << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') |
| 3313 | << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") |
| 3314 | << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') |
| 3315 | << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3316 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 3317 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3318 | //---------------------------------------------------------------------- |
| 3319 | // DumpELFSectionHeaders |
| 3320 | // |
| 3321 | // Dump all of the ELF section header to the specified output stream |
| 3322 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3323 | void ObjectFileELF::DumpELFSectionHeaders(Stream *s) { |
| 3324 | if (!ParseSectionHeaders()) |
| 3325 | return; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 3326 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3327 | s->PutCString("Section Headers\n"); |
| 3328 | s->PutCString("IDX name type flags " |
| 3329 | "addr offset size link info addralgn " |
| 3330 | "entsize Name\n"); |
| 3331 | s->PutCString("==== -------- ------------ -------------------------------- " |
| 3332 | "-------- -------- -------- -------- -------- -------- " |
| 3333 | "-------- ====================\n"); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 3334 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3335 | uint32_t idx = 0; |
| 3336 | for (SectionHeaderCollConstIter I = m_section_headers.begin(); |
| 3337 | I != m_section_headers.end(); ++I, ++idx) { |
| 3338 | s->Printf("[%2u] ", idx); |
| 3339 | ObjectFileELF::DumpELFSectionHeader(s, *I); |
| 3340 | const char *section_name = I->section_name.AsCString(""); |
| 3341 | if (section_name) |
| 3342 | *s << ' ' << section_name << "\n"; |
| 3343 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 3344 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3345 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3346 | void ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) { |
| 3347 | size_t num_modules = ParseDependentModules(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3348 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3349 | if (num_modules > 0) { |
| 3350 | s->PutCString("Dependent Modules:\n"); |
| 3351 | for (unsigned i = 0; i < num_modules; ++i) { |
| 3352 | const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); |
| 3353 | s->Printf(" %s\n", spec.GetFilename().GetCString()); |
| 3354 | } |
| 3355 | } |
| 3356 | } |
| 3357 | |
| 3358 | bool ObjectFileELF::GetArchitecture(ArchSpec &arch) { |
| 3359 | if (!ParseHeader()) |
| 3360 | return false; |
| 3361 | |
| 3362 | if (m_section_headers.empty()) { |
| 3363 | // Allow elf notes to be parsed which may affect the detected architecture. |
| 3364 | ParseSectionHeaders(); |
| 3365 | } |
| 3366 | |
| 3367 | if (CalculateType() == eTypeCoreFile && |
| 3368 | m_arch_spec.TripleOSIsUnspecifiedUnknown()) { |
| 3369 | // Core files don't have section headers yet they have PT_NOTE program |
| 3370 | // headers |
| 3371 | // that might shed more light on the architecture |
| 3372 | if (ParseProgramHeaders()) { |
Kamil Rytarowski | 12801f1 | 2017-03-26 15:34:57 +0000 | [diff] [blame] | 3373 | for (size_t i = 1, count = GetProgramHeaderCount(); i <= count; ++i) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3374 | const elf::ELFProgramHeader *header = GetProgramHeaderByIndex(i); |
| 3375 | if (header && header->p_type == PT_NOTE && header->p_offset != 0 && |
| 3376 | header->p_filesz > 0) { |
| 3377 | DataExtractor data; |
| 3378 | if (data.SetData(m_data, header->p_offset, header->p_filesz) == |
| 3379 | header->p_filesz) { |
| 3380 | lldb_private::UUID uuid; |
| 3381 | RefineModuleDetailsFromNote(data, m_arch_spec, uuid); |
| 3382 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3383 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3384 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3385 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3386 | } |
| 3387 | arch = m_arch_spec; |
| 3388 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3391 | ObjectFile::Type ObjectFileELF::CalculateType() { |
| 3392 | switch (m_header.e_type) { |
| 3393 | case llvm::ELF::ET_NONE: |
| 3394 | // 0 - No file type |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 3395 | return eTypeUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3396 | |
| 3397 | case llvm::ELF::ET_REL: |
| 3398 | // 1 - Relocatable file |
| 3399 | return eTypeObjectFile; |
| 3400 | |
| 3401 | case llvm::ELF::ET_EXEC: |
| 3402 | // 2 - Executable file |
| 3403 | return eTypeExecutable; |
| 3404 | |
| 3405 | case llvm::ELF::ET_DYN: |
| 3406 | // 3 - Shared object file |
| 3407 | return eTypeSharedLibrary; |
| 3408 | |
| 3409 | case ET_CORE: |
| 3410 | // 4 - Core file |
| 3411 | return eTypeCoreFile; |
| 3412 | |
| 3413 | default: |
| 3414 | break; |
| 3415 | } |
| 3416 | return eTypeUnknown; |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3419 | ObjectFile::Strata ObjectFileELF::CalculateStrata() { |
| 3420 | switch (m_header.e_type) { |
| 3421 | case llvm::ELF::ET_NONE: |
| 3422 | // 0 - No file type |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 3423 | return eStrataUnknown; |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 3424 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3425 | case llvm::ELF::ET_REL: |
| 3426 | // 1 - Relocatable file |
| 3427 | return eStrataUnknown; |
| 3428 | |
| 3429 | case llvm::ELF::ET_EXEC: |
| 3430 | // 2 - Executable file |
| 3431 | // TODO: is there any way to detect that an executable is a kernel |
| 3432 | // related executable by inspecting the program headers, section |
| 3433 | // headers, symbols, or any other flag bits??? |
| 3434 | return eStrataUser; |
| 3435 | |
| 3436 | case llvm::ELF::ET_DYN: |
| 3437 | // 3 - Shared object file |
| 3438 | // TODO: is there any way to detect that an shared library is a kernel |
| 3439 | // related executable by inspecting the program headers, section |
| 3440 | // headers, symbols, or any other flag bits??? |
| 3441 | return eStrataUnknown; |
| 3442 | |
| 3443 | case ET_CORE: |
| 3444 | // 4 - Core file |
| 3445 | // TODO: is there any way to detect that an core file is a kernel |
| 3446 | // related executable by inspecting the program headers, section |
| 3447 | // headers, symbols, or any other flag bits??? |
| 3448 | return eStrataUnknown; |
| 3449 | |
| 3450 | default: |
| 3451 | break; |
| 3452 | } |
| 3453 | return eStrataUnknown; |
| 3454 | } |